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