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