Debian Codegraph @

gcc/ada/gcc-interface/trans.cc

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