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