* gcc-interface/trans.c (Attribute_to_gnu) <Attr_Deref>: New case.
[official-gcc.git] / gcc / ada / gcc-interface / trans.c
blob7379477215868790973dba1f6f78f5574a61d1ae
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2015, Free Software Foundation, Inc. *
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- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
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 *
18 * Public License distributed with GNAT; see file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
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 ****************************************************************************/
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "vec.h"
33 #include "double-int.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "real.h"
40 #include "tree.h"
41 #include "fold-const.h"
42 #include "stringpool.h"
43 #include "stor-layout.h"
44 #include "stmt.h"
45 #include "varasm.h"
46 #include "flags.h"
47 #include "output.h"
48 #include "libfuncs.h" /* For set_stack_check_libfunc. */
49 #include "tree-iterator.h"
50 #include "gimple-expr.h"
51 #include "gimplify.h"
52 #include "bitmap.h"
53 #include "hash-map.h"
54 #include "is-a.h"
55 #include "plugin-api.h"
56 #include "hard-reg-set.h"
57 #include "input.h"
58 #include "function.h"
59 #include "ipa-ref.h"
60 #include "cgraph.h"
61 #include "diagnostic.h"
62 #include "opts.h"
63 #include "target.h"
64 #include "common/common-target.h"
66 #include "ada.h"
67 #include "adadecode.h"
68 #include "types.h"
69 #include "atree.h"
70 #include "elists.h"
71 #include "namet.h"
72 #include "nlists.h"
73 #include "snames.h"
74 #include "stringt.h"
75 #include "uintp.h"
76 #include "urealp.h"
77 #include "fe.h"
78 #include "sinfo.h"
79 #include "einfo.h"
80 #include "gadaint.h"
81 #include "ada-tree.h"
82 #include "gigi.h"
84 /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
85 for fear of running out of stack space. If we need more, we use xmalloc
86 instead. */
87 #define ALLOCA_THRESHOLD 1000
89 /* In configurations where blocks have no end_locus attached, just
90 sink assignments into a dummy global. */
91 #ifndef BLOCK_SOURCE_END_LOCATION
92 static location_t block_end_locus_sink;
93 #define BLOCK_SOURCE_END_LOCATION(BLOCK) block_end_locus_sink
94 #endif
96 /* Pointers to front-end tables accessed through macros. */
97 struct Node *Nodes_Ptr;
98 struct Flags *Flags_Ptr;
99 Node_Id *Next_Node_Ptr;
100 Node_Id *Prev_Node_Ptr;
101 struct Elist_Header *Elists_Ptr;
102 struct Elmt_Item *Elmts_Ptr;
103 struct String_Entry *Strings_Ptr;
104 Char_Code *String_Chars_Ptr;
105 struct List_Header *List_Headers_Ptr;
107 /* Highest number in the front-end node table. */
108 int max_gnat_nodes;
110 /* Current node being treated, in case abort called. */
111 Node_Id error_gnat_node;
113 /* True when gigi is being called on an analyzed but unexpanded
114 tree, and the only purpose of the call is to properly annotate
115 types with representation information. */
116 bool type_annotate_only;
118 /* Current filename without path. */
119 const char *ref_filename;
122 /* List of N_Validate_Unchecked_Conversion nodes in the unit. */
123 static vec<Node_Id> gnat_validate_uc_list;
125 /* When not optimizing, we cache the 'First, 'Last and 'Length attributes
126 of unconstrained array IN parameters to avoid emitting a great deal of
127 redundant instructions to recompute them each time. */
128 struct GTY (()) parm_attr_d {
129 int id; /* GTY doesn't like Entity_Id. */
130 int dim;
131 tree first;
132 tree last;
133 tree length;
136 typedef struct parm_attr_d *parm_attr;
139 struct GTY(()) language_function {
140 vec<parm_attr, va_gc> *parm_attr_cache;
141 bitmap named_ret_val;
142 vec<tree, va_gc> *other_ret_val;
143 int gnat_ret;
146 #define f_parm_attr_cache \
147 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
149 #define f_named_ret_val \
150 DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val
152 #define f_other_ret_val \
153 DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val
155 #define f_gnat_ret \
156 DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret
158 /* A structure used to gather together information about a statement group.
159 We use this to gather related statements, for example the "then" part
160 of a IF. In the case where it represents a lexical scope, we may also
161 have a BLOCK node corresponding to it and/or cleanups. */
163 struct GTY((chain_next ("%h.previous"))) stmt_group {
164 struct stmt_group *previous; /* Previous code group. */
165 tree stmt_list; /* List of statements for this code group. */
166 tree block; /* BLOCK for this code group, if any. */
167 tree cleanups; /* Cleanups for this code group, if any. */
170 static GTY(()) struct stmt_group *current_stmt_group;
172 /* List of unused struct stmt_group nodes. */
173 static GTY((deletable)) struct stmt_group *stmt_group_free_list;
175 /* A structure used to record information on elaboration procedures
176 we've made and need to process.
178 ??? gnat_node should be Node_Id, but gengtype gets confused. */
180 struct GTY((chain_next ("%h.next"))) elab_info {
181 struct elab_info *next; /* Pointer to next in chain. */
182 tree elab_proc; /* Elaboration procedure. */
183 int gnat_node; /* The N_Compilation_Unit. */
186 static GTY(()) struct elab_info *elab_info_list;
188 /* Stack of exception pointer variables. Each entry is the VAR_DECL
189 that stores the address of the raised exception. Nonzero means we
190 are in an exception handler. Not used in the zero-cost case. */
191 static GTY(()) vec<tree, va_gc> *gnu_except_ptr_stack;
193 /* In ZCX case, current exception pointer. Used to re-raise it. */
194 static GTY(()) tree gnu_incoming_exc_ptr;
196 /* Stack for storing the current elaboration procedure decl. */
197 static GTY(()) vec<tree, va_gc> *gnu_elab_proc_stack;
199 /* Stack of labels to be used as a goto target instead of a return in
200 some functions. See processing for N_Subprogram_Body. */
201 static GTY(()) vec<tree, va_gc> *gnu_return_label_stack;
203 /* Stack of variable for the return value of a function with copy-in/copy-out
204 parameters. See processing for N_Subprogram_Body. */
205 static GTY(()) vec<tree, va_gc> *gnu_return_var_stack;
207 /* Structure used to record information for a range check. */
208 struct GTY(()) range_check_info_d {
209 tree low_bound;
210 tree high_bound;
211 tree type;
212 tree invariant_cond;
215 typedef struct range_check_info_d *range_check_info;
218 /* Structure used to record information for a loop. */
219 struct GTY(()) loop_info_d {
220 tree stmt;
221 tree loop_var;
222 vec<range_check_info, va_gc> *checks;
225 typedef struct loop_info_d *loop_info;
228 /* Stack of loop_info structures associated with LOOP_STMT nodes. */
229 static GTY(()) vec<loop_info, va_gc> *gnu_loop_stack;
231 /* The stacks for N_{Push,Pop}_*_Label. */
232 static GTY(()) vec<tree, va_gc> *gnu_constraint_error_label_stack;
233 static GTY(()) vec<tree, va_gc> *gnu_storage_error_label_stack;
234 static GTY(()) vec<tree, va_gc> *gnu_program_error_label_stack;
236 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
237 static enum tree_code gnu_codes[Number_Node_Kinds];
239 static void init_code_table (void);
240 static void Compilation_Unit_to_gnu (Node_Id);
241 static void record_code_position (Node_Id);
242 static void insert_code_for (Node_Id);
243 static void add_cleanup (tree, Node_Id);
244 static void add_stmt_list (List_Id);
245 static void push_exception_label_stack (vec<tree, va_gc> **, Entity_Id);
246 static tree build_stmt_group (List_Id, bool);
247 static inline bool stmt_group_may_fallthru (void);
248 static enum gimplify_status gnat_gimplify_stmt (tree *);
249 static void elaborate_all_entities (Node_Id);
250 static void process_freeze_entity (Node_Id);
251 static void process_decls (List_Id, List_Id, Node_Id, bool, bool);
252 static tree emit_range_check (tree, Node_Id, Node_Id);
253 static tree emit_index_check (tree, tree, tree, tree, Node_Id);
254 static tree emit_check (tree, tree, int, Node_Id);
255 static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id);
256 static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id);
257 static tree convert_with_check (Entity_Id, tree, bool, bool, bool, Node_Id);
258 static bool addressable_p (tree, tree);
259 static tree assoc_to_constructor (Entity_Id, Node_Id, tree);
260 static tree extract_values (tree, tree);
261 static tree pos_to_constructor (Node_Id, tree, Entity_Id);
262 static void validate_unchecked_conversion (Node_Id);
263 static tree maybe_implicit_deref (tree);
264 static void set_expr_location_from_node (tree, Node_Id);
265 static void set_expr_location_from_node1 (tree, Node_Id, bool);
266 static bool Sloc_to_locus1 (Source_Ptr, location_t *, bool);
267 static bool set_end_locus_from_node (tree, Node_Id);
268 static void set_gnu_expr_location_from_node (tree, Node_Id);
269 static int lvalue_required_p (Node_Id, tree, bool, bool, bool);
270 static tree build_raise_check (int, enum exception_info_kind);
271 static tree create_init_temporary (const char *, tree, tree *, Node_Id);
273 /* Hooks for debug info back-ends, only supported and used in a restricted set
274 of configurations. */
275 static const char *extract_encoding (const char *) ATTRIBUTE_UNUSED;
276 static const char *decode_name (const char *) ATTRIBUTE_UNUSED;
278 /* This is the main program of the back-end. It sets up all the table
279 structures and then generates code. */
281 void
282 gigi (Node_Id gnat_root,
283 int max_gnat_node,
284 int number_name ATTRIBUTE_UNUSED,
285 struct Node *nodes_ptr,
286 struct Flags *flags_ptr,
287 Node_Id *next_node_ptr,
288 Node_Id *prev_node_ptr,
289 struct Elist_Header *elists_ptr,
290 struct Elmt_Item *elmts_ptr,
291 struct String_Entry *strings_ptr,
292 Char_Code *string_chars_ptr,
293 struct List_Header *list_headers_ptr,
294 Nat number_file,
295 struct File_Info_Type *file_info_ptr,
296 Entity_Id standard_boolean,
297 Entity_Id standard_integer,
298 Entity_Id standard_character,
299 Entity_Id standard_long_long_float,
300 Entity_Id standard_exception_type,
301 Int gigi_operating_mode)
303 Node_Id gnat_iter;
304 Entity_Id gnat_literal;
305 tree t, ftype, int64_type;
306 struct elab_info *info;
307 int i;
309 max_gnat_nodes = max_gnat_node;
311 Nodes_Ptr = nodes_ptr;
312 Flags_Ptr = flags_ptr;
313 Next_Node_Ptr = next_node_ptr;
314 Prev_Node_Ptr = prev_node_ptr;
315 Elists_Ptr = elists_ptr;
316 Elmts_Ptr = elmts_ptr;
317 Strings_Ptr = strings_ptr;
318 String_Chars_Ptr = string_chars_ptr;
319 List_Headers_Ptr = list_headers_ptr;
321 type_annotate_only = (gigi_operating_mode == 1);
323 for (i = 0; i < number_file; i++)
325 /* Use the identifier table to make a permanent copy of the filename as
326 the name table gets reallocated after Gigi returns but before all the
327 debugging information is output. The __gnat_to_canonical_file_spec
328 call translates filenames from pragmas Source_Reference that contain
329 host style syntax not understood by gdb. */
330 const char *filename
331 = IDENTIFIER_POINTER
332 (get_identifier
333 (__gnat_to_canonical_file_spec
334 (Get_Name_String (file_info_ptr[i].File_Name))));
336 /* We rely on the order isomorphism between files and line maps. */
337 gcc_assert ((int) LINEMAPS_ORDINARY_USED (line_table) == i);
339 /* We create the line map for a source file at once, with a fixed number
340 of columns chosen to avoid jumping over the next power of 2. */
341 linemap_add (line_table, LC_ENTER, 0, filename, 1);
342 linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252);
343 linemap_position_for_column (line_table, 252 - 1);
344 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
347 gcc_assert (Nkind (gnat_root) == N_Compilation_Unit);
349 /* Declare the name of the compilation unit as the first global
350 name in order to make the middle-end fully deterministic. */
351 t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL);
352 first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t));
354 /* Initialize ourselves. */
355 init_code_table ();
356 init_gnat_decl ();
357 init_gnat_utils ();
359 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
360 errors. */
361 if (type_annotate_only)
363 TYPE_SIZE (void_type_node) = bitsize_zero_node;
364 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
367 /* Enable GNAT stack checking method if needed */
368 if (!Stack_Check_Probes_On_Target)
369 set_stack_check_libfunc ("_gnat_stack_check");
371 /* Retrieve alignment settings. */
372 double_float_alignment = get_target_double_float_alignment ();
373 double_scalar_alignment = get_target_double_scalar_alignment ();
375 /* Record the builtin types. Define `integer' and `character' first so that
376 dbx will output them first. */
377 record_builtin_type ("integer", integer_type_node, false);
378 record_builtin_type ("character", unsigned_char_type_node, false);
379 record_builtin_type ("boolean", boolean_type_node, false);
380 record_builtin_type ("void", void_type_node, false);
382 /* Save the type we made for integer as the type for Standard.Integer. */
383 save_gnu_tree (Base_Type (standard_integer),
384 TYPE_NAME (integer_type_node),
385 false);
387 /* Likewise for character as the type for Standard.Character. */
388 save_gnu_tree (Base_Type (standard_character),
389 TYPE_NAME (unsigned_char_type_node),
390 false);
392 /* Likewise for boolean as the type for Standard.Boolean. */
393 save_gnu_tree (Base_Type (standard_boolean),
394 TYPE_NAME (boolean_type_node),
395 false);
396 gnat_literal = First_Literal (Base_Type (standard_boolean));
397 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
398 gcc_assert (t == boolean_false_node);
399 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
400 boolean_type_node, t, true, false, false, false,
401 NULL, gnat_literal);
402 DECL_IGNORED_P (t) = 1;
403 save_gnu_tree (gnat_literal, t, false);
404 gnat_literal = Next_Literal (gnat_literal);
405 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
406 gcc_assert (t == boolean_true_node);
407 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
408 boolean_type_node, t, true, false, false, false,
409 NULL, gnat_literal);
410 DECL_IGNORED_P (t) = 1;
411 save_gnu_tree (gnat_literal, t, false);
413 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
414 ptr_void_ftype = build_pointer_type (void_ftype);
416 /* Now declare run-time functions. */
417 ftype = build_function_type_list (ptr_void_type_node, sizetype, NULL_TREE);
419 /* malloc is a function declaration tree for a function to allocate
420 memory. */
421 malloc_decl
422 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE,
423 ftype, NULL_TREE, is_disabled, true, true, true,
424 NULL, Empty);
425 DECL_IS_MALLOC (malloc_decl) = 1;
427 /* free is a function declaration tree for a function to free memory. */
428 free_decl
429 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
430 build_function_type_list (void_type_node,
431 ptr_void_type_node,
432 NULL_TREE),
433 NULL_TREE, is_disabled, true, true, true, NULL,
434 Empty);
436 /* This is used for 64-bit multiplication with overflow checking. */
437 int64_type = gnat_type_for_size (64, 0);
438 mulv64_decl
439 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE,
440 build_function_type_list (int64_type, int64_type,
441 int64_type, NULL_TREE),
442 NULL_TREE, is_disabled, true, true, true, NULL,
443 Empty);
445 /* Name of the _Parent field in tagged record types. */
446 parent_name_id = get_identifier (Get_Name_String (Name_uParent));
448 /* Name of the Exception_Data type defined in System.Standard_Library. */
449 exception_data_name_id
450 = get_identifier ("system__standard_library__exception_data");
452 /* Make the types and functions used for exception processing. */
453 jmpbuf_type
454 = build_array_type (gnat_type_for_mode (Pmode, 0),
455 build_index_type (size_int (5)));
456 record_builtin_type ("JMPBUF_T", jmpbuf_type, true);
457 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
459 /* Functions to get and set the jumpbuf pointer for the current thread. */
460 get_jmpbuf_decl
461 = create_subprog_decl
462 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
463 NULL_TREE, build_function_type_list (jmpbuf_ptr_type, NULL_TREE),
464 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
465 DECL_IGNORED_P (get_jmpbuf_decl) = 1;
467 set_jmpbuf_decl
468 = create_subprog_decl
469 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
470 NULL_TREE, build_function_type_list (void_type_node, jmpbuf_ptr_type,
471 NULL_TREE),
472 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
473 DECL_IGNORED_P (set_jmpbuf_decl) = 1;
475 /* setjmp returns an integer and has one operand, which is a pointer to
476 a jmpbuf. */
477 setjmp_decl
478 = create_subprog_decl
479 (get_identifier ("__builtin_setjmp"), NULL_TREE,
480 build_function_type_list (integer_type_node, jmpbuf_ptr_type,
481 NULL_TREE),
482 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
483 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
484 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
486 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
487 address. */
488 update_setjmp_buf_decl
489 = create_subprog_decl
490 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
491 build_function_type_list (void_type_node, jmpbuf_ptr_type, NULL_TREE),
492 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
493 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
494 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
496 /* Hooks to call when entering/leaving an exception handler. */
497 ftype
498 = build_function_type_list (void_type_node, ptr_void_type_node, NULL_TREE);
500 begin_handler_decl
501 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
502 ftype, NULL_TREE, is_disabled, true, true, true,
503 NULL, Empty);
504 DECL_IGNORED_P (begin_handler_decl) = 1;
506 end_handler_decl
507 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
508 ftype, NULL_TREE, is_disabled, true, true, true,
509 NULL, Empty);
510 DECL_IGNORED_P (end_handler_decl) = 1;
512 unhandled_except_decl
513 = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
514 NULL_TREE,
515 ftype, NULL_TREE, is_disabled, true, true, true,
516 NULL, Empty);
517 DECL_IGNORED_P (unhandled_except_decl) = 1;
519 reraise_zcx_decl
520 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE,
521 ftype, NULL_TREE, is_disabled, true, true, true,
522 NULL, Empty);
523 /* Indicate that these never return. */
524 DECL_IGNORED_P (reraise_zcx_decl) = 1;
525 TREE_THIS_VOLATILE (reraise_zcx_decl) = 1;
526 TREE_SIDE_EFFECTS (reraise_zcx_decl) = 1;
527 TREE_TYPE (reraise_zcx_decl)
528 = build_qualified_type (TREE_TYPE (reraise_zcx_decl), TYPE_QUAL_VOLATILE);
530 /* If in no exception handlers mode, all raise statements are redirected to
531 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl since
532 this procedure will never be called in this mode. */
533 if (No_Exception_Handlers_Set ())
535 tree decl
536 = create_subprog_decl
537 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE,
538 build_function_type_list (void_type_node,
539 build_pointer_type
540 (unsigned_char_type_node),
541 integer_type_node, NULL_TREE),
542 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
543 TREE_THIS_VOLATILE (decl) = 1;
544 TREE_SIDE_EFFECTS (decl) = 1;
545 TREE_TYPE (decl)
546 = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
547 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
548 gnat_raise_decls[i] = decl;
550 else
552 /* Otherwise, make one decl for each exception reason. */
553 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
554 gnat_raise_decls[i] = build_raise_check (i, exception_simple);
555 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++)
556 gnat_raise_decls_ext[i]
557 = build_raise_check (i,
558 i == CE_Index_Check_Failed
559 || i == CE_Range_Check_Failed
560 || i == CE_Invalid_Data
561 ? exception_range : exception_column);
564 /* Set the types that GCC and Gigi use from the front end. */
565 except_type_node = gnat_to_gnu_type (Base_Type (standard_exception_type));
567 /* Make other functions used for exception processing. */
568 get_excptr_decl
569 = create_subprog_decl
570 (get_identifier ("system__soft_links__get_gnat_exception"), NULL_TREE,
571 build_function_type_list (build_pointer_type (except_type_node),
572 NULL_TREE),
573 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
574 DECL_IGNORED_P (get_excptr_decl) = 1;
576 set_exception_parameter_decl
577 = create_subprog_decl
578 (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE,
579 build_function_type_list (void_type_node,
580 ptr_void_type_node,
581 ptr_void_type_node,
582 NULL_TREE),
583 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
585 raise_nodefer_decl
586 = create_subprog_decl
587 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE,
588 build_function_type_list (void_type_node,
589 build_pointer_type (except_type_node),
590 NULL_TREE),
591 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
593 /* Indicate that it never returns. */
594 TREE_THIS_VOLATILE (raise_nodefer_decl) = 1;
595 TREE_SIDE_EFFECTS (raise_nodefer_decl) = 1;
596 TREE_TYPE (raise_nodefer_decl)
597 = build_qualified_type (TREE_TYPE (raise_nodefer_decl),
598 TYPE_QUAL_VOLATILE);
600 /* Build the special descriptor type and its null node if needed. */
601 if (TARGET_VTABLE_USES_DESCRIPTORS)
603 tree null_node = fold_convert (ptr_void_ftype, null_pointer_node);
604 tree field_list = NULL_TREE;
605 int j;
606 vec<constructor_elt, va_gc> *null_vec = NULL;
607 constructor_elt *elt;
609 fdesc_type_node = make_node (RECORD_TYPE);
610 vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS);
611 elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
613 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
615 tree field
616 = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node,
617 NULL_TREE, NULL_TREE, 0, 1);
618 DECL_CHAIN (field) = field_list;
619 field_list = field;
620 elt->index = field;
621 elt->value = null_node;
622 elt--;
625 finish_record_type (fdesc_type_node, nreverse (field_list), 0, false);
626 record_builtin_type ("descriptor", fdesc_type_node, true);
627 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec);
630 longest_float_type_node
631 = get_unpadded_type (Base_Type (standard_long_long_float));
633 /* Dummy objects to materialize "others" and "all others" in the exception
634 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
635 the types to use. */
636 others_decl
637 = create_var_decl (get_identifier ("OTHERS"),
638 get_identifier ("__gnat_others_value"),
639 unsigned_char_type_node,
640 NULL_TREE, true, false, true, false, NULL, Empty);
642 all_others_decl
643 = create_var_decl (get_identifier ("ALL_OTHERS"),
644 get_identifier ("__gnat_all_others_value"),
645 unsigned_char_type_node,
646 NULL_TREE, true, false, true, false, NULL, Empty);
648 unhandled_others_decl
649 = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
650 get_identifier ("__gnat_unhandled_others_value"),
651 unsigned_char_type_node,
652 NULL_TREE, true, false, true, false, NULL, Empty);
654 main_identifier_node = get_identifier ("main");
656 /* Install the builtins we might need, either internally or as
657 user available facilities for Intrinsic imports. */
658 gnat_install_builtins ();
660 vec_safe_push (gnu_except_ptr_stack, NULL_TREE);
661 vec_safe_push (gnu_constraint_error_label_stack, NULL_TREE);
662 vec_safe_push (gnu_storage_error_label_stack, NULL_TREE);
663 vec_safe_push (gnu_program_error_label_stack, NULL_TREE);
665 /* Process any Pragma Ident for the main unit. */
666 if (Present (Ident_String (Main_Unit)))
667 targetm.asm_out.output_ident
668 (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
670 /* If we are using the GCC exception mechanism, let GCC know. */
671 if (Exception_Mechanism == Back_End_Exceptions)
672 gnat_init_gcc_eh ();
674 /* Initialize the GCC support for FP operations. */
675 gnat_init_gcc_fp ();
677 /* Force -fno-strict-aliasing if the configuration pragma was seen. */
678 if (No_Strict_Aliasing_CP)
679 flag_strict_aliasing = 0;
681 /* Save the current optimization options again after the above possible
682 global_options changes. */
683 optimization_default_node = build_optimization_node (&global_options);
684 optimization_current_node = optimization_default_node;
686 /* Now translate the compilation unit proper. */
687 Compilation_Unit_to_gnu (gnat_root);
689 /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at
690 the very end to avoid having to second-guess the front-end when we run
691 into dummy nodes during the regular processing. */
692 for (i = 0; gnat_validate_uc_list.iterate (i, &gnat_iter); i++)
693 validate_unchecked_conversion (gnat_iter);
694 gnat_validate_uc_list.release ();
696 /* Finally see if we have any elaboration procedures to deal with. */
697 for (info = elab_info_list; info; info = info->next)
699 tree gnu_body = DECL_SAVED_TREE (info->elab_proc), gnu_stmts;
701 /* We should have a BIND_EXPR but it may not have any statements in it.
702 If it doesn't have any, we have nothing to do except for setting the
703 flag on the GNAT node. Otherwise, process the function as others. */
704 gnu_stmts = gnu_body;
705 if (TREE_CODE (gnu_stmts) == BIND_EXPR)
706 gnu_stmts = BIND_EXPR_BODY (gnu_stmts);
707 if (!gnu_stmts || !STATEMENT_LIST_HEAD (gnu_stmts))
708 Set_Has_No_Elaboration_Code (info->gnat_node, 1);
709 else
711 begin_subprog_body (info->elab_proc);
712 end_subprog_body (gnu_body);
713 rest_of_subprog_body_compilation (info->elab_proc);
717 /* Destroy ourselves. */
718 destroy_gnat_decl ();
719 destroy_gnat_utils ();
721 /* We cannot track the location of errors past this point. */
722 error_gnat_node = Empty;
725 /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given
726 CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */
728 static tree
729 build_raise_check (int check, enum exception_info_kind kind)
731 tree result, ftype;
732 const char pfx[] = "__gnat_rcheck_";
734 strcpy (Name_Buffer, pfx);
735 Name_Len = sizeof (pfx) - 1;
736 Get_RT_Exception_Name (check);
738 if (kind == exception_simple)
740 Name_Buffer[Name_Len] = 0;
741 ftype
742 = build_function_type_list (void_type_node,
743 build_pointer_type
744 (unsigned_char_type_node),
745 integer_type_node, NULL_TREE);
747 else
749 tree t = (kind == exception_column ? NULL_TREE : integer_type_node);
751 strcpy (Name_Buffer + Name_Len, "_ext");
752 Name_Buffer[Name_Len + 4] = 0;
753 ftype
754 = build_function_type_list (void_type_node,
755 build_pointer_type
756 (unsigned_char_type_node),
757 integer_type_node, integer_type_node,
758 t, t, NULL_TREE);
761 result
762 = create_subprog_decl (get_identifier (Name_Buffer),
763 NULL_TREE, ftype, NULL_TREE,
764 is_disabled, true, true, true, NULL, Empty);
766 /* Indicate that it never returns. */
767 TREE_THIS_VOLATILE (result) = 1;
768 TREE_SIDE_EFFECTS (result) = 1;
769 TREE_TYPE (result)
770 = build_qualified_type (TREE_TYPE (result), TYPE_QUAL_VOLATILE);
772 return result;
775 /* Return a positive value if an lvalue is required for GNAT_NODE, which is
776 an N_Attribute_Reference. */
778 static int
779 lvalue_required_for_attribute_p (Node_Id gnat_node)
781 switch (Get_Attribute_Id (Attribute_Name (gnat_node)))
783 case Attr_Pos:
784 case Attr_Val:
785 case Attr_Pred:
786 case Attr_Succ:
787 case Attr_First:
788 case Attr_Last:
789 case Attr_Range_Length:
790 case Attr_Length:
791 case Attr_Object_Size:
792 case Attr_Value_Size:
793 case Attr_Component_Size:
794 case Attr_Descriptor_Size:
795 case Attr_Max_Size_In_Storage_Elements:
796 case Attr_Min:
797 case Attr_Max:
798 case Attr_Null_Parameter:
799 case Attr_Passed_By_Reference:
800 case Attr_Mechanism_Code:
801 case Attr_Machine:
802 case Attr_Model:
803 return 0;
805 case Attr_Address:
806 case Attr_Access:
807 case Attr_Unchecked_Access:
808 case Attr_Unrestricted_Access:
809 case Attr_Code_Address:
810 case Attr_Pool_Address:
811 case Attr_Size:
812 case Attr_Alignment:
813 case Attr_Bit_Position:
814 case Attr_Position:
815 case Attr_First_Bit:
816 case Attr_Last_Bit:
817 case Attr_Bit:
818 case Attr_Asm_Input:
819 case Attr_Asm_Output:
820 default:
821 return 1;
825 /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE
826 is the type that will be used for GNAT_NODE in the translated GNU tree.
827 CONSTANT indicates whether the underlying object represented by GNAT_NODE
828 is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates
829 whether its value is the address of a constant and ALIASED whether it is
830 aliased. If it isn't, ADDRESS_OF_CONSTANT and ALIASED are ignored.
832 The function climbs up the GNAT tree starting from the node and returns 1
833 upon encountering a node that effectively requires an lvalue downstream.
834 It returns int instead of bool to facilitate usage in non-purely binary
835 logic contexts. */
837 static int
838 lvalue_required_p (Node_Id gnat_node, tree gnu_type, bool constant,
839 bool address_of_constant, bool aliased)
841 Node_Id gnat_parent = Parent (gnat_node), gnat_temp;
843 switch (Nkind (gnat_parent))
845 case N_Reference:
846 return 1;
848 case N_Attribute_Reference:
849 return lvalue_required_for_attribute_p (gnat_parent);
851 case N_Parameter_Association:
852 case N_Function_Call:
853 case N_Procedure_Call_Statement:
854 /* If the parameter is by reference, an lvalue is required. */
855 return (!constant
856 || must_pass_by_ref (gnu_type)
857 || default_pass_by_ref (gnu_type));
859 case N_Indexed_Component:
860 /* Only the array expression can require an lvalue. */
861 if (Prefix (gnat_parent) != gnat_node)
862 return 0;
864 /* ??? Consider that referencing an indexed component with a
865 non-constant index forces the whole aggregate to memory.
866 Note that N_Integer_Literal is conservative, any static
867 expression in the RM sense could probably be accepted. */
868 for (gnat_temp = First (Expressions (gnat_parent));
869 Present (gnat_temp);
870 gnat_temp = Next (gnat_temp))
871 if (Nkind (gnat_temp) != N_Integer_Literal)
872 return 1;
874 /* ... fall through ... */
876 case N_Slice:
877 /* Only the array expression can require an lvalue. */
878 if (Prefix (gnat_parent) != gnat_node)
879 return 0;
881 aliased |= Has_Aliased_Components (Etype (gnat_node));
882 return lvalue_required_p (gnat_parent, gnu_type, constant,
883 address_of_constant, aliased);
885 case N_Selected_Component:
886 aliased |= Is_Aliased (Entity (Selector_Name (gnat_parent)));
887 return lvalue_required_p (gnat_parent, gnu_type, constant,
888 address_of_constant, aliased);
890 case N_Object_Renaming_Declaration:
891 /* We need to preserve addresses through a renaming. */
892 return 1;
894 case N_Object_Declaration:
895 /* We cannot use a constructor if this is an atomic object because
896 the actual assignment might end up being done component-wise. */
897 return (!constant
898 ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
899 && Is_Atomic (Defining_Entity (gnat_parent)))
900 /* We don't use a constructor if this is a class-wide object
901 because the effective type of the object is the equivalent
902 type of the class-wide subtype and it smashes most of the
903 data into an array of bytes to which we cannot convert. */
904 || Ekind ((Etype (Defining_Entity (gnat_parent))))
905 == E_Class_Wide_Subtype);
907 case N_Assignment_Statement:
908 /* We cannot use a constructor if the LHS is an atomic object because
909 the actual assignment might end up being done component-wise. */
910 return (!constant
911 || Name (gnat_parent) == gnat_node
912 || (Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
913 && Is_Atomic (Entity (Name (gnat_parent)))));
915 case N_Unchecked_Type_Conversion:
916 if (!constant)
917 return 1;
919 /* ... fall through ... */
921 case N_Type_Conversion:
922 case N_Qualified_Expression:
923 /* We must look through all conversions because we may need to bypass
924 an intermediate conversion that is meant to be purely formal. */
925 return lvalue_required_p (gnat_parent,
926 get_unpadded_type (Etype (gnat_parent)),
927 constant, address_of_constant, aliased);
929 case N_Allocator:
930 /* We should only reach here through the N_Qualified_Expression case.
931 Force an lvalue for composite types since a block-copy to the newly
932 allocated area of memory is made. */
933 return Is_Composite_Type (Underlying_Type (Etype (gnat_node)));
935 case N_Explicit_Dereference:
936 /* We look through dereferences for address of constant because we need
937 to handle the special cases listed above. */
938 if (constant && address_of_constant)
939 return lvalue_required_p (gnat_parent,
940 get_unpadded_type (Etype (gnat_parent)),
941 true, false, true);
943 /* ... fall through ... */
945 default:
946 return 0;
949 gcc_unreachable ();
952 /* Return true if T is a constant DECL node that can be safely replaced
953 by its initializer. */
955 static bool
956 constant_decl_with_initializer_p (tree t)
958 if (!TREE_CONSTANT (t) || !DECL_P (t) || !DECL_INITIAL (t))
959 return false;
961 /* Return false for aggregate types that contain a placeholder since
962 their initializers cannot be manipulated easily. */
963 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
964 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (t))
965 && type_contains_placeholder_p (TREE_TYPE (t)))
966 return false;
968 return true;
971 /* Return an expression equivalent to EXP but where constant DECL nodes
972 have been replaced by their initializer. */
974 static tree
975 fold_constant_decl_in_expr (tree exp)
977 enum tree_code code = TREE_CODE (exp);
978 tree op0;
980 switch (code)
982 case CONST_DECL:
983 case VAR_DECL:
984 if (!constant_decl_with_initializer_p (exp))
985 return exp;
987 return DECL_INITIAL (exp);
989 case BIT_FIELD_REF:
990 case COMPONENT_REF:
991 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
992 if (op0 == TREE_OPERAND (exp, 0))
993 return exp;
995 return fold_build3 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1),
996 TREE_OPERAND (exp, 2));
998 case ARRAY_REF:
999 case ARRAY_RANGE_REF:
1000 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
1001 if (op0 == TREE_OPERAND (exp, 0))
1002 return exp;
1004 return fold (build4 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1),
1005 TREE_OPERAND (exp, 2), TREE_OPERAND (exp, 3)));
1007 case VIEW_CONVERT_EXPR:
1008 case REALPART_EXPR:
1009 case IMAGPART_EXPR:
1010 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
1011 if (op0 == TREE_OPERAND (exp, 0))
1012 return exp;
1014 return fold_build1 (code, TREE_TYPE (exp), op0);
1016 default:
1017 return exp;
1020 gcc_unreachable ();
1023 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
1024 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer
1025 to where we should place the result type. */
1027 static tree
1028 Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
1030 Node_Id gnat_temp, gnat_temp_type;
1031 tree gnu_result, gnu_result_type;
1033 /* Whether we should require an lvalue for GNAT_NODE. Needed in
1034 specific circumstances only, so evaluated lazily. < 0 means
1035 unknown, > 0 means known true, 0 means known false. */
1036 int require_lvalue = -1;
1038 /* If GNAT_NODE is a constant, whether we should use the initialization
1039 value instead of the constant entity, typically for scalars with an
1040 address clause when the parent doesn't require an lvalue. */
1041 bool use_constant_initializer = false;
1043 /* If the Etype of this node does not equal the Etype of the Entity,
1044 something is wrong with the entity map, probably in generic
1045 instantiation. However, this does not apply to types. Since we sometime
1046 have strange Ekind's, just do this test for objects. Also, if the Etype of
1047 the Entity is private, the Etype of the N_Identifier is allowed to be the
1048 full type and also we consider a packed array type to be the same as the
1049 original type. Similarly, a class-wide type is equivalent to a subtype of
1050 itself. Finally, if the types are Itypes, one may be a copy of the other,
1051 which is also legal. */
1052 gnat_temp = (Nkind (gnat_node) == N_Defining_Identifier
1053 ? gnat_node : Entity (gnat_node));
1054 gnat_temp_type = Etype (gnat_temp);
1056 gcc_assert (Etype (gnat_node) == gnat_temp_type
1057 || (Is_Packed (gnat_temp_type)
1058 && (Etype (gnat_node)
1059 == Packed_Array_Impl_Type (gnat_temp_type)))
1060 || (Is_Class_Wide_Type (Etype (gnat_node)))
1061 || (IN (Ekind (gnat_temp_type), Private_Kind)
1062 && Present (Full_View (gnat_temp_type))
1063 && ((Etype (gnat_node) == Full_View (gnat_temp_type))
1064 || (Is_Packed (Full_View (gnat_temp_type))
1065 && (Etype (gnat_node)
1066 == Packed_Array_Impl_Type
1067 (Full_View (gnat_temp_type))))))
1068 || (Is_Itype (Etype (gnat_node)) && Is_Itype (gnat_temp_type))
1069 || !(Ekind (gnat_temp) == E_Variable
1070 || Ekind (gnat_temp) == E_Component
1071 || Ekind (gnat_temp) == E_Constant
1072 || Ekind (gnat_temp) == E_Loop_Parameter
1073 || IN (Ekind (gnat_temp), Formal_Kind)));
1075 /* If this is a reference to a deferred constant whose partial view is an
1076 unconstrained private type, the proper type is on the full view of the
1077 constant, not on the full view of the type, which may be unconstrained.
1079 This may be a reference to a type, for example in the prefix of the
1080 attribute Position, generated for dispatching code (see Make_DT in
1081 exp_disp,adb). In that case we need the type itself, not is parent,
1082 in particular if it is a derived type */
1083 if (Ekind (gnat_temp) == E_Constant
1084 && Is_Private_Type (gnat_temp_type)
1085 && (Has_Unknown_Discriminants (gnat_temp_type)
1086 || (Present (Full_View (gnat_temp_type))
1087 && Has_Discriminants (Full_View (gnat_temp_type))))
1088 && Present (Full_View (gnat_temp)))
1090 gnat_temp = Full_View (gnat_temp);
1091 gnat_temp_type = Etype (gnat_temp);
1093 else
1095 /* We want to use the Actual_Subtype if it has already been elaborated,
1096 otherwise the Etype. Avoid using Actual_Subtype for packed arrays to
1097 simplify things. */
1098 if ((Ekind (gnat_temp) == E_Constant
1099 || Ekind (gnat_temp) == E_Variable || Is_Formal (gnat_temp))
1100 && !(Is_Array_Type (Etype (gnat_temp))
1101 && Present (Packed_Array_Impl_Type (Etype (gnat_temp))))
1102 && Present (Actual_Subtype (gnat_temp))
1103 && present_gnu_tree (Actual_Subtype (gnat_temp)))
1104 gnat_temp_type = Actual_Subtype (gnat_temp);
1105 else
1106 gnat_temp_type = Etype (gnat_node);
1109 /* Expand the type of this identifier first, in case it is an enumeral
1110 literal, which only get made when the type is expanded. There is no
1111 order-of-elaboration issue here. */
1112 gnu_result_type = get_unpadded_type (gnat_temp_type);
1114 /* If this is a non-imported elementary constant with an address clause,
1115 retrieve the value instead of a pointer to be dereferenced unless
1116 an lvalue is required. This is generally more efficient and actually
1117 required if this is a static expression because it might be used
1118 in a context where a dereference is inappropriate, such as a case
1119 statement alternative or a record discriminant. There is no possible
1120 volatile-ness short-circuit here since Volatile constants must be
1121 imported per C.6. */
1122 if (Ekind (gnat_temp) == E_Constant
1123 && Is_Elementary_Type (gnat_temp_type)
1124 && !Is_Imported (gnat_temp)
1125 && Present (Address_Clause (gnat_temp)))
1127 require_lvalue = lvalue_required_p (gnat_node, gnu_result_type, true,
1128 false, Is_Aliased (gnat_temp));
1129 use_constant_initializer = !require_lvalue;
1132 if (use_constant_initializer)
1134 /* If this is a deferred constant, the initializer is attached to
1135 the full view. */
1136 if (Present (Full_View (gnat_temp)))
1137 gnat_temp = Full_View (gnat_temp);
1139 gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_temp)));
1141 else
1142 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0);
1144 /* Some objects (such as parameters passed by reference, globals of
1145 variable size, and renamed objects) actually represent the address
1146 of the object. In that case, we must do the dereference. Likewise,
1147 deal with parameters to foreign convention subprograms. */
1148 if (DECL_P (gnu_result)
1149 && (DECL_BY_REF_P (gnu_result)
1150 || (TREE_CODE (gnu_result) == PARM_DECL
1151 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
1153 const bool read_only = DECL_POINTS_TO_READONLY_P (gnu_result);
1155 /* If it's a PARM_DECL to foreign convention subprogram, convert it. */
1156 if (TREE_CODE (gnu_result) == PARM_DECL
1157 && DECL_BY_COMPONENT_PTR_P (gnu_result))
1158 gnu_result
1159 = convert (build_pointer_type (gnu_result_type), gnu_result);
1161 /* If it's a CONST_DECL, return the underlying constant like below. */
1162 else if (TREE_CODE (gnu_result) == CONST_DECL
1163 && !(DECL_CONST_ADDRESS_P (gnu_result)
1164 && lvalue_required_p (gnat_node, gnu_result_type, true,
1165 true, false)))
1166 gnu_result = DECL_INITIAL (gnu_result);
1168 /* If it's a renaming pointer and, either the renamed object is constant
1169 or we are at the right binding level, we can reference the renamed
1170 object directly, since it is constant or has been protected against
1171 multiple evaluations. */
1172 if (TREE_CODE (gnu_result) == VAR_DECL
1173 && !DECL_LOOP_PARM_P (gnu_result)
1174 && DECL_RENAMED_OBJECT (gnu_result)
1175 && (TREE_CONSTANT (DECL_RENAMED_OBJECT (gnu_result))
1176 || !DECL_RENAMING_GLOBAL_P (gnu_result)
1177 || global_bindings_p ()))
1178 gnu_result = DECL_RENAMED_OBJECT (gnu_result);
1180 /* Otherwise, do the final dereference. */
1181 else
1183 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
1185 if ((TREE_CODE (gnu_result) == INDIRECT_REF
1186 || TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
1187 && No (Address_Clause (gnat_temp)))
1188 TREE_THIS_NOTRAP (gnu_result) = 1;
1190 if (read_only)
1191 TREE_READONLY (gnu_result) = 1;
1195 /* If we have a constant declaration and its initializer, try to return the
1196 latter to avoid the need to call fold in lots of places and the need for
1197 elaboration code if this identifier is used as an initializer itself. */
1198 if (constant_decl_with_initializer_p (gnu_result))
1200 bool constant_only = (TREE_CODE (gnu_result) == CONST_DECL
1201 && !DECL_CONST_CORRESPONDING_VAR (gnu_result));
1202 bool address_of_constant = (TREE_CODE (gnu_result) == CONST_DECL
1203 && DECL_CONST_ADDRESS_P (gnu_result));
1205 /* If there is a (corresponding) variable or this is the address of a
1206 constant, we only want to return the initializer if an lvalue isn't
1207 required. Evaluate this now if we have not already done so. */
1208 if ((!constant_only || address_of_constant) && require_lvalue < 0)
1209 require_lvalue
1210 = lvalue_required_p (gnat_node, gnu_result_type, true,
1211 address_of_constant, Is_Aliased (gnat_temp));
1213 /* Finally retrieve the initializer if this is deemed valid. */
1214 if ((constant_only && !address_of_constant) || !require_lvalue)
1215 gnu_result = DECL_INITIAL (gnu_result);
1218 /* But for a constant renaming we couldn't do that incrementally for its
1219 definition because of the need to return an lvalue so, if the present
1220 context doesn't itself require an lvalue, we try again here. */
1221 else if (Ekind (gnat_temp) == E_Constant
1222 && Is_Elementary_Type (gnat_temp_type)
1223 && Present (Renamed_Object (gnat_temp)))
1225 if (require_lvalue < 0)
1226 require_lvalue
1227 = lvalue_required_p (gnat_node, gnu_result_type, true, false,
1228 Is_Aliased (gnat_temp));
1229 if (!require_lvalue)
1230 gnu_result = fold_constant_decl_in_expr (gnu_result);
1233 /* The GNAT tree has the type of a function set to its result type, so we
1234 adjust here. Also use the type of the result if the Etype is a subtype
1235 that is nominally unconstrained. Likewise if this is a deferred constant
1236 of a discriminated type whose full view can be elaborated statically, to
1237 avoid problematic conversions to the nominal subtype. But remove any
1238 padding from the resulting type. */
1239 if (TREE_CODE (TREE_TYPE (gnu_result)) == FUNCTION_TYPE
1240 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type)
1241 || (Ekind (gnat_temp) == E_Constant
1242 && Present (Full_View (gnat_temp))
1243 && Has_Discriminants (gnat_temp_type)
1244 && TREE_CODE (gnu_result) == CONSTRUCTOR))
1246 gnu_result_type = TREE_TYPE (gnu_result);
1247 if (TYPE_IS_PADDING_P (gnu_result_type))
1248 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
1251 *gnu_result_type_p = gnu_result_type;
1253 return gnu_result;
1256 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
1257 any statements we generate. */
1259 static tree
1260 Pragma_to_gnu (Node_Id gnat_node)
1262 tree gnu_result = alloc_stmt_list ();
1263 unsigned char pragma_id;
1264 Node_Id gnat_temp;
1266 /* Do nothing if we are just annotating types and check for (and ignore)
1267 unrecognized pragmas. */
1268 if (type_annotate_only
1269 || !Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node))))
1270 return gnu_result;
1272 pragma_id = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
1273 switch (pragma_id)
1275 case Pragma_Inspection_Point:
1276 /* Do nothing at top level: all such variables are already viewable. */
1277 if (global_bindings_p ())
1278 break;
1280 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1281 Present (gnat_temp);
1282 gnat_temp = Next (gnat_temp))
1284 Node_Id gnat_expr = Expression (gnat_temp);
1285 tree gnu_expr = gnat_to_gnu (gnat_expr);
1286 int use_address;
1287 machine_mode mode;
1288 tree asm_constraint = NULL_TREE;
1289 #ifdef ASM_COMMENT_START
1290 char *comment;
1291 #endif
1293 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
1294 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1296 /* Use the value only if it fits into a normal register,
1297 otherwise use the address. */
1298 mode = TYPE_MODE (TREE_TYPE (gnu_expr));
1299 use_address = ((GET_MODE_CLASS (mode) != MODE_INT
1300 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1301 || GET_MODE_SIZE (mode) > UNITS_PER_WORD);
1303 if (use_address)
1304 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
1306 #ifdef ASM_COMMENT_START
1307 comment = concat (ASM_COMMENT_START,
1308 " inspection point: ",
1309 Get_Name_String (Chars (gnat_expr)),
1310 use_address ? " address" : "",
1311 " is in %0",
1312 NULL);
1313 asm_constraint = build_string (strlen (comment), comment);
1314 free (comment);
1315 #endif
1316 gnu_expr = build5 (ASM_EXPR, void_type_node,
1317 asm_constraint,
1318 NULL_TREE,
1319 tree_cons
1320 (build_tree_list (NULL_TREE,
1321 build_string (1, "g")),
1322 gnu_expr, NULL_TREE),
1323 NULL_TREE, NULL_TREE);
1324 ASM_VOLATILE_P (gnu_expr) = 1;
1325 set_expr_location_from_node (gnu_expr, gnat_node);
1326 append_to_statement_list (gnu_expr, &gnu_result);
1328 break;
1330 case Pragma_Loop_Optimize:
1331 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1332 Present (gnat_temp);
1333 gnat_temp = Next (gnat_temp))
1335 tree gnu_loop_stmt = gnu_loop_stack->last ()->stmt;
1337 switch (Chars (Expression (gnat_temp)))
1339 case Name_Ivdep:
1340 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
1341 break;
1343 case Name_No_Unroll:
1344 LOOP_STMT_NO_UNROLL (gnu_loop_stmt) = 1;
1345 break;
1347 case Name_Unroll:
1348 LOOP_STMT_UNROLL (gnu_loop_stmt) = 1;
1349 break;
1351 case Name_No_Vector:
1352 LOOP_STMT_NO_VECTOR (gnu_loop_stmt) = 1;
1353 break;
1355 case Name_Vector:
1356 LOOP_STMT_VECTOR (gnu_loop_stmt) = 1;
1357 break;
1359 default:
1360 gcc_unreachable ();
1363 break;
1365 case Pragma_Optimize:
1366 switch (Chars (Expression
1367 (First (Pragma_Argument_Associations (gnat_node)))))
1369 case Name_Off:
1370 if (optimize)
1371 post_error ("must specify -O0?", gnat_node);
1372 break;
1374 case Name_Space:
1375 if (!optimize_size)
1376 post_error ("must specify -Os?", gnat_node);
1377 break;
1379 case Name_Time:
1380 if (!optimize)
1381 post_error ("insufficient -O value?", gnat_node);
1382 break;
1384 default:
1385 gcc_unreachable ();
1387 break;
1389 case Pragma_Reviewable:
1390 if (write_symbols == NO_DEBUG)
1391 post_error ("must specify -g?", gnat_node);
1392 break;
1394 case Pragma_Warning_As_Error:
1395 case Pragma_Warnings:
1397 Node_Id gnat_expr;
1398 /* Preserve the location of the pragma. */
1399 const location_t location = input_location;
1400 struct cl_option_handlers handlers;
1401 unsigned int option_index;
1402 diagnostic_t kind;
1403 bool imply;
1405 gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1407 /* This is the String form: pragma Warning{s|_As_Error}(String). */
1408 if (Nkind (Expression (gnat_temp)) == N_String_Literal)
1410 switch (pragma_id)
1412 case Pragma_Warning_As_Error:
1413 kind = DK_ERROR;
1414 imply = false;
1415 break;
1417 case Pragma_Warnings:
1418 kind = DK_WARNING;
1419 imply = true;
1420 break;
1422 default:
1423 gcc_unreachable ();
1426 gnat_expr = Expression (gnat_temp);
1429 /* This is the On/Off form: pragma Warnings (On | Off [,String]). */
1430 else if (Nkind (Expression (gnat_temp)) == N_Identifier)
1432 switch (Chars (Expression (gnat_temp)))
1434 case Name_Off:
1435 kind = DK_IGNORED;
1436 break;
1438 case Name_On:
1439 kind = DK_WARNING;
1440 break;
1442 default:
1443 gcc_unreachable ();
1446 /* Deal with optional pattern (but ignore Reason => "..."). */
1447 if (Present (Next (gnat_temp)) && No (Chars (Next (gnat_temp))))
1449 /* pragma Warnings (On | Off, Name) is handled differently. */
1450 if (Nkind (Expression (Next (gnat_temp))) != N_String_Literal)
1451 break;
1453 gnat_expr = Expression (Next (gnat_temp));
1455 else
1456 gnat_expr = Empty;
1458 imply = false;
1461 else
1462 gcc_unreachable ();
1464 /* This is the same implementation as in the C family of compilers. */
1465 if (Present (gnat_expr))
1467 tree gnu_expr = gnat_to_gnu (gnat_expr);
1468 const char *opt_string = TREE_STRING_POINTER (gnu_expr);
1469 const int len = TREE_STRING_LENGTH (gnu_expr);
1470 if (len < 3 || opt_string[0] != '-' || opt_string[1] != 'W')
1471 break;
1472 for (option_index = 0;
1473 option_index < cl_options_count;
1474 option_index++)
1475 if (strcmp (cl_options[option_index].opt_text, opt_string) == 0)
1476 break;
1477 if (option_index == cl_options_count)
1479 post_error ("unknown -W switch", gnat_node);
1480 break;
1483 else
1484 option_index = 0;
1486 set_default_handlers (&handlers);
1487 control_warning_option (option_index, (int) kind, imply, location,
1488 CL_Ada, &handlers, &global_options,
1489 &global_options_set, global_dc);
1491 break;
1493 default:
1494 break;
1497 return gnu_result;
1501 /* Check the inlining status of nested function FNDECL in the current context.
1503 If a non-inline nested function is referenced from an inline external
1504 function, we cannot honor both requests at the same time without cloning
1505 the nested function in the current unit since it is private to its unit.
1506 We could inline it as well but it's probably better to err on the side
1507 of too little inlining.
1509 This must be invoked only on nested functions present in the source code
1510 and not on nested functions generated by the compiler, e.g. finalizers,
1511 because they are not marked inline and we don't want them to block the
1512 inlining of the parent function. */
1514 static void
1515 check_inlining_for_nested_subprog (tree fndecl)
1517 if (!DECL_DECLARED_INLINE_P (fndecl)
1518 && current_function_decl
1519 && DECL_EXTERNAL (current_function_decl)
1520 && DECL_DECLARED_INLINE_P (current_function_decl))
1522 const location_t loc1 = DECL_SOURCE_LOCATION (fndecl);
1523 const location_t loc2 = DECL_SOURCE_LOCATION (current_function_decl);
1525 if (lookup_attribute ("always_inline",
1526 DECL_ATTRIBUTES (current_function_decl)))
1528 error_at (loc1, "subprogram %q+F not marked Inline_Always", fndecl);
1529 error_at (loc2, "parent subprogram cannot be inlined");
1531 else
1533 warning_at (loc1, OPT_Winline, "subprogram %q+F not marked Inline",
1534 fndecl);
1535 warning_at (loc2, OPT_Winline, "parent subprogram cannot be inlined");
1538 DECL_DECLARED_INLINE_P (current_function_decl) = 0;
1539 DECL_UNINLINABLE (current_function_decl) = 1;
1543 /* Return an expression for the length of TYPE, an integral type, computed in
1544 RESULT_TYPE, another integral type.
1546 We used to compute the length as MAX (hb - lb + 1, 0) which could overflow
1547 when lb == TYPE'First. We now compute it as (hb >= lb) ? hb - lb + 1 : 0
1548 which would only overflow in much rarer cases, for extremely large arrays
1549 we expect never to encounter in practice. Besides, the former computation
1550 required the use of potentially constraining signed arithmetics while the
1551 latter does not. Note that the comparison must be done in the original
1552 base index type in order to avoid any overflow during the conversion. */
1554 static tree
1555 get_type_length (tree type, tree result_type)
1557 tree comp_type = get_base_type (result_type);
1558 tree base_type = get_base_type (type);
1559 tree lb = convert (base_type, TYPE_MIN_VALUE (type));
1560 tree hb = convert (base_type, TYPE_MAX_VALUE (type));
1561 tree length
1562 = build_binary_op (PLUS_EXPR, comp_type,
1563 build_binary_op (MINUS_EXPR, comp_type,
1564 convert (comp_type, hb),
1565 convert (comp_type, lb)),
1566 convert (comp_type, integer_one_node));
1567 length
1568 = build_cond_expr (result_type,
1569 build_binary_op (GE_EXPR, boolean_type_node, hb, lb),
1570 convert (result_type, length),
1571 convert (result_type, integer_zero_node));
1572 return length;
1575 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node,
1576 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
1577 where we should place the result type. ATTRIBUTE is the attribute ID. */
1579 static tree
1580 Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
1582 const Node_Id gnat_prefix = Prefix (gnat_node);
1583 tree gnu_prefix, gnu_type, gnu_expr;
1584 tree gnu_result_type, gnu_result = error_mark_node;
1585 bool prefix_unused = false;
1587 /* ??? If this is an access attribute for a public subprogram to be used in
1588 a dispatch table, do not translate its type as it's useless in this case
1589 and the parameter types might be incomplete types coming from a limited
1590 context in Ada 2012 (AI05-0151). */
1591 if (Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
1592 && Is_Dispatch_Table_Entity (Etype (gnat_node))
1593 && Nkind (gnat_prefix) == N_Identifier
1594 && Is_Subprogram (Entity (gnat_prefix))
1595 && Is_Public (Entity (gnat_prefix))
1596 && !present_gnu_tree (Entity (gnat_prefix)))
1597 gnu_prefix = get_minimal_subprog_decl (Entity (gnat_prefix));
1598 else
1599 gnu_prefix = gnat_to_gnu (gnat_prefix);
1600 gnu_type = TREE_TYPE (gnu_prefix);
1602 /* If the input is a NULL_EXPR, make a new one. */
1603 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
1605 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1606 *gnu_result_type_p = gnu_result_type;
1607 return build1 (NULL_EXPR, gnu_result_type, TREE_OPERAND (gnu_prefix, 0));
1610 switch (attribute)
1612 case Attr_Pos:
1613 case Attr_Val:
1614 /* These are just conversions since representation clauses for
1615 enumeration types are handled in the front-end. */
1617 bool checkp = Do_Range_Check (First (Expressions (gnat_node)));
1618 gnu_result = gnat_to_gnu (First (Expressions (gnat_node)));
1619 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1620 gnu_result = convert_with_check (Etype (gnat_node), gnu_result,
1621 checkp, checkp, true, gnat_node);
1623 break;
1625 case Attr_Pred:
1626 case Attr_Succ:
1627 /* These just add or subtract the constant 1 since representation
1628 clauses for enumeration types are handled in the front-end. */
1629 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1630 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1632 if (Do_Range_Check (First (Expressions (gnat_node))))
1634 gnu_expr = gnat_protect_expr (gnu_expr);
1635 gnu_expr
1636 = emit_check
1637 (build_binary_op (EQ_EXPR, boolean_type_node,
1638 gnu_expr,
1639 attribute == Attr_Pred
1640 ? TYPE_MIN_VALUE (gnu_result_type)
1641 : TYPE_MAX_VALUE (gnu_result_type)),
1642 gnu_expr, CE_Range_Check_Failed, gnat_node);
1645 gnu_result
1646 = build_binary_op (attribute == Attr_Pred ? MINUS_EXPR : PLUS_EXPR,
1647 gnu_result_type, gnu_expr,
1648 convert (gnu_result_type, integer_one_node));
1649 break;
1651 case Attr_Address:
1652 case Attr_Unrestricted_Access:
1653 /* Conversions don't change addresses but can cause us to miss the
1654 COMPONENT_REF case below, so strip them off. */
1655 gnu_prefix = remove_conversions (gnu_prefix,
1656 !Must_Be_Byte_Aligned (gnat_node));
1658 /* If we are taking 'Address of an unconstrained object, this is the
1659 pointer to the underlying array. */
1660 if (attribute == Attr_Address)
1661 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1663 /* If we are building a static dispatch table, we have to honor
1664 TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible
1665 with the C++ ABI. We do it in the non-static case as well,
1666 see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */
1667 else if (TARGET_VTABLE_USES_DESCRIPTORS
1668 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
1670 tree gnu_field, t;
1671 /* Descriptors can only be built here for top-level functions. */
1672 bool build_descriptor = (global_bindings_p () != 0);
1673 int i;
1674 vec<constructor_elt, va_gc> *gnu_vec = NULL;
1675 constructor_elt *elt;
1677 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1679 /* If we're not going to build the descriptor, we have to retrieve
1680 the one which will be built by the linker (or by the compiler
1681 later if a static chain is requested). */
1682 if (!build_descriptor)
1684 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_prefix);
1685 gnu_result = fold_convert (build_pointer_type (gnu_result_type),
1686 gnu_result);
1687 gnu_result = build1 (INDIRECT_REF, gnu_result_type, gnu_result);
1690 vec_safe_grow (gnu_vec, TARGET_VTABLE_USES_DESCRIPTORS);
1691 elt = (gnu_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
1692 for (gnu_field = TYPE_FIELDS (gnu_result_type), i = 0;
1693 i < TARGET_VTABLE_USES_DESCRIPTORS;
1694 gnu_field = DECL_CHAIN (gnu_field), i++)
1696 if (build_descriptor)
1698 t = build2 (FDESC_EXPR, TREE_TYPE (gnu_field), gnu_prefix,
1699 build_int_cst (NULL_TREE, i));
1700 TREE_CONSTANT (t) = 1;
1702 else
1703 t = build3 (COMPONENT_REF, ptr_void_ftype, gnu_result,
1704 gnu_field, NULL_TREE);
1706 elt->index = gnu_field;
1707 elt->value = t;
1708 elt--;
1711 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
1712 break;
1715 /* ... fall through ... */
1717 case Attr_Access:
1718 case Attr_Unchecked_Access:
1719 case Attr_Code_Address:
1720 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1721 gnu_result
1722 = build_unary_op (((attribute == Attr_Address
1723 || attribute == Attr_Unrestricted_Access)
1724 && !Must_Be_Byte_Aligned (gnat_node))
1725 ? ATTR_ADDR_EXPR : ADDR_EXPR,
1726 gnu_result_type, gnu_prefix);
1728 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
1729 don't try to build a trampoline. */
1730 if (attribute == Attr_Code_Address)
1732 gnu_expr = remove_conversions (gnu_result, false);
1734 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
1735 TREE_NO_TRAMPOLINE (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
1738 /* For 'Access, issue an error message if the prefix is a C++ method
1739 since it can use a special calling convention on some platforms,
1740 which cannot be propagated to the access type. */
1741 else if (attribute == Attr_Access
1742 && Nkind (gnat_prefix) == N_Identifier
1743 && is_cplusplus_method (Entity (gnat_prefix)))
1744 post_error ("access to C++ constructor or member function not allowed",
1745 gnat_node);
1747 /* For other address attributes applied to a nested function,
1748 find an inner ADDR_EXPR and annotate it so that we can issue
1749 a useful warning with -Wtrampolines. */
1750 else if (TREE_CODE (TREE_TYPE (gnu_prefix)) == FUNCTION_TYPE)
1752 gnu_expr = remove_conversions (gnu_result, false);
1754 if (TREE_CODE (gnu_expr) == ADDR_EXPR
1755 && decl_function_context (TREE_OPERAND (gnu_expr, 0)))
1757 set_expr_location_from_node (gnu_expr, gnat_node);
1759 /* Also check the inlining status. */
1760 check_inlining_for_nested_subprog (TREE_OPERAND (gnu_expr, 0));
1762 /* Check that we're not violating the No_Implicit_Dynamic_Code
1763 restriction. Be conservative if we don't know anything
1764 about the trampoline strategy for the target. */
1765 Check_Implicit_Dynamic_Code_Allowed (gnat_node);
1768 break;
1770 case Attr_Pool_Address:
1772 tree gnu_ptr = gnu_prefix;
1773 tree gnu_obj_type;
1775 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1777 /* If this is fat pointer, the object must have been allocated with the
1778 template in front of the array. So compute the template address; do
1779 it by converting to a thin pointer. */
1780 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
1781 gnu_ptr
1782 = convert (build_pointer_type
1783 (TYPE_OBJECT_RECORD_TYPE
1784 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
1785 gnu_ptr);
1787 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
1789 /* If this is a thin pointer, the object must have been allocated with
1790 the template in front of the array. So compute the template address
1791 and return it. */
1792 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
1793 gnu_ptr
1794 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
1795 gnu_ptr,
1796 fold_build1 (NEGATE_EXPR, sizetype,
1797 byte_position
1798 (DECL_CHAIN
1799 TYPE_FIELDS ((gnu_obj_type)))));
1801 gnu_result = convert (gnu_result_type, gnu_ptr);
1803 break;
1805 case Attr_Size:
1806 case Attr_Object_Size:
1807 case Attr_Value_Size:
1808 case Attr_Max_Size_In_Storage_Elements:
1809 gnu_expr = gnu_prefix;
1811 /* Remove NOPs and conversions between original and packable version
1812 from GNU_EXPR, and conversions from GNU_PREFIX. We use GNU_EXPR
1813 to see if a COMPONENT_REF was involved. */
1814 while (TREE_CODE (gnu_expr) == NOP_EXPR
1815 || (TREE_CODE (gnu_expr) == VIEW_CONVERT_EXPR
1816 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1817 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
1818 == RECORD_TYPE
1819 && TYPE_NAME (TREE_TYPE (gnu_expr))
1820 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
1821 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1823 gnu_prefix = remove_conversions (gnu_prefix, true);
1824 prefix_unused = true;
1825 gnu_type = TREE_TYPE (gnu_prefix);
1827 /* Replace an unconstrained array type with the type of the underlying
1828 array. We can't do this with a call to maybe_unconstrained_array
1829 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
1830 use the record type that will be used to allocate the object and its
1831 template. */
1832 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1834 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
1835 if (attribute != Attr_Max_Size_In_Storage_Elements)
1836 gnu_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
1839 /* If we're looking for the size of a field, return the field size. */
1840 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1841 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
1843 /* Otherwise, if the prefix is an object, or if we are looking for
1844 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the
1845 GCC size of the type. We make an exception for padded objects,
1846 as we do not take into account alignment promotions for the size.
1847 This is in keeping with the object case of gnat_to_gnu_entity. */
1848 else if ((TREE_CODE (gnu_prefix) != TYPE_DECL
1849 && !(TYPE_IS_PADDING_P (gnu_type)
1850 && TREE_CODE (gnu_expr) == COMPONENT_REF))
1851 || attribute == Attr_Object_Size
1852 || attribute == Attr_Max_Size_In_Storage_Elements)
1854 /* If this is a dereference and we have a special dynamic constrained
1855 subtype on the prefix, use it to compute the size; otherwise, use
1856 the designated subtype. */
1857 if (Nkind (gnat_prefix) == N_Explicit_Dereference)
1859 Node_Id gnat_actual_subtype
1860 = Actual_Designated_Subtype (gnat_prefix);
1861 tree gnu_ptr_type
1862 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix)));
1864 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)
1865 && Present (gnat_actual_subtype))
1867 tree gnu_actual_obj_type
1868 = gnat_to_gnu_type (gnat_actual_subtype);
1869 gnu_type
1870 = build_unc_object_type_from_ptr (gnu_ptr_type,
1871 gnu_actual_obj_type,
1872 get_identifier ("SIZE"),
1873 false);
1877 gnu_result = TYPE_SIZE (gnu_type);
1880 /* Otherwise, the result is the RM size of the type. */
1881 else
1882 gnu_result = rm_size (gnu_type);
1884 /* Deal with a self-referential size by returning the maximum size for
1885 a type and by qualifying the size with the object otherwise. */
1886 if (CONTAINS_PLACEHOLDER_P (gnu_result))
1888 if (TREE_CODE (gnu_prefix) == TYPE_DECL)
1889 gnu_result = max_size (gnu_result, true);
1890 else
1891 gnu_result = substitute_placeholder_in_expr (gnu_result, gnu_expr);
1894 /* If the type contains a template, subtract its size. */
1895 if (TREE_CODE (gnu_type) == RECORD_TYPE
1896 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1897 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1898 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1900 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
1901 if (attribute == Attr_Max_Size_In_Storage_Elements)
1902 gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node);
1904 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1905 break;
1907 case Attr_Alignment:
1909 unsigned int align;
1911 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1912 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
1913 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1915 gnu_type = TREE_TYPE (gnu_prefix);
1916 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1917 prefix_unused = true;
1919 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1920 align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT;
1921 else
1923 Entity_Id gnat_type = Etype (gnat_prefix);
1924 unsigned int double_align;
1925 bool is_capped_double, align_clause;
1927 /* If the default alignment of "double" or larger scalar types is
1928 specifically capped and there is an alignment clause neither
1929 on the type nor on the prefix itself, return the cap. */
1930 if ((double_align = double_float_alignment) > 0)
1931 is_capped_double
1932 = is_double_float_or_array (gnat_type, &align_clause);
1933 else if ((double_align = double_scalar_alignment) > 0)
1934 is_capped_double
1935 = is_double_scalar_or_array (gnat_type, &align_clause);
1936 else
1937 is_capped_double = align_clause = false;
1939 if (is_capped_double
1940 && Nkind (gnat_prefix) == N_Identifier
1941 && Present (Alignment_Clause (Entity (gnat_prefix))))
1942 align_clause = true;
1944 if (is_capped_double && !align_clause)
1945 align = double_align;
1946 else
1947 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
1950 gnu_result = size_int (align);
1952 break;
1954 case Attr_First:
1955 case Attr_Last:
1956 case Attr_Range_Length:
1957 prefix_unused = true;
1959 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
1961 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1963 if (attribute == Attr_First)
1964 gnu_result = TYPE_MIN_VALUE (gnu_type);
1965 else if (attribute == Attr_Last)
1966 gnu_result = TYPE_MAX_VALUE (gnu_type);
1967 else
1968 gnu_result = get_type_length (gnu_type, gnu_result_type);
1969 break;
1972 /* ... fall through ... */
1974 case Attr_Length:
1976 int Dimension = (Present (Expressions (gnat_node))
1977 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
1978 : 1), i;
1979 struct parm_attr_d *pa = NULL;
1980 Entity_Id gnat_param = Empty;
1981 bool unconstrained_ptr_deref = false;
1983 /* Make sure any implicit dereference gets done. */
1984 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1985 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1987 /* We treat unconstrained array In parameters specially. We also note
1988 whether we are dereferencing a pointer to unconstrained array. */
1989 if (!Is_Constrained (Etype (gnat_prefix)))
1990 switch (Nkind (gnat_prefix))
1992 case N_Identifier:
1993 /* This is the direct case. */
1994 if (Ekind (Entity (gnat_prefix)) == E_In_Parameter)
1995 gnat_param = Entity (gnat_prefix);
1996 break;
1998 case N_Explicit_Dereference:
1999 /* This is the indirect case. Note that we need to be sure that
2000 the access value cannot be null as we'll hoist the load. */
2001 if (Nkind (Prefix (gnat_prefix)) == N_Identifier
2002 && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter)
2004 if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix))))
2005 gnat_param = Entity (Prefix (gnat_prefix));
2007 else
2008 unconstrained_ptr_deref = true;
2009 break;
2011 default:
2012 break;
2015 /* If the prefix is the view conversion of a constrained array to an
2016 unconstrained form, we retrieve the constrained array because we
2017 might not be able to substitute the PLACEHOLDER_EXPR coming from
2018 the conversion. This can occur with the 'Old attribute applied
2019 to a parameter with an unconstrained type, which gets rewritten
2020 into a constrained local variable very late in the game. */
2021 if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR
2022 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix)))
2023 && !CONTAINS_PLACEHOLDER_P
2024 (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
2025 gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0));
2026 else
2027 gnu_type = TREE_TYPE (gnu_prefix);
2029 prefix_unused = true;
2030 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2032 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
2034 int ndim;
2035 tree gnu_type_temp;
2037 for (ndim = 1, gnu_type_temp = gnu_type;
2038 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
2039 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
2040 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
2043 Dimension = ndim + 1 - Dimension;
2046 for (i = 1; i < Dimension; i++)
2047 gnu_type = TREE_TYPE (gnu_type);
2049 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2051 /* When not optimizing, look up the slot associated with the parameter
2052 and the dimension in the cache and create a new one on failure.
2053 Don't do this when the actual subtype needs debug info (this happens
2054 with -gnatD): in elaborate_expression_1, we create variables that
2055 hold the bounds, so caching attributes isn't very interesting and
2056 causes dependency issues between these variables and cached
2057 expressions. */
2058 if (!optimize
2059 && Present (gnat_param)
2060 && !(Present (Actual_Subtype (gnat_param))
2061 && Needs_Debug_Info (Actual_Subtype (gnat_param))))
2063 FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa)
2064 if (pa->id == gnat_param && pa->dim == Dimension)
2065 break;
2067 if (!pa)
2069 pa = ggc_cleared_alloc<parm_attr_d> ();
2070 pa->id = gnat_param;
2071 pa->dim = Dimension;
2072 vec_safe_push (f_parm_attr_cache, pa);
2076 /* Return the cached expression or build a new one. */
2077 if (attribute == Attr_First)
2079 if (pa && pa->first)
2081 gnu_result = pa->first;
2082 break;
2085 gnu_result
2086 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2089 else if (attribute == Attr_Last)
2091 if (pa && pa->last)
2093 gnu_result = pa->last;
2094 break;
2097 gnu_result
2098 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2101 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
2103 if (pa && pa->length)
2105 gnu_result = pa->length;
2106 break;
2109 gnu_result
2110 = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)),
2111 gnu_result_type);
2114 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2115 handling. Note that these attributes could not have been used on
2116 an unconstrained array type. */
2117 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2119 /* Cache the expression we have just computed. Since we want to do it
2120 at run time, we force the use of a SAVE_EXPR and let the gimplifier
2121 create the temporary in the outermost binding level. We will make
2122 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
2123 paths by forcing its evaluation on entry of the function. */
2124 if (pa)
2126 gnu_result
2127 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2128 switch (attribute)
2130 case Attr_First:
2131 pa->first = gnu_result;
2132 break;
2134 case Attr_Last:
2135 pa->last = gnu_result;
2136 break;
2138 case Attr_Length:
2139 case Attr_Range_Length:
2140 pa->length = gnu_result;
2141 break;
2143 default:
2144 gcc_unreachable ();
2148 /* Otherwise, evaluate it each time it is referenced. */
2149 else
2150 switch (attribute)
2152 case Attr_First:
2153 case Attr_Last:
2154 /* If we are dereferencing a pointer to unconstrained array, we
2155 need to capture the value because the pointed-to bounds may
2156 subsequently be released. */
2157 if (unconstrained_ptr_deref)
2158 gnu_result
2159 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2160 break;
2162 case Attr_Length:
2163 case Attr_Range_Length:
2164 /* Set the source location onto the predicate of the condition
2165 but not if the expression is cached to avoid messing up the
2166 debug info. */
2167 if (TREE_CODE (gnu_result) == COND_EXPR
2168 && EXPR_P (TREE_OPERAND (gnu_result, 0)))
2169 set_expr_location_from_node (TREE_OPERAND (gnu_result, 0),
2170 gnat_node);
2171 break;
2173 default:
2174 gcc_unreachable ();
2177 break;
2180 case Attr_Bit_Position:
2181 case Attr_Position:
2182 case Attr_First_Bit:
2183 case Attr_Last_Bit:
2184 case Attr_Bit:
2186 HOST_WIDE_INT bitsize;
2187 HOST_WIDE_INT bitpos;
2188 tree gnu_offset;
2189 tree gnu_field_bitpos;
2190 tree gnu_field_offset;
2191 tree gnu_inner;
2192 machine_mode mode;
2193 int unsignedp, volatilep;
2195 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2196 gnu_prefix = remove_conversions (gnu_prefix, true);
2197 prefix_unused = true;
2199 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
2200 the result is 0. Don't allow 'Bit on a bare component, though. */
2201 if (attribute == Attr_Bit
2202 && TREE_CODE (gnu_prefix) != COMPONENT_REF
2203 && TREE_CODE (gnu_prefix) != FIELD_DECL)
2205 gnu_result = integer_zero_node;
2206 break;
2209 else
2210 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
2211 || (attribute == Attr_Bit_Position
2212 && TREE_CODE (gnu_prefix) == FIELD_DECL));
2214 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
2215 &mode, &unsignedp, &volatilep, false);
2217 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
2219 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
2220 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
2222 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
2223 TREE_CODE (gnu_inner) == COMPONENT_REF
2224 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
2225 gnu_inner = TREE_OPERAND (gnu_inner, 0))
2227 gnu_field_bitpos
2228 = size_binop (PLUS_EXPR, gnu_field_bitpos,
2229 bit_position (TREE_OPERAND (gnu_inner, 1)));
2230 gnu_field_offset
2231 = size_binop (PLUS_EXPR, gnu_field_offset,
2232 byte_position (TREE_OPERAND (gnu_inner, 1)));
2235 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
2237 gnu_field_bitpos = bit_position (gnu_prefix);
2238 gnu_field_offset = byte_position (gnu_prefix);
2240 else
2242 gnu_field_bitpos = bitsize_zero_node;
2243 gnu_field_offset = size_zero_node;
2246 switch (attribute)
2248 case Attr_Position:
2249 gnu_result = gnu_field_offset;
2250 break;
2252 case Attr_First_Bit:
2253 case Attr_Bit:
2254 gnu_result = size_int (bitpos % BITS_PER_UNIT);
2255 break;
2257 case Attr_Last_Bit:
2258 gnu_result = bitsize_int (bitpos % BITS_PER_UNIT);
2259 gnu_result = size_binop (PLUS_EXPR, gnu_result,
2260 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
2261 /* ??? Avoid a large unsigned result that will overflow when
2262 converted to the signed universal_integer. */
2263 if (integer_zerop (gnu_result))
2264 gnu_result = integer_minus_one_node;
2265 else
2266 gnu_result
2267 = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node);
2268 break;
2270 case Attr_Bit_Position:
2271 gnu_result = gnu_field_bitpos;
2272 break;
2275 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2276 handling. */
2277 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2278 break;
2281 case Attr_Min:
2282 case Attr_Max:
2284 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
2285 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
2287 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2289 /* The result of {MIN,MAX}_EXPR is unspecified if either operand is
2290 a NaN so we implement the semantics of C99 f{min,max} to make it
2291 predictable in this case: if either operand is a NaN, the other
2292 is returned; if both operands are NaN's, a NaN is returned. */
2293 if (SCALAR_FLOAT_TYPE_P (gnu_result_type))
2295 const bool lhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_lhs);
2296 const bool rhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_rhs);
2297 tree t = builtin_decl_explicit (BUILT_IN_ISNAN);
2298 tree lhs_is_nan, rhs_is_nan;
2300 /* If the operands have side-effects, they need to be evaluated
2301 only once in spite of the multiple references in the result. */
2302 if (lhs_side_effects_p)
2303 gnu_lhs = gnat_protect_expr (gnu_lhs);
2304 if (rhs_side_effects_p)
2305 gnu_rhs = gnat_protect_expr (gnu_rhs);
2307 lhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2308 build_call_expr (t, 1, gnu_lhs),
2309 integer_zero_node);
2311 rhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2312 build_call_expr (t, 1, gnu_rhs),
2313 integer_zero_node);
2315 gnu_result = build_binary_op (attribute == Attr_Min
2316 ? MIN_EXPR : MAX_EXPR,
2317 gnu_result_type, gnu_lhs, gnu_rhs);
2318 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2319 rhs_is_nan, gnu_lhs, gnu_result);
2320 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2321 lhs_is_nan, gnu_rhs, gnu_result);
2323 /* If the operands have side-effects, they need to be evaluated
2324 before doing the tests above since the place they otherwise
2325 would end up being evaluated at run time could be wrong. */
2326 if (lhs_side_effects_p)
2327 gnu_result
2328 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_lhs, gnu_result);
2330 if (rhs_side_effects_p)
2331 gnu_result
2332 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_rhs, gnu_result);
2334 else
2335 gnu_result = build_binary_op (attribute == Attr_Min
2336 ? MIN_EXPR : MAX_EXPR,
2337 gnu_result_type, gnu_lhs, gnu_rhs);
2339 break;
2341 case Attr_Passed_By_Reference:
2342 gnu_result = size_int (default_pass_by_ref (gnu_type)
2343 || must_pass_by_ref (gnu_type));
2344 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2345 break;
2347 case Attr_Component_Size:
2348 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
2349 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
2350 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
2352 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2353 gnu_type = TREE_TYPE (gnu_prefix);
2355 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
2356 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
2358 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2359 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2360 gnu_type = TREE_TYPE (gnu_type);
2362 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2364 /* Note this size cannot be self-referential. */
2365 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
2366 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2367 prefix_unused = true;
2368 break;
2370 case Attr_Descriptor_Size:
2371 gnu_type = TREE_TYPE (gnu_prefix);
2372 gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE);
2374 /* What we want is the offset of the ARRAY field in the record
2375 that the thin pointer designates. */
2376 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
2377 gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
2378 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2379 prefix_unused = true;
2380 break;
2382 case Attr_Null_Parameter:
2383 /* This is just a zero cast to the pointer type for our prefix and
2384 dereferenced. */
2385 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2386 gnu_result
2387 = build_unary_op (INDIRECT_REF, NULL_TREE,
2388 convert (build_pointer_type (gnu_result_type),
2389 integer_zero_node));
2390 TREE_PRIVATE (gnu_result) = 1;
2391 break;
2393 case Attr_Mechanism_Code:
2395 Entity_Id gnat_obj = Entity (gnat_prefix);
2396 int code;
2398 prefix_unused = true;
2399 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2400 if (Present (Expressions (gnat_node)))
2402 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
2404 for (gnat_obj = First_Formal (gnat_obj); i > 1;
2405 i--, gnat_obj = Next_Formal (gnat_obj))
2409 code = Mechanism (gnat_obj);
2410 if (code == Default)
2411 code = ((present_gnu_tree (gnat_obj)
2412 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
2413 || ((TREE_CODE (get_gnu_tree (gnat_obj))
2414 == PARM_DECL)
2415 && (DECL_BY_COMPONENT_PTR_P
2416 (get_gnu_tree (gnat_obj))))))
2417 ? By_Reference : By_Copy);
2418 gnu_result = convert (gnu_result_type, size_int (- code));
2420 break;
2422 case Attr_Model:
2423 /* We treat Model as identical to Machine. This is true for at least
2424 IEEE and some other nice floating-point systems. */
2426 /* ... fall through ... */
2428 case Attr_Machine:
2429 /* The trick is to force the compiler to store the result in memory so
2430 that we do not have extra precision used. But do this only when this
2431 is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of
2432 the type is lower than that of the longest floating-point type. */
2433 prefix_unused = true;
2434 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2435 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2436 gnu_result = convert (gnu_result_type, gnu_expr);
2438 if (fp_arith_may_widen
2439 && TYPE_PRECISION (gnu_result_type)
2440 < TYPE_PRECISION (longest_float_type_node))
2442 tree rec_type = make_node (RECORD_TYPE);
2443 tree field
2444 = create_field_decl (get_identifier ("OBJ"), gnu_result_type,
2445 rec_type, NULL_TREE, NULL_TREE, 0, 0);
2446 tree rec_val, asm_expr;
2448 finish_record_type (rec_type, field, 0, false);
2450 rec_val = build_constructor_single (rec_type, field, gnu_result);
2451 rec_val = save_expr (rec_val);
2453 asm_expr
2454 = build5 (ASM_EXPR, void_type_node,
2455 build_string (0, ""),
2456 tree_cons (build_tree_list (NULL_TREE,
2457 build_string (2, "=m")),
2458 rec_val, NULL_TREE),
2459 tree_cons (build_tree_list (NULL_TREE,
2460 build_string (1, "m")),
2461 rec_val, NULL_TREE),
2462 NULL_TREE, NULL_TREE);
2463 ASM_VOLATILE_P (asm_expr) = 1;
2465 gnu_result
2466 = build_compound_expr (gnu_result_type, asm_expr,
2467 build_component_ref (rec_val, NULL_TREE,
2468 field, false));
2470 break;
2472 case Attr_Deref:
2473 prefix_unused = true;
2474 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2475 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2476 /* This can be a random address so build an alias-all pointer type. */
2477 gnu_expr
2478 = convert (build_pointer_type_for_mode (gnu_result_type, ptr_mode,
2479 true),
2480 gnu_expr);
2481 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_expr);
2482 break;
2484 default:
2485 /* This abort means that we have an unimplemented attribute. */
2486 gcc_unreachable ();
2489 /* If this is an attribute where the prefix was unused, force a use of it if
2490 it has a side-effect. But don't do it if the prefix is just an entity
2491 name. However, if an access check is needed, we must do it. See second
2492 example in AARM 11.6(5.e). */
2493 if (prefix_unused
2494 && TREE_SIDE_EFFECTS (gnu_prefix)
2495 && !Is_Entity_Name (gnat_prefix))
2496 gnu_result
2497 = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result);
2499 *gnu_result_type_p = gnu_result_type;
2500 return gnu_result;
2503 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
2504 to a GCC tree, which is returned. */
2506 static tree
2507 Case_Statement_to_gnu (Node_Id gnat_node)
2509 tree gnu_result, gnu_expr, gnu_label;
2510 Node_Id gnat_when;
2511 location_t end_locus;
2512 bool may_fallthru = false;
2514 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2515 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2517 /* The range of values in a case statement is determined by the rules in
2518 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
2519 of the expression. One exception arises in the case of a simple name that
2520 is parenthesized. This still has the Etype of the name, but since it is
2521 not a name, para 7 does not apply, and we need to go to the base type.
2522 This is the only case where parenthesization affects the dynamic
2523 semantics (i.e. the range of possible values at run time that is covered
2524 by the others alternative).
2526 Another exception is if the subtype of the expression is non-static. In
2527 that case, we also have to use the base type. */
2528 if (Paren_Count (Expression (gnat_node)) != 0
2529 || !Is_OK_Static_Subtype (Underlying_Type
2530 (Etype (Expression (gnat_node)))))
2531 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2533 /* We build a SWITCH_EXPR that contains the code with interspersed
2534 CASE_LABEL_EXPRs for each label. */
2535 if (!Sloc_to_locus (End_Location (gnat_node), &end_locus))
2536 end_locus = input_location;
2537 gnu_label = create_artificial_label (end_locus);
2538 start_stmt_group ();
2540 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2541 Present (gnat_when);
2542 gnat_when = Next_Non_Pragma (gnat_when))
2544 bool choices_added_p = false;
2545 Node_Id gnat_choice;
2547 /* First compile all the different case choices for the current WHEN
2548 alternative. */
2549 for (gnat_choice = First (Discrete_Choices (gnat_when));
2550 Present (gnat_choice);
2551 gnat_choice = Next (gnat_choice))
2553 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2554 tree label = create_artificial_label (input_location);
2556 switch (Nkind (gnat_choice))
2558 case N_Range:
2559 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
2560 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
2561 break;
2563 case N_Subtype_Indication:
2564 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
2565 (Constraint (gnat_choice))));
2566 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
2567 (Constraint (gnat_choice))));
2568 break;
2570 case N_Identifier:
2571 case N_Expanded_Name:
2572 /* This represents either a subtype range or a static value of
2573 some kind; Ekind says which. */
2574 if (IN (Ekind (Entity (gnat_choice)), Type_Kind))
2576 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
2578 gnu_low = TYPE_MIN_VALUE (gnu_type);
2579 gnu_high = TYPE_MAX_VALUE (gnu_type);
2580 break;
2583 /* ... fall through ... */
2585 case N_Character_Literal:
2586 case N_Integer_Literal:
2587 gnu_low = gnat_to_gnu (gnat_choice);
2588 break;
2590 case N_Others_Choice:
2591 break;
2593 default:
2594 gcc_unreachable ();
2597 /* Everything should be folded into constants at this point. */
2598 gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST);
2599 gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST);
2601 add_stmt_with_node (build_case_label (gnu_low, gnu_high, label),
2602 gnat_choice);
2603 choices_added_p = true;
2606 /* This construct doesn't define a scope so we shouldn't push a binding
2607 level around the statement list. Except that we have always done so
2608 historically and this makes it possible to reduce stack usage. As a
2609 compromise, we keep doing it for case statements, for which this has
2610 never been problematic, but not for case expressions in Ada 2012. */
2611 if (choices_added_p)
2613 const bool is_case_expression
2614 = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions);
2615 tree group
2616 = build_stmt_group (Statements (gnat_when), !is_case_expression);
2617 bool group_may_fallthru = block_may_fallthru (group);
2618 add_stmt (group);
2619 if (group_may_fallthru)
2621 tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label);
2622 SET_EXPR_LOCATION (stmt, end_locus);
2623 add_stmt (stmt);
2624 may_fallthru = true;
2629 /* Now emit a definition of the label the cases branch to, if any. */
2630 if (may_fallthru)
2631 add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label));
2632 gnu_result = build3 (SWITCH_EXPR, TREE_TYPE (gnu_expr), gnu_expr,
2633 end_stmt_group (), NULL_TREE);
2635 return gnu_result;
2638 /* Find out whether VAR is an iteration variable of an enclosing loop in the
2639 current function. If so, push a range_check_info structure onto the stack
2640 of this enclosing loop and return it. Otherwise, return NULL. */
2642 static struct range_check_info_d *
2643 push_range_check_info (tree var)
2645 struct loop_info_d *iter = NULL;
2646 unsigned int i;
2648 var = remove_conversions (var, false);
2650 if (TREE_CODE (var) != VAR_DECL)
2651 return NULL;
2653 if (decl_function_context (var) != current_function_decl)
2654 return NULL;
2656 gcc_assert (vec_safe_length (gnu_loop_stack) > 0);
2658 for (i = vec_safe_length (gnu_loop_stack) - 1;
2659 vec_safe_iterate (gnu_loop_stack, i, &iter);
2660 i--)
2661 if (var == iter->loop_var)
2662 break;
2664 if (iter)
2666 struct range_check_info_d *rci = ggc_alloc<range_check_info_d> ();
2667 vec_safe_push (iter->checks, rci);
2668 return rci;
2671 return NULL;
2674 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2675 false, or the maximum value if MAX is true, of TYPE. */
2677 static bool
2678 can_equal_min_or_max_val_p (tree val, tree type, bool max)
2680 tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
2682 if (TREE_CODE (min_or_max_val) != INTEGER_CST)
2683 return true;
2685 if (TREE_CODE (val) == NOP_EXPR)
2686 val = (max
2687 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))
2688 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))));
2690 if (TREE_CODE (val) != INTEGER_CST)
2691 return true;
2693 if (max)
2694 return tree_int_cst_lt (val, min_or_max_val) == 0;
2695 else
2696 return tree_int_cst_lt (min_or_max_val, val) == 0;
2699 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2700 If REVERSE is true, minimum value is taken as maximum value. */
2702 static inline bool
2703 can_equal_min_val_p (tree val, tree type, bool reverse)
2705 return can_equal_min_or_max_val_p (val, type, reverse);
2708 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2709 If REVERSE is true, maximum value is taken as minimum value. */
2711 static inline bool
2712 can_equal_max_val_p (tree val, tree type, bool reverse)
2714 return can_equal_min_or_max_val_p (val, type, !reverse);
2717 /* Return true if VAL1 can be lower than VAL2. */
2719 static bool
2720 can_be_lower_p (tree val1, tree val2)
2722 if (TREE_CODE (val1) == NOP_EXPR)
2723 val1 = TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val1, 0)));
2725 if (TREE_CODE (val1) != INTEGER_CST)
2726 return true;
2728 if (TREE_CODE (val2) == NOP_EXPR)
2729 val2 = TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val2, 0)));
2731 if (TREE_CODE (val2) != INTEGER_CST)
2732 return true;
2734 return tree_int_cst_lt (val1, val2);
2737 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
2738 to a GCC tree, which is returned. */
2740 static tree
2741 Loop_Statement_to_gnu (Node_Id gnat_node)
2743 const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
2744 struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> ();
2745 tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE,
2746 NULL_TREE, NULL_TREE, NULL_TREE);
2747 tree gnu_loop_label = create_artificial_label (input_location);
2748 tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2749 tree gnu_result;
2751 /* Push the loop_info structure associated with the LOOP_STMT. */
2752 vec_safe_push (gnu_loop_stack, gnu_loop_info);
2754 /* Set location information for statement and end label. */
2755 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
2756 Sloc_to_locus (Sloc (End_Label (gnat_node)),
2757 &DECL_SOURCE_LOCATION (gnu_loop_label));
2758 LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label;
2760 /* Save the statement for later reuse. */
2761 gnu_loop_info->stmt = gnu_loop_stmt;
2763 /* Set the condition under which the loop must keep going.
2764 For the case "LOOP .... END LOOP;" the condition is always true. */
2765 if (No (gnat_iter_scheme))
2768 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2769 else if (Present (Condition (gnat_iter_scheme)))
2770 LOOP_STMT_COND (gnu_loop_stmt)
2771 = gnat_to_gnu (Condition (gnat_iter_scheme));
2773 /* Otherwise we have an iteration scheme and the condition is given by the
2774 bounds of the subtype of the iteration variable. */
2775 else
2777 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
2778 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2779 Entity_Id gnat_type = Etype (gnat_loop_var);
2780 tree gnu_type = get_unpadded_type (gnat_type);
2781 tree gnu_base_type = get_base_type (gnu_type);
2782 tree gnu_one_node = convert (gnu_base_type, integer_one_node);
2783 tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt;
2784 enum tree_code update_code, test_code, shift_code;
2785 bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false;
2787 gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type));
2788 gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type));
2790 /* We must disable modulo reduction for the iteration variable, if any,
2791 in order for the loop comparison to be effective. */
2792 if (reverse)
2794 gnu_first = gnu_high;
2795 gnu_last = gnu_low;
2796 update_code = MINUS_NOMOD_EXPR;
2797 test_code = GE_EXPR;
2798 shift_code = PLUS_NOMOD_EXPR;
2800 else
2802 gnu_first = gnu_low;
2803 gnu_last = gnu_high;
2804 update_code = PLUS_NOMOD_EXPR;
2805 test_code = LE_EXPR;
2806 shift_code = MINUS_NOMOD_EXPR;
2809 /* We use two different strategies to translate the loop, depending on
2810 whether optimization is enabled.
2812 If it is, we generate the canonical loop form expected by the loop
2813 optimizer and the loop vectorizer, which is the do-while form:
2815 ENTRY_COND
2816 loop:
2817 TOP_UPDATE
2818 BODY
2819 BOTTOM_COND
2820 GOTO loop
2822 This avoids an implicit dependency on loop header copying and makes
2823 it possible to turn BOTTOM_COND into an inequality test.
2825 If optimization is disabled, loop header copying doesn't come into
2826 play and we try to generate the loop form with the fewer conditional
2827 branches. First, the default form, which is:
2829 loop:
2830 TOP_COND
2831 BODY
2832 BOTTOM_UPDATE
2833 GOTO loop
2835 It should catch most loops with constant ending point. Then, if we
2836 cannot, we try to generate the shifted form:
2838 loop:
2839 TOP_COND
2840 TOP_UPDATE
2841 BODY
2842 GOTO loop
2844 which should catch loops with constant starting point. Otherwise, if
2845 we cannot, we generate the fallback form:
2847 ENTRY_COND
2848 loop:
2849 BODY
2850 BOTTOM_COND
2851 BOTTOM_UPDATE
2852 GOTO loop
2854 which works in all cases. */
2856 if (optimize)
2858 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
2859 overflow. */
2860 if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse))
2863 /* Otherwise, use the do-while form with the help of a special
2864 induction variable in the unsigned version of the base type
2865 or the unsigned version of the size type, whichever is the
2866 largest, in order to have wrap-around arithmetics for it. */
2867 else
2869 if (TYPE_PRECISION (gnu_base_type)
2870 > TYPE_PRECISION (size_type_node))
2871 gnu_base_type
2872 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1);
2873 else
2874 gnu_base_type = size_type_node;
2876 gnu_first = convert (gnu_base_type, gnu_first);
2877 gnu_last = convert (gnu_base_type, gnu_last);
2878 gnu_one_node = convert (gnu_base_type, integer_one_node);
2879 use_iv = true;
2882 gnu_first
2883 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2884 gnu_one_node);
2885 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2886 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2888 else
2890 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
2891 if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse))
2894 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
2895 GNU_LAST-1 does. */
2896 else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)
2897 && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse))
2899 gnu_first
2900 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2901 gnu_one_node);
2902 gnu_last
2903 = build_binary_op (shift_code, gnu_base_type, gnu_last,
2904 gnu_one_node);
2905 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2908 /* Otherwise, use the fallback form. */
2909 else
2910 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2913 /* If we use the BOTTOM_COND, we can turn the test into an inequality
2914 test but we may have to add ENTRY_COND to protect the empty loop. */
2915 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt))
2917 test_code = NE_EXPR;
2918 if (can_be_lower_p (gnu_high, gnu_low))
2920 gnu_cond_expr
2921 = build3 (COND_EXPR, void_type_node,
2922 build_binary_op (LE_EXPR, boolean_type_node,
2923 gnu_low, gnu_high),
2924 NULL_TREE, alloc_stmt_list ());
2925 set_expr_location_from_node (gnu_cond_expr, gnat_loop_spec);
2929 /* Open a new nesting level that will surround the loop to declare the
2930 iteration variable. */
2931 start_stmt_group ();
2932 gnat_pushlevel ();
2934 /* If we use the special induction variable, create it and set it to
2935 its initial value. Morever, the regular iteration variable cannot
2936 itself be initialized, lest the initial value wrapped around. */
2937 if (use_iv)
2939 gnu_loop_iv
2940 = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var);
2941 add_stmt (gnu_stmt);
2942 gnu_first = NULL_TREE;
2944 else
2945 gnu_loop_iv = NULL_TREE;
2947 /* Declare the iteration variable and set it to its initial value. */
2948 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
2949 if (DECL_BY_REF_P (gnu_loop_var))
2950 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
2951 else if (use_iv)
2953 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var));
2954 SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv);
2956 gnu_loop_info->loop_var = gnu_loop_var;
2958 /* Do all the arithmetics in the base type. */
2959 gnu_loop_var = convert (gnu_base_type, gnu_loop_var);
2961 /* Set either the top or bottom exit condition. */
2962 if (use_iv)
2963 LOOP_STMT_COND (gnu_loop_stmt)
2964 = build_binary_op (test_code, boolean_type_node, gnu_loop_iv,
2965 gnu_last);
2966 else
2967 LOOP_STMT_COND (gnu_loop_stmt)
2968 = build_binary_op (test_code, boolean_type_node, gnu_loop_var,
2969 gnu_last);
2971 /* Set either the top or bottom update statement and give it the source
2972 location of the iteration for better coverage info. */
2973 if (use_iv)
2975 gnu_stmt
2976 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv,
2977 build_binary_op (update_code, gnu_base_type,
2978 gnu_loop_iv, gnu_one_node));
2979 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2980 append_to_statement_list (gnu_stmt,
2981 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2982 gnu_stmt
2983 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2984 gnu_loop_iv);
2985 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2986 append_to_statement_list (gnu_stmt,
2987 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2989 else
2991 gnu_stmt
2992 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2993 build_binary_op (update_code, gnu_base_type,
2994 gnu_loop_var, gnu_one_node));
2995 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2996 LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt;
3000 /* If the loop was named, have the name point to this loop. In this case,
3001 the association is not a DECL node, but the end label of the loop. */
3002 if (Present (Identifier (gnat_node)))
3003 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true);
3005 /* Make the loop body into its own block, so any allocated storage will be
3006 released every iteration. This is needed for stack allocation. */
3007 LOOP_STMT_BODY (gnu_loop_stmt)
3008 = build_stmt_group (Statements (gnat_node), true);
3009 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
3011 /* If we have an iteration scheme, then we are in a statement group. Add
3012 the LOOP_STMT to it, finish it and make it the "loop". */
3013 if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme)))
3015 struct range_check_info_d *rci;
3016 unsigned n_checks = vec_safe_length (gnu_loop_info->checks);
3017 unsigned int i;
3019 /* First, if we have computed a small number of invariant conditions for
3020 range checks applied to the iteration variable, then initialize these
3021 conditions in front of the loop. Otherwise, leave them set to true.
3023 ??? The heuristics need to be improved, by taking into account the
3024 following datapoints:
3025 - loop unswitching is disabled for big loops. The cap is the
3026 parameter PARAM_MAX_UNSWITCH_INSNS (50).
3027 - loop unswitching can only be applied a small number of times
3028 to a given loop. The cap is PARAM_MAX_UNSWITCH_LEVEL (3).
3029 - the front-end quickly generates useless or redundant checks
3030 that can be entirely optimized away in the end. */
3031 if (1 <= n_checks && n_checks <= 4)
3032 for (i = 0;
3033 vec_safe_iterate (gnu_loop_info->checks, i, &rci);
3034 i++)
3036 tree low_ok
3037 = rci->low_bound
3038 ? build_binary_op (GE_EXPR, boolean_type_node,
3039 convert (rci->type, gnu_low),
3040 rci->low_bound)
3041 : boolean_true_node;
3043 tree high_ok
3044 = rci->high_bound
3045 ? build_binary_op (LE_EXPR, boolean_type_node,
3046 convert (rci->type, gnu_high),
3047 rci->high_bound)
3048 : boolean_true_node;
3050 tree range_ok
3051 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
3052 low_ok, high_ok);
3054 TREE_OPERAND (rci->invariant_cond, 0)
3055 = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok);
3057 add_stmt_with_node_force (rci->invariant_cond, gnat_node);
3060 add_stmt (gnu_loop_stmt);
3061 gnat_poplevel ();
3062 gnu_loop_stmt = end_stmt_group ();
3065 /* If we have an outer COND_EXPR, that's our result and this loop is its
3066 "true" statement. Otherwise, the result is the LOOP_STMT. */
3067 if (gnu_cond_expr)
3069 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
3070 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
3071 gnu_result = gnu_cond_expr;
3073 else
3074 gnu_result = gnu_loop_stmt;
3076 gnu_loop_stack->pop ();
3078 return gnu_result;
3081 /* This page implements a form of Named Return Value optimization modelled
3082 on the C++ optimization of the same name. The main difference is that
3083 we disregard any semantical considerations when applying it here, the
3084 counterpart being that we don't try to apply it to semantically loaded
3085 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
3087 We consider a function body of the following GENERIC form:
3089 return_type R1;
3090 [...]
3091 RETURN_EXPR [<retval> = ...]
3092 [...]
3093 RETURN_EXPR [<retval> = R1]
3094 [...]
3095 return_type Ri;
3096 [...]
3097 RETURN_EXPR [<retval> = ...]
3098 [...]
3099 RETURN_EXPR [<retval> = Ri]
3100 [...]
3102 and we try to fulfill a simple criterion that would make it possible to
3103 replace one or several Ri variables with the RESULT_DECL of the function.
3105 The first observation is that RETURN_EXPRs that don't directly reference
3106 any of the Ri variables on the RHS of their assignment are transparent wrt
3107 the optimization. This is because the Ri variables aren't addressable so
3108 any transformation applied to them doesn't affect the RHS; moreover, the
3109 assignment writes the full <retval> object so existing values are entirely
3110 discarded.
3112 This property can be extended to some forms of RETURN_EXPRs that reference
3113 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3114 case, in particular when function calls are involved.
3116 Therefore the algorithm is as follows:
3118 1. Collect the list of candidates for a Named Return Value (Ri variables
3119 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3120 other expressions on the RHS of such assignments.
3122 2. Prune the members of the first list (candidates) that are referenced
3123 by a member of the second list (expressions).
3125 3. Extract a set of candidates with non-overlapping live ranges from the
3126 first list. These are the Named Return Values.
3128 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
3129 Named Return Values in the function with the RESULT_DECL.
3131 If the function returns an unconstrained type, things are a bit different
3132 because the anonymous return object is allocated on the secondary stack
3133 and RESULT_DECL is only a pointer to it. Each return object can be of a
3134 different size and is allocated separately so we need not care about the
3135 aforementioned overlapping issues. Therefore, we don't collect the other
3136 expressions and skip step #2 in the algorithm. */
3138 struct nrv_data
3140 bitmap nrv;
3141 tree result;
3142 Node_Id gnat_ret;
3143 hash_set<tree> *visited;
3146 /* Return true if T is a Named Return Value. */
3148 static inline bool
3149 is_nrv_p (bitmap nrv, tree t)
3151 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
3154 /* Helper function for walk_tree, used by finalize_nrv below. */
3156 static tree
3157 prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3159 struct nrv_data *dp = (struct nrv_data *)data;
3160 tree t = *tp;
3162 /* No need to walk into types or decls. */
3163 if (IS_TYPE_OR_DECL_P (t))
3164 *walk_subtrees = 0;
3166 if (is_nrv_p (dp->nrv, t))
3167 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3169 return NULL_TREE;
3172 /* Prune Named Return Values in BLOCK and return true if there is still a
3173 Named Return Value in BLOCK or one of its sub-blocks. */
3175 static bool
3176 prune_nrv_in_block (bitmap nrv, tree block)
3178 bool has_nrv = false;
3179 tree t;
3181 /* First recurse on the sub-blocks. */
3182 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3183 has_nrv |= prune_nrv_in_block (nrv, t);
3185 /* Then make sure to keep at most one NRV per block. */
3186 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3187 if (is_nrv_p (nrv, t))
3189 if (has_nrv)
3190 bitmap_clear_bit (nrv, DECL_UID (t));
3191 else
3192 has_nrv = true;
3195 return has_nrv;
3198 /* Helper function for walk_tree, used by finalize_nrv below. */
3200 static tree
3201 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3203 struct nrv_data *dp = (struct nrv_data *)data;
3204 tree t = *tp;
3206 /* No need to walk into types. */
3207 if (TYPE_P (t))
3208 *walk_subtrees = 0;
3210 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3211 nop, but differs from using NULL_TREE in that it indicates that we care
3212 about the value of the RESULT_DECL. */
3213 else if (TREE_CODE (t) == RETURN_EXPR
3214 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3216 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1), init_expr;
3218 /* If this is the temporary created for a return value with variable
3219 size in Call_to_gnu, we replace the RHS with the init expression. */
3220 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3221 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3222 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3223 == TREE_OPERAND (ret_val, 1))
3225 init_expr = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3226 ret_val = TREE_OPERAND (ret_val, 1);
3228 else
3229 init_expr = NULL_TREE;
3231 /* Strip useless conversions around the return value. */
3232 if (gnat_useless_type_conversion (ret_val))
3233 ret_val = TREE_OPERAND (ret_val, 0);
3235 if (is_nrv_p (dp->nrv, ret_val))
3237 if (init_expr)
3238 TREE_OPERAND (TREE_OPERAND (t, 0), 1) = init_expr;
3239 else
3240 TREE_OPERAND (t, 0) = dp->result;
3244 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3245 if needed. */
3246 else if (TREE_CODE (t) == DECL_EXPR
3247 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3249 tree var = DECL_EXPR_DECL (t), init;
3251 if (DECL_INITIAL (var))
3253 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3254 DECL_INITIAL (var));
3255 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3256 DECL_INITIAL (var) = NULL_TREE;
3258 else
3259 init = build_empty_stmt (EXPR_LOCATION (t));
3260 *tp = init;
3262 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3263 SET_DECL_VALUE_EXPR (var, dp->result);
3264 DECL_HAS_VALUE_EXPR_P (var) = 1;
3265 /* ??? Kludge to avoid an assertion failure during inlining. */
3266 DECL_SIZE (var) = bitsize_unit_node;
3267 DECL_SIZE_UNIT (var) = size_one_node;
3270 /* And replace all uses of NRVs with the RESULT_DECL. */
3271 else if (is_nrv_p (dp->nrv, t))
3272 *tp = convert (TREE_TYPE (t), dp->result);
3274 /* Avoid walking into the same tree more than once. Unfortunately, we
3275 can't just use walk_tree_without_duplicates because it would only
3276 call us for the first occurrence of NRVs in the function body. */
3277 if (dp->visited->add (*tp))
3278 *walk_subtrees = 0;
3280 return NULL_TREE;
3283 /* Likewise, but used when the function returns an unconstrained type. */
3285 static tree
3286 finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3288 struct nrv_data *dp = (struct nrv_data *)data;
3289 tree t = *tp;
3291 /* No need to walk into types. */
3292 if (TYPE_P (t))
3293 *walk_subtrees = 0;
3295 /* We need to see the DECL_EXPR of NRVs before any other references so we
3296 walk the body of BIND_EXPR before walking its variables. */
3297 else if (TREE_CODE (t) == BIND_EXPR)
3298 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3300 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3301 return value built by the allocator instead of the whole construct. */
3302 else if (TREE_CODE (t) == RETURN_EXPR
3303 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3305 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3307 /* This is the construct returned by the allocator. */
3308 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3309 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3311 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
3312 ret_val
3313 = (*CONSTRUCTOR_ELTS (TREE_OPERAND (TREE_OPERAND (ret_val, 0),
3314 1)))[1].value;
3315 else
3316 ret_val = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3319 /* Strip useless conversions around the return value. */
3320 if (gnat_useless_type_conversion (ret_val)
3321 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3322 ret_val = TREE_OPERAND (ret_val, 0);
3324 /* Strip unpadding around the return value. */
3325 if (TREE_CODE (ret_val) == COMPONENT_REF
3326 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3327 ret_val = TREE_OPERAND (ret_val, 0);
3329 /* Assign the new return value to the RESULT_DECL. */
3330 if (is_nrv_p (dp->nrv, ret_val))
3331 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3332 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3335 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3336 into a new variable. */
3337 else if (TREE_CODE (t) == DECL_EXPR
3338 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3340 tree saved_current_function_decl = current_function_decl;
3341 tree var = DECL_EXPR_DECL (t);
3342 tree alloc, p_array, new_var, new_ret;
3343 vec<constructor_elt, va_gc> *v;
3344 vec_alloc (v, 2);
3346 /* Create an artificial context to build the allocation. */
3347 current_function_decl = decl_function_context (var);
3348 start_stmt_group ();
3349 gnat_pushlevel ();
3351 /* This will return a COMPOUND_EXPR with the allocation in the first
3352 arm and the final return value in the second arm. */
3353 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3354 TREE_TYPE (dp->result),
3355 Procedure_To_Call (dp->gnat_ret),
3356 Storage_Pool (dp->gnat_ret),
3357 Empty, false);
3359 /* The new variable is built as a reference to the allocated space. */
3360 new_var
3361 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3362 build_reference_type (TREE_TYPE (var)));
3363 DECL_BY_REFERENCE (new_var) = 1;
3365 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3367 /* The new initial value is a COMPOUND_EXPR with the allocation in
3368 the first arm and the value of P_ARRAY in the second arm. */
3369 DECL_INITIAL (new_var)
3370 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3371 TREE_OPERAND (alloc, 0),
3372 (*CONSTRUCTOR_ELTS (TREE_OPERAND (alloc, 1)))[0].value);
3374 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3375 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3376 CONSTRUCTOR_APPEND_ELT (v, p_array,
3377 fold_convert (TREE_TYPE (p_array), new_var));
3378 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
3379 (*CONSTRUCTOR_ELTS (
3380 TREE_OPERAND (alloc, 1)))[1].value);
3381 new_ret = build_constructor (TREE_TYPE (alloc), v);
3383 else
3385 /* The new initial value is just the allocation. */
3386 DECL_INITIAL (new_var) = alloc;
3387 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3390 gnat_pushdecl (new_var, Empty);
3392 /* Destroy the artificial context and insert the new statements. */
3393 gnat_zaplevel ();
3394 *tp = end_stmt_group ();
3395 current_function_decl = saved_current_function_decl;
3397 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3398 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3399 DECL_CHAIN (var) = new_var;
3400 DECL_IGNORED_P (var) = 1;
3402 /* Save the new return value and the dereference of NEW_VAR. */
3403 DECL_INITIAL (var)
3404 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3405 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3406 /* ??? Kludge to avoid messing up during inlining. */
3407 DECL_CONTEXT (var) = NULL_TREE;
3410 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3411 else if (is_nrv_p (dp->nrv, t))
3412 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3414 /* Avoid walking into the same tree more than once. Unfortunately, we
3415 can't just use walk_tree_without_duplicates because it would only
3416 call us for the first occurrence of NRVs in the function body. */
3417 if (dp->visited->add (*tp))
3418 *walk_subtrees = 0;
3420 return NULL_TREE;
3423 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3424 contains the candidates for Named Return Value and OTHER is a list of
3425 the other return values. GNAT_RET is a representative return node. */
3427 static void
3428 finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
3430 struct cgraph_node *node;
3431 struct nrv_data data;
3432 walk_tree_fn func;
3433 unsigned int i;
3434 tree iter;
3436 /* We shouldn't be applying the optimization to return types that we aren't
3437 allowed to manipulate freely. */
3438 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
3440 /* Prune the candidates that are referenced by other return values. */
3441 data.nrv = nrv;
3442 data.result = NULL_TREE;
3443 data.visited = NULL;
3444 for (i = 0; vec_safe_iterate (other, i, &iter); i++)
3445 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3446 if (bitmap_empty_p (nrv))
3447 return;
3449 /* Prune also the candidates that are referenced by nested functions. */
3450 node = cgraph_node::get_create (fndecl);
3451 for (node = node->nested; node; node = node->next_nested)
3452 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), prune_nrv_r,
3453 &data);
3454 if (bitmap_empty_p (nrv))
3455 return;
3457 /* Extract a set of NRVs with non-overlapping live ranges. */
3458 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3459 return;
3461 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3462 data.nrv = nrv;
3463 data.result = DECL_RESULT (fndecl);
3464 data.gnat_ret = gnat_ret;
3465 data.visited = new hash_set<tree>;
3466 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl)))
3467 func = finalize_nrv_unc_r;
3468 else
3469 func = finalize_nrv_r;
3470 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
3471 delete data.visited;
3474 /* Return true if RET_VAL can be used as a Named Return Value for the
3475 anonymous return object RET_OBJ. */
3477 static bool
3478 return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3480 if (TREE_CODE (ret_val) != VAR_DECL)
3481 return false;
3483 if (TREE_THIS_VOLATILE (ret_val))
3484 return false;
3486 if (DECL_CONTEXT (ret_val) != current_function_decl)
3487 return false;
3489 if (TREE_STATIC (ret_val))
3490 return false;
3492 if (TREE_ADDRESSABLE (ret_val))
3493 return false;
3495 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
3496 return false;
3498 return true;
3501 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3502 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3503 around RESULT_OBJ, which may be null in this case. */
3505 static tree
3506 build_return_expr (tree ret_obj, tree ret_val)
3508 tree result_expr;
3510 if (ret_val)
3512 /* The gimplifier explicitly enforces the following invariant:
3514 RETURN_EXPR
3516 INIT_EXPR
3519 RET_OBJ ...
3521 As a consequence, type consistency dictates that we use the type
3522 of the RET_OBJ as the operation type. */
3523 tree operation_type = TREE_TYPE (ret_obj);
3525 /* Convert the right operand to the operation type. Note that this is
3526 the transformation applied in the INIT_EXPR case of build_binary_op,
3527 with the assumption that the type cannot involve a placeholder. */
3528 if (operation_type != TREE_TYPE (ret_val))
3529 ret_val = convert (operation_type, ret_val);
3531 /* We always can use an INIT_EXPR for the return object. */
3532 result_expr = build2 (INIT_EXPR, void_type_node, ret_obj, ret_val);
3534 /* If the function returns an aggregate type, find out whether this is
3535 a candidate for Named Return Value. If so, record it. Otherwise,
3536 if this is an expression of some kind, record it elsewhere. */
3537 if (optimize
3538 && AGGREGATE_TYPE_P (operation_type)
3539 && !TYPE_IS_FAT_POINTER_P (operation_type)
3540 && TYPE_MODE (operation_type) == BLKmode
3541 && aggregate_value_p (operation_type, current_function_decl))
3543 /* Recognize the temporary created for a return value with variable
3544 size in Call_to_gnu. We want to eliminate it if possible. */
3545 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3546 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3547 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3548 == TREE_OPERAND (ret_val, 1))
3549 ret_val = TREE_OPERAND (ret_val, 1);
3551 /* Strip useless conversions around the return value. */
3552 if (gnat_useless_type_conversion (ret_val))
3553 ret_val = TREE_OPERAND (ret_val, 0);
3555 /* Now apply the test to the return value. */
3556 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3558 if (!f_named_ret_val)
3559 f_named_ret_val = BITMAP_GGC_ALLOC ();
3560 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3563 /* Note that we need not care about CONSTRUCTORs here, as they are
3564 totally transparent given the read-compose-write semantics of
3565 assignments from CONSTRUCTORs. */
3566 else if (EXPR_P (ret_val))
3567 vec_safe_push (f_other_ret_val, ret_val);
3570 else
3571 result_expr = ret_obj;
3573 return build1 (RETURN_EXPR, void_type_node, result_expr);
3576 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3577 don't return anything. */
3579 static void
3580 Subprogram_Body_to_gnu (Node_Id gnat_node)
3582 /* Defining identifier of a parameter to the subprogram. */
3583 Entity_Id gnat_param;
3584 /* The defining identifier for the subprogram body. Note that if a
3585 specification has appeared before for this body, then the identifier
3586 occurring in that specification will also be a defining identifier and all
3587 the calls to this subprogram will point to that specification. */
3588 Entity_Id gnat_subprog_id
3589 = (Present (Corresponding_Spec (gnat_node))
3590 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
3591 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3592 tree gnu_subprog_decl;
3593 /* Its RESULT_DECL node. */
3594 tree gnu_result_decl;
3595 /* Its FUNCTION_TYPE node. */
3596 tree gnu_subprog_type;
3597 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3598 tree gnu_cico_list;
3599 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3600 tree gnu_return_var_elmt = NULL_TREE;
3601 tree gnu_result;
3602 location_t locus;
3603 struct language_function *gnu_subprog_language;
3604 vec<parm_attr, va_gc> *cache;
3606 /* If this is a generic object or if it has been eliminated,
3607 ignore it. */
3608 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
3609 || Ekind (gnat_subprog_id) == E_Generic_Function
3610 || Is_Eliminated (gnat_subprog_id))
3611 return;
3613 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3614 the already-elaborated tree node. However, if this subprogram had its
3615 elaboration deferred, we will already have made a tree node for it. So
3616 treat it as not being defined in that case. Such a subprogram cannot
3617 have an address clause or a freeze node, so this test is safe, though it
3618 does disable some otherwise-useful error checking. */
3619 gnu_subprog_decl
3620 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
3621 Acts_As_Spec (gnat_node)
3622 && !present_gnu_tree (gnat_subprog_id));
3623 gnu_result_decl = DECL_RESULT (gnu_subprog_decl);
3624 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
3625 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3626 if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node)
3627 gnu_return_var_elmt = gnu_cico_list;
3629 /* If the function returns by invisible reference, make it explicit in the
3630 function body. See gnat_to_gnu_entity, E_Subprogram_Type case. */
3631 if (TREE_ADDRESSABLE (gnu_subprog_type))
3633 TREE_TYPE (gnu_result_decl)
3634 = build_reference_type (TREE_TYPE (gnu_result_decl));
3635 relayout_decl (gnu_result_decl);
3638 /* Set the line number in the decl to correspond to that of the body. */
3639 Sloc_to_locus (Sloc (gnat_node), &locus);
3640 DECL_SOURCE_LOCATION (gnu_subprog_decl) = locus;
3642 /* Initialize the information structure for the function. */
3643 allocate_struct_function (gnu_subprog_decl, false);
3644 gnu_subprog_language = ggc_cleared_alloc<language_function> ();
3645 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language = gnu_subprog_language;
3646 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->function_start_locus = locus;
3647 set_cfun (NULL);
3649 begin_subprog_body (gnu_subprog_decl);
3651 /* If there are copy-in/copy-out parameters, we need to ensure that they are
3652 properly copied out by the return statement. We do this by making a new
3653 block and converting any return into a goto to a label at the end of the
3654 block. */
3655 if (gnu_cico_list)
3657 tree gnu_return_var = NULL_TREE;
3659 vec_safe_push (gnu_return_label_stack,
3660 create_artificial_label (input_location));
3662 start_stmt_group ();
3663 gnat_pushlevel ();
3665 /* If this is a function with copy-in/copy-out parameters and which does
3666 not return by invisible reference, we also need a variable for the
3667 return value to be placed. */
3668 if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type))
3670 tree gnu_return_type
3671 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3673 gnu_return_var
3674 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
3675 gnu_return_type, NULL_TREE, false, false,
3676 false, false, NULL, gnat_subprog_id);
3677 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3680 vec_safe_push (gnu_return_var_stack, gnu_return_var);
3682 /* See whether there are parameters for which we don't have a GCC tree
3683 yet. These must be Out parameters. Make a VAR_DECL for them and
3684 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3685 We can match up the entries because TYPE_CI_CO_LIST is in the order
3686 of the parameters. */
3687 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3688 Present (gnat_param);
3689 gnat_param = Next_Formal_With_Extras (gnat_param))
3690 if (!present_gnu_tree (gnat_param))
3692 tree gnu_cico_entry = gnu_cico_list;
3693 tree gnu_decl;
3695 /* Skip any entries that have been already filled in; they must
3696 correspond to In Out parameters. */
3697 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3698 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3700 /* Do any needed dereferences for by-ref objects. */
3701 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, 1);
3702 gcc_assert (DECL_P (gnu_decl));
3703 if (DECL_BY_REF_P (gnu_decl))
3704 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3706 /* Do any needed references for padded types. */
3707 TREE_VALUE (gnu_cico_entry)
3708 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
3711 else
3712 vec_safe_push (gnu_return_label_stack, NULL_TREE);
3714 /* Get a tree corresponding to the code for the subprogram. */
3715 start_stmt_group ();
3716 gnat_pushlevel ();
3718 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3720 /* Generate the code of the subprogram itself. A return statement will be
3721 present and any Out parameters will be handled there. */
3722 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3723 gnat_poplevel ();
3724 gnu_result = end_stmt_group ();
3726 /* If we populated the parameter attributes cache, we need to make sure that
3727 the cached expressions are evaluated on all the possible paths leading to
3728 their uses. So we force their evaluation on entry of the function. */
3729 cache = gnu_subprog_language->parm_attr_cache;
3730 if (cache)
3732 struct parm_attr_d *pa;
3733 int i;
3735 start_stmt_group ();
3737 FOR_EACH_VEC_ELT (*cache, i, pa)
3739 if (pa->first)
3740 add_stmt_with_node_force (pa->first, gnat_node);
3741 if (pa->last)
3742 add_stmt_with_node_force (pa->last, gnat_node);
3743 if (pa->length)
3744 add_stmt_with_node_force (pa->length, gnat_node);
3747 add_stmt (gnu_result);
3748 gnu_result = end_stmt_group ();
3750 gnu_subprog_language->parm_attr_cache = NULL;
3753 /* If we are dealing with a return from an Ada procedure with parameters
3754 passed by copy-in/copy-out, we need to return a record containing the
3755 final values of these parameters. If the list contains only one entry,
3756 return just that entry though.
3758 For a full description of the copy-in/copy-out parameter mechanism, see
3759 the part of the gnat_to_gnu_entity routine dealing with the translation
3760 of subprograms.
3762 We need to make a block that contains the definition of that label and
3763 the copying of the return value. It first contains the function, then
3764 the label and copy statement. */
3765 if (gnu_cico_list)
3767 const Node_Id gnat_end_label
3768 = End_Label (Handled_Statement_Sequence (gnat_node));
3770 gnu_return_var_stack->pop ();
3772 add_stmt (gnu_result);
3773 add_stmt (build1 (LABEL_EXPR, void_type_node,
3774 gnu_return_label_stack->last ()));
3776 /* If this is a function which returns by invisible reference, the
3777 return value has already been dealt with at the return statements,
3778 so we only need to indirectly copy out the parameters. */
3779 if (TREE_ADDRESSABLE (gnu_subprog_type))
3781 tree gnu_ret_deref
3782 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl);
3783 tree t;
3785 gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node);
3787 for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t))
3789 tree gnu_field_deref
3790 = build_component_ref (gnu_ret_deref, NULL_TREE,
3791 TREE_PURPOSE (t), true);
3792 gnu_result = build2 (MODIFY_EXPR, void_type_node,
3793 gnu_field_deref, TREE_VALUE (t));
3794 add_stmt_with_node (gnu_result, gnat_end_label);
3798 /* Otherwise, if this is a procedure or a function which does not return
3799 by invisible reference, we can do a direct block-copy out. */
3800 else
3802 tree gnu_retval;
3804 if (list_length (gnu_cico_list) == 1)
3805 gnu_retval = TREE_VALUE (gnu_cico_list);
3806 else
3807 gnu_retval
3808 = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
3809 gnu_cico_list);
3811 gnu_result = build_return_expr (gnu_result_decl, gnu_retval);
3812 add_stmt_with_node (gnu_result, gnat_end_label);
3815 gnat_poplevel ();
3816 gnu_result = end_stmt_group ();
3819 gnu_return_label_stack->pop ();
3821 /* Attempt setting the end_locus of our GCC body tree, typically a
3822 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3823 declaration tree. */
3824 set_end_locus_from_node (gnu_result, gnat_node);
3825 set_end_locus_from_node (gnu_subprog_decl, gnat_node);
3827 /* On SEH targets, install an exception handler around the main entry
3828 point to catch unhandled exceptions. */
3829 if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
3830 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
3832 tree t;
3833 tree etype;
3835 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
3836 1, integer_zero_node);
3837 t = build_call_n_expr (unhandled_except_decl, 1, t);
3839 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
3840 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
3842 t = build2 (CATCH_EXPR, void_type_node, etype, t);
3843 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
3844 gnu_result, t);
3847 end_subprog_body (gnu_result);
3849 /* Finally annotate the parameters and disconnect the trees for parameters
3850 that we have turned into variables since they are now unusable. */
3851 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3852 Present (gnat_param);
3853 gnat_param = Next_Formal_With_Extras (gnat_param))
3855 tree gnu_param = get_gnu_tree (gnat_param);
3856 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
3858 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
3859 DECL_BY_REF_P (gnu_param));
3861 if (is_var_decl)
3862 save_gnu_tree (gnat_param, NULL_TREE, false);
3865 /* Disconnect the variable created for the return value. */
3866 if (gnu_return_var_elmt)
3867 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
3869 /* If the function returns an aggregate type and we have candidates for
3870 a Named Return Value, finalize the optimization. */
3871 if (optimize && gnu_subprog_language->named_ret_val)
3873 finalize_nrv (gnu_subprog_decl,
3874 gnu_subprog_language->named_ret_val,
3875 gnu_subprog_language->other_ret_val,
3876 gnu_subprog_language->gnat_ret);
3877 gnu_subprog_language->named_ret_val = NULL;
3878 gnu_subprog_language->other_ret_val = NULL;
3881 /* If this is an inlined external function that has been marked uninlinable,
3882 drop the body and stop there. Otherwise compile the body. */
3883 if (DECL_EXTERNAL (gnu_subprog_decl) && DECL_UNINLINABLE (gnu_subprog_decl))
3884 DECL_SAVED_TREE (gnu_subprog_decl) = NULL_TREE;
3885 else
3886 rest_of_subprog_body_compilation (gnu_subprog_decl);
3889 /* Return true if GNAT_NODE requires atomic synchronization. */
3891 static bool
3892 atomic_sync_required_p (Node_Id gnat_node)
3894 const Node_Id gnat_parent = Parent (gnat_node);
3895 Node_Kind kind;
3896 unsigned char attr_id;
3898 /* First, scan the node to find the Atomic_Sync_Required flag. */
3899 kind = Nkind (gnat_node);
3900 if (kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
3902 gnat_node = Expression (gnat_node);
3903 kind = Nkind (gnat_node);
3906 switch (kind)
3908 case N_Expanded_Name:
3909 case N_Explicit_Dereference:
3910 case N_Identifier:
3911 case N_Indexed_Component:
3912 case N_Selected_Component:
3913 if (!Atomic_Sync_Required (gnat_node))
3914 return false;
3915 break;
3917 default:
3918 return false;
3921 /* Then, scan the parent to find out cases where the flag is irrelevant. */
3922 kind = Nkind (gnat_parent);
3923 switch (kind)
3925 case N_Attribute_Reference:
3926 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
3927 /* Do not mess up machine code insertions. */
3928 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
3929 return false;
3930 break;
3932 case N_Object_Renaming_Declaration:
3933 /* Do not generate a function call as a renamed object. */
3934 return false;
3936 default:
3937 break;
3940 return true;
3943 /* Create a temporary variable with PREFIX and TYPE, and return it. */
3945 static tree
3946 create_temporary (const char *prefix, tree type)
3948 tree gnu_temp = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
3949 type, NULL_TREE, false, false, false, false,
3950 NULL, Empty);
3951 DECL_ARTIFICIAL (gnu_temp) = 1;
3952 DECL_IGNORED_P (gnu_temp) = 1;
3954 return gnu_temp;
3957 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
3958 Put the initialization statement into GNU_INIT_STMT and annotate it with
3959 the SLOC of GNAT_NODE. Return the temporary variable. */
3961 static tree
3962 create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
3963 Node_Id gnat_node)
3965 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
3967 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
3968 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
3970 return gnu_temp;
3973 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
3974 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
3975 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
3976 If GNU_TARGET is non-null, this must be a function call on the RHS of a
3977 N_Assignment_Statement and the result is to be placed into that object.
3978 If, in addition, ATOMIC_SYNC is true, then the assignment to GNU_TARGET
3979 requires atomic synchronization. */
3981 static tree
3982 Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
3983 bool atomic_sync)
3985 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
3986 const bool returning_value = (function_call && !gnu_target);
3987 /* The GCC node corresponding to the GNAT subprogram name. This can either
3988 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
3989 or an indirect reference expression (an INDIRECT_REF node) pointing to a
3990 subprogram. */
3991 tree gnu_subprog = gnat_to_gnu (Name (gnat_node));
3992 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
3993 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
3994 /* The return type of the FUNCTION_TYPE. */
3995 tree gnu_result_type = TREE_TYPE (gnu_subprog_type);
3996 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog);
3997 vec<tree, va_gc> *gnu_actual_vec = NULL;
3998 tree gnu_name_list = NULL_TREE;
3999 tree gnu_stmt_list = NULL_TREE;
4000 tree gnu_after_list = NULL_TREE;
4001 tree gnu_retval = NULL_TREE;
4002 tree gnu_call, gnu_result;
4003 bool went_into_elab_proc = false;
4004 bool pushed_binding_level = false;
4005 Entity_Id gnat_formal;
4006 Node_Id gnat_actual;
4008 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
4010 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
4011 all our args first. */
4012 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL && DECL_STUBBED_P (gnu_subprog))
4014 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
4015 gnat_node, N_Raise_Program_Error);
4017 for (gnat_actual = First_Actual (gnat_node);
4018 Present (gnat_actual);
4019 gnat_actual = Next_Actual (gnat_actual))
4020 add_stmt (gnat_to_gnu (gnat_actual));
4022 if (returning_value)
4024 *gnu_result_type_p = gnu_result_type;
4025 return build1 (NULL_EXPR, gnu_result_type, call_expr);
4028 return call_expr;
4031 /* For a call to a nested function, check the inlining status. */
4032 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL
4033 && decl_function_context (gnu_subprog))
4034 check_inlining_for_nested_subprog (gnu_subprog);
4036 /* The only way we can be making a call via an access type is if Name is an
4037 explicit dereference. In that case, get the list of formal args from the
4038 type the access type is pointing to. Otherwise, get the formals from the
4039 entity being called. */
4040 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4041 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4042 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
4043 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
4044 gnat_formal = Empty;
4045 else
4046 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4048 /* The lifetime of the temporaries created for the call ends right after the
4049 return value is copied, so we can give them the scope of the elaboration
4050 routine at top level. */
4051 if (!current_function_decl)
4053 current_function_decl = get_elaboration_procedure ();
4054 went_into_elab_proc = true;
4057 /* First, create the temporary for the return value when:
4059 1. There is no target and the function has copy-in/copy-out parameters,
4060 because we need to preserve the return value before copying back the
4061 parameters.
4063 2. There is no target and this is not an object declaration, and the
4064 return type has variable size, because in these cases the gimplifier
4065 cannot create the temporary.
4067 3. There is a target and it is a slice or an array with fixed size,
4068 and the return type has variable size, because the gimplifier
4069 doesn't handle these cases.
4071 This must be done before we push a binding level around the call, since
4072 we will pop it before copying the return value. */
4073 if (function_call
4074 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
4075 || (!gnu_target
4076 && Nkind (Parent (gnat_node)) != N_Object_Declaration
4077 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)
4078 || (gnu_target
4079 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
4080 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
4081 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
4082 == INTEGER_CST))
4083 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)))
4084 gnu_retval = create_temporary ("R", gnu_result_type);
4086 /* Create the list of the actual parameters as GCC expects it, namely a
4087 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
4088 is an expression and the TREE_PURPOSE field is null. But skip Out
4089 parameters not passed by reference and that need not be copied in. */
4090 for (gnat_actual = First_Actual (gnat_node);
4091 Present (gnat_actual);
4092 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4093 gnat_actual = Next_Actual (gnat_actual))
4095 Entity_Id gnat_formal_type = Etype (gnat_formal);
4096 tree gnu_formal = present_gnu_tree (gnat_formal)
4097 ? get_gnu_tree (gnat_formal) : NULL_TREE;
4098 tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type);
4099 const bool is_true_formal_parm
4100 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
4101 const bool is_by_ref_formal_parm
4102 = is_true_formal_parm
4103 && (DECL_BY_REF_P (gnu_formal)
4104 || DECL_BY_COMPONENT_PTR_P (gnu_formal));
4105 /* In the Out or In Out case, we must suppress conversions that yield
4106 an lvalue but can nevertheless cause the creation of a temporary,
4107 because we need the real object in this case, either to pass its
4108 address if it's passed by reference or as target of the back copy
4109 done after the call if it uses the copy-in/copy-out mechanism.
4110 We do it in the In case too, except for an unchecked conversion
4111 to an elementary type or a constrained composite type because it
4112 alone can cause the actual to be misaligned and the addressability
4113 test is applied to the real object. */
4114 const bool suppress_type_conversion
4115 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
4116 && (Ekind (gnat_formal) != E_In_Parameter
4117 || (Is_Composite_Type (Underlying_Type (gnat_formal_type))
4118 && !Is_Constrained (Underlying_Type (gnat_formal_type)))))
4119 || (Nkind (gnat_actual) == N_Type_Conversion
4120 && Is_Composite_Type (Underlying_Type (gnat_formal_type))));
4121 Node_Id gnat_name = suppress_type_conversion
4122 ? Expression (gnat_actual) : gnat_actual;
4123 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
4124 tree gnu_actual;
4126 /* If it's possible we may need to use this expression twice, make sure
4127 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4128 to force side-effects before the call.
4129 ??? This is more conservative than we need since we don't need to do
4130 this for pass-by-ref with no conversion. */
4131 if (Ekind (gnat_formal) != E_In_Parameter)
4132 gnu_name = gnat_stabilize_reference (gnu_name, true, NULL);
4134 /* If we are passing a non-addressable parameter by reference, pass the
4135 address of a copy. In the Out or In Out case, set up to copy back
4136 out after the call. */
4137 if (is_by_ref_formal_parm
4138 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
4139 && !addressable_p (gnu_name, gnu_name_type))
4141 bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
4142 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4144 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4145 but sort of an instantiation for them. */
4146 if (TREE_CODE (gnu_name) == CONSTRUCTOR)
4149 /* If the type is passed by reference, a copy is not allowed. */
4150 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type))
4151 post_error ("misaligned actual cannot be passed by reference",
4152 gnat_actual);
4154 /* For users of Starlet we issue a warning because the interface
4155 apparently assumes that by-ref parameters outlive the procedure
4156 invocation. The code still will not work as intended, but we
4157 cannot do much better since low-level parts of the back-end
4158 would allocate temporaries at will because of the misalignment
4159 if we did not do so here. */
4160 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
4162 post_error
4163 ("?possible violation of implicit assumption", gnat_actual);
4164 post_error_ne
4165 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
4166 Entity (Name (gnat_node)));
4167 post_error_ne ("?because of misalignment of &", gnat_actual,
4168 gnat_formal);
4171 /* If the actual type of the object is already the nominal type,
4172 we have nothing to do, except if the size is self-referential
4173 in which case we'll remove the unpadding below. */
4174 if (TREE_TYPE (gnu_name) == gnu_name_type
4175 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4178 /* Otherwise remove the unpadding from all the objects. */
4179 else if (TREE_CODE (gnu_name) == COMPONENT_REF
4180 && TYPE_IS_PADDING_P
4181 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
4182 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
4184 /* Otherwise convert to the nominal type of the object if needed.
4185 There are several cases in which we need to make the temporary
4186 using this type instead of the actual type of the object when
4187 they are distinct, because the expectations of the callee would
4188 otherwise not be met:
4189 - if it's a justified modular type,
4190 - if the actual type is a smaller form of it,
4191 - if it's a smaller form of the actual type. */
4192 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4193 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4194 || smaller_form_type_p (TREE_TYPE (gnu_name),
4195 gnu_name_type)))
4196 || (INTEGRAL_TYPE_P (gnu_name_type)
4197 && smaller_form_type_p (gnu_name_type,
4198 TREE_TYPE (gnu_name))))
4199 gnu_name = convert (gnu_name_type, gnu_name);
4201 /* If this is an In Out or Out parameter and we're returning a value,
4202 we need to create a temporary for the return value because we must
4203 preserve it before copying back at the very end. */
4204 if (!in_param && returning_value && !gnu_retval)
4205 gnu_retval = create_temporary ("R", gnu_result_type);
4207 /* If we haven't pushed a binding level, push a new one. This will
4208 narrow the lifetime of the temporary we are about to make as much
4209 as possible. The drawback is that we'd need to create a temporary
4210 for the return value, if any (see comment before the loop). So do
4211 it only when this temporary was already created just above. */
4212 if (!pushed_binding_level && !(in_param && returning_value))
4214 start_stmt_group ();
4215 gnat_pushlevel ();
4216 pushed_binding_level = true;
4219 /* Create an explicit temporary holding the copy. */
4220 gnu_temp
4221 = create_init_temporary ("A", gnu_name, &gnu_stmt, gnat_actual);
4223 /* But initialize it on the fly like for an implicit temporary as
4224 we aren't necessarily having a statement list. */
4225 gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt,
4226 gnu_temp);
4228 /* Set up to move the copy back to the original if needed. */
4229 if (!in_param)
4231 /* If the original is a COND_EXPR whose first arm isn't meant to
4232 be further used, just deal with the second arm. This is very
4233 likely the conditional expression built for a check. */
4234 if (TREE_CODE (gnu_orig) == COND_EXPR
4235 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4236 && integer_zerop
4237 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4238 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4240 gnu_stmt
4241 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp);
4242 set_expr_location_from_node (gnu_stmt, gnat_node);
4244 append_to_statement_list (gnu_stmt, &gnu_after_list);
4248 /* Start from the real object and build the actual. */
4249 gnu_actual = gnu_name;
4251 /* If this is an atomic access of an In or In Out parameter for which
4252 synchronization is required, build the atomic load. */
4253 if (is_true_formal_parm
4254 && !is_by_ref_formal_parm
4255 && Ekind (gnat_formal) != E_Out_Parameter
4256 && atomic_sync_required_p (gnat_actual))
4257 gnu_actual = build_atomic_load (gnu_actual);
4259 /* If this was a procedure call, we may not have removed any padding.
4260 So do it here for the part we will use as an input, if any. */
4261 if (Ekind (gnat_formal) != E_Out_Parameter
4262 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4263 gnu_actual
4264 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4266 /* Put back the conversion we suppressed above in the computation of the
4267 real object. And even if we didn't suppress any conversion there, we
4268 may have suppressed a conversion to the Etype of the actual earlier,
4269 since the parent is a procedure call, so put it back here. */
4270 if (suppress_type_conversion
4271 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4272 gnu_actual
4273 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
4274 gnu_actual, No_Truncation (gnat_actual));
4275 else
4276 gnu_actual
4277 = convert (gnat_to_gnu_type (Etype (gnat_actual)), gnu_actual);
4279 /* Make sure that the actual is in range of the formal's type. */
4280 if (Ekind (gnat_formal) != E_Out_Parameter
4281 && Do_Range_Check (gnat_actual))
4282 gnu_actual
4283 = emit_range_check (gnu_actual, gnat_formal_type, gnat_actual);
4285 /* Unless this is an In parameter, we must remove any justified modular
4286 building from GNU_NAME to get an lvalue. */
4287 if (Ekind (gnat_formal) != E_In_Parameter
4288 && TREE_CODE (gnu_name) == CONSTRUCTOR
4289 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
4290 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
4291 gnu_name
4292 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))), gnu_name);
4294 /* First see if the parameter is passed by reference. */
4295 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
4297 if (Ekind (gnat_formal) != E_In_Parameter)
4299 /* In Out or Out parameters passed by reference don't use the
4300 copy-in/copy-out mechanism so the address of the real object
4301 must be passed to the function. */
4302 gnu_actual = gnu_name;
4304 /* If we have a padded type, be sure we've removed padding. */
4305 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4306 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
4307 gnu_actual);
4309 /* If we have the constructed subtype of an aliased object
4310 with an unconstrained nominal subtype, the type of the
4311 actual includes the template, although it is formally
4312 constrained. So we need to convert it back to the real
4313 constructed subtype to retrieve the constrained part
4314 and takes its address. */
4315 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4316 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
4317 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
4318 && Is_Array_Type (Underlying_Type (Etype (gnat_actual))))
4319 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4320 gnu_actual);
4323 /* There is no need to convert the actual to the formal's type before
4324 taking its address. The only exception is for unconstrained array
4325 types because of the way we build fat pointers. */
4326 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4328 /* Put back a view conversion for In Out or Out parameters. */
4329 if (Ekind (gnat_formal) != E_In_Parameter)
4330 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4331 gnu_actual);
4332 gnu_actual = convert (gnu_formal_type, gnu_actual);
4335 /* The symmetry of the paths to the type of an entity is broken here
4336 since arguments don't know that they will be passed by ref. */
4337 gnu_formal_type = TREE_TYPE (gnu_formal);
4338 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4341 /* Then see if the parameter is an array passed to a foreign convention
4342 subprogram. */
4343 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
4345 gnu_formal_type = TREE_TYPE (gnu_formal);
4346 gnu_actual = maybe_implicit_deref (gnu_actual);
4347 gnu_actual = maybe_unconstrained_array (gnu_actual);
4349 if (TYPE_IS_PADDING_P (gnu_formal_type))
4351 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
4352 gnu_actual = convert (gnu_formal_type, gnu_actual);
4355 /* Take the address of the object and convert to the proper pointer
4356 type. We'd like to actually compute the address of the beginning
4357 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
4358 possibility that the ARRAY_REF might return a constant and we'd be
4359 getting the wrong address. Neither approach is exactly correct,
4360 but this is the most likely to work in all cases. */
4361 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4364 /* Otherwise the parameter is passed by copy. */
4365 else
4367 tree gnu_size;
4369 if (Ekind (gnat_formal) != E_In_Parameter)
4370 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
4372 /* If we didn't create a PARM_DECL for the formal, this means that
4373 it is an Out parameter not passed by reference and that need not
4374 be copied in. In this case, the value of the actual need not be
4375 read. However, we still need to make sure that its side-effects
4376 are evaluated before the call, so we evaluate its address. */
4377 if (!is_true_formal_parm)
4379 if (TREE_SIDE_EFFECTS (gnu_name))
4381 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
4382 append_to_statement_list (addr, &gnu_stmt_list);
4384 continue;
4387 gnu_actual = convert (gnu_formal_type, gnu_actual);
4389 /* If this is 'Null_Parameter, pass a zero even though we are
4390 dereferencing it. */
4391 if (TREE_CODE (gnu_actual) == INDIRECT_REF
4392 && TREE_PRIVATE (gnu_actual)
4393 && (gnu_size = TYPE_SIZE (TREE_TYPE (gnu_actual)))
4394 && TREE_CODE (gnu_size) == INTEGER_CST
4395 && compare_tree_int (gnu_size, BITS_PER_WORD) <= 0)
4396 gnu_actual
4397 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
4398 convert (gnat_type_for_size
4399 (TREE_INT_CST_LOW (gnu_size), 1),
4400 integer_zero_node),
4401 false);
4402 else
4403 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
4406 vec_safe_push (gnu_actual_vec, gnu_actual);
4409 gnu_call
4410 = build_call_vec (gnu_result_type, gnu_subprog_addr, gnu_actual_vec);
4411 set_expr_location_from_node (gnu_call, gnat_node);
4413 /* If we have created a temporary for the return value, initialize it. */
4414 if (gnu_retval)
4416 tree gnu_stmt
4417 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
4418 set_expr_location_from_node (gnu_stmt, gnat_node);
4419 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4420 gnu_call = gnu_retval;
4423 /* If this is a subprogram with copy-in/copy-out parameters, we need to
4424 unpack the valued returned from the function into the In Out or Out
4425 parameters. We deal with the function return (if this is an Ada
4426 function) below. */
4427 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4429 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
4430 copy-out parameters. */
4431 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
4432 const int length = list_length (gnu_cico_list);
4434 /* The call sequence must contain one and only one call, even though the
4435 function is pure. Save the result into a temporary if needed. */
4436 if (length > 1)
4438 if (!gnu_retval)
4440 tree gnu_stmt;
4441 /* If we haven't pushed a binding level, push a new one. This
4442 will narrow the lifetime of the temporary we are about to
4443 make as much as possible. */
4444 if (!pushed_binding_level)
4446 start_stmt_group ();
4447 gnat_pushlevel ();
4448 pushed_binding_level = true;
4450 gnu_call
4451 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
4452 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4455 gnu_name_list = nreverse (gnu_name_list);
4458 /* The first entry is for the actual return value if this is a
4459 function, so skip it. */
4460 if (function_call)
4461 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4463 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4464 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4465 else
4466 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4468 for (gnat_actual = First_Actual (gnat_node);
4469 Present (gnat_actual);
4470 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4471 gnat_actual = Next_Actual (gnat_actual))
4472 /* If we are dealing with a copy-in/copy-out parameter, we must
4473 retrieve its value from the record returned in the call. */
4474 if (!(present_gnu_tree (gnat_formal)
4475 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4476 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
4477 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))))
4478 && Ekind (gnat_formal) != E_In_Parameter)
4480 /* Get the value to assign to this Out or In Out parameter. It is
4481 either the result of the function if there is only a single such
4482 parameter or the appropriate field from the record returned. */
4483 tree gnu_result
4484 = length == 1
4485 ? gnu_call
4486 : build_component_ref (gnu_call, NULL_TREE,
4487 TREE_PURPOSE (gnu_cico_list), false);
4489 /* If the actual is a conversion, get the inner expression, which
4490 will be the real destination, and convert the result to the
4491 type of the actual parameter. */
4492 tree gnu_actual
4493 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
4495 /* If the result is a padded type, remove the padding. */
4496 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4497 gnu_result
4498 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4499 gnu_result);
4501 /* If the actual is a type conversion, the real target object is
4502 denoted by the inner Expression and we need to convert the
4503 result to the associated type.
4504 We also need to convert our gnu assignment target to this type
4505 if the corresponding GNU_NAME was constructed from the GNAT
4506 conversion node and not from the inner Expression. */
4507 if (Nkind (gnat_actual) == N_Type_Conversion)
4509 gnu_result
4510 = convert_with_check
4511 (Etype (Expression (gnat_actual)), gnu_result,
4512 Do_Overflow_Check (gnat_actual),
4513 Do_Range_Check (Expression (gnat_actual)),
4514 Float_Truncate (gnat_actual), gnat_actual);
4516 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
4517 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
4520 /* Unchecked conversions as actuals for Out parameters are not
4521 allowed in user code because they are not variables, but do
4522 occur in front-end expansions. The associated GNU_NAME is
4523 always obtained from the inner expression in such cases. */
4524 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4525 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
4526 gnu_result,
4527 No_Truncation (gnat_actual));
4528 else
4530 if (Do_Range_Check (gnat_actual))
4531 gnu_result
4532 = emit_range_check (gnu_result, Etype (gnat_actual),
4533 gnat_actual);
4535 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
4536 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
4537 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
4540 if (atomic_sync_required_p (gnat_actual))
4541 gnu_result = build_atomic_store (gnu_actual, gnu_result);
4542 else
4543 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4544 gnu_actual, gnu_result);
4545 set_expr_location_from_node (gnu_result, gnat_node);
4546 append_to_statement_list (gnu_result, &gnu_stmt_list);
4547 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4548 gnu_name_list = TREE_CHAIN (gnu_name_list);
4552 /* If this is a function call, the result is the call expression unless a
4553 target is specified, in which case we copy the result into the target
4554 and return the assignment statement. */
4555 if (function_call)
4557 /* If this is a function with copy-in/copy-out parameters, extract the
4558 return value from it and update the return type. */
4559 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4561 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
4562 gnu_call = build_component_ref (gnu_call, NULL_TREE,
4563 TREE_PURPOSE (gnu_elmt), false);
4564 gnu_result_type = TREE_TYPE (gnu_call);
4567 /* If the function returns an unconstrained array or by direct reference,
4568 we have to dereference the pointer. */
4569 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type)
4570 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
4571 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
4573 if (gnu_target)
4575 Node_Id gnat_parent = Parent (gnat_node);
4576 enum tree_code op_code;
4578 /* If range check is needed, emit code to generate it. */
4579 if (Do_Range_Check (gnat_node))
4580 gnu_call
4581 = emit_range_check (gnu_call, Etype (Name (gnat_parent)),
4582 gnat_parent);
4584 /* ??? If the return type has variable size, then force the return
4585 slot optimization as we would not be able to create a temporary.
4586 Likewise if it was unconstrained as we would copy too much data.
4587 That's what has been done historically. */
4588 if (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
4589 || (TYPE_IS_PADDING_P (gnu_result_type)
4590 && CONTAINS_PLACEHOLDER_P
4591 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_result_type))))))
4592 op_code = INIT_EXPR;
4593 else
4594 op_code = MODIFY_EXPR;
4596 if (atomic_sync)
4597 gnu_call = build_atomic_store (gnu_target, gnu_call);
4598 else
4599 gnu_call
4600 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
4601 set_expr_location_from_node (gnu_call, gnat_parent);
4602 append_to_statement_list (gnu_call, &gnu_stmt_list);
4604 else
4605 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
4608 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4609 parameters, the result is just the call statement. */
4610 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
4611 append_to_statement_list (gnu_call, &gnu_stmt_list);
4613 /* Finally, add the copy back statements, if any. */
4614 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
4616 if (went_into_elab_proc)
4617 current_function_decl = NULL_TREE;
4619 /* If we have pushed a binding level, pop it and finish up the enclosing
4620 statement group. */
4621 if (pushed_binding_level)
4623 add_stmt (gnu_stmt_list);
4624 gnat_poplevel ();
4625 gnu_result = end_stmt_group ();
4628 /* Otherwise, retrieve the statement list, if any. */
4629 else if (gnu_stmt_list)
4630 gnu_result = gnu_stmt_list;
4632 /* Otherwise, just return the call expression. */
4633 else
4634 return gnu_call;
4636 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4637 But first simplify if we have only one statement in the list. */
4638 if (returning_value)
4640 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
4641 if (first == last)
4642 gnu_result = first;
4643 gnu_result
4644 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
4647 return gnu_result;
4650 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4651 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4653 static tree
4654 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
4656 tree gnu_jmpsave_decl = NULL_TREE;
4657 tree gnu_jmpbuf_decl = NULL_TREE;
4658 /* If just annotating, ignore all EH and cleanups. */
4659 bool gcc_zcx = (!type_annotate_only
4660 && Present (Exception_Handlers (gnat_node))
4661 && Exception_Mechanism == Back_End_Exceptions);
4662 bool setjmp_longjmp
4663 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
4664 && Exception_Mechanism == Setjmp_Longjmp);
4665 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
4666 bool binding_for_block = (at_end || gcc_zcx || setjmp_longjmp);
4667 tree gnu_inner_block; /* The statement(s) for the block itself. */
4668 tree gnu_result;
4669 tree gnu_expr;
4670 Node_Id gnat_temp;
4671 /* Node providing the sloc for the cleanup actions. */
4672 Node_Id gnat_cleanup_loc_node = (Present (End_Label (gnat_node)) ?
4673 End_Label (gnat_node) :
4674 gnat_node);
4676 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
4677 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
4678 add_cleanup, and when we leave the binding, end_stmt_group will create
4679 the TRY_FINALLY_EXPR.
4681 ??? The region level calls down there have been specifically put in place
4682 for a ZCX context and currently the order in which things are emitted
4683 (region/handlers) is different from the SJLJ case. Instead of putting
4684 other calls with different conditions at other places for the SJLJ case,
4685 it seems cleaner to reorder things for the SJLJ case and generalize the
4686 condition to make it not ZCX specific.
4688 If there are any exceptions or cleanup processing involved, we need an
4689 outer statement group (for Setjmp_Longjmp) and binding level. */
4690 if (binding_for_block)
4692 start_stmt_group ();
4693 gnat_pushlevel ();
4696 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
4697 area for address of previous buffer. Do this first since we need to have
4698 the setjmp buf known for any decls in this block. */
4699 if (setjmp_longjmp)
4701 gnu_jmpsave_decl
4702 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
4703 jmpbuf_ptr_type,
4704 build_call_n_expr (get_jmpbuf_decl, 0),
4705 false, false, false, false, NULL, gnat_node);
4706 DECL_ARTIFICIAL (gnu_jmpsave_decl) = 1;
4708 /* The __builtin_setjmp receivers will immediately reinstall it. Now
4709 because of the unstructured form of EH used by setjmp_longjmp, there
4710 might be forward edges going to __builtin_setjmp receivers on which
4711 it is uninitialized, although they will never be actually taken. */
4712 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
4713 gnu_jmpbuf_decl
4714 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE,
4715 jmpbuf_type,
4716 NULL_TREE,
4717 false, false, false, false, NULL, gnat_node);
4718 DECL_ARTIFICIAL (gnu_jmpbuf_decl) = 1;
4720 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
4722 /* When we exit this block, restore the saved value. */
4723 add_cleanup (build_call_n_expr (set_jmpbuf_decl, 1, gnu_jmpsave_decl),
4724 gnat_cleanup_loc_node);
4727 /* If we are to call a function when exiting this block, add a cleanup
4728 to the binding level we made above. Note that add_cleanup is FIFO
4729 so we must register this cleanup after the EH cleanup just above. */
4730 if (at_end)
4731 add_cleanup (build_call_n_expr (gnat_to_gnu (At_End_Proc (gnat_node)), 0),
4732 gnat_cleanup_loc_node);
4734 /* Now build the tree for the declarations and statements inside this block.
4735 If this is SJLJ, set our jmp_buf as the current buffer. */
4736 start_stmt_group ();
4738 if (setjmp_longjmp)
4740 gnu_expr = build_call_n_expr (set_jmpbuf_decl, 1,
4741 build_unary_op (ADDR_EXPR, NULL_TREE,
4742 gnu_jmpbuf_decl));
4743 set_expr_location_from_node (gnu_expr, gnat_node);
4744 add_stmt (gnu_expr);
4747 if (Present (First_Real_Statement (gnat_node)))
4748 process_decls (Statements (gnat_node), Empty,
4749 First_Real_Statement (gnat_node), true, true);
4751 /* Generate code for each statement in the block. */
4752 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
4753 ? First_Real_Statement (gnat_node)
4754 : First (Statements (gnat_node)));
4755 Present (gnat_temp); gnat_temp = Next (gnat_temp))
4756 add_stmt (gnat_to_gnu (gnat_temp));
4757 gnu_inner_block = end_stmt_group ();
4759 /* Now generate code for the two exception models, if either is relevant for
4760 this block. */
4761 if (setjmp_longjmp)
4763 tree *gnu_else_ptr = 0;
4764 tree gnu_handler;
4766 /* Make a binding level for the exception handling declarations and code
4767 and set up gnu_except_ptr_stack for the handlers to use. */
4768 start_stmt_group ();
4769 gnat_pushlevel ();
4771 vec_safe_push (gnu_except_ptr_stack,
4772 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE,
4773 build_pointer_type (except_type_node),
4774 build_call_n_expr (get_excptr_decl, 0),
4775 false, false, false, false,
4776 NULL, gnat_node));
4778 /* Generate code for each handler. The N_Exception_Handler case does the
4779 real work and returns a COND_EXPR for each handler, which we chain
4780 together here. */
4781 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4782 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
4784 gnu_expr = gnat_to_gnu (gnat_temp);
4786 /* If this is the first one, set it as the outer one. Otherwise,
4787 point the "else" part of the previous handler to us. Then point
4788 to our "else" part. */
4789 if (!gnu_else_ptr)
4790 add_stmt (gnu_expr);
4791 else
4792 *gnu_else_ptr = gnu_expr;
4794 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
4797 /* If none of the exception handlers did anything, re-raise but do not
4798 defer abortion. */
4799 gnu_expr = build_call_n_expr (raise_nodefer_decl, 1,
4800 gnu_except_ptr_stack->last ());
4801 set_expr_location_from_node
4802 (gnu_expr,
4803 Present (End_Label (gnat_node)) ? End_Label (gnat_node) : gnat_node);
4805 if (gnu_else_ptr)
4806 *gnu_else_ptr = gnu_expr;
4807 else
4808 add_stmt (gnu_expr);
4810 /* End the binding level dedicated to the exception handlers and get the
4811 whole statement group. */
4812 gnu_except_ptr_stack->pop ();
4813 gnat_poplevel ();
4814 gnu_handler = end_stmt_group ();
4816 /* If the setjmp returns 1, we restore our incoming longjmp value and
4817 then check the handlers. */
4818 start_stmt_group ();
4819 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl, 1,
4820 gnu_jmpsave_decl),
4821 gnat_node);
4822 add_stmt (gnu_handler);
4823 gnu_handler = end_stmt_group ();
4825 /* This block is now "if (setjmp) ... <handlers> else <block>". */
4826 gnu_result = build3 (COND_EXPR, void_type_node,
4827 (build_call_n_expr
4828 (setjmp_decl, 1,
4829 build_unary_op (ADDR_EXPR, NULL_TREE,
4830 gnu_jmpbuf_decl))),
4831 gnu_handler, gnu_inner_block);
4833 else if (gcc_zcx)
4835 tree gnu_handlers;
4836 location_t locus;
4838 /* First make a block containing the handlers. */
4839 start_stmt_group ();
4840 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4841 Present (gnat_temp);
4842 gnat_temp = Next_Non_Pragma (gnat_temp))
4843 add_stmt (gnat_to_gnu (gnat_temp));
4844 gnu_handlers = end_stmt_group ();
4846 /* Now make the TRY_CATCH_EXPR for the block. */
4847 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
4848 gnu_inner_block, gnu_handlers);
4849 /* Set a location. We need to find a unique location for the dispatching
4850 code, otherwise we can get coverage or debugging issues. Try with
4851 the location of the end label. */
4852 if (Present (End_Label (gnat_node))
4853 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
4854 SET_EXPR_LOCATION (gnu_result, locus);
4855 else
4856 /* Clear column information so that the exception handler of an
4857 implicit transient block does not incorrectly inherit the slocs
4858 of a decision, which would otherwise confuse control flow based
4859 coverage analysis tools. */
4860 set_expr_location_from_node1 (gnu_result, gnat_node, true);
4862 else
4863 gnu_result = gnu_inner_block;
4865 /* Now close our outer block, if we had to make one. */
4866 if (binding_for_block)
4868 add_stmt (gnu_result);
4869 gnat_poplevel ();
4870 gnu_result = end_stmt_group ();
4873 return gnu_result;
4876 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4877 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
4878 exception handling. */
4880 static tree
4881 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
4883 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
4884 an "if" statement to select the proper exceptions. For "Others", exclude
4885 exceptions where Handled_By_Others is nonzero unless the All_Others flag
4886 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
4887 tree gnu_choice = boolean_false_node;
4888 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
4889 Node_Id gnat_temp;
4891 for (gnat_temp = First (Exception_Choices (gnat_node));
4892 gnat_temp; gnat_temp = Next (gnat_temp))
4894 tree this_choice;
4896 if (Nkind (gnat_temp) == N_Others_Choice)
4898 if (All_Others (gnat_temp))
4899 this_choice = boolean_true_node;
4900 else
4901 this_choice
4902 = build_binary_op
4903 (EQ_EXPR, boolean_type_node,
4904 convert
4905 (integer_type_node,
4906 build_component_ref
4907 (build_unary_op
4908 (INDIRECT_REF, NULL_TREE,
4909 gnu_except_ptr_stack->last ()),
4910 get_identifier ("not_handled_by_others"), NULL_TREE,
4911 false)),
4912 integer_zero_node);
4915 else if (Nkind (gnat_temp) == N_Identifier
4916 || Nkind (gnat_temp) == N_Expanded_Name)
4918 Entity_Id gnat_ex_id = Entity (gnat_temp);
4919 tree gnu_expr;
4921 /* Exception may be a renaming. Recover original exception which is
4922 the one elaborated and registered. */
4923 if (Present (Renamed_Object (gnat_ex_id)))
4924 gnat_ex_id = Renamed_Object (gnat_ex_id);
4926 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4928 this_choice
4929 = build_binary_op
4930 (EQ_EXPR, boolean_type_node,
4931 gnu_except_ptr_stack->last (),
4932 convert (TREE_TYPE (gnu_except_ptr_stack->last ()),
4933 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
4935 else
4936 gcc_unreachable ();
4938 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
4939 gnu_choice, this_choice);
4942 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
4945 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4946 to a GCC tree, which is returned. This is the variant for ZCX. */
4948 static tree
4949 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
4951 tree gnu_etypes_list = NULL_TREE;
4952 tree gnu_current_exc_ptr, prev_gnu_incoming_exc_ptr;
4953 Node_Id gnat_temp;
4955 /* We build a TREE_LIST of nodes representing what exception types this
4956 handler can catch, with special cases for others and all others cases.
4958 Each exception type is actually identified by a pointer to the exception
4959 id, or to a dummy object for "others" and "all others". */
4960 for (gnat_temp = First (Exception_Choices (gnat_node));
4961 gnat_temp; gnat_temp = Next (gnat_temp))
4963 tree gnu_expr, gnu_etype;
4965 if (Nkind (gnat_temp) == N_Others_Choice)
4967 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
4968 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4970 else if (Nkind (gnat_temp) == N_Identifier
4971 || Nkind (gnat_temp) == N_Expanded_Name)
4973 Entity_Id gnat_ex_id = Entity (gnat_temp);
4975 /* Exception may be a renaming. Recover original exception which is
4976 the one elaborated and registered. */
4977 if (Present (Renamed_Object (gnat_ex_id)))
4978 gnat_ex_id = Renamed_Object (gnat_ex_id);
4980 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4981 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4983 else
4984 gcc_unreachable ();
4986 /* The GCC interface expects NULL to be passed for catch all handlers, so
4987 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
4988 is integer_zero_node. It would not work, however, because GCC's
4989 notion of "catch all" is stronger than our notion of "others". Until
4990 we correctly use the cleanup interface as well, doing that would
4991 prevent the "all others" handlers from being seen, because nothing
4992 can be caught beyond a catch all from GCC's point of view. */
4993 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
4996 start_stmt_group ();
4997 gnat_pushlevel ();
4999 /* Expand a call to the begin_handler hook at the beginning of the handler,
5000 and arrange for a call to the end_handler hook to occur on every possible
5001 exit path.
5003 The hooks expect a pointer to the low level occurrence. This is required
5004 for our stack management scheme because a raise inside the handler pushes
5005 a new occurrence on top of the stack, which means that this top does not
5006 necessarily match the occurrence this handler was dealing with.
5008 __builtin_eh_pointer references the exception occurrence being
5009 propagated. Upon handler entry, this is the exception for which the
5010 handler is triggered. This might not be the case upon handler exit,
5011 however, as we might have a new occurrence propagated by the handler's
5012 body, and the end_handler hook called as a cleanup in this context.
5014 We use a local variable to retrieve the incoming value at handler entry
5015 time, and reuse it to feed the end_handler hook's argument at exit. */
5017 gnu_current_exc_ptr
5018 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
5019 1, integer_zero_node);
5020 prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
5021 gnu_incoming_exc_ptr = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
5022 ptr_type_node, gnu_current_exc_ptr,
5023 false, false, false, false,
5024 NULL, gnat_node);
5026 add_stmt_with_node (build_call_n_expr (begin_handler_decl, 1,
5027 gnu_incoming_exc_ptr),
5028 gnat_node);
5030 /* Declare and initialize the choice parameter, if present. */
5031 if (Present (Choice_Parameter (gnat_node)))
5033 tree gnu_param
5034 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, 1);
5036 add_stmt (build_call_n_expr
5037 (set_exception_parameter_decl, 2,
5038 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
5039 gnu_incoming_exc_ptr));
5042 /* We don't have an End_Label at hand to set the location of the cleanup
5043 actions, so we use that of the exception handler itself instead. */
5044 add_cleanup (build_call_n_expr (end_handler_decl, 1, gnu_incoming_exc_ptr),
5045 gnat_node);
5046 add_stmt_list (Statements (gnat_node));
5047 gnat_poplevel ();
5049 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
5051 return
5052 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
5055 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
5057 static void
5058 Compilation_Unit_to_gnu (Node_Id gnat_node)
5060 const Node_Id gnat_unit = Unit (gnat_node);
5061 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
5062 || Nkind (gnat_unit) == N_Subprogram_Body);
5063 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
5064 Entity_Id gnat_entity;
5065 Node_Id gnat_pragma;
5066 /* Make the decl for the elaboration procedure. */
5067 tree gnu_elab_proc_decl
5068 = create_subprog_decl
5069 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
5070 NULL_TREE, void_ftype, NULL_TREE, is_disabled, true, false, true, NULL,
5071 gnat_unit);
5072 struct elab_info *info;
5074 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
5075 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
5077 /* Initialize the information structure for the function. */
5078 allocate_struct_function (gnu_elab_proc_decl, false);
5079 set_cfun (NULL);
5081 current_function_decl = NULL_TREE;
5083 start_stmt_group ();
5084 gnat_pushlevel ();
5086 /* For a body, first process the spec if there is one. */
5087 if (Nkind (gnat_unit) == N_Package_Body
5088 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
5089 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
5091 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
5093 elaborate_all_entities (gnat_node);
5095 if (Nkind (gnat_unit) == N_Subprogram_Declaration
5096 || Nkind (gnat_unit) == N_Generic_Package_Declaration
5097 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
5098 return;
5101 /* Then process any pragmas and declarations preceding the unit. */
5102 for (gnat_pragma = First (Context_Items (gnat_node));
5103 Present (gnat_pragma);
5104 gnat_pragma = Next (gnat_pragma))
5105 if (Nkind (gnat_pragma) == N_Pragma)
5106 add_stmt (gnat_to_gnu (gnat_pragma));
5107 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
5108 true, true);
5110 /* Process the unit itself. */
5111 add_stmt (gnat_to_gnu (gnat_unit));
5113 /* Generate code for all the inlined subprograms. */
5114 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5115 Present (gnat_entity);
5116 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
5118 Node_Id gnat_body;
5120 /* Without optimization, process only the required subprograms. */
5121 if (!optimize && !Has_Pragma_Inline_Always (gnat_entity))
5122 continue;
5124 gnat_body = Parent (Declaration_Node (gnat_entity));
5125 if (Nkind (gnat_body) != N_Subprogram_Body)
5127 /* ??? This happens when only the spec of a package is provided. */
5128 if (No (Corresponding_Body (gnat_body)))
5129 continue;
5131 gnat_body
5132 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
5135 /* Define the entity first so we set DECL_EXTERNAL. */
5136 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5137 add_stmt (gnat_to_gnu (gnat_body));
5140 /* Process any pragmas and actions following the unit. */
5141 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5142 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
5143 finalize_from_limited_with ();
5145 /* Save away what we've made so far and record this potential elaboration
5146 procedure. */
5147 info = ggc_alloc<elab_info> ();
5148 set_current_block_context (gnu_elab_proc_decl);
5149 gnat_poplevel ();
5150 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
5152 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
5154 info->next = elab_info_list;
5155 info->elab_proc = gnu_elab_proc_decl;
5156 info->gnat_node = gnat_node;
5157 elab_info_list = info;
5159 /* Generate elaboration code for this unit, if necessary, and say whether
5160 we did or not. */
5161 gnu_elab_proc_stack->pop ();
5163 /* Invalidate the global renaming pointers. This is necessary because
5164 stabilization of the renamed entities may create SAVE_EXPRs which
5165 have been tied to a specific elaboration routine just above. */
5166 invalidate_global_renaming_pointers ();
5168 /* Force the processing for all nodes that remain in the queue. */
5169 process_deferred_decl_context (true);
5172 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Raise_xxx_Error,
5173 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where
5174 we should place the result type. LABEL_P is true if there is a label to
5175 branch to for the exception. */
5177 static tree
5178 Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5180 const Node_Kind kind = Nkind (gnat_node);
5181 const int reason = UI_To_Int (Reason (gnat_node));
5182 const Node_Id gnat_cond = Condition (gnat_node);
5183 const bool with_extra_info
5184 = Exception_Extra_Info
5185 && !No_Exception_Handlers_Set ()
5186 && !get_exception_label (kind);
5187 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
5189 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5191 switch (reason)
5193 case CE_Access_Check_Failed:
5194 if (with_extra_info)
5195 gnu_result = build_call_raise_column (reason, gnat_node);
5196 break;
5198 case CE_Index_Check_Failed:
5199 case CE_Range_Check_Failed:
5200 case CE_Invalid_Data:
5201 if (Present (gnat_cond) && Nkind (gnat_cond) == N_Op_Not)
5203 Node_Id gnat_range, gnat_index, gnat_type;
5204 tree gnu_index, gnu_low_bound, gnu_high_bound;
5205 struct range_check_info_d *rci;
5207 switch (Nkind (Right_Opnd (gnat_cond)))
5209 case N_In:
5210 gnat_range = Right_Opnd (Right_Opnd (gnat_cond));
5211 gcc_assert (Nkind (gnat_range) == N_Range);
5212 gnu_low_bound = gnat_to_gnu (Low_Bound (gnat_range));
5213 gnu_high_bound = gnat_to_gnu (High_Bound (gnat_range));
5214 break;
5216 case N_Op_Ge:
5217 gnu_low_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5218 gnu_high_bound = NULL_TREE;
5219 break;
5221 case N_Op_Le:
5222 gnu_low_bound = NULL_TREE;
5223 gnu_high_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5224 break;
5226 default:
5227 goto common;
5230 gnat_index = Left_Opnd (Right_Opnd (gnat_cond));
5231 gnat_type = Etype (gnat_index);
5232 gnu_index = gnat_to_gnu (gnat_index);
5234 if (with_extra_info
5235 && gnu_low_bound
5236 && gnu_high_bound
5237 && Known_Esize (gnat_type)
5238 && UI_To_Int (Esize (gnat_type)) <= 32)
5239 gnu_result
5240 = build_call_raise_range (reason, gnat_node, gnu_index,
5241 gnu_low_bound, gnu_high_bound);
5243 /* If loop unswitching is enabled, we try to compute invariant
5244 conditions for checks applied to iteration variables, i.e.
5245 conditions that are both independent of the variable and
5246 necessary in order for the check to fail in the course of
5247 some iteration, and prepend them to the original condition
5248 of the checks. This will make it possible later for the
5249 loop unswitching pass to replace the loop with two loops,
5250 one of which has the checks eliminated and the other has
5251 the original checks reinstated, and a run time selection.
5252 The former loop will be suitable for vectorization. */
5253 if (flag_unswitch_loops
5254 && !vec_safe_is_empty (gnu_loop_stack)
5255 && (!gnu_low_bound
5256 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
5257 && (!gnu_high_bound
5258 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
5259 && (rci = push_range_check_info (gnu_index)))
5261 rci->low_bound = gnu_low_bound;
5262 rci->high_bound = gnu_high_bound;
5263 rci->type = get_unpadded_type (gnat_type);
5264 rci->invariant_cond = build1 (SAVE_EXPR, boolean_type_node,
5265 boolean_true_node);
5266 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5267 boolean_type_node,
5268 rci->invariant_cond,
5269 gnat_to_gnu (gnat_cond));
5272 break;
5274 default:
5275 break;
5278 common:
5279 if (!gnu_result)
5280 gnu_result = build_call_raise (reason, gnat_node, kind);
5281 set_expr_location_from_node (gnu_result, gnat_node);
5283 /* If the type is VOID, this is a statement, so we need to generate the code
5284 for the call. Handle a condition, if there is one. */
5285 if (VOID_TYPE_P (*gnu_result_type_p))
5287 if (Present (gnat_cond))
5289 if (!gnu_cond)
5290 gnu_cond = gnat_to_gnu (gnat_cond);
5291 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
5292 alloc_stmt_list ());
5295 else
5296 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
5298 return gnu_result;
5301 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
5302 parameter of a call. */
5304 static bool
5305 lhs_or_actual_p (Node_Id gnat_node)
5307 Node_Id gnat_parent = Parent (gnat_node);
5308 Node_Kind kind = Nkind (gnat_parent);
5310 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
5311 return true;
5313 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
5314 && Name (gnat_parent) != gnat_node)
5315 return true;
5317 if (kind == N_Parameter_Association)
5318 return true;
5320 return false;
5323 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
5324 of an assignment or an actual parameter of a call. */
5326 static bool
5327 present_in_lhs_or_actual_p (Node_Id gnat_node)
5329 Node_Kind kind;
5331 if (lhs_or_actual_p (gnat_node))
5332 return true;
5334 kind = Nkind (Parent (gnat_node));
5336 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
5337 && lhs_or_actual_p (Parent (gnat_node)))
5338 return true;
5340 return false;
5343 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
5344 as gigi is concerned. This is used to avoid conversions on the LHS. */
5346 static bool
5347 unchecked_conversion_nop (Node_Id gnat_node)
5349 Entity_Id from_type, to_type;
5351 /* The conversion must be on the LHS of an assignment or an actual parameter
5352 of a call. Otherwise, even if the conversion was essentially a no-op, it
5353 could de facto ensure type consistency and this should be preserved. */
5354 if (!lhs_or_actual_p (gnat_node))
5355 return false;
5357 from_type = Etype (Expression (gnat_node));
5359 /* We're interested in artificial conversions generated by the front-end
5360 to make private types explicit, e.g. in Expand_Assign_Array. */
5361 if (!Is_Private_Type (from_type))
5362 return false;
5364 from_type = Underlying_Type (from_type);
5365 to_type = Etype (gnat_node);
5367 /* The direct conversion to the underlying type is a no-op. */
5368 if (to_type == from_type)
5369 return true;
5371 /* For an array subtype, the conversion to the PAIT is a no-op. */
5372 if (Ekind (from_type) == E_Array_Subtype
5373 && to_type == Packed_Array_Impl_Type (from_type))
5374 return true;
5376 /* For a record subtype, the conversion to the type is a no-op. */
5377 if (Ekind (from_type) == E_Record_Subtype
5378 && to_type == Etype (from_type))
5379 return true;
5381 return false;
5384 /* This function is the driver of the GNAT to GCC tree transformation process.
5385 It is the entry point of the tree transformer. GNAT_NODE is the root of
5386 some GNAT tree. Return the root of the corresponding GCC tree. If this
5387 is an expression, return the GCC equivalent of the expression. If this
5388 is a statement, return the statement or add it to the current statement
5389 group, in which case anything returned is to be interpreted as occurring
5390 after anything added. */
5392 tree
5393 gnat_to_gnu (Node_Id gnat_node)
5395 const Node_Kind kind = Nkind (gnat_node);
5396 bool went_into_elab_proc = false;
5397 tree gnu_result = error_mark_node; /* Default to no value. */
5398 tree gnu_result_type = void_type_node;
5399 tree gnu_expr, gnu_lhs, gnu_rhs;
5400 Node_Id gnat_temp;
5402 /* Save node number for error message and set location information. */
5403 error_gnat_node = gnat_node;
5404 Sloc_to_locus (Sloc (gnat_node), &input_location);
5406 /* If this node is a statement and we are only annotating types, return an
5407 empty statement list. */
5408 if (type_annotate_only && IN (kind, N_Statement_Other_Than_Procedure_Call))
5409 return alloc_stmt_list ();
5411 /* If this node is a non-static subexpression and we are only annotating
5412 types, make this into a NULL_EXPR. */
5413 if (type_annotate_only
5414 && IN (kind, N_Subexpr)
5415 && kind != N_Identifier
5416 && !Compile_Time_Known_Value (gnat_node))
5417 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
5418 build_call_raise (CE_Range_Check_Failed, gnat_node,
5419 N_Raise_Constraint_Error));
5421 if ((IN (kind, N_Statement_Other_Than_Procedure_Call)
5422 && kind != N_Null_Statement)
5423 || kind == N_Procedure_Call_Statement
5424 || kind == N_Label
5425 || kind == N_Implicit_Label_Declaration
5426 || kind == N_Handled_Sequence_Of_Statements
5427 || (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void))
5429 tree current_elab_proc = get_elaboration_procedure ();
5431 /* If this is a statement and we are at top level, it must be part of
5432 the elaboration procedure, so mark us as being in that procedure. */
5433 if (!current_function_decl)
5435 current_function_decl = current_elab_proc;
5436 went_into_elab_proc = true;
5439 /* If we are in the elaboration procedure, check if we are violating a
5440 No_Elaboration_Code restriction by having a statement there. Don't
5441 check for a possible No_Elaboration_Code restriction violation on
5442 N_Handled_Sequence_Of_Statements, as we want to signal an error on
5443 every nested real statement instead. This also avoids triggering
5444 spurious errors on dummy (empty) sequences created by the front-end
5445 for package bodies in some cases. */
5446 if (current_function_decl == current_elab_proc
5447 && kind != N_Handled_Sequence_Of_Statements)
5448 Check_Elaboration_Code_Allowed (gnat_node);
5451 switch (kind)
5453 /********************************/
5454 /* Chapter 2: Lexical Elements */
5455 /********************************/
5457 case N_Identifier:
5458 case N_Expanded_Name:
5459 case N_Operator_Symbol:
5460 case N_Defining_Identifier:
5461 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
5463 /* If this is an atomic access on the RHS for which synchronization is
5464 required, build the atomic load. */
5465 if (atomic_sync_required_p (gnat_node)
5466 && !present_in_lhs_or_actual_p (gnat_node))
5467 gnu_result = build_atomic_load (gnu_result);
5468 break;
5470 case N_Integer_Literal:
5472 tree gnu_type;
5474 /* Get the type of the result, looking inside any padding and
5475 justified modular types. Then get the value in that type. */
5476 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
5478 if (TREE_CODE (gnu_type) == RECORD_TYPE
5479 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
5480 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
5482 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
5484 /* If the result overflows (meaning it doesn't fit in its base type),
5485 abort. We would like to check that the value is within the range
5486 of the subtype, but that causes problems with subtypes whose usage
5487 will raise Constraint_Error and with biased representation, so
5488 we don't. */
5489 gcc_assert (!TREE_OVERFLOW (gnu_result));
5491 break;
5493 case N_Character_Literal:
5494 /* If a Entity is present, it means that this was one of the
5495 literals in a user-defined character type. In that case,
5496 just return the value in the CONST_DECL. Otherwise, use the
5497 character code. In that case, the base type should be an
5498 INTEGER_TYPE, but we won't bother checking for that. */
5499 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5500 if (Present (Entity (gnat_node)))
5501 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
5502 else
5503 gnu_result
5504 = build_int_cst_type
5505 (gnu_result_type, UI_To_CC (Char_Literal_Value (gnat_node)));
5506 break;
5508 case N_Real_Literal:
5509 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5511 /* If this is of a fixed-point type, the value we want is the value of
5512 the corresponding integer. */
5513 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
5515 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
5516 gnu_result_type);
5517 gcc_assert (!TREE_OVERFLOW (gnu_result));
5520 else
5522 Ureal ur_realval = Realval (gnat_node);
5524 /* First convert the value to a machine number if it isn't already.
5525 That will force the base to 2 for non-zero values and simplify
5526 the rest of the logic. */
5527 if (!Is_Machine_Number (gnat_node))
5528 ur_realval
5529 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
5530 ur_realval, Round_Even, gnat_node);
5532 if (UR_Is_Zero (ur_realval))
5533 gnu_result = convert (gnu_result_type, integer_zero_node);
5534 else
5536 REAL_VALUE_TYPE tmp;
5538 gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
5540 /* The base must be 2 as Machine guarantees this, so we scale
5541 the value, which we know can fit in the mantissa of the type
5542 (hence the use of that type above). */
5543 gcc_assert (Rbase (ur_realval) == 2);
5544 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
5545 - UI_To_Int (Denominator (ur_realval)));
5546 gnu_result = build_real (gnu_result_type, tmp);
5549 /* Now see if we need to negate the result. Do it this way to
5550 properly handle -0. */
5551 if (UR_Is_Negative (Realval (gnat_node)))
5552 gnu_result
5553 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
5554 gnu_result);
5557 break;
5559 case N_String_Literal:
5560 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5561 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
5563 String_Id gnat_string = Strval (gnat_node);
5564 int length = String_Length (gnat_string);
5565 int i;
5566 char *string;
5567 if (length >= ALLOCA_THRESHOLD)
5568 string = XNEWVEC (char, length + 1);
5569 else
5570 string = (char *) alloca (length + 1);
5572 /* Build the string with the characters in the literal. Note
5573 that Ada strings are 1-origin. */
5574 for (i = 0; i < length; i++)
5575 string[i] = Get_String_Char (gnat_string, i + 1);
5577 /* Put a null at the end of the string in case it's in a context
5578 where GCC will want to treat it as a C string. */
5579 string[i] = 0;
5581 gnu_result = build_string (length, string);
5583 /* Strings in GCC don't normally have types, but we want
5584 this to not be converted to the array type. */
5585 TREE_TYPE (gnu_result) = gnu_result_type;
5587 if (length >= ALLOCA_THRESHOLD)
5588 free (string);
5590 else
5592 /* Build a list consisting of each character, then make
5593 the aggregate. */
5594 String_Id gnat_string = Strval (gnat_node);
5595 int length = String_Length (gnat_string);
5596 int i;
5597 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5598 tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node);
5599 vec<constructor_elt, va_gc> *gnu_vec;
5600 vec_alloc (gnu_vec, length);
5602 for (i = 0; i < length; i++)
5604 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
5605 Get_String_Char (gnat_string, i + 1));
5607 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
5608 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node);
5611 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
5613 break;
5615 case N_Pragma:
5616 gnu_result = Pragma_to_gnu (gnat_node);
5617 break;
5619 /**************************************/
5620 /* Chapter 3: Declarations and Types */
5621 /**************************************/
5623 case N_Subtype_Declaration:
5624 case N_Full_Type_Declaration:
5625 case N_Incomplete_Type_Declaration:
5626 case N_Private_Type_Declaration:
5627 case N_Private_Extension_Declaration:
5628 case N_Task_Type_Declaration:
5629 process_type (Defining_Entity (gnat_node));
5630 gnu_result = alloc_stmt_list ();
5631 break;
5633 case N_Object_Declaration:
5634 case N_Exception_Declaration:
5635 gnat_temp = Defining_Entity (gnat_node);
5636 gnu_result = alloc_stmt_list ();
5638 /* If we are just annotating types and this object has an unconstrained
5639 or task type, don't elaborate it. */
5640 if (type_annotate_only
5641 && (((Is_Array_Type (Etype (gnat_temp))
5642 || Is_Record_Type (Etype (gnat_temp)))
5643 && !Is_Constrained (Etype (gnat_temp)))
5644 || Is_Concurrent_Type (Etype (gnat_temp))))
5645 break;
5647 if (Present (Expression (gnat_node))
5648 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
5649 && (!type_annotate_only
5650 || Compile_Time_Known_Value (Expression (gnat_node))))
5652 gnu_expr = gnat_to_gnu (Expression (gnat_node));
5653 if (Do_Range_Check (Expression (gnat_node)))
5654 gnu_expr
5655 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
5657 /* If this object has its elaboration delayed, we must force
5658 evaluation of GNU_EXPR right now and save it for when the object
5659 is frozen. */
5660 if (Present (Freeze_Node (gnat_temp)))
5662 if (TREE_CONSTANT (gnu_expr))
5664 else if (global_bindings_p ())
5665 gnu_expr
5666 = create_var_decl (create_concat_name (gnat_temp, "init"),
5667 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5668 false, false, false, false,
5669 NULL, gnat_temp);
5670 else
5671 gnu_expr = gnat_save_expr (gnu_expr);
5673 save_gnu_tree (gnat_node, gnu_expr, true);
5676 else
5677 gnu_expr = NULL_TREE;
5679 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
5680 gnu_expr = NULL_TREE;
5682 /* If this is a deferred constant with an address clause, we ignore the
5683 full view since the clause is on the partial view and we cannot have
5684 2 different GCC trees for the object. The only bits of the full view
5685 we will use is the initializer, but it will be directly fetched. */
5686 if (Ekind(gnat_temp) == E_Constant
5687 && Present (Address_Clause (gnat_temp))
5688 && Present (Full_View (gnat_temp)))
5689 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
5691 if (No (Freeze_Node (gnat_temp)))
5692 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
5693 break;
5695 case N_Object_Renaming_Declaration:
5696 gnat_temp = Defining_Entity (gnat_node);
5698 /* Don't do anything if this renaming is handled by the front end or if
5699 we are just annotating types and this object has a composite or task
5700 type, don't elaborate it. We return the result in case it has any
5701 SAVE_EXPRs in it that need to be evaluated here. */
5702 if (!Is_Renaming_Of_Object (gnat_temp)
5703 && ! (type_annotate_only
5704 && (Is_Array_Type (Etype (gnat_temp))
5705 || Is_Record_Type (Etype (gnat_temp))
5706 || Is_Concurrent_Type (Etype (gnat_temp)))))
5707 gnu_result
5708 = gnat_to_gnu_entity (gnat_temp,
5709 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
5710 else
5711 gnu_result = alloc_stmt_list ();
5712 break;
5714 case N_Exception_Renaming_Declaration:
5715 gnat_temp = Defining_Entity (gnat_node);
5716 if (Renamed_Entity (gnat_temp) != Empty)
5717 gnu_result
5718 = gnat_to_gnu_entity (gnat_temp,
5719 gnat_to_gnu (Renamed_Entity (gnat_temp)), 1);
5720 else
5721 gnu_result = alloc_stmt_list ();
5722 break;
5724 case N_Implicit_Label_Declaration:
5725 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
5726 gnu_result = alloc_stmt_list ();
5727 break;
5729 case N_Number_Declaration:
5730 case N_Subprogram_Renaming_Declaration:
5731 case N_Package_Renaming_Declaration:
5732 /* These are fully handled in the front end. */
5733 /* ??? For package renamings, find a way to use GENERIC namespaces so
5734 that we get proper debug information for them. */
5735 gnu_result = alloc_stmt_list ();
5736 break;
5738 /*************************************/
5739 /* Chapter 4: Names and Expressions */
5740 /*************************************/
5742 case N_Explicit_Dereference:
5743 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5744 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5745 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
5747 /* If this is an atomic access on the RHS for which synchronization is
5748 required, build the atomic load. */
5749 if (atomic_sync_required_p (gnat_node)
5750 && !present_in_lhs_or_actual_p (gnat_node))
5751 gnu_result = build_atomic_load (gnu_result);
5752 break;
5754 case N_Indexed_Component:
5756 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
5757 tree gnu_type;
5758 int ndim;
5759 int i;
5760 Node_Id *gnat_expr_array;
5762 gnu_array_object = maybe_implicit_deref (gnu_array_object);
5764 /* Convert vector inputs to their representative array type, to fit
5765 what the code below expects. */
5766 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
5768 if (present_in_lhs_or_actual_p (gnat_node))
5769 gnat_mark_addressable (gnu_array_object);
5770 gnu_array_object = maybe_vector_array (gnu_array_object);
5773 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
5775 /* If we got a padded type, remove it too. */
5776 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
5777 gnu_array_object
5778 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
5779 gnu_array_object);
5781 gnu_result = gnu_array_object;
5783 /* The failure of this assertion will very likely come from a missing
5784 expansion for a packed array access. */
5785 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
5787 /* First compute the number of dimensions of the array, then
5788 fill the expression array, the order depending on whether
5789 this is a Convention_Fortran array or not. */
5790 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
5791 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5792 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
5793 ndim++, gnu_type = TREE_TYPE (gnu_type))
5796 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
5798 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
5799 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
5800 i >= 0;
5801 i--, gnat_temp = Next (gnat_temp))
5802 gnat_expr_array[i] = gnat_temp;
5803 else
5804 for (i = 0, gnat_temp = First (Expressions (gnat_node));
5805 i < ndim;
5806 i++, gnat_temp = Next (gnat_temp))
5807 gnat_expr_array[i] = gnat_temp;
5809 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
5810 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
5812 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
5813 gnat_temp = gnat_expr_array[i];
5814 gnu_expr = gnat_to_gnu (gnat_temp);
5816 if (Do_Range_Check (gnat_temp))
5817 gnu_expr
5818 = emit_index_check
5819 (gnu_array_object, gnu_expr,
5820 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5821 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5822 gnat_temp);
5824 gnu_result
5825 = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr);
5827 /* Array accesses are bound-checked so they cannot trap, but this
5828 is valid only if they are not hoisted ahead of the check. We
5829 need to mark them as no-trap to get decent loop optimizations
5830 in the presence of -fnon-call-exceptions, so we do it when we
5831 know that the original expression had no side-effects. */
5832 if (TREE_CODE (gnu_result) == ARRAY_REF
5833 && !(Nkind (gnat_temp) == N_Identifier
5834 && Ekind (Entity (gnat_temp)) == E_Constant))
5835 TREE_THIS_NOTRAP (gnu_result) = 1;
5838 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5840 /* If this is an atomic access on the RHS for which synchronization is
5841 required, build the atomic load. */
5842 if (atomic_sync_required_p (gnat_node)
5843 && !present_in_lhs_or_actual_p (gnat_node))
5844 gnu_result = build_atomic_load (gnu_result);
5846 break;
5848 case N_Slice:
5850 Node_Id gnat_range_node = Discrete_Range (gnat_node);
5851 tree gnu_type;
5853 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5854 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5856 /* Do any implicit dereferences of the prefix and do any needed
5857 range check. */
5858 gnu_result = maybe_implicit_deref (gnu_result);
5859 gnu_result = maybe_unconstrained_array (gnu_result);
5860 gnu_type = TREE_TYPE (gnu_result);
5861 if (Do_Range_Check (gnat_range_node))
5863 /* Get the bounds of the slice. */
5864 tree gnu_index_type
5865 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
5866 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
5867 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
5868 /* Get the permitted bounds. */
5869 tree gnu_base_index_type
5870 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
5871 tree gnu_base_min_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5872 (TYPE_MIN_VALUE (gnu_base_index_type), gnu_result);
5873 tree gnu_base_max_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5874 (TYPE_MAX_VALUE (gnu_base_index_type), gnu_result);
5875 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
5877 gnu_min_expr = gnat_protect_expr (gnu_min_expr);
5878 gnu_max_expr = gnat_protect_expr (gnu_max_expr);
5880 /* Derive a good type to convert everything to. */
5881 gnu_expr_type = get_base_type (gnu_index_type);
5883 /* Test whether the minimum slice value is too small. */
5884 gnu_expr_l = build_binary_op (LT_EXPR, boolean_type_node,
5885 convert (gnu_expr_type,
5886 gnu_min_expr),
5887 convert (gnu_expr_type,
5888 gnu_base_min_expr));
5890 /* Test whether the maximum slice value is too large. */
5891 gnu_expr_h = build_binary_op (GT_EXPR, boolean_type_node,
5892 convert (gnu_expr_type,
5893 gnu_max_expr),
5894 convert (gnu_expr_type,
5895 gnu_base_max_expr));
5897 /* Build a slice index check that returns the low bound,
5898 assuming the slice is not empty. */
5899 gnu_expr = emit_check
5900 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
5901 gnu_expr_l, gnu_expr_h),
5902 gnu_min_expr, CE_Index_Check_Failed, gnat_node);
5904 /* Build a conditional expression that does the index checks and
5905 returns the low bound if the slice is not empty (max >= min),
5906 and returns the naked low bound otherwise (max < min), unless
5907 it is non-constant and the high bound is; this prevents VRP
5908 from inferring bogus ranges on the unlikely path. */
5909 gnu_expr = fold_build3 (COND_EXPR, gnu_expr_type,
5910 build_binary_op (GE_EXPR, gnu_expr_type,
5911 convert (gnu_expr_type,
5912 gnu_max_expr),
5913 convert (gnu_expr_type,
5914 gnu_min_expr)),
5915 gnu_expr,
5916 TREE_CODE (gnu_min_expr) != INTEGER_CST
5917 && TREE_CODE (gnu_max_expr) == INTEGER_CST
5918 ? gnu_max_expr : gnu_min_expr);
5920 else
5921 /* Simply return the naked low bound. */
5922 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5924 /* If this is a slice with non-constant size of an array with constant
5925 size, set the maximum size for the allocation of temporaries. */
5926 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
5927 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_type)))
5928 TYPE_ARRAY_MAX_SIZE (gnu_result_type) = TYPE_SIZE_UNIT (gnu_type);
5930 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
5931 gnu_result, gnu_expr);
5933 break;
5935 case N_Selected_Component:
5937 Entity_Id gnat_prefix = Prefix (gnat_node);
5938 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
5939 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
5940 tree gnu_field;
5942 gnu_prefix = maybe_implicit_deref (gnu_prefix);
5944 /* For discriminant references in tagged types always substitute the
5945 corresponding discriminant as the actual selected component. */
5946 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix))))
5947 while (Present (Corresponding_Discriminant (gnat_field)))
5948 gnat_field = Corresponding_Discriminant (gnat_field);
5950 /* For discriminant references of untagged types always substitute the
5951 corresponding stored discriminant. */
5952 else if (Present (Corresponding_Discriminant (gnat_field)))
5953 gnat_field = Original_Record_Component (gnat_field);
5955 /* Handle extracting the real or imaginary part of a complex.
5956 The real part is the first field and the imaginary the last. */
5957 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
5958 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
5959 ? REALPART_EXPR : IMAGPART_EXPR,
5960 NULL_TREE, gnu_prefix);
5961 else
5963 gnu_field = gnat_to_gnu_field_decl (gnat_field);
5965 /* If there are discriminants, the prefix might be evaluated more
5966 than once, which is a problem if it has side-effects. */
5967 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
5968 ? Designated_Type (Etype
5969 (Prefix (gnat_node)))
5970 : Etype (Prefix (gnat_node))))
5971 gnu_prefix = gnat_stabilize_reference (gnu_prefix, false, NULL);
5973 gnu_result
5974 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
5975 (Nkind (Parent (gnat_node))
5976 == N_Attribute_Reference)
5977 && lvalue_required_for_attribute_p
5978 (Parent (gnat_node)));
5981 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5983 /* If this is an atomic access on the RHS for which synchronization is
5984 required, build the atomic load. */
5985 if (atomic_sync_required_p (gnat_node)
5986 && !present_in_lhs_or_actual_p (gnat_node))
5987 gnu_result = build_atomic_load (gnu_result);
5989 break;
5991 case N_Attribute_Reference:
5993 /* The attribute designator. */
5994 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
5996 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
5997 is a unit, not an object with a GCC equivalent. */
5998 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
5999 return
6000 create_subprog_decl (create_concat_name
6001 (Entity (Prefix (gnat_node)),
6002 attr == Attr_Elab_Body ? "elabb" : "elabs"),
6003 NULL_TREE, void_ftype, NULL_TREE, is_disabled,
6004 true, true, true, NULL, gnat_node);
6006 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
6008 break;
6010 case N_Reference:
6011 /* Like 'Access as far as we are concerned. */
6012 gnu_result = gnat_to_gnu (Prefix (gnat_node));
6013 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6014 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6015 break;
6017 case N_Aggregate:
6018 case N_Extension_Aggregate:
6020 tree gnu_aggr_type;
6022 /* ??? It is wrong to evaluate the type now, but there doesn't
6023 seem to be any other practical way of doing it. */
6025 gcc_assert (!Expansion_Delayed (gnat_node));
6027 gnu_aggr_type = gnu_result_type
6028 = get_unpadded_type (Etype (gnat_node));
6030 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
6031 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
6032 gnu_aggr_type
6033 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
6034 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
6035 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
6037 if (Null_Record_Present (gnat_node))
6038 gnu_result = gnat_build_constructor (gnu_aggr_type,
6039 NULL);
6041 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
6042 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
6043 gnu_result
6044 = assoc_to_constructor (Etype (gnat_node),
6045 First (Component_Associations (gnat_node)),
6046 gnu_aggr_type);
6047 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
6048 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
6049 gnu_aggr_type,
6050 Component_Type (Etype (gnat_node)));
6051 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
6052 gnu_result
6053 = build_binary_op
6054 (COMPLEX_EXPR, gnu_aggr_type,
6055 gnat_to_gnu (Expression (First
6056 (Component_Associations (gnat_node)))),
6057 gnat_to_gnu (Expression
6058 (Next
6059 (First (Component_Associations (gnat_node))))));
6060 else
6061 gcc_unreachable ();
6063 gnu_result = convert (gnu_result_type, gnu_result);
6065 break;
6067 case N_Null:
6068 if (TARGET_VTABLE_USES_DESCRIPTORS
6069 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
6070 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
6071 gnu_result = null_fdesc_node;
6072 else
6073 gnu_result = null_pointer_node;
6074 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6075 break;
6077 case N_Type_Conversion:
6078 case N_Qualified_Expression:
6079 /* Get the operand expression. */
6080 gnu_result = gnat_to_gnu (Expression (gnat_node));
6081 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6083 /* If this is a qualified expression for a tagged type, we mark the type
6084 as used. Because of polymorphism, this might be the only reference to
6085 the tagged type in the program while objects have it as dynamic type.
6086 The debugger needs to see it to display these objects properly. */
6087 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
6088 used_types_insert (gnu_result_type);
6090 gnu_result
6091 = convert_with_check (Etype (gnat_node), gnu_result,
6092 Do_Overflow_Check (gnat_node),
6093 Do_Range_Check (Expression (gnat_node)),
6094 kind == N_Type_Conversion
6095 && Float_Truncate (gnat_node), gnat_node);
6096 break;
6098 case N_Unchecked_Type_Conversion:
6099 gnu_result = gnat_to_gnu (Expression (gnat_node));
6101 /* Skip further processing if the conversion is deemed a no-op. */
6102 if (unchecked_conversion_nop (gnat_node))
6104 gnu_result_type = TREE_TYPE (gnu_result);
6105 break;
6108 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6110 /* If the result is a pointer type, see if we are improperly
6111 converting to a stricter alignment. */
6112 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
6113 && IN (Ekind (Etype (gnat_node)), Access_Kind))
6115 unsigned int align = known_alignment (gnu_result);
6116 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6117 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6119 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6120 post_error_ne_tree_2
6121 ("?source alignment (^) '< alignment of & (^)",
6122 gnat_node, Designated_Type (Etype (gnat_node)),
6123 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
6126 /* If we are converting a descriptor to a function pointer, first
6127 build the pointer. */
6128 if (TARGET_VTABLE_USES_DESCRIPTORS
6129 && TREE_TYPE (gnu_result) == fdesc_type_node
6130 && POINTER_TYPE_P (gnu_result_type))
6131 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6133 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
6134 No_Truncation (gnat_node));
6135 break;
6137 case N_In:
6138 case N_Not_In:
6140 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
6141 Node_Id gnat_range = Right_Opnd (gnat_node);
6142 tree gnu_low, gnu_high;
6144 /* GNAT_RANGE is either an N_Range node or an identifier denoting a
6145 subtype. */
6146 if (Nkind (gnat_range) == N_Range)
6148 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
6149 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
6151 else if (Nkind (gnat_range) == N_Identifier
6152 || Nkind (gnat_range) == N_Expanded_Name)
6154 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
6155 tree gnu_range_base_type = get_base_type (gnu_range_type);
6157 gnu_low
6158 = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type));
6159 gnu_high
6160 = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type));
6162 else
6163 gcc_unreachable ();
6165 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6167 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
6168 ensure that GNU_OBJ is evaluated only once and perform a full range
6169 test. */
6170 if (operand_equal_p (gnu_low, gnu_high, 0))
6171 gnu_result
6172 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
6173 else
6175 tree t1, t2;
6176 gnu_obj = gnat_protect_expr (gnu_obj);
6177 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
6178 if (EXPR_P (t1))
6179 set_expr_location_from_node (t1, gnat_node);
6180 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
6181 if (EXPR_P (t2))
6182 set_expr_location_from_node (t2, gnat_node);
6183 gnu_result
6184 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
6187 if (kind == N_Not_In)
6188 gnu_result
6189 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
6191 break;
6193 case N_Op_Divide:
6194 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6195 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6196 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6197 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
6198 ? RDIV_EXPR
6199 : (Rounded_Result (gnat_node)
6200 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
6201 gnu_result_type, gnu_lhs, gnu_rhs);
6202 break;
6204 case N_Op_Or: case N_Op_And: case N_Op_Xor:
6205 /* These can either be operations on booleans or on modular types.
6206 Fall through for boolean types since that's the way GNU_CODES is
6207 set up. */
6208 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6210 enum tree_code code
6211 = (kind == N_Op_Or ? BIT_IOR_EXPR
6212 : kind == N_Op_And ? BIT_AND_EXPR
6213 : BIT_XOR_EXPR);
6215 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6216 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6217 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6218 gnu_result = build_binary_op (code, gnu_result_type,
6219 gnu_lhs, gnu_rhs);
6220 break;
6223 /* ... fall through ... */
6225 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
6226 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
6227 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
6228 case N_Op_Mod: case N_Op_Rem:
6229 case N_Op_Rotate_Left:
6230 case N_Op_Rotate_Right:
6231 case N_Op_Shift_Left:
6232 case N_Op_Shift_Right:
6233 case N_Op_Shift_Right_Arithmetic:
6234 case N_And_Then: case N_Or_Else:
6236 enum tree_code code = gnu_codes[kind];
6237 bool ignore_lhs_overflow = false;
6238 location_t saved_location = input_location;
6239 tree gnu_type;
6241 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6242 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6243 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6245 /* Pending generic support for efficient vector logical operations in
6246 GCC, convert vectors to their representative array type view and
6247 fallthrough. */
6248 gnu_lhs = maybe_vector_array (gnu_lhs);
6249 gnu_rhs = maybe_vector_array (gnu_rhs);
6251 /* If this is a comparison operator, convert any references to an
6252 unconstrained array value into a reference to the actual array. */
6253 if (TREE_CODE_CLASS (code) == tcc_comparison)
6255 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
6256 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
6259 /* If this is a shift whose count is not guaranteed to be correct,
6260 we need to adjust the shift count. */
6261 if (IN (kind, N_Op_Shift) && !Shift_Count_OK (gnat_node))
6263 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
6264 tree gnu_max_shift
6265 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
6267 if (kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
6268 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
6269 gnu_rhs, gnu_max_shift);
6270 else if (kind == N_Op_Shift_Right_Arithmetic)
6271 gnu_rhs
6272 = build_binary_op
6273 (MIN_EXPR, gnu_count_type,
6274 build_binary_op (MINUS_EXPR,
6275 gnu_count_type,
6276 gnu_max_shift,
6277 convert (gnu_count_type,
6278 integer_one_node)),
6279 gnu_rhs);
6282 /* For right shifts, the type says what kind of shift to do,
6283 so we may need to choose a different type. In this case,
6284 we have to ignore integer overflow lest it propagates all
6285 the way down and causes a CE to be explicitly raised. */
6286 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
6288 gnu_type = gnat_unsigned_type (gnu_type);
6289 ignore_lhs_overflow = true;
6291 else if (kind == N_Op_Shift_Right_Arithmetic
6292 && TYPE_UNSIGNED (gnu_type))
6294 gnu_type = gnat_signed_type (gnu_type);
6295 ignore_lhs_overflow = true;
6298 if (gnu_type != gnu_result_type)
6300 tree gnu_old_lhs = gnu_lhs;
6301 gnu_lhs = convert (gnu_type, gnu_lhs);
6302 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
6303 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
6304 gnu_rhs = convert (gnu_type, gnu_rhs);
6307 /* Instead of expanding overflow checks for addition, subtraction
6308 and multiplication itself, the front end will leave this to
6309 the back end when Backend_Overflow_Checks_On_Target is set.
6310 As the GCC back end itself does not know yet how to properly
6311 do overflow checking, do it here. The goal is to push
6312 the expansions further into the back end over time. */
6313 if (Do_Overflow_Check (gnat_node) && Backend_Overflow_Checks_On_Target
6314 && (kind == N_Op_Add
6315 || kind == N_Op_Subtract
6316 || kind == N_Op_Multiply)
6317 && !TYPE_UNSIGNED (gnu_type)
6318 && !FLOAT_TYPE_P (gnu_type))
6319 gnu_result = build_binary_op_trapv (code, gnu_type,
6320 gnu_lhs, gnu_rhs, gnat_node);
6321 else
6323 /* Some operations, e.g. comparisons of arrays, generate complex
6324 trees that need to be annotated while they are being built. */
6325 input_location = saved_location;
6326 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
6329 /* If this is a logical shift with the shift count not verified,
6330 we must return zero if it is too large. We cannot compensate
6331 above in this case. */
6332 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
6333 && !Shift_Count_OK (gnat_node))
6334 gnu_result
6335 = build_cond_expr
6336 (gnu_type,
6337 build_binary_op (GE_EXPR, boolean_type_node,
6338 gnu_rhs,
6339 convert (TREE_TYPE (gnu_rhs),
6340 TYPE_SIZE (gnu_type))),
6341 convert (gnu_type, integer_zero_node),
6342 gnu_result);
6344 break;
6346 case N_If_Expression:
6348 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
6349 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
6350 tree gnu_false
6351 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
6353 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6354 gnu_result
6355 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
6357 break;
6359 case N_Op_Plus:
6360 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
6361 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6362 break;
6364 case N_Op_Not:
6365 /* This case can apply to a boolean or a modular type.
6366 Fall through for a boolean operand since GNU_CODES is set
6367 up to handle this. */
6368 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6370 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6371 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6372 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
6373 gnu_expr);
6374 break;
6377 /* ... fall through ... */
6379 case N_Op_Minus: case N_Op_Abs:
6380 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6381 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6383 if (Do_Overflow_Check (gnat_node)
6384 && !TYPE_UNSIGNED (gnu_result_type)
6385 && !FLOAT_TYPE_P (gnu_result_type))
6386 gnu_result
6387 = build_unary_op_trapv (gnu_codes[kind],
6388 gnu_result_type, gnu_expr, gnat_node);
6389 else
6390 gnu_result = build_unary_op (gnu_codes[kind],
6391 gnu_result_type, gnu_expr);
6392 break;
6394 case N_Allocator:
6396 tree gnu_init = 0;
6397 tree gnu_type;
6398 bool ignore_init_type = false;
6400 gnat_temp = Expression (gnat_node);
6402 /* The Expression operand can either be an N_Identifier or
6403 Expanded_Name, which must represent a type, or a
6404 N_Qualified_Expression, which contains both the object type and an
6405 initial value for the object. */
6406 if (Nkind (gnat_temp) == N_Identifier
6407 || Nkind (gnat_temp) == N_Expanded_Name)
6408 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
6409 else if (Nkind (gnat_temp) == N_Qualified_Expression)
6411 Entity_Id gnat_desig_type
6412 = Designated_Type (Underlying_Type (Etype (gnat_node)));
6414 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
6415 gnu_init = gnat_to_gnu (Expression (gnat_temp));
6417 gnu_init = maybe_unconstrained_array (gnu_init);
6418 if (Do_Range_Check (Expression (gnat_temp)))
6419 gnu_init
6420 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
6422 if (Is_Elementary_Type (gnat_desig_type)
6423 || Is_Constrained (gnat_desig_type))
6424 gnu_type = gnat_to_gnu_type (gnat_desig_type);
6425 else
6427 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
6428 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
6429 gnu_type = TREE_TYPE (gnu_init);
6432 /* See the N_Qualified_Expression case for the rationale. */
6433 if (Is_Tagged_Type (gnat_desig_type))
6434 used_types_insert (gnu_type);
6436 gnu_init = convert (gnu_type, gnu_init);
6438 else
6439 gcc_unreachable ();
6441 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6442 return build_allocator (gnu_type, gnu_init, gnu_result_type,
6443 Procedure_To_Call (gnat_node),
6444 Storage_Pool (gnat_node), gnat_node,
6445 ignore_init_type);
6447 break;
6449 /**************************/
6450 /* Chapter 5: Statements */
6451 /**************************/
6453 case N_Label:
6454 gnu_result = build1 (LABEL_EXPR, void_type_node,
6455 gnat_to_gnu (Identifier (gnat_node)));
6456 break;
6458 case N_Null_Statement:
6459 /* When not optimizing, turn null statements from source into gotos to
6460 the next statement that the middle-end knows how to preserve. */
6461 if (!optimize && Comes_From_Source (gnat_node))
6463 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
6464 DECL_IGNORED_P (label) = 1;
6465 start_stmt_group ();
6466 stmt = build1 (GOTO_EXPR, void_type_node, label);
6467 set_expr_location_from_node (stmt, gnat_node);
6468 add_stmt (stmt);
6469 stmt = build1 (LABEL_EXPR, void_type_node, label);
6470 set_expr_location_from_node (stmt, gnat_node);
6471 add_stmt (stmt);
6472 gnu_result = end_stmt_group ();
6474 else
6475 gnu_result = alloc_stmt_list ();
6476 break;
6478 case N_Assignment_Statement:
6479 /* Get the LHS and RHS of the statement and convert any reference to an
6480 unconstrained array into a reference to the underlying array. */
6481 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
6483 /* If the type has a size that overflows, convert this into raise of
6484 Storage_Error: execution shouldn't have gotten here anyway. */
6485 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
6486 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
6487 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
6488 N_Raise_Storage_Error);
6489 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
6490 gnu_result
6491 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
6492 atomic_sync_required_p (Name (gnat_node)));
6493 else
6495 const Node_Id gnat_expr = Expression (gnat_node);
6496 const Entity_Id gnat_type
6497 = Underlying_Type (Etype (Name (gnat_node)));
6498 const bool regular_array_type_p
6499 = (Is_Array_Type (gnat_type) && !Is_Bit_Packed_Array (gnat_type));
6500 const bool use_memset_p
6501 = (regular_array_type_p
6502 && Nkind (gnat_expr) == N_Aggregate
6503 && Is_Others_Aggregate (gnat_expr));
6505 /* If we'll use memset, we need to find the inner expression. */
6506 if (use_memset_p)
6508 Node_Id gnat_inner
6509 = Expression (First (Component_Associations (gnat_expr)));
6510 while (Nkind (gnat_inner) == N_Aggregate
6511 && Is_Others_Aggregate (gnat_inner))
6512 gnat_inner
6513 = Expression (First (Component_Associations (gnat_inner)));
6514 gnu_rhs = gnat_to_gnu (gnat_inner);
6516 else
6517 gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr));
6519 /* If range check is needed, emit code to generate it. */
6520 if (Do_Range_Check (gnat_expr))
6521 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
6522 gnat_node);
6524 /* If atomic synchronization is required, build an atomic store. */
6525 if (atomic_sync_required_p (Name (gnat_node)))
6526 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs);
6528 /* Or else, use memset when the conditions are met. */
6529 else if (use_memset_p)
6531 tree value = fold_convert (integer_type_node, gnu_rhs);
6532 tree to = gnu_lhs;
6533 tree type = TREE_TYPE (to);
6534 tree size
6535 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to);
6536 tree to_ptr = build_fold_addr_expr (to);
6537 tree t = builtin_decl_explicit (BUILT_IN_MEMSET);
6538 if (TREE_CODE (value) == INTEGER_CST)
6540 tree mask
6541 = build_int_cst (integer_type_node,
6542 ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
6543 value = int_const_binop (BIT_AND_EXPR, value, mask);
6545 gnu_result = build_call_expr (t, 3, to_ptr, value, size);
6548 /* Otherwise build a regular assignment. */
6549 else
6550 gnu_result
6551 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
6553 /* If the assignment type is a regular array and the two sides are
6554 not completely disjoint, play safe and use memmove. But don't do
6555 it for a bit-packed array as it might not be byte-aligned. */
6556 if (TREE_CODE (gnu_result) == MODIFY_EXPR
6557 && regular_array_type_p
6558 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
6560 tree to = TREE_OPERAND (gnu_result, 0);
6561 tree from = TREE_OPERAND (gnu_result, 1);
6562 tree type = TREE_TYPE (from);
6563 tree size
6564 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from);
6565 tree to_ptr = build_fold_addr_expr (to);
6566 tree from_ptr = build_fold_addr_expr (from);
6567 tree t = builtin_decl_explicit (BUILT_IN_MEMMOVE);
6568 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
6571 break;
6573 case N_If_Statement:
6575 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
6577 /* Make the outer COND_EXPR. Avoid non-determinism. */
6578 gnu_result = build3 (COND_EXPR, void_type_node,
6579 gnat_to_gnu (Condition (gnat_node)),
6580 NULL_TREE, NULL_TREE);
6581 COND_EXPR_THEN (gnu_result)
6582 = build_stmt_group (Then_Statements (gnat_node), false);
6583 TREE_SIDE_EFFECTS (gnu_result) = 1;
6584 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
6586 /* Now make a COND_EXPR for each of the "else if" parts. Put each
6587 into the previous "else" part and point to where to put any
6588 outer "else". Also avoid non-determinism. */
6589 if (Present (Elsif_Parts (gnat_node)))
6590 for (gnat_temp = First (Elsif_Parts (gnat_node));
6591 Present (gnat_temp); gnat_temp = Next (gnat_temp))
6593 gnu_expr = build3 (COND_EXPR, void_type_node,
6594 gnat_to_gnu (Condition (gnat_temp)),
6595 NULL_TREE, NULL_TREE);
6596 COND_EXPR_THEN (gnu_expr)
6597 = build_stmt_group (Then_Statements (gnat_temp), false);
6598 TREE_SIDE_EFFECTS (gnu_expr) = 1;
6599 set_expr_location_from_node (gnu_expr, gnat_temp);
6600 *gnu_else_ptr = gnu_expr;
6601 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
6604 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
6606 break;
6608 case N_Case_Statement:
6609 gnu_result = Case_Statement_to_gnu (gnat_node);
6610 break;
6612 case N_Loop_Statement:
6613 gnu_result = Loop_Statement_to_gnu (gnat_node);
6614 break;
6616 case N_Block_Statement:
6617 /* The only way to enter the block is to fall through to it. */
6618 if (stmt_group_may_fallthru ())
6620 start_stmt_group ();
6621 gnat_pushlevel ();
6622 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6623 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6624 gnat_poplevel ();
6625 gnu_result = end_stmt_group ();
6627 else
6628 gnu_result = alloc_stmt_list ();
6629 break;
6631 case N_Exit_Statement:
6632 gnu_result
6633 = build2 (EXIT_STMT, void_type_node,
6634 (Present (Condition (gnat_node))
6635 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
6636 (Present (Name (gnat_node))
6637 ? get_gnu_tree (Entity (Name (gnat_node)))
6638 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
6639 break;
6641 case N_Simple_Return_Statement:
6643 tree gnu_ret_obj, gnu_ret_val;
6645 /* If the subprogram is a function, we must return the expression. */
6646 if (Present (Expression (gnat_node)))
6648 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
6650 /* If this function has copy-in/copy-out parameters parameters and
6651 doesn't return by invisible reference, get the real object for
6652 the return. See Subprogram_Body_to_gnu. */
6653 if (TYPE_CI_CO_LIST (gnu_subprog_type)
6654 && !TREE_ADDRESSABLE (gnu_subprog_type))
6655 gnu_ret_obj = gnu_return_var_stack->last ();
6656 else
6657 gnu_ret_obj = DECL_RESULT (current_function_decl);
6659 /* Get the GCC tree for the expression to be returned. */
6660 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
6662 /* Do not remove the padding from GNU_RET_VAL if the inner type is
6663 self-referential since we want to allocate the fixed size. */
6664 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
6665 && TYPE_IS_PADDING_P
6666 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))
6667 && CONTAINS_PLACEHOLDER_P
6668 (TYPE_SIZE (TREE_TYPE (gnu_ret_val))))
6669 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
6671 /* If the function returns by direct reference, return a pointer
6672 to the return value. */
6673 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)
6674 || By_Ref (gnat_node))
6675 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
6677 /* Otherwise, if it returns an unconstrained array, we have to
6678 allocate a new version of the result and return it. */
6679 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type))
6681 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
6683 /* And find out whether this is a candidate for Named Return
6684 Value. If so, record it. */
6685 if (!TYPE_CI_CO_LIST (gnu_subprog_type) && optimize)
6687 tree ret_val = gnu_ret_val;
6689 /* Strip useless conversions around the return value. */
6690 if (gnat_useless_type_conversion (ret_val))
6691 ret_val = TREE_OPERAND (ret_val, 0);
6693 /* Strip unpadding around the return value. */
6694 if (TREE_CODE (ret_val) == COMPONENT_REF
6695 && TYPE_IS_PADDING_P
6696 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
6697 ret_val = TREE_OPERAND (ret_val, 0);
6699 /* Now apply the test to the return value. */
6700 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
6702 if (!f_named_ret_val)
6703 f_named_ret_val = BITMAP_GGC_ALLOC ();
6704 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
6705 if (!f_gnat_ret)
6706 f_gnat_ret = gnat_node;
6710 gnu_ret_val = build_allocator (TREE_TYPE (gnu_ret_val),
6711 gnu_ret_val,
6712 TREE_TYPE (gnu_ret_obj),
6713 Procedure_To_Call (gnat_node),
6714 Storage_Pool (gnat_node),
6715 gnat_node, false);
6718 /* Otherwise, if it returns by invisible reference, dereference
6719 the pointer it is passed using the type of the return value
6720 and build the copy operation manually. This ensures that we
6721 don't copy too much data, for example if the return type is
6722 unconstrained with a maximum size. */
6723 else if (TREE_ADDRESSABLE (gnu_subprog_type))
6725 tree gnu_ret_deref
6726 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
6727 gnu_ret_obj);
6728 gnu_result = build2 (MODIFY_EXPR, void_type_node,
6729 gnu_ret_deref, gnu_ret_val);
6730 add_stmt_with_node (gnu_result, gnat_node);
6731 gnu_ret_val = NULL_TREE;
6735 else
6736 gnu_ret_obj = gnu_ret_val = NULL_TREE;
6738 /* If we have a return label defined, convert this into a branch to
6739 that label. The return proper will be handled elsewhere. */
6740 if (gnu_return_label_stack->last ())
6742 if (gnu_ret_val)
6743 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_ret_obj,
6744 gnu_ret_val));
6746 gnu_result = build1 (GOTO_EXPR, void_type_node,
6747 gnu_return_label_stack->last ());
6749 /* When not optimizing, make sure the return is preserved. */
6750 if (!optimize && Comes_From_Source (gnat_node))
6751 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
6754 /* Otherwise, build a regular return. */
6755 else
6756 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
6758 break;
6760 case N_Goto_Statement:
6761 gnu_result
6762 = build1 (GOTO_EXPR, void_type_node, gnat_to_gnu (Name (gnat_node)));
6763 break;
6765 /***************************/
6766 /* Chapter 6: Subprograms */
6767 /***************************/
6769 case N_Subprogram_Declaration:
6770 /* Unless there is a freeze node, declare the subprogram. We consider
6771 this a "definition" even though we're not generating code for
6772 the subprogram because we will be making the corresponding GCC
6773 node here. */
6775 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6776 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
6777 NULL_TREE, 1);
6778 gnu_result = alloc_stmt_list ();
6779 break;
6781 case N_Abstract_Subprogram_Declaration:
6782 /* This subprogram doesn't exist for code generation purposes, but we
6783 have to elaborate the types of any parameters and result, unless
6784 they are imported types (nothing to generate in this case).
6786 The parameter list may contain types with freeze nodes, e.g. not null
6787 subtypes, so the subprogram itself may carry a freeze node, in which
6788 case its elaboration must be deferred. */
6790 /* Process the parameter types first. */
6791 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6792 for (gnat_temp
6793 = First_Formal_With_Extras
6794 (Defining_Entity (Specification (gnat_node)));
6795 Present (gnat_temp);
6796 gnat_temp = Next_Formal_With_Extras (gnat_temp))
6797 if (Is_Itype (Etype (gnat_temp))
6798 && !From_Limited_With (Etype (gnat_temp)))
6799 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
6801 /* Then the result type, set to Standard_Void_Type for procedures. */
6803 Entity_Id gnat_temp_type
6804 = Etype (Defining_Entity (Specification (gnat_node)));
6806 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
6807 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, 0);
6810 gnu_result = alloc_stmt_list ();
6811 break;
6813 case N_Defining_Program_Unit_Name:
6814 /* For a child unit identifier go up a level to get the specification.
6815 We get this when we try to find the spec of a child unit package
6816 that is the compilation unit being compiled. */
6817 gnu_result = gnat_to_gnu (Parent (gnat_node));
6818 break;
6820 case N_Subprogram_Body:
6821 Subprogram_Body_to_gnu (gnat_node);
6822 gnu_result = alloc_stmt_list ();
6823 break;
6825 case N_Function_Call:
6826 case N_Procedure_Call_Statement:
6827 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE, false);
6828 break;
6830 /************************/
6831 /* Chapter 7: Packages */
6832 /************************/
6834 case N_Package_Declaration:
6835 gnu_result = gnat_to_gnu (Specification (gnat_node));
6836 break;
6838 case N_Package_Specification:
6840 start_stmt_group ();
6841 process_decls (Visible_Declarations (gnat_node),
6842 Private_Declarations (gnat_node), Empty, true, true);
6843 gnu_result = end_stmt_group ();
6844 break;
6846 case N_Package_Body:
6848 /* If this is the body of a generic package - do nothing. */
6849 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
6851 gnu_result = alloc_stmt_list ();
6852 break;
6855 start_stmt_group ();
6856 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6858 if (Present (Handled_Statement_Sequence (gnat_node)))
6859 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6861 gnu_result = end_stmt_group ();
6862 break;
6864 /********************************/
6865 /* Chapter 8: Visibility Rules */
6866 /********************************/
6868 case N_Use_Package_Clause:
6869 case N_Use_Type_Clause:
6870 /* Nothing to do here - but these may appear in list of declarations. */
6871 gnu_result = alloc_stmt_list ();
6872 break;
6874 /*********************/
6875 /* Chapter 9: Tasks */
6876 /*********************/
6878 case N_Protected_Type_Declaration:
6879 gnu_result = alloc_stmt_list ();
6880 break;
6882 case N_Single_Task_Declaration:
6883 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
6884 gnu_result = alloc_stmt_list ();
6885 break;
6887 /*********************************************************/
6888 /* Chapter 10: Program Structure and Compilation Issues */
6889 /*********************************************************/
6891 case N_Compilation_Unit:
6892 /* This is not called for the main unit on which gigi is invoked. */
6893 Compilation_Unit_to_gnu (gnat_node);
6894 gnu_result = alloc_stmt_list ();
6895 break;
6897 case N_Subprogram_Body_Stub:
6898 case N_Package_Body_Stub:
6899 case N_Protected_Body_Stub:
6900 case N_Task_Body_Stub:
6901 /* Simply process whatever unit is being inserted. */
6902 if (Present (Library_Unit (gnat_node)))
6903 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
6904 else
6906 gcc_assert (type_annotate_only);
6907 gnu_result = alloc_stmt_list ();
6909 break;
6911 case N_Subunit:
6912 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
6913 break;
6915 /***************************/
6916 /* Chapter 11: Exceptions */
6917 /***************************/
6919 case N_Handled_Sequence_Of_Statements:
6920 /* If there is an At_End procedure attached to this node, and the EH
6921 mechanism is SJLJ, we must have at least a corresponding At_End
6922 handler, unless the No_Exception_Handlers restriction is set. */
6923 gcc_assert (type_annotate_only
6924 || Exception_Mechanism != Setjmp_Longjmp
6925 || No (At_End_Proc (gnat_node))
6926 || Present (Exception_Handlers (gnat_node))
6927 || No_Exception_Handlers_Set ());
6929 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
6930 break;
6932 case N_Exception_Handler:
6933 if (Exception_Mechanism == Setjmp_Longjmp)
6934 gnu_result = Exception_Handler_to_gnu_sjlj (gnat_node);
6935 else if (Exception_Mechanism == Back_End_Exceptions)
6936 gnu_result = Exception_Handler_to_gnu_zcx (gnat_node);
6937 else
6938 gcc_unreachable ();
6939 break;
6941 case N_Raise_Statement:
6942 /* Only for reraise in back-end exceptions mode. */
6943 gcc_assert (No (Name (gnat_node))
6944 && Exception_Mechanism == Back_End_Exceptions);
6946 start_stmt_group ();
6947 gnat_pushlevel ();
6949 /* Clear the current exception pointer so that the occurrence won't be
6950 deallocated. */
6951 gnu_expr = create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE,
6952 ptr_type_node, gnu_incoming_exc_ptr,
6953 false, false, false, false, NULL, gnat_node);
6955 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_incoming_exc_ptr,
6956 convert (ptr_type_node, integer_zero_node)));
6957 add_stmt (build_call_n_expr (reraise_zcx_decl, 1, gnu_expr));
6958 gnat_poplevel ();
6959 gnu_result = end_stmt_group ();
6960 break;
6962 case N_Push_Constraint_Error_Label:
6963 push_exception_label_stack (&gnu_constraint_error_label_stack,
6964 Exception_Label (gnat_node));
6965 break;
6967 case N_Push_Storage_Error_Label:
6968 push_exception_label_stack (&gnu_storage_error_label_stack,
6969 Exception_Label (gnat_node));
6970 break;
6972 case N_Push_Program_Error_Label:
6973 push_exception_label_stack (&gnu_program_error_label_stack,
6974 Exception_Label (gnat_node));
6975 break;
6977 case N_Pop_Constraint_Error_Label:
6978 gnu_constraint_error_label_stack->pop ();
6979 break;
6981 case N_Pop_Storage_Error_Label:
6982 gnu_storage_error_label_stack->pop ();
6983 break;
6985 case N_Pop_Program_Error_Label:
6986 gnu_program_error_label_stack->pop ();
6987 break;
6989 /******************************/
6990 /* Chapter 12: Generic Units */
6991 /******************************/
6993 case N_Generic_Function_Renaming_Declaration:
6994 case N_Generic_Package_Renaming_Declaration:
6995 case N_Generic_Procedure_Renaming_Declaration:
6996 case N_Generic_Package_Declaration:
6997 case N_Generic_Subprogram_Declaration:
6998 case N_Package_Instantiation:
6999 case N_Procedure_Instantiation:
7000 case N_Function_Instantiation:
7001 /* These nodes can appear on a declaration list but there is nothing to
7002 to be done with them. */
7003 gnu_result = alloc_stmt_list ();
7004 break;
7006 /**************************************************/
7007 /* Chapter 13: Representation Clauses and */
7008 /* Implementation-Dependent Features */
7009 /**************************************************/
7011 case N_Attribute_Definition_Clause:
7012 gnu_result = alloc_stmt_list ();
7014 /* The only one we need to deal with is 'Address since, for the others,
7015 the front-end puts the information elsewhere. */
7016 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
7017 break;
7019 /* And we only deal with 'Address if the object has a Freeze node. */
7020 gnat_temp = Entity (Name (gnat_node));
7021 if (No (Freeze_Node (gnat_temp)))
7022 break;
7024 /* Get the value to use as the address and save it as the equivalent
7025 for the object. When it is frozen, gnat_to_gnu_entity will do the
7026 right thing. */
7027 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
7028 break;
7030 case N_Enumeration_Representation_Clause:
7031 case N_Record_Representation_Clause:
7032 case N_At_Clause:
7033 /* We do nothing with these. SEM puts the information elsewhere. */
7034 gnu_result = alloc_stmt_list ();
7035 break;
7037 case N_Code_Statement:
7038 if (!type_annotate_only)
7040 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
7041 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
7042 tree gnu_clobbers = NULL_TREE, tail;
7043 bool allows_mem, allows_reg, fake;
7044 int ninputs, noutputs, i;
7045 const char **oconstraints;
7046 const char *constraint;
7047 char *clobber;
7049 /* First retrieve the 3 operand lists built by the front-end. */
7050 Setup_Asm_Outputs (gnat_node);
7051 while (Present (gnat_temp = Asm_Output_Variable ()))
7053 tree gnu_value = gnat_to_gnu (gnat_temp);
7054 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
7055 (Asm_Output_Constraint ()));
7057 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
7058 Next_Asm_Output ();
7061 Setup_Asm_Inputs (gnat_node);
7062 while (Present (gnat_temp = Asm_Input_Value ()))
7064 tree gnu_value = gnat_to_gnu (gnat_temp);
7065 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
7066 (Asm_Input_Constraint ()));
7068 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
7069 Next_Asm_Input ();
7072 Clobber_Setup (gnat_node);
7073 while ((clobber = Clobber_Get_Next ()))
7074 gnu_clobbers
7075 = tree_cons (NULL_TREE,
7076 build_string (strlen (clobber) + 1, clobber),
7077 gnu_clobbers);
7079 /* Then perform some standard checking and processing on the
7080 operands. In particular, mark them addressable if needed. */
7081 gnu_outputs = nreverse (gnu_outputs);
7082 noutputs = list_length (gnu_outputs);
7083 gnu_inputs = nreverse (gnu_inputs);
7084 ninputs = list_length (gnu_inputs);
7085 oconstraints = XALLOCAVEC (const char *, noutputs);
7087 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
7089 tree output = TREE_VALUE (tail);
7090 constraint
7091 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7092 oconstraints[i] = constraint;
7094 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7095 &allows_mem, &allows_reg, &fake))
7097 /* If the operand is going to end up in memory,
7098 mark it addressable. Note that we don't test
7099 allows_mem like in the input case below; this
7100 is modelled on the C front-end. */
7101 if (!allows_reg)
7103 output = remove_conversions (output, false);
7104 if (TREE_CODE (output) == CONST_DECL
7105 && DECL_CONST_CORRESPONDING_VAR (output))
7106 output = DECL_CONST_CORRESPONDING_VAR (output);
7107 if (!gnat_mark_addressable (output))
7108 output = error_mark_node;
7111 else
7112 output = error_mark_node;
7114 TREE_VALUE (tail) = output;
7117 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
7119 tree input = TREE_VALUE (tail);
7120 constraint
7121 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7123 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
7124 0, oconstraints,
7125 &allows_mem, &allows_reg))
7127 /* If the operand is going to end up in memory,
7128 mark it addressable. */
7129 if (!allows_reg && allows_mem)
7131 input = remove_conversions (input, false);
7132 if (TREE_CODE (input) == CONST_DECL
7133 && DECL_CONST_CORRESPONDING_VAR (input))
7134 input = DECL_CONST_CORRESPONDING_VAR (input);
7135 if (!gnat_mark_addressable (input))
7136 input = error_mark_node;
7139 else
7140 input = error_mark_node;
7142 TREE_VALUE (tail) = input;
7145 gnu_result = build5 (ASM_EXPR, void_type_node,
7146 gnu_template, gnu_outputs,
7147 gnu_inputs, gnu_clobbers, NULL_TREE);
7148 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
7150 else
7151 gnu_result = alloc_stmt_list ();
7153 break;
7155 /****************/
7156 /* Added Nodes */
7157 /****************/
7159 case N_Expression_With_Actions:
7160 /* This construct doesn't define a scope so we don't push a binding
7161 level around the statement list, but we wrap it in a SAVE_EXPR to
7162 protect it from unsharing. Elaborate the expression as part of the
7163 same statement group as the actions so that the type declaration
7164 gets inserted there as well. This ensures that the type elaboration
7165 code is issued past the actions computing values on which it might
7166 depend. */
7168 start_stmt_group ();
7169 add_stmt_list (Actions (gnat_node));
7170 gnu_expr = gnat_to_gnu (Expression (gnat_node));
7171 gnu_result = end_stmt_group ();
7173 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
7174 TREE_SIDE_EFFECTS (gnu_result) = 1;
7176 gnu_result
7177 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
7178 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7179 break;
7181 case N_Freeze_Entity:
7182 start_stmt_group ();
7183 process_freeze_entity (gnat_node);
7184 process_decls (Actions (gnat_node), Empty, Empty, true, true);
7185 gnu_result = end_stmt_group ();
7186 break;
7188 case N_Freeze_Generic_Entity:
7189 gnu_result = alloc_stmt_list ();
7190 break;
7192 case N_Itype_Reference:
7193 if (!present_gnu_tree (Itype (gnat_node)))
7194 process_type (Itype (gnat_node));
7196 gnu_result = alloc_stmt_list ();
7197 break;
7199 case N_Free_Statement:
7200 if (!type_annotate_only)
7202 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
7203 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
7204 tree gnu_obj_type, gnu_actual_obj_type;
7206 /* If this is a thin pointer, we must first dereference it to create
7207 a fat pointer, then go back below to a thin pointer. The reason
7208 for this is that we need to have a fat pointer someplace in order
7209 to properly compute the size. */
7210 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7211 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
7212 build_unary_op (INDIRECT_REF, NULL_TREE,
7213 gnu_ptr));
7215 /* If this is a fat pointer, the object must have been allocated with
7216 the template in front of the array. So pass the template address,
7217 and get the total size; do it by converting to a thin pointer. */
7218 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
7219 gnu_ptr
7220 = convert (build_pointer_type
7221 (TYPE_OBJECT_RECORD_TYPE
7222 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
7223 gnu_ptr);
7225 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
7227 /* If this is a thin pointer, the object must have been allocated with
7228 the template in front of the array. So pass the template address,
7229 and get the total size. */
7230 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7231 gnu_ptr
7232 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
7233 gnu_ptr,
7234 fold_build1 (NEGATE_EXPR, sizetype,
7235 byte_position
7236 (DECL_CHAIN
7237 TYPE_FIELDS ((gnu_obj_type)))));
7239 /* If we have a special dynamic constrained subtype on the node, use
7240 it to compute the size; otherwise, use the designated subtype. */
7241 if (Present (Actual_Designated_Subtype (gnat_node)))
7243 gnu_actual_obj_type
7244 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
7246 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
7247 gnu_actual_obj_type
7248 = build_unc_object_type_from_ptr (gnu_ptr_type,
7249 gnu_actual_obj_type,
7250 get_identifier ("DEALLOC"),
7251 false);
7253 else
7254 gnu_actual_obj_type = gnu_obj_type;
7256 gnu_result
7257 = build_call_alloc_dealloc (gnu_ptr,
7258 TYPE_SIZE_UNIT (gnu_actual_obj_type),
7259 gnu_obj_type,
7260 Procedure_To_Call (gnat_node),
7261 Storage_Pool (gnat_node),
7262 gnat_node);
7264 break;
7266 case N_Raise_Constraint_Error:
7267 case N_Raise_Program_Error:
7268 case N_Raise_Storage_Error:
7269 if (type_annotate_only)
7270 gnu_result = alloc_stmt_list ();
7271 else
7272 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
7273 break;
7275 case N_Validate_Unchecked_Conversion:
7276 /* The only validation we currently do on an unchecked conversion is
7277 that of aliasing assumptions. */
7278 if (flag_strict_aliasing)
7279 gnat_validate_uc_list.safe_push (gnat_node);
7280 gnu_result = alloc_stmt_list ();
7281 break;
7283 case N_Function_Specification:
7284 case N_Procedure_Specification:
7285 case N_Op_Concat:
7286 case N_Component_Association:
7287 case N_Protected_Body:
7288 case N_Task_Body:
7289 /* These nodes should only be present when annotating types. */
7290 gcc_assert (type_annotate_only);
7291 gnu_result = alloc_stmt_list ();
7292 break;
7294 default:
7295 /* Other nodes are not supposed to reach here. */
7296 gcc_unreachable ();
7299 /* If we pushed the processing of the elaboration routine, pop it back. */
7300 if (went_into_elab_proc)
7301 current_function_decl = NULL_TREE;
7303 /* When not optimizing, turn boolean rvalues B into B != false tests
7304 so that the code just below can put the location information of the
7305 reference to B on the inequality operator for better debug info. */
7306 if (!optimize
7307 && TREE_CODE (gnu_result) != INTEGER_CST
7308 && (kind == N_Identifier
7309 || kind == N_Expanded_Name
7310 || kind == N_Explicit_Dereference
7311 || kind == N_Function_Call
7312 || kind == N_Indexed_Component
7313 || kind == N_Selected_Component)
7314 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
7315 && !lvalue_required_p (gnat_node, gnu_result_type, false, false, false))
7316 gnu_result = build_binary_op (NE_EXPR, gnu_result_type,
7317 convert (gnu_result_type, gnu_result),
7318 convert (gnu_result_type,
7319 boolean_false_node));
7321 /* Set the location information on the result. Note that we may have
7322 no result if we tried to build a CALL_EXPR node to a procedure with
7323 no side-effects and optimization is enabled. */
7324 if (gnu_result && EXPR_P (gnu_result))
7325 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7327 /* If we're supposed to return something of void_type, it means we have
7328 something we're elaborating for effect, so just return. */
7329 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
7330 return gnu_result;
7332 /* If the result is a constant that overflowed, raise Constraint_Error. */
7333 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
7335 post_error ("?`Constraint_Error` will be raised at run time", gnat_node);
7336 gnu_result
7337 = build1 (NULL_EXPR, gnu_result_type,
7338 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
7339 N_Raise_Constraint_Error));
7342 /* If the result has side-effects and is of an unconstrained type, make a
7343 SAVE_EXPR so that we can be sure it will only be referenced once. But
7344 this is useless for a call to a function that returns an unconstrained
7345 type with default discriminant, as we cannot compute the size of the
7346 actual returned object. We must do this before any conversions. */
7347 if (TREE_SIDE_EFFECTS (gnu_result)
7348 && !(TREE_CODE (gnu_result) == CALL_EXPR
7349 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7350 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
7351 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))))
7352 gnu_result = gnat_stabilize_reference (gnu_result, false, NULL);
7354 /* Now convert the result to the result type, unless we are in one of the
7355 following cases:
7357 1. If this is the LHS of an assignment or an actual parameter of a
7358 call, return the result almost unmodified since the RHS will have
7359 to be converted to our type in that case, unless the result type
7360 has a simpler size. Likewise if there is just a no-op unchecked
7361 conversion in-between. Similarly, don't convert integral types
7362 that are the operands of an unchecked conversion since we need
7363 to ignore those conversions (for 'Valid).
7365 2. If we have a label (which doesn't have any well-defined type), a
7366 field or an error, return the result almost unmodified. Similarly,
7367 if the two types are record types with the same name, don't convert.
7368 This will be the case when we are converting from a packable version
7369 of a type to its original type and we need those conversions to be
7370 NOPs in order for assignments into these types to work properly.
7372 3. If the type is void or if we have no result, return error_mark_node
7373 to show we have no result.
7375 4. If this a call to a function that returns an unconstrained type with
7376 default discriminant, return the call expression unmodified since we
7377 cannot compute the size of the actual returned object.
7379 5. Finally, if the type of the result is already correct. */
7381 if (Present (Parent (gnat_node))
7382 && (lhs_or_actual_p (gnat_node)
7383 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7384 && unchecked_conversion_nop (Parent (gnat_node)))
7385 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7386 && !AGGREGATE_TYPE_P (gnu_result_type)
7387 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
7388 && !(TYPE_SIZE (gnu_result_type)
7389 && TYPE_SIZE (TREE_TYPE (gnu_result))
7390 && (AGGREGATE_TYPE_P (gnu_result_type)
7391 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
7392 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
7393 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
7394 != INTEGER_CST))
7395 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
7396 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
7397 && (CONTAINS_PLACEHOLDER_P
7398 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
7399 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
7400 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
7402 /* Remove padding only if the inner object is of self-referential
7403 size: in that case it must be an object of unconstrained type
7404 with a default discriminant and we want to avoid copying too
7405 much data. */
7406 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7407 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
7408 (TREE_TYPE (gnu_result))))))
7409 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7410 gnu_result);
7413 else if (TREE_CODE (gnu_result) == LABEL_DECL
7414 || TREE_CODE (gnu_result) == FIELD_DECL
7415 || TREE_CODE (gnu_result) == ERROR_MARK
7416 || (TYPE_NAME (gnu_result_type)
7417 == TYPE_NAME (TREE_TYPE (gnu_result))
7418 && TREE_CODE (gnu_result_type) == RECORD_TYPE
7419 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
7421 /* Remove any padding. */
7422 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7423 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7424 gnu_result);
7427 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
7428 gnu_result = error_mark_node;
7430 else if (TREE_CODE (gnu_result) == CALL_EXPR
7431 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7432 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result)))
7433 == gnu_result_type
7434 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
7437 else if (TREE_TYPE (gnu_result) != gnu_result_type)
7438 gnu_result = convert (gnu_result_type, gnu_result);
7440 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
7441 while ((TREE_CODE (gnu_result) == NOP_EXPR
7442 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
7443 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
7444 gnu_result = TREE_OPERAND (gnu_result, 0);
7446 return gnu_result;
7449 /* Subroutine of above to push the exception label stack. GNU_STACK is
7450 a pointer to the stack to update and GNAT_LABEL, if present, is the
7451 label to push onto the stack. */
7453 static void
7454 push_exception_label_stack (vec<tree, va_gc> **gnu_stack, Entity_Id gnat_label)
7456 tree gnu_label = (Present (gnat_label)
7457 ? gnat_to_gnu_entity (gnat_label, NULL_TREE, 0)
7458 : NULL_TREE);
7460 vec_safe_push (*gnu_stack, gnu_label);
7463 /* Record the current code position in GNAT_NODE. */
7465 static void
7466 record_code_position (Node_Id gnat_node)
7468 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
7470 add_stmt_with_node (stmt_stmt, gnat_node);
7471 save_gnu_tree (gnat_node, stmt_stmt, true);
7474 /* Insert the code for GNAT_NODE at the position saved for that node. */
7476 static void
7477 insert_code_for (Node_Id gnat_node)
7479 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
7480 save_gnu_tree (gnat_node, NULL_TREE, true);
7483 /* Start a new statement group chained to the previous group. */
7485 void
7486 start_stmt_group (void)
7488 struct stmt_group *group = stmt_group_free_list;
7490 /* First see if we can get one from the free list. */
7491 if (group)
7492 stmt_group_free_list = group->previous;
7493 else
7494 group = ggc_alloc<stmt_group> ();
7496 group->previous = current_stmt_group;
7497 group->stmt_list = group->block = group->cleanups = NULL_TREE;
7498 current_stmt_group = group;
7501 /* Add GNU_STMT to the current statement group. If it is an expression with
7502 no effects, it is ignored. */
7504 void
7505 add_stmt (tree gnu_stmt)
7507 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
7510 /* Similar, but the statement is always added, regardless of side-effects. */
7512 void
7513 add_stmt_force (tree gnu_stmt)
7515 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
7518 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
7520 void
7521 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
7523 /* Do not emit a location for renamings that come from generic instantiation,
7524 they are likely to disturb debugging. */
7525 if (Present (gnat_node)
7526 && !renaming_from_generic_instantiation_p (gnat_node))
7527 set_expr_location_from_node (gnu_stmt, gnat_node);
7528 add_stmt (gnu_stmt);
7531 /* Similar, but the statement is always added, regardless of side-effects. */
7533 void
7534 add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
7536 if (Present (gnat_node))
7537 set_expr_location_from_node (gnu_stmt, gnat_node);
7538 add_stmt_force (gnu_stmt);
7541 /* Add a declaration statement for GNU_DECL to the current statement group.
7542 Get SLOC from Entity_Id. */
7544 void
7545 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
7547 tree type = TREE_TYPE (gnu_decl);
7548 tree gnu_stmt, gnu_init, t;
7550 /* If this is a variable that Gigi is to ignore, we may have been given
7551 an ERROR_MARK. So test for it. We also might have been given a
7552 reference for a renaming. So only do something for a decl. Also
7553 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
7554 if (!DECL_P (gnu_decl)
7555 || (TREE_CODE (gnu_decl) == TYPE_DECL
7556 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
7557 return;
7559 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
7561 /* If we are external or global, we don't want to output the DECL_EXPR for
7562 this DECL node since we already have evaluated the expressions in the
7563 sizes and positions as globals and doing it again would be wrong. */
7564 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
7566 /* Mark everything as used to prevent node sharing with subprograms.
7567 Note that walk_tree knows how to deal with TYPE_DECL, but neither
7568 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
7569 MARK_VISITED (gnu_stmt);
7570 if (TREE_CODE (gnu_decl) == VAR_DECL
7571 || TREE_CODE (gnu_decl) == CONST_DECL)
7573 MARK_VISITED (DECL_SIZE (gnu_decl));
7574 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
7575 MARK_VISITED (DECL_INITIAL (gnu_decl));
7577 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
7578 else if (TREE_CODE (gnu_decl) == TYPE_DECL
7579 && RECORD_OR_UNION_TYPE_P (type)
7580 && !TYPE_FAT_POINTER_P (type))
7581 MARK_VISITED (TYPE_ADA_SIZE (type));
7583 else
7584 add_stmt_with_node (gnu_stmt, gnat_entity);
7586 /* If this is a variable and an initializer is attached to it, it must be
7587 valid for the context. Similar to init_const in create_var_decl_1. */
7588 if (TREE_CODE (gnu_decl) == VAR_DECL
7589 && (gnu_init = DECL_INITIAL (gnu_decl)) != NULL_TREE
7590 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
7591 || (TREE_STATIC (gnu_decl)
7592 && !initializer_constant_valid_p (gnu_init,
7593 TREE_TYPE (gnu_init)))))
7595 /* If GNU_DECL has a padded type, convert it to the unpadded
7596 type so the assignment is done properly. */
7597 if (TYPE_IS_PADDING_P (type))
7598 t = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
7599 else
7600 t = gnu_decl;
7602 gnu_stmt = build_binary_op (INIT_EXPR, NULL_TREE, t, gnu_init);
7604 DECL_INITIAL (gnu_decl) = NULL_TREE;
7605 if (TREE_READONLY (gnu_decl))
7607 TREE_READONLY (gnu_decl) = 0;
7608 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
7611 add_stmt_with_node (gnu_stmt, gnat_entity);
7615 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
7617 static tree
7618 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
7620 tree t = *tp;
7622 if (TREE_VISITED (t))
7623 *walk_subtrees = 0;
7625 /* Don't mark a dummy type as visited because we want to mark its sizes
7626 and fields once it's filled in. */
7627 else if (!TYPE_IS_DUMMY_P (t))
7628 TREE_VISITED (t) = 1;
7630 if (TYPE_P (t))
7631 TYPE_SIZES_GIMPLIFIED (t) = 1;
7633 return NULL_TREE;
7636 /* Mark nodes rooted at T with TREE_VISITED and types as having their
7637 sized gimplified. We use this to indicate all variable sizes and
7638 positions in global types may not be shared by any subprogram. */
7640 void
7641 mark_visited (tree t)
7643 walk_tree (&t, mark_visited_r, NULL, NULL);
7646 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
7647 set its location to that of GNAT_NODE if present, but with column info
7648 cleared so that conditional branches generated as part of the cleanup
7649 code do not interfere with coverage analysis tools. */
7651 static void
7652 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
7654 if (Present (gnat_node))
7655 set_expr_location_from_node1 (gnu_cleanup, gnat_node, true);
7656 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
7659 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
7661 void
7662 set_block_for_group (tree gnu_block)
7664 gcc_assert (!current_stmt_group->block);
7665 current_stmt_group->block = gnu_block;
7668 /* Return code corresponding to the current code group. It is normally
7669 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
7670 BLOCK or cleanups were set. */
7672 tree
7673 end_stmt_group (void)
7675 struct stmt_group *group = current_stmt_group;
7676 tree gnu_retval = group->stmt_list;
7678 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
7679 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
7680 make a BIND_EXPR. Note that we nest in that because the cleanup may
7681 reference variables in the block. */
7682 if (gnu_retval == NULL_TREE)
7683 gnu_retval = alloc_stmt_list ();
7685 if (group->cleanups)
7686 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
7687 group->cleanups);
7689 if (current_stmt_group->block)
7690 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
7691 gnu_retval, group->block);
7693 /* Remove this group from the stack and add it to the free list. */
7694 current_stmt_group = group->previous;
7695 group->previous = stmt_group_free_list;
7696 stmt_group_free_list = group;
7698 return gnu_retval;
7701 /* Return whether the current statement group may fall through. */
7703 static inline bool
7704 stmt_group_may_fallthru (void)
7706 if (current_stmt_group->stmt_list)
7707 return block_may_fallthru (current_stmt_group->stmt_list);
7708 else
7709 return true;
7712 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
7713 statements.*/
7715 static void
7716 add_stmt_list (List_Id gnat_list)
7718 Node_Id gnat_node;
7720 if (Present (gnat_list))
7721 for (gnat_node = First (gnat_list); Present (gnat_node);
7722 gnat_node = Next (gnat_node))
7723 add_stmt (gnat_to_gnu (gnat_node));
7726 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
7727 If BINDING_P is true, push and pop a binding level around the list. */
7729 static tree
7730 build_stmt_group (List_Id gnat_list, bool binding_p)
7732 start_stmt_group ();
7733 if (binding_p)
7734 gnat_pushlevel ();
7736 add_stmt_list (gnat_list);
7737 if (binding_p)
7738 gnat_poplevel ();
7740 return end_stmt_group ();
7743 /* Generate GIMPLE in place for the expression at *EXPR_P. */
7746 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
7747 gimple_seq *post_p ATTRIBUTE_UNUSED)
7749 tree expr = *expr_p;
7750 tree type = TREE_TYPE (expr);
7751 tree op;
7753 if (IS_ADA_STMT (expr))
7754 return gnat_gimplify_stmt (expr_p);
7756 switch (TREE_CODE (expr))
7758 case NULL_EXPR:
7759 /* If this is an aggregate type, build a null pointer of the appropriate
7760 type and dereference it. */
7761 if (AGGREGATE_TYPE_P (type)
7762 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
7763 *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE,
7764 convert (build_pointer_type (type),
7765 integer_zero_node));
7766 /* Otherwise, just make a VAR_DECL. */
7767 else
7769 *expr_p = create_tmp_var (type, NULL);
7770 TREE_NO_WARNING (*expr_p) = 1;
7773 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
7774 return GS_OK;
7776 case UNCONSTRAINED_ARRAY_REF:
7777 /* We should only do this if we are just elaborating for side-effects,
7778 but we can't know that yet. */
7779 *expr_p = TREE_OPERAND (*expr_p, 0);
7780 return GS_OK;
7782 case ADDR_EXPR:
7783 op = TREE_OPERAND (expr, 0);
7785 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
7786 is put into static memory. We know that it's going to be read-only
7787 given the semantics we have and it must be in static memory when the
7788 reference is in an elaboration procedure. */
7789 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
7791 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
7792 *expr_p = fold_convert (type, addr);
7793 return GS_ALL_DONE;
7796 return GS_UNHANDLED;
7798 case VIEW_CONVERT_EXPR:
7799 op = TREE_OPERAND (expr, 0);
7801 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
7802 type to a scalar one, explicitly create the local temporary. That's
7803 required if the type is passed by reference. */
7804 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
7805 && AGGREGATE_TYPE_P (TREE_TYPE (op))
7806 && !AGGREGATE_TYPE_P (type))
7808 tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
7809 gimple_add_tmp_var (new_var);
7811 mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
7812 gimplify_and_add (mod, pre_p);
7814 TREE_OPERAND (expr, 0) = new_var;
7815 return GS_OK;
7818 return GS_UNHANDLED;
7820 case DECL_EXPR:
7821 op = DECL_EXPR_DECL (expr);
7823 /* The expressions for the RM bounds must be gimplified to ensure that
7824 they are properly elaborated. See gimplify_decl_expr. */
7825 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
7826 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)))
7827 switch (TREE_CODE (TREE_TYPE (op)))
7829 case INTEGER_TYPE:
7830 case ENUMERAL_TYPE:
7831 case BOOLEAN_TYPE:
7832 case REAL_TYPE:
7834 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
7836 val = TYPE_RM_MIN_VALUE (type);
7837 if (val)
7839 gimplify_one_sizepos (&val, pre_p);
7840 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7841 SET_TYPE_RM_MIN_VALUE (t, val);
7844 val = TYPE_RM_MAX_VALUE (type);
7845 if (val)
7847 gimplify_one_sizepos (&val, pre_p);
7848 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7849 SET_TYPE_RM_MAX_VALUE (t, val);
7853 break;
7855 default:
7856 break;
7859 /* ... fall through ... */
7861 default:
7862 return GS_UNHANDLED;
7866 /* Generate GIMPLE in place for the statement at *STMT_P. */
7868 static enum gimplify_status
7869 gnat_gimplify_stmt (tree *stmt_p)
7871 tree stmt = *stmt_p;
7873 switch (TREE_CODE (stmt))
7875 case STMT_STMT:
7876 *stmt_p = STMT_STMT_STMT (stmt);
7877 return GS_OK;
7879 case LOOP_STMT:
7881 tree gnu_start_label = create_artificial_label (input_location);
7882 tree gnu_cond = LOOP_STMT_COND (stmt);
7883 tree gnu_update = LOOP_STMT_UPDATE (stmt);
7884 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
7886 /* Build the condition expression from the test, if any. */
7887 if (gnu_cond)
7889 /* Deal with the optimization hints. */
7890 if (LOOP_STMT_IVDEP (stmt))
7891 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7892 build_int_cst (integer_type_node,
7893 annot_expr_ivdep_kind));
7894 if (LOOP_STMT_NO_VECTOR (stmt))
7895 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7896 build_int_cst (integer_type_node,
7897 annot_expr_no_vector_kind));
7898 if (LOOP_STMT_VECTOR (stmt))
7899 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7900 build_int_cst (integer_type_node,
7901 annot_expr_vector_kind));
7903 gnu_cond
7904 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
7905 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
7908 /* Set to emit the statements of the loop. */
7909 *stmt_p = NULL_TREE;
7911 /* We first emit the start label and then a conditional jump to the
7912 end label if there's a top condition, then the update if it's at
7913 the top, then the body of the loop, then a conditional jump to
7914 the end label if there's a bottom condition, then the update if
7915 it's at the bottom, and finally a jump to the start label and the
7916 definition of the end label. */
7917 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7918 gnu_start_label),
7919 stmt_p);
7921 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
7922 append_to_statement_list (gnu_cond, stmt_p);
7924 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
7925 append_to_statement_list (gnu_update, stmt_p);
7927 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
7929 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
7930 append_to_statement_list (gnu_cond, stmt_p);
7932 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
7933 append_to_statement_list (gnu_update, stmt_p);
7935 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
7936 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
7937 append_to_statement_list (t, stmt_p);
7939 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7940 gnu_end_label),
7941 stmt_p);
7942 return GS_OK;
7945 case EXIT_STMT:
7946 /* Build a statement to jump to the corresponding end label, then
7947 see if it needs to be conditional. */
7948 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
7949 if (EXIT_STMT_COND (stmt))
7950 *stmt_p = build3 (COND_EXPR, void_type_node,
7951 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
7952 return GS_OK;
7954 default:
7955 gcc_unreachable ();
7959 /* Force references to each of the entities in packages withed by GNAT_NODE.
7960 Operate recursively but check that we aren't elaborating something more
7961 than once.
7963 This routine is exclusively called in type_annotate mode, to compute DDA
7964 information for types in withed units, for ASIS use. */
7966 static void
7967 elaborate_all_entities (Node_Id gnat_node)
7969 Entity_Id gnat_with_clause, gnat_entity;
7971 /* Process each unit only once. As we trace the context of all relevant
7972 units transitively, including generic bodies, we may encounter the
7973 same generic unit repeatedly. */
7974 if (!present_gnu_tree (gnat_node))
7975 save_gnu_tree (gnat_node, integer_zero_node, true);
7977 /* Save entities in all context units. A body may have an implicit_with
7978 on its own spec, if the context includes a child unit, so don't save
7979 the spec twice. */
7980 for (gnat_with_clause = First (Context_Items (gnat_node));
7981 Present (gnat_with_clause);
7982 gnat_with_clause = Next (gnat_with_clause))
7983 if (Nkind (gnat_with_clause) == N_With_Clause
7984 && !present_gnu_tree (Library_Unit (gnat_with_clause))
7985 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
7987 elaborate_all_entities (Library_Unit (gnat_with_clause));
7989 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
7991 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
7992 Present (gnat_entity);
7993 gnat_entity = Next_Entity (gnat_entity))
7994 if (Is_Public (gnat_entity)
7995 && Convention (gnat_entity) != Convention_Intrinsic
7996 && Ekind (gnat_entity) != E_Package
7997 && Ekind (gnat_entity) != E_Package_Body
7998 && Ekind (gnat_entity) != E_Operator
7999 && !(IN (Ekind (gnat_entity), Type_Kind)
8000 && !Is_Frozen (gnat_entity))
8001 && !((Ekind (gnat_entity) == E_Procedure
8002 || Ekind (gnat_entity) == E_Function)
8003 && Is_Intrinsic_Subprogram (gnat_entity))
8004 && !IN (Ekind (gnat_entity), Named_Kind)
8005 && !IN (Ekind (gnat_entity), Generic_Unit_Kind))
8006 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
8008 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
8010 Node_Id gnat_body
8011 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
8013 /* Retrieve compilation unit node of generic body. */
8014 while (Present (gnat_body)
8015 && Nkind (gnat_body) != N_Compilation_Unit)
8016 gnat_body = Parent (gnat_body);
8018 /* If body is available, elaborate its context. */
8019 if (Present (gnat_body))
8020 elaborate_all_entities (gnat_body);
8024 if (Nkind (Unit (gnat_node)) == N_Package_Body)
8025 elaborate_all_entities (Library_Unit (gnat_node));
8028 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
8030 static void
8031 process_freeze_entity (Node_Id gnat_node)
8033 const Entity_Id gnat_entity = Entity (gnat_node);
8034 const Entity_Kind kind = Ekind (gnat_entity);
8035 tree gnu_old, gnu_new;
8037 /* If this is a package, we need to generate code for the package. */
8038 if (kind == E_Package)
8040 insert_code_for
8041 (Parent (Corresponding_Body
8042 (Parent (Declaration_Node (gnat_entity)))));
8043 return;
8046 /* Don't do anything for class-wide types as they are always transformed
8047 into their root type. */
8048 if (kind == E_Class_Wide_Type)
8049 return;
8051 /* Check for an old definition. This freeze node might be for an Itype. */
8052 gnu_old
8053 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
8055 /* If this entity has an address representation clause, GNU_OLD is the
8056 address, so discard it here. */
8057 if (Present (Address_Clause (gnat_entity)))
8058 gnu_old = NULL_TREE;
8060 /* Don't do anything for subprograms that may have been elaborated before
8061 their freeze nodes. This can happen, for example, because of an inner
8062 call in an instance body or because of previous compilation of a spec
8063 for inlining purposes. */
8064 if (gnu_old
8065 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
8066 && (kind == E_Function || kind == E_Procedure))
8067 || (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
8068 && kind == E_Subprogram_Type)))
8069 return;
8071 /* If we have a non-dummy type old tree, we have nothing to do, except
8072 aborting if this is the public view of a private type whose full view was
8073 not delayed, as this node was never delayed as it should have been. We
8074 let this happen for concurrent types and their Corresponding_Record_Type,
8075 however, because each might legitimately be elaborated before its own
8076 freeze node, e.g. while processing the other. */
8077 if (gnu_old
8078 && !(TREE_CODE (gnu_old) == TYPE_DECL
8079 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
8081 gcc_assert ((IN (kind, Incomplete_Or_Private_Kind)
8082 && Present (Full_View (gnat_entity))
8083 && No (Freeze_Node (Full_View (gnat_entity))))
8084 || Is_Concurrent_Type (gnat_entity)
8085 || (IN (kind, Record_Kind)
8086 && Is_Concurrent_Record_Type (gnat_entity)));
8087 return;
8090 /* Reset the saved tree, if any, and elaborate the object or type for real.
8091 If there is a full view, elaborate it and use the result. And, if this
8092 is the root type of a class-wide type, reuse it for the latter. */
8093 if (gnu_old)
8095 save_gnu_tree (gnat_entity, NULL_TREE, false);
8097 if (IN (kind, Incomplete_Or_Private_Kind)
8098 && Present (Full_View (gnat_entity)))
8100 Entity_Id full_view = Full_View (gnat_entity);
8102 save_gnu_tree (full_view, NULL_TREE, false);
8104 if (IN (Ekind (full_view), Private_Kind)
8105 && Present (Underlying_Full_View (full_view)))
8107 full_view = Underlying_Full_View (full_view);
8108 save_gnu_tree (full_view, NULL_TREE, false);
8112 if (IN (kind, Type_Kind)
8113 && Present (Class_Wide_Type (gnat_entity))
8114 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8115 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
8118 if (IN (kind, Incomplete_Or_Private_Kind)
8119 && Present (Full_View (gnat_entity)))
8121 Entity_Id full_view = Full_View (gnat_entity);
8123 if (IN (Ekind (full_view), Private_Kind)
8124 && Present (Underlying_Full_View (full_view)))
8125 full_view = Underlying_Full_View (full_view);
8127 gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, 1);
8129 /* Propagate back-annotations from full view to partial view. */
8130 if (Unknown_Alignment (gnat_entity))
8131 Set_Alignment (gnat_entity, Alignment (full_view));
8133 if (Unknown_Esize (gnat_entity))
8134 Set_Esize (gnat_entity, Esize (full_view));
8136 if (Unknown_RM_Size (gnat_entity))
8137 Set_RM_Size (gnat_entity, RM_Size (full_view));
8139 /* The above call may have defined this entity (the simplest example
8140 of this is when we have a private enumeral type since the bounds
8141 will have the public view). */
8142 if (!present_gnu_tree (gnat_entity))
8143 save_gnu_tree (gnat_entity, gnu_new, false);
8145 else
8147 tree gnu_init
8148 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
8149 && present_gnu_tree (Declaration_Node (gnat_entity)))
8150 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
8152 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
8155 if (IN (kind, Type_Kind)
8156 && Present (Class_Wide_Type (gnat_entity))
8157 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8158 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
8160 /* If we have an old type and we've made pointers to this type, update those
8161 pointers. If this is a Taft amendment type in the main unit, we need to
8162 mark the type as used since other units referencing it don't see the full
8163 declaration and, therefore, cannot mark it as used themselves. */
8164 if (gnu_old)
8166 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8167 TREE_TYPE (gnu_new));
8168 if (DECL_TAFT_TYPE_P (gnu_old))
8169 used_types_insert (TREE_TYPE (gnu_new));
8173 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
8174 We make two passes, one to elaborate anything other than bodies (but
8175 we declare a function if there was no spec). The second pass
8176 elaborates the bodies.
8178 GNAT_END_LIST gives the element in the list past the end. Normally,
8179 this is Empty, but can be First_Real_Statement for a
8180 Handled_Sequence_Of_Statements.
8182 We make a complete pass through both lists if PASS1P is true, then make
8183 the second pass over both lists if PASS2P is true. The lists usually
8184 correspond to the public and private parts of a package. */
8186 static void
8187 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
8188 Node_Id gnat_end_list, bool pass1p, bool pass2p)
8190 List_Id gnat_decl_array[2];
8191 Node_Id gnat_decl;
8192 int i;
8194 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
8196 if (pass1p)
8197 for (i = 0; i <= 1; i++)
8198 if (Present (gnat_decl_array[i]))
8199 for (gnat_decl = First (gnat_decl_array[i]);
8200 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8202 /* For package specs, we recurse inside the declarations,
8203 thus taking the two pass approach inside the boundary. */
8204 if (Nkind (gnat_decl) == N_Package_Declaration
8205 && (Nkind (Specification (gnat_decl)
8206 == N_Package_Specification)))
8207 process_decls (Visible_Declarations (Specification (gnat_decl)),
8208 Private_Declarations (Specification (gnat_decl)),
8209 Empty, true, false);
8211 /* Similarly for any declarations in the actions of a
8212 freeze node. */
8213 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8215 process_freeze_entity (gnat_decl);
8216 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
8219 /* Package bodies with freeze nodes get their elaboration deferred
8220 until the freeze node, but the code must be placed in the right
8221 place, so record the code position now. */
8222 else if (Nkind (gnat_decl) == N_Package_Body
8223 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
8224 record_code_position (gnat_decl);
8226 else if (Nkind (gnat_decl) == N_Package_Body_Stub
8227 && Present (Library_Unit (gnat_decl))
8228 && Present (Freeze_Node
8229 (Corresponding_Spec
8230 (Proper_Body (Unit
8231 (Library_Unit (gnat_decl)))))))
8232 record_code_position
8233 (Proper_Body (Unit (Library_Unit (gnat_decl))));
8235 /* We defer most subprogram bodies to the second pass. */
8236 else if (Nkind (gnat_decl) == N_Subprogram_Body)
8238 if (Acts_As_Spec (gnat_decl))
8240 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
8242 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
8243 && Ekind (gnat_subprog_id) != E_Generic_Function)
8244 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8248 /* For bodies and stubs that act as their own specs, the entity
8249 itself must be elaborated in the first pass, because it may
8250 be used in other declarations. */
8251 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
8253 Node_Id gnat_subprog_id
8254 = Defining_Entity (Specification (gnat_decl));
8256 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
8257 && Ekind (gnat_subprog_id) != E_Generic_Procedure
8258 && Ekind (gnat_subprog_id) != E_Generic_Function)
8259 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8262 /* Concurrent stubs stand for the corresponding subprogram bodies,
8263 which are deferred like other bodies. */
8264 else if (Nkind (gnat_decl) == N_Task_Body_Stub
8265 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8268 else
8269 add_stmt (gnat_to_gnu (gnat_decl));
8272 /* Here we elaborate everything we deferred above except for package bodies,
8273 which are elaborated at their freeze nodes. Note that we must also
8274 go inside things (package specs and freeze nodes) the first pass did. */
8275 if (pass2p)
8276 for (i = 0; i <= 1; i++)
8277 if (Present (gnat_decl_array[i]))
8278 for (gnat_decl = First (gnat_decl_array[i]);
8279 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8281 if (Nkind (gnat_decl) == N_Subprogram_Body
8282 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
8283 || Nkind (gnat_decl) == N_Task_Body_Stub
8284 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8285 add_stmt (gnat_to_gnu (gnat_decl));
8287 else if (Nkind (gnat_decl) == N_Package_Declaration
8288 && (Nkind (Specification (gnat_decl)
8289 == N_Package_Specification)))
8290 process_decls (Visible_Declarations (Specification (gnat_decl)),
8291 Private_Declarations (Specification (gnat_decl)),
8292 Empty, false, true);
8294 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8295 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
8299 /* Make a unary operation of kind CODE using build_unary_op, but guard
8300 the operation by an overflow check. CODE can be one of NEGATE_EXPR
8301 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
8302 the operation is to be performed in that type. GNAT_NODE is the gnat
8303 node conveying the source location for which the error should be
8304 signaled. */
8306 static tree
8307 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
8308 Node_Id gnat_node)
8310 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
8312 operand = gnat_protect_expr (operand);
8314 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
8315 operand, TYPE_MIN_VALUE (gnu_type)),
8316 build_unary_op (code, gnu_type, operand),
8317 CE_Overflow_Check_Failed, gnat_node);
8320 /* Make a binary operation of kind CODE using build_binary_op, but guard
8321 the operation by an overflow check. CODE can be one of PLUS_EXPR,
8322 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
8323 Usually the operation is to be performed in that type. GNAT_NODE is
8324 the GNAT node conveying the source location for which the error should
8325 be signaled. */
8327 static tree
8328 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
8329 tree right, Node_Id gnat_node)
8331 const unsigned int precision = TYPE_PRECISION (gnu_type);
8332 tree lhs = gnat_protect_expr (left);
8333 tree rhs = gnat_protect_expr (right);
8334 tree type_max = TYPE_MAX_VALUE (gnu_type);
8335 tree type_min = TYPE_MIN_VALUE (gnu_type);
8336 tree zero = convert (gnu_type, integer_zero_node);
8337 tree gnu_expr, rhs_lt_zero, tmp1, tmp2;
8338 tree check_pos, check_neg, check;
8340 /* Assert that the precision is a power of 2. */
8341 gcc_assert ((precision & (precision - 1)) == 0);
8343 /* Prefer a constant or known-positive rhs to simplify checks. */
8344 if (!TREE_CONSTANT (rhs)
8345 && commutative_tree_code (code)
8346 && (TREE_CONSTANT (lhs)
8347 || (!tree_expr_nonnegative_p (rhs)
8348 && tree_expr_nonnegative_p (lhs))))
8350 tree tmp = lhs;
8351 lhs = rhs;
8352 rhs = tmp;
8355 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
8357 /* If we can fold the expression to a constant, just return it.
8358 The caller will deal with overflow, no need to generate a check. */
8359 if (TREE_CONSTANT (gnu_expr))
8360 return gnu_expr;
8362 rhs_lt_zero = tree_expr_nonnegative_p (rhs)
8363 ? boolean_false_node
8364 : build_binary_op (LT_EXPR, boolean_type_node, rhs, zero);
8366 /* ??? Should use more efficient check for operand_equal_p (lhs, rhs, 0) */
8368 /* Try a few strategies that may be cheaper than the general
8369 code at the end of the function, if the rhs is not known.
8370 The strategies are:
8371 - Call library function for 64-bit multiplication (complex)
8372 - Widen, if input arguments are sufficiently small
8373 - Determine overflow using wrapped result for addition/subtraction. */
8375 if (!TREE_CONSTANT (rhs))
8377 /* Even for add/subtract double size to get another base type. */
8378 const unsigned int needed_precision = precision * 2;
8380 if (code == MULT_EXPR && precision == 64)
8382 tree int_64 = gnat_type_for_size (64, 0);
8384 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
8385 convert (int_64, lhs),
8386 convert (int_64, rhs)));
8389 if (needed_precision <= BITS_PER_WORD
8390 || (code == MULT_EXPR && needed_precision <= LONG_LONG_TYPE_SIZE))
8392 tree wide_type = gnat_type_for_size (needed_precision, 0);
8393 tree wide_result = build_binary_op (code, wide_type,
8394 convert (wide_type, lhs),
8395 convert (wide_type, rhs));
8397 check = build_binary_op
8398 (TRUTH_ORIF_EXPR, boolean_type_node,
8399 build_binary_op (LT_EXPR, boolean_type_node, wide_result,
8400 convert (wide_type, type_min)),
8401 build_binary_op (GT_EXPR, boolean_type_node, wide_result,
8402 convert (wide_type, type_max)));
8404 return
8405 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8408 if (code == PLUS_EXPR || code == MINUS_EXPR)
8410 tree unsigned_type = gnat_type_for_size (precision, 1);
8411 tree wrapped_expr
8412 = convert (gnu_type,
8413 build_binary_op (code, unsigned_type,
8414 convert (unsigned_type, lhs),
8415 convert (unsigned_type, rhs)));
8417 /* Overflow when (rhs < 0) ^ (wrapped_expr < lhs)), for addition
8418 or when (rhs < 0) ^ (wrapped_expr > lhs) for subtraction. */
8419 check
8420 = build_binary_op (TRUTH_XOR_EXPR, boolean_type_node, rhs_lt_zero,
8421 build_binary_op (code == PLUS_EXPR
8422 ? LT_EXPR : GT_EXPR,
8423 boolean_type_node,
8424 wrapped_expr, lhs));
8426 return
8427 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8431 switch (code)
8433 case PLUS_EXPR:
8434 /* When rhs >= 0, overflow when lhs > type_max - rhs. */
8435 check_pos = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8436 build_binary_op (MINUS_EXPR, gnu_type,
8437 type_max, rhs)),
8439 /* When rhs < 0, overflow when lhs < type_min - rhs. */
8440 check_neg = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8441 build_binary_op (MINUS_EXPR, gnu_type,
8442 type_min, rhs));
8443 break;
8445 case MINUS_EXPR:
8446 /* When rhs >= 0, overflow when lhs < type_min + rhs. */
8447 check_pos = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8448 build_binary_op (PLUS_EXPR, gnu_type,
8449 type_min, rhs)),
8451 /* When rhs < 0, overflow when lhs > type_max + rhs. */
8452 check_neg = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8453 build_binary_op (PLUS_EXPR, gnu_type,
8454 type_max, rhs));
8455 break;
8457 case MULT_EXPR:
8458 /* The check here is designed to be efficient if the rhs is constant,
8459 but it will work for any rhs by using integer division.
8460 Four different check expressions determine whether X * C overflows,
8461 depending on C.
8462 C == 0 => false
8463 C > 0 => X > type_max / C || X < type_min / C
8464 C == -1 => X == type_min
8465 C < -1 => X > type_min / C || X < type_max / C */
8467 tmp1 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
8468 tmp2 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
8470 check_pos
8471 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
8472 build_binary_op (NE_EXPR, boolean_type_node, zero,
8473 rhs),
8474 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8475 build_binary_op (GT_EXPR,
8476 boolean_type_node,
8477 lhs, tmp1),
8478 build_binary_op (LT_EXPR,
8479 boolean_type_node,
8480 lhs, tmp2)));
8482 check_neg
8483 = fold_build3 (COND_EXPR, boolean_type_node,
8484 build_binary_op (EQ_EXPR, boolean_type_node, rhs,
8485 build_int_cst (gnu_type, -1)),
8486 build_binary_op (EQ_EXPR, boolean_type_node, lhs,
8487 type_min),
8488 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8489 build_binary_op (GT_EXPR,
8490 boolean_type_node,
8491 lhs, tmp2),
8492 build_binary_op (LT_EXPR,
8493 boolean_type_node,
8494 lhs, tmp1)));
8495 break;
8497 default:
8498 gcc_unreachable();
8501 check = fold_build3 (COND_EXPR, boolean_type_node, rhs_lt_zero, check_neg,
8502 check_pos);
8504 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8507 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
8508 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
8509 which we have to check. GNAT_NODE is the GNAT node conveying the source
8510 location for which the error should be signaled. */
8512 static tree
8513 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
8515 tree gnu_range_type = get_unpadded_type (gnat_range_type);
8516 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
8518 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
8519 This can for example happen when translating 'Val or 'Value. */
8520 if (gnu_compare_type == gnu_range_type)
8521 return gnu_expr;
8523 /* Range checks can only be applied to types with ranges. */
8524 gcc_assert (INTEGRAL_TYPE_P (gnu_range_type)
8525 || SCALAR_FLOAT_TYPE_P (gnu_range_type));
8527 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
8528 we can't do anything since we might be truncating the bounds. No
8529 check is needed in this case. */
8530 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
8531 && (TYPE_PRECISION (gnu_compare_type)
8532 < TYPE_PRECISION (get_base_type (gnu_range_type))))
8533 return gnu_expr;
8535 /* Checked expressions must be evaluated only once. */
8536 gnu_expr = gnat_protect_expr (gnu_expr);
8538 /* Note that the form of the check is
8539 (not (expr >= lo)) or (not (expr <= hi))
8540 the reason for this slightly convoluted form is that NaNs
8541 are not considered to be in range in the float case. */
8542 return emit_check
8543 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8544 invert_truthvalue
8545 (build_binary_op (GE_EXPR, boolean_type_node,
8546 convert (gnu_compare_type, gnu_expr),
8547 convert (gnu_compare_type,
8548 TYPE_MIN_VALUE
8549 (gnu_range_type)))),
8550 invert_truthvalue
8551 (build_binary_op (LE_EXPR, boolean_type_node,
8552 convert (gnu_compare_type, gnu_expr),
8553 convert (gnu_compare_type,
8554 TYPE_MAX_VALUE
8555 (gnu_range_type))))),
8556 gnu_expr, CE_Range_Check_Failed, gnat_node);
8559 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object which
8560 we are about to index, GNU_EXPR is the index expression to be checked,
8561 GNU_LOW and GNU_HIGH are the lower and upper bounds against which GNU_EXPR
8562 has to be checked. Note that for index checking we cannot simply use the
8563 emit_range_check function (although very similar code needs to be generated
8564 in both cases) since for index checking the array type against which we are
8565 checking the indices may be unconstrained and consequently we need to get
8566 the actual index bounds from the array object itself (GNU_ARRAY_OBJECT).
8567 The place where we need to do that is in subprograms having unconstrained
8568 array formal parameters. GNAT_NODE is the GNAT node conveying the source
8569 location for which the error should be signaled. */
8571 static tree
8572 emit_index_check (tree gnu_array_object, tree gnu_expr, tree gnu_low,
8573 tree gnu_high, Node_Id gnat_node)
8575 tree gnu_expr_check;
8577 /* Checked expressions must be evaluated only once. */
8578 gnu_expr = gnat_protect_expr (gnu_expr);
8580 /* Must do this computation in the base type in case the expression's
8581 type is an unsigned subtypes. */
8582 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
8584 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
8585 the object we are handling. */
8586 gnu_low = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low, gnu_array_object);
8587 gnu_high = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high, gnu_array_object);
8589 return emit_check
8590 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8591 build_binary_op (LT_EXPR, boolean_type_node,
8592 gnu_expr_check,
8593 convert (TREE_TYPE (gnu_expr_check),
8594 gnu_low)),
8595 build_binary_op (GT_EXPR, boolean_type_node,
8596 gnu_expr_check,
8597 convert (TREE_TYPE (gnu_expr_check),
8598 gnu_high))),
8599 gnu_expr, CE_Index_Check_Failed, gnat_node);
8602 /* GNU_COND contains the condition corresponding to an access, discriminant or
8603 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
8604 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
8605 REASON is the code that says why the exception was raised. GNAT_NODE is
8606 the GNAT node conveying the source location for which the error should be
8607 signaled. */
8609 static tree
8610 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
8612 tree gnu_call
8613 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
8614 tree gnu_result
8615 = fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
8616 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
8617 convert (TREE_TYPE (gnu_expr), integer_zero_node)),
8618 gnu_expr);
8620 /* GNU_RESULT has side effects if and only if GNU_EXPR has:
8621 we don't need to evaluate it just for the check. */
8622 TREE_SIDE_EFFECTS (gnu_result) = TREE_SIDE_EFFECTS (gnu_expr);
8624 return gnu_result;
8627 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
8628 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
8629 GNAT_TYPE is known to be an integral type. If TRUNCATE_P true, do a
8630 float to integer conversion with truncation; otherwise round.
8631 GNAT_NODE is the GNAT node conveying the source location for which the
8632 error should be signaled. */
8634 static tree
8635 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
8636 bool rangep, bool truncatep, Node_Id gnat_node)
8638 tree gnu_type = get_unpadded_type (gnat_type);
8639 tree gnu_in_type = TREE_TYPE (gnu_expr);
8640 tree gnu_in_basetype = get_base_type (gnu_in_type);
8641 tree gnu_base_type = get_base_type (gnu_type);
8642 tree gnu_result = gnu_expr;
8644 /* If we are not doing any checks, the output is an integral type and the
8645 input is not a floating-point type, just do the conversion. This is
8646 required for packed array types and is simpler in all cases anyway. */
8647 if (!rangep
8648 && !overflowp
8649 && INTEGRAL_TYPE_P (gnu_base_type)
8650 && !FLOAT_TYPE_P (gnu_in_type))
8651 return convert (gnu_type, gnu_expr);
8653 /* First convert the expression to its base type. This
8654 will never generate code, but makes the tests below much simpler.
8655 But don't do this if converting from an integer type to an unconstrained
8656 array type since then we need to get the bounds from the original
8657 (unpacked) type. */
8658 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
8659 gnu_result = convert (gnu_in_basetype, gnu_result);
8661 /* If overflow checks are requested, we need to be sure the result will
8662 fit in the output base type. But don't do this if the input
8663 is integer and the output floating-point. */
8664 if (overflowp
8665 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
8667 /* Ensure GNU_EXPR only gets evaluated once. */
8668 tree gnu_input = gnat_protect_expr (gnu_result);
8669 tree gnu_cond = boolean_false_node;
8670 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
8671 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
8672 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
8673 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
8675 /* Convert the lower bounds to signed types, so we're sure we're
8676 comparing them properly. Likewise, convert the upper bounds
8677 to unsigned types. */
8678 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TYPE_UNSIGNED (gnu_in_basetype))
8679 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
8681 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8682 && !TYPE_UNSIGNED (gnu_in_basetype))
8683 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
8685 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
8686 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
8688 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
8689 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
8691 /* Check each bound separately and only if the result bound
8692 is tighter than the bound on the input type. Note that all the
8693 types are base types, so the bounds must be constant. Also,
8694 the comparison is done in the base type of the input, which
8695 always has the proper signedness. First check for input
8696 integer (which means output integer), output float (which means
8697 both float), or mixed, in which case we always compare.
8698 Note that we have to do the comparison which would *fail* in the
8699 case of an error since if it's an FP comparison and one of the
8700 values is a NaN or Inf, the comparison will fail. */
8701 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8702 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
8703 : (FLOAT_TYPE_P (gnu_base_type)
8704 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
8705 TREE_REAL_CST (gnu_out_lb))
8706 : 1))
8707 gnu_cond
8708 = invert_truthvalue
8709 (build_binary_op (GE_EXPR, boolean_type_node,
8710 gnu_input, convert (gnu_in_basetype,
8711 gnu_out_lb)));
8713 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8714 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
8715 : (FLOAT_TYPE_P (gnu_base_type)
8716 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
8717 TREE_REAL_CST (gnu_in_lb))
8718 : 1))
8719 gnu_cond
8720 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
8721 invert_truthvalue
8722 (build_binary_op (LE_EXPR, boolean_type_node,
8723 gnu_input,
8724 convert (gnu_in_basetype,
8725 gnu_out_ub))));
8727 if (!integer_zerop (gnu_cond))
8728 gnu_result = emit_check (gnu_cond, gnu_input,
8729 CE_Overflow_Check_Failed, gnat_node);
8732 /* Now convert to the result base type. If this is a non-truncating
8733 float-to-integer conversion, round. */
8734 if (INTEGRAL_TYPE_P (gnu_base_type)
8735 && FLOAT_TYPE_P (gnu_in_basetype)
8736 && !truncatep)
8738 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
8739 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
8740 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
8741 const struct real_format *fmt;
8743 /* The following calculations depend on proper rounding to even
8744 of each arithmetic operation. In order to prevent excess
8745 precision from spoiling this property, use the widest hardware
8746 floating-point type if FP_ARITH_MAY_WIDEN is true. */
8747 calc_type
8748 = fp_arith_may_widen ? longest_float_type_node : gnu_in_basetype;
8750 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
8751 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
8752 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
8753 REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf,
8754 half_minus_pred_half);
8755 gnu_pred_half = build_real (calc_type, pred_half);
8757 /* If the input is strictly negative, subtract this value
8758 and otherwise add it from the input. For 0.5, the result
8759 is exactly between 1.0 and the machine number preceding 1.0
8760 (for calc_type). Since the last bit of 1.0 is even, this 0.5
8761 will round to 1.0, while all other number with an absolute
8762 value less than 0.5 round to 0.0. For larger numbers exactly
8763 halfway between integers, rounding will always be correct as
8764 the true mathematical result will be closer to the higher
8765 integer compared to the lower one. So, this constant works
8766 for all floating-point numbers.
8768 The reason to use the same constant with subtract/add instead
8769 of a positive and negative constant is to allow the comparison
8770 to be scheduled in parallel with retrieval of the constant and
8771 conversion of the input to the calc_type (if necessary). */
8773 gnu_zero = convert (gnu_in_basetype, integer_zero_node);
8774 gnu_result = gnat_protect_expr (gnu_result);
8775 gnu_conv = convert (calc_type, gnu_result);
8776 gnu_comp
8777 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
8778 gnu_add_pred_half
8779 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8780 gnu_subtract_pred_half
8781 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8782 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
8783 gnu_add_pred_half, gnu_subtract_pred_half);
8786 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8787 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
8788 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
8789 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
8790 else
8791 gnu_result = convert (gnu_base_type, gnu_result);
8793 /* Finally, do the range check if requested. Note that if the result type
8794 is a modular type, the range check is actually an overflow check. */
8795 if (rangep
8796 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8797 && TYPE_MODULAR_P (gnu_base_type) && overflowp))
8798 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
8800 return convert (gnu_type, gnu_result);
8803 /* Return true if GNU_EXPR can be directly addressed. This is the case
8804 unless it is an expression involving computation or if it involves a
8805 reference to a bitfield or to an object not sufficiently aligned for
8806 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
8807 be directly addressed as an object of this type.
8809 *** Notes on addressability issues in the Ada compiler ***
8811 This predicate is necessary in order to bridge the gap between Gigi
8812 and the middle-end about addressability of GENERIC trees. A tree
8813 is said to be addressable if it can be directly addressed, i.e. if
8814 its address can be taken, is a multiple of the type's alignment on
8815 strict-alignment architectures and returns the first storage unit
8816 assigned to the object represented by the tree.
8818 In the C family of languages, everything is in practice addressable
8819 at the language level, except for bit-fields. This means that these
8820 compilers will take the address of any tree that doesn't represent
8821 a bit-field reference and expect the result to be the first storage
8822 unit assigned to the object. Even in cases where this will result
8823 in unaligned accesses at run time, nothing is supposed to be done
8824 and the program is considered as erroneous instead (see PR c/18287).
8826 The implicit assumptions made in the middle-end are in keeping with
8827 the C viewpoint described above:
8828 - the address of a bit-field reference is supposed to be never
8829 taken; the compiler (generally) will stop on such a construct,
8830 - any other tree is addressable if it is formally addressable,
8831 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
8833 In Ada, the viewpoint is the opposite one: nothing is addressable
8834 at the language level unless explicitly declared so. This means
8835 that the compiler will both make sure that the trees representing
8836 references to addressable ("aliased" in Ada parlance) objects are
8837 addressable and make no real attempts at ensuring that the trees
8838 representing references to non-addressable objects are addressable.
8840 In the first case, Ada is effectively equivalent to C and handing
8841 down the direct result of applying ADDR_EXPR to these trees to the
8842 middle-end works flawlessly. In the second case, Ada cannot afford
8843 to consider the program as erroneous if the address of trees that
8844 are not addressable is requested for technical reasons, unlike C;
8845 as a consequence, the Ada compiler must arrange for either making
8846 sure that this address is not requested in the middle-end or for
8847 compensating by inserting temporaries if it is requested in Gigi.
8849 The first goal can be achieved because the middle-end should not
8850 request the address of non-addressable trees on its own; the only
8851 exception is for the invocation of low-level block operations like
8852 memcpy, for which the addressability requirements are lower since
8853 the type's alignment can be disregarded. In practice, this means
8854 that Gigi must make sure that such operations cannot be applied to
8855 non-BLKmode bit-fields.
8857 The second goal is achieved by means of the addressable_p predicate,
8858 which computes whether a temporary must be inserted by Gigi when the
8859 address of a tree is requested; if so, the address of the temporary
8860 will be used in lieu of that of the original tree and some glue code
8861 generated to connect everything together. */
8863 static bool
8864 addressable_p (tree gnu_expr, tree gnu_type)
8866 /* For an integral type, the size of the actual type of the object may not
8867 be greater than that of the expected type, otherwise an indirect access
8868 in the latter type wouldn't correctly set all the bits of the object. */
8869 if (gnu_type
8870 && INTEGRAL_TYPE_P (gnu_type)
8871 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
8872 return false;
8874 /* The size of the actual type of the object may not be smaller than that
8875 of the expected type, otherwise an indirect access in the latter type
8876 would be larger than the object. But only record types need to be
8877 considered in practice for this case. */
8878 if (gnu_type
8879 && TREE_CODE (gnu_type) == RECORD_TYPE
8880 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
8881 return false;
8883 switch (TREE_CODE (gnu_expr))
8885 case VAR_DECL:
8886 case PARM_DECL:
8887 case FUNCTION_DECL:
8888 case RESULT_DECL:
8889 /* All DECLs are addressable: if they are in a register, we can force
8890 them to memory. */
8891 return true;
8893 case UNCONSTRAINED_ARRAY_REF:
8894 case INDIRECT_REF:
8895 /* Taking the address of a dereference yields the original pointer. */
8896 return true;
8898 case STRING_CST:
8899 case INTEGER_CST:
8900 /* Taking the address yields a pointer to the constant pool. */
8901 return true;
8903 case CONSTRUCTOR:
8904 /* Taking the address of a static constructor yields a pointer to the
8905 tree constant pool. */
8906 return TREE_STATIC (gnu_expr) ? true : false;
8908 case NULL_EXPR:
8909 case SAVE_EXPR:
8910 case CALL_EXPR:
8911 case PLUS_EXPR:
8912 case MINUS_EXPR:
8913 case BIT_IOR_EXPR:
8914 case BIT_XOR_EXPR:
8915 case BIT_AND_EXPR:
8916 case BIT_NOT_EXPR:
8917 /* All rvalues are deemed addressable since taking their address will
8918 force a temporary to be created by the middle-end. */
8919 return true;
8921 case COMPOUND_EXPR:
8922 /* The address of a compound expression is that of its 2nd operand. */
8923 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
8925 case COND_EXPR:
8926 /* We accept &COND_EXPR as soon as both operands are addressable and
8927 expect the outcome to be the address of the selected operand. */
8928 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
8929 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
8931 case COMPONENT_REF:
8932 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
8933 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
8934 the field is sufficiently aligned, in case it is subject
8935 to a pragma Component_Alignment. But we don't need to
8936 check the alignment of the containing record, as it is
8937 guaranteed to be not smaller than that of its most
8938 aligned field that is not a bit-field. */
8939 && (!STRICT_ALIGNMENT
8940 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
8941 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
8942 /* The field of a padding record is always addressable. */
8943 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
8944 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8946 case ARRAY_REF: case ARRAY_RANGE_REF:
8947 case REALPART_EXPR: case IMAGPART_EXPR:
8948 case NOP_EXPR:
8949 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
8951 case CONVERT_EXPR:
8952 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
8953 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8955 case VIEW_CONVERT_EXPR:
8957 /* This is addressable if we can avoid a copy. */
8958 tree type = TREE_TYPE (gnu_expr);
8959 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
8960 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
8961 && (!STRICT_ALIGNMENT
8962 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8963 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
8964 || ((TYPE_MODE (type) == BLKmode
8965 || TYPE_MODE (inner_type) == BLKmode)
8966 && (!STRICT_ALIGNMENT
8967 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8968 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
8969 || TYPE_ALIGN_OK (type)
8970 || TYPE_ALIGN_OK (inner_type))))
8971 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8974 default:
8975 return false;
8979 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
8980 a separate Freeze node exists, delay the bulk of the processing. Otherwise
8981 make a GCC type for GNAT_ENTITY and set up the correspondence. */
8983 void
8984 process_type (Entity_Id gnat_entity)
8986 tree gnu_old
8987 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
8988 tree gnu_new;
8990 /* If we are to delay elaboration of this type, just do any
8991 elaborations needed for expressions within the declaration and
8992 make a dummy type entry for this node and its Full_View (if
8993 any) in case something points to it. Don't do this if it
8994 has already been done (the only way that can happen is if
8995 the private completion is also delayed). */
8996 if (Present (Freeze_Node (gnat_entity))
8997 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8998 && Present (Full_View (gnat_entity))
8999 && Present (Freeze_Node (Full_View (gnat_entity)))
9000 && !present_gnu_tree (Full_View (gnat_entity))))
9002 elaborate_entity (gnat_entity);
9004 if (!gnu_old)
9006 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
9007 save_gnu_tree (gnat_entity, gnu_decl, false);
9008 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
9009 && Present (Full_View (gnat_entity)))
9011 if (Has_Completion_In_Body (gnat_entity))
9012 DECL_TAFT_TYPE_P (gnu_decl) = 1;
9013 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
9017 return;
9020 /* If we saved away a dummy type for this node it means that this
9021 made the type that corresponds to the full type of an incomplete
9022 type. Clear that type for now and then update the type in the
9023 pointers. */
9024 if (gnu_old)
9026 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
9027 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
9029 save_gnu_tree (gnat_entity, NULL_TREE, false);
9032 /* Now fully elaborate the type. */
9033 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
9034 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
9036 /* If we have an old type and we've made pointers to this type, update those
9037 pointers. If this is a Taft amendment type in the main unit, we need to
9038 mark the type as used since other units referencing it don't see the full
9039 declaration and, therefore, cannot mark it as used themselves. */
9040 if (gnu_old)
9042 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
9043 TREE_TYPE (gnu_new));
9044 if (DECL_TAFT_TYPE_P (gnu_old))
9045 used_types_insert (TREE_TYPE (gnu_new));
9048 /* If this is a record type corresponding to a task or protected type
9049 that is a completion of an incomplete type, perform a similar update
9050 on the type. ??? Including protected types here is a guess. */
9051 if (IN (Ekind (gnat_entity), Record_Kind)
9052 && Is_Concurrent_Record_Type (gnat_entity)
9053 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
9055 tree gnu_task_old
9056 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
9058 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
9059 NULL_TREE, false);
9060 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
9061 gnu_new, false);
9063 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
9064 TREE_TYPE (gnu_new));
9068 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
9069 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
9070 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
9072 static tree
9073 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
9075 tree gnu_list = NULL_TREE, gnu_result;
9077 /* We test for GNU_FIELD being empty in the case where a variant
9078 was the last thing since we don't take things off GNAT_ASSOC in
9079 that case. We check GNAT_ASSOC in case we have a variant, but it
9080 has no fields. */
9082 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
9084 Node_Id gnat_field = First (Choices (gnat_assoc));
9085 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
9086 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
9088 /* The expander is supposed to put a single component selector name
9089 in every record component association. */
9090 gcc_assert (No (Next (gnat_field)));
9092 /* Ignore fields that have Corresponding_Discriminants since we'll
9093 be setting that field in the parent. */
9094 if (Present (Corresponding_Discriminant (Entity (gnat_field)))
9095 && Is_Tagged_Type (Scope (Entity (gnat_field))))
9096 continue;
9098 /* Also ignore discriminants of Unchecked_Unions. */
9099 if (Is_Unchecked_Union (gnat_entity)
9100 && Ekind (Entity (gnat_field)) == E_Discriminant)
9101 continue;
9103 /* Before assigning a value in an aggregate make sure range checks
9104 are done if required. Then convert to the type of the field. */
9105 if (Do_Range_Check (Expression (gnat_assoc)))
9106 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
9108 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
9110 /* Add the field and expression to the list. */
9111 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
9114 gnu_result = extract_values (gnu_list, gnu_type);
9116 #ifdef ENABLE_CHECKING
9117 /* Verify that every entry in GNU_LIST was used. */
9118 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
9119 gcc_assert (TREE_ADDRESSABLE (gnu_list));
9120 #endif
9122 return gnu_result;
9125 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
9126 the first element of an array aggregate. It may itself be an aggregate.
9127 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
9128 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
9129 for range checking. */
9131 static tree
9132 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
9133 Entity_Id gnat_component_type)
9135 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
9136 tree gnu_expr;
9137 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
9139 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
9141 /* If the expression is itself an array aggregate then first build the
9142 innermost constructor if it is part of our array (multi-dimensional
9143 case). */
9144 if (Nkind (gnat_expr) == N_Aggregate
9145 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
9146 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
9147 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
9148 TREE_TYPE (gnu_array_type),
9149 gnat_component_type);
9150 else
9152 gnu_expr = gnat_to_gnu (gnat_expr);
9154 /* Before assigning the element to the array, make sure it is
9155 in range. */
9156 if (Do_Range_Check (gnat_expr))
9157 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
9160 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
9161 convert (TREE_TYPE (gnu_array_type), gnu_expr));
9163 gnu_index = int_const_binop (PLUS_EXPR, gnu_index,
9164 convert (TREE_TYPE (gnu_index),
9165 integer_one_node));
9168 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
9171 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
9172 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
9173 of the associations that are from RECORD_TYPE. If we see an internal
9174 record, make a recursive call to fill it in as well. */
9176 static tree
9177 extract_values (tree values, tree record_type)
9179 tree field, tem;
9180 vec<constructor_elt, va_gc> *v = NULL;
9182 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9184 tree value = 0;
9186 /* _Parent is an internal field, but may have values in the aggregate,
9187 so check for values first. */
9188 if ((tem = purpose_member (field, values)))
9190 value = TREE_VALUE (tem);
9191 TREE_ADDRESSABLE (tem) = 1;
9194 else if (DECL_INTERNAL_P (field))
9196 value = extract_values (values, TREE_TYPE (field));
9197 if (TREE_CODE (value) == CONSTRUCTOR
9198 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
9199 value = 0;
9201 else
9202 /* If we have a record subtype, the names will match, but not the
9203 actual FIELD_DECLs. */
9204 for (tem = values; tem; tem = TREE_CHAIN (tem))
9205 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
9207 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
9208 TREE_ADDRESSABLE (tem) = 1;
9211 if (!value)
9212 continue;
9214 CONSTRUCTOR_APPEND_ELT (v, field, value);
9217 return gnat_build_constructor (record_type, v);
9220 /* Process a N_Validate_Unchecked_Conversion node. */
9222 static void
9223 validate_unchecked_conversion (Node_Id gnat_node)
9225 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
9226 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
9228 /* If the target is a pointer type, see if we are either converting from a
9229 non-pointer or from a pointer to a type with a different alias set and
9230 warn if so, unless the pointer has been marked to alias everything. */
9231 if (POINTER_TYPE_P (gnu_target_type)
9232 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
9234 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
9235 ? TREE_TYPE (gnu_source_type)
9236 : NULL_TREE;
9237 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
9238 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9240 if (target_alias_set != 0
9241 && (!POINTER_TYPE_P (gnu_source_type)
9242 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9243 target_alias_set)))
9245 post_error_ne ("?possible aliasing problem for type&",
9246 gnat_node, Target_Type (gnat_node));
9247 post_error ("\\?use -fno-strict-aliasing switch for references",
9248 gnat_node);
9249 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
9250 gnat_node, Target_Type (gnat_node));
9254 /* Likewise if the target is a fat pointer type, but we have no mechanism to
9255 mitigate the problem in this case, so we unconditionally warn. */
9256 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
9258 tree gnu_source_desig_type
9259 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
9260 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
9261 : NULL_TREE;
9262 tree gnu_target_desig_type
9263 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
9264 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9266 if (target_alias_set != 0
9267 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
9268 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9269 target_alias_set)))
9271 post_error_ne ("?possible aliasing problem for type&",
9272 gnat_node, Target_Type (gnat_node));
9273 post_error ("\\?use -fno-strict-aliasing switch for references",
9274 gnat_node);
9279 /* EXP is to be treated as an array or record. Handle the cases when it is
9280 an access object and perform the required dereferences. */
9282 static tree
9283 maybe_implicit_deref (tree exp)
9285 /* If the type is a pointer, dereference it. */
9286 if (POINTER_TYPE_P (TREE_TYPE (exp))
9287 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp)))
9288 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
9290 /* If we got a padded type, remove it too. */
9291 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
9292 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
9294 return exp;
9297 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
9298 location and false if it doesn't. In the former case, set the Gigi global
9299 variable REF_FILENAME to the simple debug file name as given by sinput.
9300 If clear_column is true, set column information to 0. */
9302 static bool
9303 Sloc_to_locus1 (Source_Ptr Sloc, location_t *locus, bool clear_column)
9305 if (Sloc == No_Location)
9306 return false;
9308 if (Sloc <= Standard_Location)
9310 *locus = BUILTINS_LOCATION;
9311 return false;
9313 else
9315 Source_File_Index file = Get_Source_File_Index (Sloc);
9316 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
9317 Column_Number column = (clear_column ? 0 : Get_Column_Number (Sloc));
9318 struct line_map *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
9320 /* We can have zero if pragma Source_Reference is in effect. */
9321 if (line < 1)
9322 line = 1;
9324 /* Translate the location. */
9325 *locus = linemap_position_for_line_and_column (map, line, column);
9328 ref_filename
9329 = IDENTIFIER_POINTER
9330 (get_identifier
9331 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
9333 return true;
9336 /* Similar to the above, not clearing the column information. */
9338 bool
9339 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
9341 return Sloc_to_locus1 (Sloc, locus, false);
9344 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
9345 don't do anything if it doesn't correspond to a source location. */
9347 static void
9348 set_expr_location_from_node1 (tree node, Node_Id gnat_node, bool clear_column)
9350 location_t locus;
9352 if (!Sloc_to_locus1 (Sloc (gnat_node), &locus, clear_column))
9353 return;
9355 SET_EXPR_LOCATION (node, locus);
9358 /* Similar to the above, not clearing the column information. */
9360 static void
9361 set_expr_location_from_node (tree node, Node_Id gnat_node)
9363 set_expr_location_from_node1 (node, gnat_node, false);
9366 /* More elaborate version of set_expr_location_from_node to be used in more
9367 general contexts, for example the result of the translation of a generic
9368 GNAT node. */
9370 static void
9371 set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
9373 /* Set the location information on the node if it is a real expression.
9374 References can be reused for multiple GNAT nodes and they would get
9375 the location information of their last use. Also make sure not to
9376 overwrite an existing location as it is probably more precise. */
9378 switch (TREE_CODE (node))
9380 CASE_CONVERT:
9381 case NON_LVALUE_EXPR:
9382 case SAVE_EXPR:
9383 break;
9385 case COMPOUND_EXPR:
9386 if (EXPR_P (TREE_OPERAND (node, 1)))
9387 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
9389 /* ... fall through ... */
9391 default:
9392 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
9394 set_expr_location_from_node (node, gnat_node);
9395 set_end_locus_from_node (node, gnat_node);
9397 break;
9401 /* Return a colon-separated list of encodings contained in encoded Ada
9402 name. */
9404 static const char *
9405 extract_encoding (const char *name)
9407 char *encoding = (char *) ggc_alloc_atomic (strlen (name));
9408 get_encoding (name, encoding);
9409 return encoding;
9412 /* Extract the Ada name from an encoded name. */
9414 static const char *
9415 decode_name (const char *name)
9417 char *decoded = (char *) ggc_alloc_atomic (strlen (name) * 2 + 60);
9418 __gnat_decode (name, decoded, 0);
9419 return decoded;
9422 /* Post an error message. MSG is the error message, properly annotated.
9423 NODE is the node at which to post the error and the node to use for the
9424 '&' substitution. */
9426 void
9427 post_error (const char *msg, Node_Id node)
9429 String_Template temp;
9430 String_Pointer sp;
9432 if (No (node))
9433 return;
9435 temp.Low_Bound = 1;
9436 temp.High_Bound = strlen (msg);
9437 sp.Bounds = &temp;
9438 sp.Array = msg;
9439 Error_Msg_N (sp, node);
9442 /* Similar to post_error, but NODE is the node at which to post the error and
9443 ENT is the node to use for the '&' substitution. */
9445 void
9446 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
9448 String_Template temp;
9449 String_Pointer sp;
9451 if (No (node))
9452 return;
9454 temp.Low_Bound = 1;
9455 temp.High_Bound = strlen (msg);
9456 sp.Bounds = &temp;
9457 sp.Array = msg;
9458 Error_Msg_NE (sp, node, ent);
9461 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
9463 void
9464 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
9466 Error_Msg_Uint_1 = UI_From_Int (num);
9467 post_error_ne (msg, node, ent);
9470 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
9471 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
9472 most sense. Return true if a sensible assignment was performed. */
9474 static bool
9475 set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
9477 Node_Id gnat_end_label = Empty;
9478 location_t end_locus;
9480 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
9481 end_locus when there is one. We consider only GNAT nodes with a possible
9482 End_Label attached. If the End_Label actually was unassigned, fallback
9483 on the original node. We'd better assign an explicit sloc associated with
9484 the outer construct in any case. */
9486 switch (Nkind (gnat_node))
9488 case N_Package_Body:
9489 case N_Subprogram_Body:
9490 case N_Block_Statement:
9491 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
9492 break;
9494 case N_Package_Declaration:
9495 gnat_end_label = End_Label (Specification (gnat_node));
9496 break;
9498 default:
9499 return false;
9502 gnat_node = Present (gnat_end_label) ? gnat_end_label : gnat_node;
9504 /* Some expanded subprograms have neither an End_Label nor a Sloc
9505 attached. Notify that to callers. For a block statement with no
9506 End_Label, clear column information, so that the tree for a
9507 transient block does not receive the sloc of a source condition. */
9509 if (!Sloc_to_locus1 (Sloc (gnat_node), &end_locus,
9510 No (gnat_end_label) &&
9511 (Nkind (gnat_node) == N_Block_Statement)))
9512 return false;
9514 switch (TREE_CODE (gnu_node))
9516 case BIND_EXPR:
9517 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
9518 return true;
9520 case FUNCTION_DECL:
9521 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
9522 return true;
9524 default:
9525 return false;
9529 /* Similar to post_error_ne, but T is a GCC tree representing the number to
9530 write. If T represents a constant, the text inside curly brackets in
9531 MSG will be output (presumably including a '^'). Otherwise it will not
9532 be output and the text inside square brackets will be output instead. */
9534 void
9535 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
9537 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
9538 char start_yes, end_yes, start_no, end_no;
9539 const char *p;
9540 char *q;
9542 if (TREE_CODE (t) == INTEGER_CST)
9544 Error_Msg_Uint_1 = UI_From_gnu (t);
9545 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
9547 else
9548 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
9550 for (p = msg, q = new_msg; *p; p++)
9552 if (*p == start_yes)
9553 for (p++; *p != end_yes; p++)
9554 *q++ = *p;
9555 else if (*p == start_no)
9556 for (p++; *p != end_no; p++)
9558 else
9559 *q++ = *p;
9562 *q = 0;
9564 post_error_ne (new_msg, node, ent);
9567 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
9569 void
9570 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
9571 int num)
9573 Error_Msg_Uint_2 = UI_From_Int (num);
9574 post_error_ne_tree (msg, node, ent, t);
9577 /* Initialize the table that maps GNAT codes to GCC codes for simple
9578 binary and unary operations. */
9580 static void
9581 init_code_table (void)
9583 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
9584 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
9586 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
9587 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
9588 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
9589 gnu_codes[N_Op_Eq] = EQ_EXPR;
9590 gnu_codes[N_Op_Ne] = NE_EXPR;
9591 gnu_codes[N_Op_Lt] = LT_EXPR;
9592 gnu_codes[N_Op_Le] = LE_EXPR;
9593 gnu_codes[N_Op_Gt] = GT_EXPR;
9594 gnu_codes[N_Op_Ge] = GE_EXPR;
9595 gnu_codes[N_Op_Add] = PLUS_EXPR;
9596 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
9597 gnu_codes[N_Op_Multiply] = MULT_EXPR;
9598 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
9599 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
9600 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
9601 gnu_codes[N_Op_Abs] = ABS_EXPR;
9602 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
9603 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
9604 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
9605 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
9606 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
9607 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
9610 /* Return a label to branch to for the exception type in KIND or NULL_TREE
9611 if none. */
9613 tree
9614 get_exception_label (char kind)
9616 if (kind == N_Raise_Constraint_Error)
9617 return gnu_constraint_error_label_stack->last ();
9618 else if (kind == N_Raise_Storage_Error)
9619 return gnu_storage_error_label_stack->last ();
9620 else if (kind == N_Raise_Program_Error)
9621 return gnu_program_error_label_stack->last ();
9622 else
9623 return NULL_TREE;
9626 /* Return the decl for the current elaboration procedure. */
9628 tree
9629 get_elaboration_procedure (void)
9631 return gnu_elab_proc_stack->last ();
9634 #include "gt-ada-trans.h"