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