d4c9c85a9894797317cbbd54f534c19e6c2af181
[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 gnu_result = build_binary_op (attribute == Attr_Min
2272 ? MIN_EXPR : MAX_EXPR,
2273 gnu_result_type, gnu_lhs, gnu_rhs);
2274 }
2275 break;
2276
2277 case Attr_Passed_By_Reference:
2278 gnu_result = size_int (default_pass_by_ref (gnu_type)
2279 || must_pass_by_ref (gnu_type));
2280 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2281 break;
2282
2283 case Attr_Component_Size:
2284 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
2285 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
2286 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
2287
2288 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2289 gnu_type = TREE_TYPE (gnu_prefix);
2290
2291 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
2292 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
2293
2294 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2295 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2296 gnu_type = TREE_TYPE (gnu_type);
2297
2298 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2299
2300 /* Note this size cannot be self-referential. */
2301 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
2302 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2303 prefix_unused = true;
2304 break;
2305
2306 case Attr_Descriptor_Size:
2307 gnu_type = TREE_TYPE (gnu_prefix);
2308 gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE);
2309
2310 /* What we want is the offset of the ARRAY field in the record
2311 that the thin pointer designates. */
2312 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
2313 gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
2314 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2315 prefix_unused = true;
2316 break;
2317
2318 case Attr_Null_Parameter:
2319 /* This is just a zero cast to the pointer type for our prefix and
2320 dereferenced. */
2321 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2322 gnu_result
2323 = build_unary_op (INDIRECT_REF, NULL_TREE,
2324 convert (build_pointer_type (gnu_result_type),
2325 integer_zero_node));
2326 TREE_PRIVATE (gnu_result) = 1;
2327 break;
2328
2329 case Attr_Mechanism_Code:
2330 {
2331 Entity_Id gnat_obj = Entity (gnat_prefix);
2332 int code;
2333
2334 prefix_unused = true;
2335 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2336 if (Present (Expressions (gnat_node)))
2337 {
2338 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
2339
2340 for (gnat_obj = First_Formal (gnat_obj); i > 1;
2341 i--, gnat_obj = Next_Formal (gnat_obj))
2342 ;
2343 }
2344
2345 code = Mechanism (gnat_obj);
2346 if (code == Default)
2347 code = ((present_gnu_tree (gnat_obj)
2348 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
2349 || ((TREE_CODE (get_gnu_tree (gnat_obj))
2350 == PARM_DECL)
2351 && (DECL_BY_COMPONENT_PTR_P
2352 (get_gnu_tree (gnat_obj))))))
2353 ? By_Reference : By_Copy);
2354 gnu_result = convert (gnu_result_type, size_int (- code));
2355 }
2356 break;
2357
2358 case Attr_Model:
2359 /* We treat Model as identical to Machine. This is true for at least
2360 IEEE and some other nice floating-point systems. */
2361
2362 /* ... fall through ... */
2363
2364 case Attr_Machine:
2365 /* The trick is to force the compiler to store the result in memory so
2366 that we do not have extra precision used. But do this only when this
2367 is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of
2368 the type is lower than that of the longest floating-point type. */
2369 prefix_unused = true;
2370 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2371 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2372 gnu_result = convert (gnu_result_type, gnu_expr);
2373
2374 if (fp_arith_may_widen
2375 && TYPE_PRECISION (gnu_result_type)
2376 < TYPE_PRECISION (longest_float_type_node))
2377 {
2378 tree rec_type = make_node (RECORD_TYPE);
2379 tree field
2380 = create_field_decl (get_identifier ("OBJ"), gnu_result_type,
2381 rec_type, NULL_TREE, NULL_TREE, 0, 0);
2382 tree rec_val, asm_expr;
2383
2384 finish_record_type (rec_type, field, 0, false);
2385
2386 rec_val = build_constructor_single (rec_type, field, gnu_result);
2387 rec_val = save_expr (rec_val);
2388
2389 asm_expr
2390 = build5 (ASM_EXPR, void_type_node,
2391 build_string (0, ""),
2392 tree_cons (build_tree_list (NULL_TREE,
2393 build_string (2, "=m")),
2394 rec_val, NULL_TREE),
2395 tree_cons (build_tree_list (NULL_TREE,
2396 build_string (1, "m")),
2397 rec_val, NULL_TREE),
2398 NULL_TREE, NULL_TREE);
2399 ASM_VOLATILE_P (asm_expr) = 1;
2400
2401 gnu_result
2402 = build_compound_expr (gnu_result_type, asm_expr,
2403 build_component_ref (rec_val, NULL_TREE,
2404 field, false));
2405 }
2406 break;
2407
2408 default:
2409 /* This abort means that we have an unimplemented attribute. */
2410 gcc_unreachable ();
2411 }
2412
2413 /* If this is an attribute where the prefix was unused, force a use of it if
2414 it has a side-effect. But don't do it if the prefix is just an entity
2415 name. However, if an access check is needed, we must do it. See second
2416 example in AARM 11.6(5.e). */
2417 if (prefix_unused
2418 && TREE_SIDE_EFFECTS (gnu_prefix)
2419 && !Is_Entity_Name (gnat_prefix))
2420 gnu_result
2421 = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result);
2422
2423 *gnu_result_type_p = gnu_result_type;
2424 return gnu_result;
2425 }
2426 \f
2427 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
2428 to a GCC tree, which is returned. */
2429
2430 static tree
2431 Case_Statement_to_gnu (Node_Id gnat_node)
2432 {
2433 tree gnu_result, gnu_expr, gnu_label;
2434 Node_Id gnat_when;
2435 location_t end_locus;
2436 bool may_fallthru = false;
2437
2438 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2439 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2440
2441 /* The range of values in a case statement is determined by the rules in
2442 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
2443 of the expression. One exception arises in the case of a simple name that
2444 is parenthesized. This still has the Etype of the name, but since it is
2445 not a name, para 7 does not apply, and we need to go to the base type.
2446 This is the only case where parenthesization affects the dynamic
2447 semantics (i.e. the range of possible values at run time that is covered
2448 by the others alternative).
2449
2450 Another exception is if the subtype of the expression is non-static. In
2451 that case, we also have to use the base type. */
2452 if (Paren_Count (Expression (gnat_node)) != 0
2453 || !Is_OK_Static_Subtype (Underlying_Type
2454 (Etype (Expression (gnat_node)))))
2455 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2456
2457 /* We build a SWITCH_EXPR that contains the code with interspersed
2458 CASE_LABEL_EXPRs for each label. */
2459 if (!Sloc_to_locus (End_Location (gnat_node), &end_locus))
2460 end_locus = input_location;
2461 gnu_label = create_artificial_label (end_locus);
2462 start_stmt_group ();
2463
2464 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2465 Present (gnat_when);
2466 gnat_when = Next_Non_Pragma (gnat_when))
2467 {
2468 bool choices_added_p = false;
2469 Node_Id gnat_choice;
2470
2471 /* First compile all the different case choices for the current WHEN
2472 alternative. */
2473 for (gnat_choice = First (Discrete_Choices (gnat_when));
2474 Present (gnat_choice);
2475 gnat_choice = Next (gnat_choice))
2476 {
2477 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2478 tree label = create_artificial_label (input_location);
2479
2480 switch (Nkind (gnat_choice))
2481 {
2482 case N_Range:
2483 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
2484 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
2485 break;
2486
2487 case N_Subtype_Indication:
2488 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
2489 (Constraint (gnat_choice))));
2490 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
2491 (Constraint (gnat_choice))));
2492 break;
2493
2494 case N_Identifier:
2495 case N_Expanded_Name:
2496 /* This represents either a subtype range or a static value of
2497 some kind; Ekind says which. */
2498 if (IN (Ekind (Entity (gnat_choice)), Type_Kind))
2499 {
2500 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
2501
2502 gnu_low = TYPE_MIN_VALUE (gnu_type);
2503 gnu_high = TYPE_MAX_VALUE (gnu_type);
2504 break;
2505 }
2506
2507 /* ... fall through ... */
2508
2509 case N_Character_Literal:
2510 case N_Integer_Literal:
2511 gnu_low = gnat_to_gnu (gnat_choice);
2512 break;
2513
2514 case N_Others_Choice:
2515 break;
2516
2517 default:
2518 gcc_unreachable ();
2519 }
2520
2521 /* Everything should be folded into constants at this point. */
2522 gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST);
2523 gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST);
2524
2525 add_stmt_with_node (build_case_label (gnu_low, gnu_high, label),
2526 gnat_choice);
2527 choices_added_p = true;
2528 }
2529
2530 /* This construct doesn't define a scope so we shouldn't push a binding
2531 level around the statement list. Except that we have always done so
2532 historically and this makes it possible to reduce stack usage. As a
2533 compromise, we keep doing it for case statements, for which this has
2534 never been problematic, but not for case expressions in Ada 2012. */
2535 if (choices_added_p)
2536 {
2537 const bool is_case_expression
2538 = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions);
2539 tree group
2540 = build_stmt_group (Statements (gnat_when), !is_case_expression);
2541 bool group_may_fallthru = block_may_fallthru (group);
2542 add_stmt (group);
2543 if (group_may_fallthru)
2544 {
2545 tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label);
2546 SET_EXPR_LOCATION (stmt, end_locus);
2547 add_stmt (stmt);
2548 may_fallthru = true;
2549 }
2550 }
2551 }
2552
2553 /* Now emit a definition of the label the cases branch to, if any. */
2554 if (may_fallthru)
2555 add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label));
2556 gnu_result = build3 (SWITCH_EXPR, TREE_TYPE (gnu_expr), gnu_expr,
2557 end_stmt_group (), NULL_TREE);
2558
2559 return gnu_result;
2560 }
2561 \f
2562 /* Find out whether VAR is an iteration variable of an enclosing loop in the
2563 current function. If so, push a range_check_info structure onto the stack
2564 of this enclosing loop and return it. Otherwise, return NULL. */
2565
2566 static struct range_check_info_d *
2567 push_range_check_info (tree var)
2568 {
2569 struct loop_info_d *iter = NULL;
2570 unsigned int i;
2571
2572 var = remove_conversions (var, false);
2573
2574 if (TREE_CODE (var) != VAR_DECL)
2575 return NULL;
2576
2577 if (decl_function_context (var) != current_function_decl)
2578 return NULL;
2579
2580 gcc_assert (vec_safe_length (gnu_loop_stack) > 0);
2581
2582 for (i = vec_safe_length (gnu_loop_stack) - 1;
2583 vec_safe_iterate (gnu_loop_stack, i, &iter);
2584 i--)
2585 if (var == iter->loop_var)
2586 break;
2587
2588 if (iter)
2589 {
2590 struct range_check_info_d *rci = ggc_alloc<range_check_info_d> ();
2591 vec_safe_push (iter->checks, rci);
2592 return rci;
2593 }
2594
2595 return NULL;
2596 }
2597
2598 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2599 false, or the maximum value if MAX is true, of TYPE. */
2600
2601 static bool
2602 can_equal_min_or_max_val_p (tree val, tree type, bool max)
2603 {
2604 tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
2605
2606 if (TREE_CODE (min_or_max_val) != INTEGER_CST)
2607 return true;
2608
2609 if (TREE_CODE (val) == NOP_EXPR)
2610 val = (max
2611 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))
2612 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))));
2613
2614 if (TREE_CODE (val) != INTEGER_CST)
2615 return true;
2616
2617 if (max)
2618 return tree_int_cst_lt (val, min_or_max_val) == 0;
2619 else
2620 return tree_int_cst_lt (min_or_max_val, val) == 0;
2621 }
2622
2623 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2624 If REVERSE is true, minimum value is taken as maximum value. */
2625
2626 static inline bool
2627 can_equal_min_val_p (tree val, tree type, bool reverse)
2628 {
2629 return can_equal_min_or_max_val_p (val, type, reverse);
2630 }
2631
2632 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2633 If REVERSE is true, maximum value is taken as minimum value. */
2634
2635 static inline bool
2636 can_equal_max_val_p (tree val, tree type, bool reverse)
2637 {
2638 return can_equal_min_or_max_val_p (val, type, !reverse);
2639 }
2640
2641 /* Return true if VAL1 can be lower than VAL2. */
2642
2643 static bool
2644 can_be_lower_p (tree val1, tree val2)
2645 {
2646 if (TREE_CODE (val1) == NOP_EXPR)
2647 val1 = TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val1, 0)));
2648
2649 if (TREE_CODE (val1) != INTEGER_CST)
2650 return true;
2651
2652 if (TREE_CODE (val2) == NOP_EXPR)
2653 val2 = TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val2, 0)));
2654
2655 if (TREE_CODE (val2) != INTEGER_CST)
2656 return true;
2657
2658 return tree_int_cst_lt (val1, val2);
2659 }
2660
2661 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
2662 to a GCC tree, which is returned. */
2663
2664 static tree
2665 Loop_Statement_to_gnu (Node_Id gnat_node)
2666 {
2667 const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
2668 struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> ();
2669 tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE,
2670 NULL_TREE, NULL_TREE, NULL_TREE);
2671 tree gnu_loop_label = create_artificial_label (input_location);
2672 tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2673 tree gnu_result;
2674
2675 /* Push the loop_info structure associated with the LOOP_STMT. */
2676 vec_safe_push (gnu_loop_stack, gnu_loop_info);
2677
2678 /* Set location information for statement and end label. */
2679 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
2680 Sloc_to_locus (Sloc (End_Label (gnat_node)),
2681 &DECL_SOURCE_LOCATION (gnu_loop_label));
2682 LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label;
2683
2684 /* Save the statement for later reuse. */
2685 gnu_loop_info->stmt = gnu_loop_stmt;
2686
2687 /* Set the condition under which the loop must keep going.
2688 For the case "LOOP .... END LOOP;" the condition is always true. */
2689 if (No (gnat_iter_scheme))
2690 ;
2691
2692 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2693 else if (Present (Condition (gnat_iter_scheme)))
2694 LOOP_STMT_COND (gnu_loop_stmt)
2695 = gnat_to_gnu (Condition (gnat_iter_scheme));
2696
2697 /* Otherwise we have an iteration scheme and the condition is given by the
2698 bounds of the subtype of the iteration variable. */
2699 else
2700 {
2701 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
2702 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2703 Entity_Id gnat_type = Etype (gnat_loop_var);
2704 tree gnu_type = get_unpadded_type (gnat_type);
2705 tree gnu_base_type = get_base_type (gnu_type);
2706 tree gnu_one_node = convert (gnu_base_type, integer_one_node);
2707 tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt;
2708 enum tree_code update_code, test_code, shift_code;
2709 bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false;
2710
2711 gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type));
2712 gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type));
2713
2714 /* We must disable modulo reduction for the iteration variable, if any,
2715 in order for the loop comparison to be effective. */
2716 if (reverse)
2717 {
2718 gnu_first = gnu_high;
2719 gnu_last = gnu_low;
2720 update_code = MINUS_NOMOD_EXPR;
2721 test_code = GE_EXPR;
2722 shift_code = PLUS_NOMOD_EXPR;
2723 }
2724 else
2725 {
2726 gnu_first = gnu_low;
2727 gnu_last = gnu_high;
2728 update_code = PLUS_NOMOD_EXPR;
2729 test_code = LE_EXPR;
2730 shift_code = MINUS_NOMOD_EXPR;
2731 }
2732
2733 /* We use two different strategies to translate the loop, depending on
2734 whether optimization is enabled.
2735
2736 If it is, we generate the canonical loop form expected by the loop
2737 optimizer and the loop vectorizer, which is the do-while form:
2738
2739 ENTRY_COND
2740 loop:
2741 TOP_UPDATE
2742 BODY
2743 BOTTOM_COND
2744 GOTO loop
2745
2746 This avoids an implicit dependency on loop header copying and makes
2747 it possible to turn BOTTOM_COND into an inequality test.
2748
2749 If optimization is disabled, loop header copying doesn't come into
2750 play and we try to generate the loop form with the fewer conditional
2751 branches. First, the default form, which is:
2752
2753 loop:
2754 TOP_COND
2755 BODY
2756 BOTTOM_UPDATE
2757 GOTO loop
2758
2759 It should catch most loops with constant ending point. Then, if we
2760 cannot, we try to generate the shifted form:
2761
2762 loop:
2763 TOP_COND
2764 TOP_UPDATE
2765 BODY
2766 GOTO loop
2767
2768 which should catch loops with constant starting point. Otherwise, if
2769 we cannot, we generate the fallback form:
2770
2771 ENTRY_COND
2772 loop:
2773 BODY
2774 BOTTOM_COND
2775 BOTTOM_UPDATE
2776 GOTO loop
2777
2778 which works in all cases. */
2779
2780 if (optimize)
2781 {
2782 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
2783 overflow. */
2784 if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse))
2785 ;
2786
2787 /* Otherwise, use the do-while form with the help of a special
2788 induction variable in the unsigned version of the base type
2789 or the unsigned version of the size type, whichever is the
2790 largest, in order to have wrap-around arithmetics for it. */
2791 else
2792 {
2793 if (TYPE_PRECISION (gnu_base_type)
2794 > TYPE_PRECISION (size_type_node))
2795 gnu_base_type
2796 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1);
2797 else
2798 gnu_base_type = size_type_node;
2799
2800 gnu_first = convert (gnu_base_type, gnu_first);
2801 gnu_last = convert (gnu_base_type, gnu_last);
2802 gnu_one_node = convert (gnu_base_type, integer_one_node);
2803 use_iv = true;
2804 }
2805
2806 gnu_first
2807 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2808 gnu_one_node);
2809 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2810 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2811 }
2812 else
2813 {
2814 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
2815 if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse))
2816 ;
2817
2818 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
2819 GNU_LAST-1 does. */
2820 else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)
2821 && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse))
2822 {
2823 gnu_first
2824 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2825 gnu_one_node);
2826 gnu_last
2827 = build_binary_op (shift_code, gnu_base_type, gnu_last,
2828 gnu_one_node);
2829 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2830 }
2831
2832 /* Otherwise, use the fallback form. */
2833 else
2834 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2835 }
2836
2837 /* If we use the BOTTOM_COND, we can turn the test into an inequality
2838 test but we may have to add ENTRY_COND to protect the empty loop. */
2839 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt))
2840 {
2841 test_code = NE_EXPR;
2842 if (can_be_lower_p (gnu_high, gnu_low))
2843 {
2844 gnu_cond_expr
2845 = build3 (COND_EXPR, void_type_node,
2846 build_binary_op (LE_EXPR, boolean_type_node,
2847 gnu_low, gnu_high),
2848 NULL_TREE, alloc_stmt_list ());
2849 set_expr_location_from_node (gnu_cond_expr, gnat_loop_spec);
2850 }
2851 }
2852
2853 /* Open a new nesting level that will surround the loop to declare the
2854 iteration variable. */
2855 start_stmt_group ();
2856 gnat_pushlevel ();
2857
2858 /* If we use the special induction variable, create it and set it to
2859 its initial value. Morever, the regular iteration variable cannot
2860 itself be initialized, lest the initial value wrapped around. */
2861 if (use_iv)
2862 {
2863 gnu_loop_iv
2864 = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var);
2865 add_stmt (gnu_stmt);
2866 gnu_first = NULL_TREE;
2867 }
2868 else
2869 gnu_loop_iv = NULL_TREE;
2870
2871 /* Declare the iteration variable and set it to its initial value. */
2872 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
2873 if (DECL_BY_REF_P (gnu_loop_var))
2874 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
2875 else if (use_iv)
2876 {
2877 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var));
2878 SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv);
2879 }
2880 gnu_loop_info->loop_var = gnu_loop_var;
2881
2882 /* Do all the arithmetics in the base type. */
2883 gnu_loop_var = convert (gnu_base_type, gnu_loop_var);
2884
2885 /* Set either the top or bottom exit condition. */
2886 if (use_iv)
2887 LOOP_STMT_COND (gnu_loop_stmt)
2888 = build_binary_op (test_code, boolean_type_node, gnu_loop_iv,
2889 gnu_last);
2890 else
2891 LOOP_STMT_COND (gnu_loop_stmt)
2892 = build_binary_op (test_code, boolean_type_node, gnu_loop_var,
2893 gnu_last);
2894
2895 /* Set either the top or bottom update statement and give it the source
2896 location of the iteration for better coverage info. */
2897 if (use_iv)
2898 {
2899 gnu_stmt
2900 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv,
2901 build_binary_op (update_code, gnu_base_type,
2902 gnu_loop_iv, gnu_one_node));
2903 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2904 append_to_statement_list (gnu_stmt,
2905 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2906 gnu_stmt
2907 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2908 gnu_loop_iv);
2909 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2910 append_to_statement_list (gnu_stmt,
2911 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2912 }
2913 else
2914 {
2915 gnu_stmt
2916 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2917 build_binary_op (update_code, gnu_base_type,
2918 gnu_loop_var, gnu_one_node));
2919 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2920 LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt;
2921 }
2922 }
2923
2924 /* If the loop was named, have the name point to this loop. In this case,
2925 the association is not a DECL node, but the end label of the loop. */
2926 if (Present (Identifier (gnat_node)))
2927 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true);
2928
2929 /* Make the loop body into its own block, so any allocated storage will be
2930 released every iteration. This is needed for stack allocation. */
2931 LOOP_STMT_BODY (gnu_loop_stmt)
2932 = build_stmt_group (Statements (gnat_node), true);
2933 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
2934
2935 /* If we have an iteration scheme, then we are in a statement group. Add
2936 the LOOP_STMT to it, finish it and make it the "loop". */
2937 if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme)))
2938 {
2939 struct range_check_info_d *rci;
2940 unsigned n_checks = vec_safe_length (gnu_loop_info->checks);
2941 unsigned int i;
2942
2943 /* First, if we have computed a small number of invariant conditions for
2944 range checks applied to the iteration variable, then initialize these
2945 conditions in front of the loop. Otherwise, leave them set to true.
2946
2947 ??? The heuristics need to be improved, by taking into account the
2948 following datapoints:
2949 - loop unswitching is disabled for big loops. The cap is the
2950 parameter PARAM_MAX_UNSWITCH_INSNS (50).
2951 - loop unswitching can only be applied a small number of times
2952 to a given loop. The cap is PARAM_MAX_UNSWITCH_LEVEL (3).
2953 - the front-end quickly generates useless or redundant checks
2954 that can be entirely optimized away in the end. */
2955 if (1 <= n_checks && n_checks <= 4)
2956 for (i = 0;
2957 vec_safe_iterate (gnu_loop_info->checks, i, &rci);
2958 i++)
2959 {
2960 tree low_ok
2961 = rci->low_bound
2962 ? build_binary_op (GE_EXPR, boolean_type_node,
2963 convert (rci->type, gnu_low),
2964 rci->low_bound)
2965 : boolean_true_node;
2966
2967 tree high_ok
2968 = rci->high_bound
2969 ? build_binary_op (LE_EXPR, boolean_type_node,
2970 convert (rci->type, gnu_high),
2971 rci->high_bound)
2972 : boolean_true_node;
2973
2974 tree range_ok
2975 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
2976 low_ok, high_ok);
2977
2978 TREE_OPERAND (rci->invariant_cond, 0)
2979 = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok);
2980
2981 add_stmt_with_node_force (rci->invariant_cond, gnat_node);
2982 }
2983
2984 add_stmt (gnu_loop_stmt);
2985 gnat_poplevel ();
2986 gnu_loop_stmt = end_stmt_group ();
2987 }
2988
2989 /* If we have an outer COND_EXPR, that's our result and this loop is its
2990 "true" statement. Otherwise, the result is the LOOP_STMT. */
2991 if (gnu_cond_expr)
2992 {
2993 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
2994 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
2995 gnu_result = gnu_cond_expr;
2996 }
2997 else
2998 gnu_result = gnu_loop_stmt;
2999
3000 gnu_loop_stack->pop ();
3001
3002 return gnu_result;
3003 }
3004 \f
3005 /* This page implements a form of Named Return Value optimization modelled
3006 on the C++ optimization of the same name. The main difference is that
3007 we disregard any semantical considerations when applying it here, the
3008 counterpart being that we don't try to apply it to semantically loaded
3009 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
3010
3011 We consider a function body of the following GENERIC form:
3012
3013 return_type R1;
3014 [...]
3015 RETURN_EXPR [<retval> = ...]
3016 [...]
3017 RETURN_EXPR [<retval> = R1]
3018 [...]
3019 return_type Ri;
3020 [...]
3021 RETURN_EXPR [<retval> = ...]
3022 [...]
3023 RETURN_EXPR [<retval> = Ri]
3024 [...]
3025
3026 and we try to fulfill a simple criterion that would make it possible to
3027 replace one or several Ri variables with the RESULT_DECL of the function.
3028
3029 The first observation is that RETURN_EXPRs that don't directly reference
3030 any of the Ri variables on the RHS of their assignment are transparent wrt
3031 the optimization. This is because the Ri variables aren't addressable so
3032 any transformation applied to them doesn't affect the RHS; moreover, the
3033 assignment writes the full <retval> object so existing values are entirely
3034 discarded.
3035
3036 This property can be extended to some forms of RETURN_EXPRs that reference
3037 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3038 case, in particular when function calls are involved.
3039
3040 Therefore the algorithm is as follows:
3041
3042 1. Collect the list of candidates for a Named Return Value (Ri variables
3043 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3044 other expressions on the RHS of such assignments.
3045
3046 2. Prune the members of the first list (candidates) that are referenced
3047 by a member of the second list (expressions).
3048
3049 3. Extract a set of candidates with non-overlapping live ranges from the
3050 first list. These are the Named Return Values.
3051
3052 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
3053 Named Return Values in the function with the RESULT_DECL.
3054
3055 If the function returns an unconstrained type, things are a bit different
3056 because the anonymous return object is allocated on the secondary stack
3057 and RESULT_DECL is only a pointer to it. Each return object can be of a
3058 different size and is allocated separately so we need not care about the
3059 aforementioned overlapping issues. Therefore, we don't collect the other
3060 expressions and skip step #2 in the algorithm. */
3061
3062 struct nrv_data
3063 {
3064 bitmap nrv;
3065 tree result;
3066 Node_Id gnat_ret;
3067 hash_set<tree> *visited;
3068 };
3069
3070 /* Return true if T is a Named Return Value. */
3071
3072 static inline bool
3073 is_nrv_p (bitmap nrv, tree t)
3074 {
3075 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
3076 }
3077
3078 /* Helper function for walk_tree, used by finalize_nrv below. */
3079
3080 static tree
3081 prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3082 {
3083 struct nrv_data *dp = (struct nrv_data *)data;
3084 tree t = *tp;
3085
3086 /* No need to walk into types or decls. */
3087 if (IS_TYPE_OR_DECL_P (t))
3088 *walk_subtrees = 0;
3089
3090 if (is_nrv_p (dp->nrv, t))
3091 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3092
3093 return NULL_TREE;
3094 }
3095
3096 /* Prune Named Return Values in BLOCK and return true if there is still a
3097 Named Return Value in BLOCK or one of its sub-blocks. */
3098
3099 static bool
3100 prune_nrv_in_block (bitmap nrv, tree block)
3101 {
3102 bool has_nrv = false;
3103 tree t;
3104
3105 /* First recurse on the sub-blocks. */
3106 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3107 has_nrv |= prune_nrv_in_block (nrv, t);
3108
3109 /* Then make sure to keep at most one NRV per block. */
3110 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3111 if (is_nrv_p (nrv, t))
3112 {
3113 if (has_nrv)
3114 bitmap_clear_bit (nrv, DECL_UID (t));
3115 else
3116 has_nrv = true;
3117 }
3118
3119 return has_nrv;
3120 }
3121
3122 /* Helper function for walk_tree, used by finalize_nrv below. */
3123
3124 static tree
3125 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3126 {
3127 struct nrv_data *dp = (struct nrv_data *)data;
3128 tree t = *tp;
3129
3130 /* No need to walk into types. */
3131 if (TYPE_P (t))
3132 *walk_subtrees = 0;
3133
3134 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3135 nop, but differs from using NULL_TREE in that it indicates that we care
3136 about the value of the RESULT_DECL. */
3137 else if (TREE_CODE (t) == RETURN_EXPR
3138 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3139 {
3140 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1), init_expr;
3141
3142 /* If this is the temporary created for a return value with variable
3143 size in Call_to_gnu, we replace the RHS with the init expression. */
3144 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3145 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3146 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3147 == TREE_OPERAND (ret_val, 1))
3148 {
3149 init_expr = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3150 ret_val = TREE_OPERAND (ret_val, 1);
3151 }
3152 else
3153 init_expr = NULL_TREE;
3154
3155 /* Strip useless conversions around the return value. */
3156 if (gnat_useless_type_conversion (ret_val))
3157 ret_val = TREE_OPERAND (ret_val, 0);
3158
3159 if (is_nrv_p (dp->nrv, ret_val))
3160 {
3161 if (init_expr)
3162 TREE_OPERAND (TREE_OPERAND (t, 0), 1) = init_expr;
3163 else
3164 TREE_OPERAND (t, 0) = dp->result;
3165 }
3166 }
3167
3168 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3169 if needed. */
3170 else if (TREE_CODE (t) == DECL_EXPR
3171 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3172 {
3173 tree var = DECL_EXPR_DECL (t), init;
3174
3175 if (DECL_INITIAL (var))
3176 {
3177 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3178 DECL_INITIAL (var));
3179 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3180 DECL_INITIAL (var) = NULL_TREE;
3181 }
3182 else
3183 init = build_empty_stmt (EXPR_LOCATION (t));
3184 *tp = init;
3185
3186 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3187 SET_DECL_VALUE_EXPR (var, dp->result);
3188 DECL_HAS_VALUE_EXPR_P (var) = 1;
3189 /* ??? Kludge to avoid an assertion failure during inlining. */
3190 DECL_SIZE (var) = bitsize_unit_node;
3191 DECL_SIZE_UNIT (var) = size_one_node;
3192 }
3193
3194 /* And replace all uses of NRVs with the RESULT_DECL. */
3195 else if (is_nrv_p (dp->nrv, t))
3196 *tp = convert (TREE_TYPE (t), dp->result);
3197
3198 /* Avoid walking into the same tree more than once. Unfortunately, we
3199 can't just use walk_tree_without_duplicates because it would only
3200 call us for the first occurrence of NRVs in the function body. */
3201 if (dp->visited->add (*tp))
3202 *walk_subtrees = 0;
3203
3204 return NULL_TREE;
3205 }
3206
3207 /* Likewise, but used when the function returns an unconstrained type. */
3208
3209 static tree
3210 finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3211 {
3212 struct nrv_data *dp = (struct nrv_data *)data;
3213 tree t = *tp;
3214
3215 /* No need to walk into types. */
3216 if (TYPE_P (t))
3217 *walk_subtrees = 0;
3218
3219 /* We need to see the DECL_EXPR of NRVs before any other references so we
3220 walk the body of BIND_EXPR before walking its variables. */
3221 else if (TREE_CODE (t) == BIND_EXPR)
3222 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3223
3224 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3225 return value built by the allocator instead of the whole construct. */
3226 else if (TREE_CODE (t) == RETURN_EXPR
3227 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3228 {
3229 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3230
3231 /* This is the construct returned by the allocator. */
3232 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3233 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3234 {
3235 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
3236 ret_val
3237 = (*CONSTRUCTOR_ELTS (TREE_OPERAND (TREE_OPERAND (ret_val, 0),
3238 1)))[1].value;
3239 else
3240 ret_val = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3241 }
3242
3243 /* Strip useless conversions around the return value. */
3244 if (gnat_useless_type_conversion (ret_val)
3245 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3246 ret_val = TREE_OPERAND (ret_val, 0);
3247
3248 /* Strip unpadding around the return value. */
3249 if (TREE_CODE (ret_val) == COMPONENT_REF
3250 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3251 ret_val = TREE_OPERAND (ret_val, 0);
3252
3253 /* Assign the new return value to the RESULT_DECL. */
3254 if (is_nrv_p (dp->nrv, ret_val))
3255 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3256 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3257 }
3258
3259 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3260 into a new variable. */
3261 else if (TREE_CODE (t) == DECL_EXPR
3262 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3263 {
3264 tree saved_current_function_decl = current_function_decl;
3265 tree var = DECL_EXPR_DECL (t);
3266 tree alloc, p_array, new_var, new_ret;
3267 vec<constructor_elt, va_gc> *v;
3268 vec_alloc (v, 2);
3269
3270 /* Create an artificial context to build the allocation. */
3271 current_function_decl = decl_function_context (var);
3272 start_stmt_group ();
3273 gnat_pushlevel ();
3274
3275 /* This will return a COMPOUND_EXPR with the allocation in the first
3276 arm and the final return value in the second arm. */
3277 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3278 TREE_TYPE (dp->result),
3279 Procedure_To_Call (dp->gnat_ret),
3280 Storage_Pool (dp->gnat_ret),
3281 Empty, false);
3282
3283 /* The new variable is built as a reference to the allocated space. */
3284 new_var
3285 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3286 build_reference_type (TREE_TYPE (var)));
3287 DECL_BY_REFERENCE (new_var) = 1;
3288
3289 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3290 {
3291 /* The new initial value is a COMPOUND_EXPR with the allocation in
3292 the first arm and the value of P_ARRAY in the second arm. */
3293 DECL_INITIAL (new_var)
3294 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3295 TREE_OPERAND (alloc, 0),
3296 (*CONSTRUCTOR_ELTS (TREE_OPERAND (alloc, 1)))[0].value);
3297
3298 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3299 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3300 CONSTRUCTOR_APPEND_ELT (v, p_array,
3301 fold_convert (TREE_TYPE (p_array), new_var));
3302 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
3303 (*CONSTRUCTOR_ELTS (
3304 TREE_OPERAND (alloc, 1)))[1].value);
3305 new_ret = build_constructor (TREE_TYPE (alloc), v);
3306 }
3307 else
3308 {
3309 /* The new initial value is just the allocation. */
3310 DECL_INITIAL (new_var) = alloc;
3311 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3312 }
3313
3314 gnat_pushdecl (new_var, Empty);
3315
3316 /* Destroy the artificial context and insert the new statements. */
3317 gnat_zaplevel ();
3318 *tp = end_stmt_group ();
3319 current_function_decl = saved_current_function_decl;
3320
3321 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3322 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3323 DECL_CHAIN (var) = new_var;
3324 DECL_IGNORED_P (var) = 1;
3325
3326 /* Save the new return value and the dereference of NEW_VAR. */
3327 DECL_INITIAL (var)
3328 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3329 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3330 /* ??? Kludge to avoid messing up during inlining. */
3331 DECL_CONTEXT (var) = NULL_TREE;
3332 }
3333
3334 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3335 else if (is_nrv_p (dp->nrv, t))
3336 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3337
3338 /* Avoid walking into the same tree more than once. Unfortunately, we
3339 can't just use walk_tree_without_duplicates because it would only
3340 call us for the first occurrence of NRVs in the function body. */
3341 if (dp->visited->add (*tp))
3342 *walk_subtrees = 0;
3343
3344 return NULL_TREE;
3345 }
3346
3347 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3348 contains the candidates for Named Return Value and OTHER is a list of
3349 the other return values. GNAT_RET is a representative return node. */
3350
3351 static void
3352 finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
3353 {
3354 struct cgraph_node *node;
3355 struct nrv_data data;
3356 walk_tree_fn func;
3357 unsigned int i;
3358 tree iter;
3359
3360 /* We shouldn't be applying the optimization to return types that we aren't
3361 allowed to manipulate freely. */
3362 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
3363
3364 /* Prune the candidates that are referenced by other return values. */
3365 data.nrv = nrv;
3366 data.result = NULL_TREE;
3367 data.visited = NULL;
3368 for (i = 0; vec_safe_iterate (other, i, &iter); i++)
3369 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3370 if (bitmap_empty_p (nrv))
3371 return;
3372
3373 /* Prune also the candidates that are referenced by nested functions. */
3374 node = cgraph_node::get_create (fndecl);
3375 for (node = node->nested; node; node = node->next_nested)
3376 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), prune_nrv_r,
3377 &data);
3378 if (bitmap_empty_p (nrv))
3379 return;
3380
3381 /* Extract a set of NRVs with non-overlapping live ranges. */
3382 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3383 return;
3384
3385 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3386 data.nrv = nrv;
3387 data.result = DECL_RESULT (fndecl);
3388 data.gnat_ret = gnat_ret;
3389 data.visited = new hash_set<tree>;
3390 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl)))
3391 func = finalize_nrv_unc_r;
3392 else
3393 func = finalize_nrv_r;
3394 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
3395 delete data.visited;
3396 }
3397
3398 /* Return true if RET_VAL can be used as a Named Return Value for the
3399 anonymous return object RET_OBJ. */
3400
3401 static bool
3402 return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3403 {
3404 if (TREE_CODE (ret_val) != VAR_DECL)
3405 return false;
3406
3407 if (TREE_THIS_VOLATILE (ret_val))
3408 return false;
3409
3410 if (DECL_CONTEXT (ret_val) != current_function_decl)
3411 return false;
3412
3413 if (TREE_STATIC (ret_val))
3414 return false;
3415
3416 if (TREE_ADDRESSABLE (ret_val))
3417 return false;
3418
3419 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
3420 return false;
3421
3422 return true;
3423 }
3424
3425 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3426 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3427 around RESULT_OBJ, which may be null in this case. */
3428
3429 static tree
3430 build_return_expr (tree ret_obj, tree ret_val)
3431 {
3432 tree result_expr;
3433
3434 if (ret_val)
3435 {
3436 /* The gimplifier explicitly enforces the following invariant:
3437
3438 RETURN_EXPR
3439 |
3440 INIT_EXPR
3441 / \
3442 / \
3443 RET_OBJ ...
3444
3445 As a consequence, type consistency dictates that we use the type
3446 of the RET_OBJ as the operation type. */
3447 tree operation_type = TREE_TYPE (ret_obj);
3448
3449 /* Convert the right operand to the operation type. Note that this is
3450 the transformation applied in the INIT_EXPR case of build_binary_op,
3451 with the assumption that the type cannot involve a placeholder. */
3452 if (operation_type != TREE_TYPE (ret_val))
3453 ret_val = convert (operation_type, ret_val);
3454
3455 /* We always can use an INIT_EXPR for the return object. */
3456 result_expr = build2 (INIT_EXPR, void_type_node, ret_obj, ret_val);
3457
3458 /* If the function returns an aggregate type, find out whether this is
3459 a candidate for Named Return Value. If so, record it. Otherwise,
3460 if this is an expression of some kind, record it elsewhere. */
3461 if (optimize
3462 && AGGREGATE_TYPE_P (operation_type)
3463 && !TYPE_IS_FAT_POINTER_P (operation_type)
3464 && TYPE_MODE (operation_type) == BLKmode
3465 && aggregate_value_p (operation_type, current_function_decl))
3466 {
3467 /* Recognize the temporary created for a return value with variable
3468 size in Call_to_gnu. We want to eliminate it if possible. */
3469 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3470 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3471 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3472 == TREE_OPERAND (ret_val, 1))
3473 ret_val = TREE_OPERAND (ret_val, 1);
3474
3475 /* Strip useless conversions around the return value. */
3476 if (gnat_useless_type_conversion (ret_val))
3477 ret_val = TREE_OPERAND (ret_val, 0);
3478
3479 /* Now apply the test to the return value. */
3480 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3481 {
3482 if (!f_named_ret_val)
3483 f_named_ret_val = BITMAP_GGC_ALLOC ();
3484 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3485 }
3486
3487 /* Note that we need not care about CONSTRUCTORs here, as they are
3488 totally transparent given the read-compose-write semantics of
3489 assignments from CONSTRUCTORs. */
3490 else if (EXPR_P (ret_val))
3491 vec_safe_push (f_other_ret_val, ret_val);
3492 }
3493 }
3494 else
3495 result_expr = ret_obj;
3496
3497 return build1 (RETURN_EXPR, void_type_node, result_expr);
3498 }
3499 \f
3500 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3501 don't return anything. */
3502
3503 static void
3504 Subprogram_Body_to_gnu (Node_Id gnat_node)
3505 {
3506 /* Defining identifier of a parameter to the subprogram. */
3507 Entity_Id gnat_param;
3508 /* The defining identifier for the subprogram body. Note that if a
3509 specification has appeared before for this body, then the identifier
3510 occurring in that specification will also be a defining identifier and all
3511 the calls to this subprogram will point to that specification. */
3512 Entity_Id gnat_subprog_id
3513 = (Present (Corresponding_Spec (gnat_node))
3514 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
3515 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3516 tree gnu_subprog_decl;
3517 /* Its RESULT_DECL node. */
3518 tree gnu_result_decl;
3519 /* Its FUNCTION_TYPE node. */
3520 tree gnu_subprog_type;
3521 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3522 tree gnu_cico_list;
3523 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3524 tree gnu_return_var_elmt = NULL_TREE;
3525 tree gnu_result;
3526 location_t locus;
3527 struct language_function *gnu_subprog_language;
3528 vec<parm_attr, va_gc> *cache;
3529
3530 /* If this is a generic object or if it has been eliminated,
3531 ignore it. */
3532 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
3533 || Ekind (gnat_subprog_id) == E_Generic_Function
3534 || Is_Eliminated (gnat_subprog_id))
3535 return;
3536
3537 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3538 the already-elaborated tree node. However, if this subprogram had its
3539 elaboration deferred, we will already have made a tree node for it. So
3540 treat it as not being defined in that case. Such a subprogram cannot
3541 have an address clause or a freeze node, so this test is safe, though it
3542 does disable some otherwise-useful error checking. */
3543 gnu_subprog_decl
3544 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
3545 Acts_As_Spec (gnat_node)
3546 && !present_gnu_tree (gnat_subprog_id));
3547 gnu_result_decl = DECL_RESULT (gnu_subprog_decl);
3548 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
3549 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3550 if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node)
3551 gnu_return_var_elmt = gnu_cico_list;
3552
3553 /* If the function returns by invisible reference, make it explicit in the
3554 function body. See gnat_to_gnu_entity, E_Subprogram_Type case. */
3555 if (TREE_ADDRESSABLE (gnu_subprog_type))
3556 {
3557 TREE_TYPE (gnu_result_decl)
3558 = build_reference_type (TREE_TYPE (gnu_result_decl));
3559 relayout_decl (gnu_result_decl);
3560 }
3561
3562 /* Set the line number in the decl to correspond to that of the body. */
3563 Sloc_to_locus (Sloc (gnat_node), &locus);
3564 DECL_SOURCE_LOCATION (gnu_subprog_decl) = locus;
3565
3566 /* Initialize the information structure for the function. */
3567 allocate_struct_function (gnu_subprog_decl, false);
3568 gnu_subprog_language = ggc_cleared_alloc<language_function> ();
3569 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language = gnu_subprog_language;
3570 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->function_start_locus = locus;
3571 set_cfun (NULL);
3572
3573 begin_subprog_body (gnu_subprog_decl);
3574
3575 /* If there are copy-in/copy-out parameters, we need to ensure that they are
3576 properly copied out by the return statement. We do this by making a new
3577 block and converting any return into a goto to a label at the end of the
3578 block. */
3579 if (gnu_cico_list)
3580 {
3581 tree gnu_return_var = NULL_TREE;
3582
3583 vec_safe_push (gnu_return_label_stack,
3584 create_artificial_label (input_location));
3585
3586 start_stmt_group ();
3587 gnat_pushlevel ();
3588
3589 /* If this is a function with copy-in/copy-out parameters and which does
3590 not return by invisible reference, we also need a variable for the
3591 return value to be placed. */
3592 if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type))
3593 {
3594 tree gnu_return_type
3595 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3596
3597 gnu_return_var
3598 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
3599 gnu_return_type, NULL_TREE, false, false,
3600 false, false, NULL, gnat_subprog_id);
3601 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3602 }
3603
3604 vec_safe_push (gnu_return_var_stack, gnu_return_var);
3605
3606 /* See whether there are parameters for which we don't have a GCC tree
3607 yet. These must be Out parameters. Make a VAR_DECL for them and
3608 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3609 We can match up the entries because TYPE_CI_CO_LIST is in the order
3610 of the parameters. */
3611 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3612 Present (gnat_param);
3613 gnat_param = Next_Formal_With_Extras (gnat_param))
3614 if (!present_gnu_tree (gnat_param))
3615 {
3616 tree gnu_cico_entry = gnu_cico_list;
3617 tree gnu_decl;
3618
3619 /* Skip any entries that have been already filled in; they must
3620 correspond to In Out parameters. */
3621 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3622 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3623
3624 /* Do any needed dereferences for by-ref objects. */
3625 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, 1);
3626 gcc_assert (DECL_P (gnu_decl));
3627 if (DECL_BY_REF_P (gnu_decl))
3628 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3629
3630 /* Do any needed references for padded types. */
3631 TREE_VALUE (gnu_cico_entry)
3632 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
3633 }
3634 }
3635 else
3636 vec_safe_push (gnu_return_label_stack, NULL_TREE);
3637
3638 /* Get a tree corresponding to the code for the subprogram. */
3639 start_stmt_group ();
3640 gnat_pushlevel ();
3641
3642 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3643
3644 /* Generate the code of the subprogram itself. A return statement will be
3645 present and any Out parameters will be handled there. */
3646 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3647 gnat_poplevel ();
3648 gnu_result = end_stmt_group ();
3649
3650 /* If we populated the parameter attributes cache, we need to make sure that
3651 the cached expressions are evaluated on all the possible paths leading to
3652 their uses. So we force their evaluation on entry of the function. */
3653 cache = gnu_subprog_language->parm_attr_cache;
3654 if (cache)
3655 {
3656 struct parm_attr_d *pa;
3657 int i;
3658
3659 start_stmt_group ();
3660
3661 FOR_EACH_VEC_ELT (*cache, i, pa)
3662 {
3663 if (pa->first)
3664 add_stmt_with_node_force (pa->first, gnat_node);
3665 if (pa->last)
3666 add_stmt_with_node_force (pa->last, gnat_node);
3667 if (pa->length)
3668 add_stmt_with_node_force (pa->length, gnat_node);
3669 }
3670
3671 add_stmt (gnu_result);
3672 gnu_result = end_stmt_group ();
3673
3674 gnu_subprog_language->parm_attr_cache = NULL;
3675 }
3676
3677 /* If we are dealing with a return from an Ada procedure with parameters
3678 passed by copy-in/copy-out, we need to return a record containing the
3679 final values of these parameters. If the list contains only one entry,
3680 return just that entry though.
3681
3682 For a full description of the copy-in/copy-out parameter mechanism, see
3683 the part of the gnat_to_gnu_entity routine dealing with the translation
3684 of subprograms.
3685
3686 We need to make a block that contains the definition of that label and
3687 the copying of the return value. It first contains the function, then
3688 the label and copy statement. */
3689 if (gnu_cico_list)
3690 {
3691 const Node_Id gnat_end_label
3692 = End_Label (Handled_Statement_Sequence (gnat_node));
3693
3694 gnu_return_var_stack->pop ();
3695
3696 add_stmt (gnu_result);
3697 add_stmt (build1 (LABEL_EXPR, void_type_node,
3698 gnu_return_label_stack->last ()));
3699
3700 /* If this is a function which returns by invisible reference, the
3701 return value has already been dealt with at the return statements,
3702 so we only need to indirectly copy out the parameters. */
3703 if (TREE_ADDRESSABLE (gnu_subprog_type))
3704 {
3705 tree gnu_ret_deref
3706 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl);
3707 tree t;
3708
3709 gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node);
3710
3711 for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t))
3712 {
3713 tree gnu_field_deref
3714 = build_component_ref (gnu_ret_deref, NULL_TREE,
3715 TREE_PURPOSE (t), true);
3716 gnu_result = build2 (MODIFY_EXPR, void_type_node,
3717 gnu_field_deref, TREE_VALUE (t));
3718 add_stmt_with_node (gnu_result, gnat_end_label);
3719 }
3720 }
3721
3722 /* Otherwise, if this is a procedure or a function which does not return
3723 by invisible reference, we can do a direct block-copy out. */
3724 else
3725 {
3726 tree gnu_retval;
3727
3728 if (list_length (gnu_cico_list) == 1)
3729 gnu_retval = TREE_VALUE (gnu_cico_list);
3730 else
3731 gnu_retval
3732 = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
3733 gnu_cico_list);
3734
3735 gnu_result = build_return_expr (gnu_result_decl, gnu_retval);
3736 add_stmt_with_node (gnu_result, gnat_end_label);
3737 }
3738
3739 gnat_poplevel ();
3740 gnu_result = end_stmt_group ();
3741 }
3742
3743 gnu_return_label_stack->pop ();
3744
3745 /* Attempt setting the end_locus of our GCC body tree, typically a
3746 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3747 declaration tree. */
3748 set_end_locus_from_node (gnu_result, gnat_node);
3749 set_end_locus_from_node (gnu_subprog_decl, gnat_node);
3750
3751 /* On SEH targets, install an exception handler around the main entry
3752 point to catch unhandled exceptions. */
3753 if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
3754 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
3755 {
3756 tree t;
3757 tree etype;
3758
3759 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
3760 1, integer_zero_node);
3761 t = build_call_n_expr (unhandled_except_decl, 1, t);
3762
3763 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
3764 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
3765
3766 t = build2 (CATCH_EXPR, void_type_node, etype, t);
3767 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
3768 gnu_result, t);
3769 }
3770
3771 end_subprog_body (gnu_result);
3772
3773 /* Finally annotate the parameters and disconnect the trees for parameters
3774 that we have turned into variables since they are now unusable. */
3775 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3776 Present (gnat_param);
3777 gnat_param = Next_Formal_With_Extras (gnat_param))
3778 {
3779 tree gnu_param = get_gnu_tree (gnat_param);
3780 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
3781
3782 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
3783 DECL_BY_REF_P (gnu_param));
3784
3785 if (is_var_decl)
3786 save_gnu_tree (gnat_param, NULL_TREE, false);
3787 }
3788
3789 /* Disconnect the variable created for the return value. */
3790 if (gnu_return_var_elmt)
3791 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
3792
3793 /* If the function returns an aggregate type and we have candidates for
3794 a Named Return Value, finalize the optimization. */
3795 if (optimize && gnu_subprog_language->named_ret_val)
3796 {
3797 finalize_nrv (gnu_subprog_decl,
3798 gnu_subprog_language->named_ret_val,
3799 gnu_subprog_language->other_ret_val,
3800 gnu_subprog_language->gnat_ret);
3801 gnu_subprog_language->named_ret_val = NULL;
3802 gnu_subprog_language->other_ret_val = NULL;
3803 }
3804
3805 /* If this is an inlined external function that has been marked uninlinable,
3806 drop the body and stop there. Otherwise compile the body. */
3807 if (DECL_EXTERNAL (gnu_subprog_decl) && DECL_UNINLINABLE (gnu_subprog_decl))
3808 DECL_SAVED_TREE (gnu_subprog_decl) = NULL_TREE;
3809 else
3810 rest_of_subprog_body_compilation (gnu_subprog_decl);
3811 }
3812 \f
3813 /* Return true if GNAT_NODE requires atomic synchronization. */
3814
3815 static bool
3816 atomic_sync_required_p (Node_Id gnat_node)
3817 {
3818 const Node_Id gnat_parent = Parent (gnat_node);
3819 Node_Kind kind;
3820 unsigned char attr_id;
3821
3822 /* First, scan the node to find the Atomic_Sync_Required flag. */
3823 kind = Nkind (gnat_node);
3824 if (kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
3825 {
3826 gnat_node = Expression (gnat_node);
3827 kind = Nkind (gnat_node);
3828 }
3829
3830 switch (kind)
3831 {
3832 case N_Expanded_Name:
3833 case N_Explicit_Dereference:
3834 case N_Identifier:
3835 case N_Indexed_Component:
3836 case N_Selected_Component:
3837 if (!Atomic_Sync_Required (gnat_node))
3838 return false;
3839 break;
3840
3841 default:
3842 return false;
3843 }
3844
3845 /* Then, scan the parent to find out cases where the flag is irrelevant. */
3846 kind = Nkind (gnat_parent);
3847 switch (kind)
3848 {
3849 case N_Attribute_Reference:
3850 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
3851 /* Do not mess up machine code insertions. */
3852 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
3853 return false;
3854 break;
3855
3856 case N_Object_Renaming_Declaration:
3857 /* Do not generate a function call as a renamed object. */
3858 return false;
3859
3860 default:
3861 break;
3862 }
3863
3864 return true;
3865 }
3866 \f
3867 /* Create a temporary variable with PREFIX and TYPE, and return it. */
3868
3869 static tree
3870 create_temporary (const char *prefix, tree type)
3871 {
3872 tree gnu_temp = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
3873 type, NULL_TREE, false, false, false, false,
3874 NULL, Empty);
3875 DECL_ARTIFICIAL (gnu_temp) = 1;
3876 DECL_IGNORED_P (gnu_temp) = 1;
3877
3878 return gnu_temp;
3879 }
3880
3881 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
3882 Put the initialization statement into GNU_INIT_STMT and annotate it with
3883 the SLOC of GNAT_NODE. Return the temporary variable. */
3884
3885 static tree
3886 create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
3887 Node_Id gnat_node)
3888 {
3889 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
3890
3891 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
3892 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
3893
3894 return gnu_temp;
3895 }
3896
3897 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
3898 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
3899 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
3900 If GNU_TARGET is non-null, this must be a function call on the RHS of a
3901 N_Assignment_Statement and the result is to be placed into that object.
3902 If, in addition, ATOMIC_SYNC is true, then the assignment to GNU_TARGET
3903 requires atomic synchronization. */
3904
3905 static tree
3906 Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
3907 bool atomic_sync)
3908 {
3909 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
3910 const bool returning_value = (function_call && !gnu_target);
3911 /* The GCC node corresponding to the GNAT subprogram name. This can either
3912 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
3913 or an indirect reference expression (an INDIRECT_REF node) pointing to a
3914 subprogram. */
3915 tree gnu_subprog = gnat_to_gnu (Name (gnat_node));
3916 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
3917 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
3918 /* The return type of the FUNCTION_TYPE. */
3919 tree gnu_result_type = TREE_TYPE (gnu_subprog_type);
3920 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog);
3921 vec<tree, va_gc> *gnu_actual_vec = NULL;
3922 tree gnu_name_list = NULL_TREE;
3923 tree gnu_stmt_list = NULL_TREE;
3924 tree gnu_after_list = NULL_TREE;
3925 tree gnu_retval = NULL_TREE;
3926 tree gnu_call, gnu_result;
3927 bool went_into_elab_proc = false;
3928 bool pushed_binding_level = false;
3929 Entity_Id gnat_formal;
3930 Node_Id gnat_actual;
3931
3932 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
3933
3934 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
3935 all our args first. */
3936 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL && DECL_STUBBED_P (gnu_subprog))
3937 {
3938 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
3939 gnat_node, N_Raise_Program_Error);
3940
3941 for (gnat_actual = First_Actual (gnat_node);
3942 Present (gnat_actual);
3943 gnat_actual = Next_Actual (gnat_actual))
3944 add_stmt (gnat_to_gnu (gnat_actual));
3945
3946 if (returning_value)
3947 {
3948 *gnu_result_type_p = gnu_result_type;
3949 return build1 (NULL_EXPR, gnu_result_type, call_expr);
3950 }
3951
3952 return call_expr;
3953 }
3954
3955 /* For a call to a nested function, check the inlining status. */
3956 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL
3957 && decl_function_context (gnu_subprog))
3958 check_inlining_for_nested_subprog (gnu_subprog);
3959
3960 /* The only way we can be making a call via an access type is if Name is an
3961 explicit dereference. In that case, get the list of formal args from the
3962 type the access type is pointing to. Otherwise, get the formals from the
3963 entity being called. */
3964 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
3965 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
3966 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
3967 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
3968 gnat_formal = Empty;
3969 else
3970 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
3971
3972 /* The lifetime of the temporaries created for the call ends right after the
3973 return value is copied, so we can give them the scope of the elaboration
3974 routine at top level. */
3975 if (!current_function_decl)
3976 {
3977 current_function_decl = get_elaboration_procedure ();
3978 went_into_elab_proc = true;
3979 }
3980
3981 /* First, create the temporary for the return value when:
3982
3983 1. There is no target and the function has copy-in/copy-out parameters,
3984 because we need to preserve the return value before copying back the
3985 parameters.
3986
3987 2. There is no target and this is not an object declaration, and the
3988 return type has variable size, because in these cases the gimplifier
3989 cannot create the temporary.
3990
3991 3. There is a target and it is a slice or an array with fixed size,
3992 and the return type has variable size, because the gimplifier
3993 doesn't handle these cases.
3994
3995 This must be done before we push a binding level around the call, since
3996 we will pop it before copying the return value. */
3997 if (function_call
3998 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
3999 || (!gnu_target
4000 && Nkind (Parent (gnat_node)) != N_Object_Declaration
4001 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)
4002 || (gnu_target
4003 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
4004 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
4005 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
4006 == INTEGER_CST))
4007 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)))
4008 gnu_retval = create_temporary ("R", gnu_result_type);
4009
4010 /* Create the list of the actual parameters as GCC expects it, namely a
4011 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
4012 is an expression and the TREE_PURPOSE field is null. But skip Out
4013 parameters not passed by reference and that need not be copied in. */
4014 for (gnat_actual = First_Actual (gnat_node);
4015 Present (gnat_actual);
4016 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4017 gnat_actual = Next_Actual (gnat_actual))
4018 {
4019 Entity_Id gnat_formal_type = Etype (gnat_formal);
4020 tree gnu_formal = present_gnu_tree (gnat_formal)
4021 ? get_gnu_tree (gnat_formal) : NULL_TREE;
4022 tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type);
4023 const bool is_true_formal_parm
4024 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
4025 const bool is_by_ref_formal_parm
4026 = is_true_formal_parm
4027 && (DECL_BY_REF_P (gnu_formal)
4028 || DECL_BY_COMPONENT_PTR_P (gnu_formal));
4029 /* In the Out or In Out case, we must suppress conversions that yield
4030 an lvalue but can nevertheless cause the creation of a temporary,
4031 because we need the real object in this case, either to pass its
4032 address if it's passed by reference or as target of the back copy
4033 done after the call if it uses the copy-in/copy-out mechanism.
4034 We do it in the In case too, except for an unchecked conversion
4035 to an elementary type or a constrained composite type because it
4036 alone can cause the actual to be misaligned and the addressability
4037 test is applied to the real object. */
4038 const bool suppress_type_conversion
4039 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
4040 && (Ekind (gnat_formal) != E_In_Parameter
4041 || (Is_Composite_Type (Underlying_Type (gnat_formal_type))
4042 && !Is_Constrained (Underlying_Type (gnat_formal_type)))))
4043 || (Nkind (gnat_actual) == N_Type_Conversion
4044 && Is_Composite_Type (Underlying_Type (gnat_formal_type))));
4045 Node_Id gnat_name = suppress_type_conversion
4046 ? Expression (gnat_actual) : gnat_actual;
4047 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
4048 tree gnu_actual;
4049
4050 /* If it's possible we may need to use this expression twice, make sure
4051 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4052 to force side-effects before the call.
4053 ??? This is more conservative than we need since we don't need to do
4054 this for pass-by-ref with no conversion. */
4055 if (Ekind (gnat_formal) != E_In_Parameter)
4056 gnu_name = gnat_stabilize_reference (gnu_name, true, NULL);
4057
4058 /* If we are passing a non-addressable parameter by reference, pass the
4059 address of a copy. In the Out or In Out case, set up to copy back
4060 out after the call. */
4061 if (is_by_ref_formal_parm
4062 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
4063 && !addressable_p (gnu_name, gnu_name_type))
4064 {
4065 bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
4066 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4067
4068 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4069 but sort of an instantiation for them. */
4070 if (TREE_CODE (gnu_name) == CONSTRUCTOR)
4071 ;
4072
4073 /* If the type is passed by reference, a copy is not allowed. */
4074 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type))
4075 post_error ("misaligned actual cannot be passed by reference",
4076 gnat_actual);
4077
4078 /* For users of Starlet we issue a warning because the interface
4079 apparently assumes that by-ref parameters outlive the procedure
4080 invocation. The code still will not work as intended, but we
4081 cannot do much better since low-level parts of the back-end
4082 would allocate temporaries at will because of the misalignment
4083 if we did not do so here. */
4084 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
4085 {
4086 post_error
4087 ("?possible violation of implicit assumption", gnat_actual);
4088 post_error_ne
4089 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
4090 Entity (Name (gnat_node)));
4091 post_error_ne ("?because of misalignment of &", gnat_actual,
4092 gnat_formal);
4093 }
4094
4095 /* If the actual type of the object is already the nominal type,
4096 we have nothing to do, except if the size is self-referential
4097 in which case we'll remove the unpadding below. */
4098 if (TREE_TYPE (gnu_name) == gnu_name_type
4099 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4100 ;
4101
4102 /* Otherwise remove the unpadding from all the objects. */
4103 else if (TREE_CODE (gnu_name) == COMPONENT_REF
4104 && TYPE_IS_PADDING_P
4105 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
4106 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
4107
4108 /* Otherwise convert to the nominal type of the object if needed.
4109 There are several cases in which we need to make the temporary
4110 using this type instead of the actual type of the object when
4111 they are distinct, because the expectations of the callee would
4112 otherwise not be met:
4113 - if it's a justified modular type,
4114 - if the actual type is a smaller form of it,
4115 - if it's a smaller form of the actual type. */
4116 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4117 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4118 || smaller_form_type_p (TREE_TYPE (gnu_name),
4119 gnu_name_type)))
4120 || (INTEGRAL_TYPE_P (gnu_name_type)
4121 && smaller_form_type_p (gnu_name_type,
4122 TREE_TYPE (gnu_name))))
4123 gnu_name = convert (gnu_name_type, gnu_name);
4124
4125 /* If this is an In Out or Out parameter and we're returning a value,
4126 we need to create a temporary for the return value because we must
4127 preserve it before copying back at the very end. */
4128 if (!in_param && returning_value && !gnu_retval)
4129 gnu_retval = create_temporary ("R", gnu_result_type);
4130
4131 /* If we haven't pushed a binding level, push a new one. This will
4132 narrow the lifetime of the temporary we are about to make as much
4133 as possible. The drawback is that we'd need to create a temporary
4134 for the return value, if any (see comment before the loop). So do
4135 it only when this temporary was already created just above. */
4136 if (!pushed_binding_level && !(in_param && returning_value))
4137 {
4138 start_stmt_group ();
4139 gnat_pushlevel ();
4140 pushed_binding_level = true;
4141 }
4142
4143 /* Create an explicit temporary holding the copy. */
4144 gnu_temp
4145 = create_init_temporary ("A", gnu_name, &gnu_stmt, gnat_actual);
4146
4147 /* But initialize it on the fly like for an implicit temporary as
4148 we aren't necessarily having a statement list. */
4149 gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt,
4150 gnu_temp);
4151
4152 /* Set up to move the copy back to the original if needed. */
4153 if (!in_param)
4154 {
4155 /* If the original is a COND_EXPR whose first arm isn't meant to
4156 be further used, just deal with the second arm. This is very
4157 likely the conditional expression built for a check. */
4158 if (TREE_CODE (gnu_orig) == COND_EXPR
4159 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4160 && integer_zerop
4161 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4162 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4163
4164 gnu_stmt
4165 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp);
4166 set_expr_location_from_node (gnu_stmt, gnat_node);
4167
4168 append_to_statement_list (gnu_stmt, &gnu_after_list);
4169 }
4170 }
4171
4172 /* Start from the real object and build the actual. */
4173 gnu_actual = gnu_name;
4174
4175 /* If this is an atomic access of an In or In Out parameter for which
4176 synchronization is required, build the atomic load. */
4177 if (is_true_formal_parm
4178 && !is_by_ref_formal_parm
4179 && Ekind (gnat_formal) != E_Out_Parameter
4180 && atomic_sync_required_p (gnat_actual))
4181 gnu_actual = build_atomic_load (gnu_actual);
4182
4183 /* If this was a procedure call, we may not have removed any padding.
4184 So do it here for the part we will use as an input, if any. */
4185 if (Ekind (gnat_formal) != E_Out_Parameter
4186 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4187 gnu_actual
4188 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4189
4190 /* Put back the conversion we suppressed above in the computation of the
4191 real object. And even if we didn't suppress any conversion there, we
4192 may have suppressed a conversion to the Etype of the actual earlier,
4193 since the parent is a procedure call, so put it back here. */
4194 if (suppress_type_conversion
4195 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4196 gnu_actual
4197 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
4198 gnu_actual, No_Truncation (gnat_actual));
4199 else
4200 gnu_actual
4201 = convert (gnat_to_gnu_type (Etype (gnat_actual)), gnu_actual);
4202
4203 /* Make sure that the actual is in range of the formal's type. */
4204 if (Ekind (gnat_formal) != E_Out_Parameter
4205 && Do_Range_Check (gnat_actual))
4206 gnu_actual
4207 = emit_range_check (gnu_actual, gnat_formal_type, gnat_actual);
4208
4209 /* Unless this is an In parameter, we must remove any justified modular
4210 building from GNU_NAME to get an lvalue. */
4211 if (Ekind (gnat_formal) != E_In_Parameter
4212 && TREE_CODE (gnu_name) == CONSTRUCTOR
4213 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
4214 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
4215 gnu_name
4216 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))), gnu_name);
4217
4218 /* First see if the parameter is passed by reference. */
4219 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
4220 {
4221 if (Ekind (gnat_formal) != E_In_Parameter)
4222 {
4223 /* In Out or Out parameters passed by reference don't use the
4224 copy-in/copy-out mechanism so the address of the real object
4225 must be passed to the function. */
4226 gnu_actual = gnu_name;
4227
4228 /* If we have a padded type, be sure we've removed padding. */
4229 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4230 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
4231 gnu_actual);
4232
4233 /* If we have the constructed subtype of an aliased object
4234 with an unconstrained nominal subtype, the type of the
4235 actual includes the template, although it is formally
4236 constrained. So we need to convert it back to the real
4237 constructed subtype to retrieve the constrained part
4238 and takes its address. */
4239 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4240 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
4241 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
4242 && Is_Array_Type (Underlying_Type (Etype (gnat_actual))))
4243 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4244 gnu_actual);
4245 }
4246
4247 /* There is no need to convert the actual to the formal's type before
4248 taking its address. The only exception is for unconstrained array
4249 types because of the way we build fat pointers. */
4250 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4251 {
4252 /* Put back a view conversion for In Out or Out parameters. */
4253 if (Ekind (gnat_formal) != E_In_Parameter)
4254 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4255 gnu_actual);
4256 gnu_actual = convert (gnu_formal_type, gnu_actual);
4257 }
4258
4259 /* The symmetry of the paths to the type of an entity is broken here
4260 since arguments don't know that they will be passed by ref. */
4261 gnu_formal_type = TREE_TYPE (gnu_formal);
4262 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4263 }
4264
4265 /* Then see if the parameter is an array passed to a foreign convention
4266 subprogram. */
4267 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
4268 {
4269 gnu_formal_type = TREE_TYPE (gnu_formal);
4270 gnu_actual = maybe_implicit_deref (gnu_actual);
4271 gnu_actual = maybe_unconstrained_array (gnu_actual);
4272
4273 if (TYPE_IS_PADDING_P (gnu_formal_type))
4274 {
4275 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
4276 gnu_actual = convert (gnu_formal_type, gnu_actual);
4277 }
4278
4279 /* Take the address of the object and convert to the proper pointer
4280 type. We'd like to actually compute the address of the beginning
4281 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
4282 possibility that the ARRAY_REF might return a constant and we'd be
4283 getting the wrong address. Neither approach is exactly correct,
4284 but this is the most likely to work in all cases. */
4285 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4286 }
4287
4288 /* Otherwise the parameter is passed by copy. */
4289 else
4290 {
4291 tree gnu_size;
4292
4293 if (Ekind (gnat_formal) != E_In_Parameter)
4294 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
4295
4296 /* If we didn't create a PARM_DECL for the formal, this means that
4297 it is an Out parameter not passed by reference and that need not
4298 be copied in. In this case, the value of the actual need not be
4299 read. However, we still need to make sure that its side-effects
4300 are evaluated before the call, so we evaluate its address. */
4301 if (!is_true_formal_parm)
4302 {
4303 if (TREE_SIDE_EFFECTS (gnu_name))
4304 {
4305 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
4306 append_to_statement_list (addr, &gnu_stmt_list);
4307 }
4308 continue;
4309 }
4310
4311 gnu_actual = convert (gnu_formal_type, gnu_actual);
4312
4313 /* If this is 'Null_Parameter, pass a zero even though we are
4314 dereferencing it. */
4315 if (TREE_CODE (gnu_actual) == INDIRECT_REF
4316 && TREE_PRIVATE (gnu_actual)
4317 && (gnu_size = TYPE_SIZE (TREE_TYPE (gnu_actual)))
4318 && TREE_CODE (gnu_size) == INTEGER_CST
4319 && compare_tree_int (gnu_size, BITS_PER_WORD) <= 0)
4320 gnu_actual
4321 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
4322 convert (gnat_type_for_size
4323 (TREE_INT_CST_LOW (gnu_size), 1),
4324 integer_zero_node),
4325 false);
4326 else
4327 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
4328 }
4329
4330 vec_safe_push (gnu_actual_vec, gnu_actual);
4331 }
4332
4333 gnu_call
4334 = build_call_vec (gnu_result_type, gnu_subprog_addr, gnu_actual_vec);
4335 set_expr_location_from_node (gnu_call, gnat_node);
4336
4337 /* If we have created a temporary for the return value, initialize it. */
4338 if (gnu_retval)
4339 {
4340 tree gnu_stmt
4341 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
4342 set_expr_location_from_node (gnu_stmt, gnat_node);
4343 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4344 gnu_call = gnu_retval;
4345 }
4346
4347 /* If this is a subprogram with copy-in/copy-out parameters, we need to
4348 unpack the valued returned from the function into the In Out or Out
4349 parameters. We deal with the function return (if this is an Ada
4350 function) below. */
4351 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4352 {
4353 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
4354 copy-out parameters. */
4355 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
4356 const int length = list_length (gnu_cico_list);
4357
4358 /* The call sequence must contain one and only one call, even though the
4359 function is pure. Save the result into a temporary if needed. */
4360 if (length > 1)
4361 {
4362 if (!gnu_retval)
4363 {
4364 tree gnu_stmt;
4365 /* If we haven't pushed a binding level, push a new one. This
4366 will narrow the lifetime of the temporary we are about to
4367 make as much as possible. */
4368 if (!pushed_binding_level)
4369 {
4370 start_stmt_group ();
4371 gnat_pushlevel ();
4372 pushed_binding_level = true;
4373 }
4374 gnu_call
4375 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
4376 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4377 }
4378
4379 gnu_name_list = nreverse (gnu_name_list);
4380 }
4381
4382 /* The first entry is for the actual return value if this is a
4383 function, so skip it. */
4384 if (function_call)
4385 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4386
4387 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4388 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4389 else
4390 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4391
4392 for (gnat_actual = First_Actual (gnat_node);
4393 Present (gnat_actual);
4394 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4395 gnat_actual = Next_Actual (gnat_actual))
4396 /* If we are dealing with a copy-in/copy-out parameter, we must
4397 retrieve its value from the record returned in the call. */
4398 if (!(present_gnu_tree (gnat_formal)
4399 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4400 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
4401 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))))
4402 && Ekind (gnat_formal) != E_In_Parameter)
4403 {
4404 /* Get the value to assign to this Out or In Out parameter. It is
4405 either the result of the function if there is only a single such
4406 parameter or the appropriate field from the record returned. */
4407 tree gnu_result
4408 = length == 1
4409 ? gnu_call
4410 : build_component_ref (gnu_call, NULL_TREE,
4411 TREE_PURPOSE (gnu_cico_list), false);
4412
4413 /* If the actual is a conversion, get the inner expression, which
4414 will be the real destination, and convert the result to the
4415 type of the actual parameter. */
4416 tree gnu_actual
4417 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
4418
4419 /* If the result is a padded type, remove the padding. */
4420 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4421 gnu_result
4422 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4423 gnu_result);
4424
4425 /* If the actual is a type conversion, the real target object is
4426 denoted by the inner Expression and we need to convert the
4427 result to the associated type.
4428 We also need to convert our gnu assignment target to this type
4429 if the corresponding GNU_NAME was constructed from the GNAT
4430 conversion node and not from the inner Expression. */
4431 if (Nkind (gnat_actual) == N_Type_Conversion)
4432 {
4433 gnu_result
4434 = convert_with_check
4435 (Etype (Expression (gnat_actual)), gnu_result,
4436 Do_Overflow_Check (gnat_actual),
4437 Do_Range_Check (Expression (gnat_actual)),
4438 Float_Truncate (gnat_actual), gnat_actual);
4439
4440 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
4441 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
4442 }
4443
4444 /* Unchecked conversions as actuals for Out parameters are not
4445 allowed in user code because they are not variables, but do
4446 occur in front-end expansions. The associated GNU_NAME is
4447 always obtained from the inner expression in such cases. */
4448 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4449 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
4450 gnu_result,
4451 No_Truncation (gnat_actual));
4452 else
4453 {
4454 if (Do_Range_Check (gnat_actual))
4455 gnu_result
4456 = emit_range_check (gnu_result, Etype (gnat_actual),
4457 gnat_actual);
4458
4459 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
4460 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
4461 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
4462 }
4463
4464 if (atomic_sync_required_p (gnat_actual))
4465 gnu_result = build_atomic_store (gnu_actual, gnu_result);
4466 else
4467 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4468 gnu_actual, gnu_result);
4469 set_expr_location_from_node (gnu_result, gnat_node);
4470 append_to_statement_list (gnu_result, &gnu_stmt_list);
4471 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4472 gnu_name_list = TREE_CHAIN (gnu_name_list);
4473 }
4474 }
4475
4476 /* If this is a function call, the result is the call expression unless a
4477 target is specified, in which case we copy the result into the target
4478 and return the assignment statement. */
4479 if (function_call)
4480 {
4481 /* If this is a function with copy-in/copy-out parameters, extract the
4482 return value from it and update the return type. */
4483 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4484 {
4485 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
4486 gnu_call = build_component_ref (gnu_call, NULL_TREE,
4487 TREE_PURPOSE (gnu_elmt), false);
4488 gnu_result_type = TREE_TYPE (gnu_call);
4489 }
4490
4491 /* If the function returns an unconstrained array or by direct reference,
4492 we have to dereference the pointer. */
4493 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type)
4494 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
4495 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
4496
4497 if (gnu_target)
4498 {
4499 Node_Id gnat_parent = Parent (gnat_node);
4500 enum tree_code op_code;
4501
4502 /* If range check is needed, emit code to generate it. */
4503 if (Do_Range_Check (gnat_node))
4504 gnu_call
4505 = emit_range_check (gnu_call, Etype (Name (gnat_parent)),
4506 gnat_parent);
4507
4508 /* ??? If the return type has variable size, then force the return
4509 slot optimization as we would not be able to create a temporary.
4510 Likewise if it was unconstrained as we would copy too much data.
4511 That's what has been done historically. */
4512 if (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
4513 || (TYPE_IS_PADDING_P (gnu_result_type)
4514 && CONTAINS_PLACEHOLDER_P
4515 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_result_type))))))
4516 op_code = INIT_EXPR;
4517 else
4518 op_code = MODIFY_EXPR;
4519
4520 if (atomic_sync)
4521 gnu_call = build_atomic_store (gnu_target, gnu_call);
4522 else
4523 gnu_call
4524 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
4525 set_expr_location_from_node (gnu_call, gnat_parent);
4526 append_to_statement_list (gnu_call, &gnu_stmt_list);
4527 }
4528 else
4529 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
4530 }
4531
4532 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4533 parameters, the result is just the call statement. */
4534 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
4535 append_to_statement_list (gnu_call, &gnu_stmt_list);
4536
4537 /* Finally, add the copy back statements, if any. */
4538 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
4539
4540 if (went_into_elab_proc)
4541 current_function_decl = NULL_TREE;
4542
4543 /* If we have pushed a binding level, pop it and finish up the enclosing
4544 statement group. */
4545 if (pushed_binding_level)
4546 {
4547 add_stmt (gnu_stmt_list);
4548 gnat_poplevel ();
4549 gnu_result = end_stmt_group ();
4550 }
4551
4552 /* Otherwise, retrieve the statement list, if any. */
4553 else if (gnu_stmt_list)
4554 gnu_result = gnu_stmt_list;
4555
4556 /* Otherwise, just return the call expression. */
4557 else
4558 return gnu_call;
4559
4560 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4561 But first simplify if we have only one statement in the list. */
4562 if (returning_value)
4563 {
4564 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
4565 if (first == last)
4566 gnu_result = first;
4567 gnu_result
4568 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
4569 }
4570
4571 return gnu_result;
4572 }
4573 \f
4574 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4575 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4576
4577 static tree
4578 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
4579 {
4580 tree gnu_jmpsave_decl = NULL_TREE;
4581 tree gnu_jmpbuf_decl = NULL_TREE;
4582 /* If just annotating, ignore all EH and cleanups. */
4583 bool gcc_zcx = (!type_annotate_only
4584 && Present (Exception_Handlers (gnat_node))
4585 && Exception_Mechanism == Back_End_Exceptions);
4586 bool setjmp_longjmp
4587 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
4588 && Exception_Mechanism == Setjmp_Longjmp);
4589 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
4590 bool binding_for_block = (at_end || gcc_zcx || setjmp_longjmp);
4591 tree gnu_inner_block; /* The statement(s) for the block itself. */
4592 tree gnu_result;
4593 tree gnu_expr;
4594 Node_Id gnat_temp;
4595 /* Node providing the sloc for the cleanup actions. */
4596 Node_Id gnat_cleanup_loc_node = (Present (End_Label (gnat_node)) ?
4597 End_Label (gnat_node) :
4598 gnat_node);
4599
4600 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
4601 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
4602 add_cleanup, and when we leave the binding, end_stmt_group will create
4603 the TRY_FINALLY_EXPR.
4604
4605 ??? The region level calls down there have been specifically put in place
4606 for a ZCX context and currently the order in which things are emitted
4607 (region/handlers) is different from the SJLJ case. Instead of putting
4608 other calls with different conditions at other places for the SJLJ case,
4609 it seems cleaner to reorder things for the SJLJ case and generalize the
4610 condition to make it not ZCX specific.
4611
4612 If there are any exceptions or cleanup processing involved, we need an
4613 outer statement group (for Setjmp_Longjmp) and binding level. */
4614 if (binding_for_block)
4615 {
4616 start_stmt_group ();
4617 gnat_pushlevel ();
4618 }
4619
4620 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
4621 area for address of previous buffer. Do this first since we need to have
4622 the setjmp buf known for any decls in this block. */
4623 if (setjmp_longjmp)
4624 {
4625 gnu_jmpsave_decl
4626 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
4627 jmpbuf_ptr_type,
4628 build_call_n_expr (get_jmpbuf_decl, 0),
4629 false, false, false, false, NULL, gnat_node);
4630 DECL_ARTIFICIAL (gnu_jmpsave_decl) = 1;
4631
4632 /* The __builtin_setjmp receivers will immediately reinstall it. Now
4633 because of the unstructured form of EH used by setjmp_longjmp, there
4634 might be forward edges going to __builtin_setjmp receivers on which
4635 it is uninitialized, although they will never be actually taken. */
4636 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
4637 gnu_jmpbuf_decl
4638 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE,
4639 jmpbuf_type,
4640 NULL_TREE,
4641 false, false, false, false, NULL, gnat_node);
4642 DECL_ARTIFICIAL (gnu_jmpbuf_decl) = 1;
4643
4644 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
4645
4646 /* When we exit this block, restore the saved value. */
4647 add_cleanup (build_call_n_expr (set_jmpbuf_decl, 1, gnu_jmpsave_decl),
4648 gnat_cleanup_loc_node);
4649 }
4650
4651 /* If we are to call a function when exiting this block, add a cleanup
4652 to the binding level we made above. Note that add_cleanup is FIFO
4653 so we must register this cleanup after the EH cleanup just above. */
4654 if (at_end)
4655 add_cleanup (build_call_n_expr (gnat_to_gnu (At_End_Proc (gnat_node)), 0),
4656 gnat_cleanup_loc_node);
4657
4658 /* Now build the tree for the declarations and statements inside this block.
4659 If this is SJLJ, set our jmp_buf as the current buffer. */
4660 start_stmt_group ();
4661
4662 if (setjmp_longjmp)
4663 {
4664 gnu_expr = build_call_n_expr (set_jmpbuf_decl, 1,
4665 build_unary_op (ADDR_EXPR, NULL_TREE,
4666 gnu_jmpbuf_decl));
4667 set_expr_location_from_node (gnu_expr, gnat_node);
4668 add_stmt (gnu_expr);
4669 }
4670
4671 if (Present (First_Real_Statement (gnat_node)))
4672 process_decls (Statements (gnat_node), Empty,
4673 First_Real_Statement (gnat_node), true, true);
4674
4675 /* Generate code for each statement in the block. */
4676 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
4677 ? First_Real_Statement (gnat_node)
4678 : First (Statements (gnat_node)));
4679 Present (gnat_temp); gnat_temp = Next (gnat_temp))
4680 add_stmt (gnat_to_gnu (gnat_temp));
4681 gnu_inner_block = end_stmt_group ();
4682
4683 /* Now generate code for the two exception models, if either is relevant for
4684 this block. */
4685 if (setjmp_longjmp)
4686 {
4687 tree *gnu_else_ptr = 0;
4688 tree gnu_handler;
4689
4690 /* Make a binding level for the exception handling declarations and code
4691 and set up gnu_except_ptr_stack for the handlers to use. */
4692 start_stmt_group ();
4693 gnat_pushlevel ();
4694
4695 vec_safe_push (gnu_except_ptr_stack,
4696 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE,
4697 build_pointer_type (except_type_node),
4698 build_call_n_expr (get_excptr_decl, 0),
4699 false, false, false, false,
4700 NULL, gnat_node));
4701
4702 /* Generate code for each handler. The N_Exception_Handler case does the
4703 real work and returns a COND_EXPR for each handler, which we chain
4704 together here. */
4705 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4706 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
4707 {
4708 gnu_expr = gnat_to_gnu (gnat_temp);
4709
4710 /* If this is the first one, set it as the outer one. Otherwise,
4711 point the "else" part of the previous handler to us. Then point
4712 to our "else" part. */
4713 if (!gnu_else_ptr)
4714 add_stmt (gnu_expr);
4715 else
4716 *gnu_else_ptr = gnu_expr;
4717
4718 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
4719 }
4720
4721 /* If none of the exception handlers did anything, re-raise but do not
4722 defer abortion. */
4723 gnu_expr = build_call_n_expr (raise_nodefer_decl, 1,
4724 gnu_except_ptr_stack->last ());
4725 set_expr_location_from_node
4726 (gnu_expr,
4727 Present (End_Label (gnat_node)) ? End_Label (gnat_node) : gnat_node);
4728
4729 if (gnu_else_ptr)
4730 *gnu_else_ptr = gnu_expr;
4731 else
4732 add_stmt (gnu_expr);
4733
4734 /* End the binding level dedicated to the exception handlers and get the
4735 whole statement group. */
4736 gnu_except_ptr_stack->pop ();
4737 gnat_poplevel ();
4738 gnu_handler = end_stmt_group ();
4739
4740 /* If the setjmp returns 1, we restore our incoming longjmp value and
4741 then check the handlers. */
4742 start_stmt_group ();
4743 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl, 1,
4744 gnu_jmpsave_decl),
4745 gnat_node);
4746 add_stmt (gnu_handler);
4747 gnu_handler = end_stmt_group ();
4748
4749 /* This block is now "if (setjmp) ... <handlers> else <block>". */
4750 gnu_result = build3 (COND_EXPR, void_type_node,
4751 (build_call_n_expr
4752 (setjmp_decl, 1,
4753 build_unary_op (ADDR_EXPR, NULL_TREE,
4754 gnu_jmpbuf_decl))),
4755 gnu_handler, gnu_inner_block);
4756 }
4757 else if (gcc_zcx)
4758 {
4759 tree gnu_handlers;
4760 location_t locus;
4761
4762 /* First make a block containing the handlers. */
4763 start_stmt_group ();
4764 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4765 Present (gnat_temp);
4766 gnat_temp = Next_Non_Pragma (gnat_temp))
4767 add_stmt (gnat_to_gnu (gnat_temp));
4768 gnu_handlers = end_stmt_group ();
4769
4770 /* Now make the TRY_CATCH_EXPR for the block. */
4771 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
4772 gnu_inner_block, gnu_handlers);
4773 /* Set a location. We need to find a unique location for the dispatching
4774 code, otherwise we can get coverage or debugging issues. Try with
4775 the location of the end label. */
4776 if (Present (End_Label (gnat_node))
4777 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
4778 SET_EXPR_LOCATION (gnu_result, locus);
4779 else
4780 /* Clear column information so that the exception handler of an
4781 implicit transient block does not incorrectly inherit the slocs
4782 of a decision, which would otherwise confuse control flow based
4783 coverage analysis tools. */
4784 set_expr_location_from_node1 (gnu_result, gnat_node, true);
4785 }
4786 else
4787 gnu_result = gnu_inner_block;
4788
4789 /* Now close our outer block, if we had to make one. */
4790 if (binding_for_block)
4791 {
4792 add_stmt (gnu_result);
4793 gnat_poplevel ();
4794 gnu_result = end_stmt_group ();
4795 }
4796
4797 return gnu_result;
4798 }
4799 \f
4800 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4801 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
4802 exception handling. */
4803
4804 static tree
4805 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
4806 {
4807 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
4808 an "if" statement to select the proper exceptions. For "Others", exclude
4809 exceptions where Handled_By_Others is nonzero unless the All_Others flag
4810 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
4811 tree gnu_choice = boolean_false_node;
4812 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
4813 Node_Id gnat_temp;
4814
4815 for (gnat_temp = First (Exception_Choices (gnat_node));
4816 gnat_temp; gnat_temp = Next (gnat_temp))
4817 {
4818 tree this_choice;
4819
4820 if (Nkind (gnat_temp) == N_Others_Choice)
4821 {
4822 if (All_Others (gnat_temp))
4823 this_choice = boolean_true_node;
4824 else
4825 this_choice
4826 = build_binary_op
4827 (EQ_EXPR, boolean_type_node,
4828 convert
4829 (integer_type_node,
4830 build_component_ref
4831 (build_unary_op
4832 (INDIRECT_REF, NULL_TREE,
4833 gnu_except_ptr_stack->last ()),
4834 get_identifier ("not_handled_by_others"), NULL_TREE,
4835 false)),
4836 integer_zero_node);
4837 }
4838
4839 else if (Nkind (gnat_temp) == N_Identifier
4840 || Nkind (gnat_temp) == N_Expanded_Name)
4841 {
4842 Entity_Id gnat_ex_id = Entity (gnat_temp);
4843 tree gnu_expr;
4844
4845 /* Exception may be a renaming. Recover original exception which is
4846 the one elaborated and registered. */
4847 if (Present (Renamed_Object (gnat_ex_id)))
4848 gnat_ex_id = Renamed_Object (gnat_ex_id);
4849
4850 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4851
4852 this_choice
4853 = build_binary_op
4854 (EQ_EXPR, boolean_type_node,
4855 gnu_except_ptr_stack->last (),
4856 convert (TREE_TYPE (gnu_except_ptr_stack->last ()),
4857 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
4858 }
4859 else
4860 gcc_unreachable ();
4861
4862 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
4863 gnu_choice, this_choice);
4864 }
4865
4866 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
4867 }
4868 \f
4869 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4870 to a GCC tree, which is returned. This is the variant for ZCX. */
4871
4872 static tree
4873 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
4874 {
4875 tree gnu_etypes_list = NULL_TREE;
4876 tree gnu_current_exc_ptr, prev_gnu_incoming_exc_ptr;
4877 Node_Id gnat_temp;
4878
4879 /* We build a TREE_LIST of nodes representing what exception types this
4880 handler can catch, with special cases for others and all others cases.
4881
4882 Each exception type is actually identified by a pointer to the exception
4883 id, or to a dummy object for "others" and "all others". */
4884 for (gnat_temp = First (Exception_Choices (gnat_node));
4885 gnat_temp; gnat_temp = Next (gnat_temp))
4886 {
4887 tree gnu_expr, gnu_etype;
4888
4889 if (Nkind (gnat_temp) == N_Others_Choice)
4890 {
4891 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
4892 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4893 }
4894 else if (Nkind (gnat_temp) == N_Identifier
4895 || Nkind (gnat_temp) == N_Expanded_Name)
4896 {
4897 Entity_Id gnat_ex_id = Entity (gnat_temp);
4898
4899 /* Exception may be a renaming. Recover original exception which is
4900 the one elaborated and registered. */
4901 if (Present (Renamed_Object (gnat_ex_id)))
4902 gnat_ex_id = Renamed_Object (gnat_ex_id);
4903
4904 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4905 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4906 }
4907 else
4908 gcc_unreachable ();
4909
4910 /* The GCC interface expects NULL to be passed for catch all handlers, so
4911 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
4912 is integer_zero_node. It would not work, however, because GCC's
4913 notion of "catch all" is stronger than our notion of "others". Until
4914 we correctly use the cleanup interface as well, doing that would
4915 prevent the "all others" handlers from being seen, because nothing
4916 can be caught beyond a catch all from GCC's point of view. */
4917 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
4918 }
4919
4920 start_stmt_group ();
4921 gnat_pushlevel ();
4922
4923 /* Expand a call to the begin_handler hook at the beginning of the handler,
4924 and arrange for a call to the end_handler hook to occur on every possible
4925 exit path.
4926
4927 The hooks expect a pointer to the low level occurrence. This is required
4928 for our stack management scheme because a raise inside the handler pushes
4929 a new occurrence on top of the stack, which means that this top does not
4930 necessarily match the occurrence this handler was dealing with.
4931
4932 __builtin_eh_pointer references the exception occurrence being
4933 propagated. Upon handler entry, this is the exception for which the
4934 handler is triggered. This might not be the case upon handler exit,
4935 however, as we might have a new occurrence propagated by the handler's
4936 body, and the end_handler hook called as a cleanup in this context.
4937
4938 We use a local variable to retrieve the incoming value at handler entry
4939 time, and reuse it to feed the end_handler hook's argument at exit. */
4940
4941 gnu_current_exc_ptr
4942 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
4943 1, integer_zero_node);
4944 prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
4945 gnu_incoming_exc_ptr = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
4946 ptr_type_node, gnu_current_exc_ptr,
4947 false, false, false, false,
4948 NULL, gnat_node);
4949
4950 add_stmt_with_node (build_call_n_expr (begin_handler_decl, 1,
4951 gnu_incoming_exc_ptr),
4952 gnat_node);
4953
4954 /* Declare and initialize the choice parameter, if present. */
4955 if (Present (Choice_Parameter (gnat_node)))
4956 {
4957 tree gnu_param
4958 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, 1);
4959
4960 add_stmt (build_call_n_expr
4961 (set_exception_parameter_decl, 2,
4962 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
4963 gnu_incoming_exc_ptr));
4964 }
4965
4966 /* We don't have an End_Label at hand to set the location of the cleanup
4967 actions, so we use that of the exception handler itself instead. */
4968 add_cleanup (build_call_n_expr (end_handler_decl, 1, gnu_incoming_exc_ptr),
4969 gnat_node);
4970 add_stmt_list (Statements (gnat_node));
4971 gnat_poplevel ();
4972
4973 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
4974
4975 return
4976 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
4977 }
4978 \f
4979 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
4980
4981 static void
4982 Compilation_Unit_to_gnu (Node_Id gnat_node)
4983 {
4984 const Node_Id gnat_unit = Unit (gnat_node);
4985 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
4986 || Nkind (gnat_unit) == N_Subprogram_Body);
4987 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
4988 Entity_Id gnat_entity;
4989 Node_Id gnat_pragma;
4990 /* Make the decl for the elaboration procedure. */
4991 tree gnu_elab_proc_decl
4992 = create_subprog_decl
4993 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
4994 NULL_TREE, void_ftype, NULL_TREE, is_disabled, true, false, true, NULL,
4995 gnat_unit);
4996 struct elab_info *info;
4997
4998 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
4999 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
5000
5001 /* Initialize the information structure for the function. */
5002 allocate_struct_function (gnu_elab_proc_decl, false);
5003 set_cfun (NULL);
5004
5005 current_function_decl = NULL_TREE;
5006
5007 start_stmt_group ();
5008 gnat_pushlevel ();
5009
5010 /* For a body, first process the spec if there is one. */
5011 if (Nkind (gnat_unit) == N_Package_Body
5012 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
5013 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
5014
5015 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
5016 {
5017 elaborate_all_entities (gnat_node);
5018
5019 if (Nkind (gnat_unit) == N_Subprogram_Declaration
5020 || Nkind (gnat_unit) == N_Generic_Package_Declaration
5021 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
5022 return;
5023 }
5024
5025 /* Then process any pragmas and declarations preceding the unit. */
5026 for (gnat_pragma = First (Context_Items (gnat_node));
5027 Present (gnat_pragma);
5028 gnat_pragma = Next (gnat_pragma))
5029 if (Nkind (gnat_pragma) == N_Pragma)
5030 add_stmt (gnat_to_gnu (gnat_pragma));
5031 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
5032 true, true);
5033
5034 /* Process the unit itself. */
5035 add_stmt (gnat_to_gnu (gnat_unit));
5036
5037 /* Generate code for all the inlined subprograms. */
5038 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5039 Present (gnat_entity);
5040 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
5041 {
5042 Node_Id gnat_body;
5043
5044 /* Without optimization, process only the required subprograms. */
5045 if (!optimize && !Has_Pragma_Inline_Always (gnat_entity))
5046 continue;
5047
5048 gnat_body = Parent (Declaration_Node (gnat_entity));
5049 if (Nkind (gnat_body) != N_Subprogram_Body)
5050 {
5051 /* ??? This happens when only the spec of a package is provided. */
5052 if (No (Corresponding_Body (gnat_body)))
5053 continue;
5054
5055 gnat_body
5056 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
5057 }
5058
5059 /* Define the entity first so we set DECL_EXTERNAL. */
5060 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5061 add_stmt (gnat_to_gnu (gnat_body));
5062 }
5063
5064 /* Process any pragmas and actions following the unit. */
5065 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5066 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
5067 finalize_from_limited_with ();
5068
5069 /* Save away what we've made so far and record this potential elaboration
5070 procedure. */
5071 info = ggc_alloc<elab_info> ();
5072 set_current_block_context (gnu_elab_proc_decl);
5073 gnat_poplevel ();
5074 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
5075
5076 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
5077
5078 info->next = elab_info_list;
5079 info->elab_proc = gnu_elab_proc_decl;
5080 info->gnat_node = gnat_node;
5081 elab_info_list = info;
5082
5083 /* Generate elaboration code for this unit, if necessary, and say whether
5084 we did or not. */
5085 gnu_elab_proc_stack->pop ();
5086
5087 /* Invalidate the global renaming pointers. This is necessary because
5088 stabilization of the renamed entities may create SAVE_EXPRs which
5089 have been tied to a specific elaboration routine just above. */
5090 invalidate_global_renaming_pointers ();
5091
5092 /* Force the processing for all nodes that remain in the queue. */
5093 process_deferred_decl_context (true);
5094 }
5095 \f
5096 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Raise_xxx_Error,
5097 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where
5098 we should place the result type. LABEL_P is true if there is a label to
5099 branch to for the exception. */
5100
5101 static tree
5102 Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5103 {
5104 const Node_Kind kind = Nkind (gnat_node);
5105 const int reason = UI_To_Int (Reason (gnat_node));
5106 const Node_Id gnat_cond = Condition (gnat_node);
5107 const bool with_extra_info
5108 = Exception_Extra_Info
5109 && !No_Exception_Handlers_Set ()
5110 && !get_exception_label (kind);
5111 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
5112
5113 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5114
5115 switch (reason)
5116 {
5117 case CE_Access_Check_Failed:
5118 if (with_extra_info)
5119 gnu_result = build_call_raise_column (reason, gnat_node);
5120 break;
5121
5122 case CE_Index_Check_Failed:
5123 case CE_Range_Check_Failed:
5124 case CE_Invalid_Data:
5125 if (Present (gnat_cond) && Nkind (gnat_cond) == N_Op_Not)
5126 {
5127 Node_Id gnat_range, gnat_index, gnat_type;
5128 tree gnu_index, gnu_low_bound, gnu_high_bound;
5129 struct range_check_info_d *rci;
5130
5131 switch (Nkind (Right_Opnd (gnat_cond)))
5132 {
5133 case N_In:
5134 gnat_range = Right_Opnd (Right_Opnd (gnat_cond));
5135 gcc_assert (Nkind (gnat_range) == N_Range);
5136 gnu_low_bound = gnat_to_gnu (Low_Bound (gnat_range));
5137 gnu_high_bound = gnat_to_gnu (High_Bound (gnat_range));
5138 break;
5139
5140 case N_Op_Ge:
5141 gnu_low_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5142 gnu_high_bound = NULL_TREE;
5143 break;
5144
5145 case N_Op_Le:
5146 gnu_low_bound = NULL_TREE;
5147 gnu_high_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5148 break;
5149
5150 default:
5151 goto common;
5152 }
5153
5154 gnat_index = Left_Opnd (Right_Opnd (gnat_cond));
5155 gnat_type = Etype (gnat_index);
5156 gnu_index = gnat_to_gnu (gnat_index);
5157
5158 if (with_extra_info
5159 && gnu_low_bound
5160 && gnu_high_bound
5161 && Known_Esize (gnat_type)
5162 && UI_To_Int (Esize (gnat_type)) <= 32)
5163 gnu_result
5164 = build_call_raise_range (reason, gnat_node, gnu_index,
5165 gnu_low_bound, gnu_high_bound);
5166
5167 /* If loop unswitching is enabled, we try to compute invariant
5168 conditions for checks applied to iteration variables, i.e.
5169 conditions that are both independent of the variable and
5170 necessary in order for the check to fail in the course of
5171 some iteration, and prepend them to the original condition
5172 of the checks. This will make it possible later for the
5173 loop unswitching pass to replace the loop with two loops,
5174 one of which has the checks eliminated and the other has
5175 the original checks reinstated, and a run time selection.
5176 The former loop will be suitable for vectorization. */
5177 if (flag_unswitch_loops
5178 && !vec_safe_is_empty (gnu_loop_stack)
5179 && (!gnu_low_bound
5180 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
5181 && (!gnu_high_bound
5182 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
5183 && (rci = push_range_check_info (gnu_index)))
5184 {
5185 rci->low_bound = gnu_low_bound;
5186 rci->high_bound = gnu_high_bound;
5187 rci->type = get_unpadded_type (gnat_type);
5188 rci->invariant_cond = build1 (SAVE_EXPR, boolean_type_node,
5189 boolean_true_node);
5190 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5191 boolean_type_node,
5192 rci->invariant_cond,
5193 gnat_to_gnu (gnat_cond));
5194 }
5195 }
5196 break;
5197
5198 default:
5199 break;
5200 }
5201
5202 common:
5203 if (!gnu_result)
5204 gnu_result = build_call_raise (reason, gnat_node, kind);
5205 set_expr_location_from_node (gnu_result, gnat_node);
5206
5207 /* If the type is VOID, this is a statement, so we need to generate the code
5208 for the call. Handle a condition, if there is one. */
5209 if (VOID_TYPE_P (*gnu_result_type_p))
5210 {
5211 if (Present (gnat_cond))
5212 {
5213 if (!gnu_cond)
5214 gnu_cond = gnat_to_gnu (gnat_cond);
5215 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
5216 alloc_stmt_list ());
5217 }
5218 }
5219 else
5220 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
5221
5222 return gnu_result;
5223 }
5224 \f
5225 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
5226 parameter of a call. */
5227
5228 static bool
5229 lhs_or_actual_p (Node_Id gnat_node)
5230 {
5231 Node_Id gnat_parent = Parent (gnat_node);
5232 Node_Kind kind = Nkind (gnat_parent);
5233
5234 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
5235 return true;
5236
5237 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
5238 && Name (gnat_parent) != gnat_node)
5239 return true;
5240
5241 if (kind == N_Parameter_Association)
5242 return true;
5243
5244 return false;
5245 }
5246
5247 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
5248 of an assignment or an actual parameter of a call. */
5249
5250 static bool
5251 present_in_lhs_or_actual_p (Node_Id gnat_node)
5252 {
5253 Node_Kind kind;
5254
5255 if (lhs_or_actual_p (gnat_node))
5256 return true;
5257
5258 kind = Nkind (Parent (gnat_node));
5259
5260 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
5261 && lhs_or_actual_p (Parent (gnat_node)))
5262 return true;
5263
5264 return false;
5265 }
5266
5267 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
5268 as gigi is concerned. This is used to avoid conversions on the LHS. */
5269
5270 static bool
5271 unchecked_conversion_nop (Node_Id gnat_node)
5272 {
5273 Entity_Id from_type, to_type;
5274
5275 /* The conversion must be on the LHS of an assignment or an actual parameter
5276 of a call. Otherwise, even if the conversion was essentially a no-op, it
5277 could de facto ensure type consistency and this should be preserved. */
5278 if (!lhs_or_actual_p (gnat_node))
5279 return false;
5280
5281 from_type = Etype (Expression (gnat_node));
5282
5283 /* We're interested in artificial conversions generated by the front-end
5284 to make private types explicit, e.g. in Expand_Assign_Array. */
5285 if (!Is_Private_Type (from_type))
5286 return false;
5287
5288 from_type = Underlying_Type (from_type);
5289 to_type = Etype (gnat_node);
5290
5291 /* The direct conversion to the underlying type is a no-op. */
5292 if (to_type == from_type)
5293 return true;
5294
5295 /* For an array subtype, the conversion to the PAIT is a no-op. */
5296 if (Ekind (from_type) == E_Array_Subtype
5297 && to_type == Packed_Array_Impl_Type (from_type))
5298 return true;
5299
5300 /* For a record subtype, the conversion to the type is a no-op. */
5301 if (Ekind (from_type) == E_Record_Subtype
5302 && to_type == Etype (from_type))
5303 return true;
5304
5305 return false;
5306 }
5307
5308 /* This function is the driver of the GNAT to GCC tree transformation process.
5309 It is the entry point of the tree transformer. GNAT_NODE is the root of
5310 some GNAT tree. Return the root of the corresponding GCC tree. If this
5311 is an expression, return the GCC equivalent of the expression. If this
5312 is a statement, return the statement or add it to the current statement
5313 group, in which case anything returned is to be interpreted as occurring
5314 after anything added. */
5315
5316 tree
5317 gnat_to_gnu (Node_Id gnat_node)
5318 {
5319 const Node_Kind kind = Nkind (gnat_node);
5320 bool went_into_elab_proc = false;
5321 tree gnu_result = error_mark_node; /* Default to no value. */
5322 tree gnu_result_type = void_type_node;
5323 tree gnu_expr, gnu_lhs, gnu_rhs;
5324 Node_Id gnat_temp;
5325
5326 /* Save node number for error message and set location information. */
5327 error_gnat_node = gnat_node;
5328 Sloc_to_locus (Sloc (gnat_node), &input_location);
5329
5330 /* If this node is a statement and we are only annotating types, return an
5331 empty statement list. */
5332 if (type_annotate_only && IN (kind, N_Statement_Other_Than_Procedure_Call))
5333 return alloc_stmt_list ();
5334
5335 /* If this node is a non-static subexpression and we are only annotating
5336 types, make this into a NULL_EXPR. */
5337 if (type_annotate_only
5338 && IN (kind, N_Subexpr)
5339 && kind != N_Identifier
5340 && !Compile_Time_Known_Value (gnat_node))
5341 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
5342 build_call_raise (CE_Range_Check_Failed, gnat_node,
5343 N_Raise_Constraint_Error));
5344
5345 if ((IN (kind, N_Statement_Other_Than_Procedure_Call)
5346 && kind != N_Null_Statement)
5347 || kind == N_Procedure_Call_Statement
5348 || kind == N_Label
5349 || kind == N_Implicit_Label_Declaration
5350 || kind == N_Handled_Sequence_Of_Statements
5351 || (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void))
5352 {
5353 tree current_elab_proc = get_elaboration_procedure ();
5354
5355 /* If this is a statement and we are at top level, it must be part of
5356 the elaboration procedure, so mark us as being in that procedure. */
5357 if (!current_function_decl)
5358 {
5359 current_function_decl = current_elab_proc;
5360 went_into_elab_proc = true;
5361 }
5362
5363 /* If we are in the elaboration procedure, check if we are violating a
5364 No_Elaboration_Code restriction by having a statement there. Don't
5365 check for a possible No_Elaboration_Code restriction violation on
5366 N_Handled_Sequence_Of_Statements, as we want to signal an error on
5367 every nested real statement instead. This also avoids triggering
5368 spurious errors on dummy (empty) sequences created by the front-end
5369 for package bodies in some cases. */
5370 if (current_function_decl == current_elab_proc
5371 && kind != N_Handled_Sequence_Of_Statements)
5372 Check_Elaboration_Code_Allowed (gnat_node);
5373 }
5374
5375 switch (kind)
5376 {
5377 /********************************/
5378 /* Chapter 2: Lexical Elements */
5379 /********************************/
5380
5381 case N_Identifier:
5382 case N_Expanded_Name:
5383 case N_Operator_Symbol:
5384 case N_Defining_Identifier:
5385 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
5386
5387 /* If this is an atomic access on the RHS for which synchronization is
5388 required, build the atomic load. */
5389 if (atomic_sync_required_p (gnat_node)
5390 && !present_in_lhs_or_actual_p (gnat_node))
5391 gnu_result = build_atomic_load (gnu_result);
5392 break;
5393
5394 case N_Integer_Literal:
5395 {
5396 tree gnu_type;
5397
5398 /* Get the type of the result, looking inside any padding and
5399 justified modular types. Then get the value in that type. */
5400 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
5401
5402 if (TREE_CODE (gnu_type) == RECORD_TYPE
5403 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
5404 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
5405
5406 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
5407
5408 /* If the result overflows (meaning it doesn't fit in its base type),
5409 abort. We would like to check that the value is within the range
5410 of the subtype, but that causes problems with subtypes whose usage
5411 will raise Constraint_Error and with biased representation, so
5412 we don't. */
5413 gcc_assert (!TREE_OVERFLOW (gnu_result));
5414 }
5415 break;
5416
5417 case N_Character_Literal:
5418 /* If a Entity is present, it means that this was one of the
5419 literals in a user-defined character type. In that case,
5420 just return the value in the CONST_DECL. Otherwise, use the
5421 character code. In that case, the base type should be an
5422 INTEGER_TYPE, but we won't bother checking for that. */
5423 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5424 if (Present (Entity (gnat_node)))
5425 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
5426 else
5427 gnu_result
5428 = build_int_cst_type
5429 (gnu_result_type, UI_To_CC (Char_Literal_Value (gnat_node)));
5430 break;
5431
5432 case N_Real_Literal:
5433 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5434
5435 /* If this is of a fixed-point type, the value we want is the value of
5436 the corresponding integer. */
5437 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
5438 {
5439 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
5440 gnu_result_type);
5441 gcc_assert (!TREE_OVERFLOW (gnu_result));
5442 }
5443
5444 else
5445 {
5446 Ureal ur_realval = Realval (gnat_node);
5447
5448 /* First convert the value to a machine number if it isn't already.
5449 That will force the base to 2 for non-zero values and simplify
5450 the rest of the logic. */
5451 if (!Is_Machine_Number (gnat_node))
5452 ur_realval
5453 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
5454 ur_realval, Round_Even, gnat_node);
5455
5456 if (UR_Is_Zero (ur_realval))
5457 gnu_result = convert (gnu_result_type, integer_zero_node);
5458 else
5459 {
5460 REAL_VALUE_TYPE tmp;
5461
5462 gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
5463
5464 /* The base must be 2 as Machine guarantees this, so we scale
5465 the value, which we know can fit in the mantissa of the type
5466 (hence the use of that type above). */
5467 gcc_assert (Rbase (ur_realval) == 2);
5468 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
5469 - UI_To_Int (Denominator (ur_realval)));
5470 gnu_result = build_real (gnu_result_type, tmp);
5471 }
5472
5473 /* Now see if we need to negate the result. Do it this way to
5474 properly handle -0. */
5475 if (UR_Is_Negative (Realval (gnat_node)))
5476 gnu_result
5477 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
5478 gnu_result);
5479 }
5480
5481 break;
5482
5483 case N_String_Literal:
5484 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5485 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
5486 {
5487 String_Id gnat_string = Strval (gnat_node);
5488 int length = String_Length (gnat_string);
5489 int i;
5490 char *string;
5491 if (length >= ALLOCA_THRESHOLD)
5492 string = XNEWVEC (char, length + 1);
5493 else
5494 string = (char *) alloca (length + 1);
5495
5496 /* Build the string with the characters in the literal. Note
5497 that Ada strings are 1-origin. */
5498 for (i = 0; i < length; i++)
5499 string[i] = Get_String_Char (gnat_string, i + 1);
5500
5501 /* Put a null at the end of the string in case it's in a context
5502 where GCC will want to treat it as a C string. */
5503 string[i] = 0;
5504
5505 gnu_result = build_string (length, string);
5506
5507 /* Strings in GCC don't normally have types, but we want
5508 this to not be converted to the array type. */
5509 TREE_TYPE (gnu_result) = gnu_result_type;
5510
5511 if (length >= ALLOCA_THRESHOLD)
5512 free (string);
5513 }
5514 else
5515 {
5516 /* Build a list consisting of each character, then make
5517 the aggregate. */
5518 String_Id gnat_string = Strval (gnat_node);
5519 int length = String_Length (gnat_string);
5520 int i;
5521 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5522 tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node);
5523 vec<constructor_elt, va_gc> *gnu_vec;
5524 vec_alloc (gnu_vec, length);
5525
5526 for (i = 0; i < length; i++)
5527 {
5528 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
5529 Get_String_Char (gnat_string, i + 1));
5530
5531 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
5532 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node);
5533 }
5534
5535 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
5536 }
5537 break;
5538
5539 case N_Pragma:
5540 gnu_result = Pragma_to_gnu (gnat_node);
5541 break;
5542
5543 /**************************************/
5544 /* Chapter 3: Declarations and Types */
5545 /**************************************/
5546
5547 case N_Subtype_Declaration:
5548 case N_Full_Type_Declaration:
5549 case N_Incomplete_Type_Declaration:
5550 case N_Private_Type_Declaration:
5551 case N_Private_Extension_Declaration:
5552 case N_Task_Type_Declaration:
5553 process_type (Defining_Entity (gnat_node));
5554 gnu_result = alloc_stmt_list ();
5555 break;
5556
5557 case N_Object_Declaration:
5558 case N_Exception_Declaration:
5559 gnat_temp = Defining_Entity (gnat_node);
5560 gnu_result = alloc_stmt_list ();
5561
5562 /* If we are just annotating types and this object has an unconstrained
5563 or task type, don't elaborate it. */
5564 if (type_annotate_only
5565 && (((Is_Array_Type (Etype (gnat_temp))
5566 || Is_Record_Type (Etype (gnat_temp)))
5567 && !Is_Constrained (Etype (gnat_temp)))
5568 || Is_Concurrent_Type (Etype (gnat_temp))))
5569 break;
5570
5571 if (Present (Expression (gnat_node))
5572 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
5573 && (!type_annotate_only
5574 || Compile_Time_Known_Value (Expression (gnat_node))))
5575 {
5576 gnu_expr = gnat_to_gnu (Expression (gnat_node));
5577 if (Do_Range_Check (Expression (gnat_node)))
5578 gnu_expr
5579 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
5580
5581 /* If this object has its elaboration delayed, we must force
5582 evaluation of GNU_EXPR right now and save it for when the object
5583 is frozen. */
5584 if (Present (Freeze_Node (gnat_temp)))
5585 {
5586 if (TREE_CONSTANT (gnu_expr))
5587 ;
5588 else if (global_bindings_p ())
5589 gnu_expr
5590 = create_var_decl (create_concat_name (gnat_temp, "init"),
5591 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5592 false, false, false, false,
5593 NULL, gnat_temp);
5594 else
5595 gnu_expr = gnat_save_expr (gnu_expr);
5596
5597 save_gnu_tree (gnat_node, gnu_expr, true);
5598 }
5599 }
5600 else
5601 gnu_expr = NULL_TREE;
5602
5603 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
5604 gnu_expr = NULL_TREE;
5605
5606 /* If this is a deferred constant with an address clause, we ignore the
5607 full view since the clause is on the partial view and we cannot have
5608 2 different GCC trees for the object. The only bits of the full view
5609 we will use is the initializer, but it will be directly fetched. */
5610 if (Ekind(gnat_temp) == E_Constant
5611 && Present (Address_Clause (gnat_temp))
5612 && Present (Full_View (gnat_temp)))
5613 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
5614
5615 if (No (Freeze_Node (gnat_temp)))
5616 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
5617 break;
5618
5619 case N_Object_Renaming_Declaration:
5620 gnat_temp = Defining_Entity (gnat_node);
5621
5622 /* Don't do anything if this renaming is handled by the front end or if
5623 we are just annotating types and this object has a composite or task
5624 type, don't elaborate it. We return the result in case it has any
5625 SAVE_EXPRs in it that need to be evaluated here. */
5626 if (!Is_Renaming_Of_Object (gnat_temp)
5627 && ! (type_annotate_only
5628 && (Is_Array_Type (Etype (gnat_temp))
5629 || Is_Record_Type (Etype (gnat_temp))
5630 || Is_Concurrent_Type (Etype (gnat_temp)))))
5631 gnu_result
5632 = gnat_to_gnu_entity (gnat_temp,
5633 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
5634 else
5635 gnu_result = alloc_stmt_list ();
5636 break;
5637
5638 case N_Exception_Renaming_Declaration:
5639 gnat_temp = Defining_Entity (gnat_node);
5640 if (Renamed_Entity (gnat_temp) != Empty)
5641 gnu_result
5642 = gnat_to_gnu_entity (gnat_temp,
5643 gnat_to_gnu (Renamed_Entity (gnat_temp)), 1);
5644 else
5645 gnu_result = alloc_stmt_list ();
5646 break;
5647
5648 case N_Implicit_Label_Declaration:
5649 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
5650 gnu_result = alloc_stmt_list ();
5651 break;
5652
5653 case N_Number_Declaration:
5654 case N_Subprogram_Renaming_Declaration:
5655 case N_Package_Renaming_Declaration:
5656 /* These are fully handled in the front end. */
5657 /* ??? For package renamings, find a way to use GENERIC namespaces so
5658 that we get proper debug information for them. */
5659 gnu_result = alloc_stmt_list ();
5660 break;
5661
5662 /*************************************/
5663 /* Chapter 4: Names and Expressions */
5664 /*************************************/
5665
5666 case N_Explicit_Dereference:
5667 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5668 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5669 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
5670
5671 /* If this is an atomic access on the RHS for which synchronization is
5672 required, build the atomic load. */
5673 if (atomic_sync_required_p (gnat_node)
5674 && !present_in_lhs_or_actual_p (gnat_node))
5675 gnu_result = build_atomic_load (gnu_result);
5676 break;
5677
5678 case N_Indexed_Component:
5679 {
5680 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
5681 tree gnu_type;
5682 int ndim;
5683 int i;
5684 Node_Id *gnat_expr_array;
5685
5686 gnu_array_object = maybe_implicit_deref (gnu_array_object);
5687
5688 /* Convert vector inputs to their representative array type, to fit
5689 what the code below expects. */
5690 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
5691 {
5692 if (present_in_lhs_or_actual_p (gnat_node))
5693 gnat_mark_addressable (gnu_array_object);
5694 gnu_array_object = maybe_vector_array (gnu_array_object);
5695 }
5696
5697 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
5698
5699 /* If we got a padded type, remove it too. */
5700 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
5701 gnu_array_object
5702 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
5703 gnu_array_object);
5704
5705 gnu_result = gnu_array_object;
5706
5707 /* The failure of this assertion will very likely come from a missing
5708 expansion for a packed array access. */
5709 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
5710
5711 /* First compute the number of dimensions of the array, then
5712 fill the expression array, the order depending on whether
5713 this is a Convention_Fortran array or not. */
5714 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
5715 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5716 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
5717 ndim++, gnu_type = TREE_TYPE (gnu_type))
5718 ;
5719
5720 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
5721
5722 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
5723 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
5724 i >= 0;
5725 i--, gnat_temp = Next (gnat_temp))
5726 gnat_expr_array[i] = gnat_temp;
5727 else
5728 for (i = 0, gnat_temp = First (Expressions (gnat_node));
5729 i < ndim;
5730 i++, gnat_temp = Next (gnat_temp))
5731 gnat_expr_array[i] = gnat_temp;
5732
5733 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
5734 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
5735 {
5736 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
5737 gnat_temp = gnat_expr_array[i];
5738 gnu_expr = gnat_to_gnu (gnat_temp);
5739
5740 if (Do_Range_Check (gnat_temp))
5741 gnu_expr
5742 = emit_index_check
5743 (gnu_array_object, gnu_expr,
5744 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5745 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5746 gnat_temp);
5747
5748 gnu_result
5749 = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr);
5750
5751 /* Array accesses are bound-checked so they cannot trap, but this
5752 is valid only if they are not hoisted ahead of the check. We
5753 need to mark them as no-trap to get decent loop optimizations
5754 in the presence of -fnon-call-exceptions, so we do it when we
5755 know that the original expression had no side-effects. */
5756 if (TREE_CODE (gnu_result) == ARRAY_REF
5757 && !(Nkind (gnat_temp) == N_Identifier
5758 && Ekind (Entity (gnat_temp)) == E_Constant))
5759 TREE_THIS_NOTRAP (gnu_result) = 1;
5760 }
5761
5762 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5763
5764 /* If this is an atomic access on the RHS for which synchronization is
5765 required, build the atomic load. */
5766 if (atomic_sync_required_p (gnat_node)
5767 && !present_in_lhs_or_actual_p (gnat_node))
5768 gnu_result = build_atomic_load (gnu_result);
5769 }
5770 break;
5771
5772 case N_Slice:
5773 {
5774 Node_Id gnat_range_node = Discrete_Range (gnat_node);
5775 tree gnu_type;
5776
5777 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5778 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5779
5780 /* Do any implicit dereferences of the prefix and do any needed
5781 range check. */
5782 gnu_result = maybe_implicit_deref (gnu_result);
5783 gnu_result = maybe_unconstrained_array (gnu_result);
5784 gnu_type = TREE_TYPE (gnu_result);
5785 if (Do_Range_Check (gnat_range_node))
5786 {
5787 /* Get the bounds of the slice. */
5788 tree gnu_index_type
5789 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
5790 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
5791 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
5792 /* Get the permitted bounds. */
5793 tree gnu_base_index_type
5794 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
5795 tree gnu_base_min_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5796 (TYPE_MIN_VALUE (gnu_base_index_type), gnu_result);
5797 tree gnu_base_max_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5798 (TYPE_MAX_VALUE (gnu_base_index_type), gnu_result);
5799 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
5800
5801 gnu_min_expr = gnat_protect_expr (gnu_min_expr);
5802 gnu_max_expr = gnat_protect_expr (gnu_max_expr);
5803
5804 /* Derive a good type to convert everything to. */
5805 gnu_expr_type = get_base_type (gnu_index_type);
5806
5807 /* Test whether the minimum slice value is too small. */
5808 gnu_expr_l = build_binary_op (LT_EXPR, boolean_type_node,
5809 convert (gnu_expr_type,
5810 gnu_min_expr),
5811 convert (gnu_expr_type,
5812 gnu_base_min_expr));
5813
5814 /* Test whether the maximum slice value is too large. */
5815 gnu_expr_h = build_binary_op (GT_EXPR, boolean_type_node,
5816 convert (gnu_expr_type,
5817 gnu_max_expr),
5818 convert (gnu_expr_type,
5819 gnu_base_max_expr));
5820
5821 /* Build a slice index check that returns the low bound,
5822 assuming the slice is not empty. */
5823 gnu_expr = emit_check
5824 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
5825 gnu_expr_l, gnu_expr_h),
5826 gnu_min_expr, CE_Index_Check_Failed, gnat_node);
5827
5828 /* Build a conditional expression that does the index checks and
5829 returns the low bound if the slice is not empty (max >= min),
5830 and returns the naked low bound otherwise (max < min), unless
5831 it is non-constant and the high bound is; this prevents VRP
5832 from inferring bogus ranges on the unlikely path. */
5833 gnu_expr = fold_build3 (COND_EXPR, gnu_expr_type,
5834 build_binary_op (GE_EXPR, gnu_expr_type,
5835 convert (gnu_expr_type,
5836 gnu_max_expr),
5837 convert (gnu_expr_type,
5838 gnu_min_expr)),
5839 gnu_expr,
5840 TREE_CODE (gnu_min_expr) != INTEGER_CST
5841 && TREE_CODE (gnu_max_expr) == INTEGER_CST
5842 ? gnu_max_expr : gnu_min_expr);
5843 }
5844 else
5845 /* Simply return the naked low bound. */
5846 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5847
5848 /* If this is a slice with non-constant size of an array with constant
5849 size, set the maximum size for the allocation of temporaries. */
5850 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
5851 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_type)))
5852 TYPE_ARRAY_MAX_SIZE (gnu_result_type) = TYPE_SIZE_UNIT (gnu_type);
5853
5854 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
5855 gnu_result, gnu_expr);
5856 }
5857 break;
5858
5859 case N_Selected_Component:
5860 {
5861 Entity_Id gnat_prefix = Prefix (gnat_node);
5862 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
5863 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
5864 tree gnu_field;
5865
5866 gnu_prefix = maybe_implicit_deref (gnu_prefix);
5867
5868 /* For discriminant references in tagged types always substitute the
5869 corresponding discriminant as the actual selected component. */
5870 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix))))
5871 while (Present (Corresponding_Discriminant (gnat_field)))
5872 gnat_field = Corresponding_Discriminant (gnat_field);
5873
5874 /* For discriminant references of untagged types always substitute the
5875 corresponding stored discriminant. */
5876 else if (Present (Corresponding_Discriminant (gnat_field)))
5877 gnat_field = Original_Record_Component (gnat_field);
5878
5879 /* Handle extracting the real or imaginary part of a complex.
5880 The real part is the first field and the imaginary the last. */
5881 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
5882 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
5883 ? REALPART_EXPR : IMAGPART_EXPR,
5884 NULL_TREE, gnu_prefix);
5885 else
5886 {
5887 gnu_field = gnat_to_gnu_field_decl (gnat_field);
5888
5889 /* If there are discriminants, the prefix might be evaluated more
5890 than once, which is a problem if it has side-effects. */
5891 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
5892 ? Designated_Type (Etype
5893 (Prefix (gnat_node)))
5894 : Etype (Prefix (gnat_node))))
5895 gnu_prefix = gnat_stabilize_reference (gnu_prefix, false, NULL);
5896
5897 gnu_result
5898 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
5899 (Nkind (Parent (gnat_node))
5900 == N_Attribute_Reference)
5901 && lvalue_required_for_attribute_p
5902 (Parent (gnat_node)));
5903 }
5904
5905 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5906
5907 /* If this is an atomic access on the RHS for which synchronization is
5908 required, build the atomic load. */
5909 if (atomic_sync_required_p (gnat_node)
5910 && !present_in_lhs_or_actual_p (gnat_node))
5911 gnu_result = build_atomic_load (gnu_result);
5912 }
5913 break;
5914
5915 case N_Attribute_Reference:
5916 {
5917 /* The attribute designator. */
5918 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
5919
5920 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
5921 is a unit, not an object with a GCC equivalent. */
5922 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
5923 return
5924 create_subprog_decl (create_concat_name
5925 (Entity (Prefix (gnat_node)),
5926 attr == Attr_Elab_Body ? "elabb" : "elabs"),
5927 NULL_TREE, void_ftype, NULL_TREE, is_disabled,
5928 true, true, true, NULL, gnat_node);
5929
5930 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
5931 }
5932 break;
5933
5934 case N_Reference:
5935 /* Like 'Access as far as we are concerned. */
5936 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5937 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
5938 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5939 break;
5940
5941 case N_Aggregate:
5942 case N_Extension_Aggregate:
5943 {
5944 tree gnu_aggr_type;
5945
5946 /* ??? It is wrong to evaluate the type now, but there doesn't
5947 seem to be any other practical way of doing it. */
5948
5949 gcc_assert (!Expansion_Delayed (gnat_node));
5950
5951 gnu_aggr_type = gnu_result_type
5952 = get_unpadded_type (Etype (gnat_node));
5953
5954 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
5955 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
5956 gnu_aggr_type
5957 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
5958 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
5959 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
5960
5961 if (Null_Record_Present (gnat_node))
5962 gnu_result = gnat_build_constructor (gnu_aggr_type,
5963 NULL);
5964
5965 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
5966 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
5967 gnu_result
5968 = assoc_to_constructor (Etype (gnat_node),
5969 First (Component_Associations (gnat_node)),
5970 gnu_aggr_type);
5971 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
5972 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
5973 gnu_aggr_type,
5974 Component_Type (Etype (gnat_node)));
5975 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
5976 gnu_result
5977 = build_binary_op
5978 (COMPLEX_EXPR, gnu_aggr_type,
5979 gnat_to_gnu (Expression (First
5980 (Component_Associations (gnat_node)))),
5981 gnat_to_gnu (Expression
5982 (Next
5983 (First (Component_Associations (gnat_node))))));
5984 else
5985 gcc_unreachable ();
5986
5987 gnu_result = convert (gnu_result_type, gnu_result);
5988 }
5989 break;
5990
5991 case N_Null:
5992 if (TARGET_VTABLE_USES_DESCRIPTORS
5993 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
5994 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
5995 gnu_result = null_fdesc_node;
5996 else
5997 gnu_result = null_pointer_node;
5998 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5999 break;
6000
6001 case N_Type_Conversion:
6002 case N_Qualified_Expression:
6003 /* Get the operand expression. */
6004 gnu_result = gnat_to_gnu (Expression (gnat_node));
6005 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6006
6007 /* If this is a qualified expression for a tagged type, we mark the type
6008 as used. Because of polymorphism, this might be the only reference to
6009 the tagged type in the program while objects have it as dynamic type.
6010 The debugger needs to see it to display these objects properly. */
6011 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
6012 used_types_insert (gnu_result_type);
6013
6014 gnu_result
6015 = convert_with_check (Etype (gnat_node), gnu_result,
6016 Do_Overflow_Check (gnat_node),
6017 Do_Range_Check (Expression (gnat_node)),
6018 kind == N_Type_Conversion
6019 && Float_Truncate (gnat_node), gnat_node);
6020 break;
6021
6022 case N_Unchecked_Type_Conversion:
6023 gnu_result = gnat_to_gnu (Expression (gnat_node));
6024
6025 /* Skip further processing if the conversion is deemed a no-op. */
6026 if (unchecked_conversion_nop (gnat_node))
6027 {
6028 gnu_result_type = TREE_TYPE (gnu_result);
6029 break;
6030 }
6031
6032 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6033
6034 /* If the result is a pointer type, see if we are improperly
6035 converting to a stricter alignment. */
6036 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
6037 && IN (Ekind (Etype (gnat_node)), Access_Kind))
6038 {
6039 unsigned int align = known_alignment (gnu_result);
6040 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6041 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6042
6043 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6044 post_error_ne_tree_2
6045 ("?source alignment (^) '< alignment of & (^)",
6046 gnat_node, Designated_Type (Etype (gnat_node)),
6047 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
6048 }
6049
6050 /* If we are converting a descriptor to a function pointer, first
6051 build the pointer. */
6052 if (TARGET_VTABLE_USES_DESCRIPTORS
6053 && TREE_TYPE (gnu_result) == fdesc_type_node
6054 && POINTER_TYPE_P (gnu_result_type))
6055 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6056
6057 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
6058 No_Truncation (gnat_node));
6059 break;
6060
6061 case N_In:
6062 case N_Not_In:
6063 {
6064 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
6065 Node_Id gnat_range = Right_Opnd (gnat_node);
6066 tree gnu_low, gnu_high;
6067
6068 /* GNAT_RANGE is either an N_Range node or an identifier denoting a
6069 subtype. */
6070 if (Nkind (gnat_range) == N_Range)
6071 {
6072 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
6073 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
6074 }
6075 else if (Nkind (gnat_range) == N_Identifier
6076 || Nkind (gnat_range) == N_Expanded_Name)
6077 {
6078 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
6079 tree gnu_range_base_type = get_base_type (gnu_range_type);
6080
6081 gnu_low
6082 = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type));
6083 gnu_high
6084 = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type));
6085 }
6086 else
6087 gcc_unreachable ();
6088
6089 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6090
6091 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
6092 ensure that GNU_OBJ is evaluated only once and perform a full range
6093 test. */
6094 if (operand_equal_p (gnu_low, gnu_high, 0))
6095 gnu_result
6096 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
6097 else
6098 {
6099 tree t1, t2;
6100 gnu_obj = gnat_protect_expr (gnu_obj);
6101 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
6102 if (EXPR_P (t1))
6103 set_expr_location_from_node (t1, gnat_node);
6104 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
6105 if (EXPR_P (t2))
6106 set_expr_location_from_node (t2, gnat_node);
6107 gnu_result
6108 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
6109 }
6110
6111 if (kind == N_Not_In)
6112 gnu_result
6113 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
6114 }
6115 break;
6116
6117 case N_Op_Divide:
6118 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6119 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6120 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6121 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
6122 ? RDIV_EXPR
6123 : (Rounded_Result (gnat_node)
6124 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
6125 gnu_result_type, gnu_lhs, gnu_rhs);
6126 break;
6127
6128 case N_Op_Or: case N_Op_And: case N_Op_Xor:
6129 /* These can either be operations on booleans or on modular types.
6130 Fall through for boolean types since that's the way GNU_CODES is
6131 set up. */
6132 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6133 {
6134 enum tree_code code
6135 = (kind == N_Op_Or ? BIT_IOR_EXPR
6136 : kind == N_Op_And ? BIT_AND_EXPR
6137 : BIT_XOR_EXPR);
6138
6139 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6140 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6141 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6142 gnu_result = build_binary_op (code, gnu_result_type,
6143 gnu_lhs, gnu_rhs);
6144 break;
6145 }
6146
6147 /* ... fall through ... */
6148
6149 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
6150 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
6151 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
6152 case N_Op_Mod: case N_Op_Rem:
6153 case N_Op_Rotate_Left:
6154 case N_Op_Rotate_Right:
6155 case N_Op_Shift_Left:
6156 case N_Op_Shift_Right:
6157 case N_Op_Shift_Right_Arithmetic:
6158 case N_And_Then: case N_Or_Else:
6159 {
6160 enum tree_code code = gnu_codes[kind];
6161 bool ignore_lhs_overflow = false;
6162 location_t saved_location = input_location;
6163 tree gnu_type;
6164
6165 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6166 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6167 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6168
6169 /* Pending generic support for efficient vector logical operations in
6170 GCC, convert vectors to their representative array type view and
6171 fallthrough. */
6172 gnu_lhs = maybe_vector_array (gnu_lhs);
6173 gnu_rhs = maybe_vector_array (gnu_rhs);
6174
6175 /* If this is a comparison operator, convert any references to an
6176 unconstrained array value into a reference to the actual array. */
6177 if (TREE_CODE_CLASS (code) == tcc_comparison)
6178 {
6179 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
6180 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
6181 }
6182
6183 /* If this is a shift whose count is not guaranteed to be correct,
6184 we need to adjust the shift count. */
6185 if (IN (kind, N_Op_Shift) && !Shift_Count_OK (gnat_node))
6186 {
6187 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
6188 tree gnu_max_shift
6189 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
6190
6191 if (kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
6192 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
6193 gnu_rhs, gnu_max_shift);
6194 else if (kind == N_Op_Shift_Right_Arithmetic)
6195 gnu_rhs
6196 = build_binary_op
6197 (MIN_EXPR, gnu_count_type,
6198 build_binary_op (MINUS_EXPR,
6199 gnu_count_type,
6200 gnu_max_shift,
6201 convert (gnu_count_type,
6202 integer_one_node)),
6203 gnu_rhs);
6204 }
6205
6206 /* For right shifts, the type says what kind of shift to do,
6207 so we may need to choose a different type. In this case,
6208 we have to ignore integer overflow lest it propagates all
6209 the way down and causes a CE to be explicitly raised. */
6210 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
6211 {
6212 gnu_type = gnat_unsigned_type (gnu_type);
6213 ignore_lhs_overflow = true;
6214 }
6215 else if (kind == N_Op_Shift_Right_Arithmetic
6216 && TYPE_UNSIGNED (gnu_type))
6217 {
6218 gnu_type = gnat_signed_type (gnu_type);
6219 ignore_lhs_overflow = true;
6220 }
6221
6222 if (gnu_type != gnu_result_type)
6223 {
6224 tree gnu_old_lhs = gnu_lhs;
6225 gnu_lhs = convert (gnu_type, gnu_lhs);
6226 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
6227 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
6228 gnu_rhs = convert (gnu_type, gnu_rhs);
6229 }
6230
6231 /* Instead of expanding overflow checks for addition, subtraction
6232 and multiplication itself, the front end will leave this to
6233 the back end when Backend_Overflow_Checks_On_Target is set.
6234 As the GCC back end itself does not know yet how to properly
6235 do overflow checking, do it here. The goal is to push
6236 the expansions further into the back end over time. */
6237 if (Do_Overflow_Check (gnat_node) && Backend_Overflow_Checks_On_Target
6238 && (kind == N_Op_Add
6239 || kind == N_Op_Subtract
6240 || kind == N_Op_Multiply)
6241 && !TYPE_UNSIGNED (gnu_type)
6242 && !FLOAT_TYPE_P (gnu_type))
6243 gnu_result = build_binary_op_trapv (code, gnu_type,
6244 gnu_lhs, gnu_rhs, gnat_node);
6245 else
6246 {
6247 /* Some operations, e.g. comparisons of arrays, generate complex
6248 trees that need to be annotated while they are being built. */
6249 input_location = saved_location;
6250 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
6251 }
6252
6253 /* If this is a logical shift with the shift count not verified,
6254 we must return zero if it is too large. We cannot compensate
6255 above in this case. */
6256 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
6257 && !Shift_Count_OK (gnat_node))
6258 gnu_result
6259 = build_cond_expr
6260 (gnu_type,
6261 build_binary_op (GE_EXPR, boolean_type_node,
6262 gnu_rhs,
6263 convert (TREE_TYPE (gnu_rhs),
6264 TYPE_SIZE (gnu_type))),
6265 convert (gnu_type, integer_zero_node),
6266 gnu_result);
6267 }
6268 break;
6269
6270 case N_If_Expression:
6271 {
6272 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
6273 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
6274 tree gnu_false
6275 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
6276
6277 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6278 gnu_result
6279 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
6280 }
6281 break;
6282
6283 case N_Op_Plus:
6284 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
6285 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6286 break;
6287
6288 case N_Op_Not:
6289 /* This case can apply to a boolean or a modular type.
6290 Fall through for a boolean operand since GNU_CODES is set
6291 up to handle this. */
6292 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6293 {
6294 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6295 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6296 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
6297 gnu_expr);
6298 break;
6299 }
6300
6301 /* ... fall through ... */
6302
6303 case N_Op_Minus: case N_Op_Abs:
6304 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6305 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6306
6307 if (Do_Overflow_Check (gnat_node)
6308 && !TYPE_UNSIGNED (gnu_result_type)
6309 && !FLOAT_TYPE_P (gnu_result_type))
6310 gnu_result
6311 = build_unary_op_trapv (gnu_codes[kind],
6312 gnu_result_type, gnu_expr, gnat_node);
6313 else
6314 gnu_result = build_unary_op (gnu_codes[kind],
6315 gnu_result_type, gnu_expr);
6316 break;
6317
6318 case N_Allocator:
6319 {
6320 tree gnu_init = 0;
6321 tree gnu_type;
6322 bool ignore_init_type = false;
6323
6324 gnat_temp = Expression (gnat_node);
6325
6326 /* The Expression operand can either be an N_Identifier or
6327 Expanded_Name, which must represent a type, or a
6328 N_Qualified_Expression, which contains both the object type and an
6329 initial value for the object. */
6330 if (Nkind (gnat_temp) == N_Identifier
6331 || Nkind (gnat_temp) == N_Expanded_Name)
6332 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
6333 else if (Nkind (gnat_temp) == N_Qualified_Expression)
6334 {
6335 Entity_Id gnat_desig_type
6336 = Designated_Type (Underlying_Type (Etype (gnat_node)));
6337
6338 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
6339 gnu_init = gnat_to_gnu (Expression (gnat_temp));
6340
6341 gnu_init = maybe_unconstrained_array (gnu_init);
6342 if (Do_Range_Check (Expression (gnat_temp)))
6343 gnu_init
6344 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
6345
6346 if (Is_Elementary_Type (gnat_desig_type)
6347 || Is_Constrained (gnat_desig_type))
6348 gnu_type = gnat_to_gnu_type (gnat_desig_type);
6349 else
6350 {
6351 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
6352 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
6353 gnu_type = TREE_TYPE (gnu_init);
6354 }
6355
6356 /* See the N_Qualified_Expression case for the rationale. */
6357 if (Is_Tagged_Type (gnat_desig_type))
6358 used_types_insert (gnu_type);
6359
6360 gnu_init = convert (gnu_type, gnu_init);
6361 }
6362 else
6363 gcc_unreachable ();
6364
6365 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6366 return build_allocator (gnu_type, gnu_init, gnu_result_type,
6367 Procedure_To_Call (gnat_node),
6368 Storage_Pool (gnat_node), gnat_node,
6369 ignore_init_type);
6370 }
6371 break;
6372
6373 /**************************/
6374 /* Chapter 5: Statements */
6375 /**************************/
6376
6377 case N_Label:
6378 gnu_result = build1 (LABEL_EXPR, void_type_node,
6379 gnat_to_gnu (Identifier (gnat_node)));
6380 break;
6381
6382 case N_Null_Statement:
6383 /* When not optimizing, turn null statements from source into gotos to
6384 the next statement that the middle-end knows how to preserve. */
6385 if (!optimize && Comes_From_Source (gnat_node))
6386 {
6387 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
6388 DECL_IGNORED_P (label) = 1;
6389 start_stmt_group ();
6390 stmt = build1 (GOTO_EXPR, void_type_node, label);
6391 set_expr_location_from_node (stmt, gnat_node);
6392 add_stmt (stmt);
6393 stmt = build1 (LABEL_EXPR, void_type_node, label);
6394 set_expr_location_from_node (stmt, gnat_node);
6395 add_stmt (stmt);
6396 gnu_result = end_stmt_group ();
6397 }
6398 else
6399 gnu_result = alloc_stmt_list ();
6400 break;
6401
6402 case N_Assignment_Statement:
6403 /* Get the LHS and RHS of the statement and convert any reference to an
6404 unconstrained array into a reference to the underlying array. */
6405 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
6406
6407 /* If the type has a size that overflows, convert this into raise of
6408 Storage_Error: execution shouldn't have gotten here anyway. */
6409 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
6410 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
6411 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
6412 N_Raise_Storage_Error);
6413 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
6414 gnu_result
6415 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
6416 atomic_sync_required_p (Name (gnat_node)));
6417 else
6418 {
6419 const Node_Id gnat_expr = Expression (gnat_node);
6420 const Entity_Id gnat_type
6421 = Underlying_Type (Etype (Name (gnat_node)));
6422 const bool regular_array_type_p
6423 = (Is_Array_Type (gnat_type) && !Is_Bit_Packed_Array (gnat_type));
6424 const bool use_memset_p
6425 = (regular_array_type_p
6426 && Nkind (gnat_expr) == N_Aggregate
6427 && Is_Others_Aggregate (gnat_expr));
6428
6429 /* If we'll use memset, we need to find the inner expression. */
6430 if (use_memset_p)
6431 {
6432 Node_Id gnat_inner
6433 = Expression (First (Component_Associations (gnat_expr)));
6434 while (Nkind (gnat_inner) == N_Aggregate
6435 && Is_Others_Aggregate (gnat_inner))
6436 gnat_inner
6437 = Expression (First (Component_Associations (gnat_inner)));
6438 gnu_rhs = gnat_to_gnu (gnat_inner);
6439 }
6440 else
6441 gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr));
6442
6443 /* If range check is needed, emit code to generate it. */
6444 if (Do_Range_Check (gnat_expr))
6445 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
6446 gnat_node);
6447
6448 /* If atomic synchronization is required, build an atomic store. */
6449 if (atomic_sync_required_p (Name (gnat_node)))
6450 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs);
6451
6452 /* Or else, use memset when the conditions are met. */
6453 else if (use_memset_p)
6454 {
6455 tree value = fold_convert (integer_type_node, gnu_rhs);
6456 tree to = gnu_lhs;
6457 tree type = TREE_TYPE (to);
6458 tree size
6459 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to);
6460 tree to_ptr = build_fold_addr_expr (to);
6461 tree t = builtin_decl_implicit (BUILT_IN_MEMSET);
6462 if (TREE_CODE (value) == INTEGER_CST)
6463 {
6464 tree mask
6465 = build_int_cst (integer_type_node,
6466 ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
6467 value = int_const_binop (BIT_AND_EXPR, value, mask);
6468 }
6469 gnu_result = build_call_expr (t, 3, to_ptr, value, size);
6470 }
6471
6472 /* Otherwise build a regular assignment. */
6473 else
6474 gnu_result
6475 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
6476
6477 /* If the assignment type is a regular array and the two sides are
6478 not completely disjoint, play safe and use memmove. But don't do
6479 it for a bit-packed array as it might not be byte-aligned. */
6480 if (TREE_CODE (gnu_result) == MODIFY_EXPR
6481 && regular_array_type_p
6482 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
6483 {
6484 tree to = TREE_OPERAND (gnu_result, 0);
6485 tree from = TREE_OPERAND (gnu_result, 1);
6486 tree type = TREE_TYPE (from);
6487 tree size
6488 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from);
6489 tree to_ptr = build_fold_addr_expr (to);
6490 tree from_ptr = build_fold_addr_expr (from);
6491 tree t = builtin_decl_implicit (BUILT_IN_MEMMOVE);
6492 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
6493 }
6494 }
6495 break;
6496
6497 case N_If_Statement:
6498 {
6499 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
6500
6501 /* Make the outer COND_EXPR. Avoid non-determinism. */
6502 gnu_result = build3 (COND_EXPR, void_type_node,
6503 gnat_to_gnu (Condition (gnat_node)),
6504 NULL_TREE, NULL_TREE);
6505 COND_EXPR_THEN (gnu_result)
6506 = build_stmt_group (Then_Statements (gnat_node), false);
6507 TREE_SIDE_EFFECTS (gnu_result) = 1;
6508 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
6509
6510 /* Now make a COND_EXPR for each of the "else if" parts. Put each
6511 into the previous "else" part and point to where to put any
6512 outer "else". Also avoid non-determinism. */
6513 if (Present (Elsif_Parts (gnat_node)))
6514 for (gnat_temp = First (Elsif_Parts (gnat_node));
6515 Present (gnat_temp); gnat_temp = Next (gnat_temp))
6516 {
6517 gnu_expr = build3 (COND_EXPR, void_type_node,
6518 gnat_to_gnu (Condition (gnat_temp)),
6519 NULL_TREE, NULL_TREE);
6520 COND_EXPR_THEN (gnu_expr)
6521 = build_stmt_group (Then_Statements (gnat_temp), false);
6522 TREE_SIDE_EFFECTS (gnu_expr) = 1;
6523 set_expr_location_from_node (gnu_expr, gnat_temp);
6524 *gnu_else_ptr = gnu_expr;
6525 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
6526 }
6527
6528 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
6529 }
6530 break;
6531
6532 case N_Case_Statement:
6533 gnu_result = Case_Statement_to_gnu (gnat_node);
6534 break;
6535
6536 case N_Loop_Statement:
6537 gnu_result = Loop_Statement_to_gnu (gnat_node);
6538 break;
6539
6540 case N_Block_Statement:
6541 /* The only way to enter the block is to fall through to it. */
6542 if (stmt_group_may_fallthru ())
6543 {
6544 start_stmt_group ();
6545 gnat_pushlevel ();
6546 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6547 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6548 gnat_poplevel ();
6549 gnu_result = end_stmt_group ();
6550 }
6551 else
6552 gnu_result = alloc_stmt_list ();
6553 break;
6554
6555 case N_Exit_Statement:
6556 gnu_result
6557 = build2 (EXIT_STMT, void_type_node,
6558 (Present (Condition (gnat_node))
6559 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
6560 (Present (Name (gnat_node))
6561 ? get_gnu_tree (Entity (Name (gnat_node)))
6562 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
6563 break;
6564
6565 case N_Simple_Return_Statement:
6566 {
6567 tree gnu_ret_obj, gnu_ret_val;
6568
6569 /* If the subprogram is a function, we must return the expression. */
6570 if (Present (Expression (gnat_node)))
6571 {
6572 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
6573
6574 /* If this function has copy-in/copy-out parameters parameters and
6575 doesn't return by invisible reference, get the real object for
6576 the return. See Subprogram_Body_to_gnu. */
6577 if (TYPE_CI_CO_LIST (gnu_subprog_type)
6578 && !TREE_ADDRESSABLE (gnu_subprog_type))
6579 gnu_ret_obj = gnu_return_var_stack->last ();
6580 else
6581 gnu_ret_obj = DECL_RESULT (current_function_decl);
6582
6583 /* Get the GCC tree for the expression to be returned. */
6584 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
6585
6586 /* Do not remove the padding from GNU_RET_VAL if the inner type is
6587 self-referential since we want to allocate the fixed size. */
6588 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
6589 && TYPE_IS_PADDING_P
6590 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))
6591 && CONTAINS_PLACEHOLDER_P
6592 (TYPE_SIZE (TREE_TYPE (gnu_ret_val))))
6593 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
6594
6595 /* If the function returns by direct reference, return a pointer
6596 to the return value. */
6597 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)
6598 || By_Ref (gnat_node))
6599 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
6600
6601 /* Otherwise, if it returns an unconstrained array, we have to
6602 allocate a new version of the result and return it. */
6603 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type))
6604 {
6605 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
6606
6607 /* And find out whether this is a candidate for Named Return
6608 Value. If so, record it. */
6609 if (!TYPE_CI_CO_LIST (gnu_subprog_type) && optimize)
6610 {
6611 tree ret_val = gnu_ret_val;
6612
6613 /* Strip useless conversions around the return value. */
6614 if (gnat_useless_type_conversion (ret_val))
6615 ret_val = TREE_OPERAND (ret_val, 0);
6616
6617 /* Strip unpadding around the return value. */
6618 if (TREE_CODE (ret_val) == COMPONENT_REF
6619 && TYPE_IS_PADDING_P
6620 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
6621 ret_val = TREE_OPERAND (ret_val, 0);
6622
6623 /* Now apply the test to the return value. */
6624 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
6625 {
6626 if (!f_named_ret_val)
6627 f_named_ret_val = BITMAP_GGC_ALLOC ();
6628 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
6629 if (!f_gnat_ret)
6630 f_gnat_ret = gnat_node;
6631 }
6632 }
6633
6634 gnu_ret_val = build_allocator (TREE_TYPE (gnu_ret_val),
6635 gnu_ret_val,
6636 TREE_TYPE (gnu_ret_obj),
6637 Procedure_To_Call (gnat_node),
6638 Storage_Pool (gnat_node),
6639 gnat_node, false);
6640 }
6641
6642 /* Otherwise, if it returns by invisible reference, dereference
6643 the pointer it is passed using the type of the return value
6644 and build the copy operation manually. This ensures that we
6645 don't copy too much data, for example if the return type is
6646 unconstrained with a maximum size. */
6647 else if (TREE_ADDRESSABLE (gnu_subprog_type))
6648 {
6649 tree gnu_ret_deref
6650 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
6651 gnu_ret_obj);
6652 gnu_result = build2 (MODIFY_EXPR, void_type_node,
6653 gnu_ret_deref, gnu_ret_val);
6654 add_stmt_with_node (gnu_result, gnat_node);
6655 gnu_ret_val = NULL_TREE;
6656 }
6657 }
6658
6659 else
6660 gnu_ret_obj = gnu_ret_val = NULL_TREE;
6661
6662 /* If we have a return label defined, convert this into a branch to
6663 that label. The return proper will be handled elsewhere. */
6664 if (gnu_return_label_stack->last ())
6665 {
6666 if (gnu_ret_val)
6667 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_ret_obj,
6668 gnu_ret_val));
6669
6670 gnu_result = build1 (GOTO_EXPR, void_type_node,
6671 gnu_return_label_stack->last ());
6672
6673 /* When not optimizing, make sure the return is preserved. */
6674 if (!optimize && Comes_From_Source (gnat_node))
6675 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
6676 }
6677
6678 /* Otherwise, build a regular return. */
6679 else
6680 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
6681 }
6682 break;
6683
6684 case N_Goto_Statement:
6685 gnu_result
6686 = build1 (GOTO_EXPR, void_type_node, gnat_to_gnu (Name (gnat_node)));
6687 break;
6688
6689 /***************************/
6690 /* Chapter 6: Subprograms */
6691 /***************************/
6692
6693 case N_Subprogram_Declaration:
6694 /* Unless there is a freeze node, declare the subprogram. We consider
6695 this a "definition" even though we're not generating code for
6696 the subprogram because we will be making the corresponding GCC
6697 node here. */
6698
6699 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6700 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
6701 NULL_TREE, 1);
6702 gnu_result = alloc_stmt_list ();
6703 break;
6704
6705 case N_Abstract_Subprogram_Declaration:
6706 /* This subprogram doesn't exist for code generation purposes, but we
6707 have to elaborate the types of any parameters and result, unless
6708 they are imported types (nothing to generate in this case).
6709
6710 The parameter list may contain types with freeze nodes, e.g. not null
6711 subtypes, so the subprogram itself may carry a freeze node, in which
6712 case its elaboration must be deferred. */
6713
6714 /* Process the parameter types first. */
6715 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6716 for (gnat_temp
6717 = First_Formal_With_Extras
6718 (Defining_Entity (Specification (gnat_node)));
6719 Present (gnat_temp);
6720 gnat_temp = Next_Formal_With_Extras (gnat_temp))
6721 if (Is_Itype (Etype (gnat_temp))
6722 && !From_Limited_With (Etype (gnat_temp)))
6723 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
6724
6725 /* Then the result type, set to Standard_Void_Type for procedures. */
6726 {
6727 Entity_Id gnat_temp_type
6728 = Etype (Defining_Entity (Specification (gnat_node)));
6729
6730 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
6731 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, 0);
6732 }
6733
6734 gnu_result = alloc_stmt_list ();
6735 break;
6736
6737 case N_Defining_Program_Unit_Name:
6738 /* For a child unit identifier go up a level to get the specification.
6739 We get this when we try to find the spec of a child unit package
6740 that is the compilation unit being compiled. */
6741 gnu_result = gnat_to_gnu (Parent (gnat_node));
6742 break;
6743
6744 case N_Subprogram_Body:
6745 Subprogram_Body_to_gnu (gnat_node);
6746 gnu_result = alloc_stmt_list ();
6747 break;
6748
6749 case N_Function_Call:
6750 case N_Procedure_Call_Statement:
6751 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE, false);
6752 break;
6753
6754 /************************/
6755 /* Chapter 7: Packages */
6756 /************************/
6757
6758 case N_Package_Declaration:
6759 gnu_result = gnat_to_gnu (Specification (gnat_node));
6760 break;
6761
6762 case N_Package_Specification:
6763
6764 start_stmt_group ();
6765 process_decls (Visible_Declarations (gnat_node),
6766 Private_Declarations (gnat_node), Empty, true, true);
6767 gnu_result = end_stmt_group ();
6768 break;
6769
6770 case N_Package_Body:
6771
6772 /* If this is the body of a generic package - do nothing. */
6773 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
6774 {
6775 gnu_result = alloc_stmt_list ();
6776 break;
6777 }
6778
6779 start_stmt_group ();
6780 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6781
6782 if (Present (Handled_Statement_Sequence (gnat_node)))
6783 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6784
6785 gnu_result = end_stmt_group ();
6786 break;
6787
6788 /********************************/
6789 /* Chapter 8: Visibility Rules */
6790 /********************************/
6791
6792 case N_Use_Package_Clause:
6793 case N_Use_Type_Clause:
6794 /* Nothing to do here - but these may appear in list of declarations. */
6795 gnu_result = alloc_stmt_list ();
6796 break;
6797
6798 /*********************/
6799 /* Chapter 9: Tasks */
6800 /*********************/
6801
6802 case N_Protected_Type_Declaration:
6803 gnu_result = alloc_stmt_list ();
6804 break;
6805
6806 case N_Single_Task_Declaration:
6807 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
6808 gnu_result = alloc_stmt_list ();
6809 break;
6810
6811 /*********************************************************/
6812 /* Chapter 10: Program Structure and Compilation Issues */
6813 /*********************************************************/
6814
6815 case N_Compilation_Unit:
6816 /* This is not called for the main unit on which gigi is invoked. */
6817 Compilation_Unit_to_gnu (gnat_node);
6818 gnu_result = alloc_stmt_list ();
6819 break;
6820
6821 case N_Subprogram_Body_Stub:
6822 case N_Package_Body_Stub:
6823 case N_Protected_Body_Stub:
6824 case N_Task_Body_Stub:
6825 /* Simply process whatever unit is being inserted. */
6826 if (Present (Library_Unit (gnat_node)))
6827 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
6828 else
6829 {
6830 gcc_assert (type_annotate_only);
6831 gnu_result = alloc_stmt_list ();
6832 }
6833 break;
6834
6835 case N_Subunit:
6836 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
6837 break;
6838
6839 /***************************/
6840 /* Chapter 11: Exceptions */
6841 /***************************/
6842
6843 case N_Handled_Sequence_Of_Statements:
6844 /* If there is an At_End procedure attached to this node, and the EH
6845 mechanism is SJLJ, we must have at least a corresponding At_End
6846 handler, unless the No_Exception_Handlers restriction is set. */
6847 gcc_assert (type_annotate_only
6848 || Exception_Mechanism != Setjmp_Longjmp
6849 || No (At_End_Proc (gnat_node))
6850 || Present (Exception_Handlers (gnat_node))
6851 || No_Exception_Handlers_Set ());
6852
6853 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
6854 break;
6855
6856 case N_Exception_Handler:
6857 if (Exception_Mechanism == Setjmp_Longjmp)
6858 gnu_result = Exception_Handler_to_gnu_sjlj (gnat_node);
6859 else if (Exception_Mechanism == Back_End_Exceptions)
6860 gnu_result = Exception_Handler_to_gnu_zcx (gnat_node);
6861 else
6862 gcc_unreachable ();
6863 break;
6864
6865 case N_Raise_Statement:
6866 /* Only for reraise in back-end exceptions mode. */
6867 gcc_assert (No (Name (gnat_node))
6868 && Exception_Mechanism == Back_End_Exceptions);
6869
6870 start_stmt_group ();
6871 gnat_pushlevel ();
6872
6873 /* Clear the current exception pointer so that the occurrence won't be
6874 deallocated. */
6875 gnu_expr = create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE,
6876 ptr_type_node, gnu_incoming_exc_ptr,
6877 false, false, false, false, NULL, gnat_node);
6878
6879 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_incoming_exc_ptr,
6880 convert (ptr_type_node, integer_zero_node)));
6881 add_stmt (build_call_n_expr (reraise_zcx_decl, 1, gnu_expr));
6882 gnat_poplevel ();
6883 gnu_result = end_stmt_group ();
6884 break;
6885
6886 case N_Push_Constraint_Error_Label:
6887 push_exception_label_stack (&gnu_constraint_error_label_stack,
6888 Exception_Label (gnat_node));
6889 break;
6890
6891 case N_Push_Storage_Error_Label:
6892 push_exception_label_stack (&gnu_storage_error_label_stack,
6893 Exception_Label (gnat_node));
6894 break;
6895
6896 case N_Push_Program_Error_Label:
6897 push_exception_label_stack (&gnu_program_error_label_stack,
6898 Exception_Label (gnat_node));
6899 break;
6900
6901 case N_Pop_Constraint_Error_Label:
6902 gnu_constraint_error_label_stack->pop ();
6903 break;
6904
6905 case N_Pop_Storage_Error_Label:
6906 gnu_storage_error_label_stack->pop ();
6907 break;
6908
6909 case N_Pop_Program_Error_Label:
6910 gnu_program_error_label_stack->pop ();
6911 break;
6912
6913 /******************************/
6914 /* Chapter 12: Generic Units */
6915 /******************************/
6916
6917 case N_Generic_Function_Renaming_Declaration:
6918 case N_Generic_Package_Renaming_Declaration:
6919 case N_Generic_Procedure_Renaming_Declaration:
6920 case N_Generic_Package_Declaration:
6921 case N_Generic_Subprogram_Declaration:
6922 case N_Package_Instantiation:
6923 case N_Procedure_Instantiation:
6924 case N_Function_Instantiation:
6925 /* These nodes can appear on a declaration list but there is nothing to
6926 to be done with them. */
6927 gnu_result = alloc_stmt_list ();
6928 break;
6929
6930 /**************************************************/
6931 /* Chapter 13: Representation Clauses and */
6932 /* Implementation-Dependent Features */
6933 /**************************************************/
6934
6935 case N_Attribute_Definition_Clause:
6936 gnu_result = alloc_stmt_list ();
6937
6938 /* The only one we need to deal with is 'Address since, for the others,
6939 the front-end puts the information elsewhere. */
6940 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
6941 break;
6942
6943 /* And we only deal with 'Address if the object has a Freeze node. */
6944 gnat_temp = Entity (Name (gnat_node));
6945 if (No (Freeze_Node (gnat_temp)))
6946 break;
6947
6948 /* Get the value to use as the address and save it as the equivalent
6949 for the object. When it is frozen, gnat_to_gnu_entity will do the
6950 right thing. */
6951 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
6952 break;
6953
6954 case N_Enumeration_Representation_Clause:
6955 case N_Record_Representation_Clause:
6956 case N_At_Clause:
6957 /* We do nothing with these. SEM puts the information elsewhere. */
6958 gnu_result = alloc_stmt_list ();
6959 break;
6960
6961 case N_Code_Statement:
6962 if (!type_annotate_only)
6963 {
6964 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
6965 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
6966 tree gnu_clobbers = NULL_TREE, tail;
6967 bool allows_mem, allows_reg, fake;
6968 int ninputs, noutputs, i;
6969 const char **oconstraints;
6970 const char *constraint;
6971 char *clobber;
6972
6973 /* First retrieve the 3 operand lists built by the front-end. */
6974 Setup_Asm_Outputs (gnat_node);
6975 while (Present (gnat_temp = Asm_Output_Variable ()))
6976 {
6977 tree gnu_value = gnat_to_gnu (gnat_temp);
6978 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6979 (Asm_Output_Constraint ()));
6980
6981 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
6982 Next_Asm_Output ();
6983 }
6984
6985 Setup_Asm_Inputs (gnat_node);
6986 while (Present (gnat_temp = Asm_Input_Value ()))
6987 {
6988 tree gnu_value = gnat_to_gnu (gnat_temp);
6989 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6990 (Asm_Input_Constraint ()));
6991
6992 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
6993 Next_Asm_Input ();
6994 }
6995
6996 Clobber_Setup (gnat_node);
6997 while ((clobber = Clobber_Get_Next ()))
6998 gnu_clobbers
6999 = tree_cons (NULL_TREE,
7000 build_string (strlen (clobber) + 1, clobber),
7001 gnu_clobbers);
7002
7003 /* Then perform some standard checking and processing on the
7004 operands. In particular, mark them addressable if needed. */
7005 gnu_outputs = nreverse (gnu_outputs);
7006 noutputs = list_length (gnu_outputs);
7007 gnu_inputs = nreverse (gnu_inputs);
7008 ninputs = list_length (gnu_inputs);
7009 oconstraints = XALLOCAVEC (const char *, noutputs);
7010
7011 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
7012 {
7013 tree output = TREE_VALUE (tail);
7014 constraint
7015 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7016 oconstraints[i] = constraint;
7017
7018 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7019 &allows_mem, &allows_reg, &fake))
7020 {
7021 /* If the operand is going to end up in memory,
7022 mark it addressable. Note that we don't test
7023 allows_mem like in the input case below; this
7024 is modelled on the C front-end. */
7025 if (!allows_reg)
7026 {
7027 output = remove_conversions (output, false);
7028 if (TREE_CODE (output) == CONST_DECL
7029 && DECL_CONST_CORRESPONDING_VAR (output))
7030 output = DECL_CONST_CORRESPONDING_VAR (output);
7031 if (!gnat_mark_addressable (output))
7032 output = error_mark_node;
7033 }
7034 }
7035 else
7036 output = error_mark_node;
7037
7038 TREE_VALUE (tail) = output;
7039 }
7040
7041 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
7042 {
7043 tree input = TREE_VALUE (tail);
7044 constraint
7045 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7046
7047 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
7048 0, oconstraints,
7049 &allows_mem, &allows_reg))
7050 {
7051 /* If the operand is going to end up in memory,
7052 mark it addressable. */
7053 if (!allows_reg && allows_mem)
7054 {
7055 input = remove_conversions (input, false);
7056 if (TREE_CODE (input) == CONST_DECL
7057 && DECL_CONST_CORRESPONDING_VAR (input))
7058 input = DECL_CONST_CORRESPONDING_VAR (input);
7059 if (!gnat_mark_addressable (input))
7060 input = error_mark_node;
7061 }
7062 }
7063 else
7064 input = error_mark_node;
7065
7066 TREE_VALUE (tail) = input;
7067 }
7068
7069 gnu_result = build5 (ASM_EXPR, void_type_node,
7070 gnu_template, gnu_outputs,
7071 gnu_inputs, gnu_clobbers, NULL_TREE);
7072 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
7073 }
7074 else
7075 gnu_result = alloc_stmt_list ();
7076
7077 break;
7078
7079 /****************/
7080 /* Added Nodes */
7081 /****************/
7082
7083 case N_Expression_With_Actions:
7084 /* This construct doesn't define a scope so we don't push a binding level
7085 around the statement list; but we wrap it in a SAVE_EXPR to protect it
7086 from unsharing. */
7087 gnu_result = build_stmt_group (Actions (gnat_node), false);
7088 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
7089 TREE_SIDE_EFFECTS (gnu_result) = 1;
7090 gnu_expr = gnat_to_gnu (Expression (gnat_node));
7091 gnu_result
7092 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
7093 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7094 break;
7095
7096 case N_Freeze_Entity:
7097 start_stmt_group ();
7098 process_freeze_entity (gnat_node);
7099 process_decls (Actions (gnat_node), Empty, Empty, true, true);
7100 gnu_result = end_stmt_group ();
7101 break;
7102
7103 case N_Freeze_Generic_Entity:
7104 gnu_result = alloc_stmt_list ();
7105 break;
7106
7107 case N_Itype_Reference:
7108 if (!present_gnu_tree (Itype (gnat_node)))
7109 process_type (Itype (gnat_node));
7110
7111 gnu_result = alloc_stmt_list ();
7112 break;
7113
7114 case N_Free_Statement:
7115 if (!type_annotate_only)
7116 {
7117 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
7118 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
7119 tree gnu_obj_type, gnu_actual_obj_type;
7120
7121 /* If this is a thin pointer, we must first dereference it to create
7122 a fat pointer, then go back below to a thin pointer. The reason
7123 for this is that we need to have a fat pointer someplace in order
7124 to properly compute the size. */
7125 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7126 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
7127 build_unary_op (INDIRECT_REF, NULL_TREE,
7128 gnu_ptr));
7129
7130 /* If this is a fat pointer, the object must have been allocated with
7131 the template in front of the array. So pass the template address,
7132 and get the total size; do it by converting to a thin pointer. */
7133 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
7134 gnu_ptr
7135 = convert (build_pointer_type
7136 (TYPE_OBJECT_RECORD_TYPE
7137 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
7138 gnu_ptr);
7139
7140 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
7141
7142 /* If this is a thin pointer, the object must have been allocated with
7143 the template in front of the array. So pass the template address,
7144 and get the total size. */
7145 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7146 gnu_ptr
7147 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
7148 gnu_ptr,
7149 fold_build1 (NEGATE_EXPR, sizetype,
7150 byte_position
7151 (DECL_CHAIN
7152 TYPE_FIELDS ((gnu_obj_type)))));
7153
7154 /* If we have a special dynamic constrained subtype on the node, use
7155 it to compute the size; otherwise, use the designated subtype. */
7156 if (Present (Actual_Designated_Subtype (gnat_node)))
7157 {
7158 gnu_actual_obj_type
7159 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
7160
7161 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
7162 gnu_actual_obj_type
7163 = build_unc_object_type_from_ptr (gnu_ptr_type,
7164 gnu_actual_obj_type,
7165 get_identifier ("DEALLOC"),
7166 false);
7167 }
7168 else
7169 gnu_actual_obj_type = gnu_obj_type;
7170
7171 gnu_result
7172 = build_call_alloc_dealloc (gnu_ptr,
7173 TYPE_SIZE_UNIT (gnu_actual_obj_type),
7174 gnu_obj_type,
7175 Procedure_To_Call (gnat_node),
7176 Storage_Pool (gnat_node),
7177 gnat_node);
7178 }
7179 break;
7180
7181 case N_Raise_Constraint_Error:
7182 case N_Raise_Program_Error:
7183 case N_Raise_Storage_Error:
7184 if (type_annotate_only)
7185 gnu_result = alloc_stmt_list ();
7186 else
7187 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
7188 break;
7189
7190 case N_Validate_Unchecked_Conversion:
7191 /* The only validation we currently do on an unchecked conversion is
7192 that of aliasing assumptions. */
7193 if (flag_strict_aliasing)
7194 gnat_validate_uc_list.safe_push (gnat_node);
7195 gnu_result = alloc_stmt_list ();
7196 break;
7197
7198 case N_Function_Specification:
7199 case N_Procedure_Specification:
7200 case N_Op_Concat:
7201 case N_Component_Association:
7202 case N_Protected_Body:
7203 case N_Task_Body:
7204 /* These nodes should only be present when annotating types. */
7205 gcc_assert (type_annotate_only);
7206 gnu_result = alloc_stmt_list ();
7207 break;
7208
7209 default:
7210 /* Other nodes are not supposed to reach here. */
7211 gcc_unreachable ();
7212 }
7213
7214 /* If we pushed the processing of the elaboration routine, pop it back. */
7215 if (went_into_elab_proc)
7216 current_function_decl = NULL_TREE;
7217
7218 /* When not optimizing, turn boolean rvalues B into B != false tests
7219 so that the code just below can put the location information of the
7220 reference to B on the inequality operator for better debug info. */
7221 if (!optimize
7222 && TREE_CODE (gnu_result) != INTEGER_CST
7223 && (kind == N_Identifier
7224 || kind == N_Expanded_Name
7225 || kind == N_Explicit_Dereference
7226 || kind == N_Function_Call
7227 || kind == N_Indexed_Component
7228 || kind == N_Selected_Component)
7229 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
7230 && !lvalue_required_p (gnat_node, gnu_result_type, false, false, false))
7231 gnu_result = build_binary_op (NE_EXPR, gnu_result_type,
7232 convert (gnu_result_type, gnu_result),
7233 convert (gnu_result_type,
7234 boolean_false_node));
7235
7236 /* Set the location information on the result. Note that we may have
7237 no result if we tried to build a CALL_EXPR node to a procedure with
7238 no side-effects and optimization is enabled. */
7239 if (gnu_result && EXPR_P (gnu_result))
7240 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7241
7242 /* If we're supposed to return something of void_type, it means we have
7243 something we're elaborating for effect, so just return. */
7244 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
7245 return gnu_result;
7246
7247 /* If the result is a constant that overflowed, raise Constraint_Error. */
7248 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
7249 {
7250 post_error ("?`Constraint_Error` will be raised at run time", gnat_node);
7251 gnu_result
7252 = build1 (NULL_EXPR, gnu_result_type,
7253 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
7254 N_Raise_Constraint_Error));
7255 }
7256
7257 /* If the result has side-effects and is of an unconstrained type, make a
7258 SAVE_EXPR so that we can be sure it will only be referenced once. But
7259 this is useless for a call to a function that returns an unconstrained
7260 type with default discriminant, as we cannot compute the size of the
7261 actual returned object. We must do this before any conversions. */
7262 if (TREE_SIDE_EFFECTS (gnu_result)
7263 && !(TREE_CODE (gnu_result) == CALL_EXPR
7264 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7265 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
7266 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))))
7267 gnu_result = gnat_stabilize_reference (gnu_result, false, NULL);
7268
7269 /* Now convert the result to the result type, unless we are in one of the
7270 following cases:
7271
7272 1. If this is the LHS of an assignment or an actual parameter of a
7273 call, return the result almost unmodified since the RHS will have
7274 to be converted to our type in that case, unless the result type
7275 has a simpler size. Likewise if there is just a no-op unchecked
7276 conversion in-between. Similarly, don't convert integral types
7277 that are the operands of an unchecked conversion since we need
7278 to ignore those conversions (for 'Valid).
7279
7280 2. If we have a label (which doesn't have any well-defined type), a
7281 field or an error, return the result almost unmodified. Similarly,
7282 if the two types are record types with the same name, don't convert.
7283 This will be the case when we are converting from a packable version
7284 of a type to its original type and we need those conversions to be
7285 NOPs in order for assignments into these types to work properly.
7286
7287 3. If the type is void or if we have no result, return error_mark_node
7288 to show we have no result.
7289
7290 4. If this a call to a function that returns an unconstrained type with
7291 default discriminant, return the call expression unmodified since we
7292 cannot compute the size of the actual returned object.
7293
7294 5. Finally, if the type of the result is already correct. */
7295
7296 if (Present (Parent (gnat_node))
7297 && (lhs_or_actual_p (gnat_node)
7298 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7299 && unchecked_conversion_nop (Parent (gnat_node)))
7300 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7301 && !AGGREGATE_TYPE_P (gnu_result_type)
7302 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
7303 && !(TYPE_SIZE (gnu_result_type)
7304 && TYPE_SIZE (TREE_TYPE (gnu_result))
7305 && (AGGREGATE_TYPE_P (gnu_result_type)
7306 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
7307 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
7308 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
7309 != INTEGER_CST))
7310 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
7311 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
7312 && (CONTAINS_PLACEHOLDER_P
7313 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
7314 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
7315 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
7316 {
7317 /* Remove padding only if the inner object is of self-referential
7318 size: in that case it must be an object of unconstrained type
7319 with a default discriminant and we want to avoid copying too
7320 much data. */
7321 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7322 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
7323 (TREE_TYPE (gnu_result))))))
7324 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7325 gnu_result);
7326 }
7327
7328 else if (TREE_CODE (gnu_result) == LABEL_DECL
7329 || TREE_CODE (gnu_result) == FIELD_DECL
7330 || TREE_CODE (gnu_result) == ERROR_MARK
7331 || (TYPE_NAME (gnu_result_type)
7332 == TYPE_NAME (TREE_TYPE (gnu_result))
7333 && TREE_CODE (gnu_result_type) == RECORD_TYPE
7334 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
7335 {
7336 /* Remove any padding. */
7337 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7338 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7339 gnu_result);
7340 }
7341
7342 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
7343 gnu_result = error_mark_node;
7344
7345 else if (TREE_CODE (gnu_result) == CALL_EXPR
7346 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7347 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result)))
7348 == gnu_result_type
7349 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
7350 ;
7351
7352 else if (TREE_TYPE (gnu_result) != gnu_result_type)
7353 gnu_result = convert (gnu_result_type, gnu_result);
7354
7355 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
7356 while ((TREE_CODE (gnu_result) == NOP_EXPR
7357 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
7358 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
7359 gnu_result = TREE_OPERAND (gnu_result, 0);
7360
7361 return gnu_result;
7362 }
7363 \f
7364 /* Subroutine of above to push the exception label stack. GNU_STACK is
7365 a pointer to the stack to update and GNAT_LABEL, if present, is the
7366 label to push onto the stack. */
7367
7368 static void
7369 push_exception_label_stack (vec<tree, va_gc> **gnu_stack, Entity_Id gnat_label)
7370 {
7371 tree gnu_label = (Present (gnat_label)
7372 ? gnat_to_gnu_entity (gnat_label, NULL_TREE, 0)
7373 : NULL_TREE);
7374
7375 vec_safe_push (*gnu_stack, gnu_label);
7376 }
7377 \f
7378 /* Record the current code position in GNAT_NODE. */
7379
7380 static void
7381 record_code_position (Node_Id gnat_node)
7382 {
7383 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
7384
7385 add_stmt_with_node (stmt_stmt, gnat_node);
7386 save_gnu_tree (gnat_node, stmt_stmt, true);
7387 }
7388
7389 /* Insert the code for GNAT_NODE at the position saved for that node. */
7390
7391 static void
7392 insert_code_for (Node_Id gnat_node)
7393 {
7394 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
7395 save_gnu_tree (gnat_node, NULL_TREE, true);
7396 }
7397 \f
7398 /* Start a new statement group chained to the previous group. */
7399
7400 void
7401 start_stmt_group (void)
7402 {
7403 struct stmt_group *group = stmt_group_free_list;
7404
7405 /* First see if we can get one from the free list. */
7406 if (group)
7407 stmt_group_free_list = group->previous;
7408 else
7409 group = ggc_alloc<stmt_group> ();
7410
7411 group->previous = current_stmt_group;
7412 group->stmt_list = group->block = group->cleanups = NULL_TREE;
7413 current_stmt_group = group;
7414 }
7415
7416 /* Add GNU_STMT to the current statement group. If it is an expression with
7417 no effects, it is ignored. */
7418
7419 void
7420 add_stmt (tree gnu_stmt)
7421 {
7422 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
7423 }
7424
7425 /* Similar, but the statement is always added, regardless of side-effects. */
7426
7427 void
7428 add_stmt_force (tree gnu_stmt)
7429 {
7430 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
7431 }
7432
7433 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
7434
7435 void
7436 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
7437 {
7438 /* Do not emit a location for renamings that come from generic instantiation,
7439 they are likely to disturb debugging. */
7440 if (Present (gnat_node)
7441 && !renaming_from_generic_instantiation_p (gnat_node))
7442 set_expr_location_from_node (gnu_stmt, gnat_node);
7443 add_stmt (gnu_stmt);
7444 }
7445
7446 /* Similar, but the statement is always added, regardless of side-effects. */
7447
7448 void
7449 add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
7450 {
7451 if (Present (gnat_node))
7452 set_expr_location_from_node (gnu_stmt, gnat_node);
7453 add_stmt_force (gnu_stmt);
7454 }
7455
7456 /* Add a declaration statement for GNU_DECL to the current statement group.
7457 Get SLOC from Entity_Id. */
7458
7459 void
7460 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
7461 {
7462 tree type = TREE_TYPE (gnu_decl);
7463 tree gnu_stmt, gnu_init, t;
7464
7465 /* If this is a variable that Gigi is to ignore, we may have been given
7466 an ERROR_MARK. So test for it. We also might have been given a
7467 reference for a renaming. So only do something for a decl. Also
7468 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
7469 if (!DECL_P (gnu_decl)
7470 || (TREE_CODE (gnu_decl) == TYPE_DECL
7471 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
7472 return;
7473
7474 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
7475
7476 /* If we are external or global, we don't want to output the DECL_EXPR for
7477 this DECL node since we already have evaluated the expressions in the
7478 sizes and positions as globals and doing it again would be wrong. */
7479 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
7480 {
7481 /* Mark everything as used to prevent node sharing with subprograms.
7482 Note that walk_tree knows how to deal with TYPE_DECL, but neither
7483 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
7484 MARK_VISITED (gnu_stmt);
7485 if (TREE_CODE (gnu_decl) == VAR_DECL
7486 || TREE_CODE (gnu_decl) == CONST_DECL)
7487 {
7488 MARK_VISITED (DECL_SIZE (gnu_decl));
7489 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
7490 MARK_VISITED (DECL_INITIAL (gnu_decl));
7491 }
7492 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
7493 else if (TREE_CODE (gnu_decl) == TYPE_DECL
7494 && RECORD_OR_UNION_TYPE_P (type)
7495 && !TYPE_FAT_POINTER_P (type))
7496 MARK_VISITED (TYPE_ADA_SIZE (type));
7497 }
7498 else
7499 add_stmt_with_node (gnu_stmt, gnat_entity);
7500
7501 /* If this is a variable and an initializer is attached to it, it must be
7502 valid for the context. Similar to init_const in create_var_decl_1. */
7503 if (TREE_CODE (gnu_decl) == VAR_DECL
7504 && (gnu_init = DECL_INITIAL (gnu_decl)) != NULL_TREE
7505 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
7506 || (TREE_STATIC (gnu_decl)
7507 && !initializer_constant_valid_p (gnu_init,
7508 TREE_TYPE (gnu_init)))))
7509 {
7510 /* If GNU_DECL has a padded type, convert it to the unpadded
7511 type so the assignment is done properly. */
7512 if (TYPE_IS_PADDING_P (type))
7513 t = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
7514 else
7515 t = gnu_decl;
7516
7517 gnu_stmt = build_binary_op (INIT_EXPR, NULL_TREE, t, gnu_init);
7518
7519 DECL_INITIAL (gnu_decl) = NULL_TREE;
7520 if (TREE_READONLY (gnu_decl))
7521 {
7522 TREE_READONLY (gnu_decl) = 0;
7523 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
7524 }
7525
7526 add_stmt_with_node (gnu_stmt, gnat_entity);
7527 }
7528 }
7529
7530 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
7531
7532 static tree
7533 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
7534 {
7535 tree t = *tp;
7536
7537 if (TREE_VISITED (t))
7538 *walk_subtrees = 0;
7539
7540 /* Don't mark a dummy type as visited because we want to mark its sizes
7541 and fields once it's filled in. */
7542 else if (!TYPE_IS_DUMMY_P (t))
7543 TREE_VISITED (t) = 1;
7544
7545 if (TYPE_P (t))
7546 TYPE_SIZES_GIMPLIFIED (t) = 1;
7547
7548 return NULL_TREE;
7549 }
7550
7551 /* Mark nodes rooted at T with TREE_VISITED and types as having their
7552 sized gimplified. We use this to indicate all variable sizes and
7553 positions in global types may not be shared by any subprogram. */
7554
7555 void
7556 mark_visited (tree t)
7557 {
7558 walk_tree (&t, mark_visited_r, NULL, NULL);
7559 }
7560
7561 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
7562 set its location to that of GNAT_NODE if present, but with column info
7563 cleared so that conditional branches generated as part of the cleanup
7564 code do not interfere with coverage analysis tools. */
7565
7566 static void
7567 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
7568 {
7569 if (Present (gnat_node))
7570 set_expr_location_from_node1 (gnu_cleanup, gnat_node, true);
7571 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
7572 }
7573
7574 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
7575
7576 void
7577 set_block_for_group (tree gnu_block)
7578 {
7579 gcc_assert (!current_stmt_group->block);
7580 current_stmt_group->block = gnu_block;
7581 }
7582
7583 /* Return code corresponding to the current code group. It is normally
7584 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
7585 BLOCK or cleanups were set. */
7586
7587 tree
7588 end_stmt_group (void)
7589 {
7590 struct stmt_group *group = current_stmt_group;
7591 tree gnu_retval = group->stmt_list;
7592
7593 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
7594 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
7595 make a BIND_EXPR. Note that we nest in that because the cleanup may
7596 reference variables in the block. */
7597 if (gnu_retval == NULL_TREE)
7598 gnu_retval = alloc_stmt_list ();
7599
7600 if (group->cleanups)
7601 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
7602 group->cleanups);
7603
7604 if (current_stmt_group->block)
7605 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
7606 gnu_retval, group->block);
7607
7608 /* Remove this group from the stack and add it to the free list. */
7609 current_stmt_group = group->previous;
7610 group->previous = stmt_group_free_list;
7611 stmt_group_free_list = group;
7612
7613 return gnu_retval;
7614 }
7615
7616 /* Return whether the current statement group may fall through. */
7617
7618 static inline bool
7619 stmt_group_may_fallthru (void)
7620 {
7621 if (current_stmt_group->stmt_list)
7622 return block_may_fallthru (current_stmt_group->stmt_list);
7623 else
7624 return true;
7625 }
7626
7627 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
7628 statements.*/
7629
7630 static void
7631 add_stmt_list (List_Id gnat_list)
7632 {
7633 Node_Id gnat_node;
7634
7635 if (Present (gnat_list))
7636 for (gnat_node = First (gnat_list); Present (gnat_node);
7637 gnat_node = Next (gnat_node))
7638 add_stmt (gnat_to_gnu (gnat_node));
7639 }
7640
7641 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
7642 If BINDING_P is true, push and pop a binding level around the list. */
7643
7644 static tree
7645 build_stmt_group (List_Id gnat_list, bool binding_p)
7646 {
7647 start_stmt_group ();
7648 if (binding_p)
7649 gnat_pushlevel ();
7650
7651 add_stmt_list (gnat_list);
7652 if (binding_p)
7653 gnat_poplevel ();
7654
7655 return end_stmt_group ();
7656 }
7657 \f
7658 /* Generate GIMPLE in place for the expression at *EXPR_P. */
7659
7660 int
7661 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
7662 gimple_seq *post_p ATTRIBUTE_UNUSED)
7663 {
7664 tree expr = *expr_p;
7665 tree type = TREE_TYPE (expr);
7666 tree op;
7667
7668 if (IS_ADA_STMT (expr))
7669 return gnat_gimplify_stmt (expr_p);
7670
7671 switch (TREE_CODE (expr))
7672 {
7673 case NULL_EXPR:
7674 /* If this is an aggregate type, build a null pointer of the appropriate
7675 type and dereference it. */
7676 if (AGGREGATE_TYPE_P (type)
7677 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
7678 *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE,
7679 convert (build_pointer_type (type),
7680 integer_zero_node));
7681 /* Otherwise, just make a VAR_DECL. */
7682 else
7683 {
7684 *expr_p = create_tmp_var (type, NULL);
7685 TREE_NO_WARNING (*expr_p) = 1;
7686 }
7687
7688 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
7689 return GS_OK;
7690
7691 case UNCONSTRAINED_ARRAY_REF:
7692 /* We should only do this if we are just elaborating for side-effects,
7693 but we can't know that yet. */
7694 *expr_p = TREE_OPERAND (*expr_p, 0);
7695 return GS_OK;
7696
7697 case ADDR_EXPR:
7698 op = TREE_OPERAND (expr, 0);
7699
7700 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
7701 is put into static memory. We know that it's going to be read-only
7702 given the semantics we have and it must be in static memory when the
7703 reference is in an elaboration procedure. */
7704 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
7705 {
7706 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
7707 *expr_p = fold_convert (type, addr);
7708 return GS_ALL_DONE;
7709 }
7710
7711 return GS_UNHANDLED;
7712
7713 case VIEW_CONVERT_EXPR:
7714 op = TREE_OPERAND (expr, 0);
7715
7716 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
7717 type to a scalar one, explicitly create the local temporary. That's
7718 required if the type is passed by reference. */
7719 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
7720 && AGGREGATE_TYPE_P (TREE_TYPE (op))
7721 && !AGGREGATE_TYPE_P (type))
7722 {
7723 tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
7724 gimple_add_tmp_var (new_var);
7725
7726 mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
7727 gimplify_and_add (mod, pre_p);
7728
7729 TREE_OPERAND (expr, 0) = new_var;
7730 return GS_OK;
7731 }
7732
7733 return GS_UNHANDLED;
7734
7735 case DECL_EXPR:
7736 op = DECL_EXPR_DECL (expr);
7737
7738 /* The expressions for the RM bounds must be gimplified to ensure that
7739 they are properly elaborated. See gimplify_decl_expr. */
7740 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
7741 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)))
7742 switch (TREE_CODE (TREE_TYPE (op)))
7743 {
7744 case INTEGER_TYPE:
7745 case ENUMERAL_TYPE:
7746 case BOOLEAN_TYPE:
7747 case REAL_TYPE:
7748 {
7749 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
7750
7751 val = TYPE_RM_MIN_VALUE (type);
7752 if (val)
7753 {
7754 gimplify_one_sizepos (&val, pre_p);
7755 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7756 SET_TYPE_RM_MIN_VALUE (t, val);
7757 }
7758
7759 val = TYPE_RM_MAX_VALUE (type);
7760 if (val)
7761 {
7762 gimplify_one_sizepos (&val, pre_p);
7763 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7764 SET_TYPE_RM_MAX_VALUE (t, val);
7765 }
7766
7767 }
7768 break;
7769
7770 default:
7771 break;
7772 }
7773
7774 /* ... fall through ... */
7775
7776 default:
7777 return GS_UNHANDLED;
7778 }
7779 }
7780
7781 /* Generate GIMPLE in place for the statement at *STMT_P. */
7782
7783 static enum gimplify_status
7784 gnat_gimplify_stmt (tree *stmt_p)
7785 {
7786 tree stmt = *stmt_p;
7787
7788 switch (TREE_CODE (stmt))
7789 {
7790 case STMT_STMT:
7791 *stmt_p = STMT_STMT_STMT (stmt);
7792 return GS_OK;
7793
7794 case LOOP_STMT:
7795 {
7796 tree gnu_start_label = create_artificial_label (input_location);
7797 tree gnu_cond = LOOP_STMT_COND (stmt);
7798 tree gnu_update = LOOP_STMT_UPDATE (stmt);
7799 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
7800
7801 /* Build the condition expression from the test, if any. */
7802 if (gnu_cond)
7803 {
7804 /* Deal with the optimization hints. */
7805 if (LOOP_STMT_IVDEP (stmt))
7806 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7807 build_int_cst (integer_type_node,
7808 annot_expr_ivdep_kind));
7809 if (LOOP_STMT_NO_VECTOR (stmt))
7810 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7811 build_int_cst (integer_type_node,
7812 annot_expr_no_vector_kind));
7813 if (LOOP_STMT_VECTOR (stmt))
7814 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7815 build_int_cst (integer_type_node,
7816 annot_expr_vector_kind));
7817
7818 gnu_cond
7819 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
7820 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
7821 }
7822
7823 /* Set to emit the statements of the loop. */
7824 *stmt_p = NULL_TREE;
7825
7826 /* We first emit the start label and then a conditional jump to the
7827 end label if there's a top condition, then the update if it's at
7828 the top, then the body of the loop, then a conditional jump to
7829 the end label if there's a bottom condition, then the update if
7830 it's at the bottom, and finally a jump to the start label and the
7831 definition of the end label. */
7832 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7833 gnu_start_label),
7834 stmt_p);
7835
7836 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
7837 append_to_statement_list (gnu_cond, stmt_p);
7838
7839 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
7840 append_to_statement_list (gnu_update, stmt_p);
7841
7842 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
7843
7844 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
7845 append_to_statement_list (gnu_cond, stmt_p);
7846
7847 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
7848 append_to_statement_list (gnu_update, stmt_p);
7849
7850 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
7851 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
7852 append_to_statement_list (t, stmt_p);
7853
7854 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7855 gnu_end_label),
7856 stmt_p);
7857 return GS_OK;
7858 }
7859
7860 case EXIT_STMT:
7861 /* Build a statement to jump to the corresponding end label, then
7862 see if it needs to be conditional. */
7863 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
7864 if (EXIT_STMT_COND (stmt))
7865 *stmt_p = build3 (COND_EXPR, void_type_node,
7866 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
7867 return GS_OK;
7868
7869 default:
7870 gcc_unreachable ();
7871 }
7872 }
7873 \f
7874 /* Force references to each of the entities in packages withed by GNAT_NODE.
7875 Operate recursively but check that we aren't elaborating something more
7876 than once.
7877
7878 This routine is exclusively called in type_annotate mode, to compute DDA
7879 information for types in withed units, for ASIS use. */
7880
7881 static void
7882 elaborate_all_entities (Node_Id gnat_node)
7883 {
7884 Entity_Id gnat_with_clause, gnat_entity;
7885
7886 /* Process each unit only once. As we trace the context of all relevant
7887 units transitively, including generic bodies, we may encounter the
7888 same generic unit repeatedly. */
7889 if (!present_gnu_tree (gnat_node))
7890 save_gnu_tree (gnat_node, integer_zero_node, true);
7891
7892 /* Save entities in all context units. A body may have an implicit_with
7893 on its own spec, if the context includes a child unit, so don't save
7894 the spec twice. */
7895 for (gnat_with_clause = First (Context_Items (gnat_node));
7896 Present (gnat_with_clause);
7897 gnat_with_clause = Next (gnat_with_clause))
7898 if (Nkind (gnat_with_clause) == N_With_Clause
7899 && !present_gnu_tree (Library_Unit (gnat_with_clause))
7900 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
7901 {
7902 elaborate_all_entities (Library_Unit (gnat_with_clause));
7903
7904 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
7905 {
7906 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
7907 Present (gnat_entity);
7908 gnat_entity = Next_Entity (gnat_entity))
7909 if (Is_Public (gnat_entity)
7910 && Convention (gnat_entity) != Convention_Intrinsic
7911 && Ekind (gnat_entity) != E_Package
7912 && Ekind (gnat_entity) != E_Package_Body
7913 && Ekind (gnat_entity) != E_Operator
7914 && !(IN (Ekind (gnat_entity), Type_Kind)
7915 && !Is_Frozen (gnat_entity))
7916 && !((Ekind (gnat_entity) == E_Procedure
7917 || Ekind (gnat_entity) == E_Function)
7918 && Is_Intrinsic_Subprogram (gnat_entity))
7919 && !IN (Ekind (gnat_entity), Named_Kind)
7920 && !IN (Ekind (gnat_entity), Generic_Unit_Kind))
7921 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
7922 }
7923 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
7924 {
7925 Node_Id gnat_body
7926 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
7927
7928 /* Retrieve compilation unit node of generic body. */
7929 while (Present (gnat_body)
7930 && Nkind (gnat_body) != N_Compilation_Unit)
7931 gnat_body = Parent (gnat_body);
7932
7933 /* If body is available, elaborate its context. */
7934 if (Present (gnat_body))
7935 elaborate_all_entities (gnat_body);
7936 }
7937 }
7938
7939 if (Nkind (Unit (gnat_node)) == N_Package_Body)
7940 elaborate_all_entities (Library_Unit (gnat_node));
7941 }
7942 \f
7943 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
7944
7945 static void
7946 process_freeze_entity (Node_Id gnat_node)
7947 {
7948 const Entity_Id gnat_entity = Entity (gnat_node);
7949 const Entity_Kind kind = Ekind (gnat_entity);
7950 tree gnu_old, gnu_new;
7951
7952 /* If this is a package, we need to generate code for the package. */
7953 if (kind == E_Package)
7954 {
7955 insert_code_for
7956 (Parent (Corresponding_Body
7957 (Parent (Declaration_Node (gnat_entity)))));
7958 return;
7959 }
7960
7961 /* Don't do anything for class-wide types as they are always transformed
7962 into their root type. */
7963 if (kind == E_Class_Wide_Type)
7964 return;
7965
7966 /* Check for an old definition. This freeze node might be for an Itype. */
7967 gnu_old
7968 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
7969
7970 /* If this entity has an address representation clause, GNU_OLD is the
7971 address, so discard it here. */
7972 if (Present (Address_Clause (gnat_entity)))
7973 gnu_old = NULL_TREE;
7974
7975 /* Don't do anything for subprograms that may have been elaborated before
7976 their freeze nodes. This can happen, for example, because of an inner
7977 call in an instance body or because of previous compilation of a spec
7978 for inlining purposes. */
7979 if (gnu_old
7980 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
7981 && (kind == E_Function || kind == E_Procedure))
7982 || (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
7983 && kind == E_Subprogram_Type)))
7984 return;
7985
7986 /* If we have a non-dummy type old tree, we have nothing to do, except
7987 aborting if this is the public view of a private type whose full view was
7988 not delayed, as this node was never delayed as it should have been. We
7989 let this happen for concurrent types and their Corresponding_Record_Type,
7990 however, because each might legitimately be elaborated before its own
7991 freeze node, e.g. while processing the other. */
7992 if (gnu_old
7993 && !(TREE_CODE (gnu_old) == TYPE_DECL
7994 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
7995 {
7996 gcc_assert ((IN (kind, Incomplete_Or_Private_Kind)
7997 && Present (Full_View (gnat_entity))
7998 && No (Freeze_Node (Full_View (gnat_entity))))
7999 || Is_Concurrent_Type (gnat_entity)
8000 || (IN (kind, Record_Kind)
8001 && Is_Concurrent_Record_Type (gnat_entity)));
8002 return;
8003 }
8004
8005 /* Reset the saved tree, if any, and elaborate the object or type for real.
8006 If there is a full view, elaborate it and use the result. And, if this
8007 is the root type of a class-wide type, reuse it for the latter. */
8008 if (gnu_old)
8009 {
8010 save_gnu_tree (gnat_entity, NULL_TREE, false);
8011
8012 if (IN (kind, Incomplete_Or_Private_Kind)
8013 && Present (Full_View (gnat_entity)))
8014 {
8015 Entity_Id full_view = Full_View (gnat_entity);
8016
8017 save_gnu_tree (full_view, NULL_TREE, false);
8018
8019 if (IN (Ekind (full_view), Private_Kind)
8020 && Present (Underlying_Full_View (full_view)))
8021 {
8022 full_view = Underlying_Full_View (full_view);
8023 save_gnu_tree (full_view, NULL_TREE, false);
8024 }
8025 }
8026
8027 if (IN (kind, Type_Kind)
8028 && Present (Class_Wide_Type (gnat_entity))
8029 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8030 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
8031 }
8032
8033 if (IN (kind, Incomplete_Or_Private_Kind)
8034 && Present (Full_View (gnat_entity)))
8035 {
8036 Entity_Id full_view = Full_View (gnat_entity);
8037
8038 if (IN (Ekind (full_view), Private_Kind)
8039 && Present (Underlying_Full_View (full_view)))
8040 full_view = Underlying_Full_View (full_view);
8041
8042 gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, 1);
8043
8044 /* Propagate back-annotations from full view to partial view. */
8045 if (Unknown_Alignment (gnat_entity))
8046 Set_Alignment (gnat_entity, Alignment (full_view));
8047
8048 if (Unknown_Esize (gnat_entity))
8049 Set_Esize (gnat_entity, Esize (full_view));
8050
8051 if (Unknown_RM_Size (gnat_entity))
8052 Set_RM_Size (gnat_entity, RM_Size (full_view));
8053
8054 /* The above call may have defined this entity (the simplest example
8055 of this is when we have a private enumeral type since the bounds
8056 will have the public view). */
8057 if (!present_gnu_tree (gnat_entity))
8058 save_gnu_tree (gnat_entity, gnu_new, false);
8059 }
8060 else
8061 {
8062 tree gnu_init
8063 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
8064 && present_gnu_tree (Declaration_Node (gnat_entity)))
8065 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
8066
8067 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
8068 }
8069
8070 if (IN (kind, Type_Kind)
8071 && Present (Class_Wide_Type (gnat_entity))
8072 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8073 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
8074
8075 /* If we have an old type and we've made pointers to this type, update those
8076 pointers. If this is a Taft amendment type in the main unit, we need to
8077 mark the type as used since other units referencing it don't see the full
8078 declaration and, therefore, cannot mark it as used themselves. */
8079 if (gnu_old)
8080 {
8081 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8082 TREE_TYPE (gnu_new));
8083 if (DECL_TAFT_TYPE_P (gnu_old))
8084 used_types_insert (TREE_TYPE (gnu_new));
8085 }
8086 }
8087 \f
8088 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
8089 We make two passes, one to elaborate anything other than bodies (but
8090 we declare a function if there was no spec). The second pass
8091 elaborates the bodies.
8092
8093 GNAT_END_LIST gives the element in the list past the end. Normally,
8094 this is Empty, but can be First_Real_Statement for a
8095 Handled_Sequence_Of_Statements.
8096
8097 We make a complete pass through both lists if PASS1P is true, then make
8098 the second pass over both lists if PASS2P is true. The lists usually
8099 correspond to the public and private parts of a package. */
8100
8101 static void
8102 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
8103 Node_Id gnat_end_list, bool pass1p, bool pass2p)
8104 {
8105 List_Id gnat_decl_array[2];
8106 Node_Id gnat_decl;
8107 int i;
8108
8109 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
8110
8111 if (pass1p)
8112 for (i = 0; i <= 1; i++)
8113 if (Present (gnat_decl_array[i]))
8114 for (gnat_decl = First (gnat_decl_array[i]);
8115 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8116 {
8117 /* For package specs, we recurse inside the declarations,
8118 thus taking the two pass approach inside the boundary. */
8119 if (Nkind (gnat_decl) == N_Package_Declaration
8120 && (Nkind (Specification (gnat_decl)
8121 == N_Package_Specification)))
8122 process_decls (Visible_Declarations (Specification (gnat_decl)),
8123 Private_Declarations (Specification (gnat_decl)),
8124 Empty, true, false);
8125
8126 /* Similarly for any declarations in the actions of a
8127 freeze node. */
8128 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8129 {
8130 process_freeze_entity (gnat_decl);
8131 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
8132 }
8133
8134 /* Package bodies with freeze nodes get their elaboration deferred
8135 until the freeze node, but the code must be placed in the right
8136 place, so record the code position now. */
8137 else if (Nkind (gnat_decl) == N_Package_Body
8138 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
8139 record_code_position (gnat_decl);
8140
8141 else if (Nkind (gnat_decl) == N_Package_Body_Stub
8142 && Present (Library_Unit (gnat_decl))
8143 && Present (Freeze_Node
8144 (Corresponding_Spec
8145 (Proper_Body (Unit
8146 (Library_Unit (gnat_decl)))))))
8147 record_code_position
8148 (Proper_Body (Unit (Library_Unit (gnat_decl))));
8149
8150 /* We defer most subprogram bodies to the second pass. */
8151 else if (Nkind (gnat_decl) == N_Subprogram_Body)
8152 {
8153 if (Acts_As_Spec (gnat_decl))
8154 {
8155 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
8156
8157 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
8158 && Ekind (gnat_subprog_id) != E_Generic_Function)
8159 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8160 }
8161 }
8162
8163 /* For bodies and stubs that act as their own specs, the entity
8164 itself must be elaborated in the first pass, because it may
8165 be used in other declarations. */
8166 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
8167 {
8168 Node_Id gnat_subprog_id
8169 = Defining_Entity (Specification (gnat_decl));
8170
8171 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
8172 && Ekind (gnat_subprog_id) != E_Generic_Procedure
8173 && Ekind (gnat_subprog_id) != E_Generic_Function)
8174 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8175 }
8176
8177 /* Concurrent stubs stand for the corresponding subprogram bodies,
8178 which are deferred like other bodies. */
8179 else if (Nkind (gnat_decl) == N_Task_Body_Stub
8180 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8181 ;
8182
8183 else
8184 add_stmt (gnat_to_gnu (gnat_decl));
8185 }
8186
8187 /* Here we elaborate everything we deferred above except for package bodies,
8188 which are elaborated at their freeze nodes. Note that we must also
8189 go inside things (package specs and freeze nodes) the first pass did. */
8190 if (pass2p)
8191 for (i = 0; i <= 1; i++)
8192 if (Present (gnat_decl_array[i]))
8193 for (gnat_decl = First (gnat_decl_array[i]);
8194 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8195 {
8196 if (Nkind (gnat_decl) == N_Subprogram_Body
8197 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
8198 || Nkind (gnat_decl) == N_Task_Body_Stub
8199 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8200 add_stmt (gnat_to_gnu (gnat_decl));
8201
8202 else if (Nkind (gnat_decl) == N_Package_Declaration
8203 && (Nkind (Specification (gnat_decl)
8204 == N_Package_Specification)))
8205 process_decls (Visible_Declarations (Specification (gnat_decl)),
8206 Private_Declarations (Specification (gnat_decl)),
8207 Empty, false, true);
8208
8209 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8210 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
8211 }
8212 }
8213 \f
8214 /* Make a unary operation of kind CODE using build_unary_op, but guard
8215 the operation by an overflow check. CODE can be one of NEGATE_EXPR
8216 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
8217 the operation is to be performed in that type. GNAT_NODE is the gnat
8218 node conveying the source location for which the error should be
8219 signaled. */
8220
8221 static tree
8222 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
8223 Node_Id gnat_node)
8224 {
8225 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
8226
8227 operand = gnat_protect_expr (operand);
8228
8229 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
8230 operand, TYPE_MIN_VALUE (gnu_type)),
8231 build_unary_op (code, gnu_type, operand),
8232 CE_Overflow_Check_Failed, gnat_node);
8233 }
8234
8235 /* Make a binary operation of kind CODE using build_binary_op, but guard
8236 the operation by an overflow check. CODE can be one of PLUS_EXPR,
8237 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
8238 Usually the operation is to be performed in that type. GNAT_NODE is
8239 the GNAT node conveying the source location for which the error should
8240 be signaled. */
8241
8242 static tree
8243 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
8244 tree right, Node_Id gnat_node)
8245 {
8246 const unsigned int precision = TYPE_PRECISION (gnu_type);
8247 tree lhs = gnat_protect_expr (left);
8248 tree rhs = gnat_protect_expr (right);
8249 tree type_max = TYPE_MAX_VALUE (gnu_type);
8250 tree type_min = TYPE_MIN_VALUE (gnu_type);
8251 tree zero = convert (gnu_type, integer_zero_node);
8252 tree gnu_expr, rhs_lt_zero, tmp1, tmp2;
8253 tree check_pos, check_neg, check;
8254
8255 /* Assert that the precision is a power of 2. */
8256 gcc_assert ((precision & (precision - 1)) == 0);
8257
8258 /* Prefer a constant or known-positive rhs to simplify checks. */
8259 if (!TREE_CONSTANT (rhs)
8260 && commutative_tree_code (code)
8261 && (TREE_CONSTANT (lhs)
8262 || (!tree_expr_nonnegative_p (rhs)
8263 && tree_expr_nonnegative_p (lhs))))
8264 {
8265 tree tmp = lhs;
8266 lhs = rhs;
8267 rhs = tmp;
8268 }
8269
8270 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
8271
8272 /* If we can fold the expression to a constant, just return it.
8273 The caller will deal with overflow, no need to generate a check. */
8274 if (TREE_CONSTANT (gnu_expr))
8275 return gnu_expr;
8276
8277 rhs_lt_zero = tree_expr_nonnegative_p (rhs)
8278 ? boolean_false_node
8279 : build_binary_op (LT_EXPR, boolean_type_node, rhs, zero);
8280
8281 /* ??? Should use more efficient check for operand_equal_p (lhs, rhs, 0) */
8282
8283 /* Try a few strategies that may be cheaper than the general
8284 code at the end of the function, if the rhs is not known.
8285 The strategies are:
8286 - Call library function for 64-bit multiplication (complex)
8287 - Widen, if input arguments are sufficiently small
8288 - Determine overflow using wrapped result for addition/subtraction. */
8289
8290 if (!TREE_CONSTANT (rhs))
8291 {
8292 /* Even for add/subtract double size to get another base type. */
8293 const unsigned int needed_precision = precision * 2;
8294
8295 if (code == MULT_EXPR && precision == 64)
8296 {
8297 tree int_64 = gnat_type_for_size (64, 0);
8298
8299 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
8300 convert (int_64, lhs),
8301 convert (int_64, rhs)));
8302 }
8303
8304 if (needed_precision <= BITS_PER_WORD
8305 || (code == MULT_EXPR && needed_precision <= LONG_LONG_TYPE_SIZE))
8306 {
8307 tree wide_type = gnat_type_for_size (needed_precision, 0);
8308 tree wide_result = build_binary_op (code, wide_type,
8309 convert (wide_type, lhs),
8310 convert (wide_type, rhs));
8311
8312 check = build_binary_op
8313 (TRUTH_ORIF_EXPR, boolean_type_node,
8314 build_binary_op (LT_EXPR, boolean_type_node, wide_result,
8315 convert (wide_type, type_min)),
8316 build_binary_op (GT_EXPR, boolean_type_node, wide_result,
8317 convert (wide_type, type_max)));
8318
8319 return
8320 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8321 }
8322
8323 if (code == PLUS_EXPR || code == MINUS_EXPR)
8324 {
8325 tree unsigned_type = gnat_type_for_size (precision, 1);
8326 tree wrapped_expr
8327 = convert (gnu_type,
8328 build_binary_op (code, unsigned_type,
8329 convert (unsigned_type, lhs),
8330 convert (unsigned_type, rhs)));
8331
8332 /* Overflow when (rhs < 0) ^ (wrapped_expr < lhs)), for addition
8333 or when (rhs < 0) ^ (wrapped_expr > lhs) for subtraction. */
8334 check
8335 = build_binary_op (TRUTH_XOR_EXPR, boolean_type_node, rhs_lt_zero,
8336 build_binary_op (code == PLUS_EXPR
8337 ? LT_EXPR : GT_EXPR,
8338 boolean_type_node,
8339 wrapped_expr, lhs));
8340
8341 return
8342 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8343 }
8344 }
8345
8346 switch (code)
8347 {
8348 case PLUS_EXPR:
8349 /* When rhs >= 0, overflow when lhs > type_max - rhs. */
8350 check_pos = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8351 build_binary_op (MINUS_EXPR, gnu_type,
8352 type_max, rhs)),
8353
8354 /* When rhs < 0, overflow when lhs < type_min - rhs. */
8355 check_neg = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8356 build_binary_op (MINUS_EXPR, gnu_type,
8357 type_min, rhs));
8358 break;
8359
8360 case MINUS_EXPR:
8361 /* When rhs >= 0, overflow when lhs < type_min + rhs. */
8362 check_pos = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8363 build_binary_op (PLUS_EXPR, gnu_type,
8364 type_min, rhs)),
8365
8366 /* When rhs < 0, overflow when lhs > type_max + rhs. */
8367 check_neg = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8368 build_binary_op (PLUS_EXPR, gnu_type,
8369 type_max, rhs));
8370 break;
8371
8372 case MULT_EXPR:
8373 /* The check here is designed to be efficient if the rhs is constant,
8374 but it will work for any rhs by using integer division.
8375 Four different check expressions determine whether X * C overflows,
8376 depending on C.
8377 C == 0 => false
8378 C > 0 => X > type_max / C || X < type_min / C
8379 C == -1 => X == type_min
8380 C < -1 => X > type_min / C || X < type_max / C */
8381
8382 tmp1 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
8383 tmp2 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
8384
8385 check_pos
8386 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
8387 build_binary_op (NE_EXPR, boolean_type_node, zero,
8388 rhs),
8389 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8390 build_binary_op (GT_EXPR,
8391 boolean_type_node,
8392 lhs, tmp1),
8393 build_binary_op (LT_EXPR,
8394 boolean_type_node,
8395 lhs, tmp2)));
8396
8397 check_neg
8398 = fold_build3 (COND_EXPR, boolean_type_node,
8399 build_binary_op (EQ_EXPR, boolean_type_node, rhs,
8400 build_int_cst (gnu_type, -1)),
8401 build_binary_op (EQ_EXPR, boolean_type_node, lhs,
8402 type_min),
8403 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8404 build_binary_op (GT_EXPR,
8405 boolean_type_node,
8406 lhs, tmp2),
8407 build_binary_op (LT_EXPR,
8408 boolean_type_node,
8409 lhs, tmp1)));
8410 break;
8411
8412 default:
8413 gcc_unreachable();
8414 }
8415
8416 check = fold_build3 (COND_EXPR, boolean_type_node, rhs_lt_zero, check_neg,
8417 check_pos);
8418
8419 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8420 }
8421
8422 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
8423 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
8424 which we have to check. GNAT_NODE is the GNAT node conveying the source
8425 location for which the error should be signaled. */
8426
8427 static tree
8428 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
8429 {
8430 tree gnu_range_type = get_unpadded_type (gnat_range_type);
8431 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
8432
8433 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
8434 This can for example happen when translating 'Val or 'Value. */
8435 if (gnu_compare_type == gnu_range_type)
8436 return gnu_expr;
8437
8438 /* Range checks can only be applied to types with ranges. */
8439 gcc_assert (INTEGRAL_TYPE_P (gnu_range_type)
8440 || SCALAR_FLOAT_TYPE_P (gnu_range_type));
8441
8442 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
8443 we can't do anything since we might be truncating the bounds. No
8444 check is needed in this case. */
8445 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
8446 && (TYPE_PRECISION (gnu_compare_type)
8447 < TYPE_PRECISION (get_base_type (gnu_range_type))))
8448 return gnu_expr;
8449
8450 /* Checked expressions must be evaluated only once. */
8451 gnu_expr = gnat_protect_expr (gnu_expr);
8452
8453 /* Note that the form of the check is
8454 (not (expr >= lo)) or (not (expr <= hi))
8455 the reason for this slightly convoluted form is that NaNs
8456 are not considered to be in range in the float case. */
8457 return emit_check
8458 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8459 invert_truthvalue
8460 (build_binary_op (GE_EXPR, boolean_type_node,
8461 convert (gnu_compare_type, gnu_expr),
8462 convert (gnu_compare_type,
8463 TYPE_MIN_VALUE
8464 (gnu_range_type)))),
8465 invert_truthvalue
8466 (build_binary_op (LE_EXPR, boolean_type_node,
8467 convert (gnu_compare_type, gnu_expr),
8468 convert (gnu_compare_type,
8469 TYPE_MAX_VALUE
8470 (gnu_range_type))))),
8471 gnu_expr, CE_Range_Check_Failed, gnat_node);
8472 }
8473 \f
8474 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object which
8475 we are about to index, GNU_EXPR is the index expression to be checked,
8476 GNU_LOW and GNU_HIGH are the lower and upper bounds against which GNU_EXPR
8477 has to be checked. Note that for index checking we cannot simply use the
8478 emit_range_check function (although very similar code needs to be generated
8479 in both cases) since for index checking the array type against which we are
8480 checking the indices may be unconstrained and consequently we need to get
8481 the actual index bounds from the array object itself (GNU_ARRAY_OBJECT).
8482 The place where we need to do that is in subprograms having unconstrained
8483 array formal parameters. GNAT_NODE is the GNAT node conveying the source
8484 location for which the error should be signaled. */
8485
8486 static tree
8487 emit_index_check (tree gnu_array_object, tree gnu_expr, tree gnu_low,
8488 tree gnu_high, Node_Id gnat_node)
8489 {
8490 tree gnu_expr_check;
8491
8492 /* Checked expressions must be evaluated only once. */
8493 gnu_expr = gnat_protect_expr (gnu_expr);
8494
8495 /* Must do this computation in the base type in case the expression's
8496 type is an unsigned subtypes. */
8497 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
8498
8499 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
8500 the object we are handling. */
8501 gnu_low = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low, gnu_array_object);
8502 gnu_high = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high, gnu_array_object);
8503
8504 return emit_check
8505 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8506 build_binary_op (LT_EXPR, boolean_type_node,
8507 gnu_expr_check,
8508 convert (TREE_TYPE (gnu_expr_check),
8509 gnu_low)),
8510 build_binary_op (GT_EXPR, boolean_type_node,
8511 gnu_expr_check,
8512 convert (TREE_TYPE (gnu_expr_check),
8513 gnu_high))),
8514 gnu_expr, CE_Index_Check_Failed, gnat_node);
8515 }
8516 \f
8517 /* GNU_COND contains the condition corresponding to an access, discriminant or
8518 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
8519 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
8520 REASON is the code that says why the exception was raised. GNAT_NODE is
8521 the GNAT node conveying the source location for which the error should be
8522 signaled. */
8523
8524 static tree
8525 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
8526 {
8527 tree gnu_call
8528 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
8529 tree gnu_result
8530 = fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
8531 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
8532 convert (TREE_TYPE (gnu_expr), integer_zero_node)),
8533 gnu_expr);
8534
8535 /* GNU_RESULT has side effects if and only if GNU_EXPR has:
8536 we don't need to evaluate it just for the check. */
8537 TREE_SIDE_EFFECTS (gnu_result) = TREE_SIDE_EFFECTS (gnu_expr);
8538
8539 return gnu_result;
8540 }
8541 \f
8542 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
8543 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
8544 GNAT_TYPE is known to be an integral type. If TRUNCATE_P true, do a
8545 float to integer conversion with truncation; otherwise round.
8546 GNAT_NODE is the GNAT node conveying the source location for which the
8547 error should be signaled. */
8548
8549 static tree
8550 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
8551 bool rangep, bool truncatep, Node_Id gnat_node)
8552 {
8553 tree gnu_type = get_unpadded_type (gnat_type);
8554 tree gnu_in_type = TREE_TYPE (gnu_expr);
8555 tree gnu_in_basetype = get_base_type (gnu_in_type);
8556 tree gnu_base_type = get_base_type (gnu_type);
8557 tree gnu_result = gnu_expr;
8558
8559 /* If we are not doing any checks, the output is an integral type and the
8560 input is not a floating-point type, just do the conversion. This is
8561 required for packed array types and is simpler in all cases anyway. */
8562 if (!rangep
8563 && !overflowp
8564 && INTEGRAL_TYPE_P (gnu_base_type)
8565 && !FLOAT_TYPE_P (gnu_in_type))
8566 return convert (gnu_type, gnu_expr);
8567
8568 /* First convert the expression to its base type. This
8569 will never generate code, but makes the tests below much simpler.
8570 But don't do this if converting from an integer type to an unconstrained
8571 array type since then we need to get the bounds from the original
8572 (unpacked) type. */
8573 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
8574 gnu_result = convert (gnu_in_basetype, gnu_result);
8575
8576 /* If overflow checks are requested, we need to be sure the result will
8577 fit in the output base type. But don't do this if the input
8578 is integer and the output floating-point. */
8579 if (overflowp
8580 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
8581 {
8582 /* Ensure GNU_EXPR only gets evaluated once. */
8583 tree gnu_input = gnat_protect_expr (gnu_result);
8584 tree gnu_cond = boolean_false_node;
8585 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
8586 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
8587 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
8588 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
8589
8590 /* Convert the lower bounds to signed types, so we're sure we're
8591 comparing them properly. Likewise, convert the upper bounds
8592 to unsigned types. */
8593 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TYPE_UNSIGNED (gnu_in_basetype))
8594 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
8595
8596 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8597 && !TYPE_UNSIGNED (gnu_in_basetype))
8598 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
8599
8600 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
8601 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
8602
8603 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
8604 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
8605
8606 /* Check each bound separately and only if the result bound
8607 is tighter than the bound on the input type. Note that all the
8608 types are base types, so the bounds must be constant. Also,
8609 the comparison is done in the base type of the input, which
8610 always has the proper signedness. First check for input
8611 integer (which means output integer), output float (which means
8612 both float), or mixed, in which case we always compare.
8613 Note that we have to do the comparison which would *fail* in the
8614 case of an error since if it's an FP comparison and one of the
8615 values is a NaN or Inf, the comparison will fail. */
8616 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8617 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
8618 : (FLOAT_TYPE_P (gnu_base_type)
8619 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
8620 TREE_REAL_CST (gnu_out_lb))
8621 : 1))
8622 gnu_cond
8623 = invert_truthvalue
8624 (build_binary_op (GE_EXPR, boolean_type_node,
8625 gnu_input, convert (gnu_in_basetype,
8626 gnu_out_lb)));
8627
8628 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8629 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
8630 : (FLOAT_TYPE_P (gnu_base_type)
8631 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
8632 TREE_REAL_CST (gnu_in_lb))
8633 : 1))
8634 gnu_cond
8635 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
8636 invert_truthvalue
8637 (build_binary_op (LE_EXPR, boolean_type_node,
8638 gnu_input,
8639 convert (gnu_in_basetype,
8640 gnu_out_ub))));
8641
8642 if (!integer_zerop (gnu_cond))
8643 gnu_result = emit_check (gnu_cond, gnu_input,
8644 CE_Overflow_Check_Failed, gnat_node);
8645 }
8646
8647 /* Now convert to the result base type. If this is a non-truncating
8648 float-to-integer conversion, round. */
8649 if (INTEGRAL_TYPE_P (gnu_base_type)
8650 && FLOAT_TYPE_P (gnu_in_basetype)
8651 && !truncatep)
8652 {
8653 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
8654 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
8655 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
8656 const struct real_format *fmt;
8657
8658 /* The following calculations depend on proper rounding to even
8659 of each arithmetic operation. In order to prevent excess
8660 precision from spoiling this property, use the widest hardware
8661 floating-point type if FP_ARITH_MAY_WIDEN is true. */
8662 calc_type
8663 = fp_arith_may_widen ? longest_float_type_node : gnu_in_basetype;
8664
8665 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
8666 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
8667 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
8668 REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf,
8669 half_minus_pred_half);
8670 gnu_pred_half = build_real (calc_type, pred_half);
8671
8672 /* If the input is strictly negative, subtract this value
8673 and otherwise add it from the input. For 0.5, the result
8674 is exactly between 1.0 and the machine number preceding 1.0
8675 (for calc_type). Since the last bit of 1.0 is even, this 0.5
8676 will round to 1.0, while all other number with an absolute
8677 value less than 0.5 round to 0.0. For larger numbers exactly
8678 halfway between integers, rounding will always be correct as
8679 the true mathematical result will be closer to the higher
8680 integer compared to the lower one. So, this constant works
8681 for all floating-point numbers.
8682
8683 The reason to use the same constant with subtract/add instead
8684 of a positive and negative constant is to allow the comparison
8685 to be scheduled in parallel with retrieval of the constant and
8686 conversion of the input to the calc_type (if necessary). */
8687
8688 gnu_zero = convert (gnu_in_basetype, integer_zero_node);
8689 gnu_result = gnat_protect_expr (gnu_result);
8690 gnu_conv = convert (calc_type, gnu_result);
8691 gnu_comp
8692 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
8693 gnu_add_pred_half
8694 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8695 gnu_subtract_pred_half
8696 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8697 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
8698 gnu_add_pred_half, gnu_subtract_pred_half);
8699 }
8700
8701 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8702 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
8703 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
8704 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
8705 else
8706 gnu_result = convert (gnu_base_type, gnu_result);
8707
8708 /* Finally, do the range check if requested. Note that if the result type
8709 is a modular type, the range check is actually an overflow check. */
8710 if (rangep
8711 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8712 && TYPE_MODULAR_P (gnu_base_type) && overflowp))
8713 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
8714
8715 return convert (gnu_type, gnu_result);
8716 }
8717 \f
8718 /* Return true if GNU_EXPR can be directly addressed. This is the case
8719 unless it is an expression involving computation or if it involves a
8720 reference to a bitfield or to an object not sufficiently aligned for
8721 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
8722 be directly addressed as an object of this type.
8723
8724 *** Notes on addressability issues in the Ada compiler ***
8725
8726 This predicate is necessary in order to bridge the gap between Gigi
8727 and the middle-end about addressability of GENERIC trees. A tree
8728 is said to be addressable if it can be directly addressed, i.e. if
8729 its address can be taken, is a multiple of the type's alignment on
8730 strict-alignment architectures and returns the first storage unit
8731 assigned to the object represented by the tree.
8732
8733 In the C family of languages, everything is in practice addressable
8734 at the language level, except for bit-fields. This means that these
8735 compilers will take the address of any tree that doesn't represent
8736 a bit-field reference and expect the result to be the first storage
8737 unit assigned to the object. Even in cases where this will result
8738 in unaligned accesses at run time, nothing is supposed to be done
8739 and the program is considered as erroneous instead (see PR c/18287).
8740
8741 The implicit assumptions made in the middle-end are in keeping with
8742 the C viewpoint described above:
8743 - the address of a bit-field reference is supposed to be never
8744 taken; the compiler (generally) will stop on such a construct,
8745 - any other tree is addressable if it is formally addressable,
8746 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
8747
8748 In Ada, the viewpoint is the opposite one: nothing is addressable
8749 at the language level unless explicitly declared so. This means
8750 that the compiler will both make sure that the trees representing
8751 references to addressable ("aliased" in Ada parlance) objects are
8752 addressable and make no real attempts at ensuring that the trees
8753 representing references to non-addressable objects are addressable.
8754
8755 In the first case, Ada is effectively equivalent to C and handing
8756 down the direct result of applying ADDR_EXPR to these trees to the
8757 middle-end works flawlessly. In the second case, Ada cannot afford
8758 to consider the program as erroneous if the address of trees that
8759 are not addressable is requested for technical reasons, unlike C;
8760 as a consequence, the Ada compiler must arrange for either making
8761 sure that this address is not requested in the middle-end or for
8762 compensating by inserting temporaries if it is requested in Gigi.
8763
8764 The first goal can be achieved because the middle-end should not
8765 request the address of non-addressable trees on its own; the only
8766 exception is for the invocation of low-level block operations like
8767 memcpy, for which the addressability requirements are lower since
8768 the type's alignment can be disregarded. In practice, this means
8769 that Gigi must make sure that such operations cannot be applied to
8770 non-BLKmode bit-fields.
8771
8772 The second goal is achieved by means of the addressable_p predicate,
8773 which computes whether a temporary must be inserted by Gigi when the
8774 address of a tree is requested; if so, the address of the temporary
8775 will be used in lieu of that of the original tree and some glue code
8776 generated to connect everything together. */
8777
8778 static bool
8779 addressable_p (tree gnu_expr, tree gnu_type)
8780 {
8781 /* For an integral type, the size of the actual type of the object may not
8782 be greater than that of the expected type, otherwise an indirect access
8783 in the latter type wouldn't correctly set all the bits of the object. */
8784 if (gnu_type
8785 && INTEGRAL_TYPE_P (gnu_type)
8786 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
8787 return false;
8788
8789 /* The size of the actual type of the object may not be smaller than that
8790 of the expected type, otherwise an indirect access in the latter type
8791 would be larger than the object. But only record types need to be
8792 considered in practice for this case. */
8793 if (gnu_type
8794 && TREE_CODE (gnu_type) == RECORD_TYPE
8795 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
8796 return false;
8797
8798 switch (TREE_CODE (gnu_expr))
8799 {
8800 case VAR_DECL:
8801 case PARM_DECL:
8802 case FUNCTION_DECL:
8803 case RESULT_DECL:
8804 /* All DECLs are addressable: if they are in a register, we can force
8805 them to memory. */
8806 return true;
8807
8808 case UNCONSTRAINED_ARRAY_REF:
8809 case INDIRECT_REF:
8810 /* Taking the address of a dereference yields the original pointer. */
8811 return true;
8812
8813 case STRING_CST:
8814 case INTEGER_CST:
8815 /* Taking the address yields a pointer to the constant pool. */
8816 return true;
8817
8818 case CONSTRUCTOR:
8819 /* Taking the address of a static constructor yields a pointer to the
8820 tree constant pool. */
8821 return TREE_STATIC (gnu_expr) ? true : false;
8822
8823 case NULL_EXPR:
8824 case SAVE_EXPR:
8825 case CALL_EXPR:
8826 case PLUS_EXPR:
8827 case MINUS_EXPR:
8828 case BIT_IOR_EXPR:
8829 case BIT_XOR_EXPR:
8830 case BIT_AND_EXPR:
8831 case BIT_NOT_EXPR:
8832 /* All rvalues are deemed addressable since taking their address will
8833 force a temporary to be created by the middle-end. */
8834 return true;
8835
8836 case COMPOUND_EXPR:
8837 /* The address of a compound expression is that of its 2nd operand. */
8838 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
8839
8840 case COND_EXPR:
8841 /* We accept &COND_EXPR as soon as both operands are addressable and
8842 expect the outcome to be the address of the selected operand. */
8843 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
8844 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
8845
8846 case COMPONENT_REF:
8847 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
8848 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
8849 the field is sufficiently aligned, in case it is subject
8850 to a pragma Component_Alignment. But we don't need to
8851 check the alignment of the containing record, as it is
8852 guaranteed to be not smaller than that of its most
8853 aligned field that is not a bit-field. */
8854 && (!STRICT_ALIGNMENT
8855 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
8856 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
8857 /* The field of a padding record is always addressable. */
8858 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
8859 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8860
8861 case ARRAY_REF: case ARRAY_RANGE_REF:
8862 case REALPART_EXPR: case IMAGPART_EXPR:
8863 case NOP_EXPR:
8864 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
8865
8866 case CONVERT_EXPR:
8867 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
8868 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8869
8870 case VIEW_CONVERT_EXPR:
8871 {
8872 /* This is addressable if we can avoid a copy. */
8873 tree type = TREE_TYPE (gnu_expr);
8874 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
8875 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
8876 && (!STRICT_ALIGNMENT
8877 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8878 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
8879 || ((TYPE_MODE (type) == BLKmode
8880 || TYPE_MODE (inner_type) == BLKmode)
8881 && (!STRICT_ALIGNMENT
8882 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8883 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
8884 || TYPE_ALIGN_OK (type)
8885 || TYPE_ALIGN_OK (inner_type))))
8886 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8887 }
8888
8889 default:
8890 return false;
8891 }
8892 }
8893 \f
8894 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
8895 a separate Freeze node exists, delay the bulk of the processing. Otherwise
8896 make a GCC type for GNAT_ENTITY and set up the correspondence. */
8897
8898 void
8899 process_type (Entity_Id gnat_entity)
8900 {
8901 tree gnu_old
8902 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
8903 tree gnu_new;
8904
8905 /* If we are to delay elaboration of this type, just do any
8906 elaborations needed for expressions within the declaration and
8907 make a dummy type entry for this node and its Full_View (if
8908 any) in case something points to it. Don't do this if it
8909 has already been done (the only way that can happen is if
8910 the private completion is also delayed). */
8911 if (Present (Freeze_Node (gnat_entity))
8912 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8913 && Present (Full_View (gnat_entity))
8914 && Present (Freeze_Node (Full_View (gnat_entity)))
8915 && !present_gnu_tree (Full_View (gnat_entity))))
8916 {
8917 elaborate_entity (gnat_entity);
8918
8919 if (!gnu_old)
8920 {
8921 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
8922 save_gnu_tree (gnat_entity, gnu_decl, false);
8923 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8924 && Present (Full_View (gnat_entity)))
8925 {
8926 if (Has_Completion_In_Body (gnat_entity))
8927 DECL_TAFT_TYPE_P (gnu_decl) = 1;
8928 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
8929 }
8930 }
8931
8932 return;
8933 }
8934
8935 /* If we saved away a dummy type for this node it means that this
8936 made the type that corresponds to the full type of an incomplete
8937 type. Clear that type for now and then update the type in the
8938 pointers. */
8939 if (gnu_old)
8940 {
8941 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
8942 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
8943
8944 save_gnu_tree (gnat_entity, NULL_TREE, false);
8945 }
8946
8947 /* Now fully elaborate the type. */
8948 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
8949 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
8950
8951 /* If we have an old type and we've made pointers to this type, update those
8952 pointers. If this is a Taft amendment type in the main unit, we need to
8953 mark the type as used since other units referencing it don't see the full
8954 declaration and, therefore, cannot mark it as used themselves. */
8955 if (gnu_old)
8956 {
8957 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8958 TREE_TYPE (gnu_new));
8959 if (DECL_TAFT_TYPE_P (gnu_old))
8960 used_types_insert (TREE_TYPE (gnu_new));
8961 }
8962
8963 /* If this is a record type corresponding to a task or protected type
8964 that is a completion of an incomplete type, perform a similar update
8965 on the type. ??? Including protected types here is a guess. */
8966 if (IN (Ekind (gnat_entity), Record_Kind)
8967 && Is_Concurrent_Record_Type (gnat_entity)
8968 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
8969 {
8970 tree gnu_task_old
8971 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
8972
8973 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8974 NULL_TREE, false);
8975 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8976 gnu_new, false);
8977
8978 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
8979 TREE_TYPE (gnu_new));
8980 }
8981 }
8982 \f
8983 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
8984 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
8985 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
8986
8987 static tree
8988 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
8989 {
8990 tree gnu_list = NULL_TREE, gnu_result;
8991
8992 /* We test for GNU_FIELD being empty in the case where a variant
8993 was the last thing since we don't take things off GNAT_ASSOC in
8994 that case. We check GNAT_ASSOC in case we have a variant, but it
8995 has no fields. */
8996
8997 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
8998 {
8999 Node_Id gnat_field = First (Choices (gnat_assoc));
9000 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
9001 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
9002
9003 /* The expander is supposed to put a single component selector name
9004 in every record component association. */
9005 gcc_assert (No (Next (gnat_field)));
9006
9007 /* Ignore fields that have Corresponding_Discriminants since we'll
9008 be setting that field in the parent. */
9009 if (Present (Corresponding_Discriminant (Entity (gnat_field)))
9010 && Is_Tagged_Type (Scope (Entity (gnat_field))))
9011 continue;
9012
9013 /* Also ignore discriminants of Unchecked_Unions. */
9014 if (Is_Unchecked_Union (gnat_entity)
9015 && Ekind (Entity (gnat_field)) == E_Discriminant)
9016 continue;
9017
9018 /* Before assigning a value in an aggregate make sure range checks
9019 are done if required. Then convert to the type of the field. */
9020 if (Do_Range_Check (Expression (gnat_assoc)))
9021 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
9022
9023 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
9024
9025 /* Add the field and expression to the list. */
9026 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
9027 }
9028
9029 gnu_result = extract_values (gnu_list, gnu_type);
9030
9031 #ifdef ENABLE_CHECKING
9032 /* Verify that every entry in GNU_LIST was used. */
9033 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
9034 gcc_assert (TREE_ADDRESSABLE (gnu_list));
9035 #endif
9036
9037 return gnu_result;
9038 }
9039
9040 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
9041 the first element of an array aggregate. It may itself be an aggregate.
9042 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
9043 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
9044 for range checking. */
9045
9046 static tree
9047 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
9048 Entity_Id gnat_component_type)
9049 {
9050 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
9051 tree gnu_expr;
9052 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
9053
9054 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
9055 {
9056 /* If the expression is itself an array aggregate then first build the
9057 innermost constructor if it is part of our array (multi-dimensional
9058 case). */
9059 if (Nkind (gnat_expr) == N_Aggregate
9060 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
9061 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
9062 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
9063 TREE_TYPE (gnu_array_type),
9064 gnat_component_type);
9065 else
9066 {
9067 gnu_expr = gnat_to_gnu (gnat_expr);
9068
9069 /* Before assigning the element to the array, make sure it is
9070 in range. */
9071 if (Do_Range_Check (gnat_expr))
9072 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
9073 }
9074
9075 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
9076 convert (TREE_TYPE (gnu_array_type), gnu_expr));
9077
9078 gnu_index = int_const_binop (PLUS_EXPR, gnu_index,
9079 convert (TREE_TYPE (gnu_index),
9080 integer_one_node));
9081 }
9082
9083 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
9084 }
9085 \f
9086 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
9087 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
9088 of the associations that are from RECORD_TYPE. If we see an internal
9089 record, make a recursive call to fill it in as well. */
9090
9091 static tree
9092 extract_values (tree values, tree record_type)
9093 {
9094 tree field, tem;
9095 vec<constructor_elt, va_gc> *v = NULL;
9096
9097 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9098 {
9099 tree value = 0;
9100
9101 /* _Parent is an internal field, but may have values in the aggregate,
9102 so check for values first. */
9103 if ((tem = purpose_member (field, values)))
9104 {
9105 value = TREE_VALUE (tem);
9106 TREE_ADDRESSABLE (tem) = 1;
9107 }
9108
9109 else if (DECL_INTERNAL_P (field))
9110 {
9111 value = extract_values (values, TREE_TYPE (field));
9112 if (TREE_CODE (value) == CONSTRUCTOR
9113 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
9114 value = 0;
9115 }
9116 else
9117 /* If we have a record subtype, the names will match, but not the
9118 actual FIELD_DECLs. */
9119 for (tem = values; tem; tem = TREE_CHAIN (tem))
9120 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
9121 {
9122 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
9123 TREE_ADDRESSABLE (tem) = 1;
9124 }
9125
9126 if (!value)
9127 continue;
9128
9129 CONSTRUCTOR_APPEND_ELT (v, field, value);
9130 }
9131
9132 return gnat_build_constructor (record_type, v);
9133 }
9134 \f
9135 /* Process a N_Validate_Unchecked_Conversion node. */
9136
9137 static void
9138 validate_unchecked_conversion (Node_Id gnat_node)
9139 {
9140 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
9141 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
9142
9143 /* If the target is a pointer type, see if we are either converting from a
9144 non-pointer or from a pointer to a type with a different alias set and
9145 warn if so, unless the pointer has been marked to alias everything. */
9146 if (POINTER_TYPE_P (gnu_target_type)
9147 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
9148 {
9149 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
9150 ? TREE_TYPE (gnu_source_type)
9151 : NULL_TREE;
9152 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
9153 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9154
9155 if (target_alias_set != 0
9156 && (!POINTER_TYPE_P (gnu_source_type)
9157 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9158 target_alias_set)))
9159 {
9160 post_error_ne ("?possible aliasing problem for type&",
9161 gnat_node, Target_Type (gnat_node));
9162 post_error ("\\?use -fno-strict-aliasing switch for references",
9163 gnat_node);
9164 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
9165 gnat_node, Target_Type (gnat_node));
9166 }
9167 }
9168
9169 /* Likewise if the target is a fat pointer type, but we have no mechanism to
9170 mitigate the problem in this case, so we unconditionally warn. */
9171 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
9172 {
9173 tree gnu_source_desig_type
9174 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
9175 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
9176 : NULL_TREE;
9177 tree gnu_target_desig_type
9178 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
9179 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9180
9181 if (target_alias_set != 0
9182 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
9183 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9184 target_alias_set)))
9185 {
9186 post_error_ne ("?possible aliasing problem for type&",
9187 gnat_node, Target_Type (gnat_node));
9188 post_error ("\\?use -fno-strict-aliasing switch for references",
9189 gnat_node);
9190 }
9191 }
9192 }
9193 \f
9194 /* EXP is to be treated as an array or record. Handle the cases when it is
9195 an access object and perform the required dereferences. */
9196
9197 static tree
9198 maybe_implicit_deref (tree exp)
9199 {
9200 /* If the type is a pointer, dereference it. */
9201 if (POINTER_TYPE_P (TREE_TYPE (exp))
9202 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp)))
9203 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
9204
9205 /* If we got a padded type, remove it too. */
9206 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
9207 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
9208
9209 return exp;
9210 }
9211 \f
9212 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
9213 location and false if it doesn't. In the former case, set the Gigi global
9214 variable REF_FILENAME to the simple debug file name as given by sinput.
9215 If clear_column is true, set column information to 0. */
9216
9217 static bool
9218 Sloc_to_locus1 (Source_Ptr Sloc, location_t *locus, bool clear_column)
9219 {
9220 if (Sloc == No_Location)
9221 return false;
9222
9223 if (Sloc <= Standard_Location)
9224 {
9225 *locus = BUILTINS_LOCATION;
9226 return false;
9227 }
9228 else
9229 {
9230 Source_File_Index file = Get_Source_File_Index (Sloc);
9231 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
9232 Column_Number column = (clear_column ? 0 : Get_Column_Number (Sloc));
9233 struct line_map *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
9234
9235 /* We can have zero if pragma Source_Reference is in effect. */
9236 if (line < 1)
9237 line = 1;
9238
9239 /* Translate the location. */
9240 *locus = linemap_position_for_line_and_column (map, line, column);
9241 }
9242
9243 ref_filename
9244 = IDENTIFIER_POINTER
9245 (get_identifier
9246 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
9247
9248 return true;
9249 }
9250
9251 /* Similar to the above, not clearing the column information. */
9252
9253 bool
9254 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
9255 {
9256 return Sloc_to_locus1 (Sloc, locus, false);
9257 }
9258
9259 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
9260 don't do anything if it doesn't correspond to a source location. */
9261
9262 static void
9263 set_expr_location_from_node1 (tree node, Node_Id gnat_node, bool clear_column)
9264 {
9265 location_t locus;
9266
9267 if (!Sloc_to_locus1 (Sloc (gnat_node), &locus, clear_column))
9268 return;
9269
9270 SET_EXPR_LOCATION (node, locus);
9271 }
9272
9273 /* Similar to the above, not clearing the column information. */
9274
9275 static void
9276 set_expr_location_from_node (tree node, Node_Id gnat_node)
9277 {
9278 set_expr_location_from_node1 (node, gnat_node, false);
9279 }
9280
9281 /* More elaborate version of set_expr_location_from_node to be used in more
9282 general contexts, for example the result of the translation of a generic
9283 GNAT node. */
9284
9285 static void
9286 set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
9287 {
9288 /* Set the location information on the node if it is a real expression.
9289 References can be reused for multiple GNAT nodes and they would get
9290 the location information of their last use. Also make sure not to
9291 overwrite an existing location as it is probably more precise. */
9292
9293 switch (TREE_CODE (node))
9294 {
9295 CASE_CONVERT:
9296 case NON_LVALUE_EXPR:
9297 case SAVE_EXPR:
9298 break;
9299
9300 case COMPOUND_EXPR:
9301 if (EXPR_P (TREE_OPERAND (node, 1)))
9302 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
9303
9304 /* ... fall through ... */
9305
9306 default:
9307 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
9308 {
9309 set_expr_location_from_node (node, gnat_node);
9310 set_end_locus_from_node (node, gnat_node);
9311 }
9312 break;
9313 }
9314 }
9315 \f
9316 /* Return a colon-separated list of encodings contained in encoded Ada
9317 name. */
9318
9319 static const char *
9320 extract_encoding (const char *name)
9321 {
9322 char *encoding = (char *) ggc_alloc_atomic (strlen (name));
9323 get_encoding (name, encoding);
9324 return encoding;
9325 }
9326
9327 /* Extract the Ada name from an encoded name. */
9328
9329 static const char *
9330 decode_name (const char *name)
9331 {
9332 char *decoded = (char *) ggc_alloc_atomic (strlen (name) * 2 + 60);
9333 __gnat_decode (name, decoded, 0);
9334 return decoded;
9335 }
9336 \f
9337 /* Post an error message. MSG is the error message, properly annotated.
9338 NODE is the node at which to post the error and the node to use for the
9339 '&' substitution. */
9340
9341 void
9342 post_error (const char *msg, Node_Id node)
9343 {
9344 String_Template temp;
9345 String_Pointer sp;
9346
9347 if (No (node))
9348 return;
9349
9350 temp.Low_Bound = 1;
9351 temp.High_Bound = strlen (msg);
9352 sp.Bounds = &temp;
9353 sp.Array = msg;
9354 Error_Msg_N (sp, node);
9355 }
9356
9357 /* Similar to post_error, but NODE is the node at which to post the error and
9358 ENT is the node to use for the '&' substitution. */
9359
9360 void
9361 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
9362 {
9363 String_Template temp;
9364 String_Pointer sp;
9365
9366 if (No (node))
9367 return;
9368
9369 temp.Low_Bound = 1;
9370 temp.High_Bound = strlen (msg);
9371 sp.Bounds = &temp;
9372 sp.Array = msg;
9373 Error_Msg_NE (sp, node, ent);
9374 }
9375
9376 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
9377
9378 void
9379 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
9380 {
9381 Error_Msg_Uint_1 = UI_From_Int (num);
9382 post_error_ne (msg, node, ent);
9383 }
9384
9385 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
9386 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
9387 most sense. Return true if a sensible assignment was performed. */
9388
9389 static bool
9390 set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
9391 {
9392 Node_Id gnat_end_label = Empty;
9393 location_t end_locus;
9394
9395 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
9396 end_locus when there is one. We consider only GNAT nodes with a possible
9397 End_Label attached. If the End_Label actually was unassigned, fallback
9398 on the original node. We'd better assign an explicit sloc associated with
9399 the outer construct in any case. */
9400
9401 switch (Nkind (gnat_node))
9402 {
9403 case N_Package_Body:
9404 case N_Subprogram_Body:
9405 case N_Block_Statement:
9406 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
9407 break;
9408
9409 case N_Package_Declaration:
9410 gnat_end_label = End_Label (Specification (gnat_node));
9411 break;
9412
9413 default:
9414 return false;
9415 }
9416
9417 gnat_node = Present (gnat_end_label) ? gnat_end_label : gnat_node;
9418
9419 /* Some expanded subprograms have neither an End_Label nor a Sloc
9420 attached. Notify that to callers. For a block statement with no
9421 End_Label, clear column information, so that the tree for a
9422 transient block does not receive the sloc of a source condition. */
9423
9424 if (!Sloc_to_locus1 (Sloc (gnat_node), &end_locus,
9425 No (gnat_end_label) &&
9426 (Nkind (gnat_node) == N_Block_Statement)))
9427 return false;
9428
9429 switch (TREE_CODE (gnu_node))
9430 {
9431 case BIND_EXPR:
9432 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
9433 return true;
9434
9435 case FUNCTION_DECL:
9436 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
9437 return true;
9438
9439 default:
9440 return false;
9441 }
9442 }
9443 \f
9444 /* Similar to post_error_ne, but T is a GCC tree representing the number to
9445 write. If T represents a constant, the text inside curly brackets in
9446 MSG will be output (presumably including a '^'). Otherwise it will not
9447 be output and the text inside square brackets will be output instead. */
9448
9449 void
9450 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
9451 {
9452 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
9453 char start_yes, end_yes, start_no, end_no;
9454 const char *p;
9455 char *q;
9456
9457 if (TREE_CODE (t) == INTEGER_CST)
9458 {
9459 Error_Msg_Uint_1 = UI_From_gnu (t);
9460 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
9461 }
9462 else
9463 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
9464
9465 for (p = msg, q = new_msg; *p; p++)
9466 {
9467 if (*p == start_yes)
9468 for (p++; *p != end_yes; p++)
9469 *q++ = *p;
9470 else if (*p == start_no)
9471 for (p++; *p != end_no; p++)
9472 ;
9473 else
9474 *q++ = *p;
9475 }
9476
9477 *q = 0;
9478
9479 post_error_ne (new_msg, node, ent);
9480 }
9481
9482 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
9483
9484 void
9485 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
9486 int num)
9487 {
9488 Error_Msg_Uint_2 = UI_From_Int (num);
9489 post_error_ne_tree (msg, node, ent, t);
9490 }
9491 \f
9492 /* Initialize the table that maps GNAT codes to GCC codes for simple
9493 binary and unary operations. */
9494
9495 static void
9496 init_code_table (void)
9497 {
9498 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
9499 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
9500
9501 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
9502 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
9503 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
9504 gnu_codes[N_Op_Eq] = EQ_EXPR;
9505 gnu_codes[N_Op_Ne] = NE_EXPR;
9506 gnu_codes[N_Op_Lt] = LT_EXPR;
9507 gnu_codes[N_Op_Le] = LE_EXPR;
9508 gnu_codes[N_Op_Gt] = GT_EXPR;
9509 gnu_codes[N_Op_Ge] = GE_EXPR;
9510 gnu_codes[N_Op_Add] = PLUS_EXPR;
9511 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
9512 gnu_codes[N_Op_Multiply] = MULT_EXPR;
9513 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
9514 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
9515 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
9516 gnu_codes[N_Op_Abs] = ABS_EXPR;
9517 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
9518 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
9519 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
9520 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
9521 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
9522 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
9523 }
9524
9525 /* Return a label to branch to for the exception type in KIND or NULL_TREE
9526 if none. */
9527
9528 tree
9529 get_exception_label (char kind)
9530 {
9531 if (kind == N_Raise_Constraint_Error)
9532 return gnu_constraint_error_label_stack->last ();
9533 else if (kind == N_Raise_Storage_Error)
9534 return gnu_storage_error_label_stack->last ();
9535 else if (kind == N_Raise_Program_Error)
9536 return gnu_program_error_label_stack->last ();
9537 else
9538 return NULL_TREE;
9539 }
9540
9541 /* Return the decl for the current elaboration procedure. */
9542
9543 tree
9544 get_elaboration_procedure (void)
9545 {
9546 return gnu_elab_proc_stack->last ();
9547 }
9548
9549 #include "gt-ada-trans.h"