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