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