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