Fix merge error.
[official-gcc.git] / gcc / ada / gcc-interface / trans.c
blob9163eb10a7c86eb901229b34c00406916134cea3
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2017, 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 "target.h"
30 #include "function.h"
31 #include "bitmap.h"
32 #include "tree.h"
33 #include "gimple-expr.h"
34 #include "stringpool.h"
35 #include "cgraph.h"
36 #include "predict.h"
37 #include "diagnostic.h"
38 #include "alias.h"
39 #include "fold-const.h"
40 #include "stor-layout.h"
41 #include "stmt.h"
42 #include "varasm.h"
43 #include "output.h"
44 #include "libfuncs.h" /* For set_stack_check_libfunc. */
45 #include "tree-iterator.h"
46 #include "gimplify.h"
47 #include "opts.h"
48 #include "common/common-target.h"
49 #include "stringpool.h"
50 #include "attribs.h"
52 #include "ada.h"
53 #include "adadecode.h"
54 #include "types.h"
55 #include "atree.h"
56 #include "namet.h"
57 #include "nlists.h"
58 #include "snames.h"
59 #include "stringt.h"
60 #include "uintp.h"
61 #include "urealp.h"
62 #include "fe.h"
63 #include "sinfo.h"
64 #include "einfo.h"
65 #include "gadaint.h"
66 #include "ada-tree.h"
67 #include "gigi.h"
69 /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
70 for fear of running out of stack space. If we need more, we use xmalloc
71 instead. */
72 #define ALLOCA_THRESHOLD 1000
74 /* Pointers to front-end tables accessed through macros. */
75 struct Node *Nodes_Ptr;
76 struct Flags *Flags_Ptr;
77 Node_Id *Next_Node_Ptr;
78 Node_Id *Prev_Node_Ptr;
79 struct Elist_Header *Elists_Ptr;
80 struct Elmt_Item *Elmts_Ptr;
81 struct String_Entry *Strings_Ptr;
82 Char_Code *String_Chars_Ptr;
83 struct List_Header *List_Headers_Ptr;
85 /* Highest number in the front-end node table. */
86 int max_gnat_nodes;
88 /* Current node being treated, in case abort called. */
89 Node_Id error_gnat_node;
91 /* True when gigi is being called on an analyzed but unexpanded
92 tree, and the only purpose of the call is to properly annotate
93 types with representation information. */
94 bool type_annotate_only;
96 /* List of N_Validate_Unchecked_Conversion nodes in the unit. */
97 static vec<Node_Id> gnat_validate_uc_list;
99 /* When not optimizing, we cache the 'First, 'Last and 'Length attributes
100 of unconstrained array IN parameters to avoid emitting a great deal of
101 redundant instructions to recompute them each time. */
102 struct GTY (()) parm_attr_d {
103 int id; /* GTY doesn't like Entity_Id. */
104 int dim;
105 tree first;
106 tree last;
107 tree length;
110 typedef struct parm_attr_d *parm_attr;
113 struct GTY(()) language_function {
114 vec<parm_attr, va_gc> *parm_attr_cache;
115 bitmap named_ret_val;
116 vec<tree, va_gc> *other_ret_val;
117 int gnat_ret;
120 #define f_parm_attr_cache \
121 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
123 #define f_named_ret_val \
124 DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val
126 #define f_other_ret_val \
127 DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val
129 #define f_gnat_ret \
130 DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret
132 /* A structure used to gather together information about a statement group.
133 We use this to gather related statements, for example the "then" part
134 of a IF. In the case where it represents a lexical scope, we may also
135 have a BLOCK node corresponding to it and/or cleanups. */
137 struct GTY((chain_next ("%h.previous"))) stmt_group {
138 struct stmt_group *previous; /* Previous code group. */
139 tree stmt_list; /* List of statements for this code group. */
140 tree block; /* BLOCK for this code group, if any. */
141 tree cleanups; /* Cleanups for this code group, if any. */
144 static GTY(()) struct stmt_group *current_stmt_group;
146 /* List of unused struct stmt_group nodes. */
147 static GTY((deletable)) struct stmt_group *stmt_group_free_list;
149 /* A structure used to record information on elaboration procedures
150 we've made and need to process.
152 ??? gnat_node should be Node_Id, but gengtype gets confused. */
154 struct GTY((chain_next ("%h.next"))) elab_info {
155 struct elab_info *next; /* Pointer to next in chain. */
156 tree elab_proc; /* Elaboration procedure. */
157 int gnat_node; /* The N_Compilation_Unit. */
160 static GTY(()) struct elab_info *elab_info_list;
162 /* Stack of exception pointer variables. Each entry is the VAR_DECL
163 that stores the address of the raised exception. Nonzero means we
164 are in an exception handler. Not used in the zero-cost case. */
165 static GTY(()) vec<tree, va_gc> *gnu_except_ptr_stack;
167 /* In ZCX case, current exception pointer. Used to re-raise it. */
168 static GTY(()) tree gnu_incoming_exc_ptr;
170 /* Stack for storing the current elaboration procedure decl. */
171 static GTY(()) vec<tree, va_gc> *gnu_elab_proc_stack;
173 /* Stack of labels to be used as a goto target instead of a return in
174 some functions. See processing for N_Subprogram_Body. */
175 static GTY(()) vec<tree, va_gc> *gnu_return_label_stack;
177 /* Stack of variable for the return value of a function with copy-in/copy-out
178 parameters. See processing for N_Subprogram_Body. */
179 static GTY(()) vec<tree, va_gc> *gnu_return_var_stack;
181 /* Structure used to record information for a range check. */
182 struct GTY(()) range_check_info_d {
183 tree low_bound;
184 tree high_bound;
185 tree disp;
186 bool neg_p;
187 tree type;
188 tree invariant_cond;
189 tree inserted_cond;
192 typedef struct range_check_info_d *range_check_info;
195 /* Structure used to record information for a loop. */
196 struct GTY(()) loop_info_d {
197 tree stmt;
198 tree loop_var;
199 tree low_bound;
200 tree high_bound;
201 vec<range_check_info, va_gc> *checks;
202 bool artificial;
203 bool has_checks;
204 bool warned_aggressive_loop_optimizations;
207 typedef struct loop_info_d *loop_info;
210 /* Stack of loop_info structures associated with LOOP_STMT nodes. */
211 static GTY(()) vec<loop_info, va_gc> *gnu_loop_stack;
213 /* The stacks for N_{Push,Pop}_*_Label. */
214 static GTY(()) vec<tree, va_gc> *gnu_constraint_error_label_stack;
215 static GTY(()) vec<tree, va_gc> *gnu_storage_error_label_stack;
216 static GTY(()) vec<tree, va_gc> *gnu_program_error_label_stack;
218 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
219 static enum tree_code gnu_codes[Number_Node_Kinds];
221 static void init_code_table (void);
222 static tree get_elaboration_procedure (void);
223 static void Compilation_Unit_to_gnu (Node_Id);
224 static bool empty_stmt_list_p (tree);
225 static void record_code_position (Node_Id);
226 static void insert_code_for (Node_Id);
227 static void add_cleanup (tree, Node_Id);
228 static void add_stmt_list (List_Id);
229 static void push_exception_label_stack (vec<tree, va_gc> **, Entity_Id);
230 static tree build_stmt_group (List_Id, bool);
231 static inline bool stmt_group_may_fallthru (void);
232 static enum gimplify_status gnat_gimplify_stmt (tree *);
233 static void elaborate_all_entities (Node_Id);
234 static void process_freeze_entity (Node_Id);
235 static void process_decls (List_Id, List_Id, Node_Id, bool, bool);
236 static tree emit_range_check (tree, Node_Id, Node_Id);
237 static tree emit_check (tree, tree, int, Node_Id);
238 static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id);
239 static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id);
240 static tree convert_with_check (Entity_Id, tree, bool, bool, bool, Node_Id);
241 static bool addressable_p (tree, tree);
242 static tree assoc_to_constructor (Entity_Id, Node_Id, tree);
243 static tree pos_to_constructor (Node_Id, tree, Entity_Id);
244 static void validate_unchecked_conversion (Node_Id);
245 static Node_Id adjust_for_implicit_deref (Node_Id);
246 static tree maybe_implicit_deref (tree);
247 static void set_expr_location_from_node (tree, Node_Id, bool = false);
248 static void set_gnu_expr_location_from_node (tree, Node_Id);
249 static bool set_end_locus_from_node (tree, Node_Id);
250 static int lvalue_required_p (Node_Id, tree, bool, bool, bool);
251 static tree build_raise_check (int, enum exception_info_kind);
252 static tree create_init_temporary (const char *, tree, tree *, Node_Id);
254 /* Hooks for debug info back-ends, only supported and used in a restricted set
255 of configurations. */
256 static const char *extract_encoding (const char *) ATTRIBUTE_UNUSED;
257 static const char *decode_name (const char *) ATTRIBUTE_UNUSED;
259 /* This is the main program of the back-end. It sets up all the table
260 structures and then generates code. */
262 void
263 gigi (Node_Id gnat_root,
264 int max_gnat_node,
265 int number_name ATTRIBUTE_UNUSED,
266 struct Node *nodes_ptr,
267 struct Flags *flags_ptr,
268 Node_Id *next_node_ptr,
269 Node_Id *prev_node_ptr,
270 struct Elist_Header *elists_ptr,
271 struct Elmt_Item *elmts_ptr,
272 struct String_Entry *strings_ptr,
273 Char_Code *string_chars_ptr,
274 struct List_Header *list_headers_ptr,
275 Nat number_file,
276 struct File_Info_Type *file_info_ptr,
277 Entity_Id standard_boolean,
278 Entity_Id standard_integer,
279 Entity_Id standard_character,
280 Entity_Id standard_long_long_float,
281 Entity_Id standard_exception_type,
282 Int gigi_operating_mode)
284 Node_Id gnat_iter;
285 Entity_Id gnat_literal;
286 tree t, ftype, int64_type;
287 struct elab_info *info;
288 int i;
290 max_gnat_nodes = max_gnat_node;
292 Nodes_Ptr = nodes_ptr;
293 Flags_Ptr = flags_ptr;
294 Next_Node_Ptr = next_node_ptr;
295 Prev_Node_Ptr = prev_node_ptr;
296 Elists_Ptr = elists_ptr;
297 Elmts_Ptr = elmts_ptr;
298 Strings_Ptr = strings_ptr;
299 String_Chars_Ptr = string_chars_ptr;
300 List_Headers_Ptr = list_headers_ptr;
302 type_annotate_only = (gigi_operating_mode == 1);
304 for (i = 0; i < number_file; i++)
306 /* Use the identifier table to make a permanent copy of the filename as
307 the name table gets reallocated after Gigi returns but before all the
308 debugging information is output. The __gnat_to_canonical_file_spec
309 call translates filenames from pragmas Source_Reference that contain
310 host style syntax not understood by gdb. */
311 const char *filename
312 = IDENTIFIER_POINTER
313 (get_identifier
314 (__gnat_to_canonical_file_spec
315 (Get_Name_String (file_info_ptr[i].File_Name))));
317 /* We rely on the order isomorphism between files and line maps. */
318 gcc_assert ((int) LINEMAPS_ORDINARY_USED (line_table) == i);
320 /* We create the line map for a source file at once, with a fixed number
321 of columns chosen to avoid jumping over the next power of 2. */
322 linemap_add (line_table, LC_ENTER, 0, filename, 1);
323 linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252);
324 linemap_position_for_column (line_table, 252 - 1);
325 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
328 gcc_assert (Nkind (gnat_root) == N_Compilation_Unit);
330 /* Declare the name of the compilation unit as the first global
331 name in order to make the middle-end fully deterministic. */
332 t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL);
333 first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t));
335 /* Initialize ourselves. */
336 init_code_table ();
337 init_gnat_decl ();
338 init_gnat_utils ();
340 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
341 errors. */
342 if (type_annotate_only)
344 TYPE_SIZE (void_type_node) = bitsize_zero_node;
345 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
348 /* Enable GNAT stack checking method if needed */
349 if (!Stack_Check_Probes_On_Target)
350 set_stack_check_libfunc ("_gnat_stack_check");
352 /* Retrieve alignment settings. */
353 double_float_alignment = get_target_double_float_alignment ();
354 double_scalar_alignment = get_target_double_scalar_alignment ();
356 /* Record the builtin types. Define `integer' and `character' first so that
357 dbx will output them first. */
358 record_builtin_type ("integer", integer_type_node, false);
359 record_builtin_type ("character", char_type_node, false);
360 record_builtin_type ("boolean", boolean_type_node, false);
361 record_builtin_type ("void", void_type_node, false);
363 /* Save the type we made for integer as the type for Standard.Integer. */
364 save_gnu_tree (Base_Type (standard_integer),
365 TYPE_NAME (integer_type_node),
366 false);
368 /* Likewise for character as the type for Standard.Character. */
369 finish_character_type (char_type_node);
370 save_gnu_tree (Base_Type (standard_character),
371 TYPE_NAME (char_type_node),
372 false);
374 /* Likewise for boolean as the type for Standard.Boolean. */
375 save_gnu_tree (Base_Type (standard_boolean),
376 TYPE_NAME (boolean_type_node),
377 false);
378 gnat_literal = First_Literal (Base_Type (standard_boolean));
379 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
380 gcc_assert (t == boolean_false_node);
381 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
382 boolean_type_node, t, true, false, false, false, false,
383 true, false, NULL, gnat_literal);
384 save_gnu_tree (gnat_literal, t, false);
385 gnat_literal = Next_Literal (gnat_literal);
386 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
387 gcc_assert (t == boolean_true_node);
388 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
389 boolean_type_node, t, true, false, false, false, false,
390 true, false, NULL, gnat_literal);
391 save_gnu_tree (gnat_literal, t, false);
393 /* Declare the building blocks of function nodes. */
394 void_list_node = build_tree_list (NULL_TREE, void_type_node);
395 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
396 ptr_void_ftype = build_pointer_type (void_ftype);
398 /* Now declare run-time functions. */
399 ftype = build_function_type_list (ptr_type_node, sizetype, NULL_TREE);
400 malloc_decl
401 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE,
402 ftype,
403 NULL_TREE, is_disabled, true, true, true, false,
404 false, NULL, Empty);
405 DECL_IS_MALLOC (malloc_decl) = 1;
407 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
408 free_decl
409 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
410 ftype,
411 NULL_TREE, is_disabled, true, true, true, false,
412 false, NULL, Empty);
414 ftype = build_function_type_list (ptr_type_node, ptr_type_node, sizetype,
415 NULL_TREE);
416 realloc_decl
417 = create_subprog_decl (get_identifier ("__gnat_realloc"), NULL_TREE,
418 ftype,
419 NULL_TREE, is_disabled, true, true, true, false,
420 false, NULL, Empty);
422 /* This is used for 64-bit multiplication with overflow checking. */
423 int64_type = gnat_type_for_size (64, 0);
424 mulv64_decl
425 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE,
426 build_function_type_list (int64_type, int64_type,
427 int64_type, NULL_TREE),
428 NULL_TREE, is_disabled, true, true, true, false,
429 false, NULL, Empty);
431 /* Name of the _Parent field in tagged record types. */
432 parent_name_id = get_identifier (Get_Name_String (Name_uParent));
434 /* Name of the Exception_Data type defined in System.Standard_Library. */
435 exception_data_name_id
436 = get_identifier ("system__standard_library__exception_data");
438 /* Make the types and functions used for exception processing. */
439 except_type_node = gnat_to_gnu_type (Base_Type (standard_exception_type));
441 jmpbuf_type
442 = build_array_type (gnat_type_for_mode (Pmode, 0),
443 build_index_type (size_int (5)));
444 record_builtin_type ("JMPBUF_T", jmpbuf_type, true);
445 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
447 /* Functions to get and set the jumpbuf pointer for the current thread. */
448 get_jmpbuf_decl
449 = create_subprog_decl
450 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
451 NULL_TREE, build_function_type_list (jmpbuf_ptr_type, NULL_TREE),
452 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
454 set_jmpbuf_decl
455 = create_subprog_decl
456 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
457 NULL_TREE, build_function_type_list (void_type_node, jmpbuf_ptr_type,
458 NULL_TREE),
459 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
461 get_excptr_decl
462 = create_subprog_decl
463 (get_identifier ("system__soft_links__get_gnat_exception"), NULL_TREE,
464 build_function_type_list (build_pointer_type (except_type_node),
465 NULL_TREE),
466 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
468 not_handled_by_others_decl = get_identifier ("not_handled_by_others");
469 for (t = TYPE_FIELDS (except_type_node); t; t = DECL_CHAIN (t))
470 if (DECL_NAME (t) == not_handled_by_others_decl)
472 not_handled_by_others_decl = t;
473 break;
475 gcc_assert (DECL_P (not_handled_by_others_decl));
477 /* setjmp returns an integer and has one operand, which is a pointer to
478 a jmpbuf. */
479 setjmp_decl
480 = create_subprog_decl
481 (get_identifier ("__builtin_setjmp"), NULL_TREE,
482 build_function_type_list (integer_type_node, jmpbuf_ptr_type,
483 NULL_TREE),
484 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
485 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
486 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
488 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
489 address. */
490 update_setjmp_buf_decl
491 = create_subprog_decl
492 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
493 build_function_type_list (void_type_node, jmpbuf_ptr_type, NULL_TREE),
494 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
495 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
496 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
498 /* Indicate that it never returns. */
499 ftype = build_function_type_list (void_type_node,
500 build_pointer_type (except_type_node),
501 NULL_TREE);
502 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
503 raise_nodefer_decl
504 = create_subprog_decl
505 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE, ftype,
506 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
508 set_exception_parameter_decl
509 = create_subprog_decl
510 (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE,
511 build_function_type_list (void_type_node, ptr_type_node, ptr_type_node,
512 NULL_TREE),
513 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
515 /* Hooks to call when entering/leaving an exception handler. */
516 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
518 begin_handler_decl
519 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
520 ftype, NULL_TREE,
521 is_disabled, true, true, true, false, false, NULL,
522 Empty);
523 /* __gnat_begin_handler is a dummy procedure. */
524 TREE_NOTHROW (begin_handler_decl) = 1;
526 end_handler_decl
527 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
528 ftype, NULL_TREE,
529 is_disabled, true, true, true, false, false, NULL,
530 Empty);
532 unhandled_except_decl
533 = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
534 NULL_TREE, ftype, NULL_TREE,
535 is_disabled, true, true, true, false, false, NULL,
536 Empty);
538 /* Indicate that it never returns. */
539 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
540 reraise_zcx_decl
541 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE,
542 ftype, NULL_TREE,
543 is_disabled, true, true, true, false, false, NULL,
544 Empty);
546 /* Dummy objects to materialize "others" and "all others" in the exception
547 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
548 the types to use. */
549 others_decl
550 = create_var_decl (get_identifier ("OTHERS"),
551 get_identifier ("__gnat_others_value"),
552 char_type_node, NULL_TREE,
553 true, false, true, false, false, true, false,
554 NULL, Empty);
556 all_others_decl
557 = create_var_decl (get_identifier ("ALL_OTHERS"),
558 get_identifier ("__gnat_all_others_value"),
559 char_type_node, NULL_TREE,
560 true, false, true, false, false, true, false,
561 NULL, Empty);
563 unhandled_others_decl
564 = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
565 get_identifier ("__gnat_unhandled_others_value"),
566 char_type_node, NULL_TREE,
567 true, false, true, false, false, true, false,
568 NULL, Empty);
570 /* If in no exception handlers mode, all raise statements are redirected to
571 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl since
572 this procedure will never be called in this mode. */
573 if (No_Exception_Handlers_Set ())
575 /* Indicate that it never returns. */
576 ftype = build_function_type_list (void_type_node,
577 build_pointer_type (char_type_node),
578 integer_type_node, NULL_TREE);
579 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
580 tree decl
581 = create_subprog_decl
582 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE, ftype,
583 NULL_TREE, is_disabled, true, true, true, false, false, NULL,
584 Empty);
585 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
586 gnat_raise_decls[i] = decl;
588 else
590 /* Otherwise, make one decl for each exception reason. */
591 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
592 gnat_raise_decls[i] = build_raise_check (i, exception_simple);
593 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++)
594 gnat_raise_decls_ext[i]
595 = build_raise_check (i,
596 i == CE_Index_Check_Failed
597 || i == CE_Range_Check_Failed
598 || i == CE_Invalid_Data
599 ? exception_range : exception_column);
602 /* Build the special descriptor type and its null node if needed. */
603 if (TARGET_VTABLE_USES_DESCRIPTORS)
605 tree null_node = fold_convert (ptr_void_ftype, null_pointer_node);
606 tree field_list = NULL_TREE;
607 int j;
608 vec<constructor_elt, va_gc> *null_vec = NULL;
609 constructor_elt *elt;
611 fdesc_type_node = make_node (RECORD_TYPE);
612 vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS);
613 elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
615 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
617 tree field
618 = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node,
619 NULL_TREE, NULL_TREE, 0, 1);
620 DECL_CHAIN (field) = field_list;
621 field_list = field;
622 elt->index = field;
623 elt->value = null_node;
624 elt--;
627 finish_record_type (fdesc_type_node, nreverse (field_list), 0, false);
628 record_builtin_type ("descriptor", fdesc_type_node, true);
629 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec);
632 longest_float_type_node
633 = get_unpadded_type (Base_Type (standard_long_long_float));
635 main_identifier_node = get_identifier ("main");
637 /* If we are using the GCC exception mechanism, let GCC know. */
638 if (Back_End_Exceptions ())
639 gnat_init_gcc_eh ();
641 /* Initialize the GCC support for FP operations. */
642 gnat_init_gcc_fp ();
644 /* Install the builtins we might need, either internally or as user-available
645 facilities for Intrinsic imports. Note that this must be done after the
646 GCC exception mechanism is initialized. */
647 gnat_install_builtins ();
649 vec_safe_push (gnu_except_ptr_stack, NULL_TREE);
650 vec_safe_push (gnu_constraint_error_label_stack, NULL_TREE);
651 vec_safe_push (gnu_storage_error_label_stack, NULL_TREE);
652 vec_safe_push (gnu_program_error_label_stack, NULL_TREE);
654 /* Process any Pragma Ident for the main unit. */
655 if (Present (Ident_String (Main_Unit)))
656 targetm.asm_out.output_ident
657 (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit))));
659 /* Force -fno-strict-aliasing if the configuration pragma was seen. */
660 if (No_Strict_Aliasing_CP)
661 flag_strict_aliasing = 0;
663 /* Save the current optimization options again after the above possible
664 global_options changes. */
665 optimization_default_node = build_optimization_node (&global_options);
666 optimization_current_node = optimization_default_node;
668 /* Now translate the compilation unit proper. */
669 Compilation_Unit_to_gnu (gnat_root);
671 /* Disable -Waggressive-loop-optimizations since we implement our own
672 version of the warning. */
673 warn_aggressive_loop_optimizations = 0;
675 /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at
676 the very end to avoid having to second-guess the front-end when we run
677 into dummy nodes during the regular processing. */
678 for (i = 0; gnat_validate_uc_list.iterate (i, &gnat_iter); i++)
679 validate_unchecked_conversion (gnat_iter);
680 gnat_validate_uc_list.release ();
682 /* Finally see if we have any elaboration procedures to deal with. */
683 for (info = elab_info_list; info; info = info->next)
685 tree gnu_body = DECL_SAVED_TREE (info->elab_proc);
687 /* We should have a BIND_EXPR but it may not have any statements in it.
688 If it doesn't have any, we have nothing to do except for setting the
689 flag on the GNAT node. Otherwise, process the function as others. */
690 tree gnu_stmts = gnu_body;
691 if (TREE_CODE (gnu_stmts) == BIND_EXPR)
692 gnu_stmts = BIND_EXPR_BODY (gnu_stmts);
693 if (!gnu_stmts || empty_stmt_list_p (gnu_stmts))
694 Set_Has_No_Elaboration_Code (info->gnat_node, 1);
695 else
697 begin_subprog_body (info->elab_proc);
698 end_subprog_body (gnu_body);
699 rest_of_subprog_body_compilation (info->elab_proc);
703 /* Destroy ourselves. */
704 destroy_gnat_decl ();
705 destroy_gnat_utils ();
707 /* We cannot track the location of errors past this point. */
708 error_gnat_node = Empty;
711 /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given
712 CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */
714 static tree
715 build_raise_check (int check, enum exception_info_kind kind)
717 tree result, ftype;
718 const char pfx[] = "__gnat_rcheck_";
720 strcpy (Name_Buffer, pfx);
721 Name_Len = sizeof (pfx) - 1;
722 Get_RT_Exception_Name (check);
724 if (kind == exception_simple)
726 Name_Buffer[Name_Len] = 0;
727 ftype
728 = build_function_type_list (void_type_node,
729 build_pointer_type (char_type_node),
730 integer_type_node, NULL_TREE);
732 else
734 tree t = (kind == exception_column ? NULL_TREE : integer_type_node);
736 strcpy (Name_Buffer + Name_Len, "_ext");
737 Name_Buffer[Name_Len + 4] = 0;
738 ftype
739 = build_function_type_list (void_type_node,
740 build_pointer_type (char_type_node),
741 integer_type_node, integer_type_node,
742 t, t, NULL_TREE);
745 /* Indicate that it never returns. */
746 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
747 result
748 = create_subprog_decl (get_identifier (Name_Buffer), NULL_TREE, ftype,
749 NULL_TREE, is_disabled, true, true, true, false,
750 false, NULL, Empty);
752 return result;
755 /* Return a positive value if an lvalue is required for GNAT_NODE, which is
756 an N_Attribute_Reference. */
758 static int
759 lvalue_required_for_attribute_p (Node_Id gnat_node)
761 switch (Get_Attribute_Id (Attribute_Name (gnat_node)))
763 case Attr_Pos:
764 case Attr_Val:
765 case Attr_Pred:
766 case Attr_Succ:
767 case Attr_First:
768 case Attr_Last:
769 case Attr_Range_Length:
770 case Attr_Length:
771 case Attr_Object_Size:
772 case Attr_Value_Size:
773 case Attr_Component_Size:
774 case Attr_Descriptor_Size:
775 case Attr_Max_Size_In_Storage_Elements:
776 case Attr_Min:
777 case Attr_Max:
778 case Attr_Null_Parameter:
779 case Attr_Passed_By_Reference:
780 case Attr_Mechanism_Code:
781 case Attr_Machine:
782 case Attr_Model:
783 return 0;
785 case Attr_Address:
786 case Attr_Access:
787 case Attr_Unchecked_Access:
788 case Attr_Unrestricted_Access:
789 case Attr_Code_Address:
790 case Attr_Pool_Address:
791 case Attr_Size:
792 case Attr_Alignment:
793 case Attr_Bit_Position:
794 case Attr_Position:
795 case Attr_First_Bit:
796 case Attr_Last_Bit:
797 case Attr_Bit:
798 case Attr_Asm_Input:
799 case Attr_Asm_Output:
800 default:
801 return 1;
805 /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE
806 is the type that will be used for GNAT_NODE in the translated GNU tree.
807 CONSTANT indicates whether the underlying object represented by GNAT_NODE
808 is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates
809 whether its value is the address of a constant and ALIASED whether it is
810 aliased. If it isn't, ADDRESS_OF_CONSTANT and ALIASED are ignored.
812 The function climbs up the GNAT tree starting from the node and returns 1
813 upon encountering a node that effectively requires an lvalue downstream.
814 It returns int instead of bool to facilitate usage in non-purely binary
815 logic contexts. */
817 static int
818 lvalue_required_p (Node_Id gnat_node, tree gnu_type, bool constant,
819 bool address_of_constant, bool aliased)
821 Node_Id gnat_parent = Parent (gnat_node), gnat_temp;
823 switch (Nkind (gnat_parent))
825 case N_Reference:
826 return 1;
828 case N_Attribute_Reference:
829 return lvalue_required_for_attribute_p (gnat_parent);
831 case N_Parameter_Association:
832 case N_Function_Call:
833 case N_Procedure_Call_Statement:
834 /* If the parameter is by reference, an lvalue is required. */
835 return (!constant
836 || must_pass_by_ref (gnu_type)
837 || default_pass_by_ref (gnu_type));
839 case N_Indexed_Component:
840 /* Only the array expression can require an lvalue. */
841 if (Prefix (gnat_parent) != gnat_node)
842 return 0;
844 /* ??? Consider that referencing an indexed component with a variable
845 index forces the whole aggregate to memory. Note that testing only
846 for literals is conservative, any static expression in the RM sense
847 could probably be accepted with some additional work. */
848 for (gnat_temp = First (Expressions (gnat_parent));
849 Present (gnat_temp);
850 gnat_temp = Next (gnat_temp))
851 if (Nkind (gnat_temp) != N_Character_Literal
852 && Nkind (gnat_temp) != N_Integer_Literal
853 && !(Is_Entity_Name (gnat_temp)
854 && Ekind (Entity (gnat_temp)) == E_Enumeration_Literal))
855 return 1;
857 /* ... fall through ... */
859 case N_Slice:
860 /* Only the array expression can require an lvalue. */
861 if (Prefix (gnat_parent) != gnat_node)
862 return 0;
864 aliased |= Has_Aliased_Components (Etype (gnat_node));
865 return lvalue_required_p (gnat_parent, gnu_type, constant,
866 address_of_constant, aliased);
868 case N_Selected_Component:
869 aliased |= Is_Aliased (Entity (Selector_Name (gnat_parent)));
870 return lvalue_required_p (gnat_parent, gnu_type, constant,
871 address_of_constant, aliased);
873 case N_Object_Renaming_Declaration:
874 /* We need to preserve addresses through a renaming. */
875 return 1;
877 case N_Object_Declaration:
878 /* We cannot use a constructor if this is an atomic object because
879 the actual assignment might end up being done component-wise. */
880 return (!constant
881 ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
882 && Is_Atomic_Or_VFA (Defining_Entity (gnat_parent)))
883 /* We don't use a constructor if this is a class-wide object
884 because the effective type of the object is the equivalent
885 type of the class-wide subtype and it smashes most of the
886 data into an array of bytes to which we cannot convert. */
887 || Ekind ((Etype (Defining_Entity (gnat_parent))))
888 == E_Class_Wide_Subtype);
890 case N_Assignment_Statement:
891 /* We cannot use a constructor if the LHS is an atomic object because
892 the actual assignment might end up being done component-wise. */
893 return (!constant
894 || Name (gnat_parent) == gnat_node
895 || (Is_Composite_Type (Underlying_Type (Etype (gnat_node)))
896 && Is_Entity_Name (Name (gnat_parent))
897 && Is_Atomic_Or_VFA (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 COMPONENT_REF:
974 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
975 if (op0 == TREE_OPERAND (exp, 0))
976 return exp;
978 return fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0,
979 TREE_OPERAND (exp, 1), NULL_TREE);
981 case BIT_FIELD_REF:
982 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
983 if (op0 == TREE_OPERAND (exp, 0))
984 return exp;
986 return fold_build3 (BIT_FIELD_REF, TREE_TYPE (exp), op0,
987 TREE_OPERAND (exp, 1), TREE_OPERAND (exp, 2));
989 case ARRAY_REF:
990 case ARRAY_RANGE_REF:
991 /* If the index is not itself constant, then nothing can be folded. */
992 if (!TREE_CONSTANT (TREE_OPERAND (exp, 1)))
993 return exp;
994 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
995 if (op0 == TREE_OPERAND (exp, 0))
996 return exp;
998 return fold (build4 (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1),
999 TREE_OPERAND (exp, 2), NULL_TREE));
1001 case REALPART_EXPR:
1002 case IMAGPART_EXPR:
1003 case VIEW_CONVERT_EXPR:
1004 op0 = fold_constant_decl_in_expr (TREE_OPERAND (exp, 0));
1005 if (op0 == TREE_OPERAND (exp, 0))
1006 return exp;
1008 return fold_build1 (code, TREE_TYPE (exp), op0);
1010 default:
1011 return exp;
1014 gcc_unreachable ();
1017 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
1018 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer
1019 to where we should place the result type. */
1021 static tree
1022 Identifier_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
1024 Node_Id gnat_temp, gnat_temp_type;
1025 tree gnu_result, gnu_result_type;
1027 /* Whether we should require an lvalue for GNAT_NODE. Needed in
1028 specific circumstances only, so evaluated lazily. < 0 means
1029 unknown, > 0 means known true, 0 means known false. */
1030 int require_lvalue = -1;
1032 /* If GNAT_NODE is a constant, whether we should use the initialization
1033 value instead of the constant entity, typically for scalars with an
1034 address clause when the parent doesn't require an lvalue. */
1035 bool use_constant_initializer = false;
1037 /* If the Etype of this node is not the same as that of the Entity, then
1038 something went wrong, probably in generic instantiation. However, this
1039 does not apply to types. Since we sometime have strange Ekind's, just
1040 do this test for objects. Moreover, if the Etype of the Entity is private
1041 or incomplete coming from a limited context, the Etype of the N_Identifier
1042 is allowed to be the full/non-limited view and we also consider a packed
1043 array type to be the same as the original type. Similarly, a CW type is
1044 equivalent to a subtype of itself. Finally, if the types are Itypes, one
1045 may be a copy of the other, which is also legal. */
1046 gnat_temp = ((Nkind (gnat_node) == N_Defining_Identifier
1047 || Nkind (gnat_node) == N_Defining_Operator_Symbol)
1048 ? gnat_node : Entity (gnat_node));
1049 gnat_temp_type = Etype (gnat_temp);
1051 gcc_assert (Etype (gnat_node) == gnat_temp_type
1052 || (Is_Packed (gnat_temp_type)
1053 && (Etype (gnat_node)
1054 == Packed_Array_Impl_Type (gnat_temp_type)))
1055 || (Is_Class_Wide_Type (Etype (gnat_node)))
1056 || (Is_Incomplete_Or_Private_Type (gnat_temp_type)
1057 && Present (Full_View (gnat_temp_type))
1058 && ((Etype (gnat_node) == Full_View (gnat_temp_type))
1059 || (Is_Packed (Full_View (gnat_temp_type))
1060 && (Etype (gnat_node)
1061 == Packed_Array_Impl_Type
1062 (Full_View (gnat_temp_type))))))
1063 || (Is_Incomplete_Type (gnat_temp_type)
1064 && From_Limited_With (gnat_temp_type)
1065 && Present (Non_Limited_View (gnat_temp_type))
1066 && Etype (gnat_node) == Non_Limited_View (gnat_temp_type))
1067 || (Is_Itype (Etype (gnat_node)) && Is_Itype (gnat_temp_type))
1068 || !(Ekind (gnat_temp) == E_Variable
1069 || Ekind (gnat_temp) == E_Component
1070 || Ekind (gnat_temp) == E_Constant
1071 || Ekind (gnat_temp) == E_Loop_Parameter
1072 || Is_Formal (gnat_temp)));
1074 /* If this is a reference to a deferred constant whose partial view is an
1075 unconstrained private type, the proper type is on the full view of the
1076 constant, not on the full view of the type, which may be unconstrained.
1078 This may be a reference to a type, for example in the prefix of the
1079 attribute Position, generated for dispatching code (see Make_DT in
1080 exp_disp,adb). In that case we need the type itself, not is parent,
1081 in particular if it is a derived type */
1082 if (Ekind (gnat_temp) == E_Constant
1083 && Is_Private_Type (gnat_temp_type)
1084 && (Has_Unknown_Discriminants (gnat_temp_type)
1085 || (Present (Full_View (gnat_temp_type))
1086 && Has_Discriminants (Full_View (gnat_temp_type))))
1087 && Present (Full_View (gnat_temp)))
1089 gnat_temp = Full_View (gnat_temp);
1090 gnat_temp_type = Etype (gnat_temp);
1092 else
1094 /* We want to use the Actual_Subtype if it has already been elaborated,
1095 otherwise the Etype. Avoid using Actual_Subtype for packed arrays to
1096 simplify things. */
1097 if ((Ekind (gnat_temp) == E_Constant
1098 || Ekind (gnat_temp) == E_Variable || Is_Formal (gnat_temp))
1099 && !(Is_Array_Type (Etype (gnat_temp))
1100 && Present (Packed_Array_Impl_Type (Etype (gnat_temp))))
1101 && Present (Actual_Subtype (gnat_temp))
1102 && present_gnu_tree (Actual_Subtype (gnat_temp)))
1103 gnat_temp_type = Actual_Subtype (gnat_temp);
1104 else
1105 gnat_temp_type = Etype (gnat_node);
1108 /* Expand the type of this identifier first, in case it is an enumeral
1109 literal, which only get made when the type is expanded. There is no
1110 order-of-elaboration issue here. */
1111 gnu_result_type = get_unpadded_type (gnat_temp_type);
1113 /* If this is a non-imported elementary constant with an address clause,
1114 retrieve the value instead of a pointer to be dereferenced unless
1115 an lvalue is required. This is generally more efficient and actually
1116 required if this is a static expression because it might be used
1117 in a context where a dereference is inappropriate, such as a case
1118 statement alternative or a record discriminant. There is no possible
1119 volatile-ness short-circuit here since Volatile constants must be
1120 imported per C.6. */
1121 if (Ekind (gnat_temp) == E_Constant
1122 && Is_Elementary_Type (gnat_temp_type)
1123 && !Is_Imported (gnat_temp)
1124 && Present (Address_Clause (gnat_temp)))
1126 require_lvalue = lvalue_required_p (gnat_node, gnu_result_type, true,
1127 false, Is_Aliased (gnat_temp));
1128 use_constant_initializer = !require_lvalue;
1131 if (use_constant_initializer)
1133 /* If this is a deferred constant, the initializer is attached to
1134 the full view. */
1135 if (Present (Full_View (gnat_temp)))
1136 gnat_temp = Full_View (gnat_temp);
1138 gnu_result = gnat_to_gnu (Expression (Declaration_Node (gnat_temp)));
1140 else
1141 gnu_result = gnat_to_gnu_entity (gnat_temp, NULL_TREE, false);
1143 /* Some objects (such as parameters passed by reference, globals of
1144 variable size, and renamed objects) actually represent the address
1145 of the object. In that case, we must do the dereference. Likewise,
1146 deal with parameters to foreign convention subprograms. */
1147 if (DECL_P (gnu_result)
1148 && (DECL_BY_REF_P (gnu_result)
1149 || (TREE_CODE (gnu_result) == PARM_DECL
1150 && DECL_BY_COMPONENT_PTR_P (gnu_result))))
1152 const bool read_only = DECL_POINTS_TO_READONLY_P (gnu_result);
1154 /* If it's a PARM_DECL to foreign convention subprogram, convert it. */
1155 if (TREE_CODE (gnu_result) == PARM_DECL
1156 && DECL_BY_COMPONENT_PTR_P (gnu_result))
1157 gnu_result
1158 = convert (build_pointer_type (gnu_result_type), gnu_result);
1160 /* If it's a CONST_DECL, return the underlying constant like below. */
1161 else if (TREE_CODE (gnu_result) == CONST_DECL
1162 && !(DECL_CONST_ADDRESS_P (gnu_result)
1163 && lvalue_required_p (gnat_node, gnu_result_type, true,
1164 true, false)))
1165 gnu_result = DECL_INITIAL (gnu_result);
1167 /* If it's a renaming pointer, get to the renamed object. */
1168 if (TREE_CODE (gnu_result) == VAR_DECL
1169 && !DECL_LOOP_PARM_P (gnu_result)
1170 && DECL_RENAMED_OBJECT (gnu_result))
1171 gnu_result = DECL_RENAMED_OBJECT (gnu_result);
1173 /* Otherwise, do the final dereference. */
1174 else
1176 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
1178 if ((TREE_CODE (gnu_result) == INDIRECT_REF
1179 || TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
1180 && No (Address_Clause (gnat_temp)))
1181 TREE_THIS_NOTRAP (gnu_result) = 1;
1183 if (read_only)
1184 TREE_READONLY (gnu_result) = 1;
1188 /* If we have a constant declaration and its initializer, try to return the
1189 latter to avoid the need to call fold in lots of places and the need for
1190 elaboration code if this identifier is used as an initializer itself. */
1191 if (constant_decl_with_initializer_p (gnu_result))
1193 bool constant_only = (TREE_CODE (gnu_result) == CONST_DECL
1194 && !DECL_CONST_CORRESPONDING_VAR (gnu_result));
1195 bool address_of_constant = (TREE_CODE (gnu_result) == CONST_DECL
1196 && DECL_CONST_ADDRESS_P (gnu_result));
1198 /* If there is a (corresponding) variable or this is the address of a
1199 constant, we only want to return the initializer if an lvalue isn't
1200 required. Evaluate this now if we have not already done so. */
1201 if ((!constant_only || address_of_constant) && require_lvalue < 0)
1202 require_lvalue
1203 = lvalue_required_p (gnat_node, gnu_result_type, true,
1204 address_of_constant, Is_Aliased (gnat_temp));
1206 /* Finally retrieve the initializer if this is deemed valid. */
1207 if ((constant_only && !address_of_constant) || !require_lvalue)
1208 gnu_result = DECL_INITIAL (gnu_result);
1211 /* But for a constant renaming we couldn't do that incrementally for its
1212 definition because of the need to return an lvalue so, if the present
1213 context doesn't itself require an lvalue, we try again here. */
1214 else if (Ekind (gnat_temp) == E_Constant
1215 && Is_Elementary_Type (gnat_temp_type)
1216 && Present (Renamed_Object (gnat_temp)))
1218 if (require_lvalue < 0)
1219 require_lvalue
1220 = lvalue_required_p (gnat_node, gnu_result_type, true, false,
1221 Is_Aliased (gnat_temp));
1222 if (!require_lvalue)
1223 gnu_result = fold_constant_decl_in_expr (gnu_result);
1226 /* The GNAT tree has the type of a function set to its result type, so we
1227 adjust here. Also use the type of the result if the Etype is a subtype
1228 that is nominally unconstrained. Likewise if this is a deferred constant
1229 of a discriminated type whose full view can be elaborated statically, to
1230 avoid problematic conversions to the nominal subtype. But remove any
1231 padding from the resulting type. */
1232 if (TREE_CODE (TREE_TYPE (gnu_result)) == FUNCTION_TYPE
1233 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type)
1234 || (Ekind (gnat_temp) == E_Constant
1235 && Present (Full_View (gnat_temp))
1236 && Has_Discriminants (gnat_temp_type)
1237 && TREE_CODE (gnu_result) == CONSTRUCTOR))
1239 gnu_result_type = TREE_TYPE (gnu_result);
1240 if (TYPE_IS_PADDING_P (gnu_result_type))
1241 gnu_result_type = TREE_TYPE (TYPE_FIELDS (gnu_result_type));
1244 *gnu_result_type_p = gnu_result_type;
1246 return gnu_result;
1249 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
1250 any statements we generate. */
1252 static tree
1253 Pragma_to_gnu (Node_Id gnat_node)
1255 tree gnu_result = alloc_stmt_list ();
1256 unsigned char pragma_id;
1257 Node_Id gnat_temp;
1259 /* Do nothing if we are just annotating types and check for (and ignore)
1260 unrecognized pragmas. */
1261 if (type_annotate_only
1262 || !Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node))))
1263 return gnu_result;
1265 pragma_id = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)));
1266 switch (pragma_id)
1268 case Pragma_Inspection_Point:
1269 /* Do nothing at top level: all such variables are already viewable. */
1270 if (global_bindings_p ())
1271 break;
1273 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1274 Present (gnat_temp);
1275 gnat_temp = Next (gnat_temp))
1277 Node_Id gnat_expr = Expression (gnat_temp);
1278 tree gnu_expr = gnat_to_gnu (gnat_expr);
1279 int use_address;
1280 machine_mode mode;
1281 scalar_int_mode int_mode;
1282 tree asm_constraint = NULL_TREE;
1283 #ifdef ASM_COMMENT_START
1284 char *comment;
1285 #endif
1287 if (TREE_CODE (gnu_expr) == UNCONSTRAINED_ARRAY_REF)
1288 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1290 /* Use the value only if it fits into a normal register,
1291 otherwise use the address. */
1292 mode = TYPE_MODE (TREE_TYPE (gnu_expr));
1293 use_address = (!is_a <scalar_int_mode> (mode, &int_mode)
1294 || GET_MODE_SIZE (int_mode) > UNITS_PER_WORD);
1296 if (use_address)
1297 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
1299 #ifdef ASM_COMMENT_START
1300 comment = concat (ASM_COMMENT_START,
1301 " inspection point: ",
1302 Get_Name_String (Chars (gnat_expr)),
1303 use_address ? " address" : "",
1304 " is in %0",
1305 NULL);
1306 asm_constraint = build_string (strlen (comment), comment);
1307 free (comment);
1308 #endif
1309 gnu_expr = build5 (ASM_EXPR, void_type_node,
1310 asm_constraint,
1311 NULL_TREE,
1312 tree_cons
1313 (build_tree_list (NULL_TREE,
1314 build_string (1, "g")),
1315 gnu_expr, NULL_TREE),
1316 NULL_TREE, NULL_TREE);
1317 ASM_VOLATILE_P (gnu_expr) = 1;
1318 set_expr_location_from_node (gnu_expr, gnat_node);
1319 append_to_statement_list (gnu_expr, &gnu_result);
1321 break;
1323 case Pragma_Loop_Optimize:
1324 for (gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1325 Present (gnat_temp);
1326 gnat_temp = Next (gnat_temp))
1328 tree gnu_loop_stmt = gnu_loop_stack->last ()->stmt;
1330 switch (Chars (Expression (gnat_temp)))
1332 case Name_Ivdep:
1333 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
1334 break;
1336 case Name_No_Unroll:
1337 LOOP_STMT_NO_UNROLL (gnu_loop_stmt) = 1;
1338 break;
1340 case Name_Unroll:
1341 LOOP_STMT_UNROLL (gnu_loop_stmt) = 1;
1342 break;
1344 case Name_No_Vector:
1345 LOOP_STMT_NO_VECTOR (gnu_loop_stmt) = 1;
1346 break;
1348 case Name_Vector:
1349 LOOP_STMT_VECTOR (gnu_loop_stmt) = 1;
1350 break;
1352 default:
1353 gcc_unreachable ();
1356 break;
1358 case Pragma_Optimize:
1359 switch (Chars (Expression
1360 (First (Pragma_Argument_Associations (gnat_node)))))
1362 case Name_Off:
1363 if (optimize)
1364 post_error ("must specify -O0?", gnat_node);
1365 break;
1367 case Name_Space:
1368 if (!optimize_size)
1369 post_error ("must specify -Os?", gnat_node);
1370 break;
1372 case Name_Time:
1373 if (!optimize)
1374 post_error ("insufficient -O value?", gnat_node);
1375 break;
1377 default:
1378 gcc_unreachable ();
1380 break;
1382 case Pragma_Reviewable:
1383 if (write_symbols == NO_DEBUG)
1384 post_error ("must specify -g?", gnat_node);
1385 break;
1387 case Pragma_Warning_As_Error:
1388 case Pragma_Warnings:
1390 Node_Id gnat_expr;
1391 /* Preserve the location of the pragma. */
1392 const location_t location = input_location;
1393 struct cl_option_handlers handlers;
1394 unsigned int option_index;
1395 diagnostic_t kind;
1396 bool imply;
1398 gnat_temp = First (Pragma_Argument_Associations (gnat_node));
1400 /* This is the String form: pragma Warning{s|_As_Error}(String). */
1401 if (Nkind (Expression (gnat_temp)) == N_String_Literal)
1403 switch (pragma_id)
1405 case Pragma_Warning_As_Error:
1406 kind = DK_ERROR;
1407 imply = false;
1408 break;
1410 case Pragma_Warnings:
1411 kind = DK_WARNING;
1412 imply = true;
1413 break;
1415 default:
1416 gcc_unreachable ();
1419 gnat_expr = Expression (gnat_temp);
1422 /* This is the On/Off form: pragma Warnings (On | Off [,String]). */
1423 else if (Nkind (Expression (gnat_temp)) == N_Identifier)
1425 switch (Chars (Expression (gnat_temp)))
1427 case Name_Off:
1428 kind = DK_IGNORED;
1429 break;
1431 case Name_On:
1432 kind = DK_WARNING;
1433 break;
1435 default:
1436 gcc_unreachable ();
1439 /* Deal with optional pattern (but ignore Reason => "..."). */
1440 if (Present (Next (gnat_temp))
1441 && Chars (Next (gnat_temp)) != Name_Reason)
1443 /* pragma Warnings (On | Off, Name) is handled differently. */
1444 if (Nkind (Expression (Next (gnat_temp))) != N_String_Literal)
1445 break;
1447 gnat_expr = Expression (Next (gnat_temp));
1449 else
1450 gnat_expr = Empty;
1452 imply = false;
1455 else
1456 gcc_unreachable ();
1458 /* This is the same implementation as in the C family of compilers. */
1459 const unsigned int lang_mask = CL_Ada | CL_COMMON;
1460 const char *arg = NULL;
1461 if (Present (gnat_expr))
1463 tree gnu_expr = gnat_to_gnu (gnat_expr);
1464 const char *option_string = TREE_STRING_POINTER (gnu_expr);
1465 const int len = TREE_STRING_LENGTH (gnu_expr);
1466 if (len < 3 || option_string[0] != '-' || option_string[1] != 'W')
1467 break;
1468 option_index = find_opt (option_string + 1, lang_mask);
1469 if (option_index == OPT_SPECIAL_unknown)
1471 post_error ("?unknown -W switch", gnat_node);
1472 break;
1474 else if (!(cl_options[option_index].flags & CL_WARNING))
1476 post_error ("?-W switch does not control warning", gnat_node);
1477 break;
1479 else if (!(cl_options[option_index].flags & lang_mask))
1481 post_error ("?-W switch not valid for Ada", gnat_node);
1482 break;
1484 if (cl_options[option_index].flags & CL_JOINED)
1485 arg = option_string + 1 + cl_options[option_index].opt_len;
1487 else
1488 option_index = 0;
1490 set_default_handlers (&handlers, NULL);
1491 control_warning_option (option_index, (int) kind, arg, imply, location,
1492 lang_mask, &handlers, &global_options,
1493 &global_options_set, global_dc);
1495 break;
1497 default:
1498 break;
1501 return gnu_result;
1505 /* Check the inline status of nested function FNDECL wrt its parent function.
1507 If a non-inline nested function is referenced from an inline external
1508 function, we cannot honor both requests at the same time without cloning
1509 the nested function in the current unit since it is private to its unit.
1510 We could inline it as well but it's probably better to err on the side
1511 of too little inlining.
1513 This must be done only on nested functions present in the source code
1514 and not on nested functions generated by the compiler, e.g. finalizers,
1515 because they may be not marked inline and we don't want them to block
1516 the inlining of the parent function. */
1518 static void
1519 check_inlining_for_nested_subprog (tree fndecl)
1521 if (DECL_IGNORED_P (current_function_decl) || DECL_IGNORED_P (fndecl))
1522 return;
1524 if (DECL_DECLARED_INLINE_P (fndecl))
1525 return;
1527 tree parent_decl = decl_function_context (fndecl);
1528 if (DECL_EXTERNAL (parent_decl) && DECL_DECLARED_INLINE_P (parent_decl))
1530 const location_t loc1 = DECL_SOURCE_LOCATION (fndecl);
1531 const location_t loc2 = DECL_SOURCE_LOCATION (parent_decl);
1533 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (parent_decl)))
1535 error_at (loc1, "subprogram %q+F not marked Inline_Always", fndecl);
1536 error_at (loc2, "parent subprogram cannot be inlined");
1538 else
1540 warning_at (loc1, OPT_Winline, "subprogram %q+F not marked Inline",
1541 fndecl);
1542 warning_at (loc2, OPT_Winline, "parent subprogram cannot be inlined");
1545 DECL_DECLARED_INLINE_P (parent_decl) = 0;
1546 DECL_UNINLINABLE (parent_decl) = 1;
1550 /* Return an expression for the length of TYPE, an integral type, computed in
1551 RESULT_TYPE, another integral type.
1553 We used to compute the length as MAX (hb - lb + 1, 0) which could overflow
1554 when lb == TYPE'First. We now compute it as (hb >= lb) ? hb - lb + 1 : 0
1555 which would only overflow in much rarer cases, for extremely large arrays
1556 we expect never to encounter in practice. Besides, the former computation
1557 required the use of potentially constraining signed arithmetics while the
1558 latter does not. Note that the comparison must be done in the original
1559 base index type in order to avoid any overflow during the conversion. */
1561 static tree
1562 get_type_length (tree type, tree result_type)
1564 tree comp_type = get_base_type (result_type);
1565 tree base_type = maybe_character_type (get_base_type (type));
1566 tree lb = convert (base_type, TYPE_MIN_VALUE (type));
1567 tree hb = convert (base_type, TYPE_MAX_VALUE (type));
1568 tree length
1569 = build_binary_op (PLUS_EXPR, comp_type,
1570 build_binary_op (MINUS_EXPR, comp_type,
1571 convert (comp_type, hb),
1572 convert (comp_type, lb)),
1573 build_int_cst (comp_type, 1));
1574 length
1575 = build_cond_expr (result_type,
1576 build_binary_op (GE_EXPR, boolean_type_node, hb, lb),
1577 convert (result_type, length),
1578 build_int_cst (result_type, 0));
1579 return length;
1582 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node,
1583 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
1584 where we should place the result type. ATTRIBUTE is the attribute ID. */
1586 static tree
1587 Attribute_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, int attribute)
1589 const Node_Id gnat_prefix = Prefix (gnat_node);
1590 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
1591 tree gnu_type = TREE_TYPE (gnu_prefix);
1592 tree gnu_expr, gnu_result_type, gnu_result = error_mark_node;
1593 bool prefix_unused = false;
1595 /* If the input is a NULL_EXPR, make a new one. */
1596 if (TREE_CODE (gnu_prefix) == NULL_EXPR)
1598 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1599 *gnu_result_type_p = gnu_result_type;
1600 return build1 (NULL_EXPR, gnu_result_type, TREE_OPERAND (gnu_prefix, 0));
1603 switch (attribute)
1605 case Attr_Pos:
1606 case Attr_Val:
1607 /* These are just conversions since representation clauses for
1608 enumeration types are handled in the front-end. */
1609 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1610 if (attribute == Attr_Pos)
1611 gnu_expr = maybe_character_value (gnu_expr);
1612 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1613 gnu_result = convert (gnu_result_type, gnu_expr);
1614 break;
1616 case Attr_Pred:
1617 case Attr_Succ:
1618 /* These just add or subtract the constant 1 since representation
1619 clauses for enumeration types are handled in the front-end. */
1620 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
1621 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1622 gnu_type = maybe_character_type (gnu_result_type);
1623 if (TREE_TYPE (gnu_expr) != gnu_type)
1624 gnu_expr = convert (gnu_type, gnu_expr);
1625 gnu_result
1626 = build_binary_op (attribute == Attr_Pred ? MINUS_EXPR : PLUS_EXPR,
1627 gnu_type, gnu_expr, build_int_cst (gnu_type, 1));
1628 break;
1630 case Attr_Address:
1631 case Attr_Unrestricted_Access:
1632 /* Conversions don't change the address of references but can cause
1633 build_unary_op to miss the references below, so strip them off.
1634 On the contrary, if the address-of operation causes a temporary
1635 to be created, then it must be created with the proper type. */
1636 gnu_expr = remove_conversions (gnu_prefix,
1637 !Must_Be_Byte_Aligned (gnat_node));
1638 if (REFERENCE_CLASS_P (gnu_expr))
1639 gnu_prefix = gnu_expr;
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;
1720 /* On targets for which function symbols denote a descriptor, the
1721 code address is stored within the first slot of the descriptor
1722 so we do an additional dereference:
1723 result = *((result_type *) result)
1724 where we expect result to be of some pointer type already. */
1725 if (targetm.calls.custom_function_descriptors == 0)
1726 gnu_result
1727 = build_unary_op (INDIRECT_REF, NULL_TREE,
1728 convert (build_pointer_type (gnu_result_type),
1729 gnu_result));
1732 /* For 'Access, issue an error message if the prefix is a C++ method
1733 since it can use a special calling convention on some platforms,
1734 which cannot be propagated to the access type. */
1735 else if (attribute == Attr_Access
1736 && Nkind (gnat_prefix) == N_Identifier
1737 && is_cplusplus_method (Entity (gnat_prefix)))
1738 post_error ("access to C++ constructor or member function not allowed",
1739 gnat_node);
1741 /* For other address attributes applied to a nested function,
1742 find an inner ADDR_EXPR and annotate it so that we can issue
1743 a useful warning with -Wtrampolines. */
1744 else if (TREE_CODE (TREE_TYPE (gnu_prefix)) == FUNCTION_TYPE)
1746 gnu_expr = remove_conversions (gnu_result, false);
1748 if (TREE_CODE (gnu_expr) == ADDR_EXPR
1749 && decl_function_context (TREE_OPERAND (gnu_expr, 0)))
1751 set_expr_location_from_node (gnu_expr, gnat_node);
1753 /* Also check the inlining status. */
1754 check_inlining_for_nested_subprog (TREE_OPERAND (gnu_expr, 0));
1756 /* Moreover, for 'Access or 'Unrestricted_Access with non-
1757 foreign-compatible representation, mark the ADDR_EXPR so
1758 that we can build a descriptor instead of a trampoline. */
1759 if ((attribute == Attr_Access
1760 || attribute == Attr_Unrestricted_Access)
1761 && targetm.calls.custom_function_descriptors > 0
1762 && Can_Use_Internal_Rep (Etype (gnat_node)))
1763 FUNC_ADDR_BY_DESCRIPTOR (gnu_expr) = 1;
1765 /* Otherwise, we need to check that we are not violating the
1766 No_Implicit_Dynamic_Code restriction. */
1767 else if (targetm.calls.custom_function_descriptors != 0)
1768 Check_Implicit_Dynamic_Code_Allowed (gnat_node);
1771 break;
1773 case Attr_Pool_Address:
1775 tree gnu_ptr = gnu_prefix;
1776 tree gnu_obj_type;
1778 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1780 /* If this is fat pointer, the object must have been allocated with the
1781 template in front of the array. So compute the template address; do
1782 it by converting to a thin pointer. */
1783 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
1784 gnu_ptr
1785 = convert (build_pointer_type
1786 (TYPE_OBJECT_RECORD_TYPE
1787 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
1788 gnu_ptr);
1790 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
1792 /* If this is a thin pointer, the object must have been allocated with
1793 the template in front of the array. So compute the template address
1794 and return it. */
1795 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
1796 gnu_ptr
1797 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
1798 gnu_ptr,
1799 fold_build1 (NEGATE_EXPR, sizetype,
1800 byte_position
1801 (DECL_CHAIN
1802 TYPE_FIELDS ((gnu_obj_type)))));
1804 gnu_result = convert (gnu_result_type, gnu_ptr);
1806 break;
1808 case Attr_Size:
1809 case Attr_Object_Size:
1810 case Attr_Value_Size:
1811 case Attr_Max_Size_In_Storage_Elements:
1812 gnu_expr = gnu_prefix;
1814 /* Remove NOPs and conversions between original and packable version
1815 from GNU_EXPR, and conversions from GNU_PREFIX. We use GNU_EXPR
1816 to see if a COMPONENT_REF was involved. */
1817 while (TREE_CODE (gnu_expr) == NOP_EXPR
1818 || (TREE_CODE (gnu_expr) == VIEW_CONVERT_EXPR
1819 && TREE_CODE (TREE_TYPE (gnu_expr)) == RECORD_TYPE
1820 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
1821 == RECORD_TYPE
1822 && TYPE_NAME (TREE_TYPE (gnu_expr))
1823 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
1824 gnu_expr = TREE_OPERAND (gnu_expr, 0);
1826 gnu_prefix = remove_conversions (gnu_prefix, true);
1827 prefix_unused = true;
1828 gnu_type = TREE_TYPE (gnu_prefix);
1830 /* Replace an unconstrained array type with the type of the underlying
1831 array. We can't do this with a call to maybe_unconstrained_array
1832 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
1833 use the record type that will be used to allocate the object and its
1834 template. */
1835 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
1837 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
1838 if (attribute != Attr_Max_Size_In_Storage_Elements)
1839 gnu_type = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
1842 /* If we're looking for the size of a field, return the field size. */
1843 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1844 gnu_result = DECL_SIZE (TREE_OPERAND (gnu_prefix, 1));
1846 /* Otherwise, if the prefix is an object, or if we are looking for
1847 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the
1848 GCC size of the type. We make an exception for padded objects,
1849 as we do not take into account alignment promotions for the size.
1850 This is in keeping with the object case of gnat_to_gnu_entity. */
1851 else if ((TREE_CODE (gnu_prefix) != TYPE_DECL
1852 && !(TYPE_IS_PADDING_P (gnu_type)
1853 && TREE_CODE (gnu_expr) == COMPONENT_REF))
1854 || attribute == Attr_Object_Size
1855 || attribute == Attr_Max_Size_In_Storage_Elements)
1857 /* If this is a dereference and we have a special dynamic constrained
1858 subtype on the prefix, use it to compute the size; otherwise, use
1859 the designated subtype. */
1860 if (Nkind (gnat_prefix) == N_Explicit_Dereference)
1862 Node_Id gnat_actual_subtype
1863 = Actual_Designated_Subtype (gnat_prefix);
1864 tree gnu_ptr_type
1865 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix)));
1867 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)
1868 && Present (gnat_actual_subtype))
1870 tree gnu_actual_obj_type
1871 = gnat_to_gnu_type (gnat_actual_subtype);
1872 gnu_type
1873 = build_unc_object_type_from_ptr (gnu_ptr_type,
1874 gnu_actual_obj_type,
1875 get_identifier ("SIZE"),
1876 false);
1880 gnu_result = TYPE_SIZE (gnu_type);
1883 /* Otherwise, the result is the RM size of the type. */
1884 else
1885 gnu_result = rm_size (gnu_type);
1887 /* Deal with a self-referential size by returning the maximum size for
1888 a type and by qualifying the size with the object otherwise. */
1889 if (CONTAINS_PLACEHOLDER_P (gnu_result))
1891 if (TREE_CODE (gnu_prefix) == TYPE_DECL)
1892 gnu_result = max_size (gnu_result, true);
1893 else
1894 gnu_result = substitute_placeholder_in_expr (gnu_result, gnu_expr);
1897 /* If the type contains a template, subtract its size. */
1898 if (TREE_CODE (gnu_type) == RECORD_TYPE
1899 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1900 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1901 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1903 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
1904 if (attribute == Attr_Max_Size_In_Storage_Elements)
1905 gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node);
1907 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1908 break;
1910 case Attr_Alignment:
1912 unsigned int align;
1914 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1915 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
1916 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1918 gnu_type = TREE_TYPE (gnu_prefix);
1919 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1920 prefix_unused = true;
1922 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1923 align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT;
1924 else
1926 Entity_Id gnat_type = Etype (gnat_prefix);
1927 unsigned int double_align;
1928 bool is_capped_double, align_clause;
1930 /* If the default alignment of "double" or larger scalar types is
1931 specifically capped and there is an alignment clause neither
1932 on the type nor on the prefix itself, return the cap. */
1933 if ((double_align = double_float_alignment) > 0)
1934 is_capped_double
1935 = is_double_float_or_array (gnat_type, &align_clause);
1936 else if ((double_align = double_scalar_alignment) > 0)
1937 is_capped_double
1938 = is_double_scalar_or_array (gnat_type, &align_clause);
1939 else
1940 is_capped_double = align_clause = false;
1942 if (is_capped_double
1943 && Nkind (gnat_prefix) == N_Identifier
1944 && Present (Alignment_Clause (Entity (gnat_prefix))))
1945 align_clause = true;
1947 if (is_capped_double && !align_clause)
1948 align = double_align;
1949 else
1950 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
1953 gnu_result = size_int (align);
1955 break;
1957 case Attr_First:
1958 case Attr_Last:
1959 case Attr_Range_Length:
1960 prefix_unused = true;
1962 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
1964 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1966 if (attribute == Attr_First)
1967 gnu_result = TYPE_MIN_VALUE (gnu_type);
1968 else if (attribute == Attr_Last)
1969 gnu_result = TYPE_MAX_VALUE (gnu_type);
1970 else
1971 gnu_result = get_type_length (gnu_type, gnu_result_type);
1972 break;
1975 /* ... fall through ... */
1977 case Attr_Length:
1979 int Dimension = (Present (Expressions (gnat_node))
1980 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
1981 : 1), i;
1982 struct parm_attr_d *pa = NULL;
1983 Entity_Id gnat_param = Empty;
1984 bool unconstrained_ptr_deref = false;
1986 /* Make sure any implicit dereference gets done. */
1987 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1988 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1990 /* We treat unconstrained array In parameters specially. We also note
1991 whether we are dereferencing a pointer to unconstrained array. */
1992 if (!Is_Constrained (Etype (gnat_prefix)))
1993 switch (Nkind (gnat_prefix))
1995 case N_Identifier:
1996 /* This is the direct case. */
1997 if (Ekind (Entity (gnat_prefix)) == E_In_Parameter)
1998 gnat_param = Entity (gnat_prefix);
1999 break;
2001 case N_Explicit_Dereference:
2002 /* This is the indirect case. Note that we need to be sure that
2003 the access value cannot be null as we'll hoist the load. */
2004 if (Nkind (Prefix (gnat_prefix)) == N_Identifier
2005 && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter)
2007 if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix))))
2008 gnat_param = Entity (Prefix (gnat_prefix));
2010 else
2011 unconstrained_ptr_deref = true;
2012 break;
2014 default:
2015 break;
2018 /* If the prefix is the view conversion of a constrained array to an
2019 unconstrained form, we retrieve the constrained array because we
2020 might not be able to substitute the PLACEHOLDER_EXPR coming from
2021 the conversion. This can occur with the 'Old attribute applied
2022 to a parameter with an unconstrained type, which gets rewritten
2023 into a constrained local variable very late in the game. */
2024 if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR
2025 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix)))
2026 && !CONTAINS_PLACEHOLDER_P
2027 (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
2028 gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0));
2029 else
2030 gnu_type = TREE_TYPE (gnu_prefix);
2032 prefix_unused = true;
2033 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2035 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
2037 int ndim;
2038 tree gnu_type_temp;
2040 for (ndim = 1, gnu_type_temp = gnu_type;
2041 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
2042 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
2043 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
2046 Dimension = ndim + 1 - Dimension;
2049 for (i = 1; i < Dimension; i++)
2050 gnu_type = TREE_TYPE (gnu_type);
2052 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2054 /* When not optimizing, look up the slot associated with the parameter
2055 and the dimension in the cache and create a new one on failure.
2056 Don't do this when the actual subtype needs debug info (this happens
2057 with -gnatD): in elaborate_expression_1, we create variables that
2058 hold the bounds, so caching attributes isn't very interesting and
2059 causes dependency issues between these variables and cached
2060 expressions. */
2061 if (!optimize
2062 && Present (gnat_param)
2063 && !(Present (Actual_Subtype (gnat_param))
2064 && Needs_Debug_Info (Actual_Subtype (gnat_param))))
2066 FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa)
2067 if (pa->id == gnat_param && pa->dim == Dimension)
2068 break;
2070 if (!pa)
2072 pa = ggc_cleared_alloc<parm_attr_d> ();
2073 pa->id = gnat_param;
2074 pa->dim = Dimension;
2075 vec_safe_push (f_parm_attr_cache, pa);
2079 /* Return the cached expression or build a new one. */
2080 if (attribute == Attr_First)
2082 if (pa && pa->first)
2084 gnu_result = pa->first;
2085 break;
2088 gnu_result
2089 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2092 else if (attribute == Attr_Last)
2094 if (pa && pa->last)
2096 gnu_result = pa->last;
2097 break;
2100 gnu_result
2101 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2104 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
2106 if (pa && pa->length)
2108 gnu_result = pa->length;
2109 break;
2112 gnu_result
2113 = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)),
2114 gnu_result_type);
2117 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2118 handling. Note that these attributes could not have been used on
2119 an unconstrained array type. */
2120 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2122 /* Cache the expression we have just computed. Since we want to do it
2123 at run time, we force the use of a SAVE_EXPR and let the gimplifier
2124 create the temporary in the outermost binding level. We will make
2125 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
2126 paths by forcing its evaluation on entry of the function. */
2127 if (pa)
2129 gnu_result
2130 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2131 switch (attribute)
2133 case Attr_First:
2134 pa->first = gnu_result;
2135 break;
2137 case Attr_Last:
2138 pa->last = gnu_result;
2139 break;
2141 case Attr_Length:
2142 case Attr_Range_Length:
2143 pa->length = gnu_result;
2144 break;
2146 default:
2147 gcc_unreachable ();
2151 /* Otherwise, evaluate it each time it is referenced. */
2152 else
2153 switch (attribute)
2155 case Attr_First:
2156 case Attr_Last:
2157 /* If we are dereferencing a pointer to unconstrained array, we
2158 need to capture the value because the pointed-to bounds may
2159 subsequently be released. */
2160 if (unconstrained_ptr_deref)
2161 gnu_result
2162 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2163 break;
2165 case Attr_Length:
2166 case Attr_Range_Length:
2167 /* Set the source location onto the predicate of the condition
2168 but not if the expression is cached to avoid messing up the
2169 debug info. */
2170 if (TREE_CODE (gnu_result) == COND_EXPR
2171 && EXPR_P (TREE_OPERAND (gnu_result, 0)))
2172 set_expr_location_from_node (TREE_OPERAND (gnu_result, 0),
2173 gnat_node);
2174 break;
2176 default:
2177 gcc_unreachable ();
2180 break;
2183 case Attr_Bit_Position:
2184 case Attr_Position:
2185 case Attr_First_Bit:
2186 case Attr_Last_Bit:
2187 case Attr_Bit:
2189 HOST_WIDE_INT bitsize;
2190 HOST_WIDE_INT bitpos;
2191 tree gnu_offset;
2192 tree gnu_field_bitpos;
2193 tree gnu_field_offset;
2194 tree gnu_inner;
2195 machine_mode mode;
2196 int unsignedp, reversep, volatilep;
2198 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2199 gnu_prefix = remove_conversions (gnu_prefix, true);
2200 prefix_unused = true;
2202 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
2203 the result is 0. Don't allow 'Bit on a bare component, though. */
2204 if (attribute == Attr_Bit
2205 && TREE_CODE (gnu_prefix) != COMPONENT_REF
2206 && TREE_CODE (gnu_prefix) != FIELD_DECL)
2208 gnu_result = integer_zero_node;
2209 break;
2212 else
2213 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
2214 || (attribute == Attr_Bit_Position
2215 && TREE_CODE (gnu_prefix) == FIELD_DECL));
2217 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
2218 &mode, &unsignedp, &reversep, &volatilep);
2220 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
2222 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
2223 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
2225 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
2226 TREE_CODE (gnu_inner) == COMPONENT_REF
2227 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
2228 gnu_inner = TREE_OPERAND (gnu_inner, 0))
2230 gnu_field_bitpos
2231 = size_binop (PLUS_EXPR, gnu_field_bitpos,
2232 bit_position (TREE_OPERAND (gnu_inner, 1)));
2233 gnu_field_offset
2234 = size_binop (PLUS_EXPR, gnu_field_offset,
2235 byte_position (TREE_OPERAND (gnu_inner, 1)));
2238 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
2240 gnu_field_bitpos = bit_position (gnu_prefix);
2241 gnu_field_offset = byte_position (gnu_prefix);
2243 else
2245 gnu_field_bitpos = bitsize_zero_node;
2246 gnu_field_offset = size_zero_node;
2249 switch (attribute)
2251 case Attr_Position:
2252 gnu_result = gnu_field_offset;
2253 break;
2255 case Attr_First_Bit:
2256 case Attr_Bit:
2257 gnu_result = size_int (bitpos % BITS_PER_UNIT);
2258 break;
2260 case Attr_Last_Bit:
2261 gnu_result = bitsize_int (bitpos % BITS_PER_UNIT);
2262 gnu_result = size_binop (PLUS_EXPR, gnu_result,
2263 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
2264 /* ??? Avoid a large unsigned result that will overflow when
2265 converted to the signed universal_integer. */
2266 if (integer_zerop (gnu_result))
2267 gnu_result = integer_minus_one_node;
2268 else
2269 gnu_result
2270 = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node);
2271 break;
2273 case Attr_Bit_Position:
2274 gnu_result = gnu_field_bitpos;
2275 break;
2278 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2279 handling. */
2280 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2281 break;
2284 case Attr_Min:
2285 case Attr_Max:
2287 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
2288 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
2290 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2292 /* The result of {MIN,MAX}_EXPR is unspecified if either operand is
2293 a NaN so we implement the semantics of C99 f{min,max} to make it
2294 predictable in this case: if either operand is a NaN, the other
2295 is returned; if both operands are NaN's, a NaN is returned. */
2296 if (SCALAR_FLOAT_TYPE_P (gnu_result_type)
2297 && !Machine_Overflows_On_Target)
2299 const bool lhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_lhs);
2300 const bool rhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_rhs);
2301 tree t = builtin_decl_explicit (BUILT_IN_ISNAN);
2302 tree lhs_is_nan, rhs_is_nan;
2304 /* If the operands have side-effects, they need to be evaluated
2305 only once in spite of the multiple references in the result. */
2306 if (lhs_side_effects_p)
2307 gnu_lhs = gnat_protect_expr (gnu_lhs);
2308 if (rhs_side_effects_p)
2309 gnu_rhs = gnat_protect_expr (gnu_rhs);
2311 lhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2312 build_call_expr (t, 1, gnu_lhs),
2313 integer_zero_node);
2315 rhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2316 build_call_expr (t, 1, gnu_rhs),
2317 integer_zero_node);
2319 gnu_result = build_binary_op (attribute == Attr_Min
2320 ? MIN_EXPR : MAX_EXPR,
2321 gnu_result_type, gnu_lhs, gnu_rhs);
2322 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2323 rhs_is_nan, gnu_lhs, gnu_result);
2324 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2325 lhs_is_nan, gnu_rhs, gnu_result);
2327 /* If the operands have side-effects, they need to be evaluated
2328 before doing the tests above since the place they otherwise
2329 would end up being evaluated at run time could be wrong. */
2330 if (lhs_side_effects_p)
2331 gnu_result
2332 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_lhs, gnu_result);
2334 if (rhs_side_effects_p)
2335 gnu_result
2336 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_rhs, gnu_result);
2338 else
2339 gnu_result = build_binary_op (attribute == Attr_Min
2340 ? MIN_EXPR : MAX_EXPR,
2341 gnu_result_type, gnu_lhs, gnu_rhs);
2343 break;
2345 case Attr_Passed_By_Reference:
2346 gnu_result = size_int (default_pass_by_ref (gnu_type)
2347 || must_pass_by_ref (gnu_type));
2348 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2349 break;
2351 case Attr_Component_Size:
2352 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
2353 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
2354 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
2356 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2357 gnu_type = TREE_TYPE (gnu_prefix);
2359 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
2360 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
2362 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2363 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2364 gnu_type = TREE_TYPE (gnu_type);
2366 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2368 /* Note this size cannot be self-referential. */
2369 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
2370 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2371 prefix_unused = true;
2372 break;
2374 case Attr_Descriptor_Size:
2375 gnu_type = TREE_TYPE (gnu_prefix);
2376 gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE);
2378 /* What we want is the offset of the ARRAY field in the record
2379 that the thin pointer designates. */
2380 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
2381 gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
2382 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2383 prefix_unused = true;
2384 break;
2386 case Attr_Null_Parameter:
2387 /* This is just a zero cast to the pointer type for our prefix and
2388 dereferenced. */
2389 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2390 gnu_result
2391 = build_unary_op (INDIRECT_REF, NULL_TREE,
2392 convert (build_pointer_type (gnu_result_type),
2393 integer_zero_node));
2394 TREE_PRIVATE (gnu_result) = 1;
2395 break;
2397 case Attr_Mechanism_Code:
2399 Entity_Id gnat_obj = Entity (gnat_prefix);
2400 int code;
2402 prefix_unused = true;
2403 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2404 if (Present (Expressions (gnat_node)))
2406 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
2408 for (gnat_obj = First_Formal (gnat_obj); i > 1;
2409 i--, gnat_obj = Next_Formal (gnat_obj))
2413 code = Mechanism (gnat_obj);
2414 if (code == Default)
2415 code = ((present_gnu_tree (gnat_obj)
2416 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
2417 || ((TREE_CODE (get_gnu_tree (gnat_obj))
2418 == PARM_DECL)
2419 && (DECL_BY_COMPONENT_PTR_P
2420 (get_gnu_tree (gnat_obj))))))
2421 ? By_Reference : By_Copy);
2422 gnu_result = convert (gnu_result_type, size_int (- code));
2424 break;
2426 case Attr_Model:
2427 /* We treat Model as identical to Machine. This is true for at least
2428 IEEE and some other nice floating-point systems. */
2430 /* ... fall through ... */
2432 case Attr_Machine:
2433 /* The trick is to force the compiler to store the result in memory so
2434 that we do not have extra precision used. But do this only when this
2435 is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of
2436 the type is lower than that of the longest floating-point type. */
2437 prefix_unused = true;
2438 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2439 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2440 gnu_result = convert (gnu_result_type, gnu_expr);
2442 if (TREE_CODE (gnu_result) != REAL_CST
2443 && fp_arith_may_widen
2444 && TYPE_PRECISION (gnu_result_type)
2445 < TYPE_PRECISION (longest_float_type_node))
2447 tree rec_type = make_node (RECORD_TYPE);
2448 tree field
2449 = create_field_decl (get_identifier ("OBJ"), gnu_result_type,
2450 rec_type, NULL_TREE, NULL_TREE, 0, 0);
2451 tree rec_val, asm_expr;
2453 finish_record_type (rec_type, field, 0, false);
2455 rec_val = build_constructor_single (rec_type, field, gnu_result);
2456 rec_val = build1 (SAVE_EXPR, rec_type, rec_val);
2458 asm_expr
2459 = build5 (ASM_EXPR, void_type_node,
2460 build_string (0, ""),
2461 tree_cons (build_tree_list (NULL_TREE,
2462 build_string (2, "=m")),
2463 rec_val, NULL_TREE),
2464 tree_cons (build_tree_list (NULL_TREE,
2465 build_string (1, "m")),
2466 rec_val, NULL_TREE),
2467 NULL_TREE, NULL_TREE);
2468 ASM_VOLATILE_P (asm_expr) = 1;
2470 gnu_result
2471 = build_compound_expr (gnu_result_type, asm_expr,
2472 build_component_ref (rec_val, field,
2473 false));
2475 break;
2477 case Attr_Deref:
2478 prefix_unused = true;
2479 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2480 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2481 /* This can be a random address so build an alias-all pointer type. */
2482 gnu_expr
2483 = convert (build_pointer_type_for_mode (gnu_result_type, ptr_mode,
2484 true),
2485 gnu_expr);
2486 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_expr);
2487 break;
2489 default:
2490 /* This abort means that we have an unimplemented attribute. */
2491 gcc_unreachable ();
2494 /* If this is an attribute where the prefix was unused, force a use of it if
2495 it has a side-effect. But don't do it if the prefix is just an entity
2496 name. However, if an access check is needed, we must do it. See second
2497 example in AARM 11.6(5.e). */
2498 if (prefix_unused
2499 && TREE_SIDE_EFFECTS (gnu_prefix)
2500 && !Is_Entity_Name (gnat_prefix))
2501 gnu_result
2502 = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result);
2504 *gnu_result_type_p = gnu_result_type;
2505 return gnu_result;
2508 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
2509 to a GCC tree, which is returned. */
2511 static tree
2512 Case_Statement_to_gnu (Node_Id gnat_node)
2514 tree gnu_result, gnu_expr, gnu_type, gnu_label;
2515 Node_Id gnat_when;
2516 location_t end_locus;
2517 bool may_fallthru = false;
2519 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2520 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2521 gnu_expr = maybe_character_value (gnu_expr);
2522 gnu_type = TREE_TYPE (gnu_expr);
2524 /* We build a SWITCH_EXPR that contains the code with interspersed
2525 CASE_LABEL_EXPRs for each label. */
2526 if (!Sloc_to_locus (End_Location (gnat_node), &end_locus))
2527 end_locus = input_location;
2528 gnu_label = create_artificial_label (end_locus);
2529 start_stmt_group ();
2531 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2532 Present (gnat_when);
2533 gnat_when = Next_Non_Pragma (gnat_when))
2535 bool choices_added_p = false;
2536 Node_Id gnat_choice;
2538 /* First compile all the different case choices for the current WHEN
2539 alternative. */
2540 for (gnat_choice = First (Discrete_Choices (gnat_when));
2541 Present (gnat_choice);
2542 gnat_choice = Next (gnat_choice))
2544 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2545 tree label = create_artificial_label (input_location);
2547 switch (Nkind (gnat_choice))
2549 case N_Range:
2550 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
2551 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
2552 break;
2554 case N_Subtype_Indication:
2555 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
2556 (Constraint (gnat_choice))));
2557 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
2558 (Constraint (gnat_choice))));
2559 break;
2561 case N_Identifier:
2562 case N_Expanded_Name:
2563 /* This represents either a subtype range or a static value of
2564 some kind; Ekind says which. */
2565 if (Is_Type (Entity (gnat_choice)))
2567 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
2569 gnu_low = TYPE_MIN_VALUE (gnu_type);
2570 gnu_high = TYPE_MAX_VALUE (gnu_type);
2571 break;
2574 /* ... fall through ... */
2576 case N_Character_Literal:
2577 case N_Integer_Literal:
2578 gnu_low = gnat_to_gnu (gnat_choice);
2579 break;
2581 case N_Others_Choice:
2582 break;
2584 default:
2585 gcc_unreachable ();
2588 /* Everything should be folded into constants at this point. */
2589 gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST);
2590 gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST);
2592 if (gnu_low && TREE_TYPE (gnu_low) != gnu_type)
2593 gnu_low = convert (gnu_type, gnu_low);
2594 if (gnu_high && TREE_TYPE (gnu_high) != gnu_type)
2595 gnu_high = convert (gnu_type, gnu_high);
2597 add_stmt_with_node (build_case_label (gnu_low, gnu_high, label),
2598 gnat_choice);
2599 choices_added_p = true;
2602 /* This construct doesn't define a scope so we shouldn't push a binding
2603 level around the statement list. Except that we have always done so
2604 historically and this makes it possible to reduce stack usage. As a
2605 compromise, we keep doing it for case statements, for which this has
2606 never been problematic, but not for case expressions in Ada 2012. */
2607 if (choices_added_p)
2609 const bool is_case_expression
2610 = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions);
2611 tree group
2612 = build_stmt_group (Statements (gnat_when), !is_case_expression);
2613 bool group_may_fallthru = block_may_fallthru (group);
2614 add_stmt (group);
2615 if (group_may_fallthru)
2617 tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label);
2618 SET_EXPR_LOCATION (stmt, end_locus);
2619 add_stmt (stmt);
2620 may_fallthru = true;
2625 /* Now emit a definition of the label the cases branch to, if any. */
2626 if (may_fallthru)
2627 add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label));
2628 gnu_result
2629 = build3 (SWITCH_EXPR, gnu_type, gnu_expr, end_stmt_group (), NULL_TREE);
2631 return gnu_result;
2634 /* Return true if we are in the body of a loop. */
2636 static inline bool
2637 inside_loop_p (void)
2639 return !vec_safe_is_empty (gnu_loop_stack);
2642 /* Find out whether EXPR is a simple additive expression based on the iteration
2643 variable of some enclosing loop in the current function. If so, return the
2644 loop and set *DISP to the displacement and *NEG_P to true if this is for a
2645 subtraction; otherwise, return NULL. */
2647 static struct loop_info_d *
2648 find_loop_for (tree expr, tree *disp = NULL, bool *neg_p = NULL)
2650 tree var, add, cst;
2651 bool minus_p;
2652 struct loop_info_d *iter = NULL;
2653 unsigned int i;
2655 if (is_simple_additive_expression (expr, &add, &cst, &minus_p))
2657 var = add;
2658 if (disp)
2659 *disp = cst;
2660 if (neg_p)
2661 *neg_p = minus_p;
2663 else
2665 var = expr;
2666 if (disp)
2667 *disp = NULL_TREE;
2668 if (neg_p)
2669 *neg_p = false;
2672 var = remove_conversions (var, false);
2674 if (TREE_CODE (var) != VAR_DECL)
2675 return NULL;
2677 if (decl_function_context (var) != current_function_decl)
2678 return NULL;
2680 gcc_assert (vec_safe_length (gnu_loop_stack) > 0);
2682 FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack, i, iter)
2683 if (var == iter->loop_var)
2684 break;
2686 return iter;
2689 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2690 false, or the maximum value if MAX is true, of TYPE. */
2692 static bool
2693 can_equal_min_or_max_val_p (tree val, tree type, bool max)
2695 tree min_or_max_val = (max ? TYPE_MAX_VALUE (type) : TYPE_MIN_VALUE (type));
2697 if (TREE_CODE (min_or_max_val) != INTEGER_CST)
2698 return true;
2700 if (TREE_CODE (val) == NOP_EXPR)
2701 val = (max
2702 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val, 0)))
2703 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val, 0))));
2705 if (TREE_CODE (val) != INTEGER_CST)
2706 return true;
2708 if (max)
2709 return tree_int_cst_lt (val, min_or_max_val) == 0;
2710 else
2711 return tree_int_cst_lt (min_or_max_val, val) == 0;
2714 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2715 If REVERSE is true, minimum value is taken as maximum value. */
2717 static inline bool
2718 can_equal_min_val_p (tree val, tree type, bool reverse)
2720 return can_equal_min_or_max_val_p (val, type, reverse);
2723 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2724 If REVERSE is true, maximum value is taken as minimum value. */
2726 static inline bool
2727 can_equal_max_val_p (tree val, tree type, bool reverse)
2729 return can_equal_min_or_max_val_p (val, type, !reverse);
2732 /* Return true if VAL1 can be lower than VAL2. */
2734 static bool
2735 can_be_lower_p (tree val1, tree val2)
2737 if (TREE_CODE (val1) == NOP_EXPR)
2738 val1 = TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val1, 0)));
2740 if (TREE_CODE (val1) != INTEGER_CST)
2741 return true;
2743 if (TREE_CODE (val2) == NOP_EXPR)
2744 val2 = TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val2, 0)));
2746 if (TREE_CODE (val2) != INTEGER_CST)
2747 return true;
2749 return tree_int_cst_lt (val1, val2);
2752 /* Replace EXPR1 and EXPR2 by invariant expressions if possible. Return
2753 true if both expressions have been replaced and false otherwise. */
2755 static bool
2756 make_invariant (tree *expr1, tree *expr2)
2758 tree inv_expr1 = gnat_invariant_expr (*expr1);
2759 tree inv_expr2 = gnat_invariant_expr (*expr2);
2761 if (inv_expr1)
2762 *expr1 = inv_expr1;
2764 if (inv_expr2)
2765 *expr2 = inv_expr2;
2767 return inv_expr1 && inv_expr2;
2770 /* Helper function for walk_tree, used by independent_iterations_p below. */
2772 static tree
2773 scan_rhs_r (tree *tp, int *walk_subtrees, void *data)
2775 bitmap *params = (bitmap *)data;
2776 tree t = *tp;
2778 /* No need to walk into types or decls. */
2779 if (IS_TYPE_OR_DECL_P (t))
2780 *walk_subtrees = 0;
2782 if (TREE_CODE (t) == PARM_DECL && bitmap_bit_p (*params, DECL_UID (t)))
2783 return t;
2785 return NULL_TREE;
2788 /* Return true if STMT_LIST generates independent iterations in a loop. */
2790 static bool
2791 independent_iterations_p (tree stmt_list)
2793 tree_stmt_iterator tsi;
2794 bitmap params = BITMAP_GGC_ALLOC();
2795 auto_vec<tree> rhs;
2796 tree iter;
2797 int i;
2799 if (TREE_CODE (stmt_list) == BIND_EXPR)
2800 stmt_list = BIND_EXPR_BODY (stmt_list);
2802 /* Scan the list and return false on anything that is not either a check
2803 or an assignment to a parameter with restricted aliasing. */
2804 for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi))
2806 tree stmt = tsi_stmt (tsi);
2808 switch (TREE_CODE (stmt))
2810 case COND_EXPR:
2812 if (COND_EXPR_ELSE (stmt))
2813 return false;
2814 if (TREE_CODE (COND_EXPR_THEN (stmt)) != CALL_EXPR)
2815 return false;
2816 tree func = get_callee_fndecl (COND_EXPR_THEN (stmt));
2817 if (!(func && TREE_THIS_VOLATILE (func)))
2818 return false;
2819 break;
2822 case MODIFY_EXPR:
2824 tree lhs = TREE_OPERAND (stmt, 0);
2825 while (handled_component_p (lhs))
2826 lhs = TREE_OPERAND (lhs, 0);
2827 if (TREE_CODE (lhs) != INDIRECT_REF)
2828 return false;
2829 lhs = TREE_OPERAND (lhs, 0);
2830 if (!(TREE_CODE (lhs) == PARM_DECL
2831 && DECL_RESTRICTED_ALIASING_P (lhs)))
2832 return false;
2833 bitmap_set_bit (params, DECL_UID (lhs));
2834 rhs.safe_push (TREE_OPERAND (stmt, 1));
2835 break;
2838 default:
2839 return false;
2843 /* At this point we know that the list contains only statements that will
2844 modify parameters with restricted aliasing. Check that the statements
2845 don't at the time read from these parameters. */
2846 FOR_EACH_VEC_ELT (rhs, i, iter)
2847 if (walk_tree_without_duplicates (&iter, scan_rhs_r, &params))
2848 return false;
2850 return true;
2853 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
2854 to a GCC tree, which is returned. */
2856 static tree
2857 Loop_Statement_to_gnu (Node_Id gnat_node)
2859 const Node_Id gnat_iter_scheme = Iteration_Scheme (gnat_node);
2860 struct loop_info_d *gnu_loop_info = ggc_cleared_alloc<loop_info_d> ();
2861 tree gnu_loop_stmt = build4 (LOOP_STMT, void_type_node, NULL_TREE,
2862 NULL_TREE, NULL_TREE, NULL_TREE);
2863 tree gnu_loop_label = create_artificial_label (input_location);
2864 tree gnu_cond_expr = NULL_TREE, gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2865 tree gnu_result;
2867 /* Push the loop_info structure associated with the LOOP_STMT. */
2868 vec_safe_push (gnu_loop_stack, gnu_loop_info);
2870 /* Set location information for statement and end label. */
2871 set_expr_location_from_node (gnu_loop_stmt, gnat_node);
2872 Sloc_to_locus (Sloc (End_Label (gnat_node)),
2873 &DECL_SOURCE_LOCATION (gnu_loop_label));
2874 LOOP_STMT_LABEL (gnu_loop_stmt) = gnu_loop_label;
2876 /* Save the statement for later reuse. */
2877 gnu_loop_info->stmt = gnu_loop_stmt;
2878 gnu_loop_info->artificial = !Comes_From_Source (gnat_node);
2880 /* Set the condition under which the loop must keep going.
2881 For the case "LOOP .... END LOOP;" the condition is always true. */
2882 if (No (gnat_iter_scheme))
2885 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2886 else if (Present (Condition (gnat_iter_scheme)))
2887 LOOP_STMT_COND (gnu_loop_stmt)
2888 = gnat_to_gnu (Condition (gnat_iter_scheme));
2890 /* Otherwise we have an iteration scheme and the condition is given by the
2891 bounds of the subtype of the iteration variable. */
2892 else
2894 Node_Id gnat_loop_spec = Loop_Parameter_Specification (gnat_iter_scheme);
2895 Entity_Id gnat_loop_var = Defining_Entity (gnat_loop_spec);
2896 Entity_Id gnat_type = Etype (gnat_loop_var);
2897 tree gnu_type = get_unpadded_type (gnat_type);
2898 tree gnu_base_type = maybe_character_type (get_base_type (gnu_type));
2899 tree gnu_one_node = build_int_cst (gnu_base_type, 1);
2900 tree gnu_loop_var, gnu_loop_iv, gnu_first, gnu_last, gnu_stmt;
2901 enum tree_code update_code, test_code, shift_code;
2902 bool reverse = Reverse_Present (gnat_loop_spec), use_iv = false;
2904 gnu_low = convert (gnu_base_type, TYPE_MIN_VALUE (gnu_type));
2905 gnu_high = convert (gnu_base_type, TYPE_MAX_VALUE (gnu_type));
2907 /* We must disable modulo reduction for the iteration variable, if any,
2908 in order for the loop comparison to be effective. */
2909 if (reverse)
2911 gnu_first = gnu_high;
2912 gnu_last = gnu_low;
2913 update_code = MINUS_NOMOD_EXPR;
2914 test_code = GE_EXPR;
2915 shift_code = PLUS_NOMOD_EXPR;
2917 else
2919 gnu_first = gnu_low;
2920 gnu_last = gnu_high;
2921 update_code = PLUS_NOMOD_EXPR;
2922 test_code = LE_EXPR;
2923 shift_code = MINUS_NOMOD_EXPR;
2926 /* We use two different strategies to translate the loop, depending on
2927 whether optimization is enabled.
2929 If it is, we generate the canonical loop form expected by the loop
2930 optimizer and the loop vectorizer, which is the do-while form:
2932 ENTRY_COND
2933 loop:
2934 TOP_UPDATE
2935 BODY
2936 BOTTOM_COND
2937 GOTO loop
2939 This avoids an implicit dependency on loop header copying and makes
2940 it possible to turn BOTTOM_COND into an inequality test.
2942 If optimization is disabled, loop header copying doesn't come into
2943 play and we try to generate the loop form with the fewer conditional
2944 branches. First, the default form, which is:
2946 loop:
2947 TOP_COND
2948 BODY
2949 BOTTOM_UPDATE
2950 GOTO loop
2952 It should catch most loops with constant ending point. Then, if we
2953 cannot, we try to generate the shifted form:
2955 loop:
2956 TOP_COND
2957 TOP_UPDATE
2958 BODY
2959 GOTO loop
2961 which should catch loops with constant starting point. Otherwise, if
2962 we cannot, we generate the fallback form:
2964 ENTRY_COND
2965 loop:
2966 BODY
2967 BOTTOM_COND
2968 BOTTOM_UPDATE
2969 GOTO loop
2971 which works in all cases. */
2973 if (optimize)
2975 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
2976 overflow. */
2977 if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse))
2980 /* Otherwise, use the do-while form with the help of a special
2981 induction variable in the unsigned version of the base type
2982 or the unsigned version of the size type, whichever is the
2983 largest, in order to have wrap-around arithmetics for it. */
2984 else
2986 if (TYPE_PRECISION (gnu_base_type)
2987 > TYPE_PRECISION (size_type_node))
2988 gnu_base_type
2989 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type), 1);
2990 else
2991 gnu_base_type = size_type_node;
2993 gnu_first = convert (gnu_base_type, gnu_first);
2994 gnu_last = convert (gnu_base_type, gnu_last);
2995 gnu_one_node = build_int_cst (gnu_base_type, 1);
2996 use_iv = true;
2999 gnu_first
3000 = build_binary_op (shift_code, gnu_base_type, gnu_first,
3001 gnu_one_node);
3002 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
3003 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
3005 else
3007 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
3008 if (!can_equal_max_val_p (gnu_last, gnu_base_type, reverse))
3011 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
3012 GNU_LAST-1 does. */
3013 else if (!can_equal_min_val_p (gnu_first, gnu_base_type, reverse)
3014 && !can_equal_min_val_p (gnu_last, gnu_base_type, reverse))
3016 gnu_first
3017 = build_binary_op (shift_code, gnu_base_type, gnu_first,
3018 gnu_one_node);
3019 gnu_last
3020 = build_binary_op (shift_code, gnu_base_type, gnu_last,
3021 gnu_one_node);
3022 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt) = 1;
3025 /* Otherwise, use the fallback form. */
3026 else
3027 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt) = 1;
3030 /* If we use the BOTTOM_COND, we can turn the test into an inequality
3031 test but we may have to add ENTRY_COND to protect the empty loop. */
3032 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt))
3034 test_code = NE_EXPR;
3035 if (can_be_lower_p (gnu_high, gnu_low))
3037 gnu_cond_expr
3038 = build3 (COND_EXPR, void_type_node,
3039 build_binary_op (LE_EXPR, boolean_type_node,
3040 gnu_low, gnu_high),
3041 NULL_TREE, alloc_stmt_list ());
3042 set_expr_location_from_node (gnu_cond_expr, gnat_loop_spec);
3046 /* Open a new nesting level that will surround the loop to declare the
3047 iteration variable. */
3048 start_stmt_group ();
3049 gnat_pushlevel ();
3051 /* If we use the special induction variable, create it and set it to
3052 its initial value. Morever, the regular iteration variable cannot
3053 itself be initialized, lest the initial value wrapped around. */
3054 if (use_iv)
3056 gnu_loop_iv
3057 = create_init_temporary ("I", gnu_first, &gnu_stmt, gnat_loop_var);
3058 add_stmt (gnu_stmt);
3059 gnu_first = NULL_TREE;
3061 else
3062 gnu_loop_iv = NULL_TREE;
3064 /* Declare the iteration variable and set it to its initial value. */
3065 gnu_loop_var = gnat_to_gnu_entity (gnat_loop_var, gnu_first, true);
3066 if (DECL_BY_REF_P (gnu_loop_var))
3067 gnu_loop_var = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_loop_var);
3068 else if (use_iv)
3070 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var));
3071 SET_DECL_INDUCTION_VAR (gnu_loop_var, gnu_loop_iv);
3073 gnu_loop_info->loop_var = gnu_loop_var;
3074 gnu_loop_info->low_bound = gnu_low;
3075 gnu_loop_info->high_bound = gnu_high;
3077 /* Do all the arithmetics in the base type. */
3078 gnu_loop_var = convert (gnu_base_type, gnu_loop_var);
3080 /* Set either the top or bottom exit condition. */
3081 if (use_iv)
3082 LOOP_STMT_COND (gnu_loop_stmt)
3083 = build_binary_op (test_code, boolean_type_node, gnu_loop_iv,
3084 gnu_last);
3085 else
3086 LOOP_STMT_COND (gnu_loop_stmt)
3087 = build_binary_op (test_code, boolean_type_node, gnu_loop_var,
3088 gnu_last);
3090 /* Set either the top or bottom update statement and give it the source
3091 location of the iteration for better coverage info. */
3092 if (use_iv)
3094 gnu_stmt
3095 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_iv,
3096 build_binary_op (update_code, gnu_base_type,
3097 gnu_loop_iv, gnu_one_node));
3098 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
3099 append_to_statement_list (gnu_stmt,
3100 &LOOP_STMT_UPDATE (gnu_loop_stmt));
3101 gnu_stmt
3102 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
3103 gnu_loop_iv);
3104 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
3105 append_to_statement_list (gnu_stmt,
3106 &LOOP_STMT_UPDATE (gnu_loop_stmt));
3108 else
3110 gnu_stmt
3111 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_loop_var,
3112 build_binary_op (update_code, gnu_base_type,
3113 gnu_loop_var, gnu_one_node));
3114 set_expr_location_from_node (gnu_stmt, gnat_iter_scheme);
3115 LOOP_STMT_UPDATE (gnu_loop_stmt) = gnu_stmt;
3119 /* If the loop was named, have the name point to this loop. In this case,
3120 the association is not a DECL node, but the end label of the loop. */
3121 if (Present (Identifier (gnat_node)))
3122 save_gnu_tree (Entity (Identifier (gnat_node)), gnu_loop_label, true);
3124 /* Make the loop body into its own block, so any allocated storage will be
3125 released every iteration. This is needed for stack allocation. */
3126 LOOP_STMT_BODY (gnu_loop_stmt)
3127 = build_stmt_group (Statements (gnat_node), true);
3128 TREE_SIDE_EFFECTS (gnu_loop_stmt) = 1;
3130 /* If we have an iteration scheme, then we are in a statement group. Add
3131 the LOOP_STMT to it, finish it and make it the "loop". */
3132 if (Present (gnat_iter_scheme) && No (Condition (gnat_iter_scheme)))
3134 /* First, if we have computed invariant conditions for range (or index)
3135 checks applied to the iteration variable, find out whether they can
3136 be evaluated to false at compile time; otherwise, if there are not
3137 too many of them, combine them with the original checks. If loop
3138 unswitching is enabled, do not require the loop bounds to be also
3139 invariant, as their evaluation will still be ahead of the loop. */
3140 if (vec_safe_length (gnu_loop_info->checks) > 0
3141 && (make_invariant (&gnu_low, &gnu_high) || flag_unswitch_loops))
3143 struct range_check_info_d *rci;
3144 unsigned int i, n_remaining_checks = 0;
3146 FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci)
3148 tree low_ok, high_ok;
3150 if (rci->low_bound)
3152 tree gnu_adjusted_low = convert (rci->type, gnu_low);
3153 if (rci->disp)
3154 gnu_adjusted_low
3155 = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR,
3156 rci->type, gnu_adjusted_low, rci->disp);
3157 low_ok
3158 = build_binary_op (GE_EXPR, boolean_type_node,
3159 gnu_adjusted_low, rci->low_bound);
3161 else
3162 low_ok = boolean_true_node;
3164 if (rci->high_bound)
3166 tree gnu_adjusted_high = convert (rci->type, gnu_high);
3167 if (rci->disp)
3168 gnu_adjusted_high
3169 = fold_build2 (rci->neg_p ? MINUS_EXPR : PLUS_EXPR,
3170 rci->type, gnu_adjusted_high, rci->disp);
3171 high_ok
3172 = build_binary_op (LE_EXPR, boolean_type_node,
3173 gnu_adjusted_high, rci->high_bound);
3175 else
3176 high_ok = boolean_true_node;
3178 tree range_ok
3179 = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
3180 low_ok, high_ok);
3182 rci->invariant_cond
3183 = build_unary_op (TRUTH_NOT_EXPR, boolean_type_node, range_ok);
3185 if (rci->invariant_cond == boolean_false_node)
3186 TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond;
3187 else
3188 n_remaining_checks++;
3191 /* Note that loop unswitching can only be applied a small number of
3192 times to a given loop (PARAM_MAX_UNSWITCH_LEVEL default to 3). */
3193 if (0 < n_remaining_checks && n_remaining_checks <= 3
3194 && optimize > 1 && !optimize_size)
3195 FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci)
3196 if (rci->invariant_cond != boolean_false_node)
3198 TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond;
3200 if (flag_unswitch_loops)
3201 add_stmt_with_node_force (rci->inserted_cond, gnat_node);
3205 /* Second, if loop vectorization is enabled and the iterations of the
3206 loop can easily be proved as independent, mark the loop. */
3207 if (optimize
3208 && flag_tree_loop_vectorize
3209 && independent_iterations_p (LOOP_STMT_BODY (gnu_loop_stmt)))
3210 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
3212 add_stmt (gnu_loop_stmt);
3213 gnat_poplevel ();
3214 gnu_loop_stmt = end_stmt_group ();
3217 /* If we have an outer COND_EXPR, that's our result and this loop is its
3218 "true" statement. Otherwise, the result is the LOOP_STMT. */
3219 if (gnu_cond_expr)
3221 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
3222 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
3223 gnu_result = gnu_cond_expr;
3225 else
3226 gnu_result = gnu_loop_stmt;
3228 gnu_loop_stack->pop ();
3230 return gnu_result;
3233 /* This page implements a form of Named Return Value optimization modelled
3234 on the C++ optimization of the same name. The main difference is that
3235 we disregard any semantical considerations when applying it here, the
3236 counterpart being that we don't try to apply it to semantically loaded
3237 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
3239 We consider a function body of the following GENERIC form:
3241 return_type R1;
3242 [...]
3243 RETURN_EXPR [<retval> = ...]
3244 [...]
3245 RETURN_EXPR [<retval> = R1]
3246 [...]
3247 return_type Ri;
3248 [...]
3249 RETURN_EXPR [<retval> = ...]
3250 [...]
3251 RETURN_EXPR [<retval> = Ri]
3252 [...]
3254 where the Ri are not addressable and we try to fulfill a simple criterion
3255 that would make it possible to replace one or several Ri variables by the
3256 single RESULT_DECL of the function.
3258 The first observation is that RETURN_EXPRs that don't directly reference
3259 any of the Ri variables on the RHS of their assignment are transparent wrt
3260 the optimization. This is because the Ri variables aren't addressable so
3261 any transformation applied to them doesn't affect the RHS; moreover, the
3262 assignment writes the full <retval> object so existing values are entirely
3263 discarded.
3265 This property can be extended to some forms of RETURN_EXPRs that reference
3266 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3267 case, in particular when function calls are involved.
3269 Therefore the algorithm is as follows:
3271 1. Collect the list of candidates for a Named Return Value (Ri variables
3272 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3273 other expressions on the RHS of such assignments.
3275 2. Prune the members of the first list (candidates) that are referenced
3276 by a member of the second list (expressions).
3278 3. Extract a set of candidates with non-overlapping live ranges from the
3279 first list. These are the Named Return Values.
3281 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
3282 Named Return Values in the function with the RESULT_DECL.
3284 If the function returns an unconstrained type, things are a bit different
3285 because the anonymous return object is allocated on the secondary stack
3286 and RESULT_DECL is only a pointer to it. Each return object can be of a
3287 different size and is allocated separately so we need not care about the
3288 addressability and the aforementioned overlapping issues. Therefore, we
3289 don't collect the other expressions and skip step #2 in the algorithm. */
3291 struct nrv_data
3293 bitmap nrv;
3294 tree result;
3295 Node_Id gnat_ret;
3296 hash_set<tree> *visited;
3299 /* Return true if T is a Named Return Value. */
3301 static inline bool
3302 is_nrv_p (bitmap nrv, tree t)
3304 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
3307 /* Helper function for walk_tree, used by finalize_nrv below. */
3309 static tree
3310 prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3312 struct nrv_data *dp = (struct nrv_data *)data;
3313 tree t = *tp;
3315 /* No need to walk into types or decls. */
3316 if (IS_TYPE_OR_DECL_P (t))
3317 *walk_subtrees = 0;
3319 if (is_nrv_p (dp->nrv, t))
3320 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3322 return NULL_TREE;
3325 /* Prune Named Return Values in BLOCK and return true if there is still a
3326 Named Return Value in BLOCK or one of its sub-blocks. */
3328 static bool
3329 prune_nrv_in_block (bitmap nrv, tree block)
3331 bool has_nrv = false;
3332 tree t;
3334 /* First recurse on the sub-blocks. */
3335 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3336 has_nrv |= prune_nrv_in_block (nrv, t);
3338 /* Then make sure to keep at most one NRV per block. */
3339 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3340 if (is_nrv_p (nrv, t))
3342 if (has_nrv)
3343 bitmap_clear_bit (nrv, DECL_UID (t));
3344 else
3345 has_nrv = true;
3348 return has_nrv;
3351 /* Helper function for walk_tree, used by finalize_nrv below. */
3353 static tree
3354 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3356 struct nrv_data *dp = (struct nrv_data *)data;
3357 tree t = *tp;
3359 /* No need to walk into types. */
3360 if (TYPE_P (t))
3361 *walk_subtrees = 0;
3363 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3364 nop, but differs from using NULL_TREE in that it indicates that we care
3365 about the value of the RESULT_DECL. */
3366 else if (TREE_CODE (t) == RETURN_EXPR
3367 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3369 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3371 /* Strip useless conversions around the return value. */
3372 if (gnat_useless_type_conversion (ret_val))
3373 ret_val = TREE_OPERAND (ret_val, 0);
3375 if (is_nrv_p (dp->nrv, ret_val))
3376 TREE_OPERAND (t, 0) = dp->result;
3379 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3380 if needed. */
3381 else if (TREE_CODE (t) == DECL_EXPR
3382 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3384 tree var = DECL_EXPR_DECL (t), init;
3386 if (DECL_INITIAL (var))
3388 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3389 DECL_INITIAL (var));
3390 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3391 DECL_INITIAL (var) = NULL_TREE;
3393 else
3394 init = build_empty_stmt (EXPR_LOCATION (t));
3395 *tp = init;
3397 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3398 SET_DECL_VALUE_EXPR (var, dp->result);
3399 DECL_HAS_VALUE_EXPR_P (var) = 1;
3400 /* ??? Kludge to avoid an assertion failure during inlining. */
3401 DECL_SIZE (var) = bitsize_unit_node;
3402 DECL_SIZE_UNIT (var) = size_one_node;
3405 /* And replace all uses of NRVs with the RESULT_DECL. */
3406 else if (is_nrv_p (dp->nrv, t))
3407 *tp = convert (TREE_TYPE (t), dp->result);
3409 /* Avoid walking into the same tree more than once. Unfortunately, we
3410 can't just use walk_tree_without_duplicates because it would only
3411 call us for the first occurrence of NRVs in the function body. */
3412 if (dp->visited->add (*tp))
3413 *walk_subtrees = 0;
3415 return NULL_TREE;
3418 /* Likewise, but used when the function returns an unconstrained type. */
3420 static tree
3421 finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3423 struct nrv_data *dp = (struct nrv_data *)data;
3424 tree t = *tp;
3426 /* No need to walk into types. */
3427 if (TYPE_P (t))
3428 *walk_subtrees = 0;
3430 /* We need to see the DECL_EXPR of NRVs before any other references so we
3431 walk the body of BIND_EXPR before walking its variables. */
3432 else if (TREE_CODE (t) == BIND_EXPR)
3433 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3435 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3436 return value built by the allocator instead of the whole construct. */
3437 else if (TREE_CODE (t) == RETURN_EXPR
3438 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3440 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3442 /* This is the construct returned by the allocator. */
3443 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3444 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3446 tree rhs = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3448 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
3449 ret_val = CONSTRUCTOR_ELT (rhs, 1)->value;
3450 else
3451 ret_val = rhs;
3454 /* Strip useless conversions around the return value. */
3455 if (gnat_useless_type_conversion (ret_val)
3456 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3457 ret_val = TREE_OPERAND (ret_val, 0);
3459 /* Strip unpadding around the return value. */
3460 if (TREE_CODE (ret_val) == COMPONENT_REF
3461 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3462 ret_val = TREE_OPERAND (ret_val, 0);
3464 /* Assign the new return value to the RESULT_DECL. */
3465 if (is_nrv_p (dp->nrv, ret_val))
3466 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3467 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3470 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3471 into a new variable. */
3472 else if (TREE_CODE (t) == DECL_EXPR
3473 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3475 tree saved_current_function_decl = current_function_decl;
3476 tree var = DECL_EXPR_DECL (t);
3477 tree alloc, p_array, new_var, new_ret;
3478 vec<constructor_elt, va_gc> *v;
3479 vec_alloc (v, 2);
3481 /* Create an artificial context to build the allocation. */
3482 current_function_decl = decl_function_context (var);
3483 start_stmt_group ();
3484 gnat_pushlevel ();
3486 /* This will return a COMPOUND_EXPR with the allocation in the first
3487 arm and the final return value in the second arm. */
3488 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3489 TREE_TYPE (dp->result),
3490 Procedure_To_Call (dp->gnat_ret),
3491 Storage_Pool (dp->gnat_ret),
3492 Empty, false);
3494 /* The new variable is built as a reference to the allocated space. */
3495 new_var
3496 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3497 build_reference_type (TREE_TYPE (var)));
3498 DECL_BY_REFERENCE (new_var) = 1;
3500 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3502 tree cst = TREE_OPERAND (alloc, 1);
3504 /* The new initial value is a COMPOUND_EXPR with the allocation in
3505 the first arm and the value of P_ARRAY in the second arm. */
3506 DECL_INITIAL (new_var)
3507 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3508 TREE_OPERAND (alloc, 0),
3509 CONSTRUCTOR_ELT (cst, 0)->value);
3511 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3512 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3513 CONSTRUCTOR_APPEND_ELT (v, p_array,
3514 fold_convert (TREE_TYPE (p_array), new_var));
3515 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
3516 CONSTRUCTOR_ELT (cst, 1)->value);
3517 new_ret = build_constructor (TREE_TYPE (alloc), v);
3519 else
3521 /* The new initial value is just the allocation. */
3522 DECL_INITIAL (new_var) = alloc;
3523 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3526 gnat_pushdecl (new_var, Empty);
3528 /* Destroy the artificial context and insert the new statements. */
3529 gnat_zaplevel ();
3530 *tp = end_stmt_group ();
3531 current_function_decl = saved_current_function_decl;
3533 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3534 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3535 DECL_CHAIN (var) = new_var;
3536 DECL_IGNORED_P (var) = 1;
3538 /* Save the new return value and the dereference of NEW_VAR. */
3539 DECL_INITIAL (var)
3540 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3541 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3542 /* ??? Kludge to avoid messing up during inlining. */
3543 DECL_CONTEXT (var) = NULL_TREE;
3546 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3547 else if (is_nrv_p (dp->nrv, t))
3548 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3550 /* Avoid walking into the same tree more than once. Unfortunately, we
3551 can't just use walk_tree_without_duplicates because it would only
3552 call us for the first occurrence of NRVs in the function body. */
3553 if (dp->visited->add (*tp))
3554 *walk_subtrees = 0;
3556 return NULL_TREE;
3559 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3560 contains the candidates for Named Return Value and OTHER is a list of
3561 the other return values. GNAT_RET is a representative return node. */
3563 static void
3564 finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
3566 struct cgraph_node *node;
3567 struct nrv_data data;
3568 walk_tree_fn func;
3569 unsigned int i;
3570 tree iter;
3572 /* We shouldn't be applying the optimization to return types that we aren't
3573 allowed to manipulate freely. */
3574 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
3576 /* Prune the candidates that are referenced by other return values. */
3577 data.nrv = nrv;
3578 data.result = NULL_TREE;
3579 data.gnat_ret = Empty;
3580 data.visited = NULL;
3581 FOR_EACH_VEC_SAFE_ELT (other, i, iter)
3582 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3583 if (bitmap_empty_p (nrv))
3584 return;
3586 /* Prune also the candidates that are referenced by nested functions. */
3587 node = cgraph_node::get_create (fndecl);
3588 for (node = node->nested; node; node = node->next_nested)
3589 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), prune_nrv_r,
3590 &data);
3591 if (bitmap_empty_p (nrv))
3592 return;
3594 /* Extract a set of NRVs with non-overlapping live ranges. */
3595 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3596 return;
3598 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3599 data.nrv = nrv;
3600 data.result = DECL_RESULT (fndecl);
3601 data.gnat_ret = gnat_ret;
3602 data.visited = new hash_set<tree>;
3603 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl)))
3604 func = finalize_nrv_unc_r;
3605 else
3606 func = finalize_nrv_r;
3607 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
3608 delete data.visited;
3611 /* Return true if RET_VAL can be used as a Named Return Value for the
3612 anonymous return object RET_OBJ. */
3614 static bool
3615 return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3617 if (TREE_CODE (ret_val) != VAR_DECL)
3618 return false;
3620 if (TREE_THIS_VOLATILE (ret_val))
3621 return false;
3623 if (DECL_CONTEXT (ret_val) != current_function_decl)
3624 return false;
3626 if (TREE_STATIC (ret_val))
3627 return false;
3629 /* For the constrained case, test for addressability. */
3630 if (ret_obj && TREE_ADDRESSABLE (ret_val))
3631 return false;
3633 /* For the constrained case, test for overalignment. */
3634 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
3635 return false;
3637 /* For the unconstrained case, test for bogus initialization. */
3638 if (!ret_obj
3639 && DECL_INITIAL (ret_val)
3640 && TREE_CODE (DECL_INITIAL (ret_val)) == NULL_EXPR)
3641 return false;
3643 return true;
3646 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3647 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3648 around RESULT_OBJ, which may be null in this case. */
3650 static tree
3651 build_return_expr (tree ret_obj, tree ret_val)
3653 tree result_expr;
3655 if (ret_val)
3657 /* The gimplifier explicitly enforces the following invariant:
3659 RETURN_EXPR
3661 INIT_EXPR
3664 RET_OBJ ...
3666 As a consequence, type consistency dictates that we use the type
3667 of the RET_OBJ as the operation type. */
3668 tree operation_type = TREE_TYPE (ret_obj);
3670 /* Convert the right operand to the operation type. Note that this is
3671 the transformation applied in the INIT_EXPR case of build_binary_op,
3672 with the assumption that the type cannot involve a placeholder. */
3673 if (operation_type != TREE_TYPE (ret_val))
3674 ret_val = convert (operation_type, ret_val);
3676 /* We always can use an INIT_EXPR for the return object. */
3677 result_expr = build2 (INIT_EXPR, void_type_node, ret_obj, ret_val);
3679 /* If the function returns an aggregate type, find out whether this is
3680 a candidate for Named Return Value. If so, record it. Otherwise,
3681 if this is an expression of some kind, record it elsewhere. */
3682 if (optimize
3683 && AGGREGATE_TYPE_P (operation_type)
3684 && !TYPE_IS_FAT_POINTER_P (operation_type)
3685 && TYPE_MODE (operation_type) == BLKmode
3686 && aggregate_value_p (operation_type, current_function_decl))
3688 /* Strip useless conversions around the return value. */
3689 if (gnat_useless_type_conversion (ret_val))
3690 ret_val = TREE_OPERAND (ret_val, 0);
3692 /* Now apply the test to the return value. */
3693 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3695 if (!f_named_ret_val)
3696 f_named_ret_val = BITMAP_GGC_ALLOC ();
3697 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3700 /* Note that we need not care about CONSTRUCTORs here, as they are
3701 totally transparent given the read-compose-write semantics of
3702 assignments from CONSTRUCTORs. */
3703 else if (EXPR_P (ret_val))
3704 vec_safe_push (f_other_ret_val, ret_val);
3707 else
3708 result_expr = ret_obj;
3710 return build1 (RETURN_EXPR, void_type_node, result_expr);
3713 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3714 don't return anything. */
3716 static void
3717 Subprogram_Body_to_gnu (Node_Id gnat_node)
3719 /* Defining identifier of a parameter to the subprogram. */
3720 Entity_Id gnat_param;
3721 /* The defining identifier for the subprogram body. Note that if a
3722 specification has appeared before for this body, then the identifier
3723 occurring in that specification will also be a defining identifier and all
3724 the calls to this subprogram will point to that specification. */
3725 Entity_Id gnat_subprog_id
3726 = (Present (Corresponding_Spec (gnat_node))
3727 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
3728 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3729 tree gnu_subprog_decl;
3730 /* Its RESULT_DECL node. */
3731 tree gnu_result_decl;
3732 /* Its FUNCTION_TYPE node. */
3733 tree gnu_subprog_type;
3734 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3735 tree gnu_cico_list;
3736 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3737 tree gnu_return_var_elmt = NULL_TREE;
3738 tree gnu_result;
3739 location_t locus;
3740 struct language_function *gnu_subprog_language;
3741 vec<parm_attr, va_gc> *cache;
3743 /* If this is a generic object or if it has been eliminated,
3744 ignore it. */
3745 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
3746 || Ekind (gnat_subprog_id) == E_Generic_Function
3747 || Is_Eliminated (gnat_subprog_id))
3748 return;
3750 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3751 the already-elaborated tree node. However, if this subprogram had its
3752 elaboration deferred, we will already have made a tree node for it. So
3753 treat it as not being defined in that case. Such a subprogram cannot
3754 have an address clause or a freeze node, so this test is safe, though it
3755 does disable some otherwise-useful error checking. */
3756 gnu_subprog_decl
3757 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
3758 Acts_As_Spec (gnat_node)
3759 && !present_gnu_tree (gnat_subprog_id));
3760 DECL_FUNCTION_IS_DEF (gnu_subprog_decl) = true;
3761 gnu_result_decl = DECL_RESULT (gnu_subprog_decl);
3762 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
3763 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3764 if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node)
3765 gnu_return_var_elmt = gnu_cico_list;
3767 /* If the function returns by invisible reference, make it explicit in the
3768 function body. See gnat_to_gnu_entity, E_Subprogram_Type case. */
3769 if (TREE_ADDRESSABLE (gnu_subprog_type))
3771 TREE_TYPE (gnu_result_decl)
3772 = build_reference_type (TREE_TYPE (gnu_result_decl));
3773 relayout_decl (gnu_result_decl);
3776 /* Set the line number in the decl to correspond to that of the body. */
3777 Sloc_to_locus (Sloc (gnat_node), &locus);
3778 DECL_SOURCE_LOCATION (gnu_subprog_decl) = locus;
3780 /* Initialize the information structure for the function. */
3781 allocate_struct_function (gnu_subprog_decl, false);
3782 gnu_subprog_language = ggc_cleared_alloc<language_function> ();
3783 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language = gnu_subprog_language;
3784 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->function_start_locus = locus;
3785 set_cfun (NULL);
3787 begin_subprog_body (gnu_subprog_decl);
3789 /* If there are copy-in/copy-out parameters, we need to ensure that they are
3790 properly copied out by the return statement. We do this by making a new
3791 block and converting any return into a goto to a label at the end of the
3792 block. */
3793 if (gnu_cico_list)
3795 tree gnu_return_var = NULL_TREE;
3797 vec_safe_push (gnu_return_label_stack,
3798 create_artificial_label (input_location));
3800 start_stmt_group ();
3801 gnat_pushlevel ();
3803 /* If this is a function with copy-in/copy-out parameters and which does
3804 not return by invisible reference, we also need a variable for the
3805 return value to be placed. */
3806 if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type))
3808 tree gnu_return_type
3809 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3811 gnu_return_var
3812 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
3813 gnu_return_type, NULL_TREE,
3814 false, false, false, false, false,
3815 true, false, NULL, gnat_subprog_id);
3816 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3819 vec_safe_push (gnu_return_var_stack, gnu_return_var);
3821 /* See whether there are parameters for which we don't have a GCC tree
3822 yet. These must be Out parameters. Make a VAR_DECL for them and
3823 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3824 We can match up the entries because TYPE_CI_CO_LIST is in the order
3825 of the parameters. */
3826 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3827 Present (gnat_param);
3828 gnat_param = Next_Formal_With_Extras (gnat_param))
3829 if (!present_gnu_tree (gnat_param))
3831 tree gnu_cico_entry = gnu_cico_list;
3832 tree gnu_decl;
3834 /* Skip any entries that have been already filled in; they must
3835 correspond to In Out parameters. */
3836 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3837 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3839 /* Do any needed dereferences for by-ref objects. */
3840 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, true);
3841 gcc_assert (DECL_P (gnu_decl));
3842 if (DECL_BY_REF_P (gnu_decl))
3843 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3845 /* Do any needed references for padded types. */
3846 TREE_VALUE (gnu_cico_entry)
3847 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
3850 else
3851 vec_safe_push (gnu_return_label_stack, NULL_TREE);
3853 /* Get a tree corresponding to the code for the subprogram. */
3854 start_stmt_group ();
3855 gnat_pushlevel ();
3857 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3859 /* Generate the code of the subprogram itself. A return statement will be
3860 present and any Out parameters will be handled there. */
3861 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3862 gnat_poplevel ();
3863 gnu_result = end_stmt_group ();
3865 /* If we populated the parameter attributes cache, we need to make sure that
3866 the cached expressions are evaluated on all the possible paths leading to
3867 their uses. So we force their evaluation on entry of the function. */
3868 cache = gnu_subprog_language->parm_attr_cache;
3869 if (cache)
3871 struct parm_attr_d *pa;
3872 int i;
3874 start_stmt_group ();
3876 FOR_EACH_VEC_ELT (*cache, i, pa)
3878 if (pa->first)
3879 add_stmt_with_node_force (pa->first, gnat_node);
3880 if (pa->last)
3881 add_stmt_with_node_force (pa->last, gnat_node);
3882 if (pa->length)
3883 add_stmt_with_node_force (pa->length, gnat_node);
3886 add_stmt (gnu_result);
3887 gnu_result = end_stmt_group ();
3889 gnu_subprog_language->parm_attr_cache = NULL;
3892 /* If we are dealing with a return from an Ada procedure with parameters
3893 passed by copy-in/copy-out, we need to return a record containing the
3894 final values of these parameters. If the list contains only one entry,
3895 return just that entry though.
3897 For a full description of the copy-in/copy-out parameter mechanism, see
3898 the part of the gnat_to_gnu_entity routine dealing with the translation
3899 of subprograms.
3901 We need to make a block that contains the definition of that label and
3902 the copying of the return value. It first contains the function, then
3903 the label and copy statement. */
3904 if (gnu_cico_list)
3906 const Node_Id gnat_end_label
3907 = End_Label (Handled_Statement_Sequence (gnat_node));
3909 gnu_return_var_stack->pop ();
3911 add_stmt (gnu_result);
3912 add_stmt (build1 (LABEL_EXPR, void_type_node,
3913 gnu_return_label_stack->last ()));
3915 /* If this is a function which returns by invisible reference, the
3916 return value has already been dealt with at the return statements,
3917 so we only need to indirectly copy out the parameters. */
3918 if (TREE_ADDRESSABLE (gnu_subprog_type))
3920 tree gnu_ret_deref
3921 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl);
3922 tree t;
3924 gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node);
3926 for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t))
3928 tree gnu_field_deref
3929 = build_component_ref (gnu_ret_deref, TREE_PURPOSE (t), true);
3930 gnu_result = build2 (MODIFY_EXPR, void_type_node,
3931 gnu_field_deref, TREE_VALUE (t));
3932 add_stmt_with_node (gnu_result, gnat_end_label);
3936 /* Otherwise, if this is a procedure or a function which does not return
3937 by invisible reference, we can do a direct block-copy out. */
3938 else
3940 tree gnu_retval;
3942 if (list_length (gnu_cico_list) == 1)
3943 gnu_retval = TREE_VALUE (gnu_cico_list);
3944 else
3945 gnu_retval
3946 = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
3947 gnu_cico_list);
3949 gnu_result = build_return_expr (gnu_result_decl, gnu_retval);
3950 add_stmt_with_node (gnu_result, gnat_end_label);
3953 gnat_poplevel ();
3954 gnu_result = end_stmt_group ();
3957 gnu_return_label_stack->pop ();
3959 /* Attempt setting the end_locus of our GCC body tree, typically a
3960 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3961 declaration tree. */
3962 set_end_locus_from_node (gnu_result, gnat_node);
3963 set_end_locus_from_node (gnu_subprog_decl, gnat_node);
3965 /* On SEH targets, install an exception handler around the main entry
3966 point to catch unhandled exceptions. */
3967 if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
3968 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
3970 tree t;
3971 tree etype;
3973 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
3974 1, integer_zero_node);
3975 t = build_call_n_expr (unhandled_except_decl, 1, t);
3977 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
3978 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
3980 t = build2 (CATCH_EXPR, void_type_node, etype, t);
3981 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
3982 gnu_result, t);
3985 end_subprog_body (gnu_result);
3987 /* Finally annotate the parameters and disconnect the trees for parameters
3988 that we have turned into variables since they are now unusable. */
3989 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3990 Present (gnat_param);
3991 gnat_param = Next_Formal_With_Extras (gnat_param))
3993 tree gnu_param = get_gnu_tree (gnat_param);
3994 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
3996 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
3997 DECL_BY_REF_P (gnu_param));
3999 if (is_var_decl)
4000 save_gnu_tree (gnat_param, NULL_TREE, false);
4003 /* Disconnect the variable created for the return value. */
4004 if (gnu_return_var_elmt)
4005 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
4007 /* If the function returns an aggregate type and we have candidates for
4008 a Named Return Value, finalize the optimization. */
4009 if (optimize && gnu_subprog_language->named_ret_val)
4011 finalize_nrv (gnu_subprog_decl,
4012 gnu_subprog_language->named_ret_val,
4013 gnu_subprog_language->other_ret_val,
4014 gnu_subprog_language->gnat_ret);
4015 gnu_subprog_language->named_ret_val = NULL;
4016 gnu_subprog_language->other_ret_val = NULL;
4019 /* If this is an inlined external function that has been marked uninlinable,
4020 drop the body and stop there. Otherwise compile the body. */
4021 if (DECL_EXTERNAL (gnu_subprog_decl) && DECL_UNINLINABLE (gnu_subprog_decl))
4022 DECL_SAVED_TREE (gnu_subprog_decl) = NULL_TREE;
4023 else
4024 rest_of_subprog_body_compilation (gnu_subprog_decl);
4027 /* Return true if GNAT_NODE references an Atomic entity. */
4029 static bool
4030 node_is_atomic (Node_Id gnat_node)
4032 Entity_Id gnat_entity;
4034 switch (Nkind (gnat_node))
4036 case N_Identifier:
4037 case N_Expanded_Name:
4038 gnat_entity = Entity (gnat_node);
4039 if (Ekind (gnat_entity) != E_Variable)
4040 break;
4041 return Is_Atomic (gnat_entity) || Is_Atomic (Etype (gnat_entity));
4043 case N_Selected_Component:
4044 gnat_entity = Entity (Selector_Name (gnat_node));
4045 return Is_Atomic (gnat_entity) || Is_Atomic (Etype (gnat_entity));
4047 case N_Indexed_Component:
4048 if (Has_Atomic_Components (Etype (Prefix (gnat_node))))
4049 return true;
4050 if (Is_Entity_Name (Prefix (gnat_node))
4051 && Has_Atomic_Components (Entity (Prefix (gnat_node))))
4052 return true;
4054 /* ... fall through ... */
4056 case N_Explicit_Dereference:
4057 return Is_Atomic (Etype (gnat_node));
4059 default:
4060 break;
4063 return false;
4066 /* Return true if GNAT_NODE references a Volatile_Full_Access entity. */
4068 static bool
4069 node_has_volatile_full_access (Node_Id gnat_node)
4071 Entity_Id gnat_entity;
4073 switch (Nkind (gnat_node))
4075 case N_Identifier:
4076 case N_Expanded_Name:
4077 gnat_entity = Entity (gnat_node);
4078 if (Ekind (gnat_entity) != E_Variable)
4079 break;
4080 return Is_Volatile_Full_Access (gnat_entity)
4081 || Is_Volatile_Full_Access (Etype (gnat_entity));
4083 case N_Selected_Component:
4084 gnat_entity = Entity (Selector_Name (gnat_node));
4085 return Is_Volatile_Full_Access (gnat_entity)
4086 || Is_Volatile_Full_Access (Etype (gnat_entity));
4088 case N_Indexed_Component:
4089 case N_Explicit_Dereference:
4090 return Is_Volatile_Full_Access (Etype (gnat_node));
4092 default:
4093 break;
4096 return false;
4099 /* Strip any type conversion on GNAT_NODE and return the result. */
4101 static Node_Id
4102 gnat_strip_type_conversion (Node_Id gnat_node)
4104 Node_Kind kind = Nkind (gnat_node);
4106 if (kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
4107 gnat_node = Expression (gnat_node);
4109 return gnat_node;
4112 /* Return true if GNAT_NODE requires outer atomic access, i.e. atomic access
4113 of an object of which GNAT_NODE is a component. */
4115 static bool
4116 outer_atomic_access_required_p (Node_Id gnat_node)
4118 gnat_node = gnat_strip_type_conversion (gnat_node);
4120 while (true)
4122 switch (Nkind (gnat_node))
4124 case N_Identifier:
4125 case N_Expanded_Name:
4126 if (No (Renamed_Object (Entity (gnat_node))))
4127 return false;
4128 gnat_node
4129 = gnat_strip_type_conversion (Renamed_Object (Entity (gnat_node)));
4130 break;
4132 case N_Indexed_Component:
4133 case N_Selected_Component:
4134 case N_Slice:
4135 gnat_node = gnat_strip_type_conversion (Prefix (gnat_node));
4136 if (node_has_volatile_full_access (gnat_node))
4137 return true;
4138 break;
4140 default:
4141 return false;
4145 gcc_unreachable ();
4148 /* Return true if GNAT_NODE requires atomic access and set SYNC according to
4149 the associated synchronization setting. */
4151 static bool
4152 atomic_access_required_p (Node_Id gnat_node, bool *sync)
4154 const Node_Id gnat_parent = Parent (gnat_node);
4155 unsigned char attr_id;
4156 bool as_a_whole = true;
4158 /* First, scan the parent to find out cases where the flag is irrelevant. */
4159 switch (Nkind (gnat_parent))
4161 case N_Attribute_Reference:
4162 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
4163 /* Do not mess up machine code insertions. */
4164 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
4165 return false;
4167 /* Nothing to do if we are the prefix of an attribute, since we do not
4168 want an atomic access for things like 'Size. */
4170 /* ... fall through ... */
4172 case N_Reference:
4173 /* The N_Reference node is like an attribute. */
4174 if (Prefix (gnat_parent) == gnat_node)
4175 return false;
4176 break;
4178 case N_Indexed_Component:
4179 case N_Selected_Component:
4180 case N_Slice:
4181 /* If we are the prefix, then the access is only partial. */
4182 if (Prefix (gnat_parent) == gnat_node)
4183 as_a_whole = false;
4184 break;
4186 case N_Object_Renaming_Declaration:
4187 /* Nothing to do for the identifier in an object renaming declaration,
4188 the renaming itself does not need atomic access. */
4189 return false;
4191 default:
4192 break;
4195 /* Then, scan the node to find the atomic object. */
4196 gnat_node = gnat_strip_type_conversion (gnat_node);
4198 /* For Atomic itself, only reads and updates of the object as a whole require
4199 atomic access (RM C.6 (15)). But for Volatile_Full_Access, all reads and
4200 updates require atomic access. */
4201 if (!(as_a_whole && node_is_atomic (gnat_node))
4202 && !node_has_volatile_full_access (gnat_node))
4203 return false;
4205 /* If an outer atomic access will also be required, it cancels this one. */
4206 if (outer_atomic_access_required_p (gnat_node))
4207 return false;
4209 *sync = Atomic_Sync_Required (gnat_node);
4211 return true;
4214 /* Create a temporary variable with PREFIX and TYPE, and return it. */
4216 static tree
4217 create_temporary (const char *prefix, tree type)
4219 tree gnu_temp
4220 = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
4221 type, NULL_TREE,
4222 false, false, false, false, false,
4223 true, false, NULL, Empty);
4224 return gnu_temp;
4227 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
4228 Put the initialization statement into GNU_INIT_STMT and annotate it with
4229 the SLOC of GNAT_NODE. Return the temporary variable. */
4231 static tree
4232 create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
4233 Node_Id gnat_node)
4235 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
4237 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
4238 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
4240 return gnu_temp;
4243 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
4244 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
4245 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
4246 If GNU_TARGET is non-null, this must be a function call on the RHS of a
4247 N_Assignment_Statement and the result is to be placed into that object.
4248 If OUTER_ATOMIC_ACCESS is true, then the assignment to GNU_TARGET must be a
4249 load-modify-store sequence. Otherwise, if ATOMIC_ACCESS is true, then the
4250 assignment to GNU_TARGET must be atomic. If, in addition, ATOMIC_SYNC is
4251 true, then the assignment to GNU_TARGET requires atomic synchronization. */
4253 static tree
4254 Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
4255 bool outer_atomic_access, bool atomic_access, bool atomic_sync)
4257 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
4258 const bool returning_value = (function_call && !gnu_target);
4259 /* The GCC node corresponding to the GNAT subprogram name. This can either
4260 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
4261 or an indirect reference expression (an INDIRECT_REF node) pointing to a
4262 subprogram. */
4263 tree gnu_subprog = gnat_to_gnu (Name (gnat_node));
4264 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
4265 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
4266 /* The return type of the FUNCTION_TYPE. */
4267 tree gnu_result_type = TREE_TYPE (gnu_subprog_type);
4268 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog);
4269 vec<tree, va_gc> *gnu_actual_vec = NULL;
4270 tree gnu_name_list = NULL_TREE;
4271 tree gnu_stmt_list = NULL_TREE;
4272 tree gnu_after_list = NULL_TREE;
4273 tree gnu_retval = NULL_TREE;
4274 tree gnu_call, gnu_result;
4275 bool by_descriptor = false;
4276 bool went_into_elab_proc = false;
4277 bool pushed_binding_level = false;
4278 Entity_Id gnat_formal;
4279 Node_Id gnat_actual;
4280 bool sync;
4282 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
4284 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
4285 all our args first. */
4286 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL && DECL_STUBBED_P (gnu_subprog))
4288 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
4289 gnat_node, N_Raise_Program_Error);
4291 for (gnat_actual = First_Actual (gnat_node);
4292 Present (gnat_actual);
4293 gnat_actual = Next_Actual (gnat_actual))
4294 add_stmt (gnat_to_gnu (gnat_actual));
4296 if (returning_value)
4298 *gnu_result_type_p = gnu_result_type;
4299 return build1 (NULL_EXPR, gnu_result_type, call_expr);
4302 return call_expr;
4305 /* For a call to a nested function, check the inlining status. */
4306 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL
4307 && decl_function_context (gnu_subprog))
4308 check_inlining_for_nested_subprog (gnu_subprog);
4310 /* The only way we can be making a call via an access type is if Name is an
4311 explicit dereference. In that case, get the list of formal args from the
4312 type the access type is pointing to. Otherwise, get the formals from the
4313 entity being called. */
4314 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4316 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4318 /* If the access type doesn't require foreign-compatible representation,
4319 be prepared for descriptors. */
4320 if (targetm.calls.custom_function_descriptors > 0
4321 && Can_Use_Internal_Rep (Etype (Prefix (Name (gnat_node)))))
4322 by_descriptor = true;
4324 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
4325 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
4326 gnat_formal = Empty;
4327 else
4328 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4330 /* The lifetime of the temporaries created for the call ends right after the
4331 return value is copied, so we can give them the scope of the elaboration
4332 routine at top level. */
4333 if (!current_function_decl)
4335 current_function_decl = get_elaboration_procedure ();
4336 went_into_elab_proc = true;
4339 /* First, create the temporary for the return value when:
4341 1. There is no target and the function has copy-in/copy-out parameters,
4342 because we need to preserve the return value before copying back the
4343 parameters.
4345 2. There is no target and the call is made for neither an object nor a
4346 renaming declaration, nor a return statement, nor an allocator, and
4347 the return type has variable size because in this case the gimplifier
4348 cannot create the temporary, or more generally is simply an aggregate
4349 type, because the gimplifier would then create the temporary in the
4350 outermost scope instead of locally.
4352 3. There is a target and it is a slice or an array with fixed size,
4353 and the return type has variable size, because the gimplifier
4354 doesn't handle these cases.
4356 4. There is no target and we have misaligned In Out or Out parameters
4357 passed by reference, because we need to preserve the return value
4358 before copying back the parameters. However, in this case, we'll
4359 defer creating the temporary, see below.
4361 This must be done before we push a binding level around the call, since
4362 we will pop it before copying the return value. */
4363 if (function_call
4364 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
4365 || (!gnu_target
4366 && Nkind (Parent (gnat_node)) != N_Object_Declaration
4367 && Nkind (Parent (gnat_node)) != N_Object_Renaming_Declaration
4368 && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement
4369 && !(Nkind (Parent (gnat_node)) == N_Qualified_Expression
4370 && Nkind (Parent (Parent (gnat_node))) == N_Allocator)
4371 && AGGREGATE_TYPE_P (gnu_result_type)
4372 && !TYPE_IS_FAT_POINTER_P (gnu_result_type))
4373 || (gnu_target
4374 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
4375 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
4376 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
4377 == INTEGER_CST))
4378 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)))
4380 gnu_retval = create_temporary ("R", gnu_result_type);
4381 DECL_RETURN_VALUE_P (gnu_retval) = 1;
4384 /* If we don't need a value or have already created it, push a binding level
4385 around the call. This will narrow the lifetime of the temporaries we may
4386 need to make when translating the parameters as much as possible. */
4387 if (!returning_value || gnu_retval)
4389 start_stmt_group ();
4390 gnat_pushlevel ();
4391 pushed_binding_level = true;
4394 /* Create the list of the actual parameters as GCC expects it, namely a
4395 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
4396 is an expression and the TREE_PURPOSE field is null. But skip Out
4397 parameters not passed by reference and that need not be copied in. */
4398 for (gnat_actual = First_Actual (gnat_node);
4399 Present (gnat_actual);
4400 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4401 gnat_actual = Next_Actual (gnat_actual))
4403 Entity_Id gnat_formal_type = Etype (gnat_formal);
4404 tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type);
4405 tree gnu_formal = present_gnu_tree (gnat_formal)
4406 ? get_gnu_tree (gnat_formal) : NULL_TREE;
4407 const bool is_true_formal_parm
4408 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
4409 const bool is_by_ref_formal_parm
4410 = is_true_formal_parm
4411 && (DECL_BY_REF_P (gnu_formal)
4412 || DECL_BY_COMPONENT_PTR_P (gnu_formal));
4413 /* In the Out or In Out case, we must suppress conversions that yield
4414 an lvalue but can nevertheless cause the creation of a temporary,
4415 because we need the real object in this case, either to pass its
4416 address if it's passed by reference or as target of the back copy
4417 done after the call if it uses the copy-in/copy-out mechanism.
4418 We do it in the In case too, except for an unchecked conversion
4419 to an elementary type or a constrained composite type because it
4420 alone can cause the actual to be misaligned and the addressability
4421 test is applied to the real object. */
4422 const bool suppress_type_conversion
4423 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
4424 && (Ekind (gnat_formal) != E_In_Parameter
4425 || (Is_Composite_Type (Underlying_Type (gnat_formal_type))
4426 && !Is_Constrained (Underlying_Type (gnat_formal_type)))))
4427 || (Nkind (gnat_actual) == N_Type_Conversion
4428 && Is_Composite_Type (Underlying_Type (gnat_formal_type))));
4429 Node_Id gnat_name = suppress_type_conversion
4430 ? Expression (gnat_actual) : gnat_actual;
4431 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
4433 /* If it's possible we may need to use this expression twice, make sure
4434 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4435 to force side-effects before the call. */
4436 if (Ekind (gnat_formal) != E_In_Parameter && !is_by_ref_formal_parm)
4438 tree init = NULL_TREE;
4439 gnu_name = gnat_stabilize_reference (gnu_name, true, &init);
4440 if (init)
4441 gnu_name
4442 = build_compound_expr (TREE_TYPE (gnu_name), init, gnu_name);
4445 /* If we are passing a non-addressable parameter by reference, pass the
4446 address of a copy. In the Out or In Out case, set up to copy back
4447 out after the call. */
4448 if (is_by_ref_formal_parm
4449 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
4450 && !addressable_p (gnu_name, gnu_name_type))
4452 bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
4453 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4455 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4456 but sort of an instantiation for them. */
4457 if (TREE_CODE (gnu_name) == CONSTRUCTOR)
4460 /* If the type is passed by reference, a copy is not allowed. */
4461 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type))
4462 post_error ("misaligned actual cannot be passed by reference",
4463 gnat_actual);
4465 /* For users of Starlet we issue a warning because the interface
4466 apparently assumes that by-ref parameters outlive the procedure
4467 invocation. The code still will not work as intended, but we
4468 cannot do much better since low-level parts of the back-end
4469 would allocate temporaries at will because of the misalignment
4470 if we did not do so here. */
4471 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
4473 post_error
4474 ("?possible violation of implicit assumption", gnat_actual);
4475 post_error_ne
4476 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
4477 Entity (Name (gnat_node)));
4478 post_error_ne ("?because of misalignment of &", gnat_actual,
4479 gnat_formal);
4482 /* If the actual type of the object is already the nominal type,
4483 we have nothing to do, except if the size is self-referential
4484 in which case we'll remove the unpadding below. */
4485 if (TREE_TYPE (gnu_name) == gnu_name_type
4486 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4489 /* Otherwise remove the unpadding from all the objects. */
4490 else if (TREE_CODE (gnu_name) == COMPONENT_REF
4491 && TYPE_IS_PADDING_P
4492 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
4493 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
4495 /* Otherwise convert to the nominal type of the object if needed.
4496 There are several cases in which we need to make the temporary
4497 using this type instead of the actual type of the object when
4498 they are distinct, because the expectations of the callee would
4499 otherwise not be met:
4500 - if it's a justified modular type,
4501 - if the actual type is a smaller form of it,
4502 - if it's a smaller form of the actual type. */
4503 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4504 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4505 || smaller_form_type_p (TREE_TYPE (gnu_name),
4506 gnu_name_type)))
4507 || (INTEGRAL_TYPE_P (gnu_name_type)
4508 && smaller_form_type_p (gnu_name_type,
4509 TREE_TYPE (gnu_name))))
4510 gnu_name = convert (gnu_name_type, gnu_name);
4512 /* If this is an In Out or Out parameter and we're returning a value,
4513 we need to create a temporary for the return value because we must
4514 preserve it before copying back at the very end. */
4515 if (!in_param && returning_value && !gnu_retval)
4517 gnu_retval = create_temporary ("R", gnu_result_type);
4518 DECL_RETURN_VALUE_P (gnu_retval) = 1;
4521 /* If we haven't pushed a binding level, push it now. This will
4522 narrow the lifetime of the temporary we are about to make as
4523 much as possible. */
4524 if (!pushed_binding_level && (!returning_value || gnu_retval))
4526 start_stmt_group ();
4527 gnat_pushlevel ();
4528 pushed_binding_level = true;
4531 /* Create an explicit temporary holding the copy. */
4532 gnu_temp
4533 = create_init_temporary ("A", gnu_name, &gnu_stmt, gnat_actual);
4535 /* But initialize it on the fly like for an implicit temporary as
4536 we aren't necessarily having a statement list. */
4537 gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt,
4538 gnu_temp);
4540 /* Set up to move the copy back to the original if needed. */
4541 if (!in_param)
4543 /* If the original is a COND_EXPR whose first arm isn't meant to
4544 be further used, just deal with the second arm. This is very
4545 likely the conditional expression built for a check. */
4546 if (TREE_CODE (gnu_orig) == COND_EXPR
4547 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4548 && integer_zerop
4549 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4550 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4552 gnu_stmt
4553 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp);
4554 set_expr_location_from_node (gnu_stmt, gnat_node);
4556 append_to_statement_list (gnu_stmt, &gnu_after_list);
4560 /* Start from the real object and build the actual. */
4561 tree gnu_actual = gnu_name;
4563 /* If atomic access is required for an In or In Out actual parameter,
4564 build the atomic load. */
4565 if (is_true_formal_parm
4566 && !is_by_ref_formal_parm
4567 && Ekind (gnat_formal) != E_Out_Parameter
4568 && atomic_access_required_p (gnat_actual, &sync))
4569 gnu_actual = build_atomic_load (gnu_actual, sync);
4571 /* If this was a procedure call, we may not have removed any padding.
4572 So do it here for the part we will use as an input, if any. */
4573 if (Ekind (gnat_formal) != E_Out_Parameter
4574 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4575 gnu_actual
4576 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4578 /* Put back the conversion we suppressed above in the computation of the
4579 real object. And even if we didn't suppress any conversion there, we
4580 may have suppressed a conversion to the Etype of the actual earlier,
4581 since the parent is a procedure call, so put it back here. Note that
4582 we might have a dummy type here if the actual is the dereference of a
4583 pointer to it, but that's OK if the formal is passed by reference. */
4584 tree gnu_actual_type = gnat_to_gnu_type (Etype (gnat_actual));
4585 if (TYPE_IS_DUMMY_P (gnu_actual_type))
4586 gcc_assert (is_true_formal_parm && DECL_BY_REF_P (gnu_formal));
4587 else if (suppress_type_conversion
4588 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4589 gnu_actual = unchecked_convert (gnu_actual_type, gnu_actual,
4590 No_Truncation (gnat_actual));
4591 else
4592 gnu_actual = convert (gnu_actual_type, gnu_actual);
4594 /* Make sure that the actual is in range of the formal's type. */
4595 if (Ekind (gnat_formal) != E_Out_Parameter
4596 && Do_Range_Check (gnat_actual))
4597 gnu_actual
4598 = emit_range_check (gnu_actual, gnat_formal_type, gnat_actual);
4600 /* Unless this is an In parameter, we must remove any justified modular
4601 building from GNU_NAME to get an lvalue. */
4602 if (Ekind (gnat_formal) != E_In_Parameter
4603 && TREE_CODE (gnu_name) == CONSTRUCTOR
4604 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
4605 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
4606 gnu_name
4607 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))), gnu_name);
4609 /* First see if the parameter is passed by reference. */
4610 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
4612 if (Ekind (gnat_formal) != E_In_Parameter)
4614 /* In Out or Out parameters passed by reference don't use the
4615 copy-in/copy-out mechanism so the address of the real object
4616 must be passed to the function. */
4617 gnu_actual = gnu_name;
4619 /* If we have a padded type, be sure we've removed padding. */
4620 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4621 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
4622 gnu_actual);
4624 /* If we have the constructed subtype of an aliased object
4625 with an unconstrained nominal subtype, the type of the
4626 actual includes the template, although it is formally
4627 constrained. So we need to convert it back to the real
4628 constructed subtype to retrieve the constrained part
4629 and takes its address. */
4630 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4631 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
4632 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
4633 && Is_Array_Type (Underlying_Type (Etype (gnat_actual))))
4634 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4635 gnu_actual);
4638 /* There is no need to convert the actual to the formal's type before
4639 taking its address. The only exception is for unconstrained array
4640 types because of the way we build fat pointers. */
4641 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4643 /* Put back a view conversion for In Out or Out parameters. */
4644 if (Ekind (gnat_formal) != E_In_Parameter)
4645 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4646 gnu_actual);
4647 gnu_actual = convert (gnu_formal_type, gnu_actual);
4650 /* The symmetry of the paths to the type of an entity is broken here
4651 since arguments don't know that they will be passed by ref. */
4652 gnu_formal_type = TREE_TYPE (gnu_formal);
4653 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4656 /* Then see if the parameter is an array passed to a foreign convention
4657 subprogram. */
4658 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
4660 gnu_formal_type = TREE_TYPE (gnu_formal);
4661 gnu_actual = maybe_implicit_deref (gnu_actual);
4662 gnu_actual = maybe_unconstrained_array (gnu_actual);
4664 if (TYPE_IS_PADDING_P (gnu_formal_type))
4666 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
4667 gnu_actual = convert (gnu_formal_type, gnu_actual);
4670 /* Take the address of the object and convert to the proper pointer
4671 type. We'd like to actually compute the address of the beginning
4672 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
4673 possibility that the ARRAY_REF might return a constant and we'd be
4674 getting the wrong address. Neither approach is exactly correct,
4675 but this is the most likely to work in all cases. */
4676 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4679 /* Otherwise the parameter is passed by copy. */
4680 else
4682 tree gnu_size;
4684 if (Ekind (gnat_formal) != E_In_Parameter)
4685 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
4687 /* If we didn't create a PARM_DECL for the formal, this means that
4688 it is an Out parameter not passed by reference and that need not
4689 be copied in. In this case, the value of the actual need not be
4690 read. However, we still need to make sure that its side-effects
4691 are evaluated before the call, so we evaluate its address. */
4692 if (!is_true_formal_parm)
4694 if (TREE_SIDE_EFFECTS (gnu_name))
4696 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
4697 append_to_statement_list (addr, &gnu_stmt_list);
4699 continue;
4702 gnu_actual = convert (gnu_formal_type, gnu_actual);
4704 /* If this is 'Null_Parameter, pass a zero even though we are
4705 dereferencing it. */
4706 if (TREE_CODE (gnu_actual) == INDIRECT_REF
4707 && TREE_PRIVATE (gnu_actual)
4708 && (gnu_size = TYPE_SIZE (TREE_TYPE (gnu_actual)))
4709 && TREE_CODE (gnu_size) == INTEGER_CST
4710 && compare_tree_int (gnu_size, BITS_PER_WORD) <= 0)
4712 tree type_for_size
4713 = gnat_type_for_size (TREE_INT_CST_LOW (gnu_size), 1);
4714 gnu_actual
4715 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
4716 build_int_cst (type_for_size, 0),
4717 false);
4719 else
4720 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
4723 vec_safe_push (gnu_actual_vec, gnu_actual);
4726 gnu_call
4727 = build_call_vec (gnu_result_type, gnu_subprog_addr, gnu_actual_vec);
4728 CALL_EXPR_BY_DESCRIPTOR (gnu_call) = by_descriptor;
4729 set_expr_location_from_node (gnu_call, gnat_node);
4731 /* If we have created a temporary for the return value, initialize it. */
4732 if (gnu_retval)
4734 tree gnu_stmt
4735 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
4736 set_expr_location_from_node (gnu_stmt, gnat_node);
4737 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4738 gnu_call = gnu_retval;
4741 /* If this is a subprogram with copy-in/copy-out parameters, we need to
4742 unpack the valued returned from the function into the In Out or Out
4743 parameters. We deal with the function return (if this is an Ada
4744 function) below. */
4745 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4747 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
4748 copy-out parameters. */
4749 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
4750 const int length = list_length (gnu_cico_list);
4752 /* The call sequence must contain one and only one call, even though the
4753 function is pure. Save the result into a temporary if needed. */
4754 if (length > 1)
4756 if (!gnu_retval)
4758 tree gnu_stmt;
4759 gnu_call
4760 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
4761 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4764 gnu_name_list = nreverse (gnu_name_list);
4767 /* The first entry is for the actual return value if this is a
4768 function, so skip it. */
4769 if (function_call)
4770 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4772 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4773 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4774 else
4775 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4777 for (gnat_actual = First_Actual (gnat_node);
4778 Present (gnat_actual);
4779 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4780 gnat_actual = Next_Actual (gnat_actual))
4781 /* If we are dealing with a copy-in/copy-out parameter, we must
4782 retrieve its value from the record returned in the call. */
4783 if (!(present_gnu_tree (gnat_formal)
4784 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4785 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
4786 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))))
4787 && Ekind (gnat_formal) != E_In_Parameter)
4789 /* Get the value to assign to this Out or In Out parameter. It is
4790 either the result of the function if there is only a single such
4791 parameter or the appropriate field from the record returned. */
4792 tree gnu_result
4793 = length == 1
4794 ? gnu_call
4795 : build_component_ref (gnu_call, TREE_PURPOSE (gnu_cico_list),
4796 false);
4798 /* If the actual is a conversion, get the inner expression, which
4799 will be the real destination, and convert the result to the
4800 type of the actual parameter. */
4801 tree gnu_actual
4802 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
4804 /* If the result is a padded type, remove the padding. */
4805 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4806 gnu_result
4807 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4808 gnu_result);
4810 /* If the actual is a type conversion, the real target object is
4811 denoted by the inner Expression and we need to convert the
4812 result to the associated type.
4813 We also need to convert our gnu assignment target to this type
4814 if the corresponding GNU_NAME was constructed from the GNAT
4815 conversion node and not from the inner Expression. */
4816 if (Nkind (gnat_actual) == N_Type_Conversion)
4818 gnu_result
4819 = convert_with_check
4820 (Etype (Expression (gnat_actual)), gnu_result,
4821 Do_Overflow_Check (gnat_actual),
4822 Do_Range_Check (Expression (gnat_actual)),
4823 Float_Truncate (gnat_actual), gnat_actual);
4825 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
4826 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
4829 /* Unchecked conversions as actuals for Out parameters are not
4830 allowed in user code because they are not variables, but do
4831 occur in front-end expansions. The associated GNU_NAME is
4832 always obtained from the inner expression in such cases. */
4833 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4834 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
4835 gnu_result,
4836 No_Truncation (gnat_actual));
4837 else
4839 if (Do_Range_Check (gnat_actual))
4840 gnu_result
4841 = emit_range_check (gnu_result, Etype (gnat_actual),
4842 gnat_actual);
4844 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
4845 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
4846 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
4849 /* If an outer atomic access is required for an actual parameter,
4850 build the load-modify-store sequence. */
4851 if (outer_atomic_access_required_p (gnat_actual))
4852 gnu_result
4853 = build_load_modify_store (gnu_actual, gnu_result, gnat_node);
4855 /* Or else, if simple atomic access is required, build the atomic
4856 store. */
4857 else if (atomic_access_required_p (gnat_actual, &sync))
4858 gnu_result = build_atomic_store (gnu_actual, gnu_result, sync);
4860 /* Otherwise build a regular assignment. */
4861 else
4862 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4863 gnu_actual, gnu_result);
4865 if (EXPR_P (gnu_result))
4866 set_expr_location_from_node (gnu_result, gnat_node);
4867 append_to_statement_list (gnu_result, &gnu_stmt_list);
4868 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4869 gnu_name_list = TREE_CHAIN (gnu_name_list);
4873 /* If this is a function call, the result is the call expression unless a
4874 target is specified, in which case we copy the result into the target
4875 and return the assignment statement. */
4876 if (function_call)
4878 /* If this is a function with copy-in/copy-out parameters, extract the
4879 return value from it and update the return type. */
4880 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4882 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
4883 gnu_call
4884 = build_component_ref (gnu_call, TREE_PURPOSE (gnu_elmt), false);
4885 gnu_result_type = TREE_TYPE (gnu_call);
4888 /* If the function returns an unconstrained array or by direct reference,
4889 we have to dereference the pointer. */
4890 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type)
4891 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
4892 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
4894 if (gnu_target)
4896 Node_Id gnat_parent = Parent (gnat_node);
4897 enum tree_code op_code;
4899 /* If range check is needed, emit code to generate it. */
4900 if (Do_Range_Check (gnat_node))
4901 gnu_call
4902 = emit_range_check (gnu_call, Etype (Name (gnat_parent)),
4903 gnat_parent);
4905 /* ??? If the return type has variable size, then force the return
4906 slot optimization as we would not be able to create a temporary.
4907 That's what has been done historically. */
4908 if (return_type_with_variable_size_p (gnu_result_type))
4909 op_code = INIT_EXPR;
4910 else
4911 op_code = MODIFY_EXPR;
4913 /* Use the required method to move the result to the target. */
4914 if (outer_atomic_access)
4915 gnu_call
4916 = build_load_modify_store (gnu_target, gnu_call, gnat_node);
4917 else if (atomic_access)
4918 gnu_call = build_atomic_store (gnu_target, gnu_call, atomic_sync);
4919 else
4920 gnu_call
4921 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
4923 if (EXPR_P (gnu_call))
4924 set_expr_location_from_node (gnu_call, gnat_parent);
4925 append_to_statement_list (gnu_call, &gnu_stmt_list);
4927 else
4928 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
4931 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4932 parameters, the result is just the call statement. */
4933 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
4934 append_to_statement_list (gnu_call, &gnu_stmt_list);
4936 /* Finally, add the copy back statements, if any. */
4937 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
4939 if (went_into_elab_proc)
4940 current_function_decl = NULL_TREE;
4942 /* If we have pushed a binding level, pop it and finish up the enclosing
4943 statement group. */
4944 if (pushed_binding_level)
4946 add_stmt (gnu_stmt_list);
4947 gnat_poplevel ();
4948 gnu_result = end_stmt_group ();
4951 /* Otherwise, retrieve the statement list, if any. */
4952 else if (gnu_stmt_list)
4953 gnu_result = gnu_stmt_list;
4955 /* Otherwise, just return the call expression. */
4956 else
4957 return gnu_call;
4959 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4960 But first simplify if we have only one statement in the list. */
4961 if (returning_value)
4963 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
4964 if (first == last)
4965 gnu_result = first;
4966 gnu_result
4967 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
4970 return gnu_result;
4973 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4974 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4976 static tree
4977 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
4979 tree gnu_jmpsave_decl = NULL_TREE;
4980 tree gnu_jmpbuf_decl = NULL_TREE;
4981 /* If just annotating, ignore all EH and cleanups. */
4982 bool gcc_eh = (!type_annotate_only
4983 && Present (Exception_Handlers (gnat_node))
4984 && Back_End_Exceptions ());
4985 bool fe_sjlj
4986 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
4987 && Exception_Mechanism == Front_End_SJLJ);
4988 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
4989 bool binding_for_block = (at_end || gcc_eh || fe_sjlj);
4990 tree gnu_inner_block; /* The statement(s) for the block itself. */
4991 tree gnu_result;
4992 tree gnu_expr;
4993 Node_Id gnat_temp;
4995 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
4996 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
4997 add_cleanup, and when we leave the binding, end_stmt_group will create
4998 the TRY_FINALLY_EXPR.
5000 ??? The region level calls down there have been specifically put in place
5001 for a ZCX context and currently the order in which things are emitted
5002 (region/handlers) is different from the SJLJ case. Instead of putting
5003 other calls with different conditions at other places for the SJLJ case,
5004 it seems cleaner to reorder things for the SJLJ case and generalize the
5005 condition to make it not ZCX specific.
5007 If there are any exceptions or cleanup processing involved, we need an
5008 outer statement group (for Fe_Sjlj) and binding level. */
5009 if (binding_for_block)
5011 start_stmt_group ();
5012 gnat_pushlevel ();
5015 /* If using fe_sjlj, make the variables for the setjmp buffer and save
5016 area for address of previous buffer. Do this first since we need to have
5017 the setjmp buf known for any decls in this block. */
5018 if (fe_sjlj)
5020 gnu_jmpsave_decl
5021 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
5022 jmpbuf_ptr_type,
5023 build_call_n_expr (get_jmpbuf_decl, 0),
5024 false, false, false, false, false, true, false,
5025 NULL, gnat_node);
5027 /* The __builtin_setjmp receivers will immediately reinstall it. Now
5028 because of the unstructured form of EH used by fe_sjlj, there
5029 might be forward edges going to __builtin_setjmp receivers on which
5030 it is uninitialized, although they will never be actually taken. */
5031 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
5032 gnu_jmpbuf_decl
5033 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE,
5034 jmpbuf_type,
5035 NULL_TREE,
5036 false, false, false, false, false, true, false,
5037 NULL, gnat_node);
5039 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
5041 /* When we exit this block, restore the saved value. */
5042 add_cleanup (build_call_n_expr (set_jmpbuf_decl, 1, gnu_jmpsave_decl),
5043 Present (End_Label (gnat_node))
5044 ? End_Label (gnat_node) : gnat_node);
5047 /* If we are to call a function when exiting this block, add a cleanup
5048 to the binding level we made above. Note that add_cleanup is FIFO
5049 so we must register this cleanup after the EH cleanup just above. */
5050 if (at_end)
5052 tree proc_decl = gnat_to_gnu (At_End_Proc (gnat_node));
5054 /* When not optimizing, disable inlining of finalizers as this can
5055 create a more complex CFG in the parent function. */
5056 if (!optimize)
5057 DECL_DECLARED_INLINE_P (proc_decl) = 0;
5059 /* If there is no end label attached, we use the location of the At_End
5060 procedure because Expand_Cleanup_Actions might reset the location of
5061 the enclosing construct to that of an inner statement. */
5062 add_cleanup (build_call_n_expr (proc_decl, 0),
5063 Present (End_Label (gnat_node))
5064 ? End_Label (gnat_node) : At_End_Proc (gnat_node));
5067 /* Now build the tree for the declarations and statements inside this block.
5068 If this is SJLJ, set our jmp_buf as the current buffer. */
5069 start_stmt_group ();
5071 if (fe_sjlj)
5073 gnu_expr = build_call_n_expr (set_jmpbuf_decl, 1,
5074 build_unary_op (ADDR_EXPR, NULL_TREE,
5075 gnu_jmpbuf_decl));
5076 set_expr_location_from_node (gnu_expr, gnat_node);
5077 add_stmt (gnu_expr);
5080 if (Present (First_Real_Statement (gnat_node)))
5081 process_decls (Statements (gnat_node), Empty,
5082 First_Real_Statement (gnat_node), true, true);
5084 /* Generate code for each statement in the block. */
5085 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
5086 ? First_Real_Statement (gnat_node)
5087 : First (Statements (gnat_node)));
5088 Present (gnat_temp); gnat_temp = Next (gnat_temp))
5089 add_stmt (gnat_to_gnu (gnat_temp));
5090 gnu_inner_block = end_stmt_group ();
5092 /* Now generate code for the two exception models, if either is relevant for
5093 this block. */
5094 if (fe_sjlj)
5096 tree *gnu_else_ptr = 0;
5097 tree gnu_handler;
5099 /* Make a binding level for the exception handling declarations and code
5100 and set up gnu_except_ptr_stack for the handlers to use. */
5101 start_stmt_group ();
5102 gnat_pushlevel ();
5104 vec_safe_push (gnu_except_ptr_stack,
5105 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE,
5106 build_pointer_type (except_type_node),
5107 build_call_n_expr (get_excptr_decl, 0),
5108 false, false, false, false, false,
5109 true, false, NULL, gnat_node));
5111 /* Generate code for each handler. The N_Exception_Handler case does the
5112 real work and returns a COND_EXPR for each handler, which we chain
5113 together here. */
5114 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
5115 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
5117 gnu_expr = gnat_to_gnu (gnat_temp);
5119 /* If this is the first one, set it as the outer one. Otherwise,
5120 point the "else" part of the previous handler to us. Then point
5121 to our "else" part. */
5122 if (!gnu_else_ptr)
5123 add_stmt (gnu_expr);
5124 else
5125 *gnu_else_ptr = gnu_expr;
5127 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
5130 /* If none of the exception handlers did anything, re-raise but do not
5131 defer abortion. */
5132 gnu_expr = build_call_n_expr (raise_nodefer_decl, 1,
5133 gnu_except_ptr_stack->last ());
5134 set_expr_location_from_node
5135 (gnu_expr,
5136 Present (End_Label (gnat_node)) ? End_Label (gnat_node) : gnat_node);
5138 if (gnu_else_ptr)
5139 *gnu_else_ptr = gnu_expr;
5140 else
5141 add_stmt (gnu_expr);
5143 /* End the binding level dedicated to the exception handlers and get the
5144 whole statement group. */
5145 gnu_except_ptr_stack->pop ();
5146 gnat_poplevel ();
5147 gnu_handler = end_stmt_group ();
5149 /* If the setjmp returns 1, we restore our incoming longjmp value and
5150 then check the handlers. */
5151 start_stmt_group ();
5152 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl, 1,
5153 gnu_jmpsave_decl),
5154 gnat_node);
5155 add_stmt (gnu_handler);
5156 gnu_handler = end_stmt_group ();
5158 /* This block is now "if (setjmp) ... <handlers> else <block>". */
5159 gnu_result = build3 (COND_EXPR, void_type_node,
5160 (build_call_n_expr
5161 (setjmp_decl, 1,
5162 build_unary_op (ADDR_EXPR, NULL_TREE,
5163 gnu_jmpbuf_decl))),
5164 gnu_handler, gnu_inner_block);
5166 else if (gcc_eh)
5168 tree gnu_handlers;
5169 location_t locus;
5171 /* First make a block containing the handlers. */
5172 start_stmt_group ();
5173 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
5174 Present (gnat_temp);
5175 gnat_temp = Next_Non_Pragma (gnat_temp))
5176 add_stmt (gnat_to_gnu (gnat_temp));
5177 gnu_handlers = end_stmt_group ();
5179 /* Now make the TRY_CATCH_EXPR for the block. */
5180 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
5181 gnu_inner_block, gnu_handlers);
5182 /* Set a location. We need to find a unique location for the dispatching
5183 code, otherwise we can get coverage or debugging issues. Try with
5184 the location of the end label. */
5185 if (Present (End_Label (gnat_node))
5186 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
5187 SET_EXPR_LOCATION (gnu_result, locus);
5188 else
5189 /* Clear column information so that the exception handler of an
5190 implicit transient block does not incorrectly inherit the slocs
5191 of a decision, which would otherwise confuse control flow based
5192 coverage analysis tools. */
5193 set_expr_location_from_node (gnu_result, gnat_node, true);
5195 else
5196 gnu_result = gnu_inner_block;
5198 /* Now close our outer block, if we had to make one. */
5199 if (binding_for_block)
5201 add_stmt (gnu_result);
5202 gnat_poplevel ();
5203 gnu_result = end_stmt_group ();
5206 return gnu_result;
5209 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
5210 to a GCC tree, which is returned. This is the variant for front-end sjlj
5211 exception handling. */
5213 static tree
5214 Exception_Handler_to_gnu_fe_sjlj (Node_Id gnat_node)
5216 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
5217 an "if" statement to select the proper exceptions. For "Others", exclude
5218 exceptions where Handled_By_Others is nonzero unless the All_Others flag
5219 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
5220 tree gnu_choice = boolean_false_node;
5221 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
5222 Node_Id gnat_temp;
5224 for (gnat_temp = First (Exception_Choices (gnat_node));
5225 gnat_temp; gnat_temp = Next (gnat_temp))
5227 tree this_choice;
5229 if (Nkind (gnat_temp) == N_Others_Choice)
5231 if (All_Others (gnat_temp))
5232 this_choice = boolean_true_node;
5233 else
5234 this_choice
5235 = build_binary_op
5236 (EQ_EXPR, boolean_type_node,
5237 convert
5238 (integer_type_node,
5239 build_component_ref
5240 (build_unary_op
5241 (INDIRECT_REF, NULL_TREE,
5242 gnu_except_ptr_stack->last ()),
5243 not_handled_by_others_decl,
5244 false)),
5245 integer_zero_node);
5248 else if (Nkind (gnat_temp) == N_Identifier
5249 || Nkind (gnat_temp) == N_Expanded_Name)
5251 Entity_Id gnat_ex_id = Entity (gnat_temp);
5252 tree gnu_expr;
5254 /* Exception may be a renaming. Recover original exception which is
5255 the one elaborated and registered. */
5256 if (Present (Renamed_Object (gnat_ex_id)))
5257 gnat_ex_id = Renamed_Object (gnat_ex_id);
5259 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, false);
5261 this_choice
5262 = build_binary_op
5263 (EQ_EXPR, boolean_type_node,
5264 gnu_except_ptr_stack->last (),
5265 convert (TREE_TYPE (gnu_except_ptr_stack->last ()),
5266 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
5268 else
5269 gcc_unreachable ();
5271 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
5272 gnu_choice, this_choice);
5275 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
5278 /* Return true if no statement in GNAT_LIST can alter the control flow. */
5280 static bool
5281 stmt_list_cannot_alter_control_flow_p (List_Id gnat_list)
5283 if (No (gnat_list))
5284 return true;
5286 /* This is very conservative, we reject everything except for simple
5287 assignments between identifiers or literals. */
5288 for (Node_Id gnat_node = First (gnat_list);
5289 Present (gnat_node);
5290 gnat_node = Next (gnat_node))
5292 if (Nkind (gnat_node) != N_Assignment_Statement)
5293 return false;
5295 if (Nkind (Name (gnat_node)) != N_Identifier)
5296 return false;
5298 Node_Kind nkind = Nkind (Expression (gnat_node));
5299 if (nkind != N_Identifier
5300 && nkind != N_Integer_Literal
5301 && nkind != N_Real_Literal)
5302 return false;
5305 return true;
5308 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
5309 to a GCC tree, which is returned. This is the variant for GCC exception
5310 schemes. */
5312 static tree
5313 Exception_Handler_to_gnu_gcc (Node_Id gnat_node)
5315 tree gnu_etypes_list = NULL_TREE;
5317 /* We build a TREE_LIST of nodes representing what exception types this
5318 handler can catch, with special cases for others and all others cases.
5320 Each exception type is actually identified by a pointer to the exception
5321 id, or to a dummy object for "others" and "all others". */
5322 for (Node_Id gnat_temp = First (Exception_Choices (gnat_node));
5323 gnat_temp;
5324 gnat_temp = Next (gnat_temp))
5326 tree gnu_expr, gnu_etype;
5328 if (Nkind (gnat_temp) == N_Others_Choice)
5330 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
5331 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
5333 else if (Nkind (gnat_temp) == N_Identifier
5334 || Nkind (gnat_temp) == N_Expanded_Name)
5336 Entity_Id gnat_ex_id = Entity (gnat_temp);
5338 /* Exception may be a renaming. Recover original exception which is
5339 the one elaborated and registered. */
5340 if (Present (Renamed_Object (gnat_ex_id)))
5341 gnat_ex_id = Renamed_Object (gnat_ex_id);
5343 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, false);
5344 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
5346 else
5347 gcc_unreachable ();
5349 /* The GCC interface expects NULL to be passed for catch all handlers, so
5350 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
5351 is integer_zero_node. It would not work, however, because GCC's
5352 notion of "catch all" is stronger than our notion of "others". Until
5353 we correctly use the cleanup interface as well, doing that would
5354 prevent the "all others" handlers from being seen, because nothing
5355 can be caught beyond a catch all from GCC's point of view. */
5356 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
5359 start_stmt_group ();
5360 gnat_pushlevel ();
5362 /* Expand a call to the begin_handler hook at the beginning of the handler,
5363 and arrange for a call to the end_handler hook to occur on every possible
5364 exit path.
5366 The hooks expect a pointer to the low level occurrence. This is required
5367 for our stack management scheme because a raise inside the handler pushes
5368 a new occurrence on top of the stack, which means that this top does not
5369 necessarily match the occurrence this handler was dealing with.
5371 __builtin_eh_pointer references the exception occurrence being
5372 propagated. Upon handler entry, this is the exception for which the
5373 handler is triggered. This might not be the case upon handler exit,
5374 however, as we might have a new occurrence propagated by the handler's
5375 body, and the end_handler hook called as a cleanup in this context.
5377 We use a local variable to retrieve the incoming value at handler entry
5378 time, and reuse it to feed the end_handler hook's argument at exit. */
5380 tree gnu_current_exc_ptr
5381 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
5382 1, integer_zero_node);
5383 tree prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
5384 gnu_incoming_exc_ptr
5385 = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
5386 ptr_type_node, gnu_current_exc_ptr,
5387 false, false, false, false, false, true, true,
5388 NULL, gnat_node);
5390 add_stmt_with_node (build_call_n_expr (begin_handler_decl, 1,
5391 gnu_incoming_exc_ptr),
5392 gnat_node);
5394 /* Declare and initialize the choice parameter, if present. */
5395 if (Present (Choice_Parameter (gnat_node)))
5397 tree gnu_param
5398 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, true);
5400 add_stmt (build_call_n_expr
5401 (set_exception_parameter_decl, 2,
5402 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
5403 gnu_incoming_exc_ptr));
5406 add_stmt_list (Statements (gnat_node));
5408 /* We don't have an End_Label at hand to set the location of the cleanup
5409 actions, so we use that of the exception handler itself instead. */
5410 tree stmt = build_call_n_expr (end_handler_decl, 1, gnu_incoming_exc_ptr);
5411 if (stmt_list_cannot_alter_control_flow_p (Statements (gnat_node)))
5412 add_stmt_with_node (stmt, gnat_node);
5413 else
5414 add_cleanup (stmt, gnat_node);
5416 gnat_poplevel ();
5418 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
5420 return
5421 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
5424 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
5426 static void
5427 Compilation_Unit_to_gnu (Node_Id gnat_node)
5429 const Node_Id gnat_unit = Unit (gnat_node);
5430 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
5431 || Nkind (gnat_unit) == N_Subprogram_Body);
5432 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
5433 Entity_Id gnat_entity;
5434 Node_Id gnat_pragma;
5435 /* Make the decl for the elaboration procedure. Emit debug info for it, so
5436 that users can break into their elaboration code in debuggers. Kludge:
5437 don't consider it as a definition so that we have a line map for its body,
5438 but no subprogram description in debug info. */
5439 tree gnu_elab_proc_decl
5440 = create_subprog_decl
5441 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
5442 NULL_TREE, void_ftype, NULL_TREE,
5443 is_disabled, true, false, true, true, false, NULL, gnat_unit);
5444 struct elab_info *info;
5446 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
5447 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
5449 /* Initialize the information structure for the function. */
5450 allocate_struct_function (gnu_elab_proc_decl, false);
5451 set_cfun (NULL);
5453 current_function_decl = NULL_TREE;
5455 start_stmt_group ();
5456 gnat_pushlevel ();
5458 /* For a body, first process the spec if there is one. */
5459 if (Nkind (gnat_unit) == N_Package_Body
5460 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
5461 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
5463 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
5465 elaborate_all_entities (gnat_node);
5467 if (Nkind (gnat_unit) == N_Subprogram_Declaration
5468 || Nkind (gnat_unit) == N_Generic_Package_Declaration
5469 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
5470 return;
5473 /* Then process any pragmas and declarations preceding the unit. */
5474 for (gnat_pragma = First (Context_Items (gnat_node));
5475 Present (gnat_pragma);
5476 gnat_pragma = Next (gnat_pragma))
5477 if (Nkind (gnat_pragma) == N_Pragma)
5478 add_stmt (gnat_to_gnu (gnat_pragma));
5479 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
5480 true, true);
5482 /* Process the unit itself. */
5483 add_stmt (gnat_to_gnu (gnat_unit));
5485 /* Generate code for all the inlined subprograms. */
5486 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5487 Present (gnat_entity);
5488 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
5490 Node_Id gnat_body;
5492 /* Without optimization, process only the required subprograms. */
5493 if (!optimize && !Has_Pragma_Inline_Always (gnat_entity))
5494 continue;
5496 /* The set of inlined subprograms is computed from data recorded early
5497 during expansion and it can be a strict superset of the final set
5498 computed after semantic analysis, for example if a call to such a
5499 subprogram occurs in a pragma Assert and assertions are disabled.
5500 In that case, semantic analysis resets Is_Public to false but the
5501 entry for the subprogram in the inlining tables is stalled. */
5502 if (!Is_Public (gnat_entity))
5503 continue;
5505 gnat_body = Parent (Declaration_Node (gnat_entity));
5506 if (Nkind (gnat_body) != N_Subprogram_Body)
5508 /* ??? This happens when only the spec of a package is provided. */
5509 if (No (Corresponding_Body (gnat_body)))
5510 continue;
5512 gnat_body
5513 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
5516 /* Define the entity first so we set DECL_EXTERNAL. */
5517 gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
5518 add_stmt (gnat_to_gnu (gnat_body));
5521 /* Process any pragmas and actions following the unit. */
5522 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5523 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
5524 finalize_from_limited_with ();
5526 /* Save away what we've made so far and finish it up. */
5527 set_current_block_context (gnu_elab_proc_decl);
5528 gnat_poplevel ();
5529 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
5530 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
5531 gnu_elab_proc_stack->pop ();
5533 /* Record this potential elaboration procedure for later processing. */
5534 info = ggc_alloc<elab_info> ();
5535 info->next = elab_info_list;
5536 info->elab_proc = gnu_elab_proc_decl;
5537 info->gnat_node = gnat_node;
5538 elab_info_list = info;
5540 /* Force the processing for all nodes that remain in the queue. */
5541 process_deferred_decl_context (true);
5544 /* Mark COND, a boolean expression, as predicating a call to a noreturn
5545 function, i.e. predict that it is very likely false, and return it.
5547 The compiler will automatically predict the last edge leading to a call
5548 to a noreturn function as very unlikely taken. This function makes it
5549 possible to expand the prediction to predecessors in case the condition
5550 is made up of several short-circuit operators. */
5552 static tree
5553 build_noreturn_cond (tree cond)
5555 tree fn = builtin_decl_explicit (BUILT_IN_EXPECT);
5556 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
5557 tree pred_type = TREE_VALUE (arg_types);
5558 tree expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
5560 tree t = build_call_expr (fn, 3,
5561 fold_convert (pred_type, cond),
5562 build_int_cst (expected_type, 0),
5563 build_int_cst (integer_type_node,
5564 PRED_NORETURN));
5566 return build1 (NOP_EXPR, boolean_type_node, t);
5569 /* Subroutine of gnat_to_gnu to translate GNAT_RANGE, a node representing a
5570 range of values, into GNU_LOW and GNU_HIGH bounds. */
5572 static void
5573 Range_to_gnu (Node_Id gnat_range, tree *gnu_low, tree *gnu_high)
5575 /* GNAT_RANGE is either an N_Range or an identifier denoting a subtype. */
5576 switch (Nkind (gnat_range))
5578 case N_Range:
5579 *gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
5580 *gnu_high = gnat_to_gnu (High_Bound (gnat_range));
5581 break;
5583 case N_Expanded_Name:
5584 case N_Identifier:
5586 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
5587 tree gnu_range_base_type = get_base_type (gnu_range_type);
5589 *gnu_low
5590 = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type));
5591 *gnu_high
5592 = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type));
5594 break;
5596 default:
5597 gcc_unreachable ();
5601 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Raise_xxx_Error,
5602 to a GCC tree and return it. GNU_RESULT_TYPE_P is a pointer to where
5603 we should place the result type. */
5605 static tree
5606 Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5608 const Node_Kind kind = Nkind (gnat_node);
5609 const int reason = UI_To_Int (Reason (gnat_node));
5610 const Node_Id gnat_cond = Condition (gnat_node);
5611 const bool with_extra_info
5612 = Exception_Extra_Info
5613 && !No_Exception_Handlers_Set ()
5614 && !get_exception_label (kind);
5615 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
5617 /* The following processing is not required for correctness. Its purpose is
5618 to give more precise error messages and to record some information. */
5619 switch (reason)
5621 case CE_Access_Check_Failed:
5622 if (with_extra_info)
5623 gnu_result = build_call_raise_column (reason, gnat_node, kind);
5624 break;
5626 case CE_Index_Check_Failed:
5627 case CE_Range_Check_Failed:
5628 case CE_Invalid_Data:
5629 if (Present (gnat_cond) && Nkind (gnat_cond) == N_Op_Not)
5631 Node_Id gnat_index, gnat_type;
5632 tree gnu_type, gnu_index, gnu_low_bound, gnu_high_bound, disp;
5633 bool neg_p;
5634 struct loop_info_d *loop;
5636 switch (Nkind (Right_Opnd (gnat_cond)))
5638 case N_In:
5639 Range_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)),
5640 &gnu_low_bound, &gnu_high_bound);
5641 break;
5643 case N_Op_Ge:
5644 gnu_low_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5645 gnu_high_bound = NULL_TREE;
5646 break;
5648 case N_Op_Le:
5649 gnu_low_bound = NULL_TREE;
5650 gnu_high_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5651 break;
5653 default:
5654 goto common;
5657 gnat_index = Left_Opnd (Right_Opnd (gnat_cond));
5658 gnat_type = Etype (gnat_index);
5659 gnu_type = maybe_character_type (get_unpadded_type (gnat_type));
5660 gnu_index = gnat_to_gnu (gnat_index);
5662 if (TREE_TYPE (gnu_index) != gnu_type)
5664 if (gnu_low_bound)
5665 gnu_low_bound = convert (gnu_type, gnu_low_bound);
5666 if (gnu_high_bound)
5667 gnu_high_bound = convert (gnu_type, gnu_high_bound);
5668 gnu_index = convert (gnu_type, gnu_index);
5671 if (with_extra_info
5672 && gnu_low_bound
5673 && gnu_high_bound
5674 && Known_Esize (gnat_type)
5675 && UI_To_Int (Esize (gnat_type)) <= 32)
5676 gnu_result
5677 = build_call_raise_range (reason, gnat_node, kind, gnu_index,
5678 gnu_low_bound, gnu_high_bound);
5680 /* If optimization is enabled and we are inside a loop, we try to
5681 compute invariant conditions for checks applied to the iteration
5682 variable, i.e. conditions that are independent of the variable
5683 and necessary in order for the checks to fail in the course of
5684 some iteration. If we succeed, we consider an alternative:
5686 1. If loop unswitching is enabled, we prepend these conditions
5687 to the original conditions of the checks. This will make it
5688 possible for the loop unswitching pass to replace the loop
5689 with two loops, one of which has the checks eliminated and
5690 the other has the original checks reinstated, and a prologue
5691 implementing a run-time selection. The former loop will be
5692 for example suitable for vectorization.
5694 2. Otherwise, we instead append the conditions to the original
5695 conditions of the checks. At worse, if the conditions cannot
5696 be evaluated at compile time, they will be evaluated as true
5697 at run time only when the checks have already failed, thus
5698 contributing negatively only to the size of the executable.
5699 But the hope is that these invariant conditions be evaluated
5700 at compile time to false, thus taking away the entire checks
5701 with them. */
5702 if (optimize
5703 && inside_loop_p ()
5704 && (!gnu_low_bound
5705 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
5706 && (!gnu_high_bound
5707 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
5708 && (loop = find_loop_for (gnu_index, &disp, &neg_p)))
5710 struct range_check_info_d *rci = ggc_alloc<range_check_info_d> ();
5711 rci->low_bound = gnu_low_bound;
5712 rci->high_bound = gnu_high_bound;
5713 rci->disp = disp;
5714 rci->neg_p = neg_p;
5715 rci->type = gnu_type;
5716 rci->inserted_cond
5717 = build1 (SAVE_EXPR, boolean_type_node, boolean_true_node);
5718 vec_safe_push (loop->checks, rci);
5719 loop->has_checks = true;
5720 gnu_cond = build_noreturn_cond (gnat_to_gnu (gnat_cond));
5721 if (flag_unswitch_loops)
5722 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5723 boolean_type_node,
5724 rci->inserted_cond,
5725 gnu_cond);
5726 else
5727 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5728 boolean_type_node,
5729 gnu_cond,
5730 rci->inserted_cond);
5733 /* Or else, if aggressive loop optimizations are enabled, we just
5734 record that there are checks applied to iteration variables. */
5735 else if (optimize
5736 && flag_aggressive_loop_optimizations
5737 && inside_loop_p ()
5738 && (loop = find_loop_for (gnu_index)))
5739 loop->has_checks = true;
5741 break;
5743 default:
5744 break;
5747 /* The following processing does the common work. */
5748 common:
5749 if (!gnu_result)
5750 gnu_result = build_call_raise (reason, gnat_node, kind);
5751 set_expr_location_from_node (gnu_result, gnat_node);
5753 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5755 /* If the type is VOID, this is a statement, so we need to generate the code
5756 for the call. Handle a condition, if there is one. */
5757 if (VOID_TYPE_P (*gnu_result_type_p))
5759 if (Present (gnat_cond))
5761 if (!gnu_cond)
5762 gnu_cond = gnat_to_gnu (gnat_cond);
5763 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
5764 alloc_stmt_list ());
5767 else
5768 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
5770 return gnu_result;
5773 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
5774 parameter of a call. */
5776 static bool
5777 lhs_or_actual_p (Node_Id gnat_node)
5779 Node_Id gnat_parent = Parent (gnat_node);
5780 Node_Kind kind = Nkind (gnat_parent);
5782 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
5783 return true;
5785 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
5786 && Name (gnat_parent) != gnat_node)
5787 return true;
5789 if (kind == N_Parameter_Association)
5790 return true;
5792 return false;
5795 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
5796 of an assignment or an actual parameter of a call. */
5798 static bool
5799 present_in_lhs_or_actual_p (Node_Id gnat_node)
5801 Node_Kind kind;
5803 if (lhs_or_actual_p (gnat_node))
5804 return true;
5806 kind = Nkind (Parent (gnat_node));
5808 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
5809 && lhs_or_actual_p (Parent (gnat_node)))
5810 return true;
5812 return false;
5815 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
5816 as gigi is concerned. This is used to avoid conversions on the LHS. */
5818 static bool
5819 unchecked_conversion_nop (Node_Id gnat_node)
5821 Entity_Id from_type, to_type;
5823 /* The conversion must be on the LHS of an assignment or an actual parameter
5824 of a call. Otherwise, even if the conversion was essentially a no-op, it
5825 could de facto ensure type consistency and this should be preserved. */
5826 if (!lhs_or_actual_p (gnat_node))
5827 return false;
5829 from_type = Etype (Expression (gnat_node));
5831 /* We're interested in artificial conversions generated by the front-end
5832 to make private types explicit, e.g. in Expand_Assign_Array. */
5833 if (!Is_Private_Type (from_type))
5834 return false;
5836 from_type = Underlying_Type (from_type);
5837 to_type = Etype (gnat_node);
5839 /* The direct conversion to the underlying type is a no-op. */
5840 if (to_type == from_type)
5841 return true;
5843 /* For an array subtype, the conversion to the PAIT is a no-op. */
5844 if (Ekind (from_type) == E_Array_Subtype
5845 && to_type == Packed_Array_Impl_Type (from_type))
5846 return true;
5848 /* For a record subtype, the conversion to the type is a no-op. */
5849 if (Ekind (from_type) == E_Record_Subtype
5850 && to_type == Etype (from_type))
5851 return true;
5853 return false;
5856 /* Return true if GNAT_NODE represents a statement. */
5858 static bool
5859 statement_node_p (Node_Id gnat_node)
5861 const Node_Kind kind = Nkind (gnat_node);
5863 if (kind == N_Label)
5864 return true;
5866 if (IN (kind, N_Statement_Other_Than_Procedure_Call))
5867 return true;
5869 if (kind == N_Procedure_Call_Statement)
5870 return true;
5872 if (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void)
5873 return true;
5875 return false;
5878 /* This function is the driver of the GNAT to GCC tree transformation process.
5879 It is the entry point of the tree transformer. GNAT_NODE is the root of
5880 some GNAT tree. Return the root of the corresponding GCC tree. If this
5881 is an expression, return the GCC equivalent of the expression. If this
5882 is a statement, return the statement or add it to the current statement
5883 group, in which case anything returned is to be interpreted as occurring
5884 after anything added. */
5886 tree
5887 gnat_to_gnu (Node_Id gnat_node)
5889 const Node_Kind kind = Nkind (gnat_node);
5890 bool went_into_elab_proc = false;
5891 tree gnu_result = error_mark_node; /* Default to no value. */
5892 tree gnu_result_type = void_type_node;
5893 tree gnu_expr, gnu_lhs, gnu_rhs;
5894 Node_Id gnat_temp;
5895 bool sync = false;
5897 /* Save node number for error message and set location information. */
5898 error_gnat_node = gnat_node;
5899 Sloc_to_locus (Sloc (gnat_node), &input_location);
5901 /* If we are only annotating types and this node is a statement, return
5902 an empty statement list. */
5903 if (type_annotate_only && statement_node_p (gnat_node))
5904 return alloc_stmt_list ();
5906 /* If we are only annotating types and this node is a subexpression, return
5907 a NULL_EXPR, but filter out nodes appearing in the expressions attached
5908 to packed array implementation types. */
5909 if (type_annotate_only
5910 && IN (kind, N_Subexpr)
5911 && !(((IN (kind, N_Op) && kind != N_Op_Expon)
5912 || kind == N_Type_Conversion)
5913 && Is_Integer_Type (Etype (gnat_node)))
5914 && !(kind == N_Attribute_Reference
5915 && Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Length
5916 && Ekind (Etype (Prefix (gnat_node))) == E_Array_Subtype
5917 && !Is_Constr_Subt_For_U_Nominal (Etype (Prefix (gnat_node))))
5918 && kind != N_Expanded_Name
5919 && kind != N_Identifier
5920 && !Compile_Time_Known_Value (gnat_node))
5921 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
5922 build_call_raise (CE_Range_Check_Failed, gnat_node,
5923 N_Raise_Constraint_Error));
5925 if ((statement_node_p (gnat_node) && kind != N_Null_Statement)
5926 || kind == N_Handled_Sequence_Of_Statements
5927 || kind == N_Implicit_Label_Declaration)
5929 tree current_elab_proc = get_elaboration_procedure ();
5931 /* If this is a statement and we are at top level, it must be part of
5932 the elaboration procedure, so mark us as being in that procedure. */
5933 if (!current_function_decl)
5935 current_function_decl = current_elab_proc;
5936 went_into_elab_proc = true;
5939 /* If we are in the elaboration procedure, check if we are violating a
5940 No_Elaboration_Code restriction by having a statement there. Don't
5941 check for a possible No_Elaboration_Code restriction violation on
5942 N_Handled_Sequence_Of_Statements, as we want to signal an error on
5943 every nested real statement instead. This also avoids triggering
5944 spurious errors on dummy (empty) sequences created by the front-end
5945 for package bodies in some cases. */
5946 if (current_function_decl == current_elab_proc
5947 && kind != N_Handled_Sequence_Of_Statements
5948 && kind != N_Implicit_Label_Declaration)
5949 Check_Elaboration_Code_Allowed (gnat_node);
5952 switch (kind)
5954 /********************************/
5955 /* Chapter 2: Lexical Elements */
5956 /********************************/
5958 case N_Identifier:
5959 case N_Expanded_Name:
5960 case N_Operator_Symbol:
5961 case N_Defining_Identifier:
5962 case N_Defining_Operator_Symbol:
5963 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
5965 /* If atomic access is required on the RHS, build the atomic load. */
5966 if (atomic_access_required_p (gnat_node, &sync)
5967 && !present_in_lhs_or_actual_p (gnat_node))
5968 gnu_result = build_atomic_load (gnu_result, sync);
5969 break;
5971 case N_Integer_Literal:
5973 tree gnu_type;
5975 /* Get the type of the result, looking inside any padding and
5976 justified modular types. Then get the value in that type. */
5977 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
5979 if (TREE_CODE (gnu_type) == RECORD_TYPE
5980 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
5981 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
5983 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
5985 /* If the result overflows (meaning it doesn't fit in its base type),
5986 abort. We would like to check that the value is within the range
5987 of the subtype, but that causes problems with subtypes whose usage
5988 will raise Constraint_Error and with biased representation, so
5989 we don't. */
5990 gcc_assert (!TREE_OVERFLOW (gnu_result));
5992 break;
5994 case N_Character_Literal:
5995 /* If a Entity is present, it means that this was one of the
5996 literals in a user-defined character type. In that case,
5997 just return the value in the CONST_DECL. Otherwise, use the
5998 character code. In that case, the base type should be an
5999 INTEGER_TYPE, but we won't bother checking for that. */
6000 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6001 if (Present (Entity (gnat_node)))
6002 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
6003 else
6004 gnu_result
6005 = build_int_cst (gnu_result_type,
6006 UI_To_CC (Char_Literal_Value (gnat_node)));
6007 break;
6009 case N_Real_Literal:
6010 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6012 /* If this is of a fixed-point type, the value we want is the value of
6013 the corresponding integer. */
6014 if (Is_Fixed_Point_Type (Underlying_Type (Etype (gnat_node))))
6016 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
6017 gnu_result_type);
6018 gcc_assert (!TREE_OVERFLOW (gnu_result));
6021 else
6023 Ureal ur_realval = Realval (gnat_node);
6025 /* First convert the value to a machine number if it isn't already.
6026 That will force the base to 2 for non-zero values and simplify
6027 the rest of the logic. */
6028 if (!Is_Machine_Number (gnat_node))
6029 ur_realval
6030 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
6031 ur_realval, Round_Even, gnat_node);
6033 if (UR_Is_Zero (ur_realval))
6034 gnu_result = build_real (gnu_result_type, dconst0);
6035 else
6037 REAL_VALUE_TYPE tmp;
6039 gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
6041 /* The base must be 2 as Machine guarantees this, so we scale
6042 the value, which we know can fit in the mantissa of the type
6043 (hence the use of that type above). */
6044 gcc_assert (Rbase (ur_realval) == 2);
6045 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
6046 - UI_To_Int (Denominator (ur_realval)));
6047 gnu_result = build_real (gnu_result_type, tmp);
6050 /* Now see if we need to negate the result. Do it this way to
6051 properly handle -0. */
6052 if (UR_Is_Negative (Realval (gnat_node)))
6053 gnu_result
6054 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
6055 gnu_result);
6058 break;
6060 case N_String_Literal:
6061 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6062 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
6064 String_Id gnat_string = Strval (gnat_node);
6065 int length = String_Length (gnat_string);
6066 int i;
6067 char *string;
6068 if (length >= ALLOCA_THRESHOLD)
6069 string = XNEWVEC (char, length + 1);
6070 else
6071 string = (char *) alloca (length + 1);
6073 /* Build the string with the characters in the literal. Note
6074 that Ada strings are 1-origin. */
6075 for (i = 0; i < length; i++)
6076 string[i] = Get_String_Char (gnat_string, i + 1);
6078 /* Put a null at the end of the string in case it's in a context
6079 where GCC will want to treat it as a C string. */
6080 string[i] = 0;
6082 gnu_result = build_string (length, string);
6084 /* Strings in GCC don't normally have types, but we want
6085 this to not be converted to the array type. */
6086 TREE_TYPE (gnu_result) = gnu_result_type;
6088 if (length >= ALLOCA_THRESHOLD)
6089 free (string);
6091 else
6093 /* Build a list consisting of each character, then make
6094 the aggregate. */
6095 String_Id gnat_string = Strval (gnat_node);
6096 int length = String_Length (gnat_string);
6097 int i;
6098 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
6099 tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node);
6100 vec<constructor_elt, va_gc> *gnu_vec;
6101 vec_alloc (gnu_vec, length);
6103 for (i = 0; i < length; i++)
6105 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
6106 Get_String_Char (gnat_string, i + 1));
6108 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
6109 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node);
6112 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
6114 break;
6116 case N_Pragma:
6117 gnu_result = Pragma_to_gnu (gnat_node);
6118 break;
6120 /**************************************/
6121 /* Chapter 3: Declarations and Types */
6122 /**************************************/
6124 case N_Subtype_Declaration:
6125 case N_Full_Type_Declaration:
6126 case N_Incomplete_Type_Declaration:
6127 case N_Private_Type_Declaration:
6128 case N_Private_Extension_Declaration:
6129 case N_Task_Type_Declaration:
6130 process_type (Defining_Entity (gnat_node));
6131 gnu_result = alloc_stmt_list ();
6132 break;
6134 case N_Object_Declaration:
6135 case N_Exception_Declaration:
6136 gnat_temp = Defining_Entity (gnat_node);
6137 gnu_result = alloc_stmt_list ();
6139 /* If we are just annotating types and this object has an unconstrained
6140 or task type, don't elaborate it. */
6141 if (type_annotate_only
6142 && (((Is_Array_Type (Etype (gnat_temp))
6143 || Is_Record_Type (Etype (gnat_temp)))
6144 && !Is_Constrained (Etype (gnat_temp)))
6145 || Is_Concurrent_Type (Etype (gnat_temp))))
6146 break;
6148 if (Present (Expression (gnat_node))
6149 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
6150 && (!type_annotate_only
6151 || Compile_Time_Known_Value (Expression (gnat_node))))
6153 gnu_expr = gnat_to_gnu (Expression (gnat_node));
6154 if (Do_Range_Check (Expression (gnat_node)))
6155 gnu_expr
6156 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
6158 if (type_annotate_only && TREE_CODE (gnu_expr) == ERROR_MARK)
6159 gnu_expr = NULL_TREE;
6161 else
6162 gnu_expr = NULL_TREE;
6164 /* If this is a deferred constant with an address clause, we ignore the
6165 full view since the clause is on the partial view and we cannot have
6166 2 different GCC trees for the object. The only bits of the full view
6167 we will use is the initializer, but it will be directly fetched. */
6168 if (Ekind (gnat_temp) == E_Constant
6169 && Present (Address_Clause (gnat_temp))
6170 && Present (Full_View (gnat_temp)))
6171 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
6173 /* If this object has its elaboration delayed, we must force evaluation
6174 of GNU_EXPR now and save it for the freeze point. Note that we need
6175 not do anything special at the global level since the lifetime of the
6176 temporary is fully contained within the elaboration routine. */
6177 if (Present (Freeze_Node (gnat_temp)))
6179 if (gnu_expr)
6181 gnu_result = gnat_save_expr (gnu_expr);
6182 save_gnu_tree (gnat_node, gnu_result, true);
6185 else
6186 gnat_to_gnu_entity (gnat_temp, gnu_expr, true);
6187 break;
6189 case N_Object_Renaming_Declaration:
6190 gnat_temp = Defining_Entity (gnat_node);
6191 gnu_result = alloc_stmt_list ();
6193 /* Don't do anything if this renaming is handled by the front end and it
6194 does not need debug info. Note that we consider renamings don't need
6195 debug info when optimizing: our way to describe them has a
6196 memory/elaboration footprint.
6198 Don't do anything neither if we are just annotating types and this
6199 object has a composite or task type, don't elaborate it. */
6200 if ((!Is_Renaming_Of_Object (gnat_temp)
6201 || (Needs_Debug_Info (gnat_temp)
6202 && !optimize
6203 && can_materialize_object_renaming_p
6204 (Renamed_Object (gnat_temp))))
6205 && ! (type_annotate_only
6206 && (Is_Array_Type (Etype (gnat_temp))
6207 || Is_Record_Type (Etype (gnat_temp))
6208 || Is_Concurrent_Type (Etype (gnat_temp)))))
6210 tree gnu_temp
6211 = gnat_to_gnu_entity (gnat_temp,
6212 gnat_to_gnu (Renamed_Object (gnat_temp)),
6213 true);
6214 /* See case 2 of renaming in gnat_to_gnu_entity. */
6215 if (TREE_SIDE_EFFECTS (gnu_temp))
6216 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_temp);
6218 break;
6220 case N_Exception_Renaming_Declaration:
6221 gnat_temp = Defining_Entity (gnat_node);
6222 gnu_result = alloc_stmt_list ();
6224 /* See the above case for the rationale. */
6225 if (Present (Renamed_Entity (gnat_temp)))
6227 tree gnu_temp
6228 = gnat_to_gnu_entity (gnat_temp,
6229 gnat_to_gnu (Renamed_Entity (gnat_temp)),
6230 true);
6231 if (TREE_SIDE_EFFECTS (gnu_temp))
6232 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_temp);
6234 break;
6236 case N_Subprogram_Renaming_Declaration:
6238 const Node_Id gnat_renaming = Defining_Entity (gnat_node);
6239 const Node_Id gnat_renamed = Renamed_Entity (gnat_renaming);
6241 gnu_result = alloc_stmt_list ();
6243 /* Materializing renamed subprograms will only benefit the debugging
6244 information as they aren't referenced in the generated code. So
6245 skip them when they aren't needed. Avoid doing this if:
6247 - there is a freeze node: in this case the renamed entity is not
6248 elaborated yet,
6249 - the renamed subprogram is intrinsic: it will not be available in
6250 the debugging information (note that both or only one of the
6251 renaming and the renamed subprograms can be intrinsic). */
6252 if (!type_annotate_only
6253 && Needs_Debug_Info (gnat_renaming)
6254 && No (Freeze_Node (gnat_renaming))
6255 && Present (gnat_renamed)
6256 && (Ekind (gnat_renamed) == E_Function
6257 || Ekind (gnat_renamed) == E_Procedure)
6258 && !Is_Intrinsic_Subprogram (gnat_renaming)
6259 && !Is_Intrinsic_Subprogram (gnat_renamed))
6260 gnat_to_gnu_entity (gnat_renaming, gnat_to_gnu (gnat_renamed), true);
6261 break;
6264 case N_Implicit_Label_Declaration:
6265 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true);
6266 gnu_result = alloc_stmt_list ();
6267 break;
6269 case N_Number_Declaration:
6270 case N_Package_Renaming_Declaration:
6271 /* These are fully handled in the front end. */
6272 /* ??? For package renamings, find a way to use GENERIC namespaces so
6273 that we get proper debug information for them. */
6274 gnu_result = alloc_stmt_list ();
6275 break;
6277 /*************************************/
6278 /* Chapter 4: Names and Expressions */
6279 /*************************************/
6281 case N_Explicit_Dereference:
6282 /* Make sure the designated type is complete before dereferencing. */
6283 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6284 gnu_result = gnat_to_gnu (Prefix (gnat_node));
6285 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
6287 /* If atomic access is required on the RHS, build the atomic load. */
6288 if (atomic_access_required_p (gnat_node, &sync)
6289 && !present_in_lhs_or_actual_p (gnat_node))
6290 gnu_result = build_atomic_load (gnu_result, sync);
6291 break;
6293 case N_Indexed_Component:
6295 tree gnu_array_object
6296 = gnat_to_gnu (adjust_for_implicit_deref (Prefix (gnat_node)));
6297 tree gnu_type;
6298 int ndim;
6299 int i;
6300 Node_Id *gnat_expr_array;
6302 gnu_array_object = maybe_implicit_deref (gnu_array_object);
6304 /* Convert vector inputs to their representative array type, to fit
6305 what the code below expects. */
6306 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
6308 if (present_in_lhs_or_actual_p (gnat_node))
6309 gnat_mark_addressable (gnu_array_object);
6310 gnu_array_object = maybe_vector_array (gnu_array_object);
6313 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
6315 /* If we got a padded type, remove it too. */
6316 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
6317 gnu_array_object
6318 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
6319 gnu_array_object);
6321 /* The failure of this assertion will very likely come from a missing
6322 expansion for a packed array access. */
6323 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
6325 /* First compute the number of dimensions of the array, then
6326 fill the expression array, the order depending on whether
6327 this is a Convention_Fortran array or not. */
6328 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
6329 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6330 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
6331 ndim++, gnu_type = TREE_TYPE (gnu_type))
6334 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
6336 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
6337 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
6338 i >= 0;
6339 i--, gnat_temp = Next (gnat_temp))
6340 gnat_expr_array[i] = gnat_temp;
6341 else
6342 for (i = 0, gnat_temp = First (Expressions (gnat_node));
6343 i < ndim;
6344 i++, gnat_temp = Next (gnat_temp))
6345 gnat_expr_array[i] = gnat_temp;
6347 /* Start with the prefix and build the successive references. */
6348 gnu_result = gnu_array_object;
6350 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
6351 i < ndim;
6352 i++, gnu_type = TREE_TYPE (gnu_type))
6354 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
6355 gnat_temp = gnat_expr_array[i];
6356 gnu_expr = maybe_character_value (gnat_to_gnu (gnat_temp));
6357 struct loop_info_d *loop;
6359 gnu_result
6360 = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr);
6362 /* Array accesses are bound-checked so they cannot trap, but this
6363 is valid only if they are not hoisted ahead of the check. We
6364 need to mark them as no-trap to get decent loop optimizations
6365 in the presence of -fnon-call-exceptions, so we do it when we
6366 know that the original expression had no side-effects. */
6367 if (TREE_CODE (gnu_result) == ARRAY_REF
6368 && !(Nkind (gnat_temp) == N_Identifier
6369 && Ekind (Entity (gnat_temp)) == E_Constant))
6370 TREE_THIS_NOTRAP (gnu_result) = 1;
6372 /* If aggressive loop optimizations are enabled, we warn for loops
6373 overrunning a simple array of size 1 not at the end of a record.
6374 This is aimed to catch misuses of the trailing array idiom. */
6375 if (optimize
6376 && flag_aggressive_loop_optimizations
6377 && inside_loop_p ()
6378 && TREE_CODE (TREE_TYPE (gnu_type)) != ARRAY_TYPE
6379 && TREE_CODE (gnu_array_object) != ARRAY_REF
6380 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_type)),
6381 TYPE_MAX_VALUE (TYPE_DOMAIN (gnu_type)))
6382 && !array_at_struct_end_p (gnu_result)
6383 && (loop = find_loop_for (gnu_expr))
6384 && !loop->artificial
6385 && !loop->has_checks
6386 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_type)),
6387 loop->low_bound)
6388 && can_be_lower_p (loop->low_bound, loop->high_bound)
6389 && !loop->warned_aggressive_loop_optimizations
6390 && warning (OPT_Waggressive_loop_optimizations,
6391 "out-of-bounds access may be optimized away"))
6393 inform (EXPR_LOCATION (loop->stmt), "containing loop");
6394 loop->warned_aggressive_loop_optimizations = true;
6398 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6400 /* If atomic access is required on the RHS, build the atomic load. */
6401 if (atomic_access_required_p (gnat_node, &sync)
6402 && !present_in_lhs_or_actual_p (gnat_node))
6403 gnu_result = build_atomic_load (gnu_result, sync);
6405 break;
6407 case N_Slice:
6409 tree gnu_array_object
6410 = gnat_to_gnu (adjust_for_implicit_deref (Prefix (gnat_node)));
6412 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6414 gnu_array_object = maybe_implicit_deref (gnu_array_object);
6415 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
6417 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
6418 gnu_expr = maybe_character_value (gnu_expr);
6420 /* If this is a slice with non-constant size of an array with constant
6421 size, set the maximum size for the allocation of temporaries. */
6422 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
6423 && TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object))))
6424 TYPE_ARRAY_MAX_SIZE (gnu_result_type)
6425 = TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object));
6427 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
6428 gnu_array_object, gnu_expr);
6430 break;
6432 case N_Selected_Component:
6434 Entity_Id gnat_prefix
6435 = adjust_for_implicit_deref (Prefix (gnat_node));
6436 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
6437 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
6439 gnu_prefix = maybe_implicit_deref (gnu_prefix);
6441 /* gnat_to_gnu_entity does not save the GNU tree made for renamed
6442 discriminants so avoid making recursive calls on each reference
6443 to them by following the appropriate link directly here. */
6444 if (Ekind (gnat_field) == E_Discriminant)
6446 /* For discriminant references in tagged types always substitute
6447 the corresponding discriminant as the actual component. */
6448 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix))))
6449 while (Present (Corresponding_Discriminant (gnat_field)))
6450 gnat_field = Corresponding_Discriminant (gnat_field);
6452 /* For discriminant references in untagged types always substitute
6453 the corresponding stored discriminant. */
6454 else if (Present (Corresponding_Discriminant (gnat_field)))
6455 gnat_field = Original_Record_Component (gnat_field);
6458 /* Handle extracting the real or imaginary part of a complex.
6459 The real part is the first field and the imaginary the last. */
6460 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
6461 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
6462 ? REALPART_EXPR : IMAGPART_EXPR,
6463 NULL_TREE, gnu_prefix);
6464 else
6466 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
6468 gnu_result
6469 = build_component_ref (gnu_prefix, gnu_field,
6470 (Nkind (Parent (gnat_node))
6471 == N_Attribute_Reference)
6472 && lvalue_required_for_attribute_p
6473 (Parent (gnat_node)));
6476 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6478 /* If atomic access is required on the RHS, build the atomic load. */
6479 if (atomic_access_required_p (gnat_node, &sync)
6480 && !present_in_lhs_or_actual_p (gnat_node))
6481 gnu_result = build_atomic_load (gnu_result, sync);
6483 break;
6485 case N_Attribute_Reference:
6487 /* The attribute designator. */
6488 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
6490 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
6491 is a unit, not an object with a GCC equivalent. */
6492 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
6493 return
6494 create_subprog_decl (create_concat_name
6495 (Entity (Prefix (gnat_node)),
6496 attr == Attr_Elab_Body ? "elabb" : "elabs"),
6497 NULL_TREE, void_ftype, NULL_TREE, is_disabled,
6498 true, true, true, true, false, NULL,
6499 gnat_node);
6501 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
6503 break;
6505 case N_Reference:
6506 /* Like 'Access as far as we are concerned. */
6507 gnu_result = gnat_to_gnu (Prefix (gnat_node));
6508 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6509 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6510 break;
6512 case N_Aggregate:
6513 case N_Extension_Aggregate:
6515 tree gnu_aggr_type;
6517 /* ??? It is wrong to evaluate the type now, but there doesn't
6518 seem to be any other practical way of doing it. */
6520 gcc_assert (!Expansion_Delayed (gnat_node));
6522 gnu_aggr_type = gnu_result_type
6523 = get_unpadded_type (Etype (gnat_node));
6525 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
6526 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
6527 gnu_aggr_type
6528 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
6529 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
6530 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
6532 if (Null_Record_Present (gnat_node))
6533 gnu_result = gnat_build_constructor (gnu_aggr_type, NULL);
6535 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
6536 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
6537 gnu_result
6538 = assoc_to_constructor (Etype (gnat_node),
6539 First (Component_Associations (gnat_node)),
6540 gnu_aggr_type);
6541 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
6542 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
6543 gnu_aggr_type,
6544 Component_Type (Etype (gnat_node)));
6545 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
6546 gnu_result
6547 = build_binary_op
6548 (COMPLEX_EXPR, gnu_aggr_type,
6549 gnat_to_gnu (Expression (First
6550 (Component_Associations (gnat_node)))),
6551 gnat_to_gnu (Expression
6552 (Next
6553 (First (Component_Associations (gnat_node))))));
6554 else
6555 gcc_unreachable ();
6557 gnu_result = convert (gnu_result_type, gnu_result);
6559 break;
6561 case N_Null:
6562 if (TARGET_VTABLE_USES_DESCRIPTORS
6563 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
6564 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
6565 gnu_result = null_fdesc_node;
6566 else
6567 gnu_result = null_pointer_node;
6568 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6569 break;
6571 case N_Type_Conversion:
6572 case N_Qualified_Expression:
6573 gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node)));
6574 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6576 /* If this is a qualified expression for a tagged type, we mark the type
6577 as used. Because of polymorphism, this might be the only reference to
6578 the tagged type in the program while objects have it as dynamic type.
6579 The debugger needs to see it to display these objects properly. */
6580 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
6581 used_types_insert (gnu_result_type);
6583 gnu_result
6584 = convert_with_check (Etype (gnat_node), gnu_expr,
6585 Do_Overflow_Check (gnat_node),
6586 Do_Range_Check (Expression (gnat_node)),
6587 kind == N_Type_Conversion
6588 && Float_Truncate (gnat_node), gnat_node);
6589 break;
6591 case N_Unchecked_Type_Conversion:
6592 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6593 gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node)));
6595 /* Skip further processing if the conversion is deemed a no-op. */
6596 if (unchecked_conversion_nop (gnat_node))
6598 gnu_result = gnu_expr;
6599 gnu_result_type = TREE_TYPE (gnu_result);
6600 break;
6603 /* If the result is a pointer type, see if we are improperly
6604 converting to a stricter alignment. */
6605 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
6606 && Is_Access_Type (Etype (gnat_node)))
6608 unsigned int align = known_alignment (gnu_expr);
6609 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6610 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6612 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6613 post_error_ne_tree_2
6614 ("?source alignment (^) '< alignment of & (^)",
6615 gnat_node, Designated_Type (Etype (gnat_node)),
6616 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
6619 /* If we are converting a descriptor to a function pointer, first
6620 build the pointer. */
6621 if (TARGET_VTABLE_USES_DESCRIPTORS
6622 && TREE_TYPE (gnu_expr) == fdesc_type_node
6623 && POINTER_TYPE_P (gnu_result_type))
6624 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
6626 gnu_result = unchecked_convert (gnu_result_type, gnu_expr,
6627 No_Truncation (gnat_node));
6628 break;
6630 case N_In:
6631 case N_Not_In:
6633 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
6634 tree gnu_low, gnu_high;
6636 Range_to_gnu (Right_Opnd (gnat_node), &gnu_low, &gnu_high);
6637 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6639 tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_obj));
6640 if (TREE_TYPE (gnu_obj) != gnu_op_type)
6642 gnu_obj = convert (gnu_op_type, gnu_obj);
6643 gnu_low = convert (gnu_op_type, gnu_low);
6644 gnu_high = convert (gnu_op_type, gnu_high);
6647 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
6648 ensure that GNU_OBJ is evaluated only once and perform a full range
6649 test. */
6650 if (operand_equal_p (gnu_low, gnu_high, 0))
6651 gnu_result
6652 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
6653 else
6655 tree t1, t2;
6656 gnu_obj = gnat_protect_expr (gnu_obj);
6657 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
6658 if (EXPR_P (t1))
6659 set_expr_location_from_node (t1, gnat_node);
6660 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
6661 if (EXPR_P (t2))
6662 set_expr_location_from_node (t2, gnat_node);
6663 gnu_result
6664 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
6667 if (kind == N_Not_In)
6668 gnu_result
6669 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
6671 break;
6673 case N_Op_Divide:
6674 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6675 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6676 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6677 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
6678 ? RDIV_EXPR
6679 : (Rounded_Result (gnat_node)
6680 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
6681 gnu_result_type, gnu_lhs, gnu_rhs);
6682 break;
6684 case N_Op_And:
6685 case N_Op_Or:
6686 case N_Op_Xor:
6687 /* These can either be operations on booleans or on modular types.
6688 Fall through for boolean types since that's the way GNU_CODES is
6689 set up. */
6690 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6692 enum tree_code code
6693 = (kind == N_Op_Or ? BIT_IOR_EXPR
6694 : kind == N_Op_And ? BIT_AND_EXPR
6695 : BIT_XOR_EXPR);
6697 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6698 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6699 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6700 gnu_result = build_binary_op (code, gnu_result_type,
6701 gnu_lhs, gnu_rhs);
6702 break;
6705 /* ... fall through ... */
6707 case N_Op_Eq:
6708 case N_Op_Ne:
6709 case N_Op_Lt:
6710 case N_Op_Le:
6711 case N_Op_Gt:
6712 case N_Op_Ge:
6713 case N_Op_Add:
6714 case N_Op_Subtract:
6715 case N_Op_Multiply:
6716 case N_Op_Mod:
6717 case N_Op_Rem:
6718 case N_Op_Rotate_Left:
6719 case N_Op_Rotate_Right:
6720 case N_Op_Shift_Left:
6721 case N_Op_Shift_Right:
6722 case N_Op_Shift_Right_Arithmetic:
6723 case N_And_Then:
6724 case N_Or_Else:
6726 enum tree_code code = gnu_codes[kind];
6727 bool ignore_lhs_overflow = false;
6728 location_t saved_location = input_location;
6729 tree gnu_type;
6731 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6732 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6733 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6735 /* Pending generic support for efficient vector logical operations in
6736 GCC, convert vectors to their representative array type view and
6737 fallthrough. */
6738 gnu_lhs = maybe_vector_array (gnu_lhs);
6739 gnu_rhs = maybe_vector_array (gnu_rhs);
6741 /* If this is a comparison operator, convert any references to an
6742 unconstrained array value into a reference to the actual array. */
6743 if (TREE_CODE_CLASS (code) == tcc_comparison)
6745 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
6746 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
6748 tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_lhs));
6749 if (TREE_TYPE (gnu_lhs) != gnu_op_type)
6751 gnu_lhs = convert (gnu_op_type, gnu_lhs);
6752 gnu_rhs = convert (gnu_op_type, gnu_rhs);
6756 /* If this is a shift whose count is not guaranteed to be correct,
6757 we need to adjust the shift count. */
6758 if (IN (kind, N_Op_Shift) && !Shift_Count_OK (gnat_node))
6760 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
6761 tree gnu_max_shift
6762 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
6764 if (kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
6765 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
6766 gnu_rhs, gnu_max_shift);
6767 else if (kind == N_Op_Shift_Right_Arithmetic)
6768 gnu_rhs
6769 = build_binary_op
6770 (MIN_EXPR, gnu_count_type,
6771 build_binary_op (MINUS_EXPR,
6772 gnu_count_type,
6773 gnu_max_shift,
6774 build_int_cst (gnu_count_type, 1)),
6775 gnu_rhs);
6778 /* For right shifts, the type says what kind of shift to do,
6779 so we may need to choose a different type. In this case,
6780 we have to ignore integer overflow lest it propagates all
6781 the way down and causes a CE to be explicitly raised. */
6782 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
6784 gnu_type = gnat_unsigned_type_for (gnu_type);
6785 ignore_lhs_overflow = true;
6787 else if (kind == N_Op_Shift_Right_Arithmetic
6788 && TYPE_UNSIGNED (gnu_type))
6790 gnu_type = gnat_signed_type_for (gnu_type);
6791 ignore_lhs_overflow = true;
6794 if (gnu_type != gnu_result_type)
6796 tree gnu_old_lhs = gnu_lhs;
6797 gnu_lhs = convert (gnu_type, gnu_lhs);
6798 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
6799 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
6800 gnu_rhs = convert (gnu_type, gnu_rhs);
6803 /* Instead of expanding overflow checks for addition, subtraction
6804 and multiplication itself, the front end will leave this to
6805 the back end when Backend_Overflow_Checks_On_Target is set. */
6806 if (Do_Overflow_Check (gnat_node)
6807 && Backend_Overflow_Checks_On_Target
6808 && (code == PLUS_EXPR || code == MINUS_EXPR || code == MULT_EXPR)
6809 && !TYPE_UNSIGNED (gnu_type)
6810 && !FLOAT_TYPE_P (gnu_type))
6811 gnu_result = build_binary_op_trapv (code, gnu_type,
6812 gnu_lhs, gnu_rhs, gnat_node);
6813 else
6815 /* Some operations, e.g. comparisons of arrays, generate complex
6816 trees that need to be annotated while they are being built. */
6817 input_location = saved_location;
6818 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
6821 /* If this is a logical shift with the shift count not verified,
6822 we must return zero if it is too large. We cannot compensate
6823 above in this case. */
6824 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
6825 && !Shift_Count_OK (gnat_node))
6826 gnu_result
6827 = build_cond_expr
6828 (gnu_type,
6829 build_binary_op (GE_EXPR, boolean_type_node,
6830 gnu_rhs,
6831 convert (TREE_TYPE (gnu_rhs),
6832 TYPE_SIZE (gnu_type))),
6833 build_int_cst (gnu_type, 0),
6834 gnu_result);
6836 break;
6838 case N_If_Expression:
6840 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
6841 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
6842 tree gnu_false
6843 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
6845 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6846 gnu_result
6847 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
6849 break;
6851 case N_Op_Plus:
6852 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
6853 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6854 break;
6856 case N_Op_Not:
6857 /* This case can apply to a boolean or a modular type.
6858 Fall through for a boolean operand since GNU_CODES is set
6859 up to handle this. */
6860 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6862 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6863 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6864 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
6865 gnu_expr);
6866 break;
6869 /* ... fall through ... */
6871 case N_Op_Minus:
6872 case N_Op_Abs:
6873 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6874 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6876 /* Instead of expanding overflow checks for negation and absolute
6877 value itself, the front end will leave this to the back end
6878 when Backend_Overflow_Checks_On_Target is set. */
6879 if (Do_Overflow_Check (gnat_node)
6880 && Backend_Overflow_Checks_On_Target
6881 && !TYPE_UNSIGNED (gnu_result_type)
6882 && !FLOAT_TYPE_P (gnu_result_type))
6883 gnu_result
6884 = build_unary_op_trapv (gnu_codes[kind],
6885 gnu_result_type, gnu_expr, gnat_node);
6886 else
6887 gnu_result = build_unary_op (gnu_codes[kind],
6888 gnu_result_type, gnu_expr);
6889 break;
6891 case N_Allocator:
6893 tree gnu_init = NULL_TREE;
6894 tree gnu_type;
6895 bool ignore_init_type = false;
6897 gnat_temp = Expression (gnat_node);
6899 /* The Expression operand can either be an N_Identifier or
6900 Expanded_Name, which must represent a type, or a
6901 N_Qualified_Expression, which contains both the object type and an
6902 initial value for the object. */
6903 if (Nkind (gnat_temp) == N_Identifier
6904 || Nkind (gnat_temp) == N_Expanded_Name)
6905 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
6906 else if (Nkind (gnat_temp) == N_Qualified_Expression)
6908 Entity_Id gnat_desig_type
6909 = Designated_Type (Underlying_Type (Etype (gnat_node)));
6911 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
6912 gnu_init = gnat_to_gnu (Expression (gnat_temp));
6914 gnu_init = maybe_unconstrained_array (gnu_init);
6915 if (Do_Range_Check (Expression (gnat_temp)))
6916 gnu_init
6917 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
6919 if (Is_Elementary_Type (gnat_desig_type)
6920 || Is_Constrained (gnat_desig_type))
6921 gnu_type = gnat_to_gnu_type (gnat_desig_type);
6922 else
6924 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
6925 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
6926 gnu_type = TREE_TYPE (gnu_init);
6929 /* See the N_Qualified_Expression case for the rationale. */
6930 if (Is_Tagged_Type (gnat_desig_type))
6931 used_types_insert (gnu_type);
6933 gnu_init = convert (gnu_type, gnu_init);
6935 else
6936 gcc_unreachable ();
6938 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6939 return build_allocator (gnu_type, gnu_init, gnu_result_type,
6940 Procedure_To_Call (gnat_node),
6941 Storage_Pool (gnat_node), gnat_node,
6942 ignore_init_type);
6944 break;
6946 /**************************/
6947 /* Chapter 5: Statements */
6948 /**************************/
6950 case N_Label:
6951 gnu_result = build1 (LABEL_EXPR, void_type_node,
6952 gnat_to_gnu (Identifier (gnat_node)));
6953 break;
6955 case N_Null_Statement:
6956 /* When not optimizing, turn null statements from source into gotos to
6957 the next statement that the middle-end knows how to preserve. */
6958 if (!optimize && Comes_From_Source (gnat_node))
6960 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
6961 DECL_IGNORED_P (label) = 1;
6962 start_stmt_group ();
6963 stmt = build1 (GOTO_EXPR, void_type_node, label);
6964 set_expr_location_from_node (stmt, gnat_node);
6965 add_stmt (stmt);
6966 stmt = build1 (LABEL_EXPR, void_type_node, label);
6967 set_expr_location_from_node (stmt, gnat_node);
6968 add_stmt (stmt);
6969 gnu_result = end_stmt_group ();
6971 else
6972 gnu_result = alloc_stmt_list ();
6973 break;
6975 case N_Assignment_Statement:
6976 /* Get the LHS and RHS of the statement and convert any reference to an
6977 unconstrained array into a reference to the underlying array. */
6978 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
6980 /* If the type has a size that overflows, convert this into raise of
6981 Storage_Error: execution shouldn't have gotten here anyway. */
6982 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
6983 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
6984 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
6985 N_Raise_Storage_Error);
6986 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
6988 bool outer_atomic_access
6989 = outer_atomic_access_required_p (Name (gnat_node));
6990 bool atomic_access
6991 = !outer_atomic_access
6992 && atomic_access_required_p (Name (gnat_node), &sync);
6993 gnu_result
6994 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
6995 outer_atomic_access, atomic_access, sync);
6997 else
6999 const Node_Id gnat_expr = Expression (gnat_node);
7000 const Entity_Id gnat_type
7001 = Underlying_Type (Etype (Name (gnat_node)));
7002 const bool regular_array_type_p
7003 = (Is_Array_Type (gnat_type) && !Is_Bit_Packed_Array (gnat_type));
7004 const bool use_memset_p
7005 = (regular_array_type_p
7006 && Nkind (gnat_expr) == N_Aggregate
7007 && Is_Others_Aggregate (gnat_expr));
7009 /* If we'll use memset, we need to find the inner expression. */
7010 if (use_memset_p)
7012 Node_Id gnat_inner
7013 = Expression (First (Component_Associations (gnat_expr)));
7014 while (Nkind (gnat_inner) == N_Aggregate
7015 && Is_Others_Aggregate (gnat_inner))
7016 gnat_inner
7017 = Expression (First (Component_Associations (gnat_inner)));
7018 gnu_rhs = gnat_to_gnu (gnat_inner);
7020 else
7021 gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr));
7023 /* If range check is needed, emit code to generate it. */
7024 if (Do_Range_Check (gnat_expr))
7025 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
7026 gnat_node);
7028 /* If an outer atomic access is required on the LHS, build the load-
7029 modify-store sequence. */
7030 if (outer_atomic_access_required_p (Name (gnat_node)))
7031 gnu_result = build_load_modify_store (gnu_lhs, gnu_rhs, gnat_node);
7033 /* Or else, if atomic access is required, build the atomic store. */
7034 else if (atomic_access_required_p (Name (gnat_node), &sync))
7035 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs, sync);
7037 /* Or else, use memset when the conditions are met. */
7038 else if (use_memset_p)
7040 tree value = fold_convert (integer_type_node, gnu_rhs);
7041 tree to = gnu_lhs;
7042 tree type = TREE_TYPE (to);
7043 tree size
7044 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to);
7045 tree to_ptr = build_fold_addr_expr (to);
7046 tree t = builtin_decl_explicit (BUILT_IN_MEMSET);
7047 if (TREE_CODE (value) == INTEGER_CST)
7049 tree mask
7050 = build_int_cst (integer_type_node,
7051 ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
7052 value = int_const_binop (BIT_AND_EXPR, value, mask);
7054 gnu_result = build_call_expr (t, 3, to_ptr, value, size);
7057 /* Otherwise build a regular assignment. */
7058 else
7059 gnu_result
7060 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
7062 /* If the assignment type is a regular array and the two sides are
7063 not completely disjoint, play safe and use memmove. But don't do
7064 it for a bit-packed array as it might not be byte-aligned. */
7065 if (TREE_CODE (gnu_result) == MODIFY_EXPR
7066 && regular_array_type_p
7067 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
7069 tree to = TREE_OPERAND (gnu_result, 0);
7070 tree from = TREE_OPERAND (gnu_result, 1);
7071 tree type = TREE_TYPE (from);
7072 tree size
7073 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from);
7074 tree to_ptr = build_fold_addr_expr (to);
7075 tree from_ptr = build_fold_addr_expr (from);
7076 tree t = builtin_decl_explicit (BUILT_IN_MEMMOVE);
7077 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
7080 break;
7082 case N_If_Statement:
7084 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
7086 /* Make the outer COND_EXPR. Avoid non-determinism. */
7087 gnu_result = build3 (COND_EXPR, void_type_node,
7088 gnat_to_gnu (Condition (gnat_node)),
7089 NULL_TREE, NULL_TREE);
7090 COND_EXPR_THEN (gnu_result)
7091 = build_stmt_group (Then_Statements (gnat_node), false);
7092 TREE_SIDE_EFFECTS (gnu_result) = 1;
7093 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
7095 /* Now make a COND_EXPR for each of the "else if" parts. Put each
7096 into the previous "else" part and point to where to put any
7097 outer "else". Also avoid non-determinism. */
7098 if (Present (Elsif_Parts (gnat_node)))
7099 for (gnat_temp = First (Elsif_Parts (gnat_node));
7100 Present (gnat_temp); gnat_temp = Next (gnat_temp))
7102 gnu_expr = build3 (COND_EXPR, void_type_node,
7103 gnat_to_gnu (Condition (gnat_temp)),
7104 NULL_TREE, NULL_TREE);
7105 COND_EXPR_THEN (gnu_expr)
7106 = build_stmt_group (Then_Statements (gnat_temp), false);
7107 TREE_SIDE_EFFECTS (gnu_expr) = 1;
7108 set_expr_location_from_node (gnu_expr, gnat_temp);
7109 *gnu_else_ptr = gnu_expr;
7110 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
7113 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
7115 break;
7117 case N_Case_Statement:
7118 gnu_result = Case_Statement_to_gnu (gnat_node);
7119 break;
7121 case N_Loop_Statement:
7122 gnu_result = Loop_Statement_to_gnu (gnat_node);
7123 break;
7125 case N_Block_Statement:
7126 /* The only way to enter the block is to fall through to it. */
7127 if (stmt_group_may_fallthru ())
7129 start_stmt_group ();
7130 gnat_pushlevel ();
7131 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
7132 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
7133 gnat_poplevel ();
7134 gnu_result = end_stmt_group ();
7136 else
7137 gnu_result = alloc_stmt_list ();
7138 break;
7140 case N_Exit_Statement:
7141 gnu_result
7142 = build2 (EXIT_STMT, void_type_node,
7143 (Present (Condition (gnat_node))
7144 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
7145 (Present (Name (gnat_node))
7146 ? get_gnu_tree (Entity (Name (gnat_node)))
7147 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
7148 break;
7150 case N_Simple_Return_Statement:
7152 tree gnu_ret_obj, gnu_ret_val;
7154 /* If the subprogram is a function, we must return the expression. */
7155 if (Present (Expression (gnat_node)))
7157 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
7159 /* If this function has copy-in/copy-out parameters parameters and
7160 doesn't return by invisible reference, get the real object for
7161 the return. See Subprogram_Body_to_gnu. */
7162 if (TYPE_CI_CO_LIST (gnu_subprog_type)
7163 && !TREE_ADDRESSABLE (gnu_subprog_type))
7164 gnu_ret_obj = gnu_return_var_stack->last ();
7165 else
7166 gnu_ret_obj = DECL_RESULT (current_function_decl);
7168 /* Get the GCC tree for the expression to be returned. */
7169 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
7171 /* Do not remove the padding from GNU_RET_VAL if the inner type is
7172 self-referential since we want to allocate the fixed size. */
7173 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
7174 && type_is_padding_self_referential
7175 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0))))
7176 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
7178 /* If the function returns by direct reference, return a pointer
7179 to the return value. */
7180 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)
7181 || By_Ref (gnat_node))
7182 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
7184 /* Otherwise, if it returns an unconstrained array, we have to
7185 allocate a new version of the result and return it. */
7186 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type))
7188 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
7190 /* And find out whether this is a candidate for Named Return
7191 Value. If so, record it. */
7192 if (!TYPE_CI_CO_LIST (gnu_subprog_type) && optimize)
7194 tree ret_val = gnu_ret_val;
7196 /* Strip useless conversions around the return value. */
7197 if (gnat_useless_type_conversion (ret_val))
7198 ret_val = TREE_OPERAND (ret_val, 0);
7200 /* Strip unpadding around the return value. */
7201 if (TREE_CODE (ret_val) == COMPONENT_REF
7202 && TYPE_IS_PADDING_P
7203 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
7204 ret_val = TREE_OPERAND (ret_val, 0);
7206 /* Now apply the test to the return value. */
7207 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
7209 if (!f_named_ret_val)
7210 f_named_ret_val = BITMAP_GGC_ALLOC ();
7211 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
7212 if (!f_gnat_ret)
7213 f_gnat_ret = gnat_node;
7217 gnu_ret_val = build_allocator (TREE_TYPE (gnu_ret_val),
7218 gnu_ret_val,
7219 TREE_TYPE (gnu_ret_obj),
7220 Procedure_To_Call (gnat_node),
7221 Storage_Pool (gnat_node),
7222 gnat_node, false);
7225 /* Otherwise, if it returns by invisible reference, dereference
7226 the pointer it is passed using the type of the return value
7227 and build the copy operation manually. This ensures that we
7228 don't copy too much data, for example if the return type is
7229 unconstrained with a maximum size. */
7230 else if (TREE_ADDRESSABLE (gnu_subprog_type))
7232 tree gnu_ret_deref
7233 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
7234 gnu_ret_obj);
7235 gnu_result = build2 (INIT_EXPR, void_type_node,
7236 gnu_ret_deref, gnu_ret_val);
7237 add_stmt_with_node (gnu_result, gnat_node);
7238 gnu_ret_val = NULL_TREE;
7242 else
7243 gnu_ret_obj = gnu_ret_val = NULL_TREE;
7245 /* If we have a return label defined, convert this into a branch to
7246 that label. The return proper will be handled elsewhere. */
7247 if (gnu_return_label_stack->last ())
7249 if (gnu_ret_val)
7250 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_ret_obj,
7251 gnu_ret_val));
7253 gnu_result = build1 (GOTO_EXPR, void_type_node,
7254 gnu_return_label_stack->last ());
7256 /* When not optimizing, make sure the return is preserved. */
7257 if (!optimize && Comes_From_Source (gnat_node))
7258 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
7261 /* Otherwise, build a regular return. */
7262 else
7263 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
7265 break;
7267 case N_Goto_Statement:
7268 gnu_result
7269 = build1 (GOTO_EXPR, void_type_node, gnat_to_gnu (Name (gnat_node)));
7270 break;
7272 /***************************/
7273 /* Chapter 6: Subprograms */
7274 /***************************/
7276 case N_Subprogram_Declaration:
7277 /* Unless there is a freeze node, declare the entity. We consider
7278 this a definition even though we're not generating code for the
7279 subprogram because we will be making the corresponding GCC node.
7280 When there is a freeze node, it is considered the definition of
7281 the subprogram and we do nothing until after it is encountered.
7282 That's an efficiency issue: the types involved in the profile
7283 are far more likely to be frozen between the declaration and
7284 the freeze node than before the declaration, so we save some
7285 updates of the GCC node by waiting until the freeze node.
7286 The counterpart is that we assume that there is no reference
7287 to the subprogram between the declaration and the freeze node
7288 in the expanded code; otherwise, it will be interpreted as an
7289 external reference and very likely give rise to a link failure. */
7290 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
7291 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
7292 NULL_TREE, true);
7293 gnu_result = alloc_stmt_list ();
7294 break;
7296 case N_Abstract_Subprogram_Declaration:
7297 /* This subprogram doesn't exist for code generation purposes, but we
7298 have to elaborate the types of any parameters and result, unless
7299 they are imported types (nothing to generate in this case).
7301 The parameter list may contain types with freeze nodes, e.g. not null
7302 subtypes, so the subprogram itself may carry a freeze node, in which
7303 case its elaboration must be deferred. */
7305 /* Process the parameter types first. */
7306 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
7307 for (gnat_temp
7308 = First_Formal_With_Extras
7309 (Defining_Entity (Specification (gnat_node)));
7310 Present (gnat_temp);
7311 gnat_temp = Next_Formal_With_Extras (gnat_temp))
7312 if (Is_Itype (Etype (gnat_temp))
7313 && !From_Limited_With (Etype (gnat_temp)))
7314 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
7316 /* Then the result type, set to Standard_Void_Type for procedures. */
7318 Entity_Id gnat_temp_type
7319 = Etype (Defining_Entity (Specification (gnat_node)));
7321 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
7322 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, false);
7325 gnu_result = alloc_stmt_list ();
7326 break;
7328 case N_Defining_Program_Unit_Name:
7329 /* For a child unit identifier go up a level to get the specification.
7330 We get this when we try to find the spec of a child unit package
7331 that is the compilation unit being compiled. */
7332 gnu_result = gnat_to_gnu (Parent (gnat_node));
7333 break;
7335 case N_Subprogram_Body:
7336 Subprogram_Body_to_gnu (gnat_node);
7337 gnu_result = alloc_stmt_list ();
7338 break;
7340 case N_Function_Call:
7341 case N_Procedure_Call_Statement:
7342 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE,
7343 false, false, false);
7344 break;
7346 /************************/
7347 /* Chapter 7: Packages */
7348 /************************/
7350 case N_Package_Declaration:
7351 gnu_result = gnat_to_gnu (Specification (gnat_node));
7352 break;
7354 case N_Package_Specification:
7356 start_stmt_group ();
7357 process_decls (Visible_Declarations (gnat_node),
7358 Private_Declarations (gnat_node), Empty, true, true);
7359 gnu_result = end_stmt_group ();
7360 break;
7362 case N_Package_Body:
7364 /* If this is the body of a generic package - do nothing. */
7365 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
7367 gnu_result = alloc_stmt_list ();
7368 break;
7371 start_stmt_group ();
7372 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
7374 if (Present (Handled_Statement_Sequence (gnat_node)))
7375 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
7377 gnu_result = end_stmt_group ();
7378 break;
7380 /********************************/
7381 /* Chapter 8: Visibility Rules */
7382 /********************************/
7384 case N_Use_Package_Clause:
7385 case N_Use_Type_Clause:
7386 /* Nothing to do here - but these may appear in list of declarations. */
7387 gnu_result = alloc_stmt_list ();
7388 break;
7390 /*********************/
7391 /* Chapter 9: Tasks */
7392 /*********************/
7394 case N_Protected_Type_Declaration:
7395 gnu_result = alloc_stmt_list ();
7396 break;
7398 case N_Single_Task_Declaration:
7399 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true);
7400 gnu_result = alloc_stmt_list ();
7401 break;
7403 /*********************************************************/
7404 /* Chapter 10: Program Structure and Compilation Issues */
7405 /*********************************************************/
7407 case N_Compilation_Unit:
7408 /* This is not called for the main unit on which gigi is invoked. */
7409 Compilation_Unit_to_gnu (gnat_node);
7410 gnu_result = alloc_stmt_list ();
7411 break;
7413 case N_Subunit:
7414 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
7415 break;
7417 case N_Entry_Body:
7418 case N_Protected_Body:
7419 case N_Task_Body:
7420 /* These nodes should only be present when annotating types. */
7421 gcc_assert (type_annotate_only);
7422 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
7423 gnu_result = alloc_stmt_list ();
7424 break;
7426 case N_Subprogram_Body_Stub:
7427 case N_Package_Body_Stub:
7428 case N_Protected_Body_Stub:
7429 case N_Task_Body_Stub:
7430 /* Simply process whatever unit is being inserted. */
7431 if (Present (Library_Unit (gnat_node)))
7432 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
7433 else
7435 gcc_assert (type_annotate_only);
7436 gnu_result = alloc_stmt_list ();
7438 break;
7440 /***************************/
7441 /* Chapter 11: Exceptions */
7442 /***************************/
7444 case N_Handled_Sequence_Of_Statements:
7445 /* If there is an At_End procedure attached to this node, and the EH
7446 mechanism is front-end, we must have at least a corresponding At_End
7447 handler, unless the No_Exception_Handlers restriction is set. */
7448 gcc_assert (type_annotate_only
7449 || !Front_End_Exceptions ()
7450 || No (At_End_Proc (gnat_node))
7451 || Present (Exception_Handlers (gnat_node))
7452 || No_Exception_Handlers_Set ());
7454 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
7455 break;
7457 case N_Exception_Handler:
7458 if (Exception_Mechanism == Front_End_SJLJ)
7459 gnu_result = Exception_Handler_to_gnu_fe_sjlj (gnat_node);
7460 else if (Back_End_Exceptions ())
7461 gnu_result = Exception_Handler_to_gnu_gcc (gnat_node);
7462 else
7463 gcc_unreachable ();
7464 break;
7466 case N_Raise_Statement:
7467 /* Only for reraise in back-end exceptions mode. */
7468 gcc_assert (No (Name (gnat_node))
7469 && Back_End_Exceptions ());
7471 start_stmt_group ();
7472 gnat_pushlevel ();
7474 /* Clear the current exception pointer so that the occurrence won't be
7475 deallocated. */
7476 gnu_expr = create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE,
7477 ptr_type_node, gnu_incoming_exc_ptr,
7478 false, false, false, false, false,
7479 true, true, NULL, gnat_node);
7481 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_incoming_exc_ptr,
7482 build_int_cst (ptr_type_node, 0)));
7483 add_stmt (build_call_n_expr (reraise_zcx_decl, 1, gnu_expr));
7484 gnat_poplevel ();
7485 gnu_result = end_stmt_group ();
7486 break;
7488 case N_Push_Constraint_Error_Label:
7489 push_exception_label_stack (&gnu_constraint_error_label_stack,
7490 Exception_Label (gnat_node));
7491 break;
7493 case N_Push_Storage_Error_Label:
7494 push_exception_label_stack (&gnu_storage_error_label_stack,
7495 Exception_Label (gnat_node));
7496 break;
7498 case N_Push_Program_Error_Label:
7499 push_exception_label_stack (&gnu_program_error_label_stack,
7500 Exception_Label (gnat_node));
7501 break;
7503 case N_Pop_Constraint_Error_Label:
7504 gnu_constraint_error_label_stack->pop ();
7505 break;
7507 case N_Pop_Storage_Error_Label:
7508 gnu_storage_error_label_stack->pop ();
7509 break;
7511 case N_Pop_Program_Error_Label:
7512 gnu_program_error_label_stack->pop ();
7513 break;
7515 /******************************/
7516 /* Chapter 12: Generic Units */
7517 /******************************/
7519 case N_Generic_Function_Renaming_Declaration:
7520 case N_Generic_Package_Renaming_Declaration:
7521 case N_Generic_Procedure_Renaming_Declaration:
7522 case N_Generic_Package_Declaration:
7523 case N_Generic_Subprogram_Declaration:
7524 case N_Package_Instantiation:
7525 case N_Procedure_Instantiation:
7526 case N_Function_Instantiation:
7527 /* These nodes can appear on a declaration list but there is nothing to
7528 to be done with them. */
7529 gnu_result = alloc_stmt_list ();
7530 break;
7532 /**************************************************/
7533 /* Chapter 13: Representation Clauses and */
7534 /* Implementation-Dependent Features */
7535 /**************************************************/
7537 case N_Attribute_Definition_Clause:
7538 gnu_result = alloc_stmt_list ();
7540 /* The only one we need to deal with is 'Address since, for the others,
7541 the front-end puts the information elsewhere. */
7542 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
7543 break;
7545 /* And we only deal with 'Address if the object has a Freeze node. */
7546 gnat_temp = Entity (Name (gnat_node));
7547 if (No (Freeze_Node (gnat_temp)))
7548 break;
7550 /* Get the value to use as the address and save it as the equivalent
7551 for the object. When it is frozen, gnat_to_gnu_entity will do the
7552 right thing. */
7553 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
7554 break;
7556 case N_Enumeration_Representation_Clause:
7557 case N_Record_Representation_Clause:
7558 case N_At_Clause:
7559 /* We do nothing with these. SEM puts the information elsewhere. */
7560 gnu_result = alloc_stmt_list ();
7561 break;
7563 case N_Code_Statement:
7564 if (!type_annotate_only)
7566 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
7567 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
7568 tree gnu_clobbers = NULL_TREE, tail;
7569 bool allows_mem, allows_reg, fake;
7570 int ninputs, noutputs, i;
7571 const char **oconstraints;
7572 const char *constraint;
7573 char *clobber;
7575 /* First retrieve the 3 operand lists built by the front-end. */
7576 Setup_Asm_Outputs (gnat_node);
7577 while (Present (gnat_temp = Asm_Output_Variable ()))
7579 tree gnu_value = gnat_to_gnu (gnat_temp);
7580 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
7581 (Asm_Output_Constraint ()));
7583 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
7584 Next_Asm_Output ();
7587 Setup_Asm_Inputs (gnat_node);
7588 while (Present (gnat_temp = Asm_Input_Value ()))
7590 tree gnu_value = gnat_to_gnu (gnat_temp);
7591 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
7592 (Asm_Input_Constraint ()));
7594 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
7595 Next_Asm_Input ();
7598 Clobber_Setup (gnat_node);
7599 while ((clobber = Clobber_Get_Next ()))
7600 gnu_clobbers
7601 = tree_cons (NULL_TREE,
7602 build_string (strlen (clobber) + 1, clobber),
7603 gnu_clobbers);
7605 /* Then perform some standard checking and processing on the
7606 operands. In particular, mark them addressable if needed. */
7607 gnu_outputs = nreverse (gnu_outputs);
7608 noutputs = list_length (gnu_outputs);
7609 gnu_inputs = nreverse (gnu_inputs);
7610 ninputs = list_length (gnu_inputs);
7611 oconstraints = XALLOCAVEC (const char *, noutputs);
7613 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
7615 tree output = TREE_VALUE (tail);
7616 constraint
7617 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7618 oconstraints[i] = constraint;
7620 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7621 &allows_mem, &allows_reg, &fake))
7623 /* If the operand is going to end up in memory,
7624 mark it addressable. Note that we don't test
7625 allows_mem like in the input case below; this
7626 is modelled on the C front-end. */
7627 if (!allows_reg)
7629 output = remove_conversions (output, false);
7630 if (TREE_CODE (output) == CONST_DECL
7631 && DECL_CONST_CORRESPONDING_VAR (output))
7632 output = DECL_CONST_CORRESPONDING_VAR (output);
7633 if (!gnat_mark_addressable (output))
7634 output = error_mark_node;
7637 else
7638 output = error_mark_node;
7640 TREE_VALUE (tail) = output;
7643 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
7645 tree input = TREE_VALUE (tail);
7646 constraint
7647 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7649 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
7650 0, oconstraints,
7651 &allows_mem, &allows_reg))
7653 /* If the operand is going to end up in memory,
7654 mark it addressable. */
7655 if (!allows_reg && allows_mem)
7657 input = remove_conversions (input, false);
7658 if (TREE_CODE (input) == CONST_DECL
7659 && DECL_CONST_CORRESPONDING_VAR (input))
7660 input = DECL_CONST_CORRESPONDING_VAR (input);
7661 if (!gnat_mark_addressable (input))
7662 input = error_mark_node;
7665 else
7666 input = error_mark_node;
7668 TREE_VALUE (tail) = input;
7671 gnu_result = build5 (ASM_EXPR, void_type_node,
7672 gnu_template, gnu_outputs,
7673 gnu_inputs, gnu_clobbers, NULL_TREE);
7674 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
7676 else
7677 gnu_result = alloc_stmt_list ();
7679 break;
7681 /****************/
7682 /* Added Nodes */
7683 /****************/
7685 case N_Expression_With_Actions:
7686 /* This construct doesn't define a scope so we don't push a binding
7687 level around the statement list, but we wrap it in a SAVE_EXPR to
7688 protect it from unsharing. Elaborate the expression as part of the
7689 same statement group as the actions so that the type declaration
7690 gets inserted there as well. This ensures that the type elaboration
7691 code is issued past the actions computing values on which it might
7692 depend. */
7693 start_stmt_group ();
7694 add_stmt_list (Actions (gnat_node));
7695 gnu_expr = gnat_to_gnu (Expression (gnat_node));
7696 gnu_result = end_stmt_group ();
7698 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
7699 TREE_SIDE_EFFECTS (gnu_result) = 1;
7701 gnu_result
7702 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
7703 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7704 break;
7706 case N_Freeze_Entity:
7707 start_stmt_group ();
7708 process_freeze_entity (gnat_node);
7709 process_decls (Actions (gnat_node), Empty, Empty, true, true);
7710 gnu_result = end_stmt_group ();
7711 break;
7713 case N_Freeze_Generic_Entity:
7714 gnu_result = alloc_stmt_list ();
7715 break;
7717 case N_Itype_Reference:
7718 if (!present_gnu_tree (Itype (gnat_node)))
7719 process_type (Itype (gnat_node));
7720 gnu_result = alloc_stmt_list ();
7721 break;
7723 case N_Free_Statement:
7724 if (!type_annotate_only)
7726 tree gnu_ptr
7727 = gnat_to_gnu (adjust_for_implicit_deref (Expression (gnat_node)));
7728 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
7729 tree gnu_obj_type, gnu_actual_obj_type;
7731 /* If this is a thin pointer, we must first dereference it to create
7732 a fat pointer, then go back below to a thin pointer. The reason
7733 for this is that we need to have a fat pointer someplace in order
7734 to properly compute the size. */
7735 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7736 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
7737 build_unary_op (INDIRECT_REF, NULL_TREE,
7738 gnu_ptr));
7740 /* If this is a fat pointer, the object must have been allocated with
7741 the template in front of the array. So pass the template address,
7742 and get the total size; do it by converting to a thin pointer. */
7743 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
7744 gnu_ptr
7745 = convert (build_pointer_type
7746 (TYPE_OBJECT_RECORD_TYPE
7747 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
7748 gnu_ptr);
7750 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
7752 /* If this is a thin pointer, the object must have been allocated with
7753 the template in front of the array. So pass the template address,
7754 and get the total size. */
7755 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7756 gnu_ptr
7757 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
7758 gnu_ptr,
7759 fold_build1 (NEGATE_EXPR, sizetype,
7760 byte_position
7761 (DECL_CHAIN
7762 TYPE_FIELDS ((gnu_obj_type)))));
7764 /* If we have a special dynamic constrained subtype on the node, use
7765 it to compute the size; otherwise, use the designated subtype. */
7766 if (Present (Actual_Designated_Subtype (gnat_node)))
7768 gnu_actual_obj_type
7769 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
7771 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
7772 gnu_actual_obj_type
7773 = build_unc_object_type_from_ptr (gnu_ptr_type,
7774 gnu_actual_obj_type,
7775 get_identifier ("DEALLOC"),
7776 false);
7778 else
7779 gnu_actual_obj_type = gnu_obj_type;
7781 tree gnu_size = TYPE_SIZE_UNIT (gnu_actual_obj_type);
7782 gnu_size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, gnu_ptr);
7784 gnu_result
7785 = build_call_alloc_dealloc (gnu_ptr, gnu_size, gnu_obj_type,
7786 Procedure_To_Call (gnat_node),
7787 Storage_Pool (gnat_node),
7788 gnat_node);
7790 break;
7792 case N_Raise_Constraint_Error:
7793 case N_Raise_Program_Error:
7794 case N_Raise_Storage_Error:
7795 if (type_annotate_only)
7796 gnu_result = alloc_stmt_list ();
7797 else
7798 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
7799 break;
7801 case N_Validate_Unchecked_Conversion:
7802 /* The only validation we currently do on an unchecked conversion is
7803 that of aliasing assumptions. */
7804 if (flag_strict_aliasing)
7805 gnat_validate_uc_list.safe_push (gnat_node);
7806 gnu_result = alloc_stmt_list ();
7807 break;
7809 case N_Function_Specification:
7810 case N_Procedure_Specification:
7811 case N_Op_Concat:
7812 case N_Component_Association:
7813 /* These nodes should only be present when annotating types. */
7814 gcc_assert (type_annotate_only);
7815 gnu_result = alloc_stmt_list ();
7816 break;
7818 default:
7819 /* Other nodes are not supposed to reach here. */
7820 gcc_unreachable ();
7823 /* If we pushed the processing of the elaboration routine, pop it back. */
7824 if (went_into_elab_proc)
7825 current_function_decl = NULL_TREE;
7827 /* When not optimizing, turn boolean rvalues B into B != false tests
7828 so that we can put the location information of the reference to B on
7829 the inequality operator for better debug info. */
7830 if (!optimize
7831 && TREE_CODE (gnu_result) != INTEGER_CST
7832 && TREE_CODE (gnu_result) != TYPE_DECL
7833 && (kind == N_Identifier
7834 || kind == N_Expanded_Name
7835 || kind == N_Explicit_Dereference
7836 || kind == N_Indexed_Component
7837 || kind == N_Selected_Component)
7838 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
7839 && !lvalue_required_p (gnat_node, gnu_result_type, false, false, false))
7841 gnu_result
7842 = build_binary_op (NE_EXPR, gnu_result_type,
7843 convert (gnu_result_type, gnu_result),
7844 convert (gnu_result_type, boolean_false_node));
7845 if (TREE_CODE (gnu_result) != INTEGER_CST)
7846 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7849 /* Set the location information on the result if it's not a simple name.
7850 Note that we may have no result if we tried to build a CALL_EXPR node
7851 to a procedure with no side-effects and optimization is enabled. */
7852 else if (kind != N_Identifier && gnu_result && EXPR_P (gnu_result))
7853 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7855 /* If we're supposed to return something of void_type, it means we have
7856 something we're elaborating for effect, so just return. */
7857 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
7858 return gnu_result;
7860 /* If the result is a constant that overflowed, raise Constraint_Error. */
7861 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
7863 post_error ("?`Constraint_Error` will be raised at run time", gnat_node);
7864 gnu_result
7865 = build1 (NULL_EXPR, gnu_result_type,
7866 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
7867 N_Raise_Constraint_Error));
7870 /* If the result has side-effects and is of an unconstrained type, protect
7871 the expression in case it will be referenced multiple times, i.e. for
7872 its value and to compute the size of an object. But do it neither for
7873 an object nor a renaming declaration, nor a return statement of a call
7874 to a function that returns an unconstrained record type with default
7875 discriminant, because there is no size to be computed in these cases
7876 and this will create a useless temporary. We must do this before any
7877 conversions. */
7878 if (TREE_SIDE_EFFECTS (gnu_result)
7879 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
7880 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
7881 && !(TREE_CODE (gnu_result) == CALL_EXPR
7882 && type_is_padding_self_referential (TREE_TYPE (gnu_result))
7883 && (Nkind (Parent (gnat_node)) == N_Object_Declaration
7884 || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration
7885 || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement)))
7886 gnu_result = gnat_protect_expr (gnu_result);
7888 /* Now convert the result to the result type, unless we are in one of the
7889 following cases:
7891 1. If this is the LHS of an assignment or an actual parameter of a
7892 call, return the result almost unmodified since the RHS will have
7893 to be converted to our type in that case, unless the result type
7894 has a simpler size. Likewise if there is just a no-op unchecked
7895 conversion in-between. Similarly, don't convert integral types
7896 that are the operands of an unchecked conversion since we need
7897 to ignore those conversions (for 'Valid).
7899 2. If we have a label (which doesn't have any well-defined type), a
7900 field or an error, return the result almost unmodified. Similarly,
7901 if the two types are record types with the same name, don't convert.
7902 This will be the case when we are converting from a packable version
7903 of a type to its original type and we need those conversions to be
7904 NOPs in order for assignments into these types to work properly.
7906 3. If the type is void or if we have no result, return error_mark_node
7907 to show we have no result.
7909 4. If this is a call to a function that returns with variable size and
7910 the call is used as the expression in either an object or a renaming
7911 declaration, return the result unmodified because we want to use the
7912 return slot optimization in this case.
7914 5. Finally, if the type of the result is already correct. */
7916 if (Present (Parent (gnat_node))
7917 && (lhs_or_actual_p (gnat_node)
7918 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7919 && unchecked_conversion_nop (Parent (gnat_node)))
7920 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7921 && !AGGREGATE_TYPE_P (gnu_result_type)
7922 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
7923 && !(TYPE_SIZE (gnu_result_type)
7924 && TYPE_SIZE (TREE_TYPE (gnu_result))
7925 && (AGGREGATE_TYPE_P (gnu_result_type)
7926 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
7927 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
7928 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
7929 != INTEGER_CST))
7930 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
7931 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
7932 && (CONTAINS_PLACEHOLDER_P
7933 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
7934 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
7935 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
7937 /* Remove padding only if the inner object is of self-referential
7938 size: in that case it must be an object of unconstrained type
7939 with a default discriminant and we want to avoid copying too
7940 much data. */
7941 if (type_is_padding_self_referential (TREE_TYPE (gnu_result)))
7942 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7943 gnu_result);
7946 else if (TREE_CODE (gnu_result) == LABEL_DECL
7947 || TREE_CODE (gnu_result) == FIELD_DECL
7948 || TREE_CODE (gnu_result) == ERROR_MARK
7949 || (TYPE_NAME (gnu_result_type)
7950 == TYPE_NAME (TREE_TYPE (gnu_result))
7951 && TREE_CODE (gnu_result_type) == RECORD_TYPE
7952 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
7954 /* Remove any padding. */
7955 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7956 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7957 gnu_result);
7960 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
7961 gnu_result = error_mark_node;
7963 else if (Present (Parent (gnat_node))
7964 && (Nkind (Parent (gnat_node)) == N_Object_Declaration
7965 || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration)
7966 && TREE_CODE (gnu_result) == CALL_EXPR
7967 && return_type_with_variable_size_p (TREE_TYPE (gnu_result)))
7970 else if (TREE_TYPE (gnu_result) != gnu_result_type)
7971 gnu_result = convert (gnu_result_type, gnu_result);
7973 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
7974 while ((TREE_CODE (gnu_result) == NOP_EXPR
7975 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
7976 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
7977 gnu_result = TREE_OPERAND (gnu_result, 0);
7979 return gnu_result;
7982 /* Similar to gnat_to_gnu, but discard any object that might be created in
7983 the course of the translation of GNAT_NODE, which must be an "external"
7984 expression in the sense that it will be elaborated elsewhere. */
7986 tree
7987 gnat_to_gnu_external (Node_Id gnat_node)
7989 const int save_force_global = force_global;
7990 bool went_into_elab_proc = false;
7992 /* Force the local context and create a fake scope that we zap
7993 at the end so declarations will not be stuck either in the
7994 global varpool or in the current scope. */
7995 if (!current_function_decl)
7997 current_function_decl = get_elaboration_procedure ();
7998 went_into_elab_proc = true;
8000 force_global = 0;
8001 gnat_pushlevel ();
8003 tree gnu_result = gnat_to_gnu (gnat_node);
8005 gnat_zaplevel ();
8006 force_global = save_force_global;
8007 if (went_into_elab_proc)
8008 current_function_decl = NULL_TREE;
8010 /* Do not import locations from external units. */
8011 if (gnu_result && EXPR_P (gnu_result))
8012 SET_EXPR_LOCATION (gnu_result, UNKNOWN_LOCATION);
8014 return gnu_result;
8017 /* Subroutine of above to push the exception label stack. GNU_STACK is
8018 a pointer to the stack to update and GNAT_LABEL, if present, is the
8019 label to push onto the stack. */
8021 static void
8022 push_exception_label_stack (vec<tree, va_gc> **gnu_stack, Entity_Id gnat_label)
8024 tree gnu_label = (Present (gnat_label)
8025 ? gnat_to_gnu_entity (gnat_label, NULL_TREE, false)
8026 : NULL_TREE);
8028 vec_safe_push (*gnu_stack, gnu_label);
8031 /* Return true if the statement list STMT_LIST is empty. */
8033 static bool
8034 empty_stmt_list_p (tree stmt_list)
8036 tree_stmt_iterator tsi;
8038 for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi))
8040 tree stmt = tsi_stmt (tsi);
8042 /* Anything else than an empty STMT_STMT counts as something. */
8043 if (TREE_CODE (stmt) != STMT_STMT || STMT_STMT_STMT (stmt))
8044 return false;
8047 return true;
8050 /* Record the current code position in GNAT_NODE. */
8052 static void
8053 record_code_position (Node_Id gnat_node)
8055 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
8057 add_stmt_with_node (stmt_stmt, gnat_node);
8058 save_gnu_tree (gnat_node, stmt_stmt, true);
8061 /* Insert the code for GNAT_NODE at the position saved for that node. */
8063 static void
8064 insert_code_for (Node_Id gnat_node)
8066 tree code = gnat_to_gnu (gnat_node);
8068 /* It's too late to remove the STMT_STMT itself at this point. */
8069 if (!empty_stmt_list_p (code))
8070 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = code;
8072 save_gnu_tree (gnat_node, NULL_TREE, true);
8075 /* Start a new statement group chained to the previous group. */
8077 void
8078 start_stmt_group (void)
8080 struct stmt_group *group = stmt_group_free_list;
8082 /* First see if we can get one from the free list. */
8083 if (group)
8084 stmt_group_free_list = group->previous;
8085 else
8086 group = ggc_alloc<stmt_group> ();
8088 group->previous = current_stmt_group;
8089 group->stmt_list = group->block = group->cleanups = NULL_TREE;
8090 current_stmt_group = group;
8093 /* Add GNU_STMT to the current statement group. If it is an expression with
8094 no effects, it is ignored. */
8096 void
8097 add_stmt (tree gnu_stmt)
8099 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
8102 /* Similar, but the statement is always added, regardless of side-effects. */
8104 void
8105 add_stmt_force (tree gnu_stmt)
8107 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
8110 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
8112 void
8113 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
8115 /* Do not emit a location for renamings that come from generic instantiation,
8116 they are likely to disturb debugging. */
8117 if (Present (gnat_node) && !renaming_from_instantiation_p (gnat_node))
8118 set_expr_location_from_node (gnu_stmt, gnat_node);
8119 add_stmt (gnu_stmt);
8122 /* Similar, but the statement is always added, regardless of side-effects. */
8124 void
8125 add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
8127 if (Present (gnat_node))
8128 set_expr_location_from_node (gnu_stmt, gnat_node);
8129 add_stmt_force (gnu_stmt);
8132 /* Add a declaration statement for GNU_DECL to the current statement group.
8133 Get SLOC from Entity_Id. */
8135 void
8136 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
8138 tree type = TREE_TYPE (gnu_decl);
8139 tree gnu_stmt, gnu_init;
8141 /* If this is a variable that Gigi is to ignore, we may have been given
8142 an ERROR_MARK. So test for it. We also might have been given a
8143 reference for a renaming. So only do something for a decl. Also
8144 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
8145 if (!DECL_P (gnu_decl)
8146 || (TREE_CODE (gnu_decl) == TYPE_DECL
8147 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
8148 return;
8150 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
8152 /* If we are external or global, we don't want to output the DECL_EXPR for
8153 this DECL node since we already have evaluated the expressions in the
8154 sizes and positions as globals and doing it again would be wrong. */
8155 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
8157 /* Mark everything as used to prevent node sharing with subprograms.
8158 Note that walk_tree knows how to deal with TYPE_DECL, but neither
8159 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
8160 MARK_VISITED (gnu_stmt);
8161 if (TREE_CODE (gnu_decl) == VAR_DECL
8162 || TREE_CODE (gnu_decl) == CONST_DECL)
8164 MARK_VISITED (DECL_SIZE (gnu_decl));
8165 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
8166 MARK_VISITED (DECL_INITIAL (gnu_decl));
8168 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
8169 else if (TREE_CODE (gnu_decl) == TYPE_DECL
8170 && RECORD_OR_UNION_TYPE_P (type)
8171 && !TYPE_FAT_POINTER_P (type))
8172 MARK_VISITED (TYPE_ADA_SIZE (type));
8174 else
8175 add_stmt_with_node (gnu_stmt, gnat_entity);
8177 /* If this is a variable and an initializer is attached to it, it must be
8178 valid for the context. Similar to init_const in create_var_decl. */
8179 if (TREE_CODE (gnu_decl) == VAR_DECL
8180 && (gnu_init = DECL_INITIAL (gnu_decl))
8181 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
8182 || (TREE_STATIC (gnu_decl)
8183 && !initializer_constant_valid_p (gnu_init,
8184 TREE_TYPE (gnu_init)))))
8186 DECL_INITIAL (gnu_decl) = NULL_TREE;
8187 if (TREE_READONLY (gnu_decl))
8189 TREE_READONLY (gnu_decl) = 0;
8190 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
8193 /* If GNU_DECL has a padded type, convert it to the unpadded
8194 type so the assignment is done properly. */
8195 if (TYPE_IS_PADDING_P (type))
8196 gnu_decl = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
8198 gnu_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_decl, gnu_init);
8199 add_stmt_with_node (gnu_stmt, gnat_entity);
8203 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
8205 static tree
8206 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
8208 tree t = *tp;
8210 if (TREE_VISITED (t))
8211 *walk_subtrees = 0;
8213 /* Don't mark a dummy type as visited because we want to mark its sizes
8214 and fields once it's filled in. */
8215 else if (!TYPE_IS_DUMMY_P (t))
8216 TREE_VISITED (t) = 1;
8218 if (TYPE_P (t))
8219 TYPE_SIZES_GIMPLIFIED (t) = 1;
8221 return NULL_TREE;
8224 /* Mark nodes rooted at T with TREE_VISITED and types as having their
8225 sized gimplified. We use this to indicate all variable sizes and
8226 positions in global types may not be shared by any subprogram. */
8228 void
8229 mark_visited (tree t)
8231 walk_tree (&t, mark_visited_r, NULL, NULL);
8234 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
8235 set its location to that of GNAT_NODE if present, but with column info
8236 cleared so that conditional branches generated as part of the cleanup
8237 code do not interfere with coverage analysis tools. */
8239 static void
8240 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
8242 if (Present (gnat_node))
8243 set_expr_location_from_node (gnu_cleanup, gnat_node, true);
8244 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
8247 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
8249 void
8250 set_block_for_group (tree gnu_block)
8252 gcc_assert (!current_stmt_group->block);
8253 current_stmt_group->block = gnu_block;
8256 /* Return code corresponding to the current code group. It is normally
8257 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
8258 BLOCK or cleanups were set. */
8260 tree
8261 end_stmt_group (void)
8263 struct stmt_group *group = current_stmt_group;
8264 tree gnu_retval = group->stmt_list;
8266 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
8267 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
8268 make a BIND_EXPR. Note that we nest in that because the cleanup may
8269 reference variables in the block. */
8270 if (!gnu_retval)
8271 gnu_retval = alloc_stmt_list ();
8273 if (group->cleanups)
8274 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
8275 group->cleanups);
8277 if (current_stmt_group->block)
8278 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
8279 gnu_retval, group->block);
8281 /* Remove this group from the stack and add it to the free list. */
8282 current_stmt_group = group->previous;
8283 group->previous = stmt_group_free_list;
8284 stmt_group_free_list = group;
8286 return gnu_retval;
8289 /* Return whether the current statement group may fall through. */
8291 static inline bool
8292 stmt_group_may_fallthru (void)
8294 if (current_stmt_group->stmt_list)
8295 return block_may_fallthru (current_stmt_group->stmt_list);
8296 else
8297 return true;
8300 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
8301 statements.*/
8303 static void
8304 add_stmt_list (List_Id gnat_list)
8306 Node_Id gnat_node;
8308 if (Present (gnat_list))
8309 for (gnat_node = First (gnat_list); Present (gnat_node);
8310 gnat_node = Next (gnat_node))
8311 add_stmt (gnat_to_gnu (gnat_node));
8314 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
8315 If BINDING_P is true, push and pop a binding level around the list. */
8317 static tree
8318 build_stmt_group (List_Id gnat_list, bool binding_p)
8320 start_stmt_group ();
8322 if (binding_p)
8323 gnat_pushlevel ();
8325 add_stmt_list (gnat_list);
8327 if (binding_p)
8328 gnat_poplevel ();
8330 return end_stmt_group ();
8333 /* Generate GIMPLE in place for the expression at *EXPR_P. */
8336 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
8337 gimple_seq *post_p ATTRIBUTE_UNUSED)
8339 tree expr = *expr_p;
8340 tree type = TREE_TYPE (expr);
8341 tree op;
8343 if (IS_ADA_STMT (expr))
8344 return gnat_gimplify_stmt (expr_p);
8346 switch (TREE_CODE (expr))
8348 case NULL_EXPR:
8349 /* If this is an aggregate type, build a null pointer of the appropriate
8350 type and dereference it. */
8351 if (AGGREGATE_TYPE_P (type)
8352 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
8353 *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE,
8354 convert (build_pointer_type (type),
8355 integer_zero_node));
8356 /* Otherwise, just make a VAR_DECL. */
8357 else
8359 *expr_p = create_tmp_var (type, NULL);
8360 TREE_NO_WARNING (*expr_p) = 1;
8363 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
8364 return GS_OK;
8366 case UNCONSTRAINED_ARRAY_REF:
8367 /* We should only do this if we are just elaborating for side-effects,
8368 but we can't know that yet. */
8369 *expr_p = TREE_OPERAND (*expr_p, 0);
8370 return GS_OK;
8372 case ADDR_EXPR:
8373 op = TREE_OPERAND (expr, 0);
8375 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
8376 is put into static memory. We know that it's going to be read-only
8377 given the semantics we have and it must be in static memory when the
8378 reference is in an elaboration procedure. */
8379 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
8381 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
8382 *expr_p = fold_convert (type, addr);
8383 return GS_ALL_DONE;
8386 /* Replace atomic loads with their first argument. That's necessary
8387 because the gimplifier would create a temporary otherwise. */
8388 if (TREE_SIDE_EFFECTS (op))
8389 while (handled_component_p (op) || CONVERT_EXPR_P (op))
8391 tree inner = TREE_OPERAND (op, 0);
8392 if (TREE_CODE (inner) == CALL_EXPR && call_is_atomic_load (inner))
8394 tree t = CALL_EXPR_ARG (inner, 0);
8395 if (TREE_CODE (t) == NOP_EXPR)
8396 t = TREE_OPERAND (t, 0);
8397 if (TREE_CODE (t) == ADDR_EXPR)
8398 TREE_OPERAND (op, 0) = TREE_OPERAND (t, 0);
8399 else
8400 TREE_OPERAND (op, 0) = build_fold_indirect_ref (t);
8402 else
8403 op = inner;
8406 return GS_UNHANDLED;
8408 case VIEW_CONVERT_EXPR:
8409 op = TREE_OPERAND (expr, 0);
8411 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
8412 type to a scalar one, explicitly create the local temporary. That's
8413 required if the type is passed by reference. */
8414 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
8415 && AGGREGATE_TYPE_P (TREE_TYPE (op))
8416 && !AGGREGATE_TYPE_P (type))
8418 tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
8419 gimple_add_tmp_var (new_var);
8421 mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
8422 gimplify_and_add (mod, pre_p);
8424 TREE_OPERAND (expr, 0) = new_var;
8425 return GS_OK;
8428 return GS_UNHANDLED;
8430 case DECL_EXPR:
8431 op = DECL_EXPR_DECL (expr);
8433 /* The expressions for the RM bounds must be gimplified to ensure that
8434 they are properly elaborated. See gimplify_decl_expr. */
8435 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
8436 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)))
8437 switch (TREE_CODE (TREE_TYPE (op)))
8439 case INTEGER_TYPE:
8440 case ENUMERAL_TYPE:
8441 case BOOLEAN_TYPE:
8442 case REAL_TYPE:
8444 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
8446 val = TYPE_RM_MIN_VALUE (type);
8447 if (val)
8449 gimplify_one_sizepos (&val, pre_p);
8450 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
8451 SET_TYPE_RM_MIN_VALUE (t, val);
8454 val = TYPE_RM_MAX_VALUE (type);
8455 if (val)
8457 gimplify_one_sizepos (&val, pre_p);
8458 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
8459 SET_TYPE_RM_MAX_VALUE (t, val);
8463 break;
8465 default:
8466 break;
8469 /* ... fall through ... */
8471 default:
8472 return GS_UNHANDLED;
8476 /* Generate GIMPLE in place for the statement at *STMT_P. */
8478 static enum gimplify_status
8479 gnat_gimplify_stmt (tree *stmt_p)
8481 tree stmt = *stmt_p;
8483 switch (TREE_CODE (stmt))
8485 case STMT_STMT:
8486 *stmt_p = STMT_STMT_STMT (stmt);
8487 return GS_OK;
8489 case LOOP_STMT:
8491 tree gnu_start_label = create_artificial_label (input_location);
8492 tree gnu_cond = LOOP_STMT_COND (stmt);
8493 tree gnu_update = LOOP_STMT_UPDATE (stmt);
8494 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
8496 /* Build the condition expression from the test, if any. */
8497 if (gnu_cond)
8499 /* Deal with the optimization hints. */
8500 if (LOOP_STMT_IVDEP (stmt))
8501 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8502 build_int_cst (integer_type_node,
8503 annot_expr_ivdep_kind));
8504 if (LOOP_STMT_NO_VECTOR (stmt))
8505 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8506 build_int_cst (integer_type_node,
8507 annot_expr_no_vector_kind));
8508 if (LOOP_STMT_VECTOR (stmt))
8509 gnu_cond = build2 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8510 build_int_cst (integer_type_node,
8511 annot_expr_vector_kind));
8513 gnu_cond
8514 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
8515 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
8518 /* Set to emit the statements of the loop. */
8519 *stmt_p = NULL_TREE;
8521 /* We first emit the start label and then a conditional jump to the
8522 end label if there's a top condition, then the update if it's at
8523 the top, then the body of the loop, then a conditional jump to
8524 the end label if there's a bottom condition, then the update if
8525 it's at the bottom, and finally a jump to the start label and the
8526 definition of the end label. */
8527 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
8528 gnu_start_label),
8529 stmt_p);
8531 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
8532 append_to_statement_list (gnu_cond, stmt_p);
8534 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
8535 append_to_statement_list (gnu_update, stmt_p);
8537 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
8539 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
8540 append_to_statement_list (gnu_cond, stmt_p);
8542 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
8543 append_to_statement_list (gnu_update, stmt_p);
8545 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
8546 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
8547 append_to_statement_list (t, stmt_p);
8549 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
8550 gnu_end_label),
8551 stmt_p);
8552 return GS_OK;
8555 case EXIT_STMT:
8556 /* Build a statement to jump to the corresponding end label, then
8557 see if it needs to be conditional. */
8558 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
8559 if (EXIT_STMT_COND (stmt))
8560 *stmt_p = build3 (COND_EXPR, void_type_node,
8561 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
8562 return GS_OK;
8564 default:
8565 gcc_unreachable ();
8569 /* Force a reference to each of the entities in GNAT_PACKAGE recursively.
8571 This routine is exclusively called in type_annotate mode, to compute DDA
8572 information for types in withed units, for ASIS use. */
8574 static void
8575 elaborate_all_entities_for_package (Entity_Id gnat_package)
8577 Entity_Id gnat_entity;
8579 for (gnat_entity = First_Entity (gnat_package);
8580 Present (gnat_entity);
8581 gnat_entity = Next_Entity (gnat_entity))
8583 const Entity_Kind kind = Ekind (gnat_entity);
8585 /* We are interested only in entities visible from the main unit. */
8586 if (!Is_Public (gnat_entity))
8587 continue;
8589 /* Skip stuff internal to the compiler. */
8590 if (Convention (gnat_entity) == Convention_Intrinsic)
8591 continue;
8592 if (kind == E_Operator)
8593 continue;
8594 if (IN (kind, Subprogram_Kind) && Is_Intrinsic_Subprogram (gnat_entity))
8595 continue;
8596 if (Is_Itype (gnat_entity))
8597 continue;
8599 /* Skip named numbers. */
8600 if (IN (kind, Named_Kind))
8601 continue;
8603 /* Skip generic declarations. */
8604 if (IN (kind, Generic_Unit_Kind))
8605 continue;
8607 /* Skip formal objects. */
8608 if (IN (kind, Formal_Object_Kind))
8609 continue;
8611 /* Skip package bodies. */
8612 if (kind == E_Package_Body)
8613 continue;
8615 /* Skip limited views that point back to the main unit. */
8616 if (IN (kind, Incomplete_Kind)
8617 && From_Limited_With (gnat_entity)
8618 && In_Extended_Main_Code_Unit (Non_Limited_View (gnat_entity)))
8619 continue;
8621 /* Skip types that aren't frozen. */
8622 if (IN (kind, Type_Kind) && !Is_Frozen (gnat_entity))
8623 continue;
8625 /* Recurse on real packages that aren't in the main unit. */
8626 if (kind == E_Package)
8628 if (No (Renamed_Entity (gnat_entity))
8629 && !In_Extended_Main_Code_Unit (gnat_entity))
8630 elaborate_all_entities_for_package (gnat_entity);
8632 else
8633 gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
8637 /* Force a reference to each of the entities in packages withed by GNAT_NODE.
8638 Operate recursively but check that we aren't elaborating something more
8639 than once.
8641 This routine is exclusively called in type_annotate mode, to compute DDA
8642 information for types in withed units, for ASIS use. */
8644 static void
8645 elaborate_all_entities (Node_Id gnat_node)
8647 Entity_Id gnat_with_clause;
8649 /* Process each unit only once. As we trace the context of all relevant
8650 units transitively, including generic bodies, we may encounter the
8651 same generic unit repeatedly. */
8652 if (!present_gnu_tree (gnat_node))
8653 save_gnu_tree (gnat_node, integer_zero_node, true);
8655 /* Save entities in all context units. A body may have an implicit_with
8656 on its own spec, if the context includes a child unit, so don't save
8657 the spec twice. */
8658 for (gnat_with_clause = First (Context_Items (gnat_node));
8659 Present (gnat_with_clause);
8660 gnat_with_clause = Next (gnat_with_clause))
8661 if (Nkind (gnat_with_clause) == N_With_Clause
8662 && !present_gnu_tree (Library_Unit (gnat_with_clause))
8663 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
8665 Node_Id gnat_unit = Library_Unit (gnat_with_clause);
8666 Entity_Id gnat_entity = Entity (Name (gnat_with_clause));
8668 elaborate_all_entities (gnat_unit);
8670 if (Ekind (gnat_entity) == E_Package)
8671 elaborate_all_entities_for_package (gnat_entity);
8673 else if (Ekind (gnat_entity) == E_Generic_Package)
8675 Node_Id gnat_body = Corresponding_Body (Unit (gnat_unit));
8677 /* Retrieve compilation unit node of generic body. */
8678 while (Present (gnat_body)
8679 && Nkind (gnat_body) != N_Compilation_Unit)
8680 gnat_body = Parent (gnat_body);
8682 /* If body is available, elaborate its context. */
8683 if (Present (gnat_body))
8684 elaborate_all_entities (gnat_body);
8688 if (Nkind (Unit (gnat_node)) == N_Package_Body)
8689 elaborate_all_entities (Library_Unit (gnat_node));
8692 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
8694 static void
8695 process_freeze_entity (Node_Id gnat_node)
8697 const Entity_Id gnat_entity = Entity (gnat_node);
8698 const Entity_Kind kind = Ekind (gnat_entity);
8699 tree gnu_old, gnu_new;
8701 /* If this is a package, we need to generate code for the package. */
8702 if (kind == E_Package)
8704 insert_code_for
8705 (Parent (Corresponding_Body
8706 (Parent (Declaration_Node (gnat_entity)))));
8707 return;
8710 /* Don't do anything for class-wide types as they are always transformed
8711 into their root type. */
8712 if (kind == E_Class_Wide_Type)
8713 return;
8715 /* Check for an old definition if this isn't an object with address clause,
8716 since the saved GCC tree is the address expression in that case. */
8717 gnu_old
8718 = present_gnu_tree (gnat_entity) && No (Address_Clause (gnat_entity))
8719 ? get_gnu_tree (gnat_entity) : NULL_TREE;
8721 /* Don't do anything for subprograms that may have been elaborated before
8722 their freeze nodes. This can happen, for example, because of an inner
8723 call in an instance body or because of previous compilation of a spec
8724 for inlining purposes. */
8725 if (gnu_old
8726 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
8727 && (kind == E_Function || kind == E_Procedure))
8728 || (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
8729 && kind == E_Subprogram_Type)))
8730 return;
8732 /* If we have a non-dummy type old tree, we have nothing to do, except for
8733 aborting, since this node was never delayed as it should have been. We
8734 let this happen for concurrent types and their Corresponding_Record_Type,
8735 however, because each might legitimately be elaborated before its own
8736 freeze node, e.g. while processing the other. */
8737 if (gnu_old
8738 && !(TREE_CODE (gnu_old) == TYPE_DECL
8739 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
8741 gcc_assert (Is_Concurrent_Type (gnat_entity)
8742 || (Is_Record_Type (gnat_entity)
8743 && Is_Concurrent_Record_Type (gnat_entity)));
8744 return;
8747 /* Reset the saved tree, if any, and elaborate the object or type for real.
8748 If there is a full view, elaborate it and use the result. And, if this
8749 is the root type of a class-wide type, reuse it for the latter. */
8750 if (gnu_old)
8752 save_gnu_tree (gnat_entity, NULL_TREE, false);
8754 if (Is_Incomplete_Or_Private_Type (gnat_entity)
8755 && Present (Full_View (gnat_entity)))
8757 Entity_Id full_view = Full_View (gnat_entity);
8759 save_gnu_tree (full_view, NULL_TREE, false);
8761 if (Is_Private_Type (full_view)
8762 && Present (Underlying_Full_View (full_view)))
8764 full_view = Underlying_Full_View (full_view);
8765 save_gnu_tree (full_view, NULL_TREE, false);
8769 if (Is_Type (gnat_entity)
8770 && Present (Class_Wide_Type (gnat_entity))
8771 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8772 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
8775 if (Is_Incomplete_Or_Private_Type (gnat_entity)
8776 && Present (Full_View (gnat_entity)))
8778 Entity_Id full_view = Full_View (gnat_entity);
8780 if (Is_Private_Type (full_view)
8781 && Present (Underlying_Full_View (full_view)))
8782 full_view = Underlying_Full_View (full_view);
8784 gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, true);
8786 /* Propagate back-annotations from full view to partial view. */
8787 if (Unknown_Alignment (gnat_entity))
8788 Set_Alignment (gnat_entity, Alignment (full_view));
8790 if (Unknown_Esize (gnat_entity))
8791 Set_Esize (gnat_entity, Esize (full_view));
8793 if (Unknown_RM_Size (gnat_entity))
8794 Set_RM_Size (gnat_entity, RM_Size (full_view));
8796 /* The above call may have defined this entity (the simplest example
8797 of this is when we have a private enumeral type since the bounds
8798 will have the public view). */
8799 if (!present_gnu_tree (gnat_entity))
8800 save_gnu_tree (gnat_entity, gnu_new, false);
8802 else
8804 tree gnu_init
8805 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
8806 && present_gnu_tree (Declaration_Node (gnat_entity)))
8807 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
8809 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, true);
8812 if (Is_Type (gnat_entity)
8813 && Present (Class_Wide_Type (gnat_entity))
8814 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8815 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
8817 /* If we have an old type and we've made pointers to this type, update those
8818 pointers. If this is a Taft amendment type in the main unit, we need to
8819 mark the type as used since other units referencing it don't see the full
8820 declaration and, therefore, cannot mark it as used themselves. */
8821 if (gnu_old)
8823 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8824 TREE_TYPE (gnu_new));
8825 if (TYPE_DUMMY_IN_PROFILE_P (TREE_TYPE (gnu_old)))
8826 update_profiles_with (TREE_TYPE (gnu_old));
8827 if (DECL_TAFT_TYPE_P (gnu_old))
8828 used_types_insert (TREE_TYPE (gnu_new));
8832 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
8833 We make two passes, one to elaborate anything other than bodies (but
8834 we declare a function if there was no spec). The second pass
8835 elaborates the bodies.
8837 GNAT_END_LIST gives the element in the list past the end. Normally,
8838 this is Empty, but can be First_Real_Statement for a
8839 Handled_Sequence_Of_Statements.
8841 We make a complete pass through both lists if PASS1P is true, then make
8842 the second pass over both lists if PASS2P is true. The lists usually
8843 correspond to the public and private parts of a package. */
8845 static void
8846 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
8847 Node_Id gnat_end_list, bool pass1p, bool pass2p)
8849 List_Id gnat_decl_array[2];
8850 Node_Id gnat_decl;
8851 int i;
8853 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
8855 if (pass1p)
8856 for (i = 0; i <= 1; i++)
8857 if (Present (gnat_decl_array[i]))
8858 for (gnat_decl = First (gnat_decl_array[i]);
8859 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8861 /* For package specs, we recurse inside the declarations,
8862 thus taking the two pass approach inside the boundary. */
8863 if (Nkind (gnat_decl) == N_Package_Declaration
8864 && (Nkind (Specification (gnat_decl)
8865 == N_Package_Specification)))
8866 process_decls (Visible_Declarations (Specification (gnat_decl)),
8867 Private_Declarations (Specification (gnat_decl)),
8868 Empty, true, false);
8870 /* Similarly for any declarations in the actions of a
8871 freeze node. */
8872 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8874 process_freeze_entity (gnat_decl);
8875 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
8878 /* Package bodies with freeze nodes get their elaboration deferred
8879 until the freeze node, but the code must be placed in the right
8880 place, so record the code position now. */
8881 else if (Nkind (gnat_decl) == N_Package_Body
8882 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
8883 record_code_position (gnat_decl);
8885 else if (Nkind (gnat_decl) == N_Package_Body_Stub
8886 && Present (Library_Unit (gnat_decl))
8887 && Present (Freeze_Node
8888 (Corresponding_Spec
8889 (Proper_Body (Unit
8890 (Library_Unit (gnat_decl)))))))
8891 record_code_position
8892 (Proper_Body (Unit (Library_Unit (gnat_decl))));
8894 /* We defer most subprogram bodies to the second pass. */
8895 else if (Nkind (gnat_decl) == N_Subprogram_Body)
8897 if (Acts_As_Spec (gnat_decl))
8899 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
8901 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
8902 && Ekind (gnat_subprog_id) != E_Generic_Function)
8903 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, true);
8907 /* For bodies and stubs that act as their own specs, the entity
8908 itself must be elaborated in the first pass, because it may
8909 be used in other declarations. */
8910 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
8912 Node_Id gnat_subprog_id
8913 = Defining_Entity (Specification (gnat_decl));
8915 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
8916 && Ekind (gnat_subprog_id) != E_Generic_Procedure
8917 && Ekind (gnat_subprog_id) != E_Generic_Function)
8918 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, true);
8921 /* Concurrent stubs stand for the corresponding subprogram bodies,
8922 which are deferred like other bodies. */
8923 else if (Nkind (gnat_decl) == N_Task_Body_Stub
8924 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8927 /* Renamed subprograms may not be elaborated yet at this point
8928 since renamings do not trigger freezing. Wait for the second
8929 pass to take care of them. */
8930 else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration)
8933 else
8934 add_stmt (gnat_to_gnu (gnat_decl));
8937 /* Here we elaborate everything we deferred above except for package bodies,
8938 which are elaborated at their freeze nodes. Note that we must also
8939 go inside things (package specs and freeze nodes) the first pass did. */
8940 if (pass2p)
8941 for (i = 0; i <= 1; i++)
8942 if (Present (gnat_decl_array[i]))
8943 for (gnat_decl = First (gnat_decl_array[i]);
8944 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8946 if (Nkind (gnat_decl) == N_Subprogram_Body
8947 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
8948 || Nkind (gnat_decl) == N_Task_Body_Stub
8949 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8950 add_stmt (gnat_to_gnu (gnat_decl));
8952 else if (Nkind (gnat_decl) == N_Package_Declaration
8953 && (Nkind (Specification (gnat_decl)
8954 == N_Package_Specification)))
8955 process_decls (Visible_Declarations (Specification (gnat_decl)),
8956 Private_Declarations (Specification (gnat_decl)),
8957 Empty, false, true);
8959 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8960 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
8962 else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration)
8963 add_stmt (gnat_to_gnu (gnat_decl));
8967 /* Make a unary operation of kind CODE using build_unary_op, but guard
8968 the operation by an overflow check. CODE can be one of NEGATE_EXPR
8969 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
8970 the operation is to be performed in that type. GNAT_NODE is the gnat
8971 node conveying the source location for which the error should be
8972 signaled. */
8974 static tree
8975 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
8976 Node_Id gnat_node)
8978 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
8980 operand = gnat_protect_expr (operand);
8982 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
8983 operand, TYPE_MIN_VALUE (gnu_type)),
8984 build_unary_op (code, gnu_type, operand),
8985 CE_Overflow_Check_Failed, gnat_node);
8988 /* Make a binary operation of kind CODE using build_binary_op, but guard
8989 the operation by an overflow check. CODE can be one of PLUS_EXPR,
8990 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
8991 Usually the operation is to be performed in that type. GNAT_NODE is
8992 the GNAT node conveying the source location for which the error should
8993 be signaled. */
8995 static tree
8996 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
8997 tree right, Node_Id gnat_node)
8999 const unsigned int precision = TYPE_PRECISION (gnu_type);
9000 tree lhs = gnat_protect_expr (left);
9001 tree rhs = gnat_protect_expr (right);
9002 tree type_max = TYPE_MAX_VALUE (gnu_type);
9003 tree type_min = TYPE_MIN_VALUE (gnu_type);
9004 tree gnu_expr, check;
9005 int sgn;
9007 /* Assert that the precision is a power of 2. */
9008 gcc_assert ((precision & (precision - 1)) == 0);
9010 /* Prefer a constant on the RHS to simplify checks. */
9011 if (TREE_CODE (rhs) != INTEGER_CST
9012 && TREE_CODE (lhs) == INTEGER_CST
9013 && (code == PLUS_EXPR || code == MULT_EXPR))
9015 tree tmp = lhs;
9016 lhs = rhs;
9017 rhs = tmp;
9020 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
9022 /* If we can fold the expression to a constant, just return it.
9023 The caller will deal with overflow, no need to generate a check. */
9024 if (TREE_CODE (gnu_expr) == INTEGER_CST)
9025 return gnu_expr;
9027 /* If no operand is a constant, we use the generic implementation. */
9028 if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (rhs) != INTEGER_CST)
9030 /* Never inline a 64-bit mult for a 32-bit target, it's way too long. */
9031 if (code == MULT_EXPR && precision == 64 && BITS_PER_WORD < 64)
9033 tree int64 = gnat_type_for_size (64, 0);
9034 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
9035 convert (int64, lhs),
9036 convert (int64, rhs)));
9039 enum internal_fn icode;
9041 switch (code)
9043 case PLUS_EXPR:
9044 icode = IFN_ADD_OVERFLOW;
9045 break;
9046 case MINUS_EXPR:
9047 icode = IFN_SUB_OVERFLOW;
9048 break;
9049 case MULT_EXPR:
9050 icode = IFN_MUL_OVERFLOW;
9051 break;
9052 default:
9053 gcc_unreachable ();
9056 tree gnu_ctype = build_complex_type (gnu_type);
9057 tree call
9058 = build_call_expr_internal_loc (UNKNOWN_LOCATION, icode, gnu_ctype, 2,
9059 lhs, rhs);
9060 tree tgt = save_expr (call);
9061 gnu_expr = build1 (REALPART_EXPR, gnu_type, tgt);
9062 check = fold_build2 (NE_EXPR, boolean_type_node,
9063 build1 (IMAGPART_EXPR, gnu_type, tgt),
9064 build_int_cst (gnu_type, 0));
9065 return
9066 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
9069 /* If one operand is a constant, we expose the overflow condition to enable
9070 a subsequent simplication or even elimination. */
9071 switch (code)
9073 case PLUS_EXPR:
9074 sgn = tree_int_cst_sgn (rhs);
9075 if (sgn > 0)
9076 /* When rhs > 0, overflow when lhs > type_max - rhs. */
9077 check = build_binary_op (GT_EXPR, boolean_type_node, lhs,
9078 build_binary_op (MINUS_EXPR, gnu_type,
9079 type_max, rhs));
9080 else if (sgn < 0)
9081 /* When rhs < 0, overflow when lhs < type_min - rhs. */
9082 check = build_binary_op (LT_EXPR, boolean_type_node, lhs,
9083 build_binary_op (MINUS_EXPR, gnu_type,
9084 type_min, rhs));
9085 else
9086 return gnu_expr;
9087 break;
9089 case MINUS_EXPR:
9090 if (TREE_CODE (lhs) == INTEGER_CST)
9092 sgn = tree_int_cst_sgn (lhs);
9093 if (sgn > 0)
9094 /* When lhs > 0, overflow when rhs < lhs - type_max. */
9095 check = build_binary_op (LT_EXPR, boolean_type_node, rhs,
9096 build_binary_op (MINUS_EXPR, gnu_type,
9097 lhs, type_max));
9098 else if (sgn < 0)
9099 /* When lhs < 0, overflow when rhs > lhs - type_min. */
9100 check = build_binary_op (GT_EXPR, boolean_type_node, rhs,
9101 build_binary_op (MINUS_EXPR, gnu_type,
9102 lhs, type_min));
9103 else
9104 return gnu_expr;
9106 else
9108 sgn = tree_int_cst_sgn (rhs);
9109 if (sgn > 0)
9110 /* When rhs > 0, overflow when lhs < type_min + rhs. */
9111 check = build_binary_op (LT_EXPR, boolean_type_node, lhs,
9112 build_binary_op (PLUS_EXPR, gnu_type,
9113 type_min, rhs));
9114 else if (sgn < 0)
9115 /* When rhs < 0, overflow when lhs > type_max + rhs. */
9116 check = build_binary_op (GT_EXPR, boolean_type_node, lhs,
9117 build_binary_op (PLUS_EXPR, gnu_type,
9118 type_max, rhs));
9119 else
9120 return gnu_expr;
9122 break;
9124 case MULT_EXPR:
9125 sgn = tree_int_cst_sgn (rhs);
9126 if (sgn > 0)
9128 if (integer_onep (rhs))
9129 return gnu_expr;
9131 tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
9132 tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
9134 /* When rhs > 1, overflow outside [type_min/rhs; type_max/rhs]. */
9135 check
9136 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9137 build_binary_op (LT_EXPR, boolean_type_node,
9138 lhs, lb),
9139 build_binary_op (GT_EXPR, boolean_type_node,
9140 lhs, ub));
9142 else if (sgn < 0)
9144 tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
9145 tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
9147 if (integer_minus_onep (rhs))
9148 /* When rhs == -1, overflow if lhs == type_min. */
9149 check
9150 = build_binary_op (EQ_EXPR, boolean_type_node, lhs, type_min);
9151 else
9152 /* When rhs < -1, overflow outside [type_max/rhs; type_min/rhs]. */
9153 check
9154 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9155 build_binary_op (LT_EXPR, boolean_type_node,
9156 lhs, lb),
9157 build_binary_op (GT_EXPR, boolean_type_node,
9158 lhs, ub));
9160 else
9161 return gnu_expr;
9162 break;
9164 default:
9165 gcc_unreachable ();
9168 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
9171 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
9172 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
9173 which we have to check. GNAT_NODE is the GNAT node conveying the source
9174 location for which the error should be signaled. */
9176 static tree
9177 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
9179 tree gnu_range_type = get_unpadded_type (gnat_range_type);
9180 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
9182 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
9183 This can for example happen when translating 'Val or 'Value. */
9184 if (gnu_compare_type == gnu_range_type)
9185 return gnu_expr;
9187 /* Range checks can only be applied to types with ranges. */
9188 gcc_assert (INTEGRAL_TYPE_P (gnu_range_type)
9189 || SCALAR_FLOAT_TYPE_P (gnu_range_type));
9191 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
9192 we can't do anything since we might be truncating the bounds. No
9193 check is needed in this case. */
9194 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
9195 && (TYPE_PRECISION (gnu_compare_type)
9196 < TYPE_PRECISION (get_base_type (gnu_range_type))))
9197 return gnu_expr;
9199 /* Checked expressions must be evaluated only once. */
9200 gnu_expr = gnat_protect_expr (gnu_expr);
9202 /* Note that the form of the check is
9203 (not (expr >= lo)) or (not (expr <= hi))
9204 the reason for this slightly convoluted form is that NaNs
9205 are not considered to be in range in the float case. */
9206 return emit_check
9207 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9208 invert_truthvalue
9209 (build_binary_op (GE_EXPR, boolean_type_node,
9210 convert (gnu_compare_type, gnu_expr),
9211 convert (gnu_compare_type,
9212 TYPE_MIN_VALUE
9213 (gnu_range_type)))),
9214 invert_truthvalue
9215 (build_binary_op (LE_EXPR, boolean_type_node,
9216 convert (gnu_compare_type, gnu_expr),
9217 convert (gnu_compare_type,
9218 TYPE_MAX_VALUE
9219 (gnu_range_type))))),
9220 gnu_expr, CE_Range_Check_Failed, gnat_node);
9223 /* GNU_COND contains the condition corresponding to an index, overflow or
9224 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR
9225 if GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
9226 REASON is the code that says why the exception is raised. GNAT_NODE is
9227 the node conveying the source location for which the error should be
9228 signaled.
9230 We used to propagate TREE_SIDE_EFFECTS from GNU_EXPR to the COND_EXPR,
9231 overwriting the setting inherited from the call statement, on the ground
9232 that the expression need not be evaluated just for the check. However
9233 that's incorrect because, in the GCC type system, its value is presumed
9234 to be valid so its comparison against the type bounds always yields true
9235 and, therefore, could be done without evaluating it; given that it can
9236 be a computation that overflows the bounds, the language may require the
9237 check to fail and thus the expression to be evaluated in this case. */
9239 static tree
9240 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
9242 tree gnu_call
9243 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
9244 return
9245 fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
9246 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
9247 SCALAR_FLOAT_TYPE_P (TREE_TYPE (gnu_expr))
9248 ? build_real (TREE_TYPE (gnu_expr), dconst0)
9249 : build_int_cst (TREE_TYPE (gnu_expr), 0)),
9250 gnu_expr);
9253 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
9254 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
9255 If TRUNCATE_P true, do a float-to-integer conversion with truncation,
9256 otherwise round. GNAT_NODE is the GNAT node conveying the source location
9257 for which the error should be signaled. */
9259 static tree
9260 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflow_p,
9261 bool range_p, bool truncate_p, Node_Id gnat_node)
9263 tree gnu_type = get_unpadded_type (gnat_type);
9264 tree gnu_base_type = get_base_type (gnu_type);
9265 tree gnu_in_type = TREE_TYPE (gnu_expr);
9266 tree gnu_in_base_type = get_base_type (gnu_in_type);
9267 tree gnu_result = gnu_expr;
9269 /* If we are not doing any checks, the output is an integral type and the
9270 input is not a floating-point type, just do the conversion. This is
9271 required for packed array types and is simpler in all cases anyway. */
9272 if (!range_p
9273 && !overflow_p
9274 && INTEGRAL_TYPE_P (gnu_base_type)
9275 && !FLOAT_TYPE_P (gnu_in_base_type))
9276 return convert (gnu_type, gnu_expr);
9278 /* If the mode of the input base type is larger, then converting to it below
9279 may pessimize the final conversion step, for example generate a libcall
9280 instead of a simple instruction, so use a narrower type in this case. */
9281 if (TYPE_MODE (gnu_in_base_type) != TYPE_MODE (gnu_in_type)
9282 && !(TREE_CODE (gnu_in_type) == INTEGER_TYPE
9283 && TYPE_BIASED_REPRESENTATION_P (gnu_in_type)))
9284 gnu_in_base_type = gnat_type_for_mode (TYPE_MODE (gnu_in_type),
9285 TYPE_UNSIGNED (gnu_in_type));
9287 /* First convert the expression to the base type. This will never generate
9288 code, but makes the tests below simpler. But don't do this if converting
9289 from an integer type to an unconstrained array type since then we need to
9290 get the bounds from the original (unpacked) type. */
9291 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
9292 gnu_result = convert (gnu_in_base_type, gnu_result);
9294 /* If overflow checks are requested, we need to be sure the result will fit
9295 in the output base type. But don't do this if the input is integer and
9296 the output floating-point. */
9297 if (overflow_p
9298 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_base_type)))
9300 /* Ensure GNU_EXPR only gets evaluated once. */
9301 tree gnu_input = gnat_protect_expr (gnu_result);
9302 tree gnu_cond = boolean_false_node;
9303 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_base_type);
9304 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_base_type);
9305 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
9306 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
9308 /* Convert the lower bounds to signed types, so we're sure we're
9309 comparing them properly. Likewise, convert the upper bounds
9310 to unsigned types. */
9311 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9312 && TYPE_UNSIGNED (gnu_in_base_type))
9313 gnu_in_lb
9314 = convert (gnat_signed_type_for (gnu_in_base_type), gnu_in_lb);
9316 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9317 && !TYPE_UNSIGNED (gnu_in_base_type))
9318 gnu_in_ub
9319 = convert (gnat_unsigned_type_for (gnu_in_base_type), gnu_in_ub);
9321 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
9322 gnu_out_lb
9323 = convert (gnat_signed_type_for (gnu_base_type), gnu_out_lb);
9325 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
9326 gnu_out_ub
9327 = convert (gnat_unsigned_type_for (gnu_base_type), gnu_out_ub);
9329 /* Check each bound separately and only if the result bound
9330 is tighter than the bound on the input type. Note that all the
9331 types are base types, so the bounds must be constant. Also,
9332 the comparison is done in the base type of the input, which
9333 always has the proper signedness. First check for input
9334 integer (which means output integer), output float (which means
9335 both float), or mixed, in which case we always compare.
9336 Note that we have to do the comparison which would *fail* in the
9337 case of an error since if it's an FP comparison and one of the
9338 values is a NaN or Inf, the comparison will fail. */
9339 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9340 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
9341 : (FLOAT_TYPE_P (gnu_base_type)
9342 ? real_less (&TREE_REAL_CST (gnu_in_lb),
9343 &TREE_REAL_CST (gnu_out_lb))
9344 : 1))
9345 gnu_cond
9346 = invert_truthvalue
9347 (build_binary_op (GE_EXPR, boolean_type_node,
9348 gnu_input, convert (gnu_in_base_type,
9349 gnu_out_lb)));
9351 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9352 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
9353 : (FLOAT_TYPE_P (gnu_base_type)
9354 ? real_less (&TREE_REAL_CST (gnu_out_ub),
9355 &TREE_REAL_CST (gnu_in_lb))
9356 : 1))
9357 gnu_cond
9358 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
9359 invert_truthvalue
9360 (build_binary_op (LE_EXPR, boolean_type_node,
9361 gnu_input,
9362 convert (gnu_in_base_type,
9363 gnu_out_ub))));
9365 if (!integer_zerop (gnu_cond))
9366 gnu_result = emit_check (gnu_cond, gnu_input,
9367 CE_Overflow_Check_Failed, gnat_node);
9370 /* Now convert to the result base type. If this is a non-truncating
9371 float-to-integer conversion, round. */
9372 if (INTEGRAL_TYPE_P (gnu_base_type)
9373 && FLOAT_TYPE_P (gnu_in_base_type)
9374 && !truncate_p)
9376 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
9377 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
9378 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
9379 const struct real_format *fmt;
9381 /* The following calculations depend on proper rounding to even
9382 of each arithmetic operation. In order to prevent excess
9383 precision from spoiling this property, use the widest hardware
9384 floating-point type if FP_ARITH_MAY_WIDEN is true. */
9385 calc_type
9386 = fp_arith_may_widen ? longest_float_type_node : gnu_in_base_type;
9388 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
9389 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
9390 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
9391 real_arithmetic (&pred_half, MINUS_EXPR, &dconsthalf,
9392 &half_minus_pred_half);
9393 gnu_pred_half = build_real (calc_type, pred_half);
9395 /* If the input is strictly negative, subtract this value
9396 and otherwise add it from the input. For 0.5, the result
9397 is exactly between 1.0 and the machine number preceding 1.0
9398 (for calc_type). Since the last bit of 1.0 is even, this 0.5
9399 will round to 1.0, while all other number with an absolute
9400 value less than 0.5 round to 0.0. For larger numbers exactly
9401 halfway between integers, rounding will always be correct as
9402 the true mathematical result will be closer to the higher
9403 integer compared to the lower one. So, this constant works
9404 for all floating-point numbers.
9406 The reason to use the same constant with subtract/add instead
9407 of a positive and negative constant is to allow the comparison
9408 to be scheduled in parallel with retrieval of the constant and
9409 conversion of the input to the calc_type (if necessary). */
9411 gnu_zero = build_real (gnu_in_base_type, dconst0);
9412 gnu_result = gnat_protect_expr (gnu_result);
9413 gnu_conv = convert (calc_type, gnu_result);
9414 gnu_comp
9415 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
9416 gnu_add_pred_half
9417 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
9418 gnu_subtract_pred_half
9419 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
9420 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
9421 gnu_add_pred_half, gnu_subtract_pred_half);
9424 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
9425 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
9426 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
9427 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
9428 else
9429 gnu_result = convert (gnu_base_type, gnu_result);
9431 /* Finally, do the range check if requested. Note that if the result type
9432 is a modular type, the range check is actually an overflow check. */
9433 if (range_p
9434 || (overflow_p
9435 && TREE_CODE (gnu_base_type) == INTEGER_TYPE
9436 && TYPE_MODULAR_P (gnu_base_type)))
9437 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
9439 return convert (gnu_type, gnu_result);
9442 /* Return true if GNU_EXPR can be directly addressed. This is the case
9443 unless it is an expression involving computation or if it involves a
9444 reference to a bitfield or to an object not sufficiently aligned for
9445 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
9446 be directly addressed as an object of this type.
9448 *** Notes on addressability issues in the Ada compiler ***
9450 This predicate is necessary in order to bridge the gap between Gigi
9451 and the middle-end about addressability of GENERIC trees. A tree
9452 is said to be addressable if it can be directly addressed, i.e. if
9453 its address can be taken, is a multiple of the type's alignment on
9454 strict-alignment architectures and returns the first storage unit
9455 assigned to the object represented by the tree.
9457 In the C family of languages, everything is in practice addressable
9458 at the language level, except for bit-fields. This means that these
9459 compilers will take the address of any tree that doesn't represent
9460 a bit-field reference and expect the result to be the first storage
9461 unit assigned to the object. Even in cases where this will result
9462 in unaligned accesses at run time, nothing is supposed to be done
9463 and the program is considered as erroneous instead (see PR c/18287).
9465 The implicit assumptions made in the middle-end are in keeping with
9466 the C viewpoint described above:
9467 - the address of a bit-field reference is supposed to be never
9468 taken; the compiler (generally) will stop on such a construct,
9469 - any other tree is addressable if it is formally addressable,
9470 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
9472 In Ada, the viewpoint is the opposite one: nothing is addressable
9473 at the language level unless explicitly declared so. This means
9474 that the compiler will both make sure that the trees representing
9475 references to addressable ("aliased" in Ada parlance) objects are
9476 addressable and make no real attempts at ensuring that the trees
9477 representing references to non-addressable objects are addressable.
9479 In the first case, Ada is effectively equivalent to C and handing
9480 down the direct result of applying ADDR_EXPR to these trees to the
9481 middle-end works flawlessly. In the second case, Ada cannot afford
9482 to consider the program as erroneous if the address of trees that
9483 are not addressable is requested for technical reasons, unlike C;
9484 as a consequence, the Ada compiler must arrange for either making
9485 sure that this address is not requested in the middle-end or for
9486 compensating by inserting temporaries if it is requested in Gigi.
9488 The first goal can be achieved because the middle-end should not
9489 request the address of non-addressable trees on its own; the only
9490 exception is for the invocation of low-level block operations like
9491 memcpy, for which the addressability requirements are lower since
9492 the type's alignment can be disregarded. In practice, this means
9493 that Gigi must make sure that such operations cannot be applied to
9494 non-BLKmode bit-fields.
9496 The second goal is achieved by means of the addressable_p predicate,
9497 which computes whether a temporary must be inserted by Gigi when the
9498 address of a tree is requested; if so, the address of the temporary
9499 will be used in lieu of that of the original tree and some glue code
9500 generated to connect everything together. */
9502 static bool
9503 addressable_p (tree gnu_expr, tree gnu_type)
9505 /* For an integral type, the size of the actual type of the object may not
9506 be greater than that of the expected type, otherwise an indirect access
9507 in the latter type wouldn't correctly set all the bits of the object. */
9508 if (gnu_type
9509 && INTEGRAL_TYPE_P (gnu_type)
9510 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
9511 return false;
9513 /* The size of the actual type of the object may not be smaller than that
9514 of the expected type, otherwise an indirect access in the latter type
9515 would be larger than the object. But only record types need to be
9516 considered in practice for this case. */
9517 if (gnu_type
9518 && TREE_CODE (gnu_type) == RECORD_TYPE
9519 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
9520 return false;
9522 switch (TREE_CODE (gnu_expr))
9524 case VAR_DECL:
9525 case PARM_DECL:
9526 case FUNCTION_DECL:
9527 case RESULT_DECL:
9528 /* All DECLs are addressable: if they are in a register, we can force
9529 them to memory. */
9530 return true;
9532 case UNCONSTRAINED_ARRAY_REF:
9533 case INDIRECT_REF:
9534 /* Taking the address of a dereference yields the original pointer. */
9535 return true;
9537 case STRING_CST:
9538 case INTEGER_CST:
9539 /* Taking the address yields a pointer to the constant pool. */
9540 return true;
9542 case CONSTRUCTOR:
9543 /* Taking the address of a static constructor yields a pointer to the
9544 tree constant pool. */
9545 return TREE_STATIC (gnu_expr) ? true : false;
9547 case NULL_EXPR:
9548 case SAVE_EXPR:
9549 case CALL_EXPR:
9550 case PLUS_EXPR:
9551 case MINUS_EXPR:
9552 case BIT_IOR_EXPR:
9553 case BIT_XOR_EXPR:
9554 case BIT_AND_EXPR:
9555 case BIT_NOT_EXPR:
9556 /* All rvalues are deemed addressable since taking their address will
9557 force a temporary to be created by the middle-end. */
9558 return true;
9560 case COMPOUND_EXPR:
9561 /* The address of a compound expression is that of its 2nd operand. */
9562 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
9564 case COND_EXPR:
9565 /* We accept &COND_EXPR as soon as both operands are addressable and
9566 expect the outcome to be the address of the selected operand. */
9567 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
9568 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
9570 case COMPONENT_REF:
9571 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
9572 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
9573 the field is sufficiently aligned, in case it is subject
9574 to a pragma Component_Alignment. But we don't need to
9575 check the alignment of the containing record, as it is
9576 guaranteed to be not smaller than that of its most
9577 aligned field that is not a bit-field. */
9578 && (!STRICT_ALIGNMENT
9579 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
9580 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
9581 /* The field of a padding record is always addressable. */
9582 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
9583 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
9585 case ARRAY_REF: case ARRAY_RANGE_REF:
9586 case REALPART_EXPR: case IMAGPART_EXPR:
9587 case NOP_EXPR:
9588 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
9590 case CONVERT_EXPR:
9591 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
9592 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
9594 case VIEW_CONVERT_EXPR:
9596 /* This is addressable if we can avoid a copy. */
9597 tree type = TREE_TYPE (gnu_expr);
9598 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
9599 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
9600 && (!STRICT_ALIGNMENT
9601 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
9602 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
9603 || ((TYPE_MODE (type) == BLKmode
9604 || TYPE_MODE (inner_type) == BLKmode)
9605 && (!STRICT_ALIGNMENT
9606 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
9607 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
9608 || TYPE_ALIGN_OK (type)
9609 || TYPE_ALIGN_OK (inner_type))))
9610 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
9613 default:
9614 return false;
9618 /* Do the processing for the declaration of a GNAT_ENTITY, a type or subtype.
9619 If a Freeze node exists for the entity, delay the bulk of the processing.
9620 Otherwise make a GCC type for GNAT_ENTITY and set up the correspondence. */
9622 void
9623 process_type (Entity_Id gnat_entity)
9625 tree gnu_old
9626 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
9628 /* If we are to delay elaboration of this type, just do any elaboration
9629 needed for expressions within the declaration and make a dummy node
9630 for it and its Full_View (if any), in case something points to it.
9631 Do not do this if it has already been done (the only way that can
9632 happen is if the private completion is also delayed). */
9633 if (Present (Freeze_Node (gnat_entity)))
9635 elaborate_entity (gnat_entity);
9637 if (!gnu_old)
9639 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
9640 save_gnu_tree (gnat_entity, gnu_decl, false);
9641 if (Is_Incomplete_Or_Private_Type (gnat_entity)
9642 && Present (Full_View (gnat_entity)))
9644 if (Has_Completion_In_Body (gnat_entity))
9645 DECL_TAFT_TYPE_P (gnu_decl) = 1;
9646 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
9650 return;
9653 /* If we saved away a dummy type for this node, it means that this made the
9654 type that corresponds to the full type of an incomplete type. Clear that
9655 type for now and then update the type in the pointers below. But, if the
9656 saved type is not dummy, it very likely means that we have a use before
9657 declaration for the type in the tree, what we really cannot handle. */
9658 if (gnu_old)
9660 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
9661 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
9663 save_gnu_tree (gnat_entity, NULL_TREE, false);
9666 /* Now fully elaborate the type. */
9667 tree gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, true);
9668 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
9670 /* If we have an old type and we've made pointers to this type, update those
9671 pointers. If this is a Taft amendment type in the main unit, we need to
9672 mark the type as used since other units referencing it don't see the full
9673 declaration and, therefore, cannot mark it as used themselves. */
9674 if (gnu_old)
9676 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
9677 TREE_TYPE (gnu_new));
9678 if (DECL_TAFT_TYPE_P (gnu_old))
9679 used_types_insert (TREE_TYPE (gnu_new));
9682 /* If this is a record type corresponding to a task or protected type
9683 that is a completion of an incomplete type, perform a similar update
9684 on the type. ??? Including protected types here is a guess. */
9685 if (Is_Record_Type (gnat_entity)
9686 && Is_Concurrent_Record_Type (gnat_entity)
9687 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
9689 tree gnu_task_old
9690 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
9692 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
9693 NULL_TREE, false);
9694 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
9695 gnu_new, false);
9697 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
9698 TREE_TYPE (gnu_new));
9702 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
9703 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting of the
9704 associations that are from RECORD_TYPE. If we see an internal record, make
9705 a recursive call to fill it in as well. */
9707 static tree
9708 extract_values (tree values, tree record_type)
9710 vec<constructor_elt, va_gc> *v = NULL;
9711 tree field;
9713 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9715 tree tem, value = NULL_TREE;
9717 /* _Parent is an internal field, but may have values in the aggregate,
9718 so check for values first. */
9719 if ((tem = purpose_member (field, values)))
9721 value = TREE_VALUE (tem);
9722 TREE_ADDRESSABLE (tem) = 1;
9725 else if (DECL_INTERNAL_P (field))
9727 value = extract_values (values, TREE_TYPE (field));
9728 if (TREE_CODE (value) == CONSTRUCTOR
9729 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
9730 value = NULL_TREE;
9732 else
9733 /* If we have a record subtype, the names will match, but not the
9734 actual FIELD_DECLs. */
9735 for (tem = values; tem; tem = TREE_CHAIN (tem))
9736 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
9738 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
9739 TREE_ADDRESSABLE (tem) = 1;
9742 if (!value)
9743 continue;
9745 CONSTRUCTOR_APPEND_ELT (v, field, value);
9748 return gnat_build_constructor (record_type, v);
9751 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
9752 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
9753 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
9755 static tree
9756 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
9758 tree gnu_list = NULL_TREE, gnu_result;
9760 /* We test for GNU_FIELD being empty in the case where a variant
9761 was the last thing since we don't take things off GNAT_ASSOC in
9762 that case. We check GNAT_ASSOC in case we have a variant, but it
9763 has no fields. */
9765 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
9767 Node_Id gnat_field = First (Choices (gnat_assoc));
9768 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
9769 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
9771 /* The expander is supposed to put a single component selector name
9772 in every record component association. */
9773 gcc_assert (No (Next (gnat_field)));
9775 /* Ignore discriminants that have Corresponding_Discriminants in tagged
9776 types since we'll be setting those fields in the parent subtype. */
9777 if (Ekind (Entity (gnat_field)) == E_Discriminant
9778 && Present (Corresponding_Discriminant (Entity (gnat_field)))
9779 && Is_Tagged_Type (Scope (Entity (gnat_field))))
9780 continue;
9782 /* Also ignore discriminants of Unchecked_Unions. */
9783 if (Ekind (Entity (gnat_field)) == E_Discriminant
9784 && Is_Unchecked_Union (gnat_entity))
9785 continue;
9787 /* Before assigning a value in an aggregate make sure range checks
9788 are done if required. Then convert to the type of the field. */
9789 if (Do_Range_Check (Expression (gnat_assoc)))
9790 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
9792 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
9794 /* Add the field and expression to the list. */
9795 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
9798 gnu_result = extract_values (gnu_list, gnu_type);
9800 if (flag_checking)
9802 /* Verify that every entry in GNU_LIST was used. */
9803 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
9804 gcc_assert (TREE_ADDRESSABLE (gnu_list));
9807 return gnu_result;
9810 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
9811 the first element of an array aggregate. It may itself be an aggregate.
9812 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
9813 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
9814 for range checking. */
9816 static tree
9817 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
9818 Entity_Id gnat_component_type)
9820 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
9821 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
9823 for (; Present (gnat_expr); gnat_expr = Next (gnat_expr))
9825 tree gnu_expr;
9827 /* If the expression is itself an array aggregate then first build the
9828 innermost constructor if it is part of our array (multi-dimensional
9829 case). */
9830 if (Nkind (gnat_expr) == N_Aggregate
9831 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
9832 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
9833 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
9834 TREE_TYPE (gnu_array_type),
9835 gnat_component_type);
9836 else
9838 /* If the expression is a conversion to an unconstrained array type,
9839 skip it to avoid spilling to memory. */
9840 if (Nkind (gnat_expr) == N_Type_Conversion
9841 && Is_Array_Type (Etype (gnat_expr))
9842 && !Is_Constrained (Etype (gnat_expr)))
9843 gnu_expr = gnat_to_gnu (Expression (gnat_expr));
9844 else
9845 gnu_expr = gnat_to_gnu (gnat_expr);
9847 /* Before assigning the element to the array, make sure it is
9848 in range. */
9849 if (Do_Range_Check (gnat_expr))
9850 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
9853 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
9854 convert (TREE_TYPE (gnu_array_type), gnu_expr));
9856 gnu_index = int_const_binop (PLUS_EXPR, gnu_index,
9857 convert (TREE_TYPE (gnu_index),
9858 integer_one_node));
9861 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
9864 /* Process a N_Validate_Unchecked_Conversion node. */
9866 static void
9867 validate_unchecked_conversion (Node_Id gnat_node)
9869 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
9870 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
9872 /* If the target is a pointer type, see if we are either converting from a
9873 non-pointer or from a pointer to a type with a different alias set and
9874 warn if so, unless the pointer has been marked to alias everything. */
9875 if (POINTER_TYPE_P (gnu_target_type)
9876 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
9878 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
9879 ? TREE_TYPE (gnu_source_type)
9880 : NULL_TREE;
9881 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
9882 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9884 if (target_alias_set != 0
9885 && (!POINTER_TYPE_P (gnu_source_type)
9886 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9887 target_alias_set)))
9889 post_error_ne ("?possible aliasing problem for type&",
9890 gnat_node, Target_Type (gnat_node));
9891 post_error ("\\?use -fno-strict-aliasing switch for references",
9892 gnat_node);
9893 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
9894 gnat_node, Target_Type (gnat_node));
9898 /* Likewise if the target is a fat pointer type, but we have no mechanism to
9899 mitigate the problem in this case, so we unconditionally warn. */
9900 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
9902 tree gnu_source_desig_type
9903 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
9904 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
9905 : NULL_TREE;
9906 tree gnu_target_desig_type
9907 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
9908 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9910 if (target_alias_set != 0
9911 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
9912 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9913 target_alias_set)))
9915 post_error_ne ("?possible aliasing problem for type&",
9916 gnat_node, Target_Type (gnat_node));
9917 post_error ("\\?use -fno-strict-aliasing switch for references",
9918 gnat_node);
9923 /* EXP is to be used in a context where access objects are implicitly
9924 dereferenced. Handle the cases when it is an access object. */
9926 static Node_Id
9927 adjust_for_implicit_deref (Node_Id exp)
9929 Entity_Id type = Underlying_Type (Etype (exp));
9931 /* Make sure the designated type is complete before dereferencing. */
9932 if (Is_Access_Type (type))
9933 gnat_to_gnu_entity (Designated_Type (type), NULL_TREE, false);
9935 return exp;
9938 /* EXP is to be treated as an array or record. Handle the cases when it is
9939 an access object and perform the required dereferences. */
9941 static tree
9942 maybe_implicit_deref (tree exp)
9944 /* If the type is a pointer, dereference it. */
9945 if (POINTER_TYPE_P (TREE_TYPE (exp))
9946 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp)))
9947 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
9949 /* If we got a padded type, remove it too. */
9950 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
9951 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
9953 return exp;
9956 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
9957 location and false if it doesn't. If CLEAR_COLUMN is true, set the column
9958 information to 0. */
9960 bool
9961 Sloc_to_locus (Source_Ptr Sloc, location_t *locus, bool clear_column)
9963 if (Sloc == No_Location)
9964 return false;
9966 if (Sloc <= Standard_Location)
9968 *locus = BUILTINS_LOCATION;
9969 return false;
9972 Source_File_Index file = Get_Source_File_Index (Sloc);
9973 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
9974 Column_Number column = (clear_column ? 0 : Get_Column_Number (Sloc));
9975 line_map_ordinary *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
9977 /* We can have zero if pragma Source_Reference is in effect. */
9978 if (line < 1)
9979 line = 1;
9981 /* Translate the location. */
9982 *locus
9983 = linemap_position_for_line_and_column (line_table, map, line, column);
9985 return true;
9988 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
9989 don't do anything if it doesn't correspond to a source location. And,
9990 if CLEAR_COLUMN is true, set the column information to 0. */
9992 static void
9993 set_expr_location_from_node (tree node, Node_Id gnat_node, bool clear_column)
9995 location_t locus;
9997 if (!Sloc_to_locus (Sloc (gnat_node), &locus, clear_column))
9998 return;
10000 SET_EXPR_LOCATION (node, locus);
10003 /* More elaborate version of set_expr_location_from_node to be used in more
10004 general contexts, for example the result of the translation of a generic
10005 GNAT node. */
10007 static void
10008 set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
10010 /* Set the location information on the node if it is a real expression.
10011 References can be reused for multiple GNAT nodes and they would get
10012 the location information of their last use. Also make sure not to
10013 overwrite an existing location as it is probably more precise. */
10015 switch (TREE_CODE (node))
10017 CASE_CONVERT:
10018 case NON_LVALUE_EXPR:
10019 case SAVE_EXPR:
10020 break;
10022 case COMPOUND_EXPR:
10023 if (EXPR_P (TREE_OPERAND (node, 1)))
10024 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
10026 /* ... fall through ... */
10028 default:
10029 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
10031 set_expr_location_from_node (node, gnat_node);
10032 set_end_locus_from_node (node, gnat_node);
10034 break;
10038 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
10039 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
10040 most sense. Return true if a sensible assignment was performed. */
10042 static bool
10043 set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
10045 Node_Id gnat_end_label;
10046 location_t end_locus;
10048 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
10049 end_locus when there is one. We consider only GNAT nodes with a possible
10050 End_Label attached. If the End_Label actually was unassigned, fallback
10051 on the original node. We'd better assign an explicit sloc associated with
10052 the outer construct in any case. */
10054 switch (Nkind (gnat_node))
10056 case N_Package_Body:
10057 case N_Subprogram_Body:
10058 case N_Block_Statement:
10059 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
10060 break;
10062 case N_Package_Declaration:
10063 gnat_end_label = End_Label (Specification (gnat_node));
10064 break;
10066 default:
10067 return false;
10070 if (Present (gnat_end_label))
10071 gnat_node = gnat_end_label;
10073 /* Some expanded subprograms have neither an End_Label nor a Sloc
10074 attached. Notify that to callers. For a block statement with no
10075 End_Label, clear column information, so that the tree for a
10076 transient block does not receive the sloc of a source condition. */
10077 if (!Sloc_to_locus (Sloc (gnat_node), &end_locus,
10078 No (gnat_end_label)
10079 && (Nkind (gnat_node) == N_Block_Statement)))
10080 return false;
10082 switch (TREE_CODE (gnu_node))
10084 case BIND_EXPR:
10085 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
10086 return true;
10088 case FUNCTION_DECL:
10089 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
10090 return true;
10092 default:
10093 return false;
10097 /* Return a colon-separated list of encodings contained in encoded Ada
10098 name. */
10100 static const char *
10101 extract_encoding (const char *name)
10103 char *encoding = (char *) ggc_alloc_atomic (strlen (name));
10104 get_encoding (name, encoding);
10105 return encoding;
10108 /* Extract the Ada name from an encoded name. */
10110 static const char *
10111 decode_name (const char *name)
10113 char *decoded = (char *) ggc_alloc_atomic (strlen (name) * 2 + 60);
10114 __gnat_decode (name, decoded, 0);
10115 return decoded;
10118 /* Post an error message. MSG is the error message, properly annotated.
10119 NODE is the node at which to post the error and the node to use for the
10120 '&' substitution. */
10122 void
10123 post_error (const char *msg, Node_Id node)
10125 String_Template temp;
10126 String_Pointer sp;
10128 if (No (node))
10129 return;
10131 temp.Low_Bound = 1;
10132 temp.High_Bound = strlen (msg);
10133 sp.Bounds = &temp;
10134 sp.Array = msg;
10135 Error_Msg_N (sp, node);
10138 /* Similar to post_error, but NODE is the node at which to post the error and
10139 ENT is the node to use for the '&' substitution. */
10141 void
10142 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
10144 String_Template temp;
10145 String_Pointer sp;
10147 if (No (node))
10148 return;
10150 temp.Low_Bound = 1;
10151 temp.High_Bound = strlen (msg);
10152 sp.Bounds = &temp;
10153 sp.Array = msg;
10154 Error_Msg_NE (sp, node, ent);
10157 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
10159 void
10160 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
10162 Error_Msg_Uint_1 = UI_From_Int (num);
10163 post_error_ne (msg, node, ent);
10166 /* Similar to post_error_ne, but T is a GCC tree representing the number to
10167 write. If T represents a constant, the text inside curly brackets in
10168 MSG will be output (presumably including a '^'). Otherwise it will not
10169 be output and the text inside square brackets will be output instead. */
10171 void
10172 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
10174 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
10175 char start_yes, end_yes, start_no, end_no;
10176 const char *p;
10177 char *q;
10179 if (TREE_CODE (t) == INTEGER_CST)
10181 Error_Msg_Uint_1 = UI_From_gnu (t);
10182 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
10184 else
10185 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
10187 for (p = msg, q = new_msg; *p; p++)
10189 if (*p == start_yes)
10190 for (p++; *p != end_yes; p++)
10191 *q++ = *p;
10192 else if (*p == start_no)
10193 for (p++; *p != end_no; p++)
10195 else
10196 *q++ = *p;
10199 *q = 0;
10201 post_error_ne (new_msg, node, ent);
10204 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
10206 void
10207 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
10208 int num)
10210 Error_Msg_Uint_2 = UI_From_Int (num);
10211 post_error_ne_tree (msg, node, ent, t);
10214 /* Return a label to branch to for the exception type in KIND or NULL_TREE
10215 if none. */
10217 tree
10218 get_exception_label (char kind)
10220 switch (kind)
10222 case N_Raise_Constraint_Error:
10223 return gnu_constraint_error_label_stack->last ();
10225 case N_Raise_Storage_Error:
10226 return gnu_storage_error_label_stack->last ();
10228 case N_Raise_Program_Error:
10229 return gnu_program_error_label_stack->last ();
10231 default:
10232 break;
10235 return NULL_TREE;
10238 /* Return the decl for the current elaboration procedure. */
10240 static tree
10241 get_elaboration_procedure (void)
10243 return gnu_elab_proc_stack->last ();
10246 /* Initialize the table that maps GNAT codes to GCC codes for simple
10247 binary and unary operations. */
10249 static void
10250 init_code_table (void)
10252 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
10253 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
10254 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
10255 gnu_codes[N_Op_Eq] = EQ_EXPR;
10256 gnu_codes[N_Op_Ne] = NE_EXPR;
10257 gnu_codes[N_Op_Lt] = LT_EXPR;
10258 gnu_codes[N_Op_Le] = LE_EXPR;
10259 gnu_codes[N_Op_Gt] = GT_EXPR;
10260 gnu_codes[N_Op_Ge] = GE_EXPR;
10261 gnu_codes[N_Op_Add] = PLUS_EXPR;
10262 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
10263 gnu_codes[N_Op_Multiply] = MULT_EXPR;
10264 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
10265 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
10266 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
10267 gnu_codes[N_Op_Abs] = ABS_EXPR;
10268 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
10269 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
10270 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
10271 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
10272 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
10273 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
10274 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
10275 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
10278 #include "gt-ada-trans.h"