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