]> gcc.gnu.org Git - gcc.git/blame - gcc/ada/gcc-interface/trans.cc
Fix small regression in Ada
[gcc.git] / gcc / ada / gcc-interface / trans.cc
CommitLineData
a1ab4c31
AC
1/****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
b2263428 9 * Copyright (C) 1992-2023, Free Software Foundation, Inc. *
a1ab4c31
AC
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
748086b7 13 * ware Foundation; either version 3, or (at your option) any later ver- *
a1ab4c31
AC
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
748086b7
JJ
18 * Public License distributed with GNAT; see file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
a1ab4c31
AC
20 * *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
23 * *
24 ****************************************************************************/
25
26#include "config.h"
27#include "system.h"
28#include "coretypes.h"
2adfab87
AM
29#include "target.h"
30#include "function.h"
31#include "bitmap.h"
a1ab4c31 32#include "tree.h"
2adfab87 33#include "gimple-expr.h"
d8a2d370 34#include "stringpool.h"
2adfab87 35#include "cgraph.h"
64235766 36#include "predict.h"
2adfab87
AM
37#include "diagnostic.h"
38#include "alias.h"
39#include "fold-const.h"
d8a2d370
DN
40#include "stor-layout.h"
41#include "stmt.h"
42#include "varasm.h"
a1ab4c31 43#include "output.h"
fa6fd7b7 44#include "debug.h"
d477d1fe 45#include "libfuncs.h" /* For set_stack_check_libfunc. */
a1ab4c31 46#include "tree-iterator.h"
2fb9a547 47#include "gimplify.h"
f2423384 48#include "opts.h"
48a24fcf 49#include "common/common-target.h"
7beb2b9d 50#include "gomp-constants.h"
314e6352
ML
51#include "stringpool.h"
52#include "attribs.h"
89576d86 53#include "tree-nested.h"
8713b7e4 54
a1ab4c31 55#include "ada.h"
8713b7e4 56#include "adadecode.h"
a1ab4c31
AC
57#include "types.h"
58#include "atree.h"
a1ab4c31
AC
59#include "namet.h"
60#include "nlists.h"
61#include "snames.h"
62#include "stringt.h"
63#include "uintp.h"
64#include "urealp.h"
65#include "fe.h"
66#include "sinfo.h"
67#include "einfo.h"
831f44c6 68#include "gadaint.h"
a1ab4c31
AC
69#include "ada-tree.h"
70#include "gigi.h"
a1ab4c31
AC
71
72/* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
73 for fear of running out of stack space. If we need more, we use xmalloc
74 instead. */
75#define ALLOCA_THRESHOLD 1000
76
831f44c6 77/* Pointers to front-end tables accessed through macros. */
99e30ba8 78Node_Header *Node_Offsets_Ptr;
a6d3b499 79any_slot *Slots_Ptr;
a1ab4c31
AC
80Node_Id *Next_Node_Ptr;
81Node_Id *Prev_Node_Ptr;
82struct Elist_Header *Elists_Ptr;
83struct Elmt_Item *Elmts_Ptr;
84struct String_Entry *Strings_Ptr;
85Char_Code *String_Chars_Ptr;
86struct List_Header *List_Headers_Ptr;
87
831f44c6
EB
88/* Highest number in the front-end node table. */
89int max_gnat_nodes;
90
1e17ef87 91/* True when gigi is being called on an analyzed but unexpanded
a1ab4c31 92 tree, and the only purpose of the call is to properly annotate
1e17ef87 93 types with representation information. */
a1ab4c31
AC
94bool type_annotate_only;
95
f04b8d69 96/* List of N_Validate_Unchecked_Conversion nodes in the unit. */
9771b263 97static vec<Node_Id> gnat_validate_uc_list;
f04b8d69 98
ec4a0d83
EB
99/* List of expressions of pragma Compile_Time_{Error|Warning} in the unit. */
100static vec<Node_Id> gnat_compile_time_expr_list;
101
a1ab4c31
AC
102/* When not optimizing, we cache the 'First, 'Last and 'Length attributes
103 of unconstrained array IN parameters to avoid emitting a great deal of
104 redundant instructions to recompute them each time. */
6bf68a93 105struct GTY (()) parm_attr_d {
a1ab4c31
AC
106 int id; /* GTY doesn't like Entity_Id. */
107 int dim;
108 tree first;
109 tree last;
110 tree length;
111};
112
6bf68a93 113typedef struct parm_attr_d *parm_attr;
a1ab4c31 114
852dd866 115/* Structure used to record information for a function. */
d1b38208 116struct GTY(()) language_function {
9771b263 117 vec<parm_attr, va_gc> *parm_attr_cache;
71196d4e 118 bitmap named_ret_val;
9771b263 119 vec<tree, va_gc> *other_ret_val;
088b91c7 120 int gnat_ret;
a1ab4c31
AC
121};
122
123#define f_parm_attr_cache \
124 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
125
71196d4e
EB
126#define f_named_ret_val \
127 DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val
128
129#define f_other_ret_val \
130 DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val
131
088b91c7
EB
132#define f_gnat_ret \
133 DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret
134
a1ab4c31
AC
135/* A structure used to gather together information about a statement group.
136 We use this to gather related statements, for example the "then" part
137 of a IF. In the case where it represents a lexical scope, we may also
138 have a BLOCK node corresponding to it and/or cleanups. */
139
d1b38208 140struct GTY((chain_next ("%h.previous"))) stmt_group {
a1ab4c31 141 struct stmt_group *previous; /* Previous code group. */
1e17ef87
EB
142 tree stmt_list; /* List of statements for this code group. */
143 tree block; /* BLOCK for this code group, if any. */
a1ab4c31
AC
144 tree cleanups; /* Cleanups for this code group, if any. */
145};
146
147static GTY(()) struct stmt_group *current_stmt_group;
148
149/* List of unused struct stmt_group nodes. */
150static GTY((deletable)) struct stmt_group *stmt_group_free_list;
151
152/* A structure used to record information on elaboration procedures
153 we've made and need to process.
154
155 ??? gnat_node should be Node_Id, but gengtype gets confused. */
156
d1b38208 157struct GTY((chain_next ("%h.next"))) elab_info {
1e17ef87 158 struct elab_info *next; /* Pointer to next in chain. */
a1ab4c31
AC
159 tree elab_proc; /* Elaboration procedure. */
160 int gnat_node; /* The N_Compilation_Unit. */
161};
162
163static GTY(()) struct elab_info *elab_info_list;
164
39f579c7
NF
165/* Stack of exception pointer variables. Each entry is the VAR_DECL
166 that stores the address of the raised exception. Nonzero means we
167 are in an exception handler. Not used in the zero-cost case. */
9771b263 168static GTY(()) vec<tree, va_gc> *gnu_except_ptr_stack;
a1ab4c31 169
624e1688
AC
170/* In ZCX case, current exception pointer. Used to re-raise it. */
171static GTY(()) tree gnu_incoming_exc_ptr;
172
39f579c7 173/* Stack for storing the current elaboration procedure decl. */
9771b263 174static GTY(()) vec<tree, va_gc> *gnu_elab_proc_stack;
a1ab4c31 175
39f579c7
NF
176/* Stack of labels to be used as a goto target instead of a return in
177 some functions. See processing for N_Subprogram_Body. */
9771b263 178static GTY(()) vec<tree, va_gc> *gnu_return_label_stack;
a1ab4c31 179
35a382b8
EB
180/* Stack of variable for the return value of a function with copy-in/copy-out
181 parameters. See processing for N_Subprogram_Body. */
9771b263 182static GTY(()) vec<tree, va_gc> *gnu_return_var_stack;
35a382b8 183
15bf7d19
EB
184/* Structure used to record information for a range check. */
185struct GTY(()) range_check_info_d {
186 tree low_bound;
187 tree high_bound;
933a7325
EB
188 tree disp;
189 bool neg_p;
15bf7d19
EB
190 tree type;
191 tree invariant_cond;
64235766 192 tree inserted_cond;
15bf7d19
EB
193};
194
195typedef struct range_check_info_d *range_check_info;
196
15bf7d19
EB
197/* Structure used to record information for a loop. */
198struct GTY(()) loop_info_d {
852dd866 199 tree fndecl;
633a3f2a 200 tree stmt;
15bf7d19 201 tree loop_var;
87ab2b04
EB
202 tree low_bound;
203 tree high_bound;
7beb2b9d
OH
204 tree omp_loop_clauses;
205 tree omp_construct_clauses;
206 enum tree_code omp_code;
9771b263 207 vec<range_check_info, va_gc> *checks;
852dd866 208 vec<tree, va_gc> *invariants;
15bf7d19
EB
209};
210
211typedef struct loop_info_d *loop_info;
212
15bf7d19 213/* Stack of loop_info structures associated with LOOP_STMT nodes. */
9771b263 214static GTY(()) vec<loop_info, va_gc> *gnu_loop_stack;
a1ab4c31 215
39f579c7 216/* The stacks for N_{Push,Pop}_*_Label. */
8f8f531f
PMR
217static vec<Entity_Id> gnu_constraint_error_label_stack;
218static vec<Entity_Id> gnu_storage_error_label_stack;
219static vec<Entity_Id> gnu_program_error_label_stack;
a1ab4c31
AC
220
221/* Map GNAT tree codes to GCC tree codes for simple expressions. */
222static enum tree_code gnu_codes[Number_Node_Kinds];
223
a1ab4c31 224static void init_code_table (void);
7543c8b6 225static tree get_elaboration_procedure (void);
a1ab4c31 226static void Compilation_Unit_to_gnu (Node_Id);
102a1631 227static bool empty_stmt_list_p (tree);
a1ab4c31
AC
228static void record_code_position (Node_Id);
229static void insert_code_for (Node_Id);
230static void add_cleanup (tree, Node_Id);
a1ab4c31 231static void add_stmt_list (List_Id);
a1ab4c31 232static tree build_stmt_group (List_Id, bool);
a712b009 233static inline bool stmt_group_may_fallthru (void);
a1ab4c31
AC
234static enum gimplify_status gnat_gimplify_stmt (tree *);
235static void elaborate_all_entities (Node_Id);
236static void process_freeze_entity (Node_Id);
6d16658d 237static void process_decls (List_Id, List_Id, bool, bool);
10069d53
EB
238static tree emit_check (tree, tree, int, Node_Id);
239static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id);
240static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id);
815b5368 241static tree convert_with_check (Entity_Id, tree, bool, bool, Node_Id);
a1ab4c31
AC
242static bool addressable_p (tree, tree);
243static tree assoc_to_constructor (Entity_Id, Node_Id, tree);
815b5368 244static tree pos_to_constructor (Node_Id, tree);
f04b8d69 245static void validate_unchecked_conversion (Node_Id);
ba464315 246static void set_expr_location_from_node (tree, Node_Id, bool = false);
17c168fe 247static void set_gnu_expr_location_from_node (tree, Node_Id);
ba464315 248static bool set_end_locus_from_node (tree, Node_Id);
86da71db 249static int lvalue_required_p (Node_Id, tree, bool, bool);
c1fd8753 250static tree build_raise_check (int, enum exception_info_kind);
6162cec0 251static tree create_init_temporary (const char *, tree, tree *, Node_Id);
44662f68 252static bool maybe_make_gnu_thunk (Entity_Id gnat_thunk, tree gnu_thunk);
a1ab4c31 253
fa6fd7b7
AO
254/* This makes gigi's file_info_ptr visible in this translation unit,
255 so that Sloc_to_locus can look it up when deciding whether to map
256 decls to instances. */
257
258static struct File_Info_Type *file_map;
259
39fa0de5
EB
260/* Return the string of the identifier allocated for the file name Id. */
261
262static const char*
263File_Name_to_gnu (Name_Id Id)
264{
265 /* __gnat_to_canonical_file_spec translates file names from pragmas
266 Source_Reference that contain host style syntax not understood by GDB. */
267 const char *name = __gnat_to_canonical_file_spec (Get_Name_String (Id));
268
269 /* Use the identifier table to make a permanent copy of the file name as
270 the name table gets reallocated after Gigi returns but before all the
271 debugging information is output. */
272 return IDENTIFIER_POINTER (get_identifier (name));
273}
274
a1ab4c31
AC
275/* This is the main program of the back-end. It sets up all the table
276 structures and then generates code. */
277
278void
4bcf6815
AC
279gigi (Node_Id gnat_root,
280 int max_gnat_node,
281 int number_name ATTRIBUTE_UNUSED,
99e30ba8 282 Node_Header *node_offsets_ptr,
a6d3b499 283 any_slot *slots_ptr,
4bcf6815
AC
284 Node_Id *next_node_ptr,
285 Node_Id *prev_node_ptr,
286 struct Elist_Header *elists_ptr,
287 struct Elmt_Item *elmts_ptr,
288 struct String_Entry *strings_ptr,
289 Char_Code *string_chars_ptr,
290 struct List_Header *list_headers_ptr,
291 Nat number_file,
6936c61a 292 struct File_Info_Type *file_info_ptr,
4bcf6815
AC
293 Entity_Id standard_boolean,
294 Entity_Id standard_integer,
295 Entity_Id standard_character,
296 Entity_Id standard_long_long_float,
297 Entity_Id standard_exception_type,
298 Int gigi_operating_mode)
a1ab4c31 299{
f04b8d69 300 Node_Id gnat_iter;
01ddebf2 301 Entity_Id gnat_literal;
1eb58520 302 tree t, ftype, int64_type;
a1ab4c31
AC
303 struct elab_info *info;
304 int i;
305
306 max_gnat_nodes = max_gnat_node;
831f44c6 307
76f9c7f4
BD
308 Node_Offsets_Ptr = node_offsets_ptr;
309 Slots_Ptr = slots_ptr;
a1ab4c31
AC
310 Next_Node_Ptr = next_node_ptr;
311 Prev_Node_Ptr = prev_node_ptr;
312 Elists_Ptr = elists_ptr;
313 Elmts_Ptr = elmts_ptr;
314 Strings_Ptr = strings_ptr;
315 String_Chars_Ptr = string_chars_ptr;
316 List_Headers_Ptr = list_headers_ptr;
317
318 type_annotate_only = (gigi_operating_mode == 1);
319
fa6fd7b7
AO
320 if (Generate_SCO_Instance_Table != 0)
321 {
322 file_map = file_info_ptr;
323 maybe_create_decl_to_instance_map (number_file);
324 }
325
831f44c6 326 for (i = 0; i < number_file; i++)
a1ab4c31 327 {
a1ab4c31 328 /* We rely on the order isomorphism between files and line maps. */
39fa0de5
EB
329 if ((int) LINEMAPS_ORDINARY_USED (line_table) != i)
330 {
331 gcc_assert (i > 0);
332 error ("%s contains too many lines",
333 File_Name_to_gnu (file_info_ptr[i - 1].File_Name));
334 }
a1ab4c31
AC
335
336 /* We create the line map for a source file at once, with a fixed number
337 of columns chosen to avoid jumping over the next power of 2. */
39fa0de5
EB
338 linemap_add (line_table, LC_ENTER, 0,
339 File_Name_to_gnu (file_info_ptr[i].File_Name), 1);
a1ab4c31
AC
340 linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252);
341 linemap_position_for_column (line_table, 252 - 1);
342 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
343 }
344
52e04e56
EB
345 gcc_assert (Nkind (gnat_root) == N_Compilation_Unit);
346
347 /* Declare the name of the compilation unit as the first global
348 name in order to make the middle-end fully deterministic. */
349 t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL);
350 first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t));
351
a1ab4c31
AC
352 /* Initialize ourselves. */
353 init_code_table ();
4116e7d0 354 init_gnat_decl ();
842d4ee2 355 init_gnat_utils ();
a1ab4c31
AC
356
357 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
358 errors. */
359 if (type_annotate_only)
360 {
361 TYPE_SIZE (void_type_node) = bitsize_zero_node;
362 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
363 }
364
a1ab4c31
AC
365 /* Enable GNAT stack checking method if needed */
366 if (!Stack_Check_Probes_On_Target)
1f3f64b9
EB
367 {
368 set_stack_check_libfunc ("__gnat_stack_check");
369 if (flag_stack_check != NO_STACK_CHECK)
370 Check_Restriction_No_Dependence_On_System (Name_Stack_Checking,
371 gnat_root);
372 }
a1ab4c31 373
caa9d12a
EB
374 /* Retrieve alignment settings. */
375 double_float_alignment = get_target_double_float_alignment ();
376 double_scalar_alignment = get_target_double_scalar_alignment ();
377
6936c61a
EB
378 /* Record the builtin types. Define `integer' and `character' first so that
379 dbx will output them first. */
1aeb40dd 380 record_builtin_type ("integer", integer_type_node, false);
825da0d2 381 record_builtin_type ("character", char_type_node, false);
1aeb40dd
EB
382 record_builtin_type ("boolean", boolean_type_node, false);
383 record_builtin_type ("void", void_type_node, false);
10069d53
EB
384
385 /* Save the type we made for integer as the type for Standard.Integer. */
6936c61a
EB
386 save_gnu_tree (Base_Type (standard_integer),
387 TYPE_NAME (integer_type_node),
10069d53 388 false);
a1ab4c31 389
6936c61a 390 /* Likewise for character as the type for Standard.Character. */
825da0d2 391 finish_character_type (char_type_node);
6936c61a 392 save_gnu_tree (Base_Type (standard_character),
825da0d2 393 TYPE_NAME (char_type_node),
6936c61a
EB
394 false);
395
396 /* Likewise for boolean as the type for Standard.Boolean. */
397 save_gnu_tree (Base_Type (standard_boolean),
398 TYPE_NAME (boolean_type_node),
01ddebf2
EB
399 false);
400 gnat_literal = First_Literal (Base_Type (standard_boolean));
401 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
402 gcc_assert (t == boolean_false_node);
403 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
2056c5ed 404 boolean_type_node, t, true, false, false, false, false,
c1a569ef 405 true, false, NULL, gnat_literal);
01ddebf2
EB
406 save_gnu_tree (gnat_literal, t, false);
407 gnat_literal = Next_Literal (gnat_literal);
408 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
409 gcc_assert (t == boolean_true_node);
410 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
2056c5ed 411 boolean_type_node, t, true, false, false, false, false,
c1a569ef 412 true, false, NULL, gnat_literal);
01ddebf2
EB
413 save_gnu_tree (gnat_literal, t, false);
414
9182f718 415 /* Declare the building blocks of function nodes. */
c1fd8753 416 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
10069d53
EB
417 ptr_void_ftype = build_pointer_type (void_ftype);
418
c01fe451 419 /* Now declare run-time functions. */
10069d53
EB
420 malloc_decl
421 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE,
ab3dd4ab
EB
422 build_function_type_list (ptr_type_node, sizetype,
423 NULL_TREE),
13a6dfe3 424 NULL_TREE, is_default, true, true, true, false,
ff9baa5f 425 false, NULL, Empty);
10069d53
EB
426 DECL_IS_MALLOC (malloc_decl) = 1;
427
10069d53
EB
428 free_decl
429 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
ab3dd4ab
EB
430 build_function_type_list (void_type_node,
431 ptr_type_node, NULL_TREE),
13a6dfe3 432 NULL_TREE, is_default, true, true, true, false,
ff9baa5f 433 false, NULL, Empty);
9182f718 434
9182f718
EB
435 realloc_decl
436 = create_subprog_decl (get_identifier ("__gnat_realloc"), NULL_TREE,
ab3dd4ab
EB
437 build_function_type_list (ptr_type_node,
438 ptr_type_node, sizetype,
439 NULL_TREE),
13a6dfe3 440 NULL_TREE, is_default, true, true, true, false,
ff9baa5f 441 false, NULL, Empty);
10069d53
EB
442
443 /* This is used for 64-bit multiplication with overflow checking. */
1eb58520 444 int64_type = gnat_type_for_size (64, 0);
10069d53
EB
445 mulv64_decl
446 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE,
447 build_function_type_list (int64_type, int64_type,
448 int64_type, NULL_TREE),
13a6dfe3 449 NULL_TREE, is_default, true, true, true, false,
ff9baa5f 450 false, NULL, Empty);
10069d53 451
f2d9f95e
EB
452 if (Enable_128bit_Types)
453 {
454 tree int128_type = gnat_type_for_size (128, 0);
455 mulv128_decl
456 = create_subprog_decl (get_identifier ("__gnat_mulv128"), NULL_TREE,
457 build_function_type_list (int128_type,
458 int128_type,
459 int128_type,
460 NULL_TREE),
461 NULL_TREE, is_default, true, true, true, false,
462 false, NULL, Empty);
463 }
464
76af763d
EB
465 /* Name of the _Parent field in tagged record types. */
466 parent_name_id = get_identifier (Get_Name_String (Name_uParent));
467
17ba0ad5
EB
468 /* Name of the Not_Handled_By_Others field in exception record types. */
469 not_handled_by_others_name_id = get_identifier ("not_handled_by_others");
871fda0a 470
10069d53 471 /* Make the types and functions used for exception processing. */
64235766
EB
472 except_type_node = gnat_to_gnu_type (Base_Type (standard_exception_type));
473
64235766
EB
474 set_exception_parameter_decl
475 = create_subprog_decl
476 (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE,
477 build_function_type_list (void_type_node, ptr_type_node, ptr_type_node,
478 NULL_TREE),
13a6dfe3 479 NULL_TREE, is_default, true, true, true, false, false, NULL, Empty);
64235766 480
10069d53 481 /* Hooks to call when entering/leaving an exception handler. */
5d733372
AO
482 ftype = build_function_type_list (ptr_type_node,
483 ptr_type_node, NULL_TREE);
10069d53 484 begin_handler_decl
5d733372
AO
485 = create_subprog_decl (get_identifier ("__gnat_begin_handler_v1"),
486 NULL_TREE, ftype, NULL_TREE,
13a6dfe3 487 is_default, true, true, true, false, false, NULL,
ff9baa5f 488 Empty);
5d733372
AO
489 /* __gnat_begin_handler_v1 is not a dummy procedure, but we arrange
490 for it not to throw. */
4ec7c4ec 491 TREE_NOTHROW (begin_handler_decl) = 1;
10069d53 492
5d733372
AO
493 ftype = build_function_type_list (ptr_type_node,
494 ptr_type_node, ptr_type_node,
495 ptr_type_node, NULL_TREE);
10069d53 496 end_handler_decl
5d733372 497 = create_subprog_decl (get_identifier ("__gnat_end_handler_v1"), NULL_TREE,
c1a569ef 498 ftype, NULL_TREE,
13a6dfe3 499 is_default, true, true, true, false, false, NULL,
ff9baa5f 500 Empty);
10069d53 501
5d733372 502 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
48a24fcf
TG
503 unhandled_except_decl
504 = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
c1a569ef 505 NULL_TREE, ftype, NULL_TREE,
13a6dfe3 506 is_default, true, true, true, false, false, NULL,
ff9baa5f 507 Empty);
1e55d29a
EB
508
509 /* Indicate that it never returns. */
510 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
511 reraise_zcx_decl
512 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE,
513 ftype, NULL_TREE,
13a6dfe3 514 is_default, true, true, true, false, false, NULL,
ff9baa5f 515 Empty);
48a24fcf 516
64235766
EB
517 /* Dummy objects to materialize "others" and "all others" in the exception
518 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
519 the types to use. */
520 others_decl
521 = create_var_decl (get_identifier ("OTHERS"),
522 get_identifier ("__gnat_others_value"),
825da0d2 523 char_type_node, NULL_TREE,
2056c5ed 524 true, false, true, false, false, true, false,
64235766
EB
525 NULL, Empty);
526
527 all_others_decl
528 = create_var_decl (get_identifier ("ALL_OTHERS"),
529 get_identifier ("__gnat_all_others_value"),
825da0d2 530 char_type_node, NULL_TREE,
2056c5ed 531 true, false, true, false, false, true, false,
64235766
EB
532 NULL, Empty);
533
534 unhandled_others_decl
535 = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
536 get_identifier ("__gnat_unhandled_others_value"),
825da0d2 537 char_type_node, NULL_TREE,
2056c5ed 538 true, false, true, false, false, true, false,
64235766 539 NULL, Empty);
624e1688 540
10069d53 541 /* If in no exception handlers mode, all raise statements are redirected to
d6d9f534 542 __gnat_last_chance_handler. */
10069d53
EB
543 if (No_Exception_Handlers_Set ())
544 {
1e55d29a
EB
545 /* Indicate that it never returns. */
546 ftype = build_function_type_list (void_type_node,
547 build_pointer_type (char_type_node),
548 integer_type_node, NULL_TREE);
549 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
10069d53
EB
550 tree decl
551 = create_subprog_decl
1e55d29a 552 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE, ftype,
13a6dfe3 553 NULL_TREE, is_default, true, true, true, false, false, NULL,
ff9baa5f 554 Empty);
cfc839a4
EB
555 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
556 gnat_raise_decls[i] = decl;
10069d53
EB
557 }
558 else
10069d53 559 {
437f8c1e
AC
560 /* Otherwise, make one decl for each exception reason. */
561 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
c1fd8753 562 gnat_raise_decls[i] = build_raise_check (i, exception_simple);
437f8c1e
AC
563 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++)
564 gnat_raise_decls_ext[i]
c1fd8753 565 = build_raise_check (i,
437f8c1e 566 i == CE_Index_Check_Failed
ea034236
AC
567 || i == CE_Range_Check_Failed
568 || i == CE_Invalid_Data
569 ? exception_range : exception_column);
10069d53
EB
570 }
571
10069d53
EB
572 /* Build the special descriptor type and its null node if needed. */
573 if (TARGET_VTABLE_USES_DESCRIPTORS)
574 {
575 tree null_node = fold_convert (ptr_void_ftype, null_pointer_node);
0e228dd9 576 tree field_list = NULL_TREE;
10069d53 577 int j;
9771b263 578 vec<constructor_elt, va_gc> *null_vec = NULL;
0e228dd9 579 constructor_elt *elt;
10069d53
EB
580
581 fdesc_type_node = make_node (RECORD_TYPE);
cb3874dc 582 vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS, true);
9771b263 583 elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
10069d53
EB
584
585 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
586 {
da01bfee
EB
587 tree field
588 = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node,
589 NULL_TREE, NULL_TREE, 0, 1);
7d76717d 590 DECL_CHAIN (field) = field_list;
10069d53 591 field_list = field;
0e228dd9
NF
592 elt->index = field;
593 elt->value = null_node;
594 elt--;
10069d53
EB
595 }
596
032d1b71 597 finish_record_type (fdesc_type_node, nreverse (field_list), 0, false);
1aeb40dd 598 record_builtin_type ("descriptor", fdesc_type_node, true);
0e228dd9 599 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec);
10069d53
EB
600 }
601
1eb58520
AC
602 longest_float_type_node
603 = get_unpadded_type (Base_Type (standard_long_long_float));
f7ebc6a8 604
10069d53
EB
605 main_identifier_node = get_identifier ("main");
606
ce19ac12 607 gnat_init_gcc_eh ();
c8a23c29
EB
608
609 /* Initialize the GCC support for FP operations. */
610 gnat_init_gcc_fp ();
611
612 /* Install the builtins we might need, either internally or as user-available
613 facilities for Intrinsic imports. Note that this must be done after the
614 GCC exception mechanism is initialized. */
10069d53 615 gnat_install_builtins ();
a1ab4c31 616
9771b263 617 vec_safe_push (gnu_except_ptr_stack, NULL_TREE);
8f8f531f
PMR
618
619 gnu_constraint_error_label_stack.safe_push (Empty);
620 gnu_storage_error_label_stack.safe_push (Empty);
621 gnu_program_error_label_stack.safe_push (Empty);
a1ab4c31 622
a1ab4c31 623 /* Process any Pragma Ident for the main unit. */
a1ab4c31 624 if (Present (Ident_String (Main_Unit)))
a8781821
SB
625 targetm.asm_out.output_ident
626 (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
a1ab4c31 627
c3831524
AC
628 /* Force -fno-strict-aliasing if the configuration pragma was seen. */
629 if (No_Strict_Aliasing_CP)
630 flag_strict_aliasing = 0;
631
c0c91386
JJ
632 /* Save the current optimization options again after the above possible
633 global_options changes. */
ba948b37
JJ
634 optimization_default_node
635 = build_optimization_node (&global_options, &global_options_set);
c0c91386
JJ
636 optimization_current_node = optimization_default_node;
637
6a7a3f31 638 /* Now translate the compilation unit proper. */
a1ab4c31
AC
639 Compilation_Unit_to_gnu (gnat_root);
640
f04b8d69
EB
641 /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at
642 the very end to avoid having to second-guess the front-end when we run
643 into dummy nodes during the regular processing. */
9771b263 644 for (i = 0; gnat_validate_uc_list.iterate (i, &gnat_iter); i++)
f04b8d69 645 validate_unchecked_conversion (gnat_iter);
9771b263 646 gnat_validate_uc_list.release ();
f04b8d69 647
6a7a3f31 648 /* Finally see if we have any elaboration procedures to deal with. */
a1ab4c31
AC
649 for (info = elab_info_list; info; info = info->next)
650 {
102a1631 651 tree gnu_body = DECL_SAVED_TREE (info->elab_proc);
a1ab4c31 652
2fa03086
EB
653 /* We should have a BIND_EXPR but it may not have any statements in it.
654 If it doesn't have any, we have nothing to do except for setting the
655 flag on the GNAT node. Otherwise, process the function as others. */
102a1631 656 tree gnu_stmts = gnu_body;
a406865a
RG
657 if (TREE_CODE (gnu_stmts) == BIND_EXPR)
658 gnu_stmts = BIND_EXPR_BODY (gnu_stmts);
102a1631 659 if (!gnu_stmts || empty_stmt_list_p (gnu_stmts))
2fa03086 660 Set_Has_No_Elaboration_Code (info->gnat_node, 1);
a406865a
RG
661 else
662 {
a406865a
RG
663 begin_subprog_body (info->elab_proc);
664 end_subprog_body (gnu_body);
71196d4e 665 rest_of_subprog_body_compilation (info->elab_proc);
a406865a 666 }
a1ab4c31
AC
667 }
668
f04b8d69 669 /* Destroy ourselves. */
fa6fd7b7 670 file_map = NULL;
4116e7d0 671 destroy_gnat_decl ();
842d4ee2 672 destroy_gnat_utils ();
f04b8d69 673
a1ab4c31 674 /* We cannot track the location of errors past this point. */
738b83cd 675 Current_Error_Node = Empty;
a1ab4c31 676}
ce2d0ce2 677
437f8c1e 678/* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given
c1fd8753 679 CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */
437f8c1e
AC
680
681static tree
c1fd8753 682build_raise_check (int check, enum exception_info_kind kind)
437f8c1e 683{
c1fd8753 684 tree result, ftype;
0c644c99
TG
685 const char pfx[] = "__gnat_rcheck_";
686
687 strcpy (Name_Buffer, pfx);
688 Name_Len = sizeof (pfx) - 1;
a6d3b499 689 Get_RT_Exception_Name ((enum RT_Exception_Code) check);
437f8c1e 690
c1fd8753 691 if (kind == exception_simple)
437f8c1e 692 {
0c644c99 693 Name_Buffer[Name_Len] = 0;
c1fd8753
NF
694 ftype
695 = build_function_type_list (void_type_node,
825da0d2 696 build_pointer_type (char_type_node),
c1fd8753 697 integer_type_node, NULL_TREE);
437f8c1e
AC
698 }
699 else
700 {
c1fd8753 701 tree t = (kind == exception_column ? NULL_TREE : integer_type_node);
0c644c99
TG
702
703 strcpy (Name_Buffer + Name_Len, "_ext");
704 Name_Buffer[Name_Len + 4] = 0;
c1fd8753
NF
705 ftype
706 = build_function_type_list (void_type_node,
825da0d2 707 build_pointer_type (char_type_node),
c1fd8753
NF
708 integer_type_node, integer_type_node,
709 t, t, NULL_TREE);
437f8c1e 710 }
cfc839a4 711
2056c5ed 712 /* Indicate that it never returns. */
1e55d29a 713 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
c1fd8753 714 result
1e55d29a 715 = create_subprog_decl (get_identifier (Name_Buffer), NULL_TREE, ftype,
13a6dfe3 716 NULL_TREE, is_default, true, true, true, false,
ff9baa5f 717 false, NULL, Empty);
c1fd8753 718
437f8c1e
AC
719 return result;
720}
ce2d0ce2 721
3cd64bab
EB
722/* Return a positive value if an lvalue is required for GNAT_NODE, which is
723 an N_Attribute_Reference. */
724
725static int
726lvalue_required_for_attribute_p (Node_Id gnat_node)
727{
728 switch (Get_Attribute_Id (Attribute_Name (gnat_node)))
729 {
3cd64bab
EB
730 case Attr_Pred:
731 case Attr_Succ:
732 case Attr_First:
733 case Attr_Last:
734 case Attr_Range_Length:
735 case Attr_Length:
736 case Attr_Object_Size:
79069232 737 case Attr_Size:
3cd64bab
EB
738 case Attr_Value_Size:
739 case Attr_Component_Size:
24228312 740 case Attr_Descriptor_Size:
3cd64bab
EB
741 case Attr_Max_Size_In_Storage_Elements:
742 case Attr_Min:
743 case Attr_Max:
744 case Attr_Null_Parameter:
745 case Attr_Passed_By_Reference:
746 case Attr_Mechanism_Code:
24228312
AC
747 case Attr_Machine:
748 case Attr_Model:
3cd64bab
EB
749 return 0;
750
751 case Attr_Address:
752 case Attr_Access:
753 case Attr_Unchecked_Access:
754 case Attr_Unrestricted_Access:
755 case Attr_Code_Address:
756 case Attr_Pool_Address:
3cd64bab
EB
757 case Attr_Alignment:
758 case Attr_Bit_Position:
759 case Attr_Position:
760 case Attr_First_Bit:
761 case Attr_Last_Bit:
762 case Attr_Bit:
7e4680c1
EB
763 case Attr_Asm_Input:
764 case Attr_Asm_Output:
3cd64bab
EB
765 default:
766 return 1;
767 }
768}
769
22d12fc2
EB
770/* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE
771 is the type that will be used for GNAT_NODE in the translated GNU tree.
772 CONSTANT indicates whether the underlying object represented by GNAT_NODE
cb3d597d 773 is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates
86da71db
EB
774 whether its value is the address of another constant. If it isn't, then
775 ADDRESS_OF_CONSTANT is ignored.
22d12fc2
EB
776
777 The function climbs up the GNAT tree starting from the node and returns 1
778 upon encountering a node that effectively requires an lvalue downstream.
779 It returns int instead of bool to facilitate usage in non-purely binary
780 logic contexts. */
a1ab4c31
AC
781
782static int
03b6f8a2 783lvalue_required_p (Node_Id gnat_node, tree gnu_type, bool constant,
86da71db 784 bool address_of_constant)
a1ab4c31
AC
785{
786 Node_Id gnat_parent = Parent (gnat_node), gnat_temp;
787
788 switch (Nkind (gnat_parent))
789 {
790 case N_Reference:
791 return 1;
792
793 case N_Attribute_Reference:
3cd64bab 794 return lvalue_required_for_attribute_p (gnat_parent);
a1ab4c31
AC
795
796 case N_Parameter_Association:
797 case N_Function_Call:
798 case N_Procedure_Call_Statement:
1fc24649
EB
799 /* If the parameter is by reference, an lvalue is required. */
800 return (!constant
801 || must_pass_by_ref (gnu_type)
802 || default_pass_by_ref (gnu_type));
a1ab4c31 803
0c6fbbfc
EB
804 case N_Pragma_Argument_Association:
805 return lvalue_required_p (gnat_parent, gnu_type, constant,
806 address_of_constant);
807
808 case N_Pragma:
809 if (Is_Pragma_Name (Chars (Pragma_Identifier (gnat_parent))))
810 {
811 const unsigned char id
812 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_parent)));
813 return id == Pragma_Inspection_Point;
814 }
815 else
816 return 0;
817
a1ab4c31
AC
818 case N_Indexed_Component:
819 /* Only the array expression can require an lvalue. */
820 if (Prefix (gnat_parent) != gnat_node)
821 return 0;
822
ed98eed8
EB
823 /* ??? Consider that referencing an indexed component with a variable
824 index forces the whole aggregate to memory. Note that testing only
825 for literals is conservative, any static expression in the RM sense
826 could probably be accepted with some additional work. */
a1ab4c31
AC
827 for (gnat_temp = First (Expressions (gnat_parent));
828 Present (gnat_temp);
829 gnat_temp = Next (gnat_temp))
ed98eed8
EB
830 if (Nkind (gnat_temp) != N_Character_Literal
831 && Nkind (gnat_temp) != N_Integer_Literal
832 && !(Is_Entity_Name (gnat_temp)
833 && Ekind (Entity (gnat_temp)) == E_Enumeration_Literal))
a1ab4c31
AC
834 return 1;
835
9c453de7 836 /* ... fall through ... */
a1ab4c31 837
527ed00b 838 case N_Selected_Component:
a1ab4c31 839 case N_Slice:
527ed00b 840 /* Only the prefix expression can require an lvalue. */
a1ab4c31
AC
841 if (Prefix (gnat_parent) != gnat_node)
842 return 0;
843
7f6dd102
EB
844 return lvalue_required_p (gnat_parent,
845 get_unpadded_type (Etype (gnat_parent)),
846 constant, address_of_constant);
a1ab4c31 847
a1ab4c31 848 case N_Object_Renaming_Declaration:
e297e2ea
EB
849 /* We need to preserve addresses through a renaming. */
850 return 1;
a1ab4c31 851
bbaba73f
EB
852 case N_Object_Declaration:
853 /* We cannot use a constructor if this is an atomic object because
854 the actual assignment might end up being done component-wise. */
1fc24649
EB
855 return (!constant
856 ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
b120ca61 857 && Is_Full_Access (Defining_Entity (gnat_parent)))
cb3d597d
EB
858 /* We don't use a constructor if this is a class-wide object
859 because the effective type of the object is the equivalent
860 type of the class-wide subtype and it smashes most of the
861 data into an array of bytes to which we cannot convert. */
862 || Ekind ((Etype (Defining_Entity (gnat_parent))))
863 == E_Class_Wide_Subtype);
bbaba73f
EB
864
865 case N_Assignment_Statement:
866 /* We cannot use a constructor if the LHS is an atomic object because
867 the actual assignment might end up being done component-wise. */
1fc24649
EB
868 return (!constant
869 || Name (gnat_parent) == gnat_node
03b6f8a2 870 || (Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
f797c2b7 871 && Is_Entity_Name (Name (gnat_parent))
b120ca61 872 && Is_Full_Access (Entity (Name (gnat_parent)))));
bbaba73f 873
054d6b83
EB
874 case N_Unchecked_Type_Conversion:
875 if (!constant)
876 return 1;
76af763d 877
9c453de7 878 /* ... fall through ... */
76af763d 879
054d6b83
EB
880 case N_Type_Conversion:
881 case N_Qualified_Expression:
882 /* We must look through all conversions because we may need to bypass
883 an intermediate conversion that is meant to be purely formal. */
884 return lvalue_required_p (gnat_parent,
885 get_unpadded_type (Etype (gnat_parent)),
86da71db 886 constant, address_of_constant);
cb3d597d
EB
887
888 case N_Explicit_Dereference:
889 /* We look through dereferences for address of constant because we need
890 to handle the special cases listed above. */
891 if (constant && address_of_constant)
892 return lvalue_required_p (gnat_parent,
893 get_unpadded_type (Etype (gnat_parent)),
86da71db 894 true, false);
cb3d597d 895
9c453de7 896 /* ... fall through ... */
22d12fc2 897
a1ab4c31
AC
898 default:
899 return 0;
900 }
901
902 gcc_unreachable ();
903}
904
527ed00b
EB
905/* Return true if an lvalue should be used for GNAT_NODE. GNU_TYPE is the type
906 that will be used for GNAT_NODE in the translated GNU tree and is assumed to
907 be an aggregate type.
908
909 The function climbs up the GNAT tree starting from the node and returns true
910 upon encountering a node that makes it doable to decide. lvalue_required_p
911 should have been previously invoked on the arguments and returned false. */
912
913static bool
914lvalue_for_aggregate_p (Node_Id gnat_node, tree gnu_type)
915{
916 Node_Id gnat_parent = Parent (gnat_node);
917
918 switch (Nkind (gnat_parent))
919 {
920 case N_Parameter_Association:
921 case N_Function_Call:
922 case N_Procedure_Call_Statement:
923 /* Even if the parameter is by copy, prefer an lvalue. */
924 return true;
925
6a1e04b2
EB
926 case N_Simple_Return_Statement:
927 /* Likewise for a return value. */
928 return true;
929
527ed00b
EB
930 case N_Indexed_Component:
931 case N_Selected_Component:
932 /* If an elementary component is used, take it from the constant. */
933 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_parent))))
934 return false;
935
936 /* ... fall through ... */
937
938 case N_Slice:
939 return lvalue_for_aggregate_p (gnat_parent,
940 get_unpadded_type (Etype (gnat_parent)));
941
942 case N_Object_Declaration:
fe47e816
EB
943 /* For an aggregate object declaration, return false consistently. */
944 return false;
527ed00b
EB
945
946 case N_Assignment_Statement:
947 /* For an aggregate assignment, decide based on the size. */
948 {
949 const HOST_WIDE_INT size = int_size_in_bytes (gnu_type);
950 return size < 0 || size >= param_large_stack_frame / 4;
951 }
952
953 case N_Unchecked_Type_Conversion:
954 case N_Type_Conversion:
955 case N_Qualified_Expression:
956 return lvalue_for_aggregate_p (gnat_parent,
957 get_unpadded_type (Etype (gnat_parent)));
958
959 case N_Allocator:
960 /* We should only reach here through the N_Qualified_Expression case.
961 Force an lvalue for aggregate types since a block-copy to the newly
962 allocated area of memory is made. */
963 return true;
964
965 default:
966 return false;
967 }
968
969 gcc_unreachable ();
970}
971
972
e297e2ea
EB
973/* Return true if T is a constant DECL node that can be safely replaced
974 by its initializer. */
975
976static bool
977constant_decl_with_initializer_p (tree t)
978{
979 if (!TREE_CONSTANT (t) || !DECL_P (t) || !DECL_INITIAL (t))
980 return false;
981
982 /* Return false for aggregate types that contain a placeholder since
983 their initializers cannot be manipulated easily. */
984 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
985 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (t))
986 && type_contains_placeholder_p (TREE_TYPE (t)))
987 return false;
988
989 return true;
990}
991
992/* Return an expression equivalent to EXP but where constant DECL nodes
993 have been replaced by their initializer. */
994
995static tree
996fold_constant_decl_in_expr (tree exp)
997{
998 enum tree_code code = TREE_CODE (exp);
999 tree op0;
1000
1001 switch (code)
1002 {
1003 case CONST_DECL:
1004 case VAR_DECL:
1005 if (!constant_decl_with_initializer_p (exp))
1006 return exp;
1007
1008 return DECL_INITIAL (exp);
1009
e297e2ea
EB
1010 case COMPONENT_REF:
1011 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
1012 if (op0 == TREE_OPERAND (exp, 0))
1013 return exp;
1014
552cc590
EB
1015 return fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0,
1016 TREE_OPERAND (exp, 1), NULL_TREE);
1017
1018 case BIT_FIELD_REF:
1019 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
1020 if (op0 == TREE_OPERAND (exp, 0))
1021 return exp;
1022
1023 return fold_build3 (BIT_FIELD_REF, TREE_TYPE (exp), op0,
1024 TREE_OPERAND (exp, 1), TREE_OPERAND (exp, 2));
e297e2ea
EB
1025
1026 case ARRAY_REF:
1027 case ARRAY_RANGE_REF:
a239279e
EB
1028 /* If the index is not itself constant, then nothing can be folded. */
1029 if (!TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1030 return exp;
e297e2ea
EB
1031 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
1032 if (op0 == TREE_OPERAND (exp, 0))
1033 return exp;
1034
1035 return fold (build4 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1),
544d14e1 1036 TREE_OPERAND (exp, 2), TREE_OPERAND (exp, 3)));
e297e2ea 1037
e297e2ea
EB
1038 case REALPART_EXPR:
1039 case IMAGPART_EXPR:
ea292448 1040 case VIEW_CONVERT_EXPR:
e297e2ea
EB
1041 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
1042 if (op0 == TREE_OPERAND (exp, 0))
1043 return exp;
1044
1045 return fold_build1 (code, TREE_TYPE (exp), op0);
1046
1047 default:
1048 return exp;
1049 }
1050
1051 gcc_unreachable ();
1052}
1053
3016ec8a
EB
1054/* Return true if TYPE and DEF_TYPE are compatible GNAT types for Gigi. */
1055
1056static bool
1057Gigi_Types_Compatible (Entity_Id type, Entity_Id def_type)
1058{
1059 /* The trivial case. */
1060 if (type == def_type)
1061 return true;
1062
1063 /* A class-wide type is equivalent to a subtype of itself. */
1064 if (Is_Class_Wide_Type (type))
1065 return true;
1066
1067 /* A packed array type is compatible with its implementation type. */
1068 if (Is_Packed (def_type) && type == Packed_Array_Impl_Type (def_type))
1069 return true;
1070
1071 /* If both types are Itypes, one may be a copy of the other. */
1072 if (Is_Itype (def_type) && Is_Itype (type))
1073 return true;
1074
1075 /* If the type is incomplete and comes from a limited context, then also
1076 consider its non-limited view. */
1077 if (Is_Incomplete_Type (def_type)
1078 && From_Limited_With (def_type)
1079 && Present (Non_Limited_View (def_type)))
1080 return Gigi_Types_Compatible (type, Non_Limited_View (def_type));
1081
1082 /* If the type is incomplete/private, then also consider its full view. */
1083 if (Is_Incomplete_Or_Private_Type (def_type)
1084 && Present (Full_View (def_type)))
1085 return Gigi_Types_Compatible (type, Full_View (def_type));
1086
1087 return false;
1088}
1089
ef12e74c
EB
1090/* Return the full view of a private constant E, or of a renaming thereof, if
1091 its type has discriminants, and Empty otherwise. */
1092
1093static Entity_Id
1094Full_View_Of_Private_Constant (Entity_Id E)
1095{
1096 while (Present (Renamed_Object (E)) && Is_Entity_Name (Renamed_Object (E)))
1097 E = Entity (Renamed_Object (E));
1098
1099 if (Ekind (E) != E_Constant || No (Full_View (E)))
1100 return Empty;
1101
1102 const Entity_Id T = Etype (E);
1103
1104 if (Is_Private_Type (T)
1105 && (Has_Unknown_Discriminants (T)
1106 || (Present (Full_View (T)) && Has_Discriminants (Full_View (T)))))
1107 return Full_View (E);
1108
1109 return Empty;
1110}
1111
e08f1aad
EB
1112/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Identifier, to a GCC
1113 tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we should
1114 place the result type. */
a1ab4c31
AC
1115
1116static tree
1117Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
1118{
ef12e74c
EB
1119 Entity_Id gnat_entity = (Nkind (gnat_node) == N_Defining_Identifier
1120 || Nkind (gnat_node) == N_Defining_Operator_Symbol)
1121 ? gnat_node : Entity (gnat_node);
1122 Entity_Id gnat_result_type;
1123 tree gnu_result, gnu_result_type;
a1ab4c31
AC
1124 /* If GNAT_NODE is a constant, whether we should use the initialization
1125 value instead of the constant entity, typically for scalars with an
1126 address clause when the parent doesn't require an lvalue. */
ef12e74c 1127 bool use_constant_initializer;
3016ec8a
EB
1128 /* Whether we should require an lvalue for GNAT_NODE. Needed in
1129 specific circumstances only, so evaluated lazily. < 0 means
1130 unknown, > 0 means known true, 0 means known false. */
ef12e74c 1131 int require_lvalue;
a1ab4c31 1132
1e55d29a
EB
1133 /* If the Etype of this node is not the same as that of the Entity, then
1134 something went wrong, probably in generic instantiation. However, this
1135 does not apply to types. Since we sometime have strange Ekind's, just
3016ec8a
EB
1136 do this test for objects, except for discriminants because their type
1137 may have been changed to a subtype by Exp_Ch3.Adjust_Discriminants. */
1138 gcc_assert (!Is_Object (gnat_entity)
1139 || Ekind (gnat_entity) == E_Discriminant
ef12e74c
EB
1140 || Etype (gnat_node) == Etype (gnat_entity)
1141 || Gigi_Types_Compatible (Etype (gnat_node),
1142 Etype (gnat_entity)));
a1ab4c31 1143
ef12e74c 1144 /* If this is a reference to a deferred constant whose partial view is of
a1ab4c31 1145 unconstrained private type, the proper type is on the full view of the
ef12e74c
EB
1146 constant, not on the full view of the type which may be unconstrained. */
1147 const Entity_Id gnat_full_view = Full_View_Of_Private_Constant (gnat_entity);
1148 if (Present (gnat_full_view))
a1ab4c31 1149 {
ef12e74c 1150 gnat_entity = gnat_full_view;
3016ec8a 1151 gnat_result_type = Etype (gnat_entity);
a1ab4c31
AC
1152 }
1153 else
1154 {
eb0f4e48
EB
1155 /* We use the Actual_Subtype only if it has already been elaborated,
1156 as we may be invoked precisely during its elaboration, otherwise
79069232
EB
1157 the Etype. Avoid using it for packed arrays to simplify things,
1158 except in a return statement because we need the actual size and
1159 the front-end does not make it explicit in this case. */
3016ec8a 1160 if ((Ekind (gnat_entity) == E_Constant
eb0f4e48
EB
1161 || Ekind (gnat_entity) == E_Variable
1162 || Is_Formal (gnat_entity))
3016ec8a 1163 && !(Is_Array_Type (Etype (gnat_entity))
79069232
EB
1164 && Present (Packed_Array_Impl_Type (Etype (gnat_entity)))
1165 && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement)
3016ec8a
EB
1166 && Present (Actual_Subtype (gnat_entity))
1167 && present_gnu_tree (Actual_Subtype (gnat_entity)))
1168 gnat_result_type = Actual_Subtype (gnat_entity);
a1ab4c31 1169 else
3016ec8a 1170 gnat_result_type = Etype (gnat_node);
a1ab4c31
AC
1171 }
1172
59a6274d
EB
1173 /* Expand the type of this identifier first if it is needed, in case it is an
1174 enumeral literal, which only get made when the type is expanded. There is
1175 no order-of-elaboration issue here. */
1176 if (Is_Subprogram (gnat_entity))
1177 gnu_result_type = NULL_TREE;
1178 else
1179 gnu_result_type = get_unpadded_type (gnat_result_type);
a1ab4c31 1180
e4270465 1181 /* If this is a non-imported elementary constant with an address clause,
a1ab4c31
AC
1182 retrieve the value instead of a pointer to be dereferenced unless
1183 an lvalue is required. This is generally more efficient and actually
1184 required if this is a static expression because it might be used
1185 in a context where a dereference is inappropriate, such as a case
1186 statement alternative or a record discriminant. There is no possible
308e6f3a 1187 volatile-ness short-circuit here since Volatile constants must be
1e17ef87 1188 imported per C.6. */
3016ec8a
EB
1189 if (Ekind (gnat_entity) == E_Constant
1190 && Is_Elementary_Type (gnat_result_type)
1191 && !Is_Imported (gnat_entity)
1192 && Present (Address_Clause (gnat_entity)))
a1ab4c31 1193 {
86da71db
EB
1194 require_lvalue
1195 = lvalue_required_p (gnat_node, gnu_result_type, true, false);
a1ab4c31
AC
1196 use_constant_initializer = !require_lvalue;
1197 }
ef12e74c
EB
1198 else
1199 {
1200 require_lvalue = -1;
1201 use_constant_initializer = false;
1202 }
a1ab4c31 1203
ef12e74c 1204 /* Fetch the initialization value of a constant if requested. */
a1ab4c31
AC
1205 if (use_constant_initializer)
1206 {
1207 /* If this is a deferred constant, the initializer is attached to
1208 the full view. */
3016ec8a
EB
1209 if (Present (Full_View (gnat_entity)))
1210 gnat_entity = Full_View (gnat_entity);
a1ab4c31 1211
3016ec8a 1212 gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
a1ab4c31
AC
1213 }
1214 else
3016ec8a 1215 gnu_result = gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
a1ab4c31 1216
a1ab4c31
AC
1217 /* Some objects (such as parameters passed by reference, globals of
1218 variable size, and renamed objects) actually represent the address
1219 of the object. In that case, we must do the dereference. Likewise,
1220 deal with parameters to foreign convention subprograms. */
1221 if (DECL_P (gnu_result)
1222 && (DECL_BY_REF_P (gnu_result)
1223 || (TREE_CODE (gnu_result) == PARM_DECL
1224 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
1225 {
ced57283 1226 const bool read_only = DECL_POINTS_TO_READONLY_P (gnu_result);
a1ab4c31 1227
ad1d36ba 1228 /* If it's a PARM_DECL to foreign convention subprogram, convert it. */
a1ab4c31
AC
1229 if (TREE_CODE (gnu_result) == PARM_DECL
1230 && DECL_BY_COMPONENT_PTR_P (gnu_result))
ad1d36ba
EB
1231 gnu_result
1232 = convert (build_pointer_type (gnu_result_type), gnu_result);
1233
1234 /* If it's a CONST_DECL, return the underlying constant like below. */
e4270465
EB
1235 else if (TREE_CODE (gnu_result) == CONST_DECL
1236 && !(DECL_CONST_ADDRESS_P (gnu_result)
1237 && lvalue_required_p (gnat_node, gnu_result_type, true,
86da71db 1238 true)))
ad1d36ba 1239 gnu_result = DECL_INITIAL (gnu_result);
a1ab4c31 1240
5bdd063b
EB
1241 /* Do the final dereference. */
1242 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
a1ab4c31 1243
5bdd063b
EB
1244 if ((TREE_CODE (gnu_result) == INDIRECT_REF
1245 || TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
1246 && No (Address_Clause (gnat_entity)))
1247 TREE_THIS_NOTRAP (gnu_result) = 1;
ad1d36ba 1248
5bdd063b
EB
1249 if (read_only)
1250 TREE_READONLY (gnu_result) = 1;
a1ab4c31
AC
1251 }
1252
58c8f770
EB
1253 /* If we have a constant declaration and its initializer, try to return the
1254 latter to avoid the need to call fold in lots of places and the need for
e297e2ea
EB
1255 elaboration code if this identifier is used as an initializer itself. */
1256 if (constant_decl_with_initializer_p (gnu_result))
a1ab4c31 1257 {
c34f3839
EB
1258 bool constant_only = (TREE_CODE (gnu_result) == CONST_DECL
1259 && !DECL_CONST_CORRESPONDING_VAR (gnu_result));
cb3d597d
EB
1260 bool address_of_constant = (TREE_CODE (gnu_result) == CONST_DECL
1261 && DECL_CONST_ADDRESS_P (gnu_result));
1262
1263 /* If there is a (corresponding) variable or this is the address of a
1264 constant, we only want to return the initializer if an lvalue isn't
1265 required. Evaluate this now if we have not already done so. */
1266 if ((!constant_only || address_of_constant) && require_lvalue < 0)
1267 require_lvalue
1268 = lvalue_required_p (gnat_node, gnu_result_type, true,
527ed00b
EB
1269 address_of_constant)
1270 || (AGGREGATE_TYPE_P (gnu_result_type)
1271 && lvalue_for_aggregate_p (gnat_node, gnu_result_type));
cb3d597d 1272
6ba4f08f 1273 /* Finally retrieve the initializer if this is deemed valid. */
cb3d597d 1274 if ((constant_only && !address_of_constant) || !require_lvalue)
6ba4f08f 1275 gnu_result = DECL_INITIAL (gnu_result);
a1ab4c31
AC
1276 }
1277
e297e2ea
EB
1278 /* But for a constant renaming we couldn't do that incrementally for its
1279 definition because of the need to return an lvalue so, if the present
1280 context doesn't itself require an lvalue, we try again here. */
3016ec8a
EB
1281 else if (Ekind (gnat_entity) == E_Constant
1282 && Is_Elementary_Type (gnat_result_type)
1283 && Present (Renamed_Object (gnat_entity)))
e297e2ea
EB
1284 {
1285 if (require_lvalue < 0)
1286 require_lvalue
86da71db 1287 = lvalue_required_p (gnat_node, gnu_result_type, true, false);
e297e2ea
EB
1288 if (!require_lvalue)
1289 gnu_result = fold_constant_decl_in_expr (gnu_result);
1290 }
1291
1c4ae4e5
EB
1292 /* The GNAT tree has the type of a function set to its result type, so we
1293 adjust here. Also use the type of the result if the Etype is a subtype
1294 that is nominally unconstrained. Likewise if this is a deferred constant
1295 of a discriminated type whose full view can be elaborated statically, to
1296 avoid problematic conversions to the nominal subtype. But remove any
1297 padding from the resulting type. */
69720717 1298 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_result))
3016ec8a
EB
1299 || Is_Constr_Subt_For_UN_Aliased (gnat_result_type)
1300 || (Ekind (gnat_entity) == E_Constant
1301 && Present (Full_View (gnat_entity))
1302 && Has_Discriminants (gnat_result_type)
1c4ae4e5
EB
1303 && TREE_CODE (gnu_result) == CONSTRUCTOR))
1304 {
1305 gnu_result_type = TREE_TYPE (gnu_result);
1306 if (TYPE_IS_PADDING_P (gnu_result_type))
1307 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
1308 }
1309
a1ab4c31 1310 *gnu_result_type_p = gnu_result_type;
58c8f770 1311
a1ab4c31
AC
1312 return gnu_result;
1313}
e76a8cac 1314
e08f1aad
EB
1315/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Pragma, to a GCC
1316 tree, which is returned. */
a1ab4c31
AC
1317
1318static tree
1319Pragma_to_gnu (Node_Id gnat_node)
1320{
47625858 1321 tree gnu_result = alloc_stmt_list ();
f2423384 1322 Node_Id gnat_temp;
a1ab4c31 1323
ec4a0d83
EB
1324 /* Check for (and ignore) unrecognized pragmas. */
1325 if (!Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node))))
a1ab4c31
AC
1326 return gnu_result;
1327
ec4a0d83
EB
1328 const unsigned char id
1329 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
1330
1331 /* Save the expression of pragma Compile_Time_{Error|Warning} for later. */
1332 if (id == Pragma_Compile_Time_Error || id == Pragma_Compile_Time_Warning)
1333 {
1334 gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1335 gnat_compile_time_expr_list.safe_push (Expression (gnat_temp));
1336 return gnu_result;
1337 }
1338
1339 /* Stop there if we are just annotating types. */
1340 if (type_annotate_only)
1341 return gnu_result;
1342
1343 switch (id)
a1ab4c31
AC
1344 {
1345 case Pragma_Inspection_Point:
1346 /* Do nothing at top level: all such variables are already viewable. */
1347 if (global_bindings_p ())
1348 break;
1349
1350 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1351 Present (gnat_temp);
1352 gnat_temp = Next (gnat_temp))
1353 {
1354 Node_Id gnat_expr = Expression (gnat_temp);
1355 tree gnu_expr = gnat_to_gnu (gnat_expr);
a1ab4c31
AC
1356 tree asm_constraint = NULL_TREE;
1357#ifdef ASM_COMMENT_START
1358 char *comment;
1359#endif
9e4cacfa 1360 gnu_expr = maybe_unconstrained_array (gnu_expr);
0c6fbbfc
EB
1361 if (TREE_CODE (gnu_expr) == CONST_DECL
1362 && DECL_CONST_CORRESPONDING_VAR (gnu_expr))
1363 gnu_expr = DECL_CONST_CORRESPONDING_VAR (gnu_expr);
9e4cacfa 1364 gnat_mark_addressable (gnu_expr);
a1ab4c31
AC
1365
1366#ifdef ASM_COMMENT_START
1367 comment = concat (ASM_COMMENT_START,
1368 " inspection point: ",
1369 Get_Name_String (Chars (gnat_expr)),
9e4cacfa 1370 " is at %0",
a1ab4c31
AC
1371 NULL);
1372 asm_constraint = build_string (strlen (comment), comment);
1373 free (comment);
1374#endif
1c384bf1 1375 gnu_expr = build5 (ASM_EXPR, void_type_node,
a1ab4c31
AC
1376 asm_constraint,
1377 NULL_TREE,
1378 tree_cons
1379 (build_tree_list (NULL_TREE,
9e4cacfa
EB
1380 build_string (1, "m")),
1381 gnu_expr, NULL_TREE),
1c384bf1 1382 NULL_TREE, NULL_TREE);
a1ab4c31
AC
1383 ASM_VOLATILE_P (gnu_expr) = 1;
1384 set_expr_location_from_node (gnu_expr, gnat_node);
1385 append_to_statement_list (gnu_expr, &gnu_result);
1386 }
1387 break;
1388
633a3f2a
EB
1389 case Pragma_Loop_Optimize:
1390 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1391 Present (gnat_temp);
1392 gnat_temp = Next (gnat_temp))
1393 {
3418f5e9 1394 tree gnu_loop_stmt = gnu_loop_stack->last ()->stmt;
633a3f2a
EB
1395
1396 switch (Chars (Expression (gnat_temp)))
1397 {
3418f5e9
EB
1398 case Name_Ivdep:
1399 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
1400 break;
1401
633a3f2a
EB
1402 case Name_No_Unroll:
1403 LOOP_STMT_NO_UNROLL (gnu_loop_stmt) = 1;
1404 break;
1405
1406 case Name_Unroll:
1407 LOOP_STMT_UNROLL (gnu_loop_stmt) = 1;
1408 break;
1409
1410 case Name_No_Vector:
1411 LOOP_STMT_NO_VECTOR (gnu_loop_stmt) = 1;
1412 break;
1413
1414 case Name_Vector:
1415 LOOP_STMT_VECTOR (gnu_loop_stmt) = 1;
1416 break;
1417
1418 default:
1419 gcc_unreachable ();
1420 }
1421 }
1422 break;
1423
a1ab4c31
AC
1424 case Pragma_Optimize:
1425 switch (Chars (Expression
1426 (First (Pragma_Argument_Associations (gnat_node)))))
1427 {
a1ab4c31 1428 case Name_Off:
e84319a3 1429 if (optimize)
4a29b8d6 1430 post_error ("must specify -O0??", gnat_node);
a1ab4c31
AC
1431 break;
1432
257e81a6
EB
1433 case Name_Space:
1434 if (!optimize_size)
4a29b8d6 1435 post_error ("must specify -Os??", gnat_node);
257e81a6
EB
1436 break;
1437
1438 case Name_Time:
1439 if (!optimize)
4a29b8d6 1440 post_error ("insufficient -O value??", gnat_node);
257e81a6
EB
1441 break;
1442
a1ab4c31
AC
1443 default:
1444 gcc_unreachable ();
1445 }
1446 break;
1447
1448 case Pragma_Reviewable:
1449 if (write_symbols == NO_DEBUG)
4a29b8d6 1450 post_error ("must specify -g??", gnat_node);
a1ab4c31 1451 break;
f2423384 1452
2749b01b 1453 case Pragma_Warning_As_Error:
f2423384
EB
1454 case Pragma_Warnings:
1455 {
1456 Node_Id gnat_expr;
1457 /* Preserve the location of the pragma. */
1458 const location_t location = input_location;
1459 struct cl_option_handlers handlers;
1460 unsigned int option_index;
1461 diagnostic_t kind;
1462 bool imply;
1463
1464 gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1465
2749b01b 1466 /* This is the String form: pragma Warning{s|_As_Error}(String). */
f2423384
EB
1467 if (Nkind (Expression (gnat_temp)) == N_String_Literal)
1468 {
ec4a0d83 1469 switch (id)
2749b01b
EB
1470 {
1471 case Pragma_Warning_As_Error:
1472 kind = DK_ERROR;
1473 imply = false;
1474 break;
1475
1476 case Pragma_Warnings:
1477 kind = DK_WARNING;
1478 imply = true;
1479 break;
1480
1481 default:
1482 gcc_unreachable ();
1483 }
1484
f2423384 1485 gnat_expr = Expression (gnat_temp);
f2423384
EB
1486 }
1487
1488 /* This is the On/Off form: pragma Warnings (On | Off [,String]). */
1489 else if (Nkind (Expression (gnat_temp)) == N_Identifier)
1490 {
1491 switch (Chars (Expression (gnat_temp)))
1492 {
1493 case Name_Off:
1494 kind = DK_IGNORED;
1495 break;
1496
1497 case Name_On:
1498 kind = DK_WARNING;
1499 break;
1500
1501 default:
1502 gcc_unreachable ();
1503 }
1504
f10ff6cc 1505 /* Deal with optional pattern (but ignore Reason => "..."). */
113c69ff
EB
1506 if (Present (Next (gnat_temp))
1507 && Chars (Next (gnat_temp)) != Name_Reason)
f2423384
EB
1508 {
1509 /* pragma Warnings (On | Off, Name) is handled differently. */
1510 if (Nkind (Expression (Next (gnat_temp))) != N_String_Literal)
1511 break;
1512
1513 gnat_expr = Expression (Next (gnat_temp));
1514 }
1515 else
e3371908
EB
1516 {
1517 gnat_expr = Empty;
1518
1519 /* For pragma Warnings (Off), we save the current state... */
1520 if (kind == DK_IGNORED)
1521 diagnostic_push_diagnostics (global_dc, location);
1522
1523 /* ...so that, for pragma Warnings (On), we do not enable all
1524 the warnings but just restore the previous state. */
1525 else
1526 {
1527 diagnostic_pop_diagnostics (global_dc, location);
1528 break;
1529 }
1530 }
f2423384
EB
1531
1532 imply = false;
1533 }
1534
1535 else
1536 gcc_unreachable ();
1537
1538 /* This is the same implementation as in the C family of compilers. */
0cec1c2d 1539 const unsigned int lang_mask = CL_Ada | CL_COMMON;
63bbf46d 1540 const char *arg = NULL;
f2423384
EB
1541 if (Present (gnat_expr))
1542 {
1543 tree gnu_expr = gnat_to_gnu (gnat_expr);
0cec1c2d 1544 const char *option_string = TREE_STRING_POINTER (gnu_expr);
f2423384 1545 const int len = TREE_STRING_LENGTH (gnu_expr);
0cec1c2d 1546 if (len < 3 || option_string[0] != '-' || option_string[1] != 'W')
f2423384 1547 break;
0cec1c2d
EB
1548 option_index = find_opt (option_string + 1, lang_mask);
1549 if (option_index == OPT_SPECIAL_unknown)
1550 {
4a29b8d6 1551 post_error ("unknown -W switch??", gnat_node);
f2423384 1552 break;
0cec1c2d
EB
1553 }
1554 else if (!(cl_options[option_index].flags & CL_WARNING))
1555 {
4a29b8d6 1556 post_error ("-W switch does not control warning??", gnat_node);
0cec1c2d
EB
1557 break;
1558 }
1559 else if (!(cl_options[option_index].flags & lang_mask))
f2423384 1560 {
4a29b8d6 1561 post_error ("-W switch not valid for Ada??", gnat_node);
f2423384
EB
1562 break;
1563 }
63bbf46d
JJ
1564 if (cl_options[option_index].flags & CL_JOINED)
1565 arg = option_string + 1 + cl_options[option_index].opt_len;
f2423384
EB
1566 }
1567 else
1568 option_index = 0;
1569
0694da45 1570 set_default_handlers (&handlers, NULL);
63bbf46d 1571 control_warning_option (option_index, (int) kind, arg, imply, location,
0cec1c2d 1572 lang_mask, &handlers, &global_options,
f2423384
EB
1573 &global_options_set, global_dc);
1574 }
1575 break;
633a3f2a
EB
1576
1577 default:
1578 break;
a1ab4c31
AC
1579 }
1580
1581 return gnu_result;
1582}
ce2d0ce2 1583
ae5a77fa 1584/* Check the inline status of nested function FNDECL wrt its parent function.
87411e95
EB
1585
1586 If a non-inline nested function is referenced from an inline external
1587 function, we cannot honor both requests at the same time without cloning
1588 the nested function in the current unit since it is private to its unit.
1589 We could inline it as well but it's probably better to err on the side
1590 of too little inlining.
1591
ae5a77fa 1592 This must be done only on nested functions present in the source code
87411e95 1593 and not on nested functions generated by the compiler, e.g. finalizers,
ae5a77fa
EB
1594 because they may be not marked inline and we don't want them to block
1595 the inlining of the parent function. */
87411e95
EB
1596
1597static void
1598check_inlining_for_nested_subprog (tree fndecl)
1599{
ae5a77fa
EB
1600 if (DECL_IGNORED_P (current_function_decl) || DECL_IGNORED_P (fndecl))
1601 return;
1602
1603 if (DECL_DECLARED_INLINE_P (fndecl))
1604 return;
1605
1606 tree parent_decl = decl_function_context (fndecl);
1607 if (DECL_EXTERNAL (parent_decl) && DECL_DECLARED_INLINE_P (parent_decl))
87411e95
EB
1608 {
1609 const location_t loc1 = DECL_SOURCE_LOCATION (fndecl);
ae5a77fa 1610 const location_t loc2 = DECL_SOURCE_LOCATION (parent_decl);
87411e95 1611
ae5a77fa 1612 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (parent_decl)))
87411e95 1613 {
a9c697b8
MS
1614 error_at (loc1, "subprogram %q+F not marked %<Inline_Always%>",
1615 fndecl);
87411e95
EB
1616 error_at (loc2, "parent subprogram cannot be inlined");
1617 }
1618 else
1619 {
a9c697b8 1620 warning_at (loc1, OPT_Winline, "subprogram %q+F not marked %<Inline%>",
87411e95
EB
1621 fndecl);
1622 warning_at (loc2, OPT_Winline, "parent subprogram cannot be inlined");
1623 }
1624
ae5a77fa
EB
1625 DECL_DECLARED_INLINE_P (parent_decl) = 0;
1626 DECL_UNINLINABLE (parent_decl) = 1;
87411e95
EB
1627 }
1628}
ce2d0ce2 1629
1eb58520
AC
1630/* Return an expression for the length of TYPE, an integral type, computed in
1631 RESULT_TYPE, another integral type.
1632
1633 We used to compute the length as MAX (hb - lb + 1, 0) which could overflow
1634 when lb == TYPE'First. We now compute it as (hb >= lb) ? hb - lb + 1 : 0
1635 which would only overflow in much rarer cases, for extremely large arrays
1636 we expect never to encounter in practice. Besides, the former computation
1637 required the use of potentially constraining signed arithmetics while the
1638 latter does not. Note that the comparison must be done in the original
1639 base index type in order to avoid any overflow during the conversion. */
1640
1641static tree
1642get_type_length (tree type, tree result_type)
1643{
1644 tree comp_type = get_base_type (result_type);
825da0d2 1645 tree base_type = maybe_character_type (get_base_type (type));
1eb58520
AC
1646 tree lb = convert (base_type, TYPE_MIN_VALUE (type));
1647 tree hb = convert (base_type, TYPE_MAX_VALUE (type));
1648 tree length
1649 = build_binary_op (PLUS_EXPR, comp_type,
1650 build_binary_op (MINUS_EXPR, comp_type,
1651 convert (comp_type, hb),
1652 convert (comp_type, lb)),
9a1bdc31 1653 build_int_cst (comp_type, 1));
1eb58520
AC
1654 length
1655 = build_cond_expr (result_type,
1656 build_binary_op (GE_EXPR, boolean_type_node, hb, lb),
1657 convert (result_type, length),
9a1bdc31 1658 build_int_cst (result_type, 0));
1eb58520
AC
1659 return length;
1660}
1661
e08f1aad
EB
1662/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node, to a
1663 GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we
1664 should place the result type. ATTRIBUTE is the attribute ID. */
a1ab4c31
AC
1665
1666static tree
1667Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
1668{
088d3b0f 1669 const Node_Id gnat_prefix = Prefix (gnat_node);
1e55d29a
EB
1670 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
1671 tree gnu_type = TREE_TYPE (gnu_prefix);
1672 tree gnu_expr, gnu_result_type, gnu_result = error_mark_node;
caa9d12a 1673 bool prefix_unused = false;
544d14e1 1674 Entity_Id gnat_smo;
a1ab4c31
AC
1675
1676 /* If the input is a NULL_EXPR, make a new one. */
1677 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
1678 {
feec4372
EB
1679 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1680 *gnu_result_type_p = gnu_result_type;
1681 return build1 (NULL_EXPR, gnu_result_type, TREE_OPERAND (gnu_prefix, 0));
a1ab4c31
AC
1682 }
1683
544d14e1
EB
1684 /* If the input is a LOAD_EXPR of an unconstrained array type, the second
1685 operand contains the storage model object. */
1686 if (TREE_CODE (gnu_prefix) == LOAD_EXPR
1687 && TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1688 gnat_smo = tree_to_shwi (TREE_OPERAND (gnu_prefix, 1));
1689 else
1690 gnat_smo = Empty;
1691
a1ab4c31
AC
1692 switch (attribute)
1693 {
a1ab4c31
AC
1694 case Attr_Pred:
1695 case Attr_Succ:
feec4372
EB
1696 /* These just add or subtract the constant 1 since representation
1697 clauses for enumeration types are handled in the front-end. */
a1ab4c31
AC
1698 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1699 gnu_result_type = get_unpadded_type (Etype (gnat_node));
825da0d2
EB
1700 gnu_type = maybe_character_type (gnu_result_type);
1701 if (TREE_TYPE (gnu_expr) != gnu_type)
1702 gnu_expr = convert (gnu_type, gnu_expr);
a1ab4c31 1703 gnu_result
feec4372 1704 = build_binary_op (attribute == Attr_Pred ? MINUS_EXPR : PLUS_EXPR,
825da0d2 1705 gnu_type, gnu_expr, build_int_cst (gnu_type, 1));
a1ab4c31
AC
1706 break;
1707
1708 case Attr_Address:
1709 case Attr_Unrestricted_Access:
3fd9ae96
EB
1710 /* Conversions don't change the address of references but can cause
1711 build_unary_op to miss the references below, so strip them off.
1712 On the contrary, if the address-of operation causes a temporary
1713 to be created, then it must be created with the proper type. */
1714 gnu_expr = remove_conversions (gnu_prefix,
1715 !Must_Be_Byte_Aligned (gnat_node));
1716 if (REFERENCE_CLASS_P (gnu_expr))
1717 gnu_prefix = gnu_expr;
a1ab4c31
AC
1718
1719 /* If we are taking 'Address of an unconstrained object, this is the
1720 pointer to the underlying array. */
1721 if (attribute == Attr_Address)
1722 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1723
1724 /* If we are building a static dispatch table, we have to honor
1725 TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible
1726 with the C++ ABI. We do it in the non-static case as well,
1727 see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */
1728 else if (TARGET_VTABLE_USES_DESCRIPTORS
1729 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
1730 {
0e228dd9 1731 tree gnu_field, t;
a1ab4c31
AC
1732 /* Descriptors can only be built here for top-level functions. */
1733 bool build_descriptor = (global_bindings_p () != 0);
1734 int i;
9771b263 1735 vec<constructor_elt, va_gc> *gnu_vec = NULL;
0e228dd9 1736 constructor_elt *elt;
a1ab4c31
AC
1737
1738 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1739
1740 /* If we're not going to build the descriptor, we have to retrieve
1741 the one which will be built by the linker (or by the compiler
1742 later if a static chain is requested). */
1743 if (!build_descriptor)
1744 {
1745 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_prefix);
1746 gnu_result = fold_convert (build_pointer_type (gnu_result_type),
1747 gnu_result);
1748 gnu_result = build1 (INDIRECT_REF, gnu_result_type, gnu_result);
1749 }
1750
cb3874dc 1751 vec_safe_grow (gnu_vec, TARGET_VTABLE_USES_DESCRIPTORS, true);
9771b263 1752 elt = (gnu_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
a1ab4c31
AC
1753 for (gnu_field = TYPE_FIELDS (gnu_result_type), i = 0;
1754 i < TARGET_VTABLE_USES_DESCRIPTORS;
7d76717d 1755 gnu_field = DECL_CHAIN (gnu_field), i++)
a1ab4c31
AC
1756 {
1757 if (build_descriptor)
1758 {
1759 t = build2 (FDESC_EXPR, TREE_TYPE (gnu_field), gnu_prefix,
1760 build_int_cst (NULL_TREE, i));
1761 TREE_CONSTANT (t) = 1;
1762 }
1763 else
1764 t = build3 (COMPONENT_REF, ptr_void_ftype, gnu_result,
1765 gnu_field, NULL_TREE);
1766
0e228dd9
NF
1767 elt->index = gnu_field;
1768 elt->value = t;
1769 elt--;
a1ab4c31
AC
1770 }
1771
0e228dd9 1772 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
a1ab4c31
AC
1773 break;
1774 }
1775
9c453de7 1776 /* ... fall through ... */
a1ab4c31
AC
1777
1778 case Attr_Access:
1779 case Attr_Unchecked_Access:
1780 case Attr_Code_Address:
a5720c08
EB
1781 /* Taking the address of a type does not make sense. */
1782 gcc_assert (TREE_CODE (gnu_prefix) != TYPE_DECL);
1783
a1ab4c31
AC
1784 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1785 gnu_result
1786 = build_unary_op (((attribute == Attr_Address
1787 || attribute == Attr_Unrestricted_Access)
1788 && !Must_Be_Byte_Aligned (gnat_node))
1789 ? ATTR_ADDR_EXPR : ADDR_EXPR,
1790 gnu_result_type, gnu_prefix);
1791
1792 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
1e039275 1793 don't try to build a trampoline. */
a1ab4c31
AC
1794 if (attribute == Attr_Code_Address)
1795 {
722356ce 1796 gnu_expr = remove_conversions (gnu_result, false);
a1ab4c31
AC
1797
1798 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
1799 TREE_NO_TRAMPOLINE (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
4c640e26
EB
1800
1801 /* On targets for which function symbols denote a descriptor, the
1802 code address is stored within the first slot of the descriptor
1803 so we do an additional dereference:
1804 result = *((result_type *) result)
1805 where we expect result to be of some pointer type already. */
1806 if (targetm.calls.custom_function_descriptors == 0)
1807 gnu_result
1808 = build_unary_op (INDIRECT_REF, NULL_TREE,
1809 convert (build_pointer_type (gnu_result_type),
1810 gnu_result));
a1ab4c31
AC
1811 }
1812
28dd0055
EB
1813 /* For 'Access, issue an error message if the prefix is a C++ method
1814 since it can use a special calling convention on some platforms,
1815 which cannot be propagated to the access type. */
1816 else if (attribute == Attr_Access
69720717 1817 && TREE_CODE (TREE_TYPE (gnu_prefix)) == METHOD_TYPE)
28dd0055
EB
1818 post_error ("access to C++ constructor or member function not allowed",
1819 gnat_node);
1820
a1ab4c31
AC
1821 /* For other address attributes applied to a nested function,
1822 find an inner ADDR_EXPR and annotate it so that we can issue
1823 a useful warning with -Wtrampolines. */
ef5a9557
EB
1824 else if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_prefix))
1825 && (gnu_expr = remove_conversions (gnu_result, false))
1826 && TREE_CODE (gnu_expr) == ADDR_EXPR
1827 && decl_function_context (TREE_OPERAND (gnu_expr, 0)))
a1ab4c31 1828 {
ef5a9557 1829 set_expr_location_from_node (gnu_expr, gnat_node);
a1ab4c31 1830
ef5a9557
EB
1831 /* Also check the inlining status. */
1832 check_inlining_for_nested_subprog (TREE_OPERAND (gnu_expr, 0));
1833
1834 /* Moreover, for 'Access or 'Unrestricted_Access with non-
1835 foreign-compatible representation, mark the ADDR_EXPR so
1836 that we can build a descriptor instead of a trampoline. */
1837 if ((attribute == Attr_Access
1838 || attribute == Attr_Unrestricted_Access)
1839 && targetm.calls.custom_function_descriptors > 0
1840 && Can_Use_Internal_Rep (Underlying_Type (Etype (gnat_node))))
1841 FUNC_ADDR_BY_DESCRIPTOR (gnu_expr) = 1;
1842
1843 /* Otherwise, we need to check that we are not violating the
1844 No_Implicit_Dynamic_Code restriction. */
1845 else if (targetm.calls.custom_function_descriptors != 0)
1846 Check_Implicit_Dynamic_Code_Allowed (gnat_node);
a1ab4c31
AC
1847 }
1848 break;
1849
1850 case Attr_Pool_Address:
1851 {
a1ab4c31 1852 tree gnu_ptr = gnu_prefix;
0d7de0e1 1853 tree gnu_obj_type;
a1ab4c31
AC
1854
1855 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1856
0d7de0e1
EB
1857 /* If this is fat pointer, the object must have been allocated with the
1858 template in front of the array. So compute the template address; do
1859 it by converting to a thin pointer. */
315cff15 1860 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
a1ab4c31
AC
1861 gnu_ptr
1862 = convert (build_pointer_type
1863 (TYPE_OBJECT_RECORD_TYPE
1864 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
1865 gnu_ptr);
1866
1867 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
0d7de0e1
EB
1868
1869 /* If this is a thin pointer, the object must have been allocated with
1870 the template in front of the array. So compute the template address
1871 and return it. */
1872 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
1873 gnu_ptr
1874 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
1875 gnu_ptr,
2b45154d
EB
1876 fold_build1 (NEGATE_EXPR, sizetype,
1877 byte_position
1878 (DECL_CHAIN
1879 TYPE_FIELDS ((gnu_obj_type)))));
a1ab4c31
AC
1880
1881 gnu_result = convert (gnu_result_type, gnu_ptr);
1882 }
1883 break;
1884
1885 case Attr_Size:
1886 case Attr_Object_Size:
1887 case Attr_Value_Size:
1888 case Attr_Max_Size_In_Storage_Elements:
79069232
EB
1889 /* Strip NOPs, conversions between original and packable versions, and
1890 unpadding from GNU_PREFIX. Note that we cannot simply strip every
1891 VIEW_CONVERT_EXPR because some of them may give the actual size, e.g.
1892 for nominally unconstrained packed array. We use GNU_EXPR to see
1893 if a COMPONENT_REF was involved. */
1894 while (CONVERT_EXPR_P (gnu_prefix)
1895 || TREE_CODE (gnu_prefix) == NON_LVALUE_EXPR
1896 || (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR
1897 && TREE_CODE (TREE_TYPE (gnu_prefix)) == RECORD_TYPE
1898 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))
20faffe7 1899 == RECORD_TYPE
79069232
EB
1900 && TYPE_NAME (TREE_TYPE (gnu_prefix))
1901 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
1902 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1903 gnu_expr = gnu_prefix;
1904 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1905 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
1906 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
a1ab4c31
AC
1907 prefix_unused = true;
1908 gnu_type = TREE_TYPE (gnu_prefix);
1909
1910 /* Replace an unconstrained array type with the type of the underlying
65e0a92b
EB
1911 array, except for 'Max_Size_In_Storage_Elements because we need to
1912 return the (maximum) size requested for an allocator. */
a1ab4c31
AC
1913 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1914 {
1915 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
1916 if (attribute != Attr_Max_Size_In_Storage_Elements)
7d76717d 1917 gnu_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
a1ab4c31
AC
1918 }
1919
da81cc57
EB
1920 /* The type must be frozen at this point. */
1921 gcc_assert (COMPLETE_TYPE_P (gnu_type));
1922
842d4ee2 1923 /* If we're looking for the size of a field, return the field size. */
a1ab4c31
AC
1924 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1925 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
842d4ee2
EB
1926
1927 /* Otherwise, if the prefix is an object, or if we are looking for
1928 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the
1929 GCC size of the type. We make an exception for padded objects,
1930 as we do not take into account alignment promotions for the size.
1931 This is in keeping with the object case of gnat_to_gnu_entity. */
1932 else if ((TREE_CODE (gnu_prefix) != TYPE_DECL
1933 && !(TYPE_IS_PADDING_P (gnu_type)
4d39941e
EB
1934 && TREE_CODE (gnu_expr) == COMPONENT_REF
1935 && pad_type_has_rm_size (gnu_type)))
a1ab4c31
AC
1936 || attribute == Attr_Object_Size
1937 || attribute == Attr_Max_Size_In_Storage_Elements)
1938 {
842d4ee2
EB
1939 /* If this is a dereference and we have a special dynamic constrained
1940 subtype on the prefix, use it to compute the size; otherwise, use
1941 the designated subtype. */
088d3b0f 1942 if (Nkind (gnat_prefix) == N_Explicit_Dereference)
a1ab4c31 1943 {
1e17ef87 1944 Node_Id gnat_actual_subtype
088d3b0f 1945 = Actual_Designated_Subtype (gnat_prefix);
1e17ef87 1946 tree gnu_ptr_type
088d3b0f 1947 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix)));
1e17ef87 1948
315cff15 1949 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)
1e17ef87
EB
1950 && Present (gnat_actual_subtype))
1951 {
1952 tree gnu_actual_obj_type
1953 = gnat_to_gnu_type (gnat_actual_subtype);
1954 gnu_type
1955 = build_unc_object_type_from_ptr (gnu_ptr_type,
1956 gnu_actual_obj_type,
928dfa4b
EB
1957 get_identifier ("SIZE"),
1958 false);
1e17ef87 1959 }
a1ab4c31 1960 }
842d4ee2
EB
1961
1962 gnu_result = TYPE_SIZE (gnu_type);
a1ab4c31 1963 }
842d4ee2
EB
1964
1965 /* Otherwise, the result is the RM size of the type. */
a1ab4c31
AC
1966 else
1967 gnu_result = rm_size (gnu_type);
1968
683ccd05
EB
1969 /* Deal with a self-referential size by qualifying the size with the
1970 object or returning the maximum size for a type. */
1971 if (TREE_CODE (gnu_prefix) != TYPE_DECL)
544d14e1
EB
1972 {
1973 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
1974 if (Present (gnat_smo))
1975 gnu_result = INSTANTIATE_LOAD_IN_EXPR (gnu_result, gnat_smo);
1976 }
683ccd05
EB
1977 else if (CONTAINS_PLACEHOLDER_P (gnu_result))
1978 gnu_result = max_size (gnu_result, true);
a1ab4c31 1979
65e0a92b
EB
1980 /* If the type contains a template, subtract the padded size of the
1981 template, except for 'Max_Size_In_Storage_Elements because we need
1982 to return the (maximum) size requested for an allocator. */
a1ab4c31 1983 if (TREE_CODE (gnu_type) == RECORD_TYPE
65e0a92b
EB
1984 && TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1985 && attribute != Attr_Max_Size_In_Storage_Elements)
1986 gnu_result
1987 = size_binop (MINUS_EXPR, gnu_result,
1988 bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
a1ab4c31 1989
58c8f770 1990 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
a1ab4c31 1991 if (attribute == Attr_Max_Size_In_Storage_Elements)
58c8f770
EB
1992 gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node);
1993
1994 gnu_result_type = get_unpadded_type (Etype (gnat_node));
a1ab4c31
AC
1995 break;
1996
1997 case Attr_Alignment:
caa9d12a
EB
1998 {
1999 unsigned int align;
a1ab4c31 2000
caa9d12a 2001 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
315cff15 2002 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
caa9d12a 2003 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
a1ab4c31 2004
caa9d12a
EB
2005 gnu_type = TREE_TYPE (gnu_prefix);
2006 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2007 prefix_unused = true;
2008
2009 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
2010 align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT;
2011 else
2012 {
caa9d12a
EB
2013 Entity_Id gnat_type = Etype (gnat_prefix);
2014 unsigned int double_align;
2015 bool is_capped_double, align_clause;
2016
2017 /* If the default alignment of "double" or larger scalar types is
2018 specifically capped and there is an alignment clause neither
2019 on the type nor on the prefix itself, return the cap. */
2020 if ((double_align = double_float_alignment) > 0)
2021 is_capped_double
2022 = is_double_float_or_array (gnat_type, &align_clause);
2023 else if ((double_align = double_scalar_alignment) > 0)
2024 is_capped_double
2025 = is_double_scalar_or_array (gnat_type, &align_clause);
2026 else
2027 is_capped_double = align_clause = false;
2028
2029 if (is_capped_double
2030 && Nkind (gnat_prefix) == N_Identifier
2031 && Present (Alignment_Clause (Entity (gnat_prefix))))
2032 align_clause = true;
2033
2034 if (is_capped_double && !align_clause)
2035 align = double_align;
2036 else
2037 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
2038 }
2039
2040 gnu_result = size_int (align);
2041 }
a1ab4c31
AC
2042 break;
2043
2044 case Attr_First:
2045 case Attr_Last:
2046 case Attr_Range_Length:
2047 prefix_unused = true;
2048
c95f808d 2049 if (INTEGRAL_TYPE_P (gnu_type) || SCALAR_FLOAT_TYPE_P (gnu_type))
a1ab4c31
AC
2050 {
2051 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2052
2053 if (attribute == Attr_First)
2054 gnu_result = TYPE_MIN_VALUE (gnu_type);
2055 else if (attribute == Attr_Last)
2056 gnu_result = TYPE_MAX_VALUE (gnu_type);
2057 else
1eb58520 2058 gnu_result = get_type_length (gnu_type, gnu_result_type);
a1ab4c31
AC
2059 break;
2060 }
2061
9c453de7 2062 /* ... fall through ... */
a1ab4c31
AC
2063
2064 case Attr_Length:
2065 {
2066 int Dimension = (Present (Expressions (gnat_node))
2067 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
2068 : 1), i;
6bf68a93 2069 struct parm_attr_d *pa = NULL;
a1ab4c31 2070 Entity_Id gnat_param = Empty;
088d3b0f 2071 bool unconstrained_ptr_deref = false;
a1ab4c31 2072
bb24f343 2073 gnu_prefix = maybe_padded_object (gnu_prefix);
a1ab4c31 2074 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
ad1d36ba 2075
088d3b0f
EB
2076 /* We treat unconstrained array In parameters specially. We also note
2077 whether we are dereferencing a pointer to unconstrained array. */
2078 if (!Is_Constrained (Etype (gnat_prefix)))
2079 switch (Nkind (gnat_prefix))
2080 {
2081 case N_Identifier:
2082 /* This is the direct case. */
2083 if (Ekind (Entity (gnat_prefix)) == E_In_Parameter)
2084 gnat_param = Entity (gnat_prefix);
2085 break;
2086
2087 case N_Explicit_Dereference:
2088 /* This is the indirect case. Note that we need to be sure that
2089 the access value cannot be null as we'll hoist the load. */
2090 if (Nkind (Prefix (gnat_prefix)) == N_Identifier
2091 && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter)
2092 {
2093 if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix))))
2094 gnat_param = Entity (Prefix (gnat_prefix));
2095 }
2096 else
2097 unconstrained_ptr_deref = true;
2098 break;
2099
2100 default:
2101 break;
ad1d36ba
EB
2102 }
2103
9b17f12b
EB
2104 /* If the prefix is the view conversion of a constrained array to an
2105 unconstrained form, we retrieve the constrained array because we
2106 might not be able to substitute the PLACEHOLDER_EXPR coming from
2107 the conversion. This can occur with the 'Old attribute applied
2108 to a parameter with an unconstrained type, which gets rewritten
2109 into a constrained local variable very late in the game. */
2110 if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR
2111 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix)))
2112 && !CONTAINS_PLACEHOLDER_P
2113 (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
2114 gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0));
2115 else
2116 gnu_type = TREE_TYPE (gnu_prefix);
2117
a1ab4c31
AC
2118 prefix_unused = true;
2119 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2120
2121 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
2122 {
2123 int ndim;
2124 tree gnu_type_temp;
2125
2126 for (ndim = 1, gnu_type_temp = gnu_type;
2127 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
2128 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
2129 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
2130 ;
2131
2132 Dimension = ndim + 1 - Dimension;
2133 }
2134
2135 for (i = 1; i < Dimension; i++)
2136 gnu_type = TREE_TYPE (gnu_type);
2137
2138 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2139
2140 /* When not optimizing, look up the slot associated with the parameter
9a30c7c4
AC
2141 and the dimension in the cache and create a new one on failure.
2142 Don't do this when the actual subtype needs debug info (this happens
2143 with -gnatD): in elaborate_expression_1, we create variables that
2144 hold the bounds, so caching attributes isn't very interesting and
2145 causes dependency issues between these variables and cached
2146 expressions. */
2147 if (!optimize
2148 && Present (gnat_param)
2149 && !(Present (Actual_Subtype (gnat_param))
2150 && Needs_Debug_Info (Actual_Subtype (gnat_param))))
a1ab4c31 2151 {
9771b263 2152 FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa)
a1ab4c31
AC
2153 if (pa->id == gnat_param && pa->dim == Dimension)
2154 break;
2155
2156 if (!pa)
2157 {
766090c2 2158 pa = ggc_cleared_alloc<parm_attr_d> ();
a1ab4c31
AC
2159 pa->id = gnat_param;
2160 pa->dim = Dimension;
9771b263 2161 vec_safe_push (f_parm_attr_cache, pa);
a1ab4c31
AC
2162 }
2163 }
2164
2165 /* Return the cached expression or build a new one. */
2166 if (attribute == Attr_First)
2167 {
2168 if (pa && pa->first)
2169 {
2170 gnu_result = pa->first;
2171 break;
2172 }
2173
2174 gnu_result
2175 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2176 }
2177
2178 else if (attribute == Attr_Last)
2179 {
2180 if (pa && pa->last)
2181 {
2182 gnu_result = pa->last;
2183 break;
2184 }
2185
2186 gnu_result
2187 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2188 }
2189
2190 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
2191 {
2192 if (pa && pa->length)
2193 {
2194 gnu_result = pa->length;
2195 break;
2196 }
1eb58520
AC
2197
2198 gnu_result
2199 = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)),
2200 gnu_result_type);
a1ab4c31
AC
2201 }
2202
2203 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2204 handling. Note that these attributes could not have been used on
2205 an unconstrained array type. */
4e6602a8 2206 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
544d14e1
EB
2207 if (Present (gnat_smo))
2208 gnu_result = INSTANTIATE_LOAD_IN_EXPR (gnu_result, gnat_smo);
a1ab4c31
AC
2209
2210 /* Cache the expression we have just computed. Since we want to do it
c01fe451 2211 at run time, we force the use of a SAVE_EXPR and let the gimplifier
586fea26
EB
2212 create the temporary in the outermost binding level. We will make
2213 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
2214 paths by forcing its evaluation on entry of the function. */
a1ab4c31
AC
2215 if (pa)
2216 {
2217 gnu_result
2218 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
088d3b0f
EB
2219 switch (attribute)
2220 {
2221 case Attr_First:
2222 pa->first = gnu_result;
2223 break;
2224
2225 case Attr_Last:
2226 pa->last = gnu_result;
2227 break;
2228
2229 case Attr_Length:
2230 case Attr_Range_Length:
2231 pa->length = gnu_result;
2232 break;
2233
2234 default:
2235 gcc_unreachable ();
2236 }
a1ab4c31 2237 }
321e10dd 2238
088d3b0f
EB
2239 /* Otherwise, evaluate it each time it is referenced. */
2240 else
2241 switch (attribute)
2242 {
2243 case Attr_First:
2244 case Attr_Last:
2245 /* If we are dereferencing a pointer to unconstrained array, we
2246 need to capture the value because the pointed-to bounds may
2247 subsequently be released. */
2248 if (unconstrained_ptr_deref)
2249 gnu_result
2250 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2251 break;
2252
2253 case Attr_Length:
2254 case Attr_Range_Length:
2255 /* Set the source location onto the predicate of the condition
2256 but not if the expression is cached to avoid messing up the
2257 debug info. */
2258 if (TREE_CODE (gnu_result) == COND_EXPR
2259 && EXPR_P (TREE_OPERAND (gnu_result, 0)))
2260 set_expr_location_from_node (TREE_OPERAND (gnu_result, 0),
2261 gnat_node);
2262 break;
2263
2264 default:
2265 gcc_unreachable ();
2266 }
321e10dd 2267
a1ab4c31
AC
2268 break;
2269 }
2270
2271 case Attr_Bit_Position:
2272 case Attr_Position:
2273 case Attr_First_Bit:
2274 case Attr_Last_Bit:
2275 case Attr_Bit:
2276 {
f37fac2b
RS
2277 poly_int64 bitsize;
2278 poly_int64 bitpos;
a1ab4c31
AC
2279 tree gnu_offset;
2280 tree gnu_field_bitpos;
2281 tree gnu_field_offset;
2282 tree gnu_inner;
ef4bddc2 2283 machine_mode mode;
ee45a32d 2284 int unsignedp, reversep, volatilep;
a1ab4c31
AC
2285
2286 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2287 gnu_prefix = remove_conversions (gnu_prefix, true);
2288 prefix_unused = true;
2289
2290 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
1e17ef87 2291 the result is 0. Don't allow 'Bit on a bare component, though. */
a1ab4c31
AC
2292 if (attribute == Attr_Bit
2293 && TREE_CODE (gnu_prefix) != COMPONENT_REF
2294 && TREE_CODE (gnu_prefix) != FIELD_DECL)
2295 {
2296 gnu_result = integer_zero_node;
2297 break;
2298 }
2299
2300 else
2301 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
2302 || (attribute == Attr_Bit_Position
2303 && TREE_CODE (gnu_prefix) == FIELD_DECL));
2304
2305 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
25b75a48 2306 &mode, &unsignedp, &reversep, &volatilep);
a1ab4c31
AC
2307
2308 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
2309 {
2310 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
2311 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
2312
2313 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
2314 TREE_CODE (gnu_inner) == COMPONENT_REF
2315 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
2316 gnu_inner = TREE_OPERAND (gnu_inner, 0))
2317 {
2318 gnu_field_bitpos
2319 = size_binop (PLUS_EXPR, gnu_field_bitpos,
2320 bit_position (TREE_OPERAND (gnu_inner, 1)));
2321 gnu_field_offset
2322 = size_binop (PLUS_EXPR, gnu_field_offset,
2323 byte_position (TREE_OPERAND (gnu_inner, 1)));
2324 }
2325 }
2326 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
2327 {
2328 gnu_field_bitpos = bit_position (gnu_prefix);
2329 gnu_field_offset = byte_position (gnu_prefix);
2330 }
2331 else
2332 {
2333 gnu_field_bitpos = bitsize_zero_node;
2334 gnu_field_offset = size_zero_node;
2335 }
2336
2337 switch (attribute)
2338 {
2339 case Attr_Position:
2340 gnu_result = gnu_field_offset;
2341 break;
2342
2343 case Attr_First_Bit:
2344 case Attr_Bit:
f37fac2b 2345 gnu_result = size_int (num_trailing_bits (bitpos));
a1ab4c31
AC
2346 break;
2347
2348 case Attr_Last_Bit:
f37fac2b 2349 gnu_result = bitsize_int (num_trailing_bits (bitpos));
a1ab4c31
AC
2350 gnu_result = size_binop (PLUS_EXPR, gnu_result,
2351 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
7cc15171
EB
2352 /* ??? Avoid a large unsigned result that will overflow when
2353 converted to the signed universal_integer. */
2354 if (integer_zerop (gnu_result))
2355 gnu_result = integer_minus_one_node;
2356 else
2357 gnu_result
2358 = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node);
a1ab4c31
AC
2359 break;
2360
2361 case Attr_Bit_Position:
2362 gnu_result = gnu_field_bitpos;
2363 break;
7cc15171 2364 }
a1ab4c31 2365
feec4372
EB
2366 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2367 handling. */
a1ab4c31 2368 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
544d14e1
EB
2369 if (Present (gnat_smo))
2370 gnu_result = INSTANTIATE_LOAD_IN_EXPR (gnu_result, gnat_smo);
a1ab4c31
AC
2371 break;
2372 }
2373
2374 case Attr_Min:
2375 case Attr_Max:
2376 {
2377 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
2378 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
2379
2380 gnu_result_type = get_unpadded_type (Etype (gnat_node));
881cdd61
EB
2381
2382 /* The result of {MIN,MAX}_EXPR is unspecified if either operand is
2383 a NaN so we implement the semantics of C99 f{min,max} to make it
2384 predictable in this case: if either operand is a NaN, the other
2385 is returned; if both operands are NaN's, a NaN is returned. */
89d5c50b
EB
2386 if (SCALAR_FLOAT_TYPE_P (gnu_result_type)
2387 && !Machine_Overflows_On_Target)
881cdd61
EB
2388 {
2389 const bool lhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_lhs);
2390 const bool rhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_rhs);
2391 tree t = builtin_decl_explicit (BUILT_IN_ISNAN);
2392 tree lhs_is_nan, rhs_is_nan;
2393
2394 /* If the operands have side-effects, they need to be evaluated
2395 only once in spite of the multiple references in the result. */
2396 if (lhs_side_effects_p)
2397 gnu_lhs = gnat_protect_expr (gnu_lhs);
2398 if (rhs_side_effects_p)
2399 gnu_rhs = gnat_protect_expr (gnu_rhs);
2400
2401 lhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2402 build_call_expr (t, 1, gnu_lhs),
2403 integer_zero_node);
2404
2405 rhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2406 build_call_expr (t, 1, gnu_rhs),
2407 integer_zero_node);
2408
2409 gnu_result = build_binary_op (attribute == Attr_Min
2410 ? MIN_EXPR : MAX_EXPR,
2411 gnu_result_type, gnu_lhs, gnu_rhs);
2412 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2413 rhs_is_nan, gnu_lhs, gnu_result);
2414 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2415 lhs_is_nan, gnu_rhs, gnu_result);
2416
2417 /* If the operands have side-effects, they need to be evaluated
2418 before doing the tests above since the place they otherwise
2419 would end up being evaluated at run time could be wrong. */
2420 if (lhs_side_effects_p)
2421 gnu_result
2422 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_lhs, gnu_result);
2423
2424 if (rhs_side_effects_p)
2425 gnu_result
2426 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_rhs, gnu_result);
2427 }
2428 else
2429 gnu_result = build_binary_op (attribute == Attr_Min
2430 ? MIN_EXPR : MAX_EXPR,
2431 gnu_result_type, gnu_lhs, gnu_rhs);
a1ab4c31
AC
2432 }
2433 break;
2434
2435 case Attr_Passed_By_Reference:
2436 gnu_result = size_int (default_pass_by_ref (gnu_type)
2437 || must_pass_by_ref (gnu_type));
2438 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2439 break;
2440
2441 case Attr_Component_Size:
bb24f343 2442 gnu_prefix = maybe_padded_object (gnu_prefix);
a1ab4c31
AC
2443 gnu_type = TREE_TYPE (gnu_prefix);
2444
2445 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
2446 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
2447
2448 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2449 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2450 gnu_type = TREE_TYPE (gnu_type);
2451
2452 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2453
2454 /* Note this size cannot be self-referential. */
2455 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
2456 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2457 prefix_unused = true;
2458 break;
2459
203ddcea
AC
2460 case Attr_Descriptor_Size:
2461 gnu_type = TREE_TYPE (gnu_prefix);
2462 gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE);
2463
65e0a92b 2464 /* Return the padded size of the template in the object record type. */
203ddcea 2465 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
2b45154d 2466 gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
203ddcea
AC
2467 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2468 prefix_unused = true;
2469 break;
2470
a1ab4c31 2471 case Attr_Null_Parameter:
feec4372
EB
2472 /* This is just a zero cast to the pointer type for our prefix and
2473 dereferenced. */
a1ab4c31
AC
2474 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2475 gnu_result
2476 = build_unary_op (INDIRECT_REF, NULL_TREE,
2477 convert (build_pointer_type (gnu_result_type),
2478 integer_zero_node));
a1ab4c31
AC
2479 break;
2480
2481 case Attr_Mechanism_Code:
2482 {
088d3b0f 2483 Entity_Id gnat_obj = Entity (gnat_prefix);
a1ab4c31 2484 int code;
a1ab4c31
AC
2485
2486 prefix_unused = true;
2487 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2488 if (Present (Expressions (gnat_node)))
2489 {
2490 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
2491
2492 for (gnat_obj = First_Formal (gnat_obj); i > 1;
2493 i--, gnat_obj = Next_Formal (gnat_obj))
2494 ;
2495 }
2496
2497 code = Mechanism (gnat_obj);
2498 if (code == Default)
2499 code = ((present_gnu_tree (gnat_obj)
2500 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
2501 || ((TREE_CODE (get_gnu_tree (gnat_obj))
2502 == PARM_DECL)
2503 && (DECL_BY_COMPONENT_PTR_P
2504 (get_gnu_tree (gnat_obj))))))
2505 ? By_Reference : By_Copy);
2506 gnu_result = convert (gnu_result_type, size_int (- code));
2507 }
2508 break;
2509
24228312
AC
2510 case Attr_Model:
2511 /* We treat Model as identical to Machine. This is true for at least
2512 IEEE and some other nice floating-point systems. */
2513
9c453de7 2514 /* ... fall through ... */
24228312
AC
2515
2516 case Attr_Machine:
2517 /* The trick is to force the compiler to store the result in memory so
2518 that we do not have extra precision used. But do this only when this
1eb58520
AC
2519 is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of
2520 the type is lower than that of the longest floating-point type. */
24228312
AC
2521 prefix_unused = true;
2522 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2523 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2524 gnu_result = convert (gnu_result_type, gnu_expr);
2525
c68cdfac
EB
2526 if (TREE_CODE (gnu_result) != REAL_CST
2527 && fp_arith_may_widen
1eb58520
AC
2528 && TYPE_PRECISION (gnu_result_type)
2529 < TYPE_PRECISION (longest_float_type_node))
24228312
AC
2530 {
2531 tree rec_type = make_node (RECORD_TYPE);
2532 tree field
2533 = create_field_decl (get_identifier ("OBJ"), gnu_result_type,
2534 rec_type, NULL_TREE, NULL_TREE, 0, 0);
2535 tree rec_val, asm_expr;
2536
2537 finish_record_type (rec_type, field, 0, false);
2538
2539 rec_val = build_constructor_single (rec_type, field, gnu_result);
c68cdfac 2540 rec_val = build1 (SAVE_EXPR, rec_type, rec_val);
24228312
AC
2541
2542 asm_expr
2543 = build5 (ASM_EXPR, void_type_node,
2544 build_string (0, ""),
2545 tree_cons (build_tree_list (NULL_TREE,
2546 build_string (2, "=m")),
2547 rec_val, NULL_TREE),
2548 tree_cons (build_tree_list (NULL_TREE,
2549 build_string (1, "m")),
2550 rec_val, NULL_TREE),
2551 NULL_TREE, NULL_TREE);
2552 ASM_VOLATILE_P (asm_expr) = 1;
2553
2554 gnu_result
2555 = build_compound_expr (gnu_result_type, asm_expr,
64235766
EB
2556 build_component_ref (rec_val, field,
2557 false));
24228312
AC
2558 }
2559 break;
2560
06d75031
EB
2561 case Attr_Deref:
2562 prefix_unused = true;
2563 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2564 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2565 /* This can be a random address so build an alias-all pointer type. */
2566 gnu_expr
2567 = convert (build_pointer_type_for_mode (gnu_result_type, ptr_mode,
2568 true),
2569 gnu_expr);
2570 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_expr);
2571 break;
2572
a1ab4c31 2573 default:
010dffa9
EB
2574 /* This abort means that we have an unimplemented attribute. */
2575 gcc_unreachable ();
a1ab4c31
AC
2576 }
2577
2578 /* If this is an attribute where the prefix was unused, force a use of it if
2579 it has a side-effect. But don't do it if the prefix is just an entity
2580 name. However, if an access check is needed, we must do it. See second
1e17ef87 2581 example in AARM 11.6(5.e). */
088d3b0f
EB
2582 if (prefix_unused
2583 && TREE_SIDE_EFFECTS (gnu_prefix)
2584 && !Is_Entity_Name (gnat_prefix))
2585 gnu_result
24228312 2586 = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result);
a1ab4c31
AC
2587
2588 *gnu_result_type_p = gnu_result_type;
2589 return gnu_result;
2590}
ce2d0ce2 2591
e08f1aad
EB
2592/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Case_Statement, to a
2593 GCC tree, which is returned. */
a1ab4c31
AC
2594
2595static tree
2596Case_Statement_to_gnu (Node_Id gnat_node)
2597{
a31d78c6 2598 tree gnu_result, gnu_expr, gnu_type, gnu_label;
a1ab4c31 2599 Node_Id gnat_when;
2d3c7e4f 2600 location_t end_locus;
83e279c4 2601 bool may_fallthru = false;
a1ab4c31
AC
2602
2603 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2604 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
a31d78c6
EB
2605 gnu_expr = maybe_character_value (gnu_expr);
2606 gnu_type = TREE_TYPE (gnu_expr);
a1ab4c31 2607
a1ab4c31
AC
2608 /* We build a SWITCH_EXPR that contains the code with interspersed
2609 CASE_LABEL_EXPRs for each label. */
08b8b90c 2610 if (!Sloc_to_locus (End_Location (gnat_node), &end_locus))
2d3c7e4f
EB
2611 end_locus = input_location;
2612 gnu_label = create_artificial_label (end_locus);
a1ab4c31 2613 start_stmt_group ();
b4f73deb 2614
a1ab4c31
AC
2615 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2616 Present (gnat_when);
2617 gnat_when = Next_Non_Pragma (gnat_when))
2618 {
9c69c3af 2619 bool choices_added_p = false;
a1ab4c31 2620 Node_Id gnat_choice;
a1ab4c31
AC
2621
2622 /* First compile all the different case choices for the current WHEN
2623 alternative. */
2624 for (gnat_choice = First (Discrete_Choices (gnat_when));
ac43e11e
AC
2625 Present (gnat_choice);
2626 gnat_choice = Next (gnat_choice))
a1ab4c31
AC
2627 {
2628 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
ac43e11e 2629 tree label = create_artificial_label (input_location);
a1ab4c31
AC
2630
2631 switch (Nkind (gnat_choice))
2632 {
2633 case N_Range:
2634 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
2635 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
2636 break;
2637
2638 case N_Subtype_Indication:
2639 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
2640 (Constraint (gnat_choice))));
2641 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
2642 (Constraint (gnat_choice))));
2643 break;
2644
2645 case N_Identifier:
2646 case N_Expanded_Name:
2647 /* This represents either a subtype range or a static value of
2648 some kind; Ekind says which. */
7ed9919d 2649 if (Is_Type (Entity (gnat_choice)))
a1ab4c31
AC
2650 {
2651 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
2652
ac43e11e
AC
2653 gnu_low = TYPE_MIN_VALUE (gnu_type);
2654 gnu_high = TYPE_MAX_VALUE (gnu_type);
a1ab4c31
AC
2655 break;
2656 }
2657
9c453de7 2658 /* ... fall through ... */
a1ab4c31
AC
2659
2660 case N_Character_Literal:
2661 case N_Integer_Literal:
2662 gnu_low = gnat_to_gnu (gnat_choice);
2663 break;
2664
2665 case N_Others_Choice:
2666 break;
2667
2668 default:
2669 gcc_unreachable ();
2670 }
2671
ac43e11e
AC
2672 /* Everything should be folded into constants at this point. */
2673 gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST);
2674 gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST);
2675
a31d78c6
EB
2676 if (gnu_low && TREE_TYPE (gnu_low) != gnu_type)
2677 gnu_low = convert (gnu_type, gnu_low);
2678 if (gnu_high && TREE_TYPE (gnu_high) != gnu_type)
2679 gnu_high = convert (gnu_type, gnu_high);
2680
ac43e11e
AC
2681 add_stmt_with_node (build_case_label (gnu_low, gnu_high, label),
2682 gnat_choice);
2683 choices_added_p = true;
a1ab4c31
AC
2684 }
2685
a1d7a124
EB
2686 /* This construct doesn't define a scope so we shouldn't push a binding
2687 level around the statement list. Except that we have always done so
2688 historically and this makes it possible to reduce stack usage. As a
2689 compromise, we keep doing it for case statements, for which this has
2690 never been problematic, but not for case expressions in Ada 2012. */
9c69c3af 2691 if (choices_added_p)
a1ab4c31 2692 {
a1d7a124
EB
2693 const bool is_case_expression
2694 = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions);
2695 tree group
2696 = build_stmt_group (Statements (gnat_when), !is_case_expression);
83e279c4
EB
2697 bool group_may_fallthru = block_may_fallthru (group);
2698 add_stmt (group);
2699 if (group_may_fallthru)
2700 {
2d3c7e4f
EB
2701 tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label);
2702 SET_EXPR_LOCATION (stmt, end_locus);
2703 add_stmt (stmt);
83e279c4
EB
2704 may_fallthru = true;
2705 }
a1ab4c31
AC
2706 }
2707 }
2708
41a961e9 2709 /* Now emit a definition of the label the cases branch to, if any. */
83e279c4
EB
2710 if (may_fallthru)
2711 add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label));
9e851845 2712 gnu_result = build2 (SWITCH_EXPR, gnu_type, gnu_expr, end_stmt_group ());
a1ab4c31
AC
2713
2714 return gnu_result;
2715}
ce2d0ce2 2716
87ab2b04
EB
2717/* Return true if we are in the body of a loop. */
2718
2719static inline bool
2720inside_loop_p (void)
2721{
2722 return !vec_safe_is_empty (gnu_loop_stack);
2723}
2724
933a7325
EB
2725/* Find out whether EXPR is a simple additive expression based on the iteration
2726 variable of some enclosing loop in the current function. If so, return the
2727 loop and set *DISP to the displacement and *NEG_P to true if this is for a
2728 subtraction; otherwise, return NULL. */
15bf7d19 2729
87ab2b04 2730static struct loop_info_d *
22903f22 2731find_loop_for (tree expr, tree *disp, bool *neg_p)
15bf7d19 2732{
933a7325
EB
2733 tree var, add, cst;
2734 bool minus_p;
15bf7d19
EB
2735 struct loop_info_d *iter = NULL;
2736 unsigned int i;
2737
933a7325
EB
2738 if (is_simple_additive_expression (expr, &add, &cst, &minus_p))
2739 {
2740 var = add;
2741 if (disp)
2742 *disp = cst;
2743 if (neg_p)
2744 *neg_p = minus_p;
2745 }
2746 else
2747 {
2748 var = expr;
2749 if (disp)
2750 *disp = NULL_TREE;
2751 if (neg_p)
2752 *neg_p = false;
2753 }
2754
722356ce 2755 var = remove_conversions (var, false);
15bf7d19
EB
2756
2757 if (TREE_CODE (var) != VAR_DECL)
2758 return NULL;
2759
852dd866
EB
2760 gcc_checking_assert (vec_safe_length (gnu_loop_stack) > 0);
2761
2762 FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack, i, iter)
2763 if (iter->loop_var == var && iter->fndecl == current_function_decl)
2764 break;
2765
2766 return iter;
2767}
15bf7d19 2768
852dd866
EB
2769/* Return the innermost enclosing loop in the current function. */
2770
2771static struct loop_info_d *
2772find_loop (void)
2773{
2774 struct loop_info_d *iter = NULL;
2775 unsigned int i;
2776
2777 gcc_checking_assert (vec_safe_length (gnu_loop_stack) > 0);
90b4c164 2778
547bbe49 2779 FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack, i, iter)
852dd866 2780 if (iter->fndecl == current_function_decl)
15bf7d19
EB
2781 break;
2782
87ab2b04 2783 return iter;
15bf7d19
EB
2784}
2785
d88bbbb9
EB
2786/* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2787 false, or the maximum value if MAX is true, of TYPE. */
2788
2789static bool
2790can_equal_min_or_max_val_p (tree val, tree type, bool max)
2791{
2792 tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
2793
2794 if (TREE_CODE (min_or_max_val) != INTEGER_CST)
2795 return true;
2796
2797 if (TREE_CODE (val) == NOP_EXPR)
2798 val = (max
2799 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))
2800 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))));
2801
2802 if (TREE_CODE (val) != INTEGER_CST)
2803 return true;
2804
2efdbf0f
EB
2805 if (max)
2806 return tree_int_cst_lt (val, min_or_max_val) == 0;
2807 else
2808 return tree_int_cst_lt (min_or_max_val, val) == 0;
d88bbbb9
EB
2809}
2810
2811/* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2812 If REVERSE is true, minimum value is taken as maximum value. */
2813
2814static inline bool
2815can_equal_min_val_p (tree val, tree type, bool reverse)
2816{
2817 return can_equal_min_or_max_val_p (val, type, reverse);
2818}
2819
2820/* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2821 If REVERSE is true, maximum value is taken as minimum value. */
2822
2823static inline bool
2824can_equal_max_val_p (tree val, tree type, bool reverse)
2825{
2826 return can_equal_min_or_max_val_p (val, type, !reverse);
2827}
2828
64235766
EB
2829/* Replace EXPR1 and EXPR2 by invariant expressions if possible. Return
2830 true if both expressions have been replaced and false otherwise. */
2831
2832static bool
2833make_invariant (tree *expr1, tree *expr2)
2834{
2835 tree inv_expr1 = gnat_invariant_expr (*expr1);
2836 tree inv_expr2 = gnat_invariant_expr (*expr2);
2837
2838 if (inv_expr1)
2839 *expr1 = inv_expr1;
2840
2841 if (inv_expr2)
2842 *expr2 = inv_expr2;
2843
2844 return inv_expr1 && inv_expr2;
2845}
2846
1ddde8dc
EB
2847/* Helper function for walk_tree, used by independent_iterations_p below. */
2848
2849static tree
2850scan_rhs_r (tree *tp, int *walk_subtrees, void *data)
2851{
2852 bitmap *params = (bitmap *)data;
2853 tree t = *tp;
2854
2855 /* No need to walk into types or decls. */
2856 if (IS_TYPE_OR_DECL_P (t))
2857 *walk_subtrees = 0;
2858
2859 if (TREE_CODE (t) == PARM_DECL && bitmap_bit_p (*params, DECL_UID (t)))
2860 return t;
2861
2862 return NULL_TREE;
2863}
2864
2865/* Return true if STMT_LIST generates independent iterations in a loop. */
2866
2867static bool
2868independent_iterations_p (tree stmt_list)
2869{
2870 tree_stmt_iterator tsi;
2871 bitmap params = BITMAP_GGC_ALLOC();
b9778c45 2872 auto_vec<tree, 16> rhs;
1ddde8dc
EB
2873 tree iter;
2874 int i;
2875
2876 if (TREE_CODE (stmt_list) == BIND_EXPR)
2877 stmt_list = BIND_EXPR_BODY (stmt_list);
2878
2879 /* Scan the list and return false on anything that is not either a check
2880 or an assignment to a parameter with restricted aliasing. */
2881 for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi))
2882 {
2883 tree stmt = tsi_stmt (tsi);
2884
2885 switch (TREE_CODE (stmt))
2886 {
2887 case COND_EXPR:
2888 {
2889 if (COND_EXPR_ELSE (stmt))
2890 return false;
2891 if (TREE_CODE (COND_EXPR_THEN (stmt)) != CALL_EXPR)
2892 return false;
2893 tree func = get_callee_fndecl (COND_EXPR_THEN (stmt));
2894 if (!(func && TREE_THIS_VOLATILE (func)))
2895 return false;
2896 break;
2897 }
2898
2899 case MODIFY_EXPR:
2900 {
2901 tree lhs = TREE_OPERAND (stmt, 0);
2902 while (handled_component_p (lhs))
2903 lhs = TREE_OPERAND (lhs, 0);
2904 if (TREE_CODE (lhs) != INDIRECT_REF)
2905 return false;
2906 lhs = TREE_OPERAND (lhs, 0);
2907 if (!(TREE_CODE (lhs) == PARM_DECL
2908 && DECL_RESTRICTED_ALIASING_P (lhs)))
2909 return false;
2910 bitmap_set_bit (params, DECL_UID (lhs));
2911 rhs.safe_push (TREE_OPERAND (stmt, 1));
2912 break;
2913 }
2914
2915 default:
2916 return false;
2917 }
2918 }
2919
2920 /* At this point we know that the list contains only statements that will
2921 modify parameters with restricted aliasing. Check that the statements
2922 don't at the time read from these parameters. */
2923 FOR_EACH_VEC_ELT (rhs, i, iter)
2924 if (walk_tree_without_duplicates (&iter, scan_rhs_r, &params))
2925 return false;
2926
2927 return true;
2928}
2929
e08f1aad
EB
2930/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Loop_Statement, to a
2931 GCC tree, which is returned. */
7beb2b9d
OH
2932
2933static tree
852dd866 2934Loop_Statement_to_gnu (Node_Id gnat_node)
7beb2b9d 2935{
7beb2b9d 2936 const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
852dd866
EB
2937 struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> ();
2938 tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE,
2939 NULL_TREE, NULL_TREE, NULL_TREE);
2940 tree gnu_loop_label = create_artificial_label (input_location);
2941 tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2942 tree gnu_result;
2943
2944 /* Push the loop_info structure associated with the LOOP_STMT. */
2945 gnu_loop_info->fndecl = current_function_decl;
2946 gnu_loop_info->stmt = gnu_loop_stmt;
2947 vec_safe_push (gnu_loop_stack, gnu_loop_info);
2948
2949 /* Set location information for statement and end label. */
2950 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
2951 Sloc_to_locus (Sloc (End_Label (gnat_node)),
2952 &DECL_SOURCE_LOCATION (gnu_loop_label));
2953 LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label;
7beb2b9d 2954
93101d23
EB
2955 /* Set the condition under which the loop must keep going. If we have an
2956 explicit condition, use it to set the location information throughout
2957 the translation of the loop statement to avoid having multiple SLOCs.
a1ab4c31 2958
7fda1596 2959 For the case "LOOP .... END LOOP;" the condition is always true. */
a1ab4c31
AC
2960 if (No (gnat_iter_scheme))
2961 ;
7fda1596
EB
2962
2963 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
a1ab4c31 2964 else if (Present (Condition (gnat_iter_scheme)))
93101d23
EB
2965 {
2966 LOOP_STMT_COND (gnu_loop_stmt)
2967 = gnat_to_gnu (Condition (gnat_iter_scheme));
2968
2969 set_expr_location_from_node (gnu_loop_stmt, gnat_iter_scheme);
2970 }
7fda1596 2971
58c8f770
EB
2972 /* Otherwise we have an iteration scheme and the condition is given by the
2973 bounds of the subtype of the iteration variable. */
a1ab4c31
AC
2974 else
2975 {
a1ab4c31
AC
2976 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
2977 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2978 Entity_Id gnat_type = Etype (gnat_loop_var);
2979 tree gnu_type = get_unpadded_type (gnat_type);
825da0d2 2980 tree gnu_base_type = maybe_character_type (get_base_type (gnu_type));
9a1bdc31 2981 tree gnu_one_node = build_int_cst (gnu_base_type, 1);
6162cec0 2982 tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt;
d88bbbb9 2983 enum tree_code update_code, test_code, shift_code;
6162cec0 2984 bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false;
82d3b03a 2985
1eb58520
AC
2986 gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type));
2987 gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type));
15bf7d19 2988
58c8f770 2989 /* We must disable modulo reduction for the iteration variable, if any,
82d3b03a 2990 in order for the loop comparison to be effective. */
d88bbbb9 2991 if (reverse)
82d3b03a
EB
2992 {
2993 gnu_first = gnu_high;
2994 gnu_last = gnu_low;
2995 update_code = MINUS_NOMOD_EXPR;
58c8f770 2996 test_code = GE_EXPR;
d88bbbb9 2997 shift_code = PLUS_NOMOD_EXPR;
82d3b03a
EB
2998 }
2999 else
3000 {
3001 gnu_first = gnu_low;
3002 gnu_last = gnu_high;
3003 update_code = PLUS_NOMOD_EXPR;
58c8f770 3004 test_code = LE_EXPR;
d88bbbb9
EB
3005 shift_code = MINUS_NOMOD_EXPR;
3006 }
3007
3008 /* We use two different strategies to translate the loop, depending on
3009 whether optimization is enabled.
3010
6162cec0
EB
3011 If it is, we generate the canonical loop form expected by the loop
3012 optimizer and the loop vectorizer, which is the do-while form:
d88bbbb9
EB
3013
3014 ENTRY_COND
3015 loop:
3016 TOP_UPDATE
3017 BODY
3018 BOTTOM_COND
3019 GOTO loop
3020
6162cec0
EB
3021 This avoids an implicit dependency on loop header copying and makes
3022 it possible to turn BOTTOM_COND into an inequality test.
3023
3024 If optimization is disabled, loop header copying doesn't come into
3025 play and we try to generate the loop form with the fewer conditional
3026 branches. First, the default form, which is:
d88bbbb9
EB
3027
3028 loop:
3029 TOP_COND
3030 BODY
3031 BOTTOM_UPDATE
3032 GOTO loop
3033
6162cec0
EB
3034 It should catch most loops with constant ending point. Then, if we
3035 cannot, we try to generate the shifted form:
d88bbbb9 3036
d88bbbb9 3037 loop:
6162cec0
EB
3038 TOP_COND
3039 TOP_UPDATE
d88bbbb9 3040 BODY
d88bbbb9
EB
3041 GOTO loop
3042
6162cec0
EB
3043 which should catch loops with constant starting point. Otherwise, if
3044 we cannot, we generate the fallback form:
d88bbbb9 3045
6162cec0 3046 ENTRY_COND
d88bbbb9 3047 loop:
d88bbbb9 3048 BODY
6162cec0
EB
3049 BOTTOM_COND
3050 BOTTOM_UPDATE
d88bbbb9
EB
3051 GOTO loop
3052
6162cec0 3053 which works in all cases. */
d88bbbb9 3054
09248547 3055 if (optimize && !optimize_debug)
d88bbbb9 3056 {
6162cec0
EB
3057 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
3058 overflow. */
d88bbbb9 3059 if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse))
d88bbbb9
EB
3060 ;
3061
6162cec0 3062 /* Otherwise, use the do-while form with the help of a special
15bf7d19 3063 induction variable in the unsigned version of the base type
48d3d75d 3064 or the unsigned version of the size type, whichever is the
15bf7d19 3065 largest, in order to have wrap-around arithmetics for it. */
d88bbbb9 3066 else
6162cec0 3067 {
48d3d75d
EB
3068 if (TYPE_PRECISION (gnu_base_type)
3069 > TYPE_PRECISION (size_type_node))
00a22e5e
EB
3070 gnu_base_type
3071 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1);
15bf7d19 3072 else
48d3d75d 3073 gnu_base_type = size_type_node;
15bf7d19
EB
3074
3075 gnu_first = convert (gnu_base_type, gnu_first);
3076 gnu_last = convert (gnu_base_type, gnu_last);
9a1bdc31 3077 gnu_one_node = build_int_cst (gnu_base_type, 1);
6162cec0
EB
3078 use_iv = true;
3079 }
3080
3081 gnu_first
3082 = build_binary_op (shift_code, gnu_base_type, gnu_first,
3083 gnu_one_node);
3084 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
3085 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
d88bbbb9
EB
3086 }
3087 else
3088 {
3089 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
3090 if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse))
3091 ;
3092
3093 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
3094 GNU_LAST-1 does. */
3095 else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)
3096 && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse))
3097 {
6162cec0
EB
3098 gnu_first
3099 = build_binary_op (shift_code, gnu_base_type, gnu_first,
3100 gnu_one_node);
3101 gnu_last
3102 = build_binary_op (shift_code, gnu_base_type, gnu_last,
3103 gnu_one_node);
d88bbbb9
EB
3104 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
3105 }
3106
3107 /* Otherwise, use the fallback form. */
3108 else
6162cec0 3109 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
82d3b03a 3110 }
a1ab4c31 3111
d88bbbb9 3112 /* If we use the BOTTOM_COND, we can turn the test into an inequality
ec1b8711 3113 test but we have to add ENTRY_COND to protect the empty loop. */
d88bbbb9 3114 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt))
a1ab4c31 3115 {
d88bbbb9 3116 test_code = NE_EXPR;
ec1b8711
EB
3117 gnu_cond_expr
3118 = build3 (COND_EXPR, void_type_node,
3119 build_binary_op (LE_EXPR, boolean_type_node,
3120 gnu_low, gnu_high),
3121 NULL_TREE, alloc_stmt_list ());
3122 set_expr_location_from_node (gnu_cond_expr, gnat_iter_scheme);
a1ab4c31
AC
3123 }
3124
3125 /* Open a new nesting level that will surround the loop to declare the
58c8f770 3126 iteration variable. */
a1ab4c31
AC
3127 start_stmt_group ();
3128 gnat_pushlevel ();
3129
6162cec0
EB
3130 /* If we use the special induction variable, create it and set it to
3131 its initial value. Morever, the regular iteration variable cannot
3132 itself be initialized, lest the initial value wrapped around. */
3133 if (use_iv)
3134 {
3135 gnu_loop_iv
3136 = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var);
3137 add_stmt (gnu_stmt);
3138 gnu_first = NULL_TREE;
3139 }
3140 else
3141 gnu_loop_iv = NULL_TREE;
3142
58c8f770 3143 /* Declare the iteration variable and set it to its initial value. */
afc737f0 3144 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, true);
a1ab4c31
AC
3145 if (DECL_BY_REF_P (gnu_loop_var))
3146 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
15bf7d19
EB
3147 else if (use_iv)
3148 {
3149 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var));
3150 SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv);
3151 }
3152 gnu_loop_info->loop_var = gnu_loop_var;
87ab2b04
EB
3153 gnu_loop_info->low_bound = gnu_low;
3154 gnu_loop_info->high_bound = gnu_high;
a1ab4c31 3155
58c8f770
EB
3156 /* Do all the arithmetics in the base type. */
3157 gnu_loop_var = convert (gnu_base_type, gnu_loop_var);
a1ab4c31 3158
d88bbbb9 3159 /* Set either the top or bottom exit condition. */
6162cec0
EB
3160 if (use_iv)
3161 LOOP_STMT_COND (gnu_loop_stmt)
3162 = build_binary_op (test_code, boolean_type_node, gnu_loop_iv,
3163 gnu_last);
3164 else
3165 LOOP_STMT_COND (gnu_loop_stmt)
3166 = build_binary_op (test_code, boolean_type_node, gnu_loop_var,
3167 gnu_last);
a1ab4c31 3168
d88bbbb9
EB
3169 /* Set either the top or bottom update statement and give it the source
3170 location of the iteration for better coverage info. */
6162cec0
EB
3171 if (use_iv)
3172 {
3173 gnu_stmt
3174 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv,
3175 build_binary_op (update_code, gnu_base_type,
3176 gnu_loop_iv, gnu_one_node));
3177 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
3178 append_to_statement_list (gnu_stmt,
3179 &LOOP_STMT_UPDATE (gnu_loop_stmt));
3180 gnu_stmt
3181 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
3182 gnu_loop_iv);
3183 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
3184 append_to_statement_list (gnu_stmt,
3185 &LOOP_STMT_UPDATE (gnu_loop_stmt));
3186 }
3187 else
3188 {
3189 gnu_stmt
3190 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
3191 build_binary_op (update_code, gnu_base_type,
3192 gnu_loop_var, gnu_one_node));
3193 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
3194 LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt;
3195 }
93101d23
EB
3196
3197 set_expr_location_from_node (gnu_loop_stmt, gnat_iter_scheme);
a1ab4c31
AC
3198 }
3199
3200 /* If the loop was named, have the name point to this loop. In this case,
58c8f770 3201 the association is not a DECL node, but the end label of the loop. */
a1ab4c31 3202 if (Present (Identifier (gnat_node)))
58c8f770 3203 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true);
a1ab4c31
AC
3204
3205 /* Make the loop body into its own block, so any allocated storage will be
3206 released every iteration. This is needed for stack allocation. */
3207 LOOP_STMT_BODY (gnu_loop_stmt)
3208 = build_stmt_group (Statements (gnat_node), true);
58c8f770 3209 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
a1ab4c31 3210
6162cec0
EB
3211 /* If we have an iteration scheme, then we are in a statement group. Add
3212 the LOOP_STMT to it, finish it and make it the "loop". */
3213 if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme)))
a1ab4c31 3214 {
64235766
EB
3215 /* First, if we have computed invariant conditions for range (or index)
3216 checks applied to the iteration variable, find out whether they can
3217 be evaluated to false at compile time; otherwise, if there are not
3218 too many of them, combine them with the original checks. If loop
3219 unswitching is enabled, do not require the loop bounds to be also
3220 invariant, as their evaluation will still be ahead of the loop. */
3221 if (vec_safe_length (gnu_loop_info->checks) > 0
3bdf0b64 3222 && (make_invariant (&gnu_low, &gnu_high) || optimize >= 3))
64235766
EB
3223 {
3224 struct range_check_info_d *rci;
3225 unsigned int i, n_remaining_checks = 0;
3226
3227 FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci)
3228 {
933a7325
EB
3229 tree low_ok, high_ok;
3230
3231 if (rci->low_bound)
3232 {
3233 tree gnu_adjusted_low = convert (rci->type, gnu_low);
3234 if (rci->disp)
3235 gnu_adjusted_low
3236 = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR,
3237 rci->type, gnu_adjusted_low, rci->disp);
3238 low_ok
3239 = build_binary_op (GE_EXPR, boolean_type_node,
3240 gnu_adjusted_low, rci->low_bound);
3241 }
3242 else
3243 low_ok = boolean_true_node;
3244
3245 if (rci->high_bound)
3246 {
3247 tree gnu_adjusted_high = convert (rci->type, gnu_high);
3248 if (rci->disp)
3249 gnu_adjusted_high
3250 = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR,
3251 rci->type, gnu_adjusted_high, rci->disp);
3252 high_ok
3253 = build_binary_op (LE_EXPR, boolean_type_node,
3254 gnu_adjusted_high, rci->high_bound);
3255 }
3256 else
3257 high_ok = boolean_true_node;
64235766
EB
3258
3259 tree range_ok
3260 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
3261 low_ok, high_ok);
3262
3263 rci->invariant_cond
3264 = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok);
3265
3266 if (rci->invariant_cond == boolean_false_node)
3267 TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond;
3268 else
3269 n_remaining_checks++;
3270 }
3271
3272 /* Note that loop unswitching can only be applied a small number of
3273 times to a given loop (PARAM_MAX_UNSWITCH_LEVEL default to 3). */
278f422c 3274 if (IN_RANGE (n_remaining_checks, 1, 3)
3bdf0b64 3275 && optimize >= 2
278f422c 3276 && !optimize_size)
64235766
EB
3277 FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci)
3278 if (rci->invariant_cond != boolean_false_node)
3279 {
3280 TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond;
3281
3bdf0b64 3282 if (optimize >= 3)
64235766
EB
3283 add_stmt_with_node_force (rci->inserted_cond, gnat_node);
3284 }
3285 }
15bf7d19 3286
852dd866
EB
3287 /* Second, if we have recorded invariants to be hoisted, emit them. */
3288 if (vec_safe_length (gnu_loop_info->invariants) > 0)
3289 {
3290 tree *iter;
3291 unsigned int i;
3292 FOR_EACH_VEC_ELT (*gnu_loop_info->invariants, i, iter)
3293 add_stmt_with_node_force (*iter, gnat_node);
3294 }
3295
3296 /* Third, if loop vectorization is enabled and the iterations of the
1ddde8dc 3297 loop can easily be proved as independent, mark the loop. */
3bdf0b64 3298 if (optimize >= 3
1ddde8dc
EB
3299 && independent_iterations_p (LOOP_STMT_BODY (gnu_loop_stmt)))
3300 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
3301
a1ab4c31
AC
3302 add_stmt (gnu_loop_stmt);
3303 gnat_poplevel ();
3304 gnu_loop_stmt = end_stmt_group ();
3305 }
3306
3307 /* If we have an outer COND_EXPR, that's our result and this loop is its
7fda1596 3308 "true" statement. Otherwise, the result is the LOOP_STMT. */
a1ab4c31
AC
3309 if (gnu_cond_expr)
3310 {
3311 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
66c14933 3312 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
a1ab4c31 3313 gnu_result = gnu_cond_expr;
a1ab4c31
AC
3314 }
3315 else
3316 gnu_result = gnu_loop_stmt;
3317
9771b263 3318 gnu_loop_stack->pop ();
a1ab4c31
AC
3319
3320 return gnu_result;
3321}
ce2d0ce2 3322
17a98a3d 3323/* This page implements a form of Named Return Value optimization modeled
71196d4e
EB
3324 on the C++ optimization of the same name. The main difference is that
3325 we disregard any semantical considerations when applying it here, the
3326 counterpart being that we don't try to apply it to semantically loaded
a0b8b1b7 3327 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
71196d4e
EB
3328
3329 We consider a function body of the following GENERIC form:
3330
3331 return_type R1;
3332 [...]
3333 RETURN_EXPR [<retval> = ...]
3334 [...]
3335 RETURN_EXPR [<retval> = R1]
3336 [...]
3337 return_type Ri;
3338 [...]
3339 RETURN_EXPR [<retval> = ...]
3340 [...]
3341 RETURN_EXPR [<retval> = Ri]
3342 [...]
3343
13b6b773
EB
3344 where the Ri are not addressable and we try to fulfill a simple criterion
3345 that would make it possible to replace one or several Ri variables by the
3346 single RESULT_DECL of the function.
71196d4e
EB
3347
3348 The first observation is that RETURN_EXPRs that don't directly reference
3349 any of the Ri variables on the RHS of their assignment are transparent wrt
3350 the optimization. This is because the Ri variables aren't addressable so
3351 any transformation applied to them doesn't affect the RHS; moreover, the
3352 assignment writes the full <retval> object so existing values are entirely
3353 discarded.
3354
3355 This property can be extended to some forms of RETURN_EXPRs that reference
3356 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3357 case, in particular when function calls are involved.
3358
3359 Therefore the algorithm is as follows:
3360
3361 1. Collect the list of candidates for a Named Return Value (Ri variables
3362 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3363 other expressions on the RHS of such assignments.
3364
3365 2. Prune the members of the first list (candidates) that are referenced
3366 by a member of the second list (expressions).
3367
3368 3. Extract a set of candidates with non-overlapping live ranges from the
3369 first list. These are the Named Return Values.
3370
3371 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
088b91c7
EB
3372 Named Return Values in the function with the RESULT_DECL.
3373
3374 If the function returns an unconstrained type, things are a bit different
3375 because the anonymous return object is allocated on the secondary stack
3376 and RESULT_DECL is only a pointer to it. Each return object can be of a
3377 different size and is allocated separately so we need not care about the
13b6b773
EB
3378 addressability and the aforementioned overlapping issues. Therefore, we
3379 don't collect the other expressions and skip step #2 in the algorithm. */
71196d4e
EB
3380
3381struct nrv_data
3382{
3383 bitmap nrv;
3384 tree result;
088b91c7 3385 Node_Id gnat_ret;
6e2830c3 3386 hash_set<tree> *visited;
71196d4e
EB
3387};
3388
3389/* Return true if T is a Named Return Value. */
3390
3391static inline bool
3392is_nrv_p (bitmap nrv, tree t)
3393{
3394 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
3395}
3396
3397/* Helper function for walk_tree, used by finalize_nrv below. */
3398
3399static tree
3400prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3401{
3402 struct nrv_data *dp = (struct nrv_data *)data;
3403 tree t = *tp;
3404
3405 /* No need to walk into types or decls. */
3406 if (IS_TYPE_OR_DECL_P (t))
3407 *walk_subtrees = 0;
3408
3409 if (is_nrv_p (dp->nrv, t))
3410 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3411
3412 return NULL_TREE;
3413}
3414
3415/* Prune Named Return Values in BLOCK and return true if there is still a
3416 Named Return Value in BLOCK or one of its sub-blocks. */
3417
3418static bool
3419prune_nrv_in_block (bitmap nrv, tree block)
3420{
3421 bool has_nrv = false;
3422 tree t;
3423
3424 /* First recurse on the sub-blocks. */
3425 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3426 has_nrv |= prune_nrv_in_block (nrv, t);
3427
3428 /* Then make sure to keep at most one NRV per block. */
3429 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3430 if (is_nrv_p (nrv, t))
3431 {
3432 if (has_nrv)
3433 bitmap_clear_bit (nrv, DECL_UID (t));
3434 else
3435 has_nrv = true;
3436 }
3437
3438 return has_nrv;
3439}
3440
3441/* Helper function for walk_tree, used by finalize_nrv below. */
3442
3443static tree
3444finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3445{
3446 struct nrv_data *dp = (struct nrv_data *)data;
3447 tree t = *tp;
3448
3449 /* No need to walk into types. */
3450 if (TYPE_P (t))
3451 *walk_subtrees = 0;
3452
3453 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3454 nop, but differs from using NULL_TREE in that it indicates that we care
3455 about the value of the RESULT_DECL. */
3456 else if (TREE_CODE (t) == RETURN_EXPR
d864aeeb 3457 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
71196d4e 3458 {
5d2a63dc 3459 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
71196d4e
EB
3460
3461 /* Strip useless conversions around the return value. */
3462 if (gnat_useless_type_conversion (ret_val))
3463 ret_val = TREE_OPERAND (ret_val, 0);
3464
3465 if (is_nrv_p (dp->nrv, ret_val))
5d2a63dc 3466 TREE_OPERAND (t, 0) = dp->result;
71196d4e
EB
3467 }
3468
3469 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3470 if needed. */
3471 else if (TREE_CODE (t) == DECL_EXPR
3472 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3473 {
3474 tree var = DECL_EXPR_DECL (t), init;
3475
3476 if (DECL_INITIAL (var))
3477 {
3478 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3479 DECL_INITIAL (var));
3480 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3481 DECL_INITIAL (var) = NULL_TREE;
3482 }
3483 else
3484 init = build_empty_stmt (EXPR_LOCATION (t));
3485 *tp = init;
3486
3487 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3488 SET_DECL_VALUE_EXPR (var, dp->result);
3489 DECL_HAS_VALUE_EXPR_P (var) = 1;
3490 /* ??? Kludge to avoid an assertion failure during inlining. */
3491 DECL_SIZE (var) = bitsize_unit_node;
3492 DECL_SIZE_UNIT (var) = size_one_node;
3493 }
3494
3495 /* And replace all uses of NRVs with the RESULT_DECL. */
3496 else if (is_nrv_p (dp->nrv, t))
3497 *tp = convert (TREE_TYPE (t), dp->result);
3498
3499 /* Avoid walking into the same tree more than once. Unfortunately, we
088b91c7
EB
3500 can't just use walk_tree_without_duplicates because it would only
3501 call us for the first occurrence of NRVs in the function body. */
6e2830c3 3502 if (dp->visited->add (*tp))
088b91c7
EB
3503 *walk_subtrees = 0;
3504
3505 return NULL_TREE;
3506}
3507
3508/* Likewise, but used when the function returns an unconstrained type. */
3509
3510static tree
3511finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3512{
3513 struct nrv_data *dp = (struct nrv_data *)data;
3514 tree t = *tp;
3515
3516 /* No need to walk into types. */
3517 if (TYPE_P (t))
3518 *walk_subtrees = 0;
3519
3520 /* We need to see the DECL_EXPR of NRVs before any other references so we
3521 walk the body of BIND_EXPR before walking its variables. */
3522 else if (TREE_CODE (t) == BIND_EXPR)
3523 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3524
3525 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3526 return value built by the allocator instead of the whole construct. */
3527 else if (TREE_CODE (t) == RETURN_EXPR
d864aeeb 3528 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
088b91c7
EB
3529 {
3530 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3531
3532 /* This is the construct returned by the allocator. */
3533 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3534 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3535 {
2117b9bb
EB
3536 tree rhs = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3537
088b91c7 3538 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
2117b9bb 3539 ret_val = CONSTRUCTOR_ELT (rhs, 1)->value;
088b91c7 3540 else
2117b9bb 3541 ret_val = rhs;
088b91c7
EB
3542 }
3543
3544 /* Strip useless conversions around the return value. */
3545 if (gnat_useless_type_conversion (ret_val)
3546 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3547 ret_val = TREE_OPERAND (ret_val, 0);
3548
3549 /* Strip unpadding around the return value. */
3550 if (TREE_CODE (ret_val) == COMPONENT_REF
3551 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3552 ret_val = TREE_OPERAND (ret_val, 0);
3553
3554 /* Assign the new return value to the RESULT_DECL. */
3555 if (is_nrv_p (dp->nrv, ret_val))
3556 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3557 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3558 }
3559
3560 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3561 into a new variable. */
3562 else if (TREE_CODE (t) == DECL_EXPR
3563 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3564 {
3565 tree saved_current_function_decl = current_function_decl;
3566 tree var = DECL_EXPR_DECL (t);
3567 tree alloc, p_array, new_var, new_ret;
9771b263
DN
3568 vec<constructor_elt, va_gc> *v;
3569 vec_alloc (v, 2);
088b91c7
EB
3570
3571 /* Create an artificial context to build the allocation. */
3572 current_function_decl = decl_function_context (var);
3573 start_stmt_group ();
3574 gnat_pushlevel ();
3575
3576 /* This will return a COMPOUND_EXPR with the allocation in the first
3577 arm and the final return value in the second arm. */
3578 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3579 TREE_TYPE (dp->result),
3580 Procedure_To_Call (dp->gnat_ret),
3581 Storage_Pool (dp->gnat_ret),
3582 Empty, false);
3583
3584 /* The new variable is built as a reference to the allocated space. */
3585 new_var
3586 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3587 build_reference_type (TREE_TYPE (var)));
3588 DECL_BY_REFERENCE (new_var) = 1;
3589
3590 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3591 {
2117b9bb
EB
3592 tree cst = TREE_OPERAND (alloc, 1);
3593
088b91c7
EB
3594 /* The new initial value is a COMPOUND_EXPR with the allocation in
3595 the first arm and the value of P_ARRAY in the second arm. */
3596 DECL_INITIAL (new_var)
3597 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3598 TREE_OPERAND (alloc, 0),
2117b9bb 3599 CONSTRUCTOR_ELT (cst, 0)->value);
088b91c7
EB
3600
3601 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3602 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3603 CONSTRUCTOR_APPEND_ELT (v, p_array,
3604 fold_convert (TREE_TYPE (p_array), new_var));
3605 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
2117b9bb 3606 CONSTRUCTOR_ELT (cst, 1)->value);
088b91c7
EB
3607 new_ret = build_constructor (TREE_TYPE (alloc), v);
3608 }
3609 else
3610 {
3611 /* The new initial value is just the allocation. */
3612 DECL_INITIAL (new_var) = alloc;
3613 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3614 }
3615
3616 gnat_pushdecl (new_var, Empty);
3617
3618 /* Destroy the artificial context and insert the new statements. */
3619 gnat_zaplevel ();
3620 *tp = end_stmt_group ();
3621 current_function_decl = saved_current_function_decl;
3622
3623 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3624 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3625 DECL_CHAIN (var) = new_var;
3626 DECL_IGNORED_P (var) = 1;
3627
3628 /* Save the new return value and the dereference of NEW_VAR. */
3629 DECL_INITIAL (var)
3630 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3631 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3632 /* ??? Kludge to avoid messing up during inlining. */
3633 DECL_CONTEXT (var) = NULL_TREE;
3634 }
3635
3636 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3637 else if (is_nrv_p (dp->nrv, t))
3638 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3639
3640 /* Avoid walking into the same tree more than once. Unfortunately, we
3641 can't just use walk_tree_without_duplicates because it would only
3642 call us for the first occurrence of NRVs in the function body. */
6e2830c3 3643 if (dp->visited->add (*tp))
71196d4e
EB
3644 *walk_subtrees = 0;
3645
3646 return NULL_TREE;
3647}
3648
81edaf2f
EB
3649/* Apply FUNC to all the sub-trees of nested functions in NODE. FUNC is called
3650 with the DATA and the address of each sub-tree. If FUNC returns a non-NULL
3651 value, the traversal is stopped. */
3652
3653static void
3654walk_nesting_tree (struct cgraph_node *node, walk_tree_fn func, void *data)
3655{
89576d86
JH
3656 for (node = first_nested_function (node);
3657 node; node = next_nested_function (node))
81edaf2f
EB
3658 {
3659 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), func, data);
3660 walk_nesting_tree (node, func, data);
3661 }
3662}
3663
71196d4e
EB
3664/* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3665 contains the candidates for Named Return Value and OTHER is a list of
088b91c7 3666 the other return values. GNAT_RET is a representative return node. */
71196d4e
EB
3667
3668static void
9771b263 3669finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
71196d4e 3670{
71196d4e 3671 struct nrv_data data;
088b91c7 3672 walk_tree_fn func;
71196d4e
EB
3673 unsigned int i;
3674 tree iter;
3675
3676 /* We shouldn't be applying the optimization to return types that we aren't
3677 allowed to manipulate freely. */
a0b8b1b7 3678 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
71196d4e
EB
3679
3680 /* Prune the candidates that are referenced by other return values. */
3681 data.nrv = nrv;
3682 data.result = NULL_TREE;
547bbe49 3683 data.gnat_ret = Empty;
71196d4e 3684 data.visited = NULL;
547bbe49 3685 FOR_EACH_VEC_SAFE_ELT (other, i, iter)
71196d4e
EB
3686 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3687 if (bitmap_empty_p (nrv))
3688 return;
3689
3690 /* Prune also the candidates that are referenced by nested functions. */
81edaf2f 3691 walk_nesting_tree (cgraph_node::get_create (fndecl), prune_nrv_r, &data);
71196d4e
EB
3692 if (bitmap_empty_p (nrv))
3693 return;
3694
3695 /* Extract a set of NRVs with non-overlapping live ranges. */
3696 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3697 return;
3698
3699 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3700 data.nrv = nrv;
3701 data.result = DECL_RESULT (fndecl);
088b91c7 3702 data.gnat_ret = gnat_ret;
6e2830c3 3703 data.visited = new hash_set<tree>;
89e037d0 3704 if (TYPE_RETURN_BY_DIRECT_REF_P (TREE_TYPE (fndecl)))
088b91c7
EB
3705 func = finalize_nrv_unc_r;
3706 else
3707 func = finalize_nrv_r;
3708 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
6e2830c3 3709 delete data.visited;
71196d4e
EB
3710}
3711
3712/* Return true if RET_VAL can be used as a Named Return Value for the
3713 anonymous return object RET_OBJ. */
3714
3715static bool
3716return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3717{
3718 if (TREE_CODE (ret_val) != VAR_DECL)
3719 return false;
3720
3721 if (TREE_THIS_VOLATILE (ret_val))
3722 return false;
3723
3724 if (DECL_CONTEXT (ret_val) != current_function_decl)
3725 return false;
3726
3727 if (TREE_STATIC (ret_val))
3728 return false;
3729
13b6b773
EB
3730 /* For the constrained case, test for addressability. */
3731 if (ret_obj && TREE_ADDRESSABLE (ret_val))
71196d4e
EB
3732 return false;
3733
0b9cdb9a 3734 /* For the constrained case, test for overalignment. */
088b91c7 3735 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
71196d4e
EB
3736 return false;
3737
0b9cdb9a
EB
3738 /* For the unconstrained case, test for bogus initialization. */
3739 if (!ret_obj
3740 && DECL_INITIAL (ret_val)
3741 && TREE_CODE (DECL_INITIAL (ret_val)) == NULL_EXPR)
3742 return false;
3743
71196d4e
EB
3744 return true;
3745}
3746
3747/* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3748 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3749 around RESULT_OBJ, which may be null in this case. */
f3d34576
EB
3750
3751static tree
3752build_return_expr (tree ret_obj, tree ret_val)
3753{
3754 tree result_expr;
3755
3756 if (ret_val)
3757 {
3758 /* The gimplifier explicitly enforces the following invariant:
3759
3760 RETURN_EXPR
3761 |
d864aeeb 3762 INIT_EXPR
f3d34576
EB
3763 / \
3764 / \
3765 RET_OBJ ...
3766
3767 As a consequence, type consistency dictates that we use the type
3768 of the RET_OBJ as the operation type. */
3769 tree operation_type = TREE_TYPE (ret_obj);
3770
d864aeeb
EB
3771 /* Convert the right operand to the operation type. Note that this is
3772 the transformation applied in the INIT_EXPR case of build_binary_op,
f3d34576
EB
3773 with the assumption that the type cannot involve a placeholder. */
3774 if (operation_type != TREE_TYPE (ret_val))
3775 ret_val = convert (operation_type, ret_val);
3776
d864aeeb
EB
3777 /* We always can use an INIT_EXPR for the return object. */
3778 result_expr = build2 (INIT_EXPR, void_type_node, ret_obj, ret_val);
71196d4e
EB
3779
3780 /* If the function returns an aggregate type, find out whether this is
3781 a candidate for Named Return Value. If so, record it. Otherwise,
3782 if this is an expression of some kind, record it elsewhere. */
3783 if (optimize
09248547 3784 && !optimize_debug
71196d4e
EB
3785 && AGGREGATE_TYPE_P (operation_type)
3786 && !TYPE_IS_FAT_POINTER_P (operation_type)
f563ce55 3787 && TYPE_MODE (operation_type) == BLKmode
71196d4e
EB
3788 && aggregate_value_p (operation_type, current_function_decl))
3789 {
71196d4e
EB
3790 /* Strip useless conversions around the return value. */
3791 if (gnat_useless_type_conversion (ret_val))
3792 ret_val = TREE_OPERAND (ret_val, 0);
3793
3794 /* Now apply the test to the return value. */
3795 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3796 {
3797 if (!f_named_ret_val)
3798 f_named_ret_val = BITMAP_GGC_ALLOC ();
3799 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3800 }
3801
3802 /* Note that we need not care about CONSTRUCTORs here, as they are
3803 totally transparent given the read-compose-write semantics of
3804 assignments from CONSTRUCTORs. */
3805 else if (EXPR_P (ret_val))
9771b263 3806 vec_safe_push (f_other_ret_val, ret_val);
71196d4e 3807 }
f3d34576
EB
3808 }
3809 else
3810 result_expr = ret_obj;
3811
3812 return build1 (RETURN_EXPR, void_type_node, result_expr);
3813}
ce2d0ce2 3814
6d16658d
BD
3815/* Subroutine of gnat_to_gnu to translate the At_End_Proc of GNAT_NODE, an
3816 N_Block_Statement or N_Handled_Sequence_Of_Statements or N_*_Body node.
3817
3818 To invoked the GCC mechanism, we call add_cleanup and when we leave the
3819 group, end_stmt_group will create the TRY_FINALLY_EXPR construct. */
3820
3821static void
3822At_End_Proc_to_gnu (Node_Id gnat_node)
3823{
3824 tree proc_decl = gnat_to_gnu (At_End_Proc (gnat_node));
0b66f882 3825 Node_Id gnat_end_label;
6d16658d
BD
3826
3827 /* When not optimizing, disable inlining of finalizers as this can
3828 create a more complex CFG in the parent function. */
3829 if (!optimize || optimize_debug)
3830 DECL_DECLARED_INLINE_P (proc_decl) = 0;
3831
0b66f882
EB
3832 /* Retrieve the end label attached to the node, if any. */
3833 if (Nkind (gnat_node) == N_Handled_Sequence_Of_Statements)
3834 gnat_end_label = End_Label (gnat_node);
3835 else if (Present (Handled_Statement_Sequence (gnat_node)))
3836 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
3837 else
3838 gnat_end_label = Empty;
3839
6d16658d
BD
3840 /* If there is no end label attached, we use the location of the At_End
3841 procedure because Expand_Cleanup_Actions might reset the location of
0b66f882 3842 the enclosing construct to that of an inner statement. */
6d16658d 3843 add_cleanup (build_call_n_expr (proc_decl, 0),
0b66f882
EB
3844 Present (gnat_end_label)
3845 ? gnat_end_label : At_End_Proc (gnat_node));
6d16658d
BD
3846}
3847
e08f1aad 3848/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Subprogram_Body. */
a1ab4c31
AC
3849
3850static void
3851Subprogram_Body_to_gnu (Node_Id gnat_node)
3852{
a1ab4c31
AC
3853 /* The defining identifier for the subprogram body. Note that if a
3854 specification has appeared before for this body, then the identifier
e08f1aad
EB
3855 occurring in that specification will also be a defining identifier
3856 and calls to this subprogram will point to that specification. */
4e279bd9 3857 Entity_Id gnat_subprog
a1ab4c31
AC
3858 = (Present (Corresponding_Spec (gnat_node))
3859 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
e08f1aad 3860 /* The FUNCTION_DECL node corresponding to the defining identifier. */
4e279bd9 3861 tree gnu_subprog;
d47d0a8d
EB
3862 /* Its RESULT_DECL node. */
3863 tree gnu_result_decl;
35a382b8 3864 /* Its FUNCTION_TYPE node. */
a1ab4c31 3865 tree gnu_subprog_type;
35a382b8 3866 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
a1ab4c31 3867 tree gnu_cico_list;
35a382b8 3868 /* The entry in the CI_CO_LIST that represents a function return, if any. */
e08f1aad
EB
3869 tree gnu_return_var_elmt;
3870 /* Its source location. */
40e63ada 3871 location_t locus;
a1ab4c31 3872
4e279bd9
EB
3873 /* If this is a generic subprogram or it has been eliminated, ignore it. */
3874 if (Is_Generic_Subprogram (gnat_subprog) || Is_Eliminated (gnat_subprog))
a1ab4c31
AC
3875 return;
3876
3877 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3878 the already-elaborated tree node. However, if this subprogram had its
3879 elaboration deferred, we will already have made a tree node for it. So
3880 treat it as not being defined in that case. Such a subprogram cannot
3881 have an address clause or a freeze node, so this test is safe, though it
3882 does disable some otherwise-useful error checking. */
4e279bd9
EB
3883 gnu_subprog
3884 = gnat_to_gnu_entity (gnat_subprog, NULL_TREE,
a1ab4c31 3885 Acts_As_Spec (gnat_node)
4e279bd9
EB
3886 && !present_gnu_tree (gnat_subprog));
3887 DECL_FUNCTION_IS_DEF (gnu_subprog) = true;
3888 gnu_result_decl = DECL_RESULT (gnu_subprog);
3889 gnu_subprog_type = TREE_TYPE (gnu_subprog);
35a382b8 3890 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
2374257a
EB
3891 if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node)
3892 gnu_return_var_elmt = gnu_cico_list;
e08f1aad
EB
3893 else
3894 gnu_return_var_elmt = NULL_TREE;
a1ab4c31 3895
d47d0a8d 3896 /* If the function returns by invisible reference, make it explicit in the
e08f1aad 3897 function body. See gnat_to_gnu_subprog_type for more details. */
2374257a 3898 if (TREE_ADDRESSABLE (gnu_subprog_type))
d47d0a8d
EB
3899 {
3900 TREE_TYPE (gnu_result_decl)
3901 = build_reference_type (TREE_TYPE (gnu_result_decl));
3902 relayout_decl (gnu_result_decl);
3903 }
3904
40e63ada 3905 /* Set the line number in the decl to correspond to that of the body. */
4e279bd9 3906 if (DECL_IGNORED_P (gnu_subprog))
a45918f8 3907 locus = UNKNOWN_LOCATION;
4e279bd9 3908 else if (!Sloc_to_locus (Sloc (gnat_node), &locus, false, gnu_subprog))
0c8af82a 3909 locus = input_location;
4e279bd9 3910 DECL_SOURCE_LOCATION (gnu_subprog) = locus;
a1ab4c31 3911
44662f68 3912 /* Try to create a bona-fide thunk and hand it over to the middle-end. */
4e279bd9 3913 if (Is_Thunk (gnat_subprog)
89e037d0 3914 && !Is_Secondary_Stack_Thunk (gnat_subprog)
4e279bd9 3915 && maybe_make_gnu_thunk (gnat_subprog, gnu_subprog))
44662f68
EB
3916 return;
3917
a1ab4c31 3918 /* Initialize the information structure for the function. */
4e279bd9 3919 allocate_struct_function (gnu_subprog, false);
e08f1aad
EB
3920 language_function *gnu_subprog_lang = ggc_cleared_alloc<language_function> ();
3921 DECL_STRUCT_FUNCTION (gnu_subprog)->language = gnu_subprog_lang;
4e279bd9 3922 DECL_STRUCT_FUNCTION (gnu_subprog)->function_start_locus = locus;
58c8f770 3923 set_cfun (NULL);
a1ab4c31 3924
4e279bd9 3925 begin_subprog_body (gnu_subprog);
a1ab4c31 3926
2374257a
EB
3927 /* If there are copy-in/copy-out parameters, we need to ensure that they are
3928 properly copied out by the return statement. We do this by making a new
3929 block and converting any return into a goto to a label at the end of the
3930 block. */
a963da4d
EB
3931 if (gnu_cico_list)
3932 {
e08f1aad 3933 tree gnu_return_var;
35a382b8 3934
b16b6cc9 3935 vec_safe_push (gnu_return_label_stack,
a963da4d
EB
3936 create_artificial_label (input_location));
3937
3938 start_stmt_group ();
3939 gnat_pushlevel ();
3940
2374257a
EB
3941 /* If this is a function with copy-in/copy-out parameters and which does
3942 not return by invisible reference, we also need a variable for the
3943 return value to be placed. */
3944 if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type))
35a382b8
EB
3945 {
3946 tree gnu_return_type
3947 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3948
35a382b8
EB
3949 gnu_return_var
3950 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
2056c5ed
EB
3951 gnu_return_type, NULL_TREE,
3952 false, false, false, false, false,
4e279bd9 3953 true, false, NULL, gnat_subprog);
35a382b8
EB
3954 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3955 }
e08f1aad
EB
3956 else
3957 gnu_return_var = NULL_TREE;
35a382b8 3958
9771b263 3959 vec_safe_push (gnu_return_var_stack, gnu_return_var);
35a382b8 3960
a963da4d
EB
3961 /* See whether there are parameters for which we don't have a GCC tree
3962 yet. These must be Out parameters. Make a VAR_DECL for them and
3963 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3964 We can match up the entries because TYPE_CI_CO_LIST is in the order
3965 of the parameters. */
e08f1aad 3966 for (Entity_Id gnat_param = First_Formal_With_Extras (gnat_subprog);
a963da4d
EB
3967 Present (gnat_param);
3968 gnat_param = Next_Formal_With_Extras (gnat_param))
3969 if (!present_gnu_tree (gnat_param))
3970 {
3971 tree gnu_cico_entry = gnu_cico_list;
54625ca1 3972 tree gnu_decl;
a963da4d
EB
3973
3974 /* Skip any entries that have been already filled in; they must
3975 correspond to In Out parameters. */
3976 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3977 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3978
54625ca1 3979 /* Do any needed dereferences for by-ref objects. */
afc737f0 3980 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, true);
54625ca1
EB
3981 gcc_assert (DECL_P (gnu_decl));
3982 if (DECL_BY_REF_P (gnu_decl))
3983 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3984
a963da4d
EB
3985 /* Do any needed references for padded types. */
3986 TREE_VALUE (gnu_cico_entry)
54625ca1 3987 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
a963da4d
EB
3988 }
3989 }
3990 else
9771b263 3991 vec_safe_push (gnu_return_label_stack, NULL_TREE);
a1ab4c31
AC
3992
3993 /* Get a tree corresponding to the code for the subprogram. */
3994 start_stmt_group ();
3995 gnat_pushlevel ();
3996
e08f1aad 3997 /* First translate the declarations of the subprogram. */
6d16658d 3998 process_decls (Declarations (gnat_node), Empty, true, true);
a1ab4c31 3999
e08f1aad
EB
4000 /* Then generate the code of the subprogram itself. A return statement will
4001 be present and any Out parameters will be handled there. */
a1ab4c31 4002 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
e08f1aad 4003
6d16658d
BD
4004 /* Process the At_End_Proc, if any. */
4005 if (Present (At_End_Proc (gnat_node)))
4006 At_End_Proc_to_gnu (gnat_node);
4007
a1ab4c31 4008 gnat_poplevel ();
e08f1aad 4009 tree gnu_result = end_stmt_group ();
a1ab4c31 4010
cc997e0c
EB
4011 /* Attempt setting the end_locus of our GCC body tree, typically a BIND_EXPR,
4012 then the end_locus of our GCC subprogram declaration tree. */
4013 set_end_locus_from_node (gnu_result, gnat_node);
4e279bd9 4014 set_end_locus_from_node (gnu_subprog, gnat_node);
cc997e0c 4015
0394741f
EB
4016 /* If we populated the parameter attributes cache, we need to make sure that
4017 the cached expressions are evaluated on all the possible paths leading to
4018 their uses. So we force their evaluation on entry of the function. */
e08f1aad 4019 vec<parm_attr, va_gc> *cache = gnu_subprog_lang->parm_attr_cache;
0394741f
EB
4020 if (cache)
4021 {
4022 struct parm_attr_d *pa;
4023 int i;
4024
4025 start_stmt_group ();
4026
9771b263 4027 FOR_EACH_VEC_ELT (*cache, i, pa)
0394741f
EB
4028 {
4029 if (pa->first)
4030 add_stmt_with_node_force (pa->first, gnat_node);
4031 if (pa->last)
4032 add_stmt_with_node_force (pa->last, gnat_node);
4033 if (pa->length)
4034 add_stmt_with_node_force (pa->length, gnat_node);
4035 }
4036
4037 add_stmt (gnu_result);
4038 gnu_result = end_stmt_group ();
f3d34576 4039
e08f1aad 4040 gnu_subprog_lang->parm_attr_cache = NULL;
0394741f
EB
4041 }
4042
a963da4d
EB
4043 /* If we are dealing with a return from an Ada procedure with parameters
4044 passed by copy-in/copy-out, we need to return a record containing the
4045 final values of these parameters. If the list contains only one entry,
4046 return just that entry though.
4047
4048 For a full description of the copy-in/copy-out parameter mechanism, see
4049 the part of the gnat_to_gnu_entity routine dealing with the translation
4050 of subprograms.
4051
4052 We need to make a block that contains the definition of that label and
4053 the copying of the return value. It first contains the function, then
4054 the label and copy statement. */
4055 if (gnu_cico_list)
4056 {
2374257a
EB
4057 const Node_Id gnat_end_label
4058 = End_Label (Handled_Statement_Sequence (gnat_node));
a963da4d 4059
df2abf54
EB
4060 gnu_return_var_stack->pop ();
4061
a963da4d
EB
4062 add_stmt (gnu_result);
4063 add_stmt (build1 (LABEL_EXPR, void_type_node,
9771b263 4064 gnu_return_label_stack->last ()));
a963da4d 4065
2374257a
EB
4066 /* If this is a function which returns by invisible reference, the
4067 return value has already been dealt with at the return statements,
4068 so we only need to indirectly copy out the parameters. */
4069 if (TREE_ADDRESSABLE (gnu_subprog_type))
4070 {
4071 tree gnu_ret_deref
4072 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl);
4073 tree t;
4074
4075 gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node);
4076
4077 for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t))
4078 {
4079 tree gnu_field_deref
64235766 4080 = build_component_ref (gnu_ret_deref, TREE_PURPOSE (t), true);
2374257a
EB
4081 gnu_result = build2 (MODIFY_EXPR, void_type_node,
4082 gnu_field_deref, TREE_VALUE (t));
4083 add_stmt_with_node (gnu_result, gnat_end_label);
4084 }
4085 }
4086
4087 /* Otherwise, if this is a procedure or a function which does not return
4088 by invisible reference, we can do a direct block-copy out. */
a963da4d 4089 else
2374257a
EB
4090 {
4091 tree gnu_retval;
4092
4093 if (list_length (gnu_cico_list) == 1)
4094 gnu_retval = TREE_VALUE (gnu_cico_list);
4095 else
4096 gnu_retval
4097 = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
4098 gnu_cico_list);
4099
4100 gnu_result = build_return_expr (gnu_result_decl, gnu_retval);
4101 add_stmt_with_node (gnu_result, gnat_end_label);
4102 }
a963da4d 4103
a963da4d
EB
4104 gnat_poplevel ();
4105 gnu_result = end_stmt_group ();
4106 }
4107
9771b263 4108 gnu_return_label_stack->pop ();
a963da4d 4109
48a24fcf
TG
4110 /* On SEH targets, install an exception handler around the main entry
4111 point to catch unhandled exceptions. */
4e279bd9 4112 if (DECL_NAME (gnu_subprog) == main_identifier_node
48a24fcf
TG
4113 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
4114 {
4115 tree t;
4116 tree etype;
4117
4118 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
4119 1, integer_zero_node);
4120 t = build_call_n_expr (unhandled_except_decl, 1, t);
4121
4122 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
4123 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
4124
4125 t = build2 (CATCH_EXPR, void_type_node, etype, t);
4126 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
4127 gnu_result, t);
4128 }
4129
f3d34576
EB
4130 end_subprog_body (gnu_result);
4131
f4cd2542
EB
4132 /* Finally annotate the parameters and disconnect the trees for parameters
4133 that we have turned into variables since they are now unusable. */
e08f1aad 4134 for (Entity_Id gnat_param = First_Formal_With_Extras (gnat_subprog);
a1ab4c31
AC
4135 Present (gnat_param);
4136 gnat_param = Next_Formal_With_Extras (gnat_param))
f4cd2542
EB
4137 {
4138 tree gnu_param = get_gnu_tree (gnat_param);
0c700259
EB
4139 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
4140
f4cd2542 4141 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
491f54a7 4142 DECL_BY_REF_P (gnu_param));
0c700259
EB
4143
4144 if (is_var_decl)
f4cd2542
EB
4145 save_gnu_tree (gnat_param, NULL_TREE, false);
4146 }
a1ab4c31 4147
0d24bf76 4148 /* Disconnect the variable created for the return value. */
35a382b8
EB
4149 if (gnu_return_var_elmt)
4150 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
4151
71196d4e
EB
4152 /* If the function returns an aggregate type and we have candidates for
4153 a Named Return Value, finalize the optimization. */
e08f1aad 4154 if (optimize && !optimize_debug && gnu_subprog_lang->named_ret_val)
71196d4e 4155 {
4e279bd9 4156 finalize_nrv (gnu_subprog,
e08f1aad
EB
4157 gnu_subprog_lang->named_ret_val,
4158 gnu_subprog_lang->other_ret_val,
4159 gnu_subprog_lang->gnat_ret);
4160 gnu_subprog_lang->named_ret_val = NULL;
4161 gnu_subprog_lang->other_ret_val = NULL;
71196d4e
EB
4162 }
4163
87411e95
EB
4164 /* If this is an inlined external function that has been marked uninlinable,
4165 drop the body and stop there. Otherwise compile the body. */
4e279bd9
EB
4166 if (DECL_EXTERNAL (gnu_subprog) && DECL_UNINLINABLE (gnu_subprog))
4167 DECL_SAVED_TREE (gnu_subprog) = NULL_TREE;
87411e95 4168 else
4e279bd9 4169 rest_of_subprog_body_compilation (gnu_subprog);
a1ab4c31 4170}
ce2d0ce2 4171
17a98a3d
EB
4172/* The type of an atomic access. */
4173
4174typedef enum { NOT_ATOMIC, SIMPLE_ATOMIC, OUTER_ATOMIC } atomic_acces_t;
4175
4176/* Return true if GNAT_NODE references an Atomic entity. This is modeled on
4177 the Is_Atomic_Object predicate of the front-end, but additionally handles
4178 explicit dereferences. */
033ba5bf
EB
4179
4180static bool
f797c2b7 4181node_is_atomic (Node_Id gnat_node)
033ba5bf 4182{
f797c2b7 4183 Entity_Id gnat_entity;
033ba5bf 4184
f797c2b7 4185 switch (Nkind (gnat_node))
033ba5bf 4186 {
f797c2b7
EB
4187 case N_Identifier:
4188 case N_Expanded_Name:
4189 gnat_entity = Entity (gnat_node);
1141fbd7 4190 if (!Is_Object (gnat_entity))
f797c2b7 4191 break;
1141fbd7
EB
4192 return Is_Atomic (gnat_entity)
4193 || (Is_Atomic (Etype (gnat_entity))
4194 && !simple_constant_p (gnat_entity));
f797c2b7
EB
4195
4196 case N_Selected_Component:
17a98a3d
EB
4197 return Is_Atomic (Etype (gnat_node))
4198 || Is_Atomic (Entity (Selector_Name (gnat_node)));
f797c2b7
EB
4199
4200 case N_Indexed_Component:
17a98a3d
EB
4201 return Is_Atomic (Etype (gnat_node))
4202 || Has_Atomic_Components (Etype (Prefix (gnat_node)))
4203 || (Is_Entity_Name (Prefix (gnat_node))
4204 && Has_Atomic_Components (Entity (Prefix (gnat_node))));
f797c2b7
EB
4205
4206 case N_Explicit_Dereference:
4207 return Is_Atomic (Etype (gnat_node));
4208
4209 default:
4210 break;
033ba5bf
EB
4211 }
4212
f797c2b7
EB
4213 return false;
4214}
4215
17a98a3d 4216/* Return true if GNAT_NODE references a Volatile_Full_Access entity. This is
2cee58d8
EB
4217 modeled on the Is_Volatile_Full_Access_Object predicate of the front-end,
4218 but additionally handles explicit dereferences. */
f797c2b7
EB
4219
4220static bool
17a98a3d 4221node_is_volatile_full_access (Node_Id gnat_node)
f797c2b7
EB
4222{
4223 Entity_Id gnat_entity;
4224
4225 switch (Nkind (gnat_node))
033ba5bf 4226 {
033ba5bf 4227 case N_Identifier:
f797c2b7
EB
4228 case N_Expanded_Name:
4229 gnat_entity = Entity (gnat_node);
f1bc6414
EB
4230 if (!Is_Object (gnat_entity))
4231 break;
f797c2b7 4232 return Is_Volatile_Full_Access (gnat_entity)
1141fbd7
EB
4233 || (Is_Volatile_Full_Access (Etype (gnat_entity))
4234 && !simple_constant_p (gnat_entity));
f797c2b7 4235
033ba5bf 4236 case N_Selected_Component:
17a98a3d
EB
4237 return Is_Volatile_Full_Access (Etype (gnat_node))
4238 || Is_Volatile_Full_Access (Entity (Selector_Name (gnat_node)));
f797c2b7
EB
4239
4240 case N_Indexed_Component:
4241 case N_Explicit_Dereference:
4242 return Is_Volatile_Full_Access (Etype (gnat_node));
033ba5bf
EB
4243
4244 default:
f797c2b7 4245 break;
033ba5bf
EB
4246 }
4247
f797c2b7
EB
4248 return false;
4249}
4250
17a98a3d 4251/* Return true if GNAT_NODE references a component of a larger object. */
f797c2b7 4252
17a98a3d
EB
4253static inline bool
4254node_is_component (Node_Id gnat_node)
f797c2b7 4255{
17a98a3d
EB
4256 const Node_Kind k = Nkind (gnat_node);
4257 return
4258 (k == N_Indexed_Component || k == N_Selected_Component || k == N_Slice);
f797c2b7
EB
4259}
4260
17a98a3d
EB
4261/* Compute whether GNAT_NODE requires atomic access and set TYPE to the type
4262 of access and SYNC according to the associated synchronization setting.
241125b2 4263
17a98a3d 4264 We implement 3 different semantics of atomicity in this function:
241125b2 4265
17a98a3d 4266 1. the Ada 95/2005/2012 semantics of the Atomic aspect/pragma,
81e68a19 4267 2. the Ada 2022 semantics of the Atomic aspect/pragma,
17a98a3d 4268 3. the semantics of the Volatile_Full_Access GNAT aspect/pragma.
f797c2b7 4269
17a98a3d 4270 They are mutually exclusive and the FE should have rejected conflicts. */
f797c2b7 4271
17a98a3d
EB
4272static void
4273get_atomic_access (Node_Id gnat_node, atomic_acces_t *type, bool *sync)
f797c2b7 4274{
17a98a3d 4275 Node_Id gnat_parent, gnat_temp;
f797c2b7 4276 unsigned char attr_id;
f797c2b7 4277
17a98a3d
EB
4278 /* First, scan the parent to filter out irrelevant cases. */
4279 gnat_parent = Parent (gnat_node);
f797c2b7 4280 switch (Nkind (gnat_parent))
033ba5bf
EB
4281 {
4282 case N_Attribute_Reference:
4283 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
4284 /* Do not mess up machine code insertions. */
4285 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
17a98a3d 4286 goto not_atomic;
f797c2b7
EB
4287
4288 /* Nothing to do if we are the prefix of an attribute, since we do not
4289 want an atomic access for things like 'Size. */
4290
9c453de7 4291 /* ... fall through ... */
f797c2b7
EB
4292
4293 case N_Reference:
4294 /* The N_Reference node is like an attribute. */
4295 if (Prefix (gnat_parent) == gnat_node)
17a98a3d 4296 goto not_atomic;
033ba5bf
EB
4297 break;
4298
4299 case N_Object_Renaming_Declaration:
f797c2b7
EB
4300 /* Nothing to do for the identifier in an object renaming declaration,
4301 the renaming itself does not need atomic access. */
17a98a3d 4302 goto not_atomic;
033ba5bf
EB
4303
4304 default:
4305 break;
4306 }
4307
17a98a3d
EB
4308 /* Now strip any type conversion from GNAT_NODE. */
4309 if (Nkind (gnat_node) == N_Type_Conversion
4310 || Nkind (gnat_node) == N_Unchecked_Type_Conversion)
4311 gnat_node = Expression (gnat_node);
f797c2b7 4312
17a98a3d 4313 /* Up to Ada 2012, for Atomic itself, only reads and updates of the object as
81e68a19 4314 a whole require atomic access (RM C.6(15)). But, starting with Ada 2022,
17a98a3d
EB
4315 reads of or writes to a nonatomic subcomponent of the object also require
4316 atomic access (RM C.6(19)). */
4317 if (node_is_atomic (gnat_node))
4318 {
4319 bool as_a_whole = true;
f797c2b7 4320
17a98a3d
EB
4321 /* If we are the prefix of the parent, then the access is partial. */
4322 for (gnat_temp = gnat_node, gnat_parent = Parent (gnat_temp);
4323 node_is_component (gnat_parent) && Prefix (gnat_parent) == gnat_temp;
4324 gnat_temp = gnat_parent, gnat_parent = Parent (gnat_temp))
81e68a19 4325 if (Ada_Version < Ada_2022 || node_is_atomic (gnat_parent))
17a98a3d
EB
4326 goto not_atomic;
4327 else
4328 as_a_whole = false;
f797c2b7 4329
17a98a3d
EB
4330 /* We consider that partial accesses are not sequential actions and,
4331 therefore, do not require synchronization. */
4332 *type = SIMPLE_ATOMIC;
4333 *sync = as_a_whole ? Atomic_Sync_Required (gnat_node) : false;
4334 return;
4335 }
f797c2b7 4336
17a98a3d
EB
4337 /* Look for an outer atomic access of a nonatomic subcomponent. Note that,
4338 for VFA, we do this before looking at the node itself because we need to
4339 access the outermost VFA object atomically, unlike for Atomic where it is
4340 the innermost atomic object (RM C.6(19)). */
4341 for (gnat_temp = gnat_node;
4342 node_is_component (gnat_temp);
4343 gnat_temp = Prefix (gnat_temp))
81e68a19 4344 if ((Ada_Version >= Ada_2022 && node_is_atomic (Prefix (gnat_temp)))
17a98a3d
EB
4345 || node_is_volatile_full_access (Prefix (gnat_temp)))
4346 {
4347 *type = OUTER_ATOMIC;
4348 *sync = false;
4349 return;
4350 }
4351
4352 /* Unlike Atomic, accessing a VFA object always requires atomic access. */
4353 if (node_is_volatile_full_access (gnat_node))
4354 {
4355 *type = SIMPLE_ATOMIC;
4356 *sync = false;
4357 return;
4358 }
4359
4360not_atomic:
4361 *type = NOT_ATOMIC;
4362 *sync = false;
033ba5bf 4363}
ce2d0ce2
EB
4364
4365/* Return true if GNAT_NODE requires simple atomic access and, if so, set SYNC
17a98a3d
EB
4366 according to the associated synchronization setting. */
4367
4368static inline bool
4369simple_atomic_access_required_p (Node_Id gnat_node, bool *sync)
4370{
4371 atomic_acces_t type;
4372 get_atomic_access (gnat_node, &type, sync);
4373 return type == SIMPLE_ATOMIC;
4374}
4375
544d14e1
EB
4376/* Return the storage model specified by GNAT_NODE, or else Empty. */
4377
4378static Entity_Id
4379get_storage_model (Node_Id gnat_node)
4380{
4381 if (Nkind (gnat_node) == N_Explicit_Dereference
4382 && Has_Designated_Storage_Model_Aspect (Etype (Prefix (gnat_node))))
4383 return Storage_Model_Object (Etype (Prefix (gnat_node)));
4384 else
4385 return Empty;
4386}
4387
4388/* Compute whether GNAT_NODE requires storage model access and set GNAT_SMO to
4389 the storage model object to be used for it if it does, or else Empty. */
4390
4391static void
4392get_storage_model_access (Node_Id gnat_node, Entity_Id *gnat_smo)
4393{
4394 const Node_Id gnat_parent = Parent (gnat_node);
4395
4396 /* If we are the prefix of the parent, then the access is above us. */
4397 if (node_is_component (gnat_parent) && Prefix (gnat_parent) == gnat_node)
4398 {
4399 *gnat_smo = Empty;
4400 return;
4401 }
4402
798c08ca
EB
4403 /* Now strip any type conversion from GNAT_NODE. */
4404 if (Nkind (gnat_node) == N_Type_Conversion
4405 || Nkind (gnat_node) == N_Unchecked_Type_Conversion)
4406 gnat_node = Expression (gnat_node);
4407
544d14e1
EB
4408 while (node_is_component (gnat_node))
4409 gnat_node = Prefix (gnat_node);
4410
4411 *gnat_smo = get_storage_model (gnat_node);
4412}
4413
4414/* Return true if GNAT_NODE requires storage model access and, if so, set
4415 GNAT_SMO to the storage model object to be used for it. */
4416
4417static bool
4418storage_model_access_required_p (Node_Id gnat_node, Entity_Id *gnat_smo)
4419{
4420 get_storage_model_access (gnat_node, gnat_smo);
4421 return Present (*gnat_smo);
4422}
4423
ddb5a105
EB
4424/* Create a temporary variable with PREFIX and TYPE, and return it. */
4425
4426static tree
4427create_temporary (const char *prefix, tree type)
4428{
2056c5ed
EB
4429 tree gnu_temp
4430 = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
4431 type, NULL_TREE,
4432 false, false, false, false, false,
4433 true, false, NULL, Empty);
ddb5a105
EB
4434 return gnu_temp;
4435}
35a382b8
EB
4436
4437/* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
4438 Put the initialization statement into GNU_INIT_STMT and annotate it with
4439 the SLOC of GNAT_NODE. Return the temporary variable. */
4440
4441static tree
4442create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
4443 Node_Id gnat_node)
4444{
ddb5a105 4445 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
35a382b8
EB
4446
4447 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
4448 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
4449
4450 return gnu_temp;
4451}
4452
b81e2d5e
EB
4453/* Return true if TYPE is an array of scalar type. */
4454
4455static bool
4456is_array_of_scalar_type (tree type)
4457{
4458 if (TREE_CODE (type) != ARRAY_TYPE)
4459 return false;
4460
4461 type = TREE_TYPE (type);
4462
4463 return !AGGREGATE_TYPE_P (type) && !POINTER_TYPE_P (type);
4464}
4465
4466/* Helper function for walk_tree, used by return_slot_opt_for_pure_call_p. */
4467
4468static tree
4469find_decls_r (tree *tp, int *walk_subtrees, void *data)
4470{
4471 bitmap decls = (bitmap) data;
4472
4473 if (TYPE_P (*tp))
4474 *walk_subtrees = 0;
4475
4476 else if (DECL_P (*tp))
4477 bitmap_set_bit (decls, DECL_UID (*tp));
4478
4479 return NULL_TREE;
4480}
4481
4482/* Return whether the assignment TARGET = CALL can be subject to the return
4483 slot optimization, under the assumption that the called function be pure
4484 in the Ada sense and return an array of scalar type. */
4485
4486static bool
4487return_slot_opt_for_pure_call_p (tree target, tree call)
4488{
4489 /* Check that the target is a DECL. */
4490 if (!DECL_P (target))
4491 return false;
4492
4493 const bitmap decls = BITMAP_GGC_ALLOC ();
4494 call_expr_arg_iterator iter;
4495 tree arg;
4496
4497 /* Check that all the arguments have either a scalar type (we assume that
4498 this means by-copy passing mechanism) or array of scalar type. */
4499 FOR_EACH_CALL_EXPR_ARG (arg, iter, call)
4500 {
4501 tree arg_type = TREE_TYPE (arg);
4502 if (TREE_CODE (arg_type) == REFERENCE_TYPE)
4503 arg_type = TREE_TYPE (arg_type);
4504
4505 if (is_array_of_scalar_type (arg_type))
4506 walk_tree_without_duplicates (&arg, find_decls_r, decls);
4507
4508 else if (AGGREGATE_TYPE_P (arg_type) || POINTER_TYPE_P (arg_type))
4509 return false;
4510 }
4511
4512 /* Check that the target is not referenced by the non-scalar arguments. */
4513 return !bitmap_bit_p (decls, DECL_UID (target));
4514}
4515
a42dd9fe
EB
4516/* Elaborate types referenced in the profile (FIRST_FORMAL, RESULT_TYPE). */
4517
4518static void
4519elaborate_profile (Entity_Id first_formal, Entity_Id result_type)
4520{
4521 Entity_Id formal;
4522
4523 for (formal = first_formal;
4524 Present (formal);
4525 formal = Next_Formal_With_Extras (formal))
4526 (void) gnat_to_gnu_type (Etype (formal));
4527
4528 if (Present (result_type) && Ekind (result_type) != E_Void)
4529 (void) gnat_to_gnu_type (result_type);
4530}
4531
e08f1aad 4532/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Function_Call
a1ab4c31
AC
4533 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
4534 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
0b3467c4 4535 If GNU_TARGET is non-null, this must be a function call on the RHS of a
033ba5bf 4536 N_Assignment_Statement and the result is to be placed into that object.
17a98a3d
EB
4537 ATOMIC_ACCESS is the type of atomic access to be used for the assignment
4538 to GNU_TARGET. If, in addition, ATOMIC_SYNC is true, then the assignment
544d14e1
EB
4539 to GNU_TARGET requires atomic synchronization. GNAT_STORAGE_MODEL is the
4540 storage model object to be used for the assignment to GNU_TARGET or Empty
4541 if there is none. */
a1ab4c31
AC
4542
4543static tree
80096613 4544Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
544d14e1
EB
4545 atomic_acces_t atomic_access, bool atomic_sync,
4546 Entity_Id gnat_storage_model)
a1ab4c31 4547{
ddb5a105
EB
4548 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
4549 const bool returning_value = (function_call && !gnu_target);
a1ab4c31
AC
4550 /* The GCC node corresponding to the GNAT subprogram name. This can either
4551 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
4552 or an indirect reference expression (an INDIRECT_REF node) pointing to a
4553 subprogram. */
298339ef
EB
4554 const Node_Id gnat_subprog = Name (gnat_node);
4555 tree gnu_subprog = gnat_to_gnu (gnat_subprog);
a1ab4c31 4556 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
ced57283 4557 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
ddb5a105 4558 /* The return type of the FUNCTION_TYPE. */
a42dd9fe 4559 tree gnu_result_type;;
b9778c45
EB
4560 const bool frontend_builtin
4561 = (TREE_CODE (gnu_subprog) == FUNCTION_DECL
4562 && DECL_BUILT_IN_CLASS (gnu_subprog) == BUILT_IN_FRONTEND);
4563 auto_vec<tree, 16> gnu_actual_vec;
a1ab4c31 4564 tree gnu_name_list = NULL_TREE;
ddb5a105 4565 tree gnu_stmt_list = NULL_TREE;
a1ab4c31 4566 tree gnu_after_list = NULL_TREE;
ddb5a105 4567 tree gnu_retval = NULL_TREE;
35a382b8 4568 tree gnu_call, gnu_result;
f53aff92
EB
4569 bool went_into_elab_proc;
4570 bool pushed_binding_level;
c95f808d
EB
4571 bool variadic;
4572 bool by_descriptor;
ddb5a105 4573 Entity_Id gnat_formal;
a42dd9fe 4574 Entity_Id gnat_result_type;
ddb5a105 4575 Node_Id gnat_actual;
17a98a3d
EB
4576 atomic_acces_t aa_type;
4577 bool aa_sync;
544d14e1 4578 Entity_Id gnat_smo;
a1ab4c31 4579
f15ad1e3 4580 /* The only way we can make a call via an access type is if GNAT_NAME is an
a1ab4c31 4581 explicit dereference. In that case, get the list of formal args from the
ced57283 4582 type the access type is pointing to. Otherwise, get the formals from the
a1ab4c31 4583 entity being called. */
298339ef 4584 if (Nkind (gnat_subprog) == N_Explicit_Dereference)
4c640e26 4585 {
c95f808d 4586 const Entity_Id gnat_prefix_type
298339ef 4587 = Underlying_Type (Etype (Prefix (gnat_subprog)));
c95f808d 4588
298339ef 4589 gnat_formal = First_Formal_With_Extras (Etype (gnat_subprog));
a42dd9fe 4590 gnat_result_type = Etype (Etype (gnat_subprog));
c95f808d 4591 variadic = IN (Convention (gnat_prefix_type), Convention_C_Variadic);
4c640e26
EB
4592
4593 /* If the access type doesn't require foreign-compatible representation,
4594 be prepared for descriptors. */
c95f808d
EB
4595 by_descriptor
4596 = targetm.calls.custom_function_descriptors > 0
4597 && Can_Use_Internal_Rep (gnat_prefix_type);
4c640e26 4598 }
f15ad1e3 4599
298339ef 4600 else if (Nkind (gnat_subprog) == N_Attribute_Reference)
c95f808d
EB
4601 {
4602 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
4603 gnat_formal = Empty;
a42dd9fe 4604 gnat_result_type = Empty;
c95f808d
EB
4605 variadic = false;
4606 by_descriptor = false;
4607 }
f15ad1e3 4608
a1ab4c31 4609 else
c95f808d 4610 {
298339ef 4611 gcc_checking_assert (Is_Entity_Name (gnat_subprog));
f15ad1e3 4612
298339ef 4613 gnat_formal = First_Formal_With_Extras (Entity (gnat_subprog));
a42dd9fe 4614 gnat_result_type = Etype (Entity_Id (gnat_subprog));
298339ef 4615 variadic = IN (Convention (Entity (gnat_subprog)), Convention_C_Variadic);
c95f808d 4616 by_descriptor = false;
f15ad1e3
EB
4617
4618 /* If we are calling a stubbed function, then raise Program_Error, but
4619 elaborate all our args first. */
298339ef 4620 if (Convention (Entity (gnat_subprog)) == Convention_Stubbed)
f15ad1e3
EB
4621 {
4622 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
4623 gnat_node, N_Raise_Program_Error);
4624
4625 for (gnat_actual = First_Actual (gnat_node);
4626 Present (gnat_actual);
4627 gnat_actual = Next_Actual (gnat_actual))
4628 add_stmt (gnat_to_gnu (gnat_actual));
4629
4630 if (returning_value)
4631 {
a42dd9fe 4632 gnu_result_type = TREE_TYPE (gnu_subprog_type);
f15ad1e3
EB
4633 *gnu_result_type_p = gnu_result_type;
4634 return build1 (NULL_EXPR, gnu_result_type, call_expr);
4635 }
4636
4637 return call_expr;
4638 }
4639 }
4640
a42dd9fe
EB
4641 /* We must elaborate the entire profile now because, if it references types
4642 that were initially incomplete,, their elaboration changes the contents
4643 of GNU_SUBPROG_TYPE and, in particular, may change the result type. */
4644 elaborate_profile (gnat_formal, gnat_result_type);
4645
f15ad1e3 4646 gcc_assert (FUNC_OR_METHOD_TYPE_P (gnu_subprog_type));
a42dd9fe 4647 gnu_result_type = TREE_TYPE (gnu_subprog_type);
f15ad1e3
EB
4648
4649 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL)
4650 {
4651 /* For a call to a nested function, check the inlining status. */
4652 if (decl_function_context (gnu_subprog))
4653 check_inlining_for_nested_subprog (gnu_subprog);
4654
4655 /* For a recursive call, avoid explosion due to recursive inlining. */
4656 if (gnu_subprog == current_function_decl)
4657 DECL_DISREGARD_INLINE_LIMITS (gnu_subprog) = 0;
c95f808d 4658 }
a1ab4c31 4659
ddb5a105
EB
4660 /* The lifetime of the temporaries created for the call ends right after the
4661 return value is copied, so we can give them the scope of the elaboration
4662 routine at top level. */
35a382b8 4663 if (!current_function_decl)
0b3467c4 4664 {
2231f17f 4665 current_function_decl = get_elaboration_procedure ();
0b3467c4
EB
4666 went_into_elab_proc = true;
4667 }
f53aff92
EB
4668 else
4669 went_into_elab_proc = false;
0b3467c4 4670
4a582c9f
EB
4671 /* First, create the temporary for the return value when:
4672
4673 1. There is no target and the function has copy-in/copy-out parameters,
4674 because we need to preserve the return value before copying back the
4675 parameters.
4676
b81e2d5e
EB
4677 2. There is no target and the call is made for neither the declaration
4678 of an object (regular or renaming), nor a return statement, nor an
4679 allocator, nor an aggregate, and the return type has variable size
4680 because in this case the gimplifier cannot create the temporary, or
4681 more generally is an aggregate type, because the gimplifier would
4682 create the temporary in the outermost scope instead of locally here.
4683 But there is an exception for an allocator of unconstrained record
4684 type with default discriminant because we allocate the actual size
4685 in this case, unlike in the other cases, so we need a temporary to
4686 fetch the discriminant and we create it here.
4a582c9f
EB
4687
4688 3. There is a target and it is a slice or an array with fixed size,
4689 and the return type has variable size, because the gimplifier
4690 doesn't handle these cases.
4691
02221bed
EB
4692 4. There is a target which is a bit-field and the function returns an
4693 unconstrained record type with default discriminant, because the
4694 return may copy more data than the bit-field can contain.
4695
544d14e1
EB
4696 5. There is a target which needs to be accessed with a storage model.
4697
4698 6. There is no target and we have misaligned In Out or Out parameters
5d2a63dc
EB
4699 passed by reference, because we need to preserve the return value
4700 before copying back the parameters. However, in this case, we'll
4701 defer creating the temporary, see below.
4702
4a582c9f
EB
4703 This must be done before we push a binding level around the call, since
4704 we will pop it before copying the return value. */
ddb5a105 4705 if (function_call
4a582c9f
EB
4706 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
4707 || (!gnu_target
4708 && Nkind (Parent (gnat_node)) != N_Object_Declaration
fc7a823e 4709 && Nkind (Parent (gnat_node)) != N_Object_Renaming_Declaration
5d2a63dc 4710 && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement
1b6f8e97
EB
4711 && (!(Nkind (Parent (gnat_node)) == N_Qualified_Expression
4712 && Nkind (Parent (Parent (gnat_node))) == N_Allocator)
4713 || type_is_padding_self_referential (gnu_result_type))
b81e2d5e 4714 && Nkind (Parent (gnat_node)) != N_Aggregate
5d2a63dc
EB
4715 && AGGREGATE_TYPE_P (gnu_result_type)
4716 && !TYPE_IS_FAT_POINTER_P (gnu_result_type))
4a582c9f
EB
4717 || (gnu_target
4718 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
4719 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
4720 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
4721 == INTEGER_CST))
02221bed
EB
4722 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)
4723 || (gnu_target
4724 && TREE_CODE (gnu_target) == COMPONENT_REF
4725 && DECL_BIT_FIELD (TREE_OPERAND (gnu_target, 1))
1e66a917
EB
4726 && DECL_SIZE (TREE_OPERAND (gnu_target, 1))
4727 != TYPE_SIZE (TREE_TYPE (gnu_target))
544d14e1
EB
4728 && type_is_padding_self_referential (gnu_result_type))
4729 || (gnu_target
4730 && Present (gnat_storage_model)
4731 && Present (Storage_Model_Copy_To (gnat_storage_model)))))
93e708f9
EB
4732 {
4733 gnu_retval = create_temporary ("R", gnu_result_type);
4734 DECL_RETURN_VALUE_P (gnu_retval) = 1;
4735 }
ddb5a105 4736
5d2a63dc
EB
4737 /* If we don't need a value or have already created it, push a binding level
4738 around the call. This will narrow the lifetime of the temporaries we may
4739 need to make when translating the parameters as much as possible. */
4740 if (!returning_value || gnu_retval)
4741 {
4742 start_stmt_group ();
4743 gnat_pushlevel ();
4744 pushed_binding_level = true;
4745 }
f53aff92
EB
4746 else
4747 pushed_binding_level = false;
5d2a63dc 4748
ced57283
EB
4749 /* Create the list of the actual parameters as GCC expects it, namely a
4750 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
4751 is an expression and the TREE_PURPOSE field is null. But skip Out
4752 parameters not passed by reference and that need not be copied in. */
a1ab4c31
AC
4753 for (gnat_actual = First_Actual (gnat_node);
4754 Present (gnat_actual);
4755 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4756 gnat_actual = Next_Actual (gnat_actual))
4757 {
19f51f28 4758 Entity_Id gnat_formal_type = Etype (gnat_formal);
7cdb6871 4759 tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type);
ced57283
EB
4760 tree gnu_formal = present_gnu_tree (gnat_formal)
4761 ? get_gnu_tree (gnat_formal) : NULL_TREE;
6e03839f 4762 const bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
c946adde
EB
4763 const bool is_true_formal_parm
4764 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
033ba5bf 4765 const bool is_by_ref_formal_parm
1eb58520
AC
4766 = is_true_formal_parm
4767 && (DECL_BY_REF_P (gnu_formal)
4768 || DECL_BY_COMPONENT_PTR_P (gnu_formal));
6e03839f 4769 /* In the In Out or Out case, we must suppress conversions that yield
c34f3839
EB
4770 an lvalue but can nevertheless cause the creation of a temporary,
4771 because we need the real object in this case, either to pass its
4772 address if it's passed by reference or as target of the back copy
ddb5a105 4773 done after the call if it uses the copy-in/copy-out mechanism.
c34f3839 4774 We do it in the In case too, except for an unchecked conversion
19f51f28
EB
4775 to an elementary type or a constrained composite type because it
4776 alone can cause the actual to be misaligned and the addressability
4777 test is applied to the real object. */
c946adde 4778 const bool suppress_type_conversion
a1ab4c31 4779 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
6e03839f 4780 && (!in_param
1ab1c4ee 4781 || !is_by_ref_formal_parm
19f51f28
EB
4782 || (Is_Composite_Type (Underlying_Type (gnat_formal_type))
4783 && !Is_Constrained (Underlying_Type (gnat_formal_type)))))
a1ab4c31 4784 || (Nkind (gnat_actual) == N_Type_Conversion
19f51f28 4785 && Is_Composite_Type (Underlying_Type (gnat_formal_type))));
ced57283
EB
4786 Node_Id gnat_name = suppress_type_conversion
4787 ? Expression (gnat_actual) : gnat_actual;
a1ab4c31 4788 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
a1ab4c31
AC
4789
4790 /* If it's possible we may need to use this expression twice, make sure
ced57283 4791 that any side-effects are handled via SAVE_EXPRs; likewise if we need
7194767c 4792 to force side-effects before the call. */
6e03839f 4793 if (!in_param && !is_by_ref_formal_parm)
fc7a823e
EB
4794 {
4795 tree init = NULL_TREE;
4796 gnu_name = gnat_stabilize_reference (gnu_name, true, &init);
4797 if (init)
4798 gnu_name
4799 = build_compound_expr (TREE_TYPE (gnu_name), init, gnu_name);
4800 }
a1ab4c31 4801
544d14e1
EB
4802 get_storage_model_access (gnat_actual, &gnat_smo);
4803
4804 /* If we are passing a non-addressable actual parameter by reference,
4805 pass the address of a copy. Likewise if it needs to be accessed with
4806 a storage model. In the In Out or Out case, set up to copy back out
4807 after the call. */
033ba5bf 4808 if (is_by_ref_formal_parm
a1ab4c31 4809 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
544d14e1
EB
4810 && (!addressable_p (gnu_name, gnu_name_type)
4811 || (Present (gnat_smo)
4812 && (Present (Storage_Model_Copy_From (gnat_smo))
4813 || (!in_param
4814 && Present (Storage_Model_Copy_To (gnat_smo)))))))
a1ab4c31 4815 {
0b3467c4
EB
4816 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4817
4818 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4819 but sort of an instantiation for them. */
1edbeb15 4820 if (TREE_CODE (remove_conversions (gnu_name, true)) == CONSTRUCTOR)
0b3467c4
EB
4821 ;
4822
1edbeb15
EB
4823 /* If the formal is passed by reference, a copy is not allowed. */
4824 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type)
4825 || Is_Aliased (gnat_formal))
0b3467c4
EB
4826 post_error ("misaligned actual cannot be passed by reference",
4827 gnat_actual);
4828
1edbeb15
EB
4829 /* If the mechanism was forced to by-ref, a copy is not allowed but
4830 we issue only a warning because this case is not strict Ada. */
4831 else if (DECL_FORCED_BY_REF_P (gnu_formal))
4832 post_error ("misaligned actual cannot be passed by reference??",
4833 gnat_actual);
a1ab4c31 4834
56fe7b05
EB
4835 /* If the actual type of the object is already the nominal type,
4836 we have nothing to do, except if the size is self-referential
4837 in which case we'll remove the unpadding below. */
4838 if (TREE_TYPE (gnu_name) == gnu_name_type
4839 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4840 ;
4841
0b3467c4 4842 /* Otherwise remove the unpadding from all the objects. */
56fe7b05 4843 else if (TREE_CODE (gnu_name) == COMPONENT_REF
315cff15
EB
4844 && TYPE_IS_PADDING_P
4845 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
0b3467c4 4846 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
a1ab4c31 4847
169afcb9
EB
4848 /* Otherwise convert to the nominal type of the object if needed.
4849 There are several cases in which we need to make the temporary
4850 using this type instead of the actual type of the object when
4851 they are distinct, because the expectations of the callee would
4852 otherwise not be met:
a1ab4c31 4853 - if it's a justified modular type,
169afcb9
EB
4854 - if the actual type is a smaller form of it,
4855 - if it's a smaller form of the actual type. */
4856 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4857 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4858 || smaller_form_type_p (TREE_TYPE (gnu_name),
4859 gnu_name_type)))
4860 || (INTEGRAL_TYPE_P (gnu_name_type)
4861 && smaller_form_type_p (gnu_name_type,
4862 TREE_TYPE (gnu_name))))
a1ab4c31
AC
4863 gnu_name = convert (gnu_name_type, gnu_name);
4864
ddb5a105
EB
4865 /* If this is an In Out or Out parameter and we're returning a value,
4866 we need to create a temporary for the return value because we must
4867 preserve it before copying back at the very end. */
4868 if (!in_param && returning_value && !gnu_retval)
93e708f9
EB
4869 {
4870 gnu_retval = create_temporary ("R", gnu_result_type);
4871 DECL_RETURN_VALUE_P (gnu_retval) = 1;
4872 }
ddb5a105 4873
5d2a63dc
EB
4874 /* If we haven't pushed a binding level, push it now. This will
4875 narrow the lifetime of the temporary we are about to make as
4876 much as possible. */
4877 if (!pushed_binding_level && (!returning_value || gnu_retval))
35a382b8
EB
4878 {
4879 start_stmt_group ();
4880 gnat_pushlevel ();
4881 pushed_binding_level = true;
4882 }
4883
ddb5a105 4884 /* Create an explicit temporary holding the copy. */
544d14e1
EB
4885 tree gnu_temp_type;
4886 if (Nkind (gnat_actual) == N_Explicit_Dereference
4887 && Present (Actual_Designated_Subtype (gnat_actual)))
4888 gnu_temp_type
4889 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_actual));
4890 else
4891 gnu_temp_type = TREE_TYPE (gnu_name);
4892
8da3776f
EB
4893 /* Do not initialize it for the _Init parameter of an initialization
4894 procedure since no data is meant to be passed in. */
4895 if (Ekind (gnat_formal) == E_Out_Parameter
298339ef
EB
4896 && Is_Entity_Name (gnat_subprog)
4897 && Is_Init_Proc (Entity (gnat_subprog)))
544d14e1 4898 gnu_name = gnu_temp = create_temporary ("A", gnu_temp_type);
8da3776f
EB
4899
4900 /* Initialize it on the fly like for an implicit temporary in the
4901 other cases, as we don't necessarily have a statement list. */
4902 else
4903 {
544d14e1
EB
4904 if (Present (gnat_smo)
4905 && Present (Storage_Model_Copy_From (gnat_smo)))
4906 {
4907 gnu_temp = create_temporary ("A", gnu_temp_type);
4908 gnu_stmt
4909 = build_storage_model_load (gnat_smo, gnu_temp,
4910 gnu_name,
4911 TYPE_SIZE_UNIT (gnu_temp_type));
4912 set_expr_location_from_node (gnu_stmt, gnat_actual);
4913 }
4914 else
4915 gnu_temp = create_init_temporary ("A", gnu_name, &gnu_stmt,
4916 gnat_actual);
4917
4918 gnu_name = build_compound_expr (gnu_temp_type, gnu_stmt,
8da3776f
EB
4919 gnu_temp);
4920 }
cb3d597d 4921
ced57283 4922 /* Set up to move the copy back to the original if needed. */
35a382b8 4923 if (!in_param)
a1ab4c31 4924 {
bb29e9ff
EB
4925 /* If the original is a COND_EXPR whose first arm isn't meant to
4926 be further used, just deal with the second arm. This is very
4927 likely the conditional expression built for a check. */
4928 if (TREE_CODE (gnu_orig) == COND_EXPR
4929 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4930 && integer_zerop
4931 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4932 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4933
544d14e1
EB
4934 if (Present (gnat_smo)
4935 && Present (Storage_Model_Copy_To (gnat_smo)))
4936 gnu_stmt
4937 = build_storage_model_store (gnat_smo, gnu_orig,
4938 gnu_temp,
4939 TYPE_SIZE_UNIT (gnu_temp_type));
4940 else
4941 gnu_stmt
4942 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig,
4943 gnu_temp);
0b3467c4 4944 set_expr_location_from_node (gnu_stmt, gnat_node);
bb29e9ff 4945
0b3467c4 4946 append_to_statement_list (gnu_stmt, &gnu_after_list);
a1ab4c31
AC
4947 }
4948 }
4949
4950 /* Start from the real object and build the actual. */
8234d02a 4951 tree gnu_actual = gnu_name;
a1ab4c31 4952
f797c2b7 4953 /* If atomic access is required for an In or In Out actual parameter,
544d14e1
EB
4954 build the atomic load. Or else, if storage model access is required,
4955 build the special load. */
033ba5bf
EB
4956 if (is_true_formal_parm
4957 && !is_by_ref_formal_parm
544d14e1
EB
4958 && Ekind (gnat_formal) != E_Out_Parameter)
4959 {
4960 if (simple_atomic_access_required_p (gnat_actual, &aa_sync))
4961 gnu_actual = build_atomic_load (gnu_actual, aa_sync);
4962
4963 else if (Present (gnat_smo)
4964 && Present (Storage_Model_Copy_From (gnat_smo)))
4965 gnu_actual = build_storage_model_load (gnat_smo, gnu_actual);
4966 }
033ba5bf 4967
a1ab4c31
AC
4968 /* If this was a procedure call, we may not have removed any padding.
4969 So do it here for the part we will use as an input, if any. */
4970 if (Ekind (gnat_formal) != E_Out_Parameter
a1ab4c31 4971 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
c34f3839
EB
4972 gnu_actual
4973 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4974
4975 /* Put back the conversion we suppressed above in the computation of the
4976 real object. And even if we didn't suppress any conversion there, we
4977 may have suppressed a conversion to the Etype of the actual earlier,
8234d02a
EB
4978 since the parent is a procedure call, so put it back here. Note that
4979 we might have a dummy type here if the actual is the dereference of a
fccc47dd
EB
4980 pointer to it, but that's OK when the formal is passed by reference.
4981 We also do not put back a conversion between an actual and a formal
4982 that are unconstrained array types to avoid creating local bounds. */
7a0877c0 4983 tree gnu_actual_type = get_unpadded_type (Etype (gnat_actual));
8234d02a
EB
4984 if (TYPE_IS_DUMMY_P (gnu_actual_type))
4985 gcc_assert (is_true_formal_parm && DECL_BY_REF_P (gnu_formal));
4986 else if (suppress_type_conversion
4987 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4988 gnu_actual = unchecked_convert (gnu_actual_type, gnu_actual,
4989 No_Truncation (gnat_actual));
fccc47dd
EB
4990 else if ((TREE_CODE (TREE_TYPE (gnu_actual)) == UNCONSTRAINED_ARRAY_TYPE
4991 || (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4992 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))))
4993 && TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4994 ;
a1ab4c31 4995 else
8234d02a 4996 gnu_actual = convert (gnu_actual_type, gnu_actual);
c34f3839 4997
815b5368 4998 gigi_checking_assert (!Do_Range_Check (gnat_actual));
a1ab4c31 4999
96540b24 5000 /* First see if the parameter is passed by reference. */
c946adde 5001 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
a1ab4c31 5002 {
6e03839f 5003 if (!in_param)
a1ab4c31
AC
5004 {
5005 /* In Out or Out parameters passed by reference don't use the
ddb5a105 5006 copy-in/copy-out mechanism so the address of the real object
a1ab4c31
AC
5007 must be passed to the function. */
5008 gnu_actual = gnu_name;
5009
5010 /* If we have a padded type, be sure we've removed padding. */
0b3467c4 5011 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
a1ab4c31
AC
5012 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
5013 gnu_actual);
5014
5015 /* If we have the constructed subtype of an aliased object
5016 with an unconstrained nominal subtype, the type of the
5017 actual includes the template, although it is formally
5018 constrained. So we need to convert it back to the real
5019 constructed subtype to retrieve the constrained part
5020 and takes its address. */
5021 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
5022 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
a1ab4c31 5023 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
43a4dd82 5024 && Is_Array_Type (Underlying_Type (Etype (gnat_actual))))
6e03839f 5025 gnu_actual = convert (gnu_actual_type, gnu_actual);
a1ab4c31
AC
5026 }
5027
0b3467c4
EB
5028 /* There is no need to convert the actual to the formal's type before
5029 taking its address. The only exception is for unconstrained array
5030 types because of the way we build fat pointers. */
7bf9a5ac
EB
5031 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
5032 {
6e03839f
EB
5033 /* Put back the conversion we suppressed above for In Out or Out
5034 parameters, since it may set the bounds of the actual. */
5035 if (!in_param && suppress_type_conversion)
5036 gnu_actual = convert (gnu_actual_type, gnu_actual);
7bf9a5ac
EB
5037 gnu_actual = convert (gnu_formal_type, gnu_actual);
5038 }
0b3467c4 5039
6e03839f
EB
5040 /* Take the address of the object and convert to the proper pointer
5041 type. */
7bf9a5ac 5042 gnu_formal_type = TREE_TYPE (gnu_formal);
a1ab4c31
AC
5043 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
5044 }
96540b24
EB
5045
5046 /* Then see if the parameter is an array passed to a foreign convention
5047 subprogram. */
c946adde 5048 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
a1ab4c31 5049 {
bb24f343 5050 gnu_actual = maybe_padded_object (gnu_actual);
a1ab4c31
AC
5051 gnu_actual = maybe_unconstrained_array (gnu_actual);
5052
a1ab4c31
AC
5053 /* Take the address of the object and convert to the proper pointer
5054 type. We'd like to actually compute the address of the beginning
5055 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
5056 possibility that the ARRAY_REF might return a constant and we'd be
5057 getting the wrong address. Neither approach is exactly correct,
5058 but this is the most likely to work in all cases. */
6e03839f 5059 gnu_formal_type = TREE_TYPE (gnu_formal);
0b3467c4 5060 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
a1ab4c31 5061 }
96540b24 5062
c95f808d
EB
5063 /* Then see if the parameter is passed by copy. */
5064 else if (is_true_formal_parm)
a1ab4c31 5065 {
6e03839f 5066 if (!in_param)
a1ab4c31
AC
5067 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
5068
0b3467c4
EB
5069 gnu_actual = convert (gnu_formal_type, gnu_actual);
5070
b9778c45
EB
5071 /* If this is a front-end built-in function, there is no need to
5072 convert to the type used to pass the argument. */
ad00a297 5073 if (!frontend_builtin)
a1ab4c31
AC
5074 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
5075 }
5076
c95f808d
EB
5077 /* Then see if this is an unnamed parameter in a variadic C function. */
5078 else if (variadic)
5079 {
5080 /* This is based on the processing done in gnat_to_gnu_param, but
5081 we expect the mechanism to be set in (almost) all cases. */
5082 const Mechanism_Type mech = Mechanism (gnat_formal);
5083
5084 /* Strip off possible padding type. */
5085 if (TYPE_IS_PADDING_P (gnu_formal_type))
5086 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
5087
5088 /* Arrays are passed as pointers to element type. First check for
5089 unconstrained array and get the underlying array. */
5090 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
5091 gnu_formal_type
5092 = TREE_TYPE
5093 (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_formal_type))));
5094
5095 /* Arrays are passed as pointers to element type. */
5096 if (mech != By_Copy && TREE_CODE (gnu_formal_type) == ARRAY_TYPE)
5097 {
bb24f343 5098 gnu_actual = maybe_padded_object (gnu_actual);
c95f808d
EB
5099 gnu_actual = maybe_unconstrained_array (gnu_actual);
5100
5101 /* Strip off any multi-dimensional entries, then strip
5102 off the last array to get the component type. */
5103 while (TREE_CODE (TREE_TYPE (gnu_formal_type)) == ARRAY_TYPE
5104 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_formal_type)))
5105 gnu_formal_type = TREE_TYPE (gnu_formal_type);
5106
5107 gnu_formal_type = TREE_TYPE (gnu_formal_type);
5108 gnu_formal_type = build_pointer_type (gnu_formal_type);
5109 gnu_actual
5110 = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
5111 }
5112
5113 /* Fat pointers are passed as thin pointers. */
5114 else if (TYPE_IS_FAT_POINTER_P (gnu_formal_type))
5115 gnu_formal_type
5116 = make_type_from_size (gnu_formal_type,
5117 size_int (POINTER_SIZE), 0);
5118
5119 /* If we were requested or muss pass by reference, do so.
5120 If we were requested to pass by copy, do so.
5121 Otherwise, pass In Out or Out parameters or aggregates by
5122 reference. */
5123 else if (mech == By_Reference
5124 || must_pass_by_ref (gnu_formal_type)
5125 || (mech != By_Copy
5126 && (!in_param || AGGREGATE_TYPE_P (gnu_formal_type))))
5127 {
5128 gnu_formal_type = build_reference_type (gnu_formal_type);
5129 gnu_actual
5130 = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
5131 }
5132
5133 /* Otherwise pass by copy after applying default C promotions. */
5134 else
5135 {
5136 if (INTEGRAL_TYPE_P (gnu_formal_type)
5137 && TYPE_PRECISION (gnu_formal_type)
5138 < TYPE_PRECISION (integer_type_node))
5139 gnu_formal_type = integer_type_node;
5140
5141 else if (SCALAR_FLOAT_TYPE_P (gnu_formal_type)
5142 && TYPE_PRECISION (gnu_formal_type)
5143 < TYPE_PRECISION (double_type_node))
5144 gnu_formal_type = double_type_node;
5145 }
5146
5147 gnu_actual = convert (gnu_formal_type, gnu_actual);
5148 }
5149
5150 /* If we didn't create a PARM_DECL for the formal, this means that
5151 it is an Out parameter not passed by reference and that need not
5152 be copied in. In this case, the value of the actual need not be
5153 read. However, we still need to make sure that its side-effects
5154 are evaluated before the call, so we evaluate its address. */
5155 else
5156 {
5157 if (!in_param)
5158 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
5159
5160 if (TREE_SIDE_EFFECTS (gnu_name))
5161 {
5162 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
5163 append_to_statement_list (addr, &gnu_stmt_list);
5164 }
5165
5166 continue;
5167 }
5168
b9778c45
EB
5169 gnu_actual_vec.safe_push (gnu_actual);
5170 }
5171
5172 if (frontend_builtin)
5173 {
5174 tree pred_cst = build_int_cst (integer_type_node, PRED_BUILTIN_EXPECT);
5175 enum internal_fn icode = IFN_BUILTIN_EXPECT;
5176
4d732405 5177 switch (DECL_FE_FUNCTION_CODE (gnu_subprog))
b9778c45
EB
5178 {
5179 case BUILT_IN_EXPECT:
5180 break;
5181 case BUILT_IN_LIKELY:
5182 gnu_actual_vec.safe_push (boolean_true_node);
5183 break;
5184 case BUILT_IN_UNLIKELY:
5185 gnu_actual_vec.safe_push (boolean_false_node);
5186 break;
5187 default:
5188 gcc_unreachable ();
5189 }
5190
5191 gnu_actual_vec.safe_push (pred_cst);
5192
5193 gnu_call
5194 = build_call_expr_internal_loc_array (UNKNOWN_LOCATION,
5195 icode,
5196 gnu_result_type,
5197 gnu_actual_vec.length (),
5198 gnu_actual_vec.begin ());
5199 }
5200 else
5201 {
5202 gnu_call
5203 = build_call_array_loc (UNKNOWN_LOCATION,
5204 gnu_result_type,
5205 build_unary_op (ADDR_EXPR, NULL_TREE,
5206 gnu_subprog),
5207 gnu_actual_vec.length (),
5208 gnu_actual_vec.begin ());
5209 CALL_EXPR_BY_DESCRIPTOR (gnu_call) = by_descriptor;
a1ab4c31
AC
5210 }
5211
ced57283 5212 set_expr_location_from_node (gnu_call, gnat_node);
a1ab4c31 5213
ddb5a105
EB
5214 /* If we have created a temporary for the return value, initialize it. */
5215 if (gnu_retval)
5216 {
5217 tree gnu_stmt
5218 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
5219 set_expr_location_from_node (gnu_stmt, gnat_node);
5220 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
5221 gnu_call = gnu_retval;
5222 }
5223
35a382b8
EB
5224 /* If this is a subprogram with copy-in/copy-out parameters, we need to
5225 unpack the valued returned from the function into the In Out or Out
5226 parameters. We deal with the function return (if this is an Ada
5227 function) below. */
d47d0a8d 5228 if (TYPE_CI_CO_LIST (gnu_subprog_type))
a1ab4c31 5229 {
0b3467c4
EB
5230 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
5231 copy-out parameters. */
a09d56d8
EB
5232 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
5233 const int length = list_length (gnu_cico_list);
a1ab4c31 5234
35a382b8
EB
5235 /* The call sequence must contain one and only one call, even though the
5236 function is pure. Save the result into a temporary if needed. */
a1ab4c31
AC
5237 if (length > 1)
5238 {
ddb5a105
EB
5239 if (!gnu_retval)
5240 {
5241 tree gnu_stmt;
ddb5a105
EB
5242 gnu_call
5243 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
5244 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
5245 }
0b3467c4 5246
a1ab4c31 5247 gnu_name_list = nreverse (gnu_name_list);
a1ab4c31
AC
5248 }
5249
35a382b8
EB
5250 /* The first entry is for the actual return value if this is a
5251 function, so skip it. */
b83053bf 5252 if (function_call)
35a382b8
EB
5253 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
5254
298339ef
EB
5255 if (Nkind (gnat_subprog) == N_Explicit_Dereference)
5256 gnat_formal = First_Formal_With_Extras (Etype (gnat_subprog));
a1ab4c31 5257 else
298339ef 5258 gnat_formal = First_Formal_With_Extras (Entity (gnat_subprog));
a1ab4c31
AC
5259
5260 for (gnat_actual = First_Actual (gnat_node);
5261 Present (gnat_actual);
5262 gnat_formal = Next_Formal_With_Extras (gnat_formal),
5263 gnat_actual = Next_Actual (gnat_actual))
35a382b8 5264 /* If we are dealing with a copy-in/copy-out parameter, we must
a1ab4c31
AC
5265 retrieve its value from the record returned in the call. */
5266 if (!(present_gnu_tree (gnat_formal)
5267 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
5268 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
1eb58520 5269 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))))
a1ab4c31
AC
5270 && Ekind (gnat_formal) != E_In_Parameter)
5271 {
6e03839f 5272 /* Get the value to assign to this In Out or Out parameter. It is
a1ab4c31
AC
5273 either the result of the function if there is only a single such
5274 parameter or the appropriate field from the record returned. */
5275 tree gnu_result
ced57283
EB
5276 = length == 1
5277 ? gnu_call
64235766
EB
5278 : build_component_ref (gnu_call, TREE_PURPOSE (gnu_cico_list),
5279 false);
a1ab4c31
AC
5280
5281 /* If the actual is a conversion, get the inner expression, which
5282 will be the real destination, and convert the result to the
5283 type of the actual parameter. */
5284 tree gnu_actual
5285 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
5286
ad00a297
EB
5287 /* If the result is padded, remove the padding. */
5288 gnu_result = maybe_padded_object (gnu_result);
a1ab4c31
AC
5289
5290 /* If the actual is a type conversion, the real target object is
5291 denoted by the inner Expression and we need to convert the
5292 result to the associated type.
5293 We also need to convert our gnu assignment target to this type
5294 if the corresponding GNU_NAME was constructed from the GNAT
5295 conversion node and not from the inner Expression. */
5296 if (Nkind (gnat_actual) == N_Type_Conversion)
5297 {
815b5368
EB
5298 const Node_Id gnat_expr = Expression (gnat_actual);
5299
5300 gigi_checking_assert (!Do_Range_Check (gnat_expr));
5301
a1ab4c31 5302 gnu_result
815b5368
EB
5303 = convert_with_check (Etype (gnat_expr), gnu_result,
5304 Do_Overflow_Check (gnat_actual),
5305 Float_Truncate (gnat_actual),
5306 gnat_actual);
a1ab4c31
AC
5307
5308 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
5309 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
5310 }
5311
5312 /* Unchecked conversions as actuals for Out parameters are not
5313 allowed in user code because they are not variables, but do
5314 occur in front-end expansions. The associated GNU_NAME is
5315 always obtained from the inner expression in such cases. */
5316 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
5317 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
5318 gnu_result,
5319 No_Truncation (gnat_actual));
5320 else
5321 {
815b5368 5322 gigi_checking_assert (!Do_Range_Check (gnat_actual));
a1ab4c31
AC
5323
5324 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
5325 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
5326 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
5327 }
5328
17a98a3d 5329 get_atomic_access (gnat_actual, &aa_type, &aa_sync);
544d14e1 5330 get_storage_model_access (gnat_actual, &gnat_smo);
17a98a3d 5331
f797c2b7
EB
5332 /* If an outer atomic access is required for an actual parameter,
5333 build the load-modify-store sequence. */
17a98a3d 5334 if (aa_type == OUTER_ATOMIC)
f797c2b7
EB
5335 gnu_result
5336 = build_load_modify_store (gnu_actual, gnu_result, gnat_node);
5337
17a98a3d 5338 /* Or else, if a simple atomic access is required, build the atomic
f797c2b7 5339 store. */
17a98a3d
EB
5340 else if (aa_type == SIMPLE_ATOMIC)
5341 gnu_result
5342 = build_atomic_store (gnu_actual, gnu_result, aa_sync);
f797c2b7 5343
544d14e1
EB
5344 /* Or else, if a storage model access is required, build the special
5345 store. */
5346 else if (Present (gnat_smo)
5347 && Present (Storage_Model_Copy_To (gnat_smo)))
5348 gnu_result
5349 = build_storage_model_store (gnat_smo, gnu_actual, gnu_result);
5350
f797c2b7 5351 /* Otherwise build a regular assignment. */
033ba5bf
EB
5352 else
5353 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
5354 gnu_actual, gnu_result);
f797c2b7
EB
5355
5356 if (EXPR_P (gnu_result))
5357 set_expr_location_from_node (gnu_result, gnat_node);
ddb5a105 5358 append_to_statement_list (gnu_result, &gnu_stmt_list);
a09d56d8 5359 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
a1ab4c31
AC
5360 gnu_name_list = TREE_CHAIN (gnu_name_list);
5361 }
ced57283 5362 }
35a382b8
EB
5363
5364 /* If this is a function call, the result is the call expression unless a
5365 target is specified, in which case we copy the result into the target
5366 and return the assignment statement. */
ddb5a105 5367 if (function_call)
35a382b8 5368 {
35a382b8
EB
5369 /* If this is a function with copy-in/copy-out parameters, extract the
5370 return value from it and update the return type. */
5371 if (TYPE_CI_CO_LIST (gnu_subprog_type))
5372 {
b83053bf 5373 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
64235766
EB
5374 gnu_call
5375 = build_component_ref (gnu_call, TREE_PURPOSE (gnu_elmt), false);
35a382b8
EB
5376 gnu_result_type = TREE_TYPE (gnu_call);
5377 }
5378
89e037d0
EB
5379 /* If the function returns by direct reference, we have to dereference
5380 the pointer. */
5381 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
35a382b8
EB
5382 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
5383
5384 if (gnu_target)
5385 {
5386 Node_Id gnat_parent = Parent (gnat_node);
5387 enum tree_code op_code;
5388
815b5368 5389 gigi_checking_assert (!Do_Range_Check (gnat_node));
35a382b8 5390
ddb5a105
EB
5391 /* ??? If the return type has variable size, then force the return
5392 slot optimization as we would not be able to create a temporary.
ddb5a105 5393 That's what has been done historically. */
fc7a823e 5394 if (return_type_with_variable_size_p (gnu_result_type))
35a382b8 5395 op_code = INIT_EXPR;
b81e2d5e
EB
5396
5397 /* If this is a call to a pure function returning an array of scalar
5398 type, try to apply the return slot optimization. */
5399 else if ((TYPE_READONLY (gnu_subprog_type)
5400 || TYPE_RESTRICT (gnu_subprog_type))
5401 && is_array_of_scalar_type (gnu_result_type)
5402 && TYPE_MODE (gnu_result_type) == BLKmode
5403 && aggregate_value_p (gnu_result_type, gnu_subprog_type)
5404 && return_slot_opt_for_pure_call_p (gnu_target, gnu_call))
5405 op_code = INIT_EXPR;
5406
9aa9ba95
EB
5407 /* If this is the initialization of a return object in a function
5408 returning by invisible reference, we can always use the return
5409 slot optimization. */
5410 else if (TREE_CODE (gnu_target) == INDIRECT_REF
5411 && TREE_CODE (TREE_OPERAND (gnu_target, 0)) == RESULT_DECL
5412 && current_function_decl
5413 && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl)))
5414 op_code = INIT_EXPR;
5415
35a382b8
EB
5416 else
5417 op_code = MODIFY_EXPR;
5418
f797c2b7 5419 /* Use the required method to move the result to the target. */
17a98a3d 5420 if (atomic_access == OUTER_ATOMIC)
f797c2b7
EB
5421 gnu_call
5422 = build_load_modify_store (gnu_target, gnu_call, gnat_node);
17a98a3d 5423 else if (atomic_access == SIMPLE_ATOMIC)
f797c2b7 5424 gnu_call = build_atomic_store (gnu_target, gnu_call, atomic_sync);
544d14e1
EB
5425 else if (Present (gnat_storage_model)
5426 && Present (Storage_Model_Copy_To (gnat_storage_model)))
5427 gnu_call
5428 = build_storage_model_store (gnat_storage_model, gnu_target,
5429 gnu_call);
033ba5bf
EB
5430 else
5431 gnu_call
5432 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
f797c2b7
EB
5433
5434 if (EXPR_P (gnu_call))
5435 set_expr_location_from_node (gnu_call, gnat_parent);
ddb5a105 5436 append_to_statement_list (gnu_call, &gnu_stmt_list);
35a382b8
EB
5437 }
5438 else
5439 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5440 }
5441
5442 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
5443 parameters, the result is just the call statement. */
5444 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
ddb5a105
EB
5445 append_to_statement_list (gnu_call, &gnu_stmt_list);
5446
5447 /* Finally, add the copy back statements, if any. */
5448 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
a1ab4c31 5449
35a382b8
EB
5450 if (went_into_elab_proc)
5451 current_function_decl = NULL_TREE;
ced57283 5452
ddb5a105
EB
5453 /* If we have pushed a binding level, pop it and finish up the enclosing
5454 statement group. */
35a382b8
EB
5455 if (pushed_binding_level)
5456 {
ddb5a105 5457 add_stmt (gnu_stmt_list);
35a382b8
EB
5458 gnat_poplevel ();
5459 gnu_result = end_stmt_group ();
5460 }
ddb5a105
EB
5461
5462 /* Otherwise, retrieve the statement list, if any. */
5463 else if (gnu_stmt_list)
5464 gnu_result = gnu_stmt_list;
5465
5466 /* Otherwise, just return the call expression. */
35a382b8
EB
5467 else
5468 return gnu_call;
5469
71196d4e
EB
5470 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
5471 But first simplify if we have only one statement in the list. */
35a382b8 5472 if (returning_value)
71196d4e
EB
5473 {
5474 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
5475 if (first == last)
5476 gnu_result = first;
5477 gnu_result
5478 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
5479 }
35a382b8
EB
5480
5481 return gnu_result;
a1ab4c31 5482}
ce2d0ce2 5483
e08f1aad 5484/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an
a1ab4c31
AC
5485 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
5486
5487static tree
5488Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
5489{
a1ab4c31 5490 /* If just annotating, ignore all EH and cleanups. */
6d16658d 5491 const bool eh
ce19ac12 5492 = !type_annotate_only && Present (Exception_Handlers (gnat_node));
736e16ef 5493 const bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
a1ab4c31 5494 tree gnu_result;
a1ab4c31
AC
5495 Node_Id gnat_temp;
5496
6d16658d
BD
5497 /* The exception handling mechanism can handle both ZCX and SJLJ schemes, and
5498 is exposed through the TRY_CATCH_EXPR construct that we build manually.
a1ab4c31
AC
5499
5500 ??? The region level calls down there have been specifically put in place
5501 for a ZCX context and currently the order in which things are emitted
736e16ef 5502 (region/handlers) is different from the SJLJ case. Instead of putting
a1ab4c31
AC
5503 other calls with different conditions at other places for the SJLJ case,
5504 it seems cleaner to reorder things for the SJLJ case and generalize the
6d16658d 5505 condition to make it not ZCX specific. */
a1ab4c31 5506
6d16658d 5507 /* First build the tree for the statements inside the sequence. */
a1ab4c31
AC
5508 start_stmt_group ();
5509
6d16658d
BD
5510 for (gnat_temp = First (Statements (gnat_node));
5511 Present (gnat_temp);
5512 gnat_temp = Next (gnat_temp))
a1ab4c31 5513 add_stmt (gnat_to_gnu (gnat_temp));
736e16ef 5514
6d16658d 5515 gnu_result = end_stmt_group ();
a1ab4c31 5516
6d16658d
BD
5517 /* Then process the exception handlers, if any. */
5518 if (eh)
a1ab4c31
AC
5519 {
5520 tree gnu_handlers;
678400b3 5521 location_t locus;
a1ab4c31 5522
6d16658d 5523 /* First make a group containing the handlers. */
a1ab4c31
AC
5524 start_stmt_group ();
5525 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
5526 Present (gnat_temp);
5527 gnat_temp = Next_Non_Pragma (gnat_temp))
5528 add_stmt (gnat_to_gnu (gnat_temp));
5529 gnu_handlers = end_stmt_group ();
5530
6d16658d
BD
5531 /* Now make the TRY_CATCH_EXPR for the group. */
5532 gnu_result
5533 = build2 (TRY_CATCH_EXPR, void_type_node, gnu_result, gnu_handlers);
5534
362db0b2 5535 /* Set a location. We need to find a unique location for the dispatching
678400b3
AC
5536 code, otherwise we can get coverage or debugging issues. Try with
5537 the location of the end label. */
5538 if (Present (End_Label (gnat_node))
5539 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
5540 SET_EXPR_LOCATION (gnu_result, locus);
5541 else
362db0b2
TQ
5542 /* Clear column information so that the exception handler of an
5543 implicit transient block does not incorrectly inherit the slocs
5544 of a decision, which would otherwise confuse control flow based
5545 coverage analysis tools. */
ba464315 5546 set_expr_location_from_node (gnu_result, gnat_node, true);
a1ab4c31 5547 }
a1ab4c31 5548
6d16658d
BD
5549 /* Process the At_End_Proc, if any. */
5550 if (at_end)
a1ab4c31 5551 {
6d16658d 5552 start_stmt_group ();
a1ab4c31 5553 add_stmt (gnu_result);
6d16658d 5554 At_End_Proc_to_gnu (gnat_node);
a1ab4c31
AC
5555 gnu_result = end_stmt_group ();
5556 }
5557
5558 return gnu_result;
5559}
ce2d0ce2 5560
4ec7c4ec
EB
5561/* Return true if no statement in GNAT_LIST can alter the control flow. */
5562
5563static bool
5564stmt_list_cannot_alter_control_flow_p (List_Id gnat_list)
5565{
5566 if (No (gnat_list))
5567 return true;
5568
5569 /* This is very conservative, we reject everything except for simple
5570 assignments between identifiers or literals. */
5571 for (Node_Id gnat_node = First (gnat_list);
5572 Present (gnat_node);
5573 gnat_node = Next (gnat_node))
5574 {
5575 if (Nkind (gnat_node) != N_Assignment_Statement)
5576 return false;
5577
5578 if (Nkind (Name (gnat_node)) != N_Identifier)
5579 return false;
5580
5581 Node_Kind nkind = Nkind (Expression (gnat_node));
5582 if (nkind != N_Identifier
5583 && nkind != N_Integer_Literal
5584 && nkind != N_Real_Literal)
5585 return false;
5586 }
5587
5588 return true;
5589}
5590
e08f1aad
EB
5591/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Exception_Handler,
5592 to a GCC tree, which is returned. */
a1ab4c31
AC
5593
5594static tree
e08f1aad 5595Exception_Handler_to_gnu (Node_Id gnat_node)
a1ab4c31
AC
5596{
5597 tree gnu_etypes_list = NULL_TREE;
a1ab4c31
AC
5598
5599 /* We build a TREE_LIST of nodes representing what exception types this
5600 handler can catch, with special cases for others and all others cases.
5601
5602 Each exception type is actually identified by a pointer to the exception
1a710808 5603 id, or to a dummy object for "others" and "all others". */
4ec7c4ec
EB
5604 for (Node_Id gnat_temp = First (Exception_Choices (gnat_node));
5605 gnat_temp;
5606 gnat_temp = Next (gnat_temp))
a1ab4c31 5607 {
fd6e497e
EB
5608 tree gnu_expr, gnu_etype;
5609
a1ab4c31
AC
5610 if (Nkind (gnat_temp) == N_Others_Choice)
5611 {
fd6e497e
EB
5612 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
5613 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
a1ab4c31
AC
5614 }
5615 else if (Nkind (gnat_temp) == N_Identifier
5616 || Nkind (gnat_temp) == N_Expanded_Name)
5617 {
5618 Entity_Id gnat_ex_id = Entity (gnat_temp);
5619
fd6e497e 5620 /* Exception may be a renaming. Recover original exception which is
a1ab4c31
AC
5621 the one elaborated and registered. */
5622 if (Present (Renamed_Object (gnat_ex_id)))
5623 gnat_ex_id = Renamed_Object (gnat_ex_id);
5624
afc737f0 5625 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, false);
a1ab4c31 5626 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
a1ab4c31
AC
5627 }
5628 else
5629 gcc_unreachable ();
5630
5631 /* The GCC interface expects NULL to be passed for catch all handlers, so
5632 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
5633 is integer_zero_node. It would not work, however, because GCC's
5634 notion of "catch all" is stronger than our notion of "others". Until
5635 we correctly use the cleanup interface as well, doing that would
5636 prevent the "all others" handlers from being seen, because nothing
5637 can be caught beyond a catch all from GCC's point of view. */
5638 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
5639 }
5640
5641 start_stmt_group ();
a1ab4c31 5642
5d733372
AO
5643 /* Expand a call to the begin_handler hook at the beginning of the
5644 handler, and arrange for a call to the end_handler hook to occur
5645 on every possible exit path. GDB sets a breakpoint in the
5646 begin_handler for catchpoints.
a1ab4c31 5647
5d733372
AO
5648 A v1 begin handler saves the cleanup from the exception object,
5649 and marks the exception as in use, so that it will not be
5650 released by other handlers. A v1 end handler restores the
5651 cleanup and releases the exception object, unless it is still
5652 claimed, or the exception is being propagated (reraised).
a1ab4c31 5653
1d65f45c 5654 __builtin_eh_pointer references the exception occurrence being
5d733372
AO
5655 handled or propagated. Within the handler region, it is the
5656 former, but within the else branch of the EH_ELSE_EXPR, i.e. the
5657 exceptional cleanup path, it is the latter, so we must save the
5658 occurrence being handled early on, so that, should an exception
5659 be (re)raised, we can release the current exception, or figure
5660 out we're not to release it because we're propagating a reraise
5661 thereof.
5662
5663 We use local variables to retrieve the incoming value at handler
5664 entry time (EXPTR), the saved cleanup (EXCLN) and the token
5665 (EXVTK), and reuse them to feed the end_handler hook's argument
5666 at exit. */
5667
5668 /* CODE: void *EXPTR = __builtin_eh_pointer (0); */
4ec7c4ec 5669 tree gnu_current_exc_ptr
e79983f4 5670 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
1d65f45c 5671 1, integer_zero_node);
5d733372 5672 tree exc_ptr
c1a569ef
EB
5673 = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
5674 ptr_type_node, gnu_current_exc_ptr,
5d733372 5675 true, false, false, false, false, true, true,
c1a569ef 5676 NULL, gnat_node);
a1ab4c31 5677
5d733372
AO
5678 tree prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
5679 gnu_incoming_exc_ptr = exc_ptr;
5680
5681 /* begin_handler_decl must not throw, so we can use it as an
5682 initializer for a variable used in cleanups.
5683
5684 CODE: void *EXCLN = __gnat_begin_handler_v1 (EXPTR); */
5685 tree exc_cleanup
5686 = create_var_decl (get_identifier ("EXCLN"), NULL_TREE,
5687 ptr_type_node,
5688 build_call_n_expr (begin_handler_decl, 1,
5689 exc_ptr),
5690 true, false, false, false, false,
5691 true, true, NULL, gnat_node);
811189d9
AC
5692
5693 /* Declare and initialize the choice parameter, if present. */
5694 if (Present (Choice_Parameter (gnat_node)))
5695 {
fd6e497e 5696 tree gnu_param
afc737f0 5697 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, true);
811189d9 5698
5d733372 5699 /* CODE: __gnat_set_exception_parameter (&choice_param, EXPTR); */
811189d9
AC
5700 add_stmt (build_call_n_expr
5701 (set_exception_parameter_decl, 2,
5702 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
5703 gnu_incoming_exc_ptr));
5704 }
5705
5d733372 5706 /* CODE: <handler proper> */
4ec7c4ec
EB
5707 add_stmt_list (Statements (gnat_node));
5708
5d733372
AO
5709 tree call = build_call_n_expr (end_handler_decl, 3,
5710 exc_ptr,
5711 exc_cleanup,
5712 null_pointer_node);
5713 /* If the handler can only end by falling off the end, don't bother
5714 with cleanups. */
4ec7c4ec 5715 if (stmt_list_cannot_alter_control_flow_p (Statements (gnat_node)))
5d733372
AO
5716 /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, NULL); */
5717 add_stmt_with_node (call, gnat_node);
5718 /* Otherwise, all of the above is after
5719 CODE: try {
5720
5721 The call above will appear after
5722 CODE: } finally {
5723
5724 And the code below will appear after
5725 CODE: } else {
5726
5727 The else block to a finally block is taken instead of the finally
5728 block when an exception propagates out of the try block. */
4ec7c4ec 5729 else
5d733372
AO
5730 {
5731 start_stmt_group ();
6d16658d 5732
5d733372
AO
5733 /* CODE: void *EXPRP = __builtin_eh_handler (0); */
5734 tree prop_ptr
5735 = create_var_decl (get_identifier ("EXPRP"), NULL_TREE,
5736 ptr_type_node,
5737 build_call_expr (builtin_decl_explicit
5738 (BUILT_IN_EH_POINTER),
5739 1, integer_zero_node),
5740 true, false, false, false, false,
5741 true, true, NULL, gnat_node);
5742
5743 /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, EXPRP); */
5744 tree ecall = build_call_n_expr (end_handler_decl, 3,
5745 exc_ptr,
5746 exc_cleanup,
5747 prop_ptr);
5748
5749 add_stmt_with_node (ecall, gnat_node);
5750
5751 /* CODE: } */
5d733372
AO
5752 tree eblk = end_stmt_group ();
5753 tree ehls = build2 (EH_ELSE_EXPR, void_type_node, call, eblk);
5754 add_cleanup (ehls, gnat_node);
5755 }
4ec7c4ec 5756
624e1688
AC
5757 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
5758
fd6e497e
EB
5759 return
5760 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
a1ab4c31 5761}
ce2d0ce2 5762
e08f1aad 5763/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Compilation_Unit. */
a1ab4c31
AC
5764
5765static void
5766Compilation_Unit_to_gnu (Node_Id gnat_node)
5767{
58c8f770
EB
5768 const Node_Id gnat_unit = Unit (gnat_node);
5769 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
5770 || Nkind (gnat_unit) == N_Subprogram_Body);
5771 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
384e3fb1 5772 Entity_Id gnat_entity;
ec4a0d83 5773 Node_Id gnat_pragma, gnat_iter;
ff9baa5f
PMR
5774 /* Make the decl for the elaboration procedure. Emit debug info for it, so
5775 that users can break into their elaboration code in debuggers. Kludge:
19d9ce46
OH
5776 don't consider it as a definition so that we have a line map for its
5777 body, but no subprogram description in debug info. In addition, don't
5778 qualify it as artificial, even though it is not a user subprogram per se,
5779 in particular for specs. Unlike, say, clones created internally by the
5780 compiler, this subprogram materializes specific user code and flagging it
5781 artificial would take elab code away from gcov's analysis. */
a1ab4c31
AC
5782 tree gnu_elab_proc_decl
5783 = create_subprog_decl
58c8f770 5784 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
2056c5ed 5785 NULL_TREE, void_ftype, NULL_TREE,
19d9ce46 5786 is_default, true, false, false, true, false, NULL, gnat_unit);
a1ab4c31
AC
5787 struct elab_info *info;
5788
9771b263 5789 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
a1ab4c31 5790 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
58c8f770
EB
5791
5792 /* Initialize the information structure for the function. */
a1ab4c31 5793 allocate_struct_function (gnu_elab_proc_decl, false);
a1ab4c31 5794 set_cfun (NULL);
58c8f770
EB
5795
5796 current_function_decl = NULL_TREE;
5797
a09d56d8
EB
5798 start_stmt_group ();
5799 gnat_pushlevel ();
a1ab4c31 5800
1e17ef87 5801 /* For a body, first process the spec if there is one. */
6ddf9843
EB
5802 if (Nkind (gnat_unit) == N_Package_Body
5803 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
5804 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
a1ab4c31 5805
5daed84a
EB
5806 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
5807 {
5808 elaborate_all_entities (gnat_node);
5809
6ddf9843
EB
5810 if (Nkind (gnat_unit) == N_Subprogram_Declaration
5811 || Nkind (gnat_unit) == N_Generic_Package_Declaration
5812 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
5daed84a
EB
5813 return;
5814 }
5815
257e81a6
EB
5816 /* Then process any pragmas and declarations preceding the unit. */
5817 for (gnat_pragma = First (Context_Items (gnat_node));
5818 Present (gnat_pragma);
5819 gnat_pragma = Next (gnat_pragma))
5820 if (Nkind (gnat_pragma) == N_Pragma)
5821 add_stmt (gnat_to_gnu (gnat_pragma));
6d16658d 5822 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty,
5daed84a 5823 true, true);
257e81a6
EB
5824
5825 /* Process the unit itself. */
6ddf9843 5826 add_stmt (gnat_to_gnu (gnat_unit));
5daed84a 5827
384e3fb1
JM
5828 /* Generate code for all the inlined subprograms. */
5829 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5830 Present (gnat_entity);
5831 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
58c8f770 5832 {
384e3fb1 5833 Node_Id gnat_body;
58c8f770 5834
384e3fb1
JM
5835 /* Without optimization, process only the required subprograms. */
5836 if (!optimize && !Has_Pragma_Inline_Always (gnat_entity))
5837 continue;
58c8f770 5838
0c09a01e
EB
5839 /* The set of inlined subprograms is computed from data recorded early
5840 during expansion and it can be a strict superset of the final set
5841 computed after semantic analysis, for example if a call to such a
5842 subprogram occurs in a pragma Assert and assertions are disabled.
5843 In that case, semantic analysis resets Is_Public to false but the
5844 entry for the subprogram in the inlining tables is stalled. */
5845 if (!Is_Public (gnat_entity))
5846 continue;
5847
384e3fb1
JM
5848 gnat_body = Parent (Declaration_Node (gnat_entity));
5849 if (Nkind (gnat_body) != N_Subprogram_Body)
5850 {
5851 /* ??? This happens when only the spec of a package is provided. */
5852 if (No (Corresponding_Body (gnat_body)))
5853 continue;
58c8f770 5854
384e3fb1
JM
5855 gnat_body
5856 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
58c8f770 5857 }
384e3fb1
JM
5858
5859 /* Define the entity first so we set DECL_EXTERNAL. */
afc737f0 5860 gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
384e3fb1 5861 add_stmt (gnat_to_gnu (gnat_body));
58c8f770 5862 }
a1ab4c31 5863
a1ab4c31
AC
5864 /* Process any pragmas and actions following the unit. */
5865 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5866 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
7b56a91b 5867 finalize_from_limited_with ();
a1ab4c31 5868
ec4a0d83
EB
5869 /* Then process the expressions of pragma Compile_Time_{Error|Warning} to
5870 annotate types referenced therein if they have not been annotated. */
5871 for (int i = 0; gnat_compile_time_expr_list.iterate (i, &gnat_iter); i++)
5872 (void) gnat_to_gnu_external (gnat_iter);
5873 gnat_compile_time_expr_list.release ();
5874
ea292448 5875 /* Save away what we've made so far and finish it up. */
a1ab4c31
AC
5876 set_current_block_context (gnu_elab_proc_decl);
5877 gnat_poplevel ();
5878 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
2a02d090 5879 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
ea292448 5880 gnu_elab_proc_stack->pop ();
58c8f770 5881
ea292448
EB
5882 /* Record this potential elaboration procedure for later processing. */
5883 info = ggc_alloc<elab_info> ();
a1ab4c31
AC
5884 info->next = elab_info_list;
5885 info->elab_proc = gnu_elab_proc_decl;
5886 info->gnat_node = gnat_node;
5887 elab_info_list = info;
5888
9a30c7c4
AC
5889 /* Force the processing for all nodes that remain in the queue. */
5890 process_deferred_decl_context (true);
a1ab4c31 5891}
ce2d0ce2 5892
64235766
EB
5893/* Mark COND, a boolean expression, as predicating a call to a noreturn
5894 function, i.e. predict that it is very likely false, and return it.
5895
5896 The compiler will automatically predict the last edge leading to a call
5897 to a noreturn function as very unlikely taken. This function makes it
b9778c45 5898 possible to extend the prediction to predecessors in case the condition
64235766
EB
5899 is made up of several short-circuit operators. */
5900
5901static tree
5902build_noreturn_cond (tree cond)
5903{
b9778c45
EB
5904 tree pred_cst = build_int_cst (integer_type_node, PRED_NORETURN);
5905 return
5906 build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_BUILTIN_EXPECT,
5907 boolean_type_node, 3, cond,
5908 boolean_false_node, pred_cst);
64235766
EB
5909}
5910
3eaec285
EB
5911/* Subroutine of gnat_to_gnu to translate GNAT_RANGE, a node representing a
5912 range of values, into GNU_LOW and GNU_HIGH bounds. */
5913
5914static void
5915Range_to_gnu (Node_Id gnat_range, tree *gnu_low, tree *gnu_high)
5916{
5917 /* GNAT_RANGE is either an N_Range or an identifier denoting a subtype. */
5918 switch (Nkind (gnat_range))
5919 {
5920 case N_Range:
5921 *gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
5922 *gnu_high = gnat_to_gnu (High_Bound (gnat_range));
5923 break;
5924
5925 case N_Expanded_Name:
5926 case N_Identifier:
5927 {
5928 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
5929 tree gnu_range_base_type = get_base_type (gnu_range_type);
5930
5931 *gnu_low
5932 = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type));
5933 *gnu_high
5934 = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type));
5935 }
5936 break;
5937
5938 default:
5939 gcc_unreachable ();
5940 }
5941}
5942
9a1bdc31 5943/* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Raise_xxx_Error,
e08f1aad
EB
5944 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
5945 where we should place the result type. */
80096613
EB
5946
5947static tree
5948Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5949{
5950 const Node_Kind kind = Nkind (gnat_node);
80096613 5951 const Node_Id gnat_cond = Condition (gnat_node);
c900c700 5952 const int reason = UI_To_Int (Reason (gnat_node));
80096613
EB
5953 const bool with_extra_info
5954 = Exception_Extra_Info
5955 && !No_Exception_Handlers_Set ()
8f8f531f 5956 && No (get_exception_label (kind));
80096613 5957 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
c900c700 5958 Node_Id gnat_rcond;
80096613 5959
9a1bdc31
EB
5960 /* The following processing is not required for correctness. Its purpose is
5961 to give more precise error messages and to record some information. */
80096613
EB
5962 switch (reason)
5963 {
5964 case CE_Access_Check_Failed:
5965 if (with_extra_info)
9a1bdc31 5966 gnu_result = build_call_raise_column (reason, gnat_node, kind);
80096613
EB
5967 break;
5968
5969 case CE_Index_Check_Failed:
5970 case CE_Range_Check_Failed:
5971 case CE_Invalid_Data:
c900c700
EB
5972 if (No (gnat_cond) || Nkind (gnat_cond) != N_Op_Not)
5973 break;
5974 gnat_rcond = Right_Opnd (gnat_cond);
5975 if (Nkind (gnat_rcond) == N_In
5976 || Nkind (gnat_rcond) == N_Op_Ge
5977 || Nkind (gnat_rcond) == N_Op_Le)
80096613 5978 {
c900c700
EB
5979 const Node_Id gnat_index = Left_Opnd (gnat_rcond);
5980 const Node_Id gnat_type = Etype (gnat_index);
5981 tree gnu_index = gnat_to_gnu (gnat_index);
5982 tree gnu_type = get_unpadded_type (gnat_type);
5983 tree gnu_low_bound, gnu_high_bound, disp;
87ab2b04 5984 struct loop_info_d *loop;
c900c700 5985 bool neg_p;
80096613 5986
c900c700 5987 switch (Nkind (gnat_rcond))
80096613
EB
5988 {
5989 case N_In:
c900c700 5990 Range_to_gnu (Right_Opnd (gnat_rcond),
3eaec285 5991 &gnu_low_bound, &gnu_high_bound);
80096613
EB
5992 break;
5993
5994 case N_Op_Ge:
c900c700
EB
5995 gnu_low_bound = gnat_to_gnu (Right_Opnd (gnat_rcond));
5996 gnu_high_bound = TYPE_MAX_VALUE (gnu_type);
80096613
EB
5997 break;
5998
5999 case N_Op_Le:
c900c700
EB
6000 gnu_low_bound = TYPE_MIN_VALUE (gnu_type);
6001 gnu_high_bound = gnat_to_gnu (Right_Opnd (gnat_rcond));
80096613
EB
6002 break;
6003
6004 default:
c900c700 6005 gcc_unreachable ();
80096613
EB
6006 }
6007
c900c700 6008 gnu_type = maybe_character_type (gnu_type);
825da0d2
EB
6009 if (TREE_TYPE (gnu_index) != gnu_type)
6010 {
c900c700
EB
6011 gnu_low_bound = convert (gnu_type, gnu_low_bound);
6012 gnu_high_bound = convert (gnu_type, gnu_high_bound);
825da0d2
EB
6013 gnu_index = convert (gnu_type, gnu_index);
6014 }
6015
80096613 6016 if (with_extra_info
80096613
EB
6017 && Known_Esize (gnat_type)
6018 && UI_To_Int (Esize (gnat_type)) <= 32)
6019 gnu_result
9a1bdc31 6020 = build_call_raise_range (reason, gnat_node, kind, gnu_index,
80096613
EB
6021 gnu_low_bound, gnu_high_bound);
6022
64235766
EB
6023 /* If optimization is enabled and we are inside a loop, we try to
6024 compute invariant conditions for checks applied to the iteration
6025 variable, i.e. conditions that are independent of the variable
6026 and necessary in order for the checks to fail in the course of
6027 some iteration. If we succeed, we consider an alternative:
6028
6029 1. If loop unswitching is enabled, we prepend these conditions
6030 to the original conditions of the checks. This will make it
6031 possible for the loop unswitching pass to replace the loop
6032 with two loops, one of which has the checks eliminated and
6033 the other has the original checks reinstated, and a prologue
6034 implementing a run-time selection. The former loop will be
6035 for example suitable for vectorization.
6036
6037 2. Otherwise, we instead append the conditions to the original
6038 conditions of the checks. At worse, if the conditions cannot
6039 be evaluated at compile time, they will be evaluated as true
6040 at run time only when the checks have already failed, thus
6041 contributing negatively only to the size of the executable.
6042 But the hope is that these invariant conditions be evaluated
6043 at compile time to false, thus taking away the entire checks
6044 with them. */
87ab2b04 6045 if (optimize
87ab2b04 6046 && inside_loop_p ()
80096613
EB
6047 && (!gnu_low_bound
6048 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
6049 && (!gnu_high_bound
6050 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
933a7325 6051 && (loop = find_loop_for (gnu_index, &disp, &neg_p)))
80096613 6052 {
87ab2b04 6053 struct range_check_info_d *rci = ggc_alloc<range_check_info_d> ();
80096613
EB
6054 rci->low_bound = gnu_low_bound;
6055 rci->high_bound = gnu_high_bound;
933a7325
EB
6056 rci->disp = disp;
6057 rci->neg_p = neg_p;
825da0d2 6058 rci->type = gnu_type;
64235766
EB
6059 rci->inserted_cond
6060 = build1 (SAVE_EXPR, boolean_type_node, boolean_true_node);
87ab2b04 6061 vec_safe_push (loop->checks, rci);
64235766 6062 gnu_cond = build_noreturn_cond (gnat_to_gnu (gnat_cond));
3bdf0b64 6063 if (optimize >= 3)
64235766
EB
6064 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
6065 boolean_type_node,
6066 rci->inserted_cond,
6067 gnu_cond);
6068 else
6069 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
6070 boolean_type_node,
6071 gnu_cond,
6072 rci->inserted_cond);
80096613
EB
6073 }
6074 }
6075 break;
6076
6077 default:
6078 break;
6079 }
6080
c900c700
EB
6081 /* The following processing does the real work, but we must nevertheless make
6082 sure not to override the result of the previous processing. */
80096613
EB
6083 if (!gnu_result)
6084 gnu_result = build_call_raise (reason, gnat_node, kind);
6085 set_expr_location_from_node (gnu_result, gnat_node);
6086
9a1bdc31
EB
6087 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
6088
80096613
EB
6089 /* If the type is VOID, this is a statement, so we need to generate the code
6090 for the call. Handle a condition, if there is one. */
6091 if (VOID_TYPE_P (*gnu_result_type_p))
6092 {
6093 if (Present (gnat_cond))
6094 {
6095 if (!gnu_cond)
6096 gnu_cond = gnat_to_gnu (gnat_cond);
f53aff92
EB
6097 if (integer_zerop (gnu_cond))
6098 return alloc_stmt_list ();
80096613
EB
6099 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
6100 alloc_stmt_list ());
6101 }
6102 }
6103 else
6fdacaf4
PT
6104 {
6105 /* The condition field must not be present when the node is used as an
6106 expression form. */
6107 gigi_checking_assert (No (gnat_cond));
6108 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
6109 }
80096613
EB
6110
6111 return gnu_result;
6112}
ce2d0ce2 6113
27ab5bd8
EB
6114/* Return true if GNAT_NODE is on the LHS of an assignment or an actual
6115 parameter of a call. */
6116
6117static bool
6118lhs_or_actual_p (Node_Id gnat_node)
6119{
17a98a3d
EB
6120 const Node_Id gnat_parent = Parent (gnat_node);
6121 const Node_Kind kind = Nkind (gnat_parent);
27ab5bd8
EB
6122
6123 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
6124 return true;
6125
6126 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
6127 && Name (gnat_parent) != gnat_node)
6128 return true;
6129
6130 if (kind == N_Parameter_Association)
6131 return true;
6132
6133 return false;
6134}
6135
033ba5bf
EB
6136/* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
6137 of an assignment or an actual parameter of a call. */
6138
6139static bool
6140present_in_lhs_or_actual_p (Node_Id gnat_node)
6141{
033ba5bf
EB
6142 if (lhs_or_actual_p (gnat_node))
6143 return true;
6144
17a98a3d 6145 const Node_Kind kind = Nkind (Parent (gnat_node));
033ba5bf
EB
6146
6147 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
6148 && lhs_or_actual_p (Parent (gnat_node)))
6149 return true;
6150
6151 return false;
6152}
6153
4f8a6678
EB
6154/* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
6155 as gigi is concerned. This is used to avoid conversions on the LHS. */
c2efda0d
EB
6156
6157static bool
4f8a6678 6158unchecked_conversion_nop (Node_Id gnat_node)
c2efda0d
EB
6159{
6160 Entity_Id from_type, to_type;
6161
4f8a6678
EB
6162 /* The conversion must be on the LHS of an assignment or an actual parameter
6163 of a call. Otherwise, even if the conversion was essentially a no-op, it
6164 could de facto ensure type consistency and this should be preserved. */
27ab5bd8 6165 if (!lhs_or_actual_p (gnat_node))
c2efda0d
EB
6166 return false;
6167
6168 from_type = Etype (Expression (gnat_node));
6169
6170 /* We're interested in artificial conversions generated by the front-end
6171 to make private types explicit, e.g. in Expand_Assign_Array. */
6172 if (!Is_Private_Type (from_type))
6173 return false;
6174
6175 from_type = Underlying_Type (from_type);
6176 to_type = Etype (gnat_node);
6177
6178 /* The direct conversion to the underlying type is a no-op. */
6179 if (to_type == from_type)
6180 return true;
6181
1a4cb227 6182 /* For an array subtype, the conversion to the PAIT is a no-op. */
c2efda0d 6183 if (Ekind (from_type) == E_Array_Subtype
1a4cb227 6184 && to_type == Packed_Array_Impl_Type (from_type))
c2efda0d
EB
6185 return true;
6186
68a3eb69
EB
6187 /* For a record subtype, the conversion to the type is a no-op. */
6188 if (Ekind (from_type) == E_Record_Subtype
6189 && to_type == Etype (from_type))
6190 return true;
6191
c2efda0d
EB
6192 return false;
6193}
6194
4c335163
EB
6195/* Return true if GNAT_NODE represents a statement. */
6196
6197static bool
6198statement_node_p (Node_Id gnat_node)
6199{
6200 const Node_Kind kind = Nkind (gnat_node);
6201
6202 if (kind == N_Label)
6203 return true;
6204
6205 if (IN (kind, N_Statement_Other_Than_Procedure_Call))
6206 return true;
6207
6208 if (kind == N_Procedure_Call_Statement)
6209 return true;
6210
6211 if (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void)
6212 return true;
6213
6214 return false;
6215}
6216
3f13dd77
EB
6217/* This function is the driver of the GNAT to GCC tree transformation process.
6218 It is the entry point of the tree transformer. GNAT_NODE is the root of
6219 some GNAT tree. Return the root of the corresponding GCC tree. If this
6220 is an expression, return the GCC equivalent of the expression. If this
6221 is a statement, return the statement or add it to the current statement
6222 group, in which case anything returned is to be interpreted as occurring
6223 after anything added. */
a1ab4c31
AC
6224
6225tree
6226gnat_to_gnu (Node_Id gnat_node)
6227{
3f13dd77 6228 const Node_Kind kind = Nkind (gnat_node);
1e17ef87 6229 tree gnu_result = error_mark_node; /* Default to no value. */
a1ab4c31 6230 tree gnu_result_type = void_type_node;
3f13dd77 6231 tree gnu_expr, gnu_lhs, gnu_rhs;
a1ab4c31 6232 Node_Id gnat_temp;
17a98a3d 6233 atomic_acces_t aa_type;
f53aff92 6234 bool went_into_elab_proc;
17a98a3d 6235 bool aa_sync;
544d14e1 6236 Entity_Id gnat_smo;
a1ab4c31
AC
6237
6238 /* Save node number for error message and set location information. */
b64c4968
EB
6239 if (Sloc (gnat_node) > No_Location)
6240 Current_Error_Node = gnat_node;
a1ab4c31
AC
6241 Sloc_to_locus (Sloc (gnat_node), &input_location);
6242
4c335163
EB
6243 /* If we are only annotating types and this node is a statement, return
6244 an empty statement list. */
6245 if (type_annotate_only && statement_node_p (gnat_node))
a1ab4c31
AC
6246 return alloc_stmt_list ();
6247
4c335163
EB
6248 /* If we are only annotating types and this node is a subexpression, return
6249 a NULL_EXPR, but filter out nodes appearing in the expressions attached
6250 to packed array implementation types. */
a1ab4c31 6251 if (type_annotate_only
3f13dd77 6252 && IN (kind, N_Subexpr)
4c335163
EB
6253 && !(((IN (kind, N_Op) && kind != N_Op_Expon)
6254 || kind == N_Type_Conversion)
6255 && Is_Integer_Type (Etype (gnat_node)))
6256 && !(kind == N_Attribute_Reference
4b9e1bc7
EB
6257 && (Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Length
6258 || Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Size)
6259 && Is_Constrained (Etype (Prefix (gnat_node)))
4c335163 6260 && !Is_Constr_Subt_For_U_Nominal (Etype (Prefix (gnat_node))))
93200f92 6261 && kind != N_Expanded_Name
3f13dd77 6262 && kind != N_Identifier
a1ab4c31
AC
6263 && !Compile_Time_Known_Value (gnat_node))
6264 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
6265 build_call_raise (CE_Range_Check_Failed, gnat_node,
6266 N_Raise_Constraint_Error));
6267
f53aff92
EB
6268 /* If this is a statement and we are at top level, it must be part of the
6269 elaboration procedure, so mark us as being in that procedure. */
6270 if ((statement_node_p (gnat_node)
6271 || kind == N_Handled_Sequence_Of_Statements
6272 || kind == N_Implicit_Label_Declaration)
6273 && !current_function_decl)
a1ab4c31 6274 {
f53aff92
EB
6275 current_function_decl = get_elaboration_procedure ();
6276 went_into_elab_proc = true;
a1ab4c31 6277 }
f53aff92
EB
6278 else
6279 went_into_elab_proc = false;
a1ab4c31 6280
3f13dd77 6281 switch (kind)
a1ab4c31
AC
6282 {
6283 /********************************/
1e17ef87 6284 /* Chapter 2: Lexical Elements */
a1ab4c31
AC
6285 /********************************/
6286
6287 case N_Identifier:
6288 case N_Expanded_Name:
6289 case N_Operator_Symbol:
6290 case N_Defining_Identifier:
1d4b96e0 6291 case N_Defining_Operator_Symbol:
a1ab4c31 6292 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
033ba5bf 6293
f797c2b7 6294 /* If atomic access is required on the RHS, build the atomic load. */
17a98a3d 6295 if (simple_atomic_access_required_p (gnat_node, &aa_sync)
033ba5bf 6296 && !present_in_lhs_or_actual_p (gnat_node))
17a98a3d 6297 gnu_result = build_atomic_load (gnu_result, aa_sync);
a1ab4c31
AC
6298 break;
6299
6300 case N_Integer_Literal:
6301 {
6302 tree gnu_type;
6303
6304 /* Get the type of the result, looking inside any padding and
6305 justified modular types. Then get the value in that type. */
6306 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6307
6308 if (TREE_CODE (gnu_type) == RECORD_TYPE
6309 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
6310 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
6311
6312 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
6313
6314 /* If the result overflows (meaning it doesn't fit in its base type),
104099b8
EB
6315 abort, unless this is for a named number because that's not fatal.
6316 We would like to check that the value is within the range of the
6317 subtype, but that causes problems with subtypes whose usage will
6318 raise Constraint_Error and also with biased representation. */
6319 if (TREE_OVERFLOW (gnu_result))
6320 {
6321 if (Nkind (Parent (gnat_node)) == N_Number_Declaration)
6322 gnu_result = error_mark_node;
6323 else
6324 gcc_unreachable ();
6325 }
a1ab4c31
AC
6326 }
6327 break;
6328
6329 case N_Character_Literal:
6330 /* If a Entity is present, it means that this was one of the
6331 literals in a user-defined character type. In that case,
6332 just return the value in the CONST_DECL. Otherwise, use the
6333 character code. In that case, the base type should be an
6334 INTEGER_TYPE, but we won't bother checking for that. */
6335 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6336 if (Present (Entity (gnat_node)))
6337 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
6338 else
6339 gnu_result
9a1bdc31
EB
6340 = build_int_cst (gnu_result_type,
6341 UI_To_CC (Char_Literal_Value (gnat_node)));
a1ab4c31
AC
6342 break;
6343
6344 case N_Real_Literal:
015bee83
AC
6345 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6346
08477031
EB
6347 /* If this is of a fixed-point type, the value we want is the value of
6348 the corresponding integer. */
7ed9919d 6349 if (Is_Fixed_Point_Type (Underlying_Type (Etype (gnat_node))))
a1ab4c31 6350 {
a1ab4c31
AC
6351 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
6352 gnu_result_type);
6353 gcc_assert (!TREE_OVERFLOW (gnu_result));
6354 }
6355
a1ab4c31 6356 else
1e17ef87 6357 {
a1ab4c31
AC
6358 Ureal ur_realval = Realval (gnat_node);
6359
08477031
EB
6360 /* First convert the value to a machine number if it isn't already.
6361 That will force the base to 2 for non-zero values and simplify
6362 the rest of the logic. */
015bee83
AC
6363 if (!Is_Machine_Number (gnat_node))
6364 ur_realval
6365 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
6366 ur_realval, Round_Even, gnat_node);
a1ab4c31 6367
a1ab4c31 6368 if (UR_Is_Zero (ur_realval))
9a1bdc31 6369 gnu_result = build_real (gnu_result_type, dconst0);
a1ab4c31
AC
6370 else
6371 {
015bee83 6372 REAL_VALUE_TYPE tmp;
a1ab4c31 6373
08477031 6374 gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
a1ab4c31 6375
015bee83 6376 /* The base must be 2 as Machine guarantees this, so we scale
08477031
EB
6377 the value, which we know can fit in the mantissa of the type
6378 (hence the use of that type above). */
015bee83
AC
6379 gcc_assert (Rbase (ur_realval) == 2);
6380 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
6381 - UI_To_Int (Denominator (ur_realval)));
6382 gnu_result = build_real (gnu_result_type, tmp);
a1ab4c31
AC
6383 }
6384
6385 /* Now see if we need to negate the result. Do it this way to
6386 properly handle -0. */
6387 if (UR_Is_Negative (Realval (gnat_node)))
6388 gnu_result
6389 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
6390 gnu_result);
6391 }
6392
6393 break;
6394
6395 case N_String_Literal:
6396 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6397 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
6398 {
6399 String_Id gnat_string = Strval (gnat_node);
6400 int length = String_Length (gnat_string);
6401 int i;
6402 char *string;
6403 if (length >= ALLOCA_THRESHOLD)
ad00a297 6404 string = XNEWVEC (char, length);
1e17ef87 6405 else
ad00a297 6406 string = (char *) alloca (length);
a1ab4c31
AC
6407
6408 /* Build the string with the characters in the literal. Note
6409 that Ada strings are 1-origin. */
6410 for (i = 0; i < length; i++)
6411 string[i] = Get_String_Char (gnat_string, i + 1);
6412
a1ab4c31
AC
6413 gnu_result = build_string (length, string);
6414
6415 /* Strings in GCC don't normally have types, but we want
6416 this to not be converted to the array type. */
6417 TREE_TYPE (gnu_result) = gnu_result_type;
6418
1e17ef87
EB
6419 if (length >= ALLOCA_THRESHOLD)
6420 free (string);
a1ab4c31
AC
6421 }
6422 else
6423 {
6424 /* Build a list consisting of each character, then make
6425 the aggregate. */
6426 String_Id gnat_string = Strval (gnat_node);
6427 int length = String_Length (gnat_string);
6428 int i;
a1ab4c31 6429 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
8b9aec86 6430 tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node);
9771b263
DN
6431 vec<constructor_elt, va_gc> *gnu_vec;
6432 vec_alloc (gnu_vec, length);
a1ab4c31
AC
6433
6434 for (i = 0; i < length; i++)
6435 {
0e228dd9
NF
6436 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
6437 Get_String_Char (gnat_string, i + 1));
a1ab4c31 6438
0e228dd9 6439 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
8b9aec86 6440 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node);
a1ab4c31
AC
6441 }
6442
0e228dd9 6443 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
a1ab4c31
AC
6444 }
6445 break;
6446
6447 case N_Pragma:
6448 gnu_result = Pragma_to_gnu (gnat_node);
6449 break;
6450
6451 /**************************************/
1e17ef87 6452 /* Chapter 3: Declarations and Types */
a1ab4c31
AC
6453 /**************************************/
6454
6455 case N_Subtype_Declaration:
6456 case N_Full_Type_Declaration:
6457 case N_Incomplete_Type_Declaration:
6458 case N_Private_Type_Declaration:
6459 case N_Private_Extension_Declaration:
6460 case N_Task_Type_Declaration:
6461 process_type (Defining_Entity (gnat_node));
6462 gnu_result = alloc_stmt_list ();
6463 break;
6464
6465 case N_Object_Declaration:
104099b8 6466 case N_Number_Declaration:
a1ab4c31
AC
6467 case N_Exception_Declaration:
6468 gnat_temp = Defining_Entity (gnat_node);
6469 gnu_result = alloc_stmt_list ();
6470
6471 /* If we are just annotating types and this object has an unconstrained
6472 or task type, don't elaborate it. */
6473 if (type_annotate_only
6474 && (((Is_Array_Type (Etype (gnat_temp))
6475 || Is_Record_Type (Etype (gnat_temp)))
6476 && !Is_Constrained (Etype (gnat_temp)))
3d25b3ba 6477 || Is_Concurrent_Type (Etype (gnat_temp))))
a1ab4c31
AC
6478 break;
6479
9aa9ba95
EB
6480 /* If this is a constant related to a return initialized by a reference
6481 to a function call in a function returning by invisible reference:
6482
6483 type Ann is access all Result_Type;
6484 Rnn : constant Ann := Func'reference;
6485 [...]
6486 return Rnn.all;
6487
6488 then elide the temporary by forwarding the return object to Func:
6489
2473f28d 6490 result_type *Rnn = (result_type *) <retval>;
9aa9ba95
EB
6491 *<retval> = Func (); [return slot optimization]
6492 [...]
2473f28d 6493 return Rnn;
9aa9ba95
EB
6494
6495 That's necessary if the result type needs finalization because the
6496 temporary would never be adjusted as Expand_Simple_Function_Return
6497 also elides the temporary in this case. */
6498 if (Ekind (gnat_temp) == E_Constant
6499 && Is_Related_To_Func_Return (gnat_temp)
6500 && Nkind (Expression (gnat_node)) == N_Reference
6501 && Nkind (Prefix (Expression (gnat_node))) == N_Function_Call
6502 && current_function_decl
6503 && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl)))
6504 {
2473f28d
EB
6505 gnat_to_gnu_entity (gnat_temp,
6506 DECL_RESULT (current_function_decl),
6507 true);
6508 gnu_result
6509 = build_unary_op (INDIRECT_REF, NULL_TREE,
6510 DECL_RESULT (current_function_decl));
9aa9ba95
EB
6511 gnu_result
6512 = Call_to_gnu (Prefix (Expression (gnat_node)),
6513 &gnu_result_type, gnu_result,
544d14e1 6514 NOT_ATOMIC, false, Empty);
9aa9ba95
EB
6515 break;
6516 }
6517
a1ab4c31 6518 if (Present (Expression (gnat_node))
3f13dd77 6519 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
a1ab4c31
AC
6520 && (!type_annotate_only
6521 || Compile_Time_Known_Value (Expression (gnat_node))))
6522 {
815b5368
EB
6523 gigi_checking_assert (!Do_Range_Check (Expression (gnat_node)));
6524
a1ab4c31 6525 gnu_expr = gnat_to_gnu (Expression (gnat_node));
a1ab4c31 6526
b4407150 6527 /* First deal with erroneous expressions. */
104099b8
EB
6528 if (TREE_CODE (gnu_expr) == ERROR_MARK)
6529 {
6530 /* If this is a named number for which we cannot manipulate
6531 the value, just skip the declaration altogether. */
6532 if (kind == N_Number_Declaration)
6533 break;
6534 else if (type_annotate_only)
6535 gnu_expr = NULL_TREE;
6536 }
b4407150
EB
6537
6538 /* Then a special case: we do not want the SLOC of the expression
6539 of the tag to pop up every time it is referenced somewhere. */
6540 else if (EXPR_P (gnu_expr) && Is_Tag (gnat_temp))
6541 SET_EXPR_LOCATION (gnu_expr, UNKNOWN_LOCATION);
a1ab4c31
AC
6542 }
6543 else
6544 gnu_expr = NULL_TREE;
6545
8df2e902
EB
6546 /* If this is a deferred constant with an address clause, we ignore the
6547 full view since the clause is on the partial view and we cannot have
6548 2 different GCC trees for the object. The only bits of the full view
6549 we will use is the initializer, but it will be directly fetched. */
7c775aca 6550 if (Ekind (gnat_temp) == E_Constant
8df2e902
EB
6551 && Present (Address_Clause (gnat_temp))
6552 && Present (Full_View (gnat_temp)))
6553 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
6554
545b4923
EB
6555 /* If this object has its elaboration delayed, we must force evaluation
6556 of GNU_EXPR now and save it for the freeze point. Note that we need
6557 not do anything special at the global level since the lifetime of the
6558 temporary is fully contained within the elaboration routine. */
6559 if (Present (Freeze_Node (gnat_temp)))
6560 {
6561 if (gnu_expr)
6562 {
6563 gnu_result = gnat_save_expr (gnu_expr);
6564 save_gnu_tree (gnat_node, gnu_result, true);
6565 }
6566 }
6567 else
afc737f0 6568 gnat_to_gnu_entity (gnat_temp, gnu_expr, true);
a1ab4c31
AC
6569 break;
6570
6571 case N_Object_Renaming_Declaration:
6572 gnat_temp = Defining_Entity (gnat_node);
ea292448 6573 gnu_result = alloc_stmt_list ();
a1ab4c31 6574
656b0311
PMR
6575 /* Don't do anything if this renaming is handled by the front end and it
6576 does not need debug info. Note that we consider renamings don't need
6577 debug info when optimizing: our way to describe them has a
6578 memory/elaboration footprint.
6579
6580 Don't do anything neither if we are just annotating types and this
6581 object has a composite or task type, don't elaborate it. */
6582 if ((!Is_Renaming_Of_Object (gnat_temp)
6583 || (Needs_Debug_Info (gnat_temp)
6584 && !optimize
6585 && can_materialize_object_renaming_p
6586 (Renamed_Object (gnat_temp))))
a1ab4c31
AC
6587 && ! (type_annotate_only
6588 && (Is_Array_Type (Etype (gnat_temp))
6589 || Is_Record_Type (Etype (gnat_temp))
6590 || Is_Concurrent_Type (Etype (gnat_temp)))))
5bdd063b
EB
6591 gnat_to_gnu_entity (gnat_temp,
6592 gnat_to_gnu (Renamed_Object (gnat_temp)),
6593 true);
a1ab4c31
AC
6594 break;
6595
ac43e11e
AC
6596 case N_Exception_Renaming_Declaration:
6597 gnat_temp = Defining_Entity (gnat_node);
ea292448
EB
6598 gnu_result = alloc_stmt_list ();
6599
ea292448 6600 if (Present (Renamed_Entity (gnat_temp)))
5bdd063b
EB
6601 gnat_to_gnu_entity (gnat_temp,
6602 gnat_to_gnu (Renamed_Entity (gnat_temp)),
6603 true);
ac43e11e
AC
6604 break;
6605
1d4b96e0
AC
6606 case N_Subprogram_Renaming_Declaration:
6607 {
6608 const Node_Id gnat_renaming = Defining_Entity (gnat_node);
6609 const Node_Id gnat_renamed = Renamed_Entity (gnat_renaming);
6610
6611 gnu_result = alloc_stmt_list ();
6612
6613 /* Materializing renamed subprograms will only benefit the debugging
6614 information as they aren't referenced in the generated code. So
6615 skip them when they aren't needed. Avoid doing this if:
6616
6617 - there is a freeze node: in this case the renamed entity is not
16f19962 6618 elaborated yet,
1d4b96e0
AC
6619 - the renamed subprogram is intrinsic: it will not be available in
6620 the debugging information (note that both or only one of the
6621 renaming and the renamed subprograms can be intrinsic). */
16f19962 6622 if (!type_annotate_only
1d4b96e0 6623 && Needs_Debug_Info (gnat_renaming)
16f19962 6624 && No (Freeze_Node (gnat_renaming))
1d4b96e0
AC
6625 && Present (gnat_renamed)
6626 && (Ekind (gnat_renamed) == E_Function
6627 || Ekind (gnat_renamed) == E_Procedure)
6628 && !Is_Intrinsic_Subprogram (gnat_renaming)
6629 && !Is_Intrinsic_Subprogram (gnat_renamed))
afc737f0 6630 gnat_to_gnu_entity (gnat_renaming, gnat_to_gnu (gnat_renamed), true);
1d4b96e0
AC
6631 break;
6632 }
6633
a1ab4c31 6634 case N_Implicit_Label_Declaration:
afc737f0 6635 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true);
a1ab4c31
AC
6636 gnu_result = alloc_stmt_list ();
6637 break;
6638
ac43e11e 6639 case N_Package_Renaming_Declaration:
a1ab4c31 6640 /* These are fully handled in the front end. */
ac43e11e
AC
6641 /* ??? For package renamings, find a way to use GENERIC namespaces so
6642 that we get proper debug information for them. */
a1ab4c31
AC
6643 gnu_result = alloc_stmt_list ();
6644 break;
6645
6646 /*************************************/
1e17ef87 6647 /* Chapter 4: Names and Expressions */
a1ab4c31
AC
6648 /*************************************/
6649
6650 case N_Explicit_Dereference:
806fcf71 6651 /* Make sure the designated type is complete before dereferencing. */
a1ab4c31 6652 gnu_result_type = get_unpadded_type (Etype (gnat_node));
806fcf71 6653 gnu_result = gnat_to_gnu (Prefix (gnat_node));
a1ab4c31 6654 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
033ba5bf 6655
f797c2b7 6656 /* If atomic access is required on the RHS, build the atomic load. */
17a98a3d 6657 if (simple_atomic_access_required_p (gnat_node, &aa_sync)
033ba5bf 6658 && !present_in_lhs_or_actual_p (gnat_node))
17a98a3d 6659 gnu_result = build_atomic_load (gnu_result, aa_sync);
544d14e1
EB
6660
6661 /* If storage model access is required on the RHS, build the load. */
6662 else if (storage_model_access_required_p (gnat_node, &gnat_smo)
6663 && Present (Storage_Model_Copy_From (gnat_smo))
6664 && !present_in_lhs_or_actual_p (gnat_node))
6665 gnu_result = build_storage_model_load (gnat_smo, gnu_result);
a1ab4c31
AC
6666 break;
6667
6668 case N_Indexed_Component:
6669 {
544d14e1
EB
6670 const Entity_Id gnat_array_object = Prefix (gnat_node);
6671 tree gnu_array_object = gnat_to_gnu (gnat_array_object);
a1ab4c31 6672 tree gnu_type;
bb24f343 6673 int ndim, i;
a1ab4c31
AC
6674 Node_Id *gnat_expr_array;
6675
544d14e1
EB
6676 /* Get the storage model of the array. */
6677 gnat_smo = get_storage_model (gnat_array_object);
6678
bb24f343 6679 gnu_array_object = maybe_padded_object (gnu_array_object);
ad00a297 6680 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
7948ae37
OH
6681
6682 /* Convert vector inputs to their representative array type, to fit
6683 what the code below expects. */
f71d5704
EB
6684 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
6685 {
6686 if (present_in_lhs_or_actual_p (gnat_node))
6687 gnat_mark_addressable (gnu_array_object);
6688 gnu_array_object = maybe_vector_array (gnu_array_object);
6689 }
7948ae37 6690
54441a34
EB
6691 /* The failure of this assertion will very likely come from a missing
6692 expansion for a packed array access. */
6693 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
6694
a1ab4c31
AC
6695 /* First compute the number of dimensions of the array, then
6696 fill the expression array, the order depending on whether
6697 this is a Convention_Fortran array or not. */
6698 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
6699 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6700 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
6701 ndim++, gnu_type = TREE_TYPE (gnu_type))
6702 ;
6703
2bb1fc26 6704 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
a1ab4c31
AC
6705
6706 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
6707 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
6708 i >= 0;
6709 i--, gnat_temp = Next (gnat_temp))
6710 gnat_expr_array[i] = gnat_temp;
6711 else
6712 for (i = 0, gnat_temp = First (Expressions (gnat_node));
6713 i < ndim;
6714 i++, gnat_temp = Next (gnat_temp))
6715 gnat_expr_array[i] = gnat_temp;
6716
825da0d2
EB
6717 /* Start with the prefix and build the successive references. */
6718 gnu_result = gnu_array_object;
6719
a1ab4c31 6720 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
87ab2b04
EB
6721 i < ndim;
6722 i++, gnu_type = TREE_TYPE (gnu_type))
a1ab4c31
AC
6723 {
6724 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
6725 gnat_temp = gnat_expr_array[i];
825da0d2 6726 gnu_expr = maybe_character_value (gnat_to_gnu (gnat_temp));
a1ab4c31 6727
384e3fb1
JM
6728 gnu_result
6729 = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr);
544d14e1
EB
6730
6731 if (Present (gnat_smo))
6732 instantiate_load_in_array_ref (gnu_result, gnat_smo);
a1ab4c31 6733 }
a1ab4c31 6734
033ba5bf
EB
6735 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6736
f797c2b7 6737 /* If atomic access is required on the RHS, build the atomic load. */
17a98a3d 6738 if (simple_atomic_access_required_p (gnat_node, &aa_sync)
033ba5bf 6739 && !present_in_lhs_or_actual_p (gnat_node))
17a98a3d 6740 gnu_result = build_atomic_load (gnu_result, aa_sync);
544d14e1
EB
6741
6742 /* If storage model access is required on the RHS, build the load. */
6743 else if (storage_model_access_required_p (gnat_node, &gnat_smo)
6744 && Present (Storage_Model_Copy_From (gnat_smo))
6745 && !present_in_lhs_or_actual_p (gnat_node))
6746 gnu_result = build_storage_model_load (gnat_smo, gnu_result);
033ba5bf 6747 }
a1ab4c31
AC
6748 break;
6749
6750 case N_Slice:
6751 {
544d14e1
EB
6752 const Entity_Id gnat_array_object = Prefix (gnat_node);
6753 tree gnu_array_object = gnat_to_gnu (gnat_array_object);
a1ab4c31 6754
544d14e1
EB
6755 /* Get the storage model of the array. */
6756 gnat_smo = get_storage_model (gnat_array_object);
a1ab4c31 6757
bb24f343 6758 gnu_array_object = maybe_padded_object (gnu_array_object);
825da0d2
EB
6759 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
6760
544d14e1
EB
6761 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6762
825da0d2
EB
6763 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
6764 gnu_expr = maybe_character_value (gnu_expr);
a1ab4c31 6765
f76d6e6f
EB
6766 /* If this is a slice with non-constant size of an array with constant
6767 size, set the maximum size for the allocation of temporaries. */
6768 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
825da0d2
EB
6769 && TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object))))
6770 TYPE_ARRAY_MAX_SIZE (gnu_result_type)
6771 = TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object));
f76d6e6f 6772
a1ab4c31 6773 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
825da0d2 6774 gnu_array_object, gnu_expr);
544d14e1
EB
6775
6776 if (Present (gnat_smo))
6777 instantiate_load_in_array_ref (gnu_result, gnat_smo);
6778
6779 /* If storage model access is required on the RHS, build the load. */
6780 if (storage_model_access_required_p (gnat_node, &gnat_smo)
6781 && Present (Storage_Model_Copy_From (gnat_smo))
6782 && !present_in_lhs_or_actual_p (gnat_node))
6783 gnu_result = build_storage_model_load (gnat_smo, gnu_result);
a1ab4c31
AC
6784 }
6785 break;
6786
6787 case N_Selected_Component:
6788 {
bb24f343 6789 const Entity_Id gnat_prefix = Prefix (gnat_node);
a1ab4c31 6790 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
1eb58520 6791 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
a1ab4c31 6792
bb24f343 6793 gnu_prefix = maybe_padded_object (gnu_prefix);
a1ab4c31 6794
4ec7c4ec
EB
6795 /* gnat_to_gnu_entity does not save the GNU tree made for renamed
6796 discriminants so avoid making recursive calls on each reference
6797 to them by following the appropriate link directly here. */
6798 if (Ekind (gnat_field) == E_Discriminant)
6799 {
6800 /* For discriminant references in tagged types always substitute
6801 the corresponding discriminant as the actual component. */
6802 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix))))
6803 while (Present (Corresponding_Discriminant (gnat_field)))
6804 gnat_field = Corresponding_Discriminant (gnat_field);
6805
6806 /* For discriminant references in untagged types always substitute
6807 the corresponding stored discriminant. */
6808 else if (Present (Corresponding_Discriminant (gnat_field)))
6809 gnat_field = Original_Record_Component (gnat_field);
6810 }
a1ab4c31
AC
6811
6812 /* Handle extracting the real or imaginary part of a complex.
6813 The real part is the first field and the imaginary the last. */
a1ab4c31
AC
6814 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
6815 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
6816 ? REALPART_EXPR : IMAGPART_EXPR,
6817 NULL_TREE, gnu_prefix);
6818 else
6819 {
d3271136 6820 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
852dd866
EB
6821 tree gnu_offset;
6822 struct loop_info_d *loop;
d3271136 6823
a1ab4c31 6824 gnu_result
64235766 6825 = build_component_ref (gnu_prefix, gnu_field,
a1ab4c31 6826 (Nkind (Parent (gnat_node))
3cd64bab
EB
6827 == N_Attribute_Reference)
6828 && lvalue_required_for_attribute_p
6829 (Parent (gnat_node)));
852dd866
EB
6830
6831 /* If optimization is enabled and we are inside a loop, we try to
6832 hoist nonconstant but invariant offset computations outside of
6833 the loop, since they very likely contain loads that could turn
6834 out to be hard to move if they end up in active EH regions. */
6835 if (optimize
6836 && inside_loop_p ()
6837 && TREE_CODE (gnu_result) == COMPONENT_REF
6838 && (gnu_offset = component_ref_field_offset (gnu_result))
6839 && !TREE_CONSTANT (gnu_offset)
6840 && (gnu_offset = gnat_invariant_expr (gnu_offset))
6841 && (loop = find_loop ()))
6842 {
6843 tree invariant
6844 = build1 (SAVE_EXPR, TREE_TYPE (gnu_offset), gnu_offset);
6845 vec_safe_push (loop->invariants, invariant);
6846 tree field = TREE_OPERAND (gnu_result, 1);
6847 tree factor
6848 = size_int (DECL_OFFSET_ALIGN (field) / BITS_PER_UNIT);
6849 /* Divide the offset by its alignment. */
6850 TREE_OPERAND (gnu_result, 2)
6851 = size_binop (EXACT_DIV_EXPR, invariant, factor);
6852 }
a1ab4c31
AC
6853 }
6854
a1ab4c31 6855 gnu_result_type = get_unpadded_type (Etype (gnat_node));
033ba5bf 6856
f797c2b7 6857 /* If atomic access is required on the RHS, build the atomic load. */
17a98a3d 6858 if (simple_atomic_access_required_p (gnat_node, &aa_sync)
033ba5bf 6859 && !present_in_lhs_or_actual_p (gnat_node))
17a98a3d 6860 gnu_result = build_atomic_load (gnu_result, aa_sync);
544d14e1
EB
6861
6862 /* If storage model access is required on the RHS, build the load. */
6863 else if (storage_model_access_required_p (gnat_node, &gnat_smo)
6864 && Present (Storage_Model_Copy_From (gnat_smo))
6865 && !present_in_lhs_or_actual_p (gnat_node))
6866 gnu_result = build_storage_model_load (gnat_smo, gnu_result);
a1ab4c31
AC
6867 }
6868 break;
6869
6870 case N_Attribute_Reference:
6871 {
86060344
EB
6872 /* The attribute designator. */
6873 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
6874
6875 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
6876 is a unit, not an object with a GCC equivalent. */
6877 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
6878 return
6879 create_subprog_decl (create_concat_name
6880 (Entity (Prefix (gnat_node)),
6881 attr == Attr_Elab_Body ? "elabb" : "elabs"),
13a6dfe3 6882 NULL_TREE, void_ftype, NULL_TREE, is_default,
ff9baa5f
PMR
6883 true, true, true, true, false, NULL,
6884 gnat_node);
86060344
EB
6885
6886 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
a1ab4c31
AC
6887 }
6888 break;
6889
6890 case N_Reference:
6891 /* Like 'Access as far as we are concerned. */
6892 gnu_result = gnat_to_gnu (Prefix (gnat_node));
6893 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6894 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6895 break;
6896
6897 case N_Aggregate:
6898 case N_Extension_Aggregate:
6899 {
6900 tree gnu_aggr_type;
6901
14cc7b26 6902 /* Check that this aggregate has not slipped through the cracks. */
a1ab4c31
AC
6903 gcc_assert (!Expansion_Delayed (gnat_node));
6904
14cc7b26 6905 gnu_result_type = get_unpadded_type (Etype (gnat_node));
a1ab4c31
AC
6906
6907 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
6908 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
6909 gnu_aggr_type
7d76717d 6910 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
7948ae37
OH
6911 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
6912 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
14cc7b26
EB
6913 else
6914 gnu_aggr_type = gnu_result_type;
a1ab4c31
AC
6915
6916 if (Null_Record_Present (gnat_node))
b1b2b511 6917 gnu_result = gnat_build_constructor (gnu_aggr_type, NULL);
a1ab4c31
AC
6918
6919 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
6920 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
6921 gnu_result
6922 = assoc_to_constructor (Etype (gnat_node),
6923 First (Component_Associations (gnat_node)),
6924 gnu_aggr_type);
6925 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
6926 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
815b5368 6927 gnu_aggr_type);
a1ab4c31
AC
6928 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
6929 gnu_result
6930 = build_binary_op
6931 (COMPLEX_EXPR, gnu_aggr_type,
6932 gnat_to_gnu (Expression (First
6933 (Component_Associations (gnat_node)))),
6934 gnat_to_gnu (Expression
6935 (Next
6936 (First (Component_Associations (gnat_node))))));
6937 else
6938 gcc_unreachable ();
6939
6940 gnu_result = convert (gnu_result_type, gnu_result);
6941 }
6942 break;
6943
6944 case N_Null:
6945 if (TARGET_VTABLE_USES_DESCRIPTORS
6946 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
6947 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
6948 gnu_result = null_fdesc_node;
6949 else
6950 gnu_result = null_pointer_node;
6951 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6952 break;
6953
6954 case N_Type_Conversion:
6955 case N_Qualified_Expression:
825da0d2 6956 gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node)));
a1ab4c31
AC
6957 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6958
0029bafd
EB
6959 /* If this is a qualified expression for a tagged type, we mark the type
6960 as used. Because of polymorphism, this might be the only reference to
6961 the tagged type in the program while objects have it as dynamic type.
6962 The debugger needs to see it to display these objects properly. */
6963 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
6964 used_types_insert (gnu_result_type);
6965
815b5368
EB
6966 gigi_checking_assert (!Do_Range_Check (Expression (gnat_node)));
6967
a1ab4c31 6968 gnu_result
825da0d2 6969 = convert_with_check (Etype (gnat_node), gnu_expr,
a1ab4c31 6970 Do_Overflow_Check (gnat_node),
3f13dd77 6971 kind == N_Type_Conversion
10069d53 6972 && Float_Truncate (gnat_node), gnat_node);
a1ab4c31
AC
6973 break;
6974
6975 case N_Unchecked_Type_Conversion:
4ec7c4ec 6976 gnu_result_type = get_unpadded_type (Etype (gnat_node));
825da0d2 6977 gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node)));
c2efda0d
EB
6978
6979 /* Skip further processing if the conversion is deemed a no-op. */
4f8a6678 6980 if (unchecked_conversion_nop (gnat_node))
c2efda0d 6981 {
825da0d2 6982 gnu_result = gnu_expr;
c2efda0d
EB
6983 gnu_result_type = TREE_TYPE (gnu_result);
6984 break;
6985 }
6986
a1ab4c31
AC
6987 /* If the result is a pointer type, see if we are improperly
6988 converting to a stricter alignment. */
6989 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
7ed9919d 6990 && Is_Access_Type (Etype (gnat_node)))
a1ab4c31 6991 {
825da0d2 6992 unsigned int align = known_alignment (gnu_expr);
a1ab4c31
AC
6993 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6994 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6995
6996 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6997 post_error_ne_tree_2
4a29b8d6 6998 ("??source alignment (^) '< alignment of & (^)",
a1ab4c31
AC
6999 gnat_node, Designated_Type (Etype (gnat_node)),
7000 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
7001 }
7002
7003 /* If we are converting a descriptor to a function pointer, first
7004 build the pointer. */
7005 if (TARGET_VTABLE_USES_DESCRIPTORS
825da0d2 7006 && TREE_TYPE (gnu_expr) == fdesc_type_node
a1ab4c31 7007 && POINTER_TYPE_P (gnu_result_type))
825da0d2 7008 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
a1ab4c31 7009
825da0d2 7010 gnu_result = unchecked_convert (gnu_result_type, gnu_expr,
a1ab4c31
AC
7011 No_Truncation (gnat_node));
7012 break;
7013
7014 case N_In:
7015 case N_Not_In:
7016 {
da49a783 7017 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
da49a783 7018 tree gnu_low, gnu_high;
a1ab4c31 7019
3eaec285 7020 Range_to_gnu (Right_Opnd (gnat_node), &gnu_low, &gnu_high);
a1ab4c31
AC
7021 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7022
825da0d2
EB
7023 tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_obj));
7024 if (TREE_TYPE (gnu_obj) != gnu_op_type)
7025 {
7026 gnu_obj = convert (gnu_op_type, gnu_obj);
7027 gnu_low = convert (gnu_op_type, gnu_low);
7028 gnu_high = convert (gnu_op_type, gnu_high);
7029 }
7030
da49a783
EB
7031 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
7032 ensure that GNU_OBJ is evaluated only once and perform a full range
7033 test. */
a1ab4c31 7034 if (operand_equal_p (gnu_low, gnu_high, 0))
da49a783
EB
7035 gnu_result
7036 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
a1ab4c31
AC
7037 else
7038 {
da49a783 7039 tree t1, t2;
7d7a1fe8 7040 gnu_obj = gnat_protect_expr (gnu_obj);
da49a783
EB
7041 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
7042 if (EXPR_P (t1))
7043 set_expr_location_from_node (t1, gnat_node);
7044 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
7045 if (EXPR_P (t2))
7046 set_expr_location_from_node (t2, gnat_node);
a1ab4c31 7047 gnu_result
da49a783 7048 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
a1ab4c31
AC
7049 }
7050
3f13dd77 7051 if (kind == N_Not_In)
658a41ac
EB
7052 gnu_result
7053 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
a1ab4c31
AC
7054 }
7055 break;
7056
7057 case N_Op_Divide:
7058 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
7059 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
7060 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7061 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
7062 ? RDIV_EXPR
7063 : (Rounded_Result (gnat_node)
7064 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
7065 gnu_result_type, gnu_lhs, gnu_rhs);
a80e0583
EB
7066 /* If the result type is larger than a word, then declare the dependence
7067 on the libgcc routine. */
7068 if (INTEGRAL_TYPE_P (gnu_result_type)
7069 && TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD)
7070 Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node);
a1ab4c31
AC
7071 break;
7072
9a1bdc31
EB
7073 case N_Op_Eq:
7074 case N_Op_Ne:
7075 case N_Op_Lt:
7076 case N_Op_Le:
7077 case N_Op_Gt:
7078 case N_Op_Ge:
7079 case N_Op_Add:
7080 case N_Op_Subtract:
7081 case N_Op_Multiply:
7082 case N_Op_Mod:
7083 case N_Op_Rem:
a1ab4c31
AC
7084 case N_Op_Rotate_Left:
7085 case N_Op_Rotate_Right:
7086 case N_Op_Shift_Left:
7087 case N_Op_Shift_Right:
7088 case N_Op_Shift_Right_Arithmetic:
22903f22
EB
7089 case N_Op_And:
7090 case N_Op_Or:
7091 case N_Op_Xor:
9a1bdc31
EB
7092 case N_And_Then:
7093 case N_Or_Else:
a1ab4c31 7094 {
3f13dd77 7095 enum tree_code code = gnu_codes[kind];
a1ab4c31 7096 bool ignore_lhs_overflow = false;
1fc24649 7097 location_t saved_location = input_location;
73cd514a 7098 tree gnu_type, gnu_max_shift = NULL_TREE;
a1ab4c31 7099
22903f22
EB
7100 /* Fix operations set up for boolean types in GNU_CODES above. */
7101 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
7102 switch (kind)
7103 {
7104 case N_Op_And:
7105 code = BIT_AND_EXPR;
7106 break;
7107 case N_Op_Or:
7108 code = BIT_IOR_EXPR;
7109 break;
7110 case N_Op_Xor:
7111 code = BIT_XOR_EXPR;
7112 break;
7113 default:
7114 break;
7115 }
7116
a1ab4c31
AC
7117 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
7118 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
7119 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
7120
73cd514a
EB
7121 /* If this is a shift, take the count as unsigned since that is what
7122 most machines do and will generate simpler adjustments below. */
7123 if (IN (kind, N_Op_Shift))
7124 {
7125 tree gnu_count_type
7126 = gnat_unsigned_type_for (get_base_type (TREE_TYPE (gnu_rhs)));
7127 gnu_rhs = convert (gnu_count_type, gnu_rhs);
7128 gnu_max_shift
7129 = convert (TREE_TYPE (gnu_rhs), TYPE_SIZE (gnu_type));
a80e0583
EB
7130 /* If the result type is larger than a word, then declare the dependence
7131 on the libgcc routine. */
7132 if (TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD)
7133 Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node);
73cd514a
EB
7134 }
7135
1f3f64b9
EB
7136 /* If this is a comparison between (potentially) large aggregates, then
7137 declare the dependence on the memcmp routine. */
7138 else if ((kind == N_Op_Eq || kind == N_Op_Ne)
7139 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs))
7140 && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs)))
7141 || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs)),
7142 2 * BITS_PER_WORD) > 0))
7143 Check_Restriction_No_Dependence_On_System (Name_Memory_Compare,
7144 gnat_node);
7145
a80e0583
EB
7146 /* If this is a modulo/remainder and the result type is larger than a
7147 word, then declare the dependence on the libgcc routine. */
7148 else if ((kind == N_Op_Mod ||kind == N_Op_Rem)
7149 && TYPE_PRECISION (gnu_result_type) > BITS_PER_WORD)
7150 Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node);
7151
7948ae37 7152 /* Pending generic support for efficient vector logical operations in
1f3f64b9 7153 GCC, convert vectors to their representative array type view. */
7948ae37
OH
7154 gnu_lhs = maybe_vector_array (gnu_lhs);
7155 gnu_rhs = maybe_vector_array (gnu_rhs);
7156
43a4dd82
EB
7157 /* If this is a comparison operator, convert any references to an
7158 unconstrained array value into a reference to the actual array. */
a1ab4c31
AC
7159 if (TREE_CODE_CLASS (code) == tcc_comparison)
7160 {
7161 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
7162 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
825da0d2
EB
7163
7164 tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_lhs));
7165 if (TREE_TYPE (gnu_lhs) != gnu_op_type)
7166 {
7167 gnu_lhs = convert (gnu_op_type, gnu_lhs);
7168 gnu_rhs = convert (gnu_op_type, gnu_rhs);
7169 }
a1ab4c31
AC
7170 }
7171
a1ab4c31
AC
7172 /* If this is a shift whose count is not guaranteed to be correct,
7173 we need to adjust the shift count. */
73cd514a
EB
7174 if ((kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
7175 && !Shift_Count_OK (gnat_node))
7176 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, TREE_TYPE (gnu_rhs),
7177 gnu_rhs, gnu_max_shift);
7178 else if (kind == N_Op_Shift_Right_Arithmetic
7179 && !Shift_Count_OK (gnat_node))
7180 gnu_rhs
7181 = build_binary_op (MIN_EXPR, TREE_TYPE (gnu_rhs),
7182 build_binary_op (MINUS_EXPR,
7183 TREE_TYPE (gnu_rhs),
7184 gnu_max_shift,
7185 build_int_cst
7186 (TREE_TYPE (gnu_rhs), 1)),
7187 gnu_rhs);
a1ab4c31
AC
7188
7189 /* For right shifts, the type says what kind of shift to do,
7190 so we may need to choose a different type. In this case,
7191 we have to ignore integer overflow lest it propagates all
7192 the way down and causes a CE to be explicitly raised. */
3f13dd77 7193 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
a1ab4c31 7194 {
9a1bdc31 7195 gnu_type = gnat_unsigned_type_for (gnu_type);
a1ab4c31
AC
7196 ignore_lhs_overflow = true;
7197 }
3f13dd77 7198 else if (kind == N_Op_Shift_Right_Arithmetic
a1ab4c31
AC
7199 && TYPE_UNSIGNED (gnu_type))
7200 {
9a1bdc31 7201 gnu_type = gnat_signed_type_for (gnu_type);
a1ab4c31
AC
7202 ignore_lhs_overflow = true;
7203 }
7204
7205 if (gnu_type != gnu_result_type)
7206 {
7207 tree gnu_old_lhs = gnu_lhs;
7208 gnu_lhs = convert (gnu_type, gnu_lhs);
7209 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
7210 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
7211 gnu_rhs = convert (gnu_type, gnu_rhs);
1f8fc1f4
EB
7212 if (gnu_max_shift)
7213 gnu_max_shift = convert (gnu_type, gnu_max_shift);
a1ab4c31
AC
7214 }
7215
f552aa62
EB
7216 /* For signed integer addition, subtraction and multiplication, do an
7217 overflow check if required. */
9a1bdc31 7218 if (Do_Overflow_Check (gnat_node)
9a1bdc31 7219 && (code == PLUS_EXPR || code == MINUS_EXPR || code == MULT_EXPR)
b666e568
GB
7220 && !TYPE_UNSIGNED (gnu_type)
7221 && !FLOAT_TYPE_P (gnu_type))
f552aa62
EB
7222 gnu_result
7223 = build_binary_op_trapv (code, gnu_type, gnu_lhs, gnu_rhs,
7224 gnat_node);
b666e568 7225 else
1fc24649
EB
7226 {
7227 /* Some operations, e.g. comparisons of arrays, generate complex
7228 trees that need to be annotated while they are being built. */
7229 input_location = saved_location;
7230 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
7231 }
a1ab4c31
AC
7232
7233 /* If this is a logical shift with the shift count not verified,
7234 we must return zero if it is too large. We cannot compensate
73cd514a 7235 beforehand in this case. */
3f13dd77 7236 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
a1ab4c31
AC
7237 && !Shift_Count_OK (gnat_node))
7238 gnu_result
73cd514a
EB
7239 = build_cond_expr (gnu_type,
7240 build_binary_op (GE_EXPR, boolean_type_node,
7241 gnu_rhs, gnu_max_shift),
7242 build_int_cst (gnu_type, 0),
7243 gnu_result);
a1ab4c31
AC
7244 }
7245 break;
7246
9b16cb57 7247 case N_If_Expression:
a1ab4c31 7248 {
1e17ef87
EB
7249 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
7250 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
7251 tree gnu_false
7252 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
a1ab4c31
AC
7253
7254 gnu_result_type = get_unpadded_type (Etype (gnat_node));
3f13dd77
EB
7255 gnu_result
7256 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
a1ab4c31
AC
7257 }
7258 break;
7259
7260 case N_Op_Plus:
7261 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
7262 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7263 break;
7264
7265 case N_Op_Not:
7266 /* This case can apply to a boolean or a modular type.
7267 Fall through for a boolean operand since GNU_CODES is set
7268 up to handle this. */
43a4dd82 7269 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
a1ab4c31
AC
7270 {
7271 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
7272 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7273 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
7274 gnu_expr);
7275 break;
7276 }
7277
9c453de7 7278 /* ... fall through ... */
a1ab4c31 7279
9a1bdc31
EB
7280 case N_Op_Minus:
7281 case N_Op_Abs:
a1ab4c31 7282 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
fd6e497e 7283 gnu_result_type = get_unpadded_type (Etype (gnat_node));
a1ab4c31 7284
f552aa62
EB
7285 /* For signed integer negation and absolute value, do an overflow check
7286 if required. */
b666e568
GB
7287 if (Do_Overflow_Check (gnat_node)
7288 && !TYPE_UNSIGNED (gnu_result_type)
7289 && !FLOAT_TYPE_P (gnu_result_type))
10069d53 7290 gnu_result
f552aa62
EB
7291 = build_unary_op_trapv (gnu_codes[kind], gnu_result_type, gnu_expr,
7292 gnat_node);
b666e568 7293 else
f552aa62
EB
7294 gnu_result
7295 = build_unary_op (gnu_codes[kind], gnu_result_type, gnu_expr);
a1ab4c31
AC
7296 break;
7297
7298 case N_Allocator:
7299 {
d63fbcf8
EB
7300 tree gnu_type, gnu_init;
7301 bool ignore_init_type;
a1ab4c31
AC
7302
7303 gnat_temp = Expression (gnat_node);
7304
815b5368
EB
7305 /* The expression can be either an N_Identifier or an Expanded_Name,
7306 which must represent a type, or a N_Qualified_Expression, which
7307 contains both the type and an initial value for the object. */
a1ab4c31
AC
7308 if (Nkind (gnat_temp) == N_Identifier
7309 || Nkind (gnat_temp) == N_Expanded_Name)
d63fbcf8
EB
7310 {
7311 ignore_init_type = false;
7312 gnu_init = NULL_TREE;
7313 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
7314 }
7315
a1ab4c31
AC
7316 else if (Nkind (gnat_temp) == N_Qualified_Expression)
7317 {
bb24f343 7318 const Entity_Id gnat_desig_type
a1ab4c31
AC
7319 = Designated_Type (Underlying_Type (Etype (gnat_node)));
7320
ce2d0ce2 7321 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
a1ab4c31 7322
d63fbcf8 7323 gnu_init = gnat_to_gnu (Expression (gnat_temp));
a1ab4c31 7324 gnu_init = maybe_unconstrained_array (gnu_init);
815b5368
EB
7325
7326 gigi_checking_assert (!Do_Range_Check (Expression (gnat_temp)));
a1ab4c31
AC
7327
7328 if (Is_Elementary_Type (gnat_desig_type)
7329 || Is_Constrained (gnat_desig_type))
0029bafd 7330 gnu_type = gnat_to_gnu_type (gnat_desig_type);
a1ab4c31
AC
7331 else
7332 {
7333 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
7334 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
7335 gnu_type = TREE_TYPE (gnu_init);
a1ab4c31 7336 }
0029bafd
EB
7337
7338 /* See the N_Qualified_Expression case for the rationale. */
7339 if (Is_Tagged_Type (gnat_desig_type))
7340 used_types_insert (gnu_type);
7341
7342 gnu_init = convert (gnu_type, gnu_init);
a1ab4c31
AC
7343 }
7344 else
7345 gcc_unreachable ();
7346
7347 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7348 return build_allocator (gnu_type, gnu_init, gnu_result_type,
7349 Procedure_To_Call (gnat_node),
7350 Storage_Pool (gnat_node), gnat_node,
7351 ignore_init_type);
7352 }
7353 break;
7354
1e17ef87
EB
7355 /**************************/
7356 /* Chapter 5: Statements */
7357 /**************************/
a1ab4c31
AC
7358
7359 case N_Label:
7360 gnu_result = build1 (LABEL_EXPR, void_type_node,
7361 gnat_to_gnu (Identifier (gnat_node)));
7362 break;
7363
7364 case N_Null_Statement:
9c69c3af
EB
7365 /* When not optimizing, turn null statements from source into gotos to
7366 the next statement that the middle-end knows how to preserve. */
7367 if (!optimize && Comes_From_Source (gnat_node))
7368 {
88a94e2b
EB
7369 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
7370 DECL_IGNORED_P (label) = 1;
9c69c3af
EB
7371 start_stmt_group ();
7372 stmt = build1 (GOTO_EXPR, void_type_node, label);
7373 set_expr_location_from_node (stmt, gnat_node);
7374 add_stmt (stmt);
7375 stmt = build1 (LABEL_EXPR, void_type_node, label);
7376 set_expr_location_from_node (stmt, gnat_node);
7377 add_stmt (stmt);
7378 gnu_result = end_stmt_group ();
7379 }
7380 else
7381 gnu_result = alloc_stmt_list ();
a1ab4c31
AC
7382 break;
7383
7384 case N_Assignment_Statement:
0c5730a6 7385 /* First get the LHS of the statement and convert any reference to an
0b3467c4 7386 unconstrained array into a reference to the underlying array. */
a1ab4c31
AC
7387 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
7388
7389 /* If the type has a size that overflows, convert this into raise of
7390 Storage_Error: execution shouldn't have gotten here anyway. */
7391 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
ce3da0d0 7392 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
a1ab4c31
AC
7393 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
7394 N_Raise_Storage_Error);
0c5730a6
EB
7395
7396 /* If the RHS is a function call, let Call_to_gnu do the entire work. */
0b3467c4 7397 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
f797c2b7 7398 {
17a98a3d 7399 get_atomic_access (Name (gnat_node), &aa_type, &aa_sync);
544d14e1 7400 get_storage_model_access (Name (gnat_node), &gnat_smo);
f797c2b7
EB
7401 gnu_result
7402 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
544d14e1 7403 aa_type, aa_sync, gnat_smo);
f797c2b7 7404 }
0c5730a6
EB
7405
7406 /* Otherwise we need to build the assignment statement manually. */
a1ab4c31
AC
7407 else
7408 {
ac43e11e 7409 const Node_Id gnat_expr = Expression (gnat_node);
b9364a56
EB
7410 const Node_Id gnat_inner
7411 = Nkind (gnat_expr) == N_Qualified_Expression
7412 ? Expression (gnat_expr)
7413 : gnat_expr;
ac43e11e
AC
7414 const Entity_Id gnat_type
7415 = Underlying_Type (Etype (Name (gnat_node)));
ac43e11e 7416 const bool use_memset_p
95c9c1c0 7417 = Is_Array_Type (gnat_type)
b9364a56 7418 && Nkind (gnat_inner) == N_Aggregate
27c3d986 7419 && Is_Single_Aggregate (gnat_inner);
ac43e11e 7420
b9364a56 7421 /* If we use memset, we need to find the innermost expression. */
ac43e11e
AC
7422 if (use_memset_p)
7423 {
b9364a56
EB
7424 gnat_temp = gnat_inner;
7425 do {
7426 gnat_temp
7427 = Expression (First (Component_Associations (gnat_temp)));
7428 } while (Nkind (gnat_temp) == N_Aggregate
27c3d986 7429 && Is_Single_Aggregate (gnat_temp));
b9364a56 7430 gnu_rhs = gnat_to_gnu (gnat_temp);
ac43e11e 7431 }
0c5730a6
EB
7432
7433 /* Otherwise get the RHS of the statement and do the same processing
7434 as for the LHS above. */
ac43e11e
AC
7435 else
7436 gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr));
a1ab4c31 7437
815b5368 7438 gigi_checking_assert (!Do_Range_Check (gnat_expr));
a1ab4c31 7439
17a98a3d 7440 get_atomic_access (Name (gnat_node), &aa_type, &aa_sync);
544d14e1 7441 get_storage_model_access (Name (gnat_node), &gnat_smo);
17a98a3d 7442
f797c2b7
EB
7443 /* If an outer atomic access is required on the LHS, build the load-
7444 modify-store sequence. */
17a98a3d 7445 if (aa_type == OUTER_ATOMIC)
f797c2b7
EB
7446 gnu_result = build_load_modify_store (gnu_lhs, gnu_rhs, gnat_node);
7447
17a98a3d
EB
7448 /* Or else, if a simple atomic access is required, build the atomic
7449 store. */
7450 else if (aa_type == SIMPLE_ATOMIC)
7451 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs, aa_sync);
ac43e11e 7452
544d14e1
EB
7453 /* Or else, if a storage model access is required, build the special
7454 store. */
7455 else if (Present (gnat_smo)
7456 && Present (Storage_Model_Copy_To (gnat_smo)))
7457 {
7f77aa6b
EB
7458 /* We obviously cannot use memset in this case. */
7459 gcc_assert (!use_memset_p);
7460
544d14e1
EB
7461 tree t = remove_conversions (gnu_rhs, false);
7462
7463 /* If a storage model load is present on the RHS then instantiate
7464 the temporary associated with it now, lest it be of variable
7465 size and thus could not be instantiated by gimplification. */
7466 if (TREE_CODE (t) == LOAD_EXPR)
7467 {
7468 t = TREE_OPERAND (t, 1);
7469 gcc_assert (TREE_CODE (t) == CALL_EXPR);
7470
7471 tree elem
7472 = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
7473 tree size = fold_convert (sizetype, CALL_EXPR_ARG (t, 3));
7474 tree index = build_index_type (size);
7475 tree temp
7476 = create_temporary ("L", build_array_type (elem, index));
7477 tree arg = CALL_EXPR_ARG (t, 1);
7478 CALL_EXPR_ARG (t, 1)
7479 = build_unary_op (ADDR_EXPR, TREE_TYPE (arg), temp);
7480
7481 start_stmt_group ();
7482 add_stmt (t);
7483 t = build_storage_model_store (gnat_smo, gnu_lhs, temp);
7484 add_stmt (t);
7485 gnu_result = end_stmt_group ();
7486 }
7487
7488 else
7489 gnu_result
7490 = build_storage_model_store (gnat_smo, gnu_lhs, gnu_rhs);
7491 }
7492
df8aa2b7
EB
7493 /* Or else, use memset when the conditions are met. This has already
7494 been validated by Aggr_Assignment_OK_For_Backend in the front-end
7495 and the RHS is thus guaranteed to be of the appropriate form. */
ac43e11e
AC
7496 else if (use_memset_p)
7497 {
333e4f86
AC
7498 tree value
7499 = real_zerop (gnu_rhs)
7500 ? integer_zero_node
7501 : fold_convert (integer_type_node, gnu_rhs);
e54b6268 7502 tree dest = build_fold_addr_expr (gnu_lhs);
881cdd61 7503 tree t = builtin_decl_explicit (BUILT_IN_MEMSET);
e54b6268
EB
7504 /* Be extra careful not to write too much data. */
7505 tree size;
7506 if (TREE_CODE (gnu_lhs) == COMPONENT_REF)
7507 size = DECL_SIZE_UNIT (TREE_OPERAND (gnu_lhs, 1));
7508 else if (DECL_P (gnu_lhs))
7509 size = DECL_SIZE_UNIT (gnu_lhs);
7510 else
7511 size = TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs));
7512 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_lhs);
333e4f86 7513 if (TREE_CODE (value) == INTEGER_CST && !integer_zerop (value))
ac43e11e
AC
7514 {
7515 tree mask
7516 = build_int_cst (integer_type_node,
7517 ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
7518 value = int_const_binop (BIT_AND_EXPR, value, mask);
7519 }
e54b6268 7520 gnu_result = build_call_expr (t, 3, dest, value, size);
1f3f64b9
EB
7521 Check_Restriction_No_Dependence_On_System (Name_Memory_Set,
7522 gnat_node);
ac43e11e
AC
7523 }
7524
033ba5bf 7525 else
544d14e1
EB
7526 {
7527 tree t = remove_conversions (gnu_rhs, false);
7528
7529 /* If a storage model load is present on the RHS, then elide the
7530 temporary associated with it. */
7531 if (TREE_CODE (t) == LOAD_EXPR)
7532 {
7533 gnu_result = TREE_OPERAND (t, 1);
7534 gcc_assert (TREE_CODE (gnu_result) == CALL_EXPR);
7535
7536 tree arg = CALL_EXPR_ARG (gnu_result, 1);
7537 CALL_EXPR_ARG (gnu_result, 1)
7538 = build_unary_op (ADDR_EXPR, TREE_TYPE (arg), gnu_lhs);
7539 }
7540
7541 /* Otherwise build a regular assignment. */
7542 else
7543 gnu_result
7544 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
7545 }
8b659f79 7546
ac43e11e 7547 /* If the assignment type is a regular array and the two sides are
82d6f532
EB
7548 not completely disjoint, play safe and use memmove. But don't do
7549 it for a bit-packed array as it might not be byte-aligned. */
8b659f79 7550 if (TREE_CODE (gnu_result) == MODIFY_EXPR
95c9c1c0
EB
7551 && Is_Array_Type (gnat_type)
7552 && !Is_Bit_Packed_Array (gnat_type)
8b659f79
EB
7553 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
7554 {
ac43e11e
AC
7555 tree to = TREE_OPERAND (gnu_result, 0);
7556 tree from = TREE_OPERAND (gnu_result, 1);
7557 tree type = TREE_TYPE (from);
7558 tree size
7559 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from);
7560 tree to_ptr = build_fold_addr_expr (to);
7561 tree from_ptr = build_fold_addr_expr (from);
881cdd61 7562 tree t = builtin_decl_explicit (BUILT_IN_MEMMOVE);
8b659f79 7563 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
1f3f64b9
EB
7564 Check_Restriction_No_Dependence_On_System (Name_Memory_Move,
7565 gnat_node);
8b659f79 7566 }
1f3f64b9
EB
7567
7568 /* If this is an assignment between (potentially) large aggregates,
7569 then declare the dependence on the memcpy routine. */
7570 else if (AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs))
7571 && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs)))
7572 || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs)),
7573 2 * BITS_PER_WORD) > 0))
7574 Check_Restriction_No_Dependence_On_System (Name_Memory_Copy,
7575 gnat_node);
a1ab4c31
AC
7576 }
7577 break;
7578
7579 case N_If_Statement:
7580 {
1e17ef87 7581 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
a1ab4c31
AC
7582
7583 /* Make the outer COND_EXPR. Avoid non-determinism. */
7584 gnu_result = build3 (COND_EXPR, void_type_node,
7585 gnat_to_gnu (Condition (gnat_node)),
7586 NULL_TREE, NULL_TREE);
7587 COND_EXPR_THEN (gnu_result)
7588 = build_stmt_group (Then_Statements (gnat_node), false);
7589 TREE_SIDE_EFFECTS (gnu_result) = 1;
7590 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
7591
7592 /* Now make a COND_EXPR for each of the "else if" parts. Put each
7593 into the previous "else" part and point to where to put any
7594 outer "else". Also avoid non-determinism. */
7595 if (Present (Elsif_Parts (gnat_node)))
7596 for (gnat_temp = First (Elsif_Parts (gnat_node));
7597 Present (gnat_temp); gnat_temp = Next (gnat_temp))
7598 {
7599 gnu_expr = build3 (COND_EXPR, void_type_node,
7600 gnat_to_gnu (Condition (gnat_temp)),
7601 NULL_TREE, NULL_TREE);
7602 COND_EXPR_THEN (gnu_expr)
7603 = build_stmt_group (Then_Statements (gnat_temp), false);
7604 TREE_SIDE_EFFECTS (gnu_expr) = 1;
7605 set_expr_location_from_node (gnu_expr, gnat_temp);
7606 *gnu_else_ptr = gnu_expr;
7607 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
7608 }
7609
7610 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
7611 }
7612 break;
7613
7614 case N_Case_Statement:
7615 gnu_result = Case_Statement_to_gnu (gnat_node);
7616 break;
7617
7618 case N_Loop_Statement:
7619 gnu_result = Loop_Statement_to_gnu (gnat_node);
7620 break;
7621
7622 case N_Block_Statement:
a712b009
EB
7623 /* The only way to enter the block is to fall through to it. */
7624 if (stmt_group_may_fallthru ())
7625 {
7626 start_stmt_group ();
7627 gnat_pushlevel ();
6d16658d 7628 process_decls (Declarations (gnat_node), Empty, true, true);
a712b009 7629 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6d16658d
BD
7630 if (Present (At_End_Proc (gnat_node)))
7631 At_End_Proc_to_gnu (gnat_node);
a712b009
EB
7632 gnat_poplevel ();
7633 gnu_result = end_stmt_group ();
7634 }
7635 else
7636 gnu_result = alloc_stmt_list ();
a1ab4c31
AC
7637 break;
7638
7639 case N_Exit_Statement:
7640 gnu_result
7641 = build2 (EXIT_STMT, void_type_node,
7642 (Present (Condition (gnat_node))
7643 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
7644 (Present (Name (gnat_node))
7645 ? get_gnu_tree (Entity (Name (gnat_node)))
633a3f2a 7646 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
a1ab4c31
AC
7647 break;
7648
7640ef8a 7649 case N_Simple_Return_Statement:
a1ab4c31 7650 {
f3d34576 7651 tree gnu_ret_obj, gnu_ret_val;
a1ab4c31 7652
d47d0a8d
EB
7653 /* If the subprogram is a function, we must return the expression. */
7654 if (Present (Expression (gnat_node)))
a1ab4c31 7655 {
d47d0a8d 7656 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
d47d0a8d 7657
2374257a
EB
7658 /* If this function has copy-in/copy-out parameters parameters and
7659 doesn't return by invisible reference, get the real object for
7660 the return. See Subprogram_Body_to_gnu. */
7661 if (TYPE_CI_CO_LIST (gnu_subprog_type)
7662 && !TREE_ADDRESSABLE (gnu_subprog_type))
9771b263 7663 gnu_ret_obj = gnu_return_var_stack->last ();
f3d34576
EB
7664 else
7665 gnu_ret_obj = DECL_RESULT (current_function_decl);
7666
7667 /* Get the GCC tree for the expression to be returned. */
7668 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
35a382b8 7669
d47d0a8d
EB
7670 /* Do not remove the padding from GNU_RET_VAL if the inner type is
7671 self-referential since we want to allocate the fixed size. */
7672 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
fc7a823e 7673 && type_is_padding_self_referential
d020df06 7674 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0))))
d47d0a8d
EB
7675 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
7676
f3d34576 7677 /* If the function returns by direct reference, return a pointer
89e037d0
EB
7678 to the return value, possibly after allocating it. */
7679 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
a1ab4c31 7680 {
89e037d0 7681 if (Present (Storage_Pool (gnat_node)))
088b91c7 7682 {
89e037d0
EB
7683 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
7684
7685 /* And find out whether it is a candidate for Named Return
fc84bb8c 7686 Value. If so, record it. */
89e037d0
EB
7687 if (optimize
7688 && !optimize_debug
7689 && !TYPE_CI_CO_LIST (gnu_subprog_type))
088b91c7 7690 {
89e037d0
EB
7691 tree ret_val = gnu_ret_val;
7692
7693 /* Strip conversions around the return value. */
7694 if (gnat_useless_type_conversion (ret_val))
7695 ret_val = TREE_OPERAND (ret_val, 0);
7696
7697 /* Strip unpadding around the return value. */
7698 if (TREE_CODE (ret_val) == COMPONENT_REF
7699 && TYPE_IS_PADDING_P
7700 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
7701 ret_val = TREE_OPERAND (ret_val, 0);
7702
7703 /* Now apply the test to the return value. */
7704 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
7705 {
7706 if (!f_named_ret_val)
7707 f_named_ret_val = BITMAP_GGC_ALLOC ();
7708 bitmap_set_bit (f_named_ret_val,
7709 DECL_UID (ret_val));
7710 if (!f_gnat_ret)
7711 f_gnat_ret = gnat_node;
7712 }
088b91c7 7713 }
89e037d0
EB
7714
7715 gnu_ret_val
7716 = build_allocator (TREE_TYPE (gnu_ret_val),
7717 gnu_ret_val,
7718 TREE_TYPE (gnu_ret_obj),
7719 Procedure_To_Call (gnat_node),
7720 Storage_Pool (gnat_node),
7721 gnat_node,
7722 false);
088b91c7
EB
7723 }
7724
89e037d0
EB
7725 else
7726 gnu_ret_val
7727 = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
a1ab4c31 7728 }
d47d0a8d 7729
0d24bf76 7730 /* Otherwise, if it returns by invisible reference, dereference
d47d0a8d
EB
7731 the pointer it is passed using the type of the return value
7732 and build the copy operation manually. This ensures that we
7733 don't copy too much data, for example if the return type is
7734 unconstrained with a maximum size. */
0d24bf76 7735 else if (TREE_ADDRESSABLE (gnu_subprog_type))
a1ab4c31 7736 {
f3d34576 7737 tree gnu_ret_deref
d47d0a8d 7738 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
f3d34576 7739 gnu_ret_obj);
b24f7345 7740 gnu_result = build2 (INIT_EXPR, void_type_node,
2374257a 7741 gnu_ret_deref, gnu_ret_val);
c697f593
EB
7742 /* Avoid a useless copy with __builtin_return_slot. */
7743 if (TREE_CODE (gnu_ret_val) == INDIRECT_REF)
7744 gnu_result
7745 = build3 (COND_EXPR, void_type_node,
7746 fold_build2 (NE_EXPR, boolean_type_node,
7747 TREE_OPERAND (gnu_ret_val, 0),
7748 gnu_ret_obj),
7749 gnu_result, NULL_TREE);
d47d0a8d
EB
7750 add_stmt_with_node (gnu_result, gnat_node);
7751 gnu_ret_val = NULL_TREE;
a1ab4c31
AC
7752 }
7753 }
0d24bf76 7754
a1ab4c31 7755 else
0d24bf76 7756 gnu_ret_obj = gnu_ret_val = NULL_TREE;
a1ab4c31 7757
35a382b8
EB
7758 /* If we have a return label defined, convert this into a branch to
7759 that label. The return proper will be handled elsewhere. */
9771b263 7760 if (gnu_return_label_stack->last ())
35a382b8 7761 {
2374257a 7762 if (gnu_ret_val)
b1cd7461
EB
7763 add_stmt_with_node (build_binary_op (MODIFY_EXPR,
7764 NULL_TREE, gnu_ret_obj,
7765 gnu_ret_val),
7766 gnat_node);
35a382b8
EB
7767
7768 gnu_result = build1 (GOTO_EXPR, void_type_node,
9771b263 7769 gnu_return_label_stack->last ());
f3d34576 7770
35a382b8
EB
7771 /* When not optimizing, make sure the return is preserved. */
7772 if (!optimize && Comes_From_Source (gnat_node))
9771b263 7773 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
35a382b8
EB
7774 }
7775
f3d34576
EB
7776 /* Otherwise, build a regular return. */
7777 else
7778 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
a1ab4c31
AC
7779 }
7780 break;
7781
7782 case N_Goto_Statement:
8f8f531f
PMR
7783 gnu_expr = gnat_to_gnu (Name (gnat_node));
7784 gnu_result = build1 (GOTO_EXPR, void_type_node, gnu_expr);
7785 TREE_USED (gnu_expr) = 1;
a1ab4c31
AC
7786 break;
7787
1e17ef87
EB
7788 /***************************/
7789 /* Chapter 6: Subprograms */
7790 /***************************/
a1ab4c31
AC
7791
7792 case N_Subprogram_Declaration:
d5ebeb8c
EB
7793 /* Unless there is a freeze node, declare the entity. We consider
7794 this a definition even though we're not generating code for the
7795 subprogram because we will be making the corresponding GCC node.
7796 When there is a freeze node, it is considered the definition of
7797 the subprogram and we do nothing until after it is encountered.
7798 That's an efficiency issue: the types involved in the profile
7799 are far more likely to be frozen between the declaration and
7800 the freeze node than before the declaration, so we save some
7801 updates of the GCC node by waiting until the freeze node.
7802 The counterpart is that we assume that there is no reference
7803 to the subprogram between the declaration and the freeze node
7804 in the expanded code; otherwise, it will be interpreted as an
7805 external reference and very likely give rise to a link failure. */
a1ab4c31
AC
7806 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
7807 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
afc737f0 7808 NULL_TREE, true);
a1ab4c31
AC
7809 gnu_result = alloc_stmt_list ();
7810 break;
7811
7812 case N_Abstract_Subprogram_Declaration:
7813 /* This subprogram doesn't exist for code generation purposes, but we
7814 have to elaborate the types of any parameters and result, unless
76e3504f 7815 they are imported types (nothing to generate in this case).
a1ab4c31 7816
76e3504f
AC
7817 The parameter list may contain types with freeze nodes, e.g. not null
7818 subtypes, so the subprogram itself may carry a freeze node, in which
7819 case its elaboration must be deferred. */
a1ab4c31 7820
76e3504f
AC
7821 /* Process the parameter types first. */
7822 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
a1ab4c31
AC
7823 for (gnat_temp
7824 = First_Formal_With_Extras
7825 (Defining_Entity (Specification (gnat_node)));
7826 Present (gnat_temp);
7827 gnat_temp = Next_Formal_With_Extras (gnat_temp))
7828 if (Is_Itype (Etype (gnat_temp))
7b56a91b 7829 && !From_Limited_With (Etype (gnat_temp)))
afc737f0 7830 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
a1ab4c31 7831
a1ab4c31 7832 /* Then the result type, set to Standard_Void_Type for procedures. */
a1ab4c31
AC
7833 {
7834 Entity_Id gnat_temp_type
7835 = Etype (Defining_Entity (Specification (gnat_node)));
7836
7b56a91b 7837 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
afc737f0 7838 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, false);
a1ab4c31
AC
7839 }
7840
7841 gnu_result = alloc_stmt_list ();
7842 break;
7843
7844 case N_Defining_Program_Unit_Name:
1e17ef87
EB
7845 /* For a child unit identifier go up a level to get the specification.
7846 We get this when we try to find the spec of a child unit package
7847 that is the compilation unit being compiled. */
a1ab4c31
AC
7848 gnu_result = gnat_to_gnu (Parent (gnat_node));
7849 break;
7850
7851 case N_Subprogram_Body:
7852 Subprogram_Body_to_gnu (gnat_node);
7853 gnu_result = alloc_stmt_list ();
7854 break;
7855
7856 case N_Function_Call:
7857 case N_Procedure_Call_Statement:
f797c2b7 7858 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE,
544d14e1 7859 NOT_ATOMIC, false, Empty);
a1ab4c31
AC
7860 break;
7861
1e17ef87
EB
7862 /************************/
7863 /* Chapter 7: Packages */
7864 /************************/
a1ab4c31
AC
7865
7866 case N_Package_Declaration:
7867 gnu_result = gnat_to_gnu (Specification (gnat_node));
7868 break;
7869
7870 case N_Package_Specification:
a1ab4c31
AC
7871 start_stmt_group ();
7872 process_decls (Visible_Declarations (gnat_node),
6d16658d
BD
7873 Private_Declarations (gnat_node),
7874 true, true);
a1ab4c31
AC
7875 gnu_result = end_stmt_group ();
7876 break;
7877
7878 case N_Package_Body:
1e17ef87 7879 /* If this is the body of a generic package - do nothing. */
a1ab4c31
AC
7880 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
7881 {
7882 gnu_result = alloc_stmt_list ();
7883 break;
7884 }
7885
7886 start_stmt_group ();
6d16658d 7887 process_decls (Declarations (gnat_node), Empty, true, true);
a1ab4c31
AC
7888 if (Present (Handled_Statement_Sequence (gnat_node)))
7889 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6d16658d
BD
7890 if (Present (At_End_Proc (gnat_node)))
7891 At_End_Proc_to_gnu (gnat_node);
a1ab4c31
AC
7892 gnu_result = end_stmt_group ();
7893 break;
7894
1e17ef87
EB
7895 /********************************/
7896 /* Chapter 8: Visibility Rules */
7897 /********************************/
a1ab4c31
AC
7898
7899 case N_Use_Package_Clause:
7900 case N_Use_Type_Clause:
1e17ef87 7901 /* Nothing to do here - but these may appear in list of declarations. */
a1ab4c31
AC
7902 gnu_result = alloc_stmt_list ();
7903 break;
7904
1e17ef87
EB
7905 /*********************/
7906 /* Chapter 9: Tasks */
7907 /*********************/
a1ab4c31
AC
7908
7909 case N_Protected_Type_Declaration:
7910 gnu_result = alloc_stmt_list ();
7911 break;
7912
7913 case N_Single_Task_Declaration:
afc737f0 7914 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true);
a1ab4c31
AC
7915 gnu_result = alloc_stmt_list ();
7916 break;
7917
1e17ef87
EB
7918 /*********************************************************/
7919 /* Chapter 10: Program Structure and Compilation Issues */
7920 /*********************************************************/
a1ab4c31
AC
7921
7922 case N_Compilation_Unit:
a09d56d8 7923 /* This is not called for the main unit on which gigi is invoked. */
a1ab4c31
AC
7924 Compilation_Unit_to_gnu (gnat_node);
7925 gnu_result = alloc_stmt_list ();
7926 break;
7927
c4833de1
EB
7928 case N_Subunit:
7929 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
7930 break;
7931
7932 case N_Entry_Body:
7933 case N_Protected_Body:
7934 case N_Task_Body:
7935 /* These nodes should only be present when annotating types. */
7936 gcc_assert (type_annotate_only);
6d16658d 7937 process_decls (Declarations (gnat_node), Empty, true, true);
c4833de1
EB
7938 gnu_result = alloc_stmt_list ();
7939 break;
7940
a1ab4c31
AC
7941 case N_Subprogram_Body_Stub:
7942 case N_Package_Body_Stub:
7943 case N_Protected_Body_Stub:
7944 case N_Task_Body_Stub:
7945 /* Simply process whatever unit is being inserted. */
4e568a15
EB
7946 if (Present (Library_Unit (gnat_node)))
7947 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
7948 else
7949 {
7950 gcc_assert (type_annotate_only);
7951 gnu_result = alloc_stmt_list ();
7952 }
a1ab4c31
AC
7953 break;
7954
a1ab4c31 7955 /***************************/
1e17ef87 7956 /* Chapter 11: Exceptions */
a1ab4c31
AC
7957 /***************************/
7958
7959 case N_Handled_Sequence_Of_Statements:
a1ab4c31
AC
7960 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
7961 break;
7962
7963 case N_Exception_Handler:
e08f1aad 7964 gnu_result = Exception_Handler_to_gnu (gnat_node);
624e1688
AC
7965 break;
7966
7967 case N_Raise_Statement:
7968 /* Only for reraise in back-end exceptions mode. */
ce19ac12 7969 gcc_assert (No (Name (gnat_node)));
624e1688
AC
7970
7971 start_stmt_group ();
a1ab4c31 7972
5d733372
AO
7973 add_stmt_with_node (build_call_n_expr (reraise_zcx_decl, 1,
7974 gnu_incoming_exc_ptr),
7975 gnat_node);
624e1688 7976
624e1688 7977 gnu_result = end_stmt_group ();
a1ab4c31
AC
7978 break;
7979
7980 case N_Push_Constraint_Error_Label:
8f8f531f 7981 gnu_constraint_error_label_stack.safe_push (Exception_Label (gnat_node));
a1ab4c31
AC
7982 break;
7983
7984 case N_Push_Storage_Error_Label:
8f8f531f 7985 gnu_storage_error_label_stack.safe_push (Exception_Label (gnat_node));
a1ab4c31
AC
7986 break;
7987
7988 case N_Push_Program_Error_Label:
8f8f531f 7989 gnu_program_error_label_stack.safe_push (Exception_Label (gnat_node));
a1ab4c31
AC
7990 break;
7991
7992 case N_Pop_Constraint_Error_Label:
8f8f531f
PMR
7993 gnat_temp = gnu_constraint_error_label_stack.pop ();
7994 if (Present (gnat_temp)
4a0d6b70
EB
7995 && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))
7996 && No_Exception_Propagation_Active ())
8f8f531f 7997 Warn_If_No_Local_Raise (gnat_temp);
a1ab4c31
AC
7998 break;
7999
8000 case N_Pop_Storage_Error_Label:
8f8f531f
PMR
8001 gnat_temp = gnu_storage_error_label_stack.pop ();
8002 if (Present (gnat_temp)
4a0d6b70
EB
8003 && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))
8004 && No_Exception_Propagation_Active ())
8f8f531f 8005 Warn_If_No_Local_Raise (gnat_temp);
a1ab4c31
AC
8006 break;
8007
8008 case N_Pop_Program_Error_Label:
8f8f531f
PMR
8009 gnat_temp = gnu_program_error_label_stack.pop ();
8010 if (Present (gnat_temp)
4a0d6b70
EB
8011 && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false))
8012 && No_Exception_Propagation_Active ())
8f8f531f 8013 Warn_If_No_Local_Raise (gnat_temp);
a1ab4c31
AC
8014 break;
8015
1e17ef87
EB
8016 /******************************/
8017 /* Chapter 12: Generic Units */
8018 /******************************/
a1ab4c31
AC
8019
8020 case N_Generic_Function_Renaming_Declaration:
8021 case N_Generic_Package_Renaming_Declaration:
8022 case N_Generic_Procedure_Renaming_Declaration:
8023 case N_Generic_Package_Declaration:
8024 case N_Generic_Subprogram_Declaration:
8025 case N_Package_Instantiation:
8026 case N_Procedure_Instantiation:
8027 case N_Function_Instantiation:
8028 /* These nodes can appear on a declaration list but there is nothing to
8029 to be done with them. */
8030 gnu_result = alloc_stmt_list ();
8031 break;
8032
1e17ef87
EB
8033 /**************************************************/
8034 /* Chapter 13: Representation Clauses and */
8035 /* Implementation-Dependent Features */
8036 /**************************************************/
a1ab4c31
AC
8037
8038 case N_Attribute_Definition_Clause:
a1ab4c31
AC
8039 gnu_result = alloc_stmt_list ();
8040
8df2e902
EB
8041 /* The only one we need to deal with is 'Address since, for the others,
8042 the front-end puts the information elsewhere. */
8043 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
8044 break;
8045
8046 /* And we only deal with 'Address if the object has a Freeze node. */
8047 gnat_temp = Entity (Name (gnat_node));
3b9d1594
EB
8048 if (Freeze_Node (gnat_temp))
8049 {
d587d1e4 8050 tree gnu_address = gnat_to_gnu (Expression (gnat_node)), gnu_temp;
3b9d1594
EB
8051
8052 /* Get the value to use as the address and save it as the equivalent
8053 for the object; when it is frozen, gnat_to_gnu_entity will do the
8054 right thing. For a subprogram, put the naked address but build a
8055 meaningfull expression for an object in case its address is taken
8056 before the Freeze node is encountered; this can happen if the type
8057 of the object is limited and it is initialized with the result of
8058 a function call. */
8059 if (Is_Subprogram (gnat_temp))
d587d1e4 8060 gnu_temp = gnu_address;
3b9d1594
EB
8061 else
8062 {
8063 tree gnu_type = gnat_to_gnu_type (Etype (gnat_temp));
8064 /* Drop atomic and volatile qualifiers for the expression. */
8065 gnu_type = TYPE_MAIN_VARIANT (gnu_type);
8066 gnu_type
8067 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
8068 gnu_address = convert (gnu_type, gnu_address);
d587d1e4 8069 gnu_temp
3b9d1594
EB
8070 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_address);
8071 }
a1ab4c31 8072
d587d1e4 8073 save_gnu_tree (gnat_temp, gnu_temp, true);
3b9d1594 8074 }
a1ab4c31
AC
8075 break;
8076
8077 case N_Enumeration_Representation_Clause:
8078 case N_Record_Representation_Clause:
8079 case N_At_Clause:
8080 /* We do nothing with these. SEM puts the information elsewhere. */
8081 gnu_result = alloc_stmt_list ();
8082 break;
8083
8084 case N_Code_Statement:
8085 if (!type_annotate_only)
8086 {
8087 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
8088 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
8089 tree gnu_clobbers = NULL_TREE, tail;
8090 bool allows_mem, allows_reg, fake;
8091 int ninputs, noutputs, i;
8092 const char **oconstraints;
8093 const char *constraint;
8094 char *clobber;
8095
8096 /* First retrieve the 3 operand lists built by the front-end. */
8097 Setup_Asm_Outputs (gnat_node);
8098 while (Present (gnat_temp = Asm_Output_Variable ()))
8099 {
8100 tree gnu_value = gnat_to_gnu (gnat_temp);
8101 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
8102 (Asm_Output_Constraint ()));
8103
8104 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
8105 Next_Asm_Output ();
8106 }
8107
8108 Setup_Asm_Inputs (gnat_node);
8109 while (Present (gnat_temp = Asm_Input_Value ()))
8110 {
8111 tree gnu_value = gnat_to_gnu (gnat_temp);
8112 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
8113 (Asm_Input_Constraint ()));
8114
8115 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
8116 Next_Asm_Input ();
8117 }
8118
8119 Clobber_Setup (gnat_node);
b0ba442b 8120 while ((clobber = (char *) Clobber_Get_Next ()))
a1ab4c31
AC
8121 gnu_clobbers
8122 = tree_cons (NULL_TREE,
8123 build_string (strlen (clobber) + 1, clobber),
8124 gnu_clobbers);
8125
1e17ef87 8126 /* Then perform some standard checking and processing on the
a1ab4c31
AC
8127 operands. In particular, mark them addressable if needed. */
8128 gnu_outputs = nreverse (gnu_outputs);
8129 noutputs = list_length (gnu_outputs);
8130 gnu_inputs = nreverse (gnu_inputs);
8131 ninputs = list_length (gnu_inputs);
2bb1fc26 8132 oconstraints = XALLOCAVEC (const char *, noutputs);
a1ab4c31
AC
8133
8134 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
8135 {
8136 tree output = TREE_VALUE (tail);
8137 constraint
8138 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8139 oconstraints[i] = constraint;
8140
8141 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8142 &allows_mem, &allows_reg, &fake))
8143 {
8144 /* If the operand is going to end up in memory,
8145 mark it addressable. Note that we don't test
8146 allows_mem like in the input case below; this
17a98a3d 8147 is modeled on the C front-end. */
7e4680c1
EB
8148 if (!allows_reg)
8149 {
722356ce 8150 output = remove_conversions (output, false);
7e4680c1
EB
8151 if (TREE_CODE (output) == CONST_DECL
8152 && DECL_CONST_CORRESPONDING_VAR (output))
8153 output = DECL_CONST_CORRESPONDING_VAR (output);
8154 if (!gnat_mark_addressable (output))
8155 output = error_mark_node;
8156 }
a1ab4c31
AC
8157 }
8158 else
8159 output = error_mark_node;
8160
8161 TREE_VALUE (tail) = output;
8162 }
8163
8164 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
8165 {
8166 tree input = TREE_VALUE (tail);
8167 constraint
8168 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8169
8170 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
8171 0, oconstraints,
8172 &allows_mem, &allows_reg))
8173 {
8174 /* If the operand is going to end up in memory,
8175 mark it addressable. */
7e4680c1
EB
8176 if (!allows_reg && allows_mem)
8177 {
722356ce 8178 input = remove_conversions (input, false);
7e4680c1
EB
8179 if (TREE_CODE (input) == CONST_DECL
8180 && DECL_CONST_CORRESPONDING_VAR (input))
8181 input = DECL_CONST_CORRESPONDING_VAR (input);
8182 if (!gnat_mark_addressable (input))
8183 input = error_mark_node;
8184 }
a1ab4c31
AC
8185 }
8186 else
8187 input = error_mark_node;
8188
8189 TREE_VALUE (tail) = input;
8190 }
8191
1c384bf1 8192 gnu_result = build5 (ASM_EXPR, void_type_node,
a1ab4c31 8193 gnu_template, gnu_outputs,
1c384bf1 8194 gnu_inputs, gnu_clobbers, NULL_TREE);
a1ab4c31
AC
8195 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
8196 }
8197 else
8198 gnu_result = alloc_stmt_list ();
8199
8200 break;
8201
1e17ef87
EB
8202 /****************/
8203 /* Added Nodes */
8204 /****************/
a1ab4c31 8205
a3a6d41f
PMR
8206 /* Markers are created by the ABE mechanism to capture information which
8207 is either unavailable of expensive to recompute. Markers do not have
8208 and runtime semantics, and should be ignored. */
90e491a7
PMR
8209
8210 case N_Call_Marker:
a3a6d41f 8211 case N_Variable_Reference_Marker:
90e491a7
PMR
8212 gnu_result = alloc_stmt_list ();
8213 break;
8214
17c168fe 8215 case N_Expression_With_Actions:
1a9ee222
OH
8216 /* This construct doesn't define a scope so we don't push a binding
8217 level around the statement list, but we wrap it in a SAVE_EXPR to
8218 protect it from unsharing. Elaborate the expression as part of the
8219 same statement group as the actions so that the type declaration
8220 gets inserted there as well. This ensures that the type elaboration
8221 code is issued past the actions computing values on which it might
8222 depend. */
1a9ee222
OH
8223 start_stmt_group ();
8224 add_stmt_list (Actions (gnat_node));
8225 gnu_expr = gnat_to_gnu (Expression (gnat_node));
8226 gnu_result = end_stmt_group ();
8227
17c168fe
EB
8228 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
8229 TREE_SIDE_EFFECTS (gnu_result) = 1;
1a9ee222 8230
17c168fe 8231 gnu_result
39ab2e8f 8232 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
e1860041 8233 gnu_result_type = get_unpadded_type (Etype (gnat_node));
17c168fe
EB
8234 break;
8235
a1ab4c31
AC
8236 case N_Freeze_Entity:
8237 start_stmt_group ();
8238 process_freeze_entity (gnat_node);
6d16658d 8239 process_decls (Actions (gnat_node), Empty, true, true);
a1ab4c31
AC
8240 gnu_result = end_stmt_group ();
8241 break;
8242
3cd4a210
AC
8243 case N_Freeze_Generic_Entity:
8244 gnu_result = alloc_stmt_list ();
8245 break;
8246
a1ab4c31
AC
8247 case N_Itype_Reference:
8248 if (!present_gnu_tree (Itype (gnat_node)))
8249 process_type (Itype (gnat_node));
a1ab4c31
AC
8250 gnu_result = alloc_stmt_list ();
8251 break;
8252
8253 case N_Free_Statement:
bb24f343
EB
8254 gnat_temp = Expression (gnat_node);
8255
a1ab4c31
AC
8256 if (!type_annotate_only)
8257 {
bb24f343
EB
8258 const Entity_Id gnat_desig_type
8259 = Designated_Type (Underlying_Type (Etype (gnat_temp)));
544d14e1
EB
8260 const Entity_Id gnat_pool = Storage_Pool (gnat_node);
8261 const bool pool_is_storage_model
8262 = Present (gnat_pool)
8263 && Has_Storage_Model_Type_Aspect (Etype (gnat_pool))
8264 && Present (Storage_Model_Copy_From (gnat_pool));
8265 tree gnu_ptr, gnu_ptr_type, gnu_obj_type, gnu_actual_obj_type;
bb24f343
EB
8266
8267 /* Make sure the designated type is complete before dereferencing,
8268 in case it is a Taft Amendment type. */
8269 (void) gnat_to_gnu_entity (gnat_desig_type, NULL_TREE, false);
8270
8271 gnu_ptr = gnat_to_gnu (gnat_temp);
8272 gnu_ptr_type = TREE_TYPE (gnu_ptr);
0d7de0e1
EB
8273
8274 /* If this is a thin pointer, we must first dereference it to create
8275 a fat pointer, then go back below to a thin pointer. The reason
8276 for this is that we need to have a fat pointer someplace in order
8277 to properly compute the size. */
315cff15 8278 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
a1ab4c31
AC
8279 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
8280 build_unary_op (INDIRECT_REF, NULL_TREE,
8281 gnu_ptr));
8282
0d7de0e1
EB
8283 /* If this is a fat pointer, the object must have been allocated with
8284 the template in front of the array. So pass the template address,
8285 and get the total size; do it by converting to a thin pointer. */
315cff15 8286 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
a1ab4c31
AC
8287 gnu_ptr
8288 = convert (build_pointer_type
8289 (TYPE_OBJECT_RECORD_TYPE
8290 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
8291 gnu_ptr);
8292
8293 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
8294
0d7de0e1
EB
8295 /* If this is a thin pointer, the object must have been allocated with
8296 the template in front of the array. So pass the template address,
8297 and get the total size. */
8298 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
8299 gnu_ptr
8300 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
8301 gnu_ptr,
2b45154d
EB
8302 fold_build1 (NEGATE_EXPR, sizetype,
8303 byte_position
8304 (DECL_CHAIN
8305 TYPE_FIELDS ((gnu_obj_type)))));
0d7de0e1
EB
8306
8307 /* If we have a special dynamic constrained subtype on the node, use
8308 it to compute the size; otherwise, use the designated subtype. */
a1ab4c31
AC
8309 if (Present (Actual_Designated_Subtype (gnat_node)))
8310 {
8311 gnu_actual_obj_type
1e17ef87 8312 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
a1ab4c31 8313
315cff15 8314 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
1e17ef87
EB
8315 gnu_actual_obj_type
8316 = build_unc_object_type_from_ptr (gnu_ptr_type,
8317 gnu_actual_obj_type,
928dfa4b
EB
8318 get_identifier ("DEALLOC"),
8319 false);
a1ab4c31
AC
8320 }
8321 else
8322 gnu_actual_obj_type = gnu_obj_type;
8323
62801a77
EB
8324 tree gnu_size = TYPE_SIZE_UNIT (gnu_actual_obj_type);
8325 gnu_size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, gnu_ptr);
544d14e1
EB
8326 if (pool_is_storage_model)
8327 gnu_size = INSTANTIATE_LOAD_IN_EXPR (gnu_size, gnat_pool);
62801a77 8328
ff346f70 8329 gnu_result
62801a77 8330 = build_call_alloc_dealloc (gnu_ptr, gnu_size, gnu_obj_type,
ff346f70 8331 Procedure_To_Call (gnat_node),
544d14e1 8332 gnat_pool, gnat_node);
a1ab4c31
AC
8333 }
8334 break;
8335
8336 case N_Raise_Constraint_Error:
8337 case N_Raise_Program_Error:
8338 case N_Raise_Storage_Error:
80096613
EB
8339 if (type_annotate_only)
8340 gnu_result = alloc_stmt_list ();
8341 else
8342 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
a1ab4c31
AC
8343 break;
8344
8345 case N_Validate_Unchecked_Conversion:
f04b8d69
EB
8346 /* The only validation we currently do on an unchecked conversion is
8347 that of aliasing assumptions. */
8348 if (flag_strict_aliasing)
9771b263 8349 gnat_validate_uc_list.safe_push (gnat_node);
a1ab4c31
AC
8350 gnu_result = alloc_stmt_list ();
8351 break;
8352
4e568a15
EB
8353 case N_Function_Specification:
8354 case N_Procedure_Specification:
8355 case N_Op_Concat:
8356 case N_Component_Association:
4e568a15
EB
8357 /* These nodes should only be present when annotating types. */
8358 gcc_assert (type_annotate_only);
a1ab4c31 8359 gnu_result = alloc_stmt_list ();
4e568a15
EB
8360 break;
8361
8362 default:
8363 /* Other nodes are not supposed to reach here. */
8364 gcc_unreachable ();
a1ab4c31
AC
8365 }
8366
f53aff92
EB
8367 /* If we are in the elaboration procedure, check if we are violating the
8368 No_Elaboration_Code restriction by having a non-empty statement. */
8369 if (statement_node_p (gnat_node)
8370 && !(TREE_CODE (gnu_result) == STATEMENT_LIST
8371 && empty_stmt_list_p (gnu_result))
8372 && current_function_decl == get_elaboration_procedure ())
8373 Check_Elaboration_Code_Allowed (gnat_node);
8374
a09d56d8 8375 /* If we pushed the processing of the elaboration routine, pop it back. */
a1ab4c31 8376 if (went_into_elab_proc)
a09d56d8 8377 current_function_decl = NULL_TREE;
a1ab4c31 8378
1fc24649 8379 /* When not optimizing, turn boolean rvalues B into B != false tests
f1ff07ec
EB
8380 so that we can put the location information of the reference to B on
8381 the inequality operator for better debug info. */
1fc24649 8382 if (!optimize
054d6b83 8383 && TREE_CODE (gnu_result) != INTEGER_CST
f1ff07ec 8384 && TREE_CODE (gnu_result) != TYPE_DECL
1fc24649
EB
8385 && (kind == N_Identifier
8386 || kind == N_Expanded_Name
8387 || kind == N_Explicit_Dereference
1fc24649
EB
8388 || kind == N_Indexed_Component
8389 || kind == N_Selected_Component)
8390 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
aff22074 8391 && Nkind (Parent (gnat_node)) != N_Attribute_Reference
bbdf2d39 8392 && Nkind (Parent (gnat_node)) != N_Pragma_Argument_Association
aff22074
EB
8393 && Nkind (Parent (gnat_node)) != N_Variant_Part
8394 && !lvalue_required_p (gnat_node, gnu_result_type, false, false))
f1ff07ec
EB
8395 {
8396 gnu_result
8397 = build_binary_op (NE_EXPR, gnu_result_type,
8398 convert (gnu_result_type, gnu_result),
8399 convert (gnu_result_type, boolean_false_node));
8400 if (TREE_CODE (gnu_result) != INTEGER_CST)
8401 set_gnu_expr_location_from_node (gnu_result, gnat_node);
8402 }
8403
5e017b1e
EB
8404 /* Set the location information on the result if it's not a simple name
8405 or something that contains a simple name, for example a tag, because
8406 we don"t want all the references to get the location of the first use.
f1ff07ec
EB
8407 Note that we may have no result if we tried to build a CALL_EXPR node
8408 to a procedure with no side-effects and optimization is enabled. */
5e017b1e
EB
8409 else if (kind != N_Identifier
8410 && !(kind == N_Selected_Component
8411 && Chars (Selector_Name (gnat_node)) == Name_uTag)
8412 && gnu_result
8413 && EXPR_P (gnu_result))
17c168fe 8414 set_gnu_expr_location_from_node (gnu_result, gnat_node);
a1ab4c31
AC
8415
8416 /* If we're supposed to return something of void_type, it means we have
8417 something we're elaborating for effect, so just return. */
8418 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
8419 return gnu_result;
8420
c1abd261
EB
8421 /* If the result is a constant that overflowed, raise Constraint_Error. */
8422 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
a1ab4c31 8423 {
57136d60 8424 post_error ("??Constraint_Error will be raised at run time", gnat_node);
a1ab4c31
AC
8425 gnu_result
8426 = build1 (NULL_EXPR, gnu_result_type,
8427 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
8428 N_Raise_Constraint_Error));
8429 }
8430
62801a77
EB
8431 /* If the result has side-effects and is of an unconstrained type, protect
8432 the expression in case it will be referenced multiple times, i.e. for
8433 its value and to compute the size of an object. But do it neither for
8434 an object nor a renaming declaration, nor a return statement of a call
8435 to a function that returns an unconstrained record type with default
8436 discriminant, because there is no size to be computed in these cases
8437 and this will create a useless temporary. We must do this before any
8438 conversions. */
a1ab4c31
AC
8439 if (TREE_SIDE_EFFECTS (gnu_result)
8440 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
62801a77
EB
8441 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
8442 && !(TREE_CODE (gnu_result) == CALL_EXPR
8443 && type_is_padding_self_referential (TREE_TYPE (gnu_result))
8444 && (Nkind (Parent (gnat_node)) == N_Object_Declaration
8445 || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration
8446 || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement)))
7194767c 8447 gnu_result = gnat_protect_expr (gnu_result);
a1ab4c31
AC
8448
8449 /* Now convert the result to the result type, unless we are in one of the
8450 following cases:
8451
27ab5bd8
EB
8452 1. If this is the LHS of an assignment or an actual parameter of a
8453 call, return the result almost unmodified since the RHS will have
8454 to be converted to our type in that case, unless the result type
5dce843f
EB
8455 has a simpler size or for array types because this size might be
8456 changed in-between. Likewise if there is just a no-op unchecked
27ab5bd8
EB
8457 conversion in-between. Similarly, don't convert integral types
8458 that are the operands of an unchecked conversion since we need
8459 to ignore those conversions (for 'Valid).
a1ab4c31
AC
8460
8461 2. If we have a label (which doesn't have any well-defined type), a
abbc8c7b
EB
8462 field or an error, return the result almost unmodified. Similarly,
8463 if the two types are record types with the same name, don't convert.
8464 This will be the case when we are converting from a packable version
8465 of a type to its original type and we need those conversions to be
8466 NOPs in order for assignments into these types to work properly.
a1ab4c31
AC
8467
8468 3. If the type is void or if we have no result, return error_mark_node
8469 to show we have no result.
8470
fc7a823e
EB
8471 4. If this is a call to a function that returns with variable size and
8472 the call is used as the expression in either an object or a renaming
8473 declaration, return the result unmodified because we want to use the
8474 return slot optimization in this case.
16934bbf 8475
ea588d41
EB
8476 5. If this is a reference to an unconstrained array which is used either
8477 as the prefix of an attribute reference that requires an lvalue or in
8478 a return statement, then return the result unmodified because we want
8479 to return the original bounds.
eb0f4e48
EB
8480
8481 6. Finally, if the type of the result is already correct. */
a1ab4c31
AC
8482
8483 if (Present (Parent (gnat_node))
27ab5bd8 8484 && (lhs_or_actual_p (gnat_node)
c2efda0d 8485 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
4f8a6678 8486 && unchecked_conversion_nop (Parent (gnat_node)))
a1ab4c31
AC
8487 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
8488 && !AGGREGATE_TYPE_P (gnu_result_type)
8489 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
8490 && !(TYPE_SIZE (gnu_result_type)
8491 && TYPE_SIZE (TREE_TYPE (gnu_result))
5dce843f
EB
8492 && AGGREGATE_TYPE_P (gnu_result_type)
8493 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))
a1ab4c31
AC
8494 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
8495 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
8496 != INTEGER_CST))
8497 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
8498 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
8499 && (CONTAINS_PLACEHOLDER_P
5dce843f
EB
8500 (TYPE_SIZE (TREE_TYPE (gnu_result)))))
8501 || (TREE_CODE (gnu_result_type) == ARRAY_TYPE
8502 && TREE_CODE (TREE_TYPE (gnu_result)) == ARRAY_TYPE))
a1ab4c31
AC
8503 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
8504 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
8505 {
8506 /* Remove padding only if the inner object is of self-referential
8507 size: in that case it must be an object of unconstrained type
8508 with a default discriminant and we want to avoid copying too
02221bed
EB
8509 much data. But do not remove it if it is already too small. */
8510 if (type_is_padding_self_referential (TREE_TYPE (gnu_result))
8511 && !(TREE_CODE (gnu_result) == COMPONENT_REF
1e66a917
EB
8512 && DECL_BIT_FIELD (TREE_OPERAND (gnu_result, 1))
8513 && DECL_SIZE (TREE_OPERAND (gnu_result, 1))
8514 != TYPE_SIZE (TREE_TYPE (gnu_result))))
a1ab4c31
AC
8515 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
8516 gnu_result);
8517 }
8518
8519 else if (TREE_CODE (gnu_result) == LABEL_DECL
8520 || TREE_CODE (gnu_result) == FIELD_DECL
8521 || TREE_CODE (gnu_result) == ERROR_MARK
abbc8c7b
EB
8522 || (TYPE_NAME (gnu_result_type)
8523 == TYPE_NAME (TREE_TYPE (gnu_result))
a1ab4c31
AC
8524 && TREE_CODE (gnu_result_type) == RECORD_TYPE
8525 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
8526 {
8527 /* Remove any padding. */
ad00a297 8528 gnu_result = maybe_padded_object (gnu_result);
a1ab4c31
AC
8529 }
8530
8531 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
8532 gnu_result = error_mark_node;
8533
eb0f4e48
EB
8534 else if (TREE_CODE (gnu_result) == CALL_EXPR
8535 && Present (Parent (gnat_node))
fc7a823e
EB
8536 && (Nkind (Parent (gnat_node)) == N_Object_Declaration
8537 || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration)
fc7a823e 8538 && return_type_with_variable_size_p (TREE_TYPE (gnu_result)))
842d4ee2 8539 ;
16934bbf 8540
544d14e1 8541 else if (TREE_CODE (TREE_TYPE (gnu_result)) == UNCONSTRAINED_ARRAY_TYPE
eb0f4e48 8542 && Present (Parent (gnat_node))
ea588d41
EB
8543 && ((Nkind (Parent (gnat_node)) == N_Attribute_Reference
8544 && lvalue_required_for_attribute_p (Parent (gnat_node)))
8545 || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement))
eb0f4e48
EB
8546 ;
8547
16934bbf 8548 else if (TREE_TYPE (gnu_result) != gnu_result_type)
a1ab4c31
AC
8549 gnu_result = convert (gnu_result_type, gnu_result);
8550
8551 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
8552 while ((TREE_CODE (gnu_result) == NOP_EXPR
8553 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
8554 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
8555 gnu_result = TREE_OPERAND (gnu_result, 0);
8556
8557 return gnu_result;
8558}
93e708f9
EB
8559
8560/* Similar to gnat_to_gnu, but discard any object that might be created in
8561 the course of the translation of GNAT_NODE, which must be an "external"
8562 expression in the sense that it will be elaborated elsewhere. */
8563
8564tree
8565gnat_to_gnu_external (Node_Id gnat_node)
8566{
8567 const int save_force_global = force_global;
f53aff92 8568 bool went_into_elab_proc;
93e708f9
EB
8569
8570 /* Force the local context and create a fake scope that we zap
8571 at the end so declarations will not be stuck either in the
8572 global varpool or in the current scope. */
8573 if (!current_function_decl)
8574 {
8575 current_function_decl = get_elaboration_procedure ();
8576 went_into_elab_proc = true;
8577 }
f53aff92
EB
8578 else
8579 went_into_elab_proc = false;
93e708f9
EB
8580 force_global = 0;
8581 gnat_pushlevel ();
8582
8583 tree gnu_result = gnat_to_gnu (gnat_node);
8584
8585 gnat_zaplevel ();
8586 force_global = save_force_global;
8587 if (went_into_elab_proc)
8588 current_function_decl = NULL_TREE;
8589
f1ff07ec
EB
8590 /* Do not import locations from external units. */
8591 if (gnu_result && EXPR_P (gnu_result))
8592 SET_EXPR_LOCATION (gnu_result, UNKNOWN_LOCATION);
8593
93e708f9
EB
8594 return gnu_result;
8595}
ce2d0ce2 8596
102a1631
EB
8597/* Return true if the statement list STMT_LIST is empty. */
8598
8599static bool
8600empty_stmt_list_p (tree stmt_list)
8601{
8602 tree_stmt_iterator tsi;
8603
8604 for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi))
8605 {
8606 tree stmt = tsi_stmt (tsi);
8607
8608 /* Anything else than an empty STMT_STMT counts as something. */
8609 if (TREE_CODE (stmt) != STMT_STMT || STMT_STMT_STMT (stmt))
8610 return false;
8611 }
8612
8613 return true;
8614}
8615
a1ab4c31
AC
8616/* Record the current code position in GNAT_NODE. */
8617
8618static void
8619record_code_position (Node_Id gnat_node)
8620{
8621 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
8622
8623 add_stmt_with_node (stmt_stmt, gnat_node);
8624 save_gnu_tree (gnat_node, stmt_stmt, true);
8625}
8626
8627/* Insert the code for GNAT_NODE at the position saved for that node. */
8628
8629static void
8630insert_code_for (Node_Id gnat_node)
8631{
102a1631
EB
8632 tree code = gnat_to_gnu (gnat_node);
8633
8634 /* It's too late to remove the STMT_STMT itself at this point. */
8635 if (!empty_stmt_list_p (code))
8636 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = code;
8637
a1ab4c31
AC
8638 save_gnu_tree (gnat_node, NULL_TREE, true);
8639}
ce2d0ce2 8640
a1ab4c31
AC
8641/* Start a new statement group chained to the previous group. */
8642
8643void
8644start_stmt_group (void)
8645{
8646 struct stmt_group *group = stmt_group_free_list;
8647
8648 /* First see if we can get one from the free list. */
8649 if (group)
8650 stmt_group_free_list = group->previous;
8651 else
766090c2 8652 group = ggc_alloc<stmt_group> ();
a1ab4c31
AC
8653
8654 group->previous = current_stmt_group;
8655 group->stmt_list = group->block = group->cleanups = NULL_TREE;
8656 current_stmt_group = group;
8657}
8658
586fea26
EB
8659/* Add GNU_STMT to the current statement group. If it is an expression with
8660 no effects, it is ignored. */
a1ab4c31
AC
8661
8662void
8663add_stmt (tree gnu_stmt)
8664{
8665 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
8666}
8667
586fea26
EB
8668/* Similar, but the statement is always added, regardless of side-effects. */
8669
8670void
8671add_stmt_force (tree gnu_stmt)
8672{
8673 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
8674}
8675
8676/* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
a1ab4c31
AC
8677
8678void
8679add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
8680{
56b8aa0c 8681 if (Present (gnat_node))
a1ab4c31
AC
8682 set_expr_location_from_node (gnu_stmt, gnat_node);
8683 add_stmt (gnu_stmt);
8684}
8685
586fea26
EB
8686/* Similar, but the statement is always added, regardless of side-effects. */
8687
8688void
8689add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
8690{
8691 if (Present (gnat_node))
8692 set_expr_location_from_node (gnu_stmt, gnat_node);
8693 add_stmt_force (gnu_stmt);
8694}
8695
a1ab4c31 8696/* Add a declaration statement for GNU_DECL to the current statement group.
56b8aa0c 8697 Get the SLOC to be put onto the statement from GNAT_NODE. */
a1ab4c31
AC
8698
8699void
56b8aa0c 8700add_decl_expr (tree gnu_decl, Node_Id gnat_node)
a1ab4c31
AC
8701{
8702 tree type = TREE_TYPE (gnu_decl);
7352723b 8703 tree gnu_stmt, gnu_init;
a1ab4c31
AC
8704
8705 /* If this is a variable that Gigi is to ignore, we may have been given
8706 an ERROR_MARK. So test for it. We also might have been given a
8707 reference for a renaming. So only do something for a decl. Also
8708 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
8709 if (!DECL_P (gnu_decl)
8710 || (TREE_CODE (gnu_decl) == TYPE_DECL
8711 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
8712 return;
8713
8714 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
8715
6ba4f08f
EB
8716 /* If we are external or global, we don't want to output the DECL_EXPR for
8717 this DECL node since we already have evaluated the expressions in the
a1ab4c31 8718 sizes and positions as globals and doing it again would be wrong. */
6ba4f08f 8719 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
a1ab4c31
AC
8720 {
8721 /* Mark everything as used to prevent node sharing with subprograms.
8722 Note that walk_tree knows how to deal with TYPE_DECL, but neither
8723 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
3f13dd77 8724 MARK_VISITED (gnu_stmt);
a1ab4c31
AC
8725 if (TREE_CODE (gnu_decl) == VAR_DECL
8726 || TREE_CODE (gnu_decl) == CONST_DECL)
8727 {
3f13dd77
EB
8728 MARK_VISITED (DECL_SIZE (gnu_decl));
8729 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
8730 MARK_VISITED (DECL_INITIAL (gnu_decl));
a1ab4c31 8731 }
a1ab4c31 8732 }
6ba4f08f 8733 else
56b8aa0c 8734 add_stmt_with_node (gnu_stmt, gnat_node);
a1ab4c31 8735
665e80ca
EB
8736 /* Mark our TYPE_ADA_SIZE field now since it will not be gimplified. */
8737 if (TREE_CODE (gnu_decl) == TYPE_DECL
8738 && RECORD_OR_UNION_TYPE_P (type)
8739 && !TYPE_FAT_POINTER_P (type))
8740 MARK_VISITED (TYPE_ADA_SIZE (type));
8741
1f3f64b9
EB
8742 if (TREE_CODE (gnu_decl) == VAR_DECL && (gnu_init = DECL_INITIAL (gnu_decl)))
8743 {
8744 /* If this is a variable and an initializer is attached to it, it must be
8745 valid for the context. Similar to init_const in create_var_decl. */
8746 if (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
a1ab4c31
AC
8747 || (TREE_STATIC (gnu_decl)
8748 && !initializer_constant_valid_p (gnu_init,
1f3f64b9 8749 TREE_TYPE (gnu_init))))
a1ab4c31 8750 {
1f3f64b9
EB
8751 DECL_INITIAL (gnu_decl) = NULL_TREE;
8752 if (TREE_READONLY (gnu_decl))
8753 {
8754 TREE_READONLY (gnu_decl) = 0;
8755 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
8756 }
8757
8758 /* Remove any padding so the assignment is done properly. */
8759 gnu_decl = maybe_padded_object (gnu_decl);
a1ab4c31 8760
1f3f64b9
EB
8761 gnu_stmt
8762 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_decl, gnu_init);
8763 add_stmt_with_node (gnu_stmt, gnat_node);
8764 }
7352723b 8765
1f3f64b9
EB
8766 /* If this is the initialization of a (potentially) large aggregate, then
8767 declare the dependence on the memcpy routine. */
8768 if (AGGREGATE_TYPE_P (type)
8769 && (!TREE_CONSTANT (TYPE_SIZE (type))
8770 || compare_tree_int (TYPE_SIZE (type), 2 * BITS_PER_WORD) > 0))
8771 Check_Restriction_No_Dependence_On_System (Name_Memory_Copy,
8772 gnat_node);
a1ab4c31
AC
8773 }
8774}
8775
8776/* Callback for walk_tree to mark the visited trees rooted at *TP. */
8777
8778static tree
8779mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
8780{
3f13dd77
EB
8781 tree t = *tp;
8782
8783 if (TREE_VISITED (t))
a1ab4c31
AC
8784 *walk_subtrees = 0;
8785
8786 /* Don't mark a dummy type as visited because we want to mark its sizes
8787 and fields once it's filled in. */
3f13dd77
EB
8788 else if (!TYPE_IS_DUMMY_P (t))
8789 TREE_VISITED (t) = 1;
a1ab4c31 8790
4ed9ab2d 8791 /* The test in gimplify_type_sizes is on the main variant. */
3f13dd77 8792 if (TYPE_P (t))
4ed9ab2d 8793 TYPE_SIZES_GIMPLIFIED (TYPE_MAIN_VARIANT (t)) = 1;
a1ab4c31
AC
8794
8795 return NULL_TREE;
8796}
8797
3f13dd77
EB
8798/* Mark nodes rooted at T with TREE_VISITED and types as having their
8799 sized gimplified. We use this to indicate all variable sizes and
8800 positions in global types may not be shared by any subprogram. */
8801
8802void
8803mark_visited (tree t)
8804{
8805 walk_tree (&t, mark_visited_r, NULL, NULL);
8806}
8807
a1ab4c31 8808/* Add GNU_CLEANUP, a cleanup action, to the current code group and
362db0b2
TQ
8809 set its location to that of GNAT_NODE if present, but with column info
8810 cleared so that conditional branches generated as part of the cleanup
8811 code do not interfere with coverage analysis tools. */
a1ab4c31
AC
8812
8813static void
8814add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
8815{
8816 if (Present (gnat_node))
ba464315 8817 set_expr_location_from_node (gnu_cleanup, gnat_node, true);
c900c700 8818
5d733372
AO
8819 /* An EH_ELSE_EXPR must be by itself, and that's all we need when we
8820 use it. The assert below makes sure that is so. Should we ever
8821 need more than that, we could combine EH_ELSE_EXPRs, and copy
8822 non-EH_ELSE_EXPR stmts into both cleanup paths of an
8823 EH_ELSE_EXPR. */
8824 if (TREE_CODE (gnu_cleanup) == EH_ELSE_EXPR)
8825 {
8826 gcc_assert (!current_stmt_group->cleanups);
8827 current_stmt_group->cleanups = gnu_cleanup;
8828 }
8829 else
8830 {
8831 gcc_assert (!current_stmt_group->cleanups
8832 || (TREE_CODE (current_stmt_group->cleanups)
8833 != EH_ELSE_EXPR));
8834 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
8835 }
a1ab4c31
AC
8836}
8837
8838/* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
8839
8840void
8841set_block_for_group (tree gnu_block)
8842{
8843 gcc_assert (!current_stmt_group->block);
8844 current_stmt_group->block = gnu_block;
8845}
8846
8847/* Return code corresponding to the current code group. It is normally
8848 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
8849 BLOCK or cleanups were set. */
8850
8851tree
8852end_stmt_group (void)
8853{
8854 struct stmt_group *group = current_stmt_group;
8855 tree gnu_retval = group->stmt_list;
8856
8857 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
8858 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
8859 make a BIND_EXPR. Note that we nest in that because the cleanup may
8860 reference variables in the block. */
7c775aca 8861 if (!gnu_retval)
a1ab4c31
AC
8862 gnu_retval = alloc_stmt_list ();
8863
8864 if (group->cleanups)
8865 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
8866 group->cleanups);
8867
8868 if (current_stmt_group->block)
8869 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
8870 gnu_retval, group->block);
8871
8872 /* Remove this group from the stack and add it to the free list. */
8873 current_stmt_group = group->previous;
8874 group->previous = stmt_group_free_list;
8875 stmt_group_free_list = group;
8876
8877 return gnu_retval;
8878}
8879
a712b009
EB
8880/* Return whether the current statement group may fall through. */
8881
8882static inline bool
8883stmt_group_may_fallthru (void)
8884{
8885 if (current_stmt_group->stmt_list)
8886 return block_may_fallthru (current_stmt_group->stmt_list);
8887 else
8888 return true;
8889}
8890
a1ab4c31
AC
8891/* Add a list of statements from GNAT_LIST, a possibly-empty list of
8892 statements.*/
8893
8894static void
8895add_stmt_list (List_Id gnat_list)
8896{
8897 Node_Id gnat_node;
8898
8899 if (Present (gnat_list))
8900 for (gnat_node = First (gnat_list); Present (gnat_node);
8901 gnat_node = Next (gnat_node))
8902 add_stmt (gnat_to_gnu (gnat_node));
8903}
8904
8905/* Build a tree from GNAT_LIST, a possibly-empty list of statements.
8906 If BINDING_P is true, push and pop a binding level around the list. */
8907
8908static tree
8909build_stmt_group (List_Id gnat_list, bool binding_p)
8910{
8911 start_stmt_group ();
547bbe49 8912
a1ab4c31
AC
8913 if (binding_p)
8914 gnat_pushlevel ();
8915
8916 add_stmt_list (gnat_list);
547bbe49 8917
a1ab4c31
AC
8918 if (binding_p)
8919 gnat_poplevel ();
8920
8921 return end_stmt_group ();
8922}
ce2d0ce2 8923
a1ab4c31
AC
8924/* Generate GIMPLE in place for the expression at *EXPR_P. */
8925
8926int
8927gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
8928 gimple_seq *post_p ATTRIBUTE_UNUSED)
8929{
8930 tree expr = *expr_p;
5e0f1fca 8931 tree type = TREE_TYPE (expr);
a1ab4c31
AC
8932 tree op;
8933
8934 if (IS_ADA_STMT (expr))
8935 return gnat_gimplify_stmt (expr_p);
8936
8937 switch (TREE_CODE (expr))
8938 {
a1ab4c31
AC
8939 case ADDR_EXPR:
8940 op = TREE_OPERAND (expr, 0);
8941
bb021771
EB
8942 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
8943 is put into static memory. We know that it's going to be read-only
8944 given the semantics we have and it must be in static memory when the
8945 reference is in an elaboration procedure. */
8946 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
a1ab4c31 8947 {
bb021771 8948 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
5e0f1fca 8949 *expr_p = fold_convert (type, addr);
bb021771
EB
8950 return GS_ALL_DONE;
8951 }
cb3d597d 8952
241125b2
EB
8953 /* Replace atomic loads with their first argument. That's necessary
8954 because the gimplifier would create a temporary otherwise. */
8955 if (TREE_SIDE_EFFECTS (op))
8956 while (handled_component_p (op) || CONVERT_EXPR_P (op))
8957 {
8958 tree inner = TREE_OPERAND (op, 0);
8959 if (TREE_CODE (inner) == CALL_EXPR && call_is_atomic_load (inner))
8960 {
8961 tree t = CALL_EXPR_ARG (inner, 0);
8962 if (TREE_CODE (t) == NOP_EXPR)
8963 t = TREE_OPERAND (t, 0);
8964 if (TREE_CODE (t) == ADDR_EXPR)
8965 TREE_OPERAND (op, 0) = TREE_OPERAND (t, 0);
8966 else
8967 TREE_OPERAND (op, 0) = build_fold_indirect_ref (t);
8968 }
8969 else
8970 op = inner;
8971 }
9ae38416 8972 break;
456976d8 8973
fccc47dd
EB
8974 case CALL_EXPR:
8975 /* If we are passing a constant fat pointer CONSTRUCTOR, make sure it is
8976 put into static memory; this performs a restricted version of constant
8977 propagation on fat pointers in calls. But do not do it for strings to
8978 avoid blocking concatenation in the caller when it is inlined. */
8979 for (int i = 0; i < call_expr_nargs (expr); i++)
8980 {
544d14e1 8981 tree arg = CALL_EXPR_ARG (expr, i);
fccc47dd
EB
8982
8983 if (TREE_CODE (arg) == CONSTRUCTOR
8984 && TREE_CONSTANT (arg)
8985 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (arg)))
8986 {
8987 tree t = CONSTRUCTOR_ELT (arg, 0)->value;
8988 if (TREE_CODE (t) == NOP_EXPR)
8989 t = TREE_OPERAND (t, 0);
8990 if (TREE_CODE (t) == ADDR_EXPR)
8991 t = TREE_OPERAND (t, 0);
8992 if (TREE_CODE (t) != STRING_CST)
544d14e1 8993 CALL_EXPR_ARG (expr, i) = tree_output_constant_def (arg);
fccc47dd
EB
8994 }
8995 }
9ae38416
EB
8996 break;
8997
8998 case DECL_EXPR:
8999 op = DECL_EXPR_DECL (expr);
9000
9001 /* The expressions for the RM bounds must be gimplified to ensure that
9002 they are properly elaborated. See gimplify_decl_expr. */
9003 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
9004 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op))
9005 && (INTEGRAL_TYPE_P (TREE_TYPE (op))
9006 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op))))
9007 {
9008 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
9009
9010 val = TYPE_RM_MIN_VALUE (type);
9011 if (val)
9012 {
9013 gimplify_one_sizepos (&val, pre_p);
9014 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
9015 SET_TYPE_RM_MIN_VALUE (t, val);
9016 }
9017
9018 val = TYPE_RM_MAX_VALUE (type);
9019 if (val)
9020 {
9021 gimplify_one_sizepos (&val, pre_p);
9022 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
9023 SET_TYPE_RM_MAX_VALUE (t, val);
9024 }
9025 }
9026 break;
9027
9028 case NULL_EXPR:
9029 /* If this is an aggregate type, build a null pointer of the appropriate
9030 type and dereference it. */
9031 if (AGGREGATE_TYPE_P (type)
9032 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
9033 *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE,
9034 convert (build_pointer_type (type),
9035 integer_zero_node));
9036
9037 /* Otherwise, just make a VAR_DECL. */
9038 else
9039 {
9040 *expr_p = create_tmp_var (type, NULL);
9041 suppress_warning (*expr_p);
9042 }
fccc47dd 9043
9ae38416
EB
9044 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
9045 return GS_OK;
9046
889db59e
EB
9047 case SAVE_EXPR:
9048 op = TREE_OPERAND (expr, 0);
9049
9050 /* Propagate TREE_NO_WARNING from expression to temporary by using the
9051 SAVE_EXPR itself as an intermediate step. See gimplify_save_expr. */
1434eee5
EB
9052 if (type == void_type_node)
9053 ;
9054 else if (SAVE_EXPR_RESOLVED_P (expr))
889db59e
EB
9055 TREE_NO_WARNING (op) = TREE_NO_WARNING (expr);
9056 else
9057 TREE_NO_WARNING (expr) = TREE_NO_WARNING (op);
9058 break;
9059
544d14e1
EB
9060 case LOAD_EXPR:
9061 {
9062 tree new_var = create_tmp_var (type, "L");
9063 TREE_ADDRESSABLE (new_var) = 1;
9064
9065 tree init = TREE_OPERAND (expr, 1);
9066 gcc_assert (TREE_CODE (init) == CALL_EXPR);
9067 tree arg = CALL_EXPR_ARG (init, 1);
9068 CALL_EXPR_ARG (init, 1)
9069 = build_unary_op (ADDR_EXPR, TREE_TYPE (arg), new_var);
9070 gimplify_and_add (init, pre_p);
9071
9072 *expr_p = new_var;
9073 return GS_OK;
9074 }
fccc47dd 9075
819a653e
EB
9076 case VIEW_CONVERT_EXPR:
9077 op = TREE_OPERAND (expr, 0);
9078
9079 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
9080 type to a scalar one, explicitly create the local temporary. That's
9081 required if the type is passed by reference. */
9082 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
9083 && AGGREGATE_TYPE_P (TREE_TYPE (op))
5e0f1fca 9084 && !AGGREGATE_TYPE_P (type))
819a653e 9085 {
544d14e1 9086 tree new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
819a653e
EB
9087 gimple_add_tmp_var (new_var);
9088
544d14e1 9089 tree mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
819a653e
EB
9090 gimplify_and_add (mod, pre_p);
9091
9092 TREE_OPERAND (expr, 0) = new_var;
9093 return GS_OK;
9094 }
9ae38416 9095 break;
a1ab4c31 9096
544d14e1
EB
9097 case UNCONSTRAINED_ARRAY_REF:
9098 /* We should only do this if we are just elaborating for side effects,
9099 but we can't know that yet. */
9100 *expr_p = TREE_OPERAND (expr, 0);
9101 return GS_OK;
9102
a1ab4c31 9103 default:
9ae38416 9104 break;
a1ab4c31 9105 }
9ae38416
EB
9106
9107 return GS_UNHANDLED;
a1ab4c31
AC
9108}
9109
9110/* Generate GIMPLE in place for the statement at *STMT_P. */
9111
9112static enum gimplify_status
9113gnat_gimplify_stmt (tree *stmt_p)
9114{
9115 tree stmt = *stmt_p;
9116
9117 switch (TREE_CODE (stmt))
9118 {
9119 case STMT_STMT:
9120 *stmt_p = STMT_STMT_STMT (stmt);
9121 return GS_OK;
9122
9123 case LOOP_STMT:
9124 {
c172df28 9125 tree gnu_start_label = create_artificial_label (input_location);
d88bbbb9
EB
9126 tree gnu_cond = LOOP_STMT_COND (stmt);
9127 tree gnu_update = LOOP_STMT_UPDATE (stmt);
a1ab4c31 9128 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
a1ab4c31 9129
d88bbbb9
EB
9130 /* Build the condition expression from the test, if any. */
9131 if (gnu_cond)
3418f5e9
EB
9132 {
9133 /* Deal with the optimization hints. */
9134 if (LOOP_STMT_IVDEP (stmt))
ac9effed 9135 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
3418f5e9 9136 build_int_cst (integer_type_node,
ac9effed
EB
9137 annot_expr_ivdep_kind),
9138 integer_zero_node);
9139 if (LOOP_STMT_NO_UNROLL (stmt))
9140 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
9141 build_int_cst (integer_type_node,
9142 annot_expr_unroll_kind),
9143 integer_one_node);
9144 if (LOOP_STMT_UNROLL (stmt))
9145 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
9146 build_int_cst (integer_type_node,
9147 annot_expr_unroll_kind),
9148 build_int_cst (NULL_TREE, USHRT_MAX));
718c4601 9149 if (LOOP_STMT_NO_VECTOR (stmt))
ac9effed 9150 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
718c4601 9151 build_int_cst (integer_type_node,
ac9effed
EB
9152 annot_expr_no_vector_kind),
9153 integer_zero_node);
718c4601 9154 if (LOOP_STMT_VECTOR (stmt))
ac9effed 9155 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
718c4601 9156 build_int_cst (integer_type_node,
ac9effed
EB
9157 annot_expr_vector_kind),
9158 integer_zero_node);
718c4601 9159
3418f5e9
EB
9160 gnu_cond
9161 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
9162 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
9163 }
d88bbbb9 9164
a1ab4c31
AC
9165 /* Set to emit the statements of the loop. */
9166 *stmt_p = NULL_TREE;
9167
d88bbbb9
EB
9168 /* We first emit the start label and then a conditional jump to the
9169 end label if there's a top condition, then the update if it's at
9170 the top, then the body of the loop, then a conditional jump to
9171 the end label if there's a bottom condition, then the update if
9172 it's at the bottom, and finally a jump to the start label and the
9173 definition of the end label. */
a1ab4c31
AC
9174 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
9175 gnu_start_label),
9176 stmt_p);
9177
d88bbbb9
EB
9178 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
9179 append_to_statement_list (gnu_cond, stmt_p);
9180
9181 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
9182 append_to_statement_list (gnu_update, stmt_p);
a1ab4c31
AC
9183
9184 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
9185
d88bbbb9
EB
9186 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
9187 append_to_statement_list (gnu_cond, stmt_p);
9188
9189 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
9190 append_to_statement_list (gnu_update, stmt_p);
a1ab4c31 9191
3418f5e9 9192 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
a1ab4c31
AC
9193 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
9194 append_to_statement_list (t, stmt_p);
9195
9196 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
9197 gnu_end_label),
9198 stmt_p);
9199 return GS_OK;
9200 }
9201
9202 case EXIT_STMT:
9203 /* Build a statement to jump to the corresponding end label, then
9204 see if it needs to be conditional. */
9205 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
9206 if (EXIT_STMT_COND (stmt))
9207 *stmt_p = build3 (COND_EXPR, void_type_node,
9208 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
9209 return GS_OK;
9210
9211 default:
9212 gcc_unreachable ();
9213 }
9214}
ce2d0ce2 9215
2b2a2e9e
EB
9216/* Force a reference to each of the entities in GNAT_PACKAGE recursively.
9217
9218 This routine is exclusively called in type_annotate mode, to compute DDA
9219 information for types in withed units, for ASIS use. */
9220
9221static void
9222elaborate_all_entities_for_package (Entity_Id gnat_package)
9223{
9224 Entity_Id gnat_entity;
9225
9226 for (gnat_entity = First_Entity (gnat_package);
9227 Present (gnat_entity);
9228 gnat_entity = Next_Entity (gnat_entity))
9229 {
9230 const Entity_Kind kind = Ekind (gnat_entity);
9231
9232 /* We are interested only in entities visible from the main unit. */
9233 if (!Is_Public (gnat_entity))
9234 continue;
9235
9236 /* Skip stuff internal to the compiler. */
abb540a7 9237 if (Is_Intrinsic_Subprogram (gnat_entity))
2b2a2e9e
EB
9238 continue;
9239 if (kind == E_Operator)
9240 continue;
56bad7db
EB
9241 if (IN (kind, Subprogram_Kind)
9242 && (Present (Alias (gnat_entity))
9243 || Is_Intrinsic_Subprogram (gnat_entity)))
2b2a2e9e 9244 continue;
f8a9b81f
EB
9245 if (Is_Itype (gnat_entity))
9246 continue;
2b2a2e9e
EB
9247
9248 /* Skip named numbers. */
9249 if (IN (kind, Named_Kind))
9250 continue;
9251
9252 /* Skip generic declarations. */
9253 if (IN (kind, Generic_Unit_Kind))
9254 continue;
9255
cce309d7
EB
9256 /* Skip formal objects. */
9257 if (IN (kind, Formal_Object_Kind))
9258 continue;
9259
2b2a2e9e
EB
9260 /* Skip package bodies. */
9261 if (kind == E_Package_Body)
9262 continue;
9263
9264 /* Skip limited views that point back to the main unit. */
9265 if (IN (kind, Incomplete_Kind)
9266 && From_Limited_With (gnat_entity)
9267 && In_Extended_Main_Code_Unit (Non_Limited_View (gnat_entity)))
9268 continue;
9269
9270 /* Skip types that aren't frozen. */
9271 if (IN (kind, Type_Kind) && !Is_Frozen (gnat_entity))
9272 continue;
9273
9274 /* Recurse on real packages that aren't in the main unit. */
9275 if (kind == E_Package)
9276 {
9277 if (No (Renamed_Entity (gnat_entity))
9278 && !In_Extended_Main_Code_Unit (gnat_entity))
9279 elaborate_all_entities_for_package (gnat_entity);
9280 }
9281 else
afc737f0 9282 gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
2b2a2e9e
EB
9283 }
9284}
9285
9286/* Force a reference to each of the entities in packages withed by GNAT_NODE.
a1ab4c31
AC
9287 Operate recursively but check that we aren't elaborating something more
9288 than once.
9289
9290 This routine is exclusively called in type_annotate mode, to compute DDA
9291 information for types in withed units, for ASIS use. */
9292
9293static void
9294elaborate_all_entities (Node_Id gnat_node)
9295{
2b2a2e9e 9296 Entity_Id gnat_with_clause;
a1ab4c31
AC
9297
9298 /* Process each unit only once. As we trace the context of all relevant
9299 units transitively, including generic bodies, we may encounter the
9300 same generic unit repeatedly. */
9301 if (!present_gnu_tree (gnat_node))
9302 save_gnu_tree (gnat_node, integer_zero_node, true);
9303
9304 /* Save entities in all context units. A body may have an implicit_with
9305 on its own spec, if the context includes a child unit, so don't save
9306 the spec twice. */
9307 for (gnat_with_clause = First (Context_Items (gnat_node));
9308 Present (gnat_with_clause);
9309 gnat_with_clause = Next (gnat_with_clause))
9310 if (Nkind (gnat_with_clause) == N_With_Clause
9311 && !present_gnu_tree (Library_Unit (gnat_with_clause))
9312 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
9313 {
2b2a2e9e
EB
9314 Node_Id gnat_unit = Library_Unit (gnat_with_clause);
9315 Entity_Id gnat_entity = Entity (Name (gnat_with_clause));
a1ab4c31 9316
2b2a2e9e
EB
9317 elaborate_all_entities (gnat_unit);
9318
0850f23b
EB
9319 if (Ekind (gnat_entity) == E_Package
9320 && No (Renamed_Entity (gnat_entity)))
2b2a2e9e
EB
9321 elaborate_all_entities_for_package (gnat_entity);
9322
9323 else if (Ekind (gnat_entity) == E_Generic_Package)
a1ab4c31 9324 {
2b2a2e9e 9325 Node_Id gnat_body = Corresponding_Body (Unit (gnat_unit));
a1ab4c31
AC
9326
9327 /* Retrieve compilation unit node of generic body. */
9328 while (Present (gnat_body)
9329 && Nkind (gnat_body) != N_Compilation_Unit)
9330 gnat_body = Parent (gnat_body);
9331
9332 /* If body is available, elaborate its context. */
9333 if (Present (gnat_body))
9334 elaborate_all_entities (gnat_body);
9335 }
9336 }
9337
9338 if (Nkind (Unit (gnat_node)) == N_Package_Body)
9339 elaborate_all_entities (Library_Unit (gnat_node));
9340}
ce2d0ce2 9341
f08863f9 9342/* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
a1ab4c31
AC
9343
9344static void
9345process_freeze_entity (Node_Id gnat_node)
9346{
f08863f9
EB
9347 const Entity_Id gnat_entity = Entity (gnat_node);
9348 const Entity_Kind kind = Ekind (gnat_entity);
9349 tree gnu_old, gnu_new;
9350
692317f4 9351 /* If this is a package, generate code for the package body, if any. */
f08863f9 9352 if (kind == E_Package)
a1ab4c31 9353 {
692317f4
EB
9354 const Node_Id gnat_decl = Parent (Declaration_Node (gnat_entity));
9355 if (Present (Corresponding_Body (gnat_decl)))
9356 insert_code_for (Parent (Corresponding_Body (gnat_decl)));
a1ab4c31
AC
9357 return;
9358 }
9359
f08863f9
EB
9360 /* Don't do anything for class-wide types as they are always transformed
9361 into their root type. */
9362 if (kind == E_Class_Wide_Type)
9363 return;
9364
d4090614
EB
9365 /* Likewise for the entities internally used by the front-end to register
9366 primitives covering abstract interfaces, see Expand_N_Freeze_Entity. */
9367 if (Is_Subprogram (gnat_entity) && Present (Interface_Alias (gnat_entity)))
9368 return;
9369
1e55d29a
EB
9370 /* Check for an old definition if this isn't an object with address clause,
9371 since the saved GCC tree is the address expression in that case. */
a1ab4c31 9372 gnu_old
1e55d29a
EB
9373 = present_gnu_tree (gnat_entity) && No (Address_Clause (gnat_entity))
9374 ? get_gnu_tree (gnat_entity) : NULL_TREE;
a1ab4c31
AC
9375
9376 /* Don't do anything for subprograms that may have been elaborated before
f08863f9
EB
9377 their freeze nodes. This can happen, for example, because of an inner
9378 call in an instance body or because of previous compilation of a spec
9379 for inlining purposes. */
a1ab4c31
AC
9380 if (gnu_old
9381 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
f08863f9 9382 && (kind == E_Function || kind == E_Procedure))
69720717 9383 || (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_old))
f08863f9 9384 && kind == E_Subprogram_Type)))
a1ab4c31
AC
9385 return;
9386
d5ebeb8c
EB
9387 /* If we have a non-dummy type old tree, we have nothing to do, except for
9388 aborting, since this node was never delayed as it should have been. We
a1ab4c31 9389 let this happen for concurrent types and their Corresponding_Record_Type,
f08863f9 9390 however, because each might legitimately be elaborated before its own
a1ab4c31
AC
9391 freeze node, e.g. while processing the other. */
9392 if (gnu_old
9393 && !(TREE_CODE (gnu_old) == TYPE_DECL
9394 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
9395 {
d5ebeb8c 9396 gcc_assert (Is_Concurrent_Type (gnat_entity)
4ec7c4ec 9397 || (Is_Record_Type (gnat_entity)
a1ab4c31
AC
9398 && Is_Concurrent_Record_Type (gnat_entity)));
9399 return;
9400 }
9401
9402 /* Reset the saved tree, if any, and elaborate the object or type for real.
f08863f9
EB
9403 If there is a full view, elaborate it and use the result. And, if this
9404 is the root type of a class-wide type, reuse it for the latter. */
a1ab4c31
AC
9405 if (gnu_old)
9406 {
9407 save_gnu_tree (gnat_entity, NULL_TREE, false);
bf0b0e5e 9408
7ed9919d 9409 if (Is_Incomplete_Or_Private_Type (gnat_entity)
bf0b0e5e
AC
9410 && Present (Full_View (gnat_entity)))
9411 {
9412 Entity_Id full_view = Full_View (gnat_entity);
9413
f10ff6cc
AC
9414 save_gnu_tree (full_view, NULL_TREE, false);
9415
7ed9919d 9416 if (Is_Private_Type (full_view)
bf0b0e5e 9417 && Present (Underlying_Full_View (full_view)))
f10ff6cc
AC
9418 {
9419 full_view = Underlying_Full_View (full_view);
9420 save_gnu_tree (full_view, NULL_TREE, false);
9421 }
bf0b0e5e
AC
9422 }
9423
7ed9919d 9424 if (Is_Type (gnat_entity)
f08863f9
EB
9425 && Present (Class_Wide_Type (gnat_entity))
9426 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
a1ab4c31
AC
9427 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
9428 }
9429
7ed9919d 9430 if (Is_Incomplete_Or_Private_Type (gnat_entity)
a1ab4c31
AC
9431 && Present (Full_View (gnat_entity)))
9432 {
bf0b0e5e
AC
9433 Entity_Id full_view = Full_View (gnat_entity);
9434
7ed9919d 9435 if (Is_Private_Type (full_view)
bf0b0e5e
AC
9436 && Present (Underlying_Full_View (full_view)))
9437 full_view = Underlying_Full_View (full_view);
9438
afc737f0 9439 gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, true);
a1ab4c31
AC
9440
9441 /* Propagate back-annotations from full view to partial view. */
8de68eb3 9442 if (!Known_Alignment (gnat_entity))
0c8ff35e 9443 Copy_Alignment (gnat_entity, full_view);
a1ab4c31 9444
8de68eb3 9445 if (!Known_Esize (gnat_entity))
b23cdc01 9446 Copy_Esize (gnat_entity, full_view);
a1ab4c31 9447
8de68eb3 9448 if (!Known_RM_Size (gnat_entity))
b23cdc01 9449 Copy_RM_Size (gnat_entity, full_view);
a1ab4c31
AC
9450
9451 /* The above call may have defined this entity (the simplest example
f08863f9
EB
9452 of this is when we have a private enumeral type since the bounds
9453 will have the public view). */
a1ab4c31 9454 if (!present_gnu_tree (gnat_entity))
f08863f9 9455 save_gnu_tree (gnat_entity, gnu_new, false);
a1ab4c31
AC
9456 }
9457 else
f08863f9
EB
9458 {
9459 tree gnu_init
9460 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
9461 && present_gnu_tree (Declaration_Node (gnat_entity)))
9462 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
9463
afc737f0 9464 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, true);
f08863f9
EB
9465 }
9466
7ed9919d 9467 if (Is_Type (gnat_entity)
f08863f9
EB
9468 && Present (Class_Wide_Type (gnat_entity))
9469 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
9470 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
a1ab4c31 9471
65444786
EB
9472 /* If we have an old type and we've made pointers to this type, update those
9473 pointers. If this is a Taft amendment type in the main unit, we need to
9474 mark the type as used since other units referencing it don't see the full
9475 declaration and, therefore, cannot mark it as used themselves. */
a1ab4c31 9476 if (gnu_old)
65444786
EB
9477 {
9478 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
9479 TREE_TYPE (gnu_new));
1e55d29a
EB
9480 if (TYPE_DUMMY_IN_PROFILE_P (TREE_TYPE (gnu_old)))
9481 update_profiles_with (TREE_TYPE (gnu_old));
65444786
EB
9482 if (DECL_TAFT_TYPE_P (gnu_old))
9483 used_types_insert (TREE_TYPE (gnu_new));
9484 }
a1ab4c31 9485}
ce2d0ce2 9486
a1ab4c31
AC
9487/* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
9488 We make two passes, one to elaborate anything other than bodies (but
9489 we declare a function if there was no spec). The second pass
9490 elaborates the bodies.
9491
a1ab4c31
AC
9492 We make a complete pass through both lists if PASS1P is true, then make
9493 the second pass over both lists if PASS2P is true. The lists usually
9494 correspond to the public and private parts of a package. */
9495
9496static void
9497process_decls (List_Id gnat_decls, List_Id gnat_decls2,
6d16658d 9498 bool pass1p, bool pass2p)
a1ab4c31
AC
9499{
9500 List_Id gnat_decl_array[2];
9501 Node_Id gnat_decl;
9502 int i;
9503
9504 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
9505
9506 if (pass1p)
9507 for (i = 0; i <= 1; i++)
9508 if (Present (gnat_decl_array[i]))
9509 for (gnat_decl = First (gnat_decl_array[i]);
6d16658d
BD
9510 Present (gnat_decl);
9511 gnat_decl = Next (gnat_decl))
a1ab4c31
AC
9512 {
9513 /* For package specs, we recurse inside the declarations,
9514 thus taking the two pass approach inside the boundary. */
9515 if (Nkind (gnat_decl) == N_Package_Declaration
9516 && (Nkind (Specification (gnat_decl)
9517 == N_Package_Specification)))
9518 process_decls (Visible_Declarations (Specification (gnat_decl)),
9519 Private_Declarations (Specification (gnat_decl)),
6d16658d 9520 true, false);
a1ab4c31
AC
9521
9522 /* Similarly for any declarations in the actions of a
9523 freeze node. */
9524 else if (Nkind (gnat_decl) == N_Freeze_Entity)
9525 {
9526 process_freeze_entity (gnat_decl);
6d16658d 9527 process_decls (Actions (gnat_decl), Empty, true, false);
a1ab4c31
AC
9528 }
9529
9530 /* Package bodies with freeze nodes get their elaboration deferred
9531 until the freeze node, but the code must be placed in the right
9532 place, so record the code position now. */
9533 else if (Nkind (gnat_decl) == N_Package_Body
9534 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
9535 record_code_position (gnat_decl);
9536
1e17ef87 9537 else if (Nkind (gnat_decl) == N_Package_Body_Stub
a1ab4c31
AC
9538 && Present (Library_Unit (gnat_decl))
9539 && Present (Freeze_Node
9540 (Corresponding_Spec
9541 (Proper_Body (Unit
9542 (Library_Unit (gnat_decl)))))))
9543 record_code_position
9544 (Proper_Body (Unit (Library_Unit (gnat_decl))));
9545
4e279bd9
EB
9546 /* We defer most subprogram bodies to the second pass. For bodies
9547 that act as their own specs and stubs, the entity itself must be
9548 elaborated in the first pass, because it may be used in other
9549 declarations. */
a1ab4c31
AC
9550 else if (Nkind (gnat_decl) == N_Subprogram_Body)
9551 {
9552 if (Acts_As_Spec (gnat_decl))
9553 {
4e279bd9 9554 Entity_Id gnat_subprog = Defining_Entity (gnat_decl);
a1ab4c31 9555
4e279bd9
EB
9556 if (!Is_Generic_Subprogram (gnat_subprog))
9557 gnat_to_gnu_entity (gnat_subprog, NULL_TREE, true);
a1ab4c31
AC
9558 }
9559 }
1e17ef87 9560
a1ab4c31
AC
9561 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
9562 {
4e279bd9 9563 Entity_Id gnat_subprog
1e17ef87 9564 = Defining_Entity (Specification (gnat_decl));
a1ab4c31 9565
4e279bd9
EB
9566 if (!Is_Generic_Subprogram (gnat_subprog)
9567 && Ekind (gnat_subprog) != E_Subprogram_Body)
9568 gnat_to_gnu_entity (gnat_subprog, NULL_TREE, true);
1e17ef87 9569 }
a1ab4c31
AC
9570
9571 /* Concurrent stubs stand for the corresponding subprogram bodies,
9572 which are deferred like other bodies. */
9573 else if (Nkind (gnat_decl) == N_Task_Body_Stub
9574 || Nkind (gnat_decl) == N_Protected_Body_Stub)
9575 ;
1e17ef87 9576
1d4b96e0
AC
9577 /* Renamed subprograms may not be elaborated yet at this point
9578 since renamings do not trigger freezing. Wait for the second
9579 pass to take care of them. */
9580 else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration)
9581 ;
9582
a1ab4c31
AC
9583 else
9584 add_stmt (gnat_to_gnu (gnat_decl));
9585 }
9586
9587 /* Here we elaborate everything we deferred above except for package bodies,
9588 which are elaborated at their freeze nodes. Note that we must also
9589 go inside things (package specs and freeze nodes) the first pass did. */
9590 if (pass2p)
9591 for (i = 0; i <= 1; i++)
9592 if (Present (gnat_decl_array[i]))
9593 for (gnat_decl = First (gnat_decl_array[i]);
6d16658d
BD
9594 Present (gnat_decl);
9595 gnat_decl = Next (gnat_decl))
a1ab4c31
AC
9596 {
9597 if (Nkind (gnat_decl) == N_Subprogram_Body
9598 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
9599 || Nkind (gnat_decl) == N_Task_Body_Stub
9600 || Nkind (gnat_decl) == N_Protected_Body_Stub)
9601 add_stmt (gnat_to_gnu (gnat_decl));
9602
9603 else if (Nkind (gnat_decl) == N_Package_Declaration
9604 && (Nkind (Specification (gnat_decl)
9605 == N_Package_Specification)))
9606 process_decls (Visible_Declarations (Specification (gnat_decl)),
9607 Private_Declarations (Specification (gnat_decl)),
6d16658d 9608 false, true);
a1ab4c31
AC
9609
9610 else if (Nkind (gnat_decl) == N_Freeze_Entity)
6d16658d 9611 process_decls (Actions (gnat_decl), Empty, false, true);
1d4b96e0
AC
9612
9613 else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration)
9614 add_stmt (gnat_to_gnu (gnat_decl));
a1ab4c31
AC
9615 }
9616}
ce2d0ce2 9617
b666e568 9618/* Make a unary operation of kind CODE using build_unary_op, but guard
a7c43bbc
EB
9619 the operation by an overflow check. CODE can be one of NEGATE_EXPR
9620 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
10069d53
EB
9621 the operation is to be performed in that type. GNAT_NODE is the gnat
9622 node conveying the source location for which the error should be
9623 signaled. */
b666e568
GB
9624
9625static tree
10069d53
EB
9626build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
9627 Node_Id gnat_node)
b666e568 9628{
a7c43bbc 9629 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
b666e568 9630
7d7a1fe8 9631 operand = gnat_protect_expr (operand);
b666e568 9632
1139f2e8 9633 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
b666e568
GB
9634 operand, TYPE_MIN_VALUE (gnu_type)),
9635 build_unary_op (code, gnu_type, operand),
10069d53 9636 CE_Overflow_Check_Failed, gnat_node);
b666e568
GB
9637}
9638
a7c43bbc
EB
9639/* Make a binary operation of kind CODE using build_binary_op, but guard
9640 the operation by an overflow check. CODE can be one of PLUS_EXPR,
9641 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
10069d53
EB
9642 Usually the operation is to be performed in that type. GNAT_NODE is
9643 the GNAT node conveying the source location for which the error should
9644 be signaled. */
b666e568
GB
9645
9646static tree
a7c43bbc 9647build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
10069d53 9648 tree right, Node_Id gnat_node)
b666e568 9649{
3f1be5be 9650 const unsigned int precision = TYPE_PRECISION (gnu_type);
7d7a1fe8
EB
9651 tree lhs = gnat_protect_expr (left);
9652 tree rhs = gnat_protect_expr (right);
b666e568
GB
9653 tree type_max = TYPE_MAX_VALUE (gnu_type);
9654 tree type_min = TYPE_MIN_VALUE (gnu_type);
aa18f65a
EB
9655 tree gnu_expr, check;
9656 int sgn;
b666e568 9657
3f1be5be
AC
9658 /* Assert that the precision is a power of 2. */
9659 gcc_assert ((precision & (precision - 1)) == 0);
b666e568 9660
aa18f65a
EB
9661 /* Prefer a constant on the RHS to simplify checks. */
9662 if (TREE_CODE (rhs) != INTEGER_CST
9663 && TREE_CODE (lhs) == INTEGER_CST
9664 && (code == PLUS_EXPR || code == MULT_EXPR))
b666e568 9665 {
a7c43bbc
EB
9666 tree tmp = lhs;
9667 lhs = rhs;
9668 rhs = tmp;
4ae39383
GB
9669 }
9670
3f1be5be
AC
9671 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
9672
9673 /* If we can fold the expression to a constant, just return it.
9674 The caller will deal with overflow, no need to generate a check. */
aa18f65a 9675 if (TREE_CODE (gnu_expr) == INTEGER_CST)
3f1be5be
AC
9676 return gnu_expr;
9677
aa18f65a
EB
9678 /* If no operand is a constant, we use the generic implementation. */
9679 if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (rhs) != INTEGER_CST)
b666e568 9680 {
4e1a2158
EB
9681 /* First convert the operands to the result type like build_binary_op.
9682 This is where the bias is made explicit for biased types. */
9683 lhs = convert (gnu_type, lhs);
9684 rhs = convert (gnu_type, rhs);
9685
aa18f65a
EB
9686 /* Never inline a 64-bit mult for a 32-bit target, it's way too long. */
9687 if (code == MULT_EXPR && precision == 64 && BITS_PER_WORD < 64)
f7ebc6a8 9688 {
aa18f65a 9689 tree int64 = gnat_type_for_size (64, 0);
1f3f64b9 9690 Check_Restriction_No_Dependence_On_System (Name_Arith_64, gnat_node);
dddf8120 9691 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
aa18f65a
EB
9692 convert (int64, lhs),
9693 convert (int64, rhs)));
58e94443 9694 }
a7c43bbc 9695
f2d9f95e
EB
9696 /* Likewise for a 128-bit mult and a 64-bit target. */
9697 else if (code == MULT_EXPR && precision == 128 && BITS_PER_WORD < 128)
9698 {
9699 tree int128 = gnat_type_for_size (128, 0);
1f3f64b9 9700 Check_Restriction_No_Dependence_On_System (Name_Arith_128, gnat_node);
f2d9f95e
EB
9701 return convert (gnu_type, build_call_n_expr (mulv128_decl, 2,
9702 convert (int128, lhs),
9703 convert (int128, rhs)));
9704 }
9705
aa18f65a 9706 enum internal_fn icode;
4ae39383 9707
aa18f65a 9708 switch (code)
4ae39383 9709 {
aa18f65a
EB
9710 case PLUS_EXPR:
9711 icode = IFN_ADD_OVERFLOW;
9712 break;
9713 case MINUS_EXPR:
9714 icode = IFN_SUB_OVERFLOW;
9715 break;
9716 case MULT_EXPR:
9717 icode = IFN_MUL_OVERFLOW;
9718 break;
9719 default:
9720 gcc_unreachable ();
4ae39383 9721 }
aa18f65a
EB
9722
9723 tree gnu_ctype = build_complex_type (gnu_type);
9724 tree call
9725 = build_call_expr_internal_loc (UNKNOWN_LOCATION, icode, gnu_ctype, 2,
9726 lhs, rhs);
9727 tree tgt = save_expr (call);
9728 gnu_expr = build1 (REALPART_EXPR, gnu_type, tgt);
87e25aac
EB
9729 check = fold_build2 (NE_EXPR, boolean_type_node,
9730 build1 (IMAGPART_EXPR, gnu_type, tgt),
9731 build_int_cst (gnu_type, 0));
aa18f65a
EB
9732 return
9733 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
4ae39383 9734 }
b666e568 9735
aa18f65a
EB
9736 /* If one operand is a constant, we expose the overflow condition to enable
9737 a subsequent simplication or even elimination. */
b666e568
GB
9738 switch (code)
9739 {
9740 case PLUS_EXPR:
aa18f65a
EB
9741 sgn = tree_int_cst_sgn (rhs);
9742 if (sgn > 0)
9743 /* When rhs > 0, overflow when lhs > type_max - rhs. */
9744 check = build_binary_op (GT_EXPR, boolean_type_node, lhs,
9745 build_binary_op (MINUS_EXPR, gnu_type,
9746 type_max, rhs));
9747 else if (sgn < 0)
9748 /* When rhs < 0, overflow when lhs < type_min - rhs. */
9749 check = build_binary_op (LT_EXPR, boolean_type_node, lhs,
9750 build_binary_op (MINUS_EXPR, gnu_type,
9751 type_min, rhs));
9752 else
9753 return gnu_expr;
b666e568
GB
9754 break;
9755
9756 case MINUS_EXPR:
aa18f65a
EB
9757 if (TREE_CODE (lhs) == INTEGER_CST)
9758 {
9759 sgn = tree_int_cst_sgn (lhs);
9760 if (sgn > 0)
9761 /* When lhs > 0, overflow when rhs < lhs - type_max. */
9762 check = build_binary_op (LT_EXPR, boolean_type_node, rhs,
9763 build_binary_op (MINUS_EXPR, gnu_type,
9764 lhs, type_max));
9765 else if (sgn < 0)
9766 /* When lhs < 0, overflow when rhs > lhs - type_min. */
9767 check = build_binary_op (GT_EXPR, boolean_type_node, rhs,
9768 build_binary_op (MINUS_EXPR, gnu_type,
9769 lhs, type_min));
9770 else
9771 return gnu_expr;
9772 }
9773 else
9774 {
9775 sgn = tree_int_cst_sgn (rhs);
9776 if (sgn > 0)
9777 /* When rhs > 0, overflow when lhs < type_min + rhs. */
9778 check = build_binary_op (LT_EXPR, boolean_type_node, lhs,
9779 build_binary_op (PLUS_EXPR, gnu_type,
9780 type_min, rhs));
9781 else if (sgn < 0)
9782 /* When rhs < 0, overflow when lhs > type_max + rhs. */
9783 check = build_binary_op (GT_EXPR, boolean_type_node, lhs,
9784 build_binary_op (PLUS_EXPR, gnu_type,
9785 type_max, rhs));
9786 else
9787 return gnu_expr;
9788 }
b666e568
GB
9789 break;
9790
9791 case MULT_EXPR:
aa18f65a
EB
9792 sgn = tree_int_cst_sgn (rhs);
9793 if (sgn > 0)
9794 {
9795 if (integer_onep (rhs))
9796 return gnu_expr;
9797
9798 tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
9799 tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
9800
9801 /* When rhs > 1, overflow outside [type_min/rhs; type_max/rhs]. */
9802 check
9803 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9804 build_binary_op (LT_EXPR, boolean_type_node,
9805 lhs, lb),
9806 build_binary_op (GT_EXPR, boolean_type_node,
9807 lhs, ub));
9808 }
9809 else if (sgn < 0)
9810 {
9811 tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
9812 tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
9813
9814 if (integer_minus_onep (rhs))
9815 /* When rhs == -1, overflow if lhs == type_min. */
9816 check
9817 = build_binary_op (EQ_EXPR, boolean_type_node, lhs, type_min);
9818 else
9819 /* When rhs < -1, overflow outside [type_max/rhs; type_min/rhs]. */
9820 check
9821 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9822 build_binary_op (LT_EXPR, boolean_type_node,
9823 lhs, lb),
9824 build_binary_op (GT_EXPR, boolean_type_node,
9825 lhs, ub));
9826 }
9827 else
9828 return gnu_expr;
b666e568
GB
9829 break;
9830
9831 default:
7c775aca 9832 gcc_unreachable ();
b666e568
GB
9833 }
9834
10069d53 9835 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
b666e568
GB
9836}
9837
4e9a8214
EB
9838/* GNU_COND contains the condition corresponding to an index, overflow or
9839 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR
9840 if GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
9841 REASON is the code that says why the exception is raised. GNAT_NODE is
9842 the node conveying the source location for which the error should be
9843 signaled.
9844
9845 We used to propagate TREE_SIDE_EFFECTS from GNU_EXPR to the COND_EXPR,
9846 overwriting the setting inherited from the call statement, on the ground
9847 that the expression need not be evaluated just for the check. However
9848 that's incorrect because, in the GCC type system, its value is presumed
9849 to be valid so its comparison against the type bounds always yields true
9850 and, therefore, could be done without evaluating it; given that it can
9851 be a computation that overflows the bounds, the language may require the
9852 check to fail and thus the expression to be evaluated in this case. */
a1ab4c31
AC
9853
9854static tree
10069d53 9855emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
a1ab4c31 9856{
10069d53
EB
9857 tree gnu_call
9858 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
4e9a8214
EB
9859 return
9860 fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
9861 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
9a1bdc31
EB
9862 SCALAR_FLOAT_TYPE_P (TREE_TYPE (gnu_expr))
9863 ? build_real (TREE_TYPE (gnu_expr), dconst0)
9864 : build_int_cst (TREE_TYPE (gnu_expr), 0)),
4e9a8214 9865 gnu_expr);
a1ab4c31 9866}
ce2d0ce2 9867
1e17ef87 9868/* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
815b5368
EB
9869 checks if OVERFLOW_P is true. If TRUNCATE_P is true, do a fp-to-integer
9870 conversion with truncation, otherwise round. GNAT_NODE is the GNAT node
9871 conveying the source location for which the error should be signaled. */
a1ab4c31
AC
9872
9873static tree
a4f7374e 9874convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflow_p,
815b5368 9875 bool truncate_p, Node_Id gnat_node)
a1ab4c31
AC
9876{
9877 tree gnu_type = get_unpadded_type (gnat_type);
a1ab4c31 9878 tree gnu_base_type = get_base_type (gnu_type);
a4f7374e
EB
9879 tree gnu_in_type = TREE_TYPE (gnu_expr);
9880 tree gnu_in_base_type = get_base_type (gnu_in_type);
a1ab4c31
AC
9881 tree gnu_result = gnu_expr;
9882
1eb58520
AC
9883 /* If we are not doing any checks, the output is an integral type and the
9884 input is not a floating-point type, just do the conversion. This is
9885 required for packed array types and is simpler in all cases anyway. */
815b5368 9886 if (!overflow_p
1eb58520 9887 && INTEGRAL_TYPE_P (gnu_base_type)
a4f7374e 9888 && !FLOAT_TYPE_P (gnu_in_base_type))
a1ab4c31
AC
9889 return convert (gnu_type, gnu_expr);
9890
a4f7374e
EB
9891 /* If the mode of the input base type is larger, then converting to it below
9892 may pessimize the final conversion step, for example generate a libcall
9893 instead of a simple instruction, so use a narrower type in this case. */
9894 if (TYPE_MODE (gnu_in_base_type) != TYPE_MODE (gnu_in_type)
9895 && !(TREE_CODE (gnu_in_type) == INTEGER_TYPE
9896 && TYPE_BIASED_REPRESENTATION_P (gnu_in_type)))
9897 gnu_in_base_type = gnat_type_for_mode (TYPE_MODE (gnu_in_type),
9898 TYPE_UNSIGNED (gnu_in_type));
9899
9900 /* First convert the expression to the base type. This will never generate
9901 code, but makes the tests below simpler. But don't do this if converting
9902 from an integer type to an unconstrained array type since then we need to
9903 get the bounds from the original (unpacked) type. */
a1ab4c31 9904 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
a4f7374e 9905 gnu_result = convert (gnu_in_base_type, gnu_result);
a1ab4c31 9906
a4f7374e
EB
9907 /* If overflow checks are requested, we need to be sure the result will fit
9908 in the output base type. But don't do this if the input is integer and
9909 the output floating-point. */
9910 if (overflow_p
9911 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_base_type)))
a1ab4c31
AC
9912 {
9913 /* Ensure GNU_EXPR only gets evaluated once. */
7d7a1fe8 9914 tree gnu_input = gnat_protect_expr (gnu_result);
bf6490b5 9915 tree gnu_cond = boolean_false_node;
a4f7374e
EB
9916 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_base_type);
9917 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_base_type);
a1ab4c31 9918 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
815b5368
EB
9919 tree gnu_out_ub
9920 = (TREE_CODE (gnu_base_type) == INTEGER_TYPE
9921 && TYPE_MODULAR_P (gnu_base_type))
9922 ? fold_build2 (MINUS_EXPR, gnu_base_type,
9923 TYPE_MODULUS (gnu_base_type),
9924 build_int_cst (gnu_base_type, 1))
9925 : TYPE_MAX_VALUE (gnu_base_type);
a1ab4c31
AC
9926
9927 /* Convert the lower bounds to signed types, so we're sure we're
9928 comparing them properly. Likewise, convert the upper bounds
9929 to unsigned types. */
a4f7374e
EB
9930 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9931 && TYPE_UNSIGNED (gnu_in_base_type))
9a1bdc31 9932 gnu_in_lb
a4f7374e 9933 = convert (gnat_signed_type_for (gnu_in_base_type), gnu_in_lb);
a1ab4c31 9934
a4f7374e
EB
9935 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9936 && !TYPE_UNSIGNED (gnu_in_base_type))
9a1bdc31 9937 gnu_in_ub
a4f7374e 9938 = convert (gnat_unsigned_type_for (gnu_in_base_type), gnu_in_ub);
a1ab4c31
AC
9939
9940 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
9a1bdc31
EB
9941 gnu_out_lb
9942 = convert (gnat_signed_type_for (gnu_base_type), gnu_out_lb);
a1ab4c31
AC
9943
9944 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
9a1bdc31
EB
9945 gnu_out_ub
9946 = convert (gnat_unsigned_type_for (gnu_base_type), gnu_out_ub);
a1ab4c31
AC
9947
9948 /* Check each bound separately and only if the result bound
9949 is tighter than the bound on the input type. Note that all the
9950 types are base types, so the bounds must be constant. Also,
9951 the comparison is done in the base type of the input, which
9952 always has the proper signedness. First check for input
9953 integer (which means output integer), output float (which means
9954 both float), or mixed, in which case we always compare.
9955 Note that we have to do the comparison which would *fail* in the
9956 case of an error since if it's an FP comparison and one of the
9957 values is a NaN or Inf, the comparison will fail. */
a4f7374e 9958 if (INTEGRAL_TYPE_P (gnu_in_base_type)
a1ab4c31
AC
9959 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
9960 : (FLOAT_TYPE_P (gnu_base_type)
8cb41028
RS
9961 ? real_less (&TREE_REAL_CST (gnu_in_lb),
9962 &TREE_REAL_CST (gnu_out_lb))
a1ab4c31
AC
9963 : 1))
9964 gnu_cond
9965 = invert_truthvalue
1139f2e8 9966 (build_binary_op (GE_EXPR, boolean_type_node,
a4f7374e 9967 gnu_input, convert (gnu_in_base_type,
a1ab4c31
AC
9968 gnu_out_lb)));
9969
a4f7374e 9970 if (INTEGRAL_TYPE_P (gnu_in_base_type)
a1ab4c31
AC
9971 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
9972 : (FLOAT_TYPE_P (gnu_base_type)
8cb41028 9973 ? real_less (&TREE_REAL_CST (gnu_out_ub),
21288963 9974 &TREE_REAL_CST (gnu_in_ub))
a1ab4c31
AC
9975 : 1))
9976 gnu_cond
1139f2e8 9977 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
a1ab4c31 9978 invert_truthvalue
1139f2e8 9979 (build_binary_op (LE_EXPR, boolean_type_node,
a1ab4c31 9980 gnu_input,
a4f7374e 9981 convert (gnu_in_base_type,
a1ab4c31
AC
9982 gnu_out_ub))));
9983
9984 if (!integer_zerop (gnu_cond))
10069d53
EB
9985 gnu_result = emit_check (gnu_cond, gnu_input,
9986 CE_Overflow_Check_Failed, gnat_node);
a1ab4c31
AC
9987 }
9988
9989 /* Now convert to the result base type. If this is a non-truncating
9990 float-to-integer conversion, round. */
24228312 9991 if (INTEGRAL_TYPE_P (gnu_base_type)
a4f7374e
EB
9992 && FLOAT_TYPE_P (gnu_in_base_type)
9993 && !truncate_p)
a1ab4c31
AC
9994 {
9995 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
ced57283 9996 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
a1ab4c31
AC
9997 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
9998 const struct real_format *fmt;
9999
10000 /* The following calculations depend on proper rounding to even
24228312 10001 of each arithmetic operation. In order to prevent excess
1e17ef87
EB
10002 precision from spoiling this property, use the widest hardware
10003 floating-point type if FP_ARITH_MAY_WIDEN is true. */
10004 calc_type
a4f7374e 10005 = fp_arith_may_widen ? longest_float_type_node : gnu_in_base_type;
a1ab4c31 10006
1e17ef87 10007 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
a1ab4c31
AC
10008 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
10009 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
5c88ea94
RS
10010 real_arithmetic (&pred_half, MINUS_EXPR, &dconsthalf,
10011 &half_minus_pred_half);
a1ab4c31
AC
10012 gnu_pred_half = build_real (calc_type, pred_half);
10013
10014 /* If the input is strictly negative, subtract this value
ced57283 10015 and otherwise add it from the input. For 0.5, the result
1e17ef87 10016 is exactly between 1.0 and the machine number preceding 1.0
ced57283 10017 (for calc_type). Since the last bit of 1.0 is even, this 0.5
1e17ef87 10018 will round to 1.0, while all other number with an absolute
ced57283 10019 value less than 0.5 round to 0.0. For larger numbers exactly
1e17ef87
EB
10020 halfway between integers, rounding will always be correct as
10021 the true mathematical result will be closer to the higher
ced57283 10022 integer compared to the lower one. So, this constant works
1e17ef87
EB
10023 for all floating-point numbers.
10024
10025 The reason to use the same constant with subtract/add instead
10026 of a positive and negative constant is to allow the comparison
10027 to be scheduled in parallel with retrieval of the constant and
10028 conversion of the input to the calc_type (if necessary). */
a1ab4c31 10029
a4f7374e 10030 gnu_zero = build_real (gnu_in_base_type, dconst0);
7d7a1fe8 10031 gnu_result = gnat_protect_expr (gnu_result);
ced57283
EB
10032 gnu_conv = convert (calc_type, gnu_result);
10033 gnu_comp
1139f2e8 10034 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
a1ab4c31 10035 gnu_add_pred_half
ced57283 10036 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
a1ab4c31 10037 gnu_subtract_pred_half
ced57283
EB
10038 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
10039 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
10040 gnu_add_pred_half, gnu_subtract_pred_half);
a1ab4c31
AC
10041 }
10042
10043 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
10044 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
10045 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
10046 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
10047 else
10048 gnu_result = convert (gnu_base_type, gnu_result);
10049
a80e0583
EB
10050 /* If this is a conversion between an integer type larger than a word and a
10051 floating-point type, then declare the dependence on the libgcc routine. */
10052 if ((INTEGRAL_TYPE_P (gnu_in_base_type)
10053 && TYPE_PRECISION (gnu_in_base_type) > BITS_PER_WORD
10054 && FLOAT_TYPE_P (gnu_base_type))
10055 || (FLOAT_TYPE_P (gnu_in_base_type)
10056 && INTEGRAL_TYPE_P (gnu_base_type)
10057 && TYPE_PRECISION (gnu_base_type) > BITS_PER_WORD))
10058 Check_Restriction_No_Dependence_On_System (Name_Gcc, gnat_node);
10059
a1ab4c31
AC
10060 return convert (gnu_type, gnu_result);
10061}
ce2d0ce2 10062
a1ab4c31
AC
10063/* Return true if GNU_EXPR can be directly addressed. This is the case
10064 unless it is an expression involving computation or if it involves a
10065 reference to a bitfield or to an object not sufficiently aligned for
10066 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
10067 be directly addressed as an object of this type.
10068
10069 *** Notes on addressability issues in the Ada compiler ***
10070
10071 This predicate is necessary in order to bridge the gap between Gigi
10072 and the middle-end about addressability of GENERIC trees. A tree
10073 is said to be addressable if it can be directly addressed, i.e. if
10074 its address can be taken, is a multiple of the type's alignment on
10075 strict-alignment architectures and returns the first storage unit
10076 assigned to the object represented by the tree.
10077
10078 In the C family of languages, everything is in practice addressable
10079 at the language level, except for bit-fields. This means that these
10080 compilers will take the address of any tree that doesn't represent
10081 a bit-field reference and expect the result to be the first storage
10082 unit assigned to the object. Even in cases where this will result
10083 in unaligned accesses at run time, nothing is supposed to be done
10084 and the program is considered as erroneous instead (see PR c/18287).
10085
10086 The implicit assumptions made in the middle-end are in keeping with
10087 the C viewpoint described above:
10088 - the address of a bit-field reference is supposed to be never
10089 taken; the compiler (generally) will stop on such a construct,
10090 - any other tree is addressable if it is formally addressable,
10091 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
10092
10093 In Ada, the viewpoint is the opposite one: nothing is addressable
10094 at the language level unless explicitly declared so. This means
10095 that the compiler will both make sure that the trees representing
10096 references to addressable ("aliased" in Ada parlance) objects are
10097 addressable and make no real attempts at ensuring that the trees
10098 representing references to non-addressable objects are addressable.
10099
10100 In the first case, Ada is effectively equivalent to C and handing
10101 down the direct result of applying ADDR_EXPR to these trees to the
10102 middle-end works flawlessly. In the second case, Ada cannot afford
10103 to consider the program as erroneous if the address of trees that
10104 are not addressable is requested for technical reasons, unlike C;
10105 as a consequence, the Ada compiler must arrange for either making
10106 sure that this address is not requested in the middle-end or for
10107 compensating by inserting temporaries if it is requested in Gigi.
10108
10109 The first goal can be achieved because the middle-end should not
10110 request the address of non-addressable trees on its own; the only
10111 exception is for the invocation of low-level block operations like
10112 memcpy, for which the addressability requirements are lower since
10113 the type's alignment can be disregarded. In practice, this means
10114 that Gigi must make sure that such operations cannot be applied to
10115 non-BLKmode bit-fields.
10116
5a19bc0a
EB
10117 The second goal is achieved by means of the addressable_p predicate,
10118 which computes whether a temporary must be inserted by Gigi when the
10119 address of a tree is requested; if so, the address of the temporary
10120 will be used in lieu of that of the original tree and some glue code
10121 generated to connect everything together. */
a1ab4c31
AC
10122
10123static bool
10124addressable_p (tree gnu_expr, tree gnu_type)
10125{
169afcb9
EB
10126 /* For an integral type, the size of the actual type of the object may not
10127 be greater than that of the expected type, otherwise an indirect access
10128 in the latter type wouldn't correctly set all the bits of the object. */
10129 if (gnu_type
10130 && INTEGRAL_TYPE_P (gnu_type)
10131 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
10132 return false;
10133
10134 /* The size of the actual type of the object may not be smaller than that
10135 of the expected type, otherwise an indirect access in the latter type
10136 would be larger than the object. But only record types need to be
10137 considered in practice for this case. */
a1ab4c31
AC
10138 if (gnu_type
10139 && TREE_CODE (gnu_type) == RECORD_TYPE
169afcb9 10140 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
a1ab4c31
AC
10141 return false;
10142
10143 switch (TREE_CODE (gnu_expr))
10144 {
10145 case VAR_DECL:
10146 case PARM_DECL:
10147 case FUNCTION_DECL:
10148 case RESULT_DECL:
10149 /* All DECLs are addressable: if they are in a register, we can force
10150 them to memory. */
10151 return true;
10152
10153 case UNCONSTRAINED_ARRAY_REF:
10154 case INDIRECT_REF:
0b3467c4 10155 /* Taking the address of a dereference yields the original pointer. */
42c08997
EB
10156 return true;
10157
a1ab4c31
AC
10158 case STRING_CST:
10159 case INTEGER_CST:
1edbeb15 10160 case REAL_CST:
0b3467c4
EB
10161 /* Taking the address yields a pointer to the constant pool. */
10162 return true;
10163
10164 case CONSTRUCTOR:
10165 /* Taking the address of a static constructor yields a pointer to the
10166 tree constant pool. */
10167 return TREE_STATIC (gnu_expr) ? true : false;
10168
a1ab4c31 10169 case NULL_EXPR:
1edbeb15 10170 case ADDR_EXPR:
a1ab4c31
AC
10171 case SAVE_EXPR:
10172 case CALL_EXPR:
42c08997
EB
10173 case PLUS_EXPR:
10174 case MINUS_EXPR:
9f4afcd4
EB
10175 case BIT_IOR_EXPR:
10176 case BIT_XOR_EXPR:
10177 case BIT_AND_EXPR:
10178 case BIT_NOT_EXPR:
42c08997
EB
10179 /* All rvalues are deemed addressable since taking their address will
10180 force a temporary to be created by the middle-end. */
a1ab4c31
AC
10181 return true;
10182
0b3467c4
EB
10183 case COMPOUND_EXPR:
10184 /* The address of a compound expression is that of its 2nd operand. */
10185 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
10186
a1ab4c31
AC
10187 case COND_EXPR:
10188 /* We accept &COND_EXPR as soon as both operands are addressable and
10189 expect the outcome to be the address of the selected operand. */
10190 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
10191 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
10192
10193 case COMPONENT_REF:
10194 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
10195 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
10196 the field is sufficiently aligned, in case it is subject
10197 to a pragma Component_Alignment. But we don't need to
10198 check the alignment of the containing record, as it is
10199 guaranteed to be not smaller than that of its most
10200 aligned field that is not a bit-field. */
1e17ef87 10201 && (!STRICT_ALIGNMENT
a1ab4c31
AC
10202 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
10203 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
10204 /* The field of a padding record is always addressable. */
3c157c27 10205 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
a1ab4c31
AC
10206 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
10207
10208 case ARRAY_REF: case ARRAY_RANGE_REF:
10209 case REALPART_EXPR: case IMAGPART_EXPR:
10210 case NOP_EXPR:
10211 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
10212
10213 case CONVERT_EXPR:
10214 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
10215 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
10216
10217 case VIEW_CONVERT_EXPR:
10218 {
10219 /* This is addressable if we can avoid a copy. */
10220 tree type = TREE_TYPE (gnu_expr);
10221 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
10222 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
10223 && (!STRICT_ALIGNMENT
10224 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
10225 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
10226 || ((TYPE_MODE (type) == BLKmode
10227 || TYPE_MODE (inner_type) == BLKmode)
10228 && (!STRICT_ALIGNMENT
10229 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
10230 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
10231 || TYPE_ALIGN_OK (type)
10232 || TYPE_ALIGN_OK (inner_type))))
10233 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
10234 }
10235
10236 default:
10237 return false;
10238 }
10239}
ce2d0ce2 10240
d5ebeb8c
EB
10241/* Do the processing for the declaration of a GNAT_ENTITY, a type or subtype.
10242 If a Freeze node exists for the entity, delay the bulk of the processing.
10243 Otherwise make a GCC type for GNAT_ENTITY and set up the correspondence. */
a1ab4c31
AC
10244
10245void
10246process_type (Entity_Id gnat_entity)
10247{
10248 tree gnu_old
d5ebeb8c
EB
10249 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
10250
10251 /* If we are to delay elaboration of this type, just do any elaboration
10252 needed for expressions within the declaration and make a dummy node
10253 for it and its Full_View (if any), in case something points to it.
10254 Do not do this if it has already been done (the only way that can
10255 happen is if the private completion is also delayed). */
10256 if (Present (Freeze_Node (gnat_entity)))
a1ab4c31
AC
10257 {
10258 elaborate_entity (gnat_entity);
10259
10260 if (!gnu_old)
1e17ef87 10261 {
10069d53 10262 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
a1ab4c31 10263 save_gnu_tree (gnat_entity, gnu_decl, false);
7ed9919d 10264 if (Is_Incomplete_Or_Private_Type (gnat_entity)
a1ab4c31 10265 && Present (Full_View (gnat_entity)))
65444786
EB
10266 {
10267 if (Has_Completion_In_Body (gnat_entity))
10268 DECL_TAFT_TYPE_P (gnu_decl) = 1;
10269 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
10270 }
a1ab4c31
AC
10271 }
10272
10273 return;
10274 }
10275
d5ebeb8c
EB
10276 /* If we saved away a dummy type for this node, it means that this made the
10277 type that corresponds to the full type of an incomplete type. Clear that
5a4916be
EB
10278 type for now and then update the type in the pointers below. But, if the
10279 saved type is not dummy, it very likely means that we have a use before
10280 declaration for the type in the tree, what we really cannot handle. */
a1ab4c31
AC
10281 if (gnu_old)
10282 {
10283 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
10284 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
10285
10286 save_gnu_tree (gnat_entity, NULL_TREE, false);
10287 }
10288
10289 /* Now fully elaborate the type. */
d5ebeb8c 10290 tree gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, true);
a1ab4c31
AC
10291 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
10292
65444786
EB
10293 /* If we have an old type and we've made pointers to this type, update those
10294 pointers. If this is a Taft amendment type in the main unit, we need to
10295 mark the type as used since other units referencing it don't see the full
10296 declaration and, therefore, cannot mark it as used themselves. */
a1ab4c31 10297 if (gnu_old)
65444786
EB
10298 {
10299 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
10300 TREE_TYPE (gnu_new));
10301 if (DECL_TAFT_TYPE_P (gnu_old))
10302 used_types_insert (TREE_TYPE (gnu_new));
10303 }
a1ab4c31
AC
10304
10305 /* If this is a record type corresponding to a task or protected type
10306 that is a completion of an incomplete type, perform a similar update
1e17ef87 10307 on the type. ??? Including protected types here is a guess. */
4ec7c4ec 10308 if (Is_Record_Type (gnat_entity)
a1ab4c31
AC
10309 && Is_Concurrent_Record_Type (gnat_entity)
10310 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
10311 {
10312 tree gnu_task_old
10313 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
10314
10315 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
10316 NULL_TREE, false);
10317 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
10318 gnu_new, false);
10319
10320 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
10321 TREE_TYPE (gnu_new));
10322 }
10323}
ce2d0ce2 10324
b1b2b511
EB
10325/* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
10326 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting of the
10327 associations that are from RECORD_TYPE. If we see an internal record, make
10328 a recursive call to fill it in as well. */
10329
10330static tree
10331extract_values (tree values, tree record_type)
10332{
10333 vec<constructor_elt, va_gc> *v = NULL;
10334 tree field;
10335
10336 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
10337 {
10338 tree tem, value = NULL_TREE;
10339
10340 /* _Parent is an internal field, but may have values in the aggregate,
10341 so check for values first. */
10342 if ((tem = purpose_member (field, values)))
10343 {
10344 value = TREE_VALUE (tem);
10345 TREE_ADDRESSABLE (tem) = 1;
10346 }
10347
10348 else if (DECL_INTERNAL_P (field))
10349 {
10350 value = extract_values (values, TREE_TYPE (field));
10351 if (TREE_CODE (value) == CONSTRUCTOR
10352 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
10353 value = NULL_TREE;
10354 }
10355 else
10356 /* If we have a record subtype, the names will match, but not the
10357 actual FIELD_DECLs. */
10358 for (tem = values; tem; tem = TREE_CHAIN (tem))
10359 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
10360 {
10361 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
10362 TREE_ADDRESSABLE (tem) = 1;
10363 }
10364
10365 if (!value)
10366 continue;
10367
10368 CONSTRUCTOR_APPEND_ELT (v, field, value);
10369 }
10370
10371 return gnat_build_constructor (record_type, v);
10372}
10373
42acad07
EB
10374/* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
10375 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
10376 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
a1ab4c31
AC
10377
10378static tree
10379assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
10380{
42acad07 10381 tree gnu_list = NULL_TREE, gnu_result;
a1ab4c31
AC
10382
10383 /* We test for GNU_FIELD being empty in the case where a variant
10384 was the last thing since we don't take things off GNAT_ASSOC in
10385 that case. We check GNAT_ASSOC in case we have a variant, but it
10386 has no fields. */
10387
42acad07 10388 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
a1ab4c31 10389 {
815b5368
EB
10390 const Node_Id gnat_field = First (Choices (gnat_assoc));
10391 const Node_Id gnat_expr = Expression (gnat_assoc);
a1ab4c31
AC
10392 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
10393 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
10394
10395 /* The expander is supposed to put a single component selector name
1e17ef87 10396 in every record component association. */
a1ab4c31
AC
10397 gcc_assert (No (Next (gnat_field)));
10398
4ec7c4ec
EB
10399 /* Ignore discriminants that have Corresponding_Discriminants in tagged
10400 types since we'll be setting those fields in the parent subtype. */
10401 if (Ekind (Entity (gnat_field)) == E_Discriminant
10402 && Present (Corresponding_Discriminant (Entity (gnat_field)))
a1ab4c31
AC
10403 && Is_Tagged_Type (Scope (Entity (gnat_field))))
10404 continue;
10405
10406 /* Also ignore discriminants of Unchecked_Unions. */
4ec7c4ec
EB
10407 if (Ekind (Entity (gnat_field)) == E_Discriminant
10408 && Is_Unchecked_Union (gnat_entity))
a1ab4c31
AC
10409 continue;
10410
815b5368 10411 gigi_checking_assert (!Do_Range_Check (gnat_expr));
a1ab4c31 10412
815b5368 10413 /* Convert to the type of the field. */
a1ab4c31
AC
10414 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
10415
10416 /* Add the field and expression to the list. */
10417 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
10418 }
10419
10420 gnu_result = extract_values (gnu_list, gnu_type);
10421
9abe8b74
MM
10422 if (flag_checking)
10423 {
10424 /* Verify that every entry in GNU_LIST was used. */
10425 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
10426 gcc_assert (TREE_ADDRESSABLE (gnu_list));
10427 }
a1ab4c31
AC
10428
10429 return gnu_result;
10430}
10431
1e17ef87
EB
10432/* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
10433 the first element of an array aggregate. It may itself be an aggregate.
815b5368 10434 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate. */
a1ab4c31
AC
10435
10436static tree
815b5368 10437pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type)
a1ab4c31 10438{
a1ab4c31 10439 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
9771b263 10440 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
a1ab4c31 10441
b1b2b511 10442 for (; Present (gnat_expr); gnat_expr = Next (gnat_expr))
a1ab4c31 10443 {
b1b2b511
EB
10444 tree gnu_expr;
10445
a1ab4c31
AC
10446 /* If the expression is itself an array aggregate then first build the
10447 innermost constructor if it is part of our array (multi-dimensional
10448 case). */
a1ab4c31
AC
10449 if (Nkind (gnat_expr) == N_Aggregate
10450 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
10451 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
10452 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
815b5368 10453 TREE_TYPE (gnu_array_type));
a1ab4c31
AC
10454 else
10455 {
babf697a
EB
10456 /* If the expression is a conversion to an unconstrained array type,
10457 skip it to avoid spilling to memory. */
10458 if (Nkind (gnat_expr) == N_Type_Conversion
10459 && Is_Array_Type (Etype (gnat_expr))
10460 && !Is_Constrained (Etype (gnat_expr)))
10461 gnu_expr = gnat_to_gnu (Expression (gnat_expr));
10462 else
10463 gnu_expr = gnat_to_gnu (gnat_expr);
a1ab4c31 10464
815b5368 10465 gigi_checking_assert (!Do_Range_Check (gnat_expr));
a1ab4c31
AC
10466 }
10467
0e228dd9
NF
10468 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
10469 convert (TREE_TYPE (gnu_array_type), gnu_expr));
a1ab4c31 10470
8b9aec86
RS
10471 gnu_index = int_const_binop (PLUS_EXPR, gnu_index,
10472 convert (TREE_TYPE (gnu_index),
10473 integer_one_node));
a1ab4c31
AC
10474 }
10475
0e228dd9 10476 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
a1ab4c31 10477}
ce2d0ce2 10478
f04b8d69
EB
10479/* Process a N_Validate_Unchecked_Conversion node. */
10480
10481static void
10482validate_unchecked_conversion (Node_Id gnat_node)
10483{
10484 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
10485 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
10486
10487 /* If the target is a pointer type, see if we are either converting from a
10488 non-pointer or from a pointer to a type with a different alias set and
10489 warn if so, unless the pointer has been marked to alias everything. */
10490 if (POINTER_TYPE_P (gnu_target_type)
10491 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
10492 {
10493 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
10494 ? TREE_TYPE (gnu_source_type)
10495 : NULL_TREE;
10496 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
10497 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
10498
10499 if (target_alias_set != 0
10500 && (!POINTER_TYPE_P (gnu_source_type)
10501 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
10502 target_alias_set)))
10503 {
4a29b8d6 10504 post_error_ne ("??possible aliasing problem for type&",
f04b8d69
EB
10505 gnat_node, Target_Type (gnat_node));
10506 post_error ("\\?use -fno-strict-aliasing switch for references",
10507 gnat_node);
10508 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
10509 gnat_node, Target_Type (gnat_node));
10510 }
10511 }
10512
10513 /* Likewise if the target is a fat pointer type, but we have no mechanism to
10514 mitigate the problem in this case, so we unconditionally warn. */
10515 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
10516 {
10517 tree gnu_source_desig_type
10518 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
10519 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
10520 : NULL_TREE;
10521 tree gnu_target_desig_type
10522 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
10523 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
10524
10525 if (target_alias_set != 0
10526 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
10527 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
10528 target_alias_set)))
10529 {
4a29b8d6 10530 post_error_ne ("??possible aliasing problem for type&",
f04b8d69
EB
10531 gnat_node, Target_Type (gnat_node));
10532 post_error ("\\?use -fno-strict-aliasing switch for references",
10533 gnat_node);
10534 }
10535 }
10536}
ce2d0ce2 10537
fa6fd7b7
AO
10538/* Convert SLOC into LOCUS. Return true if SLOC corresponds to a
10539 source code location and false if it doesn't. If CLEAR_COLUMN is
10540 true, set the column information to 0. If DECL is given and SLOC
10541 refers to a File with an instance, map DECL to that instance. */
a1ab4c31 10542
ba464315 10543bool
fa6fd7b7
AO
10544Sloc_to_locus (Source_Ptr Sloc, location_t *locus, bool clear_column,
10545 const_tree decl)
a1ab4c31
AC
10546{
10547 if (Sloc == No_Location)
10548 return false;
10549
10550 if (Sloc <= Standard_Location)
10551 {
10069d53 10552 *locus = BUILTINS_LOCATION;
a1ab4c31
AC
10553 return false;
10554 }
a1ab4c31 10555
ba464315 10556 Source_File_Index file = Get_Source_File_Index (Sloc);
b67e2ad8
EB
10557 Line_Number_Type line = Get_Logical_Line_Number (Sloc);
10558 Column_Number_Type column = (clear_column ? 0 : Get_Column_Number (Sloc));
ba464315 10559 line_map_ordinary *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
b7562769 10560
ba464315
EB
10561 /* We can have zero if pragma Source_Reference is in effect. */
10562 if (line < 1)
10563 line = 1;
a1ab4c31 10564
ba464315 10565 /* Translate the location. */
b1b2b511
EB
10566 *locus
10567 = linemap_position_for_line_and_column (line_table, map, line, column);
a1ab4c31 10568
025e3d57 10569 if (decl && file_map && file_map[file - 1].Instance)
fa6fd7b7
AO
10570 decl_to_instance_map->put (decl, file_map[file - 1].Instance);
10571
a1ab4c31
AC
10572 return true;
10573}
10574
56b8aa0c
EB
10575/* Return whether GNAT_NODE is a defining identifier for a renaming that comes
10576 from the parameter association for the instantiation of a generic. We do
10577 not want to emit source location for them: the code generated for their
10578 initialization is likely to disturb debugging. */
10579
10580bool
10581renaming_from_instantiation_p (Node_Id gnat_node)
10582{
10583 if (Nkind (gnat_node) != N_Defining_Identifier
10584 || !Is_Object (gnat_node)
10585 || Comes_From_Source (gnat_node)
10586 || !Present (Renamed_Object (gnat_node)))
10587 return false;
10588
10589 /* Get the object declaration of the renamed object, if any and if the
10590 renamed object is a mere identifier. */
10591 gnat_node = Renamed_Object (gnat_node);
10592 if (Nkind (gnat_node) != N_Identifier)
10593 return false;
10594
10595 gnat_node = Parent (Entity (gnat_node));
10596 return (Present (gnat_node)
10597 && Nkind (gnat_node) == N_Object_Declaration
10598 && Present (Corresponding_Generic_Association (gnat_node)));
10599}
10600
a1ab4c31 10601/* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
ba464315
EB
10602 don't do anything if it doesn't correspond to a source location. And,
10603 if CLEAR_COLUMN is true, set the column information to 0. */
a1ab4c31
AC
10604
10605static void
ba464315 10606set_expr_location_from_node (tree node, Node_Id gnat_node, bool clear_column)
a1ab4c31
AC
10607{
10608 location_t locus;
10609
56b8aa0c
EB
10610 /* Do not set a location for constructs likely to disturb debugging. */
10611 if (Nkind (gnat_node) == N_Defining_Identifier)
10612 {
10613 if (Is_Type (gnat_node) && Is_Actual_Subtype (gnat_node))
10614 return;
10615
10616 if (renaming_from_instantiation_p (gnat_node))
10617 return;
10618 }
10619
ba464315 10620 if (!Sloc_to_locus (Sloc (gnat_node), &locus, clear_column))
a1ab4c31
AC
10621 return;
10622
10623 SET_EXPR_LOCATION (node, locus);
10624}
17c168fe
EB
10625
10626/* More elaborate version of set_expr_location_from_node to be used in more
10627 general contexts, for example the result of the translation of a generic
10628 GNAT node. */
10629
10630static void
10631set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
10632{
10633 /* Set the location information on the node if it is a real expression.
10634 References can be reused for multiple GNAT nodes and they would get
10635 the location information of their last use. Also make sure not to
10636 overwrite an existing location as it is probably more precise. */
10637
10638 switch (TREE_CODE (node))
10639 {
10640 CASE_CONVERT:
10641 case NON_LVALUE_EXPR:
384e3fb1 10642 case SAVE_EXPR:
17c168fe
EB
10643 break;
10644
10645 case COMPOUND_EXPR:
10646 if (EXPR_P (TREE_OPERAND (node, 1)))
10647 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
10648
9c453de7 10649 /* ... fall through ... */
17c168fe
EB
10650
10651 default:
10652 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
2a02d090
OH
10653 {
10654 set_expr_location_from_node (node, gnat_node);
10655 set_end_locus_from_node (node, gnat_node);
10656 }
17c168fe
EB
10657 break;
10658 }
10659}
ba464315
EB
10660
10661/* Set the end_locus information for GNU_NODE, if any, from an explicit end
10662 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
10663 most sense. Return true if a sensible assignment was performed. */
10664
10665static bool
10666set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
10667{
10668 Node_Id gnat_end_label;
10669 location_t end_locus;
10670
10671 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
10672 end_locus when there is one. We consider only GNAT nodes with a possible
10673 End_Label attached. If the End_Label actually was unassigned, fallback
10674 on the original node. We'd better assign an explicit sloc associated with
10675 the outer construct in any case. */
10676
10677 switch (Nkind (gnat_node))
10678 {
10679 case N_Package_Body:
10680 case N_Subprogram_Body:
10681 case N_Block_Statement:
650ad524
BD
10682 if (Present (Handled_Statement_Sequence (gnat_node)))
10683 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
10684 else
10685 gnat_end_label = Empty;
ba464315
EB
10686 break;
10687
10688 case N_Package_Declaration:
650ad524 10689 gcc_checking_assert (Present (Specification (gnat_node)));
ba464315
EB
10690 gnat_end_label = End_Label (Specification (gnat_node));
10691 break;
10692
10693 default:
10694 return false;
10695 }
10696
10697 if (Present (gnat_end_label))
10698 gnat_node = gnat_end_label;
10699
10700 /* Some expanded subprograms have neither an End_Label nor a Sloc
10701 attached. Notify that to callers. For a block statement with no
10702 End_Label, clear column information, so that the tree for a
10703 transient block does not receive the sloc of a source condition. */
10704 if (!Sloc_to_locus (Sloc (gnat_node), &end_locus,
10705 No (gnat_end_label)
0b66f882 10706 && Nkind (gnat_node) == N_Block_Statement))
ba464315
EB
10707 return false;
10708
10709 switch (TREE_CODE (gnu_node))
10710 {
10711 case BIND_EXPR:
10712 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
10713 return true;
10714
10715 case FUNCTION_DECL:
10716 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
10717 return true;
10718
10719 default:
10720 return false;
10721 }
10722}
ce2d0ce2 10723
a1ab4c31
AC
10724/* Post an error message. MSG is the error message, properly annotated.
10725 NODE is the node at which to post the error and the node to use for the
586388fd 10726 '&' substitution. */
a1ab4c31
AC
10727
10728void
10729post_error (const char *msg, Node_Id node)
10730{
10731 String_Template temp;
93582885 10732 String_Pointer sp;
a1ab4c31 10733
457f72ac
EB
10734 if (No (node))
10735 return;
10736
10737 temp.Low_Bound = 1;
10738 temp.High_Bound = strlen (msg);
93582885
EB
10739 sp.Bounds = &temp;
10740 sp.Array = msg;
10741 Error_Msg_N (sp, node);
a1ab4c31
AC
10742}
10743
586388fd
EB
10744/* Similar to post_error, but NODE is the node at which to post the error and
10745 ENT is the node to use for the '&' substitution. */
a1ab4c31
AC
10746
10747void
10748post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
10749{
10750 String_Template temp;
93582885 10751 String_Pointer sp;
a1ab4c31 10752
457f72ac
EB
10753 if (No (node))
10754 return;
10755
10756 temp.Low_Bound = 1;
10757 temp.High_Bound = strlen (msg);
93582885
EB
10758 sp.Bounds = &temp;
10759 sp.Array = msg;
10760 Error_Msg_NE (sp, node, ent);
a1ab4c31
AC
10761}
10762
586388fd 10763/* Similar to post_error_ne, but NUM is the number to use for the '^'. */
a1ab4c31
AC
10764
10765void
58c8f770 10766post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
a1ab4c31 10767{
58c8f770 10768 Error_Msg_Uint_1 = UI_From_Int (num);
586388fd 10769 post_error_ne (msg, node, ent);
a1ab4c31 10770}
2a02d090 10771
586388fd
EB
10772/* Similar to post_error_ne, but T is a GCC tree representing the number to
10773 write. If T represents a constant, the text inside curly brackets in
10774 MSG will be output (presumably including a '^'). Otherwise it will not
10775 be output and the text inside square brackets will be output instead. */
a1ab4c31
AC
10776
10777void
10778post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
10779{
586388fd 10780 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
a1ab4c31
AC
10781 char start_yes, end_yes, start_no, end_no;
10782 const char *p;
10783 char *q;
10784
586388fd 10785 if (TREE_CODE (t) == INTEGER_CST)
a1ab4c31 10786 {
586388fd 10787 Error_Msg_Uint_1 = UI_From_gnu (t);
a1ab4c31
AC
10788 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
10789 }
10790 else
10791 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
10792
586388fd 10793 for (p = msg, q = new_msg; *p; p++)
a1ab4c31
AC
10794 {
10795 if (*p == start_yes)
10796 for (p++; *p != end_yes; p++)
10797 *q++ = *p;
10798 else if (*p == start_no)
10799 for (p++; *p != end_no; p++)
10800 ;
10801 else
10802 *q++ = *p;
10803 }
10804
10805 *q = 0;
10806
586388fd 10807 post_error_ne (new_msg, node, ent);
a1ab4c31
AC
10808}
10809
586388fd 10810/* Similar to post_error_ne_tree, but NUM is a second integer to write. */
a1ab4c31
AC
10811
10812void
1e17ef87
EB
10813post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
10814 int num)
a1ab4c31
AC
10815{
10816 Error_Msg_Uint_2 = UI_From_Int (num);
10817 post_error_ne_tree (msg, node, ent, t);
10818}
7c775aca 10819
8f8f531f 10820/* Return a label to branch to for the exception type in KIND or Empty
7c775aca
EB
10821 if none. */
10822
8f8f531f 10823Entity_Id
7c775aca
EB
10824get_exception_label (char kind)
10825{
10826 switch (kind)
10827 {
10828 case N_Raise_Constraint_Error:
8f8f531f 10829 return gnu_constraint_error_label_stack.last ();
7c775aca
EB
10830
10831 case N_Raise_Storage_Error:
8f8f531f 10832 return gnu_storage_error_label_stack.last ();
7c775aca
EB
10833
10834 case N_Raise_Program_Error:
8f8f531f 10835 return gnu_program_error_label_stack.last ();
7c775aca
EB
10836
10837 default:
8f8f531f 10838 return Empty;
7c775aca
EB
10839 }
10840
8f8f531f 10841 gcc_unreachable ();
7c775aca
EB
10842}
10843
10844/* Return the decl for the current elaboration procedure. */
10845
7543c8b6 10846static tree
7c775aca
EB
10847get_elaboration_procedure (void)
10848{
10849 return gnu_elab_proc_stack->last ();
10850}
10851
44662f68
EB
10852/* Return the controlling type of a dispatching subprogram. */
10853
10854static Entity_Id
10855get_controlling_type (Entity_Id subprog)
10856{
17a98a3d 10857 /* This is modeled on Expand_Interface_Thunk. */
44662f68
EB
10858 Entity_Id controlling_type = Etype (First_Formal (subprog));
10859 if (Is_Access_Type (controlling_type))
10860 controlling_type = Directly_Designated_Type (controlling_type);
10861 controlling_type = Underlying_Type (controlling_type);
10862 if (Is_Concurrent_Type (controlling_type))
10863 controlling_type = Corresponding_Record_Type (controlling_type);
10864 controlling_type = Base_Type (controlling_type);
10865 return controlling_type;
10866}
10867
10868/* Return whether we should use an alias for the TARGET of a thunk
10869 in order to make the call generated in the thunk local. */
10870
10871static bool
10872use_alias_for_thunk_p (tree target)
10873{
10874 /* We cannot generate a local call in this case. */
10875 if (DECL_EXTERNAL (target))
10876 return false;
10877
10878 /* The call is already local in this case. */
10879 if (TREE_CODE (DECL_CONTEXT (target)) == FUNCTION_DECL)
10880 return false;
10881
10882 return TARGET_USE_LOCAL_THUNK_ALIAS_P (target);
10883}
10884
10885static GTY(()) unsigned long thunk_labelno = 0;
10886
10887/* Create an alias for TARGET to be used as the target of a thunk. */
10888
10889static tree
10890make_alias_for_thunk (tree target)
10891{
10892 char buf[64];
10893 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno++);
10894
10895 tree alias = build_decl (DECL_SOURCE_LOCATION (target), TREE_CODE (target),
10896 get_identifier (buf), TREE_TYPE (target));
10897
10898 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
10899 DECL_CONTEXT (alias) = DECL_CONTEXT (target);
10900 TREE_READONLY (alias) = TREE_READONLY (target);
10901 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
10902 DECL_ARTIFICIAL (alias) = 1;
10903 DECL_INITIAL (alias) = error_mark_node;
10904 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
10905 TREE_ADDRESSABLE (alias) = 1;
10906 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
10907
10908 cgraph_node *n = cgraph_node::create_same_body_alias (alias, target);
10909 gcc_assert (n);
10910
10911 return alias;
10912}
10913
5d01fc7c 10914/* Create the local covariant part of {GNAT,GNU}_THUNK. */
44662f68
EB
10915
10916static tree
10917make_covariant_thunk (Entity_Id gnat_thunk, tree gnu_thunk)
10918{
10919 tree gnu_name = create_concat_name (gnat_thunk, "CV");
10920 tree gnu_cv_thunk
10921 = build_decl (DECL_SOURCE_LOCATION (gnu_thunk), TREE_CODE (gnu_thunk),
10922 gnu_name, TREE_TYPE (gnu_thunk));
10923
10924 DECL_ARGUMENTS (gnu_cv_thunk) = copy_list (DECL_ARGUMENTS (gnu_thunk));
5d01fc7c
EB
10925 for (tree param_decl = DECL_ARGUMENTS (gnu_cv_thunk);
10926 param_decl;
10927 param_decl = DECL_CHAIN (param_decl))
10928 DECL_CONTEXT (param_decl) = gnu_cv_thunk;
10929
44662f68
EB
10930 DECL_RESULT (gnu_cv_thunk) = copy_node (DECL_RESULT (gnu_thunk));
10931 DECL_CONTEXT (DECL_RESULT (gnu_cv_thunk)) = gnu_cv_thunk;
10932
10933 DECL_LANG_SPECIFIC (gnu_cv_thunk) = DECL_LANG_SPECIFIC (gnu_thunk);
10934 DECL_CONTEXT (gnu_cv_thunk) = DECL_CONTEXT (gnu_thunk);
10935 TREE_READONLY (gnu_cv_thunk) = TREE_READONLY (gnu_thunk);
10936 TREE_THIS_VOLATILE (gnu_cv_thunk) = TREE_THIS_VOLATILE (gnu_thunk);
44662f68
EB
10937 DECL_ARTIFICIAL (gnu_cv_thunk) = 1;
10938
10939 return gnu_cv_thunk;
10940}
10941
10942/* Try to create a GNU thunk for {GNAT,GNU}_THUNK and return true on success.
10943
10944 GNU thunks are more efficient than GNAT thunks because they don't call into
10945 the runtime to retrieve the offset used in the displacement operation, but
10946 they are tailored to C++ and thus too limited to support the full range of
10947 thunks generated in Ada. Here's the complete list of limitations:
10948
10949 1. Multi-controlling thunks, i.e thunks with more than one controlling
10950 parameter, are simply not supported.
10951
10952 2. Covariant thunks, i.e. thunks for which the result is also controlling,
10953 are split into a pair of (this, covariant-only) thunks.
10954
10955 3. Variable-offset thunks, i.e. thunks for which the offset depends on the
10956 object and not only on its type, are supported as 2nd class citizens.
10957
10958 4. External thunks, i.e. thunks for which the target is not declared in
10959 the same unit as the thunk, are supported as 2nd class citizens.
10960
10961 5. Local thunks, i.e. thunks generated for a local type, are supported as
10962 2nd class citizens. */
10963
10964static bool
10965maybe_make_gnu_thunk (Entity_Id gnat_thunk, tree gnu_thunk)
10966{
89e037d0
EB
10967 /* We use the Thunk_Target to compute the properties of the thunk. */
10968 const Entity_Id gnat_target = Thunk_Target (gnat_thunk);
44662f68
EB
10969
10970 /* Check that the first formal of the target is the only controlling one. */
10971 Entity_Id gnat_formal = First_Formal (gnat_target);
10972 if (!Is_Controlling_Formal (gnat_formal))
10973 return false;
10974 for (gnat_formal = Next_Formal (gnat_formal);
10975 Present (gnat_formal);
10976 gnat_formal = Next_Formal (gnat_formal))
10977 if (Is_Controlling_Formal (gnat_formal))
10978 return false;
10979
10980 /* Look for the types that control the target and the thunk. */
10981 const Entity_Id gnat_controlling_type = get_controlling_type (gnat_target);
10982 const Entity_Id gnat_interface_type = get_controlling_type (gnat_thunk);
10983
20dc266e
JM
10984 /* We must have an interface type at this point. */
10985 gcc_assert (Is_Interface (gnat_interface_type));
10986
44662f68
EB
10987 /* Now compute whether the former covers the latter. */
10988 const Entity_Id gnat_interface_tag
20dc266e 10989 = Find_Interface_Tag (gnat_controlling_type, gnat_interface_type);
44662f68
EB
10990 tree gnu_interface_tag
10991 = Present (gnat_interface_tag)
10992 ? gnat_to_gnu_field_decl (gnat_interface_tag)
10993 : NULL_TREE;
10994 tree gnu_interface_offset
10995 = gnu_interface_tag ? byte_position (gnu_interface_tag) : NULL_TREE;
10996
10997 /* There are three ways to retrieve the offset between the interface view
10998 and the base object. Either the controlling type covers the interface
10999 type and the offset of the corresponding tag is fixed, in which case it
11000 can be statically encoded in the thunk (see FIXED_OFFSET below). Or the
11001 controlling type doesn't cover the interface type but is of fixed size,
11002 in which case the offset is stored in the dispatch table, two pointers
11003 above the dispatch table address (see VIRTUAL_VALUE below). Otherwise,
11004 the offset is variable and is stored right after the tag in every object
11005 (see INDIRECT_OFFSET below). See also a-tags.ads for more details. */
11006 HOST_WIDE_INT fixed_offset, virtual_value, indirect_offset;
11007 tree virtual_offset;
11008
11009 if (gnu_interface_offset && TREE_CODE (gnu_interface_offset) == INTEGER_CST)
11010 {
11011 fixed_offset = - tree_to_shwi (gnu_interface_offset);
11012 virtual_value = 0;
11013 virtual_offset = NULL_TREE;
11014 indirect_offset = 0;
11015 }
11016 else if (!gnu_interface_offset
11017 && !Is_Variable_Size_Record (gnat_controlling_type))
11018 {
11019 fixed_offset = 0;
11020 virtual_value = - 2 * (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT);
11021 virtual_offset = build_int_cst (integer_type_node, virtual_value);
11022 indirect_offset = 0;
11023 }
11024 else
11025 {
11026 /* Covariant thunks with variable offset are not supported. */
11027 if (Has_Controlling_Result (gnat_target))
11028 return false;
11029
11030 fixed_offset = 0;
11031 virtual_value = 0;
11032 virtual_offset = NULL_TREE;
11033 indirect_offset = (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT);
11034 }
11035
89e037d0
EB
11036 /* But we generate a call to the Thunk_Entity in the thunk. */
11037 tree gnu_target
11038 = gnat_to_gnu_entity (Thunk_Entity (gnat_thunk), NULL_TREE, false);
44662f68 11039
67c4d1c7
EB
11040 /* If the target is local, then thunk and target must have the same context
11041 because cgraph_node::expand_thunk can only forward the static chain. */
11042 if (DECL_STATIC_CHAIN (gnu_target)
11043 && DECL_CONTEXT (gnu_thunk) != DECL_CONTEXT (gnu_target))
11044 return false;
44662f68
EB
11045
11046 /* If the target returns by invisible reference and is external, apply the
11047 same transformation as Subprogram_Body_to_gnu here. */
11048 if (TREE_ADDRESSABLE (TREE_TYPE (gnu_target))
11049 && DECL_EXTERNAL (gnu_target)
11050 && !POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (gnu_target))))
11051 {
11052 TREE_TYPE (DECL_RESULT (gnu_target))
11053 = build_reference_type (TREE_TYPE (DECL_RESULT (gnu_target)));
11054 relayout_decl (DECL_RESULT (gnu_target));
11055 }
11056
11057 /* The thunk expander requires the return types of thunk and target to be
11058 compatible, which is not fully the case with the CICO mechanism. */
11059 if (TYPE_CI_CO_LIST (TREE_TYPE (gnu_thunk)))
11060 {
11061 tree gnu_target_type = TREE_TYPE (gnu_target);
11062 gcc_assert (TYPE_CI_CO_LIST (gnu_target_type));
11063 TYPE_CANONICAL (TREE_TYPE (TREE_TYPE (gnu_thunk)))
11064 = TYPE_CANONICAL (TREE_TYPE (gnu_target_type));
11065 }
11066
11067 cgraph_node *target_node = cgraph_node::get_create (gnu_target);
11068
5d01fc7c
EB
11069 /* We may also need to create an alias for the target in order to make
11070 the call local, depending on the linkage of the target. */
11071 tree gnu_alias = use_alias_for_thunk_p (gnu_target)
11072 ? make_alias_for_thunk (gnu_target)
11073 : gnu_target;
11074
44662f68
EB
11075 /* If the return type of the target is a controlling type, then we need
11076 both an usual this thunk and a covariant thunk in this order:
11077
11078 this thunk --> covariant thunk --> target
11079
11080 For covariant thunks, we can only handle a fixed offset. */
11081 if (Has_Controlling_Result (gnat_target))
11082 {
11083 gcc_assert (fixed_offset < 0);
11084 tree gnu_cv_thunk = make_covariant_thunk (gnat_thunk, gnu_thunk);
11085 target_node->create_thunk (gnu_cv_thunk, gnu_target, false,
11086 - fixed_offset, 0, 0,
5d01fc7c 11087 NULL_TREE, gnu_alias);
44662f68 11088
5d01fc7c 11089 gnu_alias = gnu_target = gnu_cv_thunk;
44662f68
EB
11090 }
11091
44662f68
EB
11092 target_node->create_thunk (gnu_thunk, gnu_target, true,
11093 fixed_offset, virtual_value, indirect_offset,
11094 virtual_offset, gnu_alias);
11095
11096 return true;
11097}
11098
a1ab4c31
AC
11099/* Initialize the table that maps GNAT codes to GCC codes for simple
11100 binary and unary operations. */
11101
11102static void
11103init_code_table (void)
11104{
a1ab4c31
AC
11105 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
11106 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
11107 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
11108 gnu_codes[N_Op_Eq] = EQ_EXPR;
11109 gnu_codes[N_Op_Ne] = NE_EXPR;
11110 gnu_codes[N_Op_Lt] = LT_EXPR;
11111 gnu_codes[N_Op_Le] = LE_EXPR;
11112 gnu_codes[N_Op_Gt] = GT_EXPR;
11113 gnu_codes[N_Op_Ge] = GE_EXPR;
11114 gnu_codes[N_Op_Add] = PLUS_EXPR;
11115 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
11116 gnu_codes[N_Op_Multiply] = MULT_EXPR;
11117 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
11118 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
11119 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
11120 gnu_codes[N_Op_Abs] = ABS_EXPR;
11121 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
11122 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
11123 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
11124 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
11125 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
11126 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
9a1bdc31
EB
11127 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
11128 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
a1ab4c31
AC
11129}
11130
a1ab4c31 11131#include "gt-ada-trans.h"
This page took 8.064067 seconds and 5 git commands to generate.