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