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