* gcc-interface/utils.c (create_subprog_decl): Move code dealing with
[official-gcc.git] / gcc / ada / gcc-interface / trans.c
blob258b79cbb52712f653a1b7facab15b801abc7640
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 "hash-set.h"
40 #include "gimple-expr.h"
41 #include "gimplify.h"
42 #include "bitmap.h"
43 #include "hash-map.h"
44 #include "is-a.h"
45 #include "plugin-api.h"
46 #include "vec.h"
47 #include "hashtab.h"
48 #include "machmode.h"
49 #include "hard-reg-set.h"
50 #include "input.h"
51 #include "function.h"
52 #include "ipa-ref.h"
53 #include "cgraph.h"
54 #include "diagnostic.h"
55 #include "opts.h"
56 #include "target.h"
57 #include "common/common-target.h"
59 #include "ada.h"
60 #include "adadecode.h"
61 #include "types.h"
62 #include "atree.h"
63 #include "elists.h"
64 #include "namet.h"
65 #include "nlists.h"
66 #include "snames.h"
67 #include "stringt.h"
68 #include "uintp.h"
69 #include "urealp.h"
70 #include "fe.h"
71 #include "sinfo.h"
72 #include "einfo.h"
73 #include "gadaint.h"
74 #include "ada-tree.h"
75 #include "gigi.h"
77 /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
78 for fear of running out of stack space. If we need more, we use xmalloc
79 instead. */
80 #define ALLOCA_THRESHOLD 1000
82 /* In configurations where blocks have no end_locus attached, just
83 sink assignments into a dummy global. */
84 #ifndef BLOCK_SOURCE_END_LOCATION
85 static location_t block_end_locus_sink;
86 #define BLOCK_SOURCE_END_LOCATION(BLOCK) block_end_locus_sink
87 #endif
89 /* Pointers to front-end tables accessed through macros. */
90 struct Node *Nodes_Ptr;
91 struct Flags *Flags_Ptr;
92 Node_Id *Next_Node_Ptr;
93 Node_Id *Prev_Node_Ptr;
94 struct Elist_Header *Elists_Ptr;
95 struct Elmt_Item *Elmts_Ptr;
96 struct String_Entry *Strings_Ptr;
97 Char_Code *String_Chars_Ptr;
98 struct List_Header *List_Headers_Ptr;
100 /* Highest number in the front-end node table. */
101 int max_gnat_nodes;
103 /* Current node being treated, in case abort called. */
104 Node_Id error_gnat_node;
106 /* True when gigi is being called on an analyzed but unexpanded
107 tree, and the only purpose of the call is to properly annotate
108 types with representation information. */
109 bool type_annotate_only;
111 /* Current filename without path. */
112 const char *ref_filename;
115 /* List of N_Validate_Unchecked_Conversion nodes in the unit. */
116 static vec<Node_Id> gnat_validate_uc_list;
118 /* When not optimizing, we cache the 'First, 'Last and 'Length attributes
119 of unconstrained array IN parameters to avoid emitting a great deal of
120 redundant instructions to recompute them each time. */
121 struct GTY (()) parm_attr_d {
122 int id; /* GTY doesn't like Entity_Id. */
123 int dim;
124 tree first;
125 tree last;
126 tree length;
129 typedef struct parm_attr_d *parm_attr;
132 struct GTY(()) language_function {
133 vec<parm_attr, va_gc> *parm_attr_cache;
134 bitmap named_ret_val;
135 vec<tree, va_gc> *other_ret_val;
136 int gnat_ret;
139 #define f_parm_attr_cache \
140 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
142 #define f_named_ret_val \
143 DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val
145 #define f_other_ret_val \
146 DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val
148 #define f_gnat_ret \
149 DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret
151 /* A structure used to gather together information about a statement group.
152 We use this to gather related statements, for example the "then" part
153 of a IF. In the case where it represents a lexical scope, we may also
154 have a BLOCK node corresponding to it and/or cleanups. */
156 struct GTY((chain_next ("%h.previous"))) stmt_group {
157 struct stmt_group *previous; /* Previous code group. */
158 tree stmt_list; /* List of statements for this code group. */
159 tree block; /* BLOCK for this code group, if any. */
160 tree cleanups; /* Cleanups for this code group, if any. */
163 static GTY(()) struct stmt_group *current_stmt_group;
165 /* List of unused struct stmt_group nodes. */
166 static GTY((deletable)) struct stmt_group *stmt_group_free_list;
168 /* A structure used to record information on elaboration procedures
169 we've made and need to process.
171 ??? gnat_node should be Node_Id, but gengtype gets confused. */
173 struct GTY((chain_next ("%h.next"))) elab_info {
174 struct elab_info *next; /* Pointer to next in chain. */
175 tree elab_proc; /* Elaboration procedure. */
176 int gnat_node; /* The N_Compilation_Unit. */
179 static GTY(()) struct elab_info *elab_info_list;
181 /* Stack of exception pointer variables. Each entry is the VAR_DECL
182 that stores the address of the raised exception. Nonzero means we
183 are in an exception handler. Not used in the zero-cost case. */
184 static GTY(()) vec<tree, va_gc> *gnu_except_ptr_stack;
186 /* In ZCX case, current exception pointer. Used to re-raise it. */
187 static GTY(()) tree gnu_incoming_exc_ptr;
189 /* Stack for storing the current elaboration procedure decl. */
190 static GTY(()) vec<tree, va_gc> *gnu_elab_proc_stack;
192 /* Stack of labels to be used as a goto target instead of a return in
193 some functions. See processing for N_Subprogram_Body. */
194 static GTY(()) vec<tree, va_gc> *gnu_return_label_stack;
196 /* Stack of variable for the return value of a function with copy-in/copy-out
197 parameters. See processing for N_Subprogram_Body. */
198 static GTY(()) vec<tree, va_gc> *gnu_return_var_stack;
200 /* Structure used to record information for a range check. */
201 struct GTY(()) range_check_info_d {
202 tree low_bound;
203 tree high_bound;
204 tree type;
205 tree invariant_cond;
208 typedef struct range_check_info_d *range_check_info;
211 /* Structure used to record information for a loop. */
212 struct GTY(()) loop_info_d {
213 tree stmt;
214 tree loop_var;
215 vec<range_check_info, va_gc> *checks;
218 typedef struct loop_info_d *loop_info;
221 /* Stack of loop_info structures associated with LOOP_STMT nodes. */
222 static GTY(()) vec<loop_info, va_gc> *gnu_loop_stack;
224 /* The stacks for N_{Push,Pop}_*_Label. */
225 static GTY(()) vec<tree, va_gc> *gnu_constraint_error_label_stack;
226 static GTY(()) vec<tree, va_gc> *gnu_storage_error_label_stack;
227 static GTY(()) vec<tree, va_gc> *gnu_program_error_label_stack;
229 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
230 static enum tree_code gnu_codes[Number_Node_Kinds];
232 static void init_code_table (void);
233 static void Compilation_Unit_to_gnu (Node_Id);
234 static void record_code_position (Node_Id);
235 static void insert_code_for (Node_Id);
236 static void add_cleanup (tree, Node_Id);
237 static void add_stmt_list (List_Id);
238 static void push_exception_label_stack (vec<tree, va_gc> **, Entity_Id);
239 static tree build_stmt_group (List_Id, bool);
240 static inline bool stmt_group_may_fallthru (void);
241 static enum gimplify_status gnat_gimplify_stmt (tree *);
242 static void elaborate_all_entities (Node_Id);
243 static void process_freeze_entity (Node_Id);
244 static void process_decls (List_Id, List_Id, Node_Id, bool, bool);
245 static tree emit_range_check (tree, Node_Id, Node_Id);
246 static tree emit_index_check (tree, tree, tree, tree, Node_Id);
247 static tree emit_check (tree, tree, int, Node_Id);
248 static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id);
249 static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id);
250 static tree convert_with_check (Entity_Id, tree, bool, bool, bool, Node_Id);
251 static bool addressable_p (tree, tree);
252 static tree assoc_to_constructor (Entity_Id, Node_Id, tree);
253 static tree extract_values (tree, tree);
254 static tree pos_to_constructor (Node_Id, tree, Entity_Id);
255 static void validate_unchecked_conversion (Node_Id);
256 static tree maybe_implicit_deref (tree);
257 static void set_expr_location_from_node (tree, Node_Id);
258 static void set_expr_location_from_node1 (tree, Node_Id, bool);
259 static bool Sloc_to_locus1 (Source_Ptr, location_t *, bool);
260 static bool set_end_locus_from_node (tree, Node_Id);
261 static void set_gnu_expr_location_from_node (tree, Node_Id);
262 static int lvalue_required_p (Node_Id, tree, bool, bool, bool);
263 static tree build_raise_check (int, enum exception_info_kind);
264 static tree create_init_temporary (const char *, tree, tree *, Node_Id);
266 /* Hooks for debug info back-ends, only supported and used in a restricted set
267 of configurations. */
268 static const char *extract_encoding (const char *) ATTRIBUTE_UNUSED;
269 static const char *decode_name (const char *) ATTRIBUTE_UNUSED;
271 /* This is the main program of the back-end. It sets up all the table
272 structures and then generates code. */
274 void
275 gigi (Node_Id gnat_root,
276 int max_gnat_node,
277 int number_name ATTRIBUTE_UNUSED,
278 struct Node *nodes_ptr,
279 struct Flags *flags_ptr,
280 Node_Id *next_node_ptr,
281 Node_Id *prev_node_ptr,
282 struct Elist_Header *elists_ptr,
283 struct Elmt_Item *elmts_ptr,
284 struct String_Entry *strings_ptr,
285 Char_Code *string_chars_ptr,
286 struct List_Header *list_headers_ptr,
287 Nat number_file,
288 struct File_Info_Type *file_info_ptr,
289 Entity_Id standard_boolean,
290 Entity_Id standard_integer,
291 Entity_Id standard_character,
292 Entity_Id standard_long_long_float,
293 Entity_Id standard_exception_type,
294 Int gigi_operating_mode)
296 Node_Id gnat_iter;
297 Entity_Id gnat_literal;
298 tree t, ftype, int64_type;
299 struct elab_info *info;
300 int i;
302 max_gnat_nodes = max_gnat_node;
304 Nodes_Ptr = nodes_ptr;
305 Flags_Ptr = flags_ptr;
306 Next_Node_Ptr = next_node_ptr;
307 Prev_Node_Ptr = prev_node_ptr;
308 Elists_Ptr = elists_ptr;
309 Elmts_Ptr = elmts_ptr;
310 Strings_Ptr = strings_ptr;
311 String_Chars_Ptr = string_chars_ptr;
312 List_Headers_Ptr = list_headers_ptr;
314 type_annotate_only = (gigi_operating_mode == 1);
316 for (i = 0; i < number_file; i++)
318 /* Use the identifier table to make a permanent copy of the filename as
319 the name table gets reallocated after Gigi returns but before all the
320 debugging information is output. The __gnat_to_canonical_file_spec
321 call translates filenames from pragmas Source_Reference that contain
322 host style syntax not understood by gdb. */
323 const char *filename
324 = IDENTIFIER_POINTER
325 (get_identifier
326 (__gnat_to_canonical_file_spec
327 (Get_Name_String (file_info_ptr[i].File_Name))));
329 /* We rely on the order isomorphism between files and line maps. */
330 gcc_assert ((int) LINEMAPS_ORDINARY_USED (line_table) == i);
332 /* We create the line map for a source file at once, with a fixed number
333 of columns chosen to avoid jumping over the next power of 2. */
334 linemap_add (line_table, LC_ENTER, 0, filename, 1);
335 linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252);
336 linemap_position_for_column (line_table, 252 - 1);
337 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
340 gcc_assert (Nkind (gnat_root) == N_Compilation_Unit);
342 /* Declare the name of the compilation unit as the first global
343 name in order to make the middle-end fully deterministic. */
344 t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL);
345 first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t));
347 /* Initialize ourselves. */
348 init_code_table ();
349 init_gnat_decl ();
350 init_gnat_utils ();
352 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
353 errors. */
354 if (type_annotate_only)
356 TYPE_SIZE (void_type_node) = bitsize_zero_node;
357 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
360 /* Enable GNAT stack checking method if needed */
361 if (!Stack_Check_Probes_On_Target)
362 set_stack_check_libfunc ("_gnat_stack_check");
364 /* Retrieve alignment settings. */
365 double_float_alignment = get_target_double_float_alignment ();
366 double_scalar_alignment = get_target_double_scalar_alignment ();
368 /* Record the builtin types. Define `integer' and `character' first so that
369 dbx will output them first. */
370 record_builtin_type ("integer", integer_type_node, false);
371 record_builtin_type ("character", unsigned_char_type_node, false);
372 record_builtin_type ("boolean", boolean_type_node, false);
373 record_builtin_type ("void", void_type_node, false);
375 /* Save the type we made for integer as the type for Standard.Integer. */
376 save_gnu_tree (Base_Type (standard_integer),
377 TYPE_NAME (integer_type_node),
378 false);
380 /* Likewise for character as the type for Standard.Character. */
381 save_gnu_tree (Base_Type (standard_character),
382 TYPE_NAME (unsigned_char_type_node),
383 false);
385 /* Likewise for boolean as the type for Standard.Boolean. */
386 save_gnu_tree (Base_Type (standard_boolean),
387 TYPE_NAME (boolean_type_node),
388 false);
389 gnat_literal = First_Literal (Base_Type (standard_boolean));
390 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
391 gcc_assert (t == boolean_false_node);
392 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
393 boolean_type_node, t, true, false, false, false,
394 NULL, gnat_literal);
395 DECL_IGNORED_P (t) = 1;
396 save_gnu_tree (gnat_literal, t, false);
397 gnat_literal = Next_Literal (gnat_literal);
398 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
399 gcc_assert (t == boolean_true_node);
400 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
401 boolean_type_node, t, true, false, false, false,
402 NULL, gnat_literal);
403 DECL_IGNORED_P (t) = 1;
404 save_gnu_tree (gnat_literal, t, false);
406 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
407 ptr_void_ftype = build_pointer_type (void_ftype);
409 /* Now declare run-time functions. */
410 ftype = build_function_type_list (ptr_void_type_node, sizetype, NULL_TREE);
412 /* malloc is a function declaration tree for a function to allocate
413 memory. */
414 malloc_decl
415 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE,
416 ftype, NULL_TREE, is_disabled, true, true, true,
417 NULL, Empty);
418 DECL_IS_MALLOC (malloc_decl) = 1;
420 /* free is a function declaration tree for a function to free memory. */
421 free_decl
422 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
423 build_function_type_list (void_type_node,
424 ptr_void_type_node,
425 NULL_TREE),
426 NULL_TREE, is_disabled, true, true, true, NULL,
427 Empty);
429 /* This is used for 64-bit multiplication with overflow checking. */
430 int64_type = gnat_type_for_size (64, 0);
431 mulv64_decl
432 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE,
433 build_function_type_list (int64_type, int64_type,
434 int64_type, NULL_TREE),
435 NULL_TREE, is_disabled, true, true, true, NULL,
436 Empty);
438 /* Name of the _Parent field in tagged record types. */
439 parent_name_id = get_identifier (Get_Name_String (Name_uParent));
441 /* Name of the Exception_Data type defined in System.Standard_Library. */
442 exception_data_name_id
443 = get_identifier ("system__standard_library__exception_data");
445 /* Make the types and functions used for exception processing. */
446 jmpbuf_type
447 = build_array_type (gnat_type_for_mode (Pmode, 0),
448 build_index_type (size_int (5)));
449 record_builtin_type ("JMPBUF_T", jmpbuf_type, true);
450 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
452 /* Functions to get and set the jumpbuf pointer for the current thread. */
453 get_jmpbuf_decl
454 = create_subprog_decl
455 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
456 NULL_TREE, build_function_type_list (jmpbuf_ptr_type, NULL_TREE),
457 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
458 DECL_IGNORED_P (get_jmpbuf_decl) = 1;
460 set_jmpbuf_decl
461 = create_subprog_decl
462 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
463 NULL_TREE, build_function_type_list (void_type_node, jmpbuf_ptr_type,
464 NULL_TREE),
465 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
466 DECL_IGNORED_P (set_jmpbuf_decl) = 1;
468 /* setjmp returns an integer and has one operand, which is a pointer to
469 a jmpbuf. */
470 setjmp_decl
471 = create_subprog_decl
472 (get_identifier ("__builtin_setjmp"), NULL_TREE,
473 build_function_type_list (integer_type_node, jmpbuf_ptr_type,
474 NULL_TREE),
475 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
476 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
477 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
479 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
480 address. */
481 update_setjmp_buf_decl
482 = create_subprog_decl
483 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
484 build_function_type_list (void_type_node, jmpbuf_ptr_type, NULL_TREE),
485 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
486 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
487 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
489 /* Hooks to call when entering/leaving an exception handler. */
490 ftype
491 = build_function_type_list (void_type_node, ptr_void_type_node, NULL_TREE);
493 begin_handler_decl
494 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
495 ftype, NULL_TREE, is_disabled, true, true, true,
496 NULL, Empty);
497 DECL_IGNORED_P (begin_handler_decl) = 1;
499 end_handler_decl
500 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
501 ftype, NULL_TREE, is_disabled, true, true, true,
502 NULL, Empty);
503 DECL_IGNORED_P (end_handler_decl) = 1;
505 unhandled_except_decl
506 = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
507 NULL_TREE,
508 ftype, NULL_TREE, is_disabled, true, true, true,
509 NULL, Empty);
510 DECL_IGNORED_P (unhandled_except_decl) = 1;
512 reraise_zcx_decl
513 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE,
514 ftype, NULL_TREE, is_disabled, true, true, true,
515 NULL, Empty);
516 /* Indicate that these never return. */
517 DECL_IGNORED_P (reraise_zcx_decl) = 1;
518 TREE_THIS_VOLATILE (reraise_zcx_decl) = 1;
519 TREE_SIDE_EFFECTS (reraise_zcx_decl) = 1;
520 TREE_TYPE (reraise_zcx_decl)
521 = build_qualified_type (TREE_TYPE (reraise_zcx_decl), TYPE_QUAL_VOLATILE);
523 /* If in no exception handlers mode, all raise statements are redirected to
524 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl since
525 this procedure will never be called in this mode. */
526 if (No_Exception_Handlers_Set ())
528 tree decl
529 = create_subprog_decl
530 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE,
531 build_function_type_list (void_type_node,
532 build_pointer_type
533 (unsigned_char_type_node),
534 integer_type_node, NULL_TREE),
535 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
536 TREE_THIS_VOLATILE (decl) = 1;
537 TREE_SIDE_EFFECTS (decl) = 1;
538 TREE_TYPE (decl)
539 = build_qualified_type (TREE_TYPE (decl), TYPE_QUAL_VOLATILE);
540 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
541 gnat_raise_decls[i] = decl;
543 else
545 /* Otherwise, make one decl for each exception reason. */
546 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
547 gnat_raise_decls[i] = build_raise_check (i, exception_simple);
548 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++)
549 gnat_raise_decls_ext[i]
550 = build_raise_check (i,
551 i == CE_Index_Check_Failed
552 || i == CE_Range_Check_Failed
553 || i == CE_Invalid_Data
554 ? exception_range : exception_column);
557 /* Set the types that GCC and Gigi use from the front end. */
558 except_type_node = gnat_to_gnu_type (Base_Type (standard_exception_type));
560 /* Make other functions used for exception processing. */
561 get_excptr_decl
562 = create_subprog_decl
563 (get_identifier ("system__soft_links__get_gnat_exception"), NULL_TREE,
564 build_function_type_list (build_pointer_type (except_type_node),
565 NULL_TREE),
566 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
567 DECL_IGNORED_P (get_excptr_decl) = 1;
569 set_exception_parameter_decl
570 = create_subprog_decl
571 (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE,
572 build_function_type_list (void_type_node,
573 ptr_void_type_node,
574 ptr_void_type_node,
575 NULL_TREE),
576 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
578 raise_nodefer_decl
579 = create_subprog_decl
580 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE,
581 build_function_type_list (void_type_node,
582 build_pointer_type (except_type_node),
583 NULL_TREE),
584 NULL_TREE, is_disabled, true, true, true, NULL, Empty);
586 /* Indicate that it never returns. */
587 TREE_THIS_VOLATILE (raise_nodefer_decl) = 1;
588 TREE_SIDE_EFFECTS (raise_nodefer_decl) = 1;
589 TREE_TYPE (raise_nodefer_decl)
590 = build_qualified_type (TREE_TYPE (raise_nodefer_decl),
591 TYPE_QUAL_VOLATILE);
593 /* Build the special descriptor type and its null node if needed. */
594 if (TARGET_VTABLE_USES_DESCRIPTORS)
596 tree null_node = fold_convert (ptr_void_ftype, null_pointer_node);
597 tree field_list = NULL_TREE;
598 int j;
599 vec<constructor_elt, va_gc> *null_vec = NULL;
600 constructor_elt *elt;
602 fdesc_type_node = make_node (RECORD_TYPE);
603 vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS);
604 elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
606 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
608 tree field
609 = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node,
610 NULL_TREE, NULL_TREE, 0, 1);
611 DECL_CHAIN (field) = field_list;
612 field_list = field;
613 elt->index = field;
614 elt->value = null_node;
615 elt--;
618 finish_record_type (fdesc_type_node, nreverse (field_list), 0, false);
619 record_builtin_type ("descriptor", fdesc_type_node, true);
620 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec);
623 longest_float_type_node
624 = get_unpadded_type (Base_Type (standard_long_long_float));
626 /* Dummy objects to materialize "others" and "all others" in the exception
627 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
628 the types to use. */
629 others_decl
630 = create_var_decl (get_identifier ("OTHERS"),
631 get_identifier ("__gnat_others_value"),
632 unsigned_char_type_node,
633 NULL_TREE, true, false, true, false, NULL, Empty);
635 all_others_decl
636 = create_var_decl (get_identifier ("ALL_OTHERS"),
637 get_identifier ("__gnat_all_others_value"),
638 unsigned_char_type_node,
639 NULL_TREE, true, false, true, false, NULL, Empty);
641 unhandled_others_decl
642 = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
643 get_identifier ("__gnat_unhandled_others_value"),
644 unsigned_char_type_node,
645 NULL_TREE, true, false, true, false, NULL, Empty);
647 main_identifier_node = get_identifier ("main");
649 /* Install the builtins we might need, either internally or as
650 user available facilities for Intrinsic imports. */
651 gnat_install_builtins ();
653 vec_safe_push (gnu_except_ptr_stack, NULL_TREE);
654 vec_safe_push (gnu_constraint_error_label_stack, NULL_TREE);
655 vec_safe_push (gnu_storage_error_label_stack, NULL_TREE);
656 vec_safe_push (gnu_program_error_label_stack, NULL_TREE);
658 /* Process any Pragma Ident for the main unit. */
659 if (Present (Ident_String (Main_Unit)))
660 targetm.asm_out.output_ident
661 (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
663 /* If we are using the GCC exception mechanism, let GCC know. */
664 if (Exception_Mechanism == Back_End_Exceptions)
665 gnat_init_gcc_eh ();
667 /* Initialize the GCC support for FP operations. */
668 gnat_init_gcc_fp ();
670 /* Now translate the compilation unit proper. */
671 Compilation_Unit_to_gnu (gnat_root);
673 /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at
674 the very end to avoid having to second-guess the front-end when we run
675 into dummy nodes during the regular processing. */
676 for (i = 0; gnat_validate_uc_list.iterate (i, &gnat_iter); i++)
677 validate_unchecked_conversion (gnat_iter);
678 gnat_validate_uc_list.release ();
680 /* Finally see if we have any elaboration procedures to deal with. */
681 for (info = elab_info_list; info; info = info->next)
683 tree gnu_body = DECL_SAVED_TREE (info->elab_proc), gnu_stmts;
685 /* We should have a BIND_EXPR but it may not have any statements in it.
686 If it doesn't have any, we have nothing to do except for setting the
687 flag on the GNAT node. Otherwise, process the function as others. */
688 gnu_stmts = gnu_body;
689 if (TREE_CODE (gnu_stmts) == BIND_EXPR)
690 gnu_stmts = BIND_EXPR_BODY (gnu_stmts);
691 if (!gnu_stmts || !STATEMENT_LIST_HEAD (gnu_stmts))
692 Set_Has_No_Elaboration_Code (info->gnat_node, 1);
693 else
695 begin_subprog_body (info->elab_proc);
696 end_subprog_body (gnu_body);
697 rest_of_subprog_body_compilation (info->elab_proc);
701 /* Destroy ourselves. */
702 destroy_gnat_decl ();
703 destroy_gnat_utils ();
705 /* We cannot track the location of errors past this point. */
706 error_gnat_node = Empty;
709 /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given
710 CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */
712 static tree
713 build_raise_check (int check, enum exception_info_kind kind)
715 tree result, ftype;
716 const char pfx[] = "__gnat_rcheck_";
718 strcpy (Name_Buffer, pfx);
719 Name_Len = sizeof (pfx) - 1;
720 Get_RT_Exception_Name (check);
722 if (kind == exception_simple)
724 Name_Buffer[Name_Len] = 0;
725 ftype
726 = build_function_type_list (void_type_node,
727 build_pointer_type
728 (unsigned_char_type_node),
729 integer_type_node, NULL_TREE);
731 else
733 tree t = (kind == exception_column ? NULL_TREE : integer_type_node);
735 strcpy (Name_Buffer + Name_Len, "_ext");
736 Name_Buffer[Name_Len + 4] = 0;
737 ftype
738 = build_function_type_list (void_type_node,
739 build_pointer_type
740 (unsigned_char_type_node),
741 integer_type_node, integer_type_node,
742 t, t, NULL_TREE);
745 result
746 = create_subprog_decl (get_identifier (Name_Buffer),
747 NULL_TREE, ftype, NULL_TREE,
748 is_disabled, true, true, true, NULL, Empty);
750 /* Indicate that it never returns. */
751 TREE_THIS_VOLATILE (result) = 1;
752 TREE_SIDE_EFFECTS (result) = 1;
753 TREE_TYPE (result)
754 = build_qualified_type (TREE_TYPE (result), TYPE_QUAL_VOLATILE);
756 return result;
759 /* Return a positive value if an lvalue is required for GNAT_NODE, which is
760 an N_Attribute_Reference. */
762 static int
763 lvalue_required_for_attribute_p (Node_Id gnat_node)
765 switch (Get_Attribute_Id (Attribute_Name (gnat_node)))
767 case Attr_Pos:
768 case Attr_Val:
769 case Attr_Pred:
770 case Attr_Succ:
771 case Attr_First:
772 case Attr_Last:
773 case Attr_Range_Length:
774 case Attr_Length:
775 case Attr_Object_Size:
776 case Attr_Value_Size:
777 case Attr_Component_Size:
778 case Attr_Descriptor_Size:
779 case Attr_Max_Size_In_Storage_Elements:
780 case Attr_Min:
781 case Attr_Max:
782 case Attr_Null_Parameter:
783 case Attr_Passed_By_Reference:
784 case Attr_Mechanism_Code:
785 case Attr_Machine:
786 case Attr_Model:
787 return 0;
789 case Attr_Address:
790 case Attr_Access:
791 case Attr_Unchecked_Access:
792 case Attr_Unrestricted_Access:
793 case Attr_Code_Address:
794 case Attr_Pool_Address:
795 case Attr_Size:
796 case Attr_Alignment:
797 case Attr_Bit_Position:
798 case Attr_Position:
799 case Attr_First_Bit:
800 case Attr_Last_Bit:
801 case Attr_Bit:
802 case Attr_Asm_Input:
803 case Attr_Asm_Output:
804 default:
805 return 1;
809 /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE
810 is the type that will be used for GNAT_NODE in the translated GNU tree.
811 CONSTANT indicates whether the underlying object represented by GNAT_NODE
812 is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates
813 whether its value is the address of a constant and ALIASED whether it is
814 aliased. If it isn't, ADDRESS_OF_CONSTANT and ALIASED are ignored.
816 The function climbs up the GNAT tree starting from the node and returns 1
817 upon encountering a node that effectively requires an lvalue downstream.
818 It returns int instead of bool to facilitate usage in non-purely binary
819 logic contexts. */
821 static int
822 lvalue_required_p (Node_Id gnat_node, tree gnu_type, bool constant,
823 bool address_of_constant, bool aliased)
825 Node_Id gnat_parent = Parent (gnat_node), gnat_temp;
827 switch (Nkind (gnat_parent))
829 case N_Reference:
830 return 1;
832 case N_Attribute_Reference:
833 return lvalue_required_for_attribute_p (gnat_parent);
835 case N_Parameter_Association:
836 case N_Function_Call:
837 case N_Procedure_Call_Statement:
838 /* If the parameter is by reference, an lvalue is required. */
839 return (!constant
840 || must_pass_by_ref (gnu_type)
841 || default_pass_by_ref (gnu_type));
843 case N_Indexed_Component:
844 /* Only the array expression can require an lvalue. */
845 if (Prefix (gnat_parent) != gnat_node)
846 return 0;
848 /* ??? Consider that referencing an indexed component with a
849 non-constant index forces the whole aggregate to memory.
850 Note that N_Integer_Literal is conservative, any static
851 expression in the RM sense could probably be accepted. */
852 for (gnat_temp = First (Expressions (gnat_parent));
853 Present (gnat_temp);
854 gnat_temp = Next (gnat_temp))
855 if (Nkind (gnat_temp) != N_Integer_Literal)
856 return 1;
858 /* ... fall through ... */
860 case N_Slice:
861 /* Only the array expression can require an lvalue. */
862 if (Prefix (gnat_parent) != gnat_node)
863 return 0;
865 aliased |= Has_Aliased_Components (Etype (gnat_node));
866 return lvalue_required_p (gnat_parent, gnu_type, constant,
867 address_of_constant, aliased);
869 case N_Selected_Component:
870 aliased |= Is_Aliased (Entity (Selector_Name (gnat_parent)));
871 return lvalue_required_p (gnat_parent, gnu_type, constant,
872 address_of_constant, aliased);
874 case N_Object_Renaming_Declaration:
875 /* We need to preserve addresses through a renaming. */
876 return 1;
878 case N_Object_Declaration:
879 /* We cannot use a constructor if this is an atomic object because
880 the actual assignment might end up being done component-wise. */
881 return (!constant
882 ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
883 && Is_Atomic (Defining_Entity (gnat_parent)))
884 /* We don't use a constructor if this is a class-wide object
885 because the effective type of the object is the equivalent
886 type of the class-wide subtype and it smashes most of the
887 data into an array of bytes to which we cannot convert. */
888 || Ekind ((Etype (Defining_Entity (gnat_parent))))
889 == E_Class_Wide_Subtype);
891 case N_Assignment_Statement:
892 /* We cannot use a constructor if the LHS is an atomic object because
893 the actual assignment might end up being done component-wise. */
894 return (!constant
895 || Name (gnat_parent) == gnat_node
896 || (Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
897 && Is_Atomic (Entity (Name (gnat_parent)))));
899 case N_Unchecked_Type_Conversion:
900 if (!constant)
901 return 1;
903 /* ... fall through ... */
905 case N_Type_Conversion:
906 case N_Qualified_Expression:
907 /* We must look through all conversions because we may need to bypass
908 an intermediate conversion that is meant to be purely formal. */
909 return lvalue_required_p (gnat_parent,
910 get_unpadded_type (Etype (gnat_parent)),
911 constant, address_of_constant, aliased);
913 case N_Allocator:
914 /* We should only reach here through the N_Qualified_Expression case.
915 Force an lvalue for composite types since a block-copy to the newly
916 allocated area of memory is made. */
917 return Is_Composite_Type (Underlying_Type (Etype (gnat_node)));
919 case N_Explicit_Dereference:
920 /* We look through dereferences for address of constant because we need
921 to handle the special cases listed above. */
922 if (constant && address_of_constant)
923 return lvalue_required_p (gnat_parent,
924 get_unpadded_type (Etype (gnat_parent)),
925 true, false, true);
927 /* ... fall through ... */
929 default:
930 return 0;
933 gcc_unreachable ();
936 /* Return true if T is a constant DECL node that can be safely replaced
937 by its initializer. */
939 static bool
940 constant_decl_with_initializer_p (tree t)
942 if (!TREE_CONSTANT (t) || !DECL_P (t) || !DECL_INITIAL (t))
943 return false;
945 /* Return false for aggregate types that contain a placeholder since
946 their initializers cannot be manipulated easily. */
947 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
948 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (t))
949 && type_contains_placeholder_p (TREE_TYPE (t)))
950 return false;
952 return true;
955 /* Return an expression equivalent to EXP but where constant DECL nodes
956 have been replaced by their initializer. */
958 static tree
959 fold_constant_decl_in_expr (tree exp)
961 enum tree_code code = TREE_CODE (exp);
962 tree op0;
964 switch (code)
966 case CONST_DECL:
967 case VAR_DECL:
968 if (!constant_decl_with_initializer_p (exp))
969 return exp;
971 return DECL_INITIAL (exp);
973 case BIT_FIELD_REF:
974 case COMPONENT_REF:
975 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
976 if (op0 == TREE_OPERAND (exp, 0))
977 return exp;
979 return fold_build3 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1),
980 TREE_OPERAND (exp, 2));
982 case ARRAY_REF:
983 case ARRAY_RANGE_REF:
984 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
985 if (op0 == TREE_OPERAND (exp, 0))
986 return exp;
988 return fold (build4 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1),
989 TREE_OPERAND (exp, 2), TREE_OPERAND (exp, 3)));
991 case VIEW_CONVERT_EXPR:
992 case REALPART_EXPR:
993 case IMAGPART_EXPR:
994 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
995 if (op0 == TREE_OPERAND (exp, 0))
996 return exp;
998 return fold_build1 (code, TREE_TYPE (exp), op0);
1000 default:
1001 return exp;
1004 gcc_unreachable ();
1007 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
1008 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer
1009 to where we should place the result type. */
1011 static tree
1012 Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
1014 Node_Id gnat_temp, gnat_temp_type;
1015 tree gnu_result, gnu_result_type;
1017 /* Whether we should require an lvalue for GNAT_NODE. Needed in
1018 specific circumstances only, so evaluated lazily. < 0 means
1019 unknown, > 0 means known true, 0 means known false. */
1020 int require_lvalue = -1;
1022 /* If GNAT_NODE is a constant, whether we should use the initialization
1023 value instead of the constant entity, typically for scalars with an
1024 address clause when the parent doesn't require an lvalue. */
1025 bool use_constant_initializer = false;
1027 /* If the Etype of this node does not equal the Etype of the Entity,
1028 something is wrong with the entity map, probably in generic
1029 instantiation. However, this does not apply to types. Since we sometime
1030 have strange Ekind's, just do this test for objects. Also, if the Etype of
1031 the Entity is private, the Etype of the N_Identifier is allowed to be the
1032 full type and also we consider a packed array type to be the same as the
1033 original type. Similarly, a class-wide type is equivalent to a subtype of
1034 itself. Finally, if the types are Itypes, one may be a copy of the other,
1035 which is also legal. */
1036 gnat_temp = (Nkind (gnat_node) == N_Defining_Identifier
1037 ? gnat_node : Entity (gnat_node));
1038 gnat_temp_type = Etype (gnat_temp);
1040 gcc_assert (Etype (gnat_node) == gnat_temp_type
1041 || (Is_Packed (gnat_temp_type)
1042 && (Etype (gnat_node)
1043 == Packed_Array_Impl_Type (gnat_temp_type)))
1044 || (Is_Class_Wide_Type (Etype (gnat_node)))
1045 || (IN (Ekind (gnat_temp_type), Private_Kind)
1046 && Present (Full_View (gnat_temp_type))
1047 && ((Etype (gnat_node) == Full_View (gnat_temp_type))
1048 || (Is_Packed (Full_View (gnat_temp_type))
1049 && (Etype (gnat_node)
1050 == Packed_Array_Impl_Type
1051 (Full_View (gnat_temp_type))))))
1052 || (Is_Itype (Etype (gnat_node)) && Is_Itype (gnat_temp_type))
1053 || !(Ekind (gnat_temp) == E_Variable
1054 || Ekind (gnat_temp) == E_Component
1055 || Ekind (gnat_temp) == E_Constant
1056 || Ekind (gnat_temp) == E_Loop_Parameter
1057 || IN (Ekind (gnat_temp), Formal_Kind)));
1059 /* If this is a reference to a deferred constant whose partial view is an
1060 unconstrained private type, the proper type is on the full view of the
1061 constant, not on the full view of the type, which may be unconstrained.
1063 This may be a reference to a type, for example in the prefix of the
1064 attribute Position, generated for dispatching code (see Make_DT in
1065 exp_disp,adb). In that case we need the type itself, not is parent,
1066 in particular if it is a derived type */
1067 if (Ekind (gnat_temp) == E_Constant
1068 && Is_Private_Type (gnat_temp_type)
1069 && (Has_Unknown_Discriminants (gnat_temp_type)
1070 || (Present (Full_View (gnat_temp_type))
1071 && Has_Discriminants (Full_View (gnat_temp_type))))
1072 && Present (Full_View (gnat_temp)))
1074 gnat_temp = Full_View (gnat_temp);
1075 gnat_temp_type = Etype (gnat_temp);
1077 else
1079 /* We want to use the Actual_Subtype if it has already been elaborated,
1080 otherwise the Etype. Avoid using Actual_Subtype for packed arrays to
1081 simplify things. */
1082 if ((Ekind (gnat_temp) == E_Constant
1083 || Ekind (gnat_temp) == E_Variable || Is_Formal (gnat_temp))
1084 && !(Is_Array_Type (Etype (gnat_temp))
1085 && Present (Packed_Array_Impl_Type (Etype (gnat_temp))))
1086 && Present (Actual_Subtype (gnat_temp))
1087 && present_gnu_tree (Actual_Subtype (gnat_temp)))
1088 gnat_temp_type = Actual_Subtype (gnat_temp);
1089 else
1090 gnat_temp_type = Etype (gnat_node);
1093 /* Expand the type of this identifier first, in case it is an enumeral
1094 literal, which only get made when the type is expanded. There is no
1095 order-of-elaboration issue here. */
1096 gnu_result_type = get_unpadded_type (gnat_temp_type);
1098 /* If this is a non-imported elementary constant with an address clause,
1099 retrieve the value instead of a pointer to be dereferenced unless
1100 an lvalue is required. This is generally more efficient and actually
1101 required if this is a static expression because it might be used
1102 in a context where a dereference is inappropriate, such as a case
1103 statement alternative or a record discriminant. There is no possible
1104 volatile-ness short-circuit here since Volatile constants must be
1105 imported per C.6. */
1106 if (Ekind (gnat_temp) == E_Constant
1107 && Is_Elementary_Type (gnat_temp_type)
1108 && !Is_Imported (gnat_temp)
1109 && Present (Address_Clause (gnat_temp)))
1111 require_lvalue = lvalue_required_p (gnat_node, gnu_result_type, true,
1112 false, Is_Aliased (gnat_temp));
1113 use_constant_initializer = !require_lvalue;
1116 if (use_constant_initializer)
1118 /* If this is a deferred constant, the initializer is attached to
1119 the full view. */
1120 if (Present (Full_View (gnat_temp)))
1121 gnat_temp = Full_View (gnat_temp);
1123 gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_temp)));
1125 else
1126 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0);
1128 /* Some objects (such as parameters passed by reference, globals of
1129 variable size, and renamed objects) actually represent the address
1130 of the object. In that case, we must do the dereference. Likewise,
1131 deal with parameters to foreign convention subprograms. */
1132 if (DECL_P (gnu_result)
1133 && (DECL_BY_REF_P (gnu_result)
1134 || (TREE_CODE (gnu_result) == PARM_DECL
1135 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
1137 const bool read_only = DECL_POINTS_TO_READONLY_P (gnu_result);
1139 /* If it's a PARM_DECL to foreign convention subprogram, convert it. */
1140 if (TREE_CODE (gnu_result) == PARM_DECL
1141 && DECL_BY_COMPONENT_PTR_P (gnu_result))
1142 gnu_result
1143 = convert (build_pointer_type (gnu_result_type), gnu_result);
1145 /* If it's a CONST_DECL, return the underlying constant like below. */
1146 else if (TREE_CODE (gnu_result) == CONST_DECL
1147 && !(DECL_CONST_ADDRESS_P (gnu_result)
1148 && lvalue_required_p (gnat_node, gnu_result_type, true,
1149 true, false)))
1150 gnu_result = DECL_INITIAL (gnu_result);
1152 /* If it's a renaming pointer and, either the renamed object is constant
1153 or we are at the right binding level, we can reference the renamed
1154 object directly, since it is constant or has been protected against
1155 multiple evaluations. */
1156 if (TREE_CODE (gnu_result) == VAR_DECL
1157 && !DECL_LOOP_PARM_P (gnu_result)
1158 && DECL_RENAMED_OBJECT (gnu_result)
1159 && (TREE_CONSTANT (DECL_RENAMED_OBJECT (gnu_result))
1160 || !DECL_RENAMING_GLOBAL_P (gnu_result)
1161 || global_bindings_p ()))
1162 gnu_result = DECL_RENAMED_OBJECT (gnu_result);
1164 /* Otherwise, do the final dereference. */
1165 else
1167 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
1169 if ((TREE_CODE (gnu_result) == INDIRECT_REF
1170 || TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
1171 && No (Address_Clause (gnat_temp)))
1172 TREE_THIS_NOTRAP (gnu_result) = 1;
1174 if (read_only)
1175 TREE_READONLY (gnu_result) = 1;
1179 /* If we have a constant declaration and its initializer, try to return the
1180 latter to avoid the need to call fold in lots of places and the need for
1181 elaboration code if this identifier is used as an initializer itself. */
1182 if (constant_decl_with_initializer_p (gnu_result))
1184 bool constant_only = (TREE_CODE (gnu_result) == CONST_DECL
1185 && !DECL_CONST_CORRESPONDING_VAR (gnu_result));
1186 bool address_of_constant = (TREE_CODE (gnu_result) == CONST_DECL
1187 && DECL_CONST_ADDRESS_P (gnu_result));
1189 /* If there is a (corresponding) variable or this is the address of a
1190 constant, we only want to return the initializer if an lvalue isn't
1191 required. Evaluate this now if we have not already done so. */
1192 if ((!constant_only || address_of_constant) && require_lvalue < 0)
1193 require_lvalue
1194 = lvalue_required_p (gnat_node, gnu_result_type, true,
1195 address_of_constant, Is_Aliased (gnat_temp));
1197 /* Finally retrieve the initializer if this is deemed valid. */
1198 if ((constant_only && !address_of_constant) || !require_lvalue)
1199 gnu_result = DECL_INITIAL (gnu_result);
1202 /* But for a constant renaming we couldn't do that incrementally for its
1203 definition because of the need to return an lvalue so, if the present
1204 context doesn't itself require an lvalue, we try again here. */
1205 else if (Ekind (gnat_temp) == E_Constant
1206 && Is_Elementary_Type (gnat_temp_type)
1207 && Present (Renamed_Object (gnat_temp)))
1209 if (require_lvalue < 0)
1210 require_lvalue
1211 = lvalue_required_p (gnat_node, gnu_result_type, true, false,
1212 Is_Aliased (gnat_temp));
1213 if (!require_lvalue)
1214 gnu_result = fold_constant_decl_in_expr (gnu_result);
1217 /* The GNAT tree has the type of a function set to its result type, so we
1218 adjust here. Also use the type of the result if the Etype is a subtype
1219 that is nominally unconstrained. Likewise if this is a deferred constant
1220 of a discriminated type whose full view can be elaborated statically, to
1221 avoid problematic conversions to the nominal subtype. But remove any
1222 padding from the resulting type. */
1223 if (TREE_CODE (TREE_TYPE (gnu_result)) == FUNCTION_TYPE
1224 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type)
1225 || (Ekind (gnat_temp) == E_Constant
1226 && Present (Full_View (gnat_temp))
1227 && Has_Discriminants (gnat_temp_type)
1228 && TREE_CODE (gnu_result) == CONSTRUCTOR))
1230 gnu_result_type = TREE_TYPE (gnu_result);
1231 if (TYPE_IS_PADDING_P (gnu_result_type))
1232 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
1235 *gnu_result_type_p = gnu_result_type;
1237 return gnu_result;
1240 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
1241 any statements we generate. */
1243 static tree
1244 Pragma_to_gnu (Node_Id gnat_node)
1246 tree gnu_result = alloc_stmt_list ();
1247 unsigned char pragma_id;
1248 Node_Id gnat_temp;
1250 /* Do nothing if we are just annotating types and check for (and ignore)
1251 unrecognized pragmas. */
1252 if (type_annotate_only
1253 || !Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node))))
1254 return gnu_result;
1256 pragma_id = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
1257 switch (pragma_id)
1259 case Pragma_Inspection_Point:
1260 /* Do nothing at top level: all such variables are already viewable. */
1261 if (global_bindings_p ())
1262 break;
1264 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1265 Present (gnat_temp);
1266 gnat_temp = Next (gnat_temp))
1268 Node_Id gnat_expr = Expression (gnat_temp);
1269 tree gnu_expr = gnat_to_gnu (gnat_expr);
1270 int use_address;
1271 machine_mode mode;
1272 tree asm_constraint = NULL_TREE;
1273 #ifdef ASM_COMMENT_START
1274 char *comment;
1275 #endif
1277 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
1278 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1280 /* Use the value only if it fits into a normal register,
1281 otherwise use the address. */
1282 mode = TYPE_MODE (TREE_TYPE (gnu_expr));
1283 use_address = ((GET_MODE_CLASS (mode) != MODE_INT
1284 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1285 || GET_MODE_SIZE (mode) > UNITS_PER_WORD);
1287 if (use_address)
1288 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
1290 #ifdef ASM_COMMENT_START
1291 comment = concat (ASM_COMMENT_START,
1292 " inspection point: ",
1293 Get_Name_String (Chars (gnat_expr)),
1294 use_address ? " address" : "",
1295 " is in %0",
1296 NULL);
1297 asm_constraint = build_string (strlen (comment), comment);
1298 free (comment);
1299 #endif
1300 gnu_expr = build5 (ASM_EXPR, void_type_node,
1301 asm_constraint,
1302 NULL_TREE,
1303 tree_cons
1304 (build_tree_list (NULL_TREE,
1305 build_string (1, "g")),
1306 gnu_expr, NULL_TREE),
1307 NULL_TREE, NULL_TREE);
1308 ASM_VOLATILE_P (gnu_expr) = 1;
1309 set_expr_location_from_node (gnu_expr, gnat_node);
1310 append_to_statement_list (gnu_expr, &gnu_result);
1312 break;
1314 case Pragma_Loop_Optimize:
1315 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1316 Present (gnat_temp);
1317 gnat_temp = Next (gnat_temp))
1319 tree gnu_loop_stmt = gnu_loop_stack->last ()->stmt;
1321 switch (Chars (Expression (gnat_temp)))
1323 case Name_Ivdep:
1324 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
1325 break;
1327 case Name_No_Unroll:
1328 LOOP_STMT_NO_UNROLL (gnu_loop_stmt) = 1;
1329 break;
1331 case Name_Unroll:
1332 LOOP_STMT_UNROLL (gnu_loop_stmt) = 1;
1333 break;
1335 case Name_No_Vector:
1336 LOOP_STMT_NO_VECTOR (gnu_loop_stmt) = 1;
1337 break;
1339 case Name_Vector:
1340 LOOP_STMT_VECTOR (gnu_loop_stmt) = 1;
1341 break;
1343 default:
1344 gcc_unreachable ();
1347 break;
1349 case Pragma_Optimize:
1350 switch (Chars (Expression
1351 (First (Pragma_Argument_Associations (gnat_node)))))
1353 case Name_Off:
1354 if (optimize)
1355 post_error ("must specify -O0?", gnat_node);
1356 break;
1358 case Name_Space:
1359 if (!optimize_size)
1360 post_error ("must specify -Os?", gnat_node);
1361 break;
1363 case Name_Time:
1364 if (!optimize)
1365 post_error ("insufficient -O value?", gnat_node);
1366 break;
1368 default:
1369 gcc_unreachable ();
1371 break;
1373 case Pragma_Reviewable:
1374 if (write_symbols == NO_DEBUG)
1375 post_error ("must specify -g?", gnat_node);
1376 break;
1378 case Pragma_Warning_As_Error:
1379 case Pragma_Warnings:
1381 Node_Id gnat_expr;
1382 /* Preserve the location of the pragma. */
1383 const location_t location = input_location;
1384 struct cl_option_handlers handlers;
1385 unsigned int option_index;
1386 diagnostic_t kind;
1387 bool imply;
1389 gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1391 /* This is the String form: pragma Warning{s|_As_Error}(String). */
1392 if (Nkind (Expression (gnat_temp)) == N_String_Literal)
1394 switch (pragma_id)
1396 case Pragma_Warning_As_Error:
1397 kind = DK_ERROR;
1398 imply = false;
1399 break;
1401 case Pragma_Warnings:
1402 kind = DK_WARNING;
1403 imply = true;
1404 break;
1406 default:
1407 gcc_unreachable ();
1410 gnat_expr = Expression (gnat_temp);
1413 /* This is the On/Off form: pragma Warnings (On | Off [,String]). */
1414 else if (Nkind (Expression (gnat_temp)) == N_Identifier)
1416 switch (Chars (Expression (gnat_temp)))
1418 case Name_Off:
1419 kind = DK_IGNORED;
1420 break;
1422 case Name_On:
1423 kind = DK_WARNING;
1424 break;
1426 default:
1427 gcc_unreachable ();
1430 /* Deal with optional pattern (but ignore Reason => "..."). */
1431 if (Present (Next (gnat_temp)) && No (Chars (Next (gnat_temp))))
1433 /* pragma Warnings (On | Off, Name) is handled differently. */
1434 if (Nkind (Expression (Next (gnat_temp))) != N_String_Literal)
1435 break;
1437 gnat_expr = Expression (Next (gnat_temp));
1439 else
1440 gnat_expr = Empty;
1442 imply = false;
1445 else
1446 gcc_unreachable ();
1448 /* This is the same implementation as in the C family of compilers. */
1449 if (Present (gnat_expr))
1451 tree gnu_expr = gnat_to_gnu (gnat_expr);
1452 const char *opt_string = TREE_STRING_POINTER (gnu_expr);
1453 const int len = TREE_STRING_LENGTH (gnu_expr);
1454 if (len < 3 || opt_string[0] != '-' || opt_string[1] != 'W')
1455 break;
1456 for (option_index = 0;
1457 option_index < cl_options_count;
1458 option_index++)
1459 if (strcmp (cl_options[option_index].opt_text, opt_string) == 0)
1460 break;
1461 if (option_index == cl_options_count)
1463 post_error ("unknown -W switch", gnat_node);
1464 break;
1467 else
1468 option_index = 0;
1470 set_default_handlers (&handlers);
1471 control_warning_option (option_index, (int) kind, imply, location,
1472 CL_Ada, &handlers, &global_options,
1473 &global_options_set, global_dc);
1475 break;
1477 default:
1478 break;
1481 return gnu_result;
1485 /* Check the inlining status of nested function FNDECL in the current context.
1487 If a non-inline nested function is referenced from an inline external
1488 function, we cannot honor both requests at the same time without cloning
1489 the nested function in the current unit since it is private to its unit.
1490 We could inline it as well but it's probably better to err on the side
1491 of too little inlining.
1493 This must be invoked only on nested functions present in the source code
1494 and not on nested functions generated by the compiler, e.g. finalizers,
1495 because they are not marked inline and we don't want them to block the
1496 inlining of the parent function. */
1498 static void
1499 check_inlining_for_nested_subprog (tree fndecl)
1501 if (!DECL_DECLARED_INLINE_P (fndecl)
1502 && current_function_decl
1503 && DECL_EXTERNAL (current_function_decl)
1504 && DECL_DECLARED_INLINE_P (current_function_decl))
1506 const location_t loc1 = DECL_SOURCE_LOCATION (fndecl);
1507 const location_t loc2 = DECL_SOURCE_LOCATION (current_function_decl);
1509 if (lookup_attribute ("always_inline",
1510 DECL_ATTRIBUTES (current_function_decl)))
1512 error_at (loc1, "subprogram %q+F not marked Inline_Always", fndecl);
1513 error_at (loc2, "parent subprogram cannot be inlined");
1515 else
1517 warning_at (loc1, OPT_Winline, "subprogram %q+F not marked Inline",
1518 fndecl);
1519 warning_at (loc2, OPT_Winline, "parent subprogram cannot be inlined");
1522 DECL_DECLARED_INLINE_P (current_function_decl) = 0;
1523 DECL_UNINLINABLE (current_function_decl) = 1;
1527 /* Return an expression for the length of TYPE, an integral type, computed in
1528 RESULT_TYPE, another integral type.
1530 We used to compute the length as MAX (hb - lb + 1, 0) which could overflow
1531 when lb == TYPE'First. We now compute it as (hb >= lb) ? hb - lb + 1 : 0
1532 which would only overflow in much rarer cases, for extremely large arrays
1533 we expect never to encounter in practice. Besides, the former computation
1534 required the use of potentially constraining signed arithmetics while the
1535 latter does not. Note that the comparison must be done in the original
1536 base index type in order to avoid any overflow during the conversion. */
1538 static tree
1539 get_type_length (tree type, tree result_type)
1541 tree comp_type = get_base_type (result_type);
1542 tree base_type = get_base_type (type);
1543 tree lb = convert (base_type, TYPE_MIN_VALUE (type));
1544 tree hb = convert (base_type, TYPE_MAX_VALUE (type));
1545 tree length
1546 = build_binary_op (PLUS_EXPR, comp_type,
1547 build_binary_op (MINUS_EXPR, comp_type,
1548 convert (comp_type, hb),
1549 convert (comp_type, lb)),
1550 convert (comp_type, integer_one_node));
1551 length
1552 = build_cond_expr (result_type,
1553 build_binary_op (GE_EXPR, boolean_type_node, hb, lb),
1554 convert (result_type, length),
1555 convert (result_type, integer_zero_node));
1556 return length;
1559 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node,
1560 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
1561 where we should place the result type. ATTRIBUTE is the attribute ID. */
1563 static tree
1564 Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
1566 const Node_Id gnat_prefix = Prefix (gnat_node);
1567 tree gnu_prefix, gnu_type, gnu_expr;
1568 tree gnu_result_type, gnu_result = error_mark_node;
1569 bool prefix_unused = false;
1571 /* ??? If this is an access attribute for a public subprogram to be used in
1572 a dispatch table, do not translate its type as it's useless there and the
1573 parameter types might be incomplete types coming from a limited with. */
1574 if (Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
1575 && Is_Dispatch_Table_Entity (Etype (gnat_node))
1576 && Nkind (gnat_prefix) == N_Identifier
1577 && Is_Subprogram (Entity (gnat_prefix))
1578 && Is_Public (Entity (gnat_prefix))
1579 && !present_gnu_tree (Entity (gnat_prefix)))
1580 gnu_prefix = get_minimal_subprog_decl (Entity (gnat_prefix));
1581 else
1582 gnu_prefix = gnat_to_gnu (gnat_prefix);
1583 gnu_type = TREE_TYPE (gnu_prefix);
1585 /* If the input is a NULL_EXPR, make a new one. */
1586 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
1588 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1589 *gnu_result_type_p = gnu_result_type;
1590 return build1 (NULL_EXPR, gnu_result_type, TREE_OPERAND (gnu_prefix, 0));
1593 switch (attribute)
1595 case Attr_Pos:
1596 case Attr_Val:
1597 /* These are just conversions since representation clauses for
1598 enumeration types are handled in the front-end. */
1600 bool checkp = Do_Range_Check (First (Expressions (gnat_node)));
1601 gnu_result = gnat_to_gnu (First (Expressions (gnat_node)));
1602 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1603 gnu_result = convert_with_check (Etype (gnat_node), gnu_result,
1604 checkp, checkp, true, gnat_node);
1606 break;
1608 case Attr_Pred:
1609 case Attr_Succ:
1610 /* These just add or subtract the constant 1 since representation
1611 clauses for enumeration types are handled in the front-end. */
1612 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1613 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1615 if (Do_Range_Check (First (Expressions (gnat_node))))
1617 gnu_expr = gnat_protect_expr (gnu_expr);
1618 gnu_expr
1619 = emit_check
1620 (build_binary_op (EQ_EXPR, boolean_type_node,
1621 gnu_expr,
1622 attribute == Attr_Pred
1623 ? TYPE_MIN_VALUE (gnu_result_type)
1624 : TYPE_MAX_VALUE (gnu_result_type)),
1625 gnu_expr, CE_Range_Check_Failed, gnat_node);
1628 gnu_result
1629 = build_binary_op (attribute == Attr_Pred ? MINUS_EXPR : PLUS_EXPR,
1630 gnu_result_type, gnu_expr,
1631 convert (gnu_result_type, integer_one_node));
1632 break;
1634 case Attr_Address:
1635 case Attr_Unrestricted_Access:
1636 /* Conversions don't change addresses but can cause us to miss the
1637 COMPONENT_REF case below, so strip them off. */
1638 gnu_prefix = remove_conversions (gnu_prefix,
1639 !Must_Be_Byte_Aligned (gnat_node));
1641 /* If we are taking 'Address of an unconstrained object, this is the
1642 pointer to the underlying array. */
1643 if (attribute == Attr_Address)
1644 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1646 /* If we are building a static dispatch table, we have to honor
1647 TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible
1648 with the C++ ABI. We do it in the non-static case as well,
1649 see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */
1650 else if (TARGET_VTABLE_USES_DESCRIPTORS
1651 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
1653 tree gnu_field, t;
1654 /* Descriptors can only be built here for top-level functions. */
1655 bool build_descriptor = (global_bindings_p () != 0);
1656 int i;
1657 vec<constructor_elt, va_gc> *gnu_vec = NULL;
1658 constructor_elt *elt;
1660 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1662 /* If we're not going to build the descriptor, we have to retrieve
1663 the one which will be built by the linker (or by the compiler
1664 later if a static chain is requested). */
1665 if (!build_descriptor)
1667 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_prefix);
1668 gnu_result = fold_convert (build_pointer_type (gnu_result_type),
1669 gnu_result);
1670 gnu_result = build1 (INDIRECT_REF, gnu_result_type, gnu_result);
1673 vec_safe_grow (gnu_vec, TARGET_VTABLE_USES_DESCRIPTORS);
1674 elt = (gnu_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
1675 for (gnu_field = TYPE_FIELDS (gnu_result_type), i = 0;
1676 i < TARGET_VTABLE_USES_DESCRIPTORS;
1677 gnu_field = DECL_CHAIN (gnu_field), i++)
1679 if (build_descriptor)
1681 t = build2 (FDESC_EXPR, TREE_TYPE (gnu_field), gnu_prefix,
1682 build_int_cst (NULL_TREE, i));
1683 TREE_CONSTANT (t) = 1;
1685 else
1686 t = build3 (COMPONENT_REF, ptr_void_ftype, gnu_result,
1687 gnu_field, NULL_TREE);
1689 elt->index = gnu_field;
1690 elt->value = t;
1691 elt--;
1694 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
1695 break;
1698 /* ... fall through ... */
1700 case Attr_Access:
1701 case Attr_Unchecked_Access:
1702 case Attr_Code_Address:
1703 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1704 gnu_result
1705 = build_unary_op (((attribute == Attr_Address
1706 || attribute == Attr_Unrestricted_Access)
1707 && !Must_Be_Byte_Aligned (gnat_node))
1708 ? ATTR_ADDR_EXPR : ADDR_EXPR,
1709 gnu_result_type, gnu_prefix);
1711 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
1712 don't try to build a trampoline. */
1713 if (attribute == Attr_Code_Address)
1715 gnu_expr = remove_conversions (gnu_result, false);
1717 if (TREE_CODE (gnu_expr) == ADDR_EXPR)
1718 TREE_NO_TRAMPOLINE (gnu_expr) = TREE_CONSTANT (gnu_expr) = 1;
1721 /* For 'Access, issue an error message if the prefix is a C++ method
1722 since it can use a special calling convention on some platforms,
1723 which cannot be propagated to the access type. */
1724 else if (attribute == Attr_Access
1725 && Nkind (gnat_prefix) == N_Identifier
1726 && is_cplusplus_method (Entity (gnat_prefix)))
1727 post_error ("access to C++ constructor or member function not allowed",
1728 gnat_node);
1730 /* For other address attributes applied to a nested function,
1731 find an inner ADDR_EXPR and annotate it so that we can issue
1732 a useful warning with -Wtrampolines. */
1733 else if (TREE_CODE (TREE_TYPE (gnu_prefix)) == FUNCTION_TYPE)
1735 gnu_expr = remove_conversions (gnu_result, false);
1737 if (TREE_CODE (gnu_expr) == ADDR_EXPR
1738 && decl_function_context (TREE_OPERAND (gnu_expr, 0)))
1740 set_expr_location_from_node (gnu_expr, gnat_node);
1742 /* Also check the inlining status. */
1743 check_inlining_for_nested_subprog (TREE_OPERAND (gnu_expr, 0));
1745 /* Check that we're not violating the No_Implicit_Dynamic_Code
1746 restriction. Be conservative if we don't know anything
1747 about the trampoline strategy for the target. */
1748 Check_Implicit_Dynamic_Code_Allowed (gnat_node);
1751 break;
1753 case Attr_Pool_Address:
1755 tree gnu_ptr = gnu_prefix;
1756 tree gnu_obj_type;
1758 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1760 /* If this is fat pointer, the object must have been allocated with the
1761 template in front of the array. So compute the template address; do
1762 it by converting to a thin pointer. */
1763 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
1764 gnu_ptr
1765 = convert (build_pointer_type
1766 (TYPE_OBJECT_RECORD_TYPE
1767 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
1768 gnu_ptr);
1770 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
1772 /* If this is a thin pointer, the object must have been allocated with
1773 the template in front of the array. So compute the template address
1774 and return it. */
1775 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
1776 gnu_ptr
1777 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
1778 gnu_ptr,
1779 fold_build1 (NEGATE_EXPR, sizetype,
1780 byte_position
1781 (DECL_CHAIN
1782 TYPE_FIELDS ((gnu_obj_type)))));
1784 gnu_result = convert (gnu_result_type, gnu_ptr);
1786 break;
1788 case Attr_Size:
1789 case Attr_Object_Size:
1790 case Attr_Value_Size:
1791 case Attr_Max_Size_In_Storage_Elements:
1792 gnu_expr = gnu_prefix;
1794 /* Remove NOPs and conversions between original and packable version
1795 from GNU_EXPR, and conversions from GNU_PREFIX. We use GNU_EXPR
1796 to see if a COMPONENT_REF was involved. */
1797 while (TREE_CODE (gnu_expr) == NOP_EXPR
1798 || (TREE_CODE (gnu_expr) == VIEW_CONVERT_EXPR
1799 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1800 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
1801 == RECORD_TYPE
1802 && TYPE_NAME (TREE_TYPE (gnu_expr))
1803 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
1804 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1806 gnu_prefix = remove_conversions (gnu_prefix, true);
1807 prefix_unused = true;
1808 gnu_type = TREE_TYPE (gnu_prefix);
1810 /* Replace an unconstrained array type with the type of the underlying
1811 array. We can't do this with a call to maybe_unconstrained_array
1812 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
1813 use the record type that will be used to allocate the object and its
1814 template. */
1815 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1817 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
1818 if (attribute != Attr_Max_Size_In_Storage_Elements)
1819 gnu_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
1822 /* If we're looking for the size of a field, return the field size. */
1823 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1824 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
1826 /* Otherwise, if the prefix is an object, or if we are looking for
1827 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the
1828 GCC size of the type. We make an exception for padded objects,
1829 as we do not take into account alignment promotions for the size.
1830 This is in keeping with the object case of gnat_to_gnu_entity. */
1831 else if ((TREE_CODE (gnu_prefix) != TYPE_DECL
1832 && !(TYPE_IS_PADDING_P (gnu_type)
1833 && TREE_CODE (gnu_expr) == COMPONENT_REF))
1834 || attribute == Attr_Object_Size
1835 || attribute == Attr_Max_Size_In_Storage_Elements)
1837 /* If this is a dereference and we have a special dynamic constrained
1838 subtype on the prefix, use it to compute the size; otherwise, use
1839 the designated subtype. */
1840 if (Nkind (gnat_prefix) == N_Explicit_Dereference)
1842 Node_Id gnat_actual_subtype
1843 = Actual_Designated_Subtype (gnat_prefix);
1844 tree gnu_ptr_type
1845 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix)));
1847 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)
1848 && Present (gnat_actual_subtype))
1850 tree gnu_actual_obj_type
1851 = gnat_to_gnu_type (gnat_actual_subtype);
1852 gnu_type
1853 = build_unc_object_type_from_ptr (gnu_ptr_type,
1854 gnu_actual_obj_type,
1855 get_identifier ("SIZE"),
1856 false);
1860 gnu_result = TYPE_SIZE (gnu_type);
1863 /* Otherwise, the result is the RM size of the type. */
1864 else
1865 gnu_result = rm_size (gnu_type);
1867 /* Deal with a self-referential size by returning the maximum size for
1868 a type and by qualifying the size with the object otherwise. */
1869 if (CONTAINS_PLACEHOLDER_P (gnu_result))
1871 if (TREE_CODE (gnu_prefix) == TYPE_DECL)
1872 gnu_result = max_size (gnu_result, true);
1873 else
1874 gnu_result = substitute_placeholder_in_expr (gnu_result, gnu_expr);
1877 /* If the type contains a template, subtract its size. */
1878 if (TREE_CODE (gnu_type) == RECORD_TYPE
1879 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1880 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1881 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1883 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
1884 if (attribute == Attr_Max_Size_In_Storage_Elements)
1885 gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node);
1887 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1888 break;
1890 case Attr_Alignment:
1892 unsigned int align;
1894 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1895 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
1896 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1898 gnu_type = TREE_TYPE (gnu_prefix);
1899 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1900 prefix_unused = true;
1902 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1903 align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT;
1904 else
1906 Entity_Id gnat_type = Etype (gnat_prefix);
1907 unsigned int double_align;
1908 bool is_capped_double, align_clause;
1910 /* If the default alignment of "double" or larger scalar types is
1911 specifically capped and there is an alignment clause neither
1912 on the type nor on the prefix itself, return the cap. */
1913 if ((double_align = double_float_alignment) > 0)
1914 is_capped_double
1915 = is_double_float_or_array (gnat_type, &align_clause);
1916 else if ((double_align = double_scalar_alignment) > 0)
1917 is_capped_double
1918 = is_double_scalar_or_array (gnat_type, &align_clause);
1919 else
1920 is_capped_double = align_clause = false;
1922 if (is_capped_double
1923 && Nkind (gnat_prefix) == N_Identifier
1924 && Present (Alignment_Clause (Entity (gnat_prefix))))
1925 align_clause = true;
1927 if (is_capped_double && !align_clause)
1928 align = double_align;
1929 else
1930 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
1933 gnu_result = size_int (align);
1935 break;
1937 case Attr_First:
1938 case Attr_Last:
1939 case Attr_Range_Length:
1940 prefix_unused = true;
1942 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
1944 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1946 if (attribute == Attr_First)
1947 gnu_result = TYPE_MIN_VALUE (gnu_type);
1948 else if (attribute == Attr_Last)
1949 gnu_result = TYPE_MAX_VALUE (gnu_type);
1950 else
1951 gnu_result = get_type_length (gnu_type, gnu_result_type);
1952 break;
1955 /* ... fall through ... */
1957 case Attr_Length:
1959 int Dimension = (Present (Expressions (gnat_node))
1960 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
1961 : 1), i;
1962 struct parm_attr_d *pa = NULL;
1963 Entity_Id gnat_param = Empty;
1964 bool unconstrained_ptr_deref = false;
1966 /* Make sure any implicit dereference gets done. */
1967 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1968 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1970 /* We treat unconstrained array In parameters specially. We also note
1971 whether we are dereferencing a pointer to unconstrained array. */
1972 if (!Is_Constrained (Etype (gnat_prefix)))
1973 switch (Nkind (gnat_prefix))
1975 case N_Identifier:
1976 /* This is the direct case. */
1977 if (Ekind (Entity (gnat_prefix)) == E_In_Parameter)
1978 gnat_param = Entity (gnat_prefix);
1979 break;
1981 case N_Explicit_Dereference:
1982 /* This is the indirect case. Note that we need to be sure that
1983 the access value cannot be null as we'll hoist the load. */
1984 if (Nkind (Prefix (gnat_prefix)) == N_Identifier
1985 && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter)
1987 if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix))))
1988 gnat_param = Entity (Prefix (gnat_prefix));
1990 else
1991 unconstrained_ptr_deref = true;
1992 break;
1994 default:
1995 break;
1998 /* If the prefix is the view conversion of a constrained array to an
1999 unconstrained form, we retrieve the constrained array because we
2000 might not be able to substitute the PLACEHOLDER_EXPR coming from
2001 the conversion. This can occur with the 'Old attribute applied
2002 to a parameter with an unconstrained type, which gets rewritten
2003 into a constrained local variable very late in the game. */
2004 if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR
2005 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix)))
2006 && !CONTAINS_PLACEHOLDER_P
2007 (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
2008 gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0));
2009 else
2010 gnu_type = TREE_TYPE (gnu_prefix);
2012 prefix_unused = true;
2013 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2015 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
2017 int ndim;
2018 tree gnu_type_temp;
2020 for (ndim = 1, gnu_type_temp = gnu_type;
2021 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
2022 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
2023 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
2026 Dimension = ndim + 1 - Dimension;
2029 for (i = 1; i < Dimension; i++)
2030 gnu_type = TREE_TYPE (gnu_type);
2032 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2034 /* When not optimizing, look up the slot associated with the parameter
2035 and the dimension in the cache and create a new one on failure.
2036 Don't do this when the actual subtype needs debug info (this happens
2037 with -gnatD): in elaborate_expression_1, we create variables that
2038 hold the bounds, so caching attributes isn't very interesting and
2039 causes dependency issues between these variables and cached
2040 expressions. */
2041 if (!optimize
2042 && Present (gnat_param)
2043 && !(Present (Actual_Subtype (gnat_param))
2044 && Needs_Debug_Info (Actual_Subtype (gnat_param))))
2046 FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa)
2047 if (pa->id == gnat_param && pa->dim == Dimension)
2048 break;
2050 if (!pa)
2052 pa = ggc_cleared_alloc<parm_attr_d> ();
2053 pa->id = gnat_param;
2054 pa->dim = Dimension;
2055 vec_safe_push (f_parm_attr_cache, pa);
2059 /* Return the cached expression or build a new one. */
2060 if (attribute == Attr_First)
2062 if (pa && pa->first)
2064 gnu_result = pa->first;
2065 break;
2068 gnu_result
2069 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2072 else if (attribute == Attr_Last)
2074 if (pa && pa->last)
2076 gnu_result = pa->last;
2077 break;
2080 gnu_result
2081 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2084 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
2086 if (pa && pa->length)
2088 gnu_result = pa->length;
2089 break;
2092 gnu_result
2093 = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)),
2094 gnu_result_type);
2097 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2098 handling. Note that these attributes could not have been used on
2099 an unconstrained array type. */
2100 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2102 /* Cache the expression we have just computed. Since we want to do it
2103 at run time, we force the use of a SAVE_EXPR and let the gimplifier
2104 create the temporary in the outermost binding level. We will make
2105 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
2106 paths by forcing its evaluation on entry of the function. */
2107 if (pa)
2109 gnu_result
2110 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2111 switch (attribute)
2113 case Attr_First:
2114 pa->first = gnu_result;
2115 break;
2117 case Attr_Last:
2118 pa->last = gnu_result;
2119 break;
2121 case Attr_Length:
2122 case Attr_Range_Length:
2123 pa->length = gnu_result;
2124 break;
2126 default:
2127 gcc_unreachable ();
2131 /* Otherwise, evaluate it each time it is referenced. */
2132 else
2133 switch (attribute)
2135 case Attr_First:
2136 case Attr_Last:
2137 /* If we are dereferencing a pointer to unconstrained array, we
2138 need to capture the value because the pointed-to bounds may
2139 subsequently be released. */
2140 if (unconstrained_ptr_deref)
2141 gnu_result
2142 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2143 break;
2145 case Attr_Length:
2146 case Attr_Range_Length:
2147 /* Set the source location onto the predicate of the condition
2148 but not if the expression is cached to avoid messing up the
2149 debug info. */
2150 if (TREE_CODE (gnu_result) == COND_EXPR
2151 && EXPR_P (TREE_OPERAND (gnu_result, 0)))
2152 set_expr_location_from_node (TREE_OPERAND (gnu_result, 0),
2153 gnat_node);
2154 break;
2156 default:
2157 gcc_unreachable ();
2160 break;
2163 case Attr_Bit_Position:
2164 case Attr_Position:
2165 case Attr_First_Bit:
2166 case Attr_Last_Bit:
2167 case Attr_Bit:
2169 HOST_WIDE_INT bitsize;
2170 HOST_WIDE_INT bitpos;
2171 tree gnu_offset;
2172 tree gnu_field_bitpos;
2173 tree gnu_field_offset;
2174 tree gnu_inner;
2175 machine_mode mode;
2176 int unsignedp, volatilep;
2178 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2179 gnu_prefix = remove_conversions (gnu_prefix, true);
2180 prefix_unused = true;
2182 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
2183 the result is 0. Don't allow 'Bit on a bare component, though. */
2184 if (attribute == Attr_Bit
2185 && TREE_CODE (gnu_prefix) != COMPONENT_REF
2186 && TREE_CODE (gnu_prefix) != FIELD_DECL)
2188 gnu_result = integer_zero_node;
2189 break;
2192 else
2193 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
2194 || (attribute == Attr_Bit_Position
2195 && TREE_CODE (gnu_prefix) == FIELD_DECL));
2197 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
2198 &mode, &unsignedp, &volatilep, false);
2200 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
2202 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
2203 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
2205 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
2206 TREE_CODE (gnu_inner) == COMPONENT_REF
2207 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
2208 gnu_inner = TREE_OPERAND (gnu_inner, 0))
2210 gnu_field_bitpos
2211 = size_binop (PLUS_EXPR, gnu_field_bitpos,
2212 bit_position (TREE_OPERAND (gnu_inner, 1)));
2213 gnu_field_offset
2214 = size_binop (PLUS_EXPR, gnu_field_offset,
2215 byte_position (TREE_OPERAND (gnu_inner, 1)));
2218 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
2220 gnu_field_bitpos = bit_position (gnu_prefix);
2221 gnu_field_offset = byte_position (gnu_prefix);
2223 else
2225 gnu_field_bitpos = bitsize_zero_node;
2226 gnu_field_offset = size_zero_node;
2229 switch (attribute)
2231 case Attr_Position:
2232 gnu_result = gnu_field_offset;
2233 break;
2235 case Attr_First_Bit:
2236 case Attr_Bit:
2237 gnu_result = size_int (bitpos % BITS_PER_UNIT);
2238 break;
2240 case Attr_Last_Bit:
2241 gnu_result = bitsize_int (bitpos % BITS_PER_UNIT);
2242 gnu_result = size_binop (PLUS_EXPR, gnu_result,
2243 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
2244 /* ??? Avoid a large unsigned result that will overflow when
2245 converted to the signed universal_integer. */
2246 if (integer_zerop (gnu_result))
2247 gnu_result = integer_minus_one_node;
2248 else
2249 gnu_result
2250 = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node);
2251 break;
2253 case Attr_Bit_Position:
2254 gnu_result = gnu_field_bitpos;
2255 break;
2258 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2259 handling. */
2260 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2261 break;
2264 case Attr_Min:
2265 case Attr_Max:
2267 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
2268 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
2270 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2271 gnu_result = build_binary_op (attribute == Attr_Min
2272 ? MIN_EXPR : MAX_EXPR,
2273 gnu_result_type, gnu_lhs, gnu_rhs);
2275 break;
2277 case Attr_Passed_By_Reference:
2278 gnu_result = size_int (default_pass_by_ref (gnu_type)
2279 || must_pass_by_ref (gnu_type));
2280 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2281 break;
2283 case Attr_Component_Size:
2284 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
2285 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
2286 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
2288 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2289 gnu_type = TREE_TYPE (gnu_prefix);
2291 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
2292 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
2294 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2295 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2296 gnu_type = TREE_TYPE (gnu_type);
2298 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2300 /* Note this size cannot be self-referential. */
2301 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
2302 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2303 prefix_unused = true;
2304 break;
2306 case Attr_Descriptor_Size:
2307 gnu_type = TREE_TYPE (gnu_prefix);
2308 gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE);
2310 /* What we want is the offset of the ARRAY field in the record
2311 that the thin pointer designates. */
2312 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
2313 gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
2314 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2315 prefix_unused = true;
2316 break;
2318 case Attr_Null_Parameter:
2319 /* This is just a zero cast to the pointer type for our prefix and
2320 dereferenced. */
2321 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2322 gnu_result
2323 = build_unary_op (INDIRECT_REF, NULL_TREE,
2324 convert (build_pointer_type (gnu_result_type),
2325 integer_zero_node));
2326 TREE_PRIVATE (gnu_result) = 1;
2327 break;
2329 case Attr_Mechanism_Code:
2331 Entity_Id gnat_obj = Entity (gnat_prefix);
2332 int code;
2334 prefix_unused = true;
2335 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2336 if (Present (Expressions (gnat_node)))
2338 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
2340 for (gnat_obj = First_Formal (gnat_obj); i > 1;
2341 i--, gnat_obj = Next_Formal (gnat_obj))
2345 code = Mechanism (gnat_obj);
2346 if (code == Default)
2347 code = ((present_gnu_tree (gnat_obj)
2348 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
2349 || ((TREE_CODE (get_gnu_tree (gnat_obj))
2350 == PARM_DECL)
2351 && (DECL_BY_COMPONENT_PTR_P
2352 (get_gnu_tree (gnat_obj))))))
2353 ? By_Reference : By_Copy);
2354 gnu_result = convert (gnu_result_type, size_int (- code));
2356 break;
2358 case Attr_Model:
2359 /* We treat Model as identical to Machine. This is true for at least
2360 IEEE and some other nice floating-point systems. */
2362 /* ... fall through ... */
2364 case Attr_Machine:
2365 /* The trick is to force the compiler to store the result in memory so
2366 that we do not have extra precision used. But do this only when this
2367 is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of
2368 the type is lower than that of the longest floating-point type. */
2369 prefix_unused = true;
2370 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2371 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2372 gnu_result = convert (gnu_result_type, gnu_expr);
2374 if (fp_arith_may_widen
2375 && TYPE_PRECISION (gnu_result_type)
2376 < TYPE_PRECISION (longest_float_type_node))
2378 tree rec_type = make_node (RECORD_TYPE);
2379 tree field
2380 = create_field_decl (get_identifier ("OBJ"), gnu_result_type,
2381 rec_type, NULL_TREE, NULL_TREE, 0, 0);
2382 tree rec_val, asm_expr;
2384 finish_record_type (rec_type, field, 0, false);
2386 rec_val = build_constructor_single (rec_type, field, gnu_result);
2387 rec_val = save_expr (rec_val);
2389 asm_expr
2390 = build5 (ASM_EXPR, void_type_node,
2391 build_string (0, ""),
2392 tree_cons (build_tree_list (NULL_TREE,
2393 build_string (2, "=m")),
2394 rec_val, NULL_TREE),
2395 tree_cons (build_tree_list (NULL_TREE,
2396 build_string (1, "m")),
2397 rec_val, NULL_TREE),
2398 NULL_TREE, NULL_TREE);
2399 ASM_VOLATILE_P (asm_expr) = 1;
2401 gnu_result
2402 = build_compound_expr (gnu_result_type, asm_expr,
2403 build_component_ref (rec_val, NULL_TREE,
2404 field, false));
2406 break;
2408 default:
2409 /* This abort means that we have an unimplemented attribute. */
2410 gcc_unreachable ();
2413 /* If this is an attribute where the prefix was unused, force a use of it if
2414 it has a side-effect. But don't do it if the prefix is just an entity
2415 name. However, if an access check is needed, we must do it. See second
2416 example in AARM 11.6(5.e). */
2417 if (prefix_unused
2418 && TREE_SIDE_EFFECTS (gnu_prefix)
2419 && !Is_Entity_Name (gnat_prefix))
2420 gnu_result
2421 = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result);
2423 *gnu_result_type_p = gnu_result_type;
2424 return gnu_result;
2427 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
2428 to a GCC tree, which is returned. */
2430 static tree
2431 Case_Statement_to_gnu (Node_Id gnat_node)
2433 tree gnu_result, gnu_expr, gnu_label;
2434 Node_Id gnat_when;
2435 location_t end_locus;
2436 bool may_fallthru = false;
2438 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2439 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2441 /* The range of values in a case statement is determined by the rules in
2442 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
2443 of the expression. One exception arises in the case of a simple name that
2444 is parenthesized. This still has the Etype of the name, but since it is
2445 not a name, para 7 does not apply, and we need to go to the base type.
2446 This is the only case where parenthesization affects the dynamic
2447 semantics (i.e. the range of possible values at run time that is covered
2448 by the others alternative).
2450 Another exception is if the subtype of the expression is non-static. In
2451 that case, we also have to use the base type. */
2452 if (Paren_Count (Expression (gnat_node)) != 0
2453 || !Is_OK_Static_Subtype (Underlying_Type
2454 (Etype (Expression (gnat_node)))))
2455 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2457 /* We build a SWITCH_EXPR that contains the code with interspersed
2458 CASE_LABEL_EXPRs for each label. */
2459 if (!Sloc_to_locus (End_Location (gnat_node), &end_locus))
2460 end_locus = input_location;
2461 gnu_label = create_artificial_label (end_locus);
2462 start_stmt_group ();
2464 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2465 Present (gnat_when);
2466 gnat_when = Next_Non_Pragma (gnat_when))
2468 bool choices_added_p = false;
2469 Node_Id gnat_choice;
2471 /* First compile all the different case choices for the current WHEN
2472 alternative. */
2473 for (gnat_choice = First (Discrete_Choices (gnat_when));
2474 Present (gnat_choice);
2475 gnat_choice = Next (gnat_choice))
2477 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2478 tree label = create_artificial_label (input_location);
2480 switch (Nkind (gnat_choice))
2482 case N_Range:
2483 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
2484 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
2485 break;
2487 case N_Subtype_Indication:
2488 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
2489 (Constraint (gnat_choice))));
2490 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
2491 (Constraint (gnat_choice))));
2492 break;
2494 case N_Identifier:
2495 case N_Expanded_Name:
2496 /* This represents either a subtype range or a static value of
2497 some kind; Ekind says which. */
2498 if (IN (Ekind (Entity (gnat_choice)), Type_Kind))
2500 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
2502 gnu_low = TYPE_MIN_VALUE (gnu_type);
2503 gnu_high = TYPE_MAX_VALUE (gnu_type);
2504 break;
2507 /* ... fall through ... */
2509 case N_Character_Literal:
2510 case N_Integer_Literal:
2511 gnu_low = gnat_to_gnu (gnat_choice);
2512 break;
2514 case N_Others_Choice:
2515 break;
2517 default:
2518 gcc_unreachable ();
2521 /* Everything should be folded into constants at this point. */
2522 gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST);
2523 gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST);
2525 add_stmt_with_node (build_case_label (gnu_low, gnu_high, label),
2526 gnat_choice);
2527 choices_added_p = true;
2530 /* This construct doesn't define a scope so we shouldn't push a binding
2531 level around the statement list. Except that we have always done so
2532 historically and this makes it possible to reduce stack usage. As a
2533 compromise, we keep doing it for case statements, for which this has
2534 never been problematic, but not for case expressions in Ada 2012. */
2535 if (choices_added_p)
2537 const bool is_case_expression
2538 = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions);
2539 tree group
2540 = build_stmt_group (Statements (gnat_when), !is_case_expression);
2541 bool group_may_fallthru = block_may_fallthru (group);
2542 add_stmt (group);
2543 if (group_may_fallthru)
2545 tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label);
2546 SET_EXPR_LOCATION (stmt, end_locus);
2547 add_stmt (stmt);
2548 may_fallthru = true;
2553 /* Now emit a definition of the label the cases branch to, if any. */
2554 if (may_fallthru)
2555 add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label));
2556 gnu_result = build3 (SWITCH_EXPR, TREE_TYPE (gnu_expr), gnu_expr,
2557 end_stmt_group (), NULL_TREE);
2559 return gnu_result;
2562 /* Find out whether VAR is an iteration variable of an enclosing loop in the
2563 current function. If so, push a range_check_info structure onto the stack
2564 of this enclosing loop and return it. Otherwise, return NULL. */
2566 static struct range_check_info_d *
2567 push_range_check_info (tree var)
2569 struct loop_info_d *iter = NULL;
2570 unsigned int i;
2572 if (vec_safe_is_empty (gnu_loop_stack))
2573 return NULL;
2575 var = remove_conversions (var, false);
2577 if (TREE_CODE (var) != VAR_DECL)
2578 return NULL;
2580 if (decl_function_context (var) != current_function_decl)
2581 return NULL;
2583 for (i = vec_safe_length (gnu_loop_stack) - 1;
2584 vec_safe_iterate (gnu_loop_stack, i, &iter);
2585 i--)
2586 if (var == iter->loop_var)
2587 break;
2589 if (iter)
2591 struct range_check_info_d *rci = ggc_alloc<range_check_info_d> ();
2592 vec_safe_push (iter->checks, rci);
2593 return rci;
2596 return NULL;
2599 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2600 false, or the maximum value if MAX is true, of TYPE. */
2602 static bool
2603 can_equal_min_or_max_val_p (tree val, tree type, bool max)
2605 tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
2607 if (TREE_CODE (min_or_max_val) != INTEGER_CST)
2608 return true;
2610 if (TREE_CODE (val) == NOP_EXPR)
2611 val = (max
2612 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))
2613 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))));
2615 if (TREE_CODE (val) != INTEGER_CST)
2616 return true;
2618 if (max)
2619 return tree_int_cst_lt (val, min_or_max_val) == 0;
2620 else
2621 return tree_int_cst_lt (min_or_max_val, val) == 0;
2624 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2625 If REVERSE is true, minimum value is taken as maximum value. */
2627 static inline bool
2628 can_equal_min_val_p (tree val, tree type, bool reverse)
2630 return can_equal_min_or_max_val_p (val, type, reverse);
2633 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2634 If REVERSE is true, maximum value is taken as minimum value. */
2636 static inline bool
2637 can_equal_max_val_p (tree val, tree type, bool reverse)
2639 return can_equal_min_or_max_val_p (val, type, !reverse);
2642 /* Return true if VAL1 can be lower than VAL2. */
2644 static bool
2645 can_be_lower_p (tree val1, tree val2)
2647 if (TREE_CODE (val1) == NOP_EXPR)
2648 val1 = TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val1, 0)));
2650 if (TREE_CODE (val1) != INTEGER_CST)
2651 return true;
2653 if (TREE_CODE (val2) == NOP_EXPR)
2654 val2 = TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val2, 0)));
2656 if (TREE_CODE (val2) != INTEGER_CST)
2657 return true;
2659 return tree_int_cst_lt (val1, val2);
2662 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
2663 to a GCC tree, which is returned. */
2665 static tree
2666 Loop_Statement_to_gnu (Node_Id gnat_node)
2668 const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
2669 struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> ();
2670 tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE,
2671 NULL_TREE, NULL_TREE, NULL_TREE);
2672 tree gnu_loop_label = create_artificial_label (input_location);
2673 tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2674 tree gnu_result;
2676 /* Push the loop_info structure associated with the LOOP_STMT. */
2677 vec_safe_push (gnu_loop_stack, gnu_loop_info);
2679 /* Set location information for statement and end label. */
2680 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
2681 Sloc_to_locus (Sloc (End_Label (gnat_node)),
2682 &DECL_SOURCE_LOCATION (gnu_loop_label));
2683 LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label;
2685 /* Save the statement for later reuse. */
2686 gnu_loop_info->stmt = gnu_loop_stmt;
2688 /* Set the condition under which the loop must keep going.
2689 For the case "LOOP .... END LOOP;" the condition is always true. */
2690 if (No (gnat_iter_scheme))
2693 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2694 else if (Present (Condition (gnat_iter_scheme)))
2695 LOOP_STMT_COND (gnu_loop_stmt)
2696 = gnat_to_gnu (Condition (gnat_iter_scheme));
2698 /* Otherwise we have an iteration scheme and the condition is given by the
2699 bounds of the subtype of the iteration variable. */
2700 else
2702 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
2703 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2704 Entity_Id gnat_type = Etype (gnat_loop_var);
2705 tree gnu_type = get_unpadded_type (gnat_type);
2706 tree gnu_base_type = get_base_type (gnu_type);
2707 tree gnu_one_node = convert (gnu_base_type, integer_one_node);
2708 tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt;
2709 enum tree_code update_code, test_code, shift_code;
2710 bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false;
2712 gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type));
2713 gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type));
2715 /* We must disable modulo reduction for the iteration variable, if any,
2716 in order for the loop comparison to be effective. */
2717 if (reverse)
2719 gnu_first = gnu_high;
2720 gnu_last = gnu_low;
2721 update_code = MINUS_NOMOD_EXPR;
2722 test_code = GE_EXPR;
2723 shift_code = PLUS_NOMOD_EXPR;
2725 else
2727 gnu_first = gnu_low;
2728 gnu_last = gnu_high;
2729 update_code = PLUS_NOMOD_EXPR;
2730 test_code = LE_EXPR;
2731 shift_code = MINUS_NOMOD_EXPR;
2734 /* We use two different strategies to translate the loop, depending on
2735 whether optimization is enabled.
2737 If it is, we generate the canonical loop form expected by the loop
2738 optimizer and the loop vectorizer, which is the do-while form:
2740 ENTRY_COND
2741 loop:
2742 TOP_UPDATE
2743 BODY
2744 BOTTOM_COND
2745 GOTO loop
2747 This avoids an implicit dependency on loop header copying and makes
2748 it possible to turn BOTTOM_COND into an inequality test.
2750 If optimization is disabled, loop header copying doesn't come into
2751 play and we try to generate the loop form with the fewer conditional
2752 branches. First, the default form, which is:
2754 loop:
2755 TOP_COND
2756 BODY
2757 BOTTOM_UPDATE
2758 GOTO loop
2760 It should catch most loops with constant ending point. Then, if we
2761 cannot, we try to generate the shifted form:
2763 loop:
2764 TOP_COND
2765 TOP_UPDATE
2766 BODY
2767 GOTO loop
2769 which should catch loops with constant starting point. Otherwise, if
2770 we cannot, we generate the fallback form:
2772 ENTRY_COND
2773 loop:
2774 BODY
2775 BOTTOM_COND
2776 BOTTOM_UPDATE
2777 GOTO loop
2779 which works in all cases. */
2781 if (optimize)
2783 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
2784 overflow. */
2785 if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse))
2788 /* Otherwise, use the do-while form with the help of a special
2789 induction variable in the unsigned version of the base type
2790 or the unsigned version of the size type, whichever is the
2791 largest, in order to have wrap-around arithmetics for it. */
2792 else
2794 if (TYPE_PRECISION (gnu_base_type)
2795 > TYPE_PRECISION (size_type_node))
2796 gnu_base_type
2797 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1);
2798 else
2799 gnu_base_type = size_type_node;
2801 gnu_first = convert (gnu_base_type, gnu_first);
2802 gnu_last = convert (gnu_base_type, gnu_last);
2803 gnu_one_node = convert (gnu_base_type, integer_one_node);
2804 use_iv = true;
2807 gnu_first
2808 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2809 gnu_one_node);
2810 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2811 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2813 else
2815 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
2816 if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse))
2819 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
2820 GNU_LAST-1 does. */
2821 else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)
2822 && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse))
2824 gnu_first
2825 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2826 gnu_one_node);
2827 gnu_last
2828 = build_binary_op (shift_code, gnu_base_type, gnu_last,
2829 gnu_one_node);
2830 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2833 /* Otherwise, use the fallback form. */
2834 else
2835 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2838 /* If we use the BOTTOM_COND, we can turn the test into an inequality
2839 test but we may have to add ENTRY_COND to protect the empty loop. */
2840 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt))
2842 test_code = NE_EXPR;
2843 if (can_be_lower_p (gnu_high, gnu_low))
2845 gnu_cond_expr
2846 = build3 (COND_EXPR, void_type_node,
2847 build_binary_op (LE_EXPR, boolean_type_node,
2848 gnu_low, gnu_high),
2849 NULL_TREE, alloc_stmt_list ());
2850 set_expr_location_from_node (gnu_cond_expr, gnat_loop_spec);
2854 /* Open a new nesting level that will surround the loop to declare the
2855 iteration variable. */
2856 start_stmt_group ();
2857 gnat_pushlevel ();
2859 /* If we use the special induction variable, create it and set it to
2860 its initial value. Morever, the regular iteration variable cannot
2861 itself be initialized, lest the initial value wrapped around. */
2862 if (use_iv)
2864 gnu_loop_iv
2865 = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var);
2866 add_stmt (gnu_stmt);
2867 gnu_first = NULL_TREE;
2869 else
2870 gnu_loop_iv = NULL_TREE;
2872 /* Declare the iteration variable and set it to its initial value. */
2873 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
2874 if (DECL_BY_REF_P (gnu_loop_var))
2875 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
2876 else if (use_iv)
2878 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var));
2879 SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv);
2881 gnu_loop_info->loop_var = gnu_loop_var;
2883 /* Do all the arithmetics in the base type. */
2884 gnu_loop_var = convert (gnu_base_type, gnu_loop_var);
2886 /* Set either the top or bottom exit condition. */
2887 if (use_iv)
2888 LOOP_STMT_COND (gnu_loop_stmt)
2889 = build_binary_op (test_code, boolean_type_node, gnu_loop_iv,
2890 gnu_last);
2891 else
2892 LOOP_STMT_COND (gnu_loop_stmt)
2893 = build_binary_op (test_code, boolean_type_node, gnu_loop_var,
2894 gnu_last);
2896 /* Set either the top or bottom update statement and give it the source
2897 location of the iteration for better coverage info. */
2898 if (use_iv)
2900 gnu_stmt
2901 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv,
2902 build_binary_op (update_code, gnu_base_type,
2903 gnu_loop_iv, gnu_one_node));
2904 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2905 append_to_statement_list (gnu_stmt,
2906 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2907 gnu_stmt
2908 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2909 gnu_loop_iv);
2910 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2911 append_to_statement_list (gnu_stmt,
2912 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2914 else
2916 gnu_stmt
2917 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2918 build_binary_op (update_code, gnu_base_type,
2919 gnu_loop_var, gnu_one_node));
2920 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2921 LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt;
2925 /* If the loop was named, have the name point to this loop. In this case,
2926 the association is not a DECL node, but the end label of the loop. */
2927 if (Present (Identifier (gnat_node)))
2928 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true);
2930 /* Make the loop body into its own block, so any allocated storage will be
2931 released every iteration. This is needed for stack allocation. */
2932 LOOP_STMT_BODY (gnu_loop_stmt)
2933 = build_stmt_group (Statements (gnat_node), true);
2934 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
2936 /* If we have an iteration scheme, then we are in a statement group. Add
2937 the LOOP_STMT to it, finish it and make it the "loop". */
2938 if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme)))
2940 struct range_check_info_d *rci;
2941 unsigned n_checks = vec_safe_length (gnu_loop_info->checks);
2942 unsigned int i;
2944 /* First, if we have computed a small number of invariant conditions for
2945 range checks applied to the iteration variable, then initialize these
2946 conditions in front of the loop. Otherwise, leave them set to true.
2948 ??? The heuristics need to be improved, by taking into account the
2949 following datapoints:
2950 - loop unswitching is disabled for big loops. The cap is the
2951 parameter PARAM_MAX_UNSWITCH_INSNS (50).
2952 - loop unswitching can only be applied a small number of times
2953 to a given loop. The cap is PARAM_MAX_UNSWITCH_LEVEL (3).
2954 - the front-end quickly generates useless or redundant checks
2955 that can be entirely optimized away in the end. */
2956 if (1 <= n_checks && n_checks <= 4)
2957 for (i = 0;
2958 vec_safe_iterate (gnu_loop_info->checks, i, &rci);
2959 i++)
2961 tree low_ok
2962 = rci->low_bound
2963 ? build_binary_op (GE_EXPR, boolean_type_node,
2964 convert (rci->type, gnu_low),
2965 rci->low_bound)
2966 : boolean_true_node;
2968 tree high_ok
2969 = rci->high_bound
2970 ? build_binary_op (LE_EXPR, boolean_type_node,
2971 convert (rci->type, gnu_high),
2972 rci->high_bound)
2973 : boolean_true_node;
2975 tree range_ok
2976 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
2977 low_ok, high_ok);
2979 TREE_OPERAND (rci->invariant_cond, 0)
2980 = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok);
2982 add_stmt_with_node_force (rci->invariant_cond, gnat_node);
2985 add_stmt (gnu_loop_stmt);
2986 gnat_poplevel ();
2987 gnu_loop_stmt = end_stmt_group ();
2990 /* If we have an outer COND_EXPR, that's our result and this loop is its
2991 "true" statement. Otherwise, the result is the LOOP_STMT. */
2992 if (gnu_cond_expr)
2994 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
2995 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
2996 gnu_result = gnu_cond_expr;
2998 else
2999 gnu_result = gnu_loop_stmt;
3001 gnu_loop_stack->pop ();
3003 return gnu_result;
3006 /* This page implements a form of Named Return Value optimization modelled
3007 on the C++ optimization of the same name. The main difference is that
3008 we disregard any semantical considerations when applying it here, the
3009 counterpart being that we don't try to apply it to semantically loaded
3010 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
3012 We consider a function body of the following GENERIC form:
3014 return_type R1;
3015 [...]
3016 RETURN_EXPR [<retval> = ...]
3017 [...]
3018 RETURN_EXPR [<retval> = R1]
3019 [...]
3020 return_type Ri;
3021 [...]
3022 RETURN_EXPR [<retval> = ...]
3023 [...]
3024 RETURN_EXPR [<retval> = Ri]
3025 [...]
3027 and we try to fulfill a simple criterion that would make it possible to
3028 replace one or several Ri variables with the RESULT_DECL of the function.
3030 The first observation is that RETURN_EXPRs that don't directly reference
3031 any of the Ri variables on the RHS of their assignment are transparent wrt
3032 the optimization. This is because the Ri variables aren't addressable so
3033 any transformation applied to them doesn't affect the RHS; moreover, the
3034 assignment writes the full <retval> object so existing values are entirely
3035 discarded.
3037 This property can be extended to some forms of RETURN_EXPRs that reference
3038 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3039 case, in particular when function calls are involved.
3041 Therefore the algorithm is as follows:
3043 1. Collect the list of candidates for a Named Return Value (Ri variables
3044 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3045 other expressions on the RHS of such assignments.
3047 2. Prune the members of the first list (candidates) that are referenced
3048 by a member of the second list (expressions).
3050 3. Extract a set of candidates with non-overlapping live ranges from the
3051 first list. These are the Named Return Values.
3053 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
3054 Named Return Values in the function with the RESULT_DECL.
3056 If the function returns an unconstrained type, things are a bit different
3057 because the anonymous return object is allocated on the secondary stack
3058 and RESULT_DECL is only a pointer to it. Each return object can be of a
3059 different size and is allocated separately so we need not care about the
3060 aforementioned overlapping issues. Therefore, we don't collect the other
3061 expressions and skip step #2 in the algorithm. */
3063 struct nrv_data
3065 bitmap nrv;
3066 tree result;
3067 Node_Id gnat_ret;
3068 hash_set<tree> *visited;
3071 /* Return true if T is a Named Return Value. */
3073 static inline bool
3074 is_nrv_p (bitmap nrv, tree t)
3076 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
3079 /* Helper function for walk_tree, used by finalize_nrv below. */
3081 static tree
3082 prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3084 struct nrv_data *dp = (struct nrv_data *)data;
3085 tree t = *tp;
3087 /* No need to walk into types or decls. */
3088 if (IS_TYPE_OR_DECL_P (t))
3089 *walk_subtrees = 0;
3091 if (is_nrv_p (dp->nrv, t))
3092 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3094 return NULL_TREE;
3097 /* Prune Named Return Values in BLOCK and return true if there is still a
3098 Named Return Value in BLOCK or one of its sub-blocks. */
3100 static bool
3101 prune_nrv_in_block (bitmap nrv, tree block)
3103 bool has_nrv = false;
3104 tree t;
3106 /* First recurse on the sub-blocks. */
3107 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3108 has_nrv |= prune_nrv_in_block (nrv, t);
3110 /* Then make sure to keep at most one NRV per block. */
3111 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3112 if (is_nrv_p (nrv, t))
3114 if (has_nrv)
3115 bitmap_clear_bit (nrv, DECL_UID (t));
3116 else
3117 has_nrv = true;
3120 return has_nrv;
3123 /* Helper function for walk_tree, used by finalize_nrv below. */
3125 static tree
3126 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3128 struct nrv_data *dp = (struct nrv_data *)data;
3129 tree t = *tp;
3131 /* No need to walk into types. */
3132 if (TYPE_P (t))
3133 *walk_subtrees = 0;
3135 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3136 nop, but differs from using NULL_TREE in that it indicates that we care
3137 about the value of the RESULT_DECL. */
3138 else if (TREE_CODE (t) == RETURN_EXPR
3139 && TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR)
3141 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1), init_expr;
3143 /* If this is the temporary created for a return value with variable
3144 size in Call_to_gnu, we replace the RHS with the init expression. */
3145 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3146 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3147 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3148 == TREE_OPERAND (ret_val, 1))
3150 init_expr = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3151 ret_val = TREE_OPERAND (ret_val, 1);
3153 else
3154 init_expr = NULL_TREE;
3156 /* Strip useless conversions around the return value. */
3157 if (gnat_useless_type_conversion (ret_val))
3158 ret_val = TREE_OPERAND (ret_val, 0);
3160 if (is_nrv_p (dp->nrv, ret_val))
3162 if (init_expr)
3163 TREE_OPERAND (TREE_OPERAND (t, 0), 1) = init_expr;
3164 else
3165 TREE_OPERAND (t, 0) = dp->result;
3169 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3170 if needed. */
3171 else if (TREE_CODE (t) == DECL_EXPR
3172 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3174 tree var = DECL_EXPR_DECL (t), init;
3176 if (DECL_INITIAL (var))
3178 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3179 DECL_INITIAL (var));
3180 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3181 DECL_INITIAL (var) = NULL_TREE;
3183 else
3184 init = build_empty_stmt (EXPR_LOCATION (t));
3185 *tp = init;
3187 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3188 SET_DECL_VALUE_EXPR (var, dp->result);
3189 DECL_HAS_VALUE_EXPR_P (var) = 1;
3190 /* ??? Kludge to avoid an assertion failure during inlining. */
3191 DECL_SIZE (var) = bitsize_unit_node;
3192 DECL_SIZE_UNIT (var) = size_one_node;
3195 /* And replace all uses of NRVs with the RESULT_DECL. */
3196 else if (is_nrv_p (dp->nrv, t))
3197 *tp = convert (TREE_TYPE (t), dp->result);
3199 /* Avoid walking into the same tree more than once. Unfortunately, we
3200 can't just use walk_tree_without_duplicates because it would only
3201 call us for the first occurrence of NRVs in the function body. */
3202 if (dp->visited->add (*tp))
3203 *walk_subtrees = 0;
3205 return NULL_TREE;
3208 /* Likewise, but used when the function returns an unconstrained type. */
3210 static tree
3211 finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3213 struct nrv_data *dp = (struct nrv_data *)data;
3214 tree t = *tp;
3216 /* No need to walk into types. */
3217 if (TYPE_P (t))
3218 *walk_subtrees = 0;
3220 /* We need to see the DECL_EXPR of NRVs before any other references so we
3221 walk the body of BIND_EXPR before walking its variables. */
3222 else if (TREE_CODE (t) == BIND_EXPR)
3223 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3225 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3226 return value built by the allocator instead of the whole construct. */
3227 else if (TREE_CODE (t) == RETURN_EXPR
3228 && TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR)
3230 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3232 /* This is the construct returned by the allocator. */
3233 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3234 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3236 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
3237 ret_val
3238 = (*CONSTRUCTOR_ELTS (TREE_OPERAND (TREE_OPERAND (ret_val, 0),
3239 1)))[1].value;
3240 else
3241 ret_val = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3244 /* Strip useless conversions around the return value. */
3245 if (gnat_useless_type_conversion (ret_val)
3246 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3247 ret_val = TREE_OPERAND (ret_val, 0);
3249 /* Strip unpadding around the return value. */
3250 if (TREE_CODE (ret_val) == COMPONENT_REF
3251 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3252 ret_val = TREE_OPERAND (ret_val, 0);
3254 /* Assign the new return value to the RESULT_DECL. */
3255 if (is_nrv_p (dp->nrv, ret_val))
3256 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3257 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3260 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3261 into a new variable. */
3262 else if (TREE_CODE (t) == DECL_EXPR
3263 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3265 tree saved_current_function_decl = current_function_decl;
3266 tree var = DECL_EXPR_DECL (t);
3267 tree alloc, p_array, new_var, new_ret;
3268 vec<constructor_elt, va_gc> *v;
3269 vec_alloc (v, 2);
3271 /* Create an artificial context to build the allocation. */
3272 current_function_decl = decl_function_context (var);
3273 start_stmt_group ();
3274 gnat_pushlevel ();
3276 /* This will return a COMPOUND_EXPR with the allocation in the first
3277 arm and the final return value in the second arm. */
3278 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3279 TREE_TYPE (dp->result),
3280 Procedure_To_Call (dp->gnat_ret),
3281 Storage_Pool (dp->gnat_ret),
3282 Empty, false);
3284 /* The new variable is built as a reference to the allocated space. */
3285 new_var
3286 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3287 build_reference_type (TREE_TYPE (var)));
3288 DECL_BY_REFERENCE (new_var) = 1;
3290 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3292 /* The new initial value is a COMPOUND_EXPR with the allocation in
3293 the first arm and the value of P_ARRAY in the second arm. */
3294 DECL_INITIAL (new_var)
3295 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3296 TREE_OPERAND (alloc, 0),
3297 (*CONSTRUCTOR_ELTS (TREE_OPERAND (alloc, 1)))[0].value);
3299 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3300 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3301 CONSTRUCTOR_APPEND_ELT (v, p_array,
3302 fold_convert (TREE_TYPE (p_array), new_var));
3303 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
3304 (*CONSTRUCTOR_ELTS (
3305 TREE_OPERAND (alloc, 1)))[1].value);
3306 new_ret = build_constructor (TREE_TYPE (alloc), v);
3308 else
3310 /* The new initial value is just the allocation. */
3311 DECL_INITIAL (new_var) = alloc;
3312 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3315 gnat_pushdecl (new_var, Empty);
3317 /* Destroy the artificial context and insert the new statements. */
3318 gnat_zaplevel ();
3319 *tp = end_stmt_group ();
3320 current_function_decl = saved_current_function_decl;
3322 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3323 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3324 DECL_CHAIN (var) = new_var;
3325 DECL_IGNORED_P (var) = 1;
3327 /* Save the new return value and the dereference of NEW_VAR. */
3328 DECL_INITIAL (var)
3329 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3330 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3331 /* ??? Kludge to avoid messing up during inlining. */
3332 DECL_CONTEXT (var) = NULL_TREE;
3335 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3336 else if (is_nrv_p (dp->nrv, t))
3337 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3339 /* Avoid walking into the same tree more than once. Unfortunately, we
3340 can't just use walk_tree_without_duplicates because it would only
3341 call us for the first occurrence of NRVs in the function body. */
3342 if (dp->visited->add (*tp))
3343 *walk_subtrees = 0;
3345 return NULL_TREE;
3348 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3349 contains the candidates for Named Return Value and OTHER is a list of
3350 the other return values. GNAT_RET is a representative return node. */
3352 static void
3353 finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
3355 struct cgraph_node *node;
3356 struct nrv_data data;
3357 walk_tree_fn func;
3358 unsigned int i;
3359 tree iter;
3361 /* We shouldn't be applying the optimization to return types that we aren't
3362 allowed to manipulate freely. */
3363 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
3365 /* Prune the candidates that are referenced by other return values. */
3366 data.nrv = nrv;
3367 data.result = NULL_TREE;
3368 data.visited = NULL;
3369 for (i = 0; vec_safe_iterate (other, i, &iter); i++)
3370 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3371 if (bitmap_empty_p (nrv))
3372 return;
3374 /* Prune also the candidates that are referenced by nested functions. */
3375 node = cgraph_node::get_create (fndecl);
3376 for (node = node->nested; node; node = node->next_nested)
3377 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), prune_nrv_r,
3378 &data);
3379 if (bitmap_empty_p (nrv))
3380 return;
3382 /* Extract a set of NRVs with non-overlapping live ranges. */
3383 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3384 return;
3386 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3387 data.nrv = nrv;
3388 data.result = DECL_RESULT (fndecl);
3389 data.gnat_ret = gnat_ret;
3390 data.visited = new hash_set<tree>;
3391 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl)))
3392 func = finalize_nrv_unc_r;
3393 else
3394 func = finalize_nrv_r;
3395 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
3396 delete data.visited;
3399 /* Return true if RET_VAL can be used as a Named Return Value for the
3400 anonymous return object RET_OBJ. */
3402 static bool
3403 return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3405 if (TREE_CODE (ret_val) != VAR_DECL)
3406 return false;
3408 if (TREE_THIS_VOLATILE (ret_val))
3409 return false;
3411 if (DECL_CONTEXT (ret_val) != current_function_decl)
3412 return false;
3414 if (TREE_STATIC (ret_val))
3415 return false;
3417 if (TREE_ADDRESSABLE (ret_val))
3418 return false;
3420 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
3421 return false;
3423 return true;
3426 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3427 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3428 around RESULT_OBJ, which may be null in this case. */
3430 static tree
3431 build_return_expr (tree ret_obj, tree ret_val)
3433 tree result_expr;
3435 if (ret_val)
3437 /* The gimplifier explicitly enforces the following invariant:
3439 RETURN_EXPR
3441 MODIFY_EXPR
3444 RET_OBJ ...
3446 As a consequence, type consistency dictates that we use the type
3447 of the RET_OBJ as the operation type. */
3448 tree operation_type = TREE_TYPE (ret_obj);
3450 /* Convert the right operand to the operation type. Note that it's the
3451 same transformation as in the MODIFY_EXPR case of build_binary_op,
3452 with the assumption that the type cannot involve a placeholder. */
3453 if (operation_type != TREE_TYPE (ret_val))
3454 ret_val = convert (operation_type, ret_val);
3456 result_expr = build2 (MODIFY_EXPR, void_type_node, ret_obj, ret_val);
3458 /* If the function returns an aggregate type, find out whether this is
3459 a candidate for Named Return Value. If so, record it. Otherwise,
3460 if this is an expression of some kind, record it elsewhere. */
3461 if (optimize
3462 && AGGREGATE_TYPE_P (operation_type)
3463 && !TYPE_IS_FAT_POINTER_P (operation_type)
3464 && TYPE_MODE (operation_type) == BLKmode
3465 && aggregate_value_p (operation_type, current_function_decl))
3467 /* Recognize the temporary created for a return value with variable
3468 size in Call_to_gnu. We want to eliminate it if possible. */
3469 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3470 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3471 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3472 == TREE_OPERAND (ret_val, 1))
3473 ret_val = TREE_OPERAND (ret_val, 1);
3475 /* Strip useless conversions around the return value. */
3476 if (gnat_useless_type_conversion (ret_val))
3477 ret_val = TREE_OPERAND (ret_val, 0);
3479 /* Now apply the test to the return value. */
3480 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3482 if (!f_named_ret_val)
3483 f_named_ret_val = BITMAP_GGC_ALLOC ();
3484 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3487 /* Note that we need not care about CONSTRUCTORs here, as they are
3488 totally transparent given the read-compose-write semantics of
3489 assignments from CONSTRUCTORs. */
3490 else if (EXPR_P (ret_val))
3491 vec_safe_push (f_other_ret_val, ret_val);
3494 else
3495 result_expr = ret_obj;
3497 return build1 (RETURN_EXPR, void_type_node, result_expr);
3500 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3501 don't return anything. */
3503 static void
3504 Subprogram_Body_to_gnu (Node_Id gnat_node)
3506 /* Defining identifier of a parameter to the subprogram. */
3507 Entity_Id gnat_param;
3508 /* The defining identifier for the subprogram body. Note that if a
3509 specification has appeared before for this body, then the identifier
3510 occurring in that specification will also be a defining identifier and all
3511 the calls to this subprogram will point to that specification. */
3512 Entity_Id gnat_subprog_id
3513 = (Present (Corresponding_Spec (gnat_node))
3514 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
3515 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3516 tree gnu_subprog_decl;
3517 /* Its RESULT_DECL node. */
3518 tree gnu_result_decl;
3519 /* Its FUNCTION_TYPE node. */
3520 tree gnu_subprog_type;
3521 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3522 tree gnu_cico_list;
3523 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3524 tree gnu_return_var_elmt = NULL_TREE;
3525 tree gnu_result;
3526 location_t locus;
3527 struct language_function *gnu_subprog_language;
3528 vec<parm_attr, va_gc> *cache;
3530 /* If this is a generic object or if it has been eliminated,
3531 ignore it. */
3532 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
3533 || Ekind (gnat_subprog_id) == E_Generic_Function
3534 || Is_Eliminated (gnat_subprog_id))
3535 return;
3537 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3538 the already-elaborated tree node. However, if this subprogram had its
3539 elaboration deferred, we will already have made a tree node for it. So
3540 treat it as not being defined in that case. Such a subprogram cannot
3541 have an address clause or a freeze node, so this test is safe, though it
3542 does disable some otherwise-useful error checking. */
3543 gnu_subprog_decl
3544 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
3545 Acts_As_Spec (gnat_node)
3546 && !present_gnu_tree (gnat_subprog_id));
3547 gnu_result_decl = DECL_RESULT (gnu_subprog_decl);
3548 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
3549 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3550 if (gnu_cico_list)
3551 gnu_return_var_elmt = value_member (void_type_node, gnu_cico_list);
3553 /* If the function returns by invisible reference, make it explicit in the
3554 function body. See gnat_to_gnu_entity, E_Subprogram_Type case.
3555 Handle the explicit case here and the copy-in/copy-out case below. */
3556 if (TREE_ADDRESSABLE (gnu_subprog_type) && !gnu_return_var_elmt)
3558 TREE_TYPE (gnu_result_decl)
3559 = build_reference_type (TREE_TYPE (gnu_result_decl));
3560 relayout_decl (gnu_result_decl);
3563 /* Set the line number in the decl to correspond to that of the body. */
3564 Sloc_to_locus (Sloc (gnat_node), &locus);
3565 DECL_SOURCE_LOCATION (gnu_subprog_decl) = locus;
3567 /* Initialize the information structure for the function. */
3568 allocate_struct_function (gnu_subprog_decl, false);
3569 gnu_subprog_language = ggc_cleared_alloc<language_function> ();
3570 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language = gnu_subprog_language;
3571 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->function_start_locus = locus;
3572 set_cfun (NULL);
3574 begin_subprog_body (gnu_subprog_decl);
3576 /* If there are In Out or Out parameters, we need to ensure that the return
3577 statement properly copies them out. We do this by making a new block and
3578 converting any return into a goto to a label at the end of the block. */
3579 if (gnu_cico_list)
3581 tree gnu_return_var = NULL_TREE;
3583 vec_safe_push (gnu_return_label_stack,
3584 create_artificial_label (input_location));
3586 start_stmt_group ();
3587 gnat_pushlevel ();
3589 /* If this is a function with In Out or Out parameters, we also need a
3590 variable for the return value to be placed. */
3591 if (gnu_return_var_elmt)
3593 tree gnu_return_type
3594 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3596 /* If the function returns by invisible reference, make it
3597 explicit in the function body. See gnat_to_gnu_entity,
3598 E_Subprogram_Type case. */
3599 if (TREE_ADDRESSABLE (gnu_subprog_type))
3600 gnu_return_type = build_reference_type (gnu_return_type);
3602 gnu_return_var
3603 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
3604 gnu_return_type, NULL_TREE, false, false,
3605 false, false, NULL, gnat_subprog_id);
3606 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3609 vec_safe_push (gnu_return_var_stack, gnu_return_var);
3611 /* See whether there are parameters for which we don't have a GCC tree
3612 yet. These must be Out parameters. Make a VAR_DECL for them and
3613 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3614 We can match up the entries because TYPE_CI_CO_LIST is in the order
3615 of the parameters. */
3616 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3617 Present (gnat_param);
3618 gnat_param = Next_Formal_With_Extras (gnat_param))
3619 if (!present_gnu_tree (gnat_param))
3621 tree gnu_cico_entry = gnu_cico_list;
3622 tree gnu_decl;
3624 /* Skip any entries that have been already filled in; they must
3625 correspond to In Out parameters. */
3626 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3627 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3629 /* Do any needed dereferences for by-ref objects. */
3630 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, 1);
3631 gcc_assert (DECL_P (gnu_decl));
3632 if (DECL_BY_REF_P (gnu_decl))
3633 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3635 /* Do any needed references for padded types. */
3636 TREE_VALUE (gnu_cico_entry)
3637 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
3640 else
3641 vec_safe_push (gnu_return_label_stack, NULL_TREE);
3643 /* Get a tree corresponding to the code for the subprogram. */
3644 start_stmt_group ();
3645 gnat_pushlevel ();
3647 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3649 /* Generate the code of the subprogram itself. A return statement will be
3650 present and any Out parameters will be handled there. */
3651 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3652 gnat_poplevel ();
3653 gnu_result = end_stmt_group ();
3655 /* If we populated the parameter attributes cache, we need to make sure that
3656 the cached expressions are evaluated on all the possible paths leading to
3657 their uses. So we force their evaluation on entry of the function. */
3658 cache = gnu_subprog_language->parm_attr_cache;
3659 if (cache)
3661 struct parm_attr_d *pa;
3662 int i;
3664 start_stmt_group ();
3666 FOR_EACH_VEC_ELT (*cache, i, pa)
3668 if (pa->first)
3669 add_stmt_with_node_force (pa->first, gnat_node);
3670 if (pa->last)
3671 add_stmt_with_node_force (pa->last, gnat_node);
3672 if (pa->length)
3673 add_stmt_with_node_force (pa->length, gnat_node);
3676 add_stmt (gnu_result);
3677 gnu_result = end_stmt_group ();
3679 gnu_subprog_language->parm_attr_cache = NULL;
3682 /* If we are dealing with a return from an Ada procedure with parameters
3683 passed by copy-in/copy-out, we need to return a record containing the
3684 final values of these parameters. If the list contains only one entry,
3685 return just that entry though.
3687 For a full description of the copy-in/copy-out parameter mechanism, see
3688 the part of the gnat_to_gnu_entity routine dealing with the translation
3689 of subprograms.
3691 We need to make a block that contains the definition of that label and
3692 the copying of the return value. It first contains the function, then
3693 the label and copy statement. */
3694 if (gnu_cico_list)
3696 tree gnu_retval;
3698 gnu_return_var_stack->pop ();
3700 add_stmt (gnu_result);
3701 add_stmt (build1 (LABEL_EXPR, void_type_node,
3702 gnu_return_label_stack->last ()));
3704 if (list_length (gnu_cico_list) == 1)
3705 gnu_retval = TREE_VALUE (gnu_cico_list);
3706 else
3707 gnu_retval = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
3708 gnu_cico_list);
3710 add_stmt_with_node (build_return_expr (gnu_result_decl, gnu_retval),
3711 End_Label (Handled_Statement_Sequence (gnat_node)));
3712 gnat_poplevel ();
3713 gnu_result = end_stmt_group ();
3716 gnu_return_label_stack->pop ();
3718 /* Attempt setting the end_locus of our GCC body tree, typically a
3719 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3720 declaration tree. */
3721 set_end_locus_from_node (gnu_result, gnat_node);
3722 set_end_locus_from_node (gnu_subprog_decl, gnat_node);
3724 /* On SEH targets, install an exception handler around the main entry
3725 point to catch unhandled exceptions. */
3726 if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
3727 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
3729 tree t;
3730 tree etype;
3732 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
3733 1, integer_zero_node);
3734 t = build_call_n_expr (unhandled_except_decl, 1, t);
3736 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
3737 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
3739 t = build2 (CATCH_EXPR, void_type_node, etype, t);
3740 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
3741 gnu_result, t);
3744 end_subprog_body (gnu_result);
3746 /* Finally annotate the parameters and disconnect the trees for parameters
3747 that we have turned into variables since they are now unusable. */
3748 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3749 Present (gnat_param);
3750 gnat_param = Next_Formal_With_Extras (gnat_param))
3752 tree gnu_param = get_gnu_tree (gnat_param);
3753 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
3755 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
3756 DECL_BY_REF_P (gnu_param));
3758 if (is_var_decl)
3759 save_gnu_tree (gnat_param, NULL_TREE, false);
3762 /* Disconnect the variable created for the return value. */
3763 if (gnu_return_var_elmt)
3764 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
3766 /* If the function returns an aggregate type and we have candidates for
3767 a Named Return Value, finalize the optimization. */
3768 if (optimize && gnu_subprog_language->named_ret_val)
3770 finalize_nrv (gnu_subprog_decl,
3771 gnu_subprog_language->named_ret_val,
3772 gnu_subprog_language->other_ret_val,
3773 gnu_subprog_language->gnat_ret);
3774 gnu_subprog_language->named_ret_val = NULL;
3775 gnu_subprog_language->other_ret_val = NULL;
3778 /* If this is an inlined external function that has been marked uninlinable,
3779 drop the body and stop there. Otherwise compile the body. */
3780 if (DECL_EXTERNAL (gnu_subprog_decl) && DECL_UNINLINABLE (gnu_subprog_decl))
3781 DECL_SAVED_TREE (gnu_subprog_decl) = NULL_TREE;
3782 else
3783 rest_of_subprog_body_compilation (gnu_subprog_decl);
3786 /* Return true if GNAT_NODE requires atomic synchronization. */
3788 static bool
3789 atomic_sync_required_p (Node_Id gnat_node)
3791 const Node_Id gnat_parent = Parent (gnat_node);
3792 Node_Kind kind;
3793 unsigned char attr_id;
3795 /* First, scan the node to find the Atomic_Sync_Required flag. */
3796 kind = Nkind (gnat_node);
3797 if (kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
3799 gnat_node = Expression (gnat_node);
3800 kind = Nkind (gnat_node);
3803 switch (kind)
3805 case N_Expanded_Name:
3806 case N_Explicit_Dereference:
3807 case N_Identifier:
3808 case N_Indexed_Component:
3809 case N_Selected_Component:
3810 if (!Atomic_Sync_Required (gnat_node))
3811 return false;
3812 break;
3814 default:
3815 return false;
3818 /* Then, scan the parent to find out cases where the flag is irrelevant. */
3819 kind = Nkind (gnat_parent);
3820 switch (kind)
3822 case N_Attribute_Reference:
3823 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
3824 /* Do not mess up machine code insertions. */
3825 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
3826 return false;
3827 break;
3829 case N_Object_Renaming_Declaration:
3830 /* Do not generate a function call as a renamed object. */
3831 return false;
3833 default:
3834 break;
3837 return true;
3840 /* Create a temporary variable with PREFIX and TYPE, and return it. */
3842 static tree
3843 create_temporary (const char *prefix, tree type)
3845 tree gnu_temp = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
3846 type, NULL_TREE, false, false, false, false,
3847 NULL, Empty);
3848 DECL_ARTIFICIAL (gnu_temp) = 1;
3849 DECL_IGNORED_P (gnu_temp) = 1;
3851 return gnu_temp;
3854 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
3855 Put the initialization statement into GNU_INIT_STMT and annotate it with
3856 the SLOC of GNAT_NODE. Return the temporary variable. */
3858 static tree
3859 create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
3860 Node_Id gnat_node)
3862 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
3864 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
3865 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
3867 return gnu_temp;
3870 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
3871 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
3872 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
3873 If GNU_TARGET is non-null, this must be a function call on the RHS of a
3874 N_Assignment_Statement and the result is to be placed into that object.
3875 If, in addition, ATOMIC_SYNC is true, then the assignment to GNU_TARGET
3876 requires atomic synchronization. */
3878 static tree
3879 Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
3880 bool atomic_sync)
3882 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
3883 const bool returning_value = (function_call && !gnu_target);
3884 /* The GCC node corresponding to the GNAT subprogram name. This can either
3885 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
3886 or an indirect reference expression (an INDIRECT_REF node) pointing to a
3887 subprogram. */
3888 tree gnu_subprog = gnat_to_gnu (Name (gnat_node));
3889 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
3890 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
3891 /* The return type of the FUNCTION_TYPE. */
3892 tree gnu_result_type = TREE_TYPE (gnu_subprog_type);
3893 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog);
3894 vec<tree, va_gc> *gnu_actual_vec = NULL;
3895 tree gnu_name_list = NULL_TREE;
3896 tree gnu_stmt_list = NULL_TREE;
3897 tree gnu_after_list = NULL_TREE;
3898 tree gnu_retval = NULL_TREE;
3899 tree gnu_call, gnu_result;
3900 bool went_into_elab_proc = false;
3901 bool pushed_binding_level = false;
3902 Entity_Id gnat_formal;
3903 Node_Id gnat_actual;
3905 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
3907 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
3908 all our args first. */
3909 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL && DECL_STUBBED_P (gnu_subprog))
3911 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
3912 gnat_node, N_Raise_Program_Error);
3914 for (gnat_actual = First_Actual (gnat_node);
3915 Present (gnat_actual);
3916 gnat_actual = Next_Actual (gnat_actual))
3917 add_stmt (gnat_to_gnu (gnat_actual));
3919 if (returning_value)
3921 *gnu_result_type_p = gnu_result_type;
3922 return build1 (NULL_EXPR, gnu_result_type, call_expr);
3925 return call_expr;
3928 /* For a call to a nested function, check the inlining status. */
3929 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL
3930 && decl_function_context (gnu_subprog))
3931 check_inlining_for_nested_subprog (gnu_subprog);
3933 /* The only way we can be making a call via an access type is if Name is an
3934 explicit dereference. In that case, get the list of formal args from the
3935 type the access type is pointing to. Otherwise, get the formals from the
3936 entity being called. */
3937 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
3938 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
3939 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
3940 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
3941 gnat_formal = Empty;
3942 else
3943 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
3945 /* The lifetime of the temporaries created for the call ends right after the
3946 return value is copied, so we can give them the scope of the elaboration
3947 routine at top level. */
3948 if (!current_function_decl)
3950 current_function_decl = get_elaboration_procedure ();
3951 went_into_elab_proc = true;
3954 /* First, create the temporary for the return value when:
3956 1. There is no target and the function has copy-in/copy-out parameters,
3957 because we need to preserve the return value before copying back the
3958 parameters.
3960 2. There is no target and this is not an object declaration, and the
3961 return type has variable size, because in these cases the gimplifier
3962 cannot create the temporary.
3964 3. There is a target and it is a slice or an array with fixed size,
3965 and the return type has variable size, because the gimplifier
3966 doesn't handle these cases.
3968 This must be done before we push a binding level around the call, since
3969 we will pop it before copying the return value. */
3970 if (function_call
3971 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
3972 || (!gnu_target
3973 && Nkind (Parent (gnat_node)) != N_Object_Declaration
3974 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)
3975 || (gnu_target
3976 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
3977 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
3978 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
3979 == INTEGER_CST))
3980 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)))
3981 gnu_retval = create_temporary ("R", gnu_result_type);
3983 /* Create the list of the actual parameters as GCC expects it, namely a
3984 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
3985 is an expression and the TREE_PURPOSE field is null. But skip Out
3986 parameters not passed by reference and that need not be copied in. */
3987 for (gnat_actual = First_Actual (gnat_node);
3988 Present (gnat_actual);
3989 gnat_formal = Next_Formal_With_Extras (gnat_formal),
3990 gnat_actual = Next_Actual (gnat_actual))
3992 tree gnu_formal = present_gnu_tree (gnat_formal)
3993 ? get_gnu_tree (gnat_formal) : NULL_TREE;
3994 tree gnu_formal_type = gnat_to_gnu_type (Etype (gnat_formal));
3995 const bool is_true_formal_parm
3996 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
3997 const bool is_by_ref_formal_parm
3998 = is_true_formal_parm
3999 && (DECL_BY_REF_P (gnu_formal)
4000 || DECL_BY_COMPONENT_PTR_P (gnu_formal));
4001 /* In the Out or In Out case, we must suppress conversions that yield
4002 an lvalue but can nevertheless cause the creation of a temporary,
4003 because we need the real object in this case, either to pass its
4004 address if it's passed by reference or as target of the back copy
4005 done after the call if it uses the copy-in/copy-out mechanism.
4006 We do it in the In case too, except for an unchecked conversion
4007 because it alone can cause the actual to be misaligned and the
4008 addressability test is applied to the real object. */
4009 const bool suppress_type_conversion
4010 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
4011 && Ekind (gnat_formal) != E_In_Parameter)
4012 || (Nkind (gnat_actual) == N_Type_Conversion
4013 && Is_Composite_Type (Underlying_Type (Etype (gnat_formal)))));
4014 Node_Id gnat_name = suppress_type_conversion
4015 ? Expression (gnat_actual) : gnat_actual;
4016 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
4017 tree gnu_actual;
4019 /* If it's possible we may need to use this expression twice, make sure
4020 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4021 to force side-effects before the call.
4022 ??? This is more conservative than we need since we don't need to do
4023 this for pass-by-ref with no conversion. */
4024 if (Ekind (gnat_formal) != E_In_Parameter)
4025 gnu_name = gnat_stabilize_reference (gnu_name, true, NULL);
4027 /* If we are passing a non-addressable parameter by reference, pass the
4028 address of a copy. In the Out or In Out case, set up to copy back
4029 out after the call. */
4030 if (is_by_ref_formal_parm
4031 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
4032 && !addressable_p (gnu_name, gnu_name_type))
4034 bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
4035 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4037 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4038 but sort of an instantiation for them. */
4039 if (TREE_CODE (gnu_name) == CONSTRUCTOR)
4042 /* If the type is passed by reference, a copy is not allowed. */
4043 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type))
4044 post_error ("misaligned actual cannot be passed by reference",
4045 gnat_actual);
4047 /* For users of Starlet we issue a warning because the interface
4048 apparently assumes that by-ref parameters outlive the procedure
4049 invocation. The code still will not work as intended, but we
4050 cannot do much better since low-level parts of the back-end
4051 would allocate temporaries at will because of the misalignment
4052 if we did not do so here. */
4053 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
4055 post_error
4056 ("?possible violation of implicit assumption", gnat_actual);
4057 post_error_ne
4058 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
4059 Entity (Name (gnat_node)));
4060 post_error_ne ("?because of misalignment of &", gnat_actual,
4061 gnat_formal);
4064 /* If the actual type of the object is already the nominal type,
4065 we have nothing to do, except if the size is self-referential
4066 in which case we'll remove the unpadding below. */
4067 if (TREE_TYPE (gnu_name) == gnu_name_type
4068 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4071 /* Otherwise remove the unpadding from all the objects. */
4072 else if (TREE_CODE (gnu_name) == COMPONENT_REF
4073 && TYPE_IS_PADDING_P
4074 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
4075 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
4077 /* Otherwise convert to the nominal type of the object if needed.
4078 There are several cases in which we need to make the temporary
4079 using this type instead of the actual type of the object when
4080 they are distinct, because the expectations of the callee would
4081 otherwise not be met:
4082 - if it's a justified modular type,
4083 - if the actual type is a smaller form of it,
4084 - if it's a smaller form of the actual type. */
4085 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4086 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4087 || smaller_form_type_p (TREE_TYPE (gnu_name),
4088 gnu_name_type)))
4089 || (INTEGRAL_TYPE_P (gnu_name_type)
4090 && smaller_form_type_p (gnu_name_type,
4091 TREE_TYPE (gnu_name))))
4092 gnu_name = convert (gnu_name_type, gnu_name);
4094 /* If this is an In Out or Out parameter and we're returning a value,
4095 we need to create a temporary for the return value because we must
4096 preserve it before copying back at the very end. */
4097 if (!in_param && returning_value && !gnu_retval)
4098 gnu_retval = create_temporary ("R", gnu_result_type);
4100 /* If we haven't pushed a binding level, push a new one. This will
4101 narrow the lifetime of the temporary we are about to make as much
4102 as possible. The drawback is that we'd need to create a temporary
4103 for the return value, if any (see comment before the loop). So do
4104 it only when this temporary was already created just above. */
4105 if (!pushed_binding_level && !(in_param && returning_value))
4107 start_stmt_group ();
4108 gnat_pushlevel ();
4109 pushed_binding_level = true;
4112 /* Create an explicit temporary holding the copy. */
4113 gnu_temp
4114 = create_init_temporary ("A", gnu_name, &gnu_stmt, gnat_actual);
4116 /* But initialize it on the fly like for an implicit temporary as
4117 we aren't necessarily having a statement list. */
4118 gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt,
4119 gnu_temp);
4121 /* Set up to move the copy back to the original if needed. */
4122 if (!in_param)
4124 /* If the original is a COND_EXPR whose first arm isn't meant to
4125 be further used, just deal with the second arm. This is very
4126 likely the conditional expression built for a check. */
4127 if (TREE_CODE (gnu_orig) == COND_EXPR
4128 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4129 && integer_zerop
4130 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4131 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4133 gnu_stmt
4134 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp);
4135 set_expr_location_from_node (gnu_stmt, gnat_node);
4137 append_to_statement_list (gnu_stmt, &gnu_after_list);
4141 /* Start from the real object and build the actual. */
4142 gnu_actual = gnu_name;
4144 /* If this is an atomic access of an In or In Out parameter for which
4145 synchronization is required, build the atomic load. */
4146 if (is_true_formal_parm
4147 && !is_by_ref_formal_parm
4148 && Ekind (gnat_formal) != E_Out_Parameter
4149 && atomic_sync_required_p (gnat_actual))
4150 gnu_actual = build_atomic_load (gnu_actual);
4152 /* If this was a procedure call, we may not have removed any padding.
4153 So do it here for the part we will use as an input, if any. */
4154 if (Ekind (gnat_formal) != E_Out_Parameter
4155 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4156 gnu_actual
4157 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4159 /* Put back the conversion we suppressed above in the computation of the
4160 real object. And even if we didn't suppress any conversion there, we
4161 may have suppressed a conversion to the Etype of the actual earlier,
4162 since the parent is a procedure call, so put it back here. */
4163 if (suppress_type_conversion
4164 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4165 gnu_actual
4166 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
4167 gnu_actual, No_Truncation (gnat_actual));
4168 else
4169 gnu_actual
4170 = convert (gnat_to_gnu_type (Etype (gnat_actual)), gnu_actual);
4172 /* Make sure that the actual is in range of the formal's type. */
4173 if (Ekind (gnat_formal) != E_Out_Parameter
4174 && Do_Range_Check (gnat_actual))
4175 gnu_actual
4176 = emit_range_check (gnu_actual, Etype (gnat_formal), gnat_actual);
4178 /* Unless this is an In parameter, we must remove any justified modular
4179 building from GNU_NAME to get an lvalue. */
4180 if (Ekind (gnat_formal) != E_In_Parameter
4181 && TREE_CODE (gnu_name) == CONSTRUCTOR
4182 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
4183 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
4184 gnu_name
4185 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))), gnu_name);
4187 /* First see if the parameter is passed by reference. */
4188 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
4190 if (Ekind (gnat_formal) != E_In_Parameter)
4192 /* In Out or Out parameters passed by reference don't use the
4193 copy-in/copy-out mechanism so the address of the real object
4194 must be passed to the function. */
4195 gnu_actual = gnu_name;
4197 /* If we have a padded type, be sure we've removed padding. */
4198 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4199 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
4200 gnu_actual);
4202 /* If we have the constructed subtype of an aliased object
4203 with an unconstrained nominal subtype, the type of the
4204 actual includes the template, although it is formally
4205 constrained. So we need to convert it back to the real
4206 constructed subtype to retrieve the constrained part
4207 and takes its address. */
4208 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4209 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
4210 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
4211 && Is_Array_Type (Underlying_Type (Etype (gnat_actual))))
4212 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4213 gnu_actual);
4216 /* There is no need to convert the actual to the formal's type before
4217 taking its address. The only exception is for unconstrained array
4218 types because of the way we build fat pointers. */
4219 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4221 /* Put back a view conversion for In Out or Out parameters. */
4222 if (Ekind (gnat_formal) != E_In_Parameter)
4223 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4224 gnu_actual);
4225 gnu_actual = convert (gnu_formal_type, gnu_actual);
4228 /* The symmetry of the paths to the type of an entity is broken here
4229 since arguments don't know that they will be passed by ref. */
4230 gnu_formal_type = TREE_TYPE (gnu_formal);
4231 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4234 /* Then see if the parameter is an array passed to a foreign convention
4235 subprogram. */
4236 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
4238 gnu_formal_type = TREE_TYPE (gnu_formal);
4239 gnu_actual = maybe_implicit_deref (gnu_actual);
4240 gnu_actual = maybe_unconstrained_array (gnu_actual);
4242 if (TYPE_IS_PADDING_P (gnu_formal_type))
4244 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
4245 gnu_actual = convert (gnu_formal_type, gnu_actual);
4248 /* Take the address of the object and convert to the proper pointer
4249 type. We'd like to actually compute the address of the beginning
4250 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
4251 possibility that the ARRAY_REF might return a constant and we'd be
4252 getting the wrong address. Neither approach is exactly correct,
4253 but this is the most likely to work in all cases. */
4254 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4257 /* Otherwise the parameter is passed by copy. */
4258 else
4260 tree gnu_size;
4262 if (Ekind (gnat_formal) != E_In_Parameter)
4263 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
4265 /* If we didn't create a PARM_DECL for the formal, this means that
4266 it is an Out parameter not passed by reference and that need not
4267 be copied in. In this case, the value of the actual need not be
4268 read. However, we still need to make sure that its side-effects
4269 are evaluated before the call, so we evaluate its address. */
4270 if (!is_true_formal_parm)
4272 if (TREE_SIDE_EFFECTS (gnu_name))
4274 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
4275 append_to_statement_list (addr, &gnu_stmt_list);
4277 continue;
4280 gnu_actual = convert (gnu_formal_type, gnu_actual);
4282 /* If this is 'Null_Parameter, pass a zero even though we are
4283 dereferencing it. */
4284 if (TREE_CODE (gnu_actual) == INDIRECT_REF
4285 && TREE_PRIVATE (gnu_actual)
4286 && (gnu_size = TYPE_SIZE (TREE_TYPE (gnu_actual)))
4287 && TREE_CODE (gnu_size) == INTEGER_CST
4288 && compare_tree_int (gnu_size, BITS_PER_WORD) <= 0)
4289 gnu_actual
4290 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
4291 convert (gnat_type_for_size
4292 (TREE_INT_CST_LOW (gnu_size), 1),
4293 integer_zero_node),
4294 false);
4295 else
4296 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
4299 vec_safe_push (gnu_actual_vec, gnu_actual);
4302 gnu_call
4303 = build_call_vec (gnu_result_type, gnu_subprog_addr, gnu_actual_vec);
4304 set_expr_location_from_node (gnu_call, gnat_node);
4306 /* If we have created a temporary for the return value, initialize it. */
4307 if (gnu_retval)
4309 tree gnu_stmt
4310 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
4311 set_expr_location_from_node (gnu_stmt, gnat_node);
4312 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4313 gnu_call = gnu_retval;
4316 /* If this is a subprogram with copy-in/copy-out parameters, we need to
4317 unpack the valued returned from the function into the In Out or Out
4318 parameters. We deal with the function return (if this is an Ada
4319 function) below. */
4320 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4322 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
4323 copy-out parameters. */
4324 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
4325 const int length = list_length (gnu_cico_list);
4327 /* The call sequence must contain one and only one call, even though the
4328 function is pure. Save the result into a temporary if needed. */
4329 if (length > 1)
4331 if (!gnu_retval)
4333 tree gnu_stmt;
4334 /* If we haven't pushed a binding level, push a new one. This
4335 will narrow the lifetime of the temporary we are about to
4336 make as much as possible. */
4337 if (!pushed_binding_level)
4339 start_stmt_group ();
4340 gnat_pushlevel ();
4341 pushed_binding_level = true;
4343 gnu_call
4344 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
4345 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4348 gnu_name_list = nreverse (gnu_name_list);
4351 /* The first entry is for the actual return value if this is a
4352 function, so skip it. */
4353 if (function_call)
4354 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4356 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4357 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4358 else
4359 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4361 for (gnat_actual = First_Actual (gnat_node);
4362 Present (gnat_actual);
4363 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4364 gnat_actual = Next_Actual (gnat_actual))
4365 /* If we are dealing with a copy-in/copy-out parameter, we must
4366 retrieve its value from the record returned in the call. */
4367 if (!(present_gnu_tree (gnat_formal)
4368 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4369 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
4370 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))))
4371 && Ekind (gnat_formal) != E_In_Parameter)
4373 /* Get the value to assign to this Out or In Out parameter. It is
4374 either the result of the function if there is only a single such
4375 parameter or the appropriate field from the record returned. */
4376 tree gnu_result
4377 = length == 1
4378 ? gnu_call
4379 : build_component_ref (gnu_call, NULL_TREE,
4380 TREE_PURPOSE (gnu_cico_list), false);
4382 /* If the actual is a conversion, get the inner expression, which
4383 will be the real destination, and convert the result to the
4384 type of the actual parameter. */
4385 tree gnu_actual
4386 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
4388 /* If the result is a padded type, remove the padding. */
4389 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4390 gnu_result
4391 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4392 gnu_result);
4394 /* If the actual is a type conversion, the real target object is
4395 denoted by the inner Expression and we need to convert the
4396 result to the associated type.
4397 We also need to convert our gnu assignment target to this type
4398 if the corresponding GNU_NAME was constructed from the GNAT
4399 conversion node and not from the inner Expression. */
4400 if (Nkind (gnat_actual) == N_Type_Conversion)
4402 gnu_result
4403 = convert_with_check
4404 (Etype (Expression (gnat_actual)), gnu_result,
4405 Do_Overflow_Check (gnat_actual),
4406 Do_Range_Check (Expression (gnat_actual)),
4407 Float_Truncate (gnat_actual), gnat_actual);
4409 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
4410 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
4413 /* Unchecked conversions as actuals for Out parameters are not
4414 allowed in user code because they are not variables, but do
4415 occur in front-end expansions. The associated GNU_NAME is
4416 always obtained from the inner expression in such cases. */
4417 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4418 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
4419 gnu_result,
4420 No_Truncation (gnat_actual));
4421 else
4423 if (Do_Range_Check (gnat_actual))
4424 gnu_result
4425 = emit_range_check (gnu_result, Etype (gnat_actual),
4426 gnat_actual);
4428 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
4429 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
4430 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
4433 if (atomic_sync_required_p (gnat_actual))
4434 gnu_result = build_atomic_store (gnu_actual, gnu_result);
4435 else
4436 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4437 gnu_actual, gnu_result);
4438 set_expr_location_from_node (gnu_result, gnat_node);
4439 append_to_statement_list (gnu_result, &gnu_stmt_list);
4440 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4441 gnu_name_list = TREE_CHAIN (gnu_name_list);
4445 /* If this is a function call, the result is the call expression unless a
4446 target is specified, in which case we copy the result into the target
4447 and return the assignment statement. */
4448 if (function_call)
4450 /* If this is a function with copy-in/copy-out parameters, extract the
4451 return value from it and update the return type. */
4452 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4454 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
4455 gnu_call = build_component_ref (gnu_call, NULL_TREE,
4456 TREE_PURPOSE (gnu_elmt), false);
4457 gnu_result_type = TREE_TYPE (gnu_call);
4460 /* If the function returns an unconstrained array or by direct reference,
4461 we have to dereference the pointer. */
4462 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type)
4463 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
4464 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
4466 if (gnu_target)
4468 Node_Id gnat_parent = Parent (gnat_node);
4469 enum tree_code op_code;
4471 /* If range check is needed, emit code to generate it. */
4472 if (Do_Range_Check (gnat_node))
4473 gnu_call
4474 = emit_range_check (gnu_call, Etype (Name (gnat_parent)),
4475 gnat_parent);
4477 /* ??? If the return type has variable size, then force the return
4478 slot optimization as we would not be able to create a temporary.
4479 Likewise if it was unconstrained as we would copy too much data.
4480 That's what has been done historically. */
4481 if (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
4482 || (TYPE_IS_PADDING_P (gnu_result_type)
4483 && CONTAINS_PLACEHOLDER_P
4484 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_result_type))))))
4485 op_code = INIT_EXPR;
4486 else
4487 op_code = MODIFY_EXPR;
4489 if (atomic_sync)
4490 gnu_call = build_atomic_store (gnu_target, gnu_call);
4491 else
4492 gnu_call
4493 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
4494 set_expr_location_from_node (gnu_call, gnat_parent);
4495 append_to_statement_list (gnu_call, &gnu_stmt_list);
4497 else
4498 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
4501 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4502 parameters, the result is just the call statement. */
4503 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
4504 append_to_statement_list (gnu_call, &gnu_stmt_list);
4506 /* Finally, add the copy back statements, if any. */
4507 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
4509 if (went_into_elab_proc)
4510 current_function_decl = NULL_TREE;
4512 /* If we have pushed a binding level, pop it and finish up the enclosing
4513 statement group. */
4514 if (pushed_binding_level)
4516 add_stmt (gnu_stmt_list);
4517 gnat_poplevel ();
4518 gnu_result = end_stmt_group ();
4521 /* Otherwise, retrieve the statement list, if any. */
4522 else if (gnu_stmt_list)
4523 gnu_result = gnu_stmt_list;
4525 /* Otherwise, just return the call expression. */
4526 else
4527 return gnu_call;
4529 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4530 But first simplify if we have only one statement in the list. */
4531 if (returning_value)
4533 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
4534 if (first == last)
4535 gnu_result = first;
4536 gnu_result
4537 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
4540 return gnu_result;
4543 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4544 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4546 static tree
4547 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
4549 tree gnu_jmpsave_decl = NULL_TREE;
4550 tree gnu_jmpbuf_decl = NULL_TREE;
4551 /* If just annotating, ignore all EH and cleanups. */
4552 bool gcc_zcx = (!type_annotate_only
4553 && Present (Exception_Handlers (gnat_node))
4554 && Exception_Mechanism == Back_End_Exceptions);
4555 bool setjmp_longjmp
4556 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
4557 && Exception_Mechanism == Setjmp_Longjmp);
4558 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
4559 bool binding_for_block = (at_end || gcc_zcx || setjmp_longjmp);
4560 tree gnu_inner_block; /* The statement(s) for the block itself. */
4561 tree gnu_result;
4562 tree gnu_expr;
4563 Node_Id gnat_temp;
4564 /* Node providing the sloc for the cleanup actions. */
4565 Node_Id gnat_cleanup_loc_node = (Present (End_Label (gnat_node)) ?
4566 End_Label (gnat_node) :
4567 gnat_node);
4569 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
4570 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
4571 add_cleanup, and when we leave the binding, end_stmt_group will create
4572 the TRY_FINALLY_EXPR.
4574 ??? The region level calls down there have been specifically put in place
4575 for a ZCX context and currently the order in which things are emitted
4576 (region/handlers) is different from the SJLJ case. Instead of putting
4577 other calls with different conditions at other places for the SJLJ case,
4578 it seems cleaner to reorder things for the SJLJ case and generalize the
4579 condition to make it not ZCX specific.
4581 If there are any exceptions or cleanup processing involved, we need an
4582 outer statement group (for Setjmp_Longjmp) and binding level. */
4583 if (binding_for_block)
4585 start_stmt_group ();
4586 gnat_pushlevel ();
4589 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
4590 area for address of previous buffer. Do this first since we need to have
4591 the setjmp buf known for any decls in this block. */
4592 if (setjmp_longjmp)
4594 gnu_jmpsave_decl
4595 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
4596 jmpbuf_ptr_type,
4597 build_call_n_expr (get_jmpbuf_decl, 0),
4598 false, false, false, false, NULL, gnat_node);
4599 DECL_ARTIFICIAL (gnu_jmpsave_decl) = 1;
4601 /* The __builtin_setjmp receivers will immediately reinstall it. Now
4602 because of the unstructured form of EH used by setjmp_longjmp, there
4603 might be forward edges going to __builtin_setjmp receivers on which
4604 it is uninitialized, although they will never be actually taken. */
4605 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
4606 gnu_jmpbuf_decl
4607 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE,
4608 jmpbuf_type,
4609 NULL_TREE,
4610 false, false, false, false, NULL, gnat_node);
4611 DECL_ARTIFICIAL (gnu_jmpbuf_decl) = 1;
4613 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
4615 /* When we exit this block, restore the saved value. */
4616 add_cleanup (build_call_n_expr (set_jmpbuf_decl, 1, gnu_jmpsave_decl),
4617 gnat_cleanup_loc_node);
4620 /* If we are to call a function when exiting this block, add a cleanup
4621 to the binding level we made above. Note that add_cleanup is FIFO
4622 so we must register this cleanup after the EH cleanup just above. */
4623 if (at_end)
4624 add_cleanup (build_call_n_expr (gnat_to_gnu (At_End_Proc (gnat_node)), 0),
4625 gnat_cleanup_loc_node);
4627 /* Now build the tree for the declarations and statements inside this block.
4628 If this is SJLJ, set our jmp_buf as the current buffer. */
4629 start_stmt_group ();
4631 if (setjmp_longjmp)
4632 add_stmt (build_call_n_expr (set_jmpbuf_decl, 1,
4633 build_unary_op (ADDR_EXPR, NULL_TREE,
4634 gnu_jmpbuf_decl)));
4636 if (Present (First_Real_Statement (gnat_node)))
4637 process_decls (Statements (gnat_node), Empty,
4638 First_Real_Statement (gnat_node), true, true);
4640 /* Generate code for each statement in the block. */
4641 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
4642 ? First_Real_Statement (gnat_node)
4643 : First (Statements (gnat_node)));
4644 Present (gnat_temp); gnat_temp = Next (gnat_temp))
4645 add_stmt (gnat_to_gnu (gnat_temp));
4646 gnu_inner_block = end_stmt_group ();
4648 /* Now generate code for the two exception models, if either is relevant for
4649 this block. */
4650 if (setjmp_longjmp)
4652 tree *gnu_else_ptr = 0;
4653 tree gnu_handler;
4655 /* Make a binding level for the exception handling declarations and code
4656 and set up gnu_except_ptr_stack for the handlers to use. */
4657 start_stmt_group ();
4658 gnat_pushlevel ();
4660 vec_safe_push (gnu_except_ptr_stack,
4661 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE,
4662 build_pointer_type (except_type_node),
4663 build_call_n_expr (get_excptr_decl, 0),
4664 false, false, false, false,
4665 NULL, gnat_node));
4667 /* Generate code for each handler. The N_Exception_Handler case does the
4668 real work and returns a COND_EXPR for each handler, which we chain
4669 together here. */
4670 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4671 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
4673 gnu_expr = gnat_to_gnu (gnat_temp);
4675 /* If this is the first one, set it as the outer one. Otherwise,
4676 point the "else" part of the previous handler to us. Then point
4677 to our "else" part. */
4678 if (!gnu_else_ptr)
4679 add_stmt (gnu_expr);
4680 else
4681 *gnu_else_ptr = gnu_expr;
4683 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
4686 /* If none of the exception handlers did anything, re-raise but do not
4687 defer abortion. */
4688 gnu_expr = build_call_n_expr (raise_nodefer_decl, 1,
4689 gnu_except_ptr_stack->last ());
4690 set_expr_location_from_node
4691 (gnu_expr,
4692 Present (End_Label (gnat_node)) ? End_Label (gnat_node) : gnat_node);
4694 if (gnu_else_ptr)
4695 *gnu_else_ptr = gnu_expr;
4696 else
4697 add_stmt (gnu_expr);
4699 /* End the binding level dedicated to the exception handlers and get the
4700 whole statement group. */
4701 gnu_except_ptr_stack->pop ();
4702 gnat_poplevel ();
4703 gnu_handler = end_stmt_group ();
4705 /* If the setjmp returns 1, we restore our incoming longjmp value and
4706 then check the handlers. */
4707 start_stmt_group ();
4708 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl, 1,
4709 gnu_jmpsave_decl),
4710 gnat_node);
4711 add_stmt (gnu_handler);
4712 gnu_handler = end_stmt_group ();
4714 /* This block is now "if (setjmp) ... <handlers> else <block>". */
4715 gnu_result = build3 (COND_EXPR, void_type_node,
4716 (build_call_n_expr
4717 (setjmp_decl, 1,
4718 build_unary_op (ADDR_EXPR, NULL_TREE,
4719 gnu_jmpbuf_decl))),
4720 gnu_handler, gnu_inner_block);
4722 else if (gcc_zcx)
4724 tree gnu_handlers;
4725 location_t locus;
4727 /* First make a block containing the handlers. */
4728 start_stmt_group ();
4729 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4730 Present (gnat_temp);
4731 gnat_temp = Next_Non_Pragma (gnat_temp))
4732 add_stmt (gnat_to_gnu (gnat_temp));
4733 gnu_handlers = end_stmt_group ();
4735 /* Now make the TRY_CATCH_EXPR for the block. */
4736 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
4737 gnu_inner_block, gnu_handlers);
4738 /* Set a location. We need to find a unique location for the dispatching
4739 code, otherwise we can get coverage or debugging issues. Try with
4740 the location of the end label. */
4741 if (Present (End_Label (gnat_node))
4742 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
4743 SET_EXPR_LOCATION (gnu_result, locus);
4744 else
4745 /* Clear column information so that the exception handler of an
4746 implicit transient block does not incorrectly inherit the slocs
4747 of a decision, which would otherwise confuse control flow based
4748 coverage analysis tools. */
4749 set_expr_location_from_node1 (gnu_result, gnat_node, true);
4751 else
4752 gnu_result = gnu_inner_block;
4754 /* Now close our outer block, if we had to make one. */
4755 if (binding_for_block)
4757 add_stmt (gnu_result);
4758 gnat_poplevel ();
4759 gnu_result = end_stmt_group ();
4762 return gnu_result;
4765 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4766 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
4767 exception handling. */
4769 static tree
4770 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
4772 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
4773 an "if" statement to select the proper exceptions. For "Others", exclude
4774 exceptions where Handled_By_Others is nonzero unless the All_Others flag
4775 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
4776 tree gnu_choice = boolean_false_node;
4777 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
4778 Node_Id gnat_temp;
4780 for (gnat_temp = First (Exception_Choices (gnat_node));
4781 gnat_temp; gnat_temp = Next (gnat_temp))
4783 tree this_choice;
4785 if (Nkind (gnat_temp) == N_Others_Choice)
4787 if (All_Others (gnat_temp))
4788 this_choice = boolean_true_node;
4789 else
4790 this_choice
4791 = build_binary_op
4792 (EQ_EXPR, boolean_type_node,
4793 convert
4794 (integer_type_node,
4795 build_component_ref
4796 (build_unary_op
4797 (INDIRECT_REF, NULL_TREE,
4798 gnu_except_ptr_stack->last ()),
4799 get_identifier ("not_handled_by_others"), NULL_TREE,
4800 false)),
4801 integer_zero_node);
4804 else if (Nkind (gnat_temp) == N_Identifier
4805 || Nkind (gnat_temp) == N_Expanded_Name)
4807 Entity_Id gnat_ex_id = Entity (gnat_temp);
4808 tree gnu_expr;
4810 /* Exception may be a renaming. Recover original exception which is
4811 the one elaborated and registered. */
4812 if (Present (Renamed_Object (gnat_ex_id)))
4813 gnat_ex_id = Renamed_Object (gnat_ex_id);
4815 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4817 this_choice
4818 = build_binary_op
4819 (EQ_EXPR, boolean_type_node,
4820 gnu_except_ptr_stack->last (),
4821 convert (TREE_TYPE (gnu_except_ptr_stack->last ()),
4822 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
4824 else
4825 gcc_unreachable ();
4827 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
4828 gnu_choice, this_choice);
4831 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
4834 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4835 to a GCC tree, which is returned. This is the variant for ZCX. */
4837 static tree
4838 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
4840 tree gnu_etypes_list = NULL_TREE;
4841 tree gnu_current_exc_ptr, prev_gnu_incoming_exc_ptr;
4842 Node_Id gnat_temp;
4844 /* We build a TREE_LIST of nodes representing what exception types this
4845 handler can catch, with special cases for others and all others cases.
4847 Each exception type is actually identified by a pointer to the exception
4848 id, or to a dummy object for "others" and "all others". */
4849 for (gnat_temp = First (Exception_Choices (gnat_node));
4850 gnat_temp; gnat_temp = Next (gnat_temp))
4852 tree gnu_expr, gnu_etype;
4854 if (Nkind (gnat_temp) == N_Others_Choice)
4856 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
4857 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4859 else if (Nkind (gnat_temp) == N_Identifier
4860 || Nkind (gnat_temp) == N_Expanded_Name)
4862 Entity_Id gnat_ex_id = Entity (gnat_temp);
4864 /* Exception may be a renaming. Recover original exception which is
4865 the one elaborated and registered. */
4866 if (Present (Renamed_Object (gnat_ex_id)))
4867 gnat_ex_id = Renamed_Object (gnat_ex_id);
4869 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4870 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4872 else
4873 gcc_unreachable ();
4875 /* The GCC interface expects NULL to be passed for catch all handlers, so
4876 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
4877 is integer_zero_node. It would not work, however, because GCC's
4878 notion of "catch all" is stronger than our notion of "others". Until
4879 we correctly use the cleanup interface as well, doing that would
4880 prevent the "all others" handlers from being seen, because nothing
4881 can be caught beyond a catch all from GCC's point of view. */
4882 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
4885 start_stmt_group ();
4886 gnat_pushlevel ();
4888 /* Expand a call to the begin_handler hook at the beginning of the handler,
4889 and arrange for a call to the end_handler hook to occur on every possible
4890 exit path.
4892 The hooks expect a pointer to the low level occurrence. This is required
4893 for our stack management scheme because a raise inside the handler pushes
4894 a new occurrence on top of the stack, which means that this top does not
4895 necessarily match the occurrence this handler was dealing with.
4897 __builtin_eh_pointer references the exception occurrence being
4898 propagated. Upon handler entry, this is the exception for which the
4899 handler is triggered. This might not be the case upon handler exit,
4900 however, as we might have a new occurrence propagated by the handler's
4901 body, and the end_handler hook called as a cleanup in this context.
4903 We use a local variable to retrieve the incoming value at handler entry
4904 time, and reuse it to feed the end_handler hook's argument at exit. */
4906 gnu_current_exc_ptr
4907 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
4908 1, integer_zero_node);
4909 prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
4910 gnu_incoming_exc_ptr = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
4911 ptr_type_node, gnu_current_exc_ptr,
4912 false, false, false, false,
4913 NULL, gnat_node);
4915 add_stmt_with_node (build_call_n_expr (begin_handler_decl, 1,
4916 gnu_incoming_exc_ptr),
4917 gnat_node);
4919 /* Declare and initialize the choice parameter, if present. */
4920 if (Present (Choice_Parameter (gnat_node)))
4922 tree gnu_param
4923 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, 1);
4925 add_stmt (build_call_n_expr
4926 (set_exception_parameter_decl, 2,
4927 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
4928 gnu_incoming_exc_ptr));
4931 /* We don't have an End_Label at hand to set the location of the cleanup
4932 actions, so we use that of the exception handler itself instead. */
4933 add_cleanup (build_call_n_expr (end_handler_decl, 1, gnu_incoming_exc_ptr),
4934 gnat_node);
4935 add_stmt_list (Statements (gnat_node));
4936 gnat_poplevel ();
4938 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
4940 return
4941 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
4944 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
4946 static void
4947 Compilation_Unit_to_gnu (Node_Id gnat_node)
4949 const Node_Id gnat_unit = Unit (gnat_node);
4950 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
4951 || Nkind (gnat_unit) == N_Subprogram_Body);
4952 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
4953 Entity_Id gnat_entity;
4954 Node_Id gnat_pragma;
4955 /* Make the decl for the elaboration procedure. */
4956 tree gnu_elab_proc_decl
4957 = create_subprog_decl
4958 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
4959 NULL_TREE, void_ftype, NULL_TREE, is_disabled, true, false, true, NULL,
4960 gnat_unit);
4961 struct elab_info *info;
4963 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
4964 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
4966 /* Initialize the information structure for the function. */
4967 allocate_struct_function (gnu_elab_proc_decl, false);
4968 set_cfun (NULL);
4970 current_function_decl = NULL_TREE;
4972 start_stmt_group ();
4973 gnat_pushlevel ();
4975 /* For a body, first process the spec if there is one. */
4976 if (Nkind (gnat_unit) == N_Package_Body
4977 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
4978 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
4980 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
4982 elaborate_all_entities (gnat_node);
4984 if (Nkind (gnat_unit) == N_Subprogram_Declaration
4985 || Nkind (gnat_unit) == N_Generic_Package_Declaration
4986 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
4987 return;
4990 /* Then process any pragmas and declarations preceding the unit. */
4991 for (gnat_pragma = First (Context_Items (gnat_node));
4992 Present (gnat_pragma);
4993 gnat_pragma = Next (gnat_pragma))
4994 if (Nkind (gnat_pragma) == N_Pragma)
4995 add_stmt (gnat_to_gnu (gnat_pragma));
4996 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
4997 true, true);
4999 /* Process the unit itself. */
5000 add_stmt (gnat_to_gnu (gnat_unit));
5002 /* Generate code for all the inlined subprograms. */
5003 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5004 Present (gnat_entity);
5005 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
5007 Node_Id gnat_body;
5009 /* Without optimization, process only the required subprograms. */
5010 if (!optimize && !Has_Pragma_Inline_Always (gnat_entity))
5011 continue;
5013 gnat_body = Parent (Declaration_Node (gnat_entity));
5014 if (Nkind (gnat_body) != N_Subprogram_Body)
5016 /* ??? This happens when only the spec of a package is provided. */
5017 if (No (Corresponding_Body (gnat_body)))
5018 continue;
5020 gnat_body
5021 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
5024 /* Define the entity first so we set DECL_EXTERNAL. */
5025 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5026 add_stmt (gnat_to_gnu (gnat_body));
5029 /* Process any pragmas and actions following the unit. */
5030 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5031 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
5032 finalize_from_limited_with ();
5034 /* Save away what we've made so far and record this potential elaboration
5035 procedure. */
5036 info = ggc_alloc<elab_info> ();
5037 set_current_block_context (gnu_elab_proc_decl);
5038 gnat_poplevel ();
5039 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
5041 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
5043 info->next = elab_info_list;
5044 info->elab_proc = gnu_elab_proc_decl;
5045 info->gnat_node = gnat_node;
5046 elab_info_list = info;
5048 /* Generate elaboration code for this unit, if necessary, and say whether
5049 we did or not. */
5050 gnu_elab_proc_stack->pop ();
5052 /* Invalidate the global renaming pointers. This is necessary because
5053 stabilization of the renamed entities may create SAVE_EXPRs which
5054 have been tied to a specific elaboration routine just above. */
5055 invalidate_global_renaming_pointers ();
5057 /* Force the processing for all nodes that remain in the queue. */
5058 process_deferred_decl_context (true);
5061 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Raise_xxx_Error,
5062 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where
5063 we should place the result type. LABEL_P is true if there is a label to
5064 branch to for the exception. */
5066 static tree
5067 Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5069 const Node_Kind kind = Nkind (gnat_node);
5070 const int reason = UI_To_Int (Reason (gnat_node));
5071 const Node_Id gnat_cond = Condition (gnat_node);
5072 const bool with_extra_info
5073 = Exception_Extra_Info
5074 && !No_Exception_Handlers_Set ()
5075 && !get_exception_label (kind);
5076 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
5078 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5080 switch (reason)
5082 case CE_Access_Check_Failed:
5083 if (with_extra_info)
5084 gnu_result = build_call_raise_column (reason, gnat_node);
5085 break;
5087 case CE_Index_Check_Failed:
5088 case CE_Range_Check_Failed:
5089 case CE_Invalid_Data:
5090 if (Present (gnat_cond) && Nkind (gnat_cond) == N_Op_Not)
5092 Node_Id gnat_range, gnat_index, gnat_type;
5093 tree gnu_index, gnu_low_bound, gnu_high_bound;
5094 struct range_check_info_d *rci;
5096 switch (Nkind (Right_Opnd (gnat_cond)))
5098 case N_In:
5099 gnat_range = Right_Opnd (Right_Opnd (gnat_cond));
5100 gcc_assert (Nkind (gnat_range) == N_Range);
5101 gnu_low_bound = gnat_to_gnu (Low_Bound (gnat_range));
5102 gnu_high_bound = gnat_to_gnu (High_Bound (gnat_range));
5103 break;
5105 case N_Op_Ge:
5106 gnu_low_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5107 gnu_high_bound = NULL_TREE;
5108 break;
5110 case N_Op_Le:
5111 gnu_low_bound = NULL_TREE;
5112 gnu_high_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5113 break;
5115 default:
5116 goto common;
5119 gnat_index = Left_Opnd (Right_Opnd (gnat_cond));
5120 gnat_type = Etype (gnat_index);
5121 gnu_index = gnat_to_gnu (gnat_index);
5123 if (with_extra_info
5124 && gnu_low_bound
5125 && gnu_high_bound
5126 && Known_Esize (gnat_type)
5127 && UI_To_Int (Esize (gnat_type)) <= 32)
5128 gnu_result
5129 = build_call_raise_range (reason, gnat_node, gnu_index,
5130 gnu_low_bound, gnu_high_bound);
5132 /* If loop unswitching is enabled, we try to compute invariant
5133 conditions for checks applied to iteration variables, i.e.
5134 conditions that are both independent of the variable and
5135 necessary in order for the check to fail in the course of
5136 some iteration, and prepend them to the original condition
5137 of the checks. This will make it possible later for the
5138 loop unswitching pass to replace the loop with two loops,
5139 one of which has the checks eliminated and the other has
5140 the original checks reinstated, and a run time selection.
5141 The former loop will be suitable for vectorization. */
5142 if (flag_unswitch_loops
5143 && (!gnu_low_bound
5144 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
5145 && (!gnu_high_bound
5146 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
5147 && (rci = push_range_check_info (gnu_index)))
5149 rci->low_bound = gnu_low_bound;
5150 rci->high_bound = gnu_high_bound;
5151 rci->type = get_unpadded_type (gnat_type);
5152 rci->invariant_cond = build1 (SAVE_EXPR, boolean_type_node,
5153 boolean_true_node);
5154 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5155 boolean_type_node,
5156 rci->invariant_cond,
5157 gnat_to_gnu (gnat_cond));
5160 break;
5162 default:
5163 break;
5166 common:
5167 if (!gnu_result)
5168 gnu_result = build_call_raise (reason, gnat_node, kind);
5169 set_expr_location_from_node (gnu_result, gnat_node);
5171 /* If the type is VOID, this is a statement, so we need to generate the code
5172 for the call. Handle a condition, if there is one. */
5173 if (VOID_TYPE_P (*gnu_result_type_p))
5175 if (Present (gnat_cond))
5177 if (!gnu_cond)
5178 gnu_cond = gnat_to_gnu (gnat_cond);
5179 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
5180 alloc_stmt_list ());
5183 else
5184 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
5186 return gnu_result;
5189 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
5190 parameter of a call. */
5192 static bool
5193 lhs_or_actual_p (Node_Id gnat_node)
5195 Node_Id gnat_parent = Parent (gnat_node);
5196 Node_Kind kind = Nkind (gnat_parent);
5198 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
5199 return true;
5201 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
5202 && Name (gnat_parent) != gnat_node)
5203 return true;
5205 if (kind == N_Parameter_Association)
5206 return true;
5208 return false;
5211 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
5212 of an assignment or an actual parameter of a call. */
5214 static bool
5215 present_in_lhs_or_actual_p (Node_Id gnat_node)
5217 Node_Kind kind;
5219 if (lhs_or_actual_p (gnat_node))
5220 return true;
5222 kind = Nkind (Parent (gnat_node));
5224 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
5225 && lhs_or_actual_p (Parent (gnat_node)))
5226 return true;
5228 return false;
5231 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
5232 as gigi is concerned. This is used to avoid conversions on the LHS. */
5234 static bool
5235 unchecked_conversion_nop (Node_Id gnat_node)
5237 Entity_Id from_type, to_type;
5239 /* The conversion must be on the LHS of an assignment or an actual parameter
5240 of a call. Otherwise, even if the conversion was essentially a no-op, it
5241 could de facto ensure type consistency and this should be preserved. */
5242 if (!lhs_or_actual_p (gnat_node))
5243 return false;
5245 from_type = Etype (Expression (gnat_node));
5247 /* We're interested in artificial conversions generated by the front-end
5248 to make private types explicit, e.g. in Expand_Assign_Array. */
5249 if (!Is_Private_Type (from_type))
5250 return false;
5252 from_type = Underlying_Type (from_type);
5253 to_type = Etype (gnat_node);
5255 /* The direct conversion to the underlying type is a no-op. */
5256 if (to_type == from_type)
5257 return true;
5259 /* For an array subtype, the conversion to the PAIT is a no-op. */
5260 if (Ekind (from_type) == E_Array_Subtype
5261 && to_type == Packed_Array_Impl_Type (from_type))
5262 return true;
5264 /* For a record subtype, the conversion to the type is a no-op. */
5265 if (Ekind (from_type) == E_Record_Subtype
5266 && to_type == Etype (from_type))
5267 return true;
5269 return false;
5272 /* This function is the driver of the GNAT to GCC tree transformation process.
5273 It is the entry point of the tree transformer. GNAT_NODE is the root of
5274 some GNAT tree. Return the root of the corresponding GCC tree. If this
5275 is an expression, return the GCC equivalent of the expression. If this
5276 is a statement, return the statement or add it to the current statement
5277 group, in which case anything returned is to be interpreted as occurring
5278 after anything added. */
5280 tree
5281 gnat_to_gnu (Node_Id gnat_node)
5283 const Node_Kind kind = Nkind (gnat_node);
5284 bool went_into_elab_proc = false;
5285 tree gnu_result = error_mark_node; /* Default to no value. */
5286 tree gnu_result_type = void_type_node;
5287 tree gnu_expr, gnu_lhs, gnu_rhs;
5288 Node_Id gnat_temp;
5290 /* Save node number for error message and set location information. */
5291 error_gnat_node = gnat_node;
5292 Sloc_to_locus (Sloc (gnat_node), &input_location);
5294 /* If this node is a statement and we are only annotating types, return an
5295 empty statement list. */
5296 if (type_annotate_only && IN (kind, N_Statement_Other_Than_Procedure_Call))
5297 return alloc_stmt_list ();
5299 /* If this node is a non-static subexpression and we are only annotating
5300 types, make this into a NULL_EXPR. */
5301 if (type_annotate_only
5302 && IN (kind, N_Subexpr)
5303 && kind != N_Identifier
5304 && !Compile_Time_Known_Value (gnat_node))
5305 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
5306 build_call_raise (CE_Range_Check_Failed, gnat_node,
5307 N_Raise_Constraint_Error));
5309 if ((IN (kind, N_Statement_Other_Than_Procedure_Call)
5310 && kind != N_Null_Statement)
5311 || kind == N_Procedure_Call_Statement
5312 || kind == N_Label
5313 || kind == N_Implicit_Label_Declaration
5314 || kind == N_Handled_Sequence_Of_Statements
5315 || (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void))
5317 tree current_elab_proc = get_elaboration_procedure ();
5319 /* If this is a statement and we are at top level, it must be part of
5320 the elaboration procedure, so mark us as being in that procedure. */
5321 if (!current_function_decl)
5323 current_function_decl = current_elab_proc;
5324 went_into_elab_proc = true;
5327 /* If we are in the elaboration procedure, check if we are violating a
5328 No_Elaboration_Code restriction by having a statement there. Don't
5329 check for a possible No_Elaboration_Code restriction violation on
5330 N_Handled_Sequence_Of_Statements, as we want to signal an error on
5331 every nested real statement instead. This also avoids triggering
5332 spurious errors on dummy (empty) sequences created by the front-end
5333 for package bodies in some cases. */
5334 if (current_function_decl == current_elab_proc
5335 && kind != N_Handled_Sequence_Of_Statements)
5336 Check_Elaboration_Code_Allowed (gnat_node);
5339 switch (kind)
5341 /********************************/
5342 /* Chapter 2: Lexical Elements */
5343 /********************************/
5345 case N_Identifier:
5346 case N_Expanded_Name:
5347 case N_Operator_Symbol:
5348 case N_Defining_Identifier:
5349 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
5351 /* If this is an atomic access on the RHS for which synchronization is
5352 required, build the atomic load. */
5353 if (atomic_sync_required_p (gnat_node)
5354 && !present_in_lhs_or_actual_p (gnat_node))
5355 gnu_result = build_atomic_load (gnu_result);
5356 break;
5358 case N_Integer_Literal:
5360 tree gnu_type;
5362 /* Get the type of the result, looking inside any padding and
5363 justified modular types. Then get the value in that type. */
5364 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
5366 if (TREE_CODE (gnu_type) == RECORD_TYPE
5367 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
5368 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
5370 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
5372 /* If the result overflows (meaning it doesn't fit in its base type),
5373 abort. We would like to check that the value is within the range
5374 of the subtype, but that causes problems with subtypes whose usage
5375 will raise Constraint_Error and with biased representation, so
5376 we don't. */
5377 gcc_assert (!TREE_OVERFLOW (gnu_result));
5379 break;
5381 case N_Character_Literal:
5382 /* If a Entity is present, it means that this was one of the
5383 literals in a user-defined character type. In that case,
5384 just return the value in the CONST_DECL. Otherwise, use the
5385 character code. In that case, the base type should be an
5386 INTEGER_TYPE, but we won't bother checking for that. */
5387 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5388 if (Present (Entity (gnat_node)))
5389 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
5390 else
5391 gnu_result
5392 = build_int_cst_type
5393 (gnu_result_type, UI_To_CC (Char_Literal_Value (gnat_node)));
5394 break;
5396 case N_Real_Literal:
5397 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5399 /* If this is of a fixed-point type, the value we want is the value of
5400 the corresponding integer. */
5401 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
5403 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
5404 gnu_result_type);
5405 gcc_assert (!TREE_OVERFLOW (gnu_result));
5408 else
5410 Ureal ur_realval = Realval (gnat_node);
5412 /* First convert the value to a machine number if it isn't already.
5413 That will force the base to 2 for non-zero values and simplify
5414 the rest of the logic. */
5415 if (!Is_Machine_Number (gnat_node))
5416 ur_realval
5417 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
5418 ur_realval, Round_Even, gnat_node);
5420 if (UR_Is_Zero (ur_realval))
5421 gnu_result = convert (gnu_result_type, integer_zero_node);
5422 else
5424 REAL_VALUE_TYPE tmp;
5426 gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
5428 /* The base must be 2 as Machine guarantees this, so we scale
5429 the value, which we know can fit in the mantissa of the type
5430 (hence the use of that type above). */
5431 gcc_assert (Rbase (ur_realval) == 2);
5432 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
5433 - UI_To_Int (Denominator (ur_realval)));
5434 gnu_result = build_real (gnu_result_type, tmp);
5437 /* Now see if we need to negate the result. Do it this way to
5438 properly handle -0. */
5439 if (UR_Is_Negative (Realval (gnat_node)))
5440 gnu_result
5441 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
5442 gnu_result);
5445 break;
5447 case N_String_Literal:
5448 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5449 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
5451 String_Id gnat_string = Strval (gnat_node);
5452 int length = String_Length (gnat_string);
5453 int i;
5454 char *string;
5455 if (length >= ALLOCA_THRESHOLD)
5456 string = XNEWVEC (char, length + 1);
5457 else
5458 string = (char *) alloca (length + 1);
5460 /* Build the string with the characters in the literal. Note
5461 that Ada strings are 1-origin. */
5462 for (i = 0; i < length; i++)
5463 string[i] = Get_String_Char (gnat_string, i + 1);
5465 /* Put a null at the end of the string in case it's in a context
5466 where GCC will want to treat it as a C string. */
5467 string[i] = 0;
5469 gnu_result = build_string (length, string);
5471 /* Strings in GCC don't normally have types, but we want
5472 this to not be converted to the array type. */
5473 TREE_TYPE (gnu_result) = gnu_result_type;
5475 if (length >= ALLOCA_THRESHOLD)
5476 free (string);
5478 else
5480 /* Build a list consisting of each character, then make
5481 the aggregate. */
5482 String_Id gnat_string = Strval (gnat_node);
5483 int length = String_Length (gnat_string);
5484 int i;
5485 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5486 tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node);
5487 vec<constructor_elt, va_gc> *gnu_vec;
5488 vec_alloc (gnu_vec, length);
5490 for (i = 0; i < length; i++)
5492 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
5493 Get_String_Char (gnat_string, i + 1));
5495 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
5496 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node);
5499 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
5501 break;
5503 case N_Pragma:
5504 gnu_result = Pragma_to_gnu (gnat_node);
5505 break;
5507 /**************************************/
5508 /* Chapter 3: Declarations and Types */
5509 /**************************************/
5511 case N_Subtype_Declaration:
5512 case N_Full_Type_Declaration:
5513 case N_Incomplete_Type_Declaration:
5514 case N_Private_Type_Declaration:
5515 case N_Private_Extension_Declaration:
5516 case N_Task_Type_Declaration:
5517 process_type (Defining_Entity (gnat_node));
5518 gnu_result = alloc_stmt_list ();
5519 break;
5521 case N_Object_Declaration:
5522 case N_Exception_Declaration:
5523 gnat_temp = Defining_Entity (gnat_node);
5524 gnu_result = alloc_stmt_list ();
5526 /* If we are just annotating types and this object has an unconstrained
5527 or task type, don't elaborate it. */
5528 if (type_annotate_only
5529 && (((Is_Array_Type (Etype (gnat_temp))
5530 || Is_Record_Type (Etype (gnat_temp)))
5531 && !Is_Constrained (Etype (gnat_temp)))
5532 || Is_Concurrent_Type (Etype (gnat_temp))))
5533 break;
5535 if (Present (Expression (gnat_node))
5536 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
5537 && (!type_annotate_only
5538 || Compile_Time_Known_Value (Expression (gnat_node))))
5540 gnu_expr = gnat_to_gnu (Expression (gnat_node));
5541 if (Do_Range_Check (Expression (gnat_node)))
5542 gnu_expr
5543 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
5545 /* If this object has its elaboration delayed, we must force
5546 evaluation of GNU_EXPR right now and save it for when the object
5547 is frozen. */
5548 if (Present (Freeze_Node (gnat_temp)))
5550 if (TREE_CONSTANT (gnu_expr))
5552 else if (global_bindings_p ())
5553 gnu_expr
5554 = create_var_decl (create_concat_name (gnat_temp, "init"),
5555 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5556 false, false, false, false,
5557 NULL, gnat_temp);
5558 else
5559 gnu_expr = gnat_save_expr (gnu_expr);
5561 save_gnu_tree (gnat_node, gnu_expr, true);
5564 else
5565 gnu_expr = NULL_TREE;
5567 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
5568 gnu_expr = NULL_TREE;
5570 /* If this is a deferred constant with an address clause, we ignore the
5571 full view since the clause is on the partial view and we cannot have
5572 2 different GCC trees for the object. The only bits of the full view
5573 we will use is the initializer, but it will be directly fetched. */
5574 if (Ekind(gnat_temp) == E_Constant
5575 && Present (Address_Clause (gnat_temp))
5576 && Present (Full_View (gnat_temp)))
5577 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
5579 if (No (Freeze_Node (gnat_temp)))
5580 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
5581 break;
5583 case N_Object_Renaming_Declaration:
5584 gnat_temp = Defining_Entity (gnat_node);
5586 /* Don't do anything if this renaming is handled by the front end or if
5587 we are just annotating types and this object has a composite or task
5588 type, don't elaborate it. We return the result in case it has any
5589 SAVE_EXPRs in it that need to be evaluated here. */
5590 if (!Is_Renaming_Of_Object (gnat_temp)
5591 && ! (type_annotate_only
5592 && (Is_Array_Type (Etype (gnat_temp))
5593 || Is_Record_Type (Etype (gnat_temp))
5594 || Is_Concurrent_Type (Etype (gnat_temp)))))
5595 gnu_result
5596 = gnat_to_gnu_entity (gnat_temp,
5597 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
5598 else
5599 gnu_result = alloc_stmt_list ();
5600 break;
5602 case N_Exception_Renaming_Declaration:
5603 gnat_temp = Defining_Entity (gnat_node);
5604 if (Renamed_Entity (gnat_temp) != Empty)
5605 gnu_result
5606 = gnat_to_gnu_entity (gnat_temp,
5607 gnat_to_gnu (Renamed_Entity (gnat_temp)), 1);
5608 else
5609 gnu_result = alloc_stmt_list ();
5610 break;
5612 case N_Implicit_Label_Declaration:
5613 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
5614 gnu_result = alloc_stmt_list ();
5615 break;
5617 case N_Number_Declaration:
5618 case N_Subprogram_Renaming_Declaration:
5619 case N_Package_Renaming_Declaration:
5620 /* These are fully handled in the front end. */
5621 /* ??? For package renamings, find a way to use GENERIC namespaces so
5622 that we get proper debug information for them. */
5623 gnu_result = alloc_stmt_list ();
5624 break;
5626 /*************************************/
5627 /* Chapter 4: Names and Expressions */
5628 /*************************************/
5630 case N_Explicit_Dereference:
5631 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5632 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5633 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
5635 /* If this is an atomic access on the RHS for which synchronization is
5636 required, build the atomic load. */
5637 if (atomic_sync_required_p (gnat_node)
5638 && !present_in_lhs_or_actual_p (gnat_node))
5639 gnu_result = build_atomic_load (gnu_result);
5640 break;
5642 case N_Indexed_Component:
5644 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
5645 tree gnu_type;
5646 int ndim;
5647 int i;
5648 Node_Id *gnat_expr_array;
5650 gnu_array_object = maybe_implicit_deref (gnu_array_object);
5652 /* Convert vector inputs to their representative array type, to fit
5653 what the code below expects. */
5654 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
5656 if (present_in_lhs_or_actual_p (gnat_node))
5657 gnat_mark_addressable (gnu_array_object);
5658 gnu_array_object = maybe_vector_array (gnu_array_object);
5661 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
5663 /* If we got a padded type, remove it too. */
5664 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
5665 gnu_array_object
5666 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
5667 gnu_array_object);
5669 gnu_result = gnu_array_object;
5671 /* The failure of this assertion will very likely come from a missing
5672 expansion for a packed array access. */
5673 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
5675 /* First compute the number of dimensions of the array, then
5676 fill the expression array, the order depending on whether
5677 this is a Convention_Fortran array or not. */
5678 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
5679 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5680 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
5681 ndim++, gnu_type = TREE_TYPE (gnu_type))
5684 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
5686 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
5687 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
5688 i >= 0;
5689 i--, gnat_temp = Next (gnat_temp))
5690 gnat_expr_array[i] = gnat_temp;
5691 else
5692 for (i = 0, gnat_temp = First (Expressions (gnat_node));
5693 i < ndim;
5694 i++, gnat_temp = Next (gnat_temp))
5695 gnat_expr_array[i] = gnat_temp;
5697 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
5698 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
5700 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
5701 gnat_temp = gnat_expr_array[i];
5702 gnu_expr = gnat_to_gnu (gnat_temp);
5704 if (Do_Range_Check (gnat_temp))
5705 gnu_expr
5706 = emit_index_check
5707 (gnu_array_object, gnu_expr,
5708 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5709 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5710 gnat_temp);
5712 gnu_result
5713 = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr);
5715 /* Array accesses are bound-checked so they cannot trap, but this
5716 is valid only if they are not hoisted ahead of the check. We
5717 need to mark them as no-trap to get decent loop optimizations
5718 in the presence of -fnon-call-exceptions, so we do it when we
5719 know that the original expression had no side-effects. */
5720 if (TREE_CODE (gnu_result) == ARRAY_REF
5721 && !(Nkind (gnat_temp) == N_Identifier
5722 && Ekind (Entity (gnat_temp)) == E_Constant))
5723 TREE_THIS_NOTRAP (gnu_result) = 1;
5726 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5728 /* If this is an atomic access on the RHS for which synchronization is
5729 required, build the atomic load. */
5730 if (atomic_sync_required_p (gnat_node)
5731 && !present_in_lhs_or_actual_p (gnat_node))
5732 gnu_result = build_atomic_load (gnu_result);
5734 break;
5736 case N_Slice:
5738 Node_Id gnat_range_node = Discrete_Range (gnat_node);
5739 tree gnu_type;
5741 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5742 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5744 /* Do any implicit dereferences of the prefix and do any needed
5745 range check. */
5746 gnu_result = maybe_implicit_deref (gnu_result);
5747 gnu_result = maybe_unconstrained_array (gnu_result);
5748 gnu_type = TREE_TYPE (gnu_result);
5749 if (Do_Range_Check (gnat_range_node))
5751 /* Get the bounds of the slice. */
5752 tree gnu_index_type
5753 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
5754 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
5755 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
5756 /* Get the permitted bounds. */
5757 tree gnu_base_index_type
5758 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
5759 tree gnu_base_min_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5760 (TYPE_MIN_VALUE (gnu_base_index_type), gnu_result);
5761 tree gnu_base_max_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5762 (TYPE_MAX_VALUE (gnu_base_index_type), gnu_result);
5763 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
5765 gnu_min_expr = gnat_protect_expr (gnu_min_expr);
5766 gnu_max_expr = gnat_protect_expr (gnu_max_expr);
5768 /* Derive a good type to convert everything to. */
5769 gnu_expr_type = get_base_type (gnu_index_type);
5771 /* Test whether the minimum slice value is too small. */
5772 gnu_expr_l = build_binary_op (LT_EXPR, boolean_type_node,
5773 convert (gnu_expr_type,
5774 gnu_min_expr),
5775 convert (gnu_expr_type,
5776 gnu_base_min_expr));
5778 /* Test whether the maximum slice value is too large. */
5779 gnu_expr_h = build_binary_op (GT_EXPR, boolean_type_node,
5780 convert (gnu_expr_type,
5781 gnu_max_expr),
5782 convert (gnu_expr_type,
5783 gnu_base_max_expr));
5785 /* Build a slice index check that returns the low bound,
5786 assuming the slice is not empty. */
5787 gnu_expr = emit_check
5788 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
5789 gnu_expr_l, gnu_expr_h),
5790 gnu_min_expr, CE_Index_Check_Failed, gnat_node);
5792 /* Build a conditional expression that does the index checks and
5793 returns the low bound if the slice is not empty (max >= min),
5794 and returns the naked low bound otherwise (max < min), unless
5795 it is non-constant and the high bound is; this prevents VRP
5796 from inferring bogus ranges on the unlikely path. */
5797 gnu_expr = fold_build3 (COND_EXPR, gnu_expr_type,
5798 build_binary_op (GE_EXPR, gnu_expr_type,
5799 convert (gnu_expr_type,
5800 gnu_max_expr),
5801 convert (gnu_expr_type,
5802 gnu_min_expr)),
5803 gnu_expr,
5804 TREE_CODE (gnu_min_expr) != INTEGER_CST
5805 && TREE_CODE (gnu_max_expr) == INTEGER_CST
5806 ? gnu_max_expr : gnu_min_expr);
5808 else
5809 /* Simply return the naked low bound. */
5810 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5812 /* If this is a slice with non-constant size of an array with constant
5813 size, set the maximum size for the allocation of temporaries. */
5814 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
5815 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_type)))
5816 TYPE_ARRAY_MAX_SIZE (gnu_result_type) = TYPE_SIZE_UNIT (gnu_type);
5818 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
5819 gnu_result, gnu_expr);
5821 break;
5823 case N_Selected_Component:
5825 Entity_Id gnat_prefix = Prefix (gnat_node);
5826 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
5827 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
5828 tree gnu_field;
5830 gnu_prefix = maybe_implicit_deref (gnu_prefix);
5832 /* For discriminant references in tagged types always substitute the
5833 corresponding discriminant as the actual selected component. */
5834 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix))))
5835 while (Present (Corresponding_Discriminant (gnat_field)))
5836 gnat_field = Corresponding_Discriminant (gnat_field);
5838 /* For discriminant references of untagged types always substitute the
5839 corresponding stored discriminant. */
5840 else if (Present (Corresponding_Discriminant (gnat_field)))
5841 gnat_field = Original_Record_Component (gnat_field);
5843 /* Handle extracting the real or imaginary part of a complex.
5844 The real part is the first field and the imaginary the last. */
5845 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
5846 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
5847 ? REALPART_EXPR : IMAGPART_EXPR,
5848 NULL_TREE, gnu_prefix);
5849 else
5851 gnu_field = gnat_to_gnu_field_decl (gnat_field);
5853 /* If there are discriminants, the prefix might be evaluated more
5854 than once, which is a problem if it has side-effects. */
5855 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
5856 ? Designated_Type (Etype
5857 (Prefix (gnat_node)))
5858 : Etype (Prefix (gnat_node))))
5859 gnu_prefix = gnat_stabilize_reference (gnu_prefix, false, NULL);
5861 gnu_result
5862 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
5863 (Nkind (Parent (gnat_node))
5864 == N_Attribute_Reference)
5865 && lvalue_required_for_attribute_p
5866 (Parent (gnat_node)));
5869 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5871 /* If this is an atomic access on the RHS for which synchronization is
5872 required, build the atomic load. */
5873 if (atomic_sync_required_p (gnat_node)
5874 && !present_in_lhs_or_actual_p (gnat_node))
5875 gnu_result = build_atomic_load (gnu_result);
5877 break;
5879 case N_Attribute_Reference:
5881 /* The attribute designator. */
5882 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
5884 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
5885 is a unit, not an object with a GCC equivalent. */
5886 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
5887 return
5888 create_subprog_decl (create_concat_name
5889 (Entity (Prefix (gnat_node)),
5890 attr == Attr_Elab_Body ? "elabb" : "elabs"),
5891 NULL_TREE, void_ftype, NULL_TREE, is_disabled,
5892 true, true, true, NULL, gnat_node);
5894 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
5896 break;
5898 case N_Reference:
5899 /* Like 'Access as far as we are concerned. */
5900 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5901 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
5902 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5903 break;
5905 case N_Aggregate:
5906 case N_Extension_Aggregate:
5908 tree gnu_aggr_type;
5910 /* ??? It is wrong to evaluate the type now, but there doesn't
5911 seem to be any other practical way of doing it. */
5913 gcc_assert (!Expansion_Delayed (gnat_node));
5915 gnu_aggr_type = gnu_result_type
5916 = get_unpadded_type (Etype (gnat_node));
5918 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
5919 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
5920 gnu_aggr_type
5921 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
5922 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
5923 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
5925 if (Null_Record_Present (gnat_node))
5926 gnu_result = gnat_build_constructor (gnu_aggr_type,
5927 NULL);
5929 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
5930 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
5931 gnu_result
5932 = assoc_to_constructor (Etype (gnat_node),
5933 First (Component_Associations (gnat_node)),
5934 gnu_aggr_type);
5935 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
5936 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
5937 gnu_aggr_type,
5938 Component_Type (Etype (gnat_node)));
5939 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
5940 gnu_result
5941 = build_binary_op
5942 (COMPLEX_EXPR, gnu_aggr_type,
5943 gnat_to_gnu (Expression (First
5944 (Component_Associations (gnat_node)))),
5945 gnat_to_gnu (Expression
5946 (Next
5947 (First (Component_Associations (gnat_node))))));
5948 else
5949 gcc_unreachable ();
5951 gnu_result = convert (gnu_result_type, gnu_result);
5953 break;
5955 case N_Null:
5956 if (TARGET_VTABLE_USES_DESCRIPTORS
5957 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
5958 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
5959 gnu_result = null_fdesc_node;
5960 else
5961 gnu_result = null_pointer_node;
5962 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5963 break;
5965 case N_Type_Conversion:
5966 case N_Qualified_Expression:
5967 /* Get the operand expression. */
5968 gnu_result = gnat_to_gnu (Expression (gnat_node));
5969 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5971 /* If this is a qualified expression for a tagged type, we mark the type
5972 as used. Because of polymorphism, this might be the only reference to
5973 the tagged type in the program while objects have it as dynamic type.
5974 The debugger needs to see it to display these objects properly. */
5975 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
5976 used_types_insert (gnu_result_type);
5978 gnu_result
5979 = convert_with_check (Etype (gnat_node), gnu_result,
5980 Do_Overflow_Check (gnat_node),
5981 Do_Range_Check (Expression (gnat_node)),
5982 kind == N_Type_Conversion
5983 && Float_Truncate (gnat_node), gnat_node);
5984 break;
5986 case N_Unchecked_Type_Conversion:
5987 gnu_result = gnat_to_gnu (Expression (gnat_node));
5989 /* Skip further processing if the conversion is deemed a no-op. */
5990 if (unchecked_conversion_nop (gnat_node))
5992 gnu_result_type = TREE_TYPE (gnu_result);
5993 break;
5996 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5998 /* If the result is a pointer type, see if we are improperly
5999 converting to a stricter alignment. */
6000 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
6001 && IN (Ekind (Etype (gnat_node)), Access_Kind))
6003 unsigned int align = known_alignment (gnu_result);
6004 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6005 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6007 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6008 post_error_ne_tree_2
6009 ("?source alignment (^) '< alignment of & (^)",
6010 gnat_node, Designated_Type (Etype (gnat_node)),
6011 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
6014 /* If we are converting a descriptor to a function pointer, first
6015 build the pointer. */
6016 if (TARGET_VTABLE_USES_DESCRIPTORS
6017 && TREE_TYPE (gnu_result) == fdesc_type_node
6018 && POINTER_TYPE_P (gnu_result_type))
6019 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6021 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
6022 No_Truncation (gnat_node));
6023 break;
6025 case N_In:
6026 case N_Not_In:
6028 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
6029 Node_Id gnat_range = Right_Opnd (gnat_node);
6030 tree gnu_low, gnu_high;
6032 /* GNAT_RANGE is either an N_Range node or an identifier denoting a
6033 subtype. */
6034 if (Nkind (gnat_range) == N_Range)
6036 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
6037 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
6039 else if (Nkind (gnat_range) == N_Identifier
6040 || Nkind (gnat_range) == N_Expanded_Name)
6042 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
6043 tree gnu_range_base_type = get_base_type (gnu_range_type);
6045 gnu_low
6046 = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type));
6047 gnu_high
6048 = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type));
6050 else
6051 gcc_unreachable ();
6053 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6055 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
6056 ensure that GNU_OBJ is evaluated only once and perform a full range
6057 test. */
6058 if (operand_equal_p (gnu_low, gnu_high, 0))
6059 gnu_result
6060 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
6061 else
6063 tree t1, t2;
6064 gnu_obj = gnat_protect_expr (gnu_obj);
6065 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
6066 if (EXPR_P (t1))
6067 set_expr_location_from_node (t1, gnat_node);
6068 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
6069 if (EXPR_P (t2))
6070 set_expr_location_from_node (t2, gnat_node);
6071 gnu_result
6072 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
6075 if (kind == N_Not_In)
6076 gnu_result
6077 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
6079 break;
6081 case N_Op_Divide:
6082 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6083 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6084 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6085 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
6086 ? RDIV_EXPR
6087 : (Rounded_Result (gnat_node)
6088 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
6089 gnu_result_type, gnu_lhs, gnu_rhs);
6090 break;
6092 case N_Op_Or: case N_Op_And: case N_Op_Xor:
6093 /* These can either be operations on booleans or on modular types.
6094 Fall through for boolean types since that's the way GNU_CODES is
6095 set up. */
6096 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6098 enum tree_code code
6099 = (kind == N_Op_Or ? BIT_IOR_EXPR
6100 : kind == N_Op_And ? BIT_AND_EXPR
6101 : BIT_XOR_EXPR);
6103 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6104 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6105 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6106 gnu_result = build_binary_op (code, gnu_result_type,
6107 gnu_lhs, gnu_rhs);
6108 break;
6111 /* ... fall through ... */
6113 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
6114 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
6115 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
6116 case N_Op_Mod: case N_Op_Rem:
6117 case N_Op_Rotate_Left:
6118 case N_Op_Rotate_Right:
6119 case N_Op_Shift_Left:
6120 case N_Op_Shift_Right:
6121 case N_Op_Shift_Right_Arithmetic:
6122 case N_And_Then: case N_Or_Else:
6124 enum tree_code code = gnu_codes[kind];
6125 bool ignore_lhs_overflow = false;
6126 location_t saved_location = input_location;
6127 tree gnu_type;
6129 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6130 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6131 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6133 /* Pending generic support for efficient vector logical operations in
6134 GCC, convert vectors to their representative array type view and
6135 fallthrough. */
6136 gnu_lhs = maybe_vector_array (gnu_lhs);
6137 gnu_rhs = maybe_vector_array (gnu_rhs);
6139 /* If this is a comparison operator, convert any references to an
6140 unconstrained array value into a reference to the actual array. */
6141 if (TREE_CODE_CLASS (code) == tcc_comparison)
6143 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
6144 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
6147 /* If this is a shift whose count is not guaranteed to be correct,
6148 we need to adjust the shift count. */
6149 if (IN (kind, N_Op_Shift) && !Shift_Count_OK (gnat_node))
6151 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
6152 tree gnu_max_shift
6153 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
6155 if (kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
6156 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
6157 gnu_rhs, gnu_max_shift);
6158 else if (kind == N_Op_Shift_Right_Arithmetic)
6159 gnu_rhs
6160 = build_binary_op
6161 (MIN_EXPR, gnu_count_type,
6162 build_binary_op (MINUS_EXPR,
6163 gnu_count_type,
6164 gnu_max_shift,
6165 convert (gnu_count_type,
6166 integer_one_node)),
6167 gnu_rhs);
6170 /* For right shifts, the type says what kind of shift to do,
6171 so we may need to choose a different type. In this case,
6172 we have to ignore integer overflow lest it propagates all
6173 the way down and causes a CE to be explicitly raised. */
6174 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
6176 gnu_type = gnat_unsigned_type (gnu_type);
6177 ignore_lhs_overflow = true;
6179 else if (kind == N_Op_Shift_Right_Arithmetic
6180 && TYPE_UNSIGNED (gnu_type))
6182 gnu_type = gnat_signed_type (gnu_type);
6183 ignore_lhs_overflow = true;
6186 if (gnu_type != gnu_result_type)
6188 tree gnu_old_lhs = gnu_lhs;
6189 gnu_lhs = convert (gnu_type, gnu_lhs);
6190 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
6191 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
6192 gnu_rhs = convert (gnu_type, gnu_rhs);
6195 /* Instead of expanding overflow checks for addition, subtraction
6196 and multiplication itself, the front end will leave this to
6197 the back end when Backend_Overflow_Checks_On_Target is set.
6198 As the GCC back end itself does not know yet how to properly
6199 do overflow checking, do it here. The goal is to push
6200 the expansions further into the back end over time. */
6201 if (Do_Overflow_Check (gnat_node) && Backend_Overflow_Checks_On_Target
6202 && (kind == N_Op_Add
6203 || kind == N_Op_Subtract
6204 || kind == N_Op_Multiply)
6205 && !TYPE_UNSIGNED (gnu_type)
6206 && !FLOAT_TYPE_P (gnu_type))
6207 gnu_result = build_binary_op_trapv (code, gnu_type,
6208 gnu_lhs, gnu_rhs, gnat_node);
6209 else
6211 /* Some operations, e.g. comparisons of arrays, generate complex
6212 trees that need to be annotated while they are being built. */
6213 input_location = saved_location;
6214 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
6217 /* If this is a logical shift with the shift count not verified,
6218 we must return zero if it is too large. We cannot compensate
6219 above in this case. */
6220 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
6221 && !Shift_Count_OK (gnat_node))
6222 gnu_result
6223 = build_cond_expr
6224 (gnu_type,
6225 build_binary_op (GE_EXPR, boolean_type_node,
6226 gnu_rhs,
6227 convert (TREE_TYPE (gnu_rhs),
6228 TYPE_SIZE (gnu_type))),
6229 convert (gnu_type, integer_zero_node),
6230 gnu_result);
6232 break;
6234 case N_If_Expression:
6236 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
6237 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
6238 tree gnu_false
6239 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
6241 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6242 gnu_result
6243 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
6245 break;
6247 case N_Op_Plus:
6248 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
6249 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6250 break;
6252 case N_Op_Not:
6253 /* This case can apply to a boolean or a modular type.
6254 Fall through for a boolean operand since GNU_CODES is set
6255 up to handle this. */
6256 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6258 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6259 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6260 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
6261 gnu_expr);
6262 break;
6265 /* ... fall through ... */
6267 case N_Op_Minus: case N_Op_Abs:
6268 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6269 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6271 if (Do_Overflow_Check (gnat_node)
6272 && !TYPE_UNSIGNED (gnu_result_type)
6273 && !FLOAT_TYPE_P (gnu_result_type))
6274 gnu_result
6275 = build_unary_op_trapv (gnu_codes[kind],
6276 gnu_result_type, gnu_expr, gnat_node);
6277 else
6278 gnu_result = build_unary_op (gnu_codes[kind],
6279 gnu_result_type, gnu_expr);
6280 break;
6282 case N_Allocator:
6284 tree gnu_init = 0;
6285 tree gnu_type;
6286 bool ignore_init_type = false;
6288 gnat_temp = Expression (gnat_node);
6290 /* The Expression operand can either be an N_Identifier or
6291 Expanded_Name, which must represent a type, or a
6292 N_Qualified_Expression, which contains both the object type and an
6293 initial value for the object. */
6294 if (Nkind (gnat_temp) == N_Identifier
6295 || Nkind (gnat_temp) == N_Expanded_Name)
6296 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
6297 else if (Nkind (gnat_temp) == N_Qualified_Expression)
6299 Entity_Id gnat_desig_type
6300 = Designated_Type (Underlying_Type (Etype (gnat_node)));
6302 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
6303 gnu_init = gnat_to_gnu (Expression (gnat_temp));
6305 gnu_init = maybe_unconstrained_array (gnu_init);
6306 if (Do_Range_Check (Expression (gnat_temp)))
6307 gnu_init
6308 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
6310 if (Is_Elementary_Type (gnat_desig_type)
6311 || Is_Constrained (gnat_desig_type))
6312 gnu_type = gnat_to_gnu_type (gnat_desig_type);
6313 else
6315 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
6316 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
6317 gnu_type = TREE_TYPE (gnu_init);
6320 /* See the N_Qualified_Expression case for the rationale. */
6321 if (Is_Tagged_Type (gnat_desig_type))
6322 used_types_insert (gnu_type);
6324 gnu_init = convert (gnu_type, gnu_init);
6326 else
6327 gcc_unreachable ();
6329 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6330 return build_allocator (gnu_type, gnu_init, gnu_result_type,
6331 Procedure_To_Call (gnat_node),
6332 Storage_Pool (gnat_node), gnat_node,
6333 ignore_init_type);
6335 break;
6337 /**************************/
6338 /* Chapter 5: Statements */
6339 /**************************/
6341 case N_Label:
6342 gnu_result = build1 (LABEL_EXPR, void_type_node,
6343 gnat_to_gnu (Identifier (gnat_node)));
6344 break;
6346 case N_Null_Statement:
6347 /* When not optimizing, turn null statements from source into gotos to
6348 the next statement that the middle-end knows how to preserve. */
6349 if (!optimize && Comes_From_Source (gnat_node))
6351 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
6352 DECL_IGNORED_P (label) = 1;
6353 start_stmt_group ();
6354 stmt = build1 (GOTO_EXPR, void_type_node, label);
6355 set_expr_location_from_node (stmt, gnat_node);
6356 add_stmt (stmt);
6357 stmt = build1 (LABEL_EXPR, void_type_node, label);
6358 set_expr_location_from_node (stmt, gnat_node);
6359 add_stmt (stmt);
6360 gnu_result = end_stmt_group ();
6362 else
6363 gnu_result = alloc_stmt_list ();
6364 break;
6366 case N_Assignment_Statement:
6367 /* Get the LHS and RHS of the statement and convert any reference to an
6368 unconstrained array into a reference to the underlying array. */
6369 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
6371 /* If the type has a size that overflows, convert this into raise of
6372 Storage_Error: execution shouldn't have gotten here anyway. */
6373 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
6374 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
6375 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
6376 N_Raise_Storage_Error);
6377 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
6378 gnu_result
6379 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
6380 atomic_sync_required_p (Name (gnat_node)));
6381 else
6383 const Node_Id gnat_expr = Expression (gnat_node);
6384 const Entity_Id gnat_type
6385 = Underlying_Type (Etype (Name (gnat_node)));
6386 const bool regular_array_type_p
6387 = (Is_Array_Type (gnat_type) && !Is_Bit_Packed_Array (gnat_type));
6388 const bool use_memset_p
6389 = (regular_array_type_p
6390 && Nkind (gnat_expr) == N_Aggregate
6391 && Is_Others_Aggregate (gnat_expr));
6393 /* If we'll use memset, we need to find the inner expression. */
6394 if (use_memset_p)
6396 Node_Id gnat_inner
6397 = Expression (First (Component_Associations (gnat_expr)));
6398 while (Nkind (gnat_inner) == N_Aggregate
6399 && Is_Others_Aggregate (gnat_inner))
6400 gnat_inner
6401 = Expression (First (Component_Associations (gnat_inner)));
6402 gnu_rhs = gnat_to_gnu (gnat_inner);
6404 else
6405 gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr));
6407 /* If range check is needed, emit code to generate it. */
6408 if (Do_Range_Check (gnat_expr))
6409 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
6410 gnat_node);
6412 /* If atomic synchronization is required, build an atomic store. */
6413 if (atomic_sync_required_p (Name (gnat_node)))
6414 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs);
6416 /* Or else, use memset when the conditions are met. */
6417 else if (use_memset_p)
6419 tree value = fold_convert (integer_type_node, gnu_rhs);
6420 tree to = gnu_lhs;
6421 tree type = TREE_TYPE (to);
6422 tree size
6423 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to);
6424 tree to_ptr = build_fold_addr_expr (to);
6425 tree t = builtin_decl_implicit (BUILT_IN_MEMSET);
6426 if (TREE_CODE (value) == INTEGER_CST)
6428 tree mask
6429 = build_int_cst (integer_type_node,
6430 ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
6431 value = int_const_binop (BIT_AND_EXPR, value, mask);
6433 gnu_result = build_call_expr (t, 3, to_ptr, value, size);
6436 /* Otherwise build a regular assignment. */
6437 else
6438 gnu_result
6439 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
6441 /* If the assignment type is a regular array and the two sides are
6442 not completely disjoint, play safe and use memmove. But don't do
6443 it for a bit-packed array as it might not be byte-aligned. */
6444 if (TREE_CODE (gnu_result) == MODIFY_EXPR
6445 && regular_array_type_p
6446 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
6448 tree to = TREE_OPERAND (gnu_result, 0);
6449 tree from = TREE_OPERAND (gnu_result, 1);
6450 tree type = TREE_TYPE (from);
6451 tree size
6452 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from);
6453 tree to_ptr = build_fold_addr_expr (to);
6454 tree from_ptr = build_fold_addr_expr (from);
6455 tree t = builtin_decl_implicit (BUILT_IN_MEMMOVE);
6456 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
6459 break;
6461 case N_If_Statement:
6463 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
6465 /* Make the outer COND_EXPR. Avoid non-determinism. */
6466 gnu_result = build3 (COND_EXPR, void_type_node,
6467 gnat_to_gnu (Condition (gnat_node)),
6468 NULL_TREE, NULL_TREE);
6469 COND_EXPR_THEN (gnu_result)
6470 = build_stmt_group (Then_Statements (gnat_node), false);
6471 TREE_SIDE_EFFECTS (gnu_result) = 1;
6472 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
6474 /* Now make a COND_EXPR for each of the "else if" parts. Put each
6475 into the previous "else" part and point to where to put any
6476 outer "else". Also avoid non-determinism. */
6477 if (Present (Elsif_Parts (gnat_node)))
6478 for (gnat_temp = First (Elsif_Parts (gnat_node));
6479 Present (gnat_temp); gnat_temp = Next (gnat_temp))
6481 gnu_expr = build3 (COND_EXPR, void_type_node,
6482 gnat_to_gnu (Condition (gnat_temp)),
6483 NULL_TREE, NULL_TREE);
6484 COND_EXPR_THEN (gnu_expr)
6485 = build_stmt_group (Then_Statements (gnat_temp), false);
6486 TREE_SIDE_EFFECTS (gnu_expr) = 1;
6487 set_expr_location_from_node (gnu_expr, gnat_temp);
6488 *gnu_else_ptr = gnu_expr;
6489 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
6492 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
6494 break;
6496 case N_Case_Statement:
6497 gnu_result = Case_Statement_to_gnu (gnat_node);
6498 break;
6500 case N_Loop_Statement:
6501 gnu_result = Loop_Statement_to_gnu (gnat_node);
6502 break;
6504 case N_Block_Statement:
6505 /* The only way to enter the block is to fall through to it. */
6506 if (stmt_group_may_fallthru ())
6508 start_stmt_group ();
6509 gnat_pushlevel ();
6510 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6511 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6512 gnat_poplevel ();
6513 gnu_result = end_stmt_group ();
6515 else
6516 gnu_result = alloc_stmt_list ();
6517 break;
6519 case N_Exit_Statement:
6520 gnu_result
6521 = build2 (EXIT_STMT, void_type_node,
6522 (Present (Condition (gnat_node))
6523 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
6524 (Present (Name (gnat_node))
6525 ? get_gnu_tree (Entity (Name (gnat_node)))
6526 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
6527 break;
6529 case N_Simple_Return_Statement:
6531 tree gnu_ret_obj, gnu_ret_val;
6533 /* If the subprogram is a function, we must return the expression. */
6534 if (Present (Expression (gnat_node)))
6536 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
6538 /* If this function has copy-in/copy-out parameters, get the real
6539 object for the return. See Subprogram_to_gnu. */
6540 if (TYPE_CI_CO_LIST (gnu_subprog_type))
6541 gnu_ret_obj = gnu_return_var_stack->last ();
6542 else
6543 gnu_ret_obj = DECL_RESULT (current_function_decl);
6545 /* Get the GCC tree for the expression to be returned. */
6546 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
6548 /* Do not remove the padding from GNU_RET_VAL if the inner type is
6549 self-referential since we want to allocate the fixed size. */
6550 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
6551 && TYPE_IS_PADDING_P
6552 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))
6553 && CONTAINS_PLACEHOLDER_P
6554 (TYPE_SIZE (TREE_TYPE (gnu_ret_val))))
6555 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
6557 /* If the function returns by direct reference, return a pointer
6558 to the return value. */
6559 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)
6560 || By_Ref (gnat_node))
6561 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
6563 /* Otherwise, if it returns an unconstrained array, we have to
6564 allocate a new version of the result and return it. */
6565 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type))
6567 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
6569 /* And find out whether this is a candidate for Named Return
6570 Value. If so, record it. */
6571 if (!TYPE_CI_CO_LIST (gnu_subprog_type) && optimize)
6573 tree ret_val = gnu_ret_val;
6575 /* Strip useless conversions around the return value. */
6576 if (gnat_useless_type_conversion (ret_val))
6577 ret_val = TREE_OPERAND (ret_val, 0);
6579 /* Strip unpadding around the return value. */
6580 if (TREE_CODE (ret_val) == COMPONENT_REF
6581 && TYPE_IS_PADDING_P
6582 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
6583 ret_val = TREE_OPERAND (ret_val, 0);
6585 /* Now apply the test to the return value. */
6586 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
6588 if (!f_named_ret_val)
6589 f_named_ret_val = BITMAP_GGC_ALLOC ();
6590 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
6591 if (!f_gnat_ret)
6592 f_gnat_ret = gnat_node;
6596 gnu_ret_val = build_allocator (TREE_TYPE (gnu_ret_val),
6597 gnu_ret_val,
6598 TREE_TYPE (gnu_ret_obj),
6599 Procedure_To_Call (gnat_node),
6600 Storage_Pool (gnat_node),
6601 gnat_node, false);
6604 /* Otherwise, if it returns by invisible reference, dereference
6605 the pointer it is passed using the type of the return value
6606 and build the copy operation manually. This ensures that we
6607 don't copy too much data, for example if the return type is
6608 unconstrained with a maximum size. */
6609 else if (TREE_ADDRESSABLE (gnu_subprog_type))
6611 tree gnu_ret_deref
6612 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
6613 gnu_ret_obj);
6614 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
6615 gnu_ret_deref, gnu_ret_val);
6616 add_stmt_with_node (gnu_result, gnat_node);
6617 gnu_ret_val = NULL_TREE;
6621 else
6622 gnu_ret_obj = gnu_ret_val = NULL_TREE;
6624 /* If we have a return label defined, convert this into a branch to
6625 that label. The return proper will be handled elsewhere. */
6626 if (gnu_return_label_stack->last ())
6628 if (gnu_ret_obj)
6629 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_ret_obj,
6630 gnu_ret_val));
6632 gnu_result = build1 (GOTO_EXPR, void_type_node,
6633 gnu_return_label_stack->last ());
6635 /* When not optimizing, make sure the return is preserved. */
6636 if (!optimize && Comes_From_Source (gnat_node))
6637 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
6640 /* Otherwise, build a regular return. */
6641 else
6642 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
6644 break;
6646 case N_Goto_Statement:
6647 gnu_result
6648 = build1 (GOTO_EXPR, void_type_node, gnat_to_gnu (Name (gnat_node)));
6649 break;
6651 /***************************/
6652 /* Chapter 6: Subprograms */
6653 /***************************/
6655 case N_Subprogram_Declaration:
6656 /* Unless there is a freeze node, declare the subprogram. We consider
6657 this a "definition" even though we're not generating code for
6658 the subprogram because we will be making the corresponding GCC
6659 node here. */
6661 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6662 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
6663 NULL_TREE, 1);
6664 gnu_result = alloc_stmt_list ();
6665 break;
6667 case N_Abstract_Subprogram_Declaration:
6668 /* This subprogram doesn't exist for code generation purposes, but we
6669 have to elaborate the types of any parameters and result, unless
6670 they are imported types (nothing to generate in this case).
6672 The parameter list may contain types with freeze nodes, e.g. not null
6673 subtypes, so the subprogram itself may carry a freeze node, in which
6674 case its elaboration must be deferred. */
6676 /* Process the parameter types first. */
6677 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6678 for (gnat_temp
6679 = First_Formal_With_Extras
6680 (Defining_Entity (Specification (gnat_node)));
6681 Present (gnat_temp);
6682 gnat_temp = Next_Formal_With_Extras (gnat_temp))
6683 if (Is_Itype (Etype (gnat_temp))
6684 && !From_Limited_With (Etype (gnat_temp)))
6685 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
6687 /* Then the result type, set to Standard_Void_Type for procedures. */
6689 Entity_Id gnat_temp_type
6690 = Etype (Defining_Entity (Specification (gnat_node)));
6692 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
6693 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, 0);
6696 gnu_result = alloc_stmt_list ();
6697 break;
6699 case N_Defining_Program_Unit_Name:
6700 /* For a child unit identifier go up a level to get the specification.
6701 We get this when we try to find the spec of a child unit package
6702 that is the compilation unit being compiled. */
6703 gnu_result = gnat_to_gnu (Parent (gnat_node));
6704 break;
6706 case N_Subprogram_Body:
6707 Subprogram_Body_to_gnu (gnat_node);
6708 gnu_result = alloc_stmt_list ();
6709 break;
6711 case N_Function_Call:
6712 case N_Procedure_Call_Statement:
6713 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE, false);
6714 break;
6716 /************************/
6717 /* Chapter 7: Packages */
6718 /************************/
6720 case N_Package_Declaration:
6721 gnu_result = gnat_to_gnu (Specification (gnat_node));
6722 break;
6724 case N_Package_Specification:
6726 start_stmt_group ();
6727 process_decls (Visible_Declarations (gnat_node),
6728 Private_Declarations (gnat_node), Empty, true, true);
6729 gnu_result = end_stmt_group ();
6730 break;
6732 case N_Package_Body:
6734 /* If this is the body of a generic package - do nothing. */
6735 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
6737 gnu_result = alloc_stmt_list ();
6738 break;
6741 start_stmt_group ();
6742 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6744 if (Present (Handled_Statement_Sequence (gnat_node)))
6745 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6747 gnu_result = end_stmt_group ();
6748 break;
6750 /********************************/
6751 /* Chapter 8: Visibility Rules */
6752 /********************************/
6754 case N_Use_Package_Clause:
6755 case N_Use_Type_Clause:
6756 /* Nothing to do here - but these may appear in list of declarations. */
6757 gnu_result = alloc_stmt_list ();
6758 break;
6760 /*********************/
6761 /* Chapter 9: Tasks */
6762 /*********************/
6764 case N_Protected_Type_Declaration:
6765 gnu_result = alloc_stmt_list ();
6766 break;
6768 case N_Single_Task_Declaration:
6769 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
6770 gnu_result = alloc_stmt_list ();
6771 break;
6773 /*********************************************************/
6774 /* Chapter 10: Program Structure and Compilation Issues */
6775 /*********************************************************/
6777 case N_Compilation_Unit:
6778 /* This is not called for the main unit on which gigi is invoked. */
6779 Compilation_Unit_to_gnu (gnat_node);
6780 gnu_result = alloc_stmt_list ();
6781 break;
6783 case N_Subprogram_Body_Stub:
6784 case N_Package_Body_Stub:
6785 case N_Protected_Body_Stub:
6786 case N_Task_Body_Stub:
6787 /* Simply process whatever unit is being inserted. */
6788 if (Present (Library_Unit (gnat_node)))
6789 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
6790 else
6792 gcc_assert (type_annotate_only);
6793 gnu_result = alloc_stmt_list ();
6795 break;
6797 case N_Subunit:
6798 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
6799 break;
6801 /***************************/
6802 /* Chapter 11: Exceptions */
6803 /***************************/
6805 case N_Handled_Sequence_Of_Statements:
6806 /* If there is an At_End procedure attached to this node, and the EH
6807 mechanism is SJLJ, we must have at least a corresponding At_End
6808 handler, unless the No_Exception_Handlers restriction is set. */
6809 gcc_assert (type_annotate_only
6810 || Exception_Mechanism != Setjmp_Longjmp
6811 || No (At_End_Proc (gnat_node))
6812 || Present (Exception_Handlers (gnat_node))
6813 || No_Exception_Handlers_Set ());
6815 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
6816 break;
6818 case N_Exception_Handler:
6819 if (Exception_Mechanism == Setjmp_Longjmp)
6820 gnu_result = Exception_Handler_to_gnu_sjlj (gnat_node);
6821 else if (Exception_Mechanism == Back_End_Exceptions)
6822 gnu_result = Exception_Handler_to_gnu_zcx (gnat_node);
6823 else
6824 gcc_unreachable ();
6825 break;
6827 case N_Raise_Statement:
6828 /* Only for reraise in back-end exceptions mode. */
6829 gcc_assert (No (Name (gnat_node))
6830 && Exception_Mechanism == Back_End_Exceptions);
6832 start_stmt_group ();
6833 gnat_pushlevel ();
6835 /* Clear the current exception pointer so that the occurrence won't be
6836 deallocated. */
6837 gnu_expr = create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE,
6838 ptr_type_node, gnu_incoming_exc_ptr,
6839 false, false, false, false, NULL, gnat_node);
6841 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_incoming_exc_ptr,
6842 convert (ptr_type_node, integer_zero_node)));
6843 add_stmt (build_call_n_expr (reraise_zcx_decl, 1, gnu_expr));
6844 gnat_poplevel ();
6845 gnu_result = end_stmt_group ();
6846 break;
6848 case N_Push_Constraint_Error_Label:
6849 push_exception_label_stack (&gnu_constraint_error_label_stack,
6850 Exception_Label (gnat_node));
6851 break;
6853 case N_Push_Storage_Error_Label:
6854 push_exception_label_stack (&gnu_storage_error_label_stack,
6855 Exception_Label (gnat_node));
6856 break;
6858 case N_Push_Program_Error_Label:
6859 push_exception_label_stack (&gnu_program_error_label_stack,
6860 Exception_Label (gnat_node));
6861 break;
6863 case N_Pop_Constraint_Error_Label:
6864 gnu_constraint_error_label_stack->pop ();
6865 break;
6867 case N_Pop_Storage_Error_Label:
6868 gnu_storage_error_label_stack->pop ();
6869 break;
6871 case N_Pop_Program_Error_Label:
6872 gnu_program_error_label_stack->pop ();
6873 break;
6875 /******************************/
6876 /* Chapter 12: Generic Units */
6877 /******************************/
6879 case N_Generic_Function_Renaming_Declaration:
6880 case N_Generic_Package_Renaming_Declaration:
6881 case N_Generic_Procedure_Renaming_Declaration:
6882 case N_Generic_Package_Declaration:
6883 case N_Generic_Subprogram_Declaration:
6884 case N_Package_Instantiation:
6885 case N_Procedure_Instantiation:
6886 case N_Function_Instantiation:
6887 /* These nodes can appear on a declaration list but there is nothing to
6888 to be done with them. */
6889 gnu_result = alloc_stmt_list ();
6890 break;
6892 /**************************************************/
6893 /* Chapter 13: Representation Clauses and */
6894 /* Implementation-Dependent Features */
6895 /**************************************************/
6897 case N_Attribute_Definition_Clause:
6898 gnu_result = alloc_stmt_list ();
6900 /* The only one we need to deal with is 'Address since, for the others,
6901 the front-end puts the information elsewhere. */
6902 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
6903 break;
6905 /* And we only deal with 'Address if the object has a Freeze node. */
6906 gnat_temp = Entity (Name (gnat_node));
6907 if (No (Freeze_Node (gnat_temp)))
6908 break;
6910 /* Get the value to use as the address and save it as the equivalent
6911 for the object. When it is frozen, gnat_to_gnu_entity will do the
6912 right thing. */
6913 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
6914 break;
6916 case N_Enumeration_Representation_Clause:
6917 case N_Record_Representation_Clause:
6918 case N_At_Clause:
6919 /* We do nothing with these. SEM puts the information elsewhere. */
6920 gnu_result = alloc_stmt_list ();
6921 break;
6923 case N_Code_Statement:
6924 if (!type_annotate_only)
6926 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
6927 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
6928 tree gnu_clobbers = NULL_TREE, tail;
6929 bool allows_mem, allows_reg, fake;
6930 int ninputs, noutputs, i;
6931 const char **oconstraints;
6932 const char *constraint;
6933 char *clobber;
6935 /* First retrieve the 3 operand lists built by the front-end. */
6936 Setup_Asm_Outputs (gnat_node);
6937 while (Present (gnat_temp = Asm_Output_Variable ()))
6939 tree gnu_value = gnat_to_gnu (gnat_temp);
6940 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6941 (Asm_Output_Constraint ()));
6943 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
6944 Next_Asm_Output ();
6947 Setup_Asm_Inputs (gnat_node);
6948 while (Present (gnat_temp = Asm_Input_Value ()))
6950 tree gnu_value = gnat_to_gnu (gnat_temp);
6951 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6952 (Asm_Input_Constraint ()));
6954 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
6955 Next_Asm_Input ();
6958 Clobber_Setup (gnat_node);
6959 while ((clobber = Clobber_Get_Next ()))
6960 gnu_clobbers
6961 = tree_cons (NULL_TREE,
6962 build_string (strlen (clobber) + 1, clobber),
6963 gnu_clobbers);
6965 /* Then perform some standard checking and processing on the
6966 operands. In particular, mark them addressable if needed. */
6967 gnu_outputs = nreverse (gnu_outputs);
6968 noutputs = list_length (gnu_outputs);
6969 gnu_inputs = nreverse (gnu_inputs);
6970 ninputs = list_length (gnu_inputs);
6971 oconstraints = XALLOCAVEC (const char *, noutputs);
6973 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
6975 tree output = TREE_VALUE (tail);
6976 constraint
6977 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
6978 oconstraints[i] = constraint;
6980 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
6981 &allows_mem, &allows_reg, &fake))
6983 /* If the operand is going to end up in memory,
6984 mark it addressable. Note that we don't test
6985 allows_mem like in the input case below; this
6986 is modelled on the C front-end. */
6987 if (!allows_reg)
6989 output = remove_conversions (output, false);
6990 if (TREE_CODE (output) == CONST_DECL
6991 && DECL_CONST_CORRESPONDING_VAR (output))
6992 output = DECL_CONST_CORRESPONDING_VAR (output);
6993 if (!gnat_mark_addressable (output))
6994 output = error_mark_node;
6997 else
6998 output = error_mark_node;
7000 TREE_VALUE (tail) = output;
7003 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
7005 tree input = TREE_VALUE (tail);
7006 constraint
7007 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7009 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
7010 0, oconstraints,
7011 &allows_mem, &allows_reg))
7013 /* If the operand is going to end up in memory,
7014 mark it addressable. */
7015 if (!allows_reg && allows_mem)
7017 input = remove_conversions (input, false);
7018 if (TREE_CODE (input) == CONST_DECL
7019 && DECL_CONST_CORRESPONDING_VAR (input))
7020 input = DECL_CONST_CORRESPONDING_VAR (input);
7021 if (!gnat_mark_addressable (input))
7022 input = error_mark_node;
7025 else
7026 input = error_mark_node;
7028 TREE_VALUE (tail) = input;
7031 gnu_result = build5 (ASM_EXPR, void_type_node,
7032 gnu_template, gnu_outputs,
7033 gnu_inputs, gnu_clobbers, NULL_TREE);
7034 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
7036 else
7037 gnu_result = alloc_stmt_list ();
7039 break;
7041 /****************/
7042 /* Added Nodes */
7043 /****************/
7045 case N_Expression_With_Actions:
7046 /* This construct doesn't define a scope so we don't push a binding level
7047 around the statement list; but we wrap it in a SAVE_EXPR to protect it
7048 from unsharing. */
7049 gnu_result = build_stmt_group (Actions (gnat_node), false);
7050 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
7051 TREE_SIDE_EFFECTS (gnu_result) = 1;
7052 gnu_expr = gnat_to_gnu (Expression (gnat_node));
7053 gnu_result
7054 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
7055 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7056 break;
7058 case N_Freeze_Entity:
7059 start_stmt_group ();
7060 process_freeze_entity (gnat_node);
7061 process_decls (Actions (gnat_node), Empty, Empty, true, true);
7062 gnu_result = end_stmt_group ();
7063 break;
7065 case N_Freeze_Generic_Entity:
7066 gnu_result = alloc_stmt_list ();
7067 break;
7069 case N_Itype_Reference:
7070 if (!present_gnu_tree (Itype (gnat_node)))
7071 process_type (Itype (gnat_node));
7073 gnu_result = alloc_stmt_list ();
7074 break;
7076 case N_Free_Statement:
7077 if (!type_annotate_only)
7079 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
7080 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
7081 tree gnu_obj_type, gnu_actual_obj_type;
7083 /* If this is a thin pointer, we must first dereference it to create
7084 a fat pointer, then go back below to a thin pointer. The reason
7085 for this is that we need to have a fat pointer someplace in order
7086 to properly compute the size. */
7087 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7088 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
7089 build_unary_op (INDIRECT_REF, NULL_TREE,
7090 gnu_ptr));
7092 /* If this is a fat pointer, the object must have been allocated with
7093 the template in front of the array. So pass the template address,
7094 and get the total size; do it by converting to a thin pointer. */
7095 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
7096 gnu_ptr
7097 = convert (build_pointer_type
7098 (TYPE_OBJECT_RECORD_TYPE
7099 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
7100 gnu_ptr);
7102 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
7104 /* If this is a thin pointer, the object must have been allocated with
7105 the template in front of the array. So pass the template address,
7106 and get the total size. */
7107 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7108 gnu_ptr
7109 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
7110 gnu_ptr,
7111 fold_build1 (NEGATE_EXPR, sizetype,
7112 byte_position
7113 (DECL_CHAIN
7114 TYPE_FIELDS ((gnu_obj_type)))));
7116 /* If we have a special dynamic constrained subtype on the node, use
7117 it to compute the size; otherwise, use the designated subtype. */
7118 if (Present (Actual_Designated_Subtype (gnat_node)))
7120 gnu_actual_obj_type
7121 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
7123 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
7124 gnu_actual_obj_type
7125 = build_unc_object_type_from_ptr (gnu_ptr_type,
7126 gnu_actual_obj_type,
7127 get_identifier ("DEALLOC"),
7128 false);
7130 else
7131 gnu_actual_obj_type = gnu_obj_type;
7133 gnu_result
7134 = build_call_alloc_dealloc (gnu_ptr,
7135 TYPE_SIZE_UNIT (gnu_actual_obj_type),
7136 gnu_obj_type,
7137 Procedure_To_Call (gnat_node),
7138 Storage_Pool (gnat_node),
7139 gnat_node);
7141 break;
7143 case N_Raise_Constraint_Error:
7144 case N_Raise_Program_Error:
7145 case N_Raise_Storage_Error:
7146 if (type_annotate_only)
7147 gnu_result = alloc_stmt_list ();
7148 else
7149 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
7150 break;
7152 case N_Validate_Unchecked_Conversion:
7153 /* The only validation we currently do on an unchecked conversion is
7154 that of aliasing assumptions. */
7155 if (flag_strict_aliasing)
7156 gnat_validate_uc_list.safe_push (gnat_node);
7157 gnu_result = alloc_stmt_list ();
7158 break;
7160 case N_Function_Specification:
7161 case N_Procedure_Specification:
7162 case N_Op_Concat:
7163 case N_Component_Association:
7164 case N_Protected_Body:
7165 case N_Task_Body:
7166 /* These nodes should only be present when annotating types. */
7167 gcc_assert (type_annotate_only);
7168 gnu_result = alloc_stmt_list ();
7169 break;
7171 default:
7172 /* Other nodes are not supposed to reach here. */
7173 gcc_unreachable ();
7176 /* If we pushed the processing of the elaboration routine, pop it back. */
7177 if (went_into_elab_proc)
7178 current_function_decl = NULL_TREE;
7180 /* When not optimizing, turn boolean rvalues B into B != false tests
7181 so that the code just below can put the location information of the
7182 reference to B on the inequality operator for better debug info. */
7183 if (!optimize
7184 && TREE_CODE (gnu_result) != INTEGER_CST
7185 && (kind == N_Identifier
7186 || kind == N_Expanded_Name
7187 || kind == N_Explicit_Dereference
7188 || kind == N_Function_Call
7189 || kind == N_Indexed_Component
7190 || kind == N_Selected_Component)
7191 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
7192 && !lvalue_required_p (gnat_node, gnu_result_type, false, false, false))
7193 gnu_result = build_binary_op (NE_EXPR, gnu_result_type,
7194 convert (gnu_result_type, gnu_result),
7195 convert (gnu_result_type,
7196 boolean_false_node));
7198 /* Set the location information on the result. Note that we may have
7199 no result if we tried to build a CALL_EXPR node to a procedure with
7200 no side-effects and optimization is enabled. */
7201 if (gnu_result && EXPR_P (gnu_result))
7202 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7204 /* If we're supposed to return something of void_type, it means we have
7205 something we're elaborating for effect, so just return. */
7206 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
7207 return gnu_result;
7209 /* If the result is a constant that overflowed, raise Constraint_Error. */
7210 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
7212 post_error ("?`Constraint_Error` will be raised at run time", gnat_node);
7213 gnu_result
7214 = build1 (NULL_EXPR, gnu_result_type,
7215 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
7216 N_Raise_Constraint_Error));
7219 /* If the result has side-effects and is of an unconstrained type, make a
7220 SAVE_EXPR so that we can be sure it will only be referenced once. But
7221 this is useless for a call to a function that returns an unconstrained
7222 type with default discriminant, as we cannot compute the size of the
7223 actual returned object. We must do this before any conversions. */
7224 if (TREE_SIDE_EFFECTS (gnu_result)
7225 && !(TREE_CODE (gnu_result) == CALL_EXPR
7226 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7227 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
7228 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))))
7229 gnu_result = gnat_stabilize_reference (gnu_result, false, NULL);
7231 /* Now convert the result to the result type, unless we are in one of the
7232 following cases:
7234 1. If this is the LHS of an assignment or an actual parameter of a
7235 call, return the result almost unmodified since the RHS will have
7236 to be converted to our type in that case, unless the result type
7237 has a simpler size. Likewise if there is just a no-op unchecked
7238 conversion in-between. Similarly, don't convert integral types
7239 that are the operands of an unchecked conversion since we need
7240 to ignore those conversions (for 'Valid).
7242 2. If we have a label (which doesn't have any well-defined type), a
7243 field or an error, return the result almost unmodified. Similarly,
7244 if the two types are record types with the same name, don't convert.
7245 This will be the case when we are converting from a packable version
7246 of a type to its original type and we need those conversions to be
7247 NOPs in order for assignments into these types to work properly.
7249 3. If the type is void or if we have no result, return error_mark_node
7250 to show we have no result.
7252 4. If this a call to a function that returns an unconstrained type with
7253 default discriminant, return the call expression unmodified since we
7254 cannot compute the size of the actual returned object.
7256 5. Finally, if the type of the result is already correct. */
7258 if (Present (Parent (gnat_node))
7259 && (lhs_or_actual_p (gnat_node)
7260 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7261 && unchecked_conversion_nop (Parent (gnat_node)))
7262 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7263 && !AGGREGATE_TYPE_P (gnu_result_type)
7264 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
7265 && !(TYPE_SIZE (gnu_result_type)
7266 && TYPE_SIZE (TREE_TYPE (gnu_result))
7267 && (AGGREGATE_TYPE_P (gnu_result_type)
7268 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
7269 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
7270 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
7271 != INTEGER_CST))
7272 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
7273 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
7274 && (CONTAINS_PLACEHOLDER_P
7275 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
7276 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
7277 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
7279 /* Remove padding only if the inner object is of self-referential
7280 size: in that case it must be an object of unconstrained type
7281 with a default discriminant and we want to avoid copying too
7282 much data. */
7283 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7284 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
7285 (TREE_TYPE (gnu_result))))))
7286 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7287 gnu_result);
7290 else if (TREE_CODE (gnu_result) == LABEL_DECL
7291 || TREE_CODE (gnu_result) == FIELD_DECL
7292 || TREE_CODE (gnu_result) == ERROR_MARK
7293 || (TYPE_NAME (gnu_result_type)
7294 == TYPE_NAME (TREE_TYPE (gnu_result))
7295 && TREE_CODE (gnu_result_type) == RECORD_TYPE
7296 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
7298 /* Remove any padding. */
7299 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7300 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7301 gnu_result);
7304 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
7305 gnu_result = error_mark_node;
7307 else if (TREE_CODE (gnu_result) == CALL_EXPR
7308 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7309 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result)))
7310 == gnu_result_type
7311 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
7314 else if (TREE_TYPE (gnu_result) != gnu_result_type)
7315 gnu_result = convert (gnu_result_type, gnu_result);
7317 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
7318 while ((TREE_CODE (gnu_result) == NOP_EXPR
7319 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
7320 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
7321 gnu_result = TREE_OPERAND (gnu_result, 0);
7323 return gnu_result;
7326 /* Subroutine of above to push the exception label stack. GNU_STACK is
7327 a pointer to the stack to update and GNAT_LABEL, if present, is the
7328 label to push onto the stack. */
7330 static void
7331 push_exception_label_stack (vec<tree, va_gc> **gnu_stack, Entity_Id gnat_label)
7333 tree gnu_label = (Present (gnat_label)
7334 ? gnat_to_gnu_entity (gnat_label, NULL_TREE, 0)
7335 : NULL_TREE);
7337 vec_safe_push (*gnu_stack, gnu_label);
7340 /* Record the current code position in GNAT_NODE. */
7342 static void
7343 record_code_position (Node_Id gnat_node)
7345 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
7347 add_stmt_with_node (stmt_stmt, gnat_node);
7348 save_gnu_tree (gnat_node, stmt_stmt, true);
7351 /* Insert the code for GNAT_NODE at the position saved for that node. */
7353 static void
7354 insert_code_for (Node_Id gnat_node)
7356 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
7357 save_gnu_tree (gnat_node, NULL_TREE, true);
7360 /* Start a new statement group chained to the previous group. */
7362 void
7363 start_stmt_group (void)
7365 struct stmt_group *group = stmt_group_free_list;
7367 /* First see if we can get one from the free list. */
7368 if (group)
7369 stmt_group_free_list = group->previous;
7370 else
7371 group = ggc_alloc<stmt_group> ();
7373 group->previous = current_stmt_group;
7374 group->stmt_list = group->block = group->cleanups = NULL_TREE;
7375 current_stmt_group = group;
7378 /* Add GNU_STMT to the current statement group. If it is an expression with
7379 no effects, it is ignored. */
7381 void
7382 add_stmt (tree gnu_stmt)
7384 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
7387 /* Similar, but the statement is always added, regardless of side-effects. */
7389 void
7390 add_stmt_force (tree gnu_stmt)
7392 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
7395 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
7397 void
7398 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
7400 /* Do not emit a location for renamings that come from generic instantiation,
7401 they are likely to disturb debugging. */
7402 if (Present (gnat_node)
7403 && !renaming_from_generic_instantiation_p (gnat_node))
7404 set_expr_location_from_node (gnu_stmt, gnat_node);
7405 add_stmt (gnu_stmt);
7408 /* Similar, but the statement is always added, regardless of side-effects. */
7410 void
7411 add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
7413 if (Present (gnat_node))
7414 set_expr_location_from_node (gnu_stmt, gnat_node);
7415 add_stmt_force (gnu_stmt);
7418 /* Add a declaration statement for GNU_DECL to the current statement group.
7419 Get SLOC from Entity_Id. */
7421 void
7422 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
7424 tree type = TREE_TYPE (gnu_decl);
7425 tree gnu_stmt, gnu_init, t;
7427 /* If this is a variable that Gigi is to ignore, we may have been given
7428 an ERROR_MARK. So test for it. We also might have been given a
7429 reference for a renaming. So only do something for a decl. Also
7430 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
7431 if (!DECL_P (gnu_decl)
7432 || (TREE_CODE (gnu_decl) == TYPE_DECL
7433 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
7434 return;
7436 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
7438 /* If we are external or global, we don't want to output the DECL_EXPR for
7439 this DECL node since we already have evaluated the expressions in the
7440 sizes and positions as globals and doing it again would be wrong. */
7441 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
7443 /* Mark everything as used to prevent node sharing with subprograms.
7444 Note that walk_tree knows how to deal with TYPE_DECL, but neither
7445 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
7446 MARK_VISITED (gnu_stmt);
7447 if (TREE_CODE (gnu_decl) == VAR_DECL
7448 || TREE_CODE (gnu_decl) == CONST_DECL)
7450 MARK_VISITED (DECL_SIZE (gnu_decl));
7451 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
7452 MARK_VISITED (DECL_INITIAL (gnu_decl));
7454 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
7455 else if (TREE_CODE (gnu_decl) == TYPE_DECL
7456 && RECORD_OR_UNION_TYPE_P (type)
7457 && !TYPE_FAT_POINTER_P (type))
7458 MARK_VISITED (TYPE_ADA_SIZE (type));
7460 else
7461 add_stmt_with_node (gnu_stmt, gnat_entity);
7463 /* If this is a variable and an initializer is attached to it, it must be
7464 valid for the context. Similar to init_const in create_var_decl_1. */
7465 if (TREE_CODE (gnu_decl) == VAR_DECL
7466 && (gnu_init = DECL_INITIAL (gnu_decl)) != NULL_TREE
7467 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
7468 || (TREE_STATIC (gnu_decl)
7469 && !initializer_constant_valid_p (gnu_init,
7470 TREE_TYPE (gnu_init)))))
7472 /* If GNU_DECL has a padded type, convert it to the unpadded
7473 type so the assignment is done properly. */
7474 if (TYPE_IS_PADDING_P (type))
7475 t = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
7476 else
7477 t = gnu_decl;
7479 gnu_stmt = build_binary_op (INIT_EXPR, NULL_TREE, t, gnu_init);
7481 DECL_INITIAL (gnu_decl) = NULL_TREE;
7482 if (TREE_READONLY (gnu_decl))
7484 TREE_READONLY (gnu_decl) = 0;
7485 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
7488 add_stmt_with_node (gnu_stmt, gnat_entity);
7492 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
7494 static tree
7495 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
7497 tree t = *tp;
7499 if (TREE_VISITED (t))
7500 *walk_subtrees = 0;
7502 /* Don't mark a dummy type as visited because we want to mark its sizes
7503 and fields once it's filled in. */
7504 else if (!TYPE_IS_DUMMY_P (t))
7505 TREE_VISITED (t) = 1;
7507 if (TYPE_P (t))
7508 TYPE_SIZES_GIMPLIFIED (t) = 1;
7510 return NULL_TREE;
7513 /* Mark nodes rooted at T with TREE_VISITED and types as having their
7514 sized gimplified. We use this to indicate all variable sizes and
7515 positions in global types may not be shared by any subprogram. */
7517 void
7518 mark_visited (tree t)
7520 walk_tree (&t, mark_visited_r, NULL, NULL);
7523 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
7524 set its location to that of GNAT_NODE if present, but with column info
7525 cleared so that conditional branches generated as part of the cleanup
7526 code do not interfere with coverage analysis tools. */
7528 static void
7529 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
7531 if (Present (gnat_node))
7532 set_expr_location_from_node1 (gnu_cleanup, gnat_node, true);
7533 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
7536 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
7538 void
7539 set_block_for_group (tree gnu_block)
7541 gcc_assert (!current_stmt_group->block);
7542 current_stmt_group->block = gnu_block;
7545 /* Return code corresponding to the current code group. It is normally
7546 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
7547 BLOCK or cleanups were set. */
7549 tree
7550 end_stmt_group (void)
7552 struct stmt_group *group = current_stmt_group;
7553 tree gnu_retval = group->stmt_list;
7555 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
7556 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
7557 make a BIND_EXPR. Note that we nest in that because the cleanup may
7558 reference variables in the block. */
7559 if (gnu_retval == NULL_TREE)
7560 gnu_retval = alloc_stmt_list ();
7562 if (group->cleanups)
7563 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
7564 group->cleanups);
7566 if (current_stmt_group->block)
7567 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
7568 gnu_retval, group->block);
7570 /* Remove this group from the stack and add it to the free list. */
7571 current_stmt_group = group->previous;
7572 group->previous = stmt_group_free_list;
7573 stmt_group_free_list = group;
7575 return gnu_retval;
7578 /* Return whether the current statement group may fall through. */
7580 static inline bool
7581 stmt_group_may_fallthru (void)
7583 if (current_stmt_group->stmt_list)
7584 return block_may_fallthru (current_stmt_group->stmt_list);
7585 else
7586 return true;
7589 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
7590 statements.*/
7592 static void
7593 add_stmt_list (List_Id gnat_list)
7595 Node_Id gnat_node;
7597 if (Present (gnat_list))
7598 for (gnat_node = First (gnat_list); Present (gnat_node);
7599 gnat_node = Next (gnat_node))
7600 add_stmt (gnat_to_gnu (gnat_node));
7603 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
7604 If BINDING_P is true, push and pop a binding level around the list. */
7606 static tree
7607 build_stmt_group (List_Id gnat_list, bool binding_p)
7609 start_stmt_group ();
7610 if (binding_p)
7611 gnat_pushlevel ();
7613 add_stmt_list (gnat_list);
7614 if (binding_p)
7615 gnat_poplevel ();
7617 return end_stmt_group ();
7620 /* Generate GIMPLE in place for the expression at *EXPR_P. */
7623 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
7624 gimple_seq *post_p ATTRIBUTE_UNUSED)
7626 tree expr = *expr_p;
7627 tree op;
7629 if (IS_ADA_STMT (expr))
7630 return gnat_gimplify_stmt (expr_p);
7632 switch (TREE_CODE (expr))
7634 case NULL_EXPR:
7635 /* If this is for a scalar, just make a VAR_DECL for it. If for
7636 an aggregate, get a null pointer of the appropriate type and
7637 dereference it. */
7638 if (AGGREGATE_TYPE_P (TREE_TYPE (expr)))
7639 *expr_p = build1 (INDIRECT_REF, TREE_TYPE (expr),
7640 convert (build_pointer_type (TREE_TYPE (expr)),
7641 integer_zero_node));
7642 else
7644 *expr_p = create_tmp_var (TREE_TYPE (expr), NULL);
7645 TREE_NO_WARNING (*expr_p) = 1;
7648 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
7649 return GS_OK;
7651 case UNCONSTRAINED_ARRAY_REF:
7652 /* We should only do this if we are just elaborating for side-effects,
7653 but we can't know that yet. */
7654 *expr_p = TREE_OPERAND (*expr_p, 0);
7655 return GS_OK;
7657 case ADDR_EXPR:
7658 op = TREE_OPERAND (expr, 0);
7660 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
7661 is put into static memory. We know that it's going to be read-only
7662 given the semantics we have and it must be in static memory when the
7663 reference is in an elaboration procedure. */
7664 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
7666 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
7667 *expr_p = fold_convert (TREE_TYPE (expr), addr);
7668 return GS_ALL_DONE;
7671 return GS_UNHANDLED;
7673 case VIEW_CONVERT_EXPR:
7674 op = TREE_OPERAND (expr, 0);
7676 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
7677 type to a scalar one, explicitly create the local temporary. That's
7678 required if the type is passed by reference. */
7679 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
7680 && AGGREGATE_TYPE_P (TREE_TYPE (op))
7681 && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
7683 tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
7684 gimple_add_tmp_var (new_var);
7686 mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
7687 gimplify_and_add (mod, pre_p);
7689 TREE_OPERAND (expr, 0) = new_var;
7690 return GS_OK;
7693 return GS_UNHANDLED;
7695 case DECL_EXPR:
7696 op = DECL_EXPR_DECL (expr);
7698 /* The expressions for the RM bounds must be gimplified to ensure that
7699 they are properly elaborated. See gimplify_decl_expr. */
7700 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
7701 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)))
7702 switch (TREE_CODE (TREE_TYPE (op)))
7704 case INTEGER_TYPE:
7705 case ENUMERAL_TYPE:
7706 case BOOLEAN_TYPE:
7707 case REAL_TYPE:
7709 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
7711 val = TYPE_RM_MIN_VALUE (type);
7712 if (val)
7714 gimplify_one_sizepos (&val, pre_p);
7715 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7716 SET_TYPE_RM_MIN_VALUE (t, val);
7719 val = TYPE_RM_MAX_VALUE (type);
7720 if (val)
7722 gimplify_one_sizepos (&val, pre_p);
7723 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7724 SET_TYPE_RM_MAX_VALUE (t, val);
7728 break;
7730 default:
7731 break;
7734 /* ... fall through ... */
7736 default:
7737 return GS_UNHANDLED;
7741 /* Generate GIMPLE in place for the statement at *STMT_P. */
7743 static enum gimplify_status
7744 gnat_gimplify_stmt (tree *stmt_p)
7746 tree stmt = *stmt_p;
7748 switch (TREE_CODE (stmt))
7750 case STMT_STMT:
7751 *stmt_p = STMT_STMT_STMT (stmt);
7752 return GS_OK;
7754 case LOOP_STMT:
7756 tree gnu_start_label = create_artificial_label (input_location);
7757 tree gnu_cond = LOOP_STMT_COND (stmt);
7758 tree gnu_update = LOOP_STMT_UPDATE (stmt);
7759 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
7761 /* Build the condition expression from the test, if any. */
7762 if (gnu_cond)
7764 /* Deal with the optimization hints. */
7765 if (LOOP_STMT_IVDEP (stmt))
7766 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7767 build_int_cst (integer_type_node,
7768 annot_expr_ivdep_kind));
7769 if (LOOP_STMT_NO_VECTOR (stmt))
7770 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7771 build_int_cst (integer_type_node,
7772 annot_expr_no_vector_kind));
7773 if (LOOP_STMT_VECTOR (stmt))
7774 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7775 build_int_cst (integer_type_node,
7776 annot_expr_vector_kind));
7778 gnu_cond
7779 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
7780 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
7783 /* Set to emit the statements of the loop. */
7784 *stmt_p = NULL_TREE;
7786 /* We first emit the start label and then a conditional jump to the
7787 end label if there's a top condition, then the update if it's at
7788 the top, then the body of the loop, then a conditional jump to
7789 the end label if there's a bottom condition, then the update if
7790 it's at the bottom, and finally a jump to the start label and the
7791 definition of the end label. */
7792 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7793 gnu_start_label),
7794 stmt_p);
7796 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
7797 append_to_statement_list (gnu_cond, stmt_p);
7799 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
7800 append_to_statement_list (gnu_update, stmt_p);
7802 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
7804 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
7805 append_to_statement_list (gnu_cond, stmt_p);
7807 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
7808 append_to_statement_list (gnu_update, stmt_p);
7810 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
7811 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
7812 append_to_statement_list (t, stmt_p);
7814 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7815 gnu_end_label),
7816 stmt_p);
7817 return GS_OK;
7820 case EXIT_STMT:
7821 /* Build a statement to jump to the corresponding end label, then
7822 see if it needs to be conditional. */
7823 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
7824 if (EXIT_STMT_COND (stmt))
7825 *stmt_p = build3 (COND_EXPR, void_type_node,
7826 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
7827 return GS_OK;
7829 default:
7830 gcc_unreachable ();
7834 /* Force references to each of the entities in packages withed by GNAT_NODE.
7835 Operate recursively but check that we aren't elaborating something more
7836 than once.
7838 This routine is exclusively called in type_annotate mode, to compute DDA
7839 information for types in withed units, for ASIS use. */
7841 static void
7842 elaborate_all_entities (Node_Id gnat_node)
7844 Entity_Id gnat_with_clause, gnat_entity;
7846 /* Process each unit only once. As we trace the context of all relevant
7847 units transitively, including generic bodies, we may encounter the
7848 same generic unit repeatedly. */
7849 if (!present_gnu_tree (gnat_node))
7850 save_gnu_tree (gnat_node, integer_zero_node, true);
7852 /* Save entities in all context units. A body may have an implicit_with
7853 on its own spec, if the context includes a child unit, so don't save
7854 the spec twice. */
7855 for (gnat_with_clause = First (Context_Items (gnat_node));
7856 Present (gnat_with_clause);
7857 gnat_with_clause = Next (gnat_with_clause))
7858 if (Nkind (gnat_with_clause) == N_With_Clause
7859 && !present_gnu_tree (Library_Unit (gnat_with_clause))
7860 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
7862 elaborate_all_entities (Library_Unit (gnat_with_clause));
7864 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
7866 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
7867 Present (gnat_entity);
7868 gnat_entity = Next_Entity (gnat_entity))
7869 if (Is_Public (gnat_entity)
7870 && Convention (gnat_entity) != Convention_Intrinsic
7871 && Ekind (gnat_entity) != E_Package
7872 && Ekind (gnat_entity) != E_Package_Body
7873 && Ekind (gnat_entity) != E_Operator
7874 && !(IN (Ekind (gnat_entity), Type_Kind)
7875 && !Is_Frozen (gnat_entity))
7876 && !((Ekind (gnat_entity) == E_Procedure
7877 || Ekind (gnat_entity) == E_Function)
7878 && Is_Intrinsic_Subprogram (gnat_entity))
7879 && !IN (Ekind (gnat_entity), Named_Kind)
7880 && !IN (Ekind (gnat_entity), Generic_Unit_Kind))
7881 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
7883 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
7885 Node_Id gnat_body
7886 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
7888 /* Retrieve compilation unit node of generic body. */
7889 while (Present (gnat_body)
7890 && Nkind (gnat_body) != N_Compilation_Unit)
7891 gnat_body = Parent (gnat_body);
7893 /* If body is available, elaborate its context. */
7894 if (Present (gnat_body))
7895 elaborate_all_entities (gnat_body);
7899 if (Nkind (Unit (gnat_node)) == N_Package_Body)
7900 elaborate_all_entities (Library_Unit (gnat_node));
7903 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
7905 static void
7906 process_freeze_entity (Node_Id gnat_node)
7908 const Entity_Id gnat_entity = Entity (gnat_node);
7909 const Entity_Kind kind = Ekind (gnat_entity);
7910 tree gnu_old, gnu_new;
7912 /* If this is a package, we need to generate code for the package. */
7913 if (kind == E_Package)
7915 insert_code_for
7916 (Parent (Corresponding_Body
7917 (Parent (Declaration_Node (gnat_entity)))));
7918 return;
7921 /* Don't do anything for class-wide types as they are always transformed
7922 into their root type. */
7923 if (kind == E_Class_Wide_Type)
7924 return;
7926 /* Check for an old definition. This freeze node might be for an Itype. */
7927 gnu_old
7928 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
7930 /* If this entity has an address representation clause, GNU_OLD is the
7931 address, so discard it here. */
7932 if (Present (Address_Clause (gnat_entity)))
7933 gnu_old = NULL_TREE;
7935 /* Don't do anything for subprograms that may have been elaborated before
7936 their freeze nodes. This can happen, for example, because of an inner
7937 call in an instance body or because of previous compilation of a spec
7938 for inlining purposes. */
7939 if (gnu_old
7940 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
7941 && (kind == E_Function || kind == E_Procedure))
7942 || (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
7943 && kind == E_Subprogram_Type)))
7944 return;
7946 /* If we have a non-dummy type old tree, we have nothing to do, except
7947 aborting if this is the public view of a private type whose full view was
7948 not delayed, as this node was never delayed as it should have been. We
7949 let this happen for concurrent types and their Corresponding_Record_Type,
7950 however, because each might legitimately be elaborated before its own
7951 freeze node, e.g. while processing the other. */
7952 if (gnu_old
7953 && !(TREE_CODE (gnu_old) == TYPE_DECL
7954 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
7956 gcc_assert ((IN (kind, Incomplete_Or_Private_Kind)
7957 && Present (Full_View (gnat_entity))
7958 && No (Freeze_Node (Full_View (gnat_entity))))
7959 || Is_Concurrent_Type (gnat_entity)
7960 || (IN (kind, Record_Kind)
7961 && Is_Concurrent_Record_Type (gnat_entity)));
7962 return;
7965 /* Reset the saved tree, if any, and elaborate the object or type for real.
7966 If there is a full view, elaborate it and use the result. And, if this
7967 is the root type of a class-wide type, reuse it for the latter. */
7968 if (gnu_old)
7970 save_gnu_tree (gnat_entity, NULL_TREE, false);
7972 if (IN (kind, Incomplete_Or_Private_Kind)
7973 && Present (Full_View (gnat_entity)))
7975 Entity_Id full_view = Full_View (gnat_entity);
7977 save_gnu_tree (full_view, NULL_TREE, false);
7979 if (IN (Ekind (full_view), Private_Kind)
7980 && Present (Underlying_Full_View (full_view)))
7982 full_view = Underlying_Full_View (full_view);
7983 save_gnu_tree (full_view, NULL_TREE, false);
7987 if (IN (kind, Type_Kind)
7988 && Present (Class_Wide_Type (gnat_entity))
7989 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
7990 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
7993 if (IN (kind, Incomplete_Or_Private_Kind)
7994 && Present (Full_View (gnat_entity)))
7996 Entity_Id full_view = Full_View (gnat_entity);
7998 if (IN (Ekind (full_view), Private_Kind)
7999 && Present (Underlying_Full_View (full_view)))
8000 full_view = Underlying_Full_View (full_view);
8002 gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, 1);
8004 /* Propagate back-annotations from full view to partial view. */
8005 if (Unknown_Alignment (gnat_entity))
8006 Set_Alignment (gnat_entity, Alignment (full_view));
8008 if (Unknown_Esize (gnat_entity))
8009 Set_Esize (gnat_entity, Esize (full_view));
8011 if (Unknown_RM_Size (gnat_entity))
8012 Set_RM_Size (gnat_entity, RM_Size (full_view));
8014 /* The above call may have defined this entity (the simplest example
8015 of this is when we have a private enumeral type since the bounds
8016 will have the public view). */
8017 if (!present_gnu_tree (gnat_entity))
8018 save_gnu_tree (gnat_entity, gnu_new, false);
8020 else
8022 tree gnu_init
8023 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
8024 && present_gnu_tree (Declaration_Node (gnat_entity)))
8025 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
8027 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
8030 if (IN (kind, Type_Kind)
8031 && Present (Class_Wide_Type (gnat_entity))
8032 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8033 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
8035 /* If we have an old type and we've made pointers to this type, update those
8036 pointers. If this is a Taft amendment type in the main unit, we need to
8037 mark the type as used since other units referencing it don't see the full
8038 declaration and, therefore, cannot mark it as used themselves. */
8039 if (gnu_old)
8041 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8042 TREE_TYPE (gnu_new));
8043 if (DECL_TAFT_TYPE_P (gnu_old))
8044 used_types_insert (TREE_TYPE (gnu_new));
8048 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
8049 We make two passes, one to elaborate anything other than bodies (but
8050 we declare a function if there was no spec). The second pass
8051 elaborates the bodies.
8053 GNAT_END_LIST gives the element in the list past the end. Normally,
8054 this is Empty, but can be First_Real_Statement for a
8055 Handled_Sequence_Of_Statements.
8057 We make a complete pass through both lists if PASS1P is true, then make
8058 the second pass over both lists if PASS2P is true. The lists usually
8059 correspond to the public and private parts of a package. */
8061 static void
8062 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
8063 Node_Id gnat_end_list, bool pass1p, bool pass2p)
8065 List_Id gnat_decl_array[2];
8066 Node_Id gnat_decl;
8067 int i;
8069 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
8071 if (pass1p)
8072 for (i = 0; i <= 1; i++)
8073 if (Present (gnat_decl_array[i]))
8074 for (gnat_decl = First (gnat_decl_array[i]);
8075 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8077 /* For package specs, we recurse inside the declarations,
8078 thus taking the two pass approach inside the boundary. */
8079 if (Nkind (gnat_decl) == N_Package_Declaration
8080 && (Nkind (Specification (gnat_decl)
8081 == N_Package_Specification)))
8082 process_decls (Visible_Declarations (Specification (gnat_decl)),
8083 Private_Declarations (Specification (gnat_decl)),
8084 Empty, true, false);
8086 /* Similarly for any declarations in the actions of a
8087 freeze node. */
8088 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8090 process_freeze_entity (gnat_decl);
8091 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
8094 /* Package bodies with freeze nodes get their elaboration deferred
8095 until the freeze node, but the code must be placed in the right
8096 place, so record the code position now. */
8097 else if (Nkind (gnat_decl) == N_Package_Body
8098 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
8099 record_code_position (gnat_decl);
8101 else if (Nkind (gnat_decl) == N_Package_Body_Stub
8102 && Present (Library_Unit (gnat_decl))
8103 && Present (Freeze_Node
8104 (Corresponding_Spec
8105 (Proper_Body (Unit
8106 (Library_Unit (gnat_decl)))))))
8107 record_code_position
8108 (Proper_Body (Unit (Library_Unit (gnat_decl))));
8110 /* We defer most subprogram bodies to the second pass. */
8111 else if (Nkind (gnat_decl) == N_Subprogram_Body)
8113 if (Acts_As_Spec (gnat_decl))
8115 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
8117 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
8118 && Ekind (gnat_subprog_id) != E_Generic_Function)
8119 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8123 /* For bodies and stubs that act as their own specs, the entity
8124 itself must be elaborated in the first pass, because it may
8125 be used in other declarations. */
8126 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
8128 Node_Id gnat_subprog_id
8129 = Defining_Entity (Specification (gnat_decl));
8131 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
8132 && Ekind (gnat_subprog_id) != E_Generic_Procedure
8133 && Ekind (gnat_subprog_id) != E_Generic_Function)
8134 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8137 /* Concurrent stubs stand for the corresponding subprogram bodies,
8138 which are deferred like other bodies. */
8139 else if (Nkind (gnat_decl) == N_Task_Body_Stub
8140 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8143 else
8144 add_stmt (gnat_to_gnu (gnat_decl));
8147 /* Here we elaborate everything we deferred above except for package bodies,
8148 which are elaborated at their freeze nodes. Note that we must also
8149 go inside things (package specs and freeze nodes) the first pass did. */
8150 if (pass2p)
8151 for (i = 0; i <= 1; i++)
8152 if (Present (gnat_decl_array[i]))
8153 for (gnat_decl = First (gnat_decl_array[i]);
8154 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8156 if (Nkind (gnat_decl) == N_Subprogram_Body
8157 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
8158 || Nkind (gnat_decl) == N_Task_Body_Stub
8159 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8160 add_stmt (gnat_to_gnu (gnat_decl));
8162 else if (Nkind (gnat_decl) == N_Package_Declaration
8163 && (Nkind (Specification (gnat_decl)
8164 == N_Package_Specification)))
8165 process_decls (Visible_Declarations (Specification (gnat_decl)),
8166 Private_Declarations (Specification (gnat_decl)),
8167 Empty, false, true);
8169 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8170 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
8174 /* Make a unary operation of kind CODE using build_unary_op, but guard
8175 the operation by an overflow check. CODE can be one of NEGATE_EXPR
8176 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
8177 the operation is to be performed in that type. GNAT_NODE is the gnat
8178 node conveying the source location for which the error should be
8179 signaled. */
8181 static tree
8182 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
8183 Node_Id gnat_node)
8185 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
8187 operand = gnat_protect_expr (operand);
8189 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
8190 operand, TYPE_MIN_VALUE (gnu_type)),
8191 build_unary_op (code, gnu_type, operand),
8192 CE_Overflow_Check_Failed, gnat_node);
8195 /* Make a binary operation of kind CODE using build_binary_op, but guard
8196 the operation by an overflow check. CODE can be one of PLUS_EXPR,
8197 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
8198 Usually the operation is to be performed in that type. GNAT_NODE is
8199 the GNAT node conveying the source location for which the error should
8200 be signaled. */
8202 static tree
8203 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
8204 tree right, Node_Id gnat_node)
8206 const unsigned int precision = TYPE_PRECISION (gnu_type);
8207 tree lhs = gnat_protect_expr (left);
8208 tree rhs = gnat_protect_expr (right);
8209 tree type_max = TYPE_MAX_VALUE (gnu_type);
8210 tree type_min = TYPE_MIN_VALUE (gnu_type);
8211 tree zero = convert (gnu_type, integer_zero_node);
8212 tree gnu_expr, rhs_lt_zero, tmp1, tmp2;
8213 tree check_pos, check_neg, check;
8215 /* Assert that the precision is a power of 2. */
8216 gcc_assert ((precision & (precision - 1)) == 0);
8218 /* Prefer a constant or known-positive rhs to simplify checks. */
8219 if (!TREE_CONSTANT (rhs)
8220 && commutative_tree_code (code)
8221 && (TREE_CONSTANT (lhs)
8222 || (!tree_expr_nonnegative_p (rhs)
8223 && tree_expr_nonnegative_p (lhs))))
8225 tree tmp = lhs;
8226 lhs = rhs;
8227 rhs = tmp;
8230 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
8232 /* If we can fold the expression to a constant, just return it.
8233 The caller will deal with overflow, no need to generate a check. */
8234 if (TREE_CONSTANT (gnu_expr))
8235 return gnu_expr;
8237 rhs_lt_zero = tree_expr_nonnegative_p (rhs)
8238 ? boolean_false_node
8239 : build_binary_op (LT_EXPR, boolean_type_node, rhs, zero);
8241 /* ??? Should use more efficient check for operand_equal_p (lhs, rhs, 0) */
8243 /* Try a few strategies that may be cheaper than the general
8244 code at the end of the function, if the rhs is not known.
8245 The strategies are:
8246 - Call library function for 64-bit multiplication (complex)
8247 - Widen, if input arguments are sufficiently small
8248 - Determine overflow using wrapped result for addition/subtraction. */
8250 if (!TREE_CONSTANT (rhs))
8252 /* Even for add/subtract double size to get another base type. */
8253 const unsigned int needed_precision = precision * 2;
8255 if (code == MULT_EXPR && precision == 64)
8257 tree int_64 = gnat_type_for_size (64, 0);
8259 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
8260 convert (int_64, lhs),
8261 convert (int_64, rhs)));
8264 if (needed_precision <= BITS_PER_WORD
8265 || (code == MULT_EXPR && needed_precision <= LONG_LONG_TYPE_SIZE))
8267 tree wide_type = gnat_type_for_size (needed_precision, 0);
8268 tree wide_result = build_binary_op (code, wide_type,
8269 convert (wide_type, lhs),
8270 convert (wide_type, rhs));
8272 check = build_binary_op
8273 (TRUTH_ORIF_EXPR, boolean_type_node,
8274 build_binary_op (LT_EXPR, boolean_type_node, wide_result,
8275 convert (wide_type, type_min)),
8276 build_binary_op (GT_EXPR, boolean_type_node, wide_result,
8277 convert (wide_type, type_max)));
8279 return
8280 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8283 if (code == PLUS_EXPR || code == MINUS_EXPR)
8285 tree unsigned_type = gnat_type_for_size (precision, 1);
8286 tree wrapped_expr
8287 = convert (gnu_type,
8288 build_binary_op (code, unsigned_type,
8289 convert (unsigned_type, lhs),
8290 convert (unsigned_type, rhs)));
8292 /* Overflow when (rhs < 0) ^ (wrapped_expr < lhs)), for addition
8293 or when (rhs < 0) ^ (wrapped_expr > lhs) for subtraction. */
8294 check
8295 = build_binary_op (TRUTH_XOR_EXPR, boolean_type_node, rhs_lt_zero,
8296 build_binary_op (code == PLUS_EXPR
8297 ? LT_EXPR : GT_EXPR,
8298 boolean_type_node,
8299 wrapped_expr, lhs));
8301 return
8302 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8306 switch (code)
8308 case PLUS_EXPR:
8309 /* When rhs >= 0, overflow when lhs > type_max - rhs. */
8310 check_pos = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8311 build_binary_op (MINUS_EXPR, gnu_type,
8312 type_max, rhs)),
8314 /* When rhs < 0, overflow when lhs < type_min - rhs. */
8315 check_neg = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8316 build_binary_op (MINUS_EXPR, gnu_type,
8317 type_min, rhs));
8318 break;
8320 case MINUS_EXPR:
8321 /* When rhs >= 0, overflow when lhs < type_min + rhs. */
8322 check_pos = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8323 build_binary_op (PLUS_EXPR, gnu_type,
8324 type_min, rhs)),
8326 /* When rhs < 0, overflow when lhs > type_max + rhs. */
8327 check_neg = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8328 build_binary_op (PLUS_EXPR, gnu_type,
8329 type_max, rhs));
8330 break;
8332 case MULT_EXPR:
8333 /* The check here is designed to be efficient if the rhs is constant,
8334 but it will work for any rhs by using integer division.
8335 Four different check expressions determine whether X * C overflows,
8336 depending on C.
8337 C == 0 => false
8338 C > 0 => X > type_max / C || X < type_min / C
8339 C == -1 => X == type_min
8340 C < -1 => X > type_min / C || X < type_max / C */
8342 tmp1 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
8343 tmp2 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
8345 check_pos
8346 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
8347 build_binary_op (NE_EXPR, boolean_type_node, zero,
8348 rhs),
8349 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8350 build_binary_op (GT_EXPR,
8351 boolean_type_node,
8352 lhs, tmp1),
8353 build_binary_op (LT_EXPR,
8354 boolean_type_node,
8355 lhs, tmp2)));
8357 check_neg
8358 = fold_build3 (COND_EXPR, boolean_type_node,
8359 build_binary_op (EQ_EXPR, boolean_type_node, rhs,
8360 build_int_cst (gnu_type, -1)),
8361 build_binary_op (EQ_EXPR, boolean_type_node, lhs,
8362 type_min),
8363 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8364 build_binary_op (GT_EXPR,
8365 boolean_type_node,
8366 lhs, tmp2),
8367 build_binary_op (LT_EXPR,
8368 boolean_type_node,
8369 lhs, tmp1)));
8370 break;
8372 default:
8373 gcc_unreachable();
8376 check = fold_build3 (COND_EXPR, boolean_type_node, rhs_lt_zero, check_neg,
8377 check_pos);
8379 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8382 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
8383 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
8384 which we have to check. GNAT_NODE is the GNAT node conveying the source
8385 location for which the error should be signaled. */
8387 static tree
8388 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
8390 tree gnu_range_type = get_unpadded_type (gnat_range_type);
8391 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
8393 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
8394 This can for example happen when translating 'Val or 'Value. */
8395 if (gnu_compare_type == gnu_range_type)
8396 return gnu_expr;
8398 /* Range checks can only be applied to types with ranges. */
8399 gcc_assert (INTEGRAL_TYPE_P (gnu_range_type)
8400 || SCALAR_FLOAT_TYPE_P (gnu_range_type));
8402 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
8403 we can't do anything since we might be truncating the bounds. No
8404 check is needed in this case. */
8405 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
8406 && (TYPE_PRECISION (gnu_compare_type)
8407 < TYPE_PRECISION (get_base_type (gnu_range_type))))
8408 return gnu_expr;
8410 /* Checked expressions must be evaluated only once. */
8411 gnu_expr = gnat_protect_expr (gnu_expr);
8413 /* Note that the form of the check is
8414 (not (expr >= lo)) or (not (expr <= hi))
8415 the reason for this slightly convoluted form is that NaNs
8416 are not considered to be in range in the float case. */
8417 return emit_check
8418 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8419 invert_truthvalue
8420 (build_binary_op (GE_EXPR, boolean_type_node,
8421 convert (gnu_compare_type, gnu_expr),
8422 convert (gnu_compare_type,
8423 TYPE_MIN_VALUE
8424 (gnu_range_type)))),
8425 invert_truthvalue
8426 (build_binary_op (LE_EXPR, boolean_type_node,
8427 convert (gnu_compare_type, gnu_expr),
8428 convert (gnu_compare_type,
8429 TYPE_MAX_VALUE
8430 (gnu_range_type))))),
8431 gnu_expr, CE_Range_Check_Failed, gnat_node);
8434 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object which
8435 we are about to index, GNU_EXPR is the index expression to be checked,
8436 GNU_LOW and GNU_HIGH are the lower and upper bounds against which GNU_EXPR
8437 has to be checked. Note that for index checking we cannot simply use the
8438 emit_range_check function (although very similar code needs to be generated
8439 in both cases) since for index checking the array type against which we are
8440 checking the indices may be unconstrained and consequently we need to get
8441 the actual index bounds from the array object itself (GNU_ARRAY_OBJECT).
8442 The place where we need to do that is in subprograms having unconstrained
8443 array formal parameters. GNAT_NODE is the GNAT node conveying the source
8444 location for which the error should be signaled. */
8446 static tree
8447 emit_index_check (tree gnu_array_object, tree gnu_expr, tree gnu_low,
8448 tree gnu_high, Node_Id gnat_node)
8450 tree gnu_expr_check;
8452 /* Checked expressions must be evaluated only once. */
8453 gnu_expr = gnat_protect_expr (gnu_expr);
8455 /* Must do this computation in the base type in case the expression's
8456 type is an unsigned subtypes. */
8457 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
8459 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
8460 the object we are handling. */
8461 gnu_low = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low, gnu_array_object);
8462 gnu_high = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high, gnu_array_object);
8464 return emit_check
8465 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8466 build_binary_op (LT_EXPR, boolean_type_node,
8467 gnu_expr_check,
8468 convert (TREE_TYPE (gnu_expr_check),
8469 gnu_low)),
8470 build_binary_op (GT_EXPR, boolean_type_node,
8471 gnu_expr_check,
8472 convert (TREE_TYPE (gnu_expr_check),
8473 gnu_high))),
8474 gnu_expr, CE_Index_Check_Failed, gnat_node);
8477 /* GNU_COND contains the condition corresponding to an access, discriminant or
8478 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
8479 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
8480 REASON is the code that says why the exception was raised. GNAT_NODE is
8481 the GNAT node conveying the source location for which the error should be
8482 signaled. */
8484 static tree
8485 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
8487 tree gnu_call
8488 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
8489 tree gnu_result
8490 = fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
8491 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
8492 convert (TREE_TYPE (gnu_expr), integer_zero_node)),
8493 gnu_expr);
8495 /* GNU_RESULT has side effects if and only if GNU_EXPR has:
8496 we don't need to evaluate it just for the check. */
8497 TREE_SIDE_EFFECTS (gnu_result) = TREE_SIDE_EFFECTS (gnu_expr);
8499 return gnu_result;
8502 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
8503 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
8504 GNAT_TYPE is known to be an integral type. If TRUNCATE_P true, do a
8505 float to integer conversion with truncation; otherwise round.
8506 GNAT_NODE is the GNAT node conveying the source location for which the
8507 error should be signaled. */
8509 static tree
8510 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
8511 bool rangep, bool truncatep, Node_Id gnat_node)
8513 tree gnu_type = get_unpadded_type (gnat_type);
8514 tree gnu_in_type = TREE_TYPE (gnu_expr);
8515 tree gnu_in_basetype = get_base_type (gnu_in_type);
8516 tree gnu_base_type = get_base_type (gnu_type);
8517 tree gnu_result = gnu_expr;
8519 /* If we are not doing any checks, the output is an integral type and the
8520 input is not a floating-point type, just do the conversion. This is
8521 required for packed array types and is simpler in all cases anyway. */
8522 if (!rangep
8523 && !overflowp
8524 && INTEGRAL_TYPE_P (gnu_base_type)
8525 && !FLOAT_TYPE_P (gnu_in_type))
8526 return convert (gnu_type, gnu_expr);
8528 /* First convert the expression to its base type. This
8529 will never generate code, but makes the tests below much simpler.
8530 But don't do this if converting from an integer type to an unconstrained
8531 array type since then we need to get the bounds from the original
8532 (unpacked) type. */
8533 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
8534 gnu_result = convert (gnu_in_basetype, gnu_result);
8536 /* If overflow checks are requested, we need to be sure the result will
8537 fit in the output base type. But don't do this if the input
8538 is integer and the output floating-point. */
8539 if (overflowp
8540 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
8542 /* Ensure GNU_EXPR only gets evaluated once. */
8543 tree gnu_input = gnat_protect_expr (gnu_result);
8544 tree gnu_cond = boolean_false_node;
8545 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
8546 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
8547 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
8548 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
8550 /* Convert the lower bounds to signed types, so we're sure we're
8551 comparing them properly. Likewise, convert the upper bounds
8552 to unsigned types. */
8553 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TYPE_UNSIGNED (gnu_in_basetype))
8554 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
8556 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8557 && !TYPE_UNSIGNED (gnu_in_basetype))
8558 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
8560 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
8561 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
8563 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
8564 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
8566 /* Check each bound separately and only if the result bound
8567 is tighter than the bound on the input type. Note that all the
8568 types are base types, so the bounds must be constant. Also,
8569 the comparison is done in the base type of the input, which
8570 always has the proper signedness. First check for input
8571 integer (which means output integer), output float (which means
8572 both float), or mixed, in which case we always compare.
8573 Note that we have to do the comparison which would *fail* in the
8574 case of an error since if it's an FP comparison and one of the
8575 values is a NaN or Inf, the comparison will fail. */
8576 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8577 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
8578 : (FLOAT_TYPE_P (gnu_base_type)
8579 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
8580 TREE_REAL_CST (gnu_out_lb))
8581 : 1))
8582 gnu_cond
8583 = invert_truthvalue
8584 (build_binary_op (GE_EXPR, boolean_type_node,
8585 gnu_input, convert (gnu_in_basetype,
8586 gnu_out_lb)));
8588 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8589 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
8590 : (FLOAT_TYPE_P (gnu_base_type)
8591 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
8592 TREE_REAL_CST (gnu_in_lb))
8593 : 1))
8594 gnu_cond
8595 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
8596 invert_truthvalue
8597 (build_binary_op (LE_EXPR, boolean_type_node,
8598 gnu_input,
8599 convert (gnu_in_basetype,
8600 gnu_out_ub))));
8602 if (!integer_zerop (gnu_cond))
8603 gnu_result = emit_check (gnu_cond, gnu_input,
8604 CE_Overflow_Check_Failed, gnat_node);
8607 /* Now convert to the result base type. If this is a non-truncating
8608 float-to-integer conversion, round. */
8609 if (INTEGRAL_TYPE_P (gnu_base_type)
8610 && FLOAT_TYPE_P (gnu_in_basetype)
8611 && !truncatep)
8613 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
8614 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
8615 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
8616 const struct real_format *fmt;
8618 /* The following calculations depend on proper rounding to even
8619 of each arithmetic operation. In order to prevent excess
8620 precision from spoiling this property, use the widest hardware
8621 floating-point type if FP_ARITH_MAY_WIDEN is true. */
8622 calc_type
8623 = fp_arith_may_widen ? longest_float_type_node : gnu_in_basetype;
8625 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
8626 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
8627 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
8628 REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf,
8629 half_minus_pred_half);
8630 gnu_pred_half = build_real (calc_type, pred_half);
8632 /* If the input is strictly negative, subtract this value
8633 and otherwise add it from the input. For 0.5, the result
8634 is exactly between 1.0 and the machine number preceding 1.0
8635 (for calc_type). Since the last bit of 1.0 is even, this 0.5
8636 will round to 1.0, while all other number with an absolute
8637 value less than 0.5 round to 0.0. For larger numbers exactly
8638 halfway between integers, rounding will always be correct as
8639 the true mathematical result will be closer to the higher
8640 integer compared to the lower one. So, this constant works
8641 for all floating-point numbers.
8643 The reason to use the same constant with subtract/add instead
8644 of a positive and negative constant is to allow the comparison
8645 to be scheduled in parallel with retrieval of the constant and
8646 conversion of the input to the calc_type (if necessary). */
8648 gnu_zero = convert (gnu_in_basetype, integer_zero_node);
8649 gnu_result = gnat_protect_expr (gnu_result);
8650 gnu_conv = convert (calc_type, gnu_result);
8651 gnu_comp
8652 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
8653 gnu_add_pred_half
8654 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8655 gnu_subtract_pred_half
8656 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8657 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
8658 gnu_add_pred_half, gnu_subtract_pred_half);
8661 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8662 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
8663 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
8664 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
8665 else
8666 gnu_result = convert (gnu_base_type, gnu_result);
8668 /* Finally, do the range check if requested. Note that if the result type
8669 is a modular type, the range check is actually an overflow check. */
8670 if (rangep
8671 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8672 && TYPE_MODULAR_P (gnu_base_type) && overflowp))
8673 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
8675 return convert (gnu_type, gnu_result);
8678 /* Return true if GNU_EXPR can be directly addressed. This is the case
8679 unless it is an expression involving computation or if it involves a
8680 reference to a bitfield or to an object not sufficiently aligned for
8681 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
8682 be directly addressed as an object of this type.
8684 *** Notes on addressability issues in the Ada compiler ***
8686 This predicate is necessary in order to bridge the gap between Gigi
8687 and the middle-end about addressability of GENERIC trees. A tree
8688 is said to be addressable if it can be directly addressed, i.e. if
8689 its address can be taken, is a multiple of the type's alignment on
8690 strict-alignment architectures and returns the first storage unit
8691 assigned to the object represented by the tree.
8693 In the C family of languages, everything is in practice addressable
8694 at the language level, except for bit-fields. This means that these
8695 compilers will take the address of any tree that doesn't represent
8696 a bit-field reference and expect the result to be the first storage
8697 unit assigned to the object. Even in cases where this will result
8698 in unaligned accesses at run time, nothing is supposed to be done
8699 and the program is considered as erroneous instead (see PR c/18287).
8701 The implicit assumptions made in the middle-end are in keeping with
8702 the C viewpoint described above:
8703 - the address of a bit-field reference is supposed to be never
8704 taken; the compiler (generally) will stop on such a construct,
8705 - any other tree is addressable if it is formally addressable,
8706 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
8708 In Ada, the viewpoint is the opposite one: nothing is addressable
8709 at the language level unless explicitly declared so. This means
8710 that the compiler will both make sure that the trees representing
8711 references to addressable ("aliased" in Ada parlance) objects are
8712 addressable and make no real attempts at ensuring that the trees
8713 representing references to non-addressable objects are addressable.
8715 In the first case, Ada is effectively equivalent to C and handing
8716 down the direct result of applying ADDR_EXPR to these trees to the
8717 middle-end works flawlessly. In the second case, Ada cannot afford
8718 to consider the program as erroneous if the address of trees that
8719 are not addressable is requested for technical reasons, unlike C;
8720 as a consequence, the Ada compiler must arrange for either making
8721 sure that this address is not requested in the middle-end or for
8722 compensating by inserting temporaries if it is requested in Gigi.
8724 The first goal can be achieved because the middle-end should not
8725 request the address of non-addressable trees on its own; the only
8726 exception is for the invocation of low-level block operations like
8727 memcpy, for which the addressability requirements are lower since
8728 the type's alignment can be disregarded. In practice, this means
8729 that Gigi must make sure that such operations cannot be applied to
8730 non-BLKmode bit-fields.
8732 The second goal is achieved by means of the addressable_p predicate,
8733 which computes whether a temporary must be inserted by Gigi when the
8734 address of a tree is requested; if so, the address of the temporary
8735 will be used in lieu of that of the original tree and some glue code
8736 generated to connect everything together. */
8738 static bool
8739 addressable_p (tree gnu_expr, tree gnu_type)
8741 /* For an integral type, the size of the actual type of the object may not
8742 be greater than that of the expected type, otherwise an indirect access
8743 in the latter type wouldn't correctly set all the bits of the object. */
8744 if (gnu_type
8745 && INTEGRAL_TYPE_P (gnu_type)
8746 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
8747 return false;
8749 /* The size of the actual type of the object may not be smaller than that
8750 of the expected type, otherwise an indirect access in the latter type
8751 would be larger than the object. But only record types need to be
8752 considered in practice for this case. */
8753 if (gnu_type
8754 && TREE_CODE (gnu_type) == RECORD_TYPE
8755 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
8756 return false;
8758 switch (TREE_CODE (gnu_expr))
8760 case VAR_DECL:
8761 case PARM_DECL:
8762 case FUNCTION_DECL:
8763 case RESULT_DECL:
8764 /* All DECLs are addressable: if they are in a register, we can force
8765 them to memory. */
8766 return true;
8768 case UNCONSTRAINED_ARRAY_REF:
8769 case INDIRECT_REF:
8770 /* Taking the address of a dereference yields the original pointer. */
8771 return true;
8773 case STRING_CST:
8774 case INTEGER_CST:
8775 /* Taking the address yields a pointer to the constant pool. */
8776 return true;
8778 case CONSTRUCTOR:
8779 /* Taking the address of a static constructor yields a pointer to the
8780 tree constant pool. */
8781 return TREE_STATIC (gnu_expr) ? true : false;
8783 case NULL_EXPR:
8784 case SAVE_EXPR:
8785 case CALL_EXPR:
8786 case PLUS_EXPR:
8787 case MINUS_EXPR:
8788 case BIT_IOR_EXPR:
8789 case BIT_XOR_EXPR:
8790 case BIT_AND_EXPR:
8791 case BIT_NOT_EXPR:
8792 /* All rvalues are deemed addressable since taking their address will
8793 force a temporary to be created by the middle-end. */
8794 return true;
8796 case COMPOUND_EXPR:
8797 /* The address of a compound expression is that of its 2nd operand. */
8798 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
8800 case COND_EXPR:
8801 /* We accept &COND_EXPR as soon as both operands are addressable and
8802 expect the outcome to be the address of the selected operand. */
8803 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
8804 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
8806 case COMPONENT_REF:
8807 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
8808 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
8809 the field is sufficiently aligned, in case it is subject
8810 to a pragma Component_Alignment. But we don't need to
8811 check the alignment of the containing record, as it is
8812 guaranteed to be not smaller than that of its most
8813 aligned field that is not a bit-field. */
8814 && (!STRICT_ALIGNMENT
8815 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
8816 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
8817 /* The field of a padding record is always addressable. */
8818 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
8819 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8821 case ARRAY_REF: case ARRAY_RANGE_REF:
8822 case REALPART_EXPR: case IMAGPART_EXPR:
8823 case NOP_EXPR:
8824 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
8826 case CONVERT_EXPR:
8827 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
8828 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8830 case VIEW_CONVERT_EXPR:
8832 /* This is addressable if we can avoid a copy. */
8833 tree type = TREE_TYPE (gnu_expr);
8834 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
8835 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
8836 && (!STRICT_ALIGNMENT
8837 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8838 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
8839 || ((TYPE_MODE (type) == BLKmode
8840 || TYPE_MODE (inner_type) == BLKmode)
8841 && (!STRICT_ALIGNMENT
8842 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8843 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
8844 || TYPE_ALIGN_OK (type)
8845 || TYPE_ALIGN_OK (inner_type))))
8846 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8849 default:
8850 return false;
8854 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
8855 a separate Freeze node exists, delay the bulk of the processing. Otherwise
8856 make a GCC type for GNAT_ENTITY and set up the correspondence. */
8858 void
8859 process_type (Entity_Id gnat_entity)
8861 tree gnu_old
8862 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
8863 tree gnu_new;
8865 /* If we are to delay elaboration of this type, just do any
8866 elaborations needed for expressions within the declaration and
8867 make a dummy type entry for this node and its Full_View (if
8868 any) in case something points to it. Don't do this if it
8869 has already been done (the only way that can happen is if
8870 the private completion is also delayed). */
8871 if (Present (Freeze_Node (gnat_entity))
8872 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8873 && Present (Full_View (gnat_entity))
8874 && Present (Freeze_Node (Full_View (gnat_entity)))
8875 && !present_gnu_tree (Full_View (gnat_entity))))
8877 elaborate_entity (gnat_entity);
8879 if (!gnu_old)
8881 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
8882 save_gnu_tree (gnat_entity, gnu_decl, false);
8883 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8884 && Present (Full_View (gnat_entity)))
8886 if (Has_Completion_In_Body (gnat_entity))
8887 DECL_TAFT_TYPE_P (gnu_decl) = 1;
8888 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
8892 return;
8895 /* If we saved away a dummy type for this node it means that this
8896 made the type that corresponds to the full type of an incomplete
8897 type. Clear that type for now and then update the type in the
8898 pointers. */
8899 if (gnu_old)
8901 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
8902 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
8904 save_gnu_tree (gnat_entity, NULL_TREE, false);
8907 /* Now fully elaborate the type. */
8908 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
8909 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
8911 /* If we have an old type and we've made pointers to this type, update those
8912 pointers. If this is a Taft amendment type in the main unit, we need to
8913 mark the type as used since other units referencing it don't see the full
8914 declaration and, therefore, cannot mark it as used themselves. */
8915 if (gnu_old)
8917 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8918 TREE_TYPE (gnu_new));
8919 if (DECL_TAFT_TYPE_P (gnu_old))
8920 used_types_insert (TREE_TYPE (gnu_new));
8923 /* If this is a record type corresponding to a task or protected type
8924 that is a completion of an incomplete type, perform a similar update
8925 on the type. ??? Including protected types here is a guess. */
8926 if (IN (Ekind (gnat_entity), Record_Kind)
8927 && Is_Concurrent_Record_Type (gnat_entity)
8928 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
8930 tree gnu_task_old
8931 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
8933 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8934 NULL_TREE, false);
8935 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8936 gnu_new, false);
8938 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
8939 TREE_TYPE (gnu_new));
8943 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
8944 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
8945 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
8947 static tree
8948 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
8950 tree gnu_list = NULL_TREE, gnu_result;
8952 /* We test for GNU_FIELD being empty in the case where a variant
8953 was the last thing since we don't take things off GNAT_ASSOC in
8954 that case. We check GNAT_ASSOC in case we have a variant, but it
8955 has no fields. */
8957 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
8959 Node_Id gnat_field = First (Choices (gnat_assoc));
8960 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
8961 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
8963 /* The expander is supposed to put a single component selector name
8964 in every record component association. */
8965 gcc_assert (No (Next (gnat_field)));
8967 /* Ignore fields that have Corresponding_Discriminants since we'll
8968 be setting that field in the parent. */
8969 if (Present (Corresponding_Discriminant (Entity (gnat_field)))
8970 && Is_Tagged_Type (Scope (Entity (gnat_field))))
8971 continue;
8973 /* Also ignore discriminants of Unchecked_Unions. */
8974 if (Is_Unchecked_Union (gnat_entity)
8975 && Ekind (Entity (gnat_field)) == E_Discriminant)
8976 continue;
8978 /* Before assigning a value in an aggregate make sure range checks
8979 are done if required. Then convert to the type of the field. */
8980 if (Do_Range_Check (Expression (gnat_assoc)))
8981 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
8983 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
8985 /* Add the field and expression to the list. */
8986 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
8989 gnu_result = extract_values (gnu_list, gnu_type);
8991 #ifdef ENABLE_CHECKING
8992 /* Verify that every entry in GNU_LIST was used. */
8993 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
8994 gcc_assert (TREE_ADDRESSABLE (gnu_list));
8995 #endif
8997 return gnu_result;
9000 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
9001 the first element of an array aggregate. It may itself be an aggregate.
9002 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
9003 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
9004 for range checking. */
9006 static tree
9007 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
9008 Entity_Id gnat_component_type)
9010 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
9011 tree gnu_expr;
9012 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
9014 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
9016 /* If the expression is itself an array aggregate then first build the
9017 innermost constructor if it is part of our array (multi-dimensional
9018 case). */
9019 if (Nkind (gnat_expr) == N_Aggregate
9020 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
9021 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
9022 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
9023 TREE_TYPE (gnu_array_type),
9024 gnat_component_type);
9025 else
9027 gnu_expr = gnat_to_gnu (gnat_expr);
9029 /* Before assigning the element to the array, make sure it is
9030 in range. */
9031 if (Do_Range_Check (gnat_expr))
9032 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
9035 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
9036 convert (TREE_TYPE (gnu_array_type), gnu_expr));
9038 gnu_index = int_const_binop (PLUS_EXPR, gnu_index,
9039 convert (TREE_TYPE (gnu_index),
9040 integer_one_node));
9043 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
9046 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
9047 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
9048 of the associations that are from RECORD_TYPE. If we see an internal
9049 record, make a recursive call to fill it in as well. */
9051 static tree
9052 extract_values (tree values, tree record_type)
9054 tree field, tem;
9055 vec<constructor_elt, va_gc> *v = NULL;
9057 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9059 tree value = 0;
9061 /* _Parent is an internal field, but may have values in the aggregate,
9062 so check for values first. */
9063 if ((tem = purpose_member (field, values)))
9065 value = TREE_VALUE (tem);
9066 TREE_ADDRESSABLE (tem) = 1;
9069 else if (DECL_INTERNAL_P (field))
9071 value = extract_values (values, TREE_TYPE (field));
9072 if (TREE_CODE (value) == CONSTRUCTOR
9073 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
9074 value = 0;
9076 else
9077 /* If we have a record subtype, the names will match, but not the
9078 actual FIELD_DECLs. */
9079 for (tem = values; tem; tem = TREE_CHAIN (tem))
9080 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
9082 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
9083 TREE_ADDRESSABLE (tem) = 1;
9086 if (!value)
9087 continue;
9089 CONSTRUCTOR_APPEND_ELT (v, field, value);
9092 return gnat_build_constructor (record_type, v);
9095 /* Process a N_Validate_Unchecked_Conversion node. */
9097 static void
9098 validate_unchecked_conversion (Node_Id gnat_node)
9100 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
9101 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
9103 /* If the target is a pointer type, see if we are either converting from a
9104 non-pointer or from a pointer to a type with a different alias set and
9105 warn if so, unless the pointer has been marked to alias everything. */
9106 if (POINTER_TYPE_P (gnu_target_type)
9107 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
9109 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
9110 ? TREE_TYPE (gnu_source_type)
9111 : NULL_TREE;
9112 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
9113 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9115 if (target_alias_set != 0
9116 && (!POINTER_TYPE_P (gnu_source_type)
9117 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9118 target_alias_set)))
9120 post_error_ne ("?possible aliasing problem for type&",
9121 gnat_node, Target_Type (gnat_node));
9122 post_error ("\\?use -fno-strict-aliasing switch for references",
9123 gnat_node);
9124 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
9125 gnat_node, Target_Type (gnat_node));
9129 /* Likewise if the target is a fat pointer type, but we have no mechanism to
9130 mitigate the problem in this case, so we unconditionally warn. */
9131 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
9133 tree gnu_source_desig_type
9134 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
9135 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
9136 : NULL_TREE;
9137 tree gnu_target_desig_type
9138 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
9139 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9141 if (target_alias_set != 0
9142 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
9143 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9144 target_alias_set)))
9146 post_error_ne ("?possible aliasing problem for type&",
9147 gnat_node, Target_Type (gnat_node));
9148 post_error ("\\?use -fno-strict-aliasing switch for references",
9149 gnat_node);
9154 /* EXP is to be treated as an array or record. Handle the cases when it is
9155 an access object and perform the required dereferences. */
9157 static tree
9158 maybe_implicit_deref (tree exp)
9160 /* If the type is a pointer, dereference it. */
9161 if (POINTER_TYPE_P (TREE_TYPE (exp))
9162 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp)))
9163 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
9165 /* If we got a padded type, remove it too. */
9166 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
9167 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
9169 return exp;
9172 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
9173 location and false if it doesn't. In the former case, set the Gigi global
9174 variable REF_FILENAME to the simple debug file name as given by sinput.
9175 If clear_column is true, set column information to 0. */
9177 static bool
9178 Sloc_to_locus1 (Source_Ptr Sloc, location_t *locus, bool clear_column)
9180 if (Sloc == No_Location)
9181 return false;
9183 if (Sloc <= Standard_Location)
9185 *locus = BUILTINS_LOCATION;
9186 return false;
9188 else
9190 Source_File_Index file = Get_Source_File_Index (Sloc);
9191 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
9192 Column_Number column = (clear_column ? 0 : Get_Column_Number (Sloc));
9193 struct line_map *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
9195 /* We can have zero if pragma Source_Reference is in effect. */
9196 if (line < 1)
9197 line = 1;
9199 /* Translate the location. */
9200 *locus = linemap_position_for_line_and_column (map, line, column);
9203 ref_filename
9204 = IDENTIFIER_POINTER
9205 (get_identifier
9206 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
9208 return true;
9211 /* Similar to the above, not clearing the column information. */
9213 bool
9214 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
9216 return Sloc_to_locus1 (Sloc, locus, false);
9219 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
9220 don't do anything if it doesn't correspond to a source location. */
9222 static void
9223 set_expr_location_from_node1 (tree node, Node_Id gnat_node, bool clear_column)
9225 location_t locus;
9227 if (!Sloc_to_locus1 (Sloc (gnat_node), &locus, clear_column))
9228 return;
9230 SET_EXPR_LOCATION (node, locus);
9233 /* Similar to the above, not clearing the column information. */
9235 static void
9236 set_expr_location_from_node (tree node, Node_Id gnat_node)
9238 set_expr_location_from_node1 (node, gnat_node, false);
9241 /* More elaborate version of set_expr_location_from_node to be used in more
9242 general contexts, for example the result of the translation of a generic
9243 GNAT node. */
9245 static void
9246 set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
9248 /* Set the location information on the node if it is a real expression.
9249 References can be reused for multiple GNAT nodes and they would get
9250 the location information of their last use. Also make sure not to
9251 overwrite an existing location as it is probably more precise. */
9253 switch (TREE_CODE (node))
9255 CASE_CONVERT:
9256 case NON_LVALUE_EXPR:
9257 case SAVE_EXPR:
9258 break;
9260 case COMPOUND_EXPR:
9261 if (EXPR_P (TREE_OPERAND (node, 1)))
9262 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
9264 /* ... fall through ... */
9266 default:
9267 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
9269 set_expr_location_from_node (node, gnat_node);
9270 set_end_locus_from_node (node, gnat_node);
9272 break;
9276 /* Return a colon-separated list of encodings contained in encoded Ada
9277 name. */
9279 static const char *
9280 extract_encoding (const char *name)
9282 char *encoding = (char *) ggc_alloc_atomic (strlen (name));
9283 get_encoding (name, encoding);
9284 return encoding;
9287 /* Extract the Ada name from an encoded name. */
9289 static const char *
9290 decode_name (const char *name)
9292 char *decoded = (char *) ggc_alloc_atomic (strlen (name) * 2 + 60);
9293 __gnat_decode (name, decoded, 0);
9294 return decoded;
9297 /* Post an error message. MSG is the error message, properly annotated.
9298 NODE is the node at which to post the error and the node to use for the
9299 '&' substitution. */
9301 void
9302 post_error (const char *msg, Node_Id node)
9304 String_Template temp;
9305 String_Pointer sp;
9307 if (No (node))
9308 return;
9310 temp.Low_Bound = 1;
9311 temp.High_Bound = strlen (msg);
9312 sp.Bounds = &temp;
9313 sp.Array = msg;
9314 Error_Msg_N (sp, node);
9317 /* Similar to post_error, but NODE is the node at which to post the error and
9318 ENT is the node to use for the '&' substitution. */
9320 void
9321 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
9323 String_Template temp;
9324 String_Pointer sp;
9326 if (No (node))
9327 return;
9329 temp.Low_Bound = 1;
9330 temp.High_Bound = strlen (msg);
9331 sp.Bounds = &temp;
9332 sp.Array = msg;
9333 Error_Msg_NE (sp, node, ent);
9336 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
9338 void
9339 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
9341 Error_Msg_Uint_1 = UI_From_Int (num);
9342 post_error_ne (msg, node, ent);
9345 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
9346 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
9347 most sense. Return true if a sensible assignment was performed. */
9349 static bool
9350 set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
9352 Node_Id gnat_end_label = Empty;
9353 location_t end_locus;
9355 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
9356 end_locus when there is one. We consider only GNAT nodes with a possible
9357 End_Label attached. If the End_Label actually was unassigned, fallback
9358 on the original node. We'd better assign an explicit sloc associated with
9359 the outer construct in any case. */
9361 switch (Nkind (gnat_node))
9363 case N_Package_Body:
9364 case N_Subprogram_Body:
9365 case N_Block_Statement:
9366 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
9367 break;
9369 case N_Package_Declaration:
9370 gnat_end_label = End_Label (Specification (gnat_node));
9371 break;
9373 default:
9374 return false;
9377 gnat_node = Present (gnat_end_label) ? gnat_end_label : gnat_node;
9379 /* Some expanded subprograms have neither an End_Label nor a Sloc
9380 attached. Notify that to callers. For a block statement with no
9381 End_Label, clear column information, so that the tree for a
9382 transient block does not receive the sloc of a source condition. */
9384 if (!Sloc_to_locus1 (Sloc (gnat_node), &end_locus,
9385 No (gnat_end_label) &&
9386 (Nkind (gnat_node) == N_Block_Statement)))
9387 return false;
9389 switch (TREE_CODE (gnu_node))
9391 case BIND_EXPR:
9392 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
9393 return true;
9395 case FUNCTION_DECL:
9396 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
9397 return true;
9399 default:
9400 return false;
9404 /* Similar to post_error_ne, but T is a GCC tree representing the number to
9405 write. If T represents a constant, the text inside curly brackets in
9406 MSG will be output (presumably including a '^'). Otherwise it will not
9407 be output and the text inside square brackets will be output instead. */
9409 void
9410 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
9412 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
9413 char start_yes, end_yes, start_no, end_no;
9414 const char *p;
9415 char *q;
9417 if (TREE_CODE (t) == INTEGER_CST)
9419 Error_Msg_Uint_1 = UI_From_gnu (t);
9420 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
9422 else
9423 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
9425 for (p = msg, q = new_msg; *p; p++)
9427 if (*p == start_yes)
9428 for (p++; *p != end_yes; p++)
9429 *q++ = *p;
9430 else if (*p == start_no)
9431 for (p++; *p != end_no; p++)
9433 else
9434 *q++ = *p;
9437 *q = 0;
9439 post_error_ne (new_msg, node, ent);
9442 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
9444 void
9445 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
9446 int num)
9448 Error_Msg_Uint_2 = UI_From_Int (num);
9449 post_error_ne_tree (msg, node, ent, t);
9452 /* Initialize the table that maps GNAT codes to GCC codes for simple
9453 binary and unary operations. */
9455 static void
9456 init_code_table (void)
9458 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
9459 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
9461 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
9462 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
9463 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
9464 gnu_codes[N_Op_Eq] = EQ_EXPR;
9465 gnu_codes[N_Op_Ne] = NE_EXPR;
9466 gnu_codes[N_Op_Lt] = LT_EXPR;
9467 gnu_codes[N_Op_Le] = LE_EXPR;
9468 gnu_codes[N_Op_Gt] = GT_EXPR;
9469 gnu_codes[N_Op_Ge] = GE_EXPR;
9470 gnu_codes[N_Op_Add] = PLUS_EXPR;
9471 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
9472 gnu_codes[N_Op_Multiply] = MULT_EXPR;
9473 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
9474 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
9475 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
9476 gnu_codes[N_Op_Abs] = ABS_EXPR;
9477 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
9478 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
9479 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
9480 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
9481 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
9482 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
9485 /* Return a label to branch to for the exception type in KIND or NULL_TREE
9486 if none. */
9488 tree
9489 get_exception_label (char kind)
9491 if (kind == N_Raise_Constraint_Error)
9492 return gnu_constraint_error_label_stack->last ();
9493 else if (kind == N_Raise_Storage_Error)
9494 return gnu_storage_error_label_stack->last ();
9495 else if (kind == N_Raise_Program_Error)
9496 return gnu_program_error_label_stack->last ();
9497 else
9498 return NULL_TREE;
9501 /* Return the decl for the current elaboration procedure. */
9503 tree
9504 get_elaboration_procedure (void)
9506 return gnu_elab_proc_stack->last ();
9509 #include "gt-ada-trans.h"