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