* gcc-interface/trans.c (node_has_volatile_full_access) <N_Identifier>:
[official-gcc.git] / gcc / ada / gcc-interface / trans.c
blobfbdced57a0affe6b9f643f7b246974c16426e20d
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * T R A N S *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2018, 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 vec<Entity_Id> gnu_constraint_error_label_stack;
215 static vec<Entity_Id> gnu_storage_error_label_stack;
216 static vec<Entity_Id> 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 tree build_stmt_group (List_Id, bool);
230 static inline bool stmt_group_may_fallthru (void);
231 static enum gimplify_status gnat_gimplify_stmt (tree *);
232 static void elaborate_all_entities (Node_Id);
233 static void process_freeze_entity (Node_Id);
234 static void process_decls (List_Id, List_Id, Node_Id, bool, bool);
235 static tree emit_range_check (tree, Node_Id, Node_Id);
236 static tree emit_check (tree, tree, int, Node_Id);
237 static tree build_unary_op_trapv (enum tree_code, tree, tree, Node_Id);
238 static tree build_binary_op_trapv (enum tree_code, tree, tree, tree, Node_Id);
239 static tree convert_with_check (Entity_Id, tree, bool, bool, bool, Node_Id);
240 static bool addressable_p (tree, tree);
241 static tree assoc_to_constructor (Entity_Id, Node_Id, tree);
242 static tree pos_to_constructor (Node_Id, tree, Entity_Id);
243 static void validate_unchecked_conversion (Node_Id);
244 static Node_Id adjust_for_implicit_deref (Node_Id);
245 static tree maybe_implicit_deref (tree);
246 static void set_expr_location_from_node (tree, Node_Id, bool = false);
247 static void set_gnu_expr_location_from_node (tree, Node_Id);
248 static bool set_end_locus_from_node (tree, Node_Id);
249 static int lvalue_required_p (Node_Id, tree, bool, bool, bool);
250 static tree build_raise_check (int, enum exception_info_kind);
251 static tree create_init_temporary (const char *, tree, tree *, Node_Id);
253 /* Hooks for debug info back-ends, only supported and used in a restricted set
254 of configurations. */
255 static const char *extract_encoding (const char *) ATTRIBUTE_UNUSED;
256 static const char *decode_name (const char *) ATTRIBUTE_UNUSED;
258 /* This is the main program of the back-end. It sets up all the table
259 structures and then generates code. */
261 void
262 gigi (Node_Id gnat_root,
263 int max_gnat_node,
264 int number_name ATTRIBUTE_UNUSED,
265 struct Node *nodes_ptr,
266 struct Flags *flags_ptr,
267 Node_Id *next_node_ptr,
268 Node_Id *prev_node_ptr,
269 struct Elist_Header *elists_ptr,
270 struct Elmt_Item *elmts_ptr,
271 struct String_Entry *strings_ptr,
272 Char_Code *string_chars_ptr,
273 struct List_Header *list_headers_ptr,
274 Nat number_file,
275 struct File_Info_Type *file_info_ptr,
276 Entity_Id standard_boolean,
277 Entity_Id standard_integer,
278 Entity_Id standard_character,
279 Entity_Id standard_long_long_float,
280 Entity_Id standard_exception_type,
281 Int gigi_operating_mode)
283 Node_Id gnat_iter;
284 Entity_Id gnat_literal;
285 tree t, ftype, int64_type;
286 struct elab_info *info;
287 int i;
289 max_gnat_nodes = max_gnat_node;
291 Nodes_Ptr = nodes_ptr;
292 Flags_Ptr = flags_ptr;
293 Next_Node_Ptr = next_node_ptr;
294 Prev_Node_Ptr = prev_node_ptr;
295 Elists_Ptr = elists_ptr;
296 Elmts_Ptr = elmts_ptr;
297 Strings_Ptr = strings_ptr;
298 String_Chars_Ptr = string_chars_ptr;
299 List_Headers_Ptr = list_headers_ptr;
301 type_annotate_only = (gigi_operating_mode == 1);
303 for (i = 0; i < number_file; i++)
305 /* Use the identifier table to make a permanent copy of the filename as
306 the name table gets reallocated after Gigi returns but before all the
307 debugging information is output. The __gnat_to_canonical_file_spec
308 call translates filenames from pragmas Source_Reference that contain
309 host style syntax not understood by gdb. */
310 const char *filename
311 = IDENTIFIER_POINTER
312 (get_identifier
313 (__gnat_to_canonical_file_spec
314 (Get_Name_String (file_info_ptr[i].File_Name))));
316 /* We rely on the order isomorphism between files and line maps. */
317 gcc_assert ((int) LINEMAPS_ORDINARY_USED (line_table) == i);
319 /* We create the line map for a source file at once, with a fixed number
320 of columns chosen to avoid jumping over the next power of 2. */
321 linemap_add (line_table, LC_ENTER, 0, filename, 1);
322 linemap_line_start (line_table, file_info_ptr[i].Num_Source_Lines, 252);
323 linemap_position_for_column (line_table, 252 - 1);
324 linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
327 gcc_assert (Nkind (gnat_root) == N_Compilation_Unit);
329 /* Declare the name of the compilation unit as the first global
330 name in order to make the middle-end fully deterministic. */
331 t = create_concat_name (Defining_Entity (Unit (gnat_root)), NULL);
332 first_global_object_name = ggc_strdup (IDENTIFIER_POINTER (t));
334 /* Initialize ourselves. */
335 init_code_table ();
336 init_gnat_decl ();
337 init_gnat_utils ();
339 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
340 errors. */
341 if (type_annotate_only)
343 TYPE_SIZE (void_type_node) = bitsize_zero_node;
344 TYPE_SIZE_UNIT (void_type_node) = size_zero_node;
347 /* Enable GNAT stack checking method if needed */
348 if (!Stack_Check_Probes_On_Target)
349 set_stack_check_libfunc ("_gnat_stack_check");
351 /* Retrieve alignment settings. */
352 double_float_alignment = get_target_double_float_alignment ();
353 double_scalar_alignment = get_target_double_scalar_alignment ();
355 /* Record the builtin types. Define `integer' and `character' first so that
356 dbx will output them first. */
357 record_builtin_type ("integer", integer_type_node, false);
358 record_builtin_type ("character", char_type_node, false);
359 record_builtin_type ("boolean", boolean_type_node, false);
360 record_builtin_type ("void", void_type_node, false);
362 /* Save the type we made for integer as the type for Standard.Integer. */
363 save_gnu_tree (Base_Type (standard_integer),
364 TYPE_NAME (integer_type_node),
365 false);
367 /* Likewise for character as the type for Standard.Character. */
368 finish_character_type (char_type_node);
369 save_gnu_tree (Base_Type (standard_character),
370 TYPE_NAME (char_type_node),
371 false);
373 /* Likewise for boolean as the type for Standard.Boolean. */
374 save_gnu_tree (Base_Type (standard_boolean),
375 TYPE_NAME (boolean_type_node),
376 false);
377 gnat_literal = First_Literal (Base_Type (standard_boolean));
378 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
379 gcc_assert (t == boolean_false_node);
380 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
381 boolean_type_node, t, true, false, false, false, false,
382 true, false, NULL, gnat_literal);
383 save_gnu_tree (gnat_literal, t, false);
384 gnat_literal = Next_Literal (gnat_literal);
385 t = UI_To_gnu (Enumeration_Rep (gnat_literal), boolean_type_node);
386 gcc_assert (t == boolean_true_node);
387 t = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
388 boolean_type_node, t, true, false, false, false, false,
389 true, false, NULL, gnat_literal);
390 save_gnu_tree (gnat_literal, t, false);
392 /* Declare the building blocks of function nodes. */
393 void_list_node = build_tree_list (NULL_TREE, void_type_node);
394 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
395 ptr_void_ftype = build_pointer_type (void_ftype);
397 /* Now declare run-time functions. */
398 malloc_decl
399 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE,
400 build_function_type_list (ptr_type_node, sizetype,
401 NULL_TREE),
402 NULL_TREE, is_disabled, true, true, true, false,
403 false, NULL, Empty);
404 DECL_IS_MALLOC (malloc_decl) = 1;
406 free_decl
407 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE,
408 build_function_type_list (void_type_node,
409 ptr_type_node, NULL_TREE),
410 NULL_TREE, is_disabled, true, true, true, false,
411 false, NULL, Empty);
413 realloc_decl
414 = create_subprog_decl (get_identifier ("__gnat_realloc"), NULL_TREE,
415 build_function_type_list (ptr_type_node,
416 ptr_type_node, sizetype,
417 NULL_TREE),
418 NULL_TREE, is_disabled, true, true, true, false,
419 false, NULL, Empty);
421 /* This is used for 64-bit multiplication with overflow checking. */
422 int64_type = gnat_type_for_size (64, 0);
423 mulv64_decl
424 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE,
425 build_function_type_list (int64_type, int64_type,
426 int64_type, NULL_TREE),
427 NULL_TREE, is_disabled, true, true, true, false,
428 false, NULL, Empty);
430 /* Name of the _Parent field in tagged record types. */
431 parent_name_id = get_identifier (Get_Name_String (Name_uParent));
433 /* Name of the Exception_Data type defined in System.Standard_Library. */
434 exception_data_name_id
435 = get_identifier ("system__standard_library__exception_data");
437 /* Make the types and functions used for exception processing. */
438 except_type_node = gnat_to_gnu_type (Base_Type (standard_exception_type));
440 jmpbuf_type
441 = build_array_type (gnat_type_for_mode (Pmode, 0),
442 build_index_type (size_int (5)));
443 record_builtin_type ("JMPBUF_T", jmpbuf_type, true);
444 jmpbuf_ptr_type = build_pointer_type (jmpbuf_type);
446 /* Functions to get and set the jumpbuf pointer for the current thread. */
447 get_jmpbuf_decl
448 = create_subprog_decl
449 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
450 NULL_TREE, build_function_type_list (jmpbuf_ptr_type, NULL_TREE),
451 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
453 set_jmpbuf_decl
454 = create_subprog_decl
455 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
456 NULL_TREE, build_function_type_list (void_type_node, jmpbuf_ptr_type,
457 NULL_TREE),
458 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
460 get_excptr_decl
461 = create_subprog_decl
462 (get_identifier ("system__soft_links__get_gnat_exception"), NULL_TREE,
463 build_function_type_list (build_pointer_type (except_type_node),
464 NULL_TREE),
465 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
467 not_handled_by_others_decl = get_identifier ("not_handled_by_others");
468 for (t = TYPE_FIELDS (except_type_node); t; t = DECL_CHAIN (t))
469 if (DECL_NAME (t) == not_handled_by_others_decl)
471 not_handled_by_others_decl = t;
472 break;
474 gcc_assert (DECL_P (not_handled_by_others_decl));
476 /* setjmp returns an integer and has one operand, which is a pointer to
477 a jmpbuf. */
478 setjmp_decl
479 = create_subprog_decl
480 (get_identifier ("__builtin_setjmp"), NULL_TREE,
481 build_function_type_list (integer_type_node, jmpbuf_ptr_type,
482 NULL_TREE),
483 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
484 DECL_BUILT_IN_CLASS (setjmp_decl) = BUILT_IN_NORMAL;
485 DECL_FUNCTION_CODE (setjmp_decl) = BUILT_IN_SETJMP;
487 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
488 address. */
489 update_setjmp_buf_decl
490 = create_subprog_decl
491 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE,
492 build_function_type_list (void_type_node, jmpbuf_ptr_type, NULL_TREE),
493 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
494 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl) = BUILT_IN_NORMAL;
495 DECL_FUNCTION_CODE (update_setjmp_buf_decl) = BUILT_IN_UPDATE_SETJMP_BUF;
497 /* Indicate that it never returns. */
498 ftype = build_function_type_list (void_type_node,
499 build_pointer_type (except_type_node),
500 NULL_TREE);
501 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
502 raise_nodefer_decl
503 = create_subprog_decl
504 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE, ftype,
505 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
507 set_exception_parameter_decl
508 = create_subprog_decl
509 (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE,
510 build_function_type_list (void_type_node, ptr_type_node, ptr_type_node,
511 NULL_TREE),
512 NULL_TREE, is_disabled, true, true, true, false, false, NULL, Empty);
514 /* Hooks to call when entering/leaving an exception handler. */
515 ftype = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
517 begin_handler_decl
518 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE,
519 ftype, NULL_TREE,
520 is_disabled, true, true, true, false, false, NULL,
521 Empty);
522 /* __gnat_begin_handler is a dummy procedure. */
523 TREE_NOTHROW (begin_handler_decl) = 1;
525 end_handler_decl
526 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE,
527 ftype, NULL_TREE,
528 is_disabled, true, true, true, false, false, NULL,
529 Empty);
531 unhandled_except_decl
532 = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
533 NULL_TREE, ftype, NULL_TREE,
534 is_disabled, true, true, true, false, false, NULL,
535 Empty);
537 /* Indicate that it never returns. */
538 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
539 reraise_zcx_decl
540 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE,
541 ftype, NULL_TREE,
542 is_disabled, true, true, true, false, false, NULL,
543 Empty);
545 /* Dummy objects to materialize "others" and "all others" in the exception
546 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
547 the types to use. */
548 others_decl
549 = create_var_decl (get_identifier ("OTHERS"),
550 get_identifier ("__gnat_others_value"),
551 char_type_node, NULL_TREE,
552 true, false, true, false, false, true, false,
553 NULL, Empty);
555 all_others_decl
556 = create_var_decl (get_identifier ("ALL_OTHERS"),
557 get_identifier ("__gnat_all_others_value"),
558 char_type_node, NULL_TREE,
559 true, false, true, false, false, true, false,
560 NULL, Empty);
562 unhandled_others_decl
563 = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
564 get_identifier ("__gnat_unhandled_others_value"),
565 char_type_node, NULL_TREE,
566 true, false, true, false, false, true, false,
567 NULL, Empty);
569 /* If in no exception handlers mode, all raise statements are redirected to
570 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl since
571 this procedure will never be called in this mode. */
572 if (No_Exception_Handlers_Set ())
574 /* Indicate that it never returns. */
575 ftype = build_function_type_list (void_type_node,
576 build_pointer_type (char_type_node),
577 integer_type_node, NULL_TREE);
578 ftype = build_qualified_type (ftype, TYPE_QUAL_VOLATILE);
579 tree decl
580 = create_subprog_decl
581 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE, ftype,
582 NULL_TREE, is_disabled, true, true, true, false, false, NULL,
583 Empty);
584 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
585 gnat_raise_decls[i] = decl;
587 else
589 /* Otherwise, make one decl for each exception reason. */
590 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls); i++)
591 gnat_raise_decls[i] = build_raise_check (i, exception_simple);
592 for (i = 0; i < (int) ARRAY_SIZE (gnat_raise_decls_ext); i++)
593 gnat_raise_decls_ext[i]
594 = build_raise_check (i,
595 i == CE_Index_Check_Failed
596 || i == CE_Range_Check_Failed
597 || i == CE_Invalid_Data
598 ? exception_range : exception_column);
601 /* Build the special descriptor type and its null node if needed. */
602 if (TARGET_VTABLE_USES_DESCRIPTORS)
604 tree null_node = fold_convert (ptr_void_ftype, null_pointer_node);
605 tree field_list = NULL_TREE;
606 int j;
607 vec<constructor_elt, va_gc> *null_vec = NULL;
608 constructor_elt *elt;
610 fdesc_type_node = make_node (RECORD_TYPE);
611 vec_safe_grow (null_vec, TARGET_VTABLE_USES_DESCRIPTORS);
612 elt = (null_vec->address () + TARGET_VTABLE_USES_DESCRIPTORS - 1);
614 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; j++)
616 tree field
617 = create_field_decl (NULL_TREE, ptr_void_ftype, fdesc_type_node,
618 NULL_TREE, NULL_TREE, 0, 1);
619 DECL_CHAIN (field) = field_list;
620 field_list = field;
621 elt->index = field;
622 elt->value = null_node;
623 elt--;
626 finish_record_type (fdesc_type_node, nreverse (field_list), 0, false);
627 record_builtin_type ("descriptor", fdesc_type_node, true);
628 null_fdesc_node = gnat_build_constructor (fdesc_type_node, null_vec);
631 longest_float_type_node
632 = get_unpadded_type (Base_Type (standard_long_long_float));
634 main_identifier_node = get_identifier ("main");
636 /* If we are using the GCC exception mechanism, let GCC know. */
637 if (Back_End_Exceptions ())
638 gnat_init_gcc_eh ();
640 /* Initialize the GCC support for FP operations. */
641 gnat_init_gcc_fp ();
643 /* Install the builtins we might need, either internally or as user-available
644 facilities for Intrinsic imports. Note that this must be done after the
645 GCC exception mechanism is initialized. */
646 gnat_install_builtins ();
648 vec_safe_push (gnu_except_ptr_stack, NULL_TREE);
650 gnu_constraint_error_label_stack.safe_push (Empty);
651 gnu_storage_error_label_stack.safe_push (Empty);
652 gnu_program_error_label_stack.safe_push (Empty);
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 && pad_type_has_rm_size (gnu_type)))
1855 || attribute == Attr_Object_Size
1856 || attribute == Attr_Max_Size_In_Storage_Elements)
1858 /* If this is a dereference and we have a special dynamic constrained
1859 subtype on the prefix, use it to compute the size; otherwise, use
1860 the designated subtype. */
1861 if (Nkind (gnat_prefix) == N_Explicit_Dereference)
1863 Node_Id gnat_actual_subtype
1864 = Actual_Designated_Subtype (gnat_prefix);
1865 tree gnu_ptr_type
1866 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix)));
1868 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type)
1869 && Present (gnat_actual_subtype))
1871 tree gnu_actual_obj_type
1872 = gnat_to_gnu_type (gnat_actual_subtype);
1873 gnu_type
1874 = build_unc_object_type_from_ptr (gnu_ptr_type,
1875 gnu_actual_obj_type,
1876 get_identifier ("SIZE"),
1877 false);
1881 gnu_result = TYPE_SIZE (gnu_type);
1884 /* Otherwise, the result is the RM size of the type. */
1885 else
1886 gnu_result = rm_size (gnu_type);
1888 /* Deal with a self-referential size by returning the maximum size for
1889 a type and by qualifying the size with the object otherwise. */
1890 if (CONTAINS_PLACEHOLDER_P (gnu_result))
1892 if (TREE_CODE (gnu_prefix) == TYPE_DECL)
1893 gnu_result = max_size (gnu_result, true);
1894 else
1895 gnu_result = substitute_placeholder_in_expr (gnu_result, gnu_expr);
1898 /* If the type contains a template, subtract its size. */
1899 if (TREE_CODE (gnu_type) == RECORD_TYPE
1900 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1901 gnu_result = size_binop (MINUS_EXPR, gnu_result,
1902 DECL_SIZE (TYPE_FIELDS (gnu_type)));
1904 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
1905 if (attribute == Attr_Max_Size_In_Storage_Elements)
1906 gnu_result = size_binop (CEIL_DIV_EXPR, gnu_result, bitsize_unit_node);
1908 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1909 break;
1911 case Attr_Alignment:
1913 unsigned int align;
1915 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
1916 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
1917 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
1919 gnu_type = TREE_TYPE (gnu_prefix);
1920 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1921 prefix_unused = true;
1923 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
1924 align = DECL_ALIGN (TREE_OPERAND (gnu_prefix, 1)) / BITS_PER_UNIT;
1925 else
1927 Entity_Id gnat_type = Etype (gnat_prefix);
1928 unsigned int double_align;
1929 bool is_capped_double, align_clause;
1931 /* If the default alignment of "double" or larger scalar types is
1932 specifically capped and there is an alignment clause neither
1933 on the type nor on the prefix itself, return the cap. */
1934 if ((double_align = double_float_alignment) > 0)
1935 is_capped_double
1936 = is_double_float_or_array (gnat_type, &align_clause);
1937 else if ((double_align = double_scalar_alignment) > 0)
1938 is_capped_double
1939 = is_double_scalar_or_array (gnat_type, &align_clause);
1940 else
1941 is_capped_double = align_clause = false;
1943 if (is_capped_double
1944 && Nkind (gnat_prefix) == N_Identifier
1945 && Present (Alignment_Clause (Entity (gnat_prefix))))
1946 align_clause = true;
1948 if (is_capped_double && !align_clause)
1949 align = double_align;
1950 else
1951 align = TYPE_ALIGN (gnu_type) / BITS_PER_UNIT;
1954 gnu_result = size_int (align);
1956 break;
1958 case Attr_First:
1959 case Attr_Last:
1960 case Attr_Range_Length:
1961 prefix_unused = true;
1963 if (INTEGRAL_TYPE_P (gnu_type) || TREE_CODE (gnu_type) == REAL_TYPE)
1965 gnu_result_type = get_unpadded_type (Etype (gnat_node));
1967 if (attribute == Attr_First)
1968 gnu_result = TYPE_MIN_VALUE (gnu_type);
1969 else if (attribute == Attr_Last)
1970 gnu_result = TYPE_MAX_VALUE (gnu_type);
1971 else
1972 gnu_result = get_type_length (gnu_type, gnu_result_type);
1973 break;
1976 /* ... fall through ... */
1978 case Attr_Length:
1980 int Dimension = (Present (Expressions (gnat_node))
1981 ? UI_To_Int (Intval (First (Expressions (gnat_node))))
1982 : 1), i;
1983 struct parm_attr_d *pa = NULL;
1984 Entity_Id gnat_param = Empty;
1985 bool unconstrained_ptr_deref = false;
1987 /* Make sure any implicit dereference gets done. */
1988 gnu_prefix = maybe_implicit_deref (gnu_prefix);
1989 gnu_prefix = maybe_unconstrained_array (gnu_prefix);
1991 /* We treat unconstrained array In parameters specially. We also note
1992 whether we are dereferencing a pointer to unconstrained array. */
1993 if (!Is_Constrained (Etype (gnat_prefix)))
1994 switch (Nkind (gnat_prefix))
1996 case N_Identifier:
1997 /* This is the direct case. */
1998 if (Ekind (Entity (gnat_prefix)) == E_In_Parameter)
1999 gnat_param = Entity (gnat_prefix);
2000 break;
2002 case N_Explicit_Dereference:
2003 /* This is the indirect case. Note that we need to be sure that
2004 the access value cannot be null as we'll hoist the load. */
2005 if (Nkind (Prefix (gnat_prefix)) == N_Identifier
2006 && Ekind (Entity (Prefix (gnat_prefix))) == E_In_Parameter)
2008 if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix))))
2009 gnat_param = Entity (Prefix (gnat_prefix));
2011 else
2012 unconstrained_ptr_deref = true;
2013 break;
2015 default:
2016 break;
2019 /* If the prefix is the view conversion of a constrained array to an
2020 unconstrained form, we retrieve the constrained array because we
2021 might not be able to substitute the PLACEHOLDER_EXPR coming from
2022 the conversion. This can occur with the 'Old attribute applied
2023 to a parameter with an unconstrained type, which gets rewritten
2024 into a constrained local variable very late in the game. */
2025 if (TREE_CODE (gnu_prefix) == VIEW_CONVERT_EXPR
2026 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix)))
2027 && !CONTAINS_PLACEHOLDER_P
2028 (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0)))))
2029 gnu_type = TREE_TYPE (TREE_OPERAND (gnu_prefix, 0));
2030 else
2031 gnu_type = TREE_TYPE (gnu_prefix);
2033 prefix_unused = true;
2034 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2036 if (TYPE_CONVENTION_FORTRAN_P (gnu_type))
2038 int ndim;
2039 tree gnu_type_temp;
2041 for (ndim = 1, gnu_type_temp = gnu_type;
2042 TREE_CODE (TREE_TYPE (gnu_type_temp)) == ARRAY_TYPE
2043 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp));
2044 ndim++, gnu_type_temp = TREE_TYPE (gnu_type_temp))
2047 Dimension = ndim + 1 - Dimension;
2050 for (i = 1; i < Dimension; i++)
2051 gnu_type = TREE_TYPE (gnu_type);
2053 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2055 /* When not optimizing, look up the slot associated with the parameter
2056 and the dimension in the cache and create a new one on failure.
2057 Don't do this when the actual subtype needs debug info (this happens
2058 with -gnatD): in elaborate_expression_1, we create variables that
2059 hold the bounds, so caching attributes isn't very interesting and
2060 causes dependency issues between these variables and cached
2061 expressions. */
2062 if (!optimize
2063 && Present (gnat_param)
2064 && !(Present (Actual_Subtype (gnat_param))
2065 && Needs_Debug_Info (Actual_Subtype (gnat_param))))
2067 FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache, i, pa)
2068 if (pa->id == gnat_param && pa->dim == Dimension)
2069 break;
2071 if (!pa)
2073 pa = ggc_cleared_alloc<parm_attr_d> ();
2074 pa->id = gnat_param;
2075 pa->dim = Dimension;
2076 vec_safe_push (f_parm_attr_cache, pa);
2080 /* Return the cached expression or build a new one. */
2081 if (attribute == Attr_First)
2083 if (pa && pa->first)
2085 gnu_result = pa->first;
2086 break;
2089 gnu_result
2090 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2093 else if (attribute == Attr_Last)
2095 if (pa && pa->last)
2097 gnu_result = pa->last;
2098 break;
2101 gnu_result
2102 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)));
2105 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
2107 if (pa && pa->length)
2109 gnu_result = pa->length;
2110 break;
2113 gnu_result
2114 = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type)),
2115 gnu_result_type);
2118 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2119 handling. Note that these attributes could not have been used on
2120 an unconstrained array type. */
2121 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2123 /* Cache the expression we have just computed. Since we want to do it
2124 at run time, we force the use of a SAVE_EXPR and let the gimplifier
2125 create the temporary in the outermost binding level. We will make
2126 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
2127 paths by forcing its evaluation on entry of the function. */
2128 if (pa)
2130 gnu_result
2131 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2132 switch (attribute)
2134 case Attr_First:
2135 pa->first = gnu_result;
2136 break;
2138 case Attr_Last:
2139 pa->last = gnu_result;
2140 break;
2142 case Attr_Length:
2143 case Attr_Range_Length:
2144 pa->length = gnu_result;
2145 break;
2147 default:
2148 gcc_unreachable ();
2152 /* Otherwise, evaluate it each time it is referenced. */
2153 else
2154 switch (attribute)
2156 case Attr_First:
2157 case Attr_Last:
2158 /* If we are dereferencing a pointer to unconstrained array, we
2159 need to capture the value because the pointed-to bounds may
2160 subsequently be released. */
2161 if (unconstrained_ptr_deref)
2162 gnu_result
2163 = build1 (SAVE_EXPR, TREE_TYPE (gnu_result), gnu_result);
2164 break;
2166 case Attr_Length:
2167 case Attr_Range_Length:
2168 /* Set the source location onto the predicate of the condition
2169 but not if the expression is cached to avoid messing up the
2170 debug info. */
2171 if (TREE_CODE (gnu_result) == COND_EXPR
2172 && EXPR_P (TREE_OPERAND (gnu_result, 0)))
2173 set_expr_location_from_node (TREE_OPERAND (gnu_result, 0),
2174 gnat_node);
2175 break;
2177 default:
2178 gcc_unreachable ();
2181 break;
2184 case Attr_Bit_Position:
2185 case Attr_Position:
2186 case Attr_First_Bit:
2187 case Attr_Last_Bit:
2188 case Attr_Bit:
2190 poly_int64 bitsize;
2191 poly_int64 bitpos;
2192 tree gnu_offset;
2193 tree gnu_field_bitpos;
2194 tree gnu_field_offset;
2195 tree gnu_inner;
2196 machine_mode mode;
2197 int unsignedp, reversep, volatilep;
2199 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2200 gnu_prefix = remove_conversions (gnu_prefix, true);
2201 prefix_unused = true;
2203 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
2204 the result is 0. Don't allow 'Bit on a bare component, though. */
2205 if (attribute == Attr_Bit
2206 && TREE_CODE (gnu_prefix) != COMPONENT_REF
2207 && TREE_CODE (gnu_prefix) != FIELD_DECL)
2209 gnu_result = integer_zero_node;
2210 break;
2213 else
2214 gcc_assert (TREE_CODE (gnu_prefix) == COMPONENT_REF
2215 || (attribute == Attr_Bit_Position
2216 && TREE_CODE (gnu_prefix) == FIELD_DECL));
2218 get_inner_reference (gnu_prefix, &bitsize, &bitpos, &gnu_offset,
2219 &mode, &unsignedp, &reversep, &volatilep);
2221 if (TREE_CODE (gnu_prefix) == COMPONENT_REF)
2223 gnu_field_bitpos = bit_position (TREE_OPERAND (gnu_prefix, 1));
2224 gnu_field_offset = byte_position (TREE_OPERAND (gnu_prefix, 1));
2226 for (gnu_inner = TREE_OPERAND (gnu_prefix, 0);
2227 TREE_CODE (gnu_inner) == COMPONENT_REF
2228 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner, 1));
2229 gnu_inner = TREE_OPERAND (gnu_inner, 0))
2231 gnu_field_bitpos
2232 = size_binop (PLUS_EXPR, gnu_field_bitpos,
2233 bit_position (TREE_OPERAND (gnu_inner, 1)));
2234 gnu_field_offset
2235 = size_binop (PLUS_EXPR, gnu_field_offset,
2236 byte_position (TREE_OPERAND (gnu_inner, 1)));
2239 else if (TREE_CODE (gnu_prefix) == FIELD_DECL)
2241 gnu_field_bitpos = bit_position (gnu_prefix);
2242 gnu_field_offset = byte_position (gnu_prefix);
2244 else
2246 gnu_field_bitpos = bitsize_zero_node;
2247 gnu_field_offset = size_zero_node;
2250 switch (attribute)
2252 case Attr_Position:
2253 gnu_result = gnu_field_offset;
2254 break;
2256 case Attr_First_Bit:
2257 case Attr_Bit:
2258 gnu_result = size_int (num_trailing_bits (bitpos));
2259 break;
2261 case Attr_Last_Bit:
2262 gnu_result = bitsize_int (num_trailing_bits (bitpos));
2263 gnu_result = size_binop (PLUS_EXPR, gnu_result,
2264 TYPE_SIZE (TREE_TYPE (gnu_prefix)));
2265 /* ??? Avoid a large unsigned result that will overflow when
2266 converted to the signed universal_integer. */
2267 if (integer_zerop (gnu_result))
2268 gnu_result = integer_minus_one_node;
2269 else
2270 gnu_result
2271 = size_binop (MINUS_EXPR, gnu_result, bitsize_one_node);
2272 break;
2274 case Attr_Bit_Position:
2275 gnu_result = gnu_field_bitpos;
2276 break;
2279 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2280 handling. */
2281 gnu_result = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result, gnu_prefix);
2282 break;
2285 case Attr_Min:
2286 case Attr_Max:
2288 tree gnu_lhs = gnat_to_gnu (First (Expressions (gnat_node)));
2289 tree gnu_rhs = gnat_to_gnu (Next (First (Expressions (gnat_node))));
2291 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2293 /* The result of {MIN,MAX}_EXPR is unspecified if either operand is
2294 a NaN so we implement the semantics of C99 f{min,max} to make it
2295 predictable in this case: if either operand is a NaN, the other
2296 is returned; if both operands are NaN's, a NaN is returned. */
2297 if (SCALAR_FLOAT_TYPE_P (gnu_result_type)
2298 && !Machine_Overflows_On_Target)
2300 const bool lhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_lhs);
2301 const bool rhs_side_effects_p = TREE_SIDE_EFFECTS (gnu_rhs);
2302 tree t = builtin_decl_explicit (BUILT_IN_ISNAN);
2303 tree lhs_is_nan, rhs_is_nan;
2305 /* If the operands have side-effects, they need to be evaluated
2306 only once in spite of the multiple references in the result. */
2307 if (lhs_side_effects_p)
2308 gnu_lhs = gnat_protect_expr (gnu_lhs);
2309 if (rhs_side_effects_p)
2310 gnu_rhs = gnat_protect_expr (gnu_rhs);
2312 lhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2313 build_call_expr (t, 1, gnu_lhs),
2314 integer_zero_node);
2316 rhs_is_nan = fold_build2 (NE_EXPR, boolean_type_node,
2317 build_call_expr (t, 1, gnu_rhs),
2318 integer_zero_node);
2320 gnu_result = build_binary_op (attribute == Attr_Min
2321 ? MIN_EXPR : MAX_EXPR,
2322 gnu_result_type, gnu_lhs, gnu_rhs);
2323 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2324 rhs_is_nan, gnu_lhs, gnu_result);
2325 gnu_result = fold_build3 (COND_EXPR, gnu_result_type,
2326 lhs_is_nan, gnu_rhs, gnu_result);
2328 /* If the operands have side-effects, they need to be evaluated
2329 before doing the tests above since the place they otherwise
2330 would end up being evaluated at run time could be wrong. */
2331 if (lhs_side_effects_p)
2332 gnu_result
2333 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_lhs, gnu_result);
2335 if (rhs_side_effects_p)
2336 gnu_result
2337 = build2 (COMPOUND_EXPR, gnu_result_type, gnu_rhs, gnu_result);
2339 else
2340 gnu_result = build_binary_op (attribute == Attr_Min
2341 ? MIN_EXPR : MAX_EXPR,
2342 gnu_result_type, gnu_lhs, gnu_rhs);
2344 break;
2346 case Attr_Passed_By_Reference:
2347 gnu_result = size_int (default_pass_by_ref (gnu_type)
2348 || must_pass_by_ref (gnu_type));
2349 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2350 break;
2352 case Attr_Component_Size:
2353 if (TREE_CODE (gnu_prefix) == COMPONENT_REF
2354 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix, 0))))
2355 gnu_prefix = TREE_OPERAND (gnu_prefix, 0);
2357 gnu_prefix = maybe_implicit_deref (gnu_prefix);
2358 gnu_type = TREE_TYPE (gnu_prefix);
2360 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
2361 gnu_type = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type))));
2363 while (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2364 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2365 gnu_type = TREE_TYPE (gnu_type);
2367 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
2369 /* Note this size cannot be self-referential. */
2370 gnu_result = TYPE_SIZE (TREE_TYPE (gnu_type));
2371 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2372 prefix_unused = true;
2373 break;
2375 case Attr_Descriptor_Size:
2376 gnu_type = TREE_TYPE (gnu_prefix);
2377 gcc_assert (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE);
2379 /* What we want is the offset of the ARRAY field in the record
2380 that the thin pointer designates. */
2381 gnu_type = TYPE_OBJECT_RECORD_TYPE (gnu_type);
2382 gnu_result = bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type)));
2383 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2384 prefix_unused = true;
2385 break;
2387 case Attr_Null_Parameter:
2388 /* This is just a zero cast to the pointer type for our prefix and
2389 dereferenced. */
2390 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2391 gnu_result
2392 = build_unary_op (INDIRECT_REF, NULL_TREE,
2393 convert (build_pointer_type (gnu_result_type),
2394 integer_zero_node));
2395 TREE_PRIVATE (gnu_result) = 1;
2396 break;
2398 case Attr_Mechanism_Code:
2400 Entity_Id gnat_obj = Entity (gnat_prefix);
2401 int code;
2403 prefix_unused = true;
2404 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2405 if (Present (Expressions (gnat_node)))
2407 int i = UI_To_Int (Intval (First (Expressions (gnat_node))));
2409 for (gnat_obj = First_Formal (gnat_obj); i > 1;
2410 i--, gnat_obj = Next_Formal (gnat_obj))
2414 code = Mechanism (gnat_obj);
2415 if (code == Default)
2416 code = ((present_gnu_tree (gnat_obj)
2417 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj))
2418 || ((TREE_CODE (get_gnu_tree (gnat_obj))
2419 == PARM_DECL)
2420 && (DECL_BY_COMPONENT_PTR_P
2421 (get_gnu_tree (gnat_obj))))))
2422 ? By_Reference : By_Copy);
2423 gnu_result = convert (gnu_result_type, size_int (- code));
2425 break;
2427 case Attr_Model:
2428 /* We treat Model as identical to Machine. This is true for at least
2429 IEEE and some other nice floating-point systems. */
2431 /* ... fall through ... */
2433 case Attr_Machine:
2434 /* The trick is to force the compiler to store the result in memory so
2435 that we do not have extra precision used. But do this only when this
2436 is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of
2437 the type is lower than that of the longest floating-point type. */
2438 prefix_unused = true;
2439 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2440 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2441 gnu_result = convert (gnu_result_type, gnu_expr);
2443 if (TREE_CODE (gnu_result) != REAL_CST
2444 && fp_arith_may_widen
2445 && TYPE_PRECISION (gnu_result_type)
2446 < TYPE_PRECISION (longest_float_type_node))
2448 tree rec_type = make_node (RECORD_TYPE);
2449 tree field
2450 = create_field_decl (get_identifier ("OBJ"), gnu_result_type,
2451 rec_type, NULL_TREE, NULL_TREE, 0, 0);
2452 tree rec_val, asm_expr;
2454 finish_record_type (rec_type, field, 0, false);
2456 rec_val = build_constructor_single (rec_type, field, gnu_result);
2457 rec_val = build1 (SAVE_EXPR, rec_type, rec_val);
2459 asm_expr
2460 = build5 (ASM_EXPR, void_type_node,
2461 build_string (0, ""),
2462 tree_cons (build_tree_list (NULL_TREE,
2463 build_string (2, "=m")),
2464 rec_val, NULL_TREE),
2465 tree_cons (build_tree_list (NULL_TREE,
2466 build_string (1, "m")),
2467 rec_val, NULL_TREE),
2468 NULL_TREE, NULL_TREE);
2469 ASM_VOLATILE_P (asm_expr) = 1;
2471 gnu_result
2472 = build_compound_expr (gnu_result_type, asm_expr,
2473 build_component_ref (rec_val, field,
2474 false));
2476 break;
2478 case Attr_Deref:
2479 prefix_unused = true;
2480 gnu_expr = gnat_to_gnu (First (Expressions (gnat_node)));
2481 gnu_result_type = get_unpadded_type (Etype (gnat_node));
2482 /* This can be a random address so build an alias-all pointer type. */
2483 gnu_expr
2484 = convert (build_pointer_type_for_mode (gnu_result_type, ptr_mode,
2485 true),
2486 gnu_expr);
2487 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_expr);
2488 break;
2490 default:
2491 /* This abort means that we have an unimplemented attribute. */
2492 gcc_unreachable ();
2495 /* If this is an attribute where the prefix was unused, force a use of it if
2496 it has a side-effect. But don't do it if the prefix is just an entity
2497 name. However, if an access check is needed, we must do it. See second
2498 example in AARM 11.6(5.e). */
2499 if (prefix_unused
2500 && TREE_SIDE_EFFECTS (gnu_prefix)
2501 && !Is_Entity_Name (gnat_prefix))
2502 gnu_result
2503 = build_compound_expr (TREE_TYPE (gnu_result), gnu_prefix, gnu_result);
2505 *gnu_result_type_p = gnu_result_type;
2506 return gnu_result;
2509 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
2510 to a GCC tree, which is returned. */
2512 static tree
2513 Case_Statement_to_gnu (Node_Id gnat_node)
2515 tree gnu_result, gnu_expr, gnu_type, gnu_label;
2516 Node_Id gnat_when;
2517 location_t end_locus;
2518 bool may_fallthru = false;
2520 gnu_expr = gnat_to_gnu (Expression (gnat_node));
2521 gnu_expr = convert (get_base_type (TREE_TYPE (gnu_expr)), gnu_expr);
2522 gnu_expr = maybe_character_value (gnu_expr);
2523 gnu_type = TREE_TYPE (gnu_expr);
2525 /* We build a SWITCH_EXPR that contains the code with interspersed
2526 CASE_LABEL_EXPRs for each label. */
2527 if (!Sloc_to_locus (End_Location (gnat_node), &end_locus))
2528 end_locus = input_location;
2529 gnu_label = create_artificial_label (end_locus);
2530 start_stmt_group ();
2532 for (gnat_when = First_Non_Pragma (Alternatives (gnat_node));
2533 Present (gnat_when);
2534 gnat_when = Next_Non_Pragma (gnat_when))
2536 bool choices_added_p = false;
2537 Node_Id gnat_choice;
2539 /* First compile all the different case choices for the current WHEN
2540 alternative. */
2541 for (gnat_choice = First (Discrete_Choices (gnat_when));
2542 Present (gnat_choice);
2543 gnat_choice = Next (gnat_choice))
2545 tree gnu_low = NULL_TREE, gnu_high = NULL_TREE;
2546 tree label = create_artificial_label (input_location);
2548 switch (Nkind (gnat_choice))
2550 case N_Range:
2551 gnu_low = gnat_to_gnu (Low_Bound (gnat_choice));
2552 gnu_high = gnat_to_gnu (High_Bound (gnat_choice));
2553 break;
2555 case N_Subtype_Indication:
2556 gnu_low = gnat_to_gnu (Low_Bound (Range_Expression
2557 (Constraint (gnat_choice))));
2558 gnu_high = gnat_to_gnu (High_Bound (Range_Expression
2559 (Constraint (gnat_choice))));
2560 break;
2562 case N_Identifier:
2563 case N_Expanded_Name:
2564 /* This represents either a subtype range or a static value of
2565 some kind; Ekind says which. */
2566 if (Is_Type (Entity (gnat_choice)))
2568 tree gnu_type = get_unpadded_type (Entity (gnat_choice));
2570 gnu_low = TYPE_MIN_VALUE (gnu_type);
2571 gnu_high = TYPE_MAX_VALUE (gnu_type);
2572 break;
2575 /* ... fall through ... */
2577 case N_Character_Literal:
2578 case N_Integer_Literal:
2579 gnu_low = gnat_to_gnu (gnat_choice);
2580 break;
2582 case N_Others_Choice:
2583 break;
2585 default:
2586 gcc_unreachable ();
2589 /* Everything should be folded into constants at this point. */
2590 gcc_assert (!gnu_low || TREE_CODE (gnu_low) == INTEGER_CST);
2591 gcc_assert (!gnu_high || TREE_CODE (gnu_high) == INTEGER_CST);
2593 if (gnu_low && TREE_TYPE (gnu_low) != gnu_type)
2594 gnu_low = convert (gnu_type, gnu_low);
2595 if (gnu_high && TREE_TYPE (gnu_high) != gnu_type)
2596 gnu_high = convert (gnu_type, gnu_high);
2598 add_stmt_with_node (build_case_label (gnu_low, gnu_high, label),
2599 gnat_choice);
2600 choices_added_p = true;
2603 /* This construct doesn't define a scope so we shouldn't push a binding
2604 level around the statement list. Except that we have always done so
2605 historically and this makes it possible to reduce stack usage. As a
2606 compromise, we keep doing it for case statements, for which this has
2607 never been problematic, but not for case expressions in Ada 2012. */
2608 if (choices_added_p)
2610 const bool is_case_expression
2611 = (Nkind (Parent (gnat_node)) == N_Expression_With_Actions);
2612 tree group
2613 = build_stmt_group (Statements (gnat_when), !is_case_expression);
2614 bool group_may_fallthru = block_may_fallthru (group);
2615 add_stmt (group);
2616 if (group_may_fallthru)
2618 tree stmt = build1 (GOTO_EXPR, void_type_node, gnu_label);
2619 SET_EXPR_LOCATION (stmt, end_locus);
2620 add_stmt (stmt);
2621 may_fallthru = true;
2626 /* Now emit a definition of the label the cases branch to, if any. */
2627 if (may_fallthru)
2628 add_stmt (build1 (LABEL_EXPR, void_type_node, gnu_label));
2629 gnu_result = build2 (SWITCH_EXPR, gnu_type, gnu_expr, end_stmt_group ());
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 (IN_RANGE (n_remaining_checks, 1, 3)
3194 && optimize > 1
3195 && !optimize_size)
3196 FOR_EACH_VEC_ELT (*gnu_loop_info->checks, i, rci)
3197 if (rci->invariant_cond != boolean_false_node)
3199 TREE_OPERAND (rci->inserted_cond, 0) = rci->invariant_cond;
3201 if (flag_unswitch_loops)
3202 add_stmt_with_node_force (rci->inserted_cond, gnat_node);
3206 /* Second, if loop vectorization is enabled and the iterations of the
3207 loop can easily be proved as independent, mark the loop. */
3208 if (optimize
3209 && flag_tree_loop_vectorize
3210 && independent_iterations_p (LOOP_STMT_BODY (gnu_loop_stmt)))
3211 LOOP_STMT_IVDEP (gnu_loop_stmt) = 1;
3213 add_stmt (gnu_loop_stmt);
3214 gnat_poplevel ();
3215 gnu_loop_stmt = end_stmt_group ();
3218 /* If we have an outer COND_EXPR, that's our result and this loop is its
3219 "true" statement. Otherwise, the result is the LOOP_STMT. */
3220 if (gnu_cond_expr)
3222 COND_EXPR_THEN (gnu_cond_expr) = gnu_loop_stmt;
3223 TREE_SIDE_EFFECTS (gnu_cond_expr) = 1;
3224 gnu_result = gnu_cond_expr;
3226 else
3227 gnu_result = gnu_loop_stmt;
3229 gnu_loop_stack->pop ();
3231 return gnu_result;
3234 /* This page implements a form of Named Return Value optimization modelled
3235 on the C++ optimization of the same name. The main difference is that
3236 we disregard any semantical considerations when applying it here, the
3237 counterpart being that we don't try to apply it to semantically loaded
3238 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
3240 We consider a function body of the following GENERIC form:
3242 return_type R1;
3243 [...]
3244 RETURN_EXPR [<retval> = ...]
3245 [...]
3246 RETURN_EXPR [<retval> = R1]
3247 [...]
3248 return_type Ri;
3249 [...]
3250 RETURN_EXPR [<retval> = ...]
3251 [...]
3252 RETURN_EXPR [<retval> = Ri]
3253 [...]
3255 where the Ri are not addressable and we try to fulfill a simple criterion
3256 that would make it possible to replace one or several Ri variables by the
3257 single RESULT_DECL of the function.
3259 The first observation is that RETURN_EXPRs that don't directly reference
3260 any of the Ri variables on the RHS of their assignment are transparent wrt
3261 the optimization. This is because the Ri variables aren't addressable so
3262 any transformation applied to them doesn't affect the RHS; moreover, the
3263 assignment writes the full <retval> object so existing values are entirely
3264 discarded.
3266 This property can be extended to some forms of RETURN_EXPRs that reference
3267 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3268 case, in particular when function calls are involved.
3270 Therefore the algorithm is as follows:
3272 1. Collect the list of candidates for a Named Return Value (Ri variables
3273 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3274 other expressions on the RHS of such assignments.
3276 2. Prune the members of the first list (candidates) that are referenced
3277 by a member of the second list (expressions).
3279 3. Extract a set of candidates with non-overlapping live ranges from the
3280 first list. These are the Named Return Values.
3282 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
3283 Named Return Values in the function with the RESULT_DECL.
3285 If the function returns an unconstrained type, things are a bit different
3286 because the anonymous return object is allocated on the secondary stack
3287 and RESULT_DECL is only a pointer to it. Each return object can be of a
3288 different size and is allocated separately so we need not care about the
3289 addressability and the aforementioned overlapping issues. Therefore, we
3290 don't collect the other expressions and skip step #2 in the algorithm. */
3292 struct nrv_data
3294 bitmap nrv;
3295 tree result;
3296 Node_Id gnat_ret;
3297 hash_set<tree> *visited;
3300 /* Return true if T is a Named Return Value. */
3302 static inline bool
3303 is_nrv_p (bitmap nrv, tree t)
3305 return TREE_CODE (t) == VAR_DECL && bitmap_bit_p (nrv, DECL_UID (t));
3308 /* Helper function for walk_tree, used by finalize_nrv below. */
3310 static tree
3311 prune_nrv_r (tree *tp, int *walk_subtrees, void *data)
3313 struct nrv_data *dp = (struct nrv_data *)data;
3314 tree t = *tp;
3316 /* No need to walk into types or decls. */
3317 if (IS_TYPE_OR_DECL_P (t))
3318 *walk_subtrees = 0;
3320 if (is_nrv_p (dp->nrv, t))
3321 bitmap_clear_bit (dp->nrv, DECL_UID (t));
3323 return NULL_TREE;
3326 /* Prune Named Return Values in BLOCK and return true if there is still a
3327 Named Return Value in BLOCK or one of its sub-blocks. */
3329 static bool
3330 prune_nrv_in_block (bitmap nrv, tree block)
3332 bool has_nrv = false;
3333 tree t;
3335 /* First recurse on the sub-blocks. */
3336 for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
3337 has_nrv |= prune_nrv_in_block (nrv, t);
3339 /* Then make sure to keep at most one NRV per block. */
3340 for (t = BLOCK_VARS (block); t; t = DECL_CHAIN (t))
3341 if (is_nrv_p (nrv, t))
3343 if (has_nrv)
3344 bitmap_clear_bit (nrv, DECL_UID (t));
3345 else
3346 has_nrv = true;
3349 return has_nrv;
3352 /* Helper function for walk_tree, used by finalize_nrv below. */
3354 static tree
3355 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
3357 struct nrv_data *dp = (struct nrv_data *)data;
3358 tree t = *tp;
3360 /* No need to walk into types. */
3361 if (TYPE_P (t))
3362 *walk_subtrees = 0;
3364 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3365 nop, but differs from using NULL_TREE in that it indicates that we care
3366 about the value of the RESULT_DECL. */
3367 else if (TREE_CODE (t) == RETURN_EXPR
3368 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3370 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3372 /* Strip useless conversions around the return value. */
3373 if (gnat_useless_type_conversion (ret_val))
3374 ret_val = TREE_OPERAND (ret_val, 0);
3376 if (is_nrv_p (dp->nrv, ret_val))
3377 TREE_OPERAND (t, 0) = dp->result;
3380 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3381 if needed. */
3382 else if (TREE_CODE (t) == DECL_EXPR
3383 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3385 tree var = DECL_EXPR_DECL (t), init;
3387 if (DECL_INITIAL (var))
3389 init = build_binary_op (INIT_EXPR, NULL_TREE, dp->result,
3390 DECL_INITIAL (var));
3391 SET_EXPR_LOCATION (init, EXPR_LOCATION (t));
3392 DECL_INITIAL (var) = NULL_TREE;
3394 else
3395 init = build_empty_stmt (EXPR_LOCATION (t));
3396 *tp = init;
3398 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3399 SET_DECL_VALUE_EXPR (var, dp->result);
3400 DECL_HAS_VALUE_EXPR_P (var) = 1;
3401 /* ??? Kludge to avoid an assertion failure during inlining. */
3402 DECL_SIZE (var) = bitsize_unit_node;
3403 DECL_SIZE_UNIT (var) = size_one_node;
3406 /* And replace all uses of NRVs with the RESULT_DECL. */
3407 else if (is_nrv_p (dp->nrv, t))
3408 *tp = convert (TREE_TYPE (t), dp->result);
3410 /* Avoid walking into the same tree more than once. Unfortunately, we
3411 can't just use walk_tree_without_duplicates because it would only
3412 call us for the first occurrence of NRVs in the function body. */
3413 if (dp->visited->add (*tp))
3414 *walk_subtrees = 0;
3416 return NULL_TREE;
3419 /* Likewise, but used when the function returns an unconstrained type. */
3421 static tree
3422 finalize_nrv_unc_r (tree *tp, int *walk_subtrees, void *data)
3424 struct nrv_data *dp = (struct nrv_data *)data;
3425 tree t = *tp;
3427 /* No need to walk into types. */
3428 if (TYPE_P (t))
3429 *walk_subtrees = 0;
3431 /* We need to see the DECL_EXPR of NRVs before any other references so we
3432 walk the body of BIND_EXPR before walking its variables. */
3433 else if (TREE_CODE (t) == BIND_EXPR)
3434 walk_tree (&BIND_EXPR_BODY (t), finalize_nrv_unc_r, data, NULL);
3436 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3437 return value built by the allocator instead of the whole construct. */
3438 else if (TREE_CODE (t) == RETURN_EXPR
3439 && TREE_CODE (TREE_OPERAND (t, 0)) == INIT_EXPR)
3441 tree ret_val = TREE_OPERAND (TREE_OPERAND (t, 0), 1);
3443 /* This is the construct returned by the allocator. */
3444 if (TREE_CODE (ret_val) == COMPOUND_EXPR
3445 && TREE_CODE (TREE_OPERAND (ret_val, 0)) == INIT_EXPR)
3447 tree rhs = TREE_OPERAND (TREE_OPERAND (ret_val, 0), 1);
3449 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val)))
3450 ret_val = CONSTRUCTOR_ELT (rhs, 1)->value;
3451 else
3452 ret_val = rhs;
3455 /* Strip useless conversions around the return value. */
3456 if (gnat_useless_type_conversion (ret_val)
3457 || TREE_CODE (ret_val) == VIEW_CONVERT_EXPR)
3458 ret_val = TREE_OPERAND (ret_val, 0);
3460 /* Strip unpadding around the return value. */
3461 if (TREE_CODE (ret_val) == COMPONENT_REF
3462 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
3463 ret_val = TREE_OPERAND (ret_val, 0);
3465 /* Assign the new return value to the RESULT_DECL. */
3466 if (is_nrv_p (dp->nrv, ret_val))
3467 TREE_OPERAND (TREE_OPERAND (t, 0), 1)
3468 = TREE_OPERAND (DECL_INITIAL (ret_val), 0);
3471 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3472 into a new variable. */
3473 else if (TREE_CODE (t) == DECL_EXPR
3474 && is_nrv_p (dp->nrv, DECL_EXPR_DECL (t)))
3476 tree saved_current_function_decl = current_function_decl;
3477 tree var = DECL_EXPR_DECL (t);
3478 tree alloc, p_array, new_var, new_ret;
3479 vec<constructor_elt, va_gc> *v;
3480 vec_alloc (v, 2);
3482 /* Create an artificial context to build the allocation. */
3483 current_function_decl = decl_function_context (var);
3484 start_stmt_group ();
3485 gnat_pushlevel ();
3487 /* This will return a COMPOUND_EXPR with the allocation in the first
3488 arm and the final return value in the second arm. */
3489 alloc = build_allocator (TREE_TYPE (var), DECL_INITIAL (var),
3490 TREE_TYPE (dp->result),
3491 Procedure_To_Call (dp->gnat_ret),
3492 Storage_Pool (dp->gnat_ret),
3493 Empty, false);
3495 /* The new variable is built as a reference to the allocated space. */
3496 new_var
3497 = build_decl (DECL_SOURCE_LOCATION (var), VAR_DECL, DECL_NAME (var),
3498 build_reference_type (TREE_TYPE (var)));
3499 DECL_BY_REFERENCE (new_var) = 1;
3501 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc)))
3503 tree cst = TREE_OPERAND (alloc, 1);
3505 /* The new initial value is a COMPOUND_EXPR with the allocation in
3506 the first arm and the value of P_ARRAY in the second arm. */
3507 DECL_INITIAL (new_var)
3508 = build2 (COMPOUND_EXPR, TREE_TYPE (new_var),
3509 TREE_OPERAND (alloc, 0),
3510 CONSTRUCTOR_ELT (cst, 0)->value);
3512 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3513 p_array = TYPE_FIELDS (TREE_TYPE (alloc));
3514 CONSTRUCTOR_APPEND_ELT (v, p_array,
3515 fold_convert (TREE_TYPE (p_array), new_var));
3516 CONSTRUCTOR_APPEND_ELT (v, DECL_CHAIN (p_array),
3517 CONSTRUCTOR_ELT (cst, 1)->value);
3518 new_ret = build_constructor (TREE_TYPE (alloc), v);
3520 else
3522 /* The new initial value is just the allocation. */
3523 DECL_INITIAL (new_var) = alloc;
3524 new_ret = fold_convert (TREE_TYPE (alloc), new_var);
3527 gnat_pushdecl (new_var, Empty);
3529 /* Destroy the artificial context and insert the new statements. */
3530 gnat_zaplevel ();
3531 *tp = end_stmt_group ();
3532 current_function_decl = saved_current_function_decl;
3534 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3535 DECL_CHAIN (new_var) = DECL_CHAIN (var);
3536 DECL_CHAIN (var) = new_var;
3537 DECL_IGNORED_P (var) = 1;
3539 /* Save the new return value and the dereference of NEW_VAR. */
3540 DECL_INITIAL (var)
3541 = build2 (COMPOUND_EXPR, TREE_TYPE (var), new_ret,
3542 build1 (INDIRECT_REF, TREE_TYPE (var), new_var));
3543 /* ??? Kludge to avoid messing up during inlining. */
3544 DECL_CONTEXT (var) = NULL_TREE;
3547 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3548 else if (is_nrv_p (dp->nrv, t))
3549 *tp = TREE_OPERAND (DECL_INITIAL (t), 1);
3551 /* Avoid walking into the same tree more than once. Unfortunately, we
3552 can't just use walk_tree_without_duplicates because it would only
3553 call us for the first occurrence of NRVs in the function body. */
3554 if (dp->visited->add (*tp))
3555 *walk_subtrees = 0;
3557 return NULL_TREE;
3560 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3561 contains the candidates for Named Return Value and OTHER is a list of
3562 the other return values. GNAT_RET is a representative return node. */
3564 static void
3565 finalize_nrv (tree fndecl, bitmap nrv, vec<tree, va_gc> *other, Node_Id gnat_ret)
3567 struct cgraph_node *node;
3568 struct nrv_data data;
3569 walk_tree_fn func;
3570 unsigned int i;
3571 tree iter;
3573 /* We shouldn't be applying the optimization to return types that we aren't
3574 allowed to manipulate freely. */
3575 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl))));
3577 /* Prune the candidates that are referenced by other return values. */
3578 data.nrv = nrv;
3579 data.result = NULL_TREE;
3580 data.gnat_ret = Empty;
3581 data.visited = NULL;
3582 FOR_EACH_VEC_SAFE_ELT (other, i, iter)
3583 walk_tree_without_duplicates (&iter, prune_nrv_r, &data);
3584 if (bitmap_empty_p (nrv))
3585 return;
3587 /* Prune also the candidates that are referenced by nested functions. */
3588 node = cgraph_node::get_create (fndecl);
3589 for (node = node->nested; node; node = node->next_nested)
3590 walk_tree_without_duplicates (&DECL_SAVED_TREE (node->decl), prune_nrv_r,
3591 &data);
3592 if (bitmap_empty_p (nrv))
3593 return;
3595 /* Extract a set of NRVs with non-overlapping live ranges. */
3596 if (!prune_nrv_in_block (nrv, DECL_INITIAL (fndecl)))
3597 return;
3599 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3600 data.nrv = nrv;
3601 data.result = DECL_RESULT (fndecl);
3602 data.gnat_ret = gnat_ret;
3603 data.visited = new hash_set<tree>;
3604 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl)))
3605 func = finalize_nrv_unc_r;
3606 else
3607 func = finalize_nrv_r;
3608 walk_tree (&DECL_SAVED_TREE (fndecl), func, &data, NULL);
3609 delete data.visited;
3612 /* Return true if RET_VAL can be used as a Named Return Value for the
3613 anonymous return object RET_OBJ. */
3615 static bool
3616 return_value_ok_for_nrv_p (tree ret_obj, tree ret_val)
3618 if (TREE_CODE (ret_val) != VAR_DECL)
3619 return false;
3621 if (TREE_THIS_VOLATILE (ret_val))
3622 return false;
3624 if (DECL_CONTEXT (ret_val) != current_function_decl)
3625 return false;
3627 if (TREE_STATIC (ret_val))
3628 return false;
3630 /* For the constrained case, test for addressability. */
3631 if (ret_obj && TREE_ADDRESSABLE (ret_val))
3632 return false;
3634 /* For the constrained case, test for overalignment. */
3635 if (ret_obj && DECL_ALIGN (ret_val) > DECL_ALIGN (ret_obj))
3636 return false;
3638 /* For the unconstrained case, test for bogus initialization. */
3639 if (!ret_obj
3640 && DECL_INITIAL (ret_val)
3641 && TREE_CODE (DECL_INITIAL (ret_val)) == NULL_EXPR)
3642 return false;
3644 return true;
3647 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3648 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3649 around RESULT_OBJ, which may be null in this case. */
3651 static tree
3652 build_return_expr (tree ret_obj, tree ret_val)
3654 tree result_expr;
3656 if (ret_val)
3658 /* The gimplifier explicitly enforces the following invariant:
3660 RETURN_EXPR
3662 INIT_EXPR
3665 RET_OBJ ...
3667 As a consequence, type consistency dictates that we use the type
3668 of the RET_OBJ as the operation type. */
3669 tree operation_type = TREE_TYPE (ret_obj);
3671 /* Convert the right operand to the operation type. Note that this is
3672 the transformation applied in the INIT_EXPR case of build_binary_op,
3673 with the assumption that the type cannot involve a placeholder. */
3674 if (operation_type != TREE_TYPE (ret_val))
3675 ret_val = convert (operation_type, ret_val);
3677 /* We always can use an INIT_EXPR for the return object. */
3678 result_expr = build2 (INIT_EXPR, void_type_node, ret_obj, ret_val);
3680 /* If the function returns an aggregate type, find out whether this is
3681 a candidate for Named Return Value. If so, record it. Otherwise,
3682 if this is an expression of some kind, record it elsewhere. */
3683 if (optimize
3684 && AGGREGATE_TYPE_P (operation_type)
3685 && !TYPE_IS_FAT_POINTER_P (operation_type)
3686 && TYPE_MODE (operation_type) == BLKmode
3687 && aggregate_value_p (operation_type, current_function_decl))
3689 /* Strip useless conversions around the return value. */
3690 if (gnat_useless_type_conversion (ret_val))
3691 ret_val = TREE_OPERAND (ret_val, 0);
3693 /* Now apply the test to the return value. */
3694 if (return_value_ok_for_nrv_p (ret_obj, ret_val))
3696 if (!f_named_ret_val)
3697 f_named_ret_val = BITMAP_GGC_ALLOC ();
3698 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
3701 /* Note that we need not care about CONSTRUCTORs here, as they are
3702 totally transparent given the read-compose-write semantics of
3703 assignments from CONSTRUCTORs. */
3704 else if (EXPR_P (ret_val))
3705 vec_safe_push (f_other_ret_val, ret_val);
3708 else
3709 result_expr = ret_obj;
3711 return build1 (RETURN_EXPR, void_type_node, result_expr);
3714 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3715 don't return anything. */
3717 static void
3718 Subprogram_Body_to_gnu (Node_Id gnat_node)
3720 /* Defining identifier of a parameter to the subprogram. */
3721 Entity_Id gnat_param;
3722 /* The defining identifier for the subprogram body. Note that if a
3723 specification has appeared before for this body, then the identifier
3724 occurring in that specification will also be a defining identifier and all
3725 the calls to this subprogram will point to that specification. */
3726 Entity_Id gnat_subprog_id
3727 = (Present (Corresponding_Spec (gnat_node))
3728 ? Corresponding_Spec (gnat_node) : Defining_Entity (gnat_node));
3729 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3730 tree gnu_subprog_decl;
3731 /* Its RESULT_DECL node. */
3732 tree gnu_result_decl;
3733 /* Its FUNCTION_TYPE node. */
3734 tree gnu_subprog_type;
3735 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3736 tree gnu_cico_list;
3737 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3738 tree gnu_return_var_elmt = NULL_TREE;
3739 tree gnu_result;
3740 location_t locus;
3741 struct language_function *gnu_subprog_language;
3742 vec<parm_attr, va_gc> *cache;
3744 /* If this is a generic object or if it has been eliminated,
3745 ignore it. */
3746 if (Ekind (gnat_subprog_id) == E_Generic_Procedure
3747 || Ekind (gnat_subprog_id) == E_Generic_Function
3748 || Is_Eliminated (gnat_subprog_id))
3749 return;
3751 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3752 the already-elaborated tree node. However, if this subprogram had its
3753 elaboration deferred, we will already have made a tree node for it. So
3754 treat it as not being defined in that case. Such a subprogram cannot
3755 have an address clause or a freeze node, so this test is safe, though it
3756 does disable some otherwise-useful error checking. */
3757 gnu_subprog_decl
3758 = gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE,
3759 Acts_As_Spec (gnat_node)
3760 && !present_gnu_tree (gnat_subprog_id));
3761 DECL_FUNCTION_IS_DEF (gnu_subprog_decl) = true;
3762 gnu_result_decl = DECL_RESULT (gnu_subprog_decl);
3763 gnu_subprog_type = TREE_TYPE (gnu_subprog_decl);
3764 gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
3765 if (gnu_cico_list && TREE_VALUE (gnu_cico_list) == void_type_node)
3766 gnu_return_var_elmt = gnu_cico_list;
3768 /* If the function returns by invisible reference, make it explicit in the
3769 function body. See gnat_to_gnu_entity, E_Subprogram_Type case. */
3770 if (TREE_ADDRESSABLE (gnu_subprog_type))
3772 TREE_TYPE (gnu_result_decl)
3773 = build_reference_type (TREE_TYPE (gnu_result_decl));
3774 relayout_decl (gnu_result_decl);
3777 /* Set the line number in the decl to correspond to that of the body. */
3778 if (!Sloc_to_locus (Sloc (gnat_node), &locus))
3779 locus = input_location;
3780 DECL_SOURCE_LOCATION (gnu_subprog_decl) = locus;
3782 /* If the body comes from an expression function, arrange it to be inlined
3783 in almost all cases. */
3784 if (Was_Expression_Function (gnat_node))
3785 DECL_DISREGARD_INLINE_LIMITS (gnu_subprog_decl) = 1;
3787 /* Initialize the information structure for the function. */
3788 allocate_struct_function (gnu_subprog_decl, false);
3789 gnu_subprog_language = ggc_cleared_alloc<language_function> ();
3790 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->language = gnu_subprog_language;
3791 DECL_STRUCT_FUNCTION (gnu_subprog_decl)->function_start_locus = locus;
3792 set_cfun (NULL);
3794 begin_subprog_body (gnu_subprog_decl);
3796 /* If there are copy-in/copy-out parameters, we need to ensure that they are
3797 properly copied out by the return statement. We do this by making a new
3798 block and converting any return into a goto to a label at the end of the
3799 block. */
3800 if (gnu_cico_list)
3802 tree gnu_return_var = NULL_TREE;
3804 vec_safe_push (gnu_return_label_stack,
3805 create_artificial_label (input_location));
3807 start_stmt_group ();
3808 gnat_pushlevel ();
3810 /* If this is a function with copy-in/copy-out parameters and which does
3811 not return by invisible reference, we also need a variable for the
3812 return value to be placed. */
3813 if (gnu_return_var_elmt && !TREE_ADDRESSABLE (gnu_subprog_type))
3815 tree gnu_return_type
3816 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt));
3818 gnu_return_var
3819 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE,
3820 gnu_return_type, NULL_TREE,
3821 false, false, false, false, false,
3822 true, false, NULL, gnat_subprog_id);
3823 TREE_VALUE (gnu_return_var_elmt) = gnu_return_var;
3826 vec_safe_push (gnu_return_var_stack, gnu_return_var);
3828 /* See whether there are parameters for which we don't have a GCC tree
3829 yet. These must be Out parameters. Make a VAR_DECL for them and
3830 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3831 We can match up the entries because TYPE_CI_CO_LIST is in the order
3832 of the parameters. */
3833 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3834 Present (gnat_param);
3835 gnat_param = Next_Formal_With_Extras (gnat_param))
3836 if (!present_gnu_tree (gnat_param))
3838 tree gnu_cico_entry = gnu_cico_list;
3839 tree gnu_decl;
3841 /* Skip any entries that have been already filled in; they must
3842 correspond to In Out parameters. */
3843 while (gnu_cico_entry && TREE_VALUE (gnu_cico_entry))
3844 gnu_cico_entry = TREE_CHAIN (gnu_cico_entry);
3846 /* Do any needed dereferences for by-ref objects. */
3847 gnu_decl = gnat_to_gnu_entity (gnat_param, NULL_TREE, true);
3848 gcc_assert (DECL_P (gnu_decl));
3849 if (DECL_BY_REF_P (gnu_decl))
3850 gnu_decl = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_decl);
3852 /* Do any needed references for padded types. */
3853 TREE_VALUE (gnu_cico_entry)
3854 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry)), gnu_decl);
3857 else
3858 vec_safe_push (gnu_return_label_stack, NULL_TREE);
3860 /* Get a tree corresponding to the code for the subprogram. */
3861 start_stmt_group ();
3862 gnat_pushlevel ();
3864 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
3866 /* Generate the code of the subprogram itself. A return statement will be
3867 present and any Out parameters will be handled there. */
3868 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
3869 gnat_poplevel ();
3870 gnu_result = end_stmt_group ();
3872 /* If we populated the parameter attributes cache, we need to make sure that
3873 the cached expressions are evaluated on all the possible paths leading to
3874 their uses. So we force their evaluation on entry of the function. */
3875 cache = gnu_subprog_language->parm_attr_cache;
3876 if (cache)
3878 struct parm_attr_d *pa;
3879 int i;
3881 start_stmt_group ();
3883 FOR_EACH_VEC_ELT (*cache, i, pa)
3885 if (pa->first)
3886 add_stmt_with_node_force (pa->first, gnat_node);
3887 if (pa->last)
3888 add_stmt_with_node_force (pa->last, gnat_node);
3889 if (pa->length)
3890 add_stmt_with_node_force (pa->length, gnat_node);
3893 add_stmt (gnu_result);
3894 gnu_result = end_stmt_group ();
3896 gnu_subprog_language->parm_attr_cache = NULL;
3899 /* If we are dealing with a return from an Ada procedure with parameters
3900 passed by copy-in/copy-out, we need to return a record containing the
3901 final values of these parameters. If the list contains only one entry,
3902 return just that entry though.
3904 For a full description of the copy-in/copy-out parameter mechanism, see
3905 the part of the gnat_to_gnu_entity routine dealing with the translation
3906 of subprograms.
3908 We need to make a block that contains the definition of that label and
3909 the copying of the return value. It first contains the function, then
3910 the label and copy statement. */
3911 if (gnu_cico_list)
3913 const Node_Id gnat_end_label
3914 = End_Label (Handled_Statement_Sequence (gnat_node));
3916 gnu_return_var_stack->pop ();
3918 add_stmt (gnu_result);
3919 add_stmt (build1 (LABEL_EXPR, void_type_node,
3920 gnu_return_label_stack->last ()));
3922 /* If this is a function which returns by invisible reference, the
3923 return value has already been dealt with at the return statements,
3924 so we only need to indirectly copy out the parameters. */
3925 if (TREE_ADDRESSABLE (gnu_subprog_type))
3927 tree gnu_ret_deref
3928 = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result_decl);
3929 tree t;
3931 gcc_assert (TREE_VALUE (gnu_cico_list) == void_type_node);
3933 for (t = TREE_CHAIN (gnu_cico_list); t; t = TREE_CHAIN (t))
3935 tree gnu_field_deref
3936 = build_component_ref (gnu_ret_deref, TREE_PURPOSE (t), true);
3937 gnu_result = build2 (MODIFY_EXPR, void_type_node,
3938 gnu_field_deref, TREE_VALUE (t));
3939 add_stmt_with_node (gnu_result, gnat_end_label);
3943 /* Otherwise, if this is a procedure or a function which does not return
3944 by invisible reference, we can do a direct block-copy out. */
3945 else
3947 tree gnu_retval;
3949 if (list_length (gnu_cico_list) == 1)
3950 gnu_retval = TREE_VALUE (gnu_cico_list);
3951 else
3952 gnu_retval
3953 = build_constructor_from_list (TREE_TYPE (gnu_subprog_type),
3954 gnu_cico_list);
3956 gnu_result = build_return_expr (gnu_result_decl, gnu_retval);
3957 add_stmt_with_node (gnu_result, gnat_end_label);
3960 gnat_poplevel ();
3961 gnu_result = end_stmt_group ();
3964 gnu_return_label_stack->pop ();
3966 /* Attempt setting the end_locus of our GCC body tree, typically a
3967 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3968 declaration tree. */
3969 set_end_locus_from_node (gnu_result, gnat_node);
3970 set_end_locus_from_node (gnu_subprog_decl, gnat_node);
3972 /* On SEH targets, install an exception handler around the main entry
3973 point to catch unhandled exceptions. */
3974 if (DECL_NAME (gnu_subprog_decl) == main_identifier_node
3975 && targetm_common.except_unwind_info (&global_options) == UI_SEH)
3977 tree t;
3978 tree etype;
3980 t = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
3981 1, integer_zero_node);
3982 t = build_call_n_expr (unhandled_except_decl, 1, t);
3984 etype = build_unary_op (ADDR_EXPR, NULL_TREE, unhandled_others_decl);
3985 etype = tree_cons (NULL_TREE, etype, NULL_TREE);
3987 t = build2 (CATCH_EXPR, void_type_node, etype, t);
3988 gnu_result = build2 (TRY_CATCH_EXPR, TREE_TYPE (gnu_result),
3989 gnu_result, t);
3992 end_subprog_body (gnu_result);
3994 /* Finally annotate the parameters and disconnect the trees for parameters
3995 that we have turned into variables since they are now unusable. */
3996 for (gnat_param = First_Formal_With_Extras (gnat_subprog_id);
3997 Present (gnat_param);
3998 gnat_param = Next_Formal_With_Extras (gnat_param))
4000 tree gnu_param = get_gnu_tree (gnat_param);
4001 bool is_var_decl = (TREE_CODE (gnu_param) == VAR_DECL);
4003 annotate_object (gnat_param, TREE_TYPE (gnu_param), NULL_TREE,
4004 DECL_BY_REF_P (gnu_param));
4006 if (is_var_decl)
4007 save_gnu_tree (gnat_param, NULL_TREE, false);
4010 /* Disconnect the variable created for the return value. */
4011 if (gnu_return_var_elmt)
4012 TREE_VALUE (gnu_return_var_elmt) = void_type_node;
4014 /* If the function returns an aggregate type and we have candidates for
4015 a Named Return Value, finalize the optimization. */
4016 if (optimize && gnu_subprog_language->named_ret_val)
4018 finalize_nrv (gnu_subprog_decl,
4019 gnu_subprog_language->named_ret_val,
4020 gnu_subprog_language->other_ret_val,
4021 gnu_subprog_language->gnat_ret);
4022 gnu_subprog_language->named_ret_val = NULL;
4023 gnu_subprog_language->other_ret_val = NULL;
4026 /* If this is an inlined external function that has been marked uninlinable,
4027 drop the body and stop there. Otherwise compile the body. */
4028 if (DECL_EXTERNAL (gnu_subprog_decl) && DECL_UNINLINABLE (gnu_subprog_decl))
4029 DECL_SAVED_TREE (gnu_subprog_decl) = NULL_TREE;
4030 else
4031 rest_of_subprog_body_compilation (gnu_subprog_decl);
4034 /* Return true if GNAT_NODE references an Atomic entity. */
4036 static bool
4037 node_is_atomic (Node_Id gnat_node)
4039 Entity_Id gnat_entity;
4041 switch (Nkind (gnat_node))
4043 case N_Identifier:
4044 case N_Expanded_Name:
4045 gnat_entity = Entity (gnat_node);
4046 if (Ekind (gnat_entity) != E_Variable)
4047 break;
4048 return Is_Atomic (gnat_entity) || Is_Atomic (Etype (gnat_entity));
4050 case N_Selected_Component:
4051 gnat_entity = Entity (Selector_Name (gnat_node));
4052 return Is_Atomic (gnat_entity) || Is_Atomic (Etype (gnat_entity));
4054 case N_Indexed_Component:
4055 if (Has_Atomic_Components (Etype (Prefix (gnat_node))))
4056 return true;
4057 if (Is_Entity_Name (Prefix (gnat_node))
4058 && Has_Atomic_Components (Entity (Prefix (gnat_node))))
4059 return true;
4061 /* ... fall through ... */
4063 case N_Explicit_Dereference:
4064 return Is_Atomic (Etype (gnat_node));
4066 default:
4067 break;
4070 return false;
4073 /* Return true if GNAT_NODE references a Volatile_Full_Access entity. */
4075 static bool
4076 node_has_volatile_full_access (Node_Id gnat_node)
4078 Entity_Id gnat_entity;
4080 switch (Nkind (gnat_node))
4082 case N_Identifier:
4083 case N_Expanded_Name:
4084 gnat_entity = Entity (gnat_node);
4085 if (!Is_Object (gnat_entity))
4086 break;
4087 return Is_Volatile_Full_Access (gnat_entity)
4088 || Is_Volatile_Full_Access (Etype (gnat_entity));
4090 case N_Selected_Component:
4091 gnat_entity = Entity (Selector_Name (gnat_node));
4092 return Is_Volatile_Full_Access (gnat_entity)
4093 || Is_Volatile_Full_Access (Etype (gnat_entity));
4095 case N_Indexed_Component:
4096 case N_Explicit_Dereference:
4097 return Is_Volatile_Full_Access (Etype (gnat_node));
4099 default:
4100 break;
4103 return false;
4106 /* Strip any type conversion on GNAT_NODE and return the result. */
4108 static Node_Id
4109 gnat_strip_type_conversion (Node_Id gnat_node)
4111 Node_Kind kind = Nkind (gnat_node);
4113 if (kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
4114 gnat_node = Expression (gnat_node);
4116 return gnat_node;
4119 /* Return true if GNAT_NODE requires outer atomic access, i.e. atomic access
4120 of an object of which GNAT_NODE is a component. */
4122 static bool
4123 outer_atomic_access_required_p (Node_Id gnat_node)
4125 gnat_node = gnat_strip_type_conversion (gnat_node);
4127 while (true)
4129 switch (Nkind (gnat_node))
4131 case N_Identifier:
4132 case N_Expanded_Name:
4133 if (No (Renamed_Object (Entity (gnat_node))))
4134 return false;
4135 gnat_node
4136 = gnat_strip_type_conversion (Renamed_Object (Entity (gnat_node)));
4137 break;
4139 case N_Indexed_Component:
4140 case N_Selected_Component:
4141 case N_Slice:
4142 gnat_node = gnat_strip_type_conversion (Prefix (gnat_node));
4143 if (node_has_volatile_full_access (gnat_node))
4144 return true;
4145 break;
4147 default:
4148 return false;
4152 gcc_unreachable ();
4155 /* Return true if GNAT_NODE requires atomic access and set SYNC according to
4156 the associated synchronization setting. */
4158 static bool
4159 atomic_access_required_p (Node_Id gnat_node, bool *sync)
4161 const Node_Id gnat_parent = Parent (gnat_node);
4162 unsigned char attr_id;
4163 bool as_a_whole = true;
4165 /* First, scan the parent to find out cases where the flag is irrelevant. */
4166 switch (Nkind (gnat_parent))
4168 case N_Attribute_Reference:
4169 attr_id = Get_Attribute_Id (Attribute_Name (gnat_parent));
4170 /* Do not mess up machine code insertions. */
4171 if (attr_id == Attr_Asm_Input || attr_id == Attr_Asm_Output)
4172 return false;
4174 /* Nothing to do if we are the prefix of an attribute, since we do not
4175 want an atomic access for things like 'Size. */
4177 /* ... fall through ... */
4179 case N_Reference:
4180 /* The N_Reference node is like an attribute. */
4181 if (Prefix (gnat_parent) == gnat_node)
4182 return false;
4183 break;
4185 case N_Indexed_Component:
4186 case N_Selected_Component:
4187 case N_Slice:
4188 /* If we are the prefix, then the access is only partial. */
4189 if (Prefix (gnat_parent) == gnat_node)
4190 as_a_whole = false;
4191 break;
4193 case N_Object_Renaming_Declaration:
4194 /* Nothing to do for the identifier in an object renaming declaration,
4195 the renaming itself does not need atomic access. */
4196 return false;
4198 default:
4199 break;
4202 /* Then, scan the node to find the atomic object. */
4203 gnat_node = gnat_strip_type_conversion (gnat_node);
4205 /* For Atomic itself, only reads and updates of the object as a whole require
4206 atomic access (RM C.6 (15)). But for Volatile_Full_Access, all reads and
4207 updates require atomic access. */
4208 if (!(as_a_whole && node_is_atomic (gnat_node))
4209 && !node_has_volatile_full_access (gnat_node))
4210 return false;
4212 /* If an outer atomic access will also be required, it cancels this one. */
4213 if (outer_atomic_access_required_p (gnat_node))
4214 return false;
4216 *sync = Atomic_Sync_Required (gnat_node);
4218 return true;
4221 /* Create a temporary variable with PREFIX and TYPE, and return it. */
4223 static tree
4224 create_temporary (const char *prefix, tree type)
4226 tree gnu_temp
4227 = create_var_decl (create_tmp_var_name (prefix), NULL_TREE,
4228 type, NULL_TREE,
4229 false, false, false, false, false,
4230 true, false, NULL, Empty);
4231 return gnu_temp;
4234 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
4235 Put the initialization statement into GNU_INIT_STMT and annotate it with
4236 the SLOC of GNAT_NODE. Return the temporary variable. */
4238 static tree
4239 create_init_temporary (const char *prefix, tree gnu_init, tree *gnu_init_stmt,
4240 Node_Id gnat_node)
4242 tree gnu_temp = create_temporary (prefix, TREE_TYPE (gnu_init));
4244 *gnu_init_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_temp, gnu_init);
4245 set_expr_location_from_node (*gnu_init_stmt, gnat_node);
4247 return gnu_temp;
4250 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
4251 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
4252 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
4253 If GNU_TARGET is non-null, this must be a function call on the RHS of a
4254 N_Assignment_Statement and the result is to be placed into that object.
4255 If OUTER_ATOMIC_ACCESS is true, then the assignment to GNU_TARGET must be a
4256 load-modify-store sequence. Otherwise, if ATOMIC_ACCESS is true, then the
4257 assignment to GNU_TARGET must be atomic. If, in addition, ATOMIC_SYNC is
4258 true, then the assignment to GNU_TARGET requires atomic synchronization. */
4260 static tree
4261 Call_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p, tree gnu_target,
4262 bool outer_atomic_access, bool atomic_access, bool atomic_sync)
4264 const bool function_call = (Nkind (gnat_node) == N_Function_Call);
4265 const bool returning_value = (function_call && !gnu_target);
4266 /* The GCC node corresponding to the GNAT subprogram name. This can either
4267 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
4268 or an indirect reference expression (an INDIRECT_REF node) pointing to a
4269 subprogram. */
4270 tree gnu_subprog = gnat_to_gnu (Name (gnat_node));
4271 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
4272 tree gnu_subprog_type = TREE_TYPE (gnu_subprog);
4273 /* The return type of the FUNCTION_TYPE. */
4274 tree gnu_result_type = TREE_TYPE (gnu_subprog_type);
4275 tree gnu_subprog_addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_subprog);
4276 vec<tree, va_gc> *gnu_actual_vec = NULL;
4277 tree gnu_name_list = NULL_TREE;
4278 tree gnu_stmt_list = NULL_TREE;
4279 tree gnu_after_list = NULL_TREE;
4280 tree gnu_retval = NULL_TREE;
4281 tree gnu_call, gnu_result;
4282 bool by_descriptor = false;
4283 bool went_into_elab_proc = false;
4284 bool pushed_binding_level = false;
4285 Entity_Id gnat_formal;
4286 Node_Id gnat_actual;
4287 bool sync;
4289 gcc_assert (TREE_CODE (gnu_subprog_type) == FUNCTION_TYPE);
4291 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
4292 all our args first. */
4293 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL && DECL_STUBBED_P (gnu_subprog))
4295 tree call_expr = build_call_raise (PE_Stubbed_Subprogram_Called,
4296 gnat_node, N_Raise_Program_Error);
4298 for (gnat_actual = First_Actual (gnat_node);
4299 Present (gnat_actual);
4300 gnat_actual = Next_Actual (gnat_actual))
4301 add_stmt (gnat_to_gnu (gnat_actual));
4303 if (returning_value)
4305 *gnu_result_type_p = gnu_result_type;
4306 return build1 (NULL_EXPR, gnu_result_type, call_expr);
4309 return call_expr;
4312 if (TREE_CODE (gnu_subprog) == FUNCTION_DECL)
4314 /* For a call to a nested function, check the inlining status. */
4315 if (decl_function_context (gnu_subprog))
4316 check_inlining_for_nested_subprog (gnu_subprog);
4318 /* For a recursive call, avoid explosion due to recursive inlining. */
4319 if (gnu_subprog == current_function_decl)
4320 DECL_DISREGARD_INLINE_LIMITS (gnu_subprog) = 0;
4323 /* The only way we can be making a call via an access type is if Name is an
4324 explicit dereference. In that case, get the list of formal args from the
4325 type the access type is pointing to. Otherwise, get the formals from the
4326 entity being called. */
4327 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4329 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4331 /* If the access type doesn't require foreign-compatible representation,
4332 be prepared for descriptors. */
4333 if (targetm.calls.custom_function_descriptors > 0
4334 && Can_Use_Internal_Rep (Etype (Prefix (Name (gnat_node)))))
4335 by_descriptor = true;
4337 else if (Nkind (Name (gnat_node)) == N_Attribute_Reference)
4338 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
4339 gnat_formal = Empty;
4340 else
4341 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4343 /* The lifetime of the temporaries created for the call ends right after the
4344 return value is copied, so we can give them the scope of the elaboration
4345 routine at top level. */
4346 if (!current_function_decl)
4348 current_function_decl = get_elaboration_procedure ();
4349 went_into_elab_proc = true;
4352 /* First, create the temporary for the return value when:
4354 1. There is no target and the function has copy-in/copy-out parameters,
4355 because we need to preserve the return value before copying back the
4356 parameters.
4358 2. There is no target and the call is made for neither an object nor a
4359 renaming declaration, nor a return statement, nor an allocator, and
4360 the return type has variable size because in this case the gimplifier
4361 cannot create the temporary, or more generally is simply an aggregate
4362 type, because the gimplifier would then create the temporary in the
4363 outermost scope instead of locally.
4365 3. There is a target and it is a slice or an array with fixed size,
4366 and the return type has variable size, because the gimplifier
4367 doesn't handle these cases.
4369 4. There is no target and we have misaligned In Out or Out parameters
4370 passed by reference, because we need to preserve the return value
4371 before copying back the parameters. However, in this case, we'll
4372 defer creating the temporary, see below.
4374 This must be done before we push a binding level around the call, since
4375 we will pop it before copying the return value. */
4376 if (function_call
4377 && ((!gnu_target && TYPE_CI_CO_LIST (gnu_subprog_type))
4378 || (!gnu_target
4379 && Nkind (Parent (gnat_node)) != N_Object_Declaration
4380 && Nkind (Parent (gnat_node)) != N_Object_Renaming_Declaration
4381 && Nkind (Parent (gnat_node)) != N_Simple_Return_Statement
4382 && !(Nkind (Parent (gnat_node)) == N_Qualified_Expression
4383 && Nkind (Parent (Parent (gnat_node))) == N_Allocator)
4384 && AGGREGATE_TYPE_P (gnu_result_type)
4385 && !TYPE_IS_FAT_POINTER_P (gnu_result_type))
4386 || (gnu_target
4387 && (TREE_CODE (gnu_target) == ARRAY_RANGE_REF
4388 || (TREE_CODE (TREE_TYPE (gnu_target)) == ARRAY_TYPE
4389 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target)))
4390 == INTEGER_CST))
4391 && TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST)))
4393 gnu_retval = create_temporary ("R", gnu_result_type);
4394 DECL_RETURN_VALUE_P (gnu_retval) = 1;
4397 /* If we don't need a value or have already created it, push a binding level
4398 around the call. This will narrow the lifetime of the temporaries we may
4399 need to make when translating the parameters as much as possible. */
4400 if (!returning_value || gnu_retval)
4402 start_stmt_group ();
4403 gnat_pushlevel ();
4404 pushed_binding_level = true;
4407 /* Create the list of the actual parameters as GCC expects it, namely a
4408 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
4409 is an expression and the TREE_PURPOSE field is null. But skip Out
4410 parameters not passed by reference and that need not be copied in. */
4411 for (gnat_actual = First_Actual (gnat_node);
4412 Present (gnat_actual);
4413 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4414 gnat_actual = Next_Actual (gnat_actual))
4416 Entity_Id gnat_formal_type = Etype (gnat_formal);
4417 tree gnu_formal_type = gnat_to_gnu_type (gnat_formal_type);
4418 tree gnu_formal = present_gnu_tree (gnat_formal)
4419 ? get_gnu_tree (gnat_formal) : NULL_TREE;
4420 const bool is_true_formal_parm
4421 = gnu_formal && TREE_CODE (gnu_formal) == PARM_DECL;
4422 const bool is_by_ref_formal_parm
4423 = is_true_formal_parm
4424 && (DECL_BY_REF_P (gnu_formal)
4425 || DECL_BY_COMPONENT_PTR_P (gnu_formal));
4426 /* In the Out or In Out case, we must suppress conversions that yield
4427 an lvalue but can nevertheless cause the creation of a temporary,
4428 because we need the real object in this case, either to pass its
4429 address if it's passed by reference or as target of the back copy
4430 done after the call if it uses the copy-in/copy-out mechanism.
4431 We do it in the In case too, except for an unchecked conversion
4432 to an elementary type or a constrained composite type because it
4433 alone can cause the actual to be misaligned and the addressability
4434 test is applied to the real object. */
4435 const bool suppress_type_conversion
4436 = ((Nkind (gnat_actual) == N_Unchecked_Type_Conversion
4437 && (Ekind (gnat_formal) != E_In_Parameter
4438 || (Is_Composite_Type (Underlying_Type (gnat_formal_type))
4439 && !Is_Constrained (Underlying_Type (gnat_formal_type)))))
4440 || (Nkind (gnat_actual) == N_Type_Conversion
4441 && Is_Composite_Type (Underlying_Type (gnat_formal_type))));
4442 Node_Id gnat_name = suppress_type_conversion
4443 ? Expression (gnat_actual) : gnat_actual;
4444 tree gnu_name = gnat_to_gnu (gnat_name), gnu_name_type;
4446 /* If it's possible we may need to use this expression twice, make sure
4447 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4448 to force side-effects before the call. */
4449 if (Ekind (gnat_formal) != E_In_Parameter && !is_by_ref_formal_parm)
4451 tree init = NULL_TREE;
4452 gnu_name = gnat_stabilize_reference (gnu_name, true, &init);
4453 if (init)
4454 gnu_name
4455 = build_compound_expr (TREE_TYPE (gnu_name), init, gnu_name);
4458 /* If we are passing a non-addressable parameter by reference, pass the
4459 address of a copy. In the Out or In Out case, set up to copy back
4460 out after the call. */
4461 if (is_by_ref_formal_parm
4462 && (gnu_name_type = gnat_to_gnu_type (Etype (gnat_name)))
4463 && !addressable_p (gnu_name, gnu_name_type))
4465 bool in_param = (Ekind (gnat_formal) == E_In_Parameter);
4466 tree gnu_orig = gnu_name, gnu_temp, gnu_stmt;
4468 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4469 but sort of an instantiation for them. */
4470 if (TREE_CODE (gnu_name) == CONSTRUCTOR)
4473 /* If the type is passed by reference, a copy is not allowed. */
4474 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type))
4475 post_error ("misaligned actual cannot be passed by reference",
4476 gnat_actual);
4478 /* For users of Starlet we issue a warning because the interface
4479 apparently assumes that by-ref parameters outlive the procedure
4480 invocation. The code still will not work as intended, but we
4481 cannot do much better since low-level parts of the back-end
4482 would allocate temporaries at will because of the misalignment
4483 if we did not do so here. */
4484 else if (Is_Valued_Procedure (Entity (Name (gnat_node))))
4486 post_error
4487 ("?possible violation of implicit assumption", gnat_actual);
4488 post_error_ne
4489 ("?made by pragma Import_Valued_Procedure on &", gnat_actual,
4490 Entity (Name (gnat_node)));
4491 post_error_ne ("?because of misalignment of &", gnat_actual,
4492 gnat_formal);
4495 /* If the actual type of the object is already the nominal type,
4496 we have nothing to do, except if the size is self-referential
4497 in which case we'll remove the unpadding below. */
4498 if (TREE_TYPE (gnu_name) == gnu_name_type
4499 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type)))
4502 /* Otherwise remove the unpadding from all the objects. */
4503 else if (TREE_CODE (gnu_name) == COMPONENT_REF
4504 && TYPE_IS_PADDING_P
4505 (TREE_TYPE (TREE_OPERAND (gnu_name, 0))))
4506 gnu_orig = gnu_name = TREE_OPERAND (gnu_name, 0);
4508 /* Otherwise convert to the nominal type of the object if needed.
4509 There are several cases in which we need to make the temporary
4510 using this type instead of the actual type of the object when
4511 they are distinct, because the expectations of the callee would
4512 otherwise not be met:
4513 - if it's a justified modular type,
4514 - if the actual type is a smaller form of it,
4515 - if it's a smaller form of the actual type. */
4516 else if ((TREE_CODE (gnu_name_type) == RECORD_TYPE
4517 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type)
4518 || smaller_form_type_p (TREE_TYPE (gnu_name),
4519 gnu_name_type)))
4520 || (INTEGRAL_TYPE_P (gnu_name_type)
4521 && smaller_form_type_p (gnu_name_type,
4522 TREE_TYPE (gnu_name))))
4523 gnu_name = convert (gnu_name_type, gnu_name);
4525 /* If this is an In Out or Out parameter and we're returning a value,
4526 we need to create a temporary for the return value because we must
4527 preserve it before copying back at the very end. */
4528 if (!in_param && returning_value && !gnu_retval)
4530 gnu_retval = create_temporary ("R", gnu_result_type);
4531 DECL_RETURN_VALUE_P (gnu_retval) = 1;
4534 /* If we haven't pushed a binding level, push it now. This will
4535 narrow the lifetime of the temporary we are about to make as
4536 much as possible. */
4537 if (!pushed_binding_level && (!returning_value || gnu_retval))
4539 start_stmt_group ();
4540 gnat_pushlevel ();
4541 pushed_binding_level = true;
4544 /* Create an explicit temporary holding the copy. */
4545 gnu_temp
4546 = create_init_temporary ("A", gnu_name, &gnu_stmt, gnat_actual);
4548 /* But initialize it on the fly like for an implicit temporary as
4549 we aren't necessarily having a statement list. */
4550 gnu_name = build_compound_expr (TREE_TYPE (gnu_name), gnu_stmt,
4551 gnu_temp);
4553 /* Set up to move the copy back to the original if needed. */
4554 if (!in_param)
4556 /* If the original is a COND_EXPR whose first arm isn't meant to
4557 be further used, just deal with the second arm. This is very
4558 likely the conditional expression built for a check. */
4559 if (TREE_CODE (gnu_orig) == COND_EXPR
4560 && TREE_CODE (TREE_OPERAND (gnu_orig, 1)) == COMPOUND_EXPR
4561 && integer_zerop
4562 (TREE_OPERAND (TREE_OPERAND (gnu_orig, 1), 1)))
4563 gnu_orig = TREE_OPERAND (gnu_orig, 2);
4565 gnu_stmt
4566 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_orig, gnu_temp);
4567 set_expr_location_from_node (gnu_stmt, gnat_node);
4569 append_to_statement_list (gnu_stmt, &gnu_after_list);
4573 /* Start from the real object and build the actual. */
4574 tree gnu_actual = gnu_name;
4576 /* If atomic access is required for an In or In Out actual parameter,
4577 build the atomic load. */
4578 if (is_true_formal_parm
4579 && !is_by_ref_formal_parm
4580 && Ekind (gnat_formal) != E_Out_Parameter
4581 && atomic_access_required_p (gnat_actual, &sync))
4582 gnu_actual = build_atomic_load (gnu_actual, sync);
4584 /* If this was a procedure call, we may not have removed any padding.
4585 So do it here for the part we will use as an input, if any. */
4586 if (Ekind (gnat_formal) != E_Out_Parameter
4587 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4588 gnu_actual
4589 = convert (get_unpadded_type (Etype (gnat_actual)), gnu_actual);
4591 /* Put back the conversion we suppressed above in the computation of the
4592 real object. And even if we didn't suppress any conversion there, we
4593 may have suppressed a conversion to the Etype of the actual earlier,
4594 since the parent is a procedure call, so put it back here. Note that
4595 we might have a dummy type here if the actual is the dereference of a
4596 pointer to it, but that's OK if the formal is passed by reference. */
4597 tree gnu_actual_type = gnat_to_gnu_type (Etype (gnat_actual));
4598 if (TYPE_IS_DUMMY_P (gnu_actual_type))
4599 gcc_assert (is_true_formal_parm && DECL_BY_REF_P (gnu_formal));
4600 else if (suppress_type_conversion
4601 && Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4602 gnu_actual = unchecked_convert (gnu_actual_type, gnu_actual,
4603 No_Truncation (gnat_actual));
4604 else
4605 gnu_actual = convert (gnu_actual_type, gnu_actual);
4607 /* Make sure that the actual is in range of the formal's type. */
4608 if (Ekind (gnat_formal) != E_Out_Parameter
4609 && Do_Range_Check (gnat_actual))
4610 gnu_actual
4611 = emit_range_check (gnu_actual, gnat_formal_type, gnat_actual);
4613 /* Unless this is an In parameter, we must remove any justified modular
4614 building from GNU_NAME to get an lvalue. */
4615 if (Ekind (gnat_formal) != E_In_Parameter
4616 && TREE_CODE (gnu_name) == CONSTRUCTOR
4617 && TREE_CODE (TREE_TYPE (gnu_name)) == RECORD_TYPE
4618 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name)))
4619 gnu_name
4620 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name))), gnu_name);
4622 /* First see if the parameter is passed by reference. */
4623 if (is_true_formal_parm && DECL_BY_REF_P (gnu_formal))
4625 if (Ekind (gnat_formal) != E_In_Parameter)
4627 /* In Out or Out parameters passed by reference don't use the
4628 copy-in/copy-out mechanism so the address of the real object
4629 must be passed to the function. */
4630 gnu_actual = gnu_name;
4632 /* If we have a padded type, be sure we've removed padding. */
4633 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual)))
4634 gnu_actual = convert (get_unpadded_type (Etype (gnat_actual)),
4635 gnu_actual);
4637 /* If we have the constructed subtype of an aliased object
4638 with an unconstrained nominal subtype, the type of the
4639 actual includes the template, although it is formally
4640 constrained. So we need to convert it back to the real
4641 constructed subtype to retrieve the constrained part
4642 and takes its address. */
4643 if (TREE_CODE (TREE_TYPE (gnu_actual)) == RECORD_TYPE
4644 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual))
4645 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual))
4646 && Is_Array_Type (Underlying_Type (Etype (gnat_actual))))
4647 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4648 gnu_actual);
4651 /* There is no need to convert the actual to the formal's type before
4652 taking its address. The only exception is for unconstrained array
4653 types because of the way we build fat pointers. */
4654 if (TREE_CODE (gnu_formal_type) == UNCONSTRAINED_ARRAY_TYPE)
4656 /* Put back a view conversion for In Out or Out parameters. */
4657 if (Ekind (gnat_formal) != E_In_Parameter)
4658 gnu_actual = convert (gnat_to_gnu_type (Etype (gnat_actual)),
4659 gnu_actual);
4660 gnu_actual = convert (gnu_formal_type, gnu_actual);
4663 /* The symmetry of the paths to the type of an entity is broken here
4664 since arguments don't know that they will be passed by ref. */
4665 gnu_formal_type = TREE_TYPE (gnu_formal);
4666 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4669 /* Then see if the parameter is an array passed to a foreign convention
4670 subprogram. */
4671 else if (is_true_formal_parm && DECL_BY_COMPONENT_PTR_P (gnu_formal))
4673 gnu_formal_type = TREE_TYPE (gnu_formal);
4674 gnu_actual = maybe_implicit_deref (gnu_actual);
4675 gnu_actual = maybe_unconstrained_array (gnu_actual);
4677 if (TYPE_IS_PADDING_P (gnu_formal_type))
4679 gnu_formal_type = TREE_TYPE (TYPE_FIELDS (gnu_formal_type));
4680 gnu_actual = convert (gnu_formal_type, gnu_actual);
4683 /* Take the address of the object and convert to the proper pointer
4684 type. We'd like to actually compute the address of the beginning
4685 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
4686 possibility that the ARRAY_REF might return a constant and we'd be
4687 getting the wrong address. Neither approach is exactly correct,
4688 but this is the most likely to work in all cases. */
4689 gnu_actual = build_unary_op (ADDR_EXPR, gnu_formal_type, gnu_actual);
4692 /* Otherwise the parameter is passed by copy. */
4693 else
4695 tree gnu_size;
4697 if (Ekind (gnat_formal) != E_In_Parameter)
4698 gnu_name_list = tree_cons (NULL_TREE, gnu_name, gnu_name_list);
4700 /* If we didn't create a PARM_DECL for the formal, this means that
4701 it is an Out parameter not passed by reference and that need not
4702 be copied in. In this case, the value of the actual need not be
4703 read. However, we still need to make sure that its side-effects
4704 are evaluated before the call, so we evaluate its address. */
4705 if (!is_true_formal_parm)
4707 if (TREE_SIDE_EFFECTS (gnu_name))
4709 tree addr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_name);
4710 append_to_statement_list (addr, &gnu_stmt_list);
4712 continue;
4715 gnu_actual = convert (gnu_formal_type, gnu_actual);
4717 /* If this is 'Null_Parameter, pass a zero even though we are
4718 dereferencing it. */
4719 if (TREE_CODE (gnu_actual) == INDIRECT_REF
4720 && TREE_PRIVATE (gnu_actual)
4721 && (gnu_size = TYPE_SIZE (TREE_TYPE (gnu_actual)))
4722 && TREE_CODE (gnu_size) == INTEGER_CST
4723 && compare_tree_int (gnu_size, BITS_PER_WORD) <= 0)
4725 tree type_for_size
4726 = gnat_type_for_size (TREE_INT_CST_LOW (gnu_size), 1);
4727 gnu_actual
4728 = unchecked_convert (DECL_ARG_TYPE (gnu_formal),
4729 build_int_cst (type_for_size, 0),
4730 false);
4732 else
4733 gnu_actual = convert (DECL_ARG_TYPE (gnu_formal), gnu_actual);
4736 vec_safe_push (gnu_actual_vec, gnu_actual);
4739 gnu_call
4740 = build_call_vec (gnu_result_type, gnu_subprog_addr, gnu_actual_vec);
4741 CALL_EXPR_BY_DESCRIPTOR (gnu_call) = by_descriptor;
4742 set_expr_location_from_node (gnu_call, gnat_node);
4744 /* If we have created a temporary for the return value, initialize it. */
4745 if (gnu_retval)
4747 tree gnu_stmt
4748 = build_binary_op (INIT_EXPR, NULL_TREE, gnu_retval, gnu_call);
4749 set_expr_location_from_node (gnu_stmt, gnat_node);
4750 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4751 gnu_call = gnu_retval;
4754 /* If this is a subprogram with copy-in/copy-out parameters, we need to
4755 unpack the valued returned from the function into the In Out or Out
4756 parameters. We deal with the function return (if this is an Ada
4757 function) below. */
4758 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4760 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
4761 copy-out parameters. */
4762 tree gnu_cico_list = TYPE_CI_CO_LIST (gnu_subprog_type);
4763 const int length = list_length (gnu_cico_list);
4765 /* The call sequence must contain one and only one call, even though the
4766 function is pure. Save the result into a temporary if needed. */
4767 if (length > 1)
4769 if (!gnu_retval)
4771 tree gnu_stmt;
4772 gnu_call
4773 = create_init_temporary ("P", gnu_call, &gnu_stmt, gnat_node);
4774 append_to_statement_list (gnu_stmt, &gnu_stmt_list);
4777 gnu_name_list = nreverse (gnu_name_list);
4780 /* The first entry is for the actual return value if this is a
4781 function, so skip it. */
4782 if (function_call)
4783 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4785 if (Nkind (Name (gnat_node)) == N_Explicit_Dereference)
4786 gnat_formal = First_Formal_With_Extras (Etype (Name (gnat_node)));
4787 else
4788 gnat_formal = First_Formal_With_Extras (Entity (Name (gnat_node)));
4790 for (gnat_actual = First_Actual (gnat_node);
4791 Present (gnat_actual);
4792 gnat_formal = Next_Formal_With_Extras (gnat_formal),
4793 gnat_actual = Next_Actual (gnat_actual))
4794 /* If we are dealing with a copy-in/copy-out parameter, we must
4795 retrieve its value from the record returned in the call. */
4796 if (!(present_gnu_tree (gnat_formal)
4797 && TREE_CODE (get_gnu_tree (gnat_formal)) == PARM_DECL
4798 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal))
4799 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal))))
4800 && Ekind (gnat_formal) != E_In_Parameter)
4802 /* Get the value to assign to this Out or In Out parameter. It is
4803 either the result of the function if there is only a single such
4804 parameter or the appropriate field from the record returned. */
4805 tree gnu_result
4806 = length == 1
4807 ? gnu_call
4808 : build_component_ref (gnu_call, TREE_PURPOSE (gnu_cico_list),
4809 false);
4811 /* If the actual is a conversion, get the inner expression, which
4812 will be the real destination, and convert the result to the
4813 type of the actual parameter. */
4814 tree gnu_actual
4815 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list));
4817 /* If the result is a padded type, remove the padding. */
4818 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
4819 gnu_result
4820 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
4821 gnu_result);
4823 /* If the actual is a type conversion, the real target object is
4824 denoted by the inner Expression and we need to convert the
4825 result to the associated type.
4826 We also need to convert our gnu assignment target to this type
4827 if the corresponding GNU_NAME was constructed from the GNAT
4828 conversion node and not from the inner Expression. */
4829 if (Nkind (gnat_actual) == N_Type_Conversion)
4831 gnu_result
4832 = convert_with_check
4833 (Etype (Expression (gnat_actual)), gnu_result,
4834 Do_Overflow_Check (gnat_actual),
4835 Do_Range_Check (Expression (gnat_actual)),
4836 Float_Truncate (gnat_actual), gnat_actual);
4838 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal))))
4839 gnu_actual = convert (TREE_TYPE (gnu_result), gnu_actual);
4842 /* Unchecked conversions as actuals for Out parameters are not
4843 allowed in user code because they are not variables, but do
4844 occur in front-end expansions. The associated GNU_NAME is
4845 always obtained from the inner expression in such cases. */
4846 else if (Nkind (gnat_actual) == N_Unchecked_Type_Conversion)
4847 gnu_result = unchecked_convert (TREE_TYPE (gnu_actual),
4848 gnu_result,
4849 No_Truncation (gnat_actual));
4850 else
4852 if (Do_Range_Check (gnat_actual))
4853 gnu_result
4854 = emit_range_check (gnu_result, Etype (gnat_actual),
4855 gnat_actual);
4857 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual)))
4858 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result)))))
4859 gnu_result = convert (TREE_TYPE (gnu_actual), gnu_result);
4862 /* If an outer atomic access is required for an actual parameter,
4863 build the load-modify-store sequence. */
4864 if (outer_atomic_access_required_p (gnat_actual))
4865 gnu_result
4866 = build_load_modify_store (gnu_actual, gnu_result, gnat_node);
4868 /* Or else, if simple atomic access is required, build the atomic
4869 store. */
4870 else if (atomic_access_required_p (gnat_actual, &sync))
4871 gnu_result = build_atomic_store (gnu_actual, gnu_result, sync);
4873 /* Otherwise build a regular assignment. */
4874 else
4875 gnu_result = build_binary_op (MODIFY_EXPR, NULL_TREE,
4876 gnu_actual, gnu_result);
4878 if (EXPR_P (gnu_result))
4879 set_expr_location_from_node (gnu_result, gnat_node);
4880 append_to_statement_list (gnu_result, &gnu_stmt_list);
4881 gnu_cico_list = TREE_CHAIN (gnu_cico_list);
4882 gnu_name_list = TREE_CHAIN (gnu_name_list);
4886 /* If this is a function call, the result is the call expression unless a
4887 target is specified, in which case we copy the result into the target
4888 and return the assignment statement. */
4889 if (function_call)
4891 /* If this is a function with copy-in/copy-out parameters, extract the
4892 return value from it and update the return type. */
4893 if (TYPE_CI_CO_LIST (gnu_subprog_type))
4895 tree gnu_elmt = TYPE_CI_CO_LIST (gnu_subprog_type);
4896 gnu_call
4897 = build_component_ref (gnu_call, TREE_PURPOSE (gnu_elmt), false);
4898 gnu_result_type = TREE_TYPE (gnu_call);
4901 /* If the function returns an unconstrained array or by direct reference,
4902 we have to dereference the pointer. */
4903 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type)
4904 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type))
4905 gnu_call = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_call);
4907 if (gnu_target)
4909 Node_Id gnat_parent = Parent (gnat_node);
4910 enum tree_code op_code;
4912 /* If range check is needed, emit code to generate it. */
4913 if (Do_Range_Check (gnat_node))
4914 gnu_call
4915 = emit_range_check (gnu_call, Etype (Name (gnat_parent)),
4916 gnat_parent);
4918 /* ??? If the return type has variable size, then force the return
4919 slot optimization as we would not be able to create a temporary.
4920 That's what has been done historically. */
4921 if (return_type_with_variable_size_p (gnu_result_type))
4922 op_code = INIT_EXPR;
4923 else
4924 op_code = MODIFY_EXPR;
4926 /* Use the required method to move the result to the target. */
4927 if (outer_atomic_access)
4928 gnu_call
4929 = build_load_modify_store (gnu_target, gnu_call, gnat_node);
4930 else if (atomic_access)
4931 gnu_call = build_atomic_store (gnu_target, gnu_call, atomic_sync);
4932 else
4933 gnu_call
4934 = build_binary_op (op_code, NULL_TREE, gnu_target, gnu_call);
4936 if (EXPR_P (gnu_call))
4937 set_expr_location_from_node (gnu_call, gnat_parent);
4938 append_to_statement_list (gnu_call, &gnu_stmt_list);
4940 else
4941 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
4944 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4945 parameters, the result is just the call statement. */
4946 else if (!TYPE_CI_CO_LIST (gnu_subprog_type))
4947 append_to_statement_list (gnu_call, &gnu_stmt_list);
4949 /* Finally, add the copy back statements, if any. */
4950 append_to_statement_list (gnu_after_list, &gnu_stmt_list);
4952 if (went_into_elab_proc)
4953 current_function_decl = NULL_TREE;
4955 /* If we have pushed a binding level, pop it and finish up the enclosing
4956 statement group. */
4957 if (pushed_binding_level)
4959 add_stmt (gnu_stmt_list);
4960 gnat_poplevel ();
4961 gnu_result = end_stmt_group ();
4964 /* Otherwise, retrieve the statement list, if any. */
4965 else if (gnu_stmt_list)
4966 gnu_result = gnu_stmt_list;
4968 /* Otherwise, just return the call expression. */
4969 else
4970 return gnu_call;
4972 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4973 But first simplify if we have only one statement in the list. */
4974 if (returning_value)
4976 tree first = expr_first (gnu_result), last = expr_last (gnu_result);
4977 if (first == last)
4978 gnu_result = first;
4979 gnu_result
4980 = build_compound_expr (TREE_TYPE (gnu_call), gnu_result, gnu_call);
4983 return gnu_result;
4986 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4987 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4989 static tree
4990 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node)
4992 tree gnu_jmpsave_decl = NULL_TREE;
4993 tree gnu_jmpbuf_decl = NULL_TREE;
4994 /* If just annotating, ignore all EH and cleanups. */
4995 bool gcc_eh = (!type_annotate_only
4996 && Present (Exception_Handlers (gnat_node))
4997 && Back_End_Exceptions ());
4998 bool fe_sjlj
4999 = (!type_annotate_only && Present (Exception_Handlers (gnat_node))
5000 && Exception_Mechanism == Front_End_SJLJ);
5001 bool at_end = !type_annotate_only && Present (At_End_Proc (gnat_node));
5002 bool binding_for_block = (at_end || gcc_eh || fe_sjlj);
5003 tree gnu_inner_block; /* The statement(s) for the block itself. */
5004 tree gnu_result;
5005 tree gnu_expr;
5006 Node_Id gnat_temp;
5008 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
5009 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
5010 add_cleanup, and when we leave the binding, end_stmt_group will create
5011 the TRY_FINALLY_EXPR.
5013 ??? The region level calls down there have been specifically put in place
5014 for a ZCX context and currently the order in which things are emitted
5015 (region/handlers) is different from the SJLJ case. Instead of putting
5016 other calls with different conditions at other places for the SJLJ case,
5017 it seems cleaner to reorder things for the SJLJ case and generalize the
5018 condition to make it not ZCX specific.
5020 If there are any exceptions or cleanup processing involved, we need an
5021 outer statement group (for Fe_Sjlj) and binding level. */
5022 if (binding_for_block)
5024 start_stmt_group ();
5025 gnat_pushlevel ();
5028 /* If using fe_sjlj, make the variables for the setjmp buffer and save
5029 area for address of previous buffer. Do this first since we need to have
5030 the setjmp buf known for any decls in this block. */
5031 if (fe_sjlj)
5033 gnu_jmpsave_decl
5034 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE,
5035 jmpbuf_ptr_type,
5036 build_call_n_expr (get_jmpbuf_decl, 0),
5037 false, false, false, false, false, true, false,
5038 NULL, gnat_node);
5040 /* The __builtin_setjmp receivers will immediately reinstall it. Now
5041 because of the unstructured form of EH used by fe_sjlj, there
5042 might be forward edges going to __builtin_setjmp receivers on which
5043 it is uninitialized, although they will never be actually taken. */
5044 TREE_NO_WARNING (gnu_jmpsave_decl) = 1;
5045 gnu_jmpbuf_decl
5046 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE,
5047 jmpbuf_type,
5048 NULL_TREE,
5049 false, false, false, false, false, true, false,
5050 NULL, gnat_node);
5052 set_block_jmpbuf_decl (gnu_jmpbuf_decl);
5054 /* When we exit this block, restore the saved value. */
5055 add_cleanup (build_call_n_expr (set_jmpbuf_decl, 1, gnu_jmpsave_decl),
5056 Present (End_Label (gnat_node))
5057 ? End_Label (gnat_node) : gnat_node);
5060 /* If we are to call a function when exiting this block, add a cleanup
5061 to the binding level we made above. Note that add_cleanup is FIFO
5062 so we must register this cleanup after the EH cleanup just above. */
5063 if (at_end)
5065 tree proc_decl = gnat_to_gnu (At_End_Proc (gnat_node));
5067 /* When not optimizing, disable inlining of finalizers as this can
5068 create a more complex CFG in the parent function. */
5069 if (!optimize)
5070 DECL_DECLARED_INLINE_P (proc_decl) = 0;
5072 /* If there is no end label attached, we use the location of the At_End
5073 procedure because Expand_Cleanup_Actions might reset the location of
5074 the enclosing construct to that of an inner statement. */
5075 add_cleanup (build_call_n_expr (proc_decl, 0),
5076 Present (End_Label (gnat_node))
5077 ? End_Label (gnat_node) : At_End_Proc (gnat_node));
5080 /* Now build the tree for the declarations and statements inside this block.
5081 If this is SJLJ, set our jmp_buf as the current buffer. */
5082 start_stmt_group ();
5084 if (fe_sjlj)
5086 gnu_expr = build_call_n_expr (set_jmpbuf_decl, 1,
5087 build_unary_op (ADDR_EXPR, NULL_TREE,
5088 gnu_jmpbuf_decl));
5089 set_expr_location_from_node (gnu_expr, gnat_node);
5090 add_stmt (gnu_expr);
5093 if (Present (First_Real_Statement (gnat_node)))
5094 process_decls (Statements (gnat_node), Empty,
5095 First_Real_Statement (gnat_node), true, true);
5097 /* Generate code for each statement in the block. */
5098 for (gnat_temp = (Present (First_Real_Statement (gnat_node))
5099 ? First_Real_Statement (gnat_node)
5100 : First (Statements (gnat_node)));
5101 Present (gnat_temp); gnat_temp = Next (gnat_temp))
5102 add_stmt (gnat_to_gnu (gnat_temp));
5103 gnu_inner_block = end_stmt_group ();
5105 /* Now generate code for the two exception models, if either is relevant for
5106 this block. */
5107 if (fe_sjlj)
5109 tree *gnu_else_ptr = 0;
5110 tree gnu_handler;
5112 /* Make a binding level for the exception handling declarations and code
5113 and set up gnu_except_ptr_stack for the handlers to use. */
5114 start_stmt_group ();
5115 gnat_pushlevel ();
5117 vec_safe_push (gnu_except_ptr_stack,
5118 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE,
5119 build_pointer_type (except_type_node),
5120 build_call_n_expr (get_excptr_decl, 0),
5121 false, false, false, false, false,
5122 true, false, NULL, gnat_node));
5124 /* Generate code for each handler. The N_Exception_Handler case does the
5125 real work and returns a COND_EXPR for each handler, which we chain
5126 together here. */
5127 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
5128 Present (gnat_temp); gnat_temp = Next_Non_Pragma (gnat_temp))
5130 gnu_expr = gnat_to_gnu (gnat_temp);
5132 /* If this is the first one, set it as the outer one. Otherwise,
5133 point the "else" part of the previous handler to us. Then point
5134 to our "else" part. */
5135 if (!gnu_else_ptr)
5136 add_stmt (gnu_expr);
5137 else
5138 *gnu_else_ptr = gnu_expr;
5140 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
5143 /* If none of the exception handlers did anything, re-raise but do not
5144 defer abortion. */
5145 gnu_expr = build_call_n_expr (raise_nodefer_decl, 1,
5146 gnu_except_ptr_stack->last ());
5147 set_expr_location_from_node
5148 (gnu_expr,
5149 Present (End_Label (gnat_node)) ? End_Label (gnat_node) : gnat_node);
5151 if (gnu_else_ptr)
5152 *gnu_else_ptr = gnu_expr;
5153 else
5154 add_stmt (gnu_expr);
5156 /* End the binding level dedicated to the exception handlers and get the
5157 whole statement group. */
5158 gnu_except_ptr_stack->pop ();
5159 gnat_poplevel ();
5160 gnu_handler = end_stmt_group ();
5162 /* If the setjmp returns 1, we restore our incoming longjmp value and
5163 then check the handlers. */
5164 start_stmt_group ();
5165 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl, 1,
5166 gnu_jmpsave_decl),
5167 gnat_node);
5168 add_stmt (gnu_handler);
5169 gnu_handler = end_stmt_group ();
5171 /* This block is now "if (setjmp) ... <handlers> else <block>". */
5172 gnu_result = build3 (COND_EXPR, void_type_node,
5173 (build_call_n_expr
5174 (setjmp_decl, 1,
5175 build_unary_op (ADDR_EXPR, NULL_TREE,
5176 gnu_jmpbuf_decl))),
5177 gnu_handler, gnu_inner_block);
5179 else if (gcc_eh)
5181 tree gnu_handlers;
5182 location_t locus;
5184 /* First make a block containing the handlers. */
5185 start_stmt_group ();
5186 for (gnat_temp = First_Non_Pragma (Exception_Handlers (gnat_node));
5187 Present (gnat_temp);
5188 gnat_temp = Next_Non_Pragma (gnat_temp))
5189 add_stmt (gnat_to_gnu (gnat_temp));
5190 gnu_handlers = end_stmt_group ();
5192 /* Now make the TRY_CATCH_EXPR for the block. */
5193 gnu_result = build2 (TRY_CATCH_EXPR, void_type_node,
5194 gnu_inner_block, gnu_handlers);
5195 /* Set a location. We need to find a unique location for the dispatching
5196 code, otherwise we can get coverage or debugging issues. Try with
5197 the location of the end label. */
5198 if (Present (End_Label (gnat_node))
5199 && Sloc_to_locus (Sloc (End_Label (gnat_node)), &locus))
5200 SET_EXPR_LOCATION (gnu_result, locus);
5201 else
5202 /* Clear column information so that the exception handler of an
5203 implicit transient block does not incorrectly inherit the slocs
5204 of a decision, which would otherwise confuse control flow based
5205 coverage analysis tools. */
5206 set_expr_location_from_node (gnu_result, gnat_node, true);
5208 else
5209 gnu_result = gnu_inner_block;
5211 /* Now close our outer block, if we had to make one. */
5212 if (binding_for_block)
5214 add_stmt (gnu_result);
5215 gnat_poplevel ();
5216 gnu_result = end_stmt_group ();
5219 return gnu_result;
5222 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
5223 to a GCC tree, which is returned. This is the variant for front-end sjlj
5224 exception handling. */
5226 static tree
5227 Exception_Handler_to_gnu_fe_sjlj (Node_Id gnat_node)
5229 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
5230 an "if" statement to select the proper exceptions. For "Others", exclude
5231 exceptions where Handled_By_Others is nonzero unless the All_Others flag
5232 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
5233 tree gnu_choice = boolean_false_node;
5234 tree gnu_body = build_stmt_group (Statements (gnat_node), false);
5235 Node_Id gnat_temp;
5237 for (gnat_temp = First (Exception_Choices (gnat_node));
5238 gnat_temp; gnat_temp = Next (gnat_temp))
5240 tree this_choice;
5242 if (Nkind (gnat_temp) == N_Others_Choice)
5244 if (All_Others (gnat_temp))
5245 this_choice = boolean_true_node;
5246 else
5247 this_choice
5248 = build_binary_op
5249 (EQ_EXPR, boolean_type_node,
5250 convert
5251 (integer_type_node,
5252 build_component_ref
5253 (build_unary_op
5254 (INDIRECT_REF, NULL_TREE,
5255 gnu_except_ptr_stack->last ()),
5256 not_handled_by_others_decl,
5257 false)),
5258 integer_zero_node);
5261 else if (Nkind (gnat_temp) == N_Identifier
5262 || Nkind (gnat_temp) == N_Expanded_Name)
5264 Entity_Id gnat_ex_id = Entity (gnat_temp);
5265 tree gnu_expr;
5267 /* Exception may be a renaming. Recover original exception which is
5268 the one elaborated and registered. */
5269 if (Present (Renamed_Object (gnat_ex_id)))
5270 gnat_ex_id = Renamed_Object (gnat_ex_id);
5272 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, false);
5274 this_choice
5275 = build_binary_op
5276 (EQ_EXPR, boolean_type_node,
5277 gnu_except_ptr_stack->last (),
5278 convert (TREE_TYPE (gnu_except_ptr_stack->last ()),
5279 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr)));
5281 else
5282 gcc_unreachable ();
5284 gnu_choice = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
5285 gnu_choice, this_choice);
5288 return build3 (COND_EXPR, void_type_node, gnu_choice, gnu_body, NULL_TREE);
5291 /* Return true if no statement in GNAT_LIST can alter the control flow. */
5293 static bool
5294 stmt_list_cannot_alter_control_flow_p (List_Id gnat_list)
5296 if (No (gnat_list))
5297 return true;
5299 /* This is very conservative, we reject everything except for simple
5300 assignments between identifiers or literals. */
5301 for (Node_Id gnat_node = First (gnat_list);
5302 Present (gnat_node);
5303 gnat_node = Next (gnat_node))
5305 if (Nkind (gnat_node) != N_Assignment_Statement)
5306 return false;
5308 if (Nkind (Name (gnat_node)) != N_Identifier)
5309 return false;
5311 Node_Kind nkind = Nkind (Expression (gnat_node));
5312 if (nkind != N_Identifier
5313 && nkind != N_Integer_Literal
5314 && nkind != N_Real_Literal)
5315 return false;
5318 return true;
5321 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
5322 to a GCC tree, which is returned. This is the variant for GCC exception
5323 schemes. */
5325 static tree
5326 Exception_Handler_to_gnu_gcc (Node_Id gnat_node)
5328 tree gnu_etypes_list = NULL_TREE;
5330 /* We build a TREE_LIST of nodes representing what exception types this
5331 handler can catch, with special cases for others and all others cases.
5333 Each exception type is actually identified by a pointer to the exception
5334 id, or to a dummy object for "others" and "all others". */
5335 for (Node_Id gnat_temp = First (Exception_Choices (gnat_node));
5336 gnat_temp;
5337 gnat_temp = Next (gnat_temp))
5339 tree gnu_expr, gnu_etype;
5341 if (Nkind (gnat_temp) == N_Others_Choice)
5343 gnu_expr = All_Others (gnat_temp) ? all_others_decl : others_decl;
5344 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
5346 else if (Nkind (gnat_temp) == N_Identifier
5347 || Nkind (gnat_temp) == N_Expanded_Name)
5349 Entity_Id gnat_ex_id = Entity (gnat_temp);
5351 /* Exception may be a renaming. Recover original exception which is
5352 the one elaborated and registered. */
5353 if (Present (Renamed_Object (gnat_ex_id)))
5354 gnat_ex_id = Renamed_Object (gnat_ex_id);
5356 gnu_expr = gnat_to_gnu_entity (gnat_ex_id, NULL_TREE, false);
5357 gnu_etype = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
5359 else
5360 gcc_unreachable ();
5362 /* The GCC interface expects NULL to be passed for catch all handlers, so
5363 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
5364 is integer_zero_node. It would not work, however, because GCC's
5365 notion of "catch all" is stronger than our notion of "others". Until
5366 we correctly use the cleanup interface as well, doing that would
5367 prevent the "all others" handlers from being seen, because nothing
5368 can be caught beyond a catch all from GCC's point of view. */
5369 gnu_etypes_list = tree_cons (NULL_TREE, gnu_etype, gnu_etypes_list);
5372 start_stmt_group ();
5373 gnat_pushlevel ();
5375 /* Expand a call to the begin_handler hook at the beginning of the handler,
5376 and arrange for a call to the end_handler hook to occur on every possible
5377 exit path.
5379 The hooks expect a pointer to the low level occurrence. This is required
5380 for our stack management scheme because a raise inside the handler pushes
5381 a new occurrence on top of the stack, which means that this top does not
5382 necessarily match the occurrence this handler was dealing with.
5384 __builtin_eh_pointer references the exception occurrence being
5385 propagated. Upon handler entry, this is the exception for which the
5386 handler is triggered. This might not be the case upon handler exit,
5387 however, as we might have a new occurrence propagated by the handler's
5388 body, and the end_handler hook called as a cleanup in this context.
5390 We use a local variable to retrieve the incoming value at handler entry
5391 time, and reuse it to feed the end_handler hook's argument at exit. */
5393 tree gnu_current_exc_ptr
5394 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER),
5395 1, integer_zero_node);
5396 tree prev_gnu_incoming_exc_ptr = gnu_incoming_exc_ptr;
5397 gnu_incoming_exc_ptr
5398 = create_var_decl (get_identifier ("EXPTR"), NULL_TREE,
5399 ptr_type_node, gnu_current_exc_ptr,
5400 false, false, false, false, false, true, true,
5401 NULL, gnat_node);
5403 add_stmt_with_node (build_call_n_expr (begin_handler_decl, 1,
5404 gnu_incoming_exc_ptr),
5405 gnat_node);
5407 /* Declare and initialize the choice parameter, if present. */
5408 if (Present (Choice_Parameter (gnat_node)))
5410 tree gnu_param
5411 = gnat_to_gnu_entity (Choice_Parameter (gnat_node), NULL_TREE, true);
5413 add_stmt (build_call_n_expr
5414 (set_exception_parameter_decl, 2,
5415 build_unary_op (ADDR_EXPR, NULL_TREE, gnu_param),
5416 gnu_incoming_exc_ptr));
5419 add_stmt_list (Statements (gnat_node));
5421 /* We don't have an End_Label at hand to set the location of the cleanup
5422 actions, so we use that of the exception handler itself instead. */
5423 tree stmt = build_call_n_expr (end_handler_decl, 1, gnu_incoming_exc_ptr);
5424 if (stmt_list_cannot_alter_control_flow_p (Statements (gnat_node)))
5425 add_stmt_with_node (stmt, gnat_node);
5426 else
5427 add_cleanup (stmt, gnat_node);
5429 gnat_poplevel ();
5431 gnu_incoming_exc_ptr = prev_gnu_incoming_exc_ptr;
5433 return
5434 build2 (CATCH_EXPR, void_type_node, gnu_etypes_list, end_stmt_group ());
5437 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
5439 static void
5440 Compilation_Unit_to_gnu (Node_Id gnat_node)
5442 const Node_Id gnat_unit = Unit (gnat_node);
5443 const bool body_p = (Nkind (gnat_unit) == N_Package_Body
5444 || Nkind (gnat_unit) == N_Subprogram_Body);
5445 const Entity_Id gnat_unit_entity = Defining_Entity (gnat_unit);
5446 Entity_Id gnat_entity;
5447 Node_Id gnat_pragma;
5448 /* Make the decl for the elaboration procedure. Emit debug info for it, so
5449 that users can break into their elaboration code in debuggers. Kludge:
5450 don't consider it as a definition so that we have a line map for its body,
5451 but no subprogram description in debug info. */
5452 tree gnu_elab_proc_decl
5453 = create_subprog_decl
5454 (create_concat_name (gnat_unit_entity, body_p ? "elabb" : "elabs"),
5455 NULL_TREE, void_ftype, NULL_TREE,
5456 is_disabled, true, false, true, true, false, NULL, gnat_unit);
5457 struct elab_info *info;
5459 vec_safe_push (gnu_elab_proc_stack, gnu_elab_proc_decl);
5460 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl) = 1;
5462 /* Initialize the information structure for the function. */
5463 allocate_struct_function (gnu_elab_proc_decl, false);
5464 set_cfun (NULL);
5466 current_function_decl = NULL_TREE;
5468 start_stmt_group ();
5469 gnat_pushlevel ();
5471 /* For a body, first process the spec if there is one. */
5472 if (Nkind (gnat_unit) == N_Package_Body
5473 || (Nkind (gnat_unit) == N_Subprogram_Body && !Acts_As_Spec (gnat_node)))
5474 add_stmt (gnat_to_gnu (Library_Unit (gnat_node)));
5476 if (type_annotate_only && gnat_node == Cunit (Main_Unit))
5478 elaborate_all_entities (gnat_node);
5480 if (Nkind (gnat_unit) == N_Subprogram_Declaration
5481 || Nkind (gnat_unit) == N_Generic_Package_Declaration
5482 || Nkind (gnat_unit) == N_Generic_Subprogram_Declaration)
5483 return;
5486 /* Then process any pragmas and declarations preceding the unit. */
5487 for (gnat_pragma = First (Context_Items (gnat_node));
5488 Present (gnat_pragma);
5489 gnat_pragma = Next (gnat_pragma))
5490 if (Nkind (gnat_pragma) == N_Pragma)
5491 add_stmt (gnat_to_gnu (gnat_pragma));
5492 process_decls (Declarations (Aux_Decls_Node (gnat_node)), Empty, Empty,
5493 true, true);
5495 /* Process the unit itself. */
5496 add_stmt (gnat_to_gnu (gnat_unit));
5498 /* Generate code for all the inlined subprograms. */
5499 for (gnat_entity = First_Inlined_Subprogram (gnat_node);
5500 Present (gnat_entity);
5501 gnat_entity = Next_Inlined_Subprogram (gnat_entity))
5503 Node_Id gnat_body;
5505 /* Without optimization, process only the required subprograms. */
5506 if (!optimize && !Has_Pragma_Inline_Always (gnat_entity))
5507 continue;
5509 /* The set of inlined subprograms is computed from data recorded early
5510 during expansion and it can be a strict superset of the final set
5511 computed after semantic analysis, for example if a call to such a
5512 subprogram occurs in a pragma Assert and assertions are disabled.
5513 In that case, semantic analysis resets Is_Public to false but the
5514 entry for the subprogram in the inlining tables is stalled. */
5515 if (!Is_Public (gnat_entity))
5516 continue;
5518 gnat_body = Parent (Declaration_Node (gnat_entity));
5519 if (Nkind (gnat_body) != N_Subprogram_Body)
5521 /* ??? This happens when only the spec of a package is provided. */
5522 if (No (Corresponding_Body (gnat_body)))
5523 continue;
5525 gnat_body
5526 = Parent (Declaration_Node (Corresponding_Body (gnat_body)));
5529 /* Define the entity first so we set DECL_EXTERNAL. */
5530 gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
5531 add_stmt (gnat_to_gnu (gnat_body));
5534 /* Process any pragmas and actions following the unit. */
5535 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node)));
5536 add_stmt_list (Actions (Aux_Decls_Node (gnat_node)));
5537 finalize_from_limited_with ();
5539 /* Save away what we've made so far and finish it up. */
5540 set_current_block_context (gnu_elab_proc_decl);
5541 gnat_poplevel ();
5542 DECL_SAVED_TREE (gnu_elab_proc_decl) = end_stmt_group ();
5543 set_end_locus_from_node (gnu_elab_proc_decl, gnat_unit);
5544 gnu_elab_proc_stack->pop ();
5546 /* Record this potential elaboration procedure for later processing. */
5547 info = ggc_alloc<elab_info> ();
5548 info->next = elab_info_list;
5549 info->elab_proc = gnu_elab_proc_decl;
5550 info->gnat_node = gnat_node;
5551 elab_info_list = info;
5553 /* Force the processing for all nodes that remain in the queue. */
5554 process_deferred_decl_context (true);
5557 /* Mark COND, a boolean expression, as predicating a call to a noreturn
5558 function, i.e. predict that it is very likely false, and return it.
5560 The compiler will automatically predict the last edge leading to a call
5561 to a noreturn function as very unlikely taken. This function makes it
5562 possible to expand the prediction to predecessors in case the condition
5563 is made up of several short-circuit operators. */
5565 static tree
5566 build_noreturn_cond (tree cond)
5568 tree fn = builtin_decl_explicit (BUILT_IN_EXPECT);
5569 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
5570 tree pred_type = TREE_VALUE (arg_types);
5571 tree expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
5573 tree t = build_call_expr (fn, 3,
5574 fold_convert (pred_type, cond),
5575 build_int_cst (expected_type, 0),
5576 build_int_cst (integer_type_node,
5577 PRED_NORETURN));
5579 return build1 (NOP_EXPR, boolean_type_node, t);
5582 /* Subroutine of gnat_to_gnu to translate GNAT_RANGE, a node representing a
5583 range of values, into GNU_LOW and GNU_HIGH bounds. */
5585 static void
5586 Range_to_gnu (Node_Id gnat_range, tree *gnu_low, tree *gnu_high)
5588 /* GNAT_RANGE is either an N_Range or an identifier denoting a subtype. */
5589 switch (Nkind (gnat_range))
5591 case N_Range:
5592 *gnu_low = gnat_to_gnu (Low_Bound (gnat_range));
5593 *gnu_high = gnat_to_gnu (High_Bound (gnat_range));
5594 break;
5596 case N_Expanded_Name:
5597 case N_Identifier:
5599 tree gnu_range_type = get_unpadded_type (Entity (gnat_range));
5600 tree gnu_range_base_type = get_base_type (gnu_range_type);
5602 *gnu_low
5603 = convert (gnu_range_base_type, TYPE_MIN_VALUE (gnu_range_type));
5604 *gnu_high
5605 = convert (gnu_range_base_type, TYPE_MAX_VALUE (gnu_range_type));
5607 break;
5609 default:
5610 gcc_unreachable ();
5614 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Raise_xxx_Error,
5615 to a GCC tree and return it. GNU_RESULT_TYPE_P is a pointer to where
5616 we should place the result type. */
5618 static tree
5619 Raise_Error_to_gnu (Node_Id gnat_node, tree *gnu_result_type_p)
5621 const Node_Kind kind = Nkind (gnat_node);
5622 const int reason = UI_To_Int (Reason (gnat_node));
5623 const Node_Id gnat_cond = Condition (gnat_node);
5624 const bool with_extra_info
5625 = Exception_Extra_Info
5626 && !No_Exception_Handlers_Set ()
5627 && No (get_exception_label (kind));
5628 tree gnu_result = NULL_TREE, gnu_cond = NULL_TREE;
5630 /* The following processing is not required for correctness. Its purpose is
5631 to give more precise error messages and to record some information. */
5632 switch (reason)
5634 case CE_Access_Check_Failed:
5635 if (with_extra_info)
5636 gnu_result = build_call_raise_column (reason, gnat_node, kind);
5637 break;
5639 case CE_Index_Check_Failed:
5640 case CE_Range_Check_Failed:
5641 case CE_Invalid_Data:
5642 if (Present (gnat_cond) && Nkind (gnat_cond) == N_Op_Not)
5644 Node_Id gnat_index, gnat_type;
5645 tree gnu_type, gnu_index, gnu_low_bound, gnu_high_bound, disp;
5646 bool neg_p;
5647 struct loop_info_d *loop;
5649 switch (Nkind (Right_Opnd (gnat_cond)))
5651 case N_In:
5652 Range_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)),
5653 &gnu_low_bound, &gnu_high_bound);
5654 break;
5656 case N_Op_Ge:
5657 gnu_low_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5658 gnu_high_bound = NULL_TREE;
5659 break;
5661 case N_Op_Le:
5662 gnu_low_bound = NULL_TREE;
5663 gnu_high_bound = gnat_to_gnu (Right_Opnd (Right_Opnd (gnat_cond)));
5664 break;
5666 default:
5667 goto common;
5670 gnat_index = Left_Opnd (Right_Opnd (gnat_cond));
5671 gnat_type = Etype (gnat_index);
5672 gnu_type = maybe_character_type (get_unpadded_type (gnat_type));
5673 gnu_index = gnat_to_gnu (gnat_index);
5675 if (TREE_TYPE (gnu_index) != gnu_type)
5677 if (gnu_low_bound)
5678 gnu_low_bound = convert (gnu_type, gnu_low_bound);
5679 if (gnu_high_bound)
5680 gnu_high_bound = convert (gnu_type, gnu_high_bound);
5681 gnu_index = convert (gnu_type, gnu_index);
5684 if (with_extra_info
5685 && gnu_low_bound
5686 && gnu_high_bound
5687 && Known_Esize (gnat_type)
5688 && UI_To_Int (Esize (gnat_type)) <= 32)
5689 gnu_result
5690 = build_call_raise_range (reason, gnat_node, kind, gnu_index,
5691 gnu_low_bound, gnu_high_bound);
5693 /* If optimization is enabled and we are inside a loop, we try to
5694 compute invariant conditions for checks applied to the iteration
5695 variable, i.e. conditions that are independent of the variable
5696 and necessary in order for the checks to fail in the course of
5697 some iteration. If we succeed, we consider an alternative:
5699 1. If loop unswitching is enabled, we prepend these conditions
5700 to the original conditions of the checks. This will make it
5701 possible for the loop unswitching pass to replace the loop
5702 with two loops, one of which has the checks eliminated and
5703 the other has the original checks reinstated, and a prologue
5704 implementing a run-time selection. The former loop will be
5705 for example suitable for vectorization.
5707 2. Otherwise, we instead append the conditions to the original
5708 conditions of the checks. At worse, if the conditions cannot
5709 be evaluated at compile time, they will be evaluated as true
5710 at run time only when the checks have already failed, thus
5711 contributing negatively only to the size of the executable.
5712 But the hope is that these invariant conditions be evaluated
5713 at compile time to false, thus taking away the entire checks
5714 with them. */
5715 if (optimize
5716 && inside_loop_p ()
5717 && (!gnu_low_bound
5718 || (gnu_low_bound = gnat_invariant_expr (gnu_low_bound)))
5719 && (!gnu_high_bound
5720 || (gnu_high_bound = gnat_invariant_expr (gnu_high_bound)))
5721 && (loop = find_loop_for (gnu_index, &disp, &neg_p)))
5723 struct range_check_info_d *rci = ggc_alloc<range_check_info_d> ();
5724 rci->low_bound = gnu_low_bound;
5725 rci->high_bound = gnu_high_bound;
5726 rci->disp = disp;
5727 rci->neg_p = neg_p;
5728 rci->type = gnu_type;
5729 rci->inserted_cond
5730 = build1 (SAVE_EXPR, boolean_type_node, boolean_true_node);
5731 vec_safe_push (loop->checks, rci);
5732 loop->has_checks = true;
5733 gnu_cond = build_noreturn_cond (gnat_to_gnu (gnat_cond));
5734 if (flag_unswitch_loops)
5735 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5736 boolean_type_node,
5737 rci->inserted_cond,
5738 gnu_cond);
5739 else
5740 gnu_cond = build_binary_op (TRUTH_ANDIF_EXPR,
5741 boolean_type_node,
5742 gnu_cond,
5743 rci->inserted_cond);
5746 /* Or else, if aggressive loop optimizations are enabled, we just
5747 record that there are checks applied to iteration variables. */
5748 else if (optimize
5749 && flag_aggressive_loop_optimizations
5750 && inside_loop_p ()
5751 && (loop = find_loop_for (gnu_index)))
5752 loop->has_checks = true;
5754 break;
5756 default:
5757 break;
5760 /* The following processing does the common work. */
5761 common:
5762 if (!gnu_result)
5763 gnu_result = build_call_raise (reason, gnat_node, kind);
5764 set_expr_location_from_node (gnu_result, gnat_node);
5766 *gnu_result_type_p = get_unpadded_type (Etype (gnat_node));
5768 /* If the type is VOID, this is a statement, so we need to generate the code
5769 for the call. Handle a condition, if there is one. */
5770 if (VOID_TYPE_P (*gnu_result_type_p))
5772 if (Present (gnat_cond))
5774 if (!gnu_cond)
5775 gnu_cond = gnat_to_gnu (gnat_cond);
5776 gnu_result = build3 (COND_EXPR, void_type_node, gnu_cond, gnu_result,
5777 alloc_stmt_list ());
5780 else
5781 gnu_result = build1 (NULL_EXPR, *gnu_result_type_p, gnu_result);
5783 return gnu_result;
5786 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
5787 parameter of a call. */
5789 static bool
5790 lhs_or_actual_p (Node_Id gnat_node)
5792 Node_Id gnat_parent = Parent (gnat_node);
5793 Node_Kind kind = Nkind (gnat_parent);
5795 if (kind == N_Assignment_Statement && Name (gnat_parent) == gnat_node)
5796 return true;
5798 if ((kind == N_Procedure_Call_Statement || kind == N_Function_Call)
5799 && Name (gnat_parent) != gnat_node)
5800 return true;
5802 if (kind == N_Parameter_Association)
5803 return true;
5805 return false;
5808 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
5809 of an assignment or an actual parameter of a call. */
5811 static bool
5812 present_in_lhs_or_actual_p (Node_Id gnat_node)
5814 Node_Kind kind;
5816 if (lhs_or_actual_p (gnat_node))
5817 return true;
5819 kind = Nkind (Parent (gnat_node));
5821 if ((kind == N_Type_Conversion || kind == N_Unchecked_Type_Conversion)
5822 && lhs_or_actual_p (Parent (gnat_node)))
5823 return true;
5825 return false;
5828 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
5829 as gigi is concerned. This is used to avoid conversions on the LHS. */
5831 static bool
5832 unchecked_conversion_nop (Node_Id gnat_node)
5834 Entity_Id from_type, to_type;
5836 /* The conversion must be on the LHS of an assignment or an actual parameter
5837 of a call. Otherwise, even if the conversion was essentially a no-op, it
5838 could de facto ensure type consistency and this should be preserved. */
5839 if (!lhs_or_actual_p (gnat_node))
5840 return false;
5842 from_type = Etype (Expression (gnat_node));
5844 /* We're interested in artificial conversions generated by the front-end
5845 to make private types explicit, e.g. in Expand_Assign_Array. */
5846 if (!Is_Private_Type (from_type))
5847 return false;
5849 from_type = Underlying_Type (from_type);
5850 to_type = Etype (gnat_node);
5852 /* The direct conversion to the underlying type is a no-op. */
5853 if (to_type == from_type)
5854 return true;
5856 /* For an array subtype, the conversion to the PAIT is a no-op. */
5857 if (Ekind (from_type) == E_Array_Subtype
5858 && to_type == Packed_Array_Impl_Type (from_type))
5859 return true;
5861 /* For a record subtype, the conversion to the type is a no-op. */
5862 if (Ekind (from_type) == E_Record_Subtype
5863 && to_type == Etype (from_type))
5864 return true;
5866 return false;
5869 /* Return true if GNAT_NODE represents a statement. */
5871 static bool
5872 statement_node_p (Node_Id gnat_node)
5874 const Node_Kind kind = Nkind (gnat_node);
5876 if (kind == N_Label)
5877 return true;
5879 if (IN (kind, N_Statement_Other_Than_Procedure_Call))
5880 return true;
5882 if (kind == N_Procedure_Call_Statement)
5883 return true;
5885 if (IN (kind, N_Raise_xxx_Error) && Ekind (Etype (gnat_node)) == E_Void)
5886 return true;
5888 return false;
5891 /* This function is the driver of the GNAT to GCC tree transformation process.
5892 It is the entry point of the tree transformer. GNAT_NODE is the root of
5893 some GNAT tree. Return the root of the corresponding GCC tree. If this
5894 is an expression, return the GCC equivalent of the expression. If this
5895 is a statement, return the statement or add it to the current statement
5896 group, in which case anything returned is to be interpreted as occurring
5897 after anything added. */
5899 tree
5900 gnat_to_gnu (Node_Id gnat_node)
5902 const Node_Kind kind = Nkind (gnat_node);
5903 bool went_into_elab_proc = false;
5904 tree gnu_result = error_mark_node; /* Default to no value. */
5905 tree gnu_result_type = void_type_node;
5906 tree gnu_expr, gnu_lhs, gnu_rhs;
5907 Node_Id gnat_temp;
5908 bool sync = false;
5910 /* Save node number for error message and set location information. */
5911 error_gnat_node = gnat_node;
5912 Sloc_to_locus (Sloc (gnat_node), &input_location);
5914 /* If we are only annotating types and this node is a statement, return
5915 an empty statement list. */
5916 if (type_annotate_only && statement_node_p (gnat_node))
5917 return alloc_stmt_list ();
5919 /* If we are only annotating types and this node is a subexpression, return
5920 a NULL_EXPR, but filter out nodes appearing in the expressions attached
5921 to packed array implementation types. */
5922 if (type_annotate_only
5923 && IN (kind, N_Subexpr)
5924 && !(((IN (kind, N_Op) && kind != N_Op_Expon)
5925 || kind == N_Type_Conversion)
5926 && Is_Integer_Type (Etype (gnat_node)))
5927 && !(kind == N_Attribute_Reference
5928 && Get_Attribute_Id (Attribute_Name (gnat_node)) == Attr_Length
5929 && Ekind (Etype (Prefix (gnat_node))) == E_Array_Subtype
5930 && !Is_Constr_Subt_For_U_Nominal (Etype (Prefix (gnat_node))))
5931 && kind != N_Expanded_Name
5932 && kind != N_Identifier
5933 && !Compile_Time_Known_Value (gnat_node))
5934 return build1 (NULL_EXPR, get_unpadded_type (Etype (gnat_node)),
5935 build_call_raise (CE_Range_Check_Failed, gnat_node,
5936 N_Raise_Constraint_Error));
5938 if ((statement_node_p (gnat_node) && kind != N_Null_Statement)
5939 || kind == N_Handled_Sequence_Of_Statements
5940 || kind == N_Implicit_Label_Declaration)
5942 tree current_elab_proc = get_elaboration_procedure ();
5944 /* If this is a statement and we are at top level, it must be part of
5945 the elaboration procedure, so mark us as being in that procedure. */
5946 if (!current_function_decl)
5948 current_function_decl = current_elab_proc;
5949 went_into_elab_proc = true;
5952 /* If we are in the elaboration procedure, check if we are violating a
5953 No_Elaboration_Code restriction by having a statement there. Don't
5954 check for a possible No_Elaboration_Code restriction violation on
5955 N_Handled_Sequence_Of_Statements, as we want to signal an error on
5956 every nested real statement instead. This also avoids triggering
5957 spurious errors on dummy (empty) sequences created by the front-end
5958 for package bodies in some cases. */
5959 if (current_function_decl == current_elab_proc
5960 && kind != N_Handled_Sequence_Of_Statements
5961 && kind != N_Implicit_Label_Declaration)
5962 Check_Elaboration_Code_Allowed (gnat_node);
5965 switch (kind)
5967 /********************************/
5968 /* Chapter 2: Lexical Elements */
5969 /********************************/
5971 case N_Identifier:
5972 case N_Expanded_Name:
5973 case N_Operator_Symbol:
5974 case N_Defining_Identifier:
5975 case N_Defining_Operator_Symbol:
5976 gnu_result = Identifier_to_gnu (gnat_node, &gnu_result_type);
5978 /* If atomic access is required on the RHS, build the atomic load. */
5979 if (atomic_access_required_p (gnat_node, &sync)
5980 && !present_in_lhs_or_actual_p (gnat_node))
5981 gnu_result = build_atomic_load (gnu_result, sync);
5982 break;
5984 case N_Integer_Literal:
5986 tree gnu_type;
5988 /* Get the type of the result, looking inside any padding and
5989 justified modular types. Then get the value in that type. */
5990 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
5992 if (TREE_CODE (gnu_type) == RECORD_TYPE
5993 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
5994 gnu_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
5996 gnu_result = UI_To_gnu (Intval (gnat_node), gnu_type);
5998 /* If the result overflows (meaning it doesn't fit in its base type),
5999 abort. We would like to check that the value is within the range
6000 of the subtype, but that causes problems with subtypes whose usage
6001 will raise Constraint_Error and with biased representation, so
6002 we don't. */
6003 gcc_assert (!TREE_OVERFLOW (gnu_result));
6005 break;
6007 case N_Character_Literal:
6008 /* If a Entity is present, it means that this was one of the
6009 literals in a user-defined character type. In that case,
6010 just return the value in the CONST_DECL. Otherwise, use the
6011 character code. In that case, the base type should be an
6012 INTEGER_TYPE, but we won't bother checking for that. */
6013 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6014 if (Present (Entity (gnat_node)))
6015 gnu_result = DECL_INITIAL (get_gnu_tree (Entity (gnat_node)));
6016 else
6017 gnu_result
6018 = build_int_cst (gnu_result_type,
6019 UI_To_CC (Char_Literal_Value (gnat_node)));
6020 break;
6022 case N_Real_Literal:
6023 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6025 /* If this is of a fixed-point type, the value we want is the value of
6026 the corresponding integer. */
6027 if (Is_Fixed_Point_Type (Underlying_Type (Etype (gnat_node))))
6029 gnu_result = UI_To_gnu (Corresponding_Integer_Value (gnat_node),
6030 gnu_result_type);
6031 gcc_assert (!TREE_OVERFLOW (gnu_result));
6034 else
6036 Ureal ur_realval = Realval (gnat_node);
6038 /* First convert the value to a machine number if it isn't already.
6039 That will force the base to 2 for non-zero values and simplify
6040 the rest of the logic. */
6041 if (!Is_Machine_Number (gnat_node))
6042 ur_realval
6043 = Machine (Base_Type (Underlying_Type (Etype (gnat_node))),
6044 ur_realval, Round_Even, gnat_node);
6046 if (UR_Is_Zero (ur_realval))
6047 gnu_result = build_real (gnu_result_type, dconst0);
6048 else
6050 REAL_VALUE_TYPE tmp;
6052 gnu_result = UI_To_gnu (Numerator (ur_realval), gnu_result_type);
6054 /* The base must be 2 as Machine guarantees this, so we scale
6055 the value, which we know can fit in the mantissa of the type
6056 (hence the use of that type above). */
6057 gcc_assert (Rbase (ur_realval) == 2);
6058 real_ldexp (&tmp, &TREE_REAL_CST (gnu_result),
6059 - UI_To_Int (Denominator (ur_realval)));
6060 gnu_result = build_real (gnu_result_type, tmp);
6063 /* Now see if we need to negate the result. Do it this way to
6064 properly handle -0. */
6065 if (UR_Is_Negative (Realval (gnat_node)))
6066 gnu_result
6067 = build_unary_op (NEGATE_EXPR, get_base_type (gnu_result_type),
6068 gnu_result);
6071 break;
6073 case N_String_Literal:
6074 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6075 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type)) == HOST_BITS_PER_CHAR)
6077 String_Id gnat_string = Strval (gnat_node);
6078 int length = String_Length (gnat_string);
6079 int i;
6080 char *string;
6081 if (length >= ALLOCA_THRESHOLD)
6082 string = XNEWVEC (char, length + 1);
6083 else
6084 string = (char *) alloca (length + 1);
6086 /* Build the string with the characters in the literal. Note
6087 that Ada strings are 1-origin. */
6088 for (i = 0; i < length; i++)
6089 string[i] = Get_String_Char (gnat_string, i + 1);
6091 /* Put a null at the end of the string in case it's in a context
6092 where GCC will want to treat it as a C string. */
6093 string[i] = 0;
6095 gnu_result = build_string (length, string);
6097 /* Strings in GCC don't normally have types, but we want
6098 this to not be converted to the array type. */
6099 TREE_TYPE (gnu_result) = gnu_result_type;
6101 if (length >= ALLOCA_THRESHOLD)
6102 free (string);
6104 else
6106 /* Build a list consisting of each character, then make
6107 the aggregate. */
6108 String_Id gnat_string = Strval (gnat_node);
6109 int length = String_Length (gnat_string);
6110 int i;
6111 tree gnu_idx = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
6112 tree gnu_one_node = convert (TREE_TYPE (gnu_idx), integer_one_node);
6113 vec<constructor_elt, va_gc> *gnu_vec;
6114 vec_alloc (gnu_vec, length);
6116 for (i = 0; i < length; i++)
6118 tree t = build_int_cst (TREE_TYPE (gnu_result_type),
6119 Get_String_Char (gnat_string, i + 1));
6121 CONSTRUCTOR_APPEND_ELT (gnu_vec, gnu_idx, t);
6122 gnu_idx = int_const_binop (PLUS_EXPR, gnu_idx, gnu_one_node);
6125 gnu_result = gnat_build_constructor (gnu_result_type, gnu_vec);
6127 break;
6129 case N_Pragma:
6130 gnu_result = Pragma_to_gnu (gnat_node);
6131 break;
6133 /**************************************/
6134 /* Chapter 3: Declarations and Types */
6135 /**************************************/
6137 case N_Subtype_Declaration:
6138 case N_Full_Type_Declaration:
6139 case N_Incomplete_Type_Declaration:
6140 case N_Private_Type_Declaration:
6141 case N_Private_Extension_Declaration:
6142 case N_Task_Type_Declaration:
6143 process_type (Defining_Entity (gnat_node));
6144 gnu_result = alloc_stmt_list ();
6145 break;
6147 case N_Object_Declaration:
6148 case N_Exception_Declaration:
6149 gnat_temp = Defining_Entity (gnat_node);
6150 gnu_result = alloc_stmt_list ();
6152 /* If we are just annotating types and this object has an unconstrained
6153 or task type, don't elaborate it. */
6154 if (type_annotate_only
6155 && (((Is_Array_Type (Etype (gnat_temp))
6156 || Is_Record_Type (Etype (gnat_temp)))
6157 && !Is_Constrained (Etype (gnat_temp)))
6158 || Is_Concurrent_Type (Etype (gnat_temp))))
6159 break;
6161 if (Present (Expression (gnat_node))
6162 && !(kind == N_Object_Declaration && No_Initialization (gnat_node))
6163 && (!type_annotate_only
6164 || Compile_Time_Known_Value (Expression (gnat_node))))
6166 gnu_expr = gnat_to_gnu (Expression (gnat_node));
6167 if (Do_Range_Check (Expression (gnat_node)))
6168 gnu_expr
6169 = emit_range_check (gnu_expr, Etype (gnat_temp), gnat_node);
6171 if (type_annotate_only && TREE_CODE (gnu_expr) == ERROR_MARK)
6172 gnu_expr = NULL_TREE;
6174 else
6175 gnu_expr = NULL_TREE;
6177 /* If this is a deferred constant with an address clause, we ignore the
6178 full view since the clause is on the partial view and we cannot have
6179 2 different GCC trees for the object. The only bits of the full view
6180 we will use is the initializer, but it will be directly fetched. */
6181 if (Ekind (gnat_temp) == E_Constant
6182 && Present (Address_Clause (gnat_temp))
6183 && Present (Full_View (gnat_temp)))
6184 save_gnu_tree (Full_View (gnat_temp), error_mark_node, true);
6186 /* If this object has its elaboration delayed, we must force evaluation
6187 of GNU_EXPR now and save it for the freeze point. Note that we need
6188 not do anything special at the global level since the lifetime of the
6189 temporary is fully contained within the elaboration routine. */
6190 if (Present (Freeze_Node (gnat_temp)))
6192 if (gnu_expr)
6194 gnu_result = gnat_save_expr (gnu_expr);
6195 save_gnu_tree (gnat_node, gnu_result, true);
6198 else
6199 gnat_to_gnu_entity (gnat_temp, gnu_expr, true);
6200 break;
6202 case N_Object_Renaming_Declaration:
6203 gnat_temp = Defining_Entity (gnat_node);
6204 gnu_result = alloc_stmt_list ();
6206 /* Don't do anything if this renaming is handled by the front end and it
6207 does not need debug info. Note that we consider renamings don't need
6208 debug info when optimizing: our way to describe them has a
6209 memory/elaboration footprint.
6211 Don't do anything neither if we are just annotating types and this
6212 object has a composite or task type, don't elaborate it. */
6213 if ((!Is_Renaming_Of_Object (gnat_temp)
6214 || (Needs_Debug_Info (gnat_temp)
6215 && !optimize
6216 && can_materialize_object_renaming_p
6217 (Renamed_Object (gnat_temp))))
6218 && ! (type_annotate_only
6219 && (Is_Array_Type (Etype (gnat_temp))
6220 || Is_Record_Type (Etype (gnat_temp))
6221 || Is_Concurrent_Type (Etype (gnat_temp)))))
6223 tree gnu_temp
6224 = gnat_to_gnu_entity (gnat_temp,
6225 gnat_to_gnu (Renamed_Object (gnat_temp)),
6226 true);
6227 /* See case 2 of renaming in gnat_to_gnu_entity. */
6228 if (TREE_SIDE_EFFECTS (gnu_temp))
6229 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_temp);
6231 break;
6233 case N_Exception_Renaming_Declaration:
6234 gnat_temp = Defining_Entity (gnat_node);
6235 gnu_result = alloc_stmt_list ();
6237 /* See the above case for the rationale. */
6238 if (Present (Renamed_Entity (gnat_temp)))
6240 tree gnu_temp
6241 = gnat_to_gnu_entity (gnat_temp,
6242 gnat_to_gnu (Renamed_Entity (gnat_temp)),
6243 true);
6244 if (TREE_SIDE_EFFECTS (gnu_temp))
6245 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_temp);
6247 break;
6249 case N_Subprogram_Renaming_Declaration:
6251 const Node_Id gnat_renaming = Defining_Entity (gnat_node);
6252 const Node_Id gnat_renamed = Renamed_Entity (gnat_renaming);
6254 gnu_result = alloc_stmt_list ();
6256 /* Materializing renamed subprograms will only benefit the debugging
6257 information as they aren't referenced in the generated code. So
6258 skip them when they aren't needed. Avoid doing this if:
6260 - there is a freeze node: in this case the renamed entity is not
6261 elaborated yet,
6262 - the renamed subprogram is intrinsic: it will not be available in
6263 the debugging information (note that both or only one of the
6264 renaming and the renamed subprograms can be intrinsic). */
6265 if (!type_annotate_only
6266 && Needs_Debug_Info (gnat_renaming)
6267 && No (Freeze_Node (gnat_renaming))
6268 && Present (gnat_renamed)
6269 && (Ekind (gnat_renamed) == E_Function
6270 || Ekind (gnat_renamed) == E_Procedure)
6271 && !Is_Intrinsic_Subprogram (gnat_renaming)
6272 && !Is_Intrinsic_Subprogram (gnat_renamed))
6273 gnat_to_gnu_entity (gnat_renaming, gnat_to_gnu (gnat_renamed), true);
6274 break;
6277 case N_Implicit_Label_Declaration:
6278 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true);
6279 gnu_result = alloc_stmt_list ();
6280 break;
6282 case N_Number_Declaration:
6283 case N_Package_Renaming_Declaration:
6284 /* These are fully handled in the front end. */
6285 /* ??? For package renamings, find a way to use GENERIC namespaces so
6286 that we get proper debug information for them. */
6287 gnu_result = alloc_stmt_list ();
6288 break;
6290 /*************************************/
6291 /* Chapter 4: Names and Expressions */
6292 /*************************************/
6294 case N_Explicit_Dereference:
6295 /* Make sure the designated type is complete before dereferencing. */
6296 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6297 gnu_result = gnat_to_gnu (Prefix (gnat_node));
6298 gnu_result = build_unary_op (INDIRECT_REF, NULL_TREE, gnu_result);
6300 /* If atomic access is required on the RHS, build the atomic load. */
6301 if (atomic_access_required_p (gnat_node, &sync)
6302 && !present_in_lhs_or_actual_p (gnat_node))
6303 gnu_result = build_atomic_load (gnu_result, sync);
6304 break;
6306 case N_Indexed_Component:
6308 tree gnu_array_object
6309 = gnat_to_gnu (adjust_for_implicit_deref (Prefix (gnat_node)));
6310 tree gnu_type;
6311 int ndim;
6312 int i;
6313 Node_Id *gnat_expr_array;
6315 gnu_array_object = maybe_implicit_deref (gnu_array_object);
6317 /* Convert vector inputs to their representative array type, to fit
6318 what the code below expects. */
6319 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object)))
6321 if (present_in_lhs_or_actual_p (gnat_node))
6322 gnat_mark_addressable (gnu_array_object);
6323 gnu_array_object = maybe_vector_array (gnu_array_object);
6326 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
6328 /* If we got a padded type, remove it too. */
6329 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object)))
6330 gnu_array_object
6331 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object))),
6332 gnu_array_object);
6334 /* The failure of this assertion will very likely come from a missing
6335 expansion for a packed array access. */
6336 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object)) == ARRAY_TYPE);
6338 /* First compute the number of dimensions of the array, then
6339 fill the expression array, the order depending on whether
6340 this is a Convention_Fortran array or not. */
6341 for (ndim = 1, gnu_type = TREE_TYPE (gnu_array_object);
6342 TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
6343 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type));
6344 ndim++, gnu_type = TREE_TYPE (gnu_type))
6347 gnat_expr_array = XALLOCAVEC (Node_Id, ndim);
6349 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object)))
6350 for (i = ndim - 1, gnat_temp = First (Expressions (gnat_node));
6351 i >= 0;
6352 i--, gnat_temp = Next (gnat_temp))
6353 gnat_expr_array[i] = gnat_temp;
6354 else
6355 for (i = 0, gnat_temp = First (Expressions (gnat_node));
6356 i < ndim;
6357 i++, gnat_temp = Next (gnat_temp))
6358 gnat_expr_array[i] = gnat_temp;
6360 /* Start with the prefix and build the successive references. */
6361 gnu_result = gnu_array_object;
6363 for (i = 0, gnu_type = TREE_TYPE (gnu_array_object);
6364 i < ndim;
6365 i++, gnu_type = TREE_TYPE (gnu_type))
6367 gcc_assert (TREE_CODE (gnu_type) == ARRAY_TYPE);
6368 gnat_temp = gnat_expr_array[i];
6369 gnu_expr = maybe_character_value (gnat_to_gnu (gnat_temp));
6370 struct loop_info_d *loop;
6372 gnu_result
6373 = build_binary_op (ARRAY_REF, NULL_TREE, gnu_result, gnu_expr);
6375 /* Array accesses are bound-checked so they cannot trap, but this
6376 is valid only if they are not hoisted ahead of the check. We
6377 need to mark them as no-trap to get decent loop optimizations
6378 in the presence of -fnon-call-exceptions, so we do it when we
6379 know that the original expression had no side-effects. */
6380 if (TREE_CODE (gnu_result) == ARRAY_REF
6381 && !(Nkind (gnat_temp) == N_Identifier
6382 && Ekind (Entity (gnat_temp)) == E_Constant))
6383 TREE_THIS_NOTRAP (gnu_result) = 1;
6385 /* If aggressive loop optimizations are enabled, we warn for loops
6386 overrunning a simple array of size 1 not at the end of a record.
6387 This is aimed to catch misuses of the trailing array idiom. */
6388 if (optimize
6389 && flag_aggressive_loop_optimizations
6390 && inside_loop_p ()
6391 && TREE_CODE (TREE_TYPE (gnu_type)) != ARRAY_TYPE
6392 && TREE_CODE (gnu_array_object) != ARRAY_REF
6393 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_type)),
6394 TYPE_MAX_VALUE (TYPE_DOMAIN (gnu_type)))
6395 && !array_at_struct_end_p (gnu_result)
6396 && (loop = find_loop_for (gnu_expr))
6397 && !loop->artificial
6398 && !loop->has_checks
6399 && tree_int_cst_equal (TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_type)),
6400 loop->low_bound)
6401 && can_be_lower_p (loop->low_bound, loop->high_bound)
6402 && !loop->warned_aggressive_loop_optimizations
6403 && warning (OPT_Waggressive_loop_optimizations,
6404 "out-of-bounds access may be optimized away"))
6406 inform (EXPR_LOCATION (loop->stmt), "containing loop");
6407 loop->warned_aggressive_loop_optimizations = true;
6411 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6413 /* If atomic access is required on the RHS, build the atomic load. */
6414 if (atomic_access_required_p (gnat_node, &sync)
6415 && !present_in_lhs_or_actual_p (gnat_node))
6416 gnu_result = build_atomic_load (gnu_result, sync);
6418 break;
6420 case N_Slice:
6422 tree gnu_array_object
6423 = gnat_to_gnu (adjust_for_implicit_deref (Prefix (gnat_node)));
6425 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6427 gnu_array_object = maybe_implicit_deref (gnu_array_object);
6428 gnu_array_object = maybe_unconstrained_array (gnu_array_object);
6430 gnu_expr = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type));
6431 gnu_expr = maybe_character_value (gnu_expr);
6433 /* If this is a slice with non-constant size of an array with constant
6434 size, set the maximum size for the allocation of temporaries. */
6435 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type))
6436 && TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object))))
6437 TYPE_ARRAY_MAX_SIZE (gnu_result_type)
6438 = TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object));
6440 gnu_result = build_binary_op (ARRAY_RANGE_REF, gnu_result_type,
6441 gnu_array_object, gnu_expr);
6443 break;
6445 case N_Selected_Component:
6447 Entity_Id gnat_prefix
6448 = adjust_for_implicit_deref (Prefix (gnat_node));
6449 Entity_Id gnat_field = Entity (Selector_Name (gnat_node));
6450 tree gnu_prefix = gnat_to_gnu (gnat_prefix);
6452 gnu_prefix = maybe_implicit_deref (gnu_prefix);
6454 /* gnat_to_gnu_entity does not save the GNU tree made for renamed
6455 discriminants so avoid making recursive calls on each reference
6456 to them by following the appropriate link directly here. */
6457 if (Ekind (gnat_field) == E_Discriminant)
6459 /* For discriminant references in tagged types always substitute
6460 the corresponding discriminant as the actual component. */
6461 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix))))
6462 while (Present (Corresponding_Discriminant (gnat_field)))
6463 gnat_field = Corresponding_Discriminant (gnat_field);
6465 /* For discriminant references in untagged types always substitute
6466 the corresponding stored discriminant. */
6467 else if (Present (Corresponding_Discriminant (gnat_field)))
6468 gnat_field = Original_Record_Component (gnat_field);
6471 /* Handle extracting the real or imaginary part of a complex.
6472 The real part is the first field and the imaginary the last. */
6473 if (TREE_CODE (TREE_TYPE (gnu_prefix)) == COMPLEX_TYPE)
6474 gnu_result = build_unary_op (Present (Next_Entity (gnat_field))
6475 ? REALPART_EXPR : IMAGPART_EXPR,
6476 NULL_TREE, gnu_prefix);
6477 else
6479 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
6481 gnu_result
6482 = build_component_ref (gnu_prefix, gnu_field,
6483 (Nkind (Parent (gnat_node))
6484 == N_Attribute_Reference)
6485 && lvalue_required_for_attribute_p
6486 (Parent (gnat_node)));
6489 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6491 /* If atomic access is required on the RHS, build the atomic load. */
6492 if (atomic_access_required_p (gnat_node, &sync)
6493 && !present_in_lhs_or_actual_p (gnat_node))
6494 gnu_result = build_atomic_load (gnu_result, sync);
6496 break;
6498 case N_Attribute_Reference:
6500 /* The attribute designator. */
6501 const int attr = Get_Attribute_Id (Attribute_Name (gnat_node));
6503 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
6504 is a unit, not an object with a GCC equivalent. */
6505 if (attr == Attr_Elab_Spec || attr == Attr_Elab_Body)
6506 return
6507 create_subprog_decl (create_concat_name
6508 (Entity (Prefix (gnat_node)),
6509 attr == Attr_Elab_Body ? "elabb" : "elabs"),
6510 NULL_TREE, void_ftype, NULL_TREE, is_disabled,
6511 true, true, true, true, false, NULL,
6512 gnat_node);
6514 gnu_result = Attribute_to_gnu (gnat_node, &gnu_result_type, attr);
6516 break;
6518 case N_Reference:
6519 /* Like 'Access as far as we are concerned. */
6520 gnu_result = gnat_to_gnu (Prefix (gnat_node));
6521 gnu_result = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_result);
6522 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6523 break;
6525 case N_Aggregate:
6526 case N_Extension_Aggregate:
6528 tree gnu_aggr_type;
6530 /* ??? It is wrong to evaluate the type now, but there doesn't
6531 seem to be any other practical way of doing it. */
6533 gcc_assert (!Expansion_Delayed (gnat_node));
6535 gnu_aggr_type = gnu_result_type
6536 = get_unpadded_type (Etype (gnat_node));
6538 if (TREE_CODE (gnu_result_type) == RECORD_TYPE
6539 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type))
6540 gnu_aggr_type
6541 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type)));
6542 else if (TREE_CODE (gnu_result_type) == VECTOR_TYPE)
6543 gnu_aggr_type = TYPE_REPRESENTATIVE_ARRAY (gnu_result_type);
6545 if (Null_Record_Present (gnat_node))
6546 gnu_result = gnat_build_constructor (gnu_aggr_type, NULL);
6548 else if (TREE_CODE (gnu_aggr_type) == RECORD_TYPE
6549 || TREE_CODE (gnu_aggr_type) == UNION_TYPE)
6550 gnu_result
6551 = assoc_to_constructor (Etype (gnat_node),
6552 First (Component_Associations (gnat_node)),
6553 gnu_aggr_type);
6554 else if (TREE_CODE (gnu_aggr_type) == ARRAY_TYPE)
6555 gnu_result = pos_to_constructor (First (Expressions (gnat_node)),
6556 gnu_aggr_type,
6557 Component_Type (Etype (gnat_node)));
6558 else if (TREE_CODE (gnu_aggr_type) == COMPLEX_TYPE)
6559 gnu_result
6560 = build_binary_op
6561 (COMPLEX_EXPR, gnu_aggr_type,
6562 gnat_to_gnu (Expression (First
6563 (Component_Associations (gnat_node)))),
6564 gnat_to_gnu (Expression
6565 (Next
6566 (First (Component_Associations (gnat_node))))));
6567 else
6568 gcc_unreachable ();
6570 gnu_result = convert (gnu_result_type, gnu_result);
6572 break;
6574 case N_Null:
6575 if (TARGET_VTABLE_USES_DESCRIPTORS
6576 && Ekind (Etype (gnat_node)) == E_Access_Subprogram_Type
6577 && Is_Dispatch_Table_Entity (Etype (gnat_node)))
6578 gnu_result = null_fdesc_node;
6579 else
6580 gnu_result = null_pointer_node;
6581 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6582 break;
6584 case N_Type_Conversion:
6585 case N_Qualified_Expression:
6586 gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node)));
6587 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6589 /* If this is a qualified expression for a tagged type, we mark the type
6590 as used. Because of polymorphism, this might be the only reference to
6591 the tagged type in the program while objects have it as dynamic type.
6592 The debugger needs to see it to display these objects properly. */
6593 if (kind == N_Qualified_Expression && Is_Tagged_Type (Etype (gnat_node)))
6594 used_types_insert (gnu_result_type);
6596 gnu_result
6597 = convert_with_check (Etype (gnat_node), gnu_expr,
6598 Do_Overflow_Check (gnat_node),
6599 Do_Range_Check (Expression (gnat_node)),
6600 kind == N_Type_Conversion
6601 && Float_Truncate (gnat_node), gnat_node);
6602 break;
6604 case N_Unchecked_Type_Conversion:
6605 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6606 gnu_expr = maybe_character_value (gnat_to_gnu (Expression (gnat_node)));
6608 /* Skip further processing if the conversion is deemed a no-op. */
6609 if (unchecked_conversion_nop (gnat_node))
6611 gnu_result = gnu_expr;
6612 gnu_result_type = TREE_TYPE (gnu_result);
6613 break;
6616 /* If the result is a pointer type, see if we are improperly
6617 converting to a stricter alignment. */
6618 if (STRICT_ALIGNMENT && POINTER_TYPE_P (gnu_result_type)
6619 && Is_Access_Type (Etype (gnat_node)))
6621 unsigned int align = known_alignment (gnu_expr);
6622 tree gnu_obj_type = TREE_TYPE (gnu_result_type);
6623 unsigned int oalign = TYPE_ALIGN (gnu_obj_type);
6625 if (align != 0 && align < oalign && !TYPE_ALIGN_OK (gnu_obj_type))
6626 post_error_ne_tree_2
6627 ("?source alignment (^) '< alignment of & (^)",
6628 gnat_node, Designated_Type (Etype (gnat_node)),
6629 size_int (align / BITS_PER_UNIT), oalign / BITS_PER_UNIT);
6632 /* If we are converting a descriptor to a function pointer, first
6633 build the pointer. */
6634 if (TARGET_VTABLE_USES_DESCRIPTORS
6635 && TREE_TYPE (gnu_expr) == fdesc_type_node
6636 && POINTER_TYPE_P (gnu_result_type))
6637 gnu_expr = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_expr);
6639 gnu_result = unchecked_convert (gnu_result_type, gnu_expr,
6640 No_Truncation (gnat_node));
6641 break;
6643 case N_In:
6644 case N_Not_In:
6646 tree gnu_obj = gnat_to_gnu (Left_Opnd (gnat_node));
6647 tree gnu_low, gnu_high;
6649 Range_to_gnu (Right_Opnd (gnat_node), &gnu_low, &gnu_high);
6650 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6652 tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_obj));
6653 if (TREE_TYPE (gnu_obj) != gnu_op_type)
6655 gnu_obj = convert (gnu_op_type, gnu_obj);
6656 gnu_low = convert (gnu_op_type, gnu_low);
6657 gnu_high = convert (gnu_op_type, gnu_high);
6660 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
6661 ensure that GNU_OBJ is evaluated only once and perform a full range
6662 test. */
6663 if (operand_equal_p (gnu_low, gnu_high, 0))
6664 gnu_result
6665 = build_binary_op (EQ_EXPR, gnu_result_type, gnu_obj, gnu_low);
6666 else
6668 tree t1, t2;
6669 gnu_obj = gnat_protect_expr (gnu_obj);
6670 t1 = build_binary_op (GE_EXPR, gnu_result_type, gnu_obj, gnu_low);
6671 if (EXPR_P (t1))
6672 set_expr_location_from_node (t1, gnat_node);
6673 t2 = build_binary_op (LE_EXPR, gnu_result_type, gnu_obj, gnu_high);
6674 if (EXPR_P (t2))
6675 set_expr_location_from_node (t2, gnat_node);
6676 gnu_result
6677 = build_binary_op (TRUTH_ANDIF_EXPR, gnu_result_type, t1, t2);
6680 if (kind == N_Not_In)
6681 gnu_result
6682 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result), gnu_result);
6684 break;
6686 case N_Op_Divide:
6687 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6688 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6689 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6690 gnu_result = build_binary_op (FLOAT_TYPE_P (gnu_result_type)
6691 ? RDIV_EXPR
6692 : (Rounded_Result (gnat_node)
6693 ? ROUND_DIV_EXPR : TRUNC_DIV_EXPR),
6694 gnu_result_type, gnu_lhs, gnu_rhs);
6695 break;
6697 case N_Op_And:
6698 case N_Op_Or:
6699 case N_Op_Xor:
6700 /* These can either be operations on booleans or on modular types.
6701 Fall through for boolean types since that's the way GNU_CODES is
6702 set up. */
6703 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6705 enum tree_code code
6706 = (kind == N_Op_Or ? BIT_IOR_EXPR
6707 : kind == N_Op_And ? BIT_AND_EXPR
6708 : BIT_XOR_EXPR);
6710 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6711 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6712 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6713 gnu_result = build_binary_op (code, gnu_result_type,
6714 gnu_lhs, gnu_rhs);
6715 break;
6718 /* ... fall through ... */
6720 case N_Op_Eq:
6721 case N_Op_Ne:
6722 case N_Op_Lt:
6723 case N_Op_Le:
6724 case N_Op_Gt:
6725 case N_Op_Ge:
6726 case N_Op_Add:
6727 case N_Op_Subtract:
6728 case N_Op_Multiply:
6729 case N_Op_Mod:
6730 case N_Op_Rem:
6731 case N_Op_Rotate_Left:
6732 case N_Op_Rotate_Right:
6733 case N_Op_Shift_Left:
6734 case N_Op_Shift_Right:
6735 case N_Op_Shift_Right_Arithmetic:
6736 case N_And_Then:
6737 case N_Or_Else:
6739 enum tree_code code = gnu_codes[kind];
6740 bool ignore_lhs_overflow = false;
6741 location_t saved_location = input_location;
6742 tree gnu_type;
6744 gnu_lhs = gnat_to_gnu (Left_Opnd (gnat_node));
6745 gnu_rhs = gnat_to_gnu (Right_Opnd (gnat_node));
6746 gnu_type = gnu_result_type = get_unpadded_type (Etype (gnat_node));
6748 /* Pending generic support for efficient vector logical operations in
6749 GCC, convert vectors to their representative array type view and
6750 fallthrough. */
6751 gnu_lhs = maybe_vector_array (gnu_lhs);
6752 gnu_rhs = maybe_vector_array (gnu_rhs);
6754 /* If this is a comparison operator, convert any references to an
6755 unconstrained array value into a reference to the actual array. */
6756 if (TREE_CODE_CLASS (code) == tcc_comparison)
6758 gnu_lhs = maybe_unconstrained_array (gnu_lhs);
6759 gnu_rhs = maybe_unconstrained_array (gnu_rhs);
6761 tree gnu_op_type = maybe_character_type (TREE_TYPE (gnu_lhs));
6762 if (TREE_TYPE (gnu_lhs) != gnu_op_type)
6764 gnu_lhs = convert (gnu_op_type, gnu_lhs);
6765 gnu_rhs = convert (gnu_op_type, gnu_rhs);
6769 /* If this is a shift whose count is not guaranteed to be correct,
6770 we need to adjust the shift count. */
6771 if (IN (kind, N_Op_Shift) && !Shift_Count_OK (gnat_node))
6773 tree gnu_count_type = get_base_type (TREE_TYPE (gnu_rhs));
6774 tree gnu_max_shift
6775 = convert (gnu_count_type, TYPE_SIZE (gnu_type));
6777 if (kind == N_Op_Rotate_Left || kind == N_Op_Rotate_Right)
6778 gnu_rhs = build_binary_op (TRUNC_MOD_EXPR, gnu_count_type,
6779 gnu_rhs, gnu_max_shift);
6780 else if (kind == N_Op_Shift_Right_Arithmetic)
6781 gnu_rhs
6782 = build_binary_op
6783 (MIN_EXPR, gnu_count_type,
6784 build_binary_op (MINUS_EXPR,
6785 gnu_count_type,
6786 gnu_max_shift,
6787 build_int_cst (gnu_count_type, 1)),
6788 gnu_rhs);
6791 /* For right shifts, the type says what kind of shift to do,
6792 so we may need to choose a different type. In this case,
6793 we have to ignore integer overflow lest it propagates all
6794 the way down and causes a CE to be explicitly raised. */
6795 if (kind == N_Op_Shift_Right && !TYPE_UNSIGNED (gnu_type))
6797 gnu_type = gnat_unsigned_type_for (gnu_type);
6798 ignore_lhs_overflow = true;
6800 else if (kind == N_Op_Shift_Right_Arithmetic
6801 && TYPE_UNSIGNED (gnu_type))
6803 gnu_type = gnat_signed_type_for (gnu_type);
6804 ignore_lhs_overflow = true;
6807 if (gnu_type != gnu_result_type)
6809 tree gnu_old_lhs = gnu_lhs;
6810 gnu_lhs = convert (gnu_type, gnu_lhs);
6811 if (TREE_CODE (gnu_lhs) == INTEGER_CST && ignore_lhs_overflow)
6812 TREE_OVERFLOW (gnu_lhs) = TREE_OVERFLOW (gnu_old_lhs);
6813 gnu_rhs = convert (gnu_type, gnu_rhs);
6816 /* Instead of expanding overflow checks for addition, subtraction
6817 and multiplication itself, the front end will leave this to
6818 the back end when Backend_Overflow_Checks_On_Target is set. */
6819 if (Do_Overflow_Check (gnat_node)
6820 && Backend_Overflow_Checks_On_Target
6821 && (code == PLUS_EXPR || code == MINUS_EXPR || code == MULT_EXPR)
6822 && !TYPE_UNSIGNED (gnu_type)
6823 && !FLOAT_TYPE_P (gnu_type))
6824 gnu_result = build_binary_op_trapv (code, gnu_type,
6825 gnu_lhs, gnu_rhs, gnat_node);
6826 else
6828 /* Some operations, e.g. comparisons of arrays, generate complex
6829 trees that need to be annotated while they are being built. */
6830 input_location = saved_location;
6831 gnu_result = build_binary_op (code, gnu_type, gnu_lhs, gnu_rhs);
6834 /* If this is a logical shift with the shift count not verified,
6835 we must return zero if it is too large. We cannot compensate
6836 above in this case. */
6837 if ((kind == N_Op_Shift_Left || kind == N_Op_Shift_Right)
6838 && !Shift_Count_OK (gnat_node))
6839 gnu_result
6840 = build_cond_expr
6841 (gnu_type,
6842 build_binary_op (GE_EXPR, boolean_type_node,
6843 gnu_rhs,
6844 convert (TREE_TYPE (gnu_rhs),
6845 TYPE_SIZE (gnu_type))),
6846 build_int_cst (gnu_type, 0),
6847 gnu_result);
6849 break;
6851 case N_If_Expression:
6853 tree gnu_cond = gnat_to_gnu (First (Expressions (gnat_node)));
6854 tree gnu_true = gnat_to_gnu (Next (First (Expressions (gnat_node))));
6855 tree gnu_false
6856 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node)))));
6858 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6859 gnu_result
6860 = build_cond_expr (gnu_result_type, gnu_cond, gnu_true, gnu_false);
6862 break;
6864 case N_Op_Plus:
6865 gnu_result = gnat_to_gnu (Right_Opnd (gnat_node));
6866 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6867 break;
6869 case N_Op_Not:
6870 /* This case can apply to a boolean or a modular type.
6871 Fall through for a boolean operand since GNU_CODES is set
6872 up to handle this. */
6873 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node))))
6875 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6876 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6877 gnu_result = build_unary_op (BIT_NOT_EXPR, gnu_result_type,
6878 gnu_expr);
6879 break;
6882 /* ... fall through ... */
6884 case N_Op_Minus:
6885 case N_Op_Abs:
6886 gnu_expr = gnat_to_gnu (Right_Opnd (gnat_node));
6887 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6889 /* Instead of expanding overflow checks for negation and absolute
6890 value itself, the front end will leave this to the back end
6891 when Backend_Overflow_Checks_On_Target is set. */
6892 if (Do_Overflow_Check (gnat_node)
6893 && Backend_Overflow_Checks_On_Target
6894 && !TYPE_UNSIGNED (gnu_result_type)
6895 && !FLOAT_TYPE_P (gnu_result_type))
6896 gnu_result
6897 = build_unary_op_trapv (gnu_codes[kind],
6898 gnu_result_type, gnu_expr, gnat_node);
6899 else
6900 gnu_result = build_unary_op (gnu_codes[kind],
6901 gnu_result_type, gnu_expr);
6902 break;
6904 case N_Allocator:
6906 tree gnu_init = NULL_TREE;
6907 tree gnu_type;
6908 bool ignore_init_type = false;
6910 gnat_temp = Expression (gnat_node);
6912 /* The Expression operand can either be an N_Identifier or
6913 Expanded_Name, which must represent a type, or a
6914 N_Qualified_Expression, which contains both the object type and an
6915 initial value for the object. */
6916 if (Nkind (gnat_temp) == N_Identifier
6917 || Nkind (gnat_temp) == N_Expanded_Name)
6918 gnu_type = gnat_to_gnu_type (Entity (gnat_temp));
6919 else if (Nkind (gnat_temp) == N_Qualified_Expression)
6921 Entity_Id gnat_desig_type
6922 = Designated_Type (Underlying_Type (Etype (gnat_node)));
6924 ignore_init_type = Has_Constrained_Partial_View (gnat_desig_type);
6925 gnu_init = gnat_to_gnu (Expression (gnat_temp));
6927 gnu_init = maybe_unconstrained_array (gnu_init);
6928 if (Do_Range_Check (Expression (gnat_temp)))
6929 gnu_init
6930 = emit_range_check (gnu_init, gnat_desig_type, gnat_temp);
6932 if (Is_Elementary_Type (gnat_desig_type)
6933 || Is_Constrained (gnat_desig_type))
6934 gnu_type = gnat_to_gnu_type (gnat_desig_type);
6935 else
6937 gnu_type = gnat_to_gnu_type (Etype (Expression (gnat_temp)));
6938 if (TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
6939 gnu_type = TREE_TYPE (gnu_init);
6942 /* See the N_Qualified_Expression case for the rationale. */
6943 if (Is_Tagged_Type (gnat_desig_type))
6944 used_types_insert (gnu_type);
6946 gnu_init = convert (gnu_type, gnu_init);
6948 else
6949 gcc_unreachable ();
6951 gnu_result_type = get_unpadded_type (Etype (gnat_node));
6952 return build_allocator (gnu_type, gnu_init, gnu_result_type,
6953 Procedure_To_Call (gnat_node),
6954 Storage_Pool (gnat_node), gnat_node,
6955 ignore_init_type);
6957 break;
6959 /**************************/
6960 /* Chapter 5: Statements */
6961 /**************************/
6963 case N_Label:
6964 gnu_result = build1 (LABEL_EXPR, void_type_node,
6965 gnat_to_gnu (Identifier (gnat_node)));
6966 break;
6968 case N_Null_Statement:
6969 /* When not optimizing, turn null statements from source into gotos to
6970 the next statement that the middle-end knows how to preserve. */
6971 if (!optimize && Comes_From_Source (gnat_node))
6973 tree stmt, label = create_label_decl (NULL_TREE, gnat_node);
6974 DECL_IGNORED_P (label) = 1;
6975 start_stmt_group ();
6976 stmt = build1 (GOTO_EXPR, void_type_node, label);
6977 set_expr_location_from_node (stmt, gnat_node);
6978 add_stmt (stmt);
6979 stmt = build1 (LABEL_EXPR, void_type_node, label);
6980 set_expr_location_from_node (stmt, gnat_node);
6981 add_stmt (stmt);
6982 gnu_result = end_stmt_group ();
6984 else
6985 gnu_result = alloc_stmt_list ();
6986 break;
6988 case N_Assignment_Statement:
6989 /* Get the LHS and RHS of the statement and convert any reference to an
6990 unconstrained array into a reference to the underlying array. */
6991 gnu_lhs = maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node)));
6993 /* If the type has a size that overflows, convert this into raise of
6994 Storage_Error: execution shouldn't have gotten here anyway. */
6995 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))) == INTEGER_CST
6996 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs))))
6997 gnu_result = build_call_raise (SE_Object_Too_Large, gnat_node,
6998 N_Raise_Storage_Error);
6999 else if (Nkind (Expression (gnat_node)) == N_Function_Call)
7001 bool outer_atomic_access
7002 = outer_atomic_access_required_p (Name (gnat_node));
7003 bool atomic_access
7004 = !outer_atomic_access
7005 && atomic_access_required_p (Name (gnat_node), &sync);
7006 gnu_result
7007 = Call_to_gnu (Expression (gnat_node), &gnu_result_type, gnu_lhs,
7008 outer_atomic_access, atomic_access, sync);
7010 else
7012 const Node_Id gnat_expr = Expression (gnat_node);
7013 const Entity_Id gnat_type
7014 = Underlying_Type (Etype (Name (gnat_node)));
7015 const bool regular_array_type_p
7016 = (Is_Array_Type (gnat_type) && !Is_Bit_Packed_Array (gnat_type));
7017 const bool use_memset_p
7018 = (regular_array_type_p
7019 && Nkind (gnat_expr) == N_Aggregate
7020 && Is_Others_Aggregate (gnat_expr));
7022 /* If we'll use memset, we need to find the inner expression. */
7023 if (use_memset_p)
7025 Node_Id gnat_inner
7026 = Expression (First (Component_Associations (gnat_expr)));
7027 while (Nkind (gnat_inner) == N_Aggregate
7028 && Is_Others_Aggregate (gnat_inner))
7029 gnat_inner
7030 = Expression (First (Component_Associations (gnat_inner)));
7031 gnu_rhs = gnat_to_gnu (gnat_inner);
7033 else
7034 gnu_rhs = maybe_unconstrained_array (gnat_to_gnu (gnat_expr));
7036 /* If range check is needed, emit code to generate it. */
7037 if (Do_Range_Check (gnat_expr))
7038 gnu_rhs = emit_range_check (gnu_rhs, Etype (Name (gnat_node)),
7039 gnat_node);
7041 /* If an outer atomic access is required on the LHS, build the load-
7042 modify-store sequence. */
7043 if (outer_atomic_access_required_p (Name (gnat_node)))
7044 gnu_result = build_load_modify_store (gnu_lhs, gnu_rhs, gnat_node);
7046 /* Or else, if atomic access is required, build the atomic store. */
7047 else if (atomic_access_required_p (Name (gnat_node), &sync))
7048 gnu_result = build_atomic_store (gnu_lhs, gnu_rhs, sync);
7050 /* Or else, use memset when the conditions are met. */
7051 else if (use_memset_p)
7053 tree value
7054 = real_zerop (gnu_rhs)
7055 ? integer_zero_node
7056 : fold_convert (integer_type_node, gnu_rhs);
7057 tree to = gnu_lhs;
7058 tree type = TREE_TYPE (to);
7059 tree size
7060 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), to);
7061 tree to_ptr = build_fold_addr_expr (to);
7062 tree t = builtin_decl_explicit (BUILT_IN_MEMSET);
7063 if (TREE_CODE (value) == INTEGER_CST && !integer_zerop (value))
7065 tree mask
7066 = build_int_cst (integer_type_node,
7067 ((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
7068 value = int_const_binop (BIT_AND_EXPR, value, mask);
7070 gnu_result = build_call_expr (t, 3, to_ptr, value, size);
7073 /* Otherwise build a regular assignment. */
7074 else
7075 gnu_result
7076 = build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_lhs, gnu_rhs);
7078 /* If the assignment type is a regular array and the two sides are
7079 not completely disjoint, play safe and use memmove. But don't do
7080 it for a bit-packed array as it might not be byte-aligned. */
7081 if (TREE_CODE (gnu_result) == MODIFY_EXPR
7082 && regular_array_type_p
7083 && !(Forwards_OK (gnat_node) && Backwards_OK (gnat_node)))
7085 tree to = TREE_OPERAND (gnu_result, 0);
7086 tree from = TREE_OPERAND (gnu_result, 1);
7087 tree type = TREE_TYPE (from);
7088 tree size
7089 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type), from);
7090 tree to_ptr = build_fold_addr_expr (to);
7091 tree from_ptr = build_fold_addr_expr (from);
7092 tree t = builtin_decl_explicit (BUILT_IN_MEMMOVE);
7093 gnu_result = build_call_expr (t, 3, to_ptr, from_ptr, size);
7096 break;
7098 case N_If_Statement:
7100 tree *gnu_else_ptr; /* Point to put next "else if" or "else". */
7102 /* Make the outer COND_EXPR. Avoid non-determinism. */
7103 gnu_result = build3 (COND_EXPR, void_type_node,
7104 gnat_to_gnu (Condition (gnat_node)),
7105 NULL_TREE, NULL_TREE);
7106 COND_EXPR_THEN (gnu_result)
7107 = build_stmt_group (Then_Statements (gnat_node), false);
7108 TREE_SIDE_EFFECTS (gnu_result) = 1;
7109 gnu_else_ptr = &COND_EXPR_ELSE (gnu_result);
7111 /* Now make a COND_EXPR for each of the "else if" parts. Put each
7112 into the previous "else" part and point to where to put any
7113 outer "else". Also avoid non-determinism. */
7114 if (Present (Elsif_Parts (gnat_node)))
7115 for (gnat_temp = First (Elsif_Parts (gnat_node));
7116 Present (gnat_temp); gnat_temp = Next (gnat_temp))
7118 gnu_expr = build3 (COND_EXPR, void_type_node,
7119 gnat_to_gnu (Condition (gnat_temp)),
7120 NULL_TREE, NULL_TREE);
7121 COND_EXPR_THEN (gnu_expr)
7122 = build_stmt_group (Then_Statements (gnat_temp), false);
7123 TREE_SIDE_EFFECTS (gnu_expr) = 1;
7124 set_expr_location_from_node (gnu_expr, gnat_temp);
7125 *gnu_else_ptr = gnu_expr;
7126 gnu_else_ptr = &COND_EXPR_ELSE (gnu_expr);
7129 *gnu_else_ptr = build_stmt_group (Else_Statements (gnat_node), false);
7131 break;
7133 case N_Case_Statement:
7134 gnu_result = Case_Statement_to_gnu (gnat_node);
7135 break;
7137 case N_Loop_Statement:
7138 gnu_result = Loop_Statement_to_gnu (gnat_node);
7139 break;
7141 case N_Block_Statement:
7142 /* The only way to enter the block is to fall through to it. */
7143 if (stmt_group_may_fallthru ())
7145 start_stmt_group ();
7146 gnat_pushlevel ();
7147 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
7148 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
7149 gnat_poplevel ();
7150 gnu_result = end_stmt_group ();
7152 else
7153 gnu_result = alloc_stmt_list ();
7154 break;
7156 case N_Exit_Statement:
7157 gnu_result
7158 = build2 (EXIT_STMT, void_type_node,
7159 (Present (Condition (gnat_node))
7160 ? gnat_to_gnu (Condition (gnat_node)) : NULL_TREE),
7161 (Present (Name (gnat_node))
7162 ? get_gnu_tree (Entity (Name (gnat_node)))
7163 : LOOP_STMT_LABEL (gnu_loop_stack->last ()->stmt)));
7164 break;
7166 case N_Simple_Return_Statement:
7168 tree gnu_ret_obj, gnu_ret_val;
7170 /* If the subprogram is a function, we must return the expression. */
7171 if (Present (Expression (gnat_node)))
7173 tree gnu_subprog_type = TREE_TYPE (current_function_decl);
7175 /* If this function has copy-in/copy-out parameters parameters and
7176 doesn't return by invisible reference, get the real object for
7177 the return. See Subprogram_Body_to_gnu. */
7178 if (TYPE_CI_CO_LIST (gnu_subprog_type)
7179 && !TREE_ADDRESSABLE (gnu_subprog_type))
7180 gnu_ret_obj = gnu_return_var_stack->last ();
7181 else
7182 gnu_ret_obj = DECL_RESULT (current_function_decl);
7184 /* Get the GCC tree for the expression to be returned. */
7185 gnu_ret_val = gnat_to_gnu (Expression (gnat_node));
7187 /* Do not remove the padding from GNU_RET_VAL if the inner type is
7188 self-referential since we want to allocate the fixed size. */
7189 if (TREE_CODE (gnu_ret_val) == COMPONENT_REF
7190 && type_is_padding_self_referential
7191 (TREE_TYPE (TREE_OPERAND (gnu_ret_val, 0))))
7192 gnu_ret_val = TREE_OPERAND (gnu_ret_val, 0);
7194 /* If the function returns by direct reference, return a pointer
7195 to the return value. */
7196 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type)
7197 || By_Ref (gnat_node))
7198 gnu_ret_val = build_unary_op (ADDR_EXPR, NULL_TREE, gnu_ret_val);
7200 /* Otherwise, if it returns an unconstrained array, we have to
7201 allocate a new version of the result and return it. */
7202 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type))
7204 gnu_ret_val = maybe_unconstrained_array (gnu_ret_val);
7206 /* And find out whether this is a candidate for Named Return
7207 Value. If so, record it. */
7208 if (!TYPE_CI_CO_LIST (gnu_subprog_type) && optimize)
7210 tree ret_val = gnu_ret_val;
7212 /* Strip useless conversions around the return value. */
7213 if (gnat_useless_type_conversion (ret_val))
7214 ret_val = TREE_OPERAND (ret_val, 0);
7216 /* Strip unpadding around the return value. */
7217 if (TREE_CODE (ret_val) == COMPONENT_REF
7218 && TYPE_IS_PADDING_P
7219 (TREE_TYPE (TREE_OPERAND (ret_val, 0))))
7220 ret_val = TREE_OPERAND (ret_val, 0);
7222 /* Now apply the test to the return value. */
7223 if (return_value_ok_for_nrv_p (NULL_TREE, ret_val))
7225 if (!f_named_ret_val)
7226 f_named_ret_val = BITMAP_GGC_ALLOC ();
7227 bitmap_set_bit (f_named_ret_val, DECL_UID (ret_val));
7228 if (!f_gnat_ret)
7229 f_gnat_ret = gnat_node;
7233 gnu_ret_val = build_allocator (TREE_TYPE (gnu_ret_val),
7234 gnu_ret_val,
7235 TREE_TYPE (gnu_ret_obj),
7236 Procedure_To_Call (gnat_node),
7237 Storage_Pool (gnat_node),
7238 gnat_node, false);
7241 /* Otherwise, if it returns by invisible reference, dereference
7242 the pointer it is passed using the type of the return value
7243 and build the copy operation manually. This ensures that we
7244 don't copy too much data, for example if the return type is
7245 unconstrained with a maximum size. */
7246 else if (TREE_ADDRESSABLE (gnu_subprog_type))
7248 tree gnu_ret_deref
7249 = build_unary_op (INDIRECT_REF, TREE_TYPE (gnu_ret_val),
7250 gnu_ret_obj);
7251 gnu_result = build2 (INIT_EXPR, void_type_node,
7252 gnu_ret_deref, gnu_ret_val);
7253 add_stmt_with_node (gnu_result, gnat_node);
7254 gnu_ret_val = NULL_TREE;
7258 else
7259 gnu_ret_obj = gnu_ret_val = NULL_TREE;
7261 /* If we have a return label defined, convert this into a branch to
7262 that label. The return proper will be handled elsewhere. */
7263 if (gnu_return_label_stack->last ())
7265 if (gnu_ret_val)
7266 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_ret_obj,
7267 gnu_ret_val));
7269 gnu_result = build1 (GOTO_EXPR, void_type_node,
7270 gnu_return_label_stack->last ());
7272 /* When not optimizing, make sure the return is preserved. */
7273 if (!optimize && Comes_From_Source (gnat_node))
7274 DECL_ARTIFICIAL (gnu_return_label_stack->last ()) = 0;
7277 /* Otherwise, build a regular return. */
7278 else
7279 gnu_result = build_return_expr (gnu_ret_obj, gnu_ret_val);
7281 break;
7283 case N_Goto_Statement:
7284 gnu_expr = gnat_to_gnu (Name (gnat_node));
7285 gnu_result = build1 (GOTO_EXPR, void_type_node, gnu_expr);
7286 TREE_USED (gnu_expr) = 1;
7287 break;
7289 /***************************/
7290 /* Chapter 6: Subprograms */
7291 /***************************/
7293 case N_Subprogram_Declaration:
7294 /* Unless there is a freeze node, declare the entity. We consider
7295 this a definition even though we're not generating code for the
7296 subprogram because we will be making the corresponding GCC node.
7297 When there is a freeze node, it is considered the definition of
7298 the subprogram and we do nothing until after it is encountered.
7299 That's an efficiency issue: the types involved in the profile
7300 are far more likely to be frozen between the declaration and
7301 the freeze node than before the declaration, so we save some
7302 updates of the GCC node by waiting until the freeze node.
7303 The counterpart is that we assume that there is no reference
7304 to the subprogram between the declaration and the freeze node
7305 in the expanded code; otherwise, it will be interpreted as an
7306 external reference and very likely give rise to a link failure. */
7307 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
7308 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node)),
7309 NULL_TREE, true);
7310 gnu_result = alloc_stmt_list ();
7311 break;
7313 case N_Abstract_Subprogram_Declaration:
7314 /* This subprogram doesn't exist for code generation purposes, but we
7315 have to elaborate the types of any parameters and result, unless
7316 they are imported types (nothing to generate in this case).
7318 The parameter list may contain types with freeze nodes, e.g. not null
7319 subtypes, so the subprogram itself may carry a freeze node, in which
7320 case its elaboration must be deferred. */
7322 /* Process the parameter types first. */
7323 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node)))))
7324 for (gnat_temp
7325 = First_Formal_With_Extras
7326 (Defining_Entity (Specification (gnat_node)));
7327 Present (gnat_temp);
7328 gnat_temp = Next_Formal_With_Extras (gnat_temp))
7329 if (Is_Itype (Etype (gnat_temp))
7330 && !From_Limited_With (Etype (gnat_temp)))
7331 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, false);
7333 /* Then the result type, set to Standard_Void_Type for procedures. */
7335 Entity_Id gnat_temp_type
7336 = Etype (Defining_Entity (Specification (gnat_node)));
7338 if (Is_Itype (gnat_temp_type) && !From_Limited_With (gnat_temp_type))
7339 gnat_to_gnu_entity (Etype (gnat_temp_type), NULL_TREE, false);
7342 gnu_result = alloc_stmt_list ();
7343 break;
7345 case N_Defining_Program_Unit_Name:
7346 /* For a child unit identifier go up a level to get the specification.
7347 We get this when we try to find the spec of a child unit package
7348 that is the compilation unit being compiled. */
7349 gnu_result = gnat_to_gnu (Parent (gnat_node));
7350 break;
7352 case N_Subprogram_Body:
7353 Subprogram_Body_to_gnu (gnat_node);
7354 gnu_result = alloc_stmt_list ();
7355 break;
7357 case N_Function_Call:
7358 case N_Procedure_Call_Statement:
7359 gnu_result = Call_to_gnu (gnat_node, &gnu_result_type, NULL_TREE,
7360 false, false, false);
7361 break;
7363 /************************/
7364 /* Chapter 7: Packages */
7365 /************************/
7367 case N_Package_Declaration:
7368 gnu_result = gnat_to_gnu (Specification (gnat_node));
7369 break;
7371 case N_Package_Specification:
7373 start_stmt_group ();
7374 process_decls (Visible_Declarations (gnat_node),
7375 Private_Declarations (gnat_node), Empty, true, true);
7376 gnu_result = end_stmt_group ();
7377 break;
7379 case N_Package_Body:
7381 /* If this is the body of a generic package - do nothing. */
7382 if (Ekind (Corresponding_Spec (gnat_node)) == E_Generic_Package)
7384 gnu_result = alloc_stmt_list ();
7385 break;
7388 start_stmt_group ();
7389 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
7391 if (Present (Handled_Statement_Sequence (gnat_node)))
7392 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node)));
7394 gnu_result = end_stmt_group ();
7395 break;
7397 /********************************/
7398 /* Chapter 8: Visibility Rules */
7399 /********************************/
7401 case N_Use_Package_Clause:
7402 case N_Use_Type_Clause:
7403 /* Nothing to do here - but these may appear in list of declarations. */
7404 gnu_result = alloc_stmt_list ();
7405 break;
7407 /*********************/
7408 /* Chapter 9: Tasks */
7409 /*********************/
7411 case N_Protected_Type_Declaration:
7412 gnu_result = alloc_stmt_list ();
7413 break;
7415 case N_Single_Task_Declaration:
7416 gnat_to_gnu_entity (Defining_Entity (gnat_node), NULL_TREE, true);
7417 gnu_result = alloc_stmt_list ();
7418 break;
7420 /*********************************************************/
7421 /* Chapter 10: Program Structure and Compilation Issues */
7422 /*********************************************************/
7424 case N_Compilation_Unit:
7425 /* This is not called for the main unit on which gigi is invoked. */
7426 Compilation_Unit_to_gnu (gnat_node);
7427 gnu_result = alloc_stmt_list ();
7428 break;
7430 case N_Subunit:
7431 gnu_result = gnat_to_gnu (Proper_Body (gnat_node));
7432 break;
7434 case N_Entry_Body:
7435 case N_Protected_Body:
7436 case N_Task_Body:
7437 /* These nodes should only be present when annotating types. */
7438 gcc_assert (type_annotate_only);
7439 process_decls (Declarations (gnat_node), Empty, Empty, true, true);
7440 gnu_result = alloc_stmt_list ();
7441 break;
7443 case N_Subprogram_Body_Stub:
7444 case N_Package_Body_Stub:
7445 case N_Protected_Body_Stub:
7446 case N_Task_Body_Stub:
7447 /* Simply process whatever unit is being inserted. */
7448 if (Present (Library_Unit (gnat_node)))
7449 gnu_result = gnat_to_gnu (Unit (Library_Unit (gnat_node)));
7450 else
7452 gcc_assert (type_annotate_only);
7453 gnu_result = alloc_stmt_list ();
7455 break;
7457 /***************************/
7458 /* Chapter 11: Exceptions */
7459 /***************************/
7461 case N_Handled_Sequence_Of_Statements:
7462 /* If there is an At_End procedure attached to this node, and the EH
7463 mechanism is front-end, we must have at least a corresponding At_End
7464 handler, unless the No_Exception_Handlers restriction is set. */
7465 gcc_assert (type_annotate_only
7466 || !Front_End_Exceptions ()
7467 || No (At_End_Proc (gnat_node))
7468 || Present (Exception_Handlers (gnat_node))
7469 || No_Exception_Handlers_Set ());
7471 gnu_result = Handled_Sequence_Of_Statements_to_gnu (gnat_node);
7472 break;
7474 case N_Exception_Handler:
7475 if (Exception_Mechanism == Front_End_SJLJ)
7476 gnu_result = Exception_Handler_to_gnu_fe_sjlj (gnat_node);
7477 else if (Back_End_Exceptions ())
7478 gnu_result = Exception_Handler_to_gnu_gcc (gnat_node);
7479 else
7480 gcc_unreachable ();
7481 break;
7483 case N_Raise_Statement:
7484 /* Only for reraise in back-end exceptions mode. */
7485 gcc_assert (No (Name (gnat_node))
7486 && Back_End_Exceptions ());
7488 start_stmt_group ();
7489 gnat_pushlevel ();
7491 /* Clear the current exception pointer so that the occurrence won't be
7492 deallocated. */
7493 gnu_expr = create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE,
7494 ptr_type_node, gnu_incoming_exc_ptr,
7495 false, false, false, false, false,
7496 true, true, NULL, gnat_node);
7498 add_stmt (build_binary_op (MODIFY_EXPR, NULL_TREE, gnu_incoming_exc_ptr,
7499 build_int_cst (ptr_type_node, 0)));
7500 add_stmt (build_call_n_expr (reraise_zcx_decl, 1, gnu_expr));
7501 gnat_poplevel ();
7502 gnu_result = end_stmt_group ();
7503 break;
7505 case N_Push_Constraint_Error_Label:
7506 gnu_constraint_error_label_stack.safe_push (Exception_Label (gnat_node));
7507 break;
7509 case N_Push_Storage_Error_Label:
7510 gnu_storage_error_label_stack.safe_push (Exception_Label (gnat_node));
7511 break;
7513 case N_Push_Program_Error_Label:
7514 gnu_program_error_label_stack.safe_push (Exception_Label (gnat_node));
7515 break;
7517 case N_Pop_Constraint_Error_Label:
7518 gnat_temp = gnu_constraint_error_label_stack.pop ();
7519 if (Present (gnat_temp)
7520 && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)))
7521 Warn_If_No_Local_Raise (gnat_temp);
7522 break;
7524 case N_Pop_Storage_Error_Label:
7525 gnat_temp = gnu_storage_error_label_stack.pop ();
7526 if (Present (gnat_temp)
7527 && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)))
7528 Warn_If_No_Local_Raise (gnat_temp);
7529 break;
7531 case N_Pop_Program_Error_Label:
7532 gnat_temp = gnu_program_error_label_stack.pop ();
7533 if (Present (gnat_temp)
7534 && !TREE_USED (gnat_to_gnu_entity (gnat_temp, NULL_TREE, false)))
7535 Warn_If_No_Local_Raise (gnat_temp);
7536 break;
7538 /******************************/
7539 /* Chapter 12: Generic Units */
7540 /******************************/
7542 case N_Generic_Function_Renaming_Declaration:
7543 case N_Generic_Package_Renaming_Declaration:
7544 case N_Generic_Procedure_Renaming_Declaration:
7545 case N_Generic_Package_Declaration:
7546 case N_Generic_Subprogram_Declaration:
7547 case N_Package_Instantiation:
7548 case N_Procedure_Instantiation:
7549 case N_Function_Instantiation:
7550 /* These nodes can appear on a declaration list but there is nothing to
7551 to be done with them. */
7552 gnu_result = alloc_stmt_list ();
7553 break;
7555 /**************************************************/
7556 /* Chapter 13: Representation Clauses and */
7557 /* Implementation-Dependent Features */
7558 /**************************************************/
7560 case N_Attribute_Definition_Clause:
7561 gnu_result = alloc_stmt_list ();
7563 /* The only one we need to deal with is 'Address since, for the others,
7564 the front-end puts the information elsewhere. */
7565 if (Get_Attribute_Id (Chars (gnat_node)) != Attr_Address)
7566 break;
7568 /* And we only deal with 'Address if the object has a Freeze node. */
7569 gnat_temp = Entity (Name (gnat_node));
7570 if (No (Freeze_Node (gnat_temp)))
7571 break;
7573 /* Get the value to use as the address and save it as the equivalent
7574 for the object. When it is frozen, gnat_to_gnu_entity will do the
7575 right thing. */
7576 save_gnu_tree (gnat_temp, gnat_to_gnu (Expression (gnat_node)), true);
7577 break;
7579 case N_Enumeration_Representation_Clause:
7580 case N_Record_Representation_Clause:
7581 case N_At_Clause:
7582 /* We do nothing with these. SEM puts the information elsewhere. */
7583 gnu_result = alloc_stmt_list ();
7584 break;
7586 case N_Code_Statement:
7587 if (!type_annotate_only)
7589 tree gnu_template = gnat_to_gnu (Asm_Template (gnat_node));
7590 tree gnu_inputs = NULL_TREE, gnu_outputs = NULL_TREE;
7591 tree gnu_clobbers = NULL_TREE, tail;
7592 bool allows_mem, allows_reg, fake;
7593 int ninputs, noutputs, i;
7594 const char **oconstraints;
7595 const char *constraint;
7596 char *clobber;
7598 /* First retrieve the 3 operand lists built by the front-end. */
7599 Setup_Asm_Outputs (gnat_node);
7600 while (Present (gnat_temp = Asm_Output_Variable ()))
7602 tree gnu_value = gnat_to_gnu (gnat_temp);
7603 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
7604 (Asm_Output_Constraint ()));
7606 gnu_outputs = tree_cons (gnu_constr, gnu_value, gnu_outputs);
7607 Next_Asm_Output ();
7610 Setup_Asm_Inputs (gnat_node);
7611 while (Present (gnat_temp = Asm_Input_Value ()))
7613 tree gnu_value = gnat_to_gnu (gnat_temp);
7614 tree gnu_constr = build_tree_list (NULL_TREE, gnat_to_gnu
7615 (Asm_Input_Constraint ()));
7617 gnu_inputs = tree_cons (gnu_constr, gnu_value, gnu_inputs);
7618 Next_Asm_Input ();
7621 Clobber_Setup (gnat_node);
7622 while ((clobber = Clobber_Get_Next ()))
7623 gnu_clobbers
7624 = tree_cons (NULL_TREE,
7625 build_string (strlen (clobber) + 1, clobber),
7626 gnu_clobbers);
7628 /* Then perform some standard checking and processing on the
7629 operands. In particular, mark them addressable if needed. */
7630 gnu_outputs = nreverse (gnu_outputs);
7631 noutputs = list_length (gnu_outputs);
7632 gnu_inputs = nreverse (gnu_inputs);
7633 ninputs = list_length (gnu_inputs);
7634 oconstraints = XALLOCAVEC (const char *, noutputs);
7636 for (i = 0, tail = gnu_outputs; tail; ++i, tail = TREE_CHAIN (tail))
7638 tree output = TREE_VALUE (tail);
7639 constraint
7640 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7641 oconstraints[i] = constraint;
7643 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
7644 &allows_mem, &allows_reg, &fake))
7646 /* If the operand is going to end up in memory,
7647 mark it addressable. Note that we don't test
7648 allows_mem like in the input case below; this
7649 is modelled on the C front-end. */
7650 if (!allows_reg)
7652 output = remove_conversions (output, false);
7653 if (TREE_CODE (output) == CONST_DECL
7654 && DECL_CONST_CORRESPONDING_VAR (output))
7655 output = DECL_CONST_CORRESPONDING_VAR (output);
7656 if (!gnat_mark_addressable (output))
7657 output = error_mark_node;
7660 else
7661 output = error_mark_node;
7663 TREE_VALUE (tail) = output;
7666 for (i = 0, tail = gnu_inputs; tail; ++i, tail = TREE_CHAIN (tail))
7668 tree input = TREE_VALUE (tail);
7669 constraint
7670 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
7672 if (parse_input_constraint (&constraint, i, ninputs, noutputs,
7673 0, oconstraints,
7674 &allows_mem, &allows_reg))
7676 /* If the operand is going to end up in memory,
7677 mark it addressable. */
7678 if (!allows_reg && allows_mem)
7680 input = remove_conversions (input, false);
7681 if (TREE_CODE (input) == CONST_DECL
7682 && DECL_CONST_CORRESPONDING_VAR (input))
7683 input = DECL_CONST_CORRESPONDING_VAR (input);
7684 if (!gnat_mark_addressable (input))
7685 input = error_mark_node;
7688 else
7689 input = error_mark_node;
7691 TREE_VALUE (tail) = input;
7694 gnu_result = build5 (ASM_EXPR, void_type_node,
7695 gnu_template, gnu_outputs,
7696 gnu_inputs, gnu_clobbers, NULL_TREE);
7697 ASM_VOLATILE_P (gnu_result) = Is_Asm_Volatile (gnat_node);
7699 else
7700 gnu_result = alloc_stmt_list ();
7702 break;
7704 /****************/
7705 /* Added Nodes */
7706 /****************/
7708 /* Markers are created by the ABE mechanism to capture information which
7709 is either unavailable of expensive to recompute. Markers do not have
7710 and runtime semantics, and should be ignored. */
7712 case N_Call_Marker:
7713 case N_Variable_Reference_Marker:
7714 gnu_result = alloc_stmt_list ();
7715 break;
7717 case N_Expression_With_Actions:
7718 /* This construct doesn't define a scope so we don't push a binding
7719 level around the statement list, but we wrap it in a SAVE_EXPR to
7720 protect it from unsharing. Elaborate the expression as part of the
7721 same statement group as the actions so that the type declaration
7722 gets inserted there as well. This ensures that the type elaboration
7723 code is issued past the actions computing values on which it might
7724 depend. */
7725 start_stmt_group ();
7726 add_stmt_list (Actions (gnat_node));
7727 gnu_expr = gnat_to_gnu (Expression (gnat_node));
7728 gnu_result = end_stmt_group ();
7730 gnu_result = build1 (SAVE_EXPR, void_type_node, gnu_result);
7731 TREE_SIDE_EFFECTS (gnu_result) = 1;
7733 gnu_result
7734 = build_compound_expr (TREE_TYPE (gnu_expr), gnu_result, gnu_expr);
7735 gnu_result_type = get_unpadded_type (Etype (gnat_node));
7736 break;
7738 case N_Freeze_Entity:
7739 start_stmt_group ();
7740 process_freeze_entity (gnat_node);
7741 process_decls (Actions (gnat_node), Empty, Empty, true, true);
7742 gnu_result = end_stmt_group ();
7743 break;
7745 case N_Freeze_Generic_Entity:
7746 gnu_result = alloc_stmt_list ();
7747 break;
7749 case N_Itype_Reference:
7750 if (!present_gnu_tree (Itype (gnat_node)))
7751 process_type (Itype (gnat_node));
7752 gnu_result = alloc_stmt_list ();
7753 break;
7755 case N_Free_Statement:
7756 if (!type_annotate_only)
7758 tree gnu_ptr
7759 = gnat_to_gnu (adjust_for_implicit_deref (Expression (gnat_node)));
7760 tree gnu_ptr_type = TREE_TYPE (gnu_ptr);
7761 tree gnu_obj_type, gnu_actual_obj_type;
7763 /* If this is a thin pointer, we must first dereference it to create
7764 a fat pointer, then go back below to a thin pointer. The reason
7765 for this is that we need to have a fat pointer someplace in order
7766 to properly compute the size. */
7767 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7768 gnu_ptr = build_unary_op (ADDR_EXPR, NULL_TREE,
7769 build_unary_op (INDIRECT_REF, NULL_TREE,
7770 gnu_ptr));
7772 /* If this is a fat pointer, the object must have been allocated with
7773 the template in front of the array. So pass the template address,
7774 and get the total size; do it by converting to a thin pointer. */
7775 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr)))
7776 gnu_ptr
7777 = convert (build_pointer_type
7778 (TYPE_OBJECT_RECORD_TYPE
7779 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr)))),
7780 gnu_ptr);
7782 gnu_obj_type = TREE_TYPE (TREE_TYPE (gnu_ptr));
7784 /* If this is a thin pointer, the object must have been allocated with
7785 the template in front of the array. So pass the template address,
7786 and get the total size. */
7787 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr)))
7788 gnu_ptr
7789 = build_binary_op (POINTER_PLUS_EXPR, TREE_TYPE (gnu_ptr),
7790 gnu_ptr,
7791 fold_build1 (NEGATE_EXPR, sizetype,
7792 byte_position
7793 (DECL_CHAIN
7794 TYPE_FIELDS ((gnu_obj_type)))));
7796 /* If we have a special dynamic constrained subtype on the node, use
7797 it to compute the size; otherwise, use the designated subtype. */
7798 if (Present (Actual_Designated_Subtype (gnat_node)))
7800 gnu_actual_obj_type
7801 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node));
7803 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type))
7804 gnu_actual_obj_type
7805 = build_unc_object_type_from_ptr (gnu_ptr_type,
7806 gnu_actual_obj_type,
7807 get_identifier ("DEALLOC"),
7808 false);
7810 else
7811 gnu_actual_obj_type = gnu_obj_type;
7813 tree gnu_size = TYPE_SIZE_UNIT (gnu_actual_obj_type);
7814 gnu_size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size, gnu_ptr);
7816 gnu_result
7817 = build_call_alloc_dealloc (gnu_ptr, gnu_size, gnu_obj_type,
7818 Procedure_To_Call (gnat_node),
7819 Storage_Pool (gnat_node),
7820 gnat_node);
7822 break;
7824 case N_Raise_Constraint_Error:
7825 case N_Raise_Program_Error:
7826 case N_Raise_Storage_Error:
7827 if (type_annotate_only)
7828 gnu_result = alloc_stmt_list ();
7829 else
7830 gnu_result = Raise_Error_to_gnu (gnat_node, &gnu_result_type);
7831 break;
7833 case N_Validate_Unchecked_Conversion:
7834 /* The only validation we currently do on an unchecked conversion is
7835 that of aliasing assumptions. */
7836 if (flag_strict_aliasing)
7837 gnat_validate_uc_list.safe_push (gnat_node);
7838 gnu_result = alloc_stmt_list ();
7839 break;
7841 case N_Function_Specification:
7842 case N_Procedure_Specification:
7843 case N_Op_Concat:
7844 case N_Component_Association:
7845 /* These nodes should only be present when annotating types. */
7846 gcc_assert (type_annotate_only);
7847 gnu_result = alloc_stmt_list ();
7848 break;
7850 default:
7851 /* Other nodes are not supposed to reach here. */
7852 gcc_unreachable ();
7855 /* If we pushed the processing of the elaboration routine, pop it back. */
7856 if (went_into_elab_proc)
7857 current_function_decl = NULL_TREE;
7859 /* When not optimizing, turn boolean rvalues B into B != false tests
7860 so that we can put the location information of the reference to B on
7861 the inequality operator for better debug info. */
7862 if (!optimize
7863 && TREE_CODE (gnu_result) != INTEGER_CST
7864 && TREE_CODE (gnu_result) != TYPE_DECL
7865 && (kind == N_Identifier
7866 || kind == N_Expanded_Name
7867 || kind == N_Explicit_Dereference
7868 || kind == N_Indexed_Component
7869 || kind == N_Selected_Component)
7870 && TREE_CODE (get_base_type (gnu_result_type)) == BOOLEAN_TYPE
7871 && !lvalue_required_p (gnat_node, gnu_result_type, false, false, false))
7873 gnu_result
7874 = build_binary_op (NE_EXPR, gnu_result_type,
7875 convert (gnu_result_type, gnu_result),
7876 convert (gnu_result_type, boolean_false_node));
7877 if (TREE_CODE (gnu_result) != INTEGER_CST)
7878 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7881 /* Set the location information on the result if it's not a simple name.
7882 Note that we may have no result if we tried to build a CALL_EXPR node
7883 to a procedure with no side-effects and optimization is enabled. */
7884 else if (kind != N_Identifier && gnu_result && EXPR_P (gnu_result))
7885 set_gnu_expr_location_from_node (gnu_result, gnat_node);
7887 /* If we're supposed to return something of void_type, it means we have
7888 something we're elaborating for effect, so just return. */
7889 if (TREE_CODE (gnu_result_type) == VOID_TYPE)
7890 return gnu_result;
7892 /* If the result is a constant that overflowed, raise Constraint_Error. */
7893 if (TREE_CODE (gnu_result) == INTEGER_CST && TREE_OVERFLOW (gnu_result))
7895 post_error ("?`Constraint_Error` will be raised at run time", gnat_node);
7896 gnu_result
7897 = build1 (NULL_EXPR, gnu_result_type,
7898 build_call_raise (CE_Overflow_Check_Failed, gnat_node,
7899 N_Raise_Constraint_Error));
7902 /* If the result has side-effects and is of an unconstrained type, protect
7903 the expression in case it will be referenced multiple times, i.e. for
7904 its value and to compute the size of an object. But do it neither for
7905 an object nor a renaming declaration, nor a return statement of a call
7906 to a function that returns an unconstrained record type with default
7907 discriminant, because there is no size to be computed in these cases
7908 and this will create a useless temporary. We must do this before any
7909 conversions. */
7910 if (TREE_SIDE_EFFECTS (gnu_result)
7911 && (TREE_CODE (gnu_result_type) == UNCONSTRAINED_ARRAY_TYPE
7912 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type)))
7913 && !(TREE_CODE (gnu_result) == CALL_EXPR
7914 && type_is_padding_self_referential (TREE_TYPE (gnu_result))
7915 && (Nkind (Parent (gnat_node)) == N_Object_Declaration
7916 || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration
7917 || Nkind (Parent (gnat_node)) == N_Simple_Return_Statement)))
7918 gnu_result = gnat_protect_expr (gnu_result);
7920 /* Now convert the result to the result type, unless we are in one of the
7921 following cases:
7923 1. If this is the LHS of an assignment or an actual parameter of a
7924 call, return the result almost unmodified since the RHS will have
7925 to be converted to our type in that case, unless the result type
7926 has a simpler size. Likewise if there is just a no-op unchecked
7927 conversion in-between. Similarly, don't convert integral types
7928 that are the operands of an unchecked conversion since we need
7929 to ignore those conversions (for 'Valid).
7931 2. If we have a label (which doesn't have any well-defined type), a
7932 field or an error, return the result almost unmodified. Similarly,
7933 if the two types are record types with the same name, don't convert.
7934 This will be the case when we are converting from a packable version
7935 of a type to its original type and we need those conversions to be
7936 NOPs in order for assignments into these types to work properly.
7938 3. If the type is void or if we have no result, return error_mark_node
7939 to show we have no result.
7941 4. If this is a call to a function that returns with variable size and
7942 the call is used as the expression in either an object or a renaming
7943 declaration, return the result unmodified because we want to use the
7944 return slot optimization in this case.
7946 5. Finally, if the type of the result is already correct. */
7948 if (Present (Parent (gnat_node))
7949 && (lhs_or_actual_p (gnat_node)
7950 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7951 && unchecked_conversion_nop (Parent (gnat_node)))
7952 || (Nkind (Parent (gnat_node)) == N_Unchecked_Type_Conversion
7953 && !AGGREGATE_TYPE_P (gnu_result_type)
7954 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result))))
7955 && !(TYPE_SIZE (gnu_result_type)
7956 && TYPE_SIZE (TREE_TYPE (gnu_result))
7957 && (AGGREGATE_TYPE_P (gnu_result_type)
7958 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result)))
7959 && ((TREE_CODE (TYPE_SIZE (gnu_result_type)) == INTEGER_CST
7960 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result)))
7961 != INTEGER_CST))
7962 || (TREE_CODE (TYPE_SIZE (gnu_result_type)) != INTEGER_CST
7963 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type))
7964 && (CONTAINS_PLACEHOLDER_P
7965 (TYPE_SIZE (TREE_TYPE (gnu_result))))))
7966 && !(TREE_CODE (gnu_result_type) == RECORD_TYPE
7967 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type))))
7969 /* Remove padding only if the inner object is of self-referential
7970 size: in that case it must be an object of unconstrained type
7971 with a default discriminant and we want to avoid copying too
7972 much data. */
7973 if (type_is_padding_self_referential (TREE_TYPE (gnu_result)))
7974 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7975 gnu_result);
7978 else if (TREE_CODE (gnu_result) == LABEL_DECL
7979 || TREE_CODE (gnu_result) == FIELD_DECL
7980 || TREE_CODE (gnu_result) == ERROR_MARK
7981 || (TYPE_NAME (gnu_result_type)
7982 == TYPE_NAME (TREE_TYPE (gnu_result))
7983 && TREE_CODE (gnu_result_type) == RECORD_TYPE
7984 && TREE_CODE (TREE_TYPE (gnu_result)) == RECORD_TYPE))
7986 /* Remove any padding. */
7987 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result)))
7988 gnu_result = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result))),
7989 gnu_result);
7992 else if (gnu_result == error_mark_node || gnu_result_type == void_type_node)
7993 gnu_result = error_mark_node;
7995 else if (Present (Parent (gnat_node))
7996 && (Nkind (Parent (gnat_node)) == N_Object_Declaration
7997 || Nkind (Parent (gnat_node)) == N_Object_Renaming_Declaration)
7998 && TREE_CODE (gnu_result) == CALL_EXPR
7999 && return_type_with_variable_size_p (TREE_TYPE (gnu_result)))
8002 else if (TREE_TYPE (gnu_result) != gnu_result_type)
8003 gnu_result = convert (gnu_result_type, gnu_result);
8005 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
8006 while ((TREE_CODE (gnu_result) == NOP_EXPR
8007 || TREE_CODE (gnu_result) == NON_LVALUE_EXPR)
8008 && TREE_TYPE (TREE_OPERAND (gnu_result, 0)) == TREE_TYPE (gnu_result))
8009 gnu_result = TREE_OPERAND (gnu_result, 0);
8011 return gnu_result;
8014 /* Similar to gnat_to_gnu, but discard any object that might be created in
8015 the course of the translation of GNAT_NODE, which must be an "external"
8016 expression in the sense that it will be elaborated elsewhere. */
8018 tree
8019 gnat_to_gnu_external (Node_Id gnat_node)
8021 const int save_force_global = force_global;
8022 bool went_into_elab_proc = false;
8024 /* Force the local context and create a fake scope that we zap
8025 at the end so declarations will not be stuck either in the
8026 global varpool or in the current scope. */
8027 if (!current_function_decl)
8029 current_function_decl = get_elaboration_procedure ();
8030 went_into_elab_proc = true;
8032 force_global = 0;
8033 gnat_pushlevel ();
8035 tree gnu_result = gnat_to_gnu (gnat_node);
8037 gnat_zaplevel ();
8038 force_global = save_force_global;
8039 if (went_into_elab_proc)
8040 current_function_decl = NULL_TREE;
8042 /* Do not import locations from external units. */
8043 if (gnu_result && EXPR_P (gnu_result))
8044 SET_EXPR_LOCATION (gnu_result, UNKNOWN_LOCATION);
8046 return gnu_result;
8049 /* Return true if the statement list STMT_LIST is empty. */
8051 static bool
8052 empty_stmt_list_p (tree stmt_list)
8054 tree_stmt_iterator tsi;
8056 for (tsi = tsi_start (stmt_list); !tsi_end_p (tsi); tsi_next (&tsi))
8058 tree stmt = tsi_stmt (tsi);
8060 /* Anything else than an empty STMT_STMT counts as something. */
8061 if (TREE_CODE (stmt) != STMT_STMT || STMT_STMT_STMT (stmt))
8062 return false;
8065 return true;
8068 /* Record the current code position in GNAT_NODE. */
8070 static void
8071 record_code_position (Node_Id gnat_node)
8073 tree stmt_stmt = build1 (STMT_STMT, void_type_node, NULL_TREE);
8075 add_stmt_with_node (stmt_stmt, gnat_node);
8076 save_gnu_tree (gnat_node, stmt_stmt, true);
8079 /* Insert the code for GNAT_NODE at the position saved for that node. */
8081 static void
8082 insert_code_for (Node_Id gnat_node)
8084 tree code = gnat_to_gnu (gnat_node);
8086 /* It's too late to remove the STMT_STMT itself at this point. */
8087 if (!empty_stmt_list_p (code))
8088 STMT_STMT_STMT (get_gnu_tree (gnat_node)) = code;
8090 save_gnu_tree (gnat_node, NULL_TREE, true);
8093 /* Start a new statement group chained to the previous group. */
8095 void
8096 start_stmt_group (void)
8098 struct stmt_group *group = stmt_group_free_list;
8100 /* First see if we can get one from the free list. */
8101 if (group)
8102 stmt_group_free_list = group->previous;
8103 else
8104 group = ggc_alloc<stmt_group> ();
8106 group->previous = current_stmt_group;
8107 group->stmt_list = group->block = group->cleanups = NULL_TREE;
8108 current_stmt_group = group;
8111 /* Add GNU_STMT to the current statement group. If it is an expression with
8112 no effects, it is ignored. */
8114 void
8115 add_stmt (tree gnu_stmt)
8117 append_to_statement_list (gnu_stmt, &current_stmt_group->stmt_list);
8120 /* Similar, but the statement is always added, regardless of side-effects. */
8122 void
8123 add_stmt_force (tree gnu_stmt)
8125 append_to_statement_list_force (gnu_stmt, &current_stmt_group->stmt_list);
8128 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
8130 void
8131 add_stmt_with_node (tree gnu_stmt, Node_Id gnat_node)
8133 /* Do not emit a location for renamings that come from generic instantiation,
8134 they are likely to disturb debugging. */
8135 if (Present (gnat_node) && !renaming_from_instantiation_p (gnat_node))
8136 set_expr_location_from_node (gnu_stmt, gnat_node);
8137 add_stmt (gnu_stmt);
8140 /* Similar, but the statement is always added, regardless of side-effects. */
8142 void
8143 add_stmt_with_node_force (tree gnu_stmt, Node_Id gnat_node)
8145 if (Present (gnat_node))
8146 set_expr_location_from_node (gnu_stmt, gnat_node);
8147 add_stmt_force (gnu_stmt);
8150 /* Add a declaration statement for GNU_DECL to the current statement group.
8151 Get SLOC from Entity_Id. */
8153 void
8154 add_decl_expr (tree gnu_decl, Entity_Id gnat_entity)
8156 tree type = TREE_TYPE (gnu_decl);
8157 tree gnu_stmt, gnu_init;
8159 /* If this is a variable that Gigi is to ignore, we may have been given
8160 an ERROR_MARK. So test for it. We also might have been given a
8161 reference for a renaming. So only do something for a decl. Also
8162 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
8163 if (!DECL_P (gnu_decl)
8164 || (TREE_CODE (gnu_decl) == TYPE_DECL
8165 && TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE))
8166 return;
8168 gnu_stmt = build1 (DECL_EXPR, void_type_node, gnu_decl);
8170 /* If we are external or global, we don't want to output the DECL_EXPR for
8171 this DECL node since we already have evaluated the expressions in the
8172 sizes and positions as globals and doing it again would be wrong. */
8173 if (DECL_EXTERNAL (gnu_decl) || global_bindings_p ())
8175 /* Mark everything as used to prevent node sharing with subprograms.
8176 Note that walk_tree knows how to deal with TYPE_DECL, but neither
8177 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
8178 MARK_VISITED (gnu_stmt);
8179 if (TREE_CODE (gnu_decl) == VAR_DECL
8180 || TREE_CODE (gnu_decl) == CONST_DECL)
8182 MARK_VISITED (DECL_SIZE (gnu_decl));
8183 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl));
8184 MARK_VISITED (DECL_INITIAL (gnu_decl));
8186 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
8187 else if (TREE_CODE (gnu_decl) == TYPE_DECL
8188 && RECORD_OR_UNION_TYPE_P (type)
8189 && !TYPE_FAT_POINTER_P (type))
8190 MARK_VISITED (TYPE_ADA_SIZE (type));
8192 else
8193 add_stmt_with_node (gnu_stmt, gnat_entity);
8195 /* If this is a variable and an initializer is attached to it, it must be
8196 valid for the context. Similar to init_const in create_var_decl. */
8197 if (TREE_CODE (gnu_decl) == VAR_DECL
8198 && (gnu_init = DECL_INITIAL (gnu_decl))
8199 && (!gnat_types_compatible_p (type, TREE_TYPE (gnu_init))
8200 || (TREE_STATIC (gnu_decl)
8201 && !initializer_constant_valid_p (gnu_init,
8202 TREE_TYPE (gnu_init)))))
8204 DECL_INITIAL (gnu_decl) = NULL_TREE;
8205 if (TREE_READONLY (gnu_decl))
8207 TREE_READONLY (gnu_decl) = 0;
8208 DECL_READONLY_ONCE_ELAB (gnu_decl) = 1;
8211 /* If GNU_DECL has a padded type, convert it to the unpadded
8212 type so the assignment is done properly. */
8213 if (TYPE_IS_PADDING_P (type))
8214 gnu_decl = convert (TREE_TYPE (TYPE_FIELDS (type)), gnu_decl);
8216 gnu_stmt = build_binary_op (INIT_EXPR, NULL_TREE, gnu_decl, gnu_init);
8217 add_stmt_with_node (gnu_stmt, gnat_entity);
8221 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
8223 static tree
8224 mark_visited_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
8226 tree t = *tp;
8228 if (TREE_VISITED (t))
8229 *walk_subtrees = 0;
8231 /* Don't mark a dummy type as visited because we want to mark its sizes
8232 and fields once it's filled in. */
8233 else if (!TYPE_IS_DUMMY_P (t))
8234 TREE_VISITED (t) = 1;
8236 if (TYPE_P (t))
8237 TYPE_SIZES_GIMPLIFIED (t) = 1;
8239 return NULL_TREE;
8242 /* Mark nodes rooted at T with TREE_VISITED and types as having their
8243 sized gimplified. We use this to indicate all variable sizes and
8244 positions in global types may not be shared by any subprogram. */
8246 void
8247 mark_visited (tree t)
8249 walk_tree (&t, mark_visited_r, NULL, NULL);
8252 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
8253 set its location to that of GNAT_NODE if present, but with column info
8254 cleared so that conditional branches generated as part of the cleanup
8255 code do not interfere with coverage analysis tools. */
8257 static void
8258 add_cleanup (tree gnu_cleanup, Node_Id gnat_node)
8260 if (Present (gnat_node))
8261 set_expr_location_from_node (gnu_cleanup, gnat_node, true);
8262 append_to_statement_list (gnu_cleanup, &current_stmt_group->cleanups);
8265 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
8267 void
8268 set_block_for_group (tree gnu_block)
8270 gcc_assert (!current_stmt_group->block);
8271 current_stmt_group->block = gnu_block;
8274 /* Return code corresponding to the current code group. It is normally
8275 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
8276 BLOCK or cleanups were set. */
8278 tree
8279 end_stmt_group (void)
8281 struct stmt_group *group = current_stmt_group;
8282 tree gnu_retval = group->stmt_list;
8284 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
8285 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
8286 make a BIND_EXPR. Note that we nest in that because the cleanup may
8287 reference variables in the block. */
8288 if (!gnu_retval)
8289 gnu_retval = alloc_stmt_list ();
8291 if (group->cleanups)
8292 gnu_retval = build2 (TRY_FINALLY_EXPR, void_type_node, gnu_retval,
8293 group->cleanups);
8295 if (current_stmt_group->block)
8296 gnu_retval = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (group->block),
8297 gnu_retval, group->block);
8299 /* Remove this group from the stack and add it to the free list. */
8300 current_stmt_group = group->previous;
8301 group->previous = stmt_group_free_list;
8302 stmt_group_free_list = group;
8304 return gnu_retval;
8307 /* Return whether the current statement group may fall through. */
8309 static inline bool
8310 stmt_group_may_fallthru (void)
8312 if (current_stmt_group->stmt_list)
8313 return block_may_fallthru (current_stmt_group->stmt_list);
8314 else
8315 return true;
8318 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
8319 statements.*/
8321 static void
8322 add_stmt_list (List_Id gnat_list)
8324 Node_Id gnat_node;
8326 if (Present (gnat_list))
8327 for (gnat_node = First (gnat_list); Present (gnat_node);
8328 gnat_node = Next (gnat_node))
8329 add_stmt (gnat_to_gnu (gnat_node));
8332 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
8333 If BINDING_P is true, push and pop a binding level around the list. */
8335 static tree
8336 build_stmt_group (List_Id gnat_list, bool binding_p)
8338 start_stmt_group ();
8340 if (binding_p)
8341 gnat_pushlevel ();
8343 add_stmt_list (gnat_list);
8345 if (binding_p)
8346 gnat_poplevel ();
8348 return end_stmt_group ();
8351 /* Generate GIMPLE in place for the expression at *EXPR_P. */
8354 gnat_gimplify_expr (tree *expr_p, gimple_seq *pre_p,
8355 gimple_seq *post_p ATTRIBUTE_UNUSED)
8357 tree expr = *expr_p;
8358 tree type = TREE_TYPE (expr);
8359 tree op;
8361 if (IS_ADA_STMT (expr))
8362 return gnat_gimplify_stmt (expr_p);
8364 switch (TREE_CODE (expr))
8366 case NULL_EXPR:
8367 /* If this is an aggregate type, build a null pointer of the appropriate
8368 type and dereference it. */
8369 if (AGGREGATE_TYPE_P (type)
8370 || TREE_CODE (type) == UNCONSTRAINED_ARRAY_TYPE)
8371 *expr_p = build_unary_op (INDIRECT_REF, NULL_TREE,
8372 convert (build_pointer_type (type),
8373 integer_zero_node));
8374 /* Otherwise, just make a VAR_DECL. */
8375 else
8377 *expr_p = create_tmp_var (type, NULL);
8378 TREE_NO_WARNING (*expr_p) = 1;
8381 gimplify_and_add (TREE_OPERAND (expr, 0), pre_p);
8382 return GS_OK;
8384 case UNCONSTRAINED_ARRAY_REF:
8385 /* We should only do this if we are just elaborating for side-effects,
8386 but we can't know that yet. */
8387 *expr_p = TREE_OPERAND (*expr_p, 0);
8388 return GS_OK;
8390 case ADDR_EXPR:
8391 op = TREE_OPERAND (expr, 0);
8393 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
8394 is put into static memory. We know that it's going to be read-only
8395 given the semantics we have and it must be in static memory when the
8396 reference is in an elaboration procedure. */
8397 if (TREE_CODE (op) == CONSTRUCTOR && TREE_CONSTANT (op))
8399 tree addr = build_fold_addr_expr (tree_output_constant_def (op));
8400 *expr_p = fold_convert (type, addr);
8401 return GS_ALL_DONE;
8404 /* Replace atomic loads with their first argument. That's necessary
8405 because the gimplifier would create a temporary otherwise. */
8406 if (TREE_SIDE_EFFECTS (op))
8407 while (handled_component_p (op) || CONVERT_EXPR_P (op))
8409 tree inner = TREE_OPERAND (op, 0);
8410 if (TREE_CODE (inner) == CALL_EXPR && call_is_atomic_load (inner))
8412 tree t = CALL_EXPR_ARG (inner, 0);
8413 if (TREE_CODE (t) == NOP_EXPR)
8414 t = TREE_OPERAND (t, 0);
8415 if (TREE_CODE (t) == ADDR_EXPR)
8416 TREE_OPERAND (op, 0) = TREE_OPERAND (t, 0);
8417 else
8418 TREE_OPERAND (op, 0) = build_fold_indirect_ref (t);
8420 else
8421 op = inner;
8424 return GS_UNHANDLED;
8426 case VIEW_CONVERT_EXPR:
8427 op = TREE_OPERAND (expr, 0);
8429 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
8430 type to a scalar one, explicitly create the local temporary. That's
8431 required if the type is passed by reference. */
8432 if ((TREE_CODE (op) == CONSTRUCTOR || TREE_CODE (op) == CALL_EXPR)
8433 && AGGREGATE_TYPE_P (TREE_TYPE (op))
8434 && !AGGREGATE_TYPE_P (type))
8436 tree mod, new_var = create_tmp_var_raw (TREE_TYPE (op), "C");
8437 gimple_add_tmp_var (new_var);
8439 mod = build2 (INIT_EXPR, TREE_TYPE (new_var), new_var, op);
8440 gimplify_and_add (mod, pre_p);
8442 TREE_OPERAND (expr, 0) = new_var;
8443 return GS_OK;
8446 return GS_UNHANDLED;
8448 case DECL_EXPR:
8449 op = DECL_EXPR_DECL (expr);
8451 /* The expressions for the RM bounds must be gimplified to ensure that
8452 they are properly elaborated. See gimplify_decl_expr. */
8453 if ((TREE_CODE (op) == TYPE_DECL || TREE_CODE (op) == VAR_DECL)
8454 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op)))
8455 switch (TREE_CODE (TREE_TYPE (op)))
8457 case INTEGER_TYPE:
8458 case ENUMERAL_TYPE:
8459 case BOOLEAN_TYPE:
8460 case REAL_TYPE:
8462 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (op)), t, val;
8464 val = TYPE_RM_MIN_VALUE (type);
8465 if (val)
8467 gimplify_one_sizepos (&val, pre_p);
8468 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
8469 SET_TYPE_RM_MIN_VALUE (t, val);
8472 val = TYPE_RM_MAX_VALUE (type);
8473 if (val)
8475 gimplify_one_sizepos (&val, pre_p);
8476 for (t = type; t; t = TYPE_NEXT_VARIANT (t))
8477 SET_TYPE_RM_MAX_VALUE (t, val);
8481 break;
8483 default:
8484 break;
8487 /* ... fall through ... */
8489 default:
8490 return GS_UNHANDLED;
8494 /* Generate GIMPLE in place for the statement at *STMT_P. */
8496 static enum gimplify_status
8497 gnat_gimplify_stmt (tree *stmt_p)
8499 tree stmt = *stmt_p;
8501 switch (TREE_CODE (stmt))
8503 case STMT_STMT:
8504 *stmt_p = STMT_STMT_STMT (stmt);
8505 return GS_OK;
8507 case LOOP_STMT:
8509 tree gnu_start_label = create_artificial_label (input_location);
8510 tree gnu_cond = LOOP_STMT_COND (stmt);
8511 tree gnu_update = LOOP_STMT_UPDATE (stmt);
8512 tree gnu_end_label = LOOP_STMT_LABEL (stmt);
8514 /* Build the condition expression from the test, if any. */
8515 if (gnu_cond)
8517 /* Deal with the optimization hints. */
8518 if (LOOP_STMT_IVDEP (stmt))
8519 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8520 build_int_cst (integer_type_node,
8521 annot_expr_ivdep_kind),
8522 integer_zero_node);
8523 if (LOOP_STMT_NO_UNROLL (stmt))
8524 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8525 build_int_cst (integer_type_node,
8526 annot_expr_unroll_kind),
8527 integer_one_node);
8528 if (LOOP_STMT_UNROLL (stmt))
8529 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8530 build_int_cst (integer_type_node,
8531 annot_expr_unroll_kind),
8532 build_int_cst (NULL_TREE, USHRT_MAX));
8533 if (LOOP_STMT_NO_VECTOR (stmt))
8534 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8535 build_int_cst (integer_type_node,
8536 annot_expr_no_vector_kind),
8537 integer_zero_node);
8538 if (LOOP_STMT_VECTOR (stmt))
8539 gnu_cond = build3 (ANNOTATE_EXPR, TREE_TYPE (gnu_cond), gnu_cond,
8540 build_int_cst (integer_type_node,
8541 annot_expr_vector_kind),
8542 integer_zero_node);
8544 gnu_cond
8545 = build3 (COND_EXPR, void_type_node, gnu_cond, NULL_TREE,
8546 build1 (GOTO_EXPR, void_type_node, gnu_end_label));
8549 /* Set to emit the statements of the loop. */
8550 *stmt_p = NULL_TREE;
8552 /* We first emit the start label and then a conditional jump to the
8553 end label if there's a top condition, then the update if it's at
8554 the top, then the body of the loop, then a conditional jump to
8555 the end label if there's a bottom condition, then the update if
8556 it's at the bottom, and finally a jump to the start label and the
8557 definition of the end label. */
8558 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
8559 gnu_start_label),
8560 stmt_p);
8562 if (gnu_cond && !LOOP_STMT_BOTTOM_COND_P (stmt))
8563 append_to_statement_list (gnu_cond, stmt_p);
8565 if (gnu_update && LOOP_STMT_TOP_UPDATE_P (stmt))
8566 append_to_statement_list (gnu_update, stmt_p);
8568 append_to_statement_list (LOOP_STMT_BODY (stmt), stmt_p);
8570 if (gnu_cond && LOOP_STMT_BOTTOM_COND_P (stmt))
8571 append_to_statement_list (gnu_cond, stmt_p);
8573 if (gnu_update && !LOOP_STMT_TOP_UPDATE_P (stmt))
8574 append_to_statement_list (gnu_update, stmt_p);
8576 tree t = build1 (GOTO_EXPR, void_type_node, gnu_start_label);
8577 SET_EXPR_LOCATION (t, DECL_SOURCE_LOCATION (gnu_end_label));
8578 append_to_statement_list (t, stmt_p);
8580 append_to_statement_list (build1 (LABEL_EXPR, void_type_node,
8581 gnu_end_label),
8582 stmt_p);
8583 return GS_OK;
8586 case EXIT_STMT:
8587 /* Build a statement to jump to the corresponding end label, then
8588 see if it needs to be conditional. */
8589 *stmt_p = build1 (GOTO_EXPR, void_type_node, EXIT_STMT_LABEL (stmt));
8590 if (EXIT_STMT_COND (stmt))
8591 *stmt_p = build3 (COND_EXPR, void_type_node,
8592 EXIT_STMT_COND (stmt), *stmt_p, alloc_stmt_list ());
8593 return GS_OK;
8595 default:
8596 gcc_unreachable ();
8600 /* Force a reference to each of the entities in GNAT_PACKAGE recursively.
8602 This routine is exclusively called in type_annotate mode, to compute DDA
8603 information for types in withed units, for ASIS use. */
8605 static void
8606 elaborate_all_entities_for_package (Entity_Id gnat_package)
8608 Entity_Id gnat_entity;
8610 for (gnat_entity = First_Entity (gnat_package);
8611 Present (gnat_entity);
8612 gnat_entity = Next_Entity (gnat_entity))
8614 const Entity_Kind kind = Ekind (gnat_entity);
8616 /* We are interested only in entities visible from the main unit. */
8617 if (!Is_Public (gnat_entity))
8618 continue;
8620 /* Skip stuff internal to the compiler. */
8621 if (Convention (gnat_entity) == Convention_Intrinsic)
8622 continue;
8623 if (kind == E_Operator)
8624 continue;
8625 if (IN (kind, Subprogram_Kind) && Is_Intrinsic_Subprogram (gnat_entity))
8626 continue;
8627 if (Is_Itype (gnat_entity))
8628 continue;
8630 /* Skip named numbers. */
8631 if (IN (kind, Named_Kind))
8632 continue;
8634 /* Skip generic declarations. */
8635 if (IN (kind, Generic_Unit_Kind))
8636 continue;
8638 /* Skip formal objects. */
8639 if (IN (kind, Formal_Object_Kind))
8640 continue;
8642 /* Skip package bodies. */
8643 if (kind == E_Package_Body)
8644 continue;
8646 /* Skip limited views that point back to the main unit. */
8647 if (IN (kind, Incomplete_Kind)
8648 && From_Limited_With (gnat_entity)
8649 && In_Extended_Main_Code_Unit (Non_Limited_View (gnat_entity)))
8650 continue;
8652 /* Skip types that aren't frozen. */
8653 if (IN (kind, Type_Kind) && !Is_Frozen (gnat_entity))
8654 continue;
8656 /* Recurse on real packages that aren't in the main unit. */
8657 if (kind == E_Package)
8659 if (No (Renamed_Entity (gnat_entity))
8660 && !In_Extended_Main_Code_Unit (gnat_entity))
8661 elaborate_all_entities_for_package (gnat_entity);
8663 else
8664 gnat_to_gnu_entity (gnat_entity, NULL_TREE, false);
8668 /* Force a reference to each of the entities in packages withed by GNAT_NODE.
8669 Operate recursively but check that we aren't elaborating something more
8670 than once.
8672 This routine is exclusively called in type_annotate mode, to compute DDA
8673 information for types in withed units, for ASIS use. */
8675 static void
8676 elaborate_all_entities (Node_Id gnat_node)
8678 Entity_Id gnat_with_clause;
8680 /* Process each unit only once. As we trace the context of all relevant
8681 units transitively, including generic bodies, we may encounter the
8682 same generic unit repeatedly. */
8683 if (!present_gnu_tree (gnat_node))
8684 save_gnu_tree (gnat_node, integer_zero_node, true);
8686 /* Save entities in all context units. A body may have an implicit_with
8687 on its own spec, if the context includes a child unit, so don't save
8688 the spec twice. */
8689 for (gnat_with_clause = First (Context_Items (gnat_node));
8690 Present (gnat_with_clause);
8691 gnat_with_clause = Next (gnat_with_clause))
8692 if (Nkind (gnat_with_clause) == N_With_Clause
8693 && !present_gnu_tree (Library_Unit (gnat_with_clause))
8694 && Library_Unit (gnat_with_clause) != Library_Unit (Cunit (Main_Unit)))
8696 Node_Id gnat_unit = Library_Unit (gnat_with_clause);
8697 Entity_Id gnat_entity = Entity (Name (gnat_with_clause));
8699 elaborate_all_entities (gnat_unit);
8701 if (Ekind (gnat_entity) == E_Package)
8702 elaborate_all_entities_for_package (gnat_entity);
8704 else if (Ekind (gnat_entity) == E_Generic_Package)
8706 Node_Id gnat_body = Corresponding_Body (Unit (gnat_unit));
8708 /* Retrieve compilation unit node of generic body. */
8709 while (Present (gnat_body)
8710 && Nkind (gnat_body) != N_Compilation_Unit)
8711 gnat_body = Parent (gnat_body);
8713 /* If body is available, elaborate its context. */
8714 if (Present (gnat_body))
8715 elaborate_all_entities (gnat_body);
8719 if (Nkind (Unit (gnat_node)) == N_Package_Body)
8720 elaborate_all_entities (Library_Unit (gnat_node));
8723 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
8725 static void
8726 process_freeze_entity (Node_Id gnat_node)
8728 const Entity_Id gnat_entity = Entity (gnat_node);
8729 const Entity_Kind kind = Ekind (gnat_entity);
8730 tree gnu_old, gnu_new;
8732 /* If this is a package, generate code for the package body, if any. */
8733 if (kind == E_Package)
8735 const Node_Id gnat_decl = Parent (Declaration_Node (gnat_entity));
8736 if (Present (Corresponding_Body (gnat_decl)))
8737 insert_code_for (Parent (Corresponding_Body (gnat_decl)));
8738 return;
8741 /* Don't do anything for class-wide types as they are always transformed
8742 into their root type. */
8743 if (kind == E_Class_Wide_Type)
8744 return;
8746 /* Check for an old definition if this isn't an object with address clause,
8747 since the saved GCC tree is the address expression in that case. */
8748 gnu_old
8749 = present_gnu_tree (gnat_entity) && No (Address_Clause (gnat_entity))
8750 ? get_gnu_tree (gnat_entity) : NULL_TREE;
8752 /* Don't do anything for subprograms that may have been elaborated before
8753 their freeze nodes. This can happen, for example, because of an inner
8754 call in an instance body or because of previous compilation of a spec
8755 for inlining purposes. */
8756 if (gnu_old
8757 && ((TREE_CODE (gnu_old) == FUNCTION_DECL
8758 && (kind == E_Function || kind == E_Procedure))
8759 || (TREE_CODE (TREE_TYPE (gnu_old)) == FUNCTION_TYPE
8760 && kind == E_Subprogram_Type)))
8761 return;
8763 /* If we have a non-dummy type old tree, we have nothing to do, except for
8764 aborting, since this node was never delayed as it should have been. We
8765 let this happen for concurrent types and their Corresponding_Record_Type,
8766 however, because each might legitimately be elaborated before its own
8767 freeze node, e.g. while processing the other. */
8768 if (gnu_old
8769 && !(TREE_CODE (gnu_old) == TYPE_DECL
8770 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old))))
8772 gcc_assert (Is_Concurrent_Type (gnat_entity)
8773 || (Is_Record_Type (gnat_entity)
8774 && Is_Concurrent_Record_Type (gnat_entity)));
8775 return;
8778 /* Reset the saved tree, if any, and elaborate the object or type for real.
8779 If there is a full view, elaborate it and use the result. And, if this
8780 is the root type of a class-wide type, reuse it for the latter. */
8781 if (gnu_old)
8783 save_gnu_tree (gnat_entity, NULL_TREE, false);
8785 if (Is_Incomplete_Or_Private_Type (gnat_entity)
8786 && Present (Full_View (gnat_entity)))
8788 Entity_Id full_view = Full_View (gnat_entity);
8790 save_gnu_tree (full_view, NULL_TREE, false);
8792 if (Is_Private_Type (full_view)
8793 && Present (Underlying_Full_View (full_view)))
8795 full_view = Underlying_Full_View (full_view);
8796 save_gnu_tree (full_view, NULL_TREE, false);
8800 if (Is_Type (gnat_entity)
8801 && Present (Class_Wide_Type (gnat_entity))
8802 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8803 save_gnu_tree (Class_Wide_Type (gnat_entity), NULL_TREE, false);
8806 if (Is_Incomplete_Or_Private_Type (gnat_entity)
8807 && Present (Full_View (gnat_entity)))
8809 Entity_Id full_view = Full_View (gnat_entity);
8811 if (Is_Private_Type (full_view)
8812 && Present (Underlying_Full_View (full_view)))
8813 full_view = Underlying_Full_View (full_view);
8815 gnu_new = gnat_to_gnu_entity (full_view, NULL_TREE, true);
8817 /* Propagate back-annotations from full view to partial view. */
8818 if (Unknown_Alignment (gnat_entity))
8819 Set_Alignment (gnat_entity, Alignment (full_view));
8821 if (Unknown_Esize (gnat_entity))
8822 Set_Esize (gnat_entity, Esize (full_view));
8824 if (Unknown_RM_Size (gnat_entity))
8825 Set_RM_Size (gnat_entity, RM_Size (full_view));
8827 /* The above call may have defined this entity (the simplest example
8828 of this is when we have a private enumeral type since the bounds
8829 will have the public view). */
8830 if (!present_gnu_tree (gnat_entity))
8831 save_gnu_tree (gnat_entity, gnu_new, false);
8833 else
8835 tree gnu_init
8836 = (Nkind (Declaration_Node (gnat_entity)) == N_Object_Declaration
8837 && present_gnu_tree (Declaration_Node (gnat_entity)))
8838 ? get_gnu_tree (Declaration_Node (gnat_entity)) : NULL_TREE;
8840 gnu_new = gnat_to_gnu_entity (gnat_entity, gnu_init, true);
8843 if (Is_Type (gnat_entity)
8844 && Present (Class_Wide_Type (gnat_entity))
8845 && Root_Type (Class_Wide_Type (gnat_entity)) == gnat_entity)
8846 save_gnu_tree (Class_Wide_Type (gnat_entity), gnu_new, false);
8848 /* If we have an old type and we've made pointers to this type, update those
8849 pointers. If this is a Taft amendment type in the main unit, we need to
8850 mark the type as used since other units referencing it don't see the full
8851 declaration and, therefore, cannot mark it as used themselves. */
8852 if (gnu_old)
8854 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
8855 TREE_TYPE (gnu_new));
8856 if (TYPE_DUMMY_IN_PROFILE_P (TREE_TYPE (gnu_old)))
8857 update_profiles_with (TREE_TYPE (gnu_old));
8858 if (DECL_TAFT_TYPE_P (gnu_old))
8859 used_types_insert (TREE_TYPE (gnu_new));
8863 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
8864 We make two passes, one to elaborate anything other than bodies (but
8865 we declare a function if there was no spec). The second pass
8866 elaborates the bodies.
8868 GNAT_END_LIST gives the element in the list past the end. Normally,
8869 this is Empty, but can be First_Real_Statement for a
8870 Handled_Sequence_Of_Statements.
8872 We make a complete pass through both lists if PASS1P is true, then make
8873 the second pass over both lists if PASS2P is true. The lists usually
8874 correspond to the public and private parts of a package. */
8876 static void
8877 process_decls (List_Id gnat_decls, List_Id gnat_decls2,
8878 Node_Id gnat_end_list, bool pass1p, bool pass2p)
8880 List_Id gnat_decl_array[2];
8881 Node_Id gnat_decl;
8882 int i;
8884 gnat_decl_array[0] = gnat_decls, gnat_decl_array[1] = gnat_decls2;
8886 if (pass1p)
8887 for (i = 0; i <= 1; i++)
8888 if (Present (gnat_decl_array[i]))
8889 for (gnat_decl = First (gnat_decl_array[i]);
8890 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8892 /* For package specs, we recurse inside the declarations,
8893 thus taking the two pass approach inside the boundary. */
8894 if (Nkind (gnat_decl) == N_Package_Declaration
8895 && (Nkind (Specification (gnat_decl)
8896 == N_Package_Specification)))
8897 process_decls (Visible_Declarations (Specification (gnat_decl)),
8898 Private_Declarations (Specification (gnat_decl)),
8899 Empty, true, false);
8901 /* Similarly for any declarations in the actions of a
8902 freeze node. */
8903 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8905 process_freeze_entity (gnat_decl);
8906 process_decls (Actions (gnat_decl), Empty, Empty, true, false);
8909 /* Package bodies with freeze nodes get their elaboration deferred
8910 until the freeze node, but the code must be placed in the right
8911 place, so record the code position now. */
8912 else if (Nkind (gnat_decl) == N_Package_Body
8913 && Present (Freeze_Node (Corresponding_Spec (gnat_decl))))
8914 record_code_position (gnat_decl);
8916 else if (Nkind (gnat_decl) == N_Package_Body_Stub
8917 && Present (Library_Unit (gnat_decl))
8918 && Present (Freeze_Node
8919 (Corresponding_Spec
8920 (Proper_Body (Unit
8921 (Library_Unit (gnat_decl)))))))
8922 record_code_position
8923 (Proper_Body (Unit (Library_Unit (gnat_decl))));
8925 /* We defer most subprogram bodies to the second pass. */
8926 else if (Nkind (gnat_decl) == N_Subprogram_Body)
8928 if (Acts_As_Spec (gnat_decl))
8930 Node_Id gnat_subprog_id = Defining_Entity (gnat_decl);
8932 if (Ekind (gnat_subprog_id) != E_Generic_Procedure
8933 && Ekind (gnat_subprog_id) != E_Generic_Function)
8934 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, true);
8938 /* For bodies and stubs that act as their own specs, the entity
8939 itself must be elaborated in the first pass, because it may
8940 be used in other declarations. */
8941 else if (Nkind (gnat_decl) == N_Subprogram_Body_Stub)
8943 Node_Id gnat_subprog_id
8944 = Defining_Entity (Specification (gnat_decl));
8946 if (Ekind (gnat_subprog_id) != E_Subprogram_Body
8947 && Ekind (gnat_subprog_id) != E_Generic_Procedure
8948 && Ekind (gnat_subprog_id) != E_Generic_Function)
8949 gnat_to_gnu_entity (gnat_subprog_id, NULL_TREE, true);
8952 /* Concurrent stubs stand for the corresponding subprogram bodies,
8953 which are deferred like other bodies. */
8954 else if (Nkind (gnat_decl) == N_Task_Body_Stub
8955 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8958 /* Renamed subprograms may not be elaborated yet at this point
8959 since renamings do not trigger freezing. Wait for the second
8960 pass to take care of them. */
8961 else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration)
8964 else
8965 add_stmt (gnat_to_gnu (gnat_decl));
8968 /* Here we elaborate everything we deferred above except for package bodies,
8969 which are elaborated at their freeze nodes. Note that we must also
8970 go inside things (package specs and freeze nodes) the first pass did. */
8971 if (pass2p)
8972 for (i = 0; i <= 1; i++)
8973 if (Present (gnat_decl_array[i]))
8974 for (gnat_decl = First (gnat_decl_array[i]);
8975 gnat_decl != gnat_end_list; gnat_decl = Next (gnat_decl))
8977 if (Nkind (gnat_decl) == N_Subprogram_Body
8978 || Nkind (gnat_decl) == N_Subprogram_Body_Stub
8979 || Nkind (gnat_decl) == N_Task_Body_Stub
8980 || Nkind (gnat_decl) == N_Protected_Body_Stub)
8981 add_stmt (gnat_to_gnu (gnat_decl));
8983 else if (Nkind (gnat_decl) == N_Package_Declaration
8984 && (Nkind (Specification (gnat_decl)
8985 == N_Package_Specification)))
8986 process_decls (Visible_Declarations (Specification (gnat_decl)),
8987 Private_Declarations (Specification (gnat_decl)),
8988 Empty, false, true);
8990 else if (Nkind (gnat_decl) == N_Freeze_Entity)
8991 process_decls (Actions (gnat_decl), Empty, Empty, false, true);
8993 else if (Nkind (gnat_decl) == N_Subprogram_Renaming_Declaration)
8994 add_stmt (gnat_to_gnu (gnat_decl));
8998 /* Make a unary operation of kind CODE using build_unary_op, but guard
8999 the operation by an overflow check. CODE can be one of NEGATE_EXPR
9000 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
9001 the operation is to be performed in that type. GNAT_NODE is the gnat
9002 node conveying the source location for which the error should be
9003 signaled. */
9005 static tree
9006 build_unary_op_trapv (enum tree_code code, tree gnu_type, tree operand,
9007 Node_Id gnat_node)
9009 gcc_assert (code == NEGATE_EXPR || code == ABS_EXPR);
9011 operand = gnat_protect_expr (operand);
9013 return emit_check (build_binary_op (EQ_EXPR, boolean_type_node,
9014 operand, TYPE_MIN_VALUE (gnu_type)),
9015 build_unary_op (code, gnu_type, operand),
9016 CE_Overflow_Check_Failed, gnat_node);
9019 /* Make a binary operation of kind CODE using build_binary_op, but guard
9020 the operation by an overflow check. CODE can be one of PLUS_EXPR,
9021 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
9022 Usually the operation is to be performed in that type. GNAT_NODE is
9023 the GNAT node conveying the source location for which the error should
9024 be signaled. */
9026 static tree
9027 build_binary_op_trapv (enum tree_code code, tree gnu_type, tree left,
9028 tree right, Node_Id gnat_node)
9030 const unsigned int precision = TYPE_PRECISION (gnu_type);
9031 tree lhs = gnat_protect_expr (left);
9032 tree rhs = gnat_protect_expr (right);
9033 tree type_max = TYPE_MAX_VALUE (gnu_type);
9034 tree type_min = TYPE_MIN_VALUE (gnu_type);
9035 tree gnu_expr, check;
9036 int sgn;
9038 /* Assert that the precision is a power of 2. */
9039 gcc_assert ((precision & (precision - 1)) == 0);
9041 /* Prefer a constant on the RHS to simplify checks. */
9042 if (TREE_CODE (rhs) != INTEGER_CST
9043 && TREE_CODE (lhs) == INTEGER_CST
9044 && (code == PLUS_EXPR || code == MULT_EXPR))
9046 tree tmp = lhs;
9047 lhs = rhs;
9048 rhs = tmp;
9051 gnu_expr = build_binary_op (code, gnu_type, lhs, rhs);
9053 /* If we can fold the expression to a constant, just return it.
9054 The caller will deal with overflow, no need to generate a check. */
9055 if (TREE_CODE (gnu_expr) == INTEGER_CST)
9056 return gnu_expr;
9058 /* If no operand is a constant, we use the generic implementation. */
9059 if (TREE_CODE (lhs) != INTEGER_CST && TREE_CODE (rhs) != INTEGER_CST)
9061 /* Never inline a 64-bit mult for a 32-bit target, it's way too long. */
9062 if (code == MULT_EXPR && precision == 64 && BITS_PER_WORD < 64)
9064 tree int64 = gnat_type_for_size (64, 0);
9065 return convert (gnu_type, build_call_n_expr (mulv64_decl, 2,
9066 convert (int64, lhs),
9067 convert (int64, rhs)));
9070 enum internal_fn icode;
9072 switch (code)
9074 case PLUS_EXPR:
9075 icode = IFN_ADD_OVERFLOW;
9076 break;
9077 case MINUS_EXPR:
9078 icode = IFN_SUB_OVERFLOW;
9079 break;
9080 case MULT_EXPR:
9081 icode = IFN_MUL_OVERFLOW;
9082 break;
9083 default:
9084 gcc_unreachable ();
9087 tree gnu_ctype = build_complex_type (gnu_type);
9088 tree call
9089 = build_call_expr_internal_loc (UNKNOWN_LOCATION, icode, gnu_ctype, 2,
9090 lhs, rhs);
9091 tree tgt = save_expr (call);
9092 gnu_expr = build1 (REALPART_EXPR, gnu_type, tgt);
9093 check = fold_build2 (NE_EXPR, boolean_type_node,
9094 build1 (IMAGPART_EXPR, gnu_type, tgt),
9095 build_int_cst (gnu_type, 0));
9096 return
9097 emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
9100 /* If one operand is a constant, we expose the overflow condition to enable
9101 a subsequent simplication or even elimination. */
9102 switch (code)
9104 case PLUS_EXPR:
9105 sgn = tree_int_cst_sgn (rhs);
9106 if (sgn > 0)
9107 /* When rhs > 0, overflow when lhs > type_max - rhs. */
9108 check = build_binary_op (GT_EXPR, boolean_type_node, lhs,
9109 build_binary_op (MINUS_EXPR, gnu_type,
9110 type_max, rhs));
9111 else if (sgn < 0)
9112 /* When rhs < 0, overflow when lhs < type_min - rhs. */
9113 check = build_binary_op (LT_EXPR, boolean_type_node, lhs,
9114 build_binary_op (MINUS_EXPR, gnu_type,
9115 type_min, rhs));
9116 else
9117 return gnu_expr;
9118 break;
9120 case MINUS_EXPR:
9121 if (TREE_CODE (lhs) == INTEGER_CST)
9123 sgn = tree_int_cst_sgn (lhs);
9124 if (sgn > 0)
9125 /* When lhs > 0, overflow when rhs < lhs - type_max. */
9126 check = build_binary_op (LT_EXPR, boolean_type_node, rhs,
9127 build_binary_op (MINUS_EXPR, gnu_type,
9128 lhs, type_max));
9129 else if (sgn < 0)
9130 /* When lhs < 0, overflow when rhs > lhs - type_min. */
9131 check = build_binary_op (GT_EXPR, boolean_type_node, rhs,
9132 build_binary_op (MINUS_EXPR, gnu_type,
9133 lhs, type_min));
9134 else
9135 return gnu_expr;
9137 else
9139 sgn = tree_int_cst_sgn (rhs);
9140 if (sgn > 0)
9141 /* When rhs > 0, overflow when lhs < type_min + rhs. */
9142 check = build_binary_op (LT_EXPR, boolean_type_node, lhs,
9143 build_binary_op (PLUS_EXPR, gnu_type,
9144 type_min, rhs));
9145 else if (sgn < 0)
9146 /* When rhs < 0, overflow when lhs > type_max + rhs. */
9147 check = build_binary_op (GT_EXPR, boolean_type_node, lhs,
9148 build_binary_op (PLUS_EXPR, gnu_type,
9149 type_max, rhs));
9150 else
9151 return gnu_expr;
9153 break;
9155 case MULT_EXPR:
9156 sgn = tree_int_cst_sgn (rhs);
9157 if (sgn > 0)
9159 if (integer_onep (rhs))
9160 return gnu_expr;
9162 tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
9163 tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
9165 /* When rhs > 1, overflow outside [type_min/rhs; type_max/rhs]. */
9166 check
9167 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9168 build_binary_op (LT_EXPR, boolean_type_node,
9169 lhs, lb),
9170 build_binary_op (GT_EXPR, boolean_type_node,
9171 lhs, ub));
9173 else if (sgn < 0)
9175 tree lb = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_max, rhs);
9176 tree ub = build_binary_op (TRUNC_DIV_EXPR, gnu_type, type_min, rhs);
9178 if (integer_minus_onep (rhs))
9179 /* When rhs == -1, overflow if lhs == type_min. */
9180 check
9181 = build_binary_op (EQ_EXPR, boolean_type_node, lhs, type_min);
9182 else
9183 /* When rhs < -1, overflow outside [type_max/rhs; type_min/rhs]. */
9184 check
9185 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9186 build_binary_op (LT_EXPR, boolean_type_node,
9187 lhs, lb),
9188 build_binary_op (GT_EXPR, boolean_type_node,
9189 lhs, ub));
9191 else
9192 return gnu_expr;
9193 break;
9195 default:
9196 gcc_unreachable ();
9199 return emit_check (check, gnu_expr, CE_Overflow_Check_Failed, gnat_node);
9202 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
9203 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
9204 which we have to check. GNAT_NODE is the GNAT node conveying the source
9205 location for which the error should be signaled. */
9207 static tree
9208 emit_range_check (tree gnu_expr, Entity_Id gnat_range_type, Node_Id gnat_node)
9210 tree gnu_range_type = get_unpadded_type (gnat_range_type);
9211 tree gnu_compare_type = get_base_type (TREE_TYPE (gnu_expr));
9213 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
9214 This can for example happen when translating 'Val or 'Value. */
9215 if (gnu_compare_type == gnu_range_type)
9216 return gnu_expr;
9218 /* Range checks can only be applied to types with ranges. */
9219 gcc_assert (INTEGRAL_TYPE_P (gnu_range_type)
9220 || SCALAR_FLOAT_TYPE_P (gnu_range_type));
9222 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
9223 we can't do anything since we might be truncating the bounds. No
9224 check is needed in this case. */
9225 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr))
9226 && (TYPE_PRECISION (gnu_compare_type)
9227 < TYPE_PRECISION (get_base_type (gnu_range_type))))
9228 return gnu_expr;
9230 /* Checked expressions must be evaluated only once. */
9231 gnu_expr = gnat_protect_expr (gnu_expr);
9233 /* Note that the form of the check is
9234 (not (expr >= lo)) or (not (expr <= hi))
9235 the reason for this slightly convoluted form is that NaNs
9236 are not considered to be in range in the float case. */
9237 return emit_check
9238 (build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node,
9239 invert_truthvalue
9240 (build_binary_op (GE_EXPR, boolean_type_node,
9241 convert (gnu_compare_type, gnu_expr),
9242 convert (gnu_compare_type,
9243 TYPE_MIN_VALUE
9244 (gnu_range_type)))),
9245 invert_truthvalue
9246 (build_binary_op (LE_EXPR, boolean_type_node,
9247 convert (gnu_compare_type, gnu_expr),
9248 convert (gnu_compare_type,
9249 TYPE_MAX_VALUE
9250 (gnu_range_type))))),
9251 gnu_expr, CE_Range_Check_Failed, gnat_node);
9254 /* GNU_COND contains the condition corresponding to an index, overflow or
9255 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR
9256 if GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
9257 REASON is the code that says why the exception is raised. GNAT_NODE is
9258 the node conveying the source location for which the error should be
9259 signaled.
9261 We used to propagate TREE_SIDE_EFFECTS from GNU_EXPR to the COND_EXPR,
9262 overwriting the setting inherited from the call statement, on the ground
9263 that the expression need not be evaluated just for the check. However
9264 that's incorrect because, in the GCC type system, its value is presumed
9265 to be valid so its comparison against the type bounds always yields true
9266 and, therefore, could be done without evaluating it; given that it can
9267 be a computation that overflows the bounds, the language may require the
9268 check to fail and thus the expression to be evaluated in this case. */
9270 static tree
9271 emit_check (tree gnu_cond, tree gnu_expr, int reason, Node_Id gnat_node)
9273 tree gnu_call
9274 = build_call_raise (reason, gnat_node, N_Raise_Constraint_Error);
9275 return
9276 fold_build3 (COND_EXPR, TREE_TYPE (gnu_expr), gnu_cond,
9277 build2 (COMPOUND_EXPR, TREE_TYPE (gnu_expr), gnu_call,
9278 SCALAR_FLOAT_TYPE_P (TREE_TYPE (gnu_expr))
9279 ? build_real (TREE_TYPE (gnu_expr), dconst0)
9280 : build_int_cst (TREE_TYPE (gnu_expr), 0)),
9281 gnu_expr);
9284 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
9285 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
9286 If TRUNCATE_P true, do a float-to-integer conversion with truncation,
9287 otherwise round. GNAT_NODE is the GNAT node conveying the source location
9288 for which the error should be signaled. */
9290 static tree
9291 convert_with_check (Entity_Id gnat_type, tree gnu_expr, bool overflow_p,
9292 bool range_p, bool truncate_p, Node_Id gnat_node)
9294 tree gnu_type = get_unpadded_type (gnat_type);
9295 tree gnu_base_type = get_base_type (gnu_type);
9296 tree gnu_in_type = TREE_TYPE (gnu_expr);
9297 tree gnu_in_base_type = get_base_type (gnu_in_type);
9298 tree gnu_result = gnu_expr;
9300 /* If we are not doing any checks, the output is an integral type and the
9301 input is not a floating-point type, just do the conversion. This is
9302 required for packed array types and is simpler in all cases anyway. */
9303 if (!range_p
9304 && !overflow_p
9305 && INTEGRAL_TYPE_P (gnu_base_type)
9306 && !FLOAT_TYPE_P (gnu_in_base_type))
9307 return convert (gnu_type, gnu_expr);
9309 /* If the mode of the input base type is larger, then converting to it below
9310 may pessimize the final conversion step, for example generate a libcall
9311 instead of a simple instruction, so use a narrower type in this case. */
9312 if (TYPE_MODE (gnu_in_base_type) != TYPE_MODE (gnu_in_type)
9313 && !(TREE_CODE (gnu_in_type) == INTEGER_TYPE
9314 && TYPE_BIASED_REPRESENTATION_P (gnu_in_type)))
9315 gnu_in_base_type = gnat_type_for_mode (TYPE_MODE (gnu_in_type),
9316 TYPE_UNSIGNED (gnu_in_type));
9318 /* First convert the expression to the base type. This will never generate
9319 code, but makes the tests below simpler. But don't do this if converting
9320 from an integer type to an unconstrained array type since then we need to
9321 get the bounds from the original (unpacked) type. */
9322 if (TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE)
9323 gnu_result = convert (gnu_in_base_type, gnu_result);
9325 /* If overflow checks are requested, we need to be sure the result will fit
9326 in the output base type. But don't do this if the input is integer and
9327 the output floating-point. */
9328 if (overflow_p
9329 && !(FLOAT_TYPE_P (gnu_base_type) && INTEGRAL_TYPE_P (gnu_in_base_type)))
9331 /* Ensure GNU_EXPR only gets evaluated once. */
9332 tree gnu_input = gnat_protect_expr (gnu_result);
9333 tree gnu_cond = boolean_false_node;
9334 tree gnu_in_lb = TYPE_MIN_VALUE (gnu_in_base_type);
9335 tree gnu_in_ub = TYPE_MAX_VALUE (gnu_in_base_type);
9336 tree gnu_out_lb = TYPE_MIN_VALUE (gnu_base_type);
9337 tree gnu_out_ub = TYPE_MAX_VALUE (gnu_base_type);
9339 /* Convert the lower bounds to signed types, so we're sure we're
9340 comparing them properly. Likewise, convert the upper bounds
9341 to unsigned types. */
9342 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9343 && TYPE_UNSIGNED (gnu_in_base_type))
9344 gnu_in_lb
9345 = convert (gnat_signed_type_for (gnu_in_base_type), gnu_in_lb);
9347 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9348 && !TYPE_UNSIGNED (gnu_in_base_type))
9349 gnu_in_ub
9350 = convert (gnat_unsigned_type_for (gnu_in_base_type), gnu_in_ub);
9352 if (INTEGRAL_TYPE_P (gnu_base_type) && TYPE_UNSIGNED (gnu_base_type))
9353 gnu_out_lb
9354 = convert (gnat_signed_type_for (gnu_base_type), gnu_out_lb);
9356 if (INTEGRAL_TYPE_P (gnu_base_type) && !TYPE_UNSIGNED (gnu_base_type))
9357 gnu_out_ub
9358 = convert (gnat_unsigned_type_for (gnu_base_type), gnu_out_ub);
9360 /* Check each bound separately and only if the result bound
9361 is tighter than the bound on the input type. Note that all the
9362 types are base types, so the bounds must be constant. Also,
9363 the comparison is done in the base type of the input, which
9364 always has the proper signedness. First check for input
9365 integer (which means output integer), output float (which means
9366 both float), or mixed, in which case we always compare.
9367 Note that we have to do the comparison which would *fail* in the
9368 case of an error since if it's an FP comparison and one of the
9369 values is a NaN or Inf, the comparison will fail. */
9370 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9371 ? tree_int_cst_lt (gnu_in_lb, gnu_out_lb)
9372 : (FLOAT_TYPE_P (gnu_base_type)
9373 ? real_less (&TREE_REAL_CST (gnu_in_lb),
9374 &TREE_REAL_CST (gnu_out_lb))
9375 : 1))
9376 gnu_cond
9377 = invert_truthvalue
9378 (build_binary_op (GE_EXPR, boolean_type_node,
9379 gnu_input, convert (gnu_in_base_type,
9380 gnu_out_lb)));
9382 if (INTEGRAL_TYPE_P (gnu_in_base_type)
9383 ? tree_int_cst_lt (gnu_out_ub, gnu_in_ub)
9384 : (FLOAT_TYPE_P (gnu_base_type)
9385 ? real_less (&TREE_REAL_CST (gnu_out_ub),
9386 &TREE_REAL_CST (gnu_in_ub))
9387 : 1))
9388 gnu_cond
9389 = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, gnu_cond,
9390 invert_truthvalue
9391 (build_binary_op (LE_EXPR, boolean_type_node,
9392 gnu_input,
9393 convert (gnu_in_base_type,
9394 gnu_out_ub))));
9396 if (!integer_zerop (gnu_cond))
9397 gnu_result = emit_check (gnu_cond, gnu_input,
9398 CE_Overflow_Check_Failed, gnat_node);
9401 /* Now convert to the result base type. If this is a non-truncating
9402 float-to-integer conversion, round. */
9403 if (INTEGRAL_TYPE_P (gnu_base_type)
9404 && FLOAT_TYPE_P (gnu_in_base_type)
9405 && !truncate_p)
9407 REAL_VALUE_TYPE half_minus_pred_half, pred_half;
9408 tree gnu_conv, gnu_zero, gnu_comp, calc_type;
9409 tree gnu_pred_half, gnu_add_pred_half, gnu_subtract_pred_half;
9410 const struct real_format *fmt;
9412 /* The following calculations depend on proper rounding to even
9413 of each arithmetic operation. In order to prevent excess
9414 precision from spoiling this property, use the widest hardware
9415 floating-point type if FP_ARITH_MAY_WIDEN is true. */
9416 calc_type
9417 = fp_arith_may_widen ? longest_float_type_node : gnu_in_base_type;
9419 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
9420 fmt = REAL_MODE_FORMAT (TYPE_MODE (calc_type));
9421 real_2expN (&half_minus_pred_half, -(fmt->p) - 1, TYPE_MODE (calc_type));
9422 real_arithmetic (&pred_half, MINUS_EXPR, &dconsthalf,
9423 &half_minus_pred_half);
9424 gnu_pred_half = build_real (calc_type, pred_half);
9426 /* If the input is strictly negative, subtract this value
9427 and otherwise add it from the input. For 0.5, the result
9428 is exactly between 1.0 and the machine number preceding 1.0
9429 (for calc_type). Since the last bit of 1.0 is even, this 0.5
9430 will round to 1.0, while all other number with an absolute
9431 value less than 0.5 round to 0.0. For larger numbers exactly
9432 halfway between integers, rounding will always be correct as
9433 the true mathematical result will be closer to the higher
9434 integer compared to the lower one. So, this constant works
9435 for all floating-point numbers.
9437 The reason to use the same constant with subtract/add instead
9438 of a positive and negative constant is to allow the comparison
9439 to be scheduled in parallel with retrieval of the constant and
9440 conversion of the input to the calc_type (if necessary). */
9442 gnu_zero = build_real (gnu_in_base_type, dconst0);
9443 gnu_result = gnat_protect_expr (gnu_result);
9444 gnu_conv = convert (calc_type, gnu_result);
9445 gnu_comp
9446 = fold_build2 (GE_EXPR, boolean_type_node, gnu_result, gnu_zero);
9447 gnu_add_pred_half
9448 = fold_build2 (PLUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
9449 gnu_subtract_pred_half
9450 = fold_build2 (MINUS_EXPR, calc_type, gnu_conv, gnu_pred_half);
9451 gnu_result = fold_build3 (COND_EXPR, calc_type, gnu_comp,
9452 gnu_add_pred_half, gnu_subtract_pred_half);
9455 if (TREE_CODE (gnu_base_type) == INTEGER_TYPE
9456 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type)
9457 && TREE_CODE (gnu_result) == UNCONSTRAINED_ARRAY_REF)
9458 gnu_result = unchecked_convert (gnu_base_type, gnu_result, false);
9459 else
9460 gnu_result = convert (gnu_base_type, gnu_result);
9462 /* Finally, do the range check if requested. Note that if the result type
9463 is a modular type, the range check is actually an overflow check. */
9464 if (range_p
9465 || (overflow_p
9466 && TREE_CODE (gnu_base_type) == INTEGER_TYPE
9467 && TYPE_MODULAR_P (gnu_base_type)))
9468 gnu_result = emit_range_check (gnu_result, gnat_type, gnat_node);
9470 return convert (gnu_type, gnu_result);
9473 /* Return true if GNU_EXPR can be directly addressed. This is the case
9474 unless it is an expression involving computation or if it involves a
9475 reference to a bitfield or to an object not sufficiently aligned for
9476 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
9477 be directly addressed as an object of this type.
9479 *** Notes on addressability issues in the Ada compiler ***
9481 This predicate is necessary in order to bridge the gap between Gigi
9482 and the middle-end about addressability of GENERIC trees. A tree
9483 is said to be addressable if it can be directly addressed, i.e. if
9484 its address can be taken, is a multiple of the type's alignment on
9485 strict-alignment architectures and returns the first storage unit
9486 assigned to the object represented by the tree.
9488 In the C family of languages, everything is in practice addressable
9489 at the language level, except for bit-fields. This means that these
9490 compilers will take the address of any tree that doesn't represent
9491 a bit-field reference and expect the result to be the first storage
9492 unit assigned to the object. Even in cases where this will result
9493 in unaligned accesses at run time, nothing is supposed to be done
9494 and the program is considered as erroneous instead (see PR c/18287).
9496 The implicit assumptions made in the middle-end are in keeping with
9497 the C viewpoint described above:
9498 - the address of a bit-field reference is supposed to be never
9499 taken; the compiler (generally) will stop on such a construct,
9500 - any other tree is addressable if it is formally addressable,
9501 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
9503 In Ada, the viewpoint is the opposite one: nothing is addressable
9504 at the language level unless explicitly declared so. This means
9505 that the compiler will both make sure that the trees representing
9506 references to addressable ("aliased" in Ada parlance) objects are
9507 addressable and make no real attempts at ensuring that the trees
9508 representing references to non-addressable objects are addressable.
9510 In the first case, Ada is effectively equivalent to C and handing
9511 down the direct result of applying ADDR_EXPR to these trees to the
9512 middle-end works flawlessly. In the second case, Ada cannot afford
9513 to consider the program as erroneous if the address of trees that
9514 are not addressable is requested for technical reasons, unlike C;
9515 as a consequence, the Ada compiler must arrange for either making
9516 sure that this address is not requested in the middle-end or for
9517 compensating by inserting temporaries if it is requested in Gigi.
9519 The first goal can be achieved because the middle-end should not
9520 request the address of non-addressable trees on its own; the only
9521 exception is for the invocation of low-level block operations like
9522 memcpy, for which the addressability requirements are lower since
9523 the type's alignment can be disregarded. In practice, this means
9524 that Gigi must make sure that such operations cannot be applied to
9525 non-BLKmode bit-fields.
9527 The second goal is achieved by means of the addressable_p predicate,
9528 which computes whether a temporary must be inserted by Gigi when the
9529 address of a tree is requested; if so, the address of the temporary
9530 will be used in lieu of that of the original tree and some glue code
9531 generated to connect everything together. */
9533 static bool
9534 addressable_p (tree gnu_expr, tree gnu_type)
9536 /* For an integral type, the size of the actual type of the object may not
9537 be greater than that of the expected type, otherwise an indirect access
9538 in the latter type wouldn't correctly set all the bits of the object. */
9539 if (gnu_type
9540 && INTEGRAL_TYPE_P (gnu_type)
9541 && smaller_form_type_p (gnu_type, TREE_TYPE (gnu_expr)))
9542 return false;
9544 /* The size of the actual type of the object may not be smaller than that
9545 of the expected type, otherwise an indirect access in the latter type
9546 would be larger than the object. But only record types need to be
9547 considered in practice for this case. */
9548 if (gnu_type
9549 && TREE_CODE (gnu_type) == RECORD_TYPE
9550 && smaller_form_type_p (TREE_TYPE (gnu_expr), gnu_type))
9551 return false;
9553 switch (TREE_CODE (gnu_expr))
9555 case VAR_DECL:
9556 case PARM_DECL:
9557 case FUNCTION_DECL:
9558 case RESULT_DECL:
9559 /* All DECLs are addressable: if they are in a register, we can force
9560 them to memory. */
9561 return true;
9563 case UNCONSTRAINED_ARRAY_REF:
9564 case INDIRECT_REF:
9565 /* Taking the address of a dereference yields the original pointer. */
9566 return true;
9568 case STRING_CST:
9569 case INTEGER_CST:
9570 /* Taking the address yields a pointer to the constant pool. */
9571 return true;
9573 case CONSTRUCTOR:
9574 /* Taking the address of a static constructor yields a pointer to the
9575 tree constant pool. */
9576 return TREE_STATIC (gnu_expr) ? true : false;
9578 case NULL_EXPR:
9579 case SAVE_EXPR:
9580 case CALL_EXPR:
9581 case PLUS_EXPR:
9582 case MINUS_EXPR:
9583 case BIT_IOR_EXPR:
9584 case BIT_XOR_EXPR:
9585 case BIT_AND_EXPR:
9586 case BIT_NOT_EXPR:
9587 /* All rvalues are deemed addressable since taking their address will
9588 force a temporary to be created by the middle-end. */
9589 return true;
9591 case COMPOUND_EXPR:
9592 /* The address of a compound expression is that of its 2nd operand. */
9593 return addressable_p (TREE_OPERAND (gnu_expr, 1), gnu_type);
9595 case COND_EXPR:
9596 /* We accept &COND_EXPR as soon as both operands are addressable and
9597 expect the outcome to be the address of the selected operand. */
9598 return (addressable_p (TREE_OPERAND (gnu_expr, 1), NULL_TREE)
9599 && addressable_p (TREE_OPERAND (gnu_expr, 2), NULL_TREE));
9601 case COMPONENT_REF:
9602 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr, 1))
9603 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
9604 the field is sufficiently aligned, in case it is subject
9605 to a pragma Component_Alignment. But we don't need to
9606 check the alignment of the containing record, as it is
9607 guaranteed to be not smaller than that of its most
9608 aligned field that is not a bit-field. */
9609 && (!STRICT_ALIGNMENT
9610 || DECL_ALIGN (TREE_OPERAND (gnu_expr, 1))
9611 >= TYPE_ALIGN (TREE_TYPE (gnu_expr))))
9612 /* The field of a padding record is always addressable. */
9613 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
9614 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
9616 case ARRAY_REF: case ARRAY_RANGE_REF:
9617 case REALPART_EXPR: case IMAGPART_EXPR:
9618 case NOP_EXPR:
9619 return addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE);
9621 case CONVERT_EXPR:
9622 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr))
9623 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
9625 case VIEW_CONVERT_EXPR:
9627 /* This is addressable if we can avoid a copy. */
9628 tree type = TREE_TYPE (gnu_expr);
9629 tree inner_type = TREE_TYPE (TREE_OPERAND (gnu_expr, 0));
9630 return (((TYPE_MODE (type) == TYPE_MODE (inner_type)
9631 && (!STRICT_ALIGNMENT
9632 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
9633 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT))
9634 || ((TYPE_MODE (type) == BLKmode
9635 || TYPE_MODE (inner_type) == BLKmode)
9636 && (!STRICT_ALIGNMENT
9637 || TYPE_ALIGN (type) <= TYPE_ALIGN (inner_type)
9638 || TYPE_ALIGN (inner_type) >= BIGGEST_ALIGNMENT
9639 || TYPE_ALIGN_OK (type)
9640 || TYPE_ALIGN_OK (inner_type))))
9641 && addressable_p (TREE_OPERAND (gnu_expr, 0), NULL_TREE));
9644 default:
9645 return false;
9649 /* Do the processing for the declaration of a GNAT_ENTITY, a type or subtype.
9650 If a Freeze node exists for the entity, delay the bulk of the processing.
9651 Otherwise make a GCC type for GNAT_ENTITY and set up the correspondence. */
9653 void
9654 process_type (Entity_Id gnat_entity)
9656 tree gnu_old
9657 = present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity) : NULL_TREE;
9659 /* If we are to delay elaboration of this type, just do any elaboration
9660 needed for expressions within the declaration and make a dummy node
9661 for it and its Full_View (if any), in case something points to it.
9662 Do not do this if it has already been done (the only way that can
9663 happen is if the private completion is also delayed). */
9664 if (Present (Freeze_Node (gnat_entity)))
9666 elaborate_entity (gnat_entity);
9668 if (!gnu_old)
9670 tree gnu_decl = TYPE_STUB_DECL (make_dummy_type (gnat_entity));
9671 save_gnu_tree (gnat_entity, gnu_decl, false);
9672 if (Is_Incomplete_Or_Private_Type (gnat_entity)
9673 && Present (Full_View (gnat_entity)))
9675 if (Has_Completion_In_Body (gnat_entity))
9676 DECL_TAFT_TYPE_P (gnu_decl) = 1;
9677 save_gnu_tree (Full_View (gnat_entity), gnu_decl, false);
9681 return;
9684 /* If we saved away a dummy type for this node, it means that this made the
9685 type that corresponds to the full type of an incomplete type. Clear that
9686 type for now and then update the type in the pointers below. But, if the
9687 saved type is not dummy, it very likely means that we have a use before
9688 declaration for the type in the tree, what we really cannot handle. */
9689 if (gnu_old)
9691 gcc_assert (TREE_CODE (gnu_old) == TYPE_DECL
9692 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old)));
9694 save_gnu_tree (gnat_entity, NULL_TREE, false);
9697 /* Now fully elaborate the type. */
9698 tree gnu_new = gnat_to_gnu_entity (gnat_entity, NULL_TREE, true);
9699 gcc_assert (TREE_CODE (gnu_new) == TYPE_DECL);
9701 /* If we have an old type and we've made pointers to this type, update those
9702 pointers. If this is a Taft amendment type in the main unit, we need to
9703 mark the type as used since other units referencing it don't see the full
9704 declaration and, therefore, cannot mark it as used themselves. */
9705 if (gnu_old)
9707 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old)),
9708 TREE_TYPE (gnu_new));
9709 if (DECL_TAFT_TYPE_P (gnu_old))
9710 used_types_insert (TREE_TYPE (gnu_new));
9713 /* If this is a record type corresponding to a task or protected type
9714 that is a completion of an incomplete type, perform a similar update
9715 on the type. ??? Including protected types here is a guess. */
9716 if (Is_Record_Type (gnat_entity)
9717 && Is_Concurrent_Record_Type (gnat_entity)
9718 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity)))
9720 tree gnu_task_old
9721 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity));
9723 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
9724 NULL_TREE, false);
9725 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity),
9726 gnu_new, false);
9728 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old)),
9729 TREE_TYPE (gnu_new));
9733 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
9734 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting of the
9735 associations that are from RECORD_TYPE. If we see an internal record, make
9736 a recursive call to fill it in as well. */
9738 static tree
9739 extract_values (tree values, tree record_type)
9741 vec<constructor_elt, va_gc> *v = NULL;
9742 tree field;
9744 for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
9746 tree tem, value = NULL_TREE;
9748 /* _Parent is an internal field, but may have values in the aggregate,
9749 so check for values first. */
9750 if ((tem = purpose_member (field, values)))
9752 value = TREE_VALUE (tem);
9753 TREE_ADDRESSABLE (tem) = 1;
9756 else if (DECL_INTERNAL_P (field))
9758 value = extract_values (values, TREE_TYPE (field));
9759 if (TREE_CODE (value) == CONSTRUCTOR
9760 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value)))
9761 value = NULL_TREE;
9763 else
9764 /* If we have a record subtype, the names will match, but not the
9765 actual FIELD_DECLs. */
9766 for (tem = values; tem; tem = TREE_CHAIN (tem))
9767 if (DECL_NAME (TREE_PURPOSE (tem)) == DECL_NAME (field))
9769 value = convert (TREE_TYPE (field), TREE_VALUE (tem));
9770 TREE_ADDRESSABLE (tem) = 1;
9773 if (!value)
9774 continue;
9776 CONSTRUCTOR_APPEND_ELT (v, field, value);
9779 return gnat_build_constructor (record_type, v);
9782 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
9783 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
9784 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
9786 static tree
9787 assoc_to_constructor (Entity_Id gnat_entity, Node_Id gnat_assoc, tree gnu_type)
9789 tree gnu_list = NULL_TREE, gnu_result;
9791 /* We test for GNU_FIELD being empty in the case where a variant
9792 was the last thing since we don't take things off GNAT_ASSOC in
9793 that case. We check GNAT_ASSOC in case we have a variant, but it
9794 has no fields. */
9796 for (; Present (gnat_assoc); gnat_assoc = Next (gnat_assoc))
9798 Node_Id gnat_field = First (Choices (gnat_assoc));
9799 tree gnu_field = gnat_to_gnu_field_decl (Entity (gnat_field));
9800 tree gnu_expr = gnat_to_gnu (Expression (gnat_assoc));
9802 /* The expander is supposed to put a single component selector name
9803 in every record component association. */
9804 gcc_assert (No (Next (gnat_field)));
9806 /* Ignore discriminants that have Corresponding_Discriminants in tagged
9807 types since we'll be setting those fields in the parent subtype. */
9808 if (Ekind (Entity (gnat_field)) == E_Discriminant
9809 && Present (Corresponding_Discriminant (Entity (gnat_field)))
9810 && Is_Tagged_Type (Scope (Entity (gnat_field))))
9811 continue;
9813 /* Also ignore discriminants of Unchecked_Unions. */
9814 if (Ekind (Entity (gnat_field)) == E_Discriminant
9815 && Is_Unchecked_Union (gnat_entity))
9816 continue;
9818 /* Before assigning a value in an aggregate make sure range checks
9819 are done if required. Then convert to the type of the field. */
9820 if (Do_Range_Check (Expression (gnat_assoc)))
9821 gnu_expr = emit_range_check (gnu_expr, Etype (gnat_field), Empty);
9823 gnu_expr = convert (TREE_TYPE (gnu_field), gnu_expr);
9825 /* Add the field and expression to the list. */
9826 gnu_list = tree_cons (gnu_field, gnu_expr, gnu_list);
9829 gnu_result = extract_values (gnu_list, gnu_type);
9831 if (flag_checking)
9833 /* Verify that every entry in GNU_LIST was used. */
9834 for (; gnu_list; gnu_list = TREE_CHAIN (gnu_list))
9835 gcc_assert (TREE_ADDRESSABLE (gnu_list));
9838 return gnu_result;
9841 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
9842 the first element of an array aggregate. It may itself be an aggregate.
9843 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
9844 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
9845 for range checking. */
9847 static tree
9848 pos_to_constructor (Node_Id gnat_expr, tree gnu_array_type,
9849 Entity_Id gnat_component_type)
9851 tree gnu_index = TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type));
9852 vec<constructor_elt, va_gc> *gnu_expr_vec = NULL;
9854 for (; Present (gnat_expr); gnat_expr = Next (gnat_expr))
9856 tree gnu_expr;
9858 /* If the expression is itself an array aggregate then first build the
9859 innermost constructor if it is part of our array (multi-dimensional
9860 case). */
9861 if (Nkind (gnat_expr) == N_Aggregate
9862 && TREE_CODE (TREE_TYPE (gnu_array_type)) == ARRAY_TYPE
9863 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type)))
9864 gnu_expr = pos_to_constructor (First (Expressions (gnat_expr)),
9865 TREE_TYPE (gnu_array_type),
9866 gnat_component_type);
9867 else
9869 /* If the expression is a conversion to an unconstrained array type,
9870 skip it to avoid spilling to memory. */
9871 if (Nkind (gnat_expr) == N_Type_Conversion
9872 && Is_Array_Type (Etype (gnat_expr))
9873 && !Is_Constrained (Etype (gnat_expr)))
9874 gnu_expr = gnat_to_gnu (Expression (gnat_expr));
9875 else
9876 gnu_expr = gnat_to_gnu (gnat_expr);
9878 /* Before assigning the element to the array, make sure it is
9879 in range. */
9880 if (Do_Range_Check (gnat_expr))
9881 gnu_expr = emit_range_check (gnu_expr, gnat_component_type, Empty);
9884 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec, gnu_index,
9885 convert (TREE_TYPE (gnu_array_type), gnu_expr));
9887 gnu_index = int_const_binop (PLUS_EXPR, gnu_index,
9888 convert (TREE_TYPE (gnu_index),
9889 integer_one_node));
9892 return gnat_build_constructor (gnu_array_type, gnu_expr_vec);
9895 /* Process a N_Validate_Unchecked_Conversion node. */
9897 static void
9898 validate_unchecked_conversion (Node_Id gnat_node)
9900 tree gnu_source_type = gnat_to_gnu_type (Source_Type (gnat_node));
9901 tree gnu_target_type = gnat_to_gnu_type (Target_Type (gnat_node));
9903 /* If the target is a pointer type, see if we are either converting from a
9904 non-pointer or from a pointer to a type with a different alias set and
9905 warn if so, unless the pointer has been marked to alias everything. */
9906 if (POINTER_TYPE_P (gnu_target_type)
9907 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type))
9909 tree gnu_source_desig_type = POINTER_TYPE_P (gnu_source_type)
9910 ? TREE_TYPE (gnu_source_type)
9911 : NULL_TREE;
9912 tree gnu_target_desig_type = TREE_TYPE (gnu_target_type);
9913 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9915 if (target_alias_set != 0
9916 && (!POINTER_TYPE_P (gnu_source_type)
9917 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9918 target_alias_set)))
9920 post_error_ne ("?possible aliasing problem for type&",
9921 gnat_node, Target_Type (gnat_node));
9922 post_error ("\\?use -fno-strict-aliasing switch for references",
9923 gnat_node);
9924 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
9925 gnat_node, Target_Type (gnat_node));
9929 /* Likewise if the target is a fat pointer type, but we have no mechanism to
9930 mitigate the problem in this case, so we unconditionally warn. */
9931 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type))
9933 tree gnu_source_desig_type
9934 = TYPE_IS_FAT_POINTER_P (gnu_source_type)
9935 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type)))
9936 : NULL_TREE;
9937 tree gnu_target_desig_type
9938 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type)));
9939 alias_set_type target_alias_set = get_alias_set (gnu_target_desig_type);
9941 if (target_alias_set != 0
9942 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type)
9943 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type),
9944 target_alias_set)))
9946 post_error_ne ("?possible aliasing problem for type&",
9947 gnat_node, Target_Type (gnat_node));
9948 post_error ("\\?use -fno-strict-aliasing switch for references",
9949 gnat_node);
9954 /* EXP is to be used in a context where access objects are implicitly
9955 dereferenced. Handle the cases when it is an access object. */
9957 static Node_Id
9958 adjust_for_implicit_deref (Node_Id exp)
9960 Entity_Id type = Underlying_Type (Etype (exp));
9962 /* Make sure the designated type is complete before dereferencing. */
9963 if (Is_Access_Type (type))
9964 gnat_to_gnu_entity (Designated_Type (type), NULL_TREE, false);
9966 return exp;
9969 /* EXP is to be treated as an array or record. Handle the cases when it is
9970 an access object and perform the required dereferences. */
9972 static tree
9973 maybe_implicit_deref (tree exp)
9975 /* If the type is a pointer, dereference it. */
9976 if (POINTER_TYPE_P (TREE_TYPE (exp))
9977 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp)))
9978 exp = build_unary_op (INDIRECT_REF, NULL_TREE, exp);
9980 /* If we got a padded type, remove it too. */
9981 if (TYPE_IS_PADDING_P (TREE_TYPE (exp)))
9982 exp = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp))), exp);
9984 return exp;
9987 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
9988 location and false if it doesn't. If CLEAR_COLUMN is true, set the column
9989 information to 0. */
9991 bool
9992 Sloc_to_locus (Source_Ptr Sloc, location_t *locus, bool clear_column)
9994 if (Sloc == No_Location)
9995 return false;
9997 if (Sloc <= Standard_Location)
9999 *locus = BUILTINS_LOCATION;
10000 return false;
10003 Source_File_Index file = Get_Source_File_Index (Sloc);
10004 Logical_Line_Number line = Get_Logical_Line_Number (Sloc);
10005 Column_Number column = (clear_column ? 0 : Get_Column_Number (Sloc));
10006 line_map_ordinary *map = LINEMAPS_ORDINARY_MAP_AT (line_table, file - 1);
10008 /* We can have zero if pragma Source_Reference is in effect. */
10009 if (line < 1)
10010 line = 1;
10012 /* Translate the location. */
10013 *locus
10014 = linemap_position_for_line_and_column (line_table, map, line, column);
10016 return true;
10019 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
10020 don't do anything if it doesn't correspond to a source location. And,
10021 if CLEAR_COLUMN is true, set the column information to 0. */
10023 static void
10024 set_expr_location_from_node (tree node, Node_Id gnat_node, bool clear_column)
10026 location_t locus;
10028 if (!Sloc_to_locus (Sloc (gnat_node), &locus, clear_column))
10029 return;
10031 SET_EXPR_LOCATION (node, locus);
10034 /* More elaborate version of set_expr_location_from_node to be used in more
10035 general contexts, for example the result of the translation of a generic
10036 GNAT node. */
10038 static void
10039 set_gnu_expr_location_from_node (tree node, Node_Id gnat_node)
10041 /* Set the location information on the node if it is a real expression.
10042 References can be reused for multiple GNAT nodes and they would get
10043 the location information of their last use. Also make sure not to
10044 overwrite an existing location as it is probably more precise. */
10046 switch (TREE_CODE (node))
10048 CASE_CONVERT:
10049 case NON_LVALUE_EXPR:
10050 case SAVE_EXPR:
10051 break;
10053 case COMPOUND_EXPR:
10054 if (EXPR_P (TREE_OPERAND (node, 1)))
10055 set_gnu_expr_location_from_node (TREE_OPERAND (node, 1), gnat_node);
10057 /* ... fall through ... */
10059 default:
10060 if (!REFERENCE_CLASS_P (node) && !EXPR_HAS_LOCATION (node))
10062 set_expr_location_from_node (node, gnat_node);
10063 set_end_locus_from_node (node, gnat_node);
10065 break;
10069 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
10070 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
10071 most sense. Return true if a sensible assignment was performed. */
10073 static bool
10074 set_end_locus_from_node (tree gnu_node, Node_Id gnat_node)
10076 Node_Id gnat_end_label;
10077 location_t end_locus;
10079 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
10080 end_locus when there is one. We consider only GNAT nodes with a possible
10081 End_Label attached. If the End_Label actually was unassigned, fallback
10082 on the original node. We'd better assign an explicit sloc associated with
10083 the outer construct in any case. */
10085 switch (Nkind (gnat_node))
10087 case N_Package_Body:
10088 case N_Subprogram_Body:
10089 case N_Block_Statement:
10090 gnat_end_label = End_Label (Handled_Statement_Sequence (gnat_node));
10091 break;
10093 case N_Package_Declaration:
10094 gnat_end_label = End_Label (Specification (gnat_node));
10095 break;
10097 default:
10098 return false;
10101 if (Present (gnat_end_label))
10102 gnat_node = gnat_end_label;
10104 /* Some expanded subprograms have neither an End_Label nor a Sloc
10105 attached. Notify that to callers. For a block statement with no
10106 End_Label, clear column information, so that the tree for a
10107 transient block does not receive the sloc of a source condition. */
10108 if (!Sloc_to_locus (Sloc (gnat_node), &end_locus,
10109 No (gnat_end_label)
10110 && (Nkind (gnat_node) == N_Block_Statement)))
10111 return false;
10113 switch (TREE_CODE (gnu_node))
10115 case BIND_EXPR:
10116 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node)) = end_locus;
10117 return true;
10119 case FUNCTION_DECL:
10120 DECL_STRUCT_FUNCTION (gnu_node)->function_end_locus = end_locus;
10121 return true;
10123 default:
10124 return false;
10128 /* Return a colon-separated list of encodings contained in encoded Ada
10129 name. */
10131 static const char *
10132 extract_encoding (const char *name)
10134 char *encoding = (char *) ggc_alloc_atomic (strlen (name));
10135 get_encoding (name, encoding);
10136 return encoding;
10139 /* Extract the Ada name from an encoded name. */
10141 static const char *
10142 decode_name (const char *name)
10144 char *decoded = (char *) ggc_alloc_atomic (strlen (name) * 2 + 60);
10145 __gnat_decode (name, decoded, 0);
10146 return decoded;
10149 /* Post an error message. MSG is the error message, properly annotated.
10150 NODE is the node at which to post the error and the node to use for the
10151 '&' substitution. */
10153 void
10154 post_error (const char *msg, Node_Id node)
10156 String_Template temp;
10157 String_Pointer sp;
10159 if (No (node))
10160 return;
10162 temp.Low_Bound = 1;
10163 temp.High_Bound = strlen (msg);
10164 sp.Bounds = &temp;
10165 sp.Array = msg;
10166 Error_Msg_N (sp, node);
10169 /* Similar to post_error, but NODE is the node at which to post the error and
10170 ENT is the node to use for the '&' substitution. */
10172 void
10173 post_error_ne (const char *msg, Node_Id node, Entity_Id ent)
10175 String_Template temp;
10176 String_Pointer sp;
10178 if (No (node))
10179 return;
10181 temp.Low_Bound = 1;
10182 temp.High_Bound = strlen (msg);
10183 sp.Bounds = &temp;
10184 sp.Array = msg;
10185 Error_Msg_NE (sp, node, ent);
10188 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
10190 void
10191 post_error_ne_num (const char *msg, Node_Id node, Entity_Id ent, int num)
10193 Error_Msg_Uint_1 = UI_From_Int (num);
10194 post_error_ne (msg, node, ent);
10197 /* Similar to post_error_ne, but T is a GCC tree representing the number to
10198 write. If T represents a constant, the text inside curly brackets in
10199 MSG will be output (presumably including a '^'). Otherwise it will not
10200 be output and the text inside square brackets will be output instead. */
10202 void
10203 post_error_ne_tree (const char *msg, Node_Id node, Entity_Id ent, tree t)
10205 char *new_msg = XALLOCAVEC (char, strlen (msg) + 1);
10206 char start_yes, end_yes, start_no, end_no;
10207 const char *p;
10208 char *q;
10210 if (TREE_CODE (t) == INTEGER_CST)
10212 Error_Msg_Uint_1 = UI_From_gnu (t);
10213 start_yes = '{', end_yes = '}', start_no = '[', end_no = ']';
10215 else
10216 start_yes = '[', end_yes = ']', start_no = '{', end_no = '}';
10218 for (p = msg, q = new_msg; *p; p++)
10220 if (*p == start_yes)
10221 for (p++; *p != end_yes; p++)
10222 *q++ = *p;
10223 else if (*p == start_no)
10224 for (p++; *p != end_no; p++)
10226 else
10227 *q++ = *p;
10230 *q = 0;
10232 post_error_ne (new_msg, node, ent);
10235 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
10237 void
10238 post_error_ne_tree_2 (const char *msg, Node_Id node, Entity_Id ent, tree t,
10239 int num)
10241 Error_Msg_Uint_2 = UI_From_Int (num);
10242 post_error_ne_tree (msg, node, ent, t);
10245 /* Return a label to branch to for the exception type in KIND or Empty
10246 if none. */
10248 Entity_Id
10249 get_exception_label (char kind)
10251 switch (kind)
10253 case N_Raise_Constraint_Error:
10254 return gnu_constraint_error_label_stack.last ();
10256 case N_Raise_Storage_Error:
10257 return gnu_storage_error_label_stack.last ();
10259 case N_Raise_Program_Error:
10260 return gnu_program_error_label_stack.last ();
10262 default:
10263 return Empty;
10266 gcc_unreachable ();
10269 /* Return the decl for the current elaboration procedure. */
10271 static tree
10272 get_elaboration_procedure (void)
10274 return gnu_elab_proc_stack->last ();
10277 /* Initialize the table that maps GNAT codes to GCC codes for simple
10278 binary and unary operations. */
10280 static void
10281 init_code_table (void)
10283 gnu_codes[N_Op_And] = TRUTH_AND_EXPR;
10284 gnu_codes[N_Op_Or] = TRUTH_OR_EXPR;
10285 gnu_codes[N_Op_Xor] = TRUTH_XOR_EXPR;
10286 gnu_codes[N_Op_Eq] = EQ_EXPR;
10287 gnu_codes[N_Op_Ne] = NE_EXPR;
10288 gnu_codes[N_Op_Lt] = LT_EXPR;
10289 gnu_codes[N_Op_Le] = LE_EXPR;
10290 gnu_codes[N_Op_Gt] = GT_EXPR;
10291 gnu_codes[N_Op_Ge] = GE_EXPR;
10292 gnu_codes[N_Op_Add] = PLUS_EXPR;
10293 gnu_codes[N_Op_Subtract] = MINUS_EXPR;
10294 gnu_codes[N_Op_Multiply] = MULT_EXPR;
10295 gnu_codes[N_Op_Mod] = FLOOR_MOD_EXPR;
10296 gnu_codes[N_Op_Rem] = TRUNC_MOD_EXPR;
10297 gnu_codes[N_Op_Minus] = NEGATE_EXPR;
10298 gnu_codes[N_Op_Abs] = ABS_EXPR;
10299 gnu_codes[N_Op_Not] = TRUTH_NOT_EXPR;
10300 gnu_codes[N_Op_Rotate_Left] = LROTATE_EXPR;
10301 gnu_codes[N_Op_Rotate_Right] = RROTATE_EXPR;
10302 gnu_codes[N_Op_Shift_Left] = LSHIFT_EXPR;
10303 gnu_codes[N_Op_Shift_Right] = RSHIFT_EXPR;
10304 gnu_codes[N_Op_Shift_Right_Arithmetic] = RSHIFT_EXPR;
10305 gnu_codes[N_And_Then] = TRUTH_ANDIF_EXPR;
10306 gnu_codes[N_Or_Else] = TRUTH_ORIF_EXPR;
10309 #include "gt-ada-trans.h"