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