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