* gcc-interface/trans.c (push_range_check_info): Replace early test
[official-gcc.git] / gcc / ada / gcc-interface / trans.c
blob11aca57239f0cc08b8674a6cfefda414ec31c20c
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 var = remove_conversions (var, false);
2574 if (TREE_CODE (var) != VAR_DECL)
2575 return NULL;
2577 if (decl_function_context (var) != current_function_decl)
2578 return NULL;
2580 gcc_assert (vec_safe_length (gnu_loop_stack) > 0);
2582 for (i = vec_safe_length (gnu_loop_stack) - 1;
2583 vec_safe_iterate (gnu_loop_stack, i, &iter);
2584 i--)
2585 if (var == iter->loop_var)
2586 break;
2588 if (iter)
2590 struct range_check_info_d *rci = ggc_alloc<range_check_info_d> ();
2591 vec_safe_push (iter->checks, rci);
2592 return rci;
2595 return NULL;
2598 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2599 false, or the maximum value if MAX is true, of TYPE. */
2601 static bool
2602 can_equal_min_or_max_val_p (tree val, tree type, bool max)
2604 tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
2606 if (TREE_CODE (min_or_max_val) != INTEGER_CST)
2607 return true;
2609 if (TREE_CODE (val) == NOP_EXPR)
2610 val = (max
2611 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))
2612 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))));
2614 if (TREE_CODE (val) != INTEGER_CST)
2615 return true;
2617 if (max)
2618 return tree_int_cst_lt (val, min_or_max_val) == 0;
2619 else
2620 return tree_int_cst_lt (min_or_max_val, val) == 0;
2623 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2624 If REVERSE is true, minimum value is taken as maximum value. */
2626 static inline bool
2627 can_equal_min_val_p (tree val, tree type, bool reverse)
2629 return can_equal_min_or_max_val_p (val, type, reverse);
2632 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2633 If REVERSE is true, maximum value is taken as minimum value. */
2635 static inline bool
2636 can_equal_max_val_p (tree val, tree type, bool reverse)
2638 return can_equal_min_or_max_val_p (val, type, !reverse);
2641 /* Return true if VAL1 can be lower than VAL2. */
2643 static bool
2644 can_be_lower_p (tree val1, tree val2)
2646 if (TREE_CODE (val1) == NOP_EXPR)
2647 val1 = TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val1, 0)));
2649 if (TREE_CODE (val1) != INTEGER_CST)
2650 return true;
2652 if (TREE_CODE (val2) == NOP_EXPR)
2653 val2 = TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val2, 0)));
2655 if (TREE_CODE (val2) != INTEGER_CST)
2656 return true;
2658 return tree_int_cst_lt (val1, val2);
2661 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
2662 to a GCC tree, which is returned. */
2664 static tree
2665 Loop_Statement_to_gnu (Node_Id gnat_node)
2667 const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
2668 struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> ();
2669 tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE,
2670 NULL_TREE, NULL_TREE, NULL_TREE);
2671 tree gnu_loop_label = create_artificial_label (input_location);
2672 tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2673 tree gnu_result;
2675 /* Push the loop_info structure associated with the LOOP_STMT. */
2676 vec_safe_push (gnu_loop_stack, gnu_loop_info);
2678 /* Set location information for statement and end label. */
2679 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
2680 Sloc_to_locus (Sloc (End_Label (gnat_node)),
2681 &DECL_SOURCE_LOCATION (gnu_loop_label));
2682 LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label;
2684 /* Save the statement for later reuse. */
2685 gnu_loop_info->stmt = gnu_loop_stmt;
2687 /* Set the condition under which the loop must keep going.
2688 For the case "LOOP .... END LOOP;" the condition is always true. */
2689 if (No (gnat_iter_scheme))
2692 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2693 else if (Present (Condition (gnat_iter_scheme)))
2694 LOOP_STMT_COND (gnu_loop_stmt)
2695 = gnat_to_gnu (Condition (gnat_iter_scheme));
2697 /* Otherwise we have an iteration scheme and the condition is given by the
2698 bounds of the subtype of the iteration variable. */
2699 else
2701 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
2702 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2703 Entity_Id gnat_type = Etype (gnat_loop_var);
2704 tree gnu_type = get_unpadded_type (gnat_type);
2705 tree gnu_base_type = get_base_type (gnu_type);
2706 tree gnu_one_node = convert (gnu_base_type, integer_one_node);
2707 tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt;
2708 enum tree_code update_code, test_code, shift_code;
2709 bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false;
2711 gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type));
2712 gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type));
2714 /* We must disable modulo reduction for the iteration variable, if any,
2715 in order for the loop comparison to be effective. */
2716 if (reverse)
2718 gnu_first = gnu_high;
2719 gnu_last = gnu_low;
2720 update_code = MINUS_NOMOD_EXPR;
2721 test_code = GE_EXPR;
2722 shift_code = PLUS_NOMOD_EXPR;
2724 else
2726 gnu_first = gnu_low;
2727 gnu_last = gnu_high;
2728 update_code = PLUS_NOMOD_EXPR;
2729 test_code = LE_EXPR;
2730 shift_code = MINUS_NOMOD_EXPR;
2733 /* We use two different strategies to translate the loop, depending on
2734 whether optimization is enabled.
2736 If it is, we generate the canonical loop form expected by the loop
2737 optimizer and the loop vectorizer, which is the do-while form:
2739 ENTRY_COND
2740 loop:
2741 TOP_UPDATE
2742 BODY
2743 BOTTOM_COND
2744 GOTO loop
2746 This avoids an implicit dependency on loop header copying and makes
2747 it possible to turn BOTTOM_COND into an inequality test.
2749 If optimization is disabled, loop header copying doesn't come into
2750 play and we try to generate the loop form with the fewer conditional
2751 branches. First, the default form, which is:
2753 loop:
2754 TOP_COND
2755 BODY
2756 BOTTOM_UPDATE
2757 GOTO loop
2759 It should catch most loops with constant ending point. Then, if we
2760 cannot, we try to generate the shifted form:
2762 loop:
2763 TOP_COND
2764 TOP_UPDATE
2765 BODY
2766 GOTO loop
2768 which should catch loops with constant starting point. Otherwise, if
2769 we cannot, we generate the fallback form:
2771 ENTRY_COND
2772 loop:
2773 BODY
2774 BOTTOM_COND
2775 BOTTOM_UPDATE
2776 GOTO loop
2778 which works in all cases. */
2780 if (optimize)
2782 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
2783 overflow. */
2784 if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse))
2787 /* Otherwise, use the do-while form with the help of a special
2788 induction variable in the unsigned version of the base type
2789 or the unsigned version of the size type, whichever is the
2790 largest, in order to have wrap-around arithmetics for it. */
2791 else
2793 if (TYPE_PRECISION (gnu_base_type)
2794 > TYPE_PRECISION (size_type_node))
2795 gnu_base_type
2796 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1);
2797 else
2798 gnu_base_type = size_type_node;
2800 gnu_first = convert (gnu_base_type, gnu_first);
2801 gnu_last = convert (gnu_base_type, gnu_last);
2802 gnu_one_node = convert (gnu_base_type, integer_one_node);
2803 use_iv = true;
2806 gnu_first
2807 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2808 gnu_one_node);
2809 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2810 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2812 else
2814 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
2815 if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse))
2818 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
2819 GNU_LAST-1 does. */
2820 else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)
2821 && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse))
2823 gnu_first
2824 = build_binary_op (shift_code, gnu_base_type, gnu_first,
2825 gnu_one_node);
2826 gnu_last
2827 = build_binary_op (shift_code, gnu_base_type, gnu_last,
2828 gnu_one_node);
2829 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
2832 /* Otherwise, use the fallback form. */
2833 else
2834 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
2837 /* If we use the BOTTOM_COND, we can turn the test into an inequality
2838 test but we may have to add ENTRY_COND to protect the empty loop. */
2839 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt))
2841 test_code = NE_EXPR;
2842 if (can_be_lower_p (gnu_high, gnu_low))
2844 gnu_cond_expr
2845 = build3 (COND_EXPR, void_type_node,
2846 build_binary_op (LE_EXPR, boolean_type_node,
2847 gnu_low, gnu_high),
2848 NULL_TREE, alloc_stmt_list ());
2849 set_expr_location_from_node (gnu_cond_expr, gnat_loop_spec);
2853 /* Open a new nesting level that will surround the loop to declare the
2854 iteration variable. */
2855 start_stmt_group ();
2856 gnat_pushlevel ();
2858 /* If we use the special induction variable, create it and set it to
2859 its initial value. Morever, the regular iteration variable cannot
2860 itself be initialized, lest the initial value wrapped around. */
2861 if (use_iv)
2863 gnu_loop_iv
2864 = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var);
2865 add_stmt (gnu_stmt);
2866 gnu_first = NULL_TREE;
2868 else
2869 gnu_loop_iv = NULL_TREE;
2871 /* Declare the iteration variable and set it to its initial value. */
2872 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, 1);
2873 if (DECL_BY_REF_P (gnu_loop_var))
2874 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
2875 else if (use_iv)
2877 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var));
2878 SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv);
2880 gnu_loop_info->loop_var = gnu_loop_var;
2882 /* Do all the arithmetics in the base type. */
2883 gnu_loop_var = convert (gnu_base_type, gnu_loop_var);
2885 /* Set either the top or bottom exit condition. */
2886 if (use_iv)
2887 LOOP_STMT_COND (gnu_loop_stmt)
2888 = build_binary_op (test_code, boolean_type_node, gnu_loop_iv,
2889 gnu_last);
2890 else
2891 LOOP_STMT_COND (gnu_loop_stmt)
2892 = build_binary_op (test_code, boolean_type_node, gnu_loop_var,
2893 gnu_last);
2895 /* Set either the top or bottom update statement and give it the source
2896 location of the iteration for better coverage info. */
2897 if (use_iv)
2899 gnu_stmt
2900 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv,
2901 build_binary_op (update_code, gnu_base_type,
2902 gnu_loop_iv, gnu_one_node));
2903 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2904 append_to_statement_list (gnu_stmt,
2905 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2906 gnu_stmt
2907 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2908 gnu_loop_iv);
2909 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2910 append_to_statement_list (gnu_stmt,
2911 &LOOP_STMT_UPDATE (gnu_loop_stmt));
2913 else
2915 gnu_stmt
2916 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
2917 build_binary_op (update_code, gnu_base_type,
2918 gnu_loop_var, gnu_one_node));
2919 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
2920 LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt;
2924 /* If the loop was named, have the name point to this loop. In this case,
2925 the association is not a DECL node, but the end label of the loop. */
2926 if (Present (Identifier (gnat_node)))
2927 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true);
2929 /* Make the loop body into its own block, so any allocated storage will be
2930 released every iteration. This is needed for stack allocation. */
2931 LOOP_STMT_BODY (gnu_loop_stmt)
2932 = build_stmt_group (Statements (gnat_node), true);
2933 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
2935 /* If we have an iteration scheme, then we are in a statement group. Add
2936 the LOOP_STMT to it, finish it and make it the "loop". */
2937 if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme)))
2939 struct range_check_info_d *rci;
2940 unsigned n_checks = vec_safe_length (gnu_loop_info->checks);
2941 unsigned int i;
2943 /* First, if we have computed a small number of invariant conditions for
2944 range checks applied to the iteration variable, then initialize these
2945 conditions in front of the loop. Otherwise, leave them set to true.
2947 ??? The heuristics need to be improved, by taking into account the
2948 following datapoints:
2949 - loop unswitching is disabled for big loops. The cap is the
2950 parameter PARAM_MAX_UNSWITCH_INSNS (50).
2951 - loop unswitching can only be applied a small number of times
2952 to a given loop. The cap is PARAM_MAX_UNSWITCH_LEVEL (3).
2953 - the front-end quickly generates useless or redundant checks
2954 that can be entirely optimized away in the end. */
2955 if (1 <= n_checks && n_checks <= 4)
2956 for (i = 0;
2957 vec_safe_iterate (gnu_loop_info->checks, i, &rci);
2958 i++)
2960 tree low_ok
2961 = rci->low_bound
2962 ? build_binary_op (GE_EXPR, boolean_type_node,
2963 convert (rci->type, gnu_low),
2964 rci->low_bound)
2965 : boolean_true_node;
2967 tree high_ok
2968 = rci->high_bound
2969 ? build_binary_op (LE_EXPR, boolean_type_node,
2970 convert (rci->type, gnu_high),
2971 rci->high_bound)
2972 : boolean_true_node;
2974 tree range_ok
2975 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
2976 low_ok, high_ok);
2978 TREE_OPERAND (rci->invariant_cond, 0)
2979 = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok);
2981 add_stmt_with_node_force (rci->invariant_cond, gnat_node);
2984 add_stmt (gnu_loop_stmt);
2985 gnat_poplevel ();
2986 gnu_loop_stmt = end_stmt_group ();
2989 /* If we have an outer COND_EXPR, that's our result and this loop is its
2990 "true" statement. Otherwise, the result is the LOOP_STMT. */
2991 if (gnu_cond_expr)
2993 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
2994 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
2995 gnu_result = gnu_cond_expr;
2997 else
2998 gnu_result = gnu_loop_stmt;
3000 gnu_loop_stack->pop ();
3002 return gnu_result;
3005 /* This page implements a form of Named Return Value optimization modelled
3006 on the C++ optimization of the same name. The main difference is that
3007 we disregard any semantical considerations when applying it here, the
3008 counterpart being that we don't try to apply it to semantically loaded
3009 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
3011 We consider a function body of the following GENERIC form:
3013 return_type R1;
3014 [...]
3015 RETURN_EXPR [<retval> = ...]
3016 [...]
3017 RETURN_EXPR [<retval> = R1]
3018 [...]
3019 return_type Ri;
3020 [...]
3021 RETURN_EXPR [<retval> = ...]
3022 [...]
3023 RETURN_EXPR [<retval> = Ri]
3024 [...]
3026 and we try to fulfill a simple criterion that would make it possible to
3027 replace one or several Ri variables with the RESULT_DECL of the function.
3029 The first observation is that RETURN_EXPRs that don't directly reference
3030 any of the Ri variables on the RHS of their assignment are transparent wrt
3031 the optimization. This is because the Ri variables aren't addressable so
3032 any transformation applied to them doesn't affect the RHS; moreover, the
3033 assignment writes the full <retval> object so existing values are entirely
3034 discarded.
3036 This property can be extended to some forms of RETURN_EXPRs that reference
3037 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3038 case, in particular when function calls are involved.
3040 Therefore the algorithm is as follows:
3042 1. Collect the list of candidates for a Named Return Value (Ri variables
3043 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3044 other expressions on the RHS of such assignments.
3046 2. Prune the members of the first list (candidates) that are referenced
3047 by a member of the second list (expressions).
3049 3. Extract a set of candidates with non-overlapping live ranges from the
3050 first list. These are the Named Return Values.
3052 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
3053 Named Return Values in the function with the RESULT_DECL.
3055 If the function returns an unconstrained type, things are a bit different
3056 because the anonymous return object is allocated on the secondary stack
3057 and RESULT_DECL is only a pointer to it. Each return object can be of a
3058 different size and is allocated separately so we need not care about the
3059 aforementioned overlapping issues. Therefore, we don't collect the other
3060 expressions and skip step #2 in the algorithm. */
3062 struct nrv_data
3064 bitmap nrv;
3065 tree result;
3066 Node_Id gnat_ret;
3067 hash_set<tree> *visited;
3070 /* Return true if T is a Named Return Value. */
3072 static inline bool
3073 is_nrv_p (bitmap nrv, tree t)
3075 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
3078 /* Helper function for walk_tree, used by finalize_nrv below. */
3080 static tree
3081 prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3083 struct nrv_data *dp = (struct nrv_data *)data;
3084 tree t = *tp;
3086 /* No need to walk into types or decls. */
3087 if (IS_TYPE_OR_DECL_P (t))
3088 *walk_subtrees = 0;
3090 if (is_nrv_p (dp->nrv, t))
3091 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3093 return NULL_TREE;
3096 /* Prune Named Return Values in BLOCK and return true if there is still a
3097 Named Return Value in BLOCK or one of its sub-blocks. */
3099 static bool
3100 prune_nrv_in_block (bitmap nrv, tree block)
3102 bool has_nrv = false;
3103 tree t;
3105 /* First recurse on the sub-blocks. */
3106 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3107 has_nrv |= prune_nrv_in_block (nrv, t);
3109 /* Then make sure to keep at most one NRV per block. */
3110 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3111 if (is_nrv_p (nrv, t))
3113 if (has_nrv)
3114 bitmap_clear_bit (nrv, DECL_UID (t));
3115 else
3116 has_nrv = true;
3119 return has_nrv;
3122 /* Helper function for walk_tree, used by finalize_nrv below. */
3124 static tree
3125 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3127 struct nrv_data *dp = (struct nrv_data *)data;
3128 tree t = *tp;
3130 /* No need to walk into types. */
3131 if (TYPE_P (t))
3132 *walk_subtrees = 0;
3134 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3135 nop, but differs from using NULL_TREE in that it indicates that we care
3136 about the value of the RESULT_DECL. */
3137 else if (TREE_CODE (t) == RETURN_EXPR
3138 && TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR)
3140 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1), init_expr;
3142 /* If this is the temporary created for a return value with variable
3143 size in Call_to_gnu, we replace the RHS with the init expression. */
3144 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3145 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3146 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3147 == TREE_OPERAND (ret_val, 1))
3149 init_expr = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3150 ret_val = TREE_OPERAND (ret_val, 1);
3152 else
3153 init_expr = NULL_TREE;
3155 /* Strip useless conversions around the return value. */
3156 if (gnat_useless_type_conversion (ret_val))
3157 ret_val = TREE_OPERAND (ret_val, 0);
3159 if (is_nrv_p (dp->nrv, ret_val))
3161 if (init_expr)
3162 TREE_OPERAND (TREE_OPERAND (t, 0), 1) = init_expr;
3163 else
3164 TREE_OPERAND (t, 0) = dp->result;
3168 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3169 if needed. */
3170 else if (TREE_CODE (t) == DECL_EXPR
3171 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3173 tree var = DECL_EXPR_DECL (t), init;
3175 if (DECL_INITIAL (var))
3177 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3178 DECL_INITIAL (var));
3179 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3180 DECL_INITIAL (var) = NULL_TREE;
3182 else
3183 init = build_empty_stmt (EXPR_LOCATION (t));
3184 *tp = init;
3186 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3187 SET_DECL_VALUE_EXPR (var, dp->result);
3188 DECL_HAS_VALUE_EXPR_P (var) = 1;
3189 /* ??? Kludge to avoid an assertion failure during inlining. */
3190 DECL_SIZE (var) = bitsize_unit_node;
3191 DECL_SIZE_UNIT (var) = size_one_node;
3194 /* And replace all uses of NRVs with the RESULT_DECL. */
3195 else if (is_nrv_p (dp->nrv, t))
3196 *tp = convert (TREE_TYPE (t), dp->result);
3198 /* Avoid walking into the same tree more than once. Unfortunately, we
3199 can't just use walk_tree_without_duplicates because it would only
3200 call us for the first occurrence of NRVs in the function body. */
3201 if (dp->visited->add (*tp))
3202 *walk_subtrees = 0;
3204 return NULL_TREE;
3207 /* Likewise, but used when the function returns an unconstrained type. */
3209 static tree
3210 finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3212 struct nrv_data *dp = (struct nrv_data *)data;
3213 tree t = *tp;
3215 /* No need to walk into types. */
3216 if (TYPE_P (t))
3217 *walk_subtrees = 0;
3219 /* We need to see the DECL_EXPR of NRVs before any other references so we
3220 walk the body of BIND_EXPR before walking its variables. */
3221 else if (TREE_CODE (t) == BIND_EXPR)
3222 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3224 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3225 return value built by the allocator instead of the whole construct. */
3226 else if (TREE_CODE (t) == RETURN_EXPR
3227 && TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR)
3229 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3231 /* This is the construct returned by the allocator. */
3232 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3233 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3235 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
3236 ret_val
3237 = (*CONSTRUCTOR_ELTS (TREE_OPERAND (TREE_OPERAND (ret_val, 0),
3238 1)))[1].value;
3239 else
3240 ret_val = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3243 /* Strip useless conversions around the return value. */
3244 if (gnat_useless_type_conversion (ret_val)
3245 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3246 ret_val = TREE_OPERAND (ret_val, 0);
3248 /* Strip unpadding around the return value. */
3249 if (TREE_CODE (ret_val) == COMPONENT_REF
3250 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3251 ret_val = TREE_OPERAND (ret_val, 0);
3253 /* Assign the new return value to the RESULT_DECL. */
3254 if (is_nrv_p (dp->nrv, ret_val))
3255 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3256 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3259 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3260 into a new variable. */
3261 else if (TREE_CODE (t) == DECL_EXPR
3262 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3264 tree saved_current_function_decl = current_function_decl;
3265 tree var = DECL_EXPR_DECL (t);
3266 tree alloc, p_array, new_var, new_ret;
3267 vec<constructor_elt, va_gc> *v;
3268 vec_alloc (v, 2);
3270 /* Create an artificial context to build the allocation. */
3271 current_function_decl = decl_function_context (var);
3272 start_stmt_group ();
3273 gnat_pushlevel ();
3275 /* This will return a COMPOUND_EXPR with the allocation in the first
3276 arm and the final return value in the second arm. */
3277 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3278 TREE_TYPE (dp->result),
3279 Procedure_To_Call (dp->gnat_ret),
3280 Storage_Pool (dp->gnat_ret),
3281 Empty, false);
3283 /* The new variable is built as a reference to the allocated space. */
3284 new_var
3285 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3286 build_reference_type (TREE_TYPE (var)));
3287 DECL_BY_REFERENCE (new_var) = 1;
3289 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3291 /* The new initial value is a COMPOUND_EXPR with the allocation in
3292 the first arm and the value of P_ARRAY in the second arm. */
3293 DECL_INITIAL (new_var)
3294 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3295 TREE_OPERAND (alloc, 0),
3296 (*CONSTRUCTOR_ELTS (TREE_OPERAND (alloc, 1)))[0].value);
3298 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3299 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3300 CONSTRUCTOR_APPEND_ELT (v, p_array,
3301 fold_convert (TREE_TYPE (p_array), new_var));
3302 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
3303 (*CONSTRUCTOR_ELTS (
3304 TREE_OPERAND (alloc, 1)))[1].value);
3305 new_ret = build_constructor (TREE_TYPE (alloc), v);
3307 else
3309 /* The new initial value is just the allocation. */
3310 DECL_INITIAL (new_var) = alloc;
3311 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3314 gnat_pushdecl (new_var, Empty);
3316 /* Destroy the artificial context and insert the new statements. */
3317 gnat_zaplevel ();
3318 *tp = end_stmt_group ();
3319 current_function_decl = saved_current_function_decl;
3321 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3322 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3323 DECL_CHAIN (var) = new_var;
3324 DECL_IGNORED_P (var) = 1;
3326 /* Save the new return value and the dereference of NEW_VAR. */
3327 DECL_INITIAL (var)
3328 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3329 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3330 /* ??? Kludge to avoid messing up during inlining. */
3331 DECL_CONTEXT (var) = NULL_TREE;
3334 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3335 else if (is_nrv_p (dp->nrv, t))
3336 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3338 /* Avoid walking into the same tree more than once. Unfortunately, we
3339 can't just use walk_tree_without_duplicates because it would only
3340 call us for the first occurrence of NRVs in the function body. */
3341 if (dp->visited->add (*tp))
3342 *walk_subtrees = 0;
3344 return NULL_TREE;
3347 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3348 contains the candidates for Named Return Value and OTHER is a list of
3349 the other return values. GNAT_RET is a representative return node. */
3351 static void
3352 finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
3354 struct cgraph_node *node;
3355 struct nrv_data data;
3356 walk_tree_fn func;
3357 unsigned int i;
3358 tree iter;
3360 /* We shouldn't be applying the optimization to return types that we aren't
3361 allowed to manipulate freely. */
3362 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
3364 /* Prune the candidates that are referenced by other return values. */
3365 data.nrv = nrv;
3366 data.result = NULL_TREE;
3367 data.visited = NULL;
3368 for (i = 0; vec_safe_iterate (other, i, &iter); i++)
3369 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3370 if (bitmap_empty_p (nrv))
3371 return;
3373 /* Prune also the candidates that are referenced by nested functions. */
3374 node = cgraph_node::get_create (fndecl);
3375 for (node = node->nested; node; node = node->next_nested)
3376 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), prune_nrv_r,
3377 &data);
3378 if (bitmap_empty_p (nrv))
3379 return;
3381 /* Extract a set of NRVs with non-overlapping live ranges. */
3382 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3383 return;
3385 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3386 data.nrv = nrv;
3387 data.result = DECL_RESULT (fndecl);
3388 data.gnat_ret = gnat_ret;
3389 data.visited = new hash_set<tree>;
3390 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl)))
3391 func = finalize_nrv_unc_r;
3392 else
3393 func = finalize_nrv_r;
3394 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
3395 delete data.visited;
3398 /* Return true if RET_VAL can be used as a Named Return Value for the
3399 anonymous return object RET_OBJ. */
3401 static bool
3402 return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3404 if (TREE_CODE (ret_val) != VAR_DECL)
3405 return false;
3407 if (TREE_THIS_VOLATILE (ret_val))
3408 return false;
3410 if (DECL_CONTEXT (ret_val) != current_function_decl)
3411 return false;
3413 if (TREE_STATIC (ret_val))
3414 return false;
3416 if (TREE_ADDRESSABLE (ret_val))
3417 return false;
3419 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
3420 return false;
3422 return true;
3425 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3426 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3427 around RESULT_OBJ, which may be null in this case. */
3429 static tree
3430 build_return_expr (tree ret_obj, tree ret_val)
3432 tree result_expr;
3434 if (ret_val)
3436 /* The gimplifier explicitly enforces the following invariant:
3438 RETURN_EXPR
3440 MODIFY_EXPR
3443 RET_OBJ ...
3445 As a consequence, type consistency dictates that we use the type
3446 of the RET_OBJ as the operation type. */
3447 tree operation_type = TREE_TYPE (ret_obj);
3449 /* Convert the right operand to the operation type. Note that it's the
3450 same transformation as in the MODIFY_EXPR case of build_binary_op,
3451 with the assumption that the type cannot involve a placeholder. */
3452 if (operation_type != TREE_TYPE (ret_val))
3453 ret_val = convert (operation_type, ret_val);
3455 result_expr = build2 (MODIFY_EXPR, void_type_node, ret_obj, ret_val);
3457 /* If the function returns an aggregate type, find out whether this is
3458 a candidate for Named Return Value. If so, record it. Otherwise,
3459 if this is an expression of some kind, record it elsewhere. */
3460 if (optimize
3461 && AGGREGATE_TYPE_P (operation_type)
3462 && !TYPE_IS_FAT_POINTER_P (operation_type)
3463 && TYPE_MODE (operation_type) == BLKmode
3464 && aggregate_value_p (operation_type, current_function_decl))
3466 /* Recognize the temporary created for a return value with variable
3467 size in Call_to_gnu. We want to eliminate it if possible. */
3468 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3469 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR
3470 && TREE_OPERAND (TREE_OPERAND (ret_val, 0), 0)
3471 == TREE_OPERAND (ret_val, 1))
3472 ret_val = TREE_OPERAND (ret_val, 1);
3474 /* Strip useless conversions around the return value. */
3475 if (gnat_useless_type_conversion (ret_val))
3476 ret_val = TREE_OPERAND (ret_val, 0);
3478 /* Now apply the test to the return value. */
3479 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3481 if (!f_named_ret_val)
3482 f_named_ret_val = BITMAP_GGC_ALLOC ();
3483 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3486 /* Note that we need not care about CONSTRUCTORs here, as they are
3487 totally transparent given the read-compose-write semantics of
3488 assignments from CONSTRUCTORs. */
3489 else if (EXPR_P (ret_val))
3490 vec_safe_push (f_other_ret_val, ret_val);
3493 else
3494 result_expr = ret_obj;
3496 return build1 (RETURN_EXPR, void_type_node, result_expr);
3499 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3500 don't return anything. */
3502 static void
3503 Subprogram_Body_to_gnu (Node_Id gnat_node)
3505 /* Defining identifier of a parameter to the subprogram. */
3506 Entity_Id gnat_param;
3507 /* The defining identifier for the subprogram body. Note that if a
3508 specification has appeared before for this body, then the identifier
3509 occurring in that specification will also be a defining identifier and all
3510 the calls to this subprogram will point to that specification. */
3511 Entity_Id gnat_subprog_id
3512 = (Present (Corresponding_Spec (gnat_node))
3513 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
3514 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3515 tree gnu_subprog_decl;
3516 /* Its RESULT_DECL node. */
3517 tree gnu_result_decl;
3518 /* Its FUNCTION_TYPE node. */
3519 tree gnu_subprog_type;
3520 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3521 tree gnu_cico_list;
3522 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3523 tree gnu_return_var_elmt = NULL_TREE;
3524 tree gnu_result;
3525 location_t locus;
3526 struct language_function *gnu_subprog_language;
3527 vec<parm_attr, va_gc> *cache;
3529 /* If this is a generic object or if it has been eliminated,
3530 ignore it. */
3531 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
3532 || Ekind (gnat_subprog_id) == E_Generic_Function
3533 || Is_Eliminated (gnat_subprog_id))
3534 return;
3536 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3537 the already-elaborated tree node. However, if this subprogram had its
3538 elaboration deferred, we will already have made a tree node for it. So
3539 treat it as not being defined in that case. Such a subprogram cannot
3540 have an address clause or a freeze node, so this test is safe, though it
3541 does disable some otherwise-useful error checking. */
3542 gnu_subprog_decl
3543 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
3544 Acts_As_Spec (gnat_node)
3545 && !present_gnu_tree (gnat_subprog_id));
3546 gnu_result_decl = DECL_RESULT (gnu_subprog_decl);
3547 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
3548 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3549 if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node)
3550 gnu_return_var_elmt = gnu_cico_list;
3552 /* If the function returns by invisible reference, make it explicit in the
3553 function body. See gnat_to_gnu_entity, E_Subprogram_Type case. */
3554 if (TREE_ADDRESSABLE (gnu_subprog_type))
3556 TREE_TYPE (gnu_result_decl)
3557 = build_reference_type (TREE_TYPE (gnu_result_decl));
3558 relayout_decl (gnu_result_decl);
3561 /* Set the line number in the decl to correspond to that of the body. */
3562 Sloc_to_locus (Sloc (gnat_node), &locus);
3563 DECL_SOURCE_LOCATION (gnu_subprog_decl) = locus;
3565 /* Initialize the information structure for the function. */
3566 allocate_struct_function (gnu_subprog_decl, false);
3567 gnu_subprog_language = ggc_cleared_alloc<language_function> ();
3568 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language = gnu_subprog_language;
3569 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->function_start_locus = locus;
3570 set_cfun (NULL);
3572 begin_subprog_body (gnu_subprog_decl);
3574 /* If there are copy-in/copy-out parameters, we need to ensure that they are
3575 properly copied out by the return statement. We do this by making a new
3576 block and converting any return into a goto to a label at the end of the
3577 block. */
3578 if (gnu_cico_list)
3580 tree gnu_return_var = NULL_TREE;
3582 vec_safe_push (gnu_return_label_stack,
3583 create_artificial_label (input_location));
3585 start_stmt_group ();
3586 gnat_pushlevel ();
3588 /* If this is a function with copy-in/copy-out parameters and which does
3589 not return by invisible reference, we also need a variable for the
3590 return value to be placed. */
3591 if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type))
3593 tree gnu_return_type
3594 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3596 gnu_return_var
3597 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
3598 gnu_return_type, NULL_TREE, false, false,
3599 false, false, NULL, gnat_subprog_id);
3600 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3603 vec_safe_push (gnu_return_var_stack, gnu_return_var);
3605 /* See whether there are parameters for which we don't have a GCC tree
3606 yet. These must be Out parameters. Make a VAR_DECL for them and
3607 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3608 We can match up the entries because TYPE_CI_CO_LIST is in the order
3609 of the parameters. */
3610 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3611 Present (gnat_param);
3612 gnat_param = Next_Formal_With_Extras (gnat_param))
3613 if (!present_gnu_tree (gnat_param))
3615 tree gnu_cico_entry = gnu_cico_list;
3616 tree gnu_decl;
3618 /* Skip any entries that have been already filled in; they must
3619 correspond to In Out parameters. */
3620 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3621 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3623 /* Do any needed dereferences for by-ref objects. */
3624 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, 1);
3625 gcc_assert (DECL_P (gnu_decl));
3626 if (DECL_BY_REF_P (gnu_decl))
3627 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3629 /* Do any needed references for padded types. */
3630 TREE_VALUE (gnu_cico_entry)
3631 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
3634 else
3635 vec_safe_push (gnu_return_label_stack, NULL_TREE);
3637 /* Get a tree corresponding to the code for the subprogram. */
3638 start_stmt_group ();
3639 gnat_pushlevel ();
3641 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3643 /* Generate the code of the subprogram itself. A return statement will be
3644 present and any Out parameters will be handled there. */
3645 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3646 gnat_poplevel ();
3647 gnu_result = end_stmt_group ();
3649 /* If we populated the parameter attributes cache, we need to make sure that
3650 the cached expressions are evaluated on all the possible paths leading to
3651 their uses. So we force their evaluation on entry of the function. */
3652 cache = gnu_subprog_language->parm_attr_cache;
3653 if (cache)
3655 struct parm_attr_d *pa;
3656 int i;
3658 start_stmt_group ();
3660 FOR_EACH_VEC_ELT (*cache, i, pa)
3662 if (pa->first)
3663 add_stmt_with_node_force (pa->first, gnat_node);
3664 if (pa->last)
3665 add_stmt_with_node_force (pa->last, gnat_node);
3666 if (pa->length)
3667 add_stmt_with_node_force (pa->length, gnat_node);
3670 add_stmt (gnu_result);
3671 gnu_result = end_stmt_group ();
3673 gnu_subprog_language->parm_attr_cache = NULL;
3676 /* If we are dealing with a return from an Ada procedure with parameters
3677 passed by copy-in/copy-out, we need to return a record containing the
3678 final values of these parameters. If the list contains only one entry,
3679 return just that entry though.
3681 For a full description of the copy-in/copy-out parameter mechanism, see
3682 the part of the gnat_to_gnu_entity routine dealing with the translation
3683 of subprograms.
3685 We need to make a block that contains the definition of that label and
3686 the copying of the return value. It first contains the function, then
3687 the label and copy statement. */
3688 if (gnu_cico_list)
3690 const Node_Id gnat_end_label
3691 = End_Label (Handled_Statement_Sequence (gnat_node));
3693 gnu_return_var_stack->pop ();
3695 add_stmt (gnu_result);
3696 add_stmt (build1 (LABEL_EXPR, void_type_node,
3697 gnu_return_label_stack->last ()));
3699 /* If this is a function which returns by invisible reference, the
3700 return value has already been dealt with at the return statements,
3701 so we only need to indirectly copy out the parameters. */
3702 if (TREE_ADDRESSABLE (gnu_subprog_type))
3704 tree gnu_ret_deref
3705 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl);
3706 tree t;
3708 gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node);
3710 for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t))
3712 tree gnu_field_deref
3713 = build_component_ref (gnu_ret_deref, NULL_TREE,
3714 TREE_PURPOSE (t), true);
3715 gnu_result = build2 (MODIFY_EXPR, void_type_node,
3716 gnu_field_deref, TREE_VALUE (t));
3717 add_stmt_with_node (gnu_result, gnat_end_label);
3721 /* Otherwise, if this is a procedure or a function which does not return
3722 by invisible reference, we can do a direct block-copy out. */
3723 else
3725 tree gnu_retval;
3727 if (list_length (gnu_cico_list) == 1)
3728 gnu_retval = TREE_VALUE (gnu_cico_list);
3729 else
3730 gnu_retval
3731 = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
3732 gnu_cico_list);
3734 gnu_result = build_return_expr (gnu_result_decl, gnu_retval);
3735 add_stmt_with_node (gnu_result, gnat_end_label);
3738 gnat_poplevel ();
3739 gnu_result = end_stmt_group ();
3742 gnu_return_label_stack->pop ();
3744 /* Attempt setting the end_locus of our GCC body tree, typically a
3745 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3746 declaration tree. */
3747 set_end_locus_from_node (gnu_result, gnat_node);
3748 set_end_locus_from_node (gnu_subprog_decl, gnat_node);
3750 /* On SEH targets, install an exception handler around the main entry
3751 point to catch unhandled exceptions. */
3752 if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
3753 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
3755 tree t;
3756 tree etype;
3758 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
3759 1, integer_zero_node);
3760 t = build_call_n_expr (unhandled_except_decl, 1, t);
3762 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
3763 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
3765 t = build2 (CATCH_EXPR, void_type_node, etype, t);
3766 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
3767 gnu_result, t);
3770 end_subprog_body (gnu_result);
3772 /* Finally annotate the parameters and disconnect the trees for parameters
3773 that we have turned into variables since they are now unusable. */
3774 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3775 Present (gnat_param);
3776 gnat_param = Next_Formal_With_Extras (gnat_param))
3778 tree gnu_param = get_gnu_tree (gnat_param);
3779 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
3781 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
3782 DECL_BY_REF_P (gnu_param));
3784 if (is_var_decl)
3785 save_gnu_tree (gnat_param, NULL_TREE, false);
3788 /* Disconnect the variable created for the return value. */
3789 if (gnu_return_var_elmt)
3790 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
3792 /* If the function returns an aggregate type and we have candidates for
3793 a Named Return Value, finalize the optimization. */
3794 if (optimize && gnu_subprog_language->named_ret_val)
3796 finalize_nrv (gnu_subprog_decl,
3797 gnu_subprog_language->named_ret_val,
3798 gnu_subprog_language->other_ret_val,
3799 gnu_subprog_language->gnat_ret);
3800 gnu_subprog_language->named_ret_val = NULL;
3801 gnu_subprog_language->other_ret_val = NULL;
3804 /* If this is an inlined external function that has been marked uninlinable,
3805 drop the body and stop there. Otherwise compile the body. */
3806 if (DECL_EXTERNAL (gnu_subprog_decl) && DECL_UNINLINABLE (gnu_subprog_decl))
3807 DECL_SAVED_TREE (gnu_subprog_decl) = NULL_TREE;
3808 else
3809 rest_of_subprog_body_compilation (gnu_subprog_decl);
3812 /* Return true if GNAT_NODE requires atomic synchronization. */
3814 static bool
3815 atomic_sync_required_p (Node_Id gnat_node)
3817 const Node_Id gnat_parent = Parent (gnat_node);
3818 Node_Kind kind;
3819 unsigned char attr_id;
3821 /* First, scan the node to find the Atomic_Sync_Required flag. */
3822 kind = Nkind (gnat_node);
3823 if (kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
3825 gnat_node = Expression (gnat_node);
3826 kind = Nkind (gnat_node);
3829 switch (kind)
3831 case N_Expanded_Name:
3832 case N_Explicit_Dereference:
3833 case N_Identifier:
3834 case N_Indexed_Component:
3835 case N_Selected_Component:
3836 if (!Atomic_Sync_Required (gnat_node))
3837 return false;
3838 break;
3840 default:
3841 return false;
3844 /* Then, scan the parent to find out cases where the flag is irrelevant. */
3845 kind = Nkind (gnat_parent);
3846 switch (kind)
3848 case N_Attribute_Reference:
3849 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
3850 /* Do not mess up machine code insertions. */
3851 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
3852 return false;
3853 break;
3855 case N_Object_Renaming_Declaration:
3856 /* Do not generate a function call as a renamed object. */
3857 return false;
3859 default:
3860 break;
3863 return true;
3866 /* Create a temporary variable with PREFIX and TYPE, and return it. */
3868 static tree
3869 create_temporary (const char *prefix, tree type)
3871 tree gnu_temp = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
3872 type, NULL_TREE, false, false, false, false,
3873 NULL, Empty);
3874 DECL_ARTIFICIAL (gnu_temp) = 1;
3875 DECL_IGNORED_P (gnu_temp) = 1;
3877 return gnu_temp;
3880 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
3881 Put the initialization statement into GNU_INIT_STMT and annotate it with
3882 the SLOC of GNAT_NODE. Return the temporary variable. */
3884 static tree
3885 create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
3886 Node_Id gnat_node)
3888 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
3890 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
3891 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
3893 return gnu_temp;
3896 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
3897 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
3898 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
3899 If GNU_TARGET is non-null, this must be a function call on the RHS of a
3900 N_Assignment_Statement and the result is to be placed into that object.
3901 If, in addition, ATOMIC_SYNC is true, then the assignment to GNU_TARGET
3902 requires atomic synchronization. */
3904 static tree
3905 Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
3906 bool atomic_sync)
3908 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
3909 const bool returning_value = (function_call && !gnu_target);
3910 /* The GCC node corresponding to the GNAT subprogram name. This can either
3911 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
3912 or an indirect reference expression (an INDIRECT_REF node) pointing to a
3913 subprogram. */
3914 tree gnu_subprog = gnat_to_gnu (Name (gnat_node));
3915 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
3916 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
3917 /* The return type of the FUNCTION_TYPE. */
3918 tree gnu_result_type = TREE_TYPE (gnu_subprog_type);
3919 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog);
3920 vec<tree, va_gc> *gnu_actual_vec = NULL;
3921 tree gnu_name_list = NULL_TREE;
3922 tree gnu_stmt_list = NULL_TREE;
3923 tree gnu_after_list = NULL_TREE;
3924 tree gnu_retval = NULL_TREE;
3925 tree gnu_call, gnu_result;
3926 bool went_into_elab_proc = false;
3927 bool pushed_binding_level = false;
3928 Entity_Id gnat_formal;
3929 Node_Id gnat_actual;
3931 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
3933 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
3934 all our args first. */
3935 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL && DECL_STUBBED_P (gnu_subprog))
3937 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
3938 gnat_node, N_Raise_Program_Error);
3940 for (gnat_actual = First_Actual (gnat_node);
3941 Present (gnat_actual);
3942 gnat_actual = Next_Actual (gnat_actual))
3943 add_stmt (gnat_to_gnu (gnat_actual));
3945 if (returning_value)
3947 *gnu_result_type_p = gnu_result_type;
3948 return build1 (NULL_EXPR, gnu_result_type, call_expr);
3951 return call_expr;
3954 /* For a call to a nested function, check the inlining status. */
3955 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL
3956 && decl_function_context (gnu_subprog))
3957 check_inlining_for_nested_subprog (gnu_subprog);
3959 /* The only way we can be making a call via an access type is if Name is an
3960 explicit dereference. In that case, get the list of formal args from the
3961 type the access type is pointing to. Otherwise, get the formals from the
3962 entity being called. */
3963 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
3964 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
3965 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
3966 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
3967 gnat_formal = Empty;
3968 else
3969 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
3971 /* The lifetime of the temporaries created for the call ends right after the
3972 return value is copied, so we can give them the scope of the elaboration
3973 routine at top level. */
3974 if (!current_function_decl)
3976 current_function_decl = get_elaboration_procedure ();
3977 went_into_elab_proc = true;
3980 /* First, create the temporary for the return value when:
3982 1. There is no target and the function has copy-in/copy-out parameters,
3983 because we need to preserve the return value before copying back the
3984 parameters.
3986 2. There is no target and this is not an object declaration, and the
3987 return type has variable size, because in these cases the gimplifier
3988 cannot create the temporary.
3990 3. There is a target and it is a slice or an array with fixed size,
3991 and the return type has variable size, because the gimplifier
3992 doesn't handle these cases.
3994 This must be done before we push a binding level around the call, since
3995 we will pop it before copying the return value. */
3996 if (function_call
3997 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
3998 || (!gnu_target
3999 && Nkind (Parent (gnat_node)) != N_Object_Declaration
4000 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)
4001 || (gnu_target
4002 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
4003 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
4004 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
4005 == INTEGER_CST))
4006 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)))
4007 gnu_retval = create_temporary ("R", gnu_result_type);
4009 /* Create the list of the actual parameters as GCC expects it, namely a
4010 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
4011 is an expression and the TREE_PURPOSE field is null. But skip Out
4012 parameters not passed by reference and that need not be copied in. */
4013 for (gnat_actual = First_Actual (gnat_node);
4014 Present (gnat_actual);
4015 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4016 gnat_actual = Next_Actual (gnat_actual))
4018 Entity_Id gnat_formal_type = Etype (gnat_formal);
4019 tree gnu_formal = present_gnu_tree (gnat_formal)
4020 ? get_gnu_tree (gnat_formal) : NULL_TREE;
4021 tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type);
4022 const bool is_true_formal_parm
4023 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
4024 const bool is_by_ref_formal_parm
4025 = is_true_formal_parm
4026 && (DECL_BY_REF_P (gnu_formal)
4027 || DECL_BY_COMPONENT_PTR_P (gnu_formal));
4028 /* In the Out or In Out case, we must suppress conversions that yield
4029 an lvalue but can nevertheless cause the creation of a temporary,
4030 because we need the real object in this case, either to pass its
4031 address if it's passed by reference or as target of the back copy
4032 done after the call if it uses the copy-in/copy-out mechanism.
4033 We do it in the In case too, except for an unchecked conversion
4034 to an elementary type or a constrained composite type because it
4035 alone can cause the actual to be misaligned and the addressability
4036 test is applied to the real object. */
4037 const bool suppress_type_conversion
4038 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
4039 && (Ekind (gnat_formal) != E_In_Parameter
4040 || (Is_Composite_Type (Underlying_Type (gnat_formal_type))
4041 && !Is_Constrained (Underlying_Type (gnat_formal_type)))))
4042 || (Nkind (gnat_actual) == N_Type_Conversion
4043 && Is_Composite_Type (Underlying_Type (gnat_formal_type))));
4044 Node_Id gnat_name = suppress_type_conversion
4045 ? Expression (gnat_actual) : gnat_actual;
4046 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
4047 tree gnu_actual;
4049 /* If it's possible we may need to use this expression twice, make sure
4050 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4051 to force side-effects before the call.
4052 ??? This is more conservative than we need since we don't need to do
4053 this for pass-by-ref with no conversion. */
4054 if (Ekind (gnat_formal) != E_In_Parameter)
4055 gnu_name = gnat_stabilize_reference (gnu_name, true, NULL);
4057 /* If we are passing a non-addressable parameter by reference, pass the
4058 address of a copy. In the Out or In Out case, set up to copy back
4059 out after the call. */
4060 if (is_by_ref_formal_parm
4061 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
4062 && !addressable_p (gnu_name, gnu_name_type))
4064 bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
4065 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4067 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4068 but sort of an instantiation for them. */
4069 if (TREE_CODE (gnu_name) == CONSTRUCTOR)
4072 /* If the type is passed by reference, a copy is not allowed. */
4073 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type))
4074 post_error ("misaligned actual cannot be passed by reference",
4075 gnat_actual);
4077 /* For users of Starlet we issue a warning because the interface
4078 apparently assumes that by-ref parameters outlive the procedure
4079 invocation. The code still will not work as intended, but we
4080 cannot do much better since low-level parts of the back-end
4081 would allocate temporaries at will because of the misalignment
4082 if we did not do so here. */
4083 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
4085 post_error
4086 ("?possible violation of implicit assumption", gnat_actual);
4087 post_error_ne
4088 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
4089 Entity (Name (gnat_node)));
4090 post_error_ne ("?because of misalignment of &", gnat_actual,
4091 gnat_formal);
4094 /* If the actual type of the object is already the nominal type,
4095 we have nothing to do, except if the size is self-referential
4096 in which case we'll remove the unpadding below. */
4097 if (TREE_TYPE (gnu_name) == gnu_name_type
4098 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4101 /* Otherwise remove the unpadding from all the objects. */
4102 else if (TREE_CODE (gnu_name) == COMPONENT_REF
4103 && TYPE_IS_PADDING_P
4104 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
4105 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
4107 /* Otherwise convert to the nominal type of the object if needed.
4108 There are several cases in which we need to make the temporary
4109 using this type instead of the actual type of the object when
4110 they are distinct, because the expectations of the callee would
4111 otherwise not be met:
4112 - if it's a justified modular type,
4113 - if the actual type is a smaller form of it,
4114 - if it's a smaller form of the actual type. */
4115 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4116 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4117 || smaller_form_type_p (TREE_TYPE (gnu_name),
4118 gnu_name_type)))
4119 || (INTEGRAL_TYPE_P (gnu_name_type)
4120 && smaller_form_type_p (gnu_name_type,
4121 TREE_TYPE (gnu_name))))
4122 gnu_name = convert (gnu_name_type, gnu_name);
4124 /* If this is an In Out or Out parameter and we're returning a value,
4125 we need to create a temporary for the return value because we must
4126 preserve it before copying back at the very end. */
4127 if (!in_param && returning_value && !gnu_retval)
4128 gnu_retval = create_temporary ("R", gnu_result_type);
4130 /* If we haven't pushed a binding level, push a new one. This will
4131 narrow the lifetime of the temporary we are about to make as much
4132 as possible. The drawback is that we'd need to create a temporary
4133 for the return value, if any (see comment before the loop). So do
4134 it only when this temporary was already created just above. */
4135 if (!pushed_binding_level && !(in_param && returning_value))
4137 start_stmt_group ();
4138 gnat_pushlevel ();
4139 pushed_binding_level = true;
4142 /* Create an explicit temporary holding the copy. */
4143 gnu_temp
4144 = create_init_temporary ("A", gnu_name, &gnu_stmt, gnat_actual);
4146 /* But initialize it on the fly like for an implicit temporary as
4147 we aren't necessarily having a statement list. */
4148 gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt,
4149 gnu_temp);
4151 /* Set up to move the copy back to the original if needed. */
4152 if (!in_param)
4154 /* If the original is a COND_EXPR whose first arm isn't meant to
4155 be further used, just deal with the second arm. This is very
4156 likely the conditional expression built for a check. */
4157 if (TREE_CODE (gnu_orig) == COND_EXPR
4158 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4159 && integer_zerop
4160 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4161 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4163 gnu_stmt
4164 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp);
4165 set_expr_location_from_node (gnu_stmt, gnat_node);
4167 append_to_statement_list (gnu_stmt, &gnu_after_list);
4171 /* Start from the real object and build the actual. */
4172 gnu_actual = gnu_name;
4174 /* If this is an atomic access of an In or In Out parameter for which
4175 synchronization is required, build the atomic load. */
4176 if (is_true_formal_parm
4177 && !is_by_ref_formal_parm
4178 && Ekind (gnat_formal) != E_Out_Parameter
4179 && atomic_sync_required_p (gnat_actual))
4180 gnu_actual = build_atomic_load (gnu_actual);
4182 /* If this was a procedure call, we may not have removed any padding.
4183 So do it here for the part we will use as an input, if any. */
4184 if (Ekind (gnat_formal) != E_Out_Parameter
4185 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4186 gnu_actual
4187 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4189 /* Put back the conversion we suppressed above in the computation of the
4190 real object. And even if we didn't suppress any conversion there, we
4191 may have suppressed a conversion to the Etype of the actual earlier,
4192 since the parent is a procedure call, so put it back here. */
4193 if (suppress_type_conversion
4194 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4195 gnu_actual
4196 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual)),
4197 gnu_actual, No_Truncation (gnat_actual));
4198 else
4199 gnu_actual
4200 = convert (gnat_to_gnu_type (Etype (gnat_actual)), gnu_actual);
4202 /* Make sure that the actual is in range of the formal's type. */
4203 if (Ekind (gnat_formal) != E_Out_Parameter
4204 && Do_Range_Check (gnat_actual))
4205 gnu_actual
4206 = emit_range_check (gnu_actual, gnat_formal_type, gnat_actual);
4208 /* Unless this is an In parameter, we must remove any justified modular
4209 building from GNU_NAME to get an lvalue. */
4210 if (Ekind (gnat_formal) != E_In_Parameter
4211 && TREE_CODE (gnu_name) == CONSTRUCTOR
4212 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
4213 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
4214 gnu_name
4215 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))), gnu_name);
4217 /* First see if the parameter is passed by reference. */
4218 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
4220 if (Ekind (gnat_formal) != E_In_Parameter)
4222 /* In Out or Out parameters passed by reference don't use the
4223 copy-in/copy-out mechanism so the address of the real object
4224 must be passed to the function. */
4225 gnu_actual = gnu_name;
4227 /* If we have a padded type, be sure we've removed padding. */
4228 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4229 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
4230 gnu_actual);
4232 /* If we have the constructed subtype of an aliased object
4233 with an unconstrained nominal subtype, the type of the
4234 actual includes the template, although it is formally
4235 constrained. So we need to convert it back to the real
4236 constructed subtype to retrieve the constrained part
4237 and takes its address. */
4238 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4239 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
4240 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
4241 && Is_Array_Type (Underlying_Type (Etype (gnat_actual))))
4242 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4243 gnu_actual);
4246 /* There is no need to convert the actual to the formal's type before
4247 taking its address. The only exception is for unconstrained array
4248 types because of the way we build fat pointers. */
4249 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4251 /* Put back a view conversion for In Out or Out parameters. */
4252 if (Ekind (gnat_formal) != E_In_Parameter)
4253 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4254 gnu_actual);
4255 gnu_actual = convert (gnu_formal_type, gnu_actual);
4258 /* The symmetry of the paths to the type of an entity is broken here
4259 since arguments don't know that they will be passed by ref. */
4260 gnu_formal_type = TREE_TYPE (gnu_formal);
4261 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4264 /* Then see if the parameter is an array passed to a foreign convention
4265 subprogram. */
4266 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
4268 gnu_formal_type = TREE_TYPE (gnu_formal);
4269 gnu_actual = maybe_implicit_deref (gnu_actual);
4270 gnu_actual = maybe_unconstrained_array (gnu_actual);
4272 if (TYPE_IS_PADDING_P (gnu_formal_type))
4274 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
4275 gnu_actual = convert (gnu_formal_type, gnu_actual);
4278 /* Take the address of the object and convert to the proper pointer
4279 type. We'd like to actually compute the address of the beginning
4280 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
4281 possibility that the ARRAY_REF might return a constant and we'd be
4282 getting the wrong address. Neither approach is exactly correct,
4283 but this is the most likely to work in all cases. */
4284 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4287 /* Otherwise the parameter is passed by copy. */
4288 else
4290 tree gnu_size;
4292 if (Ekind (gnat_formal) != E_In_Parameter)
4293 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
4295 /* If we didn't create a PARM_DECL for the formal, this means that
4296 it is an Out parameter not passed by reference and that need not
4297 be copied in. In this case, the value of the actual need not be
4298 read. However, we still need to make sure that its side-effects
4299 are evaluated before the call, so we evaluate its address. */
4300 if (!is_true_formal_parm)
4302 if (TREE_SIDE_EFFECTS (gnu_name))
4304 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
4305 append_to_statement_list (addr, &gnu_stmt_list);
4307 continue;
4310 gnu_actual = convert (gnu_formal_type, gnu_actual);
4312 /* If this is 'Null_Parameter, pass a zero even though we are
4313 dereferencing it. */
4314 if (TREE_CODE (gnu_actual) == INDIRECT_REF
4315 && TREE_PRIVATE (gnu_actual)
4316 && (gnu_size = TYPE_SIZE (TREE_TYPE (gnu_actual)))
4317 && TREE_CODE (gnu_size) == INTEGER_CST
4318 && compare_tree_int (gnu_size, BITS_PER_WORD) <= 0)
4319 gnu_actual
4320 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
4321 convert (gnat_type_for_size
4322 (TREE_INT_CST_LOW (gnu_size), 1),
4323 integer_zero_node),
4324 false);
4325 else
4326 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
4329 vec_safe_push (gnu_actual_vec, gnu_actual);
4332 gnu_call
4333 = build_call_vec (gnu_result_type, gnu_subprog_addr, gnu_actual_vec);
4334 set_expr_location_from_node (gnu_call, gnat_node);
4336 /* If we have created a temporary for the return value, initialize it. */
4337 if (gnu_retval)
4339 tree gnu_stmt
4340 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
4341 set_expr_location_from_node (gnu_stmt, gnat_node);
4342 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4343 gnu_call = gnu_retval;
4346 /* If this is a subprogram with copy-in/copy-out parameters, we need to
4347 unpack the valued returned from the function into the In Out or Out
4348 parameters. We deal with the function return (if this is an Ada
4349 function) below. */
4350 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4352 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
4353 copy-out parameters. */
4354 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
4355 const int length = list_length (gnu_cico_list);
4357 /* The call sequence must contain one and only one call, even though the
4358 function is pure. Save the result into a temporary if needed. */
4359 if (length > 1)
4361 if (!gnu_retval)
4363 tree gnu_stmt;
4364 /* If we haven't pushed a binding level, push a new one. This
4365 will narrow the lifetime of the temporary we are about to
4366 make as much as possible. */
4367 if (!pushed_binding_level)
4369 start_stmt_group ();
4370 gnat_pushlevel ();
4371 pushed_binding_level = true;
4373 gnu_call
4374 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
4375 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4378 gnu_name_list = nreverse (gnu_name_list);
4381 /* The first entry is for the actual return value if this is a
4382 function, so skip it. */
4383 if (function_call)
4384 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4386 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4387 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4388 else
4389 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4391 for (gnat_actual = First_Actual (gnat_node);
4392 Present (gnat_actual);
4393 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4394 gnat_actual = Next_Actual (gnat_actual))
4395 /* If we are dealing with a copy-in/copy-out parameter, we must
4396 retrieve its value from the record returned in the call. */
4397 if (!(present_gnu_tree (gnat_formal)
4398 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4399 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
4400 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))))
4401 && Ekind (gnat_formal) != E_In_Parameter)
4403 /* Get the value to assign to this Out or In Out parameter. It is
4404 either the result of the function if there is only a single such
4405 parameter or the appropriate field from the record returned. */
4406 tree gnu_result
4407 = length == 1
4408 ? gnu_call
4409 : build_component_ref (gnu_call, NULL_TREE,
4410 TREE_PURPOSE (gnu_cico_list), false);
4412 /* If the actual is a conversion, get the inner expression, which
4413 will be the real destination, and convert the result to the
4414 type of the actual parameter. */
4415 tree gnu_actual
4416 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
4418 /* If the result is a padded type, remove the padding. */
4419 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4420 gnu_result
4421 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4422 gnu_result);
4424 /* If the actual is a type conversion, the real target object is
4425 denoted by the inner Expression and we need to convert the
4426 result to the associated type.
4427 We also need to convert our gnu assignment target to this type
4428 if the corresponding GNU_NAME was constructed from the GNAT
4429 conversion node and not from the inner Expression. */
4430 if (Nkind (gnat_actual) == N_Type_Conversion)
4432 gnu_result
4433 = convert_with_check
4434 (Etype (Expression (gnat_actual)), gnu_result,
4435 Do_Overflow_Check (gnat_actual),
4436 Do_Range_Check (Expression (gnat_actual)),
4437 Float_Truncate (gnat_actual), gnat_actual);
4439 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
4440 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
4443 /* Unchecked conversions as actuals for Out parameters are not
4444 allowed in user code because they are not variables, but do
4445 occur in front-end expansions. The associated GNU_NAME is
4446 always obtained from the inner expression in such cases. */
4447 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4448 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
4449 gnu_result,
4450 No_Truncation (gnat_actual));
4451 else
4453 if (Do_Range_Check (gnat_actual))
4454 gnu_result
4455 = emit_range_check (gnu_result, Etype (gnat_actual),
4456 gnat_actual);
4458 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
4459 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
4460 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
4463 if (atomic_sync_required_p (gnat_actual))
4464 gnu_result = build_atomic_store (gnu_actual, gnu_result);
4465 else
4466 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4467 gnu_actual, gnu_result);
4468 set_expr_location_from_node (gnu_result, gnat_node);
4469 append_to_statement_list (gnu_result, &gnu_stmt_list);
4470 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4471 gnu_name_list = TREE_CHAIN (gnu_name_list);
4475 /* If this is a function call, the result is the call expression unless a
4476 target is specified, in which case we copy the result into the target
4477 and return the assignment statement. */
4478 if (function_call)
4480 /* If this is a function with copy-in/copy-out parameters, extract the
4481 return value from it and update the return type. */
4482 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4484 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
4485 gnu_call = build_component_ref (gnu_call, NULL_TREE,
4486 TREE_PURPOSE (gnu_elmt), false);
4487 gnu_result_type = TREE_TYPE (gnu_call);
4490 /* If the function returns an unconstrained array or by direct reference,
4491 we have to dereference the pointer. */
4492 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type)
4493 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
4494 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
4496 if (gnu_target)
4498 Node_Id gnat_parent = Parent (gnat_node);
4499 enum tree_code op_code;
4501 /* If range check is needed, emit code to generate it. */
4502 if (Do_Range_Check (gnat_node))
4503 gnu_call
4504 = emit_range_check (gnu_call, Etype (Name (gnat_parent)),
4505 gnat_parent);
4507 /* ??? If the return type has variable size, then force the return
4508 slot optimization as we would not be able to create a temporary.
4509 Likewise if it was unconstrained as we would copy too much data.
4510 That's what has been done historically. */
4511 if (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
4512 || (TYPE_IS_PADDING_P (gnu_result_type)
4513 && CONTAINS_PLACEHOLDER_P
4514 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_result_type))))))
4515 op_code = INIT_EXPR;
4516 else
4517 op_code = MODIFY_EXPR;
4519 if (atomic_sync)
4520 gnu_call = build_atomic_store (gnu_target, gnu_call);
4521 else
4522 gnu_call
4523 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
4524 set_expr_location_from_node (gnu_call, gnat_parent);
4525 append_to_statement_list (gnu_call, &gnu_stmt_list);
4527 else
4528 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
4531 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4532 parameters, the result is just the call statement. */
4533 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
4534 append_to_statement_list (gnu_call, &gnu_stmt_list);
4536 /* Finally, add the copy back statements, if any. */
4537 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
4539 if (went_into_elab_proc)
4540 current_function_decl = NULL_TREE;
4542 /* If we have pushed a binding level, pop it and finish up the enclosing
4543 statement group. */
4544 if (pushed_binding_level)
4546 add_stmt (gnu_stmt_list);
4547 gnat_poplevel ();
4548 gnu_result = end_stmt_group ();
4551 /* Otherwise, retrieve the statement list, if any. */
4552 else if (gnu_stmt_list)
4553 gnu_result = gnu_stmt_list;
4555 /* Otherwise, just return the call expression. */
4556 else
4557 return gnu_call;
4559 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4560 But first simplify if we have only one statement in the list. */
4561 if (returning_value)
4563 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
4564 if (first == last)
4565 gnu_result = first;
4566 gnu_result
4567 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
4570 return gnu_result;
4573 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4574 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4576 static tree
4577 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
4579 tree gnu_jmpsave_decl = NULL_TREE;
4580 tree gnu_jmpbuf_decl = NULL_TREE;
4581 /* If just annotating, ignore all EH and cleanups. */
4582 bool gcc_zcx = (!type_annotate_only
4583 && Present (Exception_Handlers (gnat_node))
4584 && Exception_Mechanism == Back_End_Exceptions);
4585 bool setjmp_longjmp
4586 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
4587 && Exception_Mechanism == Setjmp_Longjmp);
4588 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
4589 bool binding_for_block = (at_end || gcc_zcx || setjmp_longjmp);
4590 tree gnu_inner_block; /* The statement(s) for the block itself. */
4591 tree gnu_result;
4592 tree gnu_expr;
4593 Node_Id gnat_temp;
4594 /* Node providing the sloc for the cleanup actions. */
4595 Node_Id gnat_cleanup_loc_node = (Present (End_Label (gnat_node)) ?
4596 End_Label (gnat_node) :
4597 gnat_node);
4599 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
4600 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
4601 add_cleanup, and when we leave the binding, end_stmt_group will create
4602 the TRY_FINALLY_EXPR.
4604 ??? The region level calls down there have been specifically put in place
4605 for a ZCX context and currently the order in which things are emitted
4606 (region/handlers) is different from the SJLJ case. Instead of putting
4607 other calls with different conditions at other places for the SJLJ case,
4608 it seems cleaner to reorder things for the SJLJ case and generalize the
4609 condition to make it not ZCX specific.
4611 If there are any exceptions or cleanup processing involved, we need an
4612 outer statement group (for Setjmp_Longjmp) and binding level. */
4613 if (binding_for_block)
4615 start_stmt_group ();
4616 gnat_pushlevel ();
4619 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
4620 area for address of previous buffer. Do this first since we need to have
4621 the setjmp buf known for any decls in this block. */
4622 if (setjmp_longjmp)
4624 gnu_jmpsave_decl
4625 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
4626 jmpbuf_ptr_type,
4627 build_call_n_expr (get_jmpbuf_decl, 0),
4628 false, false, false, false, NULL, gnat_node);
4629 DECL_ARTIFICIAL (gnu_jmpsave_decl) = 1;
4631 /* The __builtin_setjmp receivers will immediately reinstall it. Now
4632 because of the unstructured form of EH used by setjmp_longjmp, there
4633 might be forward edges going to __builtin_setjmp receivers on which
4634 it is uninitialized, although they will never be actually taken. */
4635 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
4636 gnu_jmpbuf_decl
4637 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE,
4638 jmpbuf_type,
4639 NULL_TREE,
4640 false, false, false, false, NULL, gnat_node);
4641 DECL_ARTIFICIAL (gnu_jmpbuf_decl) = 1;
4643 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
4645 /* When we exit this block, restore the saved value. */
4646 add_cleanup (build_call_n_expr (set_jmpbuf_decl, 1, gnu_jmpsave_decl),
4647 gnat_cleanup_loc_node);
4650 /* If we are to call a function when exiting this block, add a cleanup
4651 to the binding level we made above. Note that add_cleanup is FIFO
4652 so we must register this cleanup after the EH cleanup just above. */
4653 if (at_end)
4654 add_cleanup (build_call_n_expr (gnat_to_gnu (At_End_Proc (gnat_node)), 0),
4655 gnat_cleanup_loc_node);
4657 /* Now build the tree for the declarations and statements inside this block.
4658 If this is SJLJ, set our jmp_buf as the current buffer. */
4659 start_stmt_group ();
4661 if (setjmp_longjmp)
4663 gnu_expr = build_call_n_expr (set_jmpbuf_decl, 1,
4664 build_unary_op (ADDR_EXPR, NULL_TREE,
4665 gnu_jmpbuf_decl));
4666 set_expr_location_from_node (gnu_expr, gnat_node);
4667 add_stmt (gnu_expr);
4670 if (Present (First_Real_Statement (gnat_node)))
4671 process_decls (Statements (gnat_node), Empty,
4672 First_Real_Statement (gnat_node), true, true);
4674 /* Generate code for each statement in the block. */
4675 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
4676 ? First_Real_Statement (gnat_node)
4677 : First (Statements (gnat_node)));
4678 Present (gnat_temp); gnat_temp = Next (gnat_temp))
4679 add_stmt (gnat_to_gnu (gnat_temp));
4680 gnu_inner_block = end_stmt_group ();
4682 /* Now generate code for the two exception models, if either is relevant for
4683 this block. */
4684 if (setjmp_longjmp)
4686 tree *gnu_else_ptr = 0;
4687 tree gnu_handler;
4689 /* Make a binding level for the exception handling declarations and code
4690 and set up gnu_except_ptr_stack for the handlers to use. */
4691 start_stmt_group ();
4692 gnat_pushlevel ();
4694 vec_safe_push (gnu_except_ptr_stack,
4695 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE,
4696 build_pointer_type (except_type_node),
4697 build_call_n_expr (get_excptr_decl, 0),
4698 false, false, false, false,
4699 NULL, gnat_node));
4701 /* Generate code for each handler. The N_Exception_Handler case does the
4702 real work and returns a COND_EXPR for each handler, which we chain
4703 together here. */
4704 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4705 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
4707 gnu_expr = gnat_to_gnu (gnat_temp);
4709 /* If this is the first one, set it as the outer one. Otherwise,
4710 point the "else" part of the previous handler to us. Then point
4711 to our "else" part. */
4712 if (!gnu_else_ptr)
4713 add_stmt (gnu_expr);
4714 else
4715 *gnu_else_ptr = gnu_expr;
4717 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
4720 /* If none of the exception handlers did anything, re-raise but do not
4721 defer abortion. */
4722 gnu_expr = build_call_n_expr (raise_nodefer_decl, 1,
4723 gnu_except_ptr_stack->last ());
4724 set_expr_location_from_node
4725 (gnu_expr,
4726 Present (End_Label (gnat_node)) ? End_Label (gnat_node) : gnat_node);
4728 if (gnu_else_ptr)
4729 *gnu_else_ptr = gnu_expr;
4730 else
4731 add_stmt (gnu_expr);
4733 /* End the binding level dedicated to the exception handlers and get the
4734 whole statement group. */
4735 gnu_except_ptr_stack->pop ();
4736 gnat_poplevel ();
4737 gnu_handler = end_stmt_group ();
4739 /* If the setjmp returns 1, we restore our incoming longjmp value and
4740 then check the handlers. */
4741 start_stmt_group ();
4742 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl, 1,
4743 gnu_jmpsave_decl),
4744 gnat_node);
4745 add_stmt (gnu_handler);
4746 gnu_handler = end_stmt_group ();
4748 /* This block is now "if (setjmp) ... <handlers> else <block>". */
4749 gnu_result = build3 (COND_EXPR, void_type_node,
4750 (build_call_n_expr
4751 (setjmp_decl, 1,
4752 build_unary_op (ADDR_EXPR, NULL_TREE,
4753 gnu_jmpbuf_decl))),
4754 gnu_handler, gnu_inner_block);
4756 else if (gcc_zcx)
4758 tree gnu_handlers;
4759 location_t locus;
4761 /* First make a block containing the handlers. */
4762 start_stmt_group ();
4763 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
4764 Present (gnat_temp);
4765 gnat_temp = Next_Non_Pragma (gnat_temp))
4766 add_stmt (gnat_to_gnu (gnat_temp));
4767 gnu_handlers = end_stmt_group ();
4769 /* Now make the TRY_CATCH_EXPR for the block. */
4770 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
4771 gnu_inner_block, gnu_handlers);
4772 /* Set a location. We need to find a unique location for the dispatching
4773 code, otherwise we can get coverage or debugging issues. Try with
4774 the location of the end label. */
4775 if (Present (End_Label (gnat_node))
4776 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
4777 SET_EXPR_LOCATION (gnu_result, locus);
4778 else
4779 /* Clear column information so that the exception handler of an
4780 implicit transient block does not incorrectly inherit the slocs
4781 of a decision, which would otherwise confuse control flow based
4782 coverage analysis tools. */
4783 set_expr_location_from_node1 (gnu_result, gnat_node, true);
4785 else
4786 gnu_result = gnu_inner_block;
4788 /* Now close our outer block, if we had to make one. */
4789 if (binding_for_block)
4791 add_stmt (gnu_result);
4792 gnat_poplevel ();
4793 gnu_result = end_stmt_group ();
4796 return gnu_result;
4799 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4800 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
4801 exception handling. */
4803 static tree
4804 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node)
4806 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
4807 an "if" statement to select the proper exceptions. For "Others", exclude
4808 exceptions where Handled_By_Others is nonzero unless the All_Others flag
4809 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
4810 tree gnu_choice = boolean_false_node;
4811 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
4812 Node_Id gnat_temp;
4814 for (gnat_temp = First (Exception_Choices (gnat_node));
4815 gnat_temp; gnat_temp = Next (gnat_temp))
4817 tree this_choice;
4819 if (Nkind (gnat_temp) == N_Others_Choice)
4821 if (All_Others (gnat_temp))
4822 this_choice = boolean_true_node;
4823 else
4824 this_choice
4825 = build_binary_op
4826 (EQ_EXPR, boolean_type_node,
4827 convert
4828 (integer_type_node,
4829 build_component_ref
4830 (build_unary_op
4831 (INDIRECT_REF, NULL_TREE,
4832 gnu_except_ptr_stack->last ()),
4833 get_identifier ("not_handled_by_others"), NULL_TREE,
4834 false)),
4835 integer_zero_node);
4838 else if (Nkind (gnat_temp) == N_Identifier
4839 || Nkind (gnat_temp) == N_Expanded_Name)
4841 Entity_Id gnat_ex_id = Entity (gnat_temp);
4842 tree gnu_expr;
4844 /* Exception may be a renaming. Recover original exception which is
4845 the one elaborated and registered. */
4846 if (Present (Renamed_Object (gnat_ex_id)))
4847 gnat_ex_id = Renamed_Object (gnat_ex_id);
4849 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4851 this_choice
4852 = build_binary_op
4853 (EQ_EXPR, boolean_type_node,
4854 gnu_except_ptr_stack->last (),
4855 convert (TREE_TYPE (gnu_except_ptr_stack->last ()),
4856 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
4858 else
4859 gcc_unreachable ();
4861 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
4862 gnu_choice, this_choice);
4865 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
4868 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4869 to a GCC tree, which is returned. This is the variant for ZCX. */
4871 static tree
4872 Exception_Handler_to_gnu_zcx (Node_Id gnat_node)
4874 tree gnu_etypes_list = NULL_TREE;
4875 tree gnu_current_exc_ptr, prev_gnu_incoming_exc_ptr;
4876 Node_Id gnat_temp;
4878 /* We build a TREE_LIST of nodes representing what exception types this
4879 handler can catch, with special cases for others and all others cases.
4881 Each exception type is actually identified by a pointer to the exception
4882 id, or to a dummy object for "others" and "all others". */
4883 for (gnat_temp = First (Exception_Choices (gnat_node));
4884 gnat_temp; gnat_temp = Next (gnat_temp))
4886 tree gnu_expr, gnu_etype;
4888 if (Nkind (gnat_temp) == N_Others_Choice)
4890 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
4891 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4893 else if (Nkind (gnat_temp) == N_Identifier
4894 || Nkind (gnat_temp) == N_Expanded_Name)
4896 Entity_Id gnat_ex_id = Entity (gnat_temp);
4898 /* Exception may be a renaming. Recover original exception which is
4899 the one elaborated and registered. */
4900 if (Present (Renamed_Object (gnat_ex_id)))
4901 gnat_ex_id = Renamed_Object (gnat_ex_id);
4903 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, 0);
4904 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
4906 else
4907 gcc_unreachable ();
4909 /* The GCC interface expects NULL to be passed for catch all handlers, so
4910 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
4911 is integer_zero_node. It would not work, however, because GCC's
4912 notion of "catch all" is stronger than our notion of "others". Until
4913 we correctly use the cleanup interface as well, doing that would
4914 prevent the "all others" handlers from being seen, because nothing
4915 can be caught beyond a catch all from GCC's point of view. */
4916 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
4919 start_stmt_group ();
4920 gnat_pushlevel ();
4922 /* Expand a call to the begin_handler hook at the beginning of the handler,
4923 and arrange for a call to the end_handler hook to occur on every possible
4924 exit path.
4926 The hooks expect a pointer to the low level occurrence. This is required
4927 for our stack management scheme because a raise inside the handler pushes
4928 a new occurrence on top of the stack, which means that this top does not
4929 necessarily match the occurrence this handler was dealing with.
4931 __builtin_eh_pointer references the exception occurrence being
4932 propagated. Upon handler entry, this is the exception for which the
4933 handler is triggered. This might not be the case upon handler exit,
4934 however, as we might have a new occurrence propagated by the handler's
4935 body, and the end_handler hook called as a cleanup in this context.
4937 We use a local variable to retrieve the incoming value at handler entry
4938 time, and reuse it to feed the end_handler hook's argument at exit. */
4940 gnu_current_exc_ptr
4941 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
4942 1, integer_zero_node);
4943 prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
4944 gnu_incoming_exc_ptr = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
4945 ptr_type_node, gnu_current_exc_ptr,
4946 false, false, false, false,
4947 NULL, gnat_node);
4949 add_stmt_with_node (build_call_n_expr (begin_handler_decl, 1,
4950 gnu_incoming_exc_ptr),
4951 gnat_node);
4953 /* Declare and initialize the choice parameter, if present. */
4954 if (Present (Choice_Parameter (gnat_node)))
4956 tree gnu_param
4957 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, 1);
4959 add_stmt (build_call_n_expr
4960 (set_exception_parameter_decl, 2,
4961 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
4962 gnu_incoming_exc_ptr));
4965 /* We don't have an End_Label at hand to set the location of the cleanup
4966 actions, so we use that of the exception handler itself instead. */
4967 add_cleanup (build_call_n_expr (end_handler_decl, 1, gnu_incoming_exc_ptr),
4968 gnat_node);
4969 add_stmt_list (Statements (gnat_node));
4970 gnat_poplevel ();
4972 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
4974 return
4975 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
4978 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
4980 static void
4981 Compilation_Unit_to_gnu (Node_Id gnat_node)
4983 const Node_Id gnat_unit = Unit (gnat_node);
4984 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
4985 || Nkind (gnat_unit) == N_Subprogram_Body);
4986 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
4987 Entity_Id gnat_entity;
4988 Node_Id gnat_pragma;
4989 /* Make the decl for the elaboration procedure. */
4990 tree gnu_elab_proc_decl
4991 = create_subprog_decl
4992 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
4993 NULL_TREE, void_ftype, NULL_TREE, is_disabled, true, false, true, NULL,
4994 gnat_unit);
4995 struct elab_info *info;
4997 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
4998 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
5000 /* Initialize the information structure for the function. */
5001 allocate_struct_function (gnu_elab_proc_decl, false);
5002 set_cfun (NULL);
5004 current_function_decl = NULL_TREE;
5006 start_stmt_group ();
5007 gnat_pushlevel ();
5009 /* For a body, first process the spec if there is one. */
5010 if (Nkind (gnat_unit) == N_Package_Body
5011 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
5012 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
5014 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
5016 elaborate_all_entities (gnat_node);
5018 if (Nkind (gnat_unit) == N_Subprogram_Declaration
5019 || Nkind (gnat_unit) == N_Generic_Package_Declaration
5020 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
5021 return;
5024 /* Then process any pragmas and declarations preceding the unit. */
5025 for (gnat_pragma = First (Context_Items (gnat_node));
5026 Present (gnat_pragma);
5027 gnat_pragma = Next (gnat_pragma))
5028 if (Nkind (gnat_pragma) == N_Pragma)
5029 add_stmt (gnat_to_gnu (gnat_pragma));
5030 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
5031 true, true);
5033 /* Process the unit itself. */
5034 add_stmt (gnat_to_gnu (gnat_unit));
5036 /* Generate code for all the inlined subprograms. */
5037 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5038 Present (gnat_entity);
5039 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
5041 Node_Id gnat_body;
5043 /* Without optimization, process only the required subprograms. */
5044 if (!optimize && !Has_Pragma_Inline_Always (gnat_entity))
5045 continue;
5047 gnat_body = Parent (Declaration_Node (gnat_entity));
5048 if (Nkind (gnat_body) != N_Subprogram_Body)
5050 /* ??? This happens when only the spec of a package is provided. */
5051 if (No (Corresponding_Body (gnat_body)))
5052 continue;
5054 gnat_body
5055 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
5058 /* Define the entity first so we set DECL_EXTERNAL. */
5059 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
5060 add_stmt (gnat_to_gnu (gnat_body));
5063 /* Process any pragmas and actions following the unit. */
5064 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5065 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
5066 finalize_from_limited_with ();
5068 /* Save away what we've made so far and record this potential elaboration
5069 procedure. */
5070 info = ggc_alloc<elab_info> ();
5071 set_current_block_context (gnu_elab_proc_decl);
5072 gnat_poplevel ();
5073 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
5075 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
5077 info->next = elab_info_list;
5078 info->elab_proc = gnu_elab_proc_decl;
5079 info->gnat_node = gnat_node;
5080 elab_info_list = info;
5082 /* Generate elaboration code for this unit, if necessary, and say whether
5083 we did or not. */
5084 gnu_elab_proc_stack->pop ();
5086 /* Invalidate the global renaming pointers. This is necessary because
5087 stabilization of the renamed entities may create SAVE_EXPRs which
5088 have been tied to a specific elaboration routine just above. */
5089 invalidate_global_renaming_pointers ();
5091 /* Force the processing for all nodes that remain in the queue. */
5092 process_deferred_decl_context (true);
5095 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Raise_xxx_Error,
5096 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where
5097 we should place the result type. LABEL_P is true if there is a label to
5098 branch to for the exception. */
5100 static tree
5101 Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5103 const Node_Kind kind = Nkind (gnat_node);
5104 const int reason = UI_To_Int (Reason (gnat_node));
5105 const Node_Id gnat_cond = Condition (gnat_node);
5106 const bool with_extra_info
5107 = Exception_Extra_Info
5108 && !No_Exception_Handlers_Set ()
5109 && !get_exception_label (kind);
5110 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
5112 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5114 switch (reason)
5116 case CE_Access_Check_Failed:
5117 if (with_extra_info)
5118 gnu_result = build_call_raise_column (reason, gnat_node);
5119 break;
5121 case CE_Index_Check_Failed:
5122 case CE_Range_Check_Failed:
5123 case CE_Invalid_Data:
5124 if (Present (gnat_cond) && Nkind (gnat_cond) == N_Op_Not)
5126 Node_Id gnat_range, gnat_index, gnat_type;
5127 tree gnu_index, gnu_low_bound, gnu_high_bound;
5128 struct range_check_info_d *rci;
5130 switch (Nkind (Right_Opnd (gnat_cond)))
5132 case N_In:
5133 gnat_range = Right_Opnd (Right_Opnd (gnat_cond));
5134 gcc_assert (Nkind (gnat_range) == N_Range);
5135 gnu_low_bound = gnat_to_gnu (Low_Bound (gnat_range));
5136 gnu_high_bound = gnat_to_gnu (High_Bound (gnat_range));
5137 break;
5139 case N_Op_Ge:
5140 gnu_low_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5141 gnu_high_bound = NULL_TREE;
5142 break;
5144 case N_Op_Le:
5145 gnu_low_bound = NULL_TREE;
5146 gnu_high_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5147 break;
5149 default:
5150 goto common;
5153 gnat_index = Left_Opnd (Right_Opnd (gnat_cond));
5154 gnat_type = Etype (gnat_index);
5155 gnu_index = gnat_to_gnu (gnat_index);
5157 if (with_extra_info
5158 && gnu_low_bound
5159 && gnu_high_bound
5160 && Known_Esize (gnat_type)
5161 && UI_To_Int (Esize (gnat_type)) <= 32)
5162 gnu_result
5163 = build_call_raise_range (reason, gnat_node, gnu_index,
5164 gnu_low_bound, gnu_high_bound);
5166 /* If loop unswitching is enabled, we try to compute invariant
5167 conditions for checks applied to iteration variables, i.e.
5168 conditions that are both independent of the variable and
5169 necessary in order for the check to fail in the course of
5170 some iteration, and prepend them to the original condition
5171 of the checks. This will make it possible later for the
5172 loop unswitching pass to replace the loop with two loops,
5173 one of which has the checks eliminated and the other has
5174 the original checks reinstated, and a run time selection.
5175 The former loop will be suitable for vectorization. */
5176 if (flag_unswitch_loops
5177 && !vec_safe_is_empty (gnu_loop_stack)
5178 && (!gnu_low_bound
5179 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
5180 && (!gnu_high_bound
5181 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
5182 && (rci = push_range_check_info (gnu_index)))
5184 rci->low_bound = gnu_low_bound;
5185 rci->high_bound = gnu_high_bound;
5186 rci->type = get_unpadded_type (gnat_type);
5187 rci->invariant_cond = build1 (SAVE_EXPR, boolean_type_node,
5188 boolean_true_node);
5189 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5190 boolean_type_node,
5191 rci->invariant_cond,
5192 gnat_to_gnu (gnat_cond));
5195 break;
5197 default:
5198 break;
5201 common:
5202 if (!gnu_result)
5203 gnu_result = build_call_raise (reason, gnat_node, kind);
5204 set_expr_location_from_node (gnu_result, gnat_node);
5206 /* If the type is VOID, this is a statement, so we need to generate the code
5207 for the call. Handle a condition, if there is one. */
5208 if (VOID_TYPE_P (*gnu_result_type_p))
5210 if (Present (gnat_cond))
5212 if (!gnu_cond)
5213 gnu_cond = gnat_to_gnu (gnat_cond);
5214 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
5215 alloc_stmt_list ());
5218 else
5219 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
5221 return gnu_result;
5224 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
5225 parameter of a call. */
5227 static bool
5228 lhs_or_actual_p (Node_Id gnat_node)
5230 Node_Id gnat_parent = Parent (gnat_node);
5231 Node_Kind kind = Nkind (gnat_parent);
5233 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
5234 return true;
5236 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
5237 && Name (gnat_parent) != gnat_node)
5238 return true;
5240 if (kind == N_Parameter_Association)
5241 return true;
5243 return false;
5246 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
5247 of an assignment or an actual parameter of a call. */
5249 static bool
5250 present_in_lhs_or_actual_p (Node_Id gnat_node)
5252 Node_Kind kind;
5254 if (lhs_or_actual_p (gnat_node))
5255 return true;
5257 kind = Nkind (Parent (gnat_node));
5259 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
5260 && lhs_or_actual_p (Parent (gnat_node)))
5261 return true;
5263 return false;
5266 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
5267 as gigi is concerned. This is used to avoid conversions on the LHS. */
5269 static bool
5270 unchecked_conversion_nop (Node_Id gnat_node)
5272 Entity_Id from_type, to_type;
5274 /* The conversion must be on the LHS of an assignment or an actual parameter
5275 of a call. Otherwise, even if the conversion was essentially a no-op, it
5276 could de facto ensure type consistency and this should be preserved. */
5277 if (!lhs_or_actual_p (gnat_node))
5278 return false;
5280 from_type = Etype (Expression (gnat_node));
5282 /* We're interested in artificial conversions generated by the front-end
5283 to make private types explicit, e.g. in Expand_Assign_Array. */
5284 if (!Is_Private_Type (from_type))
5285 return false;
5287 from_type = Underlying_Type (from_type);
5288 to_type = Etype (gnat_node);
5290 /* The direct conversion to the underlying type is a no-op. */
5291 if (to_type == from_type)
5292 return true;
5294 /* For an array subtype, the conversion to the PAIT is a no-op. */
5295 if (Ekind (from_type) == E_Array_Subtype
5296 && to_type == Packed_Array_Impl_Type (from_type))
5297 return true;
5299 /* For a record subtype, the conversion to the type is a no-op. */
5300 if (Ekind (from_type) == E_Record_Subtype
5301 && to_type == Etype (from_type))
5302 return true;
5304 return false;
5307 /* This function is the driver of the GNAT to GCC tree transformation process.
5308 It is the entry point of the tree transformer. GNAT_NODE is the root of
5309 some GNAT tree. Return the root of the corresponding GCC tree. If this
5310 is an expression, return the GCC equivalent of the expression. If this
5311 is a statement, return the statement or add it to the current statement
5312 group, in which case anything returned is to be interpreted as occurring
5313 after anything added. */
5315 tree
5316 gnat_to_gnu (Node_Id gnat_node)
5318 const Node_Kind kind = Nkind (gnat_node);
5319 bool went_into_elab_proc = false;
5320 tree gnu_result = error_mark_node; /* Default to no value. */
5321 tree gnu_result_type = void_type_node;
5322 tree gnu_expr, gnu_lhs, gnu_rhs;
5323 Node_Id gnat_temp;
5325 /* Save node number for error message and set location information. */
5326 error_gnat_node = gnat_node;
5327 Sloc_to_locus (Sloc (gnat_node), &input_location);
5329 /* If this node is a statement and we are only annotating types, return an
5330 empty statement list. */
5331 if (type_annotate_only && IN (kind, N_Statement_Other_Than_Procedure_Call))
5332 return alloc_stmt_list ();
5334 /* If this node is a non-static subexpression and we are only annotating
5335 types, make this into a NULL_EXPR. */
5336 if (type_annotate_only
5337 && IN (kind, N_Subexpr)
5338 && kind != N_Identifier
5339 && !Compile_Time_Known_Value (gnat_node))
5340 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
5341 build_call_raise (CE_Range_Check_Failed, gnat_node,
5342 N_Raise_Constraint_Error));
5344 if ((IN (kind, N_Statement_Other_Than_Procedure_Call)
5345 && kind != N_Null_Statement)
5346 || kind == N_Procedure_Call_Statement
5347 || kind == N_Label
5348 || kind == N_Implicit_Label_Declaration
5349 || kind == N_Handled_Sequence_Of_Statements
5350 || (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void))
5352 tree current_elab_proc = get_elaboration_procedure ();
5354 /* If this is a statement and we are at top level, it must be part of
5355 the elaboration procedure, so mark us as being in that procedure. */
5356 if (!current_function_decl)
5358 current_function_decl = current_elab_proc;
5359 went_into_elab_proc = true;
5362 /* If we are in the elaboration procedure, check if we are violating a
5363 No_Elaboration_Code restriction by having a statement there. Don't
5364 check for a possible No_Elaboration_Code restriction violation on
5365 N_Handled_Sequence_Of_Statements, as we want to signal an error on
5366 every nested real statement instead. This also avoids triggering
5367 spurious errors on dummy (empty) sequences created by the front-end
5368 for package bodies in some cases. */
5369 if (current_function_decl == current_elab_proc
5370 && kind != N_Handled_Sequence_Of_Statements)
5371 Check_Elaboration_Code_Allowed (gnat_node);
5374 switch (kind)
5376 /********************************/
5377 /* Chapter 2: Lexical Elements */
5378 /********************************/
5380 case N_Identifier:
5381 case N_Expanded_Name:
5382 case N_Operator_Symbol:
5383 case N_Defining_Identifier:
5384 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
5386 /* If this is an atomic access on the RHS for which synchronization is
5387 required, build the atomic load. */
5388 if (atomic_sync_required_p (gnat_node)
5389 && !present_in_lhs_or_actual_p (gnat_node))
5390 gnu_result = build_atomic_load (gnu_result);
5391 break;
5393 case N_Integer_Literal:
5395 tree gnu_type;
5397 /* Get the type of the result, looking inside any padding and
5398 justified modular types. Then get the value in that type. */
5399 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
5401 if (TREE_CODE (gnu_type) == RECORD_TYPE
5402 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
5403 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
5405 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
5407 /* If the result overflows (meaning it doesn't fit in its base type),
5408 abort. We would like to check that the value is within the range
5409 of the subtype, but that causes problems with subtypes whose usage
5410 will raise Constraint_Error and with biased representation, so
5411 we don't. */
5412 gcc_assert (!TREE_OVERFLOW (gnu_result));
5414 break;
5416 case N_Character_Literal:
5417 /* If a Entity is present, it means that this was one of the
5418 literals in a user-defined character type. In that case,
5419 just return the value in the CONST_DECL. Otherwise, use the
5420 character code. In that case, the base type should be an
5421 INTEGER_TYPE, but we won't bother checking for that. */
5422 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5423 if (Present (Entity (gnat_node)))
5424 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
5425 else
5426 gnu_result
5427 = build_int_cst_type
5428 (gnu_result_type, UI_To_CC (Char_Literal_Value (gnat_node)));
5429 break;
5431 case N_Real_Literal:
5432 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5434 /* If this is of a fixed-point type, the value we want is the value of
5435 the corresponding integer. */
5436 if (IN (Ekind (Underlying_Type (Etype (gnat_node))), Fixed_Point_Kind))
5438 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
5439 gnu_result_type);
5440 gcc_assert (!TREE_OVERFLOW (gnu_result));
5443 else
5445 Ureal ur_realval = Realval (gnat_node);
5447 /* First convert the value to a machine number if it isn't already.
5448 That will force the base to 2 for non-zero values and simplify
5449 the rest of the logic. */
5450 if (!Is_Machine_Number (gnat_node))
5451 ur_realval
5452 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
5453 ur_realval, Round_Even, gnat_node);
5455 if (UR_Is_Zero (ur_realval))
5456 gnu_result = convert (gnu_result_type, integer_zero_node);
5457 else
5459 REAL_VALUE_TYPE tmp;
5461 gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
5463 /* The base must be 2 as Machine guarantees this, so we scale
5464 the value, which we know can fit in the mantissa of the type
5465 (hence the use of that type above). */
5466 gcc_assert (Rbase (ur_realval) == 2);
5467 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
5468 - UI_To_Int (Denominator (ur_realval)));
5469 gnu_result = build_real (gnu_result_type, tmp);
5472 /* Now see if we need to negate the result. Do it this way to
5473 properly handle -0. */
5474 if (UR_Is_Negative (Realval (gnat_node)))
5475 gnu_result
5476 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
5477 gnu_result);
5480 break;
5482 case N_String_Literal:
5483 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5484 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
5486 String_Id gnat_string = Strval (gnat_node);
5487 int length = String_Length (gnat_string);
5488 int i;
5489 char *string;
5490 if (length >= ALLOCA_THRESHOLD)
5491 string = XNEWVEC (char, length + 1);
5492 else
5493 string = (char *) alloca (length + 1);
5495 /* Build the string with the characters in the literal. Note
5496 that Ada strings are 1-origin. */
5497 for (i = 0; i < length; i++)
5498 string[i] = Get_String_Char (gnat_string, i + 1);
5500 /* Put a null at the end of the string in case it's in a context
5501 where GCC will want to treat it as a C string. */
5502 string[i] = 0;
5504 gnu_result = build_string (length, string);
5506 /* Strings in GCC don't normally have types, but we want
5507 this to not be converted to the array type. */
5508 TREE_TYPE (gnu_result) = gnu_result_type;
5510 if (length >= ALLOCA_THRESHOLD)
5511 free (string);
5513 else
5515 /* Build a list consisting of each character, then make
5516 the aggregate. */
5517 String_Id gnat_string = Strval (gnat_node);
5518 int length = String_Length (gnat_string);
5519 int i;
5520 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5521 tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node);
5522 vec<constructor_elt, va_gc> *gnu_vec;
5523 vec_alloc (gnu_vec, length);
5525 for (i = 0; i < length; i++)
5527 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
5528 Get_String_Char (gnat_string, i + 1));
5530 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
5531 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node);
5534 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
5536 break;
5538 case N_Pragma:
5539 gnu_result = Pragma_to_gnu (gnat_node);
5540 break;
5542 /**************************************/
5543 /* Chapter 3: Declarations and Types */
5544 /**************************************/
5546 case N_Subtype_Declaration:
5547 case N_Full_Type_Declaration:
5548 case N_Incomplete_Type_Declaration:
5549 case N_Private_Type_Declaration:
5550 case N_Private_Extension_Declaration:
5551 case N_Task_Type_Declaration:
5552 process_type (Defining_Entity (gnat_node));
5553 gnu_result = alloc_stmt_list ();
5554 break;
5556 case N_Object_Declaration:
5557 case N_Exception_Declaration:
5558 gnat_temp = Defining_Entity (gnat_node);
5559 gnu_result = alloc_stmt_list ();
5561 /* If we are just annotating types and this object has an unconstrained
5562 or task type, don't elaborate it. */
5563 if (type_annotate_only
5564 && (((Is_Array_Type (Etype (gnat_temp))
5565 || Is_Record_Type (Etype (gnat_temp)))
5566 && !Is_Constrained (Etype (gnat_temp)))
5567 || Is_Concurrent_Type (Etype (gnat_temp))))
5568 break;
5570 if (Present (Expression (gnat_node))
5571 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
5572 && (!type_annotate_only
5573 || Compile_Time_Known_Value (Expression (gnat_node))))
5575 gnu_expr = gnat_to_gnu (Expression (gnat_node));
5576 if (Do_Range_Check (Expression (gnat_node)))
5577 gnu_expr
5578 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
5580 /* If this object has its elaboration delayed, we must force
5581 evaluation of GNU_EXPR right now and save it for when the object
5582 is frozen. */
5583 if (Present (Freeze_Node (gnat_temp)))
5585 if (TREE_CONSTANT (gnu_expr))
5587 else if (global_bindings_p ())
5588 gnu_expr
5589 = create_var_decl (create_concat_name (gnat_temp, "init"),
5590 NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5591 false, false, false, false,
5592 NULL, gnat_temp);
5593 else
5594 gnu_expr = gnat_save_expr (gnu_expr);
5596 save_gnu_tree (gnat_node, gnu_expr, true);
5599 else
5600 gnu_expr = NULL_TREE;
5602 if (type_annotate_only && gnu_expr && TREE_CODE (gnu_expr) == ERROR_MARK)
5603 gnu_expr = NULL_TREE;
5605 /* If this is a deferred constant with an address clause, we ignore the
5606 full view since the clause is on the partial view and we cannot have
5607 2 different GCC trees for the object. The only bits of the full view
5608 we will use is the initializer, but it will be directly fetched. */
5609 if (Ekind(gnat_temp) == E_Constant
5610 && Present (Address_Clause (gnat_temp))
5611 && Present (Full_View (gnat_temp)))
5612 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
5614 if (No (Freeze_Node (gnat_temp)))
5615 gnat_to_gnu_entity (gnat_temp, gnu_expr, 1);
5616 break;
5618 case N_Object_Renaming_Declaration:
5619 gnat_temp = Defining_Entity (gnat_node);
5621 /* Don't do anything if this renaming is handled by the front end or if
5622 we are just annotating types and this object has a composite or task
5623 type, don't elaborate it. We return the result in case it has any
5624 SAVE_EXPRs in it that need to be evaluated here. */
5625 if (!Is_Renaming_Of_Object (gnat_temp)
5626 && ! (type_annotate_only
5627 && (Is_Array_Type (Etype (gnat_temp))
5628 || Is_Record_Type (Etype (gnat_temp))
5629 || Is_Concurrent_Type (Etype (gnat_temp)))))
5630 gnu_result
5631 = gnat_to_gnu_entity (gnat_temp,
5632 gnat_to_gnu (Renamed_Object (gnat_temp)), 1);
5633 else
5634 gnu_result = alloc_stmt_list ();
5635 break;
5637 case N_Exception_Renaming_Declaration:
5638 gnat_temp = Defining_Entity (gnat_node);
5639 if (Renamed_Entity (gnat_temp) != Empty)
5640 gnu_result
5641 = gnat_to_gnu_entity (gnat_temp,
5642 gnat_to_gnu (Renamed_Entity (gnat_temp)), 1);
5643 else
5644 gnu_result = alloc_stmt_list ();
5645 break;
5647 case N_Implicit_Label_Declaration:
5648 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
5649 gnu_result = alloc_stmt_list ();
5650 break;
5652 case N_Number_Declaration:
5653 case N_Subprogram_Renaming_Declaration:
5654 case N_Package_Renaming_Declaration:
5655 /* These are fully handled in the front end. */
5656 /* ??? For package renamings, find a way to use GENERIC namespaces so
5657 that we get proper debug information for them. */
5658 gnu_result = alloc_stmt_list ();
5659 break;
5661 /*************************************/
5662 /* Chapter 4: Names and Expressions */
5663 /*************************************/
5665 case N_Explicit_Dereference:
5666 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5667 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5668 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
5670 /* If this is an atomic access on the RHS for which synchronization is
5671 required, build the atomic load. */
5672 if (atomic_sync_required_p (gnat_node)
5673 && !present_in_lhs_or_actual_p (gnat_node))
5674 gnu_result = build_atomic_load (gnu_result);
5675 break;
5677 case N_Indexed_Component:
5679 tree gnu_array_object = gnat_to_gnu (Prefix (gnat_node));
5680 tree gnu_type;
5681 int ndim;
5682 int i;
5683 Node_Id *gnat_expr_array;
5685 gnu_array_object = maybe_implicit_deref (gnu_array_object);
5687 /* Convert vector inputs to their representative array type, to fit
5688 what the code below expects. */
5689 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
5691 if (present_in_lhs_or_actual_p (gnat_node))
5692 gnat_mark_addressable (gnu_array_object);
5693 gnu_array_object = maybe_vector_array (gnu_array_object);
5696 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
5698 /* If we got a padded type, remove it too. */
5699 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
5700 gnu_array_object
5701 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
5702 gnu_array_object);
5704 gnu_result = gnu_array_object;
5706 /* The failure of this assertion will very likely come from a missing
5707 expansion for a packed array access. */
5708 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
5710 /* First compute the number of dimensions of the array, then
5711 fill the expression array, the order depending on whether
5712 this is a Convention_Fortran array or not. */
5713 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
5714 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5715 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
5716 ndim++, gnu_type = TREE_TYPE (gnu_type))
5719 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
5721 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
5722 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
5723 i >= 0;
5724 i--, gnat_temp = Next (gnat_temp))
5725 gnat_expr_array[i] = gnat_temp;
5726 else
5727 for (i = 0, gnat_temp = First (Expressions (gnat_node));
5728 i < ndim;
5729 i++, gnat_temp = Next (gnat_temp))
5730 gnat_expr_array[i] = gnat_temp;
5732 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
5733 i < ndim; i++, gnu_type = TREE_TYPE (gnu_type))
5735 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
5736 gnat_temp = gnat_expr_array[i];
5737 gnu_expr = gnat_to_gnu (gnat_temp);
5739 if (Do_Range_Check (gnat_temp))
5740 gnu_expr
5741 = emit_index_check
5742 (gnu_array_object, gnu_expr,
5743 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5744 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type))),
5745 gnat_temp);
5747 gnu_result
5748 = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr);
5750 /* Array accesses are bound-checked so they cannot trap, but this
5751 is valid only if they are not hoisted ahead of the check. We
5752 need to mark them as no-trap to get decent loop optimizations
5753 in the presence of -fnon-call-exceptions, so we do it when we
5754 know that the original expression had no side-effects. */
5755 if (TREE_CODE (gnu_result) == ARRAY_REF
5756 && !(Nkind (gnat_temp) == N_Identifier
5757 && Ekind (Entity (gnat_temp)) == E_Constant))
5758 TREE_THIS_NOTRAP (gnu_result) = 1;
5761 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5763 /* If this is an atomic access on the RHS for which synchronization is
5764 required, build the atomic load. */
5765 if (atomic_sync_required_p (gnat_node)
5766 && !present_in_lhs_or_actual_p (gnat_node))
5767 gnu_result = build_atomic_load (gnu_result);
5769 break;
5771 case N_Slice:
5773 Node_Id gnat_range_node = Discrete_Range (gnat_node);
5774 tree gnu_type;
5776 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5777 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5779 /* Do any implicit dereferences of the prefix and do any needed
5780 range check. */
5781 gnu_result = maybe_implicit_deref (gnu_result);
5782 gnu_result = maybe_unconstrained_array (gnu_result);
5783 gnu_type = TREE_TYPE (gnu_result);
5784 if (Do_Range_Check (gnat_range_node))
5786 /* Get the bounds of the slice. */
5787 tree gnu_index_type
5788 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type));
5789 tree gnu_min_expr = TYPE_MIN_VALUE (gnu_index_type);
5790 tree gnu_max_expr = TYPE_MAX_VALUE (gnu_index_type);
5791 /* Get the permitted bounds. */
5792 tree gnu_base_index_type
5793 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type));
5794 tree gnu_base_min_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5795 (TYPE_MIN_VALUE (gnu_base_index_type), gnu_result);
5796 tree gnu_base_max_expr = SUBSTITUTE_PLACEHOLDER_IN_EXPR
5797 (TYPE_MAX_VALUE (gnu_base_index_type), gnu_result);
5798 tree gnu_expr_l, gnu_expr_h, gnu_expr_type;
5800 gnu_min_expr = gnat_protect_expr (gnu_min_expr);
5801 gnu_max_expr = gnat_protect_expr (gnu_max_expr);
5803 /* Derive a good type to convert everything to. */
5804 gnu_expr_type = get_base_type (gnu_index_type);
5806 /* Test whether the minimum slice value is too small. */
5807 gnu_expr_l = build_binary_op (LT_EXPR, boolean_type_node,
5808 convert (gnu_expr_type,
5809 gnu_min_expr),
5810 convert (gnu_expr_type,
5811 gnu_base_min_expr));
5813 /* Test whether the maximum slice value is too large. */
5814 gnu_expr_h = build_binary_op (GT_EXPR, boolean_type_node,
5815 convert (gnu_expr_type,
5816 gnu_max_expr),
5817 convert (gnu_expr_type,
5818 gnu_base_max_expr));
5820 /* Build a slice index check that returns the low bound,
5821 assuming the slice is not empty. */
5822 gnu_expr = emit_check
5823 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
5824 gnu_expr_l, gnu_expr_h),
5825 gnu_min_expr, CE_Index_Check_Failed, gnat_node);
5827 /* Build a conditional expression that does the index checks and
5828 returns the low bound if the slice is not empty (max >= min),
5829 and returns the naked low bound otherwise (max < min), unless
5830 it is non-constant and the high bound is; this prevents VRP
5831 from inferring bogus ranges on the unlikely path. */
5832 gnu_expr = fold_build3 (COND_EXPR, gnu_expr_type,
5833 build_binary_op (GE_EXPR, gnu_expr_type,
5834 convert (gnu_expr_type,
5835 gnu_max_expr),
5836 convert (gnu_expr_type,
5837 gnu_min_expr)),
5838 gnu_expr,
5839 TREE_CODE (gnu_min_expr) != INTEGER_CST
5840 && TREE_CODE (gnu_max_expr) == INTEGER_CST
5841 ? gnu_max_expr : gnu_min_expr);
5843 else
5844 /* Simply return the naked low bound. */
5845 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
5847 /* If this is a slice with non-constant size of an array with constant
5848 size, set the maximum size for the allocation of temporaries. */
5849 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
5850 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_type)))
5851 TYPE_ARRAY_MAX_SIZE (gnu_result_type) = TYPE_SIZE_UNIT (gnu_type);
5853 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
5854 gnu_result, gnu_expr);
5856 break;
5858 case N_Selected_Component:
5860 Entity_Id gnat_prefix = Prefix (gnat_node);
5861 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
5862 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
5863 tree gnu_field;
5865 gnu_prefix = maybe_implicit_deref (gnu_prefix);
5867 /* For discriminant references in tagged types always substitute the
5868 corresponding discriminant as the actual selected component. */
5869 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix))))
5870 while (Present (Corresponding_Discriminant (gnat_field)))
5871 gnat_field = Corresponding_Discriminant (gnat_field);
5873 /* For discriminant references of untagged types always substitute the
5874 corresponding stored discriminant. */
5875 else if (Present (Corresponding_Discriminant (gnat_field)))
5876 gnat_field = Original_Record_Component (gnat_field);
5878 /* Handle extracting the real or imaginary part of a complex.
5879 The real part is the first field and the imaginary the last. */
5880 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
5881 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
5882 ? REALPART_EXPR : IMAGPART_EXPR,
5883 NULL_TREE, gnu_prefix);
5884 else
5886 gnu_field = gnat_to_gnu_field_decl (gnat_field);
5888 /* If there are discriminants, the prefix might be evaluated more
5889 than once, which is a problem if it has side-effects. */
5890 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node)))
5891 ? Designated_Type (Etype
5892 (Prefix (gnat_node)))
5893 : Etype (Prefix (gnat_node))))
5894 gnu_prefix = gnat_stabilize_reference (gnu_prefix, false, NULL);
5896 gnu_result
5897 = build_component_ref (gnu_prefix, NULL_TREE, gnu_field,
5898 (Nkind (Parent (gnat_node))
5899 == N_Attribute_Reference)
5900 && lvalue_required_for_attribute_p
5901 (Parent (gnat_node)));
5904 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5906 /* If this is an atomic access on the RHS for which synchronization is
5907 required, build the atomic load. */
5908 if (atomic_sync_required_p (gnat_node)
5909 && !present_in_lhs_or_actual_p (gnat_node))
5910 gnu_result = build_atomic_load (gnu_result);
5912 break;
5914 case N_Attribute_Reference:
5916 /* The attribute designator. */
5917 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
5919 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
5920 is a unit, not an object with a GCC equivalent. */
5921 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
5922 return
5923 create_subprog_decl (create_concat_name
5924 (Entity (Prefix (gnat_node)),
5925 attr == Attr_Elab_Body ? "elabb" : "elabs"),
5926 NULL_TREE, void_ftype, NULL_TREE, is_disabled,
5927 true, true, true, NULL, gnat_node);
5929 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
5931 break;
5933 case N_Reference:
5934 /* Like 'Access as far as we are concerned. */
5935 gnu_result = gnat_to_gnu (Prefix (gnat_node));
5936 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
5937 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5938 break;
5940 case N_Aggregate:
5941 case N_Extension_Aggregate:
5943 tree gnu_aggr_type;
5945 /* ??? It is wrong to evaluate the type now, but there doesn't
5946 seem to be any other practical way of doing it. */
5948 gcc_assert (!Expansion_Delayed (gnat_node));
5950 gnu_aggr_type = gnu_result_type
5951 = get_unpadded_type (Etype (gnat_node));
5953 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
5954 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
5955 gnu_aggr_type
5956 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
5957 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
5958 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
5960 if (Null_Record_Present (gnat_node))
5961 gnu_result = gnat_build_constructor (gnu_aggr_type,
5962 NULL);
5964 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
5965 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
5966 gnu_result
5967 = assoc_to_constructor (Etype (gnat_node),
5968 First (Component_Associations (gnat_node)),
5969 gnu_aggr_type);
5970 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
5971 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
5972 gnu_aggr_type,
5973 Component_Type (Etype (gnat_node)));
5974 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
5975 gnu_result
5976 = build_binary_op
5977 (COMPLEX_EXPR, gnu_aggr_type,
5978 gnat_to_gnu (Expression (First
5979 (Component_Associations (gnat_node)))),
5980 gnat_to_gnu (Expression
5981 (Next
5982 (First (Component_Associations (gnat_node))))));
5983 else
5984 gcc_unreachable ();
5986 gnu_result = convert (gnu_result_type, gnu_result);
5988 break;
5990 case N_Null:
5991 if (TARGET_VTABLE_USES_DESCRIPTORS
5992 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
5993 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
5994 gnu_result = null_fdesc_node;
5995 else
5996 gnu_result = null_pointer_node;
5997 gnu_result_type = get_unpadded_type (Etype (gnat_node));
5998 break;
6000 case N_Type_Conversion:
6001 case N_Qualified_Expression:
6002 /* Get the operand expression. */
6003 gnu_result = gnat_to_gnu (Expression (gnat_node));
6004 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6006 /* If this is a qualified expression for a tagged type, we mark the type
6007 as used. Because of polymorphism, this might be the only reference to
6008 the tagged type in the program while objects have it as dynamic type.
6009 The debugger needs to see it to display these objects properly. */
6010 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
6011 used_types_insert (gnu_result_type);
6013 gnu_result
6014 = convert_with_check (Etype (gnat_node), gnu_result,
6015 Do_Overflow_Check (gnat_node),
6016 Do_Range_Check (Expression (gnat_node)),
6017 kind == N_Type_Conversion
6018 && Float_Truncate (gnat_node), gnat_node);
6019 break;
6021 case N_Unchecked_Type_Conversion:
6022 gnu_result = gnat_to_gnu (Expression (gnat_node));
6024 /* Skip further processing if the conversion is deemed a no-op. */
6025 if (unchecked_conversion_nop (gnat_node))
6027 gnu_result_type = TREE_TYPE (gnu_result);
6028 break;
6031 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6033 /* If the result is a pointer type, see if we are improperly
6034 converting to a stricter alignment. */
6035 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
6036 && IN (Ekind (Etype (gnat_node)), Access_Kind))
6038 unsigned int align = known_alignment (gnu_result);
6039 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6040 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6042 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6043 post_error_ne_tree_2
6044 ("?source alignment (^) '< alignment of & (^)",
6045 gnat_node, Designated_Type (Etype (gnat_node)),
6046 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
6049 /* If we are converting a descriptor to a function pointer, first
6050 build the pointer. */
6051 if (TARGET_VTABLE_USES_DESCRIPTORS
6052 && TREE_TYPE (gnu_result) == fdesc_type_node
6053 && POINTER_TYPE_P (gnu_result_type))
6054 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6056 gnu_result = unchecked_convert (gnu_result_type, gnu_result,
6057 No_Truncation (gnat_node));
6058 break;
6060 case N_In:
6061 case N_Not_In:
6063 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
6064 Node_Id gnat_range = Right_Opnd (gnat_node);
6065 tree gnu_low, gnu_high;
6067 /* GNAT_RANGE is either an N_Range node or an identifier denoting a
6068 subtype. */
6069 if (Nkind (gnat_range) == N_Range)
6071 gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
6072 gnu_high = gnat_to_gnu (High_Bound (gnat_range));
6074 else if (Nkind (gnat_range) == N_Identifier
6075 || Nkind (gnat_range) == N_Expanded_Name)
6077 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
6078 tree gnu_range_base_type = get_base_type (gnu_range_type);
6080 gnu_low
6081 = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type));
6082 gnu_high
6083 = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type));
6085 else
6086 gcc_unreachable ();
6088 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6090 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
6091 ensure that GNU_OBJ is evaluated only once and perform a full range
6092 test. */
6093 if (operand_equal_p (gnu_low, gnu_high, 0))
6094 gnu_result
6095 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
6096 else
6098 tree t1, t2;
6099 gnu_obj = gnat_protect_expr (gnu_obj);
6100 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
6101 if (EXPR_P (t1))
6102 set_expr_location_from_node (t1, gnat_node);
6103 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
6104 if (EXPR_P (t2))
6105 set_expr_location_from_node (t2, gnat_node);
6106 gnu_result
6107 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
6110 if (kind == N_Not_In)
6111 gnu_result
6112 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
6114 break;
6116 case N_Op_Divide:
6117 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6118 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6119 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6120 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
6121 ? RDIV_EXPR
6122 : (Rounded_Result (gnat_node)
6123 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
6124 gnu_result_type, gnu_lhs, gnu_rhs);
6125 break;
6127 case N_Op_Or: case N_Op_And: case N_Op_Xor:
6128 /* These can either be operations on booleans or on modular types.
6129 Fall through for boolean types since that's the way GNU_CODES is
6130 set up. */
6131 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6133 enum tree_code code
6134 = (kind == N_Op_Or ? BIT_IOR_EXPR
6135 : kind == N_Op_And ? BIT_AND_EXPR
6136 : BIT_XOR_EXPR);
6138 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6139 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6140 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6141 gnu_result = build_binary_op (code, gnu_result_type,
6142 gnu_lhs, gnu_rhs);
6143 break;
6146 /* ... fall through ... */
6148 case N_Op_Eq: case N_Op_Ne: case N_Op_Lt:
6149 case N_Op_Le: case N_Op_Gt: case N_Op_Ge:
6150 case N_Op_Add: case N_Op_Subtract: case N_Op_Multiply:
6151 case N_Op_Mod: case N_Op_Rem:
6152 case N_Op_Rotate_Left:
6153 case N_Op_Rotate_Right:
6154 case N_Op_Shift_Left:
6155 case N_Op_Shift_Right:
6156 case N_Op_Shift_Right_Arithmetic:
6157 case N_And_Then: case N_Or_Else:
6159 enum tree_code code = gnu_codes[kind];
6160 bool ignore_lhs_overflow = false;
6161 location_t saved_location = input_location;
6162 tree gnu_type;
6164 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6165 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6166 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6168 /* Pending generic support for efficient vector logical operations in
6169 GCC, convert vectors to their representative array type view and
6170 fallthrough. */
6171 gnu_lhs = maybe_vector_array (gnu_lhs);
6172 gnu_rhs = maybe_vector_array (gnu_rhs);
6174 /* If this is a comparison operator, convert any references to an
6175 unconstrained array value into a reference to the actual array. */
6176 if (TREE_CODE_CLASS (code) == tcc_comparison)
6178 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
6179 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
6182 /* If this is a shift whose count is not guaranteed to be correct,
6183 we need to adjust the shift count. */
6184 if (IN (kind, N_Op_Shift) && !Shift_Count_OK (gnat_node))
6186 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
6187 tree gnu_max_shift
6188 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
6190 if (kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
6191 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
6192 gnu_rhs, gnu_max_shift);
6193 else if (kind == N_Op_Shift_Right_Arithmetic)
6194 gnu_rhs
6195 = build_binary_op
6196 (MIN_EXPR, gnu_count_type,
6197 build_binary_op (MINUS_EXPR,
6198 gnu_count_type,
6199 gnu_max_shift,
6200 convert (gnu_count_type,
6201 integer_one_node)),
6202 gnu_rhs);
6205 /* For right shifts, the type says what kind of shift to do,
6206 so we may need to choose a different type. In this case,
6207 we have to ignore integer overflow lest it propagates all
6208 the way down and causes a CE to be explicitly raised. */
6209 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
6211 gnu_type = gnat_unsigned_type (gnu_type);
6212 ignore_lhs_overflow = true;
6214 else if (kind == N_Op_Shift_Right_Arithmetic
6215 && TYPE_UNSIGNED (gnu_type))
6217 gnu_type = gnat_signed_type (gnu_type);
6218 ignore_lhs_overflow = true;
6221 if (gnu_type != gnu_result_type)
6223 tree gnu_old_lhs = gnu_lhs;
6224 gnu_lhs = convert (gnu_type, gnu_lhs);
6225 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
6226 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
6227 gnu_rhs = convert (gnu_type, gnu_rhs);
6230 /* Instead of expanding overflow checks for addition, subtraction
6231 and multiplication itself, the front end will leave this to
6232 the back end when Backend_Overflow_Checks_On_Target is set.
6233 As the GCC back end itself does not know yet how to properly
6234 do overflow checking, do it here. The goal is to push
6235 the expansions further into the back end over time. */
6236 if (Do_Overflow_Check (gnat_node) && Backend_Overflow_Checks_On_Target
6237 && (kind == N_Op_Add
6238 || kind == N_Op_Subtract
6239 || kind == N_Op_Multiply)
6240 && !TYPE_UNSIGNED (gnu_type)
6241 && !FLOAT_TYPE_P (gnu_type))
6242 gnu_result = build_binary_op_trapv (code, gnu_type,
6243 gnu_lhs, gnu_rhs, gnat_node);
6244 else
6246 /* Some operations, e.g. comparisons of arrays, generate complex
6247 trees that need to be annotated while they are being built. */
6248 input_location = saved_location;
6249 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
6252 /* If this is a logical shift with the shift count not verified,
6253 we must return zero if it is too large. We cannot compensate
6254 above in this case. */
6255 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
6256 && !Shift_Count_OK (gnat_node))
6257 gnu_result
6258 = build_cond_expr
6259 (gnu_type,
6260 build_binary_op (GE_EXPR, boolean_type_node,
6261 gnu_rhs,
6262 convert (TREE_TYPE (gnu_rhs),
6263 TYPE_SIZE (gnu_type))),
6264 convert (gnu_type, integer_zero_node),
6265 gnu_result);
6267 break;
6269 case N_If_Expression:
6271 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
6272 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
6273 tree gnu_false
6274 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
6276 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6277 gnu_result
6278 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
6280 break;
6282 case N_Op_Plus:
6283 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
6284 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6285 break;
6287 case N_Op_Not:
6288 /* This case can apply to a boolean or a modular type.
6289 Fall through for a boolean operand since GNU_CODES is set
6290 up to handle this. */
6291 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6293 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6294 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6295 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
6296 gnu_expr);
6297 break;
6300 /* ... fall through ... */
6302 case N_Op_Minus: case N_Op_Abs:
6303 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6304 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6306 if (Do_Overflow_Check (gnat_node)
6307 && !TYPE_UNSIGNED (gnu_result_type)
6308 && !FLOAT_TYPE_P (gnu_result_type))
6309 gnu_result
6310 = build_unary_op_trapv (gnu_codes[kind],
6311 gnu_result_type, gnu_expr, gnat_node);
6312 else
6313 gnu_result = build_unary_op (gnu_codes[kind],
6314 gnu_result_type, gnu_expr);
6315 break;
6317 case N_Allocator:
6319 tree gnu_init = 0;
6320 tree gnu_type;
6321 bool ignore_init_type = false;
6323 gnat_temp = Expression (gnat_node);
6325 /* The Expression operand can either be an N_Identifier or
6326 Expanded_Name, which must represent a type, or a
6327 N_Qualified_Expression, which contains both the object type and an
6328 initial value for the object. */
6329 if (Nkind (gnat_temp) == N_Identifier
6330 || Nkind (gnat_temp) == N_Expanded_Name)
6331 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
6332 else if (Nkind (gnat_temp) == N_Qualified_Expression)
6334 Entity_Id gnat_desig_type
6335 = Designated_Type (Underlying_Type (Etype (gnat_node)));
6337 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
6338 gnu_init = gnat_to_gnu (Expression (gnat_temp));
6340 gnu_init = maybe_unconstrained_array (gnu_init);
6341 if (Do_Range_Check (Expression (gnat_temp)))
6342 gnu_init
6343 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
6345 if (Is_Elementary_Type (gnat_desig_type)
6346 || Is_Constrained (gnat_desig_type))
6347 gnu_type = gnat_to_gnu_type (gnat_desig_type);
6348 else
6350 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
6351 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
6352 gnu_type = TREE_TYPE (gnu_init);
6355 /* See the N_Qualified_Expression case for the rationale. */
6356 if (Is_Tagged_Type (gnat_desig_type))
6357 used_types_insert (gnu_type);
6359 gnu_init = convert (gnu_type, gnu_init);
6361 else
6362 gcc_unreachable ();
6364 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6365 return build_allocator (gnu_type, gnu_init, gnu_result_type,
6366 Procedure_To_Call (gnat_node),
6367 Storage_Pool (gnat_node), gnat_node,
6368 ignore_init_type);
6370 break;
6372 /**************************/
6373 /* Chapter 5: Statements */
6374 /**************************/
6376 case N_Label:
6377 gnu_result = build1 (LABEL_EXPR, void_type_node,
6378 gnat_to_gnu (Identifier (gnat_node)));
6379 break;
6381 case N_Null_Statement:
6382 /* When not optimizing, turn null statements from source into gotos to
6383 the next statement that the middle-end knows how to preserve. */
6384 if (!optimize && Comes_From_Source (gnat_node))
6386 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
6387 DECL_IGNORED_P (label) = 1;
6388 start_stmt_group ();
6389 stmt = build1 (GOTO_EXPR, void_type_node, label);
6390 set_expr_location_from_node (stmt, gnat_node);
6391 add_stmt (stmt);
6392 stmt = build1 (LABEL_EXPR, void_type_node, label);
6393 set_expr_location_from_node (stmt, gnat_node);
6394 add_stmt (stmt);
6395 gnu_result = end_stmt_group ();
6397 else
6398 gnu_result = alloc_stmt_list ();
6399 break;
6401 case N_Assignment_Statement:
6402 /* Get the LHS and RHS of the statement and convert any reference to an
6403 unconstrained array into a reference to the underlying array. */
6404 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
6406 /* If the type has a size that overflows, convert this into raise of
6407 Storage_Error: execution shouldn't have gotten here anyway. */
6408 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
6409 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
6410 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
6411 N_Raise_Storage_Error);
6412 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
6413 gnu_result
6414 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
6415 atomic_sync_required_p (Name (gnat_node)));
6416 else
6418 const Node_Id gnat_expr = Expression (gnat_node);
6419 const Entity_Id gnat_type
6420 = Underlying_Type (Etype (Name (gnat_node)));
6421 const bool regular_array_type_p
6422 = (Is_Array_Type (gnat_type) && !Is_Bit_Packed_Array (gnat_type));
6423 const bool use_memset_p
6424 = (regular_array_type_p
6425 && Nkind (gnat_expr) == N_Aggregate
6426 && Is_Others_Aggregate (gnat_expr));
6428 /* If we'll use memset, we need to find the inner expression. */
6429 if (use_memset_p)
6431 Node_Id gnat_inner
6432 = Expression (First (Component_Associations (gnat_expr)));
6433 while (Nkind (gnat_inner) == N_Aggregate
6434 && Is_Others_Aggregate (gnat_inner))
6435 gnat_inner
6436 = Expression (First (Component_Associations (gnat_inner)));
6437 gnu_rhs = gnat_to_gnu (gnat_inner);
6439 else
6440 gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr));
6442 /* If range check is needed, emit code to generate it. */
6443 if (Do_Range_Check (gnat_expr))
6444 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
6445 gnat_node);
6447 /* If atomic synchronization is required, build an atomic store. */
6448 if (atomic_sync_required_p (Name (gnat_node)))
6449 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs);
6451 /* Or else, use memset when the conditions are met. */
6452 else if (use_memset_p)
6454 tree value = fold_convert (integer_type_node, gnu_rhs);
6455 tree to = gnu_lhs;
6456 tree type = TREE_TYPE (to);
6457 tree size
6458 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to);
6459 tree to_ptr = build_fold_addr_expr (to);
6460 tree t = builtin_decl_implicit (BUILT_IN_MEMSET);
6461 if (TREE_CODE (value) == INTEGER_CST)
6463 tree mask
6464 = build_int_cst (integer_type_node,
6465 ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
6466 value = int_const_binop (BIT_AND_EXPR, value, mask);
6468 gnu_result = build_call_expr (t, 3, to_ptr, value, size);
6471 /* Otherwise build a regular assignment. */
6472 else
6473 gnu_result
6474 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
6476 /* If the assignment type is a regular array and the two sides are
6477 not completely disjoint, play safe and use memmove. But don't do
6478 it for a bit-packed array as it might not be byte-aligned. */
6479 if (TREE_CODE (gnu_result) == MODIFY_EXPR
6480 && regular_array_type_p
6481 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
6483 tree to = TREE_OPERAND (gnu_result, 0);
6484 tree from = TREE_OPERAND (gnu_result, 1);
6485 tree type = TREE_TYPE (from);
6486 tree size
6487 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from);
6488 tree to_ptr = build_fold_addr_expr (to);
6489 tree from_ptr = build_fold_addr_expr (from);
6490 tree t = builtin_decl_implicit (BUILT_IN_MEMMOVE);
6491 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
6494 break;
6496 case N_If_Statement:
6498 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
6500 /* Make the outer COND_EXPR. Avoid non-determinism. */
6501 gnu_result = build3 (COND_EXPR, void_type_node,
6502 gnat_to_gnu (Condition (gnat_node)),
6503 NULL_TREE, NULL_TREE);
6504 COND_EXPR_THEN (gnu_result)
6505 = build_stmt_group (Then_Statements (gnat_node), false);
6506 TREE_SIDE_EFFECTS (gnu_result) = 1;
6507 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
6509 /* Now make a COND_EXPR for each of the "else if" parts. Put each
6510 into the previous "else" part and point to where to put any
6511 outer "else". Also avoid non-determinism. */
6512 if (Present (Elsif_Parts (gnat_node)))
6513 for (gnat_temp = First (Elsif_Parts (gnat_node));
6514 Present (gnat_temp); gnat_temp = Next (gnat_temp))
6516 gnu_expr = build3 (COND_EXPR, void_type_node,
6517 gnat_to_gnu (Condition (gnat_temp)),
6518 NULL_TREE, NULL_TREE);
6519 COND_EXPR_THEN (gnu_expr)
6520 = build_stmt_group (Then_Statements (gnat_temp), false);
6521 TREE_SIDE_EFFECTS (gnu_expr) = 1;
6522 set_expr_location_from_node (gnu_expr, gnat_temp);
6523 *gnu_else_ptr = gnu_expr;
6524 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
6527 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
6529 break;
6531 case N_Case_Statement:
6532 gnu_result = Case_Statement_to_gnu (gnat_node);
6533 break;
6535 case N_Loop_Statement:
6536 gnu_result = Loop_Statement_to_gnu (gnat_node);
6537 break;
6539 case N_Block_Statement:
6540 /* The only way to enter the block is to fall through to it. */
6541 if (stmt_group_may_fallthru ())
6543 start_stmt_group ();
6544 gnat_pushlevel ();
6545 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6546 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6547 gnat_poplevel ();
6548 gnu_result = end_stmt_group ();
6550 else
6551 gnu_result = alloc_stmt_list ();
6552 break;
6554 case N_Exit_Statement:
6555 gnu_result
6556 = build2 (EXIT_STMT, void_type_node,
6557 (Present (Condition (gnat_node))
6558 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
6559 (Present (Name (gnat_node))
6560 ? get_gnu_tree (Entity (Name (gnat_node)))
6561 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
6562 break;
6564 case N_Simple_Return_Statement:
6566 tree gnu_ret_obj, gnu_ret_val;
6568 /* If the subprogram is a function, we must return the expression. */
6569 if (Present (Expression (gnat_node)))
6571 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
6573 /* If this function has copy-in/copy-out parameters parameters and
6574 doesn't return by invisible reference, get the real object for
6575 the return. See Subprogram_Body_to_gnu. */
6576 if (TYPE_CI_CO_LIST (gnu_subprog_type)
6577 && !TREE_ADDRESSABLE (gnu_subprog_type))
6578 gnu_ret_obj = gnu_return_var_stack->last ();
6579 else
6580 gnu_ret_obj = DECL_RESULT (current_function_decl);
6582 /* Get the GCC tree for the expression to be returned. */
6583 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
6585 /* Do not remove the padding from GNU_RET_VAL if the inner type is
6586 self-referential since we want to allocate the fixed size. */
6587 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
6588 && TYPE_IS_PADDING_P
6589 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0)))
6590 && CONTAINS_PLACEHOLDER_P
6591 (TYPE_SIZE (TREE_TYPE (gnu_ret_val))))
6592 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
6594 /* If the function returns by direct reference, return a pointer
6595 to the return value. */
6596 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)
6597 || By_Ref (gnat_node))
6598 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
6600 /* Otherwise, if it returns an unconstrained array, we have to
6601 allocate a new version of the result and return it. */
6602 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type))
6604 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
6606 /* And find out whether this is a candidate for Named Return
6607 Value. If so, record it. */
6608 if (!TYPE_CI_CO_LIST (gnu_subprog_type) && optimize)
6610 tree ret_val = gnu_ret_val;
6612 /* Strip useless conversions around the return value. */
6613 if (gnat_useless_type_conversion (ret_val))
6614 ret_val = TREE_OPERAND (ret_val, 0);
6616 /* Strip unpadding around the return value. */
6617 if (TREE_CODE (ret_val) == COMPONENT_REF
6618 && TYPE_IS_PADDING_P
6619 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
6620 ret_val = TREE_OPERAND (ret_val, 0);
6622 /* Now apply the test to the return value. */
6623 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
6625 if (!f_named_ret_val)
6626 f_named_ret_val = BITMAP_GGC_ALLOC ();
6627 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
6628 if (!f_gnat_ret)
6629 f_gnat_ret = gnat_node;
6633 gnu_ret_val = build_allocator (TREE_TYPE (gnu_ret_val),
6634 gnu_ret_val,
6635 TREE_TYPE (gnu_ret_obj),
6636 Procedure_To_Call (gnat_node),
6637 Storage_Pool (gnat_node),
6638 gnat_node, false);
6641 /* Otherwise, if it returns by invisible reference, dereference
6642 the pointer it is passed using the type of the return value
6643 and build the copy operation manually. This ensures that we
6644 don't copy too much data, for example if the return type is
6645 unconstrained with a maximum size. */
6646 else if (TREE_ADDRESSABLE (gnu_subprog_type))
6648 tree gnu_ret_deref
6649 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
6650 gnu_ret_obj);
6651 gnu_result = build2 (MODIFY_EXPR, void_type_node,
6652 gnu_ret_deref, gnu_ret_val);
6653 add_stmt_with_node (gnu_result, gnat_node);
6654 gnu_ret_val = NULL_TREE;
6658 else
6659 gnu_ret_obj = gnu_ret_val = NULL_TREE;
6661 /* If we have a return label defined, convert this into a branch to
6662 that label. The return proper will be handled elsewhere. */
6663 if (gnu_return_label_stack->last ())
6665 if (gnu_ret_val)
6666 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_ret_obj,
6667 gnu_ret_val));
6669 gnu_result = build1 (GOTO_EXPR, void_type_node,
6670 gnu_return_label_stack->last ());
6672 /* When not optimizing, make sure the return is preserved. */
6673 if (!optimize && Comes_From_Source (gnat_node))
6674 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
6677 /* Otherwise, build a regular return. */
6678 else
6679 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
6681 break;
6683 case N_Goto_Statement:
6684 gnu_result
6685 = build1 (GOTO_EXPR, void_type_node, gnat_to_gnu (Name (gnat_node)));
6686 break;
6688 /***************************/
6689 /* Chapter 6: Subprograms */
6690 /***************************/
6692 case N_Subprogram_Declaration:
6693 /* Unless there is a freeze node, declare the subprogram. We consider
6694 this a "definition" even though we're not generating code for
6695 the subprogram because we will be making the corresponding GCC
6696 node here. */
6698 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6699 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
6700 NULL_TREE, 1);
6701 gnu_result = alloc_stmt_list ();
6702 break;
6704 case N_Abstract_Subprogram_Declaration:
6705 /* This subprogram doesn't exist for code generation purposes, but we
6706 have to elaborate the types of any parameters and result, unless
6707 they are imported types (nothing to generate in this case).
6709 The parameter list may contain types with freeze nodes, e.g. not null
6710 subtypes, so the subprogram itself may carry a freeze node, in which
6711 case its elaboration must be deferred. */
6713 /* Process the parameter types first. */
6714 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
6715 for (gnat_temp
6716 = First_Formal_With_Extras
6717 (Defining_Entity (Specification (gnat_node)));
6718 Present (gnat_temp);
6719 gnat_temp = Next_Formal_With_Extras (gnat_temp))
6720 if (Is_Itype (Etype (gnat_temp))
6721 && !From_Limited_With (Etype (gnat_temp)))
6722 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
6724 /* Then the result type, set to Standard_Void_Type for procedures. */
6726 Entity_Id gnat_temp_type
6727 = Etype (Defining_Entity (Specification (gnat_node)));
6729 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
6730 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, 0);
6733 gnu_result = alloc_stmt_list ();
6734 break;
6736 case N_Defining_Program_Unit_Name:
6737 /* For a child unit identifier go up a level to get the specification.
6738 We get this when we try to find the spec of a child unit package
6739 that is the compilation unit being compiled. */
6740 gnu_result = gnat_to_gnu (Parent (gnat_node));
6741 break;
6743 case N_Subprogram_Body:
6744 Subprogram_Body_to_gnu (gnat_node);
6745 gnu_result = alloc_stmt_list ();
6746 break;
6748 case N_Function_Call:
6749 case N_Procedure_Call_Statement:
6750 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE, false);
6751 break;
6753 /************************/
6754 /* Chapter 7: Packages */
6755 /************************/
6757 case N_Package_Declaration:
6758 gnu_result = gnat_to_gnu (Specification (gnat_node));
6759 break;
6761 case N_Package_Specification:
6763 start_stmt_group ();
6764 process_decls (Visible_Declarations (gnat_node),
6765 Private_Declarations (gnat_node), Empty, true, true);
6766 gnu_result = end_stmt_group ();
6767 break;
6769 case N_Package_Body:
6771 /* If this is the body of a generic package - do nothing. */
6772 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
6774 gnu_result = alloc_stmt_list ();
6775 break;
6778 start_stmt_group ();
6779 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
6781 if (Present (Handled_Statement_Sequence (gnat_node)))
6782 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
6784 gnu_result = end_stmt_group ();
6785 break;
6787 /********************************/
6788 /* Chapter 8: Visibility Rules */
6789 /********************************/
6791 case N_Use_Package_Clause:
6792 case N_Use_Type_Clause:
6793 /* Nothing to do here - but these may appear in list of declarations. */
6794 gnu_result = alloc_stmt_list ();
6795 break;
6797 /*********************/
6798 /* Chapter 9: Tasks */
6799 /*********************/
6801 case N_Protected_Type_Declaration:
6802 gnu_result = alloc_stmt_list ();
6803 break;
6805 case N_Single_Task_Declaration:
6806 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, 1);
6807 gnu_result = alloc_stmt_list ();
6808 break;
6810 /*********************************************************/
6811 /* Chapter 10: Program Structure and Compilation Issues */
6812 /*********************************************************/
6814 case N_Compilation_Unit:
6815 /* This is not called for the main unit on which gigi is invoked. */
6816 Compilation_Unit_to_gnu (gnat_node);
6817 gnu_result = alloc_stmt_list ();
6818 break;
6820 case N_Subprogram_Body_Stub:
6821 case N_Package_Body_Stub:
6822 case N_Protected_Body_Stub:
6823 case N_Task_Body_Stub:
6824 /* Simply process whatever unit is being inserted. */
6825 if (Present (Library_Unit (gnat_node)))
6826 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
6827 else
6829 gcc_assert (type_annotate_only);
6830 gnu_result = alloc_stmt_list ();
6832 break;
6834 case N_Subunit:
6835 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
6836 break;
6838 /***************************/
6839 /* Chapter 11: Exceptions */
6840 /***************************/
6842 case N_Handled_Sequence_Of_Statements:
6843 /* If there is an At_End procedure attached to this node, and the EH
6844 mechanism is SJLJ, we must have at least a corresponding At_End
6845 handler, unless the No_Exception_Handlers restriction is set. */
6846 gcc_assert (type_annotate_only
6847 || Exception_Mechanism != Setjmp_Longjmp
6848 || No (At_End_Proc (gnat_node))
6849 || Present (Exception_Handlers (gnat_node))
6850 || No_Exception_Handlers_Set ());
6852 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
6853 break;
6855 case N_Exception_Handler:
6856 if (Exception_Mechanism == Setjmp_Longjmp)
6857 gnu_result = Exception_Handler_to_gnu_sjlj (gnat_node);
6858 else if (Exception_Mechanism == Back_End_Exceptions)
6859 gnu_result = Exception_Handler_to_gnu_zcx (gnat_node);
6860 else
6861 gcc_unreachable ();
6862 break;
6864 case N_Raise_Statement:
6865 /* Only for reraise in back-end exceptions mode. */
6866 gcc_assert (No (Name (gnat_node))
6867 && Exception_Mechanism == Back_End_Exceptions);
6869 start_stmt_group ();
6870 gnat_pushlevel ();
6872 /* Clear the current exception pointer so that the occurrence won't be
6873 deallocated. */
6874 gnu_expr = create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE,
6875 ptr_type_node, gnu_incoming_exc_ptr,
6876 false, false, false, false, NULL, gnat_node);
6878 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_incoming_exc_ptr,
6879 convert (ptr_type_node, integer_zero_node)));
6880 add_stmt (build_call_n_expr (reraise_zcx_decl, 1, gnu_expr));
6881 gnat_poplevel ();
6882 gnu_result = end_stmt_group ();
6883 break;
6885 case N_Push_Constraint_Error_Label:
6886 push_exception_label_stack (&gnu_constraint_error_label_stack,
6887 Exception_Label (gnat_node));
6888 break;
6890 case N_Push_Storage_Error_Label:
6891 push_exception_label_stack (&gnu_storage_error_label_stack,
6892 Exception_Label (gnat_node));
6893 break;
6895 case N_Push_Program_Error_Label:
6896 push_exception_label_stack (&gnu_program_error_label_stack,
6897 Exception_Label (gnat_node));
6898 break;
6900 case N_Pop_Constraint_Error_Label:
6901 gnu_constraint_error_label_stack->pop ();
6902 break;
6904 case N_Pop_Storage_Error_Label:
6905 gnu_storage_error_label_stack->pop ();
6906 break;
6908 case N_Pop_Program_Error_Label:
6909 gnu_program_error_label_stack->pop ();
6910 break;
6912 /******************************/
6913 /* Chapter 12: Generic Units */
6914 /******************************/
6916 case N_Generic_Function_Renaming_Declaration:
6917 case N_Generic_Package_Renaming_Declaration:
6918 case N_Generic_Procedure_Renaming_Declaration:
6919 case N_Generic_Package_Declaration:
6920 case N_Generic_Subprogram_Declaration:
6921 case N_Package_Instantiation:
6922 case N_Procedure_Instantiation:
6923 case N_Function_Instantiation:
6924 /* These nodes can appear on a declaration list but there is nothing to
6925 to be done with them. */
6926 gnu_result = alloc_stmt_list ();
6927 break;
6929 /**************************************************/
6930 /* Chapter 13: Representation Clauses and */
6931 /* Implementation-Dependent Features */
6932 /**************************************************/
6934 case N_Attribute_Definition_Clause:
6935 gnu_result = alloc_stmt_list ();
6937 /* The only one we need to deal with is 'Address since, for the others,
6938 the front-end puts the information elsewhere. */
6939 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
6940 break;
6942 /* And we only deal with 'Address if the object has a Freeze node. */
6943 gnat_temp = Entity (Name (gnat_node));
6944 if (No (Freeze_Node (gnat_temp)))
6945 break;
6947 /* Get the value to use as the address and save it as the equivalent
6948 for the object. When it is frozen, gnat_to_gnu_entity will do the
6949 right thing. */
6950 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
6951 break;
6953 case N_Enumeration_Representation_Clause:
6954 case N_Record_Representation_Clause:
6955 case N_At_Clause:
6956 /* We do nothing with these. SEM puts the information elsewhere. */
6957 gnu_result = alloc_stmt_list ();
6958 break;
6960 case N_Code_Statement:
6961 if (!type_annotate_only)
6963 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
6964 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
6965 tree gnu_clobbers = NULL_TREE, tail;
6966 bool allows_mem, allows_reg, fake;
6967 int ninputs, noutputs, i;
6968 const char **oconstraints;
6969 const char *constraint;
6970 char *clobber;
6972 /* First retrieve the 3 operand lists built by the front-end. */
6973 Setup_Asm_Outputs (gnat_node);
6974 while (Present (gnat_temp = Asm_Output_Variable ()))
6976 tree gnu_value = gnat_to_gnu (gnat_temp);
6977 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6978 (Asm_Output_Constraint ()));
6980 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
6981 Next_Asm_Output ();
6984 Setup_Asm_Inputs (gnat_node);
6985 while (Present (gnat_temp = Asm_Input_Value ()))
6987 tree gnu_value = gnat_to_gnu (gnat_temp);
6988 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
6989 (Asm_Input_Constraint ()));
6991 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
6992 Next_Asm_Input ();
6995 Clobber_Setup (gnat_node);
6996 while ((clobber = Clobber_Get_Next ()))
6997 gnu_clobbers
6998 = tree_cons (NULL_TREE,
6999 build_string (strlen (clobber) + 1, clobber),
7000 gnu_clobbers);
7002 /* Then perform some standard checking and processing on the
7003 operands. In particular, mark them addressable if needed. */
7004 gnu_outputs = nreverse (gnu_outputs);
7005 noutputs = list_length (gnu_outputs);
7006 gnu_inputs = nreverse (gnu_inputs);
7007 ninputs = list_length (gnu_inputs);
7008 oconstraints = XALLOCAVEC (const char *, noutputs);
7010 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
7012 tree output = TREE_VALUE (tail);
7013 constraint
7014 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7015 oconstraints[i] = constraint;
7017 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7018 &allows_mem, &allows_reg, &fake))
7020 /* If the operand is going to end up in memory,
7021 mark it addressable. Note that we don't test
7022 allows_mem like in the input case below; this
7023 is modelled on the C front-end. */
7024 if (!allows_reg)
7026 output = remove_conversions (output, false);
7027 if (TREE_CODE (output) == CONST_DECL
7028 && DECL_CONST_CORRESPONDING_VAR (output))
7029 output = DECL_CONST_CORRESPONDING_VAR (output);
7030 if (!gnat_mark_addressable (output))
7031 output = error_mark_node;
7034 else
7035 output = error_mark_node;
7037 TREE_VALUE (tail) = output;
7040 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
7042 tree input = TREE_VALUE (tail);
7043 constraint
7044 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7046 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
7047 0, oconstraints,
7048 &allows_mem, &allows_reg))
7050 /* If the operand is going to end up in memory,
7051 mark it addressable. */
7052 if (!allows_reg && allows_mem)
7054 input = remove_conversions (input, false);
7055 if (TREE_CODE (input) == CONST_DECL
7056 && DECL_CONST_CORRESPONDING_VAR (input))
7057 input = DECL_CONST_CORRESPONDING_VAR (input);
7058 if (!gnat_mark_addressable (input))
7059 input = error_mark_node;
7062 else
7063 input = error_mark_node;
7065 TREE_VALUE (tail) = input;
7068 gnu_result = build5 (ASM_EXPR, void_type_node,
7069 gnu_template, gnu_outputs,
7070 gnu_inputs, gnu_clobbers, NULL_TREE);
7071 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
7073 else
7074 gnu_result = alloc_stmt_list ();
7076 break;
7078 /****************/
7079 /* Added Nodes */
7080 /****************/
7082 case N_Expression_With_Actions:
7083 /* This construct doesn't define a scope so we don't push a binding level
7084 around the statement list; but we wrap it in a SAVE_EXPR to protect it
7085 from unsharing. */
7086 gnu_result = build_stmt_group (Actions (gnat_node), false);
7087 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
7088 TREE_SIDE_EFFECTS (gnu_result) = 1;
7089 gnu_expr = gnat_to_gnu (Expression (gnat_node));
7090 gnu_result
7091 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
7092 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7093 break;
7095 case N_Freeze_Entity:
7096 start_stmt_group ();
7097 process_freeze_entity (gnat_node);
7098 process_decls (Actions (gnat_node), Empty, Empty, true, true);
7099 gnu_result = end_stmt_group ();
7100 break;
7102 case N_Freeze_Generic_Entity:
7103 gnu_result = alloc_stmt_list ();
7104 break;
7106 case N_Itype_Reference:
7107 if (!present_gnu_tree (Itype (gnat_node)))
7108 process_type (Itype (gnat_node));
7110 gnu_result = alloc_stmt_list ();
7111 break;
7113 case N_Free_Statement:
7114 if (!type_annotate_only)
7116 tree gnu_ptr = gnat_to_gnu (Expression (gnat_node));
7117 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
7118 tree gnu_obj_type, gnu_actual_obj_type;
7120 /* If this is a thin pointer, we must first dereference it to create
7121 a fat pointer, then go back below to a thin pointer. The reason
7122 for this is that we need to have a fat pointer someplace in order
7123 to properly compute the size. */
7124 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7125 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
7126 build_unary_op (INDIRECT_REF, NULL_TREE,
7127 gnu_ptr));
7129 /* If this is a fat pointer, the object must have been allocated with
7130 the template in front of the array. So pass the template address,
7131 and get the total size; do it by converting to a thin pointer. */
7132 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
7133 gnu_ptr
7134 = convert (build_pointer_type
7135 (TYPE_OBJECT_RECORD_TYPE
7136 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
7137 gnu_ptr);
7139 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
7141 /* If this is a thin pointer, the object must have been allocated with
7142 the template in front of the array. So pass the template address,
7143 and get the total size. */
7144 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7145 gnu_ptr
7146 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
7147 gnu_ptr,
7148 fold_build1 (NEGATE_EXPR, sizetype,
7149 byte_position
7150 (DECL_CHAIN
7151 TYPE_FIELDS ((gnu_obj_type)))));
7153 /* If we have a special dynamic constrained subtype on the node, use
7154 it to compute the size; otherwise, use the designated subtype. */
7155 if (Present (Actual_Designated_Subtype (gnat_node)))
7157 gnu_actual_obj_type
7158 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
7160 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
7161 gnu_actual_obj_type
7162 = build_unc_object_type_from_ptr (gnu_ptr_type,
7163 gnu_actual_obj_type,
7164 get_identifier ("DEALLOC"),
7165 false);
7167 else
7168 gnu_actual_obj_type = gnu_obj_type;
7170 gnu_result
7171 = build_call_alloc_dealloc (gnu_ptr,
7172 TYPE_SIZE_UNIT (gnu_actual_obj_type),
7173 gnu_obj_type,
7174 Procedure_To_Call (gnat_node),
7175 Storage_Pool (gnat_node),
7176 gnat_node);
7178 break;
7180 case N_Raise_Constraint_Error:
7181 case N_Raise_Program_Error:
7182 case N_Raise_Storage_Error:
7183 if (type_annotate_only)
7184 gnu_result = alloc_stmt_list ();
7185 else
7186 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
7187 break;
7189 case N_Validate_Unchecked_Conversion:
7190 /* The only validation we currently do on an unchecked conversion is
7191 that of aliasing assumptions. */
7192 if (flag_strict_aliasing)
7193 gnat_validate_uc_list.safe_push (gnat_node);
7194 gnu_result = alloc_stmt_list ();
7195 break;
7197 case N_Function_Specification:
7198 case N_Procedure_Specification:
7199 case N_Op_Concat:
7200 case N_Component_Association:
7201 case N_Protected_Body:
7202 case N_Task_Body:
7203 /* These nodes should only be present when annotating types. */
7204 gcc_assert (type_annotate_only);
7205 gnu_result = alloc_stmt_list ();
7206 break;
7208 default:
7209 /* Other nodes are not supposed to reach here. */
7210 gcc_unreachable ();
7213 /* If we pushed the processing of the elaboration routine, pop it back. */
7214 if (went_into_elab_proc)
7215 current_function_decl = NULL_TREE;
7217 /* When not optimizing, turn boolean rvalues B into B != false tests
7218 so that the code just below can put the location information of the
7219 reference to B on the inequality operator for better debug info. */
7220 if (!optimize
7221 && TREE_CODE (gnu_result) != INTEGER_CST
7222 && (kind == N_Identifier
7223 || kind == N_Expanded_Name
7224 || kind == N_Explicit_Dereference
7225 || kind == N_Function_Call
7226 || kind == N_Indexed_Component
7227 || kind == N_Selected_Component)
7228 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
7229 && !lvalue_required_p (gnat_node, gnu_result_type, false, false, false))
7230 gnu_result = build_binary_op (NE_EXPR, gnu_result_type,
7231 convert (gnu_result_type, gnu_result),
7232 convert (gnu_result_type,
7233 boolean_false_node));
7235 /* Set the location information on the result. Note that we may have
7236 no result if we tried to build a CALL_EXPR node to a procedure with
7237 no side-effects and optimization is enabled. */
7238 if (gnu_result && EXPR_P (gnu_result))
7239 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7241 /* If we're supposed to return something of void_type, it means we have
7242 something we're elaborating for effect, so just return. */
7243 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
7244 return gnu_result;
7246 /* If the result is a constant that overflowed, raise Constraint_Error. */
7247 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
7249 post_error ("?`Constraint_Error` will be raised at run time", gnat_node);
7250 gnu_result
7251 = build1 (NULL_EXPR, gnu_result_type,
7252 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
7253 N_Raise_Constraint_Error));
7256 /* If the result has side-effects and is of an unconstrained type, make a
7257 SAVE_EXPR so that we can be sure it will only be referenced once. But
7258 this is useless for a call to a function that returns an unconstrained
7259 type with default discriminant, as we cannot compute the size of the
7260 actual returned object. We must do this before any conversions. */
7261 if (TREE_SIDE_EFFECTS (gnu_result)
7262 && !(TREE_CODE (gnu_result) == CALL_EXPR
7263 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7264 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
7265 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))))
7266 gnu_result = gnat_stabilize_reference (gnu_result, false, NULL);
7268 /* Now convert the result to the result type, unless we are in one of the
7269 following cases:
7271 1. If this is the LHS of an assignment or an actual parameter of a
7272 call, return the result almost unmodified since the RHS will have
7273 to be converted to our type in that case, unless the result type
7274 has a simpler size. Likewise if there is just a no-op unchecked
7275 conversion in-between. Similarly, don't convert integral types
7276 that are the operands of an unchecked conversion since we need
7277 to ignore those conversions (for 'Valid).
7279 2. If we have a label (which doesn't have any well-defined type), a
7280 field or an error, return the result almost unmodified. Similarly,
7281 if the two types are record types with the same name, don't convert.
7282 This will be the case when we are converting from a packable version
7283 of a type to its original type and we need those conversions to be
7284 NOPs in order for assignments into these types to work properly.
7286 3. If the type is void or if we have no result, return error_mark_node
7287 to show we have no result.
7289 4. If this a call to a function that returns an unconstrained type with
7290 default discriminant, return the call expression unmodified since we
7291 cannot compute the size of the actual returned object.
7293 5. Finally, if the type of the result is already correct. */
7295 if (Present (Parent (gnat_node))
7296 && (lhs_or_actual_p (gnat_node)
7297 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7298 && unchecked_conversion_nop (Parent (gnat_node)))
7299 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7300 && !AGGREGATE_TYPE_P (gnu_result_type)
7301 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
7302 && !(TYPE_SIZE (gnu_result_type)
7303 && TYPE_SIZE (TREE_TYPE (gnu_result))
7304 && (AGGREGATE_TYPE_P (gnu_result_type)
7305 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
7306 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
7307 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
7308 != INTEGER_CST))
7309 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
7310 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
7311 && (CONTAINS_PLACEHOLDER_P
7312 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
7313 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
7314 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
7316 /* Remove padding only if the inner object is of self-referential
7317 size: in that case it must be an object of unconstrained type
7318 with a default discriminant and we want to avoid copying too
7319 much data. */
7320 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7321 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
7322 (TREE_TYPE (gnu_result))))))
7323 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7324 gnu_result);
7327 else if (TREE_CODE (gnu_result) == LABEL_DECL
7328 || TREE_CODE (gnu_result) == FIELD_DECL
7329 || TREE_CODE (gnu_result) == ERROR_MARK
7330 || (TYPE_NAME (gnu_result_type)
7331 == TYPE_NAME (TREE_TYPE (gnu_result))
7332 && TREE_CODE (gnu_result_type) == RECORD_TYPE
7333 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
7335 /* Remove any padding. */
7336 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7337 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7338 gnu_result);
7341 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
7342 gnu_result = error_mark_node;
7344 else if (TREE_CODE (gnu_result) == CALL_EXPR
7345 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_result))
7346 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result)))
7347 == gnu_result_type
7348 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
7351 else if (TREE_TYPE (gnu_result) != gnu_result_type)
7352 gnu_result = convert (gnu_result_type, gnu_result);
7354 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
7355 while ((TREE_CODE (gnu_result) == NOP_EXPR
7356 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
7357 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
7358 gnu_result = TREE_OPERAND (gnu_result, 0);
7360 return gnu_result;
7363 /* Subroutine of above to push the exception label stack. GNU_STACK is
7364 a pointer to the stack to update and GNAT_LABEL, if present, is the
7365 label to push onto the stack. */
7367 static void
7368 push_exception_label_stack (vec<tree, va_gc> **gnu_stack, Entity_Id gnat_label)
7370 tree gnu_label = (Present (gnat_label)
7371 ? gnat_to_gnu_entity (gnat_label, NULL_TREE, 0)
7372 : NULL_TREE);
7374 vec_safe_push (*gnu_stack, gnu_label);
7377 /* Record the current code position in GNAT_NODE. */
7379 static void
7380 record_code_position (Node_Id gnat_node)
7382 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
7384 add_stmt_with_node (stmt_stmt, gnat_node);
7385 save_gnu_tree (gnat_node, stmt_stmt, true);
7388 /* Insert the code for GNAT_NODE at the position saved for that node. */
7390 static void
7391 insert_code_for (Node_Id gnat_node)
7393 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = gnat_to_gnu (gnat_node);
7394 save_gnu_tree (gnat_node, NULL_TREE, true);
7397 /* Start a new statement group chained to the previous group. */
7399 void
7400 start_stmt_group (void)
7402 struct stmt_group *group = stmt_group_free_list;
7404 /* First see if we can get one from the free list. */
7405 if (group)
7406 stmt_group_free_list = group->previous;
7407 else
7408 group = ggc_alloc<stmt_group> ();
7410 group->previous = current_stmt_group;
7411 group->stmt_list = group->block = group->cleanups = NULL_TREE;
7412 current_stmt_group = group;
7415 /* Add GNU_STMT to the current statement group. If it is an expression with
7416 no effects, it is ignored. */
7418 void
7419 add_stmt (tree gnu_stmt)
7421 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
7424 /* Similar, but the statement is always added, regardless of side-effects. */
7426 void
7427 add_stmt_force (tree gnu_stmt)
7429 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
7432 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
7434 void
7435 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
7437 /* Do not emit a location for renamings that come from generic instantiation,
7438 they are likely to disturb debugging. */
7439 if (Present (gnat_node)
7440 && !renaming_from_generic_instantiation_p (gnat_node))
7441 set_expr_location_from_node (gnu_stmt, gnat_node);
7442 add_stmt (gnu_stmt);
7445 /* Similar, but the statement is always added, regardless of side-effects. */
7447 void
7448 add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
7450 if (Present (gnat_node))
7451 set_expr_location_from_node (gnu_stmt, gnat_node);
7452 add_stmt_force (gnu_stmt);
7455 /* Add a declaration statement for GNU_DECL to the current statement group.
7456 Get SLOC from Entity_Id. */
7458 void
7459 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
7461 tree type = TREE_TYPE (gnu_decl);
7462 tree gnu_stmt, gnu_init, t;
7464 /* If this is a variable that Gigi is to ignore, we may have been given
7465 an ERROR_MARK. So test for it. We also might have been given a
7466 reference for a renaming. So only do something for a decl. Also
7467 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
7468 if (!DECL_P (gnu_decl)
7469 || (TREE_CODE (gnu_decl) == TYPE_DECL
7470 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
7471 return;
7473 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
7475 /* If we are external or global, we don't want to output the DECL_EXPR for
7476 this DECL node since we already have evaluated the expressions in the
7477 sizes and positions as globals and doing it again would be wrong. */
7478 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
7480 /* Mark everything as used to prevent node sharing with subprograms.
7481 Note that walk_tree knows how to deal with TYPE_DECL, but neither
7482 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
7483 MARK_VISITED (gnu_stmt);
7484 if (TREE_CODE (gnu_decl) == VAR_DECL
7485 || TREE_CODE (gnu_decl) == CONST_DECL)
7487 MARK_VISITED (DECL_SIZE (gnu_decl));
7488 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
7489 MARK_VISITED (DECL_INITIAL (gnu_decl));
7491 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
7492 else if (TREE_CODE (gnu_decl) == TYPE_DECL
7493 && RECORD_OR_UNION_TYPE_P (type)
7494 && !TYPE_FAT_POINTER_P (type))
7495 MARK_VISITED (TYPE_ADA_SIZE (type));
7497 else
7498 add_stmt_with_node (gnu_stmt, gnat_entity);
7500 /* If this is a variable and an initializer is attached to it, it must be
7501 valid for the context. Similar to init_const in create_var_decl_1. */
7502 if (TREE_CODE (gnu_decl) == VAR_DECL
7503 && (gnu_init = DECL_INITIAL (gnu_decl)) != NULL_TREE
7504 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
7505 || (TREE_STATIC (gnu_decl)
7506 && !initializer_constant_valid_p (gnu_init,
7507 TREE_TYPE (gnu_init)))))
7509 /* If GNU_DECL has a padded type, convert it to the unpadded
7510 type so the assignment is done properly. */
7511 if (TYPE_IS_PADDING_P (type))
7512 t = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
7513 else
7514 t = gnu_decl;
7516 gnu_stmt = build_binary_op (INIT_EXPR, NULL_TREE, t, gnu_init);
7518 DECL_INITIAL (gnu_decl) = NULL_TREE;
7519 if (TREE_READONLY (gnu_decl))
7521 TREE_READONLY (gnu_decl) = 0;
7522 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
7525 add_stmt_with_node (gnu_stmt, gnat_entity);
7529 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
7531 static tree
7532 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
7534 tree t = *tp;
7536 if (TREE_VISITED (t))
7537 *walk_subtrees = 0;
7539 /* Don't mark a dummy type as visited because we want to mark its sizes
7540 and fields once it's filled in. */
7541 else if (!TYPE_IS_DUMMY_P (t))
7542 TREE_VISITED (t) = 1;
7544 if (TYPE_P (t))
7545 TYPE_SIZES_GIMPLIFIED (t) = 1;
7547 return NULL_TREE;
7550 /* Mark nodes rooted at T with TREE_VISITED and types as having their
7551 sized gimplified. We use this to indicate all variable sizes and
7552 positions in global types may not be shared by any subprogram. */
7554 void
7555 mark_visited (tree t)
7557 walk_tree (&t, mark_visited_r, NULL, NULL);
7560 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
7561 set its location to that of GNAT_NODE if present, but with column info
7562 cleared so that conditional branches generated as part of the cleanup
7563 code do not interfere with coverage analysis tools. */
7565 static void
7566 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
7568 if (Present (gnat_node))
7569 set_expr_location_from_node1 (gnu_cleanup, gnat_node, true);
7570 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
7573 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
7575 void
7576 set_block_for_group (tree gnu_block)
7578 gcc_assert (!current_stmt_group->block);
7579 current_stmt_group->block = gnu_block;
7582 /* Return code corresponding to the current code group. It is normally
7583 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
7584 BLOCK or cleanups were set. */
7586 tree
7587 end_stmt_group (void)
7589 struct stmt_group *group = current_stmt_group;
7590 tree gnu_retval = group->stmt_list;
7592 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
7593 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
7594 make a BIND_EXPR. Note that we nest in that because the cleanup may
7595 reference variables in the block. */
7596 if (gnu_retval == NULL_TREE)
7597 gnu_retval = alloc_stmt_list ();
7599 if (group->cleanups)
7600 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
7601 group->cleanups);
7603 if (current_stmt_group->block)
7604 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
7605 gnu_retval, group->block);
7607 /* Remove this group from the stack and add it to the free list. */
7608 current_stmt_group = group->previous;
7609 group->previous = stmt_group_free_list;
7610 stmt_group_free_list = group;
7612 return gnu_retval;
7615 /* Return whether the current statement group may fall through. */
7617 static inline bool
7618 stmt_group_may_fallthru (void)
7620 if (current_stmt_group->stmt_list)
7621 return block_may_fallthru (current_stmt_group->stmt_list);
7622 else
7623 return true;
7626 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
7627 statements.*/
7629 static void
7630 add_stmt_list (List_Id gnat_list)
7632 Node_Id gnat_node;
7634 if (Present (gnat_list))
7635 for (gnat_node = First (gnat_list); Present (gnat_node);
7636 gnat_node = Next (gnat_node))
7637 add_stmt (gnat_to_gnu (gnat_node));
7640 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
7641 If BINDING_P is true, push and pop a binding level around the list. */
7643 static tree
7644 build_stmt_group (List_Id gnat_list, bool binding_p)
7646 start_stmt_group ();
7647 if (binding_p)
7648 gnat_pushlevel ();
7650 add_stmt_list (gnat_list);
7651 if (binding_p)
7652 gnat_poplevel ();
7654 return end_stmt_group ();
7657 /* Generate GIMPLE in place for the expression at *EXPR_P. */
7660 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
7661 gimple_seq *post_p ATTRIBUTE_UNUSED)
7663 tree expr = *expr_p;
7664 tree type = TREE_TYPE (expr);
7665 tree op;
7667 if (IS_ADA_STMT (expr))
7668 return gnat_gimplify_stmt (expr_p);
7670 switch (TREE_CODE (expr))
7672 case NULL_EXPR:
7673 /* If this is an aggregate type, build a null pointer of the appropriate
7674 type and dereference it. */
7675 if (AGGREGATE_TYPE_P (type)
7676 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
7677 *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE,
7678 convert (build_pointer_type (type),
7679 integer_zero_node));
7680 /* Otherwise, just make a VAR_DECL. */
7681 else
7683 *expr_p = create_tmp_var (type, NULL);
7684 TREE_NO_WARNING (*expr_p) = 1;
7687 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
7688 return GS_OK;
7690 case UNCONSTRAINED_ARRAY_REF:
7691 /* We should only do this if we are just elaborating for side-effects,
7692 but we can't know that yet. */
7693 *expr_p = TREE_OPERAND (*expr_p, 0);
7694 return GS_OK;
7696 case ADDR_EXPR:
7697 op = TREE_OPERAND (expr, 0);
7699 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
7700 is put into static memory. We know that it's going to be read-only
7701 given the semantics we have and it must be in static memory when the
7702 reference is in an elaboration procedure. */
7703 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
7705 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
7706 *expr_p = fold_convert (type, addr);
7707 return GS_ALL_DONE;
7710 return GS_UNHANDLED;
7712 case VIEW_CONVERT_EXPR:
7713 op = TREE_OPERAND (expr, 0);
7715 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
7716 type to a scalar one, explicitly create the local temporary. That's
7717 required if the type is passed by reference. */
7718 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
7719 && AGGREGATE_TYPE_P (TREE_TYPE (op))
7720 && !AGGREGATE_TYPE_P (type))
7722 tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
7723 gimple_add_tmp_var (new_var);
7725 mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
7726 gimplify_and_add (mod, pre_p);
7728 TREE_OPERAND (expr, 0) = new_var;
7729 return GS_OK;
7732 return GS_UNHANDLED;
7734 case DECL_EXPR:
7735 op = DECL_EXPR_DECL (expr);
7737 /* The expressions for the RM bounds must be gimplified to ensure that
7738 they are properly elaborated. See gimplify_decl_expr. */
7739 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
7740 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)))
7741 switch (TREE_CODE (TREE_TYPE (op)))
7743 case INTEGER_TYPE:
7744 case ENUMERAL_TYPE:
7745 case BOOLEAN_TYPE:
7746 case REAL_TYPE:
7748 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
7750 val = TYPE_RM_MIN_VALUE (type);
7751 if (val)
7753 gimplify_one_sizepos (&val, pre_p);
7754 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7755 SET_TYPE_RM_MIN_VALUE (t, val);
7758 val = TYPE_RM_MAX_VALUE (type);
7759 if (val)
7761 gimplify_one_sizepos (&val, pre_p);
7762 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
7763 SET_TYPE_RM_MAX_VALUE (t, val);
7767 break;
7769 default:
7770 break;
7773 /* ... fall through ... */
7775 default:
7776 return GS_UNHANDLED;
7780 /* Generate GIMPLE in place for the statement at *STMT_P. */
7782 static enum gimplify_status
7783 gnat_gimplify_stmt (tree *stmt_p)
7785 tree stmt = *stmt_p;
7787 switch (TREE_CODE (stmt))
7789 case STMT_STMT:
7790 *stmt_p = STMT_STMT_STMT (stmt);
7791 return GS_OK;
7793 case LOOP_STMT:
7795 tree gnu_start_label = create_artificial_label (input_location);
7796 tree gnu_cond = LOOP_STMT_COND (stmt);
7797 tree gnu_update = LOOP_STMT_UPDATE (stmt);
7798 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
7800 /* Build the condition expression from the test, if any. */
7801 if (gnu_cond)
7803 /* Deal with the optimization hints. */
7804 if (LOOP_STMT_IVDEP (stmt))
7805 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7806 build_int_cst (integer_type_node,
7807 annot_expr_ivdep_kind));
7808 if (LOOP_STMT_NO_VECTOR (stmt))
7809 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7810 build_int_cst (integer_type_node,
7811 annot_expr_no_vector_kind));
7812 if (LOOP_STMT_VECTOR (stmt))
7813 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
7814 build_int_cst (integer_type_node,
7815 annot_expr_vector_kind));
7817 gnu_cond
7818 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
7819 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
7822 /* Set to emit the statements of the loop. */
7823 *stmt_p = NULL_TREE;
7825 /* We first emit the start label and then a conditional jump to the
7826 end label if there's a top condition, then the update if it's at
7827 the top, then the body of the loop, then a conditional jump to
7828 the end label if there's a bottom condition, then the update if
7829 it's at the bottom, and finally a jump to the start label and the
7830 definition of the end label. */
7831 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7832 gnu_start_label),
7833 stmt_p);
7835 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
7836 append_to_statement_list (gnu_cond, stmt_p);
7838 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
7839 append_to_statement_list (gnu_update, stmt_p);
7841 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
7843 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
7844 append_to_statement_list (gnu_cond, stmt_p);
7846 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
7847 append_to_statement_list (gnu_update, stmt_p);
7849 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
7850 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
7851 append_to_statement_list (t, stmt_p);
7853 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
7854 gnu_end_label),
7855 stmt_p);
7856 return GS_OK;
7859 case EXIT_STMT:
7860 /* Build a statement to jump to the corresponding end label, then
7861 see if it needs to be conditional. */
7862 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
7863 if (EXIT_STMT_COND (stmt))
7864 *stmt_p = build3 (COND_EXPR, void_type_node,
7865 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
7866 return GS_OK;
7868 default:
7869 gcc_unreachable ();
7873 /* Force references to each of the entities in packages withed by GNAT_NODE.
7874 Operate recursively but check that we aren't elaborating something more
7875 than once.
7877 This routine is exclusively called in type_annotate mode, to compute DDA
7878 information for types in withed units, for ASIS use. */
7880 static void
7881 elaborate_all_entities (Node_Id gnat_node)
7883 Entity_Id gnat_with_clause, gnat_entity;
7885 /* Process each unit only once. As we trace the context of all relevant
7886 units transitively, including generic bodies, we may encounter the
7887 same generic unit repeatedly. */
7888 if (!present_gnu_tree (gnat_node))
7889 save_gnu_tree (gnat_node, integer_zero_node, true);
7891 /* Save entities in all context units. A body may have an implicit_with
7892 on its own spec, if the context includes a child unit, so don't save
7893 the spec twice. */
7894 for (gnat_with_clause = First (Context_Items (gnat_node));
7895 Present (gnat_with_clause);
7896 gnat_with_clause = Next (gnat_with_clause))
7897 if (Nkind (gnat_with_clause) == N_With_Clause
7898 && !present_gnu_tree (Library_Unit (gnat_with_clause))
7899 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
7901 elaborate_all_entities (Library_Unit (gnat_with_clause));
7903 if (Ekind (Entity (Name (gnat_with_clause))) == E_Package)
7905 for (gnat_entity = First_Entity (Entity (Name (gnat_with_clause)));
7906 Present (gnat_entity);
7907 gnat_entity = Next_Entity (gnat_entity))
7908 if (Is_Public (gnat_entity)
7909 && Convention (gnat_entity) != Convention_Intrinsic
7910 && Ekind (gnat_entity) != E_Package
7911 && Ekind (gnat_entity) != E_Package_Body
7912 && Ekind (gnat_entity) != E_Operator
7913 && !(IN (Ekind (gnat_entity), Type_Kind)
7914 && !Is_Frozen (gnat_entity))
7915 && !((Ekind (gnat_entity) == E_Procedure
7916 || Ekind (gnat_entity) == E_Function)
7917 && Is_Intrinsic_Subprogram (gnat_entity))
7918 && !IN (Ekind (gnat_entity), Named_Kind)
7919 && !IN (Ekind (gnat_entity), Generic_Unit_Kind))
7920 gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
7922 else if (Ekind (Entity (Name (gnat_with_clause))) == E_Generic_Package)
7924 Node_Id gnat_body
7925 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause)));
7927 /* Retrieve compilation unit node of generic body. */
7928 while (Present (gnat_body)
7929 && Nkind (gnat_body) != N_Compilation_Unit)
7930 gnat_body = Parent (gnat_body);
7932 /* If body is available, elaborate its context. */
7933 if (Present (gnat_body))
7934 elaborate_all_entities (gnat_body);
7938 if (Nkind (Unit (gnat_node)) == N_Package_Body)
7939 elaborate_all_entities (Library_Unit (gnat_node));
7942 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
7944 static void
7945 process_freeze_entity (Node_Id gnat_node)
7947 const Entity_Id gnat_entity = Entity (gnat_node);
7948 const Entity_Kind kind = Ekind (gnat_entity);
7949 tree gnu_old, gnu_new;
7951 /* If this is a package, we need to generate code for the package. */
7952 if (kind == E_Package)
7954 insert_code_for
7955 (Parent (Corresponding_Body
7956 (Parent (Declaration_Node (gnat_entity)))));
7957 return;
7960 /* Don't do anything for class-wide types as they are always transformed
7961 into their root type. */
7962 if (kind == E_Class_Wide_Type)
7963 return;
7965 /* Check for an old definition. This freeze node might be for an Itype. */
7966 gnu_old
7967 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
7969 /* If this entity has an address representation clause, GNU_OLD is the
7970 address, so discard it here. */
7971 if (Present (Address_Clause (gnat_entity)))
7972 gnu_old = NULL_TREE;
7974 /* Don't do anything for subprograms that may have been elaborated before
7975 their freeze nodes. This can happen, for example, because of an inner
7976 call in an instance body or because of previous compilation of a spec
7977 for inlining purposes. */
7978 if (gnu_old
7979 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
7980 && (kind == E_Function || kind == E_Procedure))
7981 || (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
7982 && kind == E_Subprogram_Type)))
7983 return;
7985 /* If we have a non-dummy type old tree, we have nothing to do, except
7986 aborting if this is the public view of a private type whose full view was
7987 not delayed, as this node was never delayed as it should have been. We
7988 let this happen for concurrent types and their Corresponding_Record_Type,
7989 however, because each might legitimately be elaborated before its own
7990 freeze node, e.g. while processing the other. */
7991 if (gnu_old
7992 && !(TREE_CODE (gnu_old) == TYPE_DECL
7993 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
7995 gcc_assert ((IN (kind, Incomplete_Or_Private_Kind)
7996 && Present (Full_View (gnat_entity))
7997 && No (Freeze_Node (Full_View (gnat_entity))))
7998 || Is_Concurrent_Type (gnat_entity)
7999 || (IN (kind, Record_Kind)
8000 && Is_Concurrent_Record_Type (gnat_entity)));
8001 return;
8004 /* Reset the saved tree, if any, and elaborate the object or type for real.
8005 If there is a full view, elaborate it and use the result. And, if this
8006 is the root type of a class-wide type, reuse it for the latter. */
8007 if (gnu_old)
8009 save_gnu_tree (gnat_entity, NULL_TREE, false);
8011 if (IN (kind, Incomplete_Or_Private_Kind)
8012 && Present (Full_View (gnat_entity)))
8014 Entity_Id full_view = Full_View (gnat_entity);
8016 save_gnu_tree (full_view, NULL_TREE, false);
8018 if (IN (Ekind (full_view), Private_Kind)
8019 && Present (Underlying_Full_View (full_view)))
8021 full_view = Underlying_Full_View (full_view);
8022 save_gnu_tree (full_view, NULL_TREE, false);
8026 if (IN (kind, Type_Kind)
8027 && Present (Class_Wide_Type (gnat_entity))
8028 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8029 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
8032 if (IN (kind, Incomplete_Or_Private_Kind)
8033 && Present (Full_View (gnat_entity)))
8035 Entity_Id full_view = Full_View (gnat_entity);
8037 if (IN (Ekind (full_view), Private_Kind)
8038 && Present (Underlying_Full_View (full_view)))
8039 full_view = Underlying_Full_View (full_view);
8041 gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, 1);
8043 /* Propagate back-annotations from full view to partial view. */
8044 if (Unknown_Alignment (gnat_entity))
8045 Set_Alignment (gnat_entity, Alignment (full_view));
8047 if (Unknown_Esize (gnat_entity))
8048 Set_Esize (gnat_entity, Esize (full_view));
8050 if (Unknown_RM_Size (gnat_entity))
8051 Set_RM_Size (gnat_entity, RM_Size (full_view));
8053 /* The above call may have defined this entity (the simplest example
8054 of this is when we have a private enumeral type since the bounds
8055 will have the public view). */
8056 if (!present_gnu_tree (gnat_entity))
8057 save_gnu_tree (gnat_entity, gnu_new, false);
8059 else
8061 tree gnu_init
8062 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
8063 && present_gnu_tree (Declaration_Node (gnat_entity)))
8064 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
8066 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, 1);
8069 if (IN (kind, Type_Kind)
8070 && Present (Class_Wide_Type (gnat_entity))
8071 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8072 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
8074 /* If we have an old type and we've made pointers to this type, update those
8075 pointers. If this is a Taft amendment type in the main unit, we need to
8076 mark the type as used since other units referencing it don't see the full
8077 declaration and, therefore, cannot mark it as used themselves. */
8078 if (gnu_old)
8080 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8081 TREE_TYPE (gnu_new));
8082 if (DECL_TAFT_TYPE_P (gnu_old))
8083 used_types_insert (TREE_TYPE (gnu_new));
8087 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
8088 We make two passes, one to elaborate anything other than bodies (but
8089 we declare a function if there was no spec). The second pass
8090 elaborates the bodies.
8092 GNAT_END_LIST gives the element in the list past the end. Normally,
8093 this is Empty, but can be First_Real_Statement for a
8094 Handled_Sequence_Of_Statements.
8096 We make a complete pass through both lists if PASS1P is true, then make
8097 the second pass over both lists if PASS2P is true. The lists usually
8098 correspond to the public and private parts of a package. */
8100 static void
8101 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
8102 Node_Id gnat_end_list, bool pass1p, bool pass2p)
8104 List_Id gnat_decl_array[2];
8105 Node_Id gnat_decl;
8106 int i;
8108 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
8110 if (pass1p)
8111 for (i = 0; i <= 1; i++)
8112 if (Present (gnat_decl_array[i]))
8113 for (gnat_decl = First (gnat_decl_array[i]);
8114 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8116 /* For package specs, we recurse inside the declarations,
8117 thus taking the two pass approach inside the boundary. */
8118 if (Nkind (gnat_decl) == N_Package_Declaration
8119 && (Nkind (Specification (gnat_decl)
8120 == N_Package_Specification)))
8121 process_decls (Visible_Declarations (Specification (gnat_decl)),
8122 Private_Declarations (Specification (gnat_decl)),
8123 Empty, true, false);
8125 /* Similarly for any declarations in the actions of a
8126 freeze node. */
8127 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8129 process_freeze_entity (gnat_decl);
8130 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
8133 /* Package bodies with freeze nodes get their elaboration deferred
8134 until the freeze node, but the code must be placed in the right
8135 place, so record the code position now. */
8136 else if (Nkind (gnat_decl) == N_Package_Body
8137 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
8138 record_code_position (gnat_decl);
8140 else if (Nkind (gnat_decl) == N_Package_Body_Stub
8141 && Present (Library_Unit (gnat_decl))
8142 && Present (Freeze_Node
8143 (Corresponding_Spec
8144 (Proper_Body (Unit
8145 (Library_Unit (gnat_decl)))))))
8146 record_code_position
8147 (Proper_Body (Unit (Library_Unit (gnat_decl))));
8149 /* We defer most subprogram bodies to the second pass. */
8150 else if (Nkind (gnat_decl) == N_Subprogram_Body)
8152 if (Acts_As_Spec (gnat_decl))
8154 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
8156 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
8157 && Ekind (gnat_subprog_id) != E_Generic_Function)
8158 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8162 /* For bodies and stubs that act as their own specs, the entity
8163 itself must be elaborated in the first pass, because it may
8164 be used in other declarations. */
8165 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
8167 Node_Id gnat_subprog_id
8168 = Defining_Entity (Specification (gnat_decl));
8170 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
8171 && Ekind (gnat_subprog_id) != E_Generic_Procedure
8172 && Ekind (gnat_subprog_id) != E_Generic_Function)
8173 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, 1);
8176 /* Concurrent stubs stand for the corresponding subprogram bodies,
8177 which are deferred like other bodies. */
8178 else if (Nkind (gnat_decl) == N_Task_Body_Stub
8179 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8182 else
8183 add_stmt (gnat_to_gnu (gnat_decl));
8186 /* Here we elaborate everything we deferred above except for package bodies,
8187 which are elaborated at their freeze nodes. Note that we must also
8188 go inside things (package specs and freeze nodes) the first pass did. */
8189 if (pass2p)
8190 for (i = 0; i <= 1; i++)
8191 if (Present (gnat_decl_array[i]))
8192 for (gnat_decl = First (gnat_decl_array[i]);
8193 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8195 if (Nkind (gnat_decl) == N_Subprogram_Body
8196 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
8197 || Nkind (gnat_decl) == N_Task_Body_Stub
8198 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8199 add_stmt (gnat_to_gnu (gnat_decl));
8201 else if (Nkind (gnat_decl) == N_Package_Declaration
8202 && (Nkind (Specification (gnat_decl)
8203 == N_Package_Specification)))
8204 process_decls (Visible_Declarations (Specification (gnat_decl)),
8205 Private_Declarations (Specification (gnat_decl)),
8206 Empty, false, true);
8208 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8209 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
8213 /* Make a unary operation of kind CODE using build_unary_op, but guard
8214 the operation by an overflow check. CODE can be one of NEGATE_EXPR
8215 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
8216 the operation is to be performed in that type. GNAT_NODE is the gnat
8217 node conveying the source location for which the error should be
8218 signaled. */
8220 static tree
8221 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
8222 Node_Id gnat_node)
8224 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
8226 operand = gnat_protect_expr (operand);
8228 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
8229 operand, TYPE_MIN_VALUE (gnu_type)),
8230 build_unary_op (code, gnu_type, operand),
8231 CE_Overflow_Check_Failed, gnat_node);
8234 /* Make a binary operation of kind CODE using build_binary_op, but guard
8235 the operation by an overflow check. CODE can be one of PLUS_EXPR,
8236 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
8237 Usually the operation is to be performed in that type. GNAT_NODE is
8238 the GNAT node conveying the source location for which the error should
8239 be signaled. */
8241 static tree
8242 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
8243 tree right, Node_Id gnat_node)
8245 const unsigned int precision = TYPE_PRECISION (gnu_type);
8246 tree lhs = gnat_protect_expr (left);
8247 tree rhs = gnat_protect_expr (right);
8248 tree type_max = TYPE_MAX_VALUE (gnu_type);
8249 tree type_min = TYPE_MIN_VALUE (gnu_type);
8250 tree zero = convert (gnu_type, integer_zero_node);
8251 tree gnu_expr, rhs_lt_zero, tmp1, tmp2;
8252 tree check_pos, check_neg, check;
8254 /* Assert that the precision is a power of 2. */
8255 gcc_assert ((precision & (precision - 1)) == 0);
8257 /* Prefer a constant or known-positive rhs to simplify checks. */
8258 if (!TREE_CONSTANT (rhs)
8259 && commutative_tree_code (code)
8260 && (TREE_CONSTANT (lhs)
8261 || (!tree_expr_nonnegative_p (rhs)
8262 && tree_expr_nonnegative_p (lhs))))
8264 tree tmp = lhs;
8265 lhs = rhs;
8266 rhs = tmp;
8269 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
8271 /* If we can fold the expression to a constant, just return it.
8272 The caller will deal with overflow, no need to generate a check. */
8273 if (TREE_CONSTANT (gnu_expr))
8274 return gnu_expr;
8276 rhs_lt_zero = tree_expr_nonnegative_p (rhs)
8277 ? boolean_false_node
8278 : build_binary_op (LT_EXPR, boolean_type_node, rhs, zero);
8280 /* ??? Should use more efficient check for operand_equal_p (lhs, rhs, 0) */
8282 /* Try a few strategies that may be cheaper than the general
8283 code at the end of the function, if the rhs is not known.
8284 The strategies are:
8285 - Call library function for 64-bit multiplication (complex)
8286 - Widen, if input arguments are sufficiently small
8287 - Determine overflow using wrapped result for addition/subtraction. */
8289 if (!TREE_CONSTANT (rhs))
8291 /* Even for add/subtract double size to get another base type. */
8292 const unsigned int needed_precision = precision * 2;
8294 if (code == MULT_EXPR && precision == 64)
8296 tree int_64 = gnat_type_for_size (64, 0);
8298 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
8299 convert (int_64, lhs),
8300 convert (int_64, rhs)));
8303 if (needed_precision <= BITS_PER_WORD
8304 || (code == MULT_EXPR && needed_precision <= LONG_LONG_TYPE_SIZE))
8306 tree wide_type = gnat_type_for_size (needed_precision, 0);
8307 tree wide_result = build_binary_op (code, wide_type,
8308 convert (wide_type, lhs),
8309 convert (wide_type, rhs));
8311 check = build_binary_op
8312 (TRUTH_ORIF_EXPR, boolean_type_node,
8313 build_binary_op (LT_EXPR, boolean_type_node, wide_result,
8314 convert (wide_type, type_min)),
8315 build_binary_op (GT_EXPR, boolean_type_node, wide_result,
8316 convert (wide_type, type_max)));
8318 return
8319 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8322 if (code == PLUS_EXPR || code == MINUS_EXPR)
8324 tree unsigned_type = gnat_type_for_size (precision, 1);
8325 tree wrapped_expr
8326 = convert (gnu_type,
8327 build_binary_op (code, unsigned_type,
8328 convert (unsigned_type, lhs),
8329 convert (unsigned_type, rhs)));
8331 /* Overflow when (rhs < 0) ^ (wrapped_expr < lhs)), for addition
8332 or when (rhs < 0) ^ (wrapped_expr > lhs) for subtraction. */
8333 check
8334 = build_binary_op (TRUTH_XOR_EXPR, boolean_type_node, rhs_lt_zero,
8335 build_binary_op (code == PLUS_EXPR
8336 ? LT_EXPR : GT_EXPR,
8337 boolean_type_node,
8338 wrapped_expr, lhs));
8340 return
8341 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8345 switch (code)
8347 case PLUS_EXPR:
8348 /* When rhs >= 0, overflow when lhs > type_max - rhs. */
8349 check_pos = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8350 build_binary_op (MINUS_EXPR, gnu_type,
8351 type_max, rhs)),
8353 /* When rhs < 0, overflow when lhs < type_min - rhs. */
8354 check_neg = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8355 build_binary_op (MINUS_EXPR, gnu_type,
8356 type_min, rhs));
8357 break;
8359 case MINUS_EXPR:
8360 /* When rhs >= 0, overflow when lhs < type_min + rhs. */
8361 check_pos = build_binary_op (LT_EXPR, boolean_type_node, lhs,
8362 build_binary_op (PLUS_EXPR, gnu_type,
8363 type_min, rhs)),
8365 /* When rhs < 0, overflow when lhs > type_max + rhs. */
8366 check_neg = build_binary_op (GT_EXPR, boolean_type_node, lhs,
8367 build_binary_op (PLUS_EXPR, gnu_type,
8368 type_max, rhs));
8369 break;
8371 case MULT_EXPR:
8372 /* The check here is designed to be efficient if the rhs is constant,
8373 but it will work for any rhs by using integer division.
8374 Four different check expressions determine whether X * C overflows,
8375 depending on C.
8376 C == 0 => false
8377 C > 0 => X > type_max / C || X < type_min / C
8378 C == -1 => X == type_min
8379 C < -1 => X > type_min / C || X < type_max / C */
8381 tmp1 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
8382 tmp2 = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
8384 check_pos
8385 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
8386 build_binary_op (NE_EXPR, boolean_type_node, zero,
8387 rhs),
8388 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8389 build_binary_op (GT_EXPR,
8390 boolean_type_node,
8391 lhs, tmp1),
8392 build_binary_op (LT_EXPR,
8393 boolean_type_node,
8394 lhs, tmp2)));
8396 check_neg
8397 = fold_build3 (COND_EXPR, boolean_type_node,
8398 build_binary_op (EQ_EXPR, boolean_type_node, rhs,
8399 build_int_cst (gnu_type, -1)),
8400 build_binary_op (EQ_EXPR, boolean_type_node, lhs,
8401 type_min),
8402 build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8403 build_binary_op (GT_EXPR,
8404 boolean_type_node,
8405 lhs, tmp2),
8406 build_binary_op (LT_EXPR,
8407 boolean_type_node,
8408 lhs, tmp1)));
8409 break;
8411 default:
8412 gcc_unreachable();
8415 check = fold_build3 (COND_EXPR, boolean_type_node, rhs_lt_zero, check_neg,
8416 check_pos);
8418 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
8421 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
8422 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
8423 which we have to check. GNAT_NODE is the GNAT node conveying the source
8424 location for which the error should be signaled. */
8426 static tree
8427 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
8429 tree gnu_range_type = get_unpadded_type (gnat_range_type);
8430 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
8432 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
8433 This can for example happen when translating 'Val or 'Value. */
8434 if (gnu_compare_type == gnu_range_type)
8435 return gnu_expr;
8437 /* Range checks can only be applied to types with ranges. */
8438 gcc_assert (INTEGRAL_TYPE_P (gnu_range_type)
8439 || SCALAR_FLOAT_TYPE_P (gnu_range_type));
8441 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
8442 we can't do anything since we might be truncating the bounds. No
8443 check is needed in this case. */
8444 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
8445 && (TYPE_PRECISION (gnu_compare_type)
8446 < TYPE_PRECISION (get_base_type (gnu_range_type))))
8447 return gnu_expr;
8449 /* Checked expressions must be evaluated only once. */
8450 gnu_expr = gnat_protect_expr (gnu_expr);
8452 /* Note that the form of the check is
8453 (not (expr >= lo)) or (not (expr <= hi))
8454 the reason for this slightly convoluted form is that NaNs
8455 are not considered to be in range in the float case. */
8456 return emit_check
8457 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8458 invert_truthvalue
8459 (build_binary_op (GE_EXPR, boolean_type_node,
8460 convert (gnu_compare_type, gnu_expr),
8461 convert (gnu_compare_type,
8462 TYPE_MIN_VALUE
8463 (gnu_range_type)))),
8464 invert_truthvalue
8465 (build_binary_op (LE_EXPR, boolean_type_node,
8466 convert (gnu_compare_type, gnu_expr),
8467 convert (gnu_compare_type,
8468 TYPE_MAX_VALUE
8469 (gnu_range_type))))),
8470 gnu_expr, CE_Range_Check_Failed, gnat_node);
8473 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object which
8474 we are about to index, GNU_EXPR is the index expression to be checked,
8475 GNU_LOW and GNU_HIGH are the lower and upper bounds against which GNU_EXPR
8476 has to be checked. Note that for index checking we cannot simply use the
8477 emit_range_check function (although very similar code needs to be generated
8478 in both cases) since for index checking the array type against which we are
8479 checking the indices may be unconstrained and consequently we need to get
8480 the actual index bounds from the array object itself (GNU_ARRAY_OBJECT).
8481 The place where we need to do that is in subprograms having unconstrained
8482 array formal parameters. GNAT_NODE is the GNAT node conveying the source
8483 location for which the error should be signaled. */
8485 static tree
8486 emit_index_check (tree gnu_array_object, tree gnu_expr, tree gnu_low,
8487 tree gnu_high, Node_Id gnat_node)
8489 tree gnu_expr_check;
8491 /* Checked expressions must be evaluated only once. */
8492 gnu_expr = gnat_protect_expr (gnu_expr);
8494 /* Must do this computation in the base type in case the expression's
8495 type is an unsigned subtypes. */
8496 gnu_expr_check = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
8498 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
8499 the object we are handling. */
8500 gnu_low = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low, gnu_array_object);
8501 gnu_high = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high, gnu_array_object);
8503 return emit_check
8504 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
8505 build_binary_op (LT_EXPR, boolean_type_node,
8506 gnu_expr_check,
8507 convert (TREE_TYPE (gnu_expr_check),
8508 gnu_low)),
8509 build_binary_op (GT_EXPR, boolean_type_node,
8510 gnu_expr_check,
8511 convert (TREE_TYPE (gnu_expr_check),
8512 gnu_high))),
8513 gnu_expr, CE_Index_Check_Failed, gnat_node);
8516 /* GNU_COND contains the condition corresponding to an access, discriminant or
8517 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
8518 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
8519 REASON is the code that says why the exception was raised. GNAT_NODE is
8520 the GNAT node conveying the source location for which the error should be
8521 signaled. */
8523 static tree
8524 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
8526 tree gnu_call
8527 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
8528 tree gnu_result
8529 = fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
8530 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
8531 convert (TREE_TYPE (gnu_expr), integer_zero_node)),
8532 gnu_expr);
8534 /* GNU_RESULT has side effects if and only if GNU_EXPR has:
8535 we don't need to evaluate it just for the check. */
8536 TREE_SIDE_EFFECTS (gnu_result) = TREE_SIDE_EFFECTS (gnu_expr);
8538 return gnu_result;
8541 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
8542 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
8543 GNAT_TYPE is known to be an integral type. If TRUNCATE_P true, do a
8544 float to integer conversion with truncation; otherwise round.
8545 GNAT_NODE is the GNAT node conveying the source location for which the
8546 error should be signaled. */
8548 static tree
8549 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflowp,
8550 bool rangep, bool truncatep, Node_Id gnat_node)
8552 tree gnu_type = get_unpadded_type (gnat_type);
8553 tree gnu_in_type = TREE_TYPE (gnu_expr);
8554 tree gnu_in_basetype = get_base_type (gnu_in_type);
8555 tree gnu_base_type = get_base_type (gnu_type);
8556 tree gnu_result = gnu_expr;
8558 /* If we are not doing any checks, the output is an integral type and the
8559 input is not a floating-point type, just do the conversion. This is
8560 required for packed array types and is simpler in all cases anyway. */
8561 if (!rangep
8562 && !overflowp
8563 && INTEGRAL_TYPE_P (gnu_base_type)
8564 && !FLOAT_TYPE_P (gnu_in_type))
8565 return convert (gnu_type, gnu_expr);
8567 /* First convert the expression to its base type. This
8568 will never generate code, but makes the tests below much simpler.
8569 But don't do this if converting from an integer type to an unconstrained
8570 array type since then we need to get the bounds from the original
8571 (unpacked) type. */
8572 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
8573 gnu_result = convert (gnu_in_basetype, gnu_result);
8575 /* If overflow checks are requested, we need to be sure the result will
8576 fit in the output base type. But don't do this if the input
8577 is integer and the output floating-point. */
8578 if (overflowp
8579 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_basetype)))
8581 /* Ensure GNU_EXPR only gets evaluated once. */
8582 tree gnu_input = gnat_protect_expr (gnu_result);
8583 tree gnu_cond = boolean_false_node;
8584 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_basetype);
8585 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_basetype);
8586 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
8587 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
8589 /* Convert the lower bounds to signed types, so we're sure we're
8590 comparing them properly. Likewise, convert the upper bounds
8591 to unsigned types. */
8592 if (INTEGRAL_TYPE_P (gnu_in_basetype) && TYPE_UNSIGNED (gnu_in_basetype))
8593 gnu_in_lb = convert (gnat_signed_type (gnu_in_basetype), gnu_in_lb);
8595 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8596 && !TYPE_UNSIGNED (gnu_in_basetype))
8597 gnu_in_ub = convert (gnat_unsigned_type (gnu_in_basetype), gnu_in_ub);
8599 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
8600 gnu_out_lb = convert (gnat_signed_type (gnu_base_type), gnu_out_lb);
8602 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
8603 gnu_out_ub = convert (gnat_unsigned_type (gnu_base_type), gnu_out_ub);
8605 /* Check each bound separately and only if the result bound
8606 is tighter than the bound on the input type. Note that all the
8607 types are base types, so the bounds must be constant. Also,
8608 the comparison is done in the base type of the input, which
8609 always has the proper signedness. First check for input
8610 integer (which means output integer), output float (which means
8611 both float), or mixed, in which case we always compare.
8612 Note that we have to do the comparison which would *fail* in the
8613 case of an error since if it's an FP comparison and one of the
8614 values is a NaN or Inf, the comparison will fail. */
8615 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8616 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
8617 : (FLOAT_TYPE_P (gnu_base_type)
8618 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb),
8619 TREE_REAL_CST (gnu_out_lb))
8620 : 1))
8621 gnu_cond
8622 = invert_truthvalue
8623 (build_binary_op (GE_EXPR, boolean_type_node,
8624 gnu_input, convert (gnu_in_basetype,
8625 gnu_out_lb)));
8627 if (INTEGRAL_TYPE_P (gnu_in_basetype)
8628 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
8629 : (FLOAT_TYPE_P (gnu_base_type)
8630 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub),
8631 TREE_REAL_CST (gnu_in_lb))
8632 : 1))
8633 gnu_cond
8634 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
8635 invert_truthvalue
8636 (build_binary_op (LE_EXPR, boolean_type_node,
8637 gnu_input,
8638 convert (gnu_in_basetype,
8639 gnu_out_ub))));
8641 if (!integer_zerop (gnu_cond))
8642 gnu_result = emit_check (gnu_cond, gnu_input,
8643 CE_Overflow_Check_Failed, gnat_node);
8646 /* Now convert to the result base type. If this is a non-truncating
8647 float-to-integer conversion, round. */
8648 if (INTEGRAL_TYPE_P (gnu_base_type)
8649 && FLOAT_TYPE_P (gnu_in_basetype)
8650 && !truncatep)
8652 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
8653 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
8654 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
8655 const struct real_format *fmt;
8657 /* The following calculations depend on proper rounding to even
8658 of each arithmetic operation. In order to prevent excess
8659 precision from spoiling this property, use the widest hardware
8660 floating-point type if FP_ARITH_MAY_WIDEN is true. */
8661 calc_type
8662 = fp_arith_may_widen ? longest_float_type_node : gnu_in_basetype;
8664 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
8665 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
8666 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
8667 REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf,
8668 half_minus_pred_half);
8669 gnu_pred_half = build_real (calc_type, pred_half);
8671 /* If the input is strictly negative, subtract this value
8672 and otherwise add it from the input. For 0.5, the result
8673 is exactly between 1.0 and the machine number preceding 1.0
8674 (for calc_type). Since the last bit of 1.0 is even, this 0.5
8675 will round to 1.0, while all other number with an absolute
8676 value less than 0.5 round to 0.0. For larger numbers exactly
8677 halfway between integers, rounding will always be correct as
8678 the true mathematical result will be closer to the higher
8679 integer compared to the lower one. So, this constant works
8680 for all floating-point numbers.
8682 The reason to use the same constant with subtract/add instead
8683 of a positive and negative constant is to allow the comparison
8684 to be scheduled in parallel with retrieval of the constant and
8685 conversion of the input to the calc_type (if necessary). */
8687 gnu_zero = convert (gnu_in_basetype, integer_zero_node);
8688 gnu_result = gnat_protect_expr (gnu_result);
8689 gnu_conv = convert (calc_type, gnu_result);
8690 gnu_comp
8691 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
8692 gnu_add_pred_half
8693 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8694 gnu_subtract_pred_half
8695 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
8696 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
8697 gnu_add_pred_half, gnu_subtract_pred_half);
8700 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8701 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
8702 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
8703 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
8704 else
8705 gnu_result = convert (gnu_base_type, gnu_result);
8707 /* Finally, do the range check if requested. Note that if the result type
8708 is a modular type, the range check is actually an overflow check. */
8709 if (rangep
8710 || (TREE_CODE (gnu_base_type) == INTEGER_TYPE
8711 && TYPE_MODULAR_P (gnu_base_type) && overflowp))
8712 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
8714 return convert (gnu_type, gnu_result);
8717 /* Return true if GNU_EXPR can be directly addressed. This is the case
8718 unless it is an expression involving computation or if it involves a
8719 reference to a bitfield or to an object not sufficiently aligned for
8720 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
8721 be directly addressed as an object of this type.
8723 *** Notes on addressability issues in the Ada compiler ***
8725 This predicate is necessary in order to bridge the gap between Gigi
8726 and the middle-end about addressability of GENERIC trees. A tree
8727 is said to be addressable if it can be directly addressed, i.e. if
8728 its address can be taken, is a multiple of the type's alignment on
8729 strict-alignment architectures and returns the first storage unit
8730 assigned to the object represented by the tree.
8732 In the C family of languages, everything is in practice addressable
8733 at the language level, except for bit-fields. This means that these
8734 compilers will take the address of any tree that doesn't represent
8735 a bit-field reference and expect the result to be the first storage
8736 unit assigned to the object. Even in cases where this will result
8737 in unaligned accesses at run time, nothing is supposed to be done
8738 and the program is considered as erroneous instead (see PR c/18287).
8740 The implicit assumptions made in the middle-end are in keeping with
8741 the C viewpoint described above:
8742 - the address of a bit-field reference is supposed to be never
8743 taken; the compiler (generally) will stop on such a construct,
8744 - any other tree is addressable if it is formally addressable,
8745 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
8747 In Ada, the viewpoint is the opposite one: nothing is addressable
8748 at the language level unless explicitly declared so. This means
8749 that the compiler will both make sure that the trees representing
8750 references to addressable ("aliased" in Ada parlance) objects are
8751 addressable and make no real attempts at ensuring that the trees
8752 representing references to non-addressable objects are addressable.
8754 In the first case, Ada is effectively equivalent to C and handing
8755 down the direct result of applying ADDR_EXPR to these trees to the
8756 middle-end works flawlessly. In the second case, Ada cannot afford
8757 to consider the program as erroneous if the address of trees that
8758 are not addressable is requested for technical reasons, unlike C;
8759 as a consequence, the Ada compiler must arrange for either making
8760 sure that this address is not requested in the middle-end or for
8761 compensating by inserting temporaries if it is requested in Gigi.
8763 The first goal can be achieved because the middle-end should not
8764 request the address of non-addressable trees on its own; the only
8765 exception is for the invocation of low-level block operations like
8766 memcpy, for which the addressability requirements are lower since
8767 the type's alignment can be disregarded. In practice, this means
8768 that Gigi must make sure that such operations cannot be applied to
8769 non-BLKmode bit-fields.
8771 The second goal is achieved by means of the addressable_p predicate,
8772 which computes whether a temporary must be inserted by Gigi when the
8773 address of a tree is requested; if so, the address of the temporary
8774 will be used in lieu of that of the original tree and some glue code
8775 generated to connect everything together. */
8777 static bool
8778 addressable_p (tree gnu_expr, tree gnu_type)
8780 /* For an integral type, the size of the actual type of the object may not
8781 be greater than that of the expected type, otherwise an indirect access
8782 in the latter type wouldn't correctly set all the bits of the object. */
8783 if (gnu_type
8784 && INTEGRAL_TYPE_P (gnu_type)
8785 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
8786 return false;
8788 /* The size of the actual type of the object may not be smaller than that
8789 of the expected type, otherwise an indirect access in the latter type
8790 would be larger than the object. But only record types need to be
8791 considered in practice for this case. */
8792 if (gnu_type
8793 && TREE_CODE (gnu_type) == RECORD_TYPE
8794 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
8795 return false;
8797 switch (TREE_CODE (gnu_expr))
8799 case VAR_DECL:
8800 case PARM_DECL:
8801 case FUNCTION_DECL:
8802 case RESULT_DECL:
8803 /* All DECLs are addressable: if they are in a register, we can force
8804 them to memory. */
8805 return true;
8807 case UNCONSTRAINED_ARRAY_REF:
8808 case INDIRECT_REF:
8809 /* Taking the address of a dereference yields the original pointer. */
8810 return true;
8812 case STRING_CST:
8813 case INTEGER_CST:
8814 /* Taking the address yields a pointer to the constant pool. */
8815 return true;
8817 case CONSTRUCTOR:
8818 /* Taking the address of a static constructor yields a pointer to the
8819 tree constant pool. */
8820 return TREE_STATIC (gnu_expr) ? true : false;
8822 case NULL_EXPR:
8823 case SAVE_EXPR:
8824 case CALL_EXPR:
8825 case PLUS_EXPR:
8826 case MINUS_EXPR:
8827 case BIT_IOR_EXPR:
8828 case BIT_XOR_EXPR:
8829 case BIT_AND_EXPR:
8830 case BIT_NOT_EXPR:
8831 /* All rvalues are deemed addressable since taking their address will
8832 force a temporary to be created by the middle-end. */
8833 return true;
8835 case COMPOUND_EXPR:
8836 /* The address of a compound expression is that of its 2nd operand. */
8837 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
8839 case COND_EXPR:
8840 /* We accept &COND_EXPR as soon as both operands are addressable and
8841 expect the outcome to be the address of the selected operand. */
8842 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
8843 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
8845 case COMPONENT_REF:
8846 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
8847 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
8848 the field is sufficiently aligned, in case it is subject
8849 to a pragma Component_Alignment. But we don't need to
8850 check the alignment of the containing record, as it is
8851 guaranteed to be not smaller than that of its most
8852 aligned field that is not a bit-field. */
8853 && (!STRICT_ALIGNMENT
8854 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
8855 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
8856 /* The field of a padding record is always addressable. */
8857 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
8858 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8860 case ARRAY_REF: case ARRAY_RANGE_REF:
8861 case REALPART_EXPR: case IMAGPART_EXPR:
8862 case NOP_EXPR:
8863 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
8865 case CONVERT_EXPR:
8866 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
8867 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8869 case VIEW_CONVERT_EXPR:
8871 /* This is addressable if we can avoid a copy. */
8872 tree type = TREE_TYPE (gnu_expr);
8873 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
8874 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
8875 && (!STRICT_ALIGNMENT
8876 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8877 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
8878 || ((TYPE_MODE (type) == BLKmode
8879 || TYPE_MODE (inner_type) == BLKmode)
8880 && (!STRICT_ALIGNMENT
8881 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
8882 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
8883 || TYPE_ALIGN_OK (type)
8884 || TYPE_ALIGN_OK (inner_type))))
8885 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
8888 default:
8889 return false;
8893 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
8894 a separate Freeze node exists, delay the bulk of the processing. Otherwise
8895 make a GCC type for GNAT_ENTITY and set up the correspondence. */
8897 void
8898 process_type (Entity_Id gnat_entity)
8900 tree gnu_old
8901 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : 0;
8902 tree gnu_new;
8904 /* If we are to delay elaboration of this type, just do any
8905 elaborations needed for expressions within the declaration and
8906 make a dummy type entry for this node and its Full_View (if
8907 any) in case something points to it. Don't do this if it
8908 has already been done (the only way that can happen is if
8909 the private completion is also delayed). */
8910 if (Present (Freeze_Node (gnat_entity))
8911 || (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8912 && Present (Full_View (gnat_entity))
8913 && Present (Freeze_Node (Full_View (gnat_entity)))
8914 && !present_gnu_tree (Full_View (gnat_entity))))
8916 elaborate_entity (gnat_entity);
8918 if (!gnu_old)
8920 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
8921 save_gnu_tree (gnat_entity, gnu_decl, false);
8922 if (IN (Ekind (gnat_entity), Incomplete_Or_Private_Kind)
8923 && Present (Full_View (gnat_entity)))
8925 if (Has_Completion_In_Body (gnat_entity))
8926 DECL_TAFT_TYPE_P (gnu_decl) = 1;
8927 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
8931 return;
8934 /* If we saved away a dummy type for this node it means that this
8935 made the type that corresponds to the full type of an incomplete
8936 type. Clear that type for now and then update the type in the
8937 pointers. */
8938 if (gnu_old)
8940 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
8941 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
8943 save_gnu_tree (gnat_entity, NULL_TREE, false);
8946 /* Now fully elaborate the type. */
8947 gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 1);
8948 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
8950 /* If we have an old type and we've made pointers to this type, update those
8951 pointers. If this is a Taft amendment type in the main unit, we need to
8952 mark the type as used since other units referencing it don't see the full
8953 declaration and, therefore, cannot mark it as used themselves. */
8954 if (gnu_old)
8956 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8957 TREE_TYPE (gnu_new));
8958 if (DECL_TAFT_TYPE_P (gnu_old))
8959 used_types_insert (TREE_TYPE (gnu_new));
8962 /* If this is a record type corresponding to a task or protected type
8963 that is a completion of an incomplete type, perform a similar update
8964 on the type. ??? Including protected types here is a guess. */
8965 if (IN (Ekind (gnat_entity), Record_Kind)
8966 && Is_Concurrent_Record_Type (gnat_entity)
8967 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
8969 tree gnu_task_old
8970 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
8972 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8973 NULL_TREE, false);
8974 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
8975 gnu_new, false);
8977 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
8978 TREE_TYPE (gnu_new));
8982 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
8983 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
8984 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
8986 static tree
8987 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
8989 tree gnu_list = NULL_TREE, gnu_result;
8991 /* We test for GNU_FIELD being empty in the case where a variant
8992 was the last thing since we don't take things off GNAT_ASSOC in
8993 that case. We check GNAT_ASSOC in case we have a variant, but it
8994 has no fields. */
8996 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
8998 Node_Id gnat_field = First (Choices (gnat_assoc));
8999 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
9000 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
9002 /* The expander is supposed to put a single component selector name
9003 in every record component association. */
9004 gcc_assert (No (Next (gnat_field)));
9006 /* Ignore fields that have Corresponding_Discriminants since we'll
9007 be setting that field in the parent. */
9008 if (Present (Corresponding_Discriminant (Entity (gnat_field)))
9009 && Is_Tagged_Type (Scope (Entity (gnat_field))))
9010 continue;
9012 /* Also ignore discriminants of Unchecked_Unions. */
9013 if (Is_Unchecked_Union (gnat_entity)
9014 && Ekind (Entity (gnat_field)) == E_Discriminant)
9015 continue;
9017 /* Before assigning a value in an aggregate make sure range checks
9018 are done if required. Then convert to the type of the field. */
9019 if (Do_Range_Check (Expression (gnat_assoc)))
9020 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
9022 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
9024 /* Add the field and expression to the list. */
9025 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
9028 gnu_result = extract_values (gnu_list, gnu_type);
9030 #ifdef ENABLE_CHECKING
9031 /* Verify that every entry in GNU_LIST was used. */
9032 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
9033 gcc_assert (TREE_ADDRESSABLE (gnu_list));
9034 #endif
9036 return gnu_result;
9039 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
9040 the first element of an array aggregate. It may itself be an aggregate.
9041 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
9042 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
9043 for range checking. */
9045 static tree
9046 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
9047 Entity_Id gnat_component_type)
9049 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
9050 tree gnu_expr;
9051 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
9053 for ( ; Present (gnat_expr); gnat_expr = Next (gnat_expr))
9055 /* If the expression is itself an array aggregate then first build the
9056 innermost constructor if it is part of our array (multi-dimensional
9057 case). */
9058 if (Nkind (gnat_expr) == N_Aggregate
9059 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
9060 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
9061 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
9062 TREE_TYPE (gnu_array_type),
9063 gnat_component_type);
9064 else
9066 gnu_expr = gnat_to_gnu (gnat_expr);
9068 /* Before assigning the element to the array, make sure it is
9069 in range. */
9070 if (Do_Range_Check (gnat_expr))
9071 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
9074 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
9075 convert (TREE_TYPE (gnu_array_type), gnu_expr));
9077 gnu_index = int_const_binop (PLUS_EXPR, gnu_index,
9078 convert (TREE_TYPE (gnu_index),
9079 integer_one_node));
9082 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
9085 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
9086 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
9087 of the associations that are from RECORD_TYPE. If we see an internal
9088 record, make a recursive call to fill it in as well. */
9090 static tree
9091 extract_values (tree values, tree record_type)
9093 tree field, tem;
9094 vec<constructor_elt, va_gc> *v = NULL;
9096 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9098 tree value = 0;
9100 /* _Parent is an internal field, but may have values in the aggregate,
9101 so check for values first. */
9102 if ((tem = purpose_member (field, values)))
9104 value = TREE_VALUE (tem);
9105 TREE_ADDRESSABLE (tem) = 1;
9108 else if (DECL_INTERNAL_P (field))
9110 value = extract_values (values, TREE_TYPE (field));
9111 if (TREE_CODE (value) == CONSTRUCTOR
9112 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
9113 value = 0;
9115 else
9116 /* If we have a record subtype, the names will match, but not the
9117 actual FIELD_DECLs. */
9118 for (tem = values; tem; tem = TREE_CHAIN (tem))
9119 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
9121 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
9122 TREE_ADDRESSABLE (tem) = 1;
9125 if (!value)
9126 continue;
9128 CONSTRUCTOR_APPEND_ELT (v, field, value);
9131 return gnat_build_constructor (record_type, v);
9134 /* Process a N_Validate_Unchecked_Conversion node. */
9136 static void
9137 validate_unchecked_conversion (Node_Id gnat_node)
9139 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
9140 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
9142 /* If the target is a pointer type, see if we are either converting from a
9143 non-pointer or from a pointer to a type with a different alias set and
9144 warn if so, unless the pointer has been marked to alias everything. */
9145 if (POINTER_TYPE_P (gnu_target_type)
9146 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
9148 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
9149 ? TREE_TYPE (gnu_source_type)
9150 : NULL_TREE;
9151 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
9152 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9154 if (target_alias_set != 0
9155 && (!POINTER_TYPE_P (gnu_source_type)
9156 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9157 target_alias_set)))
9159 post_error_ne ("?possible aliasing problem for type&",
9160 gnat_node, Target_Type (gnat_node));
9161 post_error ("\\?use -fno-strict-aliasing switch for references",
9162 gnat_node);
9163 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
9164 gnat_node, Target_Type (gnat_node));
9168 /* Likewise if the target is a fat pointer type, but we have no mechanism to
9169 mitigate the problem in this case, so we unconditionally warn. */
9170 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
9172 tree gnu_source_desig_type
9173 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
9174 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
9175 : NULL_TREE;
9176 tree gnu_target_desig_type
9177 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
9178 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9180 if (target_alias_set != 0
9181 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
9182 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9183 target_alias_set)))
9185 post_error_ne ("?possible aliasing problem for type&",
9186 gnat_node, Target_Type (gnat_node));
9187 post_error ("\\?use -fno-strict-aliasing switch for references",
9188 gnat_node);
9193 /* EXP is to be treated as an array or record. Handle the cases when it is
9194 an access object and perform the required dereferences. */
9196 static tree
9197 maybe_implicit_deref (tree exp)
9199 /* If the type is a pointer, dereference it. */
9200 if (POINTER_TYPE_P (TREE_TYPE (exp))
9201 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp)))
9202 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
9204 /* If we got a padded type, remove it too. */
9205 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
9206 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
9208 return exp;
9211 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
9212 location and false if it doesn't. In the former case, set the Gigi global
9213 variable REF_FILENAME to the simple debug file name as given by sinput.
9214 If clear_column is true, set column information to 0. */
9216 static bool
9217 Sloc_to_locus1 (Source_Ptr Sloc, location_t *locus, bool clear_column)
9219 if (Sloc == No_Location)
9220 return false;
9222 if (Sloc <= Standard_Location)
9224 *locus = BUILTINS_LOCATION;
9225 return false;
9227 else
9229 Source_File_Index file = Get_Source_File_Index (Sloc);
9230 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
9231 Column_Number column = (clear_column ? 0 : Get_Column_Number (Sloc));
9232 struct line_map *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
9234 /* We can have zero if pragma Source_Reference is in effect. */
9235 if (line < 1)
9236 line = 1;
9238 /* Translate the location. */
9239 *locus = linemap_position_for_line_and_column (map, line, column);
9242 ref_filename
9243 = IDENTIFIER_POINTER
9244 (get_identifier
9245 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc)))));;
9247 return true;
9250 /* Similar to the above, not clearing the column information. */
9252 bool
9253 Sloc_to_locus (Source_Ptr Sloc, location_t *locus)
9255 return Sloc_to_locus1 (Sloc, locus, false);
9258 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
9259 don't do anything if it doesn't correspond to a source location. */
9261 static void
9262 set_expr_location_from_node1 (tree node, Node_Id gnat_node, bool clear_column)
9264 location_t locus;
9266 if (!Sloc_to_locus1 (Sloc (gnat_node), &locus, clear_column))
9267 return;
9269 SET_EXPR_LOCATION (node, locus);
9272 /* Similar to the above, not clearing the column information. */
9274 static void
9275 set_expr_location_from_node (tree node, Node_Id gnat_node)
9277 set_expr_location_from_node1 (node, gnat_node, false);
9280 /* More elaborate version of set_expr_location_from_node to be used in more
9281 general contexts, for example the result of the translation of a generic
9282 GNAT node. */
9284 static void
9285 set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
9287 /* Set the location information on the node if it is a real expression.
9288 References can be reused for multiple GNAT nodes and they would get
9289 the location information of their last use. Also make sure not to
9290 overwrite an existing location as it is probably more precise. */
9292 switch (TREE_CODE (node))
9294 CASE_CONVERT:
9295 case NON_LVALUE_EXPR:
9296 case SAVE_EXPR:
9297 break;
9299 case COMPOUND_EXPR:
9300 if (EXPR_P (TREE_OPERAND (node, 1)))
9301 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
9303 /* ... fall through ... */
9305 default:
9306 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
9308 set_expr_location_from_node (node, gnat_node);
9309 set_end_locus_from_node (node, gnat_node);
9311 break;
9315 /* Return a colon-separated list of encodings contained in encoded Ada
9316 name. */
9318 static const char *
9319 extract_encoding (const char *name)
9321 char *encoding = (char *) ggc_alloc_atomic (strlen (name));
9322 get_encoding (name, encoding);
9323 return encoding;
9326 /* Extract the Ada name from an encoded name. */
9328 static const char *
9329 decode_name (const char *name)
9331 char *decoded = (char *) ggc_alloc_atomic (strlen (name) * 2 + 60);
9332 __gnat_decode (name, decoded, 0);
9333 return decoded;
9336 /* Post an error message. MSG is the error message, properly annotated.
9337 NODE is the node at which to post the error and the node to use for the
9338 '&' substitution. */
9340 void
9341 post_error (const char *msg, Node_Id node)
9343 String_Template temp;
9344 String_Pointer sp;
9346 if (No (node))
9347 return;
9349 temp.Low_Bound = 1;
9350 temp.High_Bound = strlen (msg);
9351 sp.Bounds = &temp;
9352 sp.Array = msg;
9353 Error_Msg_N (sp, node);
9356 /* Similar to post_error, but NODE is the node at which to post the error and
9357 ENT is the node to use for the '&' substitution. */
9359 void
9360 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
9362 String_Template temp;
9363 String_Pointer sp;
9365 if (No (node))
9366 return;
9368 temp.Low_Bound = 1;
9369 temp.High_Bound = strlen (msg);
9370 sp.Bounds = &temp;
9371 sp.Array = msg;
9372 Error_Msg_NE (sp, node, ent);
9375 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
9377 void
9378 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
9380 Error_Msg_Uint_1 = UI_From_Int (num);
9381 post_error_ne (msg, node, ent);
9384 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
9385 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
9386 most sense. Return true if a sensible assignment was performed. */
9388 static bool
9389 set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
9391 Node_Id gnat_end_label = Empty;
9392 location_t end_locus;
9394 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
9395 end_locus when there is one. We consider only GNAT nodes with a possible
9396 End_Label attached. If the End_Label actually was unassigned, fallback
9397 on the original node. We'd better assign an explicit sloc associated with
9398 the outer construct in any case. */
9400 switch (Nkind (gnat_node))
9402 case N_Package_Body:
9403 case N_Subprogram_Body:
9404 case N_Block_Statement:
9405 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
9406 break;
9408 case N_Package_Declaration:
9409 gnat_end_label = End_Label (Specification (gnat_node));
9410 break;
9412 default:
9413 return false;
9416 gnat_node = Present (gnat_end_label) ? gnat_end_label : gnat_node;
9418 /* Some expanded subprograms have neither an End_Label nor a Sloc
9419 attached. Notify that to callers. For a block statement with no
9420 End_Label, clear column information, so that the tree for a
9421 transient block does not receive the sloc of a source condition. */
9423 if (!Sloc_to_locus1 (Sloc (gnat_node), &end_locus,
9424 No (gnat_end_label) &&
9425 (Nkind (gnat_node) == N_Block_Statement)))
9426 return false;
9428 switch (TREE_CODE (gnu_node))
9430 case BIND_EXPR:
9431 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
9432 return true;
9434 case FUNCTION_DECL:
9435 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
9436 return true;
9438 default:
9439 return false;
9443 /* Similar to post_error_ne, but T is a GCC tree representing the number to
9444 write. If T represents a constant, the text inside curly brackets in
9445 MSG will be output (presumably including a '^'). Otherwise it will not
9446 be output and the text inside square brackets will be output instead. */
9448 void
9449 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
9451 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
9452 char start_yes, end_yes, start_no, end_no;
9453 const char *p;
9454 char *q;
9456 if (TREE_CODE (t) == INTEGER_CST)
9458 Error_Msg_Uint_1 = UI_From_gnu (t);
9459 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
9461 else
9462 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
9464 for (p = msg, q = new_msg; *p; p++)
9466 if (*p == start_yes)
9467 for (p++; *p != end_yes; p++)
9468 *q++ = *p;
9469 else if (*p == start_no)
9470 for (p++; *p != end_no; p++)
9472 else
9473 *q++ = *p;
9476 *q = 0;
9478 post_error_ne (new_msg, node, ent);
9481 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
9483 void
9484 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
9485 int num)
9487 Error_Msg_Uint_2 = UI_From_Int (num);
9488 post_error_ne_tree (msg, node, ent, t);
9491 /* Initialize the table that maps GNAT codes to GCC codes for simple
9492 binary and unary operations. */
9494 static void
9495 init_code_table (void)
9497 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
9498 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
9500 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
9501 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
9502 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
9503 gnu_codes[N_Op_Eq] = EQ_EXPR;
9504 gnu_codes[N_Op_Ne] = NE_EXPR;
9505 gnu_codes[N_Op_Lt] = LT_EXPR;
9506 gnu_codes[N_Op_Le] = LE_EXPR;
9507 gnu_codes[N_Op_Gt] = GT_EXPR;
9508 gnu_codes[N_Op_Ge] = GE_EXPR;
9509 gnu_codes[N_Op_Add] = PLUS_EXPR;
9510 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
9511 gnu_codes[N_Op_Multiply] = MULT_EXPR;
9512 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
9513 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
9514 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
9515 gnu_codes[N_Op_Abs] = ABS_EXPR;
9516 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
9517 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
9518 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
9519 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
9520 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
9521 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
9524 /* Return a label to branch to for the exception type in KIND or NULL_TREE
9525 if none. */
9527 tree
9528 get_exception_label (char kind)
9530 if (kind == N_Raise_Constraint_Error)
9531 return gnu_constraint_error_label_stack->last ();
9532 else if (kind == N_Raise_Storage_Error)
9533 return gnu_storage_error_label_stack->last ();
9534 else if (kind == N_Raise_Program_Error)
9535 return gnu_program_error_label_stack->last ();
9536 else
9537 return NULL_TREE;
9540 /* Return the decl for the current elaboration procedure. */
9542 tree
9543 get_elaboration_procedure (void)
9545 return gnu_elab_proc_stack->last ();
9548 #include "gt-ada-trans.h"