* cfgloop.h (struct loop): Move force_vectorize down.
[official-gcc.git] / gcc / ada / gcc-interface / trans.c
blob3b6d5bd0d5e14a23ff18de6b21ea336cb16ddfd3
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2014, 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 "tree.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "stmt.h"
34 #include "varasm.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "libfuncs.h" /* For set_stack_check_libfunc. */
38 #include "tree-iterator.h"
39 #include "pointer-set.h"
40 #include "gimple-expr.h"
41 #include "gimplify.h"
42 #include "bitmap.h"
43 #include "cgraph.h"
44 #include "diagnostic.h"
45 #include "opts.h"
46 #include "target.h"
47 #include "common/common-target.h"
49 #include "ada.h"
50 #include "adadecode.h"
51 #include "types.h"
52 #include "atree.h"
53 #include "elists.h"
54 #include "namet.h"
55 #include "nlists.h"
56 #include "snames.h"
57 #include "stringt.h"
58 #include "uintp.h"
59 #include "urealp.h"
60 #include "fe.h"
61 #include "sinfo.h"
62 #include "einfo.h"
63 #include "gadaint.h"
64 #include "ada-tree.h"
65 #include "gigi.h"
67 /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
68 for fear of running out of stack space. If we need more, we use xmalloc
69 instead. */
70 #define ALLOCA_THRESHOLD 1000
72 /* In configurations where blocks have no end_locus attached, just
73 sink assignments into a dummy global. */
74 #ifndef BLOCK_SOURCE_END_LOCATION
75 static location_t block_end_locus_sink;
76 #define BLOCK_SOURCE_END_LOCATION(BLOCK) block_end_locus_sink
77 #endif
79 /* For efficient float-to-int rounding, it is necessary to know whether
80 floating-point arithmetic may use wider intermediate results. When
81 FP_ARITH_MAY_WIDEN is not defined, be conservative and only assume
82 that arithmetic does not widen if double precision is emulated. */
83 #ifndef FP_ARITH_MAY_WIDEN
84 #if defined(HAVE_extendsfdf2)
85 #define FP_ARITH_MAY_WIDEN HAVE_extendsfdf2
86 #else
87 #define FP_ARITH_MAY_WIDEN 0
88 #endif
89 #endif
91 /* Pointers to front-end tables accessed through macros. */
92 struct Node *Nodes_Ptr;
93 struct Flags *Flags_Ptr;
94 Node_Id *Next_Node_Ptr;
95 Node_Id *Prev_Node_Ptr;
96 struct Elist_Header *Elists_Ptr;
97 struct Elmt_Item *Elmts_Ptr;
98 struct String_Entry *Strings_Ptr;
99 Char_Code *String_Chars_Ptr;
100 struct List_Header *List_Headers_Ptr;
102 /* Highest number in the front-end node table. */
103 int max_gnat_nodes;
105 /* Current node being treated, in case abort called. */
106 Node_Id error_gnat_node;
108 /* True when gigi is being called on an analyzed but unexpanded
109 tree, and the only purpose of the call is to properly annotate
110 types with representation information. */
111 bool type_annotate_only;
113 /* Current filename without path. */
114 const char *ref_filename;
117 /* List of N_Validate_Unchecked_Conversion nodes in the unit. */
118 static vec<Node_Id> gnat_validate_uc_list;
120 /* When not optimizing, we cache the 'First, 'Last and 'Length attributes
121 of unconstrained array IN parameters to avoid emitting a great deal of
122 redundant instructions to recompute them each time. */
123 struct GTY (()) parm_attr_d {
124 int id; /* GTY doesn't like Entity_Id. */
125 int dim;
126 tree first;
127 tree last;
128 tree length;
131 typedef struct parm_attr_d *parm_attr;
134 struct GTY(()) language_function {
135 vec<parm_attr, va_gc> *parm_attr_cache;
136 bitmap named_ret_val;
137 vec<tree, va_gc> *other_ret_val;
138 int gnat_ret;
141 #define f_parm_attr_cache \
142 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
144 #define f_named_ret_val \
145 DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val
147 #define f_other_ret_val \
148 DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val
150 #define f_gnat_ret \
151 DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret
153 /* A structure used to gather together information about a statement group.
154 We use this to gather related statements, for example the "then" part
155 of a IF. In the case where it represents a lexical scope, we may also
156 have a BLOCK node corresponding to it and/or cleanups. */
158 struct GTY((chain_next ("%h.previous"))) stmt_group {
159 struct stmt_group *previous; /* Previous code group. */
160 tree stmt_list; /* List of statements for this code group. */
161 tree block; /* BLOCK for this code group, if any. */
162 tree cleanups; /* Cleanups for this code group, if any. */
165 static GTY(()) struct stmt_group *current_stmt_group;
167 /* List of unused struct stmt_group nodes. */
168 static GTY((deletable)) struct stmt_group *stmt_group_free_list;
170 /* A structure used to record information on elaboration procedures
171 we've made and need to process.
173 ??? gnat_node should be Node_Id, but gengtype gets confused. */
175 struct GTY((chain_next ("%h.next"))) elab_info {
176 struct elab_info *next; /* Pointer to next in chain. */
177 tree elab_proc; /* Elaboration procedure. */
178 int gnat_node; /* The N_Compilation_Unit. */
181 static GTY(()) struct elab_info *elab_info_list;
183 /* Stack of exception pointer variables. Each entry is the VAR_DECL
184 that stores the address of the raised exception. Nonzero means we
185 are in an exception handler. Not used in the zero-cost case. */
186 static GTY(()) vec<tree, va_gc> *gnu_except_ptr_stack;
188 /* In ZCX case, current exception pointer. Used to re-raise it. */
189 static GTY(()) tree gnu_incoming_exc_ptr;
191 /* Stack for storing the current elaboration procedure decl. */
192 static GTY(()) vec<tree, va_gc> *gnu_elab_proc_stack;
194 /* Stack of labels to be used as a goto target instead of a return in
195 some functions. See processing for N_Subprogram_Body. */
196 static GTY(()) vec<tree, va_gc> *gnu_return_label_stack;
198 /* Stack of variable for the return value of a function with copy-in/copy-out
199 parameters. See processing for N_Subprogram_Body. */
200 static GTY(()) vec<tree, va_gc> *gnu_return_var_stack;
202 /* Structure used to record information for a range check. */
203 struct GTY(()) range_check_info_d {
204 tree low_bound;
205 tree high_bound;
206 tree type;
207 tree invariant_cond;
210 typedef struct range_check_info_d *range_check_info;
213 /* Structure used to record information for a loop. */
214 struct GTY(()) loop_info_d {
215 tree stmt;
216 tree loop_var;
217 vec<range_check_info, va_gc> *checks;
220 typedef struct loop_info_d *loop_info;
223 /* Stack of loop_info structures associated with LOOP_STMT nodes. */
224 static GTY(()) vec<loop_info, va_gc> *gnu_loop_stack;
226 /* The stacks for N_{Push,Pop}_*_Label. */
227 static GTY(()) vec<tree, va_gc> *gnu_constraint_error_label_stack;
228 static GTY(()) vec<tree, va_gc> *gnu_storage_error_label_stack;
229 static GTY(()) vec<tree, va_gc> *gnu_program_error_label_stack;
231 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
232 static enum tree_code gnu_codes[Number_Node_Kinds];
234 static void init_code_table (void);
235 static void Compilation_Unit_to_gnu (Node_Id);
236 static void record_code_position (Node_Id);
237 static void insert_code_for (Node_Id);
238 static void add_cleanup (tree, Node_Id);
239 static void add_stmt_list (List_Id);
240 static void push_exception_label_stack (vec<tree, va_gc> **, Entity_Id);
241 static tree build_stmt_group (List_Id, bool);
242 static inline bool stmt_group_may_fallthru (void);
243 static enum gimplify_status gnat_gimplify_stmt (tree *);
244 static void elaborate_all_entities (Node_Id);
245 static void process_freeze_entity (Node_Id);
246 static void process_decls (List_Id, List_Id, Node_Id, bool, bool);
247 static tree emit_range_check (tree, Node_Id, Node_Id);
248 static tree emit_index_check (tree, tree, tree, tree, Node_Id);
249 static tree emit_check (tree, tree, int, Node_Id);
250 static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id);
251 static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id);
252 static tree convert_with_check (Entity_Id, tree, bool, bool, bool, Node_Id);
253 static bool addressable_p (tree, tree);
254 static tree assoc_to_constructor (Entity_Id, Node_Id, tree);
255 static tree extract_values (tree, tree);
256 static tree pos_to_constructor (Node_Id, tree, Entity_Id);
257 static void validate_unchecked_conversion (Node_Id);
258 static tree maybe_implicit_deref (tree);
259 static void set_expr_location_from_node (tree, Node_Id);
260 static void set_expr_location_from_node1 (tree, Node_Id, bool);
261 static bool Sloc_to_locus1 (Source_Ptr, location_t *, bool);
262 static bool set_end_locus_from_node (tree, Node_Id);
263 static void set_gnu_expr_location_from_node (tree, Node_Id);
264 static int lvalue_required_p (Node_Id, tree, bool, bool, bool);
265 static tree build_raise_check (int, enum exception_info_kind);
266 static tree create_init_temporary (const char *, tree, tree *, Node_Id);
268 /* Hooks for debug info back-ends, only supported and used in a restricted set
269 of configurations. */
270 static const char *extract_encoding (const char *) ATTRIBUTE_UNUSED;
271 static const char *decode_name (const char *) ATTRIBUTE_UNUSED;
273 /* This is the main program of the back-end. It sets up all the table
274 structures and then generates code. */
276 void
277 gigi (Node_Id gnat_root,
278 int max_gnat_node,
279 int number_name ATTRIBUTE_UNUSED,
280 struct Node *nodes_ptr,
281 struct Flags *flags_ptr,
282 Node_Id *next_node_ptr,
283 Node_Id *prev_node_ptr,
284 struct Elist_Header *elists_ptr,
285 struct Elmt_Item *elmts_ptr,
286 struct String_Entry *strings_ptr,
287 Char_Code *string_chars_ptr,
288 struct List_Header *list_headers_ptr,
289 Nat number_file,
290 struct File_Info_Type *file_info_ptr,
291 Entity_Id standard_boolean,
292 Entity_Id standard_integer,
293 Entity_Id standard_character,
294 Entity_Id standard_long_long_float,
295 Entity_Id standard_exception_type,
296 Int gigi_operating_mode)
298 Node_Id gnat_iter;
299 Entity_Id gnat_literal;
300 tree long_long_float_type, exception_type, t, ftype;
301 tree int64_type = gnat_type_for_size (64, 0);
302 struct elab_info *info;
303 int i;
305 max_gnat_nodes = max_gnat_node;
307 Nodes_Ptr = nodes_ptr;
308 Flags_Ptr = flags_ptr;
309 Next_Node_Ptr = next_node_ptr;
310 Prev_Node_Ptr = prev_node_ptr;
311 Elists_Ptr = elists_ptr;
312 Elmts_Ptr = elmts_ptr;
313 Strings_Ptr = strings_ptr;
314 String_Chars_Ptr = string_chars_ptr;
315 List_Headers_Ptr = list_headers_ptr;
317 type_annotate_only = (gigi_operating_mode == 1);
319 #if TARGET_ABI_OPEN_VMS
320 vms_float_format = Float_Format;
321 #endif
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 /* malloc32 is a function declaration tree for a function to allocate
428 32-bit memory on a 64-bit system. Needed only on 64-bit VMS. */
429 malloc32_decl
430 = create_subprog_decl (get_identifier ("__gnat_malloc32"), NULL_TREE,
431 ftype, NULL_TREE, is_disabled, true, true, true,
432 NULL, Empty);
433 DECL_IS_MALLOC (malloc32_decl) = 1;
435 /* free is a function declaration tree for a function to free memory. */
436 free_decl
437 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
438 build_function_type_list (void_type_node,
439 ptr_void_type_node,
440 NULL_TREE),
441 NULL_TREE, is_disabled, true, true, true, NULL,
442 Empty);
444 /* This is used for 64-bit multiplication with overflow checking. */
445 mulv64_decl
446 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE,
447 build_function_type_list (int64_type, int64_type,
448 int64_type, NULL_TREE),
449 NULL_TREE, is_disabled, true, true, true, NULL,
450 Empty);
452 /* Name of the _Parent field in tagged record types. */
453 parent_name_id = get_identifier (Get_Name_String (Name_uParent));
455 /* Name of the Exception_Data type defined in System.Standard_Library. */
456 exception_data_name_id
457 = get_identifier ("system__standard_library__exception_data");
459 /* Make the types and functions used for exception processing. */
460 jmpbuf_type
461 = build_array_type (gnat_type_for_mode (Pmode, 0),
462 build_index_type (size_int (5)));
463 record_builtin_type ("JMPBUF_T", jmpbuf_type, true);
464 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
466 /* Functions to get and set the jumpbuf pointer for the current thread. */
467 get_jmpbuf_decl
468 = create_subprog_decl
469 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
470 NULL_TREE, build_function_type_list (jmpbuf_ptr_type, NULL_TREE),
471 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
472 DECL_IGNORED_P (get_jmpbuf_decl) = 1;
474 set_jmpbuf_decl
475 = create_subprog_decl
476 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
477 NULL_TREE, build_function_type_list (void_type_node, jmpbuf_ptr_type,
478 NULL_TREE),
479 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
480 DECL_IGNORED_P (set_jmpbuf_decl) = 1;
482 /* setjmp returns an integer and has one operand, which is a pointer to
483 a jmpbuf. */
484 setjmp_decl
485 = create_subprog_decl
486 (get_identifier ("__builtin_setjmp"), NULL_TREE,
487 build_function_type_list (integer_type_node, jmpbuf_ptr_type,
488 NULL_TREE),
489 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
490 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
491 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
493 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
494 address. */
495 update_setjmp_buf_decl
496 = create_subprog_decl
497 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
498 build_function_type_list (void_type_node, jmpbuf_ptr_type, NULL_TREE),
499 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
500 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
501 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
503 /* Hooks to call when entering/leaving an exception handler. */
504 ftype
505 = build_function_type_list (void_type_node, ptr_void_type_node, NULL_TREE);
507 begin_handler_decl
508 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
509 ftype, NULL_TREE, is_disabled, true, true, true,
510 NULL, Empty);
511 DECL_IGNORED_P (begin_handler_decl) = 1;
513 end_handler_decl
514 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
515 ftype, NULL_TREE, is_disabled, true, true, true,
516 NULL, Empty);
517 DECL_IGNORED_P (end_handler_decl) = 1;
519 unhandled_except_decl
520 = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
521 NULL_TREE,
522 ftype, NULL_TREE, is_disabled, true, true, true,
523 NULL, Empty);
524 DECL_IGNORED_P (unhandled_except_decl) = 1;
526 reraise_zcx_decl
527 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE,
528 ftype, NULL_TREE, is_disabled, true, true, true,
529 NULL, Empty);
530 /* Indicate that these never return. */
531 DECL_IGNORED_P (reraise_zcx_decl) = 1;
532 TREE_THIS_VOLATILE (reraise_zcx_decl) = 1;
533 TREE_SIDE_EFFECTS (reraise_zcx_decl) = 1;
534 TREE_TYPE (reraise_zcx_decl)
535 = build_qualified_type (TREE_TYPE (reraise_zcx_decl), TYPE_QUAL_VOLATILE);
537 /* If in no exception handlers mode, all raise statements are redirected to
538 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl since
539 this procedure will never be called in this mode. */
540 if (No_Exception_Handlers_Set ())
542 tree decl
543 = create_subprog_decl
544 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE,
545 build_function_type_list (void_type_node,
546 build_pointer_type
547 (unsigned_char_type_node),
548 integer_type_node, NULL_TREE),
549 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
550 TREE_THIS_VOLATILE (decl) = 1;
551 TREE_SIDE_EFFECTS (decl) = 1;
552 TREE_TYPE (decl)
553 = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
554 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
555 gnat_raise_decls[i] = decl;
557 else
559 /* Otherwise, make one decl for each exception reason. */
560 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
561 gnat_raise_decls[i] = build_raise_check (i, exception_simple);
562 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++)
563 gnat_raise_decls_ext[i]
564 = build_raise_check (i,
565 i == CE_Index_Check_Failed
566 || i == CE_Range_Check_Failed
567 || i == CE_Invalid_Data
568 ? exception_range : exception_column);
571 /* Set the types that GCC and Gigi use from the front end. */
572 exception_type
573 = gnat_to_gnu_entity (Base_Type (standard_exception_type), NULL_TREE, 0);
574 except_type_node = TREE_TYPE (exception_type);
576 /* Make other functions used for exception processing. */
577 get_excptr_decl
578 = create_subprog_decl
579 (get_identifier ("system__soft_links__get_gnat_exception"), NULL_TREE,
580 build_function_type_list (build_pointer_type (except_type_node),
581 NULL_TREE),
582 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
583 DECL_IGNORED_P (get_excptr_decl) = 1;
585 set_exception_parameter_decl
586 = create_subprog_decl
587 (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE,
588 build_function_type_list (void_type_node,
589 ptr_void_type_node,
590 ptr_void_type_node,
591 NULL_TREE),
592 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
594 raise_nodefer_decl
595 = create_subprog_decl
596 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE,
597 build_function_type_list (void_type_node,
598 build_pointer_type (except_type_node),
599 NULL_TREE),
600 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
602 /* Indicate that it never returns. */
603 TREE_THIS_VOLATILE (raise_nodefer_decl) = 1;
604 TREE_SIDE_EFFECTS (raise_nodefer_decl) = 1;
605 TREE_TYPE (raise_nodefer_decl)
606 = build_qualified_type (TREE_TYPE (raise_nodefer_decl),
607 TYPE_QUAL_VOLATILE);
609 /* Build the special descriptor type and its null node if needed. */
610 if (TARGET_VTABLE_USES_DESCRIPTORS)
612 tree null_node = fold_convert (ptr_void_ftype, null_pointer_node);
613 tree field_list = NULL_TREE;
614 int j;
615 vec<constructor_elt, va_gc> *null_vec = NULL;
616 constructor_elt *elt;
618 fdesc_type_node = make_node (RECORD_TYPE);
619 vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS);
620 elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
622 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
624 tree field
625 = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node,
626 NULL_TREE, NULL_TREE, 0, 1);
627 DECL_CHAIN (field) = field_list;
628 field_list = field;
629 elt->index = field;
630 elt->value = null_node;
631 elt--;
634 finish_record_type (fdesc_type_node, nreverse (field_list), 0, false);
635 record_builtin_type ("descriptor", fdesc_type_node, true);
636 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec);
639 long_long_float_type
640 = gnat_to_gnu_entity (Base_Type (standard_long_long_float), NULL_TREE, 0);
642 if (TREE_CODE (TREE_TYPE (long_long_float_type)) == INTEGER_TYPE)
644 /* In this case, the builtin floating point types are VAX float,
645 so make up a type for use. */
646 longest_float_type_node = make_node (REAL_TYPE);
647 TYPE_PRECISION (longest_float_type_node) = LONG_DOUBLE_TYPE_SIZE;
648 layout_type (longest_float_type_node);
649 record_builtin_type ("longest float type", longest_float_type_node,
650 false);
652 else
653 longest_float_type_node = TREE_TYPE (long_long_float_type);
655 /* Dummy objects to materialize "others" and "all others" in the exception
656 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
657 the types to use. */
658 others_decl
659 = create_var_decl (get_identifier ("OTHERS"),
660 get_identifier ("__gnat_others_value"),
661 unsigned_char_type_node,
662 NULL_TREE, true, false, true, false, NULL, Empty);
664 all_others_decl
665 = create_var_decl (get_identifier ("ALL_OTHERS"),
666 get_identifier ("__gnat_all_others_value"),
667 unsigned_char_type_node,
668 NULL_TREE, true, false, true, false, NULL, Empty);
670 unhandled_others_decl
671 = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
672 get_identifier ("__gnat_unhandled_others_value"),
673 unsigned_char_type_node,
674 NULL_TREE, true, false, true, false, NULL, Empty);
676 main_identifier_node = get_identifier ("main");
678 /* Install the builtins we might need, either internally or as
679 user available facilities for Intrinsic imports. */
680 gnat_install_builtins ();
682 vec_safe_push (gnu_except_ptr_stack, NULL_TREE);
683 vec_safe_push (gnu_constraint_error_label_stack, NULL_TREE);
684 vec_safe_push (gnu_storage_error_label_stack, NULL_TREE);
685 vec_safe_push (gnu_program_error_label_stack, NULL_TREE);
687 /* Process any Pragma Ident for the main unit. */
688 if (Present (Ident_String (Main_Unit)))
689 targetm.asm_out.output_ident
690 (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
692 /* If we are using the GCC exception mechanism, let GCC know. */
693 if (Exception_Mechanism == Back_End_Exceptions)
694 gnat_init_gcc_eh ();
696 /* Initialize the GCC support for FP operations. */
697 gnat_init_gcc_fp ();
699 /* Now translate the compilation unit proper. */
700 Compilation_Unit_to_gnu (gnat_root);
702 /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at
703 the very end to avoid having to second-guess the front-end when we run
704 into dummy nodes during the regular processing. */
705 for (i = 0; gnat_validate_uc_list.iterate (i, &gnat_iter); i++)
706 validate_unchecked_conversion (gnat_iter);
707 gnat_validate_uc_list.release ();
709 /* Finally see if we have any elaboration procedures to deal with. */
710 for (info = elab_info_list; info; info = info->next)
712 tree gnu_body = DECL_SAVED_TREE (info->elab_proc), gnu_stmts;
714 /* We should have a BIND_EXPR but it may not have any statements in it.
715 If it doesn't have any, we have nothing to do except for setting the
716 flag on the GNAT node. Otherwise, process the function as others. */
717 gnu_stmts = gnu_body;
718 if (TREE_CODE (gnu_stmts) == BIND_EXPR)
719 gnu_stmts = BIND_EXPR_BODY (gnu_stmts);
720 if (!gnu_stmts || !STATEMENT_LIST_HEAD (gnu_stmts))
721 Set_Has_No_Elaboration_Code (info->gnat_node, 1);
722 else
724 begin_subprog_body (info->elab_proc);
725 end_subprog_body (gnu_body);
726 rest_of_subprog_body_compilation (info->elab_proc);
730 /* Destroy ourselves. */
731 destroy_gnat_decl ();
732 destroy_gnat_utils ();
734 /* We cannot track the location of errors past this point. */
735 error_gnat_node = Empty;
738 /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given
739 CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */
741 static tree
742 build_raise_check (int check, enum exception_info_kind kind)
744 tree result, ftype;
745 const char pfx[] = "__gnat_rcheck_";
747 strcpy (Name_Buffer, pfx);
748 Name_Len = sizeof (pfx) - 1;
749 Get_RT_Exception_Name (check);
751 if (kind == exception_simple)
753 Name_Buffer[Name_Len] = 0;
754 ftype
755 = build_function_type_list (void_type_node,
756 build_pointer_type
757 (unsigned_char_type_node),
758 integer_type_node, NULL_TREE);
760 else
762 tree t = (kind == exception_column ? NULL_TREE : integer_type_node);
764 strcpy (Name_Buffer + Name_Len, "_ext");
765 Name_Buffer[Name_Len + 4] = 0;
766 ftype
767 = build_function_type_list (void_type_node,
768 build_pointer_type
769 (unsigned_char_type_node),
770 integer_type_node, integer_type_node,
771 t, t, NULL_TREE);
774 result
775 = create_subprog_decl (get_identifier (Name_Buffer),
776 NULL_TREE, ftype, NULL_TREE,
777 is_disabled, true, true, true, NULL, Empty);
779 /* Indicate that it never returns. */
780 TREE_THIS_VOLATILE (result) = 1;
781 TREE_SIDE_EFFECTS (result) = 1;
782 TREE_TYPE (result)
783 = build_qualified_type (TREE_TYPE (result), TYPE_QUAL_VOLATILE);
785 return result;
788 /* Return a positive value if an lvalue is required for GNAT_NODE, which is
789 an N_Attribute_Reference. */
791 static int
792 lvalue_required_for_attribute_p (Node_Id gnat_node)
794 switch (Get_Attribute_Id (Attribute_Name (gnat_node)))
796 case Attr_Pos:
797 case Attr_Val:
798 case Attr_Pred:
799 case Attr_Succ:
800 case Attr_First:
801 case Attr_Last:
802 case Attr_Range_Length:
803 case Attr_Length:
804 case Attr_Object_Size:
805 case Attr_Value_Size:
806 case Attr_Component_Size:
807 case Attr_Max_Size_In_Storage_Elements:
808 case Attr_Min:
809 case Attr_Max:
810 case Attr_Null_Parameter:
811 case Attr_Passed_By_Reference:
812 case Attr_Mechanism_Code:
813 return 0;
815 case Attr_Address:
816 case Attr_Access:
817 case Attr_Unchecked_Access:
818 case Attr_Unrestricted_Access:
819 case Attr_Code_Address:
820 case Attr_Pool_Address:
821 case Attr_Size:
822 case Attr_Alignment:
823 case Attr_Bit_Position:
824 case Attr_Position:
825 case Attr_First_Bit:
826 case Attr_Last_Bit:
827 case Attr_Bit:
828 case Attr_Asm_Input:
829 case Attr_Asm_Output:
830 default:
831 return 1;
835 /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE
836 is the type that will be used for GNAT_NODE in the translated GNU tree.
837 CONSTANT indicates whether the underlying object represented by GNAT_NODE
838 is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates
839 whether its value is the address of a constant and ALIASED whether it is
840 aliased. If it isn't, ADDRESS_OF_CONSTANT and ALIASED are ignored.
842 The function climbs up the GNAT tree starting from the node and returns 1
843 upon encountering a node that effectively requires an lvalue downstream.
844 It returns int instead of bool to facilitate usage in non-purely binary
845 logic contexts. */
847 static int
848 lvalue_required_p (Node_Id gnat_node, tree gnu_type, bool constant,
849 bool address_of_constant, bool aliased)
851 Node_Id gnat_parent = Parent (gnat_node), gnat_temp;
853 switch (Nkind (gnat_parent))
855 case N_Reference:
856 return 1;
858 case N_Attribute_Reference:
859 return lvalue_required_for_attribute_p (gnat_parent);
861 case N_Parameter_Association:
862 case N_Function_Call:
863 case N_Procedure_Call_Statement:
864 /* If the parameter is by reference, an lvalue is required. */
865 return (!constant
866 || must_pass_by_ref (gnu_type)
867 || default_pass_by_ref (gnu_type));
869 case N_Indexed_Component:
870 /* Only the array expression can require an lvalue. */
871 if (Prefix (gnat_parent) != gnat_node)
872 return 0;
874 /* ??? Consider that referencing an indexed component with a
875 non-constant index forces the whole aggregate to memory.
876 Note that N_Integer_Literal is conservative, any static
877 expression in the RM sense could probably be accepted. */
878 for (gnat_temp = First (Expressions (gnat_parent));
879 Present (gnat_temp);
880 gnat_temp = Next (gnat_temp))
881 if (Nkind (gnat_temp) != N_Integer_Literal)
882 return 1;
884 /* ... fall through ... */
886 case N_Slice:
887 /* Only the array expression can require an lvalue. */
888 if (Prefix (gnat_parent) != gnat_node)
889 return 0;
891 aliased |= Has_Aliased_Components (Etype (gnat_node));
892 return lvalue_required_p (gnat_parent, gnu_type, constant,
893 address_of_constant, aliased);
895 case N_Selected_Component:
896 aliased |= Is_Aliased (Entity (Selector_Name (gnat_parent)));
897 return lvalue_required_p (gnat_parent, gnu_type, constant,
898 address_of_constant, aliased);
900 case N_Object_Renaming_Declaration:
901 /* We need to make a real renaming only if the constant object is
902 aliased or if we may use a renaming pointer; otherwise we can
903 optimize and return the rvalue. We make an exception if the object
904 is an identifier since in this case the rvalue can be propagated
905 attached to the CONST_DECL. */
906 return (!constant
907 || aliased
908 /* This should match the constant case of the renaming code. */
909 || Is_Composite_Type
910 (Underlying_Type (Etype (Name (gnat_parent))))
911 || Nkind (Name (gnat_parent)) == N_Identifier);
913 case N_Object_Declaration:
914 /* We cannot use a constructor if this is an atomic object because
915 the actual assignment might end up being done component-wise. */
916 return (!constant
917 ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
918 && Is_Atomic (Defining_Entity (gnat_parent)))
919 /* We don't use a constructor if this is a class-wide object
920 because the effective type of the object is the equivalent
921 type of the class-wide subtype and it smashes most of the
922 data into an array of bytes to which we cannot convert. */
923 || Ekind ((Etype (Defining_Entity (gnat_parent))))
924 == E_Class_Wide_Subtype);
926 case N_Assignment_Statement:
927 /* We cannot use a constructor if the LHS is an atomic object because
928 the actual assignment might end up being done component-wise. */
929 return (!constant
930 || Name (gnat_parent) == gnat_node
931 || (Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
932 && Is_Atomic (Entity (Name (gnat_parent)))));
934 case N_Unchecked_Type_Conversion:
935 if (!constant)
936 return 1;
938 /* ... fall through ... */
940 case N_Type_Conversion:
941 case N_Qualified_Expression:
942 /* We must look through all conversions because we may need to bypass
943 an intermediate conversion that is meant to be purely formal. */
944 return lvalue_required_p (gnat_parent,
945 get_unpadded_type (Etype (gnat_parent)),
946 constant, address_of_constant, aliased);
948 case N_Allocator:
949 /* We should only reach here through the N_Qualified_Expression case.
950 Force an lvalue for composite types since a block-copy to the newly
951 allocated area of memory is made. */
952 return Is_Composite_Type (Underlying_Type (Etype (gnat_node)));
954 case N_Explicit_Dereference:
955 /* We look through dereferences for address of constant because we need
956 to handle the special cases listed above. */
957 if (constant && address_of_constant)
958 return lvalue_required_p (gnat_parent,
959 get_unpadded_type (Etype (gnat_parent)),
960 true, false, true);
962 /* ... fall through ... */
964 default:
965 return 0;
968 gcc_unreachable ();
971 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
972 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer
973 to where we should place the result type. */
975 static tree
976 Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
978 Node_Id gnat_temp, gnat_temp_type;
979 tree gnu_result, gnu_result_type;
981 /* Whether we should require an lvalue for GNAT_NODE. Needed in
982 specific circumstances only, so evaluated lazily. < 0 means
983 unknown, > 0 means known true, 0 means known false. */
984 int require_lvalue = -1;
986 /* If GNAT_NODE is a constant, whether we should use the initialization
987 value instead of the constant entity, typically for scalars with an
988 address clause when the parent doesn't require an lvalue. */
989 bool use_constant_initializer = false;
991 /* If the Etype of this node does not equal the Etype of the Entity,
992 something is wrong with the entity map, probably in generic
993 instantiation. However, this does not apply to types. Since we sometime
994 have strange Ekind's, just do this test for objects. Also, if the Etype of
995 the Entity is private, the Etype of the N_Identifier is allowed to be the
996 full type and also we consider a packed array type to be the same as the
997 original type. Similarly, a class-wide type is equivalent to a subtype of
998 itself. Finally, if the types are Itypes, one may be a copy of the other,
999 which is also legal. */
1000 gnat_temp = (Nkind (gnat_node) == N_Defining_Identifier
1001 ? gnat_node : Entity (gnat_node));
1002 gnat_temp_type = Etype (gnat_temp);
1004 gcc_assert (Etype (gnat_node) == gnat_temp_type
1005 || (Is_Packed (gnat_temp_type)
1006 && Etype (gnat_node) == Packed_Array_Type (gnat_temp_type))
1007 || (Is_Class_Wide_Type (Etype (gnat_node)))
1008 || (IN (Ekind (gnat_temp_type), Private_Kind)
1009 && Present (Full_View (gnat_temp_type))
1010 && ((Etype (gnat_node) == Full_View (gnat_temp_type))
1011 || (Is_Packed (Full_View (gnat_temp_type))
1012 && (Etype (gnat_node)
1013 == Packed_Array_Type (Full_View
1014 (gnat_temp_type))))))
1015 || (Is_Itype (Etype (gnat_node)) && Is_Itype (gnat_temp_type))
1016 || !(Ekind (gnat_temp) == E_Variable
1017 || Ekind (gnat_temp) == E_Component
1018 || Ekind (gnat_temp) == E_Constant
1019 || Ekind (gnat_temp) == E_Loop_Parameter
1020 || IN (Ekind (gnat_temp), Formal_Kind)));
1022 /* If this is a reference to a deferred constant whose partial view is an
1023 unconstrained private type, the proper type is on the full view of the
1024 constant, not on the full view of the type, which may be unconstrained.
1026 This may be a reference to a type, for example in the prefix of the
1027 attribute Position, generated for dispatching code (see Make_DT in
1028 exp_disp,adb). In that case we need the type itself, not is parent,
1029 in particular if it is a derived type */
1030 if (Ekind (gnat_temp) == E_Constant
1031 && Is_Private_Type (gnat_temp_type)
1032 && (Has_Unknown_Discriminants (gnat_temp_type)
1033 || (Present (Full_View (gnat_temp_type))
1034 && Has_Discriminants (Full_View (gnat_temp_type))))
1035 && Present (Full_View (gnat_temp)))
1037 gnat_temp = Full_View (gnat_temp);
1038 gnat_temp_type = Etype (gnat_temp);
1040 else
1042 /* We want to use the Actual_Subtype if it has already been elaborated,
1043 otherwise the Etype. Avoid using Actual_Subtype for packed arrays to
1044 simplify things. */
1045 if ((Ekind (gnat_temp) == E_Constant
1046 || Ekind (gnat_temp) == E_Variable || Is_Formal (gnat_temp))
1047 && !(Is_Array_Type (Etype (gnat_temp))
1048 && Present (Packed_Array_Type (Etype (gnat_temp))))
1049 && Present (Actual_Subtype (gnat_temp))
1050 && present_gnu_tree (Actual_Subtype (gnat_temp)))
1051 gnat_temp_type = Actual_Subtype (gnat_temp);
1052 else
1053 gnat_temp_type = Etype (gnat_node);
1056 /* Expand the type of this identifier first, in case it is an enumeral
1057 literal, which only get made when the type is expanded. There is no
1058 order-of-elaboration issue here. */
1059 gnu_result_type = get_unpadded_type (gnat_temp_type);
1061 /* If this is a non-imported elementary constant with an address clause,
1062 retrieve the value instead of a pointer to be dereferenced unless
1063 an lvalue is required. This is generally more efficient and actually
1064 required if this is a static expression because it might be used
1065 in a context where a dereference is inappropriate, such as a case
1066 statement alternative or a record discriminant. There is no possible
1067 volatile-ness short-circuit here since Volatile constants must be
1068 imported per C.6. */
1069 if (Ekind (gnat_temp) == E_Constant
1070 && Is_Elementary_Type (gnat_temp_type)
1071 && !Is_Imported (gnat_temp)
1072 && Present (Address_Clause (gnat_temp)))
1074 require_lvalue = lvalue_required_p (gnat_node, gnu_result_type, true,
1075 false, Is_Aliased (gnat_temp));
1076 use_constant_initializer = !require_lvalue;
1079 if (use_constant_initializer)
1081 /* If this is a deferred constant, the initializer is attached to
1082 the full view. */
1083 if (Present (Full_View (gnat_temp)))
1084 gnat_temp = Full_View (gnat_temp);
1086 gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_temp)));
1088 else
1089 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0);
1091 /* Some objects (such as parameters passed by reference, globals of
1092 variable size, and renamed objects) actually represent the address
1093 of the object. In that case, we must do the dereference. Likewise,
1094 deal with parameters to foreign convention subprograms. */
1095 if (DECL_P (gnu_result)
1096 && (DECL_BY_REF_P (gnu_result)
1097 || (TREE_CODE (gnu_result) == PARM_DECL
1098 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
1100 const bool read_only = DECL_POINTS_TO_READONLY_P (gnu_result);
1102 /* If it's a PARM_DECL to foreign convention subprogram, convert it. */
1103 if (TREE_CODE (gnu_result) == PARM_DECL
1104 && DECL_BY_COMPONENT_PTR_P (gnu_result))
1105 gnu_result
1106 = convert (build_pointer_type (gnu_result_type), gnu_result);
1108 /* If it's a CONST_DECL, return the underlying constant like below. */
1109 else if (TREE_CODE (gnu_result) == CONST_DECL
1110 && !(DECL_CONST_ADDRESS_P (gnu_result)
1111 && lvalue_required_p (gnat_node, gnu_result_type, true,
1112 true, false)))
1113 gnu_result = DECL_INITIAL (gnu_result);
1115 /* If it's a renaming pointer and we are at the right binding level,
1116 we can reference the renamed object directly, since the renamed
1117 expression has been protected against multiple evaluations. */
1118 if (TREE_CODE (gnu_result) == VAR_DECL
1119 && !DECL_LOOP_PARM_P (gnu_result)
1120 && DECL_RENAMED_OBJECT (gnu_result)
1121 && (!DECL_RENAMING_GLOBAL_P (gnu_result) || global_bindings_p ()))
1122 gnu_result = DECL_RENAMED_OBJECT (gnu_result);
1124 /* Otherwise, do the final dereference. */
1125 else
1127 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
1129 if ((TREE_CODE (gnu_result) == INDIRECT_REF
1130 || TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
1131 && No (Address_Clause (gnat_temp)))
1132 TREE_THIS_NOTRAP (gnu_result) = 1;
1134 if (read_only)
1135 TREE_READONLY (gnu_result) = 1;
1139 /* If we have a constant declaration and its initializer, try to return the
1140 latter to avoid the need to call fold in lots of places and the need for
1141 elaboration code if this identifier is used as an initializer itself.
1142 Don't do it for aggregate types that contain a placeholder since their
1143 initializers cannot be manipulated easily. */
1144 if (TREE_CONSTANT (gnu_result)
1145 && DECL_P (gnu_result)
1146 && DECL_INITIAL (gnu_result)
1147 && !(AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))
1148 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_result))
1149 && type_contains_placeholder_p (TREE_TYPE (gnu_result))))
1151 bool constant_only = (TREE_CODE (gnu_result) == CONST_DECL
1152 && !DECL_CONST_CORRESPONDING_VAR (gnu_result));
1153 bool address_of_constant = (TREE_CODE (gnu_result) == CONST_DECL
1154 && DECL_CONST_ADDRESS_P (gnu_result));
1156 /* If there is a (corresponding) variable or this is the address of a
1157 constant, we only want to return the initializer if an lvalue isn't
1158 required. Evaluate this now if we have not already done so. */
1159 if ((!constant_only || address_of_constant) && require_lvalue < 0)
1160 require_lvalue
1161 = lvalue_required_p (gnat_node, gnu_result_type, true,
1162 address_of_constant, Is_Aliased (gnat_temp));
1164 /* Finally retrieve the initializer if this is deemed valid. */
1165 if ((constant_only && !address_of_constant) || !require_lvalue)
1166 gnu_result = DECL_INITIAL (gnu_result);
1169 /* The GNAT tree has the type of a function set to its result type, so we
1170 adjust here. Also use the type of the result if the Etype is a subtype
1171 that is nominally unconstrained. Likewise if this is a deferred constant
1172 of a discriminated type whose full view can be elaborated statically, to
1173 avoid problematic conversions to the nominal subtype. But remove any
1174 padding from the resulting type. */
1175 if (TREE_CODE (TREE_TYPE (gnu_result)) == FUNCTION_TYPE
1176 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type)
1177 || (Ekind (gnat_temp) == E_Constant
1178 && Present (Full_View (gnat_temp))
1179 && Has_Discriminants (gnat_temp_type)
1180 && TREE_CODE (gnu_result) == CONSTRUCTOR))
1182 gnu_result_type = TREE_TYPE (gnu_result);
1183 if (TYPE_IS_PADDING_P (gnu_result_type))
1184 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
1187 *gnu_result_type_p = gnu_result_type;
1189 return gnu_result;
1192 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
1193 any statements we generate. */
1195 static tree
1196 Pragma_to_gnu (Node_Id gnat_node)
1198 tree gnu_result = alloc_stmt_list ();
1199 unsigned char pragma_id;
1200 Node_Id gnat_temp;
1202 /* Do nothing if we are just annotating types and check for (and ignore)
1203 unrecognized pragmas. */
1204 if (type_annotate_only
1205 || !Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node))))
1206 return gnu_result;
1208 pragma_id = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
1209 switch (pragma_id)
1211 case Pragma_Inspection_Point:
1212 /* Do nothing at top level: all such variables are already viewable. */
1213 if (global_bindings_p ())
1214 break;
1216 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1217 Present (gnat_temp);
1218 gnat_temp = Next (gnat_temp))
1220 Node_Id gnat_expr = Expression (gnat_temp);
1221 tree gnu_expr = gnat_to_gnu (gnat_expr);
1222 int use_address;
1223 enum machine_mode mode;
1224 tree asm_constraint = NULL_TREE;
1225 #ifdef ASM_COMMENT_START
1226 char *comment;
1227 #endif
1229 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
1230 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1232 /* Use the value only if it fits into a normal register,
1233 otherwise use the address. */
1234 mode = TYPE_MODE (TREE_TYPE (gnu_expr));
1235 use_address = ((GET_MODE_CLASS (mode) != MODE_INT
1236 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1237 || GET_MODE_SIZE (mode) > UNITS_PER_WORD);
1239 if (use_address)
1240 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
1242 #ifdef ASM_COMMENT_START
1243 comment = concat (ASM_COMMENT_START,
1244 " inspection point: ",
1245 Get_Name_String (Chars (gnat_expr)),
1246 use_address ? " address" : "",
1247 " is in %0",
1248 NULL);
1249 asm_constraint = build_string (strlen (comment), comment);
1250 free (comment);
1251 #endif
1252 gnu_expr = build5 (ASM_EXPR, void_type_node,
1253 asm_constraint,
1254 NULL_TREE,
1255 tree_cons
1256 (build_tree_list (NULL_TREE,
1257 build_string (1, "g")),
1258 gnu_expr, NULL_TREE),
1259 NULL_TREE, NULL_TREE);
1260 ASM_VOLATILE_P (gnu_expr) = 1;
1261 set_expr_location_from_node (gnu_expr, gnat_node);
1262 append_to_statement_list (gnu_expr, &gnu_result);
1264 break;
1266 case Pragma_Loop_Optimize:
1267 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1268 Present (gnat_temp);
1269 gnat_temp = Next (gnat_temp))
1271 tree gnu_loop_stmt = gnu_loop_stack->last ()->stmt;
1273 switch (Chars (Expression (gnat_temp)))
1275 case Name_Ivdep:
1276 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
1277 break;
1279 case Name_No_Unroll:
1280 LOOP_STMT_NO_UNROLL (gnu_loop_stmt) = 1;
1281 break;
1283 case Name_Unroll:
1284 LOOP_STMT_UNROLL (gnu_loop_stmt) = 1;
1285 break;
1287 case Name_No_Vector:
1288 LOOP_STMT_NO_VECTOR (gnu_loop_stmt) = 1;
1289 break;
1291 case Name_Vector:
1292 LOOP_STMT_VECTOR (gnu_loop_stmt) = 1;
1293 break;
1295 default:
1296 gcc_unreachable ();
1299 break;
1301 case Pragma_Optimize:
1302 switch (Chars (Expression
1303 (First (Pragma_Argument_Associations (gnat_node)))))
1305 case Name_Off:
1306 if (optimize)
1307 post_error ("must specify -O0?", gnat_node);
1308 break;
1310 case Name_Space:
1311 if (!optimize_size)
1312 post_error ("must specify -Os?", gnat_node);
1313 break;
1315 case Name_Time:
1316 if (!optimize)
1317 post_error ("insufficient -O value?", gnat_node);
1318 break;
1320 default:
1321 gcc_unreachable ();
1323 break;
1325 case Pragma_Reviewable:
1326 if (write_symbols == NO_DEBUG)
1327 post_error ("must specify -g?", gnat_node);
1328 break;
1330 case Pragma_Warning_As_Error:
1331 case Pragma_Warnings:
1333 Node_Id gnat_expr;
1334 /* Preserve the location of the pragma. */
1335 const location_t location = input_location;
1336 struct cl_option_handlers handlers;
1337 unsigned int option_index;
1338 diagnostic_t kind;
1339 bool imply;
1341 gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1343 /* This is the String form: pragma Warning{s|_As_Error}(String). */
1344 if (Nkind (Expression (gnat_temp)) == N_String_Literal)
1346 switch (pragma_id)
1348 case Pragma_Warning_As_Error:
1349 kind = DK_ERROR;
1350 imply = false;
1351 break;
1353 case Pragma_Warnings:
1354 kind = DK_WARNING;
1355 imply = true;
1356 break;
1358 default:
1359 gcc_unreachable ();
1362 gnat_expr = Expression (gnat_temp);
1365 /* This is the On/Off form: pragma Warnings (On | Off [,String]). */
1366 else if (Nkind (Expression (gnat_temp)) == N_Identifier)
1368 switch (Chars (Expression (gnat_temp)))
1370 case Name_Off:
1371 kind = DK_IGNORED;
1372 break;
1374 case Name_On:
1375 kind = DK_WARNING;
1376 break;
1378 default:
1379 gcc_unreachable ();
1382 if (Present (Next (gnat_temp)))
1384 /* pragma Warnings (On | Off, Name) is handled differently. */
1385 if (Nkind (Expression (Next (gnat_temp))) != N_String_Literal)
1386 break;
1388 gnat_expr = Expression (Next (gnat_temp));
1390 else
1391 gnat_expr = Empty;
1393 imply = false;
1396 else
1397 gcc_unreachable ();
1399 /* This is the same implementation as in the C family of compilers. */
1400 if (Present (gnat_expr))
1402 tree gnu_expr = gnat_to_gnu (gnat_expr);
1403 const char *opt_string = TREE_STRING_POINTER (gnu_expr);
1404 const int len = TREE_STRING_LENGTH (gnu_expr);
1405 if (len < 3 || opt_string[0] != '-' || opt_string[1] != 'W')
1406 break;
1407 for (option_index = 0;
1408 option_index < cl_options_count;
1409 option_index++)
1410 if (strcmp (cl_options[option_index].opt_text, opt_string) == 0)
1411 break;
1412 if (option_index == cl_options_count)
1414 post_error ("unknown -W switch", gnat_node);
1415 break;
1418 else
1419 option_index = 0;
1421 set_default_handlers (&handlers);
1422 control_warning_option (option_index, (int) kind, imply, location,
1423 CL_Ada, &handlers, &global_options,
1424 &global_options_set, global_dc);
1426 break;
1428 default:
1429 break;
1432 return gnu_result;
1435 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node,
1436 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
1437 where we should place the result type. ATTRIBUTE is the attribute ID. */
1439 static tree
1440 Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
1442 const Node_Id gnat_prefix = Prefix (gnat_node);
1443 tree gnu_prefix, gnu_type, gnu_expr;
1444 tree gnu_result_type, gnu_result = error_mark_node;
1445 bool prefix_unused = false;
1447 /* ??? If this is an access attribute for a public subprogram to be used in
1448 a dispatch table, do not translate its type as it's useless there and the
1449 parameter types might be incomplete types coming from a limited with. */
1450 if (Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
1451 && Is_Dispatch_Table_Entity (Etype (gnat_node))
1452 && Nkind (gnat_prefix) == N_Identifier
1453 && Is_Subprogram (Entity (gnat_prefix))
1454 && Is_Public (Entity (gnat_prefix))
1455 && !present_gnu_tree (Entity (gnat_prefix)))
1456 gnu_prefix = get_minimal_subprog_decl (Entity (gnat_prefix));
1457 else
1458 gnu_prefix = gnat_to_gnu (gnat_prefix);
1459 gnu_type = TREE_TYPE (gnu_prefix);
1461 /* If the input is a NULL_EXPR, make a new one. */
1462 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
1464 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1465 *gnu_result_type_p = gnu_result_type;
1466 return build1 (NULL_EXPR, gnu_result_type, TREE_OPERAND (gnu_prefix, 0));
1469 switch (attribute)
1471 case Attr_Pos:
1472 case Attr_Val:
1473 /* These are just conversions since representation clauses for
1474 enumeration types are handled in the front-end. */
1476 bool checkp = Do_Range_Check (First (Expressions (gnat_node)));
1477 gnu_result = gnat_to_gnu (First (Expressions (gnat_node)));
1478 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1479 gnu_result = convert_with_check (Etype (gnat_node), gnu_result,
1480 checkp, checkp, true, gnat_node);
1482 break;
1484 case Attr_Pred:
1485 case Attr_Succ:
1486 /* These just add or subtract the constant 1 since representation
1487 clauses for enumeration types are handled in the front-end. */
1488 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1489 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1491 if (Do_Range_Check (First (Expressions (gnat_node))))
1493 gnu_expr = gnat_protect_expr (gnu_expr);
1494 gnu_expr
1495 = emit_check
1496 (build_binary_op (EQ_EXPR, boolean_type_node,
1497 gnu_expr,
1498 attribute == Attr_Pred
1499 ? TYPE_MIN_VALUE (gnu_result_type)
1500 : TYPE_MAX_VALUE (gnu_result_type)),
1501 gnu_expr, CE_Range_Check_Failed, gnat_node);
1504 gnu_result
1505 = build_binary_op (attribute == Attr_Pred ? MINUS_EXPR : PLUS_EXPR,
1506 gnu_result_type, gnu_expr,
1507 convert (gnu_result_type, integer_one_node));
1508 break;
1510 case Attr_Address:
1511 case Attr_Unrestricted_Access:
1512 /* Conversions don't change addresses but can cause us to miss the
1513 COMPONENT_REF case below, so strip them off. */
1514 gnu_prefix = remove_conversions (gnu_prefix,
1515 !Must_Be_Byte_Aligned (gnat_node));
1517 /* If we are taking 'Address of an unconstrained object, this is the
1518 pointer to the underlying array. */
1519 if (attribute == Attr_Address)
1520 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1522 /* If we are building a static dispatch table, we have to honor
1523 TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible
1524 with the C++ ABI. We do it in the non-static case as well,
1525 see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */
1526 else if (TARGET_VTABLE_USES_DESCRIPTORS
1527 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
1529 tree gnu_field, t;
1530 /* Descriptors can only be built here for top-level functions. */
1531 bool build_descriptor = (global_bindings_p () != 0);
1532 int i;
1533 vec<constructor_elt, va_gc> *gnu_vec = NULL;
1534 constructor_elt *elt;
1536 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1538 /* If we're not going to build the descriptor, we have to retrieve
1539 the one which will be built by the linker (or by the compiler
1540 later if a static chain is requested). */
1541 if (!build_descriptor)
1543 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_prefix);
1544 gnu_result = fold_convert (build_pointer_type (gnu_result_type),
1545 gnu_result);
1546 gnu_result = build1 (INDIRECT_REF, gnu_result_type, gnu_result);
1549 vec_safe_grow (gnu_vec, TARGET_VTABLE_USES_DESCRIPTORS);
1550 elt = (gnu_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
1551 for (gnu_field = TYPE_FIELDS (gnu_result_type), i = 0;
1552 i < TARGET_VTABLE_USES_DESCRIPTORS;
1553 gnu_field = DECL_CHAIN (gnu_field), i++)
1555 if (build_descriptor)
1557 t = build2 (FDESC_EXPR, TREE_TYPE (gnu_field), gnu_prefix,
1558 build_int_cst (NULL_TREE, i));
1559 TREE_CONSTANT (t) = 1;
1561 else
1562 t = build3 (COMPONENT_REF, ptr_void_ftype, gnu_result,
1563 gnu_field, NULL_TREE);
1565 elt->index = gnu_field;
1566 elt->value = t;
1567 elt--;
1570 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
1571 break;
1574 /* ... fall through ... */
1576 case Attr_Access:
1577 case Attr_Unchecked_Access:
1578 case Attr_Code_Address:
1579 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1580 gnu_result
1581 = build_unary_op (((attribute == Attr_Address
1582 || attribute == Attr_Unrestricted_Access)
1583 && !Must_Be_Byte_Aligned (gnat_node))
1584 ? ATTR_ADDR_EXPR : ADDR_EXPR,
1585 gnu_result_type, gnu_prefix);
1587 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
1588 don't try to build a trampoline. */
1589 if (attribute == Attr_Code_Address)
1591 gnu_expr = remove_conversions (gnu_result, false);
1593 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
1594 TREE_NO_TRAMPOLINE (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
1597 /* For 'Access, issue an error message if the prefix is a C++ method
1598 since it can use a special calling convention on some platforms,
1599 which cannot be propagated to the access type. */
1600 else if (attribute == Attr_Access
1601 && Nkind (gnat_prefix) == N_Identifier
1602 && is_cplusplus_method (Entity (gnat_prefix)))
1603 post_error ("access to C++ constructor or member function not allowed",
1604 gnat_node);
1606 /* For other address attributes applied to a nested function,
1607 find an inner ADDR_EXPR and annotate it so that we can issue
1608 a useful warning with -Wtrampolines. */
1609 else if (TREE_CODE (TREE_TYPE (gnu_prefix)) == FUNCTION_TYPE)
1611 gnu_expr = remove_conversions (gnu_result, false);
1613 if (TREE_CODE (gnu_expr) == ADDR_EXPR
1614 && decl_function_context (TREE_OPERAND (gnu_expr, 0)))
1616 set_expr_location_from_node (gnu_expr, gnat_node);
1618 /* Check that we're not violating the No_Implicit_Dynamic_Code
1619 restriction. Be conservative if we don't know anything
1620 about the trampoline strategy for the target. */
1621 Check_Implicit_Dynamic_Code_Allowed (gnat_node);
1624 break;
1626 case Attr_Pool_Address:
1628 tree gnu_ptr = gnu_prefix;
1629 tree gnu_obj_type;
1631 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1633 /* If this is fat pointer, the object must have been allocated with the
1634 template in front of the array. So compute the template address; do
1635 it by converting to a thin pointer. */
1636 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
1637 gnu_ptr
1638 = convert (build_pointer_type
1639 (TYPE_OBJECT_RECORD_TYPE
1640 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
1641 gnu_ptr);
1643 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
1645 /* If this is a thin pointer, the object must have been allocated with
1646 the template in front of the array. So compute the template address
1647 and return it. */
1648 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
1649 gnu_ptr
1650 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
1651 gnu_ptr,
1652 fold_build1 (NEGATE_EXPR, sizetype,
1653 byte_position
1654 (DECL_CHAIN
1655 TYPE_FIELDS ((gnu_obj_type)))));
1657 gnu_result = convert (gnu_result_type, gnu_ptr);
1659 break;
1661 case Attr_Size:
1662 case Attr_Object_Size:
1663 case Attr_Value_Size:
1664 case Attr_Max_Size_In_Storage_Elements:
1665 gnu_expr = gnu_prefix;
1667 /* Remove NOPs and conversions between original and packable version
1668 from GNU_EXPR, and conversions from GNU_PREFIX. We use GNU_EXPR
1669 to see if a COMPONENT_REF was involved. */
1670 while (TREE_CODE (gnu_expr) == NOP_EXPR
1671 || (TREE_CODE (gnu_expr) == VIEW_CONVERT_EXPR
1672 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1673 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
1674 == RECORD_TYPE
1675 && TYPE_NAME (TREE_TYPE (gnu_expr))
1676 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
1677 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1679 gnu_prefix = remove_conversions (gnu_prefix, true);
1680 prefix_unused = true;
1681 gnu_type = TREE_TYPE (gnu_prefix);
1683 /* Replace an unconstrained array type with the type of the underlying
1684 array. We can't do this with a call to maybe_unconstrained_array
1685 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
1686 use the record type that will be used to allocate the object and its
1687 template. */
1688 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1690 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
1691 if (attribute != Attr_Max_Size_In_Storage_Elements)
1692 gnu_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
1695 /* If we're looking for the size of a field, return the field size. */
1696 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1697 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
1699 /* Otherwise, if the prefix is an object, or if we are looking for
1700 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the
1701 GCC size of the type. We make an exception for padded objects,
1702 as we do not take into account alignment promotions for the size.
1703 This is in keeping with the object case of gnat_to_gnu_entity. */
1704 else if ((TREE_CODE (gnu_prefix) != TYPE_DECL
1705 && !(TYPE_IS_PADDING_P (gnu_type)
1706 && TREE_CODE (gnu_expr) == COMPONENT_REF))
1707 || attribute == Attr_Object_Size
1708 || attribute == Attr_Max_Size_In_Storage_Elements)
1710 /* If this is a dereference and we have a special dynamic constrained
1711 subtype on the prefix, use it to compute the size; otherwise, use
1712 the designated subtype. */
1713 if (Nkind (gnat_prefix) == N_Explicit_Dereference)
1715 Node_Id gnat_actual_subtype
1716 = Actual_Designated_Subtype (gnat_prefix);
1717 tree gnu_ptr_type
1718 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix)));
1720 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)
1721 && Present (gnat_actual_subtype))
1723 tree gnu_actual_obj_type
1724 = gnat_to_gnu_type (gnat_actual_subtype);
1725 gnu_type
1726 = build_unc_object_type_from_ptr (gnu_ptr_type,
1727 gnu_actual_obj_type,
1728 get_identifier ("SIZE"),
1729 false);
1733 gnu_result = TYPE_SIZE (gnu_type);
1736 /* Otherwise, the result is the RM size of the type. */
1737 else
1738 gnu_result = rm_size (gnu_type);
1740 /* Deal with a self-referential size by returning the maximum size for
1741 a type and by qualifying the size with the object otherwise. */
1742 if (CONTAINS_PLACEHOLDER_P (gnu_result))
1744 if (TREE_CODE (gnu_prefix) == TYPE_DECL)
1745 gnu_result = max_size (gnu_result, true);
1746 else
1747 gnu_result = substitute_placeholder_in_expr (gnu_result, gnu_expr);
1750 /* If the type contains a template, subtract its size. */
1751 if (TREE_CODE (gnu_type) == RECORD_TYPE
1752 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1753 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1754 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1756 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
1757 if (attribute == Attr_Max_Size_In_Storage_Elements)
1758 gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node);
1760 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1761 break;
1763 case Attr_Alignment:
1765 unsigned int align;
1767 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1768 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
1769 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1771 gnu_type = TREE_TYPE (gnu_prefix);
1772 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1773 prefix_unused = true;
1775 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1776 align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT;
1777 else
1779 Entity_Id gnat_type = Etype (gnat_prefix);
1780 unsigned int double_align;
1781 bool is_capped_double, align_clause;
1783 /* If the default alignment of "double" or larger scalar types is
1784 specifically capped and there is an alignment clause neither
1785 on the type nor on the prefix itself, return the cap. */
1786 if ((double_align = double_float_alignment) > 0)
1787 is_capped_double
1788 = is_double_float_or_array (gnat_type, &align_clause);
1789 else if ((double_align = double_scalar_alignment) > 0)
1790 is_capped_double
1791 = is_double_scalar_or_array (gnat_type, &align_clause);
1792 else
1793 is_capped_double = align_clause = false;
1795 if (is_capped_double
1796 && Nkind (gnat_prefix) == N_Identifier
1797 && Present (Alignment_Clause (Entity (gnat_prefix))))
1798 align_clause = true;
1800 if (is_capped_double && !align_clause)
1801 align = double_align;
1802 else
1803 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
1806 gnu_result = size_int (align);
1808 break;
1810 case Attr_First:
1811 case Attr_Last:
1812 case Attr_Range_Length:
1813 prefix_unused = true;
1815 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
1817 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1819 if (attribute == Attr_First)
1820 gnu_result = TYPE_MIN_VALUE (gnu_type);
1821 else if (attribute == Attr_Last)
1822 gnu_result = TYPE_MAX_VALUE (gnu_type);
1823 else
1824 gnu_result
1825 = build_binary_op
1826 (MAX_EXPR, get_base_type (gnu_result_type),
1827 build_binary_op
1828 (PLUS_EXPR, get_base_type (gnu_result_type),
1829 build_binary_op (MINUS_EXPR,
1830 get_base_type (gnu_result_type),
1831 convert (gnu_result_type,
1832 TYPE_MAX_VALUE (gnu_type)),
1833 convert (gnu_result_type,
1834 TYPE_MIN_VALUE (gnu_type))),
1835 convert (gnu_result_type, integer_one_node)),
1836 convert (gnu_result_type, integer_zero_node));
1838 break;
1841 /* ... fall through ... */
1843 case Attr_Length:
1845 int Dimension = (Present (Expressions (gnat_node))
1846 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
1847 : 1), i;
1848 struct parm_attr_d *pa = NULL;
1849 Entity_Id gnat_param = Empty;
1850 bool unconstrained_ptr_deref = false;
1852 /* Make sure any implicit dereference gets done. */
1853 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1854 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1856 /* We treat unconstrained array In parameters specially. We also note
1857 whether we are dereferencing a pointer to unconstrained array. */
1858 if (!Is_Constrained (Etype (gnat_prefix)))
1859 switch (Nkind (gnat_prefix))
1861 case N_Identifier:
1862 /* This is the direct case. */
1863 if (Ekind (Entity (gnat_prefix)) == E_In_Parameter)
1864 gnat_param = Entity (gnat_prefix);
1865 break;
1867 case N_Explicit_Dereference:
1868 /* This is the indirect case. Note that we need to be sure that
1869 the access value cannot be null as we'll hoist the load. */
1870 if (Nkind (Prefix (gnat_prefix)) == N_Identifier
1871 && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter)
1873 if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix))))
1874 gnat_param = Entity (Prefix (gnat_prefix));
1876 else
1877 unconstrained_ptr_deref = true;
1878 break;
1880 default:
1881 break;
1884 /* If the prefix is the view conversion of a constrained array to an
1885 unconstrained form, we retrieve the constrained array because we
1886 might not be able to substitute the PLACEHOLDER_EXPR coming from
1887 the conversion. This can occur with the 'Old attribute applied
1888 to a parameter with an unconstrained type, which gets rewritten
1889 into a constrained local variable very late in the game. */
1890 if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR
1891 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix)))
1892 && !CONTAINS_PLACEHOLDER_P
1893 (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
1894 gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0));
1895 else
1896 gnu_type = TREE_TYPE (gnu_prefix);
1898 prefix_unused = true;
1899 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1901 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
1903 int ndim;
1904 tree gnu_type_temp;
1906 for (ndim = 1, gnu_type_temp = gnu_type;
1907 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
1908 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
1909 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
1912 Dimension = ndim + 1 - Dimension;
1915 for (i = 1; i < Dimension; i++)
1916 gnu_type = TREE_TYPE (gnu_type);
1918 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
1920 /* When not optimizing, look up the slot associated with the parameter
1921 and the dimension in the cache and create a new one on failure. */
1922 if (!optimize && Present (gnat_param))
1924 FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa)
1925 if (pa->id == gnat_param && pa->dim == Dimension)
1926 break;
1928 if (!pa)
1930 pa = ggc_alloc_cleared_parm_attr_d ();
1931 pa->id = gnat_param;
1932 pa->dim = Dimension;
1933 vec_safe_push (f_parm_attr_cache, pa);
1937 /* Return the cached expression or build a new one. */
1938 if (attribute == Attr_First)
1940 if (pa && pa->first)
1942 gnu_result = pa->first;
1943 break;
1946 gnu_result
1947 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
1950 else if (attribute == Attr_Last)
1952 if (pa && pa->last)
1954 gnu_result = pa->last;
1955 break;
1958 gnu_result
1959 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
1962 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
1964 if (pa && pa->length)
1966 gnu_result = pa->length;
1967 break;
1969 else
1971 /* We used to compute the length as max (hb - lb + 1, 0),
1972 which could overflow for some cases of empty arrays, e.g.
1973 when lb == index_type'first. We now compute the length as
1974 (hb >= lb) ? hb - lb + 1 : 0, which would only overflow in
1975 much rarer cases, for extremely large arrays we expect
1976 never to encounter in practice. In addition, the former
1977 computation required the use of potentially constraining
1978 signed arithmetic while the latter doesn't. Note that
1979 the comparison must be done in the original index type,
1980 to avoid any overflow during the conversion. */
1981 tree comp_type = get_base_type (gnu_result_type);
1982 tree index_type = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
1983 tree lb = TYPE_MIN_VALUE (index_type);
1984 tree hb = TYPE_MAX_VALUE (index_type);
1985 gnu_result
1986 = build_binary_op (PLUS_EXPR, comp_type,
1987 build_binary_op (MINUS_EXPR,
1988 comp_type,
1989 convert (comp_type, hb),
1990 convert (comp_type, lb)),
1991 convert (comp_type, integer_one_node));
1992 gnu_result
1993 = build_cond_expr (comp_type,
1994 build_binary_op (GE_EXPR,
1995 boolean_type_node,
1996 hb, lb),
1997 gnu_result,
1998 convert (comp_type, integer_zero_node));
2002 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2003 handling. Note that these attributes could not have been used on
2004 an unconstrained array type. */
2005 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2007 /* Cache the expression we have just computed. Since we want to do it
2008 at run time, we force the use of a SAVE_EXPR and let the gimplifier
2009 create the temporary in the outermost binding level. We will make
2010 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
2011 paths by forcing its evaluation on entry of the function. */
2012 if (pa)
2014 gnu_result
2015 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2016 switch (attribute)
2018 case Attr_First:
2019 pa->first = gnu_result;
2020 break;
2022 case Attr_Last:
2023 pa->last = gnu_result;
2024 break;
2026 case Attr_Length:
2027 case Attr_Range_Length:
2028 pa->length = gnu_result;
2029 break;
2031 default:
2032 gcc_unreachable ();
2036 /* Otherwise, evaluate it each time it is referenced. */
2037 else
2038 switch (attribute)
2040 case Attr_First:
2041 case Attr_Last:
2042 /* If we are dereferencing a pointer to unconstrained array, we
2043 need to capture the value because the pointed-to bounds may
2044 subsequently be released. */
2045 if (unconstrained_ptr_deref)
2046 gnu_result
2047 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2048 break;
2050 case Attr_Length:
2051 case Attr_Range_Length:
2052 /* Set the source location onto the predicate of the condition
2053 but not if the expression is cached to avoid messing up the
2054 debug info. */
2055 if (TREE_CODE (gnu_result) == COND_EXPR
2056 && EXPR_P (TREE_OPERAND (gnu_result, 0)))
2057 set_expr_location_from_node (TREE_OPERAND (gnu_result, 0),
2058 gnat_node);
2059 break;
2061 default:
2062 gcc_unreachable ();
2065 break;
2068 case Attr_Bit_Position:
2069 case Attr_Position:
2070 case Attr_First_Bit:
2071 case Attr_Last_Bit:
2072 case Attr_Bit:
2074 HOST_WIDE_INT bitsize;
2075 HOST_WIDE_INT bitpos;
2076 tree gnu_offset;
2077 tree gnu_field_bitpos;
2078 tree gnu_field_offset;
2079 tree gnu_inner;
2080 enum machine_mode mode;
2081 int unsignedp, volatilep;
2083 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2084 gnu_prefix = remove_conversions (gnu_prefix, true);
2085 prefix_unused = true;
2087 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
2088 the result is 0. Don't allow 'Bit on a bare component, though. */
2089 if (attribute == Attr_Bit
2090 && TREE_CODE (gnu_prefix) != COMPONENT_REF
2091 && TREE_CODE (gnu_prefix) != FIELD_DECL)
2093 gnu_result = integer_zero_node;
2094 break;
2097 else
2098 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
2099 || (attribute == Attr_Bit_Position
2100 && TREE_CODE (gnu_prefix) == FIELD_DECL));
2102 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
2103 &mode, &unsignedp, &volatilep, false);
2105 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
2107 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
2108 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
2110 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
2111 TREE_CODE (gnu_inner) == COMPONENT_REF
2112 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
2113 gnu_inner = TREE_OPERAND (gnu_inner, 0))
2115 gnu_field_bitpos
2116 = size_binop (PLUS_EXPR, gnu_field_bitpos,
2117 bit_position (TREE_OPERAND (gnu_inner, 1)));
2118 gnu_field_offset
2119 = size_binop (PLUS_EXPR, gnu_field_offset,
2120 byte_position (TREE_OPERAND (gnu_inner, 1)));
2123 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
2125 gnu_field_bitpos = bit_position (gnu_prefix);
2126 gnu_field_offset = byte_position (gnu_prefix);
2128 else
2130 gnu_field_bitpos = bitsize_zero_node;
2131 gnu_field_offset = size_zero_node;
2134 switch (attribute)
2136 case Attr_Position:
2137 gnu_result = gnu_field_offset;
2138 break;
2140 case Attr_First_Bit:
2141 case Attr_Bit:
2142 gnu_result = size_int (bitpos % BITS_PER_UNIT);
2143 break;
2145 case Attr_Last_Bit:
2146 gnu_result = bitsize_int (bitpos % BITS_PER_UNIT);
2147 gnu_result = size_binop (PLUS_EXPR, gnu_result,
2148 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
2149 /* ??? Avoid a large unsigned result that will overflow when
2150 converted to the signed universal_integer. */
2151 if (integer_zerop (gnu_result))
2152 gnu_result = integer_minus_one_node;
2153 else
2154 gnu_result
2155 = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node);
2156 break;
2158 case Attr_Bit_Position:
2159 gnu_result = gnu_field_bitpos;
2160 break;
2163 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2164 handling. */
2165 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2166 break;
2169 case Attr_Min:
2170 case Attr_Max:
2172 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
2173 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
2175 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2176 gnu_result = build_binary_op (attribute == Attr_Min
2177 ? MIN_EXPR : MAX_EXPR,
2178 gnu_result_type, gnu_lhs, gnu_rhs);
2180 break;
2182 case Attr_Passed_By_Reference:
2183 gnu_result = size_int (default_pass_by_ref (gnu_type)
2184 || must_pass_by_ref (gnu_type));
2185 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2186 break;
2188 case Attr_Component_Size:
2189 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
2190 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
2191 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
2193 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2194 gnu_type = TREE_TYPE (gnu_prefix);
2196 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
2197 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
2199 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2200 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2201 gnu_type = TREE_TYPE (gnu_type);
2203 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2205 /* Note this size cannot be self-referential. */
2206 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
2207 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2208 prefix_unused = true;
2209 break;
2211 case Attr_Descriptor_Size:
2212 gnu_type = TREE_TYPE (gnu_prefix);
2213 gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE);
2215 /* What we want is the offset of the ARRAY field in the record
2216 that the thin pointer designates. */
2217 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
2218 gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
2219 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2220 prefix_unused = true;
2221 break;
2223 case Attr_Null_Parameter:
2224 /* This is just a zero cast to the pointer type for our prefix and
2225 dereferenced. */
2226 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2227 gnu_result
2228 = build_unary_op (INDIRECT_REF, NULL_TREE,
2229 convert (build_pointer_type (gnu_result_type),
2230 integer_zero_node));
2231 TREE_PRIVATE (gnu_result) = 1;
2232 break;
2234 case Attr_Mechanism_Code:
2236 Entity_Id gnat_obj = Entity (gnat_prefix);
2237 int code;
2239 prefix_unused = true;
2240 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2241 if (Present (Expressions (gnat_node)))
2243 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
2245 for (gnat_obj = First_Formal (gnat_obj); i > 1;
2246 i--, gnat_obj = Next_Formal (gnat_obj))
2250 code = Mechanism (gnat_obj);
2251 if (code == Default)
2252 code = ((present_gnu_tree (gnat_obj)
2253 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
2254 || ((TREE_CODE (get_gnu_tree (gnat_obj))
2255 == PARM_DECL)
2256 && (DECL_BY_COMPONENT_PTR_P
2257 (get_gnu_tree (gnat_obj))))))
2258 ? By_Reference : By_Copy);
2259 gnu_result = convert (gnu_result_type, size_int (- code));
2261 break;
2263 default:
2264 /* This abort means that we have an unimplemented attribute. */
2265 gcc_unreachable ();
2268 /* If this is an attribute where the prefix was unused, force a use of it if
2269 it has a side-effect. But don't do it if the prefix is just an entity
2270 name. However, if an access check is needed, we must do it. See second
2271 example in AARM 11.6(5.e). */
2272 if (prefix_unused
2273 && TREE_SIDE_EFFECTS (gnu_prefix)
2274 && !Is_Entity_Name (gnat_prefix))
2275 gnu_result
2276 = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result);
2278 *gnu_result_type_p = gnu_result_type;
2279 return gnu_result;
2282 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
2283 to a GCC tree, which is returned. */
2285 static tree
2286 Case_Statement_to_gnu (Node_Id gnat_node)
2288 tree gnu_result, gnu_expr, gnu_label;
2289 Node_Id gnat_when;
2290 location_t end_locus;
2291 bool may_fallthru = false;
2293 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2294 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2296 /* The range of values in a case statement is determined by the rules in
2297 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
2298 of the expression. One exception arises in the case of a simple name that
2299 is parenthesized. This still has the Etype of the name, but since it is
2300 not a name, para 7 does not apply, and we need to go to the base type.
2301 This is the only case where parenthesization affects the dynamic
2302 semantics (i.e. the range of possible values at run time that is covered
2303 by the others alternative).
2305 Another exception is if the subtype of the expression is non-static. In
2306 that case, we also have to use the base type. */
2307 if (Paren_Count (Expression (gnat_node)) != 0
2308 || !Is_OK_Static_Subtype (Underlying_Type
2309 (Etype (Expression (gnat_node)))))
2310 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2312 /* We build a SWITCH_EXPR that contains the code with interspersed
2313 CASE_LABEL_EXPRs for each label. */
2314 if (!Sloc_to_locus (Sloc (gnat_node) + UI_To_Int (End_Span (gnat_node)),
2315 &end_locus))
2316 end_locus = input_location;
2317 gnu_label = create_artificial_label (end_locus);
2318 start_stmt_group ();
2320 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2321 Present (gnat_when);
2322 gnat_when = Next_Non_Pragma (gnat_when))
2324 bool choices_added_p = false;
2325 Node_Id gnat_choice;
2327 /* First compile all the different case choices for the current WHEN
2328 alternative. */
2329 for (gnat_choice = First (Discrete_Choices (gnat_when));
2330 Present (gnat_choice); gnat_choice = Next (gnat_choice))
2332 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2334 switch (Nkind (gnat_choice))
2336 case N_Range:
2337 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
2338 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
2339 break;
2341 case N_Subtype_Indication:
2342 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
2343 (Constraint (gnat_choice))));
2344 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
2345 (Constraint (gnat_choice))));
2346 break;
2348 case N_Identifier:
2349 case N_Expanded_Name:
2350 /* This represents either a subtype range or a static value of
2351 some kind; Ekind says which. */
2352 if (IN (Ekind (Entity (gnat_choice)), Type_Kind))
2354 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
2356 gnu_low = fold (TYPE_MIN_VALUE (gnu_type));
2357 gnu_high = fold (TYPE_MAX_VALUE (gnu_type));
2358 break;
2361 /* ... fall through ... */
2363 case N_Character_Literal:
2364 case N_Integer_Literal:
2365 gnu_low = gnat_to_gnu (gnat_choice);
2366 break;
2368 case N_Others_Choice:
2369 break;
2371 default:
2372 gcc_unreachable ();
2375 /* If the case value is a subtype that raises Constraint_Error at
2376 run time because of a wrong bound, then gnu_low or gnu_high is
2377 not translated into an INTEGER_CST. In such a case, we need
2378 to ensure that the when statement is not added in the tree,
2379 otherwise it will crash the gimplifier. */
2380 if ((!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST)
2381 && (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST))
2383 add_stmt_with_node (build_case_label
2384 (gnu_low, gnu_high,
2385 create_artificial_label (input_location)),
2386 gnat_choice);
2387 choices_added_p = true;
2391 /* This construct doesn't define a scope so we shouldn't push a binding
2392 level around the statement list. Except that we have always done so
2393 historically and this makes it possible to reduce stack usage. As a
2394 compromise, we keep doing it for case statements, for which this has
2395 never been problematic, but not for case expressions in Ada 2012. */
2396 if (choices_added_p)
2398 const bool is_case_expression
2399 = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions);
2400 tree group
2401 = build_stmt_group (Statements (gnat_when), !is_case_expression);
2402 bool group_may_fallthru = block_may_fallthru (group);
2403 add_stmt (group);
2404 if (group_may_fallthru)
2406 tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label);
2407 SET_EXPR_LOCATION (stmt, end_locus);
2408 add_stmt (stmt);
2409 may_fallthru = true;
2414 /* Now emit a definition of the label the cases branch to, if any. */
2415 if (may_fallthru)
2416 add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label));
2417 gnu_result = build3 (SWITCH_EXPR, TREE_TYPE (gnu_expr), gnu_expr,
2418 end_stmt_group (), NULL_TREE);
2420 return gnu_result;
2423 /* Find out whether VAR is an iteration variable of an enclosing loop in the
2424 current function. If so, push a range_check_info structure onto the stack
2425 of this enclosing loop and return it. Otherwise, return NULL. */
2427 static struct range_check_info_d *
2428 push_range_check_info (tree var)
2430 struct loop_info_d *iter = NULL;
2431 unsigned int i;
2433 if (vec_safe_is_empty (gnu_loop_stack))
2434 return NULL;
2436 var = remove_conversions (var, false);
2438 if (TREE_CODE (var) != VAR_DECL)
2439 return NULL;
2441 if (decl_function_context (var) != current_function_decl)
2442 return NULL;
2444 for (i = vec_safe_length (gnu_loop_stack) - 1;
2445 vec_safe_iterate (gnu_loop_stack, i, &iter);
2446 i--)
2447 if (var == iter->loop_var)
2448 break;
2450 if (iter)
2452 struct range_check_info_d *rci = ggc_alloc_range_check_info_d ();
2453 vec_safe_push (iter->checks, rci);
2454 return rci;
2457 return NULL;
2460 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2461 false, or the maximum value if MAX is true, of TYPE. */
2463 static bool
2464 can_equal_min_or_max_val_p (tree val, tree type, bool max)
2466 tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
2468 if (TREE_CODE (min_or_max_val) != INTEGER_CST)
2469 return true;
2471 if (TREE_CODE (val) == NOP_EXPR)
2472 val = (max
2473 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))
2474 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))));
2476 if (TREE_CODE (val) != INTEGER_CST)
2477 return true;
2479 if (max)
2480 return tree_int_cst_lt (val, min_or_max_val) == 0;
2481 else
2482 return tree_int_cst_lt (min_or_max_val, val) == 0;
2485 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2486 If REVERSE is true, minimum value is taken as maximum value. */
2488 static inline bool
2489 can_equal_min_val_p (tree val, tree type, bool reverse)
2491 return can_equal_min_or_max_val_p (val, type, reverse);
2494 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2495 If REVERSE is true, maximum value is taken as minimum value. */
2497 static inline bool
2498 can_equal_max_val_p (tree val, tree type, bool reverse)
2500 return can_equal_min_or_max_val_p (val, type, !reverse);
2503 /* Return true if VAL1 can be lower than VAL2. */
2505 static bool
2506 can_be_lower_p (tree val1, tree val2)
2508 if (TREE_CODE (val1) == NOP_EXPR)
2509 val1 = TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val1, 0)));
2511 if (TREE_CODE (val1) != INTEGER_CST)
2512 return true;
2514 if (TREE_CODE (val2) == NOP_EXPR)
2515 val2 = TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val2, 0)));
2517 if (TREE_CODE (val2) != INTEGER_CST)
2518 return true;
2520 return tree_int_cst_lt (val1, val2);
2523 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
2524 to a GCC tree, which is returned. */
2526 static tree
2527 Loop_Statement_to_gnu (Node_Id gnat_node)
2529 const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
2530 struct loop_info_d *gnu_loop_info = ggc_alloc_cleared_loop_info_d ();
2531 tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE,
2532 NULL_TREE, NULL_TREE, NULL_TREE);
2533 tree gnu_loop_label = create_artificial_label (input_location);
2534 tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2535 tree gnu_result;
2537 /* Push the loop_info structure associated with the LOOP_STMT. */
2538 vec_safe_push (gnu_loop_stack, gnu_loop_info);
2540 /* Set location information for statement and end label. */
2541 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
2542 Sloc_to_locus (Sloc (End_Label (gnat_node)),
2543 &DECL_SOURCE_LOCATION (gnu_loop_label));
2544 LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label;
2546 /* Save the statement for later reuse. */
2547 gnu_loop_info->stmt = gnu_loop_stmt;
2549 /* Set the condition under which the loop must keep going.
2550 For the case "LOOP .... END LOOP;" the condition is always true. */
2551 if (No (gnat_iter_scheme))
2554 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2555 else if (Present (Condition (gnat_iter_scheme)))
2556 LOOP_STMT_COND (gnu_loop_stmt)
2557 = gnat_to_gnu (Condition (gnat_iter_scheme));
2559 /* Otherwise we have an iteration scheme and the condition is given by the
2560 bounds of the subtype of the iteration variable. */
2561 else
2563 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
2564 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2565 Entity_Id gnat_type = Etype (gnat_loop_var);
2566 tree gnu_type = get_unpadded_type (gnat_type);
2567 tree gnu_base_type = get_base_type (gnu_type);
2568 tree gnu_one_node = convert (gnu_base_type, integer_one_node);
2569 tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt;
2570 enum tree_code update_code, test_code, shift_code;
2571 bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false;
2573 gnu_low = TYPE_MIN_VALUE (gnu_type);
2574 gnu_high = TYPE_MAX_VALUE (gnu_type);
2576 /* We must disable modulo reduction for the iteration variable, if any,
2577 in order for the loop comparison to be effective. */
2578 if (reverse)
2580 gnu_first = gnu_high;
2581 gnu_last = gnu_low;
2582 update_code = MINUS_NOMOD_EXPR;
2583 test_code = GE_EXPR;
2584 shift_code = PLUS_NOMOD_EXPR;
2586 else
2588 gnu_first = gnu_low;
2589 gnu_last = gnu_high;
2590 update_code = PLUS_NOMOD_EXPR;
2591 test_code = LE_EXPR;
2592 shift_code = MINUS_NOMOD_EXPR;
2595 /* We use two different strategies to translate the loop, depending on
2596 whether optimization is enabled.
2598 If it is, we generate the canonical loop form expected by the loop
2599 optimizer and the loop vectorizer, which is the do-while form:
2601 ENTRY_COND
2602 loop:
2603 TOP_UPDATE
2604 BODY
2605 BOTTOM_COND
2606 GOTO loop
2608 This avoids an implicit dependency on loop header copying and makes
2609 it possible to turn BOTTOM_COND into an inequality test.
2611 If optimization is disabled, loop header copying doesn't come into
2612 play and we try to generate the loop form with the fewer conditional
2613 branches. First, the default form, which is:
2615 loop:
2616 TOP_COND
2617 BODY
2618 BOTTOM_UPDATE
2619 GOTO loop
2621 It should catch most loops with constant ending point. Then, if we
2622 cannot, we try to generate the shifted form:
2624 loop:
2625 TOP_COND
2626 TOP_UPDATE
2627 BODY
2628 GOTO loop
2630 which should catch loops with constant starting point. Otherwise, if
2631 we cannot, we generate the fallback form:
2633 ENTRY_COND
2634 loop:
2635 BODY
2636 BOTTOM_COND
2637 BOTTOM_UPDATE
2638 GOTO loop
2640 which works in all cases. */
2642 if (optimize)
2644 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
2645 overflow. */
2646 if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse))
2649 /* Otherwise, use the do-while form with the help of a special
2650 induction variable in the unsigned version of the base type
2651 or the unsigned version of the size type, whichever is the
2652 largest, in order to have wrap-around arithmetics for it. */
2653 else
2655 if (TYPE_PRECISION (gnu_base_type)
2656 > TYPE_PRECISION (size_type_node))
2657 gnu_base_type
2658 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1);
2659 else
2660 gnu_base_type = size_type_node;
2662 gnu_first = convert (gnu_base_type, gnu_first);
2663 gnu_last = convert (gnu_base_type, gnu_last);
2664 gnu_one_node = convert (gnu_base_type, integer_one_node);
2665 use_iv = true;
2668 gnu_first
2669 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2670 gnu_one_node);
2671 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2672 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2674 else
2676 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
2677 if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse))
2680 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
2681 GNU_LAST-1 does. */
2682 else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)
2683 && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse))
2685 gnu_first
2686 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2687 gnu_one_node);
2688 gnu_last
2689 = build_binary_op (shift_code, gnu_base_type, gnu_last,
2690 gnu_one_node);
2691 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2694 /* Otherwise, use the fallback form. */
2695 else
2696 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2699 /* If we use the BOTTOM_COND, we can turn the test into an inequality
2700 test but we may have to add ENTRY_COND to protect the empty loop. */
2701 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt))
2703 test_code = NE_EXPR;
2704 if (can_be_lower_p (gnu_high, gnu_low))
2706 gnu_cond_expr
2707 = build3 (COND_EXPR, void_type_node,
2708 build_binary_op (LE_EXPR, boolean_type_node,
2709 gnu_low, gnu_high),
2710 NULL_TREE, alloc_stmt_list ());
2711 set_expr_location_from_node (gnu_cond_expr, gnat_loop_spec);
2715 /* Open a new nesting level that will surround the loop to declare the
2716 iteration variable. */
2717 start_stmt_group ();
2718 gnat_pushlevel ();
2720 /* If we use the special induction variable, create it and set it to
2721 its initial value. Morever, the regular iteration variable cannot
2722 itself be initialized, lest the initial value wrapped around. */
2723 if (use_iv)
2725 gnu_loop_iv
2726 = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var);
2727 add_stmt (gnu_stmt);
2728 gnu_first = NULL_TREE;
2730 else
2731 gnu_loop_iv = NULL_TREE;
2733 /* Declare the iteration variable and set it to its initial value. */
2734 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
2735 if (DECL_BY_REF_P (gnu_loop_var))
2736 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
2737 else if (use_iv)
2739 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var));
2740 SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv);
2742 gnu_loop_info->loop_var = gnu_loop_var;
2744 /* Do all the arithmetics in the base type. */
2745 gnu_loop_var = convert (gnu_base_type, gnu_loop_var);
2747 /* Set either the top or bottom exit condition. */
2748 if (use_iv)
2749 LOOP_STMT_COND (gnu_loop_stmt)
2750 = build_binary_op (test_code, boolean_type_node, gnu_loop_iv,
2751 gnu_last);
2752 else
2753 LOOP_STMT_COND (gnu_loop_stmt)
2754 = build_binary_op (test_code, boolean_type_node, gnu_loop_var,
2755 gnu_last);
2757 /* Set either the top or bottom update statement and give it the source
2758 location of the iteration for better coverage info. */
2759 if (use_iv)
2761 gnu_stmt
2762 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv,
2763 build_binary_op (update_code, gnu_base_type,
2764 gnu_loop_iv, gnu_one_node));
2765 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2766 append_to_statement_list (gnu_stmt,
2767 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2768 gnu_stmt
2769 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2770 gnu_loop_iv);
2771 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2772 append_to_statement_list (gnu_stmt,
2773 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2775 else
2777 gnu_stmt
2778 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2779 build_binary_op (update_code, gnu_base_type,
2780 gnu_loop_var, gnu_one_node));
2781 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2782 LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt;
2786 /* If the loop was named, have the name point to this loop. In this case,
2787 the association is not a DECL node, but the end label of the loop. */
2788 if (Present (Identifier (gnat_node)))
2789 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true);
2791 /* Make the loop body into its own block, so any allocated storage will be
2792 released every iteration. This is needed for stack allocation. */
2793 LOOP_STMT_BODY (gnu_loop_stmt)
2794 = build_stmt_group (Statements (gnat_node), true);
2795 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
2797 /* If we have an iteration scheme, then we are in a statement group. Add
2798 the LOOP_STMT to it, finish it and make it the "loop". */
2799 if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme)))
2801 struct range_check_info_d *rci;
2802 unsigned n_checks = vec_safe_length (gnu_loop_info->checks);
2803 unsigned int i;
2805 /* First, if we have computed a small number of invariant conditions for
2806 range checks applied to the iteration variable, then initialize these
2807 conditions in front of the loop. Otherwise, leave them set to true.
2809 ??? The heuristics need to be improved, by taking into account the
2810 following datapoints:
2811 - loop unswitching is disabled for big loops. The cap is the
2812 parameter PARAM_MAX_UNSWITCH_INSNS (50).
2813 - loop unswitching can only be applied a small number of times
2814 to a given loop. The cap is PARAM_MAX_UNSWITCH_LEVEL (3).
2815 - the front-end quickly generates useless or redundant checks
2816 that can be entirely optimized away in the end. */
2817 if (1 <= n_checks && n_checks <= 4)
2818 for (i = 0;
2819 vec_safe_iterate (gnu_loop_info->checks, i, &rci);
2820 i++)
2822 tree low_ok
2823 = rci->low_bound
2824 ? build_binary_op (GE_EXPR, boolean_type_node,
2825 convert (rci->type, gnu_low),
2826 rci->low_bound)
2827 : boolean_true_node;
2829 tree high_ok
2830 = rci->high_bound
2831 ? build_binary_op (LE_EXPR, boolean_type_node,
2832 convert (rci->type, gnu_high),
2833 rci->high_bound)
2834 : boolean_true_node;
2836 tree range_ok
2837 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
2838 low_ok, high_ok);
2840 TREE_OPERAND (rci->invariant_cond, 0)
2841 = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok);
2843 add_stmt_with_node_force (rci->invariant_cond, gnat_node);
2846 add_stmt (gnu_loop_stmt);
2847 gnat_poplevel ();
2848 gnu_loop_stmt = end_stmt_group ();
2851 /* If we have an outer COND_EXPR, that's our result and this loop is its
2852 "true" statement. Otherwise, the result is the LOOP_STMT. */
2853 if (gnu_cond_expr)
2855 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
2856 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
2857 gnu_result = gnu_cond_expr;
2859 else
2860 gnu_result = gnu_loop_stmt;
2862 gnu_loop_stack->pop ();
2864 return gnu_result;
2867 /* Emit statements to establish __gnat_handle_vms_condition as a VMS condition
2868 handler for the current function. */
2870 /* This is implemented by issuing a call to the appropriate VMS specific
2871 builtin. To avoid having VMS specific sections in the global gigi decls
2872 array, we maintain the decls of interest here. We can't declare them
2873 inside the function because we must mark them never to be GC'd, which we
2874 can only do at the global level. */
2876 static GTY(()) tree vms_builtin_establish_handler_decl = NULL_TREE;
2877 static GTY(()) tree gnat_vms_condition_handler_decl = NULL_TREE;
2879 static void
2880 establish_gnat_vms_condition_handler (void)
2882 tree establish_stmt;
2884 /* Elaborate the required decls on the first call. Check on the decl for
2885 the gnat condition handler to decide, as this is one we create so we are
2886 sure that it will be non null on subsequent calls. The builtin decl is
2887 looked up so remains null on targets where it is not implemented yet. */
2888 if (gnat_vms_condition_handler_decl == NULL_TREE)
2890 vms_builtin_establish_handler_decl
2891 = builtin_decl_for
2892 (get_identifier ("__builtin_establish_vms_condition_handler"));
2894 gnat_vms_condition_handler_decl
2895 = create_subprog_decl (get_identifier ("__gnat_handle_vms_condition"),
2896 NULL_TREE,
2897 build_function_type_list (boolean_type_node,
2898 ptr_void_type_node,
2899 ptr_void_type_node,
2900 NULL_TREE),
2901 NULL_TREE, is_disabled, true, true, true, NULL,
2902 Empty);
2904 /* ??? DECL_CONTEXT shouldn't have been set because of DECL_EXTERNAL. */
2905 DECL_CONTEXT (gnat_vms_condition_handler_decl) = NULL_TREE;
2908 /* Do nothing if the establish builtin is not available, which might happen
2909 on targets where the facility is not implemented. */
2910 if (vms_builtin_establish_handler_decl == NULL_TREE)
2911 return;
2913 establish_stmt
2914 = build_call_n_expr (vms_builtin_establish_handler_decl, 1,
2915 build_unary_op
2916 (ADDR_EXPR, NULL_TREE,
2917 gnat_vms_condition_handler_decl));
2919 add_stmt (establish_stmt);
2922 /* This page implements a form of Named Return Value optimization modelled
2923 on the C++ optimization of the same name. The main difference is that
2924 we disregard any semantical considerations when applying it here, the
2925 counterpart being that we don't try to apply it to semantically loaded
2926 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
2928 We consider a function body of the following GENERIC form:
2930 return_type R1;
2931 [...]
2932 RETURN_EXPR [<retval> = ...]
2933 [...]
2934 RETURN_EXPR [<retval> = R1]
2935 [...]
2936 return_type Ri;
2937 [...]
2938 RETURN_EXPR [<retval> = ...]
2939 [...]
2940 RETURN_EXPR [<retval> = Ri]
2941 [...]
2943 and we try to fulfill a simple criterion that would make it possible to
2944 replace one or several Ri variables with the RESULT_DECL of the function.
2946 The first observation is that RETURN_EXPRs that don't directly reference
2947 any of the Ri variables on the RHS of their assignment are transparent wrt
2948 the optimization. This is because the Ri variables aren't addressable so
2949 any transformation applied to them doesn't affect the RHS; moreover, the
2950 assignment writes the full <retval> object so existing values are entirely
2951 discarded.
2953 This property can be extended to some forms of RETURN_EXPRs that reference
2954 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
2955 case, in particular when function calls are involved.
2957 Therefore the algorithm is as follows:
2959 1. Collect the list of candidates for a Named Return Value (Ri variables
2960 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
2961 other expressions on the RHS of such assignments.
2963 2. Prune the members of the first list (candidates) that are referenced
2964 by a member of the second list (expressions).
2966 3. Extract a set of candidates with non-overlapping live ranges from the
2967 first list. These are the Named Return Values.
2969 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
2970 Named Return Values in the function with the RESULT_DECL.
2972 If the function returns an unconstrained type, things are a bit different
2973 because the anonymous return object is allocated on the secondary stack
2974 and RESULT_DECL is only a pointer to it. Each return object can be of a
2975 different size and is allocated separately so we need not care about the
2976 aforementioned overlapping issues. Therefore, we don't collect the other
2977 expressions and skip step #2 in the algorithm. */
2979 struct nrv_data
2981 bitmap nrv;
2982 tree result;
2983 Node_Id gnat_ret;
2984 struct pointer_set_t *visited;
2987 /* Return true if T is a Named Return Value. */
2989 static inline bool
2990 is_nrv_p (bitmap nrv, tree t)
2992 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
2995 /* Helper function for walk_tree, used by finalize_nrv below. */
2997 static tree
2998 prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3000 struct nrv_data *dp = (struct nrv_data *)data;
3001 tree t = *tp;
3003 /* No need to walk into types or decls. */
3004 if (IS_TYPE_OR_DECL_P (t))
3005 *walk_subtrees = 0;
3007 if (is_nrv_p (dp->nrv, t))
3008 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3010 return NULL_TREE;
3013 /* Prune Named Return Values in BLOCK and return true if there is still a
3014 Named Return Value in BLOCK or one of its sub-blocks. */
3016 static bool
3017 prune_nrv_in_block (bitmap nrv, tree block)
3019 bool has_nrv = false;
3020 tree t;
3022 /* First recurse on the sub-blocks. */
3023 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3024 has_nrv |= prune_nrv_in_block (nrv, t);
3026 /* Then make sure to keep at most one NRV per block. */
3027 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3028 if (is_nrv_p (nrv, t))
3030 if (has_nrv)
3031 bitmap_clear_bit (nrv, DECL_UID (t));
3032 else
3033 has_nrv = true;
3036 return has_nrv;
3039 /* Helper function for walk_tree, used by finalize_nrv below. */
3041 static tree
3042 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3044 struct nrv_data *dp = (struct nrv_data *)data;
3045 tree t = *tp;
3047 /* No need to walk into types. */
3048 if (TYPE_P (t))
3049 *walk_subtrees = 0;
3051 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3052 nop, but differs from using NULL_TREE in that it indicates that we care
3053 about the value of the RESULT_DECL. */
3054 else if (TREE_CODE (t) == RETURN_EXPR
3055 && TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR)
3057 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1), init_expr;
3059 /* If this is the temporary created for a return value with variable
3060 size in Call_to_gnu, we replace the RHS with the init expression. */
3061 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3062 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3063 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3064 == TREE_OPERAND (ret_val, 1))
3066 init_expr = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3067 ret_val = TREE_OPERAND (ret_val, 1);
3069 else
3070 init_expr = NULL_TREE;
3072 /* Strip useless conversions around the return value. */
3073 if (gnat_useless_type_conversion (ret_val))
3074 ret_val = TREE_OPERAND (ret_val, 0);
3076 if (is_nrv_p (dp->nrv, ret_val))
3078 if (init_expr)
3079 TREE_OPERAND (TREE_OPERAND (t, 0), 1) = init_expr;
3080 else
3081 TREE_OPERAND (t, 0) = dp->result;
3085 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3086 if needed. */
3087 else if (TREE_CODE (t) == DECL_EXPR
3088 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3090 tree var = DECL_EXPR_DECL (t), init;
3092 if (DECL_INITIAL (var))
3094 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3095 DECL_INITIAL (var));
3096 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3097 DECL_INITIAL (var) = NULL_TREE;
3099 else
3100 init = build_empty_stmt (EXPR_LOCATION (t));
3101 *tp = init;
3103 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3104 SET_DECL_VALUE_EXPR (var, dp->result);
3105 DECL_HAS_VALUE_EXPR_P (var) = 1;
3106 /* ??? Kludge to avoid an assertion failure during inlining. */
3107 DECL_SIZE (var) = bitsize_unit_node;
3108 DECL_SIZE_UNIT (var) = size_one_node;
3111 /* And replace all uses of NRVs with the RESULT_DECL. */
3112 else if (is_nrv_p (dp->nrv, t))
3113 *tp = convert (TREE_TYPE (t), dp->result);
3115 /* Avoid walking into the same tree more than once. Unfortunately, we
3116 can't just use walk_tree_without_duplicates because it would only
3117 call us for the first occurrence of NRVs in the function body. */
3118 if (pointer_set_insert (dp->visited, *tp))
3119 *walk_subtrees = 0;
3121 return NULL_TREE;
3124 /* Likewise, but used when the function returns an unconstrained type. */
3126 static tree
3127 finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3129 struct nrv_data *dp = (struct nrv_data *)data;
3130 tree t = *tp;
3132 /* No need to walk into types. */
3133 if (TYPE_P (t))
3134 *walk_subtrees = 0;
3136 /* We need to see the DECL_EXPR of NRVs before any other references so we
3137 walk the body of BIND_EXPR before walking its variables. */
3138 else if (TREE_CODE (t) == BIND_EXPR)
3139 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3141 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3142 return value built by the allocator instead of the whole construct. */
3143 else if (TREE_CODE (t) == RETURN_EXPR
3144 && TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR)
3146 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3148 /* This is the construct returned by the allocator. */
3149 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3150 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3152 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
3153 ret_val
3154 = (*CONSTRUCTOR_ELTS (TREE_OPERAND (TREE_OPERAND (ret_val, 0),
3155 1)))[1].value;
3156 else
3157 ret_val = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3160 /* Strip useless conversions around the return value. */
3161 if (gnat_useless_type_conversion (ret_val)
3162 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3163 ret_val = TREE_OPERAND (ret_val, 0);
3165 /* Strip unpadding around the return value. */
3166 if (TREE_CODE (ret_val) == COMPONENT_REF
3167 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3168 ret_val = TREE_OPERAND (ret_val, 0);
3170 /* Assign the new return value to the RESULT_DECL. */
3171 if (is_nrv_p (dp->nrv, ret_val))
3172 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3173 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3176 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3177 into a new variable. */
3178 else if (TREE_CODE (t) == DECL_EXPR
3179 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3181 tree saved_current_function_decl = current_function_decl;
3182 tree var = DECL_EXPR_DECL (t);
3183 tree alloc, p_array, new_var, new_ret;
3184 vec<constructor_elt, va_gc> *v;
3185 vec_alloc (v, 2);
3187 /* Create an artificial context to build the allocation. */
3188 current_function_decl = decl_function_context (var);
3189 start_stmt_group ();
3190 gnat_pushlevel ();
3192 /* This will return a COMPOUND_EXPR with the allocation in the first
3193 arm and the final return value in the second arm. */
3194 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3195 TREE_TYPE (dp->result),
3196 Procedure_To_Call (dp->gnat_ret),
3197 Storage_Pool (dp->gnat_ret),
3198 Empty, false);
3200 /* The new variable is built as a reference to the allocated space. */
3201 new_var
3202 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3203 build_reference_type (TREE_TYPE (var)));
3204 DECL_BY_REFERENCE (new_var) = 1;
3206 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3208 /* The new initial value is a COMPOUND_EXPR with the allocation in
3209 the first arm and the value of P_ARRAY in the second arm. */
3210 DECL_INITIAL (new_var)
3211 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3212 TREE_OPERAND (alloc, 0),
3213 (*CONSTRUCTOR_ELTS (TREE_OPERAND (alloc, 1)))[0].value);
3215 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3216 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3217 CONSTRUCTOR_APPEND_ELT (v, p_array,
3218 fold_convert (TREE_TYPE (p_array), new_var));
3219 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
3220 (*CONSTRUCTOR_ELTS (
3221 TREE_OPERAND (alloc, 1)))[1].value);
3222 new_ret = build_constructor (TREE_TYPE (alloc), v);
3224 else
3226 /* The new initial value is just the allocation. */
3227 DECL_INITIAL (new_var) = alloc;
3228 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3231 gnat_pushdecl (new_var, Empty);
3233 /* Destroy the artificial context and insert the new statements. */
3234 gnat_zaplevel ();
3235 *tp = end_stmt_group ();
3236 current_function_decl = saved_current_function_decl;
3238 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3239 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3240 DECL_CHAIN (var) = new_var;
3241 DECL_IGNORED_P (var) = 1;
3243 /* Save the new return value and the dereference of NEW_VAR. */
3244 DECL_INITIAL (var)
3245 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3246 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3247 /* ??? Kludge to avoid messing up during inlining. */
3248 DECL_CONTEXT (var) = NULL_TREE;
3251 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3252 else if (is_nrv_p (dp->nrv, t))
3253 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3255 /* Avoid walking into the same tree more than once. Unfortunately, we
3256 can't just use walk_tree_without_duplicates because it would only
3257 call us for the first occurrence of NRVs in the function body. */
3258 if (pointer_set_insert (dp->visited, *tp))
3259 *walk_subtrees = 0;
3261 return NULL_TREE;
3264 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3265 contains the candidates for Named Return Value and OTHER is a list of
3266 the other return values. GNAT_RET is a representative return node. */
3268 static void
3269 finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
3271 struct cgraph_node *node;
3272 struct nrv_data data;
3273 walk_tree_fn func;
3274 unsigned int i;
3275 tree iter;
3277 /* We shouldn't be applying the optimization to return types that we aren't
3278 allowed to manipulate freely. */
3279 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
3281 /* Prune the candidates that are referenced by other return values. */
3282 data.nrv = nrv;
3283 data.result = NULL_TREE;
3284 data.visited = NULL;
3285 for (i = 0; vec_safe_iterate (other, i, &iter); i++)
3286 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3287 if (bitmap_empty_p (nrv))
3288 return;
3290 /* Prune also the candidates that are referenced by nested functions. */
3291 node = cgraph_get_create_node (fndecl);
3292 for (node = node->nested; node; node = node->next_nested)
3293 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), prune_nrv_r,
3294 &data);
3295 if (bitmap_empty_p (nrv))
3296 return;
3298 /* Extract a set of NRVs with non-overlapping live ranges. */
3299 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3300 return;
3302 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3303 data.nrv = nrv;
3304 data.result = DECL_RESULT (fndecl);
3305 data.gnat_ret = gnat_ret;
3306 data.visited = pointer_set_create ();
3307 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl)))
3308 func = finalize_nrv_unc_r;
3309 else
3310 func = finalize_nrv_r;
3311 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
3312 pointer_set_destroy (data.visited);
3315 /* Return true if RET_VAL can be used as a Named Return Value for the
3316 anonymous return object RET_OBJ. */
3318 static bool
3319 return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3321 if (TREE_CODE (ret_val) != VAR_DECL)
3322 return false;
3324 if (TREE_THIS_VOLATILE (ret_val))
3325 return false;
3327 if (DECL_CONTEXT (ret_val) != current_function_decl)
3328 return false;
3330 if (TREE_STATIC (ret_val))
3331 return false;
3333 if (TREE_ADDRESSABLE (ret_val))
3334 return false;
3336 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
3337 return false;
3339 return true;
3342 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3343 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3344 around RESULT_OBJ, which may be null in this case. */
3346 static tree
3347 build_return_expr (tree ret_obj, tree ret_val)
3349 tree result_expr;
3351 if (ret_val)
3353 /* The gimplifier explicitly enforces the following invariant:
3355 RETURN_EXPR
3357 MODIFY_EXPR
3360 RET_OBJ ...
3362 As a consequence, type consistency dictates that we use the type
3363 of the RET_OBJ as the operation type. */
3364 tree operation_type = TREE_TYPE (ret_obj);
3366 /* Convert the right operand to the operation type. Note that it's the
3367 same transformation as in the MODIFY_EXPR case of build_binary_op,
3368 with the assumption that the type cannot involve a placeholder. */
3369 if (operation_type != TREE_TYPE (ret_val))
3370 ret_val = convert (operation_type, ret_val);
3372 result_expr = build2 (MODIFY_EXPR, void_type_node, ret_obj, ret_val);
3374 /* If the function returns an aggregate type, find out whether this is
3375 a candidate for Named Return Value. If so, record it. Otherwise,
3376 if this is an expression of some kind, record it elsewhere. */
3377 if (optimize
3378 && AGGREGATE_TYPE_P (operation_type)
3379 && !TYPE_IS_FAT_POINTER_P (operation_type)
3380 && TYPE_MODE (operation_type) == BLKmode
3381 && aggregate_value_p (operation_type, current_function_decl))
3383 /* Recognize the temporary created for a return value with variable
3384 size in Call_to_gnu. We want to eliminate it if possible. */
3385 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3386 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3387 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3388 == TREE_OPERAND (ret_val, 1))
3389 ret_val = TREE_OPERAND (ret_val, 1);
3391 /* Strip useless conversions around the return value. */
3392 if (gnat_useless_type_conversion (ret_val))
3393 ret_val = TREE_OPERAND (ret_val, 0);
3395 /* Now apply the test to the return value. */
3396 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3398 if (!f_named_ret_val)
3399 f_named_ret_val = BITMAP_GGC_ALLOC ();
3400 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3403 /* Note that we need not care about CONSTRUCTORs here, as they are
3404 totally transparent given the read-compose-write semantics of
3405 assignments from CONSTRUCTORs. */
3406 else if (EXPR_P (ret_val))
3407 vec_safe_push (f_other_ret_val, ret_val);
3410 else
3411 result_expr = ret_obj;
3413 return build1 (RETURN_EXPR, void_type_node, result_expr);
3416 /* Build a stub for the subprogram specified by the GCC tree GNU_SUBPROG
3417 and the GNAT node GNAT_SUBPROG. */
3419 static void
3420 build_function_stub (tree gnu_subprog, Entity_Id gnat_subprog)
3422 tree gnu_subprog_type, gnu_subprog_addr, gnu_subprog_call;
3423 tree gnu_subprog_param, gnu_stub_param, gnu_param;
3424 tree gnu_stub_decl = DECL_FUNCTION_STUB (gnu_subprog);
3425 vec<tree, va_gc> *gnu_param_vec = NULL;
3427 gnu_subprog_type = TREE_TYPE (gnu_subprog);
3429 /* Initialize the information structure for the function. */
3430 allocate_struct_function (gnu_stub_decl, false);
3431 set_cfun (NULL);
3433 begin_subprog_body (gnu_stub_decl);
3435 start_stmt_group ();
3436 gnat_pushlevel ();
3438 /* Loop over the parameters of the stub and translate any of them
3439 passed by descriptor into a by reference one. */
3440 for (gnu_stub_param = DECL_ARGUMENTS (gnu_stub_decl),
3441 gnu_subprog_param = DECL_ARGUMENTS (gnu_subprog);
3442 gnu_stub_param;
3443 gnu_stub_param = DECL_CHAIN (gnu_stub_param),
3444 gnu_subprog_param = DECL_CHAIN (gnu_subprog_param))
3446 if (DECL_BY_DESCRIPTOR_P (gnu_stub_param))
3448 gcc_assert (DECL_BY_REF_P (gnu_subprog_param));
3449 gnu_param
3450 = convert_vms_descriptor (TREE_TYPE (gnu_subprog_param),
3451 gnu_stub_param,
3452 DECL_PARM_ALT_TYPE (gnu_stub_param),
3453 gnat_subprog);
3455 else
3456 gnu_param = gnu_stub_param;
3458 vec_safe_push (gnu_param_vec, gnu_param);
3461 /* Invoke the internal subprogram. */
3462 gnu_subprog_addr = build1 (ADDR_EXPR, build_pointer_type (gnu_subprog_type),
3463 gnu_subprog);
3464 gnu_subprog_call = build_call_vec (TREE_TYPE (gnu_subprog_type),
3465 gnu_subprog_addr, gnu_param_vec);
3467 /* Propagate the return value, if any. */
3468 if (VOID_TYPE_P (TREE_TYPE (gnu_subprog_type)))
3469 add_stmt (gnu_subprog_call);
3470 else
3471 add_stmt (build_return_expr (DECL_RESULT (gnu_stub_decl),
3472 gnu_subprog_call));
3474 gnat_poplevel ();
3475 end_subprog_body (end_stmt_group ());
3476 rest_of_subprog_body_compilation (gnu_stub_decl);
3479 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3480 don't return anything. */
3482 static void
3483 Subprogram_Body_to_gnu (Node_Id gnat_node)
3485 /* Defining identifier of a parameter to the subprogram. */
3486 Entity_Id gnat_param;
3487 /* The defining identifier for the subprogram body. Note that if a
3488 specification has appeared before for this body, then the identifier
3489 occurring in that specification will also be a defining identifier and all
3490 the calls to this subprogram will point to that specification. */
3491 Entity_Id gnat_subprog_id
3492 = (Present (Corresponding_Spec (gnat_node))
3493 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
3494 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3495 tree gnu_subprog_decl;
3496 /* Its RESULT_DECL node. */
3497 tree gnu_result_decl;
3498 /* Its FUNCTION_TYPE node. */
3499 tree gnu_subprog_type;
3500 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3501 tree gnu_cico_list;
3502 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3503 tree gnu_return_var_elmt = NULL_TREE;
3504 tree gnu_result;
3505 struct language_function *gnu_subprog_language;
3506 vec<parm_attr, va_gc> *cache;
3508 /* If this is a generic object or if it has been eliminated,
3509 ignore it. */
3510 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
3511 || Ekind (gnat_subprog_id) == E_Generic_Function
3512 || Is_Eliminated (gnat_subprog_id))
3513 return;
3515 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3516 the already-elaborated tree node. However, if this subprogram had its
3517 elaboration deferred, we will already have made a tree node for it. So
3518 treat it as not being defined in that case. Such a subprogram cannot
3519 have an address clause or a freeze node, so this test is safe, though it
3520 does disable some otherwise-useful error checking. */
3521 gnu_subprog_decl
3522 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
3523 Acts_As_Spec (gnat_node)
3524 && !present_gnu_tree (gnat_subprog_id));
3525 gnu_result_decl = DECL_RESULT (gnu_subprog_decl);
3526 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
3527 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3528 if (gnu_cico_list)
3529 gnu_return_var_elmt = value_member (void_type_node, gnu_cico_list);
3531 /* If the function returns by invisible reference, make it explicit in the
3532 function body. See gnat_to_gnu_entity, E_Subprogram_Type case.
3533 Handle the explicit case here and the copy-in/copy-out case below. */
3534 if (TREE_ADDRESSABLE (gnu_subprog_type) && !gnu_return_var_elmt)
3536 TREE_TYPE (gnu_result_decl)
3537 = build_reference_type (TREE_TYPE (gnu_result_decl));
3538 relayout_decl (gnu_result_decl);
3541 /* Set the line number in the decl to correspond to that of the body so that
3542 the line number notes are written correctly. */
3543 Sloc_to_locus (Sloc (gnat_node), &DECL_SOURCE_LOCATION (gnu_subprog_decl));
3545 /* Initialize the information structure for the function. */
3546 allocate_struct_function (gnu_subprog_decl, false);
3547 gnu_subprog_language = ggc_alloc_cleared_language_function ();
3548 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language = gnu_subprog_language;
3549 set_cfun (NULL);
3551 begin_subprog_body (gnu_subprog_decl);
3553 /* If there are In Out or Out parameters, we need to ensure that the return
3554 statement properly copies them out. We do this by making a new block and
3555 converting any return into a goto to a label at the end of the block. */
3556 if (gnu_cico_list)
3558 tree gnu_return_var = NULL_TREE;
3560 vec_safe_push (gnu_return_label_stack,
3561 create_artificial_label (input_location));
3563 start_stmt_group ();
3564 gnat_pushlevel ();
3566 /* If this is a function with In Out or Out parameters, we also need a
3567 variable for the return value to be placed. */
3568 if (gnu_return_var_elmt)
3570 tree gnu_return_type
3571 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3573 /* If the function returns by invisible reference, make it
3574 explicit in the function body. See gnat_to_gnu_entity,
3575 E_Subprogram_Type case. */
3576 if (TREE_ADDRESSABLE (gnu_subprog_type))
3577 gnu_return_type = build_reference_type (gnu_return_type);
3579 gnu_return_var
3580 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
3581 gnu_return_type, NULL_TREE, false, false,
3582 false, false, NULL, gnat_subprog_id);
3583 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3586 vec_safe_push (gnu_return_var_stack, gnu_return_var);
3588 /* See whether there are parameters for which we don't have a GCC tree
3589 yet. These must be Out parameters. Make a VAR_DECL for them and
3590 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3591 We can match up the entries because TYPE_CI_CO_LIST is in the order
3592 of the parameters. */
3593 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3594 Present (gnat_param);
3595 gnat_param = Next_Formal_With_Extras (gnat_param))
3596 if (!present_gnu_tree (gnat_param))
3598 tree gnu_cico_entry = gnu_cico_list;
3599 tree gnu_decl;
3601 /* Skip any entries that have been already filled in; they must
3602 correspond to In Out parameters. */
3603 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3604 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3606 /* Do any needed dereferences for by-ref objects. */
3607 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, 1);
3608 gcc_assert (DECL_P (gnu_decl));
3609 if (DECL_BY_REF_P (gnu_decl))
3610 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3612 /* Do any needed references for padded types. */
3613 TREE_VALUE (gnu_cico_entry)
3614 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
3617 else
3618 vec_safe_push (gnu_return_label_stack, NULL_TREE);
3620 /* Get a tree corresponding to the code for the subprogram. */
3621 start_stmt_group ();
3622 gnat_pushlevel ();
3624 /* On VMS, establish our condition handler to possibly turn a condition into
3625 the corresponding exception if the subprogram has a foreign convention or
3626 is exported.
3628 To ensure proper execution of local finalizations on condition instances,
3629 we must turn a condition into the corresponding exception even if there
3630 is no applicable Ada handler, and need at least one condition handler per
3631 possible call chain involving GNAT code. OTOH, establishing the handler
3632 has a cost so we want to minimize the number of subprograms into which
3633 this happens. The foreign or exported condition is expected to satisfy
3634 all the constraints. */
3635 if (TARGET_ABI_OPEN_VMS
3636 && (Has_Foreign_Convention (gnat_subprog_id)
3637 || Is_Exported (gnat_subprog_id)))
3638 establish_gnat_vms_condition_handler ();
3640 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3642 /* Generate the code of the subprogram itself. A return statement will be
3643 present and any Out parameters will be handled there. */
3644 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3645 gnat_poplevel ();
3646 gnu_result = end_stmt_group ();
3648 /* If we populated the parameter attributes cache, we need to make sure that
3649 the cached expressions are evaluated on all the possible paths leading to
3650 their uses. So we force their evaluation on entry of the function. */
3651 cache = gnu_subprog_language->parm_attr_cache;
3652 if (cache)
3654 struct parm_attr_d *pa;
3655 int i;
3657 start_stmt_group ();
3659 FOR_EACH_VEC_ELT (*cache, i, pa)
3661 if (pa->first)
3662 add_stmt_with_node_force (pa->first, gnat_node);
3663 if (pa->last)
3664 add_stmt_with_node_force (pa->last, gnat_node);
3665 if (pa->length)
3666 add_stmt_with_node_force (pa->length, gnat_node);
3669 add_stmt (gnu_result);
3670 gnu_result = end_stmt_group ();
3672 gnu_subprog_language->parm_attr_cache = NULL;
3675 /* If we are dealing with a return from an Ada procedure with parameters
3676 passed by copy-in/copy-out, we need to return a record containing the
3677 final values of these parameters. If the list contains only one entry,
3678 return just that entry though.
3680 For a full description of the copy-in/copy-out parameter mechanism, see
3681 the part of the gnat_to_gnu_entity routine dealing with the translation
3682 of subprograms.
3684 We need to make a block that contains the definition of that label and
3685 the copying of the return value. It first contains the function, then
3686 the label and copy statement. */
3687 if (gnu_cico_list)
3689 tree gnu_retval;
3691 gnu_return_var_stack->pop ();
3693 add_stmt (gnu_result);
3694 add_stmt (build1 (LABEL_EXPR, void_type_node,
3695 gnu_return_label_stack->last ()));
3697 if (list_length (gnu_cico_list) == 1)
3698 gnu_retval = TREE_VALUE (gnu_cico_list);
3699 else
3700 gnu_retval = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
3701 gnu_cico_list);
3703 add_stmt_with_node (build_return_expr (gnu_result_decl, gnu_retval),
3704 End_Label (Handled_Statement_Sequence (gnat_node)));
3705 gnat_poplevel ();
3706 gnu_result = end_stmt_group ();
3709 gnu_return_label_stack->pop ();
3711 /* Attempt setting the end_locus of our GCC body tree, typically a
3712 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3713 declaration tree. */
3714 set_end_locus_from_node (gnu_result, gnat_node);
3715 set_end_locus_from_node (gnu_subprog_decl, gnat_node);
3717 /* On SEH targets, install an exception handler around the main entry
3718 point to catch unhandled exceptions. */
3719 if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
3720 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
3722 tree t;
3723 tree etype;
3725 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
3726 1, integer_zero_node);
3727 t = build_call_n_expr (unhandled_except_decl, 1, t);
3729 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
3730 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
3732 t = build2 (CATCH_EXPR, void_type_node, etype, t);
3733 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
3734 gnu_result, t);
3737 end_subprog_body (gnu_result);
3739 /* Finally annotate the parameters and disconnect the trees for parameters
3740 that we have turned into variables since they are now unusable. */
3741 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3742 Present (gnat_param);
3743 gnat_param = Next_Formal_With_Extras (gnat_param))
3745 tree gnu_param = get_gnu_tree (gnat_param);
3746 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
3748 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
3749 DECL_BY_REF_P (gnu_param));
3751 if (is_var_decl)
3752 save_gnu_tree (gnat_param, NULL_TREE, false);
3755 /* Disconnect the variable created for the return value. */
3756 if (gnu_return_var_elmt)
3757 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
3759 /* If the function returns an aggregate type and we have candidates for
3760 a Named Return Value, finalize the optimization. */
3761 if (optimize && gnu_subprog_language->named_ret_val)
3763 finalize_nrv (gnu_subprog_decl,
3764 gnu_subprog_language->named_ret_val,
3765 gnu_subprog_language->other_ret_val,
3766 gnu_subprog_language->gnat_ret);
3767 gnu_subprog_language->named_ret_val = NULL;
3768 gnu_subprog_language->other_ret_val = NULL;
3771 rest_of_subprog_body_compilation (gnu_subprog_decl);
3773 /* If there is a stub associated with the function, build it now. */
3774 if (DECL_FUNCTION_STUB (gnu_subprog_decl))
3775 build_function_stub (gnu_subprog_decl, gnat_subprog_id);
3778 /* Return true if GNAT_NODE requires atomic synchronization. */
3780 static bool
3781 atomic_sync_required_p (Node_Id gnat_node)
3783 const Node_Id gnat_parent = Parent (gnat_node);
3784 Node_Kind kind;
3785 unsigned char attr_id;
3787 /* First, scan the node to find the Atomic_Sync_Required flag. */
3788 kind = Nkind (gnat_node);
3789 if (kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
3791 gnat_node = Expression (gnat_node);
3792 kind = Nkind (gnat_node);
3795 switch (kind)
3797 case N_Expanded_Name:
3798 case N_Explicit_Dereference:
3799 case N_Identifier:
3800 case N_Indexed_Component:
3801 case N_Selected_Component:
3802 if (!Atomic_Sync_Required (gnat_node))
3803 return false;
3804 break;
3806 default:
3807 return false;
3810 /* Then, scan the parent to find out cases where the flag is irrelevant. */
3811 kind = Nkind (gnat_parent);
3812 switch (kind)
3814 case N_Attribute_Reference:
3815 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
3816 /* Do not mess up machine code insertions. */
3817 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
3818 return false;
3819 break;
3821 case N_Object_Renaming_Declaration:
3822 /* Do not generate a function call as a renamed object. */
3823 return false;
3825 default:
3826 break;
3829 return true;
3832 /* Create a temporary variable with PREFIX and TYPE, and return it. */
3834 static tree
3835 create_temporary (const char *prefix, tree type)
3837 tree gnu_temp = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
3838 type, NULL_TREE, false, false, false, false,
3839 NULL, Empty);
3840 DECL_ARTIFICIAL (gnu_temp) = 1;
3841 DECL_IGNORED_P (gnu_temp) = 1;
3843 return gnu_temp;
3846 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
3847 Put the initialization statement into GNU_INIT_STMT and annotate it with
3848 the SLOC of GNAT_NODE. Return the temporary variable. */
3850 static tree
3851 create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
3852 Node_Id gnat_node)
3854 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
3856 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
3857 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
3859 return gnu_temp;
3862 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
3863 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
3864 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
3865 If GNU_TARGET is non-null, this must be a function call on the RHS of a
3866 N_Assignment_Statement and the result is to be placed into that object.
3867 If, in addition, ATOMIC_SYNC is true, then the assignment to GNU_TARGET
3868 requires atomic synchronization. */
3870 static tree
3871 Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
3872 bool atomic_sync)
3874 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
3875 const bool returning_value = (function_call && !gnu_target);
3876 /* The GCC node corresponding to the GNAT subprogram name. This can either
3877 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
3878 or an indirect reference expression (an INDIRECT_REF node) pointing to a
3879 subprogram. */
3880 tree gnu_subprog = gnat_to_gnu (Name (gnat_node));
3881 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
3882 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
3883 /* The return type of the FUNCTION_TYPE. */
3884 tree gnu_result_type = TREE_TYPE (gnu_subprog_type);
3885 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog);
3886 vec<tree, va_gc> *gnu_actual_vec = NULL;
3887 tree gnu_name_list = NULL_TREE;
3888 tree gnu_stmt_list = NULL_TREE;
3889 tree gnu_after_list = NULL_TREE;
3890 tree gnu_retval = NULL_TREE;
3891 tree gnu_call, gnu_result;
3892 bool went_into_elab_proc = false;
3893 bool pushed_binding_level = false;
3894 Entity_Id gnat_formal;
3895 Node_Id gnat_actual;
3897 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
3899 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
3900 all our args first. */
3901 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL && DECL_STUBBED_P (gnu_subprog))
3903 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
3904 gnat_node, N_Raise_Program_Error);
3906 for (gnat_actual = First_Actual (gnat_node);
3907 Present (gnat_actual);
3908 gnat_actual = Next_Actual (gnat_actual))
3909 add_stmt (gnat_to_gnu (gnat_actual));
3911 if (returning_value)
3913 *gnu_result_type_p = gnu_result_type;
3914 return build1 (NULL_EXPR, gnu_result_type, call_expr);
3917 return call_expr;
3920 /* The only way we can be making a call via an access type is if Name is an
3921 explicit dereference. In that case, get the list of formal args from the
3922 type the access type is pointing to. Otherwise, get the formals from the
3923 entity being called. */
3924 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
3925 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
3926 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
3927 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
3928 gnat_formal = Empty;
3929 else
3930 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
3932 /* The lifetime of the temporaries created for the call ends right after the
3933 return value is copied, so we can give them the scope of the elaboration
3934 routine at top level. */
3935 if (!current_function_decl)
3937 current_function_decl = get_elaboration_procedure ();
3938 went_into_elab_proc = true;
3941 /* First, create the temporary for the return value when:
3943 1. There is no target and the function has copy-in/copy-out parameters,
3944 because we need to preserve the return value before copying back the
3945 parameters.
3947 2. There is no target and this is not an object declaration, and the
3948 return type has variable size, because in these cases the gimplifier
3949 cannot create the temporary.
3951 3. There is a target and it is a slice or an array with fixed size,
3952 and the return type has variable size, because the gimplifier
3953 doesn't handle these cases.
3955 This must be done before we push a binding level around the call, since
3956 we will pop it before copying the return value. */
3957 if (function_call
3958 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
3959 || (!gnu_target
3960 && Nkind (Parent (gnat_node)) != N_Object_Declaration
3961 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)
3962 || (gnu_target
3963 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
3964 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
3965 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
3966 == INTEGER_CST))
3967 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)))
3968 gnu_retval = create_temporary ("R", gnu_result_type);
3970 /* Create the list of the actual parameters as GCC expects it, namely a
3971 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
3972 is an expression and the TREE_PURPOSE field is null. But skip Out
3973 parameters not passed by reference and that need not be copied in. */
3974 for (gnat_actual = First_Actual (gnat_node);
3975 Present (gnat_actual);
3976 gnat_formal = Next_Formal_With_Extras (gnat_formal),
3977 gnat_actual = Next_Actual (gnat_actual))
3979 tree gnu_formal = present_gnu_tree (gnat_formal)
3980 ? get_gnu_tree (gnat_formal) : NULL_TREE;
3981 tree gnu_formal_type = gnat_to_gnu_type (Etype (gnat_formal));
3982 const bool is_true_formal_parm
3983 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
3984 const bool is_by_ref_formal_parm
3985 = is_true_formal_parm
3986 && (DECL_BY_REF_P (gnu_formal)
3987 || DECL_BY_COMPONENT_PTR_P (gnu_formal)
3988 || DECL_BY_DESCRIPTOR_P (gnu_formal));
3989 /* In the Out or In Out case, we must suppress conversions that yield
3990 an lvalue but can nevertheless cause the creation of a temporary,
3991 because we need the real object in this case, either to pass its
3992 address if it's passed by reference or as target of the back copy
3993 done after the call if it uses the copy-in/copy-out mechanism.
3994 We do it in the In case too, except for an unchecked conversion
3995 because it alone can cause the actual to be misaligned and the
3996 addressability test is applied to the real object. */
3997 const bool suppress_type_conversion
3998 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
3999 && Ekind (gnat_formal) != E_In_Parameter)
4000 || (Nkind (gnat_actual) == N_Type_Conversion
4001 && Is_Composite_Type (Underlying_Type (Etype (gnat_formal)))));
4002 Node_Id gnat_name = suppress_type_conversion
4003 ? Expression (gnat_actual) : gnat_actual;
4004 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
4005 tree gnu_actual;
4007 /* If it's possible we may need to use this expression twice, make sure
4008 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4009 to force side-effects before the call.
4010 ??? This is more conservative than we need since we don't need to do
4011 this for pass-by-ref with no conversion. */
4012 if (Ekind (gnat_formal) != E_In_Parameter)
4013 gnu_name = gnat_stabilize_reference (gnu_name, true, NULL);
4015 /* If we are passing a non-addressable parameter by reference, pass the
4016 address of a copy. In the Out or In Out case, set up to copy back
4017 out after the call. */
4018 if (is_by_ref_formal_parm
4019 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
4020 && !addressable_p (gnu_name, gnu_name_type))
4022 bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
4023 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4025 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4026 but sort of an instantiation for them. */
4027 if (TREE_CODE (gnu_name) == CONSTRUCTOR)
4030 /* If the type is passed by reference, a copy is not allowed. */
4031 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type))
4032 post_error ("misaligned actual cannot be passed by reference",
4033 gnat_actual);
4035 /* For users of Starlet we issue a warning because the interface
4036 apparently assumes that by-ref parameters outlive the procedure
4037 invocation. The code still will not work as intended, but we
4038 cannot do much better since low-level parts of the back-end
4039 would allocate temporaries at will because of the misalignment
4040 if we did not do so here. */
4041 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
4043 post_error
4044 ("?possible violation of implicit assumption", gnat_actual);
4045 post_error_ne
4046 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
4047 Entity (Name (gnat_node)));
4048 post_error_ne ("?because of misalignment of &", gnat_actual,
4049 gnat_formal);
4052 /* If the actual type of the object is already the nominal type,
4053 we have nothing to do, except if the size is self-referential
4054 in which case we'll remove the unpadding below. */
4055 if (TREE_TYPE (gnu_name) == gnu_name_type
4056 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4059 /* Otherwise remove the unpadding from all the objects. */
4060 else if (TREE_CODE (gnu_name) == COMPONENT_REF
4061 && TYPE_IS_PADDING_P
4062 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
4063 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
4065 /* Otherwise convert to the nominal type of the object if needed.
4066 There are several cases in which we need to make the temporary
4067 using this type instead of the actual type of the object when
4068 they are distinct, because the expectations of the callee would
4069 otherwise not be met:
4070 - if it's a justified modular type,
4071 - if the actual type is a smaller form of it,
4072 - if it's a smaller form of the actual type. */
4073 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4074 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4075 || smaller_form_type_p (TREE_TYPE (gnu_name),
4076 gnu_name_type)))
4077 || (INTEGRAL_TYPE_P (gnu_name_type)
4078 && smaller_form_type_p (gnu_name_type,
4079 TREE_TYPE (gnu_name))))
4080 gnu_name = convert (gnu_name_type, gnu_name);
4082 /* If this is an In Out or Out parameter and we're returning a value,
4083 we need to create a temporary for the return value because we must
4084 preserve it before copying back at the very end. */
4085 if (!in_param && returning_value && !gnu_retval)
4086 gnu_retval = create_temporary ("R", gnu_result_type);
4088 /* If we haven't pushed a binding level, push a new one. This will
4089 narrow the lifetime of the temporary we are about to make as much
4090 as possible. The drawback is that we'd need to create a temporary
4091 for the return value, if any (see comment before the loop). So do
4092 it only when this temporary was already created just above. */
4093 if (!pushed_binding_level && !(in_param && returning_value))
4095 start_stmt_group ();
4096 gnat_pushlevel ();
4097 pushed_binding_level = true;
4100 /* Create an explicit temporary holding the copy. */
4101 gnu_temp
4102 = create_init_temporary ("A", gnu_name, &gnu_stmt, gnat_actual);
4104 /* But initialize it on the fly like for an implicit temporary as
4105 we aren't necessarily having a statement list. */
4106 gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt,
4107 gnu_temp);
4109 /* Set up to move the copy back to the original if needed. */
4110 if (!in_param)
4112 /* If the original is a COND_EXPR whose first arm isn't meant to
4113 be further used, just deal with the second arm. This is very
4114 likely the conditional expression built for a check. */
4115 if (TREE_CODE (gnu_orig) == COND_EXPR
4116 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4117 && integer_zerop
4118 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4119 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4121 gnu_stmt
4122 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp);
4123 set_expr_location_from_node (gnu_stmt, gnat_node);
4125 append_to_statement_list (gnu_stmt, &gnu_after_list);
4129 /* Start from the real object and build the actual. */
4130 gnu_actual = gnu_name;
4132 /* If this is an atomic access of an In or In Out parameter for which
4133 synchronization is required, build the atomic load. */
4134 if (is_true_formal_parm
4135 && !is_by_ref_formal_parm
4136 && Ekind (gnat_formal) != E_Out_Parameter
4137 && atomic_sync_required_p (gnat_actual))
4138 gnu_actual = build_atomic_load (gnu_actual);
4140 /* If this was a procedure call, we may not have removed any padding.
4141 So do it here for the part we will use as an input, if any. */
4142 if (Ekind (gnat_formal) != E_Out_Parameter
4143 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4144 gnu_actual
4145 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4147 /* Put back the conversion we suppressed above in the computation of the
4148 real object. And even if we didn't suppress any conversion there, we
4149 may have suppressed a conversion to the Etype of the actual earlier,
4150 since the parent is a procedure call, so put it back here. */
4151 if (suppress_type_conversion
4152 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4153 gnu_actual
4154 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
4155 gnu_actual, No_Truncation (gnat_actual));
4156 else
4157 gnu_actual
4158 = convert (gnat_to_gnu_type (Etype (gnat_actual)), gnu_actual);
4160 /* Make sure that the actual is in range of the formal's type. */
4161 if (Ekind (gnat_formal) != E_Out_Parameter
4162 && Do_Range_Check (gnat_actual))
4163 gnu_actual
4164 = emit_range_check (gnu_actual, Etype (gnat_formal), gnat_actual);
4166 /* Unless this is an In parameter, we must remove any justified modular
4167 building from GNU_NAME to get an lvalue. */
4168 if (Ekind (gnat_formal) != E_In_Parameter
4169 && TREE_CODE (gnu_name) == CONSTRUCTOR
4170 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
4171 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
4172 gnu_name
4173 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))), gnu_name);
4175 /* First see if the parameter is passed by reference. */
4176 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
4178 if (Ekind (gnat_formal) != E_In_Parameter)
4180 /* In Out or Out parameters passed by reference don't use the
4181 copy-in/copy-out mechanism so the address of the real object
4182 must be passed to the function. */
4183 gnu_actual = gnu_name;
4185 /* If we have a padded type, be sure we've removed padding. */
4186 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4187 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
4188 gnu_actual);
4190 /* If we have the constructed subtype of an aliased object
4191 with an unconstrained nominal subtype, the type of the
4192 actual includes the template, although it is formally
4193 constrained. So we need to convert it back to the real
4194 constructed subtype to retrieve the constrained part
4195 and takes its address. */
4196 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4197 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
4198 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
4199 && (Is_Array_Type (Etype (gnat_actual))
4200 || (Is_Private_Type (Etype (gnat_actual))
4201 && Is_Array_Type (Full_View (Etype (gnat_actual))))))
4202 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4203 gnu_actual);
4206 /* There is no need to convert the actual to the formal's type before
4207 taking its address. The only exception is for unconstrained array
4208 types because of the way we build fat pointers. */
4209 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4211 /* Put back a view conversion for In Out or Out parameters. */
4212 if (Ekind (gnat_formal) != E_In_Parameter)
4213 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4214 gnu_actual);
4215 gnu_actual = convert (gnu_formal_type, gnu_actual);
4218 /* The symmetry of the paths to the type of an entity is broken here
4219 since arguments don't know that they will be passed by ref. */
4220 gnu_formal_type = TREE_TYPE (gnu_formal);
4221 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4224 /* Then see if the parameter is an array passed to a foreign convention
4225 subprogram. */
4226 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
4228 gnu_formal_type = TREE_TYPE (gnu_formal);
4229 gnu_actual = maybe_implicit_deref (gnu_actual);
4230 gnu_actual = maybe_unconstrained_array (gnu_actual);
4232 if (TYPE_IS_PADDING_P (gnu_formal_type))
4234 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
4235 gnu_actual = convert (gnu_formal_type, gnu_actual);
4238 /* Take the address of the object and convert to the proper pointer
4239 type. We'd like to actually compute the address of the beginning
4240 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
4241 possibility that the ARRAY_REF might return a constant and we'd be
4242 getting the wrong address. Neither approach is exactly correct,
4243 but this is the most likely to work in all cases. */
4244 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4247 /* Then see if the parameter is passed by descriptor. */
4248 else if (is_true_formal_parm && DECL_BY_DESCRIPTOR_P (gnu_formal))
4250 gnu_actual = convert (gnu_formal_type, gnu_actual);
4252 /* If this is 'Null_Parameter, pass a zero descriptor. */
4253 if ((TREE_CODE (gnu_actual) == INDIRECT_REF
4254 || TREE_CODE (gnu_actual) == UNCONSTRAINED_ARRAY_REF)
4255 && TREE_PRIVATE (gnu_actual))
4256 gnu_actual
4257 = convert (DECL_ARG_TYPE (gnu_formal), integer_zero_node);
4258 else
4259 gnu_actual = build_unary_op (ADDR_EXPR, NULL_TREE,
4260 fill_vms_descriptor
4261 (TREE_TYPE (TREE_TYPE (gnu_formal)),
4262 gnu_actual, gnat_actual));
4265 /* Otherwise the parameter is passed by copy. */
4266 else
4268 tree gnu_size;
4270 if (Ekind (gnat_formal) != E_In_Parameter)
4271 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
4273 /* If we didn't create a PARM_DECL for the formal, this means that
4274 it is an Out parameter not passed by reference and that need not
4275 be copied in. In this case, the value of the actual need not be
4276 read. However, we still need to make sure that its side-effects
4277 are evaluated before the call, so we evaluate its address. */
4278 if (!is_true_formal_parm)
4280 if (TREE_SIDE_EFFECTS (gnu_name))
4282 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
4283 append_to_statement_list (addr, &gnu_stmt_list);
4285 continue;
4288 gnu_actual = convert (gnu_formal_type, gnu_actual);
4290 /* If this is 'Null_Parameter, pass a zero even though we are
4291 dereferencing it. */
4292 if (TREE_CODE (gnu_actual) == INDIRECT_REF
4293 && TREE_PRIVATE (gnu_actual)
4294 && (gnu_size = TYPE_SIZE (TREE_TYPE (gnu_actual)))
4295 && TREE_CODE (gnu_size) == INTEGER_CST
4296 && compare_tree_int (gnu_size, BITS_PER_WORD) <= 0)
4297 gnu_actual
4298 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
4299 convert (gnat_type_for_size
4300 (TREE_INT_CST_LOW (gnu_size), 1),
4301 integer_zero_node),
4302 false);
4303 else
4304 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
4307 vec_safe_push (gnu_actual_vec, gnu_actual);
4310 gnu_call
4311 = build_call_vec (gnu_result_type, gnu_subprog_addr, gnu_actual_vec);
4312 set_expr_location_from_node (gnu_call, gnat_node);
4314 /* If we have created a temporary for the return value, initialize it. */
4315 if (gnu_retval)
4317 tree gnu_stmt
4318 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
4319 set_expr_location_from_node (gnu_stmt, gnat_node);
4320 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4321 gnu_call = gnu_retval;
4324 /* If this is a subprogram with copy-in/copy-out parameters, we need to
4325 unpack the valued returned from the function into the In Out or Out
4326 parameters. We deal with the function return (if this is an Ada
4327 function) below. */
4328 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4330 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
4331 copy-out parameters. */
4332 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
4333 const int length = list_length (gnu_cico_list);
4335 /* The call sequence must contain one and only one call, even though the
4336 function is pure. Save the result into a temporary if needed. */
4337 if (length > 1)
4339 if (!gnu_retval)
4341 tree gnu_stmt;
4342 /* If we haven't pushed a binding level, push a new one. This
4343 will narrow the lifetime of the temporary we are about to
4344 make as much as possible. */
4345 if (!pushed_binding_level)
4347 start_stmt_group ();
4348 gnat_pushlevel ();
4349 pushed_binding_level = true;
4351 gnu_call
4352 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
4353 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4356 gnu_name_list = nreverse (gnu_name_list);
4359 /* The first entry is for the actual return value if this is a
4360 function, so skip it. */
4361 if (function_call)
4362 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4364 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4365 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4366 else
4367 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4369 for (gnat_actual = First_Actual (gnat_node);
4370 Present (gnat_actual);
4371 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4372 gnat_actual = Next_Actual (gnat_actual))
4373 /* If we are dealing with a copy-in/copy-out parameter, we must
4374 retrieve its value from the record returned in the call. */
4375 if (!(present_gnu_tree (gnat_formal)
4376 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4377 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
4378 || (TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4379 && ((DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))
4380 || (DECL_BY_DESCRIPTOR_P
4381 (get_gnu_tree (gnat_formal))))))))
4382 && Ekind (gnat_formal) != E_In_Parameter)
4384 /* Get the value to assign to this Out or In Out parameter. It is
4385 either the result of the function if there is only a single such
4386 parameter or the appropriate field from the record returned. */
4387 tree gnu_result
4388 = length == 1
4389 ? gnu_call
4390 : build_component_ref (gnu_call, NULL_TREE,
4391 TREE_PURPOSE (gnu_cico_list), false);
4393 /* If the actual is a conversion, get the inner expression, which
4394 will be the real destination, and convert the result to the
4395 type of the actual parameter. */
4396 tree gnu_actual
4397 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
4399 /* If the result is a padded type, remove the padding. */
4400 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4401 gnu_result
4402 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4403 gnu_result);
4405 /* If the actual is a type conversion, the real target object is
4406 denoted by the inner Expression and we need to convert the
4407 result to the associated type.
4408 We also need to convert our gnu assignment target to this type
4409 if the corresponding GNU_NAME was constructed from the GNAT
4410 conversion node and not from the inner Expression. */
4411 if (Nkind (gnat_actual) == N_Type_Conversion)
4413 gnu_result
4414 = convert_with_check
4415 (Etype (Expression (gnat_actual)), gnu_result,
4416 Do_Overflow_Check (gnat_actual),
4417 Do_Range_Check (Expression (gnat_actual)),
4418 Float_Truncate (gnat_actual), gnat_actual);
4420 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
4421 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
4424 /* Unchecked conversions as actuals for Out parameters are not
4425 allowed in user code because they are not variables, but do
4426 occur in front-end expansions. The associated GNU_NAME is
4427 always obtained from the inner expression in such cases. */
4428 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4429 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
4430 gnu_result,
4431 No_Truncation (gnat_actual));
4432 else
4434 if (Do_Range_Check (gnat_actual))
4435 gnu_result
4436 = emit_range_check (gnu_result, Etype (gnat_actual),
4437 gnat_actual);
4439 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
4440 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
4441 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
4444 if (atomic_sync_required_p (gnat_actual))
4445 gnu_result = build_atomic_store (gnu_actual, gnu_result);
4446 else
4447 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4448 gnu_actual, gnu_result);
4449 set_expr_location_from_node (gnu_result, gnat_node);
4450 append_to_statement_list (gnu_result, &gnu_stmt_list);
4451 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4452 gnu_name_list = TREE_CHAIN (gnu_name_list);
4456 /* If this is a function call, the result is the call expression unless a
4457 target is specified, in which case we copy the result into the target
4458 and return the assignment statement. */
4459 if (function_call)
4461 /* If this is a function with copy-in/copy-out parameters, extract the
4462 return value from it and update the return type. */
4463 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4465 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
4466 gnu_call = build_component_ref (gnu_call, NULL_TREE,
4467 TREE_PURPOSE (gnu_elmt), false);
4468 gnu_result_type = TREE_TYPE (gnu_call);
4471 /* If the function returns an unconstrained array or by direct reference,
4472 we have to dereference the pointer. */
4473 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type)
4474 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
4475 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
4477 if (gnu_target)
4479 Node_Id gnat_parent = Parent (gnat_node);
4480 enum tree_code op_code;
4482 /* If range check is needed, emit code to generate it. */
4483 if (Do_Range_Check (gnat_node))
4484 gnu_call
4485 = emit_range_check (gnu_call, Etype (Name (gnat_parent)),
4486 gnat_parent);
4488 /* ??? If the return type has variable size, then force the return
4489 slot optimization as we would not be able to create a temporary.
4490 Likewise if it was unconstrained as we would copy too much data.
4491 That's what has been done historically. */
4492 if (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
4493 || (TYPE_IS_PADDING_P (gnu_result_type)
4494 && CONTAINS_PLACEHOLDER_P
4495 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_result_type))))))
4496 op_code = INIT_EXPR;
4497 else
4498 op_code = MODIFY_EXPR;
4500 if (atomic_sync)
4501 gnu_call = build_atomic_store (gnu_target, gnu_call);
4502 else
4503 gnu_call
4504 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
4505 set_expr_location_from_node (gnu_call, gnat_parent);
4506 append_to_statement_list (gnu_call, &gnu_stmt_list);
4508 else
4509 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
4512 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4513 parameters, the result is just the call statement. */
4514 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
4515 append_to_statement_list (gnu_call, &gnu_stmt_list);
4517 /* Finally, add the copy back statements, if any. */
4518 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
4520 if (went_into_elab_proc)
4521 current_function_decl = NULL_TREE;
4523 /* If we have pushed a binding level, pop it and finish up the enclosing
4524 statement group. */
4525 if (pushed_binding_level)
4527 add_stmt (gnu_stmt_list);
4528 gnat_poplevel ();
4529 gnu_result = end_stmt_group ();
4532 /* Otherwise, retrieve the statement list, if any. */
4533 else if (gnu_stmt_list)
4534 gnu_result = gnu_stmt_list;
4536 /* Otherwise, just return the call expression. */
4537 else
4538 return gnu_call;
4540 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4541 But first simplify if we have only one statement in the list. */
4542 if (returning_value)
4544 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
4545 if (first == last)
4546 gnu_result = first;
4547 gnu_result
4548 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
4551 return gnu_result;
4554 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4555 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4557 static tree
4558 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
4560 tree gnu_jmpsave_decl = NULL_TREE;
4561 tree gnu_jmpbuf_decl = NULL_TREE;
4562 /* If just annotating, ignore all EH and cleanups. */
4563 bool gcc_zcx = (!type_annotate_only
4564 && Present (Exception_Handlers (gnat_node))
4565 && Exception_Mechanism == Back_End_Exceptions);
4566 bool setjmp_longjmp
4567 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
4568 && Exception_Mechanism == Setjmp_Longjmp);
4569 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
4570 bool binding_for_block = (at_end || gcc_zcx || setjmp_longjmp);
4571 tree gnu_inner_block; /* The statement(s) for the block itself. */
4572 tree gnu_result;
4573 tree gnu_expr;
4574 Node_Id gnat_temp;
4575 /* Node providing the sloc for the cleanup actions. */
4576 Node_Id gnat_cleanup_loc_node = (Present (End_Label (gnat_node)) ?
4577 End_Label (gnat_node) :
4578 gnat_node);
4580 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
4581 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
4582 add_cleanup, and when we leave the binding, end_stmt_group will create
4583 the TRY_FINALLY_EXPR.
4585 ??? The region level calls down there have been specifically put in place
4586 for a ZCX context and currently the order in which things are emitted
4587 (region/handlers) is different from the SJLJ case. Instead of putting
4588 other calls with different conditions at other places for the SJLJ case,
4589 it seems cleaner to reorder things for the SJLJ case and generalize the
4590 condition to make it not ZCX specific.
4592 If there are any exceptions or cleanup processing involved, we need an
4593 outer statement group (for Setjmp_Longjmp) and binding level. */
4594 if (binding_for_block)
4596 start_stmt_group ();
4597 gnat_pushlevel ();
4600 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
4601 area for address of previous buffer. Do this first since we need to have
4602 the setjmp buf known for any decls in this block. */
4603 if (setjmp_longjmp)
4605 gnu_jmpsave_decl
4606 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
4607 jmpbuf_ptr_type,
4608 build_call_n_expr (get_jmpbuf_decl, 0),
4609 false, false, false, false, NULL, gnat_node);
4610 DECL_ARTIFICIAL (gnu_jmpsave_decl) = 1;
4612 /* The __builtin_setjmp receivers will immediately reinstall it. Now
4613 because of the unstructured form of EH used by setjmp_longjmp, there
4614 might be forward edges going to __builtin_setjmp receivers on which
4615 it is uninitialized, although they will never be actually taken. */
4616 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
4617 gnu_jmpbuf_decl
4618 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE,
4619 jmpbuf_type,
4620 NULL_TREE,
4621 false, false, false, false, NULL, gnat_node);
4622 DECL_ARTIFICIAL (gnu_jmpbuf_decl) = 1;
4624 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
4626 /* When we exit this block, restore the saved value. */
4627 add_cleanup (build_call_n_expr (set_jmpbuf_decl, 1, gnu_jmpsave_decl),
4628 gnat_cleanup_loc_node);
4631 /* If we are to call a function when exiting this block, add a cleanup
4632 to the binding level we made above. Note that add_cleanup is FIFO
4633 so we must register this cleanup after the EH cleanup just above. */
4634 if (at_end)
4635 add_cleanup (build_call_n_expr (gnat_to_gnu (At_End_Proc (gnat_node)), 0),
4636 gnat_cleanup_loc_node);
4638 /* Now build the tree for the declarations and statements inside this block.
4639 If this is SJLJ, set our jmp_buf as the current buffer. */
4640 start_stmt_group ();
4642 if (setjmp_longjmp)
4643 add_stmt (build_call_n_expr (set_jmpbuf_decl, 1,
4644 build_unary_op (ADDR_EXPR, NULL_TREE,
4645 gnu_jmpbuf_decl)));
4647 if (Present (First_Real_Statement (gnat_node)))
4648 process_decls (Statements (gnat_node), Empty,
4649 First_Real_Statement (gnat_node), true, true);
4651 /* Generate code for each statement in the block. */
4652 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
4653 ? First_Real_Statement (gnat_node)
4654 : First (Statements (gnat_node)));
4655 Present (gnat_temp); gnat_temp = Next (gnat_temp))
4656 add_stmt (gnat_to_gnu (gnat_temp));
4657 gnu_inner_block = end_stmt_group ();
4659 /* Now generate code for the two exception models, if either is relevant for
4660 this block. */
4661 if (setjmp_longjmp)
4663 tree *gnu_else_ptr = 0;
4664 tree gnu_handler;
4666 /* Make a binding level for the exception handling declarations and code
4667 and set up gnu_except_ptr_stack for the handlers to use. */
4668 start_stmt_group ();
4669 gnat_pushlevel ();
4671 vec_safe_push (gnu_except_ptr_stack,
4672 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE,
4673 build_pointer_type (except_type_node),
4674 build_call_n_expr (get_excptr_decl, 0),
4675 false, false, false, false,
4676 NULL, gnat_node));
4678 /* Generate code for each handler. The N_Exception_Handler case does the
4679 real work and returns a COND_EXPR for each handler, which we chain
4680 together here. */
4681 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4682 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
4684 gnu_expr = gnat_to_gnu (gnat_temp);
4686 /* If this is the first one, set it as the outer one. Otherwise,
4687 point the "else" part of the previous handler to us. Then point
4688 to our "else" part. */
4689 if (!gnu_else_ptr)
4690 add_stmt (gnu_expr);
4691 else
4692 *gnu_else_ptr = gnu_expr;
4694 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
4697 /* If none of the exception handlers did anything, re-raise but do not
4698 defer abortion. */
4699 gnu_expr = build_call_n_expr (raise_nodefer_decl, 1,
4700 gnu_except_ptr_stack->last ());
4701 set_expr_location_from_node
4702 (gnu_expr,
4703 Present (End_Label (gnat_node)) ? End_Label (gnat_node) : gnat_node);
4705 if (gnu_else_ptr)
4706 *gnu_else_ptr = gnu_expr;
4707 else
4708 add_stmt (gnu_expr);
4710 /* End the binding level dedicated to the exception handlers and get the
4711 whole statement group. */
4712 gnu_except_ptr_stack->pop ();
4713 gnat_poplevel ();
4714 gnu_handler = end_stmt_group ();
4716 /* If the setjmp returns 1, we restore our incoming longjmp value and
4717 then check the handlers. */
4718 start_stmt_group ();
4719 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl, 1,
4720 gnu_jmpsave_decl),
4721 gnat_node);
4722 add_stmt (gnu_handler);
4723 gnu_handler = end_stmt_group ();
4725 /* This block is now "if (setjmp) ... <handlers> else <block>". */
4726 gnu_result = build3 (COND_EXPR, void_type_node,
4727 (build_call_n_expr
4728 (setjmp_decl, 1,
4729 build_unary_op (ADDR_EXPR, NULL_TREE,
4730 gnu_jmpbuf_decl))),
4731 gnu_handler, gnu_inner_block);
4733 else if (gcc_zcx)
4735 tree gnu_handlers;
4736 location_t locus;
4738 /* First make a block containing the handlers. */
4739 start_stmt_group ();
4740 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4741 Present (gnat_temp);
4742 gnat_temp = Next_Non_Pragma (gnat_temp))
4743 add_stmt (gnat_to_gnu (gnat_temp));
4744 gnu_handlers = end_stmt_group ();
4746 /* Now make the TRY_CATCH_EXPR for the block. */
4747 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
4748 gnu_inner_block, gnu_handlers);
4749 /* Set a location. We need to find a unique location for the dispatching
4750 code, otherwise we can get coverage or debugging issues. Try with
4751 the location of the end label. */
4752 if (Present (End_Label (gnat_node))
4753 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
4754 SET_EXPR_LOCATION (gnu_result, locus);
4755 else
4756 /* Clear column information so that the exception handler of an
4757 implicit transient block does not incorrectly inherit the slocs
4758 of a decision, which would otherwise confuse control flow based
4759 coverage analysis tools. */
4760 set_expr_location_from_node1 (gnu_result, gnat_node, true);
4762 else
4763 gnu_result = gnu_inner_block;
4765 /* Now close our outer block, if we had to make one. */
4766 if (binding_for_block)
4768 add_stmt (gnu_result);
4769 gnat_poplevel ();
4770 gnu_result = end_stmt_group ();
4773 return gnu_result;
4776 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4777 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
4778 exception handling. */
4780 static tree
4781 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
4783 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
4784 an "if" statement to select the proper exceptions. For "Others", exclude
4785 exceptions where Handled_By_Others is nonzero unless the All_Others flag
4786 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
4787 tree gnu_choice = boolean_false_node;
4788 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
4789 Node_Id gnat_temp;
4791 for (gnat_temp = First (Exception_Choices (gnat_node));
4792 gnat_temp; gnat_temp = Next (gnat_temp))
4794 tree this_choice;
4796 if (Nkind (gnat_temp) == N_Others_Choice)
4798 if (All_Others (gnat_temp))
4799 this_choice = boolean_true_node;
4800 else
4801 this_choice
4802 = build_binary_op
4803 (EQ_EXPR, boolean_type_node,
4804 convert
4805 (integer_type_node,
4806 build_component_ref
4807 (build_unary_op
4808 (INDIRECT_REF, NULL_TREE,
4809 gnu_except_ptr_stack->last ()),
4810 get_identifier ("not_handled_by_others"), NULL_TREE,
4811 false)),
4812 integer_zero_node);
4815 else if (Nkind (gnat_temp) == N_Identifier
4816 || Nkind (gnat_temp) == N_Expanded_Name)
4818 Entity_Id gnat_ex_id = Entity (gnat_temp);
4819 tree gnu_expr;
4821 /* Exception may be a renaming. Recover original exception which is
4822 the one elaborated and registered. */
4823 if (Present (Renamed_Object (gnat_ex_id)))
4824 gnat_ex_id = Renamed_Object (gnat_ex_id);
4826 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4828 this_choice
4829 = build_binary_op
4830 (EQ_EXPR, boolean_type_node,
4831 gnu_except_ptr_stack->last (),
4832 convert (TREE_TYPE (gnu_except_ptr_stack->last ()),
4833 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
4835 /* If this is the distinguished exception "Non_Ada_Error" (and we are
4836 in VMS mode), also allow a non-Ada exception (a VMS condition) t
4837 match. */
4838 if (Is_Non_Ada_Error (Entity (gnat_temp)))
4840 tree gnu_comp
4841 = build_component_ref
4842 (build_unary_op (INDIRECT_REF, NULL_TREE,
4843 gnu_except_ptr_stack->last ()),
4844 get_identifier ("lang"), NULL_TREE, false);
4846 this_choice
4847 = build_binary_op
4848 (TRUTH_ORIF_EXPR, boolean_type_node,
4849 build_binary_op (EQ_EXPR, boolean_type_node, gnu_comp,
4850 build_int_cst (TREE_TYPE (gnu_comp), 'V')),
4851 this_choice);
4854 else
4855 gcc_unreachable ();
4857 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
4858 gnu_choice, this_choice);
4861 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
4864 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4865 to a GCC tree, which is returned. This is the variant for ZCX. */
4867 static tree
4868 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
4870 tree gnu_etypes_list = NULL_TREE;
4871 tree gnu_current_exc_ptr, prev_gnu_incoming_exc_ptr;
4872 Node_Id gnat_temp;
4874 /* We build a TREE_LIST of nodes representing what exception types this
4875 handler can catch, with special cases for others and all others cases.
4877 Each exception type is actually identified by a pointer to the exception
4878 id, or to a dummy object for "others" and "all others". */
4879 for (gnat_temp = First (Exception_Choices (gnat_node));
4880 gnat_temp; gnat_temp = Next (gnat_temp))
4882 tree gnu_expr, gnu_etype;
4884 if (Nkind (gnat_temp) == N_Others_Choice)
4886 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
4887 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4889 else if (Nkind (gnat_temp) == N_Identifier
4890 || Nkind (gnat_temp) == N_Expanded_Name)
4892 Entity_Id gnat_ex_id = Entity (gnat_temp);
4894 /* Exception may be a renaming. Recover original exception which is
4895 the one elaborated and registered. */
4896 if (Present (Renamed_Object (gnat_ex_id)))
4897 gnat_ex_id = Renamed_Object (gnat_ex_id);
4899 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4900 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4902 /* The Non_Ada_Error case for VMS exceptions is handled
4903 by the personality routine. */
4905 else
4906 gcc_unreachable ();
4908 /* The GCC interface expects NULL to be passed for catch all handlers, so
4909 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
4910 is integer_zero_node. It would not work, however, because GCC's
4911 notion of "catch all" is stronger than our notion of "others". Until
4912 we correctly use the cleanup interface as well, doing that would
4913 prevent the "all others" handlers from being seen, because nothing
4914 can be caught beyond a catch all from GCC's point of view. */
4915 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
4918 start_stmt_group ();
4919 gnat_pushlevel ();
4921 /* Expand a call to the begin_handler hook at the beginning of the handler,
4922 and arrange for a call to the end_handler hook to occur on every possible
4923 exit path.
4925 The hooks expect a pointer to the low level occurrence. This is required
4926 for our stack management scheme because a raise inside the handler pushes
4927 a new occurrence on top of the stack, which means that this top does not
4928 necessarily match the occurrence this handler was dealing with.
4930 __builtin_eh_pointer references the exception occurrence being
4931 propagated. Upon handler entry, this is the exception for which the
4932 handler is triggered. This might not be the case upon handler exit,
4933 however, as we might have a new occurrence propagated by the handler's
4934 body, and the end_handler hook called as a cleanup in this context.
4936 We use a local variable to retrieve the incoming value at handler entry
4937 time, and reuse it to feed the end_handler hook's argument at exit. */
4939 gnu_current_exc_ptr
4940 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
4941 1, integer_zero_node);
4942 prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
4943 gnu_incoming_exc_ptr = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
4944 ptr_type_node, gnu_current_exc_ptr,
4945 false, false, false, false,
4946 NULL, gnat_node);
4948 add_stmt_with_node (build_call_n_expr (begin_handler_decl, 1,
4949 gnu_incoming_exc_ptr),
4950 gnat_node);
4952 /* Declare and initialize the choice parameter, if present. */
4953 if (Present (Choice_Parameter (gnat_node)))
4955 tree gnu_param
4956 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, 1);
4958 add_stmt (build_call_n_expr
4959 (set_exception_parameter_decl, 2,
4960 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
4961 gnu_incoming_exc_ptr));
4964 /* We don't have an End_Label at hand to set the location of the cleanup
4965 actions, so we use that of the exception handler itself instead. */
4966 add_cleanup (build_call_n_expr (end_handler_decl, 1, gnu_incoming_exc_ptr),
4967 gnat_node);
4968 add_stmt_list (Statements (gnat_node));
4969 gnat_poplevel ();
4971 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
4973 return
4974 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
4977 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
4979 static void
4980 Compilation_Unit_to_gnu (Node_Id gnat_node)
4982 const Node_Id gnat_unit = Unit (gnat_node);
4983 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
4984 || Nkind (gnat_unit) == N_Subprogram_Body);
4985 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
4986 Node_Id gnat_pragma;
4987 /* Make the decl for the elaboration procedure. */
4988 tree gnu_elab_proc_decl
4989 = create_subprog_decl
4990 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
4991 NULL_TREE, void_ftype, NULL_TREE, is_disabled, true, false, true, NULL,
4992 gnat_unit);
4993 struct elab_info *info;
4995 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
4996 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
4998 /* Initialize the information structure for the function. */
4999 allocate_struct_function (gnu_elab_proc_decl, false);
5000 set_cfun (NULL);
5002 current_function_decl = NULL_TREE;
5004 start_stmt_group ();
5005 gnat_pushlevel ();
5007 /* For a body, first process the spec if there is one. */
5008 if (Nkind (gnat_unit) == N_Package_Body
5009 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
5010 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
5012 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
5014 elaborate_all_entities (gnat_node);
5016 if (Nkind (gnat_unit) == N_Subprogram_Declaration
5017 || Nkind (gnat_unit) == N_Generic_Package_Declaration
5018 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
5019 return;
5022 /* Then process any pragmas and declarations preceding the unit. */
5023 for (gnat_pragma = First (Context_Items (gnat_node));
5024 Present (gnat_pragma);
5025 gnat_pragma = Next (gnat_pragma))
5026 if (Nkind (gnat_pragma) == N_Pragma)
5027 add_stmt (gnat_to_gnu (gnat_pragma));
5028 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
5029 true, true);
5031 /* Process the unit itself. */
5032 add_stmt (gnat_to_gnu (gnat_unit));
5034 /* If we can inline, generate code for all the inlined subprograms. */
5035 if (optimize)
5037 Entity_Id gnat_entity;
5039 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5040 Present (gnat_entity);
5041 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
5043 Node_Id gnat_body = Parent (Declaration_Node (gnat_entity));
5045 if (Nkind (gnat_body) != N_Subprogram_Body)
5047 /* ??? This really should always be present. */
5048 if (No (Corresponding_Body (gnat_body)))
5049 continue;
5050 gnat_body
5051 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
5054 if (Present (gnat_body))
5056 /* Define the entity first so we set DECL_EXTERNAL. */
5057 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5058 add_stmt (gnat_to_gnu (gnat_body));
5063 /* Process any pragmas and actions following the unit. */
5064 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5065 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
5066 finalize_from_limited_with ();
5068 /* Save away what we've made so far and record this potential elaboration
5069 procedure. */
5070 info = ggc_alloc_elab_info ();
5071 set_current_block_context (gnu_elab_proc_decl);
5072 gnat_poplevel ();
5073 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
5075 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
5077 info->next = elab_info_list;
5078 info->elab_proc = gnu_elab_proc_decl;
5079 info->gnat_node = gnat_node;
5080 elab_info_list = info;
5082 /* Generate elaboration code for this unit, if necessary, and say whether
5083 we did or not. */
5084 gnu_elab_proc_stack->pop ();
5086 /* Invalidate the global renaming pointers. This is necessary because
5087 stabilization of the renamed entities may create SAVE_EXPRs which
5088 have been tied to a specific elaboration routine just above. */
5089 invalidate_global_renaming_pointers ();
5092 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Raise_xxx_Error,
5093 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where
5094 we should place the result type. LABEL_P is true if there is a label to
5095 branch to for the exception. */
5097 static tree
5098 Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5100 const Node_Kind kind = Nkind (gnat_node);
5101 const int reason = UI_To_Int (Reason (gnat_node));
5102 const Node_Id gnat_cond = Condition (gnat_node);
5103 const bool with_extra_info
5104 = Exception_Extra_Info
5105 && !No_Exception_Handlers_Set ()
5106 && !get_exception_label (kind);
5107 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
5109 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5111 switch (reason)
5113 case CE_Access_Check_Failed:
5114 if (with_extra_info)
5115 gnu_result = build_call_raise_column (reason, gnat_node);
5116 break;
5118 case CE_Index_Check_Failed:
5119 case CE_Range_Check_Failed:
5120 case CE_Invalid_Data:
5121 if (Present (gnat_cond) && Nkind (gnat_cond) == N_Op_Not)
5123 Node_Id gnat_range, gnat_index, gnat_type;
5124 tree gnu_index, gnu_low_bound, gnu_high_bound;
5125 struct range_check_info_d *rci;
5127 switch (Nkind (Right_Opnd (gnat_cond)))
5129 case N_In:
5130 gnat_range = Right_Opnd (Right_Opnd (gnat_cond));
5131 gcc_assert (Nkind (gnat_range) == N_Range);
5132 gnu_low_bound = gnat_to_gnu (Low_Bound (gnat_range));
5133 gnu_high_bound = gnat_to_gnu (High_Bound (gnat_range));
5134 break;
5136 case N_Op_Ge:
5137 gnu_low_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5138 gnu_high_bound = NULL_TREE;
5139 break;
5141 case N_Op_Le:
5142 gnu_low_bound = NULL_TREE;
5143 gnu_high_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5144 break;
5146 default:
5147 goto common;
5150 gnat_index = Left_Opnd (Right_Opnd (gnat_cond));
5151 gnat_type = Etype (gnat_index);
5152 gnu_index = gnat_to_gnu (gnat_index);
5154 if (with_extra_info
5155 && gnu_low_bound
5156 && gnu_high_bound
5157 && Known_Esize (gnat_type)
5158 && UI_To_Int (Esize (gnat_type)) <= 32)
5159 gnu_result
5160 = build_call_raise_range (reason, gnat_node, gnu_index,
5161 gnu_low_bound, gnu_high_bound);
5163 /* If loop unswitching is enabled, we try to compute invariant
5164 conditions for checks applied to iteration variables, i.e.
5165 conditions that are both independent of the variable and
5166 necessary in order for the check to fail in the course of
5167 some iteration, and prepend them to the original condition
5168 of the checks. This will make it possible later for the
5169 loop unswitching pass to replace the loop with two loops,
5170 one of which has the checks eliminated and the other has
5171 the original checks reinstated, and a run time selection.
5172 The former loop will be suitable for vectorization. */
5173 if (flag_unswitch_loops
5174 && (!gnu_low_bound
5175 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
5176 && (!gnu_high_bound
5177 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
5178 && (rci = push_range_check_info (gnu_index)))
5180 rci->low_bound = gnu_low_bound;
5181 rci->high_bound = gnu_high_bound;
5182 rci->type = get_unpadded_type (gnat_type);
5183 rci->invariant_cond = build1 (SAVE_EXPR, boolean_type_node,
5184 boolean_true_node);
5185 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5186 boolean_type_node,
5187 rci->invariant_cond,
5188 gnat_to_gnu (gnat_cond));
5191 break;
5193 default:
5194 break;
5197 common:
5198 if (!gnu_result)
5199 gnu_result = build_call_raise (reason, gnat_node, kind);
5200 set_expr_location_from_node (gnu_result, gnat_node);
5202 /* If the type is VOID, this is a statement, so we need to generate the code
5203 for the call. Handle a condition, if there is one. */
5204 if (VOID_TYPE_P (*gnu_result_type_p))
5206 if (Present (gnat_cond))
5208 if (!gnu_cond)
5209 gnu_cond = gnat_to_gnu (gnat_cond);
5210 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
5211 alloc_stmt_list ());
5214 else
5215 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
5217 return gnu_result;
5220 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
5221 parameter of a call. */
5223 static bool
5224 lhs_or_actual_p (Node_Id gnat_node)
5226 Node_Id gnat_parent = Parent (gnat_node);
5227 Node_Kind kind = Nkind (gnat_parent);
5229 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
5230 return true;
5232 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
5233 && Name (gnat_parent) != gnat_node)
5234 return true;
5236 if (kind == N_Parameter_Association)
5237 return true;
5239 return false;
5242 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
5243 of an assignment or an actual parameter of a call. */
5245 static bool
5246 present_in_lhs_or_actual_p (Node_Id gnat_node)
5248 Node_Kind kind;
5250 if (lhs_or_actual_p (gnat_node))
5251 return true;
5253 kind = Nkind (Parent (gnat_node));
5255 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
5256 && lhs_or_actual_p (Parent (gnat_node)))
5257 return true;
5259 return false;
5262 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
5263 as gigi is concerned. This is used to avoid conversions on the LHS. */
5265 static bool
5266 unchecked_conversion_nop (Node_Id gnat_node)
5268 Entity_Id from_type, to_type;
5270 /* The conversion must be on the LHS of an assignment or an actual parameter
5271 of a call. Otherwise, even if the conversion was essentially a no-op, it
5272 could de facto ensure type consistency and this should be preserved. */
5273 if (!lhs_or_actual_p (gnat_node))
5274 return false;
5276 from_type = Etype (Expression (gnat_node));
5278 /* We're interested in artificial conversions generated by the front-end
5279 to make private types explicit, e.g. in Expand_Assign_Array. */
5280 if (!Is_Private_Type (from_type))
5281 return false;
5283 from_type = Underlying_Type (from_type);
5284 to_type = Etype (gnat_node);
5286 /* The direct conversion to the underlying type is a no-op. */
5287 if (to_type == from_type)
5288 return true;
5290 /* For an array subtype, the conversion to the PAT is a no-op. */
5291 if (Ekind (from_type) == E_Array_Subtype
5292 && to_type == Packed_Array_Type (from_type))
5293 return true;
5295 /* For a record subtype, the conversion to the type is a no-op. */
5296 if (Ekind (from_type) == E_Record_Subtype
5297 && to_type == Etype (from_type))
5298 return true;
5300 return false;
5303 /* This function is the driver of the GNAT to GCC tree transformation process.
5304 It is the entry point of the tree transformer. GNAT_NODE is the root of
5305 some GNAT tree. Return the root of the corresponding GCC tree. If this
5306 is an expression, return the GCC equivalent of the expression. If this
5307 is a statement, return the statement or add it to the current statement
5308 group, in which case anything returned is to be interpreted as occurring
5309 after anything added. */
5311 tree
5312 gnat_to_gnu (Node_Id gnat_node)
5314 const Node_Kind kind = Nkind (gnat_node);
5315 bool went_into_elab_proc = false;
5316 tree gnu_result = error_mark_node; /* Default to no value. */
5317 tree gnu_result_type = void_type_node;
5318 tree gnu_expr, gnu_lhs, gnu_rhs;
5319 Node_Id gnat_temp;
5321 /* Save node number for error message and set location information. */
5322 error_gnat_node = gnat_node;
5323 Sloc_to_locus (Sloc (gnat_node), &input_location);
5325 /* If this node is a statement and we are only annotating types, return an
5326 empty statement list. */
5327 if (type_annotate_only && IN (kind, N_Statement_Other_Than_Procedure_Call))
5328 return alloc_stmt_list ();
5330 /* If this node is a non-static subexpression and we are only annotating
5331 types, make this into a NULL_EXPR. */
5332 if (type_annotate_only
5333 && IN (kind, N_Subexpr)
5334 && kind != N_Identifier
5335 && !Compile_Time_Known_Value (gnat_node))
5336 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
5337 build_call_raise (CE_Range_Check_Failed, gnat_node,
5338 N_Raise_Constraint_Error));
5340 if ((IN (kind, N_Statement_Other_Than_Procedure_Call)
5341 && kind != N_Null_Statement)
5342 || kind == N_Procedure_Call_Statement
5343 || kind == N_Label
5344 || kind == N_Implicit_Label_Declaration
5345 || kind == N_Handled_Sequence_Of_Statements
5346 || (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void))
5348 tree current_elab_proc = get_elaboration_procedure ();
5350 /* If this is a statement and we are at top level, it must be part of
5351 the elaboration procedure, so mark us as being in that procedure. */
5352 if (!current_function_decl)
5354 current_function_decl = current_elab_proc;
5355 went_into_elab_proc = true;
5358 /* If we are in the elaboration procedure, check if we are violating a
5359 No_Elaboration_Code restriction by having a statement there. Don't
5360 check for a possible No_Elaboration_Code restriction violation on
5361 N_Handled_Sequence_Of_Statements, as we want to signal an error on
5362 every nested real statement instead. This also avoids triggering
5363 spurious errors on dummy (empty) sequences created by the front-end
5364 for package bodies in some cases. */
5365 if (current_function_decl == current_elab_proc
5366 && kind != N_Handled_Sequence_Of_Statements)
5367 Check_Elaboration_Code_Allowed (gnat_node);
5370 switch (kind)
5372 /********************************/
5373 /* Chapter 2: Lexical Elements */
5374 /********************************/
5376 case N_Identifier:
5377 case N_Expanded_Name:
5378 case N_Operator_Symbol:
5379 case N_Defining_Identifier:
5380 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
5382 /* If this is an atomic access on the RHS for which synchronization is
5383 required, build the atomic load. */
5384 if (atomic_sync_required_p (gnat_node)
5385 && !present_in_lhs_or_actual_p (gnat_node))
5386 gnu_result = build_atomic_load (gnu_result);
5387 break;
5389 case N_Integer_Literal:
5391 tree gnu_type;
5393 /* Get the type of the result, looking inside any padding and
5394 justified modular types. Then get the value in that type. */
5395 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
5397 if (TREE_CODE (gnu_type) == RECORD_TYPE
5398 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
5399 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
5401 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
5403 /* If the result overflows (meaning it doesn't fit in its base type),
5404 abort. We would like to check that the value is within the range
5405 of the subtype, but that causes problems with subtypes whose usage
5406 will raise Constraint_Error and with biased representation, so
5407 we don't. */
5408 gcc_assert (!TREE_OVERFLOW (gnu_result));
5410 break;
5412 case N_Character_Literal:
5413 /* If a Entity is present, it means that this was one of the
5414 literals in a user-defined character type. In that case,
5415 just return the value in the CONST_DECL. Otherwise, use the
5416 character code. In that case, the base type should be an
5417 INTEGER_TYPE, but we won't bother checking for that. */
5418 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5419 if (Present (Entity (gnat_node)))
5420 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
5421 else
5422 gnu_result
5423 = build_int_cst_type
5424 (gnu_result_type, UI_To_CC (Char_Literal_Value (gnat_node)));
5425 break;
5427 case N_Real_Literal:
5428 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5430 /* If this is of a fixed-point type, the value we want is the
5431 value of the corresponding integer. */
5432 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
5434 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
5435 gnu_result_type);
5436 gcc_assert (!TREE_OVERFLOW (gnu_result));
5439 /* Convert the Ureal to a vax float (represented on a signed type). */
5440 else if (Vax_Float (Underlying_Type (Etype (gnat_node))))
5442 gnu_result = UI_To_gnu (Get_Vax_Real_Literal_As_Signed (gnat_node),
5443 gnu_result_type);
5446 else
5448 Ureal ur_realval = Realval (gnat_node);
5450 /* First convert the real value to a machine number if it isn't
5451 already. That forces BASE to 2 for non-zero values and simplifies
5452 the rest of our logic. */
5454 if (!Is_Machine_Number (gnat_node))
5455 ur_realval
5456 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
5457 ur_realval, Round_Even, gnat_node);
5459 if (UR_Is_Zero (ur_realval))
5460 gnu_result = convert (gnu_result_type, integer_zero_node);
5461 else
5463 REAL_VALUE_TYPE tmp;
5465 gnu_result
5466 = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
5468 /* The base must be 2 as Machine guarantees this, so we scale
5469 the value, which we know can fit in the mantissa of the type
5470 (hence the use of that type above). */
5472 gcc_assert (Rbase (ur_realval) == 2);
5473 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
5474 - UI_To_Int (Denominator (ur_realval)));
5475 gnu_result = build_real (gnu_result_type, tmp);
5478 /* Now see if we need to negate the result. Do it this way to
5479 properly handle -0. */
5480 if (UR_Is_Negative (Realval (gnat_node)))
5481 gnu_result
5482 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
5483 gnu_result);
5486 break;
5488 case N_String_Literal:
5489 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5490 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
5492 String_Id gnat_string = Strval (gnat_node);
5493 int length = String_Length (gnat_string);
5494 int i;
5495 char *string;
5496 if (length >= ALLOCA_THRESHOLD)
5497 string = XNEWVEC (char, length + 1);
5498 else
5499 string = (char *) alloca (length + 1);
5501 /* Build the string with the characters in the literal. Note
5502 that Ada strings are 1-origin. */
5503 for (i = 0; i < length; i++)
5504 string[i] = Get_String_Char (gnat_string, i + 1);
5506 /* Put a null at the end of the string in case it's in a context
5507 where GCC will want to treat it as a C string. */
5508 string[i] = 0;
5510 gnu_result = build_string (length, string);
5512 /* Strings in GCC don't normally have types, but we want
5513 this to not be converted to the array type. */
5514 TREE_TYPE (gnu_result) = gnu_result_type;
5516 if (length >= ALLOCA_THRESHOLD)
5517 free (string);
5519 else
5521 /* Build a list consisting of each character, then make
5522 the aggregate. */
5523 String_Id gnat_string = Strval (gnat_node);
5524 int length = String_Length (gnat_string);
5525 int i;
5526 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5527 vec<constructor_elt, va_gc> *gnu_vec;
5528 vec_alloc (gnu_vec, length);
5530 for (i = 0; i < length; i++)
5532 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
5533 Get_String_Char (gnat_string, i + 1));
5535 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
5536 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, integer_one_node);
5539 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
5541 break;
5543 case N_Pragma:
5544 gnu_result = Pragma_to_gnu (gnat_node);
5545 break;
5547 /**************************************/
5548 /* Chapter 3: Declarations and Types */
5549 /**************************************/
5551 case N_Subtype_Declaration:
5552 case N_Full_Type_Declaration:
5553 case N_Incomplete_Type_Declaration:
5554 case N_Private_Type_Declaration:
5555 case N_Private_Extension_Declaration:
5556 case N_Task_Type_Declaration:
5557 process_type (Defining_Entity (gnat_node));
5558 gnu_result = alloc_stmt_list ();
5559 break;
5561 case N_Object_Declaration:
5562 case N_Exception_Declaration:
5563 gnat_temp = Defining_Entity (gnat_node);
5564 gnu_result = alloc_stmt_list ();
5566 /* If we are just annotating types and this object has an unconstrained
5567 or task type, don't elaborate it. */
5568 if (type_annotate_only
5569 && (((Is_Array_Type (Etype (gnat_temp))
5570 || Is_Record_Type (Etype (gnat_temp)))
5571 && !Is_Constrained (Etype (gnat_temp)))
5572 || Is_Concurrent_Type (Etype (gnat_temp))))
5573 break;
5575 if (Present (Expression (gnat_node))
5576 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
5577 && (!type_annotate_only
5578 || Compile_Time_Known_Value (Expression (gnat_node))))
5580 gnu_expr = gnat_to_gnu (Expression (gnat_node));
5581 if (Do_Range_Check (Expression (gnat_node)))
5582 gnu_expr
5583 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
5585 /* If this object has its elaboration delayed, we must force
5586 evaluation of GNU_EXPR right now and save it for when the object
5587 is frozen. */
5588 if (Present (Freeze_Node (gnat_temp)))
5590 if (TREE_CONSTANT (gnu_expr))
5592 else if (global_bindings_p ())
5593 gnu_expr
5594 = create_var_decl (create_concat_name (gnat_temp, "init"),
5595 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5596 false, false, false, false,
5597 NULL, gnat_temp);
5598 else
5599 gnu_expr = gnat_save_expr (gnu_expr);
5601 save_gnu_tree (gnat_node, gnu_expr, true);
5604 else
5605 gnu_expr = NULL_TREE;
5607 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
5608 gnu_expr = NULL_TREE;
5610 /* If this is a deferred constant with an address clause, we ignore the
5611 full view since the clause is on the partial view and we cannot have
5612 2 different GCC trees for the object. The only bits of the full view
5613 we will use is the initializer, but it will be directly fetched. */
5614 if (Ekind(gnat_temp) == E_Constant
5615 && Present (Address_Clause (gnat_temp))
5616 && Present (Full_View (gnat_temp)))
5617 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
5619 if (No (Freeze_Node (gnat_temp)))
5620 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
5621 break;
5623 case N_Object_Renaming_Declaration:
5624 gnat_temp = Defining_Entity (gnat_node);
5626 /* Don't do anything if this renaming is handled by the front end or if
5627 we are just annotating types and this object has a composite or task
5628 type, don't elaborate it. We return the result in case it has any
5629 SAVE_EXPRs in it that need to be evaluated here. */
5630 if (!Is_Renaming_Of_Object (gnat_temp)
5631 && ! (type_annotate_only
5632 && (Is_Array_Type (Etype (gnat_temp))
5633 || Is_Record_Type (Etype (gnat_temp))
5634 || Is_Concurrent_Type (Etype (gnat_temp)))))
5635 gnu_result
5636 = gnat_to_gnu_entity (gnat_temp,
5637 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
5638 else
5639 gnu_result = alloc_stmt_list ();
5640 break;
5642 case N_Implicit_Label_Declaration:
5643 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
5644 gnu_result = alloc_stmt_list ();
5645 break;
5647 case N_Exception_Renaming_Declaration:
5648 case N_Number_Declaration:
5649 case N_Package_Renaming_Declaration:
5650 case N_Subprogram_Renaming_Declaration:
5651 /* These are fully handled in the front end. */
5652 gnu_result = alloc_stmt_list ();
5653 break;
5655 /*************************************/
5656 /* Chapter 4: Names and Expressions */
5657 /*************************************/
5659 case N_Explicit_Dereference:
5660 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5661 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5662 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
5664 /* If this is an atomic access on the RHS for which synchronization is
5665 required, build the atomic load. */
5666 if (atomic_sync_required_p (gnat_node)
5667 && !present_in_lhs_or_actual_p (gnat_node))
5668 gnu_result = build_atomic_load (gnu_result);
5669 break;
5671 case N_Indexed_Component:
5673 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
5674 tree gnu_type;
5675 int ndim;
5676 int i;
5677 Node_Id *gnat_expr_array;
5679 gnu_array_object = maybe_implicit_deref (gnu_array_object);
5681 /* Convert vector inputs to their representative array type, to fit
5682 what the code below expects. */
5683 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
5685 if (present_in_lhs_or_actual_p (gnat_node))
5686 gnat_mark_addressable (gnu_array_object);
5687 gnu_array_object = maybe_vector_array (gnu_array_object);
5690 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
5692 /* If we got a padded type, remove it too. */
5693 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
5694 gnu_array_object
5695 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
5696 gnu_array_object);
5698 gnu_result = gnu_array_object;
5700 /* The failure of this assertion will very likely come from a missing
5701 expansion for a packed array access. */
5702 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
5704 /* First compute the number of dimensions of the array, then
5705 fill the expression array, the order depending on whether
5706 this is a Convention_Fortran array or not. */
5707 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
5708 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5709 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
5710 ndim++, gnu_type = TREE_TYPE (gnu_type))
5713 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
5715 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
5716 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
5717 i >= 0;
5718 i--, gnat_temp = Next (gnat_temp))
5719 gnat_expr_array[i] = gnat_temp;
5720 else
5721 for (i = 0, gnat_temp = First (Expressions (gnat_node));
5722 i < ndim;
5723 i++, gnat_temp = Next (gnat_temp))
5724 gnat_expr_array[i] = gnat_temp;
5726 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
5727 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
5729 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
5730 gnat_temp = gnat_expr_array[i];
5731 gnu_expr = gnat_to_gnu (gnat_temp);
5733 if (Do_Range_Check (gnat_temp))
5734 gnu_expr
5735 = emit_index_check
5736 (gnu_array_object, gnu_expr,
5737 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5738 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5739 gnat_temp);
5741 gnu_result = build_binary_op (ARRAY_REF, NULL_TREE,
5742 gnu_result, gnu_expr);
5745 gnu_result_type = get_unpadded_type (Etype (gnat_node));
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);
5753 break;
5755 case N_Slice:
5757 Node_Id gnat_range_node = Discrete_Range (gnat_node);
5758 tree gnu_type;
5760 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5761 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5763 /* Do any implicit dereferences of the prefix and do any needed
5764 range check. */
5765 gnu_result = maybe_implicit_deref (gnu_result);
5766 gnu_result = maybe_unconstrained_array (gnu_result);
5767 gnu_type = TREE_TYPE (gnu_result);
5768 if (Do_Range_Check (gnat_range_node))
5770 /* Get the bounds of the slice. */
5771 tree gnu_index_type
5772 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
5773 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
5774 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
5775 /* Get the permitted bounds. */
5776 tree gnu_base_index_type
5777 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
5778 tree gnu_base_min_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5779 (TYPE_MIN_VALUE (gnu_base_index_type), gnu_result);
5780 tree gnu_base_max_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5781 (TYPE_MAX_VALUE (gnu_base_index_type), gnu_result);
5782 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
5784 gnu_min_expr = gnat_protect_expr (gnu_min_expr);
5785 gnu_max_expr = gnat_protect_expr (gnu_max_expr);
5787 /* Derive a good type to convert everything to. */
5788 gnu_expr_type = get_base_type (gnu_index_type);
5790 /* Test whether the minimum slice value is too small. */
5791 gnu_expr_l = build_binary_op (LT_EXPR, boolean_type_node,
5792 convert (gnu_expr_type,
5793 gnu_min_expr),
5794 convert (gnu_expr_type,
5795 gnu_base_min_expr));
5797 /* Test whether the maximum slice value is too large. */
5798 gnu_expr_h = build_binary_op (GT_EXPR, boolean_type_node,
5799 convert (gnu_expr_type,
5800 gnu_max_expr),
5801 convert (gnu_expr_type,
5802 gnu_base_max_expr));
5804 /* Build a slice index check that returns the low bound,
5805 assuming the slice is not empty. */
5806 gnu_expr = emit_check
5807 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
5808 gnu_expr_l, gnu_expr_h),
5809 gnu_min_expr, CE_Index_Check_Failed, gnat_node);
5811 /* Build a conditional expression that does the index checks and
5812 returns the low bound if the slice is not empty (max >= min),
5813 and returns the naked low bound otherwise (max < min), unless
5814 it is non-constant and the high bound is; this prevents VRP
5815 from inferring bogus ranges on the unlikely path. */
5816 gnu_expr = fold_build3 (COND_EXPR, gnu_expr_type,
5817 build_binary_op (GE_EXPR, gnu_expr_type,
5818 convert (gnu_expr_type,
5819 gnu_max_expr),
5820 convert (gnu_expr_type,
5821 gnu_min_expr)),
5822 gnu_expr,
5823 TREE_CODE (gnu_min_expr) != INTEGER_CST
5824 && TREE_CODE (gnu_max_expr) == INTEGER_CST
5825 ? gnu_max_expr : gnu_min_expr);
5827 else
5828 /* Simply return the naked low bound. */
5829 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5831 /* If this is a slice with non-constant size of an array with constant
5832 size, set the maximum size for the allocation of temporaries. */
5833 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
5834 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_type)))
5835 TYPE_ARRAY_MAX_SIZE (gnu_result_type) = TYPE_SIZE_UNIT (gnu_type);
5837 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
5838 gnu_result, gnu_expr);
5840 break;
5842 case N_Selected_Component:
5844 tree gnu_prefix = gnat_to_gnu (Prefix (gnat_node));
5845 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
5846 Entity_Id gnat_pref_type = Etype (Prefix (gnat_node));
5847 tree gnu_field;
5849 while (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind)
5850 || IN (Ekind (gnat_pref_type), Access_Kind))
5852 if (IN (Ekind (gnat_pref_type), Incomplete_Or_Private_Kind))
5853 gnat_pref_type = Underlying_Type (gnat_pref_type);
5854 else if (IN (Ekind (gnat_pref_type), Access_Kind))
5855 gnat_pref_type = Designated_Type (gnat_pref_type);
5858 gnu_prefix = maybe_implicit_deref (gnu_prefix);
5860 /* For discriminant references in tagged types always substitute the
5861 corresponding discriminant as the actual selected component. */
5862 if (Is_Tagged_Type (gnat_pref_type))
5863 while (Present (Corresponding_Discriminant (gnat_field)))
5864 gnat_field = Corresponding_Discriminant (gnat_field);
5866 /* For discriminant references of untagged types always substitute the
5867 corresponding stored discriminant. */
5868 else if (Present (Corresponding_Discriminant (gnat_field)))
5869 gnat_field = Original_Record_Component (gnat_field);
5871 /* Handle extracting the real or imaginary part of a complex.
5872 The real part is the first field and the imaginary the last. */
5873 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
5874 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
5875 ? REALPART_EXPR : IMAGPART_EXPR,
5876 NULL_TREE, gnu_prefix);
5877 else
5879 gnu_field = gnat_to_gnu_field_decl (gnat_field);
5881 /* If there are discriminants, the prefix might be evaluated more
5882 than once, which is a problem if it has side-effects. */
5883 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
5884 ? Designated_Type (Etype
5885 (Prefix (gnat_node)))
5886 : Etype (Prefix (gnat_node))))
5887 gnu_prefix = gnat_stabilize_reference (gnu_prefix, false, NULL);
5889 gnu_result
5890 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
5891 (Nkind (Parent (gnat_node))
5892 == N_Attribute_Reference)
5893 && lvalue_required_for_attribute_p
5894 (Parent (gnat_node)));
5897 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5899 /* If this is an atomic access on the RHS for which synchronization is
5900 required, build the atomic load. */
5901 if (atomic_sync_required_p (gnat_node)
5902 && !present_in_lhs_or_actual_p (gnat_node))
5903 gnu_result = build_atomic_load (gnu_result);
5905 break;
5907 case N_Attribute_Reference:
5909 /* The attribute designator. */
5910 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
5912 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
5913 is a unit, not an object with a GCC equivalent. */
5914 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
5915 return
5916 create_subprog_decl (create_concat_name
5917 (Entity (Prefix (gnat_node)),
5918 attr == Attr_Elab_Body ? "elabb" : "elabs"),
5919 NULL_TREE, void_ftype, NULL_TREE, is_disabled,
5920 true, true, true, NULL, gnat_node);
5922 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
5924 break;
5926 case N_Reference:
5927 /* Like 'Access as far as we are concerned. */
5928 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5929 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
5930 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5931 break;
5933 case N_Aggregate:
5934 case N_Extension_Aggregate:
5936 tree gnu_aggr_type;
5938 /* ??? It is wrong to evaluate the type now, but there doesn't
5939 seem to be any other practical way of doing it. */
5941 gcc_assert (!Expansion_Delayed (gnat_node));
5943 gnu_aggr_type = gnu_result_type
5944 = get_unpadded_type (Etype (gnat_node));
5946 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
5947 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
5948 gnu_aggr_type
5949 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
5950 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
5951 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
5953 if (Null_Record_Present (gnat_node))
5954 gnu_result = gnat_build_constructor (gnu_aggr_type,
5955 NULL);
5957 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
5958 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
5959 gnu_result
5960 = assoc_to_constructor (Etype (gnat_node),
5961 First (Component_Associations (gnat_node)),
5962 gnu_aggr_type);
5963 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
5964 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
5965 gnu_aggr_type,
5966 Component_Type (Etype (gnat_node)));
5967 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
5968 gnu_result
5969 = build_binary_op
5970 (COMPLEX_EXPR, gnu_aggr_type,
5971 gnat_to_gnu (Expression (First
5972 (Component_Associations (gnat_node)))),
5973 gnat_to_gnu (Expression
5974 (Next
5975 (First (Component_Associations (gnat_node))))));
5976 else
5977 gcc_unreachable ();
5979 gnu_result = convert (gnu_result_type, gnu_result);
5981 break;
5983 case N_Null:
5984 if (TARGET_VTABLE_USES_DESCRIPTORS
5985 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
5986 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
5987 gnu_result = null_fdesc_node;
5988 else
5989 gnu_result = null_pointer_node;
5990 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5991 break;
5993 case N_Type_Conversion:
5994 case N_Qualified_Expression:
5995 /* Get the operand expression. */
5996 gnu_result = gnat_to_gnu (Expression (gnat_node));
5997 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5999 /* If this is a qualified expression for a tagged type, we mark the type
6000 as used. Because of polymorphism, this might be the only reference to
6001 the tagged type in the program while objects have it as dynamic type.
6002 The debugger needs to see it to display these objects properly. */
6003 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
6004 used_types_insert (gnu_result_type);
6006 gnu_result
6007 = convert_with_check (Etype (gnat_node), gnu_result,
6008 Do_Overflow_Check (gnat_node),
6009 Do_Range_Check (Expression (gnat_node)),
6010 kind == N_Type_Conversion
6011 && Float_Truncate (gnat_node), gnat_node);
6012 break;
6014 case N_Unchecked_Type_Conversion:
6015 gnu_result = gnat_to_gnu (Expression (gnat_node));
6017 /* Skip further processing if the conversion is deemed a no-op. */
6018 if (unchecked_conversion_nop (gnat_node))
6020 gnu_result_type = TREE_TYPE (gnu_result);
6021 break;
6024 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6026 /* If the result is a pointer type, see if we are improperly
6027 converting to a stricter alignment. */
6028 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
6029 && IN (Ekind (Etype (gnat_node)), Access_Kind))
6031 unsigned int align = known_alignment (gnu_result);
6032 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6033 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6035 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6036 post_error_ne_tree_2
6037 ("?source alignment (^) '< alignment of & (^)",
6038 gnat_node, Designated_Type (Etype (gnat_node)),
6039 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
6042 /* If we are converting a descriptor to a function pointer, first
6043 build the pointer. */
6044 if (TARGET_VTABLE_USES_DESCRIPTORS
6045 && TREE_TYPE (gnu_result) == fdesc_type_node
6046 && POINTER_TYPE_P (gnu_result_type))
6047 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6049 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
6050 No_Truncation (gnat_node));
6051 break;
6053 case N_In:
6054 case N_Not_In:
6056 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
6057 Node_Id gnat_range = Right_Opnd (gnat_node);
6058 tree gnu_low, gnu_high;
6060 /* GNAT_RANGE is either an N_Range node or an identifier denoting a
6061 subtype. */
6062 if (Nkind (gnat_range) == N_Range)
6064 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
6065 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
6067 else if (Nkind (gnat_range) == N_Identifier
6068 || Nkind (gnat_range) == N_Expanded_Name)
6070 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
6072 gnu_low = TYPE_MIN_VALUE (gnu_range_type);
6073 gnu_high = TYPE_MAX_VALUE (gnu_range_type);
6075 else
6076 gcc_unreachable ();
6078 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6080 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
6081 ensure that GNU_OBJ is evaluated only once and perform a full range
6082 test. */
6083 if (operand_equal_p (gnu_low, gnu_high, 0))
6084 gnu_result
6085 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
6086 else
6088 tree t1, t2;
6089 gnu_obj = gnat_protect_expr (gnu_obj);
6090 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
6091 if (EXPR_P (t1))
6092 set_expr_location_from_node (t1, gnat_node);
6093 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
6094 if (EXPR_P (t2))
6095 set_expr_location_from_node (t2, gnat_node);
6096 gnu_result
6097 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
6100 if (kind == N_Not_In)
6101 gnu_result
6102 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
6104 break;
6106 case N_Op_Divide:
6107 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6108 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6109 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6110 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
6111 ? RDIV_EXPR
6112 : (Rounded_Result (gnat_node)
6113 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
6114 gnu_result_type, gnu_lhs, gnu_rhs);
6115 break;
6117 case N_Op_Or: case N_Op_And: case N_Op_Xor:
6118 /* These can either be operations on booleans or on modular types.
6119 Fall through for boolean types since that's the way GNU_CODES is
6120 set up. */
6121 if (IN (Ekind (Underlying_Type (Etype (gnat_node))),
6122 Modular_Integer_Kind))
6124 enum tree_code code
6125 = (kind == N_Op_Or ? BIT_IOR_EXPR
6126 : kind == N_Op_And ? BIT_AND_EXPR
6127 : BIT_XOR_EXPR);
6129 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6130 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6131 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6132 gnu_result = build_binary_op (code, gnu_result_type,
6133 gnu_lhs, gnu_rhs);
6134 break;
6137 /* ... fall through ... */
6139 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
6140 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
6141 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
6142 case N_Op_Mod: case N_Op_Rem:
6143 case N_Op_Rotate_Left:
6144 case N_Op_Rotate_Right:
6145 case N_Op_Shift_Left:
6146 case N_Op_Shift_Right:
6147 case N_Op_Shift_Right_Arithmetic:
6148 case N_And_Then: case N_Or_Else:
6150 enum tree_code code = gnu_codes[kind];
6151 bool ignore_lhs_overflow = false;
6152 location_t saved_location = input_location;
6153 tree gnu_type;
6155 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6156 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6157 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6159 /* Pending generic support for efficient vector logical operations in
6160 GCC, convert vectors to their representative array type view and
6161 fallthrough. */
6162 gnu_lhs = maybe_vector_array (gnu_lhs);
6163 gnu_rhs = maybe_vector_array (gnu_rhs);
6165 /* If this is a comparison operator, convert any references to
6166 an unconstrained array value into a reference to the
6167 actual array. */
6168 if (TREE_CODE_CLASS (code) == tcc_comparison)
6170 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
6171 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
6174 /* If the result type is a private type, its full view may be a
6175 numeric subtype. The representation we need is that of its base
6176 type, given that it is the result of an arithmetic operation. */
6177 else if (Is_Private_Type (Etype (gnat_node)))
6178 gnu_type = gnu_result_type
6179 = get_unpadded_type (Base_Type (Full_View (Etype (gnat_node))));
6181 /* If this is a shift whose count is not guaranteed to be correct,
6182 we need to adjust the shift count. */
6183 if (IN (kind, N_Op_Shift) && !Shift_Count_OK (gnat_node))
6185 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
6186 tree gnu_max_shift
6187 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
6189 if (kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
6190 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
6191 gnu_rhs, gnu_max_shift);
6192 else if (kind == N_Op_Shift_Right_Arithmetic)
6193 gnu_rhs
6194 = build_binary_op
6195 (MIN_EXPR, gnu_count_type,
6196 build_binary_op (MINUS_EXPR,
6197 gnu_count_type,
6198 gnu_max_shift,
6199 convert (gnu_count_type,
6200 integer_one_node)),
6201 gnu_rhs);
6204 /* For right shifts, the type says what kind of shift to do,
6205 so we may need to choose a different type. In this case,
6206 we have to ignore integer overflow lest it propagates all
6207 the way down and causes a CE to be explicitly raised. */
6208 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
6210 gnu_type = gnat_unsigned_type (gnu_type);
6211 ignore_lhs_overflow = true;
6213 else if (kind == N_Op_Shift_Right_Arithmetic
6214 && TYPE_UNSIGNED (gnu_type))
6216 gnu_type = gnat_signed_type (gnu_type);
6217 ignore_lhs_overflow = true;
6220 if (gnu_type != gnu_result_type)
6222 tree gnu_old_lhs = gnu_lhs;
6223 gnu_lhs = convert (gnu_type, gnu_lhs);
6224 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
6225 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
6226 gnu_rhs = convert (gnu_type, gnu_rhs);
6229 /* Instead of expanding overflow checks for addition, subtraction
6230 and multiplication itself, the front end will leave this to
6231 the back end when Backend_Overflow_Checks_On_Target is set.
6232 As the GCC back end itself does not know yet how to properly
6233 do overflow checking, do it here. The goal is to push
6234 the expansions further into the back end over time. */
6235 if (Do_Overflow_Check (gnat_node) && Backend_Overflow_Checks_On_Target
6236 && (kind == N_Op_Add
6237 || kind == N_Op_Subtract
6238 || kind == N_Op_Multiply)
6239 && !TYPE_UNSIGNED (gnu_type)
6240 && !FLOAT_TYPE_P (gnu_type))
6241 gnu_result = build_binary_op_trapv (code, gnu_type,
6242 gnu_lhs, gnu_rhs, gnat_node);
6243 else
6245 /* Some operations, e.g. comparisons of arrays, generate complex
6246 trees that need to be annotated while they are being built. */
6247 input_location = saved_location;
6248 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
6251 /* If this is a logical shift with the shift count not verified,
6252 we must return zero if it is too large. We cannot compensate
6253 above in this case. */
6254 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
6255 && !Shift_Count_OK (gnat_node))
6256 gnu_result
6257 = build_cond_expr
6258 (gnu_type,
6259 build_binary_op (GE_EXPR, boolean_type_node,
6260 gnu_rhs,
6261 convert (TREE_TYPE (gnu_rhs),
6262 TYPE_SIZE (gnu_type))),
6263 convert (gnu_type, integer_zero_node),
6264 gnu_result);
6266 break;
6268 case N_If_Expression:
6270 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
6271 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
6272 tree gnu_false
6273 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
6275 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6276 gnu_result
6277 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
6279 break;
6281 case N_Op_Plus:
6282 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
6283 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6284 break;
6286 case N_Op_Not:
6287 /* This case can apply to a boolean or a modular type.
6288 Fall through for a boolean operand since GNU_CODES is set
6289 up to handle this. */
6290 if (Is_Modular_Integer_Type (Etype (gnat_node))
6291 || (Is_Private_Type (Etype (gnat_node))
6292 && Is_Modular_Integer_Type (Full_View (Etype (gnat_node)))))
6294 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6295 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6296 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
6297 gnu_expr);
6298 break;
6301 /* ... fall through ... */
6303 case N_Op_Minus: case N_Op_Abs:
6304 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6305 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6307 if (Do_Overflow_Check (gnat_node)
6308 && !TYPE_UNSIGNED (gnu_result_type)
6309 && !FLOAT_TYPE_P (gnu_result_type))
6310 gnu_result
6311 = build_unary_op_trapv (gnu_codes[kind],
6312 gnu_result_type, gnu_expr, gnat_node);
6313 else
6314 gnu_result = build_unary_op (gnu_codes[kind],
6315 gnu_result_type, gnu_expr);
6316 break;
6318 case N_Allocator:
6320 tree gnu_init = 0;
6321 tree gnu_type;
6322 bool ignore_init_type = false;
6324 gnat_temp = Expression (gnat_node);
6326 /* The Expression operand can either be an N_Identifier or
6327 Expanded_Name, which must represent a type, or a
6328 N_Qualified_Expression, which contains both the object type and an
6329 initial value for the object. */
6330 if (Nkind (gnat_temp) == N_Identifier
6331 || Nkind (gnat_temp) == N_Expanded_Name)
6332 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
6333 else if (Nkind (gnat_temp) == N_Qualified_Expression)
6335 Entity_Id gnat_desig_type
6336 = Designated_Type (Underlying_Type (Etype (gnat_node)));
6338 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
6339 gnu_init = gnat_to_gnu (Expression (gnat_temp));
6341 gnu_init = maybe_unconstrained_array (gnu_init);
6342 if (Do_Range_Check (Expression (gnat_temp)))
6343 gnu_init
6344 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
6346 if (Is_Elementary_Type (gnat_desig_type)
6347 || Is_Constrained (gnat_desig_type))
6348 gnu_type = gnat_to_gnu_type (gnat_desig_type);
6349 else
6351 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
6352 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
6353 gnu_type = TREE_TYPE (gnu_init);
6356 /* See the N_Qualified_Expression case for the rationale. */
6357 if (Is_Tagged_Type (gnat_desig_type))
6358 used_types_insert (gnu_type);
6360 gnu_init = convert (gnu_type, gnu_init);
6362 else
6363 gcc_unreachable ();
6365 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6366 return build_allocator (gnu_type, gnu_init, gnu_result_type,
6367 Procedure_To_Call (gnat_node),
6368 Storage_Pool (gnat_node), gnat_node,
6369 ignore_init_type);
6371 break;
6373 /**************************/
6374 /* Chapter 5: Statements */
6375 /**************************/
6377 case N_Label:
6378 gnu_result = build1 (LABEL_EXPR, void_type_node,
6379 gnat_to_gnu (Identifier (gnat_node)));
6380 break;
6382 case N_Null_Statement:
6383 /* When not optimizing, turn null statements from source into gotos to
6384 the next statement that the middle-end knows how to preserve. */
6385 if (!optimize && Comes_From_Source (gnat_node))
6387 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
6388 DECL_IGNORED_P (label) = 1;
6389 start_stmt_group ();
6390 stmt = build1 (GOTO_EXPR, void_type_node, label);
6391 set_expr_location_from_node (stmt, gnat_node);
6392 add_stmt (stmt);
6393 stmt = build1 (LABEL_EXPR, void_type_node, label);
6394 set_expr_location_from_node (stmt, gnat_node);
6395 add_stmt (stmt);
6396 gnu_result = end_stmt_group ();
6398 else
6399 gnu_result = alloc_stmt_list ();
6400 break;
6402 case N_Assignment_Statement:
6403 /* Get the LHS and RHS of the statement and convert any reference to an
6404 unconstrained array into a reference to the underlying array. */
6405 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
6407 /* If the type has a size that overflows, convert this into raise of
6408 Storage_Error: execution shouldn't have gotten here anyway. */
6409 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
6410 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
6411 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
6412 N_Raise_Storage_Error);
6413 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
6414 gnu_result
6415 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
6416 atomic_sync_required_p (Name (gnat_node)));
6417 else
6419 gnu_rhs
6420 = maybe_unconstrained_array (gnat_to_gnu (Expression (gnat_node)));
6422 /* If range check is needed, emit code to generate it. */
6423 if (Do_Range_Check (Expression (gnat_node)))
6424 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
6425 gnat_node);
6427 if (atomic_sync_required_p (Name (gnat_node)))
6428 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs);
6429 else
6430 gnu_result
6431 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
6433 /* If the type being assigned is an array type and the two sides are
6434 not completely disjoint, play safe and use memmove. But don't do
6435 it for a bit-packed array as it might not be byte-aligned. */
6436 if (TREE_CODE (gnu_result) == MODIFY_EXPR
6437 && Is_Array_Type (Etype (Name (gnat_node)))
6438 && !Is_Bit_Packed_Array (Etype (Name (gnat_node)))
6439 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
6441 tree to, from, size, to_ptr, from_ptr, t;
6443 to = TREE_OPERAND (gnu_result, 0);
6444 from = TREE_OPERAND (gnu_result, 1);
6446 size = TYPE_SIZE_UNIT (TREE_TYPE (from));
6447 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, from);
6449 to_ptr = build_fold_addr_expr (to);
6450 from_ptr = build_fold_addr_expr (from);
6452 t = builtin_decl_implicit (BUILT_IN_MEMMOVE);
6453 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
6456 break;
6458 case N_If_Statement:
6460 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
6462 /* Make the outer COND_EXPR. Avoid non-determinism. */
6463 gnu_result = build3 (COND_EXPR, void_type_node,
6464 gnat_to_gnu (Condition (gnat_node)),
6465 NULL_TREE, NULL_TREE);
6466 COND_EXPR_THEN (gnu_result)
6467 = build_stmt_group (Then_Statements (gnat_node), false);
6468 TREE_SIDE_EFFECTS (gnu_result) = 1;
6469 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
6471 /* Now make a COND_EXPR for each of the "else if" parts. Put each
6472 into the previous "else" part and point to where to put any
6473 outer "else". Also avoid non-determinism. */
6474 if (Present (Elsif_Parts (gnat_node)))
6475 for (gnat_temp = First (Elsif_Parts (gnat_node));
6476 Present (gnat_temp); gnat_temp = Next (gnat_temp))
6478 gnu_expr = build3 (COND_EXPR, void_type_node,
6479 gnat_to_gnu (Condition (gnat_temp)),
6480 NULL_TREE, NULL_TREE);
6481 COND_EXPR_THEN (gnu_expr)
6482 = build_stmt_group (Then_Statements (gnat_temp), false);
6483 TREE_SIDE_EFFECTS (gnu_expr) = 1;
6484 set_expr_location_from_node (gnu_expr, gnat_temp);
6485 *gnu_else_ptr = gnu_expr;
6486 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
6489 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
6491 break;
6493 case N_Case_Statement:
6494 gnu_result = Case_Statement_to_gnu (gnat_node);
6495 break;
6497 case N_Loop_Statement:
6498 gnu_result = Loop_Statement_to_gnu (gnat_node);
6499 break;
6501 case N_Block_Statement:
6502 /* The only way to enter the block is to fall through to it. */
6503 if (stmt_group_may_fallthru ())
6505 start_stmt_group ();
6506 gnat_pushlevel ();
6507 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6508 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6509 gnat_poplevel ();
6510 gnu_result = end_stmt_group ();
6512 else
6513 gnu_result = alloc_stmt_list ();
6514 break;
6516 case N_Exit_Statement:
6517 gnu_result
6518 = build2 (EXIT_STMT, void_type_node,
6519 (Present (Condition (gnat_node))
6520 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
6521 (Present (Name (gnat_node))
6522 ? get_gnu_tree (Entity (Name (gnat_node)))
6523 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
6524 break;
6526 case N_Simple_Return_Statement:
6528 tree gnu_ret_obj, gnu_ret_val;
6530 /* If the subprogram is a function, we must return the expression. */
6531 if (Present (Expression (gnat_node)))
6533 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
6535 /* If this function has copy-in/copy-out parameters, get the real
6536 object for the return. See Subprogram_to_gnu. */
6537 if (TYPE_CI_CO_LIST (gnu_subprog_type))
6538 gnu_ret_obj = gnu_return_var_stack->last ();
6539 else
6540 gnu_ret_obj = DECL_RESULT (current_function_decl);
6542 /* Get the GCC tree for the expression to be returned. */
6543 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
6545 /* Do not remove the padding from GNU_RET_VAL if the inner type is
6546 self-referential since we want to allocate the fixed size. */
6547 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
6548 && TYPE_IS_PADDING_P
6549 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))
6550 && CONTAINS_PLACEHOLDER_P
6551 (TYPE_SIZE (TREE_TYPE (gnu_ret_val))))
6552 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
6554 /* If the function returns by direct reference, return a pointer
6555 to the return value. */
6556 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)
6557 || By_Ref (gnat_node))
6558 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
6560 /* Otherwise, if it returns an unconstrained array, we have to
6561 allocate a new version of the result and return it. */
6562 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type))
6564 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
6566 /* And find out whether this is a candidate for Named Return
6567 Value. If so, record it. */
6568 if (!TYPE_CI_CO_LIST (gnu_subprog_type) && optimize)
6570 tree ret_val = gnu_ret_val;
6572 /* Strip useless conversions around the return value. */
6573 if (gnat_useless_type_conversion (ret_val))
6574 ret_val = TREE_OPERAND (ret_val, 0);
6576 /* Strip unpadding around the return value. */
6577 if (TREE_CODE (ret_val) == COMPONENT_REF
6578 && TYPE_IS_PADDING_P
6579 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
6580 ret_val = TREE_OPERAND (ret_val, 0);
6582 /* Now apply the test to the return value. */
6583 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
6585 if (!f_named_ret_val)
6586 f_named_ret_val = BITMAP_GGC_ALLOC ();
6587 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
6588 if (!f_gnat_ret)
6589 f_gnat_ret = gnat_node;
6593 gnu_ret_val = build_allocator (TREE_TYPE (gnu_ret_val),
6594 gnu_ret_val,
6595 TREE_TYPE (gnu_ret_obj),
6596 Procedure_To_Call (gnat_node),
6597 Storage_Pool (gnat_node),
6598 gnat_node, false);
6601 /* Otherwise, if it returns by invisible reference, dereference
6602 the pointer it is passed using the type of the return value
6603 and build the copy operation manually. This ensures that we
6604 don't copy too much data, for example if the return type is
6605 unconstrained with a maximum size. */
6606 else if (TREE_ADDRESSABLE (gnu_subprog_type))
6608 tree gnu_ret_deref
6609 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
6610 gnu_ret_obj);
6611 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
6612 gnu_ret_deref, gnu_ret_val);
6613 add_stmt_with_node (gnu_result, gnat_node);
6614 gnu_ret_val = NULL_TREE;
6618 else
6619 gnu_ret_obj = gnu_ret_val = NULL_TREE;
6621 /* If we have a return label defined, convert this into a branch to
6622 that label. The return proper will be handled elsewhere. */
6623 if (gnu_return_label_stack->last ())
6625 if (gnu_ret_obj)
6626 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_ret_obj,
6627 gnu_ret_val));
6629 gnu_result = build1 (GOTO_EXPR, void_type_node,
6630 gnu_return_label_stack->last ());
6632 /* When not optimizing, make sure the return is preserved. */
6633 if (!optimize && Comes_From_Source (gnat_node))
6634 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
6637 /* Otherwise, build a regular return. */
6638 else
6639 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
6641 break;
6643 case N_Goto_Statement:
6644 gnu_result
6645 = build1 (GOTO_EXPR, void_type_node, gnat_to_gnu (Name (gnat_node)));
6646 break;
6648 /***************************/
6649 /* Chapter 6: Subprograms */
6650 /***************************/
6652 case N_Subprogram_Declaration:
6653 /* Unless there is a freeze node, declare the subprogram. We consider
6654 this a "definition" even though we're not generating code for
6655 the subprogram because we will be making the corresponding GCC
6656 node here. */
6658 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6659 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
6660 NULL_TREE, 1);
6661 gnu_result = alloc_stmt_list ();
6662 break;
6664 case N_Abstract_Subprogram_Declaration:
6665 /* This subprogram doesn't exist for code generation purposes, but we
6666 have to elaborate the types of any parameters and result, unless
6667 they are imported types (nothing to generate in this case).
6669 The parameter list may contain types with freeze nodes, e.g. not null
6670 subtypes, so the subprogram itself may carry a freeze node, in which
6671 case its elaboration must be deferred. */
6673 /* Process the parameter types first. */
6674 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6675 for (gnat_temp
6676 = First_Formal_With_Extras
6677 (Defining_Entity (Specification (gnat_node)));
6678 Present (gnat_temp);
6679 gnat_temp = Next_Formal_With_Extras (gnat_temp))
6680 if (Is_Itype (Etype (gnat_temp))
6681 && !From_Limited_With (Etype (gnat_temp)))
6682 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
6684 /* Then the result type, set to Standard_Void_Type for procedures. */
6686 Entity_Id gnat_temp_type
6687 = Etype (Defining_Entity (Specification (gnat_node)));
6689 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
6690 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, 0);
6693 gnu_result = alloc_stmt_list ();
6694 break;
6696 case N_Defining_Program_Unit_Name:
6697 /* For a child unit identifier go up a level to get the specification.
6698 We get this when we try to find the spec of a child unit package
6699 that is the compilation unit being compiled. */
6700 gnu_result = gnat_to_gnu (Parent (gnat_node));
6701 break;
6703 case N_Subprogram_Body:
6704 Subprogram_Body_to_gnu (gnat_node);
6705 gnu_result = alloc_stmt_list ();
6706 break;
6708 case N_Function_Call:
6709 case N_Procedure_Call_Statement:
6710 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE, false);
6711 break;
6713 /************************/
6714 /* Chapter 7: Packages */
6715 /************************/
6717 case N_Package_Declaration:
6718 gnu_result = gnat_to_gnu (Specification (gnat_node));
6719 break;
6721 case N_Package_Specification:
6723 start_stmt_group ();
6724 process_decls (Visible_Declarations (gnat_node),
6725 Private_Declarations (gnat_node), Empty, true, true);
6726 gnu_result = end_stmt_group ();
6727 break;
6729 case N_Package_Body:
6731 /* If this is the body of a generic package - do nothing. */
6732 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
6734 gnu_result = alloc_stmt_list ();
6735 break;
6738 start_stmt_group ();
6739 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6741 if (Present (Handled_Statement_Sequence (gnat_node)))
6742 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6744 gnu_result = end_stmt_group ();
6745 break;
6747 /********************************/
6748 /* Chapter 8: Visibility Rules */
6749 /********************************/
6751 case N_Use_Package_Clause:
6752 case N_Use_Type_Clause:
6753 /* Nothing to do here - but these may appear in list of declarations. */
6754 gnu_result = alloc_stmt_list ();
6755 break;
6757 /*********************/
6758 /* Chapter 9: Tasks */
6759 /*********************/
6761 case N_Protected_Type_Declaration:
6762 gnu_result = alloc_stmt_list ();
6763 break;
6765 case N_Single_Task_Declaration:
6766 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
6767 gnu_result = alloc_stmt_list ();
6768 break;
6770 /*********************************************************/
6771 /* Chapter 10: Program Structure and Compilation Issues */
6772 /*********************************************************/
6774 case N_Compilation_Unit:
6775 /* This is not called for the main unit on which gigi is invoked. */
6776 Compilation_Unit_to_gnu (gnat_node);
6777 gnu_result = alloc_stmt_list ();
6778 break;
6780 case N_Subprogram_Body_Stub:
6781 case N_Package_Body_Stub:
6782 case N_Protected_Body_Stub:
6783 case N_Task_Body_Stub:
6784 /* Simply process whatever unit is being inserted. */
6785 if (Present (Library_Unit (gnat_node)))
6786 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
6787 else
6789 gcc_assert (type_annotate_only);
6790 gnu_result = alloc_stmt_list ();
6792 break;
6794 case N_Subunit:
6795 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
6796 break;
6798 /***************************/
6799 /* Chapter 11: Exceptions */
6800 /***************************/
6802 case N_Handled_Sequence_Of_Statements:
6803 /* If there is an At_End procedure attached to this node, and the EH
6804 mechanism is SJLJ, we must have at least a corresponding At_End
6805 handler, unless the No_Exception_Handlers restriction is set. */
6806 gcc_assert (type_annotate_only
6807 || Exception_Mechanism != Setjmp_Longjmp
6808 || No (At_End_Proc (gnat_node))
6809 || Present (Exception_Handlers (gnat_node))
6810 || No_Exception_Handlers_Set ());
6812 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
6813 break;
6815 case N_Exception_Handler:
6816 if (Exception_Mechanism == Setjmp_Longjmp)
6817 gnu_result = Exception_Handler_to_gnu_sjlj (gnat_node);
6818 else if (Exception_Mechanism == Back_End_Exceptions)
6819 gnu_result = Exception_Handler_to_gnu_zcx (gnat_node);
6820 else
6821 gcc_unreachable ();
6822 break;
6824 case N_Raise_Statement:
6825 /* Only for reraise in back-end exceptions mode. */
6826 gcc_assert (No (Name (gnat_node))
6827 && Exception_Mechanism == Back_End_Exceptions);
6829 start_stmt_group ();
6830 gnat_pushlevel ();
6832 /* Clear the current exception pointer so that the occurrence won't be
6833 deallocated. */
6834 gnu_expr = create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE,
6835 ptr_type_node, gnu_incoming_exc_ptr,
6836 false, false, false, false, NULL, gnat_node);
6838 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_incoming_exc_ptr,
6839 convert (ptr_type_node, integer_zero_node)));
6840 add_stmt (build_call_n_expr (reraise_zcx_decl, 1, gnu_expr));
6841 gnat_poplevel ();
6842 gnu_result = end_stmt_group ();
6843 break;
6845 case N_Push_Constraint_Error_Label:
6846 push_exception_label_stack (&gnu_constraint_error_label_stack,
6847 Exception_Label (gnat_node));
6848 break;
6850 case N_Push_Storage_Error_Label:
6851 push_exception_label_stack (&gnu_storage_error_label_stack,
6852 Exception_Label (gnat_node));
6853 break;
6855 case N_Push_Program_Error_Label:
6856 push_exception_label_stack (&gnu_program_error_label_stack,
6857 Exception_Label (gnat_node));
6858 break;
6860 case N_Pop_Constraint_Error_Label:
6861 gnu_constraint_error_label_stack->pop ();
6862 break;
6864 case N_Pop_Storage_Error_Label:
6865 gnu_storage_error_label_stack->pop ();
6866 break;
6868 case N_Pop_Program_Error_Label:
6869 gnu_program_error_label_stack->pop ();
6870 break;
6872 /******************************/
6873 /* Chapter 12: Generic Units */
6874 /******************************/
6876 case N_Generic_Function_Renaming_Declaration:
6877 case N_Generic_Package_Renaming_Declaration:
6878 case N_Generic_Procedure_Renaming_Declaration:
6879 case N_Generic_Package_Declaration:
6880 case N_Generic_Subprogram_Declaration:
6881 case N_Package_Instantiation:
6882 case N_Procedure_Instantiation:
6883 case N_Function_Instantiation:
6884 /* These nodes can appear on a declaration list but there is nothing to
6885 to be done with them. */
6886 gnu_result = alloc_stmt_list ();
6887 break;
6889 /**************************************************/
6890 /* Chapter 13: Representation Clauses and */
6891 /* Implementation-Dependent Features */
6892 /**************************************************/
6894 case N_Attribute_Definition_Clause:
6895 gnu_result = alloc_stmt_list ();
6897 /* The only one we need to deal with is 'Address since, for the others,
6898 the front-end puts the information elsewhere. */
6899 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
6900 break;
6902 /* And we only deal with 'Address if the object has a Freeze node. */
6903 gnat_temp = Entity (Name (gnat_node));
6904 if (No (Freeze_Node (gnat_temp)))
6905 break;
6907 /* Get the value to use as the address and save it as the equivalent
6908 for the object. When it is frozen, gnat_to_gnu_entity will do the
6909 right thing. */
6910 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
6911 break;
6913 case N_Enumeration_Representation_Clause:
6914 case N_Record_Representation_Clause:
6915 case N_At_Clause:
6916 /* We do nothing with these. SEM puts the information elsewhere. */
6917 gnu_result = alloc_stmt_list ();
6918 break;
6920 case N_Code_Statement:
6921 if (!type_annotate_only)
6923 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
6924 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
6925 tree gnu_clobbers = NULL_TREE, tail;
6926 bool allows_mem, allows_reg, fake;
6927 int ninputs, noutputs, i;
6928 const char **oconstraints;
6929 const char *constraint;
6930 char *clobber;
6932 /* First retrieve the 3 operand lists built by the front-end. */
6933 Setup_Asm_Outputs (gnat_node);
6934 while (Present (gnat_temp = Asm_Output_Variable ()))
6936 tree gnu_value = gnat_to_gnu (gnat_temp);
6937 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6938 (Asm_Output_Constraint ()));
6940 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
6941 Next_Asm_Output ();
6944 Setup_Asm_Inputs (gnat_node);
6945 while (Present (gnat_temp = Asm_Input_Value ()))
6947 tree gnu_value = gnat_to_gnu (gnat_temp);
6948 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6949 (Asm_Input_Constraint ()));
6951 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
6952 Next_Asm_Input ();
6955 Clobber_Setup (gnat_node);
6956 while ((clobber = Clobber_Get_Next ()))
6957 gnu_clobbers
6958 = tree_cons (NULL_TREE,
6959 build_string (strlen (clobber) + 1, clobber),
6960 gnu_clobbers);
6962 /* Then perform some standard checking and processing on the
6963 operands. In particular, mark them addressable if needed. */
6964 gnu_outputs = nreverse (gnu_outputs);
6965 noutputs = list_length (gnu_outputs);
6966 gnu_inputs = nreverse (gnu_inputs);
6967 ninputs = list_length (gnu_inputs);
6968 oconstraints = XALLOCAVEC (const char *, noutputs);
6970 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
6972 tree output = TREE_VALUE (tail);
6973 constraint
6974 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6975 oconstraints[i] = constraint;
6977 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6978 &allows_mem, &allows_reg, &fake))
6980 /* If the operand is going to end up in memory,
6981 mark it addressable. Note that we don't test
6982 allows_mem like in the input case below; this
6983 is modelled on the C front-end. */
6984 if (!allows_reg)
6986 output = remove_conversions (output, false);
6987 if (TREE_CODE (output) == CONST_DECL
6988 && DECL_CONST_CORRESPONDING_VAR (output))
6989 output = DECL_CONST_CORRESPONDING_VAR (output);
6990 if (!gnat_mark_addressable (output))
6991 output = error_mark_node;
6994 else
6995 output = error_mark_node;
6997 TREE_VALUE (tail) = output;
7000 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
7002 tree input = TREE_VALUE (tail);
7003 constraint
7004 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7006 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
7007 0, oconstraints,
7008 &allows_mem, &allows_reg))
7010 /* If the operand is going to end up in memory,
7011 mark it addressable. */
7012 if (!allows_reg && allows_mem)
7014 input = remove_conversions (input, false);
7015 if (TREE_CODE (input) == CONST_DECL
7016 && DECL_CONST_CORRESPONDING_VAR (input))
7017 input = DECL_CONST_CORRESPONDING_VAR (input);
7018 if (!gnat_mark_addressable (input))
7019 input = error_mark_node;
7022 else
7023 input = error_mark_node;
7025 TREE_VALUE (tail) = input;
7028 gnu_result = build5 (ASM_EXPR, void_type_node,
7029 gnu_template, gnu_outputs,
7030 gnu_inputs, gnu_clobbers, NULL_TREE);
7031 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
7033 else
7034 gnu_result = alloc_stmt_list ();
7036 break;
7038 /****************/
7039 /* Added Nodes */
7040 /****************/
7042 case N_Expression_With_Actions:
7043 /* This construct doesn't define a scope so we don't push a binding level
7044 around the statement list; but we wrap it in a SAVE_EXPR to protect it
7045 from unsharing. */
7046 gnu_result = build_stmt_group (Actions (gnat_node), false);
7047 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
7048 TREE_SIDE_EFFECTS (gnu_result) = 1;
7049 gnu_expr = gnat_to_gnu (Expression (gnat_node));
7050 gnu_result
7051 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
7052 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7053 break;
7055 case N_Freeze_Entity:
7056 start_stmt_group ();
7057 process_freeze_entity (gnat_node);
7058 process_decls (Actions (gnat_node), Empty, Empty, true, true);
7059 gnu_result = end_stmt_group ();
7060 break;
7062 case N_Freeze_Generic_Entity:
7063 gnu_result = alloc_stmt_list ();
7064 break;
7066 case N_Itype_Reference:
7067 if (!present_gnu_tree (Itype (gnat_node)))
7068 process_type (Itype (gnat_node));
7070 gnu_result = alloc_stmt_list ();
7071 break;
7073 case N_Free_Statement:
7074 if (!type_annotate_only)
7076 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
7077 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
7078 tree gnu_obj_type, gnu_actual_obj_type;
7080 /* If this is a thin pointer, we must first dereference it to create
7081 a fat pointer, then go back below to a thin pointer. The reason
7082 for this is that we need to have a fat pointer someplace in order
7083 to properly compute the size. */
7084 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7085 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
7086 build_unary_op (INDIRECT_REF, NULL_TREE,
7087 gnu_ptr));
7089 /* If this is a fat pointer, the object must have been allocated with
7090 the template in front of the array. So pass the template address,
7091 and get the total size; do it by converting to a thin pointer. */
7092 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
7093 gnu_ptr
7094 = convert (build_pointer_type
7095 (TYPE_OBJECT_RECORD_TYPE
7096 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
7097 gnu_ptr);
7099 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
7101 /* If this is a thin pointer, the object must have been allocated with
7102 the template in front of the array. So pass the template address,
7103 and get the total size. */
7104 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7105 gnu_ptr
7106 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
7107 gnu_ptr,
7108 fold_build1 (NEGATE_EXPR, sizetype,
7109 byte_position
7110 (DECL_CHAIN
7111 TYPE_FIELDS ((gnu_obj_type)))));
7113 /* If we have a special dynamic constrained subtype on the node, use
7114 it to compute the size; otherwise, use the designated subtype. */
7115 if (Present (Actual_Designated_Subtype (gnat_node)))
7117 gnu_actual_obj_type
7118 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
7120 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
7121 gnu_actual_obj_type
7122 = build_unc_object_type_from_ptr (gnu_ptr_type,
7123 gnu_actual_obj_type,
7124 get_identifier ("DEALLOC"),
7125 false);
7127 else
7128 gnu_actual_obj_type = gnu_obj_type;
7130 gnu_result
7131 = build_call_alloc_dealloc (gnu_ptr,
7132 TYPE_SIZE_UNIT (gnu_actual_obj_type),
7133 gnu_obj_type,
7134 Procedure_To_Call (gnat_node),
7135 Storage_Pool (gnat_node),
7136 gnat_node);
7138 break;
7140 case N_Raise_Constraint_Error:
7141 case N_Raise_Program_Error:
7142 case N_Raise_Storage_Error:
7143 if (type_annotate_only)
7144 gnu_result = alloc_stmt_list ();
7145 else
7146 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
7147 break;
7149 case N_Validate_Unchecked_Conversion:
7150 /* The only validation we currently do on an unchecked conversion is
7151 that of aliasing assumptions. */
7152 if (flag_strict_aliasing)
7153 gnat_validate_uc_list.safe_push (gnat_node);
7154 gnu_result = alloc_stmt_list ();
7155 break;
7157 case N_Function_Specification:
7158 case N_Procedure_Specification:
7159 case N_Op_Concat:
7160 case N_Component_Association:
7161 case N_Protected_Body:
7162 case N_Task_Body:
7163 /* These nodes should only be present when annotating types. */
7164 gcc_assert (type_annotate_only);
7165 gnu_result = alloc_stmt_list ();
7166 break;
7168 default:
7169 /* Other nodes are not supposed to reach here. */
7170 gcc_unreachable ();
7173 /* If we pushed the processing of the elaboration routine, pop it back. */
7174 if (went_into_elab_proc)
7175 current_function_decl = NULL_TREE;
7177 /* When not optimizing, turn boolean rvalues B into B != false tests
7178 so that the code just below can put the location information of the
7179 reference to B on the inequality operator for better debug info. */
7180 if (!optimize
7181 && TREE_CODE (gnu_result) != INTEGER_CST
7182 && (kind == N_Identifier
7183 || kind == N_Expanded_Name
7184 || kind == N_Explicit_Dereference
7185 || kind == N_Function_Call
7186 || kind == N_Indexed_Component
7187 || kind == N_Selected_Component)
7188 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
7189 && !lvalue_required_p (gnat_node, gnu_result_type, false, false, false))
7190 gnu_result = build_binary_op (NE_EXPR, gnu_result_type,
7191 convert (gnu_result_type, gnu_result),
7192 convert (gnu_result_type,
7193 boolean_false_node));
7195 /* Set the location information on the result. Note that we may have
7196 no result if we tried to build a CALL_EXPR node to a procedure with
7197 no side-effects and optimization is enabled. */
7198 if (gnu_result && EXPR_P (gnu_result))
7199 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7201 /* If we're supposed to return something of void_type, it means we have
7202 something we're elaborating for effect, so just return. */
7203 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
7204 return gnu_result;
7206 /* If the result is a constant that overflowed, raise Constraint_Error. */
7207 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
7209 post_error ("?`Constraint_Error` will be raised at run time", gnat_node);
7210 gnu_result
7211 = build1 (NULL_EXPR, gnu_result_type,
7212 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
7213 N_Raise_Constraint_Error));
7216 /* If the result has side-effects and is of an unconstrained type, make a
7217 SAVE_EXPR so that we can be sure it will only be referenced once. But
7218 this is useless for a call to a function that returns an unconstrained
7219 type with default discriminant, as we cannot compute the size of the
7220 actual returned object. We must do this before any conversions. */
7221 if (TREE_SIDE_EFFECTS (gnu_result)
7222 && !(TREE_CODE (gnu_result) == CALL_EXPR
7223 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7224 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
7225 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))))
7226 gnu_result = gnat_stabilize_reference (gnu_result, false, NULL);
7228 /* Now convert the result to the result type, unless we are in one of the
7229 following cases:
7231 1. If this is the LHS of an assignment or an actual parameter of a
7232 call, return the result almost unmodified since the RHS will have
7233 to be converted to our type in that case, unless the result type
7234 has a simpler size. Likewise if there is just a no-op unchecked
7235 conversion in-between. Similarly, don't convert integral types
7236 that are the operands of an unchecked conversion since we need
7237 to ignore those conversions (for 'Valid).
7239 2. If we have a label (which doesn't have any well-defined type), a
7240 field or an error, return the result almost unmodified. Similarly,
7241 if the two types are record types with the same name, don't convert.
7242 This will be the case when we are converting from a packable version
7243 of a type to its original type and we need those conversions to be
7244 NOPs in order for assignments into these types to work properly.
7246 3. If the type is void or if we have no result, return error_mark_node
7247 to show we have no result.
7249 4. If this a call to a function that returns an unconstrained type with
7250 default discriminant, return the call expression unmodified since we
7251 cannot compute the size of the actual returned object.
7253 5. Finally, if the type of the result is already correct. */
7255 if (Present (Parent (gnat_node))
7256 && (lhs_or_actual_p (gnat_node)
7257 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7258 && unchecked_conversion_nop (Parent (gnat_node)))
7259 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7260 && !AGGREGATE_TYPE_P (gnu_result_type)
7261 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
7262 && !(TYPE_SIZE (gnu_result_type)
7263 && TYPE_SIZE (TREE_TYPE (gnu_result))
7264 && (AGGREGATE_TYPE_P (gnu_result_type)
7265 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
7266 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
7267 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
7268 != INTEGER_CST))
7269 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
7270 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
7271 && (CONTAINS_PLACEHOLDER_P
7272 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
7273 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
7274 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
7276 /* Remove padding only if the inner object is of self-referential
7277 size: in that case it must be an object of unconstrained type
7278 with a default discriminant and we want to avoid copying too
7279 much data. */
7280 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7281 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
7282 (TREE_TYPE (gnu_result))))))
7283 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7284 gnu_result);
7287 else if (TREE_CODE (gnu_result) == LABEL_DECL
7288 || TREE_CODE (gnu_result) == FIELD_DECL
7289 || TREE_CODE (gnu_result) == ERROR_MARK
7290 || (TYPE_NAME (gnu_result_type)
7291 == TYPE_NAME (TREE_TYPE (gnu_result))
7292 && TREE_CODE (gnu_result_type) == RECORD_TYPE
7293 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
7295 /* Remove any padding. */
7296 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7297 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7298 gnu_result);
7301 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
7302 gnu_result = error_mark_node;
7304 else if (TREE_CODE (gnu_result) == CALL_EXPR
7305 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7306 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result)))
7307 == gnu_result_type
7308 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
7311 else if (TREE_TYPE (gnu_result) != gnu_result_type)
7312 gnu_result = convert (gnu_result_type, gnu_result);
7314 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
7315 while ((TREE_CODE (gnu_result) == NOP_EXPR
7316 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
7317 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
7318 gnu_result = TREE_OPERAND (gnu_result, 0);
7320 return gnu_result;
7323 /* Subroutine of above to push the exception label stack. GNU_STACK is
7324 a pointer to the stack to update and GNAT_LABEL, if present, is the
7325 label to push onto the stack. */
7327 static void
7328 push_exception_label_stack (vec<tree, va_gc> **gnu_stack, Entity_Id gnat_label)
7330 tree gnu_label = (Present (gnat_label)
7331 ? gnat_to_gnu_entity (gnat_label, NULL_TREE, 0)
7332 : NULL_TREE);
7334 vec_safe_push (*gnu_stack, gnu_label);
7337 /* Record the current code position in GNAT_NODE. */
7339 static void
7340 record_code_position (Node_Id gnat_node)
7342 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
7344 add_stmt_with_node (stmt_stmt, gnat_node);
7345 save_gnu_tree (gnat_node, stmt_stmt, true);
7348 /* Insert the code for GNAT_NODE at the position saved for that node. */
7350 static void
7351 insert_code_for (Node_Id gnat_node)
7353 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
7354 save_gnu_tree (gnat_node, NULL_TREE, true);
7357 /* Start a new statement group chained to the previous group. */
7359 void
7360 start_stmt_group (void)
7362 struct stmt_group *group = stmt_group_free_list;
7364 /* First see if we can get one from the free list. */
7365 if (group)
7366 stmt_group_free_list = group->previous;
7367 else
7368 group = ggc_alloc_stmt_group ();
7370 group->previous = current_stmt_group;
7371 group->stmt_list = group->block = group->cleanups = NULL_TREE;
7372 current_stmt_group = group;
7375 /* Add GNU_STMT to the current statement group. If it is an expression with
7376 no effects, it is ignored. */
7378 void
7379 add_stmt (tree gnu_stmt)
7381 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
7384 /* Similar, but the statement is always added, regardless of side-effects. */
7386 void
7387 add_stmt_force (tree gnu_stmt)
7389 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
7392 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
7394 void
7395 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
7397 if (Present (gnat_node))
7398 set_expr_location_from_node (gnu_stmt, gnat_node);
7399 add_stmt (gnu_stmt);
7402 /* Similar, but the statement is always added, regardless of side-effects. */
7404 void
7405 add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
7407 if (Present (gnat_node))
7408 set_expr_location_from_node (gnu_stmt, gnat_node);
7409 add_stmt_force (gnu_stmt);
7412 /* Add a declaration statement for GNU_DECL to the current statement group.
7413 Get SLOC from Entity_Id. */
7415 void
7416 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
7418 tree type = TREE_TYPE (gnu_decl);
7419 tree gnu_stmt, gnu_init, t;
7421 /* If this is a variable that Gigi is to ignore, we may have been given
7422 an ERROR_MARK. So test for it. We also might have been given a
7423 reference for a renaming. So only do something for a decl. Also
7424 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
7425 if (!DECL_P (gnu_decl)
7426 || (TREE_CODE (gnu_decl) == TYPE_DECL
7427 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
7428 return;
7430 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
7432 /* If we are external or global, we don't want to output the DECL_EXPR for
7433 this DECL node since we already have evaluated the expressions in the
7434 sizes and positions as globals and doing it again would be wrong. */
7435 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
7437 /* Mark everything as used to prevent node sharing with subprograms.
7438 Note that walk_tree knows how to deal with TYPE_DECL, but neither
7439 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
7440 MARK_VISITED (gnu_stmt);
7441 if (TREE_CODE (gnu_decl) == VAR_DECL
7442 || TREE_CODE (gnu_decl) == CONST_DECL)
7444 MARK_VISITED (DECL_SIZE (gnu_decl));
7445 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
7446 MARK_VISITED (DECL_INITIAL (gnu_decl));
7448 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
7449 else if (TREE_CODE (gnu_decl) == TYPE_DECL
7450 && RECORD_OR_UNION_TYPE_P (type)
7451 && !TYPE_FAT_POINTER_P (type))
7452 MARK_VISITED (TYPE_ADA_SIZE (type));
7454 else
7455 add_stmt_with_node (gnu_stmt, gnat_entity);
7457 /* If this is a variable and an initializer is attached to it, it must be
7458 valid for the context. Similar to init_const in create_var_decl_1. */
7459 if (TREE_CODE (gnu_decl) == VAR_DECL
7460 && (gnu_init = DECL_INITIAL (gnu_decl)) != NULL_TREE
7461 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
7462 || (TREE_STATIC (gnu_decl)
7463 && !initializer_constant_valid_p (gnu_init,
7464 TREE_TYPE (gnu_init)))))
7466 /* If GNU_DECL has a padded type, convert it to the unpadded
7467 type so the assignment is done properly. */
7468 if (TYPE_IS_PADDING_P (type))
7469 t = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
7470 else
7471 t = gnu_decl;
7473 gnu_stmt = build_binary_op (INIT_EXPR, NULL_TREE, t, gnu_init);
7475 DECL_INITIAL (gnu_decl) = NULL_TREE;
7476 if (TREE_READONLY (gnu_decl))
7478 TREE_READONLY (gnu_decl) = 0;
7479 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
7482 add_stmt_with_node (gnu_stmt, gnat_entity);
7486 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
7488 static tree
7489 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
7491 tree t = *tp;
7493 if (TREE_VISITED (t))
7494 *walk_subtrees = 0;
7496 /* Don't mark a dummy type as visited because we want to mark its sizes
7497 and fields once it's filled in. */
7498 else if (!TYPE_IS_DUMMY_P (t))
7499 TREE_VISITED (t) = 1;
7501 if (TYPE_P (t))
7502 TYPE_SIZES_GIMPLIFIED (t) = 1;
7504 return NULL_TREE;
7507 /* Mark nodes rooted at T with TREE_VISITED and types as having their
7508 sized gimplified. We use this to indicate all variable sizes and
7509 positions in global types may not be shared by any subprogram. */
7511 void
7512 mark_visited (tree t)
7514 walk_tree (&t, mark_visited_r, NULL, NULL);
7517 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
7518 set its location to that of GNAT_NODE if present, but with column info
7519 cleared so that conditional branches generated as part of the cleanup
7520 code do not interfere with coverage analysis tools. */
7522 static void
7523 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
7525 if (Present (gnat_node))
7526 set_expr_location_from_node1 (gnu_cleanup, gnat_node, true);
7527 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
7530 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
7532 void
7533 set_block_for_group (tree gnu_block)
7535 gcc_assert (!current_stmt_group->block);
7536 current_stmt_group->block = gnu_block;
7539 /* Return code corresponding to the current code group. It is normally
7540 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
7541 BLOCK or cleanups were set. */
7543 tree
7544 end_stmt_group (void)
7546 struct stmt_group *group = current_stmt_group;
7547 tree gnu_retval = group->stmt_list;
7549 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
7550 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
7551 make a BIND_EXPR. Note that we nest in that because the cleanup may
7552 reference variables in the block. */
7553 if (gnu_retval == NULL_TREE)
7554 gnu_retval = alloc_stmt_list ();
7556 if (group->cleanups)
7557 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
7558 group->cleanups);
7560 if (current_stmt_group->block)
7561 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
7562 gnu_retval, group->block);
7564 /* Remove this group from the stack and add it to the free list. */
7565 current_stmt_group = group->previous;
7566 group->previous = stmt_group_free_list;
7567 stmt_group_free_list = group;
7569 return gnu_retval;
7572 /* Return whether the current statement group may fall through. */
7574 static inline bool
7575 stmt_group_may_fallthru (void)
7577 if (current_stmt_group->stmt_list)
7578 return block_may_fallthru (current_stmt_group->stmt_list);
7579 else
7580 return true;
7583 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
7584 statements.*/
7586 static void
7587 add_stmt_list (List_Id gnat_list)
7589 Node_Id gnat_node;
7591 if (Present (gnat_list))
7592 for (gnat_node = First (gnat_list); Present (gnat_node);
7593 gnat_node = Next (gnat_node))
7594 add_stmt (gnat_to_gnu (gnat_node));
7597 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
7598 If BINDING_P is true, push and pop a binding level around the list. */
7600 static tree
7601 build_stmt_group (List_Id gnat_list, bool binding_p)
7603 start_stmt_group ();
7604 if (binding_p)
7605 gnat_pushlevel ();
7607 add_stmt_list (gnat_list);
7608 if (binding_p)
7609 gnat_poplevel ();
7611 return end_stmt_group ();
7614 /* Generate GIMPLE in place for the expression at *EXPR_P. */
7617 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
7618 gimple_seq *post_p ATTRIBUTE_UNUSED)
7620 tree expr = *expr_p;
7621 tree op;
7623 if (IS_ADA_STMT (expr))
7624 return gnat_gimplify_stmt (expr_p);
7626 switch (TREE_CODE (expr))
7628 case NULL_EXPR:
7629 /* If this is for a scalar, just make a VAR_DECL for it. If for
7630 an aggregate, get a null pointer of the appropriate type and
7631 dereference it. */
7632 if (AGGREGATE_TYPE_P (TREE_TYPE (expr)))
7633 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (expr),
7634 convert (build_pointer_type (TREE_TYPE (expr)),
7635 integer_zero_node));
7636 else
7638 *expr_p = create_tmp_var (TREE_TYPE (expr), NULL);
7639 TREE_NO_WARNING (*expr_p) = 1;
7642 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
7643 return GS_OK;
7645 case UNCONSTRAINED_ARRAY_REF:
7646 /* We should only do this if we are just elaborating for side-effects,
7647 but we can't know that yet. */
7648 *expr_p = TREE_OPERAND (*expr_p, 0);
7649 return GS_OK;
7651 case ADDR_EXPR:
7652 op = TREE_OPERAND (expr, 0);
7654 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
7655 is put into static memory. We know that it's going to be read-only
7656 given the semantics we have and it must be in static memory when the
7657 reference is in an elaboration procedure. */
7658 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
7660 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
7661 *expr_p = fold_convert (TREE_TYPE (expr), addr);
7662 return GS_ALL_DONE;
7665 return GS_UNHANDLED;
7667 case VIEW_CONVERT_EXPR:
7668 op = TREE_OPERAND (expr, 0);
7670 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
7671 type to a scalar one, explicitly create the local temporary. That's
7672 required if the type is passed by reference. */
7673 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
7674 && AGGREGATE_TYPE_P (TREE_TYPE (op))
7675 && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
7677 tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
7678 gimple_add_tmp_var (new_var);
7680 mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
7681 gimplify_and_add (mod, pre_p);
7683 TREE_OPERAND (expr, 0) = new_var;
7684 return GS_OK;
7687 return GS_UNHANDLED;
7689 case DECL_EXPR:
7690 op = DECL_EXPR_DECL (expr);
7692 /* The expressions for the RM bounds must be gimplified to ensure that
7693 they are properly elaborated. See gimplify_decl_expr. */
7694 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
7695 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)))
7696 switch (TREE_CODE (TREE_TYPE (op)))
7698 case INTEGER_TYPE:
7699 case ENUMERAL_TYPE:
7700 case BOOLEAN_TYPE:
7701 case REAL_TYPE:
7703 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
7705 val = TYPE_RM_MIN_VALUE (type);
7706 if (val)
7708 gimplify_one_sizepos (&val, pre_p);
7709 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7710 SET_TYPE_RM_MIN_VALUE (t, val);
7713 val = TYPE_RM_MAX_VALUE (type);
7714 if (val)
7716 gimplify_one_sizepos (&val, pre_p);
7717 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7718 SET_TYPE_RM_MAX_VALUE (t, val);
7722 break;
7724 default:
7725 break;
7728 /* ... fall through ... */
7730 default:
7731 return GS_UNHANDLED;
7735 /* Generate GIMPLE in place for the statement at *STMT_P. */
7737 static enum gimplify_status
7738 gnat_gimplify_stmt (tree *stmt_p)
7740 tree stmt = *stmt_p;
7742 switch (TREE_CODE (stmt))
7744 case STMT_STMT:
7745 *stmt_p = STMT_STMT_STMT (stmt);
7746 return GS_OK;
7748 case LOOP_STMT:
7750 tree gnu_start_label = create_artificial_label (input_location);
7751 tree gnu_cond = LOOP_STMT_COND (stmt);
7752 tree gnu_update = LOOP_STMT_UPDATE (stmt);
7753 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
7755 /* Build the condition expression from the test, if any. */
7756 if (gnu_cond)
7758 /* Deal with the optimization hints. */
7759 if (LOOP_STMT_IVDEP (stmt))
7760 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7761 build_int_cst (integer_type_node,
7762 annot_expr_ivdep_kind));
7764 if (LOOP_STMT_NO_VECTOR (stmt))
7765 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7766 build_int_cst (integer_type_node,
7767 annot_expr_no_vector_kind));
7768 if (LOOP_STMT_VECTOR (stmt))
7769 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7770 build_int_cst (integer_type_node,
7771 annot_expr_vector_kind));
7773 gnu_cond
7774 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
7775 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
7778 /* Set to emit the statements of the loop. */
7779 *stmt_p = NULL_TREE;
7781 /* We first emit the start label and then a conditional jump to the
7782 end label if there's a top condition, then the update if it's at
7783 the top, then the body of the loop, then a conditional jump to
7784 the end label if there's a bottom condition, then the update if
7785 it's at the bottom, and finally a jump to the start label and the
7786 definition of the end label. */
7787 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7788 gnu_start_label),
7789 stmt_p);
7791 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
7792 append_to_statement_list (gnu_cond, stmt_p);
7794 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
7795 append_to_statement_list (gnu_update, stmt_p);
7797 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
7799 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
7800 append_to_statement_list (gnu_cond, stmt_p);
7802 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
7803 append_to_statement_list (gnu_update, stmt_p);
7805 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
7806 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
7807 append_to_statement_list (t, stmt_p);
7809 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7810 gnu_end_label),
7811 stmt_p);
7812 return GS_OK;
7815 case EXIT_STMT:
7816 /* Build a statement to jump to the corresponding end label, then
7817 see if it needs to be conditional. */
7818 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
7819 if (EXIT_STMT_COND (stmt))
7820 *stmt_p = build3 (COND_EXPR, void_type_node,
7821 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
7822 return GS_OK;
7824 default:
7825 gcc_unreachable ();
7829 /* Force references to each of the entities in packages withed by GNAT_NODE.
7830 Operate recursively but check that we aren't elaborating something more
7831 than once.
7833 This routine is exclusively called in type_annotate mode, to compute DDA
7834 information for types in withed units, for ASIS use. */
7836 static void
7837 elaborate_all_entities (Node_Id gnat_node)
7839 Entity_Id gnat_with_clause, gnat_entity;
7841 /* Process each unit only once. As we trace the context of all relevant
7842 units transitively, including generic bodies, we may encounter the
7843 same generic unit repeatedly. */
7844 if (!present_gnu_tree (gnat_node))
7845 save_gnu_tree (gnat_node, integer_zero_node, true);
7847 /* Save entities in all context units. A body may have an implicit_with
7848 on its own spec, if the context includes a child unit, so don't save
7849 the spec twice. */
7850 for (gnat_with_clause = First (Context_Items (gnat_node));
7851 Present (gnat_with_clause);
7852 gnat_with_clause = Next (gnat_with_clause))
7853 if (Nkind (gnat_with_clause) == N_With_Clause
7854 && !present_gnu_tree (Library_Unit (gnat_with_clause))
7855 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
7857 elaborate_all_entities (Library_Unit (gnat_with_clause));
7859 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
7861 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
7862 Present (gnat_entity);
7863 gnat_entity = Next_Entity (gnat_entity))
7864 if (Is_Public (gnat_entity)
7865 && Convention (gnat_entity) != Convention_Intrinsic
7866 && Ekind (gnat_entity) != E_Package
7867 && Ekind (gnat_entity) != E_Package_Body
7868 && Ekind (gnat_entity) != E_Operator
7869 && !(IN (Ekind (gnat_entity), Type_Kind)
7870 && !Is_Frozen (gnat_entity))
7871 && !((Ekind (gnat_entity) == E_Procedure
7872 || Ekind (gnat_entity) == E_Function)
7873 && Is_Intrinsic_Subprogram (gnat_entity))
7874 && !IN (Ekind (gnat_entity), Named_Kind)
7875 && !IN (Ekind (gnat_entity), Generic_Unit_Kind))
7876 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
7878 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
7880 Node_Id gnat_body
7881 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
7883 /* Retrieve compilation unit node of generic body. */
7884 while (Present (gnat_body)
7885 && Nkind (gnat_body) != N_Compilation_Unit)
7886 gnat_body = Parent (gnat_body);
7888 /* If body is available, elaborate its context. */
7889 if (Present (gnat_body))
7890 elaborate_all_entities (gnat_body);
7894 if (Nkind (Unit (gnat_node)) == N_Package_Body)
7895 elaborate_all_entities (Library_Unit (gnat_node));
7898 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
7900 static void
7901 process_freeze_entity (Node_Id gnat_node)
7903 const Entity_Id gnat_entity = Entity (gnat_node);
7904 const Entity_Kind kind = Ekind (gnat_entity);
7905 tree gnu_old, gnu_new;
7907 /* If this is a package, we need to generate code for the package. */
7908 if (kind == E_Package)
7910 insert_code_for
7911 (Parent (Corresponding_Body
7912 (Parent (Declaration_Node (gnat_entity)))));
7913 return;
7916 /* Don't do anything for class-wide types as they are always transformed
7917 into their root type. */
7918 if (kind == E_Class_Wide_Type)
7919 return;
7921 /* Check for an old definition. This freeze node might be for an Itype. */
7922 gnu_old
7923 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
7925 /* If this entity has an address representation clause, GNU_OLD is the
7926 address, so discard it here. */
7927 if (Present (Address_Clause (gnat_entity)))
7928 gnu_old = NULL_TREE;
7930 /* Don't do anything for subprograms that may have been elaborated before
7931 their freeze nodes. This can happen, for example, because of an inner
7932 call in an instance body or because of previous compilation of a spec
7933 for inlining purposes. */
7934 if (gnu_old
7935 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
7936 && (kind == E_Function || kind == E_Procedure))
7937 || (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
7938 && kind == E_Subprogram_Type)))
7939 return;
7941 /* If we have a non-dummy type old tree, we have nothing to do, except
7942 aborting if this is the public view of a private type whose full view was
7943 not delayed, as this node was never delayed as it should have been. We
7944 let this happen for concurrent types and their Corresponding_Record_Type,
7945 however, because each might legitimately be elaborated before its own
7946 freeze node, e.g. while processing the other. */
7947 if (gnu_old
7948 && !(TREE_CODE (gnu_old) == TYPE_DECL
7949 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
7951 gcc_assert ((IN (kind, Incomplete_Or_Private_Kind)
7952 && Present (Full_View (gnat_entity))
7953 && No (Freeze_Node (Full_View (gnat_entity))))
7954 || Is_Concurrent_Type (gnat_entity)
7955 || (IN (kind, Record_Kind)
7956 && Is_Concurrent_Record_Type (gnat_entity)));
7957 return;
7960 /* Reset the saved tree, if any, and elaborate the object or type for real.
7961 If there is a full view, elaborate it and use the result. And, if this
7962 is the root type of a class-wide type, reuse it for the latter. */
7963 if (gnu_old)
7965 save_gnu_tree (gnat_entity, NULL_TREE, false);
7966 if (IN (kind, Incomplete_Or_Private_Kind)
7967 && Present (Full_View (gnat_entity))
7968 && present_gnu_tree (Full_View (gnat_entity)))
7969 save_gnu_tree (Full_View (gnat_entity), NULL_TREE, false);
7970 if (IN (kind, Type_Kind)
7971 && Present (Class_Wide_Type (gnat_entity))
7972 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
7973 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
7976 if (IN (kind, Incomplete_Or_Private_Kind)
7977 && Present (Full_View (gnat_entity)))
7979 gnu_new = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 1);
7981 /* Propagate back-annotations from full view to partial view. */
7982 if (Unknown_Alignment (gnat_entity))
7983 Set_Alignment (gnat_entity, Alignment (Full_View (gnat_entity)));
7985 if (Unknown_Esize (gnat_entity))
7986 Set_Esize (gnat_entity, Esize (Full_View (gnat_entity)));
7988 if (Unknown_RM_Size (gnat_entity))
7989 Set_RM_Size (gnat_entity, RM_Size (Full_View (gnat_entity)));
7991 /* The above call may have defined this entity (the simplest example
7992 of this is when we have a private enumeral type since the bounds
7993 will have the public view). */
7994 if (!present_gnu_tree (gnat_entity))
7995 save_gnu_tree (gnat_entity, gnu_new, false);
7997 else
7999 tree gnu_init
8000 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
8001 && present_gnu_tree (Declaration_Node (gnat_entity)))
8002 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
8004 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
8007 if (IN (kind, Type_Kind)
8008 && Present (Class_Wide_Type (gnat_entity))
8009 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8010 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
8012 /* If we have an old type and we've made pointers to this type, update those
8013 pointers. If this is a Taft amendment type in the main unit, we need to
8014 mark the type as used since other units referencing it don't see the full
8015 declaration and, therefore, cannot mark it as used themselves. */
8016 if (gnu_old)
8018 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8019 TREE_TYPE (gnu_new));
8020 if (DECL_TAFT_TYPE_P (gnu_old))
8021 used_types_insert (TREE_TYPE (gnu_new));
8025 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
8026 We make two passes, one to elaborate anything other than bodies (but
8027 we declare a function if there was no spec). The second pass
8028 elaborates the bodies.
8030 GNAT_END_LIST gives the element in the list past the end. Normally,
8031 this is Empty, but can be First_Real_Statement for a
8032 Handled_Sequence_Of_Statements.
8034 We make a complete pass through both lists if PASS1P is true, then make
8035 the second pass over both lists if PASS2P is true. The lists usually
8036 correspond to the public and private parts of a package. */
8038 static void
8039 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
8040 Node_Id gnat_end_list, bool pass1p, bool pass2p)
8042 List_Id gnat_decl_array[2];
8043 Node_Id gnat_decl;
8044 int i;
8046 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
8048 if (pass1p)
8049 for (i = 0; i <= 1; i++)
8050 if (Present (gnat_decl_array[i]))
8051 for (gnat_decl = First (gnat_decl_array[i]);
8052 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8054 /* For package specs, we recurse inside the declarations,
8055 thus taking the two pass approach inside the boundary. */
8056 if (Nkind (gnat_decl) == N_Package_Declaration
8057 && (Nkind (Specification (gnat_decl)
8058 == N_Package_Specification)))
8059 process_decls (Visible_Declarations (Specification (gnat_decl)),
8060 Private_Declarations (Specification (gnat_decl)),
8061 Empty, true, false);
8063 /* Similarly for any declarations in the actions of a
8064 freeze node. */
8065 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8067 process_freeze_entity (gnat_decl);
8068 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
8071 /* Package bodies with freeze nodes get their elaboration deferred
8072 until the freeze node, but the code must be placed in the right
8073 place, so record the code position now. */
8074 else if (Nkind (gnat_decl) == N_Package_Body
8075 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
8076 record_code_position (gnat_decl);
8078 else if (Nkind (gnat_decl) == N_Package_Body_Stub
8079 && Present (Library_Unit (gnat_decl))
8080 && Present (Freeze_Node
8081 (Corresponding_Spec
8082 (Proper_Body (Unit
8083 (Library_Unit (gnat_decl)))))))
8084 record_code_position
8085 (Proper_Body (Unit (Library_Unit (gnat_decl))));
8087 /* We defer most subprogram bodies to the second pass. */
8088 else if (Nkind (gnat_decl) == N_Subprogram_Body)
8090 if (Acts_As_Spec (gnat_decl))
8092 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
8094 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
8095 && Ekind (gnat_subprog_id) != E_Generic_Function)
8096 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8100 /* For bodies and stubs that act as their own specs, the entity
8101 itself must be elaborated in the first pass, because it may
8102 be used in other declarations. */
8103 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
8105 Node_Id gnat_subprog_id
8106 = Defining_Entity (Specification (gnat_decl));
8108 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
8109 && Ekind (gnat_subprog_id) != E_Generic_Procedure
8110 && Ekind (gnat_subprog_id) != E_Generic_Function)
8111 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8114 /* Concurrent stubs stand for the corresponding subprogram bodies,
8115 which are deferred like other bodies. */
8116 else if (Nkind (gnat_decl) == N_Task_Body_Stub
8117 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8120 else
8121 add_stmt (gnat_to_gnu (gnat_decl));
8124 /* Here we elaborate everything we deferred above except for package bodies,
8125 which are elaborated at their freeze nodes. Note that we must also
8126 go inside things (package specs and freeze nodes) the first pass did. */
8127 if (pass2p)
8128 for (i = 0; i <= 1; i++)
8129 if (Present (gnat_decl_array[i]))
8130 for (gnat_decl = First (gnat_decl_array[i]);
8131 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8133 if (Nkind (gnat_decl) == N_Subprogram_Body
8134 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
8135 || Nkind (gnat_decl) == N_Task_Body_Stub
8136 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8137 add_stmt (gnat_to_gnu (gnat_decl));
8139 else if (Nkind (gnat_decl) == N_Package_Declaration
8140 && (Nkind (Specification (gnat_decl)
8141 == N_Package_Specification)))
8142 process_decls (Visible_Declarations (Specification (gnat_decl)),
8143 Private_Declarations (Specification (gnat_decl)),
8144 Empty, false, true);
8146 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8147 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
8151 /* Make a unary operation of kind CODE using build_unary_op, but guard
8152 the operation by an overflow check. CODE can be one of NEGATE_EXPR
8153 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
8154 the operation is to be performed in that type. GNAT_NODE is the gnat
8155 node conveying the source location for which the error should be
8156 signaled. */
8158 static tree
8159 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
8160 Node_Id gnat_node)
8162 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
8164 operand = gnat_protect_expr (operand);
8166 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
8167 operand, TYPE_MIN_VALUE (gnu_type)),
8168 build_unary_op (code, gnu_type, operand),
8169 CE_Overflow_Check_Failed, gnat_node);
8172 /* Make a binary operation of kind CODE using build_binary_op, but guard
8173 the operation by an overflow check. CODE can be one of PLUS_EXPR,
8174 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
8175 Usually the operation is to be performed in that type. GNAT_NODE is
8176 the GNAT node conveying the source location for which the error should
8177 be signaled. */
8179 static tree
8180 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
8181 tree right, Node_Id gnat_node)
8183 tree lhs = gnat_protect_expr (left);
8184 tree rhs = gnat_protect_expr (right);
8185 tree type_max = TYPE_MAX_VALUE (gnu_type);
8186 tree type_min = TYPE_MIN_VALUE (gnu_type);
8187 tree gnu_expr;
8188 tree tmp1, tmp2;
8189 tree zero = convert (gnu_type, integer_zero_node);
8190 tree rhs_lt_zero;
8191 tree check_pos;
8192 tree check_neg;
8193 tree check;
8194 int precision = TYPE_PRECISION (gnu_type);
8196 gcc_assert (!(precision & (precision - 1))); /* ensure power of 2 */
8198 /* Prefer a constant or known-positive rhs to simplify checks. */
8199 if (!TREE_CONSTANT (rhs)
8200 && commutative_tree_code (code)
8201 && (TREE_CONSTANT (lhs) || (!tree_expr_nonnegative_p (rhs)
8202 && tree_expr_nonnegative_p (lhs))))
8204 tree tmp = lhs;
8205 lhs = rhs;
8206 rhs = tmp;
8209 rhs_lt_zero = tree_expr_nonnegative_p (rhs)
8210 ? boolean_false_node
8211 : build_binary_op (LT_EXPR, boolean_type_node, rhs, zero);
8213 /* ??? Should use more efficient check for operand_equal_p (lhs, rhs, 0) */
8215 /* Try a few strategies that may be cheaper than the general
8216 code at the end of the function, if the rhs is not known.
8217 The strategies are:
8218 - Call library function for 64-bit multiplication (complex)
8219 - Widen, if input arguments are sufficiently small
8220 - Determine overflow using wrapped result for addition/subtraction. */
8222 if (!TREE_CONSTANT (rhs))
8224 /* Even for add/subtract double size to get another base type. */
8225 int needed_precision = precision * 2;
8227 if (code == MULT_EXPR && precision == 64)
8229 tree int_64 = gnat_type_for_size (64, 0);
8231 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
8232 convert (int_64, lhs),
8233 convert (int_64, rhs)));
8236 else if (needed_precision <= BITS_PER_WORD
8237 || (code == MULT_EXPR
8238 && needed_precision <= LONG_LONG_TYPE_SIZE))
8240 tree wide_type = gnat_type_for_size (needed_precision, 0);
8242 tree wide_result = build_binary_op (code, wide_type,
8243 convert (wide_type, lhs),
8244 convert (wide_type, rhs));
8246 tree check = build_binary_op
8247 (TRUTH_ORIF_EXPR, boolean_type_node,
8248 build_binary_op (LT_EXPR, boolean_type_node, wide_result,
8249 convert (wide_type, type_min)),
8250 build_binary_op (GT_EXPR, boolean_type_node, wide_result,
8251 convert (wide_type, type_max)));
8253 tree result = convert (gnu_type, wide_result);
8255 return
8256 emit_check (check, result, CE_Overflow_Check_Failed, gnat_node);
8259 else if (code == PLUS_EXPR || code == MINUS_EXPR)
8261 tree unsigned_type = gnat_type_for_size (precision, 1);
8262 tree wrapped_expr = convert
8263 (gnu_type, build_binary_op (code, unsigned_type,
8264 convert (unsigned_type, lhs),
8265 convert (unsigned_type, rhs)));
8267 tree result = convert
8268 (gnu_type, build_binary_op (code, gnu_type, lhs, rhs));
8270 /* Overflow when (rhs < 0) ^ (wrapped_expr < lhs)), for addition
8271 or when (rhs < 0) ^ (wrapped_expr > lhs) for subtraction. */
8272 tree check = build_binary_op
8273 (TRUTH_XOR_EXPR, boolean_type_node, rhs_lt_zero,
8274 build_binary_op (code == PLUS_EXPR ? LT_EXPR : GT_EXPR,
8275 boolean_type_node, wrapped_expr, lhs));
8277 return
8278 emit_check (check, result, CE_Overflow_Check_Failed, gnat_node);
8282 switch (code)
8284 case PLUS_EXPR:
8285 /* When rhs >= 0, overflow when lhs > type_max - rhs. */
8286 check_pos = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8287 build_binary_op (MINUS_EXPR, gnu_type,
8288 type_max, rhs)),
8290 /* When rhs < 0, overflow when lhs < type_min - rhs. */
8291 check_neg = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8292 build_binary_op (MINUS_EXPR, gnu_type,
8293 type_min, rhs));
8294 break;
8296 case MINUS_EXPR:
8297 /* When rhs >= 0, overflow when lhs < type_min + rhs. */
8298 check_pos = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8299 build_binary_op (PLUS_EXPR, gnu_type,
8300 type_min, rhs)),
8302 /* When rhs < 0, overflow when lhs > type_max + rhs. */
8303 check_neg = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8304 build_binary_op (PLUS_EXPR, gnu_type,
8305 type_max, rhs));
8306 break;
8308 case MULT_EXPR:
8309 /* The check here is designed to be efficient if the rhs is constant,
8310 but it will work for any rhs by using integer division.
8311 Four different check expressions determine whether X * C overflows,
8312 depending on C.
8313 C == 0 => false
8314 C > 0 => X > type_max / C || X < type_min / C
8315 C == -1 => X == type_min
8316 C < -1 => X > type_min / C || X < type_max / C */
8318 tmp1 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
8319 tmp2 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
8321 check_pos
8322 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
8323 build_binary_op (NE_EXPR, boolean_type_node, zero,
8324 rhs),
8325 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8326 build_binary_op (GT_EXPR,
8327 boolean_type_node,
8328 lhs, tmp1),
8329 build_binary_op (LT_EXPR,
8330 boolean_type_node,
8331 lhs, tmp2)));
8333 check_neg
8334 = fold_build3 (COND_EXPR, boolean_type_node,
8335 build_binary_op (EQ_EXPR, boolean_type_node, rhs,
8336 build_int_cst (gnu_type, -1)),
8337 build_binary_op (EQ_EXPR, boolean_type_node, lhs,
8338 type_min),
8339 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8340 build_binary_op (GT_EXPR,
8341 boolean_type_node,
8342 lhs, tmp2),
8343 build_binary_op (LT_EXPR,
8344 boolean_type_node,
8345 lhs, tmp1)));
8346 break;
8348 default:
8349 gcc_unreachable();
8352 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
8354 /* If we can fold the expression to a constant, just return it.
8355 The caller will deal with overflow, no need to generate a check. */
8356 if (TREE_CONSTANT (gnu_expr))
8357 return gnu_expr;
8359 check = fold_build3 (COND_EXPR, boolean_type_node, rhs_lt_zero, check_neg,
8360 check_pos);
8362 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8365 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
8366 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
8367 which we have to check. GNAT_NODE is the GNAT node conveying the source
8368 location for which the error should be signaled. */
8370 static tree
8371 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
8373 tree gnu_range_type = get_unpadded_type (gnat_range_type);
8374 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
8376 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
8377 This can for example happen when translating 'Val or 'Value. */
8378 if (gnu_compare_type == gnu_range_type)
8379 return gnu_expr;
8381 /* Range checks can only be applied to types with ranges. */
8382 gcc_assert (INTEGRAL_TYPE_P (gnu_range_type)
8383 || SCALAR_FLOAT_TYPE_P (gnu_range_type));
8385 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
8386 we can't do anything since we might be truncating the bounds. No
8387 check is needed in this case. */
8388 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
8389 && (TYPE_PRECISION (gnu_compare_type)
8390 < TYPE_PRECISION (get_base_type (gnu_range_type))))
8391 return gnu_expr;
8393 /* Checked expressions must be evaluated only once. */
8394 gnu_expr = gnat_protect_expr (gnu_expr);
8396 /* Note that the form of the check is
8397 (not (expr >= lo)) or (not (expr <= hi))
8398 the reason for this slightly convoluted form is that NaNs
8399 are not considered to be in range in the float case. */
8400 return emit_check
8401 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8402 invert_truthvalue
8403 (build_binary_op (GE_EXPR, boolean_type_node,
8404 convert (gnu_compare_type, gnu_expr),
8405 convert (gnu_compare_type,
8406 TYPE_MIN_VALUE
8407 (gnu_range_type)))),
8408 invert_truthvalue
8409 (build_binary_op (LE_EXPR, boolean_type_node,
8410 convert (gnu_compare_type, gnu_expr),
8411 convert (gnu_compare_type,
8412 TYPE_MAX_VALUE
8413 (gnu_range_type))))),
8414 gnu_expr, CE_Range_Check_Failed, gnat_node);
8417 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object which
8418 we are about to index, GNU_EXPR is the index expression to be checked,
8419 GNU_LOW and GNU_HIGH are the lower and upper bounds against which GNU_EXPR
8420 has to be checked. Note that for index checking we cannot simply use the
8421 emit_range_check function (although very similar code needs to be generated
8422 in both cases) since for index checking the array type against which we are
8423 checking the indices may be unconstrained and consequently we need to get
8424 the actual index bounds from the array object itself (GNU_ARRAY_OBJECT).
8425 The place where we need to do that is in subprograms having unconstrained
8426 array formal parameters. GNAT_NODE is the GNAT node conveying the source
8427 location for which the error should be signaled. */
8429 static tree
8430 emit_index_check (tree gnu_array_object, tree gnu_expr, tree gnu_low,
8431 tree gnu_high, Node_Id gnat_node)
8433 tree gnu_expr_check;
8435 /* Checked expressions must be evaluated only once. */
8436 gnu_expr = gnat_protect_expr (gnu_expr);
8438 /* Must do this computation in the base type in case the expression's
8439 type is an unsigned subtypes. */
8440 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
8442 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
8443 the object we are handling. */
8444 gnu_low = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low, gnu_array_object);
8445 gnu_high = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high, gnu_array_object);
8447 return emit_check
8448 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8449 build_binary_op (LT_EXPR, boolean_type_node,
8450 gnu_expr_check,
8451 convert (TREE_TYPE (gnu_expr_check),
8452 gnu_low)),
8453 build_binary_op (GT_EXPR, boolean_type_node,
8454 gnu_expr_check,
8455 convert (TREE_TYPE (gnu_expr_check),
8456 gnu_high))),
8457 gnu_expr, CE_Index_Check_Failed, gnat_node);
8460 /* GNU_COND contains the condition corresponding to an access, discriminant or
8461 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
8462 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
8463 REASON is the code that says why the exception was raised. GNAT_NODE is
8464 the GNAT node conveying the source location for which the error should be
8465 signaled. */
8467 static tree
8468 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
8470 tree gnu_call
8471 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
8472 tree gnu_result
8473 = fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
8474 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
8475 convert (TREE_TYPE (gnu_expr), integer_zero_node)),
8476 gnu_expr);
8478 /* GNU_RESULT has side effects if and only if GNU_EXPR has:
8479 we don't need to evaluate it just for the check. */
8480 TREE_SIDE_EFFECTS (gnu_result) = TREE_SIDE_EFFECTS (gnu_expr);
8482 return gnu_result;
8485 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
8486 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
8487 GNAT_TYPE is known to be an integral type. If TRUNCATE_P true, do a
8488 float to integer conversion with truncation; otherwise round.
8489 GNAT_NODE is the GNAT node conveying the source location for which the
8490 error should be signaled. */
8492 static tree
8493 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
8494 bool rangep, bool truncatep, Node_Id gnat_node)
8496 tree gnu_type = get_unpadded_type (gnat_type);
8497 tree gnu_in_type = TREE_TYPE (gnu_expr);
8498 tree gnu_in_basetype = get_base_type (gnu_in_type);
8499 tree gnu_base_type = get_base_type (gnu_type);
8500 tree gnu_result = gnu_expr;
8502 /* If we are not doing any checks, the output is an integral type, and
8503 the input is not a floating type, just do the conversion. This
8504 shortcut is required to avoid problems with packed array types
8505 and simplifies code in all cases anyway. */
8506 if (!rangep && !overflowp && INTEGRAL_TYPE_P (gnu_base_type)
8507 && !FLOAT_TYPE_P (gnu_in_type))
8508 return convert (gnu_type, gnu_expr);
8510 /* First convert the expression to its base type. This
8511 will never generate code, but makes the tests below much simpler.
8512 But don't do this if converting from an integer type to an unconstrained
8513 array type since then we need to get the bounds from the original
8514 (unpacked) type. */
8515 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
8516 gnu_result = convert (gnu_in_basetype, gnu_result);
8518 /* If overflow checks are requested, we need to be sure the result will
8519 fit in the output base type. But don't do this if the input
8520 is integer and the output floating-point. */
8521 if (overflowp
8522 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
8524 /* Ensure GNU_EXPR only gets evaluated once. */
8525 tree gnu_input = gnat_protect_expr (gnu_result);
8526 tree gnu_cond = boolean_false_node;
8527 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
8528 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
8529 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
8530 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
8532 /* Convert the lower bounds to signed types, so we're sure we're
8533 comparing them properly. Likewise, convert the upper bounds
8534 to unsigned types. */
8535 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TYPE_UNSIGNED (gnu_in_basetype))
8536 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
8538 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8539 && !TYPE_UNSIGNED (gnu_in_basetype))
8540 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
8542 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
8543 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
8545 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
8546 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
8548 /* Check each bound separately and only if the result bound
8549 is tighter than the bound on the input type. Note that all the
8550 types are base types, so the bounds must be constant. Also,
8551 the comparison is done in the base type of the input, which
8552 always has the proper signedness. First check for input
8553 integer (which means output integer), output float (which means
8554 both float), or mixed, in which case we always compare.
8555 Note that we have to do the comparison which would *fail* in the
8556 case of an error since if it's an FP comparison and one of the
8557 values is a NaN or Inf, the comparison will fail. */
8558 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8559 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
8560 : (FLOAT_TYPE_P (gnu_base_type)
8561 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
8562 TREE_REAL_CST (gnu_out_lb))
8563 : 1))
8564 gnu_cond
8565 = invert_truthvalue
8566 (build_binary_op (GE_EXPR, boolean_type_node,
8567 gnu_input, convert (gnu_in_basetype,
8568 gnu_out_lb)));
8570 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8571 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
8572 : (FLOAT_TYPE_P (gnu_base_type)
8573 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
8574 TREE_REAL_CST (gnu_in_lb))
8575 : 1))
8576 gnu_cond
8577 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
8578 invert_truthvalue
8579 (build_binary_op (LE_EXPR, boolean_type_node,
8580 gnu_input,
8581 convert (gnu_in_basetype,
8582 gnu_out_ub))));
8584 if (!integer_zerop (gnu_cond))
8585 gnu_result = emit_check (gnu_cond, gnu_input,
8586 CE_Overflow_Check_Failed, gnat_node);
8589 /* Now convert to the result base type. If this is a non-truncating
8590 float-to-integer conversion, round. */
8591 if (INTEGRAL_TYPE_P (gnu_base_type) && FLOAT_TYPE_P (gnu_in_basetype)
8592 && !truncatep)
8594 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
8595 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
8596 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
8597 const struct real_format *fmt;
8599 /* The following calculations depend on proper rounding to even
8600 of each arithmetic operation. In order to prevent excess
8601 precision from spoiling this property, use the widest hardware
8602 floating-point type if FP_ARITH_MAY_WIDEN is true. */
8603 calc_type
8604 = FP_ARITH_MAY_WIDEN ? longest_float_type_node : gnu_in_basetype;
8606 /* FIXME: Should not have padding in the first place. */
8607 if (TYPE_IS_PADDING_P (calc_type))
8608 calc_type = TREE_TYPE (TYPE_FIELDS (calc_type));
8610 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
8611 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
8612 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
8613 REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf,
8614 half_minus_pred_half);
8615 gnu_pred_half = build_real (calc_type, pred_half);
8617 /* If the input is strictly negative, subtract this value
8618 and otherwise add it from the input. For 0.5, the result
8619 is exactly between 1.0 and the machine number preceding 1.0
8620 (for calc_type). Since the last bit of 1.0 is even, this 0.5
8621 will round to 1.0, while all other number with an absolute
8622 value less than 0.5 round to 0.0. For larger numbers exactly
8623 halfway between integers, rounding will always be correct as
8624 the true mathematical result will be closer to the higher
8625 integer compared to the lower one. So, this constant works
8626 for all floating-point numbers.
8628 The reason to use the same constant with subtract/add instead
8629 of a positive and negative constant is to allow the comparison
8630 to be scheduled in parallel with retrieval of the constant and
8631 conversion of the input to the calc_type (if necessary). */
8633 gnu_zero = convert (gnu_in_basetype, integer_zero_node);
8634 gnu_result = gnat_protect_expr (gnu_result);
8635 gnu_conv = convert (calc_type, gnu_result);
8636 gnu_comp
8637 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
8638 gnu_add_pred_half
8639 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8640 gnu_subtract_pred_half
8641 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8642 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
8643 gnu_add_pred_half, gnu_subtract_pred_half);
8646 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8647 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
8648 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
8649 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
8650 else
8651 gnu_result = convert (gnu_base_type, gnu_result);
8653 /* Finally, do the range check if requested. Note that if the result type
8654 is a modular type, the range check is actually an overflow check. */
8655 if (rangep
8656 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8657 && TYPE_MODULAR_P (gnu_base_type) && overflowp))
8658 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
8660 return convert (gnu_type, gnu_result);
8663 /* Return true if GNU_EXPR can be directly addressed. This is the case
8664 unless it is an expression involving computation or if it involves a
8665 reference to a bitfield or to an object not sufficiently aligned for
8666 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
8667 be directly addressed as an object of this type.
8669 *** Notes on addressability issues in the Ada compiler ***
8671 This predicate is necessary in order to bridge the gap between Gigi
8672 and the middle-end about addressability of GENERIC trees. A tree
8673 is said to be addressable if it can be directly addressed, i.e. if
8674 its address can be taken, is a multiple of the type's alignment on
8675 strict-alignment architectures and returns the first storage unit
8676 assigned to the object represented by the tree.
8678 In the C family of languages, everything is in practice addressable
8679 at the language level, except for bit-fields. This means that these
8680 compilers will take the address of any tree that doesn't represent
8681 a bit-field reference and expect the result to be the first storage
8682 unit assigned to the object. Even in cases where this will result
8683 in unaligned accesses at run time, nothing is supposed to be done
8684 and the program is considered as erroneous instead (see PR c/18287).
8686 The implicit assumptions made in the middle-end are in keeping with
8687 the C viewpoint described above:
8688 - the address of a bit-field reference is supposed to be never
8689 taken; the compiler (generally) will stop on such a construct,
8690 - any other tree is addressable if it is formally addressable,
8691 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
8693 In Ada, the viewpoint is the opposite one: nothing is addressable
8694 at the language level unless explicitly declared so. This means
8695 that the compiler will both make sure that the trees representing
8696 references to addressable ("aliased" in Ada parlance) objects are
8697 addressable and make no real attempts at ensuring that the trees
8698 representing references to non-addressable objects are addressable.
8700 In the first case, Ada is effectively equivalent to C and handing
8701 down the direct result of applying ADDR_EXPR to these trees to the
8702 middle-end works flawlessly. In the second case, Ada cannot afford
8703 to consider the program as erroneous if the address of trees that
8704 are not addressable is requested for technical reasons, unlike C;
8705 as a consequence, the Ada compiler must arrange for either making
8706 sure that this address is not requested in the middle-end or for
8707 compensating by inserting temporaries if it is requested in Gigi.
8709 The first goal can be achieved because the middle-end should not
8710 request the address of non-addressable trees on its own; the only
8711 exception is for the invocation of low-level block operations like
8712 memcpy, for which the addressability requirements are lower since
8713 the type's alignment can be disregarded. In practice, this means
8714 that Gigi must make sure that such operations cannot be applied to
8715 non-BLKmode bit-fields.
8717 The second goal is achieved by means of the addressable_p predicate,
8718 which computes whether a temporary must be inserted by Gigi when the
8719 address of a tree is requested; if so, the address of the temporary
8720 will be used in lieu of that of the original tree and some glue code
8721 generated to connect everything together. */
8723 static bool
8724 addressable_p (tree gnu_expr, tree gnu_type)
8726 /* For an integral type, the size of the actual type of the object may not
8727 be greater than that of the expected type, otherwise an indirect access
8728 in the latter type wouldn't correctly set all the bits of the object. */
8729 if (gnu_type
8730 && INTEGRAL_TYPE_P (gnu_type)
8731 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
8732 return false;
8734 /* The size of the actual type of the object may not be smaller than that
8735 of the expected type, otherwise an indirect access in the latter type
8736 would be larger than the object. But only record types need to be
8737 considered in practice for this case. */
8738 if (gnu_type
8739 && TREE_CODE (gnu_type) == RECORD_TYPE
8740 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
8741 return false;
8743 switch (TREE_CODE (gnu_expr))
8745 case VAR_DECL:
8746 case PARM_DECL:
8747 case FUNCTION_DECL:
8748 case RESULT_DECL:
8749 /* All DECLs are addressable: if they are in a register, we can force
8750 them to memory. */
8751 return true;
8753 case UNCONSTRAINED_ARRAY_REF:
8754 case INDIRECT_REF:
8755 /* Taking the address of a dereference yields the original pointer. */
8756 return true;
8758 case STRING_CST:
8759 case INTEGER_CST:
8760 /* Taking the address yields a pointer to the constant pool. */
8761 return true;
8763 case CONSTRUCTOR:
8764 /* Taking the address of a static constructor yields a pointer to the
8765 tree constant pool. */
8766 return TREE_STATIC (gnu_expr) ? true : false;
8768 case NULL_EXPR:
8769 case SAVE_EXPR:
8770 case CALL_EXPR:
8771 case PLUS_EXPR:
8772 case MINUS_EXPR:
8773 case BIT_IOR_EXPR:
8774 case BIT_XOR_EXPR:
8775 case BIT_AND_EXPR:
8776 case BIT_NOT_EXPR:
8777 /* All rvalues are deemed addressable since taking their address will
8778 force a temporary to be created by the middle-end. */
8779 return true;
8781 case COMPOUND_EXPR:
8782 /* The address of a compound expression is that of its 2nd operand. */
8783 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
8785 case COND_EXPR:
8786 /* We accept &COND_EXPR as soon as both operands are addressable and
8787 expect the outcome to be the address of the selected operand. */
8788 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
8789 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
8791 case COMPONENT_REF:
8792 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
8793 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
8794 the field is sufficiently aligned, in case it is subject
8795 to a pragma Component_Alignment. But we don't need to
8796 check the alignment of the containing record, as it is
8797 guaranteed to be not smaller than that of its most
8798 aligned field that is not a bit-field. */
8799 && (!STRICT_ALIGNMENT
8800 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
8801 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
8802 /* The field of a padding record is always addressable. */
8803 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
8804 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8806 case ARRAY_REF: case ARRAY_RANGE_REF:
8807 case REALPART_EXPR: case IMAGPART_EXPR:
8808 case NOP_EXPR:
8809 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
8811 case CONVERT_EXPR:
8812 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
8813 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8815 case VIEW_CONVERT_EXPR:
8817 /* This is addressable if we can avoid a copy. */
8818 tree type = TREE_TYPE (gnu_expr);
8819 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
8820 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
8821 && (!STRICT_ALIGNMENT
8822 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8823 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
8824 || ((TYPE_MODE (type) == BLKmode
8825 || TYPE_MODE (inner_type) == BLKmode)
8826 && (!STRICT_ALIGNMENT
8827 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8828 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
8829 || TYPE_ALIGN_OK (type)
8830 || TYPE_ALIGN_OK (inner_type))))
8831 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8834 default:
8835 return false;
8839 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
8840 a separate Freeze node exists, delay the bulk of the processing. Otherwise
8841 make a GCC type for GNAT_ENTITY and set up the correspondence. */
8843 void
8844 process_type (Entity_Id gnat_entity)
8846 tree gnu_old
8847 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
8848 tree gnu_new;
8850 /* If we are to delay elaboration of this type, just do any
8851 elaborations needed for expressions within the declaration and
8852 make a dummy type entry for this node and its Full_View (if
8853 any) in case something points to it. Don't do this if it
8854 has already been done (the only way that can happen is if
8855 the private completion is also delayed). */
8856 if (Present (Freeze_Node (gnat_entity))
8857 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8858 && Present (Full_View (gnat_entity))
8859 && Present (Freeze_Node (Full_View (gnat_entity)))
8860 && !present_gnu_tree (Full_View (gnat_entity))))
8862 elaborate_entity (gnat_entity);
8864 if (!gnu_old)
8866 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
8867 save_gnu_tree (gnat_entity, gnu_decl, false);
8868 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8869 && Present (Full_View (gnat_entity)))
8871 if (Has_Completion_In_Body (gnat_entity))
8872 DECL_TAFT_TYPE_P (gnu_decl) = 1;
8873 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
8877 return;
8880 /* If we saved away a dummy type for this node it means that this
8881 made the type that corresponds to the full type of an incomplete
8882 type. Clear that type for now and then update the type in the
8883 pointers. */
8884 if (gnu_old)
8886 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
8887 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
8889 save_gnu_tree (gnat_entity, NULL_TREE, false);
8892 /* Now fully elaborate the type. */
8893 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
8894 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
8896 /* If we have an old type and we've made pointers to this type, update those
8897 pointers. If this is a Taft amendment type in the main unit, we need to
8898 mark the type as used since other units referencing it don't see the full
8899 declaration and, therefore, cannot mark it as used themselves. */
8900 if (gnu_old)
8902 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8903 TREE_TYPE (gnu_new));
8904 if (DECL_TAFT_TYPE_P (gnu_old))
8905 used_types_insert (TREE_TYPE (gnu_new));
8908 /* If this is a record type corresponding to a task or protected type
8909 that is a completion of an incomplete type, perform a similar update
8910 on the type. ??? Including protected types here is a guess. */
8911 if (IN (Ekind (gnat_entity), Record_Kind)
8912 && Is_Concurrent_Record_Type (gnat_entity)
8913 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
8915 tree gnu_task_old
8916 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
8918 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8919 NULL_TREE, false);
8920 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8921 gnu_new, false);
8923 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
8924 TREE_TYPE (gnu_new));
8928 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
8929 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
8930 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
8932 static tree
8933 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
8935 tree gnu_list = NULL_TREE, gnu_result;
8937 /* We test for GNU_FIELD being empty in the case where a variant
8938 was the last thing since we don't take things off GNAT_ASSOC in
8939 that case. We check GNAT_ASSOC in case we have a variant, but it
8940 has no fields. */
8942 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
8944 Node_Id gnat_field = First (Choices (gnat_assoc));
8945 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
8946 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
8948 /* The expander is supposed to put a single component selector name
8949 in every record component association. */
8950 gcc_assert (No (Next (gnat_field)));
8952 /* Ignore fields that have Corresponding_Discriminants since we'll
8953 be setting that field in the parent. */
8954 if (Present (Corresponding_Discriminant (Entity (gnat_field)))
8955 && Is_Tagged_Type (Scope (Entity (gnat_field))))
8956 continue;
8958 /* Also ignore discriminants of Unchecked_Unions. */
8959 if (Is_Unchecked_Union (gnat_entity)
8960 && Ekind (Entity (gnat_field)) == E_Discriminant)
8961 continue;
8963 /* Before assigning a value in an aggregate make sure range checks
8964 are done if required. Then convert to the type of the field. */
8965 if (Do_Range_Check (Expression (gnat_assoc)))
8966 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
8968 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
8970 /* Add the field and expression to the list. */
8971 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
8974 gnu_result = extract_values (gnu_list, gnu_type);
8976 #ifdef ENABLE_CHECKING
8977 /* Verify that every entry in GNU_LIST was used. */
8978 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
8979 gcc_assert (TREE_ADDRESSABLE (gnu_list));
8980 #endif
8982 return gnu_result;
8985 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
8986 the first element of an array aggregate. It may itself be an aggregate.
8987 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
8988 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
8989 for range checking. */
8991 static tree
8992 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
8993 Entity_Id gnat_component_type)
8995 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
8996 tree gnu_expr;
8997 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
8999 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
9001 /* If the expression is itself an array aggregate then first build the
9002 innermost constructor if it is part of our array (multi-dimensional
9003 case). */
9004 if (Nkind (gnat_expr) == N_Aggregate
9005 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
9006 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
9007 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
9008 TREE_TYPE (gnu_array_type),
9009 gnat_component_type);
9010 else
9012 gnu_expr = gnat_to_gnu (gnat_expr);
9014 /* Before assigning the element to the array, make sure it is
9015 in range. */
9016 if (Do_Range_Check (gnat_expr))
9017 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
9020 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
9021 convert (TREE_TYPE (gnu_array_type), gnu_expr));
9023 gnu_index = int_const_binop (PLUS_EXPR, gnu_index, integer_one_node);
9026 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
9029 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
9030 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
9031 of the associations that are from RECORD_TYPE. If we see an internal
9032 record, make a recursive call to fill it in as well. */
9034 static tree
9035 extract_values (tree values, tree record_type)
9037 tree field, tem;
9038 vec<constructor_elt, va_gc> *v = NULL;
9040 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9042 tree value = 0;
9044 /* _Parent is an internal field, but may have values in the aggregate,
9045 so check for values first. */
9046 if ((tem = purpose_member (field, values)))
9048 value = TREE_VALUE (tem);
9049 TREE_ADDRESSABLE (tem) = 1;
9052 else if (DECL_INTERNAL_P (field))
9054 value = extract_values (values, TREE_TYPE (field));
9055 if (TREE_CODE (value) == CONSTRUCTOR
9056 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
9057 value = 0;
9059 else
9060 /* If we have a record subtype, the names will match, but not the
9061 actual FIELD_DECLs. */
9062 for (tem = values; tem; tem = TREE_CHAIN (tem))
9063 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
9065 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
9066 TREE_ADDRESSABLE (tem) = 1;
9069 if (!value)
9070 continue;
9072 CONSTRUCTOR_APPEND_ELT (v, field, value);
9075 return gnat_build_constructor (record_type, v);
9078 /* Process a N_Validate_Unchecked_Conversion node. */
9080 static void
9081 validate_unchecked_conversion (Node_Id gnat_node)
9083 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
9084 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
9086 /* If the target is a pointer type, see if we are either converting from a
9087 non-pointer or from a pointer to a type with a different alias set and
9088 warn if so, unless the pointer has been marked to alias everything. */
9089 if (POINTER_TYPE_P (gnu_target_type)
9090 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
9092 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
9093 ? TREE_TYPE (gnu_source_type)
9094 : NULL_TREE;
9095 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
9096 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9098 if (target_alias_set != 0
9099 && (!POINTER_TYPE_P (gnu_source_type)
9100 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9101 target_alias_set)))
9103 post_error_ne ("?possible aliasing problem for type&",
9104 gnat_node, Target_Type (gnat_node));
9105 post_error ("\\?use -fno-strict-aliasing switch for references",
9106 gnat_node);
9107 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
9108 gnat_node, Target_Type (gnat_node));
9112 /* Likewise if the target is a fat pointer type, but we have no mechanism to
9113 mitigate the problem in this case, so we unconditionally warn. */
9114 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
9116 tree gnu_source_desig_type
9117 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
9118 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
9119 : NULL_TREE;
9120 tree gnu_target_desig_type
9121 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
9122 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9124 if (target_alias_set != 0
9125 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
9126 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9127 target_alias_set)))
9129 post_error_ne ("?possible aliasing problem for type&",
9130 gnat_node, Target_Type (gnat_node));
9131 post_error ("\\?use -fno-strict-aliasing switch for references",
9132 gnat_node);
9137 /* EXP is to be treated as an array or record. Handle the cases when it is
9138 an access object and perform the required dereferences. */
9140 static tree
9141 maybe_implicit_deref (tree exp)
9143 /* If the type is a pointer, dereference it. */
9144 if (POINTER_TYPE_P (TREE_TYPE (exp))
9145 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp)))
9146 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
9148 /* If we got a padded type, remove it too. */
9149 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
9150 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
9152 return exp;
9155 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
9156 location and false if it doesn't. In the former case, set the Gigi global
9157 variable REF_FILENAME to the simple debug file name as given by sinput.
9158 If clear_column is true, set column information to 0. */
9160 static bool
9161 Sloc_to_locus1 (Source_Ptr Sloc, location_t *locus, bool clear_column)
9163 if (Sloc == No_Location)
9164 return false;
9166 if (Sloc <= Standard_Location)
9168 *locus = BUILTINS_LOCATION;
9169 return false;
9171 else
9173 Source_File_Index file = Get_Source_File_Index (Sloc);
9174 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
9175 Column_Number column = (clear_column ? 0 : Get_Column_Number (Sloc));
9176 struct line_map *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
9178 /* We can have zero if pragma Source_Reference is in effect. */
9179 if (line < 1)
9180 line = 1;
9182 /* Translate the location. */
9183 *locus = linemap_position_for_line_and_column (map, line, column);
9186 ref_filename
9187 = IDENTIFIER_POINTER
9188 (get_identifier
9189 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
9191 return true;
9194 /* Similar to the above, not clearing the column information. */
9196 bool
9197 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
9199 return Sloc_to_locus1 (Sloc, locus, false);
9202 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
9203 don't do anything if it doesn't correspond to a source location. */
9205 static void
9206 set_expr_location_from_node1 (tree node, Node_Id gnat_node, bool clear_column)
9208 location_t locus;
9210 if (!Sloc_to_locus1 (Sloc (gnat_node), &locus, clear_column))
9211 return;
9213 SET_EXPR_LOCATION (node, locus);
9216 /* Similar to the above, not clearing the column information. */
9218 static void
9219 set_expr_location_from_node (tree node, Node_Id gnat_node)
9221 set_expr_location_from_node1 (node, gnat_node, false);
9224 /* More elaborate version of set_expr_location_from_node to be used in more
9225 general contexts, for example the result of the translation of a generic
9226 GNAT node. */
9228 static void
9229 set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
9231 /* Set the location information on the node if it is a real expression.
9232 References can be reused for multiple GNAT nodes and they would get
9233 the location information of their last use. Also make sure not to
9234 overwrite an existing location as it is probably more precise. */
9236 switch (TREE_CODE (node))
9238 CASE_CONVERT:
9239 case NON_LVALUE_EXPR:
9240 break;
9242 case COMPOUND_EXPR:
9243 if (EXPR_P (TREE_OPERAND (node, 1)))
9244 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
9246 /* ... fall through ... */
9248 default:
9249 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
9251 set_expr_location_from_node (node, gnat_node);
9252 set_end_locus_from_node (node, gnat_node);
9254 break;
9258 /* Return a colon-separated list of encodings contained in encoded Ada
9259 name. */
9261 static const char *
9262 extract_encoding (const char *name)
9264 char *encoding = (char *) ggc_alloc_atomic (strlen (name));
9265 get_encoding (name, encoding);
9266 return encoding;
9269 /* Extract the Ada name from an encoded name. */
9271 static const char *
9272 decode_name (const char *name)
9274 char *decoded = (char *) ggc_alloc_atomic (strlen (name) * 2 + 60);
9275 __gnat_decode (name, decoded, 0);
9276 return decoded;
9279 /* Post an error message. MSG is the error message, properly annotated.
9280 NODE is the node at which to post the error and the node to use for the
9281 '&' substitution. */
9283 void
9284 post_error (const char *msg, Node_Id node)
9286 String_Template temp;
9287 Fat_Pointer fp;
9289 if (No (node))
9290 return;
9292 temp.Low_Bound = 1;
9293 temp.High_Bound = strlen (msg);
9294 fp.Bounds = &temp;
9295 fp.Array = msg;
9296 Error_Msg_N (fp, node);
9299 /* Similar to post_error, but NODE is the node at which to post the error and
9300 ENT is the node to use for the '&' substitution. */
9302 void
9303 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
9305 String_Template temp;
9306 Fat_Pointer fp;
9308 if (No (node))
9309 return;
9311 temp.Low_Bound = 1;
9312 temp.High_Bound = strlen (msg);
9313 fp.Bounds = &temp;
9314 fp.Array = msg;
9315 Error_Msg_NE (fp, node, ent);
9318 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
9320 void
9321 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
9323 Error_Msg_Uint_1 = UI_From_Int (num);
9324 post_error_ne (msg, node, ent);
9327 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
9328 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
9329 most sense. Return true if a sensible assignment was performed. */
9331 static bool
9332 set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
9334 Node_Id gnat_end_label = Empty;
9335 location_t end_locus;
9337 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
9338 end_locus when there is one. We consider only GNAT nodes with a possible
9339 End_Label attached. If the End_Label actually was unassigned, fallback
9340 on the original node. We'd better assign an explicit sloc associated with
9341 the outer construct in any case. */
9343 switch (Nkind (gnat_node))
9345 case N_Package_Body:
9346 case N_Subprogram_Body:
9347 case N_Block_Statement:
9348 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
9349 break;
9351 case N_Package_Declaration:
9352 gnat_end_label = End_Label (Specification (gnat_node));
9353 break;
9355 default:
9356 return false;
9359 gnat_node = Present (gnat_end_label) ? gnat_end_label : gnat_node;
9361 /* Some expanded subprograms have neither an End_Label nor a Sloc
9362 attached. Notify that to callers. For a block statement with no
9363 End_Label, clear column information, so that the tree for a
9364 transient block does not receive the sloc of a source condition. */
9366 if (!Sloc_to_locus1 (Sloc (gnat_node), &end_locus,
9367 No (gnat_end_label) &&
9368 (Nkind (gnat_node) == N_Block_Statement)))
9369 return false;
9371 switch (TREE_CODE (gnu_node))
9373 case BIND_EXPR:
9374 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
9375 return true;
9377 case FUNCTION_DECL:
9378 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
9379 return true;
9381 default:
9382 return false;
9386 /* Similar to post_error_ne, but T is a GCC tree representing the number to
9387 write. If T represents a constant, the text inside curly brackets in
9388 MSG will be output (presumably including a '^'). Otherwise it will not
9389 be output and the text inside square brackets will be output instead. */
9391 void
9392 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
9394 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
9395 char start_yes, end_yes, start_no, end_no;
9396 const char *p;
9397 char *q;
9399 if (TREE_CODE (t) == INTEGER_CST)
9401 Error_Msg_Uint_1 = UI_From_gnu (t);
9402 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
9404 else
9405 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
9407 for (p = msg, q = new_msg; *p; p++)
9409 if (*p == start_yes)
9410 for (p++; *p != end_yes; p++)
9411 *q++ = *p;
9412 else if (*p == start_no)
9413 for (p++; *p != end_no; p++)
9415 else
9416 *q++ = *p;
9419 *q = 0;
9421 post_error_ne (new_msg, node, ent);
9424 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
9426 void
9427 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
9428 int num)
9430 Error_Msg_Uint_2 = UI_From_Int (num);
9431 post_error_ne_tree (msg, node, ent, t);
9434 /* Initialize the table that maps GNAT codes to GCC codes for simple
9435 binary and unary operations. */
9437 static void
9438 init_code_table (void)
9440 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
9441 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
9443 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
9444 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
9445 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
9446 gnu_codes[N_Op_Eq] = EQ_EXPR;
9447 gnu_codes[N_Op_Ne] = NE_EXPR;
9448 gnu_codes[N_Op_Lt] = LT_EXPR;
9449 gnu_codes[N_Op_Le] = LE_EXPR;
9450 gnu_codes[N_Op_Gt] = GT_EXPR;
9451 gnu_codes[N_Op_Ge] = GE_EXPR;
9452 gnu_codes[N_Op_Add] = PLUS_EXPR;
9453 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
9454 gnu_codes[N_Op_Multiply] = MULT_EXPR;
9455 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
9456 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
9457 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
9458 gnu_codes[N_Op_Abs] = ABS_EXPR;
9459 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
9460 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
9461 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
9462 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
9463 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
9464 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
9467 /* Return a label to branch to for the exception type in KIND or NULL_TREE
9468 if none. */
9470 tree
9471 get_exception_label (char kind)
9473 if (kind == N_Raise_Constraint_Error)
9474 return gnu_constraint_error_label_stack->last ();
9475 else if (kind == N_Raise_Storage_Error)
9476 return gnu_storage_error_label_stack->last ();
9477 else if (kind == N_Raise_Program_Error)
9478 return gnu_program_error_label_stack->last ();
9479 else
9480 return NULL_TREE;
9483 /* Return the decl for the current elaboration procedure. */
9485 tree
9486 get_elaboration_procedure (void)
9488 return gnu_elab_proc_stack->last ();
9491 #include "gt-ada-trans.h"