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