1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2023, Free Software Foundation, Inc. *
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/>. *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 ****************************************************************************/
28 #include "coretypes.h"
33 #include "gimple-expr.h"
34 #include "stringpool.h"
37 #include "diagnostic.h"
39 #include "fold-const.h"
40 #include "stor-layout.h"
45 #include "libfuncs.h" /* For set_stack_check_libfunc. */
46 #include "tree-iterator.h"
49 #include "common/common-target.h"
50 #include "gomp-constants.h"
51 #include "stringpool.h"
53 #include "tree-nested.h"
56 #include "adadecode.h"
72 /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
73 for fear of running out of stack space. If we need more, we use xmalloc
75 #define ALLOCA_THRESHOLD 1000
77 /* Pointers to front-end tables accessed through macros. */
78 Node_Header
*Node_Offsets_Ptr
;
80 Node_Id
*Next_Node_Ptr
;
81 Node_Id
*Prev_Node_Ptr
;
82 struct Elist_Header
*Elists_Ptr
;
83 struct Elmt_Item
*Elmts_Ptr
;
84 struct String_Entry
*Strings_Ptr
;
85 Char_Code
*String_Chars_Ptr
;
86 struct List_Header
*List_Headers_Ptr
;
88 /* Highest number in the front-end node table. */
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 /* List of expressions of pragma Compile_Time_{Error|Warning} in the unit. */
100 static vec
<Node_Id
> gnat_compile_time_expr_list
;
102 /* When not optimizing, we cache the 'First, 'Last and 'Length attributes
103 of unconstrained array IN parameters to avoid emitting a great deal of
104 redundant instructions to recompute them each time. */
105 struct GTY (()) parm_attr_d
{
106 int id
; /* GTY doesn't like Entity_Id. */
113 typedef struct parm_attr_d
*parm_attr
;
115 /* Structure used to record information for a function. */
116 struct GTY(()) language_function
{
117 vec
<parm_attr
, va_gc
> *parm_attr_cache
;
118 bitmap named_ret_val
;
119 vec
<tree
, va_gc
> *other_ret_val
;
123 #define f_parm_attr_cache \
124 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
126 #define f_named_ret_val \
127 DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val
129 #define f_other_ret_val \
130 DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val
133 DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret
135 /* A structure used to gather together information about a statement group.
136 We use this to gather related statements, for example the "then" part
137 of a IF. In the case where it represents a lexical scope, we may also
138 have a BLOCK node corresponding to it and/or cleanups. */
140 struct GTY((chain_next ("%h.previous"))) stmt_group
{
141 struct stmt_group
*previous
; /* Previous code group. */
142 tree stmt_list
; /* List of statements for this code group. */
143 tree block
; /* BLOCK for this code group, if any. */
144 tree cleanups
; /* Cleanups for this code group, if any. */
147 static GTY(()) struct stmt_group
*current_stmt_group
;
149 /* List of unused struct stmt_group nodes. */
150 static GTY((deletable
)) struct stmt_group
*stmt_group_free_list
;
152 /* A structure used to record information on elaboration procedures
153 we've made and need to process.
155 ??? gnat_node should be Node_Id, but gengtype gets confused. */
157 struct GTY((chain_next ("%h.next"))) elab_info
{
158 struct elab_info
*next
; /* Pointer to next in chain. */
159 tree elab_proc
; /* Elaboration procedure. */
160 int gnat_node
; /* The N_Compilation_Unit. */
163 static GTY(()) struct elab_info
*elab_info_list
;
165 /* Stack of exception pointer variables. Each entry is the VAR_DECL
166 that stores the address of the raised exception. Nonzero means we
167 are in an exception handler. Not used in the zero-cost case. */
168 static GTY(()) vec
<tree
, va_gc
> *gnu_except_ptr_stack
;
170 /* In ZCX case, current exception pointer. Used to re-raise it. */
171 static GTY(()) tree gnu_incoming_exc_ptr
;
173 /* Stack for storing the current elaboration procedure decl. */
174 static GTY(()) vec
<tree
, va_gc
> *gnu_elab_proc_stack
;
176 /* Stack of labels to be used as a goto target instead of a return in
177 some functions. See processing for N_Subprogram_Body. */
178 static GTY(()) vec
<tree
, va_gc
> *gnu_return_label_stack
;
180 /* Stack of variable for the return value of a function with copy-in/copy-out
181 parameters. See processing for N_Subprogram_Body. */
182 static GTY(()) vec
<tree
, va_gc
> *gnu_return_var_stack
;
184 /* Structure used to record information for a range check. */
185 struct GTY(()) range_check_info_d
{
195 typedef struct range_check_info_d
*range_check_info
;
197 /* Structure used to record information for a loop. */
198 struct GTY(()) loop_info_d
{
204 tree omp_loop_clauses
;
205 tree omp_construct_clauses
;
206 enum tree_code omp_code
;
207 vec
<range_check_info
, va_gc
> *checks
;
208 vec
<tree
, va_gc
> *invariants
;
211 typedef struct loop_info_d
*loop_info
;
213 /* Stack of loop_info structures associated with LOOP_STMT nodes. */
214 static GTY(()) vec
<loop_info
, va_gc
> *gnu_loop_stack
;
216 /* The stacks for N_{Push,Pop}_*_Label. */
217 static vec
<Entity_Id
> gnu_constraint_error_label_stack
;
218 static vec
<Entity_Id
> gnu_storage_error_label_stack
;
219 static vec
<Entity_Id
> gnu_program_error_label_stack
;
221 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
222 static enum tree_code gnu_codes
[Number_Node_Kinds
];
224 static void init_code_table (void);
225 static tree
get_elaboration_procedure (void);
226 static void Compilation_Unit_to_gnu (Node_Id
);
227 static bool empty_stmt_list_p (tree
);
228 static void record_code_position (Node_Id
);
229 static void insert_code_for (Node_Id
);
230 static void add_cleanup (tree
, Node_Id
);
231 static void add_stmt_list (List_Id
);
232 static tree
build_stmt_group (List_Id
, bool);
233 static inline bool stmt_group_may_fallthru (void);
234 static enum gimplify_status
gnat_gimplify_stmt (tree
*);
235 static void elaborate_all_entities (Node_Id
);
236 static void process_freeze_entity (Node_Id
);
237 static void process_decls (List_Id
, List_Id
, bool, bool);
238 static tree
emit_check (tree
, tree
, int, Node_Id
);
239 static tree
build_unary_op_trapv (enum tree_code
, tree
, tree
, Node_Id
);
240 static tree
build_binary_op_trapv (enum tree_code
, tree
, tree
, tree
, Node_Id
);
241 static tree
convert_with_check (Entity_Id
, tree
, bool, bool, Node_Id
);
242 static bool addressable_p (tree
, tree
);
243 static tree
assoc_to_constructor (Entity_Id
, Node_Id
, tree
);
244 static tree
pos_to_constructor (Node_Id
, tree
);
245 static void validate_unchecked_conversion (Node_Id
);
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);
250 static tree
build_raise_check (int, enum exception_info_kind
);
251 static tree
create_init_temporary (const char *, tree
, tree
*, Node_Id
);
252 static bool maybe_make_gnu_thunk (Entity_Id gnat_thunk
, tree gnu_thunk
);
254 /* This makes gigi's file_info_ptr visible in this translation unit,
255 so that Sloc_to_locus can look it up when deciding whether to map
256 decls to instances. */
258 static struct File_Info_Type
*file_map
;
260 /* Return the string of the identifier allocated for the file name Id. */
263 File_Name_to_gnu (Name_Id Id
)
265 /* __gnat_to_canonical_file_spec translates file names from pragmas
266 Source_Reference that contain host style syntax not understood by GDB. */
267 const char *name
= __gnat_to_canonical_file_spec (Get_Name_String (Id
));
269 /* Use the identifier table to make a permanent copy of the file name as
270 the name table gets reallocated after Gigi returns but before all the
271 debugging information is output. */
272 return IDENTIFIER_POINTER (get_identifier (name
));
275 /* This is the main program of the back-end. It sets up all the table
276 structures and then generates code. */
279 gigi (Node_Id gnat_root
,
281 int number_name ATTRIBUTE_UNUSED
,
282 Node_Header
*node_offsets_ptr
,
284 Node_Id
*next_node_ptr
,
285 Node_Id
*prev_node_ptr
,
286 struct Elist_Header
*elists_ptr
,
287 struct Elmt_Item
*elmts_ptr
,
288 struct String_Entry
*strings_ptr
,
289 Char_Code
*string_chars_ptr
,
290 struct List_Header
*list_headers_ptr
,
292 struct File_Info_Type
*file_info_ptr
,
293 Entity_Id standard_address
,
294 Entity_Id standard_boolean
,
295 Entity_Id standard_character
,
296 Entity_Id standard_exception_type
,
297 Entity_Id standard_integer
,
298 Entity_Id standard_long_long_float
,
299 Int gigi_operating_mode
)
302 Entity_Id gnat_literal
;
303 tree t
, ftype
, int64_type
;
304 struct elab_info
*info
;
307 max_gnat_nodes
= max_gnat_node
;
309 Node_Offsets_Ptr
= node_offsets_ptr
;
310 Slots_Ptr
= slots_ptr
;
311 Next_Node_Ptr
= next_node_ptr
;
312 Prev_Node_Ptr
= prev_node_ptr
;
313 Elists_Ptr
= elists_ptr
;
314 Elmts_Ptr
= elmts_ptr
;
315 Strings_Ptr
= strings_ptr
;
316 String_Chars_Ptr
= string_chars_ptr
;
317 List_Headers_Ptr
= list_headers_ptr
;
319 type_annotate_only
= (gigi_operating_mode
== 1);
321 if (Generate_SCO_Instance_Table
!= 0)
323 file_map
= file_info_ptr
;
324 maybe_create_decl_to_instance_map (number_file
);
327 for (i
= 0; i
< number_file
; i
++)
329 /* We rely on the order isomorphism between files and line maps. */
330 if ((int) LINEMAPS_ORDINARY_USED (line_table
) != i
)
333 error ("%s contains too many lines",
334 File_Name_to_gnu (file_info_ptr
[i
- 1].File_Name
));
337 /* We create the line map for a source file at once, with a fixed number
338 of columns chosen to avoid jumping over the next power of 2. */
339 linemap_add (line_table
, LC_ENTER
, 0,
340 File_Name_to_gnu (file_info_ptr
[i
].File_Name
), 1);
341 linemap_line_start (line_table
, file_info_ptr
[i
].Num_Source_Lines
, 252);
342 linemap_position_for_column (line_table
, 252 - 1);
343 linemap_add (line_table
, LC_LEAVE
, 0, NULL
, 0);
346 gcc_assert (Nkind (gnat_root
) == N_Compilation_Unit
);
348 /* Declare the name of the compilation unit as the first global
349 name in order to make the middle-end fully deterministic. */
350 t
= create_concat_name (Defining_Entity (Unit (gnat_root
)), NULL
);
351 first_global_object_name
= ggc_strdup (IDENTIFIER_POINTER (t
));
353 /* Initialize ourselves. */
358 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
360 if (type_annotate_only
)
362 TYPE_SIZE (void_type_node
) = bitsize_zero_node
;
363 TYPE_SIZE_UNIT (void_type_node
) = size_zero_node
;
366 /* Enable GNAT stack checking method if needed */
367 if (!Stack_Check_Probes_On_Target
)
369 set_stack_check_libfunc ("__gnat_stack_check");
370 if (flag_stack_check
!= NO_STACK_CHECK
)
371 Check_Restriction_No_Dependence_On_System (Name_Stack_Checking
,
375 /* Retrieve alignment settings. */
376 double_float_alignment
= get_target_double_float_alignment ();
377 double_scalar_alignment
= get_target_double_scalar_alignment ();
379 /* Record the builtin types. */
380 record_builtin_type ("address", pointer_sized_int_node
, false);
381 record_builtin_type ("integer", integer_type_node
, false);
382 record_builtin_type ("character", char_type_node
, false);
383 record_builtin_type ("boolean", boolean_type_node
, false);
384 record_builtin_type ("void", void_type_node
, false);
386 /* Save the type we made for address as the type for Standard.Address. */
387 save_gnu_tree (Base_Type (standard_address
),
388 TYPE_NAME (pointer_sized_int_node
),
391 /* Likewise for integer as the type for Standard.Integer. */
392 save_gnu_tree (Base_Type (standard_integer
),
393 TYPE_NAME (integer_type_node
),
396 /* Likewise for character as the type for Standard.Character. */
397 finish_character_type (char_type_node
);
398 save_gnu_tree (Base_Type (standard_character
),
399 TYPE_NAME (char_type_node
),
402 /* Likewise for boolean as the type for Standard.Boolean. */
403 save_gnu_tree (Base_Type (standard_boolean
),
404 TYPE_NAME (boolean_type_node
),
406 gnat_literal
= First_Literal (Base_Type (standard_boolean
));
407 t
= UI_To_gnu (Enumeration_Rep (gnat_literal
), boolean_type_node
);
408 gcc_assert (t
== boolean_false_node
);
409 t
= create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
410 boolean_type_node
, t
, true, false, false, false, false,
411 true, false, NULL
, gnat_literal
);
412 save_gnu_tree (gnat_literal
, t
, false);
413 gnat_literal
= Next_Literal (gnat_literal
);
414 t
= UI_To_gnu (Enumeration_Rep (gnat_literal
), boolean_type_node
);
415 gcc_assert (t
== boolean_true_node
);
416 t
= create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
417 boolean_type_node
, t
, true, false, false, false, false,
418 true, false, NULL
, gnat_literal
);
419 save_gnu_tree (gnat_literal
, t
, false);
421 /* Declare the building blocks of function nodes. */
422 void_ftype
= build_function_type_list (void_type_node
, NULL_TREE
);
423 ptr_void_ftype
= build_pointer_type (void_ftype
);
425 /* Now declare run-time functions. */
427 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE
,
428 build_function_type_list (ptr_type_node
, sizetype
,
430 NULL_TREE
, is_default
, true, true, true, false,
432 DECL_IS_MALLOC (malloc_decl
) = 1;
435 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE
,
436 build_function_type_list (void_type_node
,
437 ptr_type_node
, NULL_TREE
),
438 NULL_TREE
, is_default
, true, true, true, false,
442 = create_subprog_decl (get_identifier ("__gnat_realloc"), NULL_TREE
,
443 build_function_type_list (ptr_type_node
,
444 ptr_type_node
, sizetype
,
446 NULL_TREE
, is_default
, true, true, true, false,
449 /* This is used for 64-bit multiplication with overflow checking. */
450 int64_type
= gnat_type_for_size (64, 0);
452 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE
,
453 build_function_type_list (int64_type
, int64_type
,
454 int64_type
, NULL_TREE
),
455 NULL_TREE
, is_default
, true, true, true, false,
458 if (Enable_128bit_Types
)
460 tree int128_type
= gnat_type_for_size (128, 0);
462 = create_subprog_decl (get_identifier ("__gnat_mulv128"), NULL_TREE
,
463 build_function_type_list (int128_type
,
467 NULL_TREE
, is_default
, true, true, true, false,
471 /* Name of the _Parent field in tagged record types. */
472 parent_name_id
= get_identifier (Get_Name_String (Name_uParent
));
474 /* Name of the Not_Handled_By_Others field in exception record types. */
475 not_handled_by_others_name_id
= get_identifier ("not_handled_by_others");
477 /* Make the types and functions used for exception processing. */
478 except_type_node
= gnat_to_gnu_type (Base_Type (standard_exception_type
));
480 set_exception_parameter_decl
481 = create_subprog_decl
482 (get_identifier ("__gnat_set_exception_parameter"), NULL_TREE
,
483 build_function_type_list (void_type_node
, ptr_type_node
, ptr_type_node
,
485 NULL_TREE
, is_default
, true, true, true, false, false, NULL
, Empty
);
487 /* Hooks to call when entering/leaving an exception handler. */
488 ftype
= build_function_type_list (ptr_type_node
,
489 ptr_type_node
, NULL_TREE
);
491 = create_subprog_decl (get_identifier ("__gnat_begin_handler_v1"),
492 NULL_TREE
, ftype
, NULL_TREE
,
493 is_default
, true, true, true, false, false, NULL
,
495 /* __gnat_begin_handler_v1 is not a dummy procedure, but we arrange
496 for it not to throw. */
497 TREE_NOTHROW (begin_handler_decl
) = 1;
499 ftype
= build_function_type_list (ptr_type_node
,
500 ptr_type_node
, ptr_type_node
,
501 ptr_type_node
, NULL_TREE
);
503 = create_subprog_decl (get_identifier ("__gnat_end_handler_v1"), NULL_TREE
,
505 is_default
, true, true, true, false, false, NULL
,
508 ftype
= build_function_type_list (void_type_node
, ptr_type_node
, NULL_TREE
);
509 unhandled_except_decl
510 = create_subprog_decl (get_identifier ("__gnat_unhandled_except_handler"),
511 NULL_TREE
, ftype
, NULL_TREE
,
512 is_default
, true, true, true, false, false, NULL
,
515 /* Indicate that it never returns. */
516 ftype
= build_qualified_type (ftype
, TYPE_QUAL_VOLATILE
);
518 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE
,
520 is_default
, true, true, true, false, false, NULL
,
523 /* Dummy objects to materialize "others" and "all others" in the exception
524 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
527 = create_var_decl (get_identifier ("OTHERS"),
528 get_identifier ("__gnat_others_value"),
529 char_type_node
, NULL_TREE
,
530 true, false, true, false, false, true, false,
534 = create_var_decl (get_identifier ("ALL_OTHERS"),
535 get_identifier ("__gnat_all_others_value"),
536 char_type_node
, NULL_TREE
,
537 true, false, true, false, false, true, false,
540 unhandled_others_decl
541 = create_var_decl (get_identifier ("UNHANDLED_OTHERS"),
542 get_identifier ("__gnat_unhandled_others_value"),
543 char_type_node
, NULL_TREE
,
544 true, false, true, false, false, true, false,
547 /* If in no exception handlers mode, all raise statements are redirected to
548 __gnat_last_chance_handler. */
549 if (No_Exception_Handlers_Set ())
551 /* Indicate that it never returns. */
552 ftype
= build_function_type_list (void_type_node
,
553 build_pointer_type (char_type_node
),
554 integer_type_node
, NULL_TREE
);
555 ftype
= build_qualified_type (ftype
, TYPE_QUAL_VOLATILE
);
557 = create_subprog_decl
558 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE
, ftype
,
559 NULL_TREE
, is_default
, true, true, true, false, false, NULL
,
561 for (i
= 0; i
< (int) ARRAY_SIZE (gnat_raise_decls
); i
++)
562 gnat_raise_decls
[i
] = decl
;
566 /* Otherwise, make one decl for each exception reason. */
567 for (i
= 0; i
< (int) ARRAY_SIZE (gnat_raise_decls
); i
++)
568 gnat_raise_decls
[i
] = build_raise_check (i
, exception_simple
);
569 for (i
= 0; i
< (int) ARRAY_SIZE (gnat_raise_decls_ext
); i
++)
570 gnat_raise_decls_ext
[i
]
571 = build_raise_check (i
,
572 i
== CE_Index_Check_Failed
573 || i
== CE_Range_Check_Failed
574 || i
== CE_Invalid_Data
575 ? exception_range
: exception_column
);
578 /* Build the special descriptor type and its null node if needed. */
579 if (TARGET_VTABLE_USES_DESCRIPTORS
)
581 tree null_node
= fold_convert (ptr_void_ftype
, null_pointer_node
);
582 tree field_list
= NULL_TREE
;
584 vec
<constructor_elt
, va_gc
> *null_vec
= NULL
;
585 constructor_elt
*elt
;
587 fdesc_type_node
= make_node (RECORD_TYPE
);
588 vec_safe_grow (null_vec
, TARGET_VTABLE_USES_DESCRIPTORS
, true);
589 elt
= (null_vec
->address () + TARGET_VTABLE_USES_DESCRIPTORS
- 1);
591 for (j
= 0; j
< TARGET_VTABLE_USES_DESCRIPTORS
; j
++)
594 = create_field_decl (NULL_TREE
, ptr_void_ftype
, fdesc_type_node
,
595 NULL_TREE
, NULL_TREE
, 0, 1);
596 DECL_CHAIN (field
) = field_list
;
599 elt
->value
= null_node
;
603 finish_record_type (fdesc_type_node
, nreverse (field_list
), 0, false);
604 record_builtin_type ("descriptor", fdesc_type_node
, true);
605 null_fdesc_node
= gnat_build_constructor (fdesc_type_node
, null_vec
);
608 longest_float_type_node
609 = get_unpadded_type (Base_Type (standard_long_long_float
));
611 main_identifier_node
= get_identifier ("main");
615 /* Initialize the GCC support for FP operations. */
618 /* Install the builtins we might need, either internally or as user-available
619 facilities for Intrinsic imports. Note that this must be done after the
620 GCC exception mechanism is initialized. */
621 gnat_install_builtins ();
623 vec_safe_push (gnu_except_ptr_stack
, NULL_TREE
);
625 gnu_constraint_error_label_stack
.safe_push (Empty
);
626 gnu_storage_error_label_stack
.safe_push (Empty
);
627 gnu_program_error_label_stack
.safe_push (Empty
);
629 /* Process any Pragma Ident for the main unit. */
630 if (Present (Ident_String (Main_Unit
)))
631 targetm
.asm_out
.output_ident
632 (TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit
))));
634 /* Force -fno-strict-aliasing if the configuration pragma was seen. */
635 if (No_Strict_Aliasing_CP
)
636 flag_strict_aliasing
= 0;
638 /* Save the current optimization options again after the above possible
639 global_options changes. */
640 optimization_default_node
641 = build_optimization_node (&global_options
, &global_options_set
);
642 optimization_current_node
= optimization_default_node
;
644 /* Now translate the compilation unit proper. */
645 Compilation_Unit_to_gnu (gnat_root
);
647 /* Then process the N_Validate_Unchecked_Conversion nodes. We do this at
648 the very end to avoid having to second-guess the front-end when we run
649 into dummy nodes during the regular processing. */
650 for (i
= 0; gnat_validate_uc_list
.iterate (i
, &gnat_iter
); i
++)
651 validate_unchecked_conversion (gnat_iter
);
652 gnat_validate_uc_list
.release ();
654 /* Finally see if we have any elaboration procedures to deal with. */
655 for (info
= elab_info_list
; info
; info
= info
->next
)
657 tree gnu_body
= DECL_SAVED_TREE (info
->elab_proc
);
659 /* We should have a BIND_EXPR but it may not have any statements in it.
660 If it doesn't have any, we have nothing to do except for setting the
661 flag on the GNAT node. Otherwise, process the function as others. */
662 tree gnu_stmts
= gnu_body
;
663 if (TREE_CODE (gnu_stmts
) == BIND_EXPR
)
664 gnu_stmts
= BIND_EXPR_BODY (gnu_stmts
);
665 if (!gnu_stmts
|| empty_stmt_list_p (gnu_stmts
))
666 Set_Has_No_Elaboration_Code (info
->gnat_node
, 1);
669 begin_subprog_body (info
->elab_proc
);
670 end_subprog_body (gnu_body
);
671 rest_of_subprog_body_compilation (info
->elab_proc
);
675 /* Destroy ourselves. */
677 destroy_gnat_decl ();
678 destroy_gnat_utils ();
680 /* We cannot track the location of errors past this point. */
681 Current_Error_Node
= Empty
;
684 /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given
685 CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */
688 build_raise_check (int check
, enum exception_info_kind kind
)
691 const char pfx
[] = "__gnat_rcheck_";
693 strcpy (Name_Buffer
, pfx
);
694 Name_Len
= sizeof (pfx
) - 1;
695 Get_RT_Exception_Name ((enum RT_Exception_Code
) check
);
697 if (kind
== exception_simple
)
699 Name_Buffer
[Name_Len
] = 0;
701 = build_function_type_list (void_type_node
,
702 build_pointer_type (char_type_node
),
703 integer_type_node
, NULL_TREE
);
707 tree t
= (kind
== exception_column
? NULL_TREE
: integer_type_node
);
709 strcpy (Name_Buffer
+ Name_Len
, "_ext");
710 Name_Buffer
[Name_Len
+ 4] = 0;
712 = build_function_type_list (void_type_node
,
713 build_pointer_type (char_type_node
),
714 integer_type_node
, integer_type_node
,
718 /* Indicate that it never returns. */
719 ftype
= build_qualified_type (ftype
, TYPE_QUAL_VOLATILE
);
721 = create_subprog_decl (get_identifier (Name_Buffer
), NULL_TREE
, ftype
,
722 NULL_TREE
, is_default
, true, true, true, false,
728 /* Return a positive value if an lvalue is required for GNAT_NODE, which is
729 an N_Attribute_Reference. */
732 lvalue_required_for_attribute_p (Node_Id gnat_node
)
734 switch (Get_Attribute_Id (Attribute_Name (gnat_node
)))
740 case Attr_Range_Length
:
742 case Attr_Object_Size
:
744 case Attr_Value_Size
:
745 case Attr_Component_Size
:
746 case Attr_Descriptor_Size
:
747 case Attr_Max_Size_In_Storage_Elements
:
750 case Attr_Null_Parameter
:
751 case Attr_Passed_By_Reference
:
752 case Attr_Mechanism_Code
:
759 case Attr_Unchecked_Access
:
760 case Attr_Unrestricted_Access
:
761 case Attr_Code_Address
:
762 case Attr_Pool_Address
:
764 case Attr_Bit_Position
:
770 case Attr_Asm_Output
:
776 /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE
777 is the type that will be used for GNAT_NODE in the translated GNU tree.
778 CONSTANT indicates whether the underlying object represented by GNAT_NODE
779 is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates
780 whether its value is the address of another constant. If it isn't, then
781 ADDRESS_OF_CONSTANT is ignored.
783 The function climbs up the GNAT tree starting from the node and returns 1
784 upon encountering a node that effectively requires an lvalue downstream.
785 It returns int instead of bool to facilitate usage in non-purely binary
789 lvalue_required_p (Node_Id gnat_node
, tree gnu_type
, bool constant
,
790 bool address_of_constant
)
792 Node_Id gnat_parent
= Parent (gnat_node
), gnat_temp
;
794 switch (Nkind (gnat_parent
))
799 case N_Attribute_Reference
:
800 return lvalue_required_for_attribute_p (gnat_parent
);
802 case N_Parameter_Association
:
803 case N_Function_Call
:
804 case N_Procedure_Call_Statement
:
805 /* If the parameter is by reference, an lvalue is required. */
807 || must_pass_by_ref (gnu_type
)
808 || default_pass_by_ref (gnu_type
));
810 case N_Pragma_Argument_Association
:
811 return lvalue_required_p (gnat_parent
, gnu_type
, constant
,
812 address_of_constant
);
815 if (Is_Pragma_Name (Chars (Pragma_Identifier (gnat_parent
))))
818 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_parent
)));
819 return id
== Pragma_Inspection_Point
;
824 case N_Indexed_Component
:
825 /* Only the array expression can require an lvalue. */
826 if (Prefix (gnat_parent
) != gnat_node
)
829 /* ??? Consider that referencing an indexed component with a variable
830 index forces the whole aggregate to memory. Note that testing only
831 for literals is conservative, any static expression in the RM sense
832 could probably be accepted with some additional work. */
833 for (gnat_temp
= First (Expressions (gnat_parent
));
835 gnat_temp
= Next (gnat_temp
))
836 if (Nkind (gnat_temp
) != N_Character_Literal
837 && Nkind (gnat_temp
) != N_Integer_Literal
838 && !(Is_Entity_Name (gnat_temp
)
839 && Ekind (Entity (gnat_temp
)) == E_Enumeration_Literal
))
842 /* ... fall through ... */
844 case N_Selected_Component
:
846 /* Only the prefix expression can require an lvalue. */
847 if (Prefix (gnat_parent
) != gnat_node
)
850 return lvalue_required_p (gnat_parent
,
851 get_unpadded_type (Etype (gnat_parent
)),
852 constant
, address_of_constant
);
854 case N_Object_Renaming_Declaration
:
855 /* We need to preserve addresses through a renaming. */
858 case N_Object_Declaration
:
859 /* We cannot use a constructor if this is an atomic object because
860 the actual assignment might end up being done component-wise. */
862 ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node
)))
863 && Is_Full_Access (Defining_Entity (gnat_parent
)))
864 /* We don't use a constructor if this is a class-wide object
865 because the effective type of the object is the equivalent
866 type of the class-wide subtype and it smashes most of the
867 data into an array of bytes to which we cannot convert. */
868 || Ekind ((Etype (Defining_Entity (gnat_parent
))))
869 == E_Class_Wide_Subtype
);
871 case N_Assignment_Statement
:
872 /* We cannot use a constructor if the LHS is an atomic object because
873 the actual assignment might end up being done component-wise. */
875 || Name (gnat_parent
) == gnat_node
876 || (Is_Composite_Type (Underlying_Type (Etype (gnat_node
)))
877 && Is_Entity_Name (Name (gnat_parent
))
878 && Is_Full_Access (Entity (Name (gnat_parent
)))));
880 case N_Unchecked_Type_Conversion
:
884 /* ... fall through ... */
886 case N_Type_Conversion
:
887 case N_Qualified_Expression
:
888 /* We must look through all conversions because we may need to bypass
889 an intermediate conversion that is meant to be purely formal. */
890 return lvalue_required_p (gnat_parent
,
891 get_unpadded_type (Etype (gnat_parent
)),
892 constant
, address_of_constant
);
894 case N_Explicit_Dereference
:
895 /* We look through dereferences for address of constant because we need
896 to handle the special cases listed above. */
897 if (constant
&& address_of_constant
)
898 return lvalue_required_p (gnat_parent
,
899 get_unpadded_type (Etype (gnat_parent
)),
902 /* ... fall through ... */
911 /* Return true if an lvalue should be used for GNAT_NODE. GNU_TYPE is the type
912 that will be used for GNAT_NODE in the translated GNU tree and is assumed to
913 be an aggregate type.
915 The function climbs up the GNAT tree starting from the node and returns true
916 upon encountering a node that makes it doable to decide. lvalue_required_p
917 should have been previously invoked on the arguments and returned false. */
920 lvalue_for_aggregate_p (Node_Id gnat_node
, tree gnu_type
)
922 Node_Id gnat_parent
= Parent (gnat_node
);
924 switch (Nkind (gnat_parent
))
926 case N_Parameter_Association
:
927 case N_Function_Call
:
928 case N_Procedure_Call_Statement
:
929 /* Even if the parameter is by copy, prefer an lvalue. */
932 case N_Simple_Return_Statement
:
933 /* Likewise for a return value. */
936 case N_Indexed_Component
:
937 case N_Selected_Component
:
938 /* If an elementary component is used, take it from the constant. */
939 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_parent
))))
942 /* ... fall through ... */
945 return lvalue_for_aggregate_p (gnat_parent
,
946 get_unpadded_type (Etype (gnat_parent
)));
948 case N_Object_Declaration
:
949 /* For an aggregate object declaration, return false consistently. */
952 case N_Assignment_Statement
:
953 /* For an aggregate assignment, decide based on the size. */
955 const HOST_WIDE_INT size
= int_size_in_bytes (gnu_type
);
956 return size
< 0 || size
>= param_large_stack_frame
/ 4;
959 case N_Unchecked_Type_Conversion
:
960 case N_Type_Conversion
:
961 case N_Qualified_Expression
:
962 return lvalue_for_aggregate_p (gnat_parent
,
963 get_unpadded_type (Etype (gnat_parent
)));
966 /* We should only reach here through the N_Qualified_Expression case.
967 Force an lvalue for aggregate types since a block-copy to the newly
968 allocated area of memory is made. */
979 /* Return true if T is a constant DECL node that can be safely replaced
980 by its initializer. */
983 constant_decl_with_initializer_p (tree t
)
985 if (!TREE_CONSTANT (t
) || !DECL_P (t
) || !DECL_INITIAL (t
))
988 /* Return false for aggregate types that contain a placeholder since
989 their initializers cannot be manipulated easily. */
990 if (AGGREGATE_TYPE_P (TREE_TYPE (t
))
991 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (t
))
992 && type_contains_placeholder_p (TREE_TYPE (t
)))
998 /* Return an expression equivalent to EXP but where constant DECL nodes
999 have been replaced by their initializer. */
1002 fold_constant_decl_in_expr (tree exp
)
1004 enum tree_code code
= TREE_CODE (exp
);
1011 if (!constant_decl_with_initializer_p (exp
))
1014 return DECL_INITIAL (exp
);
1017 op0
= fold_constant_decl_in_expr (TREE_OPERAND (exp
, 0));
1018 if (op0
== TREE_OPERAND (exp
, 0))
1021 return fold_build3 (COMPONENT_REF
, TREE_TYPE (exp
), op0
,
1022 TREE_OPERAND (exp
, 1), NULL_TREE
);
1025 op0
= fold_constant_decl_in_expr (TREE_OPERAND (exp
, 0));
1026 if (op0
== TREE_OPERAND (exp
, 0))
1029 return fold_build3 (BIT_FIELD_REF
, TREE_TYPE (exp
), op0
,
1030 TREE_OPERAND (exp
, 1), TREE_OPERAND (exp
, 2));
1033 case ARRAY_RANGE_REF
:
1034 /* If the index is not itself constant, then nothing can be folded. */
1035 if (!TREE_CONSTANT (TREE_OPERAND (exp
, 1)))
1037 op0
= fold_constant_decl_in_expr (TREE_OPERAND (exp
, 0));
1038 if (op0
== TREE_OPERAND (exp
, 0))
1041 return fold (build4 (code
, TREE_TYPE (exp
), op0
, TREE_OPERAND (exp
, 1),
1042 TREE_OPERAND (exp
, 2), TREE_OPERAND (exp
, 3)));
1046 case VIEW_CONVERT_EXPR
:
1047 op0
= fold_constant_decl_in_expr (TREE_OPERAND (exp
, 0));
1048 if (op0
== TREE_OPERAND (exp
, 0))
1051 return fold_build1 (code
, TREE_TYPE (exp
), op0
);
1060 /* Return true if TYPE and DEF_TYPE are compatible GNAT types for Gigi. */
1063 Gigi_Types_Compatible (Entity_Id type
, Entity_Id def_type
)
1065 /* The trivial case. */
1066 if (type
== def_type
)
1069 /* A class-wide type is equivalent to a subtype of itself. */
1070 if (Is_Class_Wide_Type (type
))
1073 /* A packed array type is compatible with its implementation type. */
1074 if (Is_Packed (def_type
) && type
== Packed_Array_Impl_Type (def_type
))
1077 /* If both types are Itypes, one may be a copy of the other. */
1078 if (Is_Itype (def_type
) && Is_Itype (type
))
1081 /* If the type is incomplete and comes from a limited context, then also
1082 consider its non-limited view. */
1083 if (Is_Incomplete_Type (def_type
)
1084 && From_Limited_With (def_type
)
1085 && Present (Non_Limited_View (def_type
)))
1086 return Gigi_Types_Compatible (type
, Non_Limited_View (def_type
));
1088 /* If the type is incomplete/private, then also consider its full view. */
1089 if (Is_Incomplete_Or_Private_Type (def_type
)
1090 && Present (Full_View (def_type
)))
1091 return Gigi_Types_Compatible (type
, Full_View (def_type
));
1096 /* Return the full view of a private constant E, or of a renaming thereof, if
1097 its type has discriminants, and Empty otherwise. */
1100 Full_View_Of_Private_Constant (Entity_Id E
)
1102 while (Present (Renamed_Object (E
)) && Is_Entity_Name (Renamed_Object (E
)))
1103 E
= Entity (Renamed_Object (E
));
1105 if (Ekind (E
) != E_Constant
|| No (Full_View (E
)))
1108 const Entity_Id T
= Etype (E
);
1110 if (Is_Private_Type (T
)
1111 && (Has_Unknown_Discriminants (T
)
1112 || (Present (Full_View (T
)) && Has_Discriminants (Full_View (T
)))))
1113 return Full_View (E
);
1118 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Identifier, to a GCC
1119 tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we should
1120 place the result type. */
1123 Identifier_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
)
1125 Entity_Id gnat_entity
= (Nkind (gnat_node
) == N_Defining_Identifier
1126 || Nkind (gnat_node
) == N_Defining_Operator_Symbol
)
1127 ? gnat_node
: Entity (gnat_node
);
1128 Entity_Id gnat_result_type
;
1129 tree gnu_result
, gnu_result_type
;
1130 /* If GNAT_NODE is a constant, whether we should use the initialization
1131 value instead of the constant entity, typically for scalars with an
1132 address clause when the parent doesn't require an lvalue. */
1133 bool use_constant_initializer
;
1134 /* Whether we should require an lvalue for GNAT_NODE. Needed in
1135 specific circumstances only, so evaluated lazily. < 0 means
1136 unknown, > 0 means known true, 0 means known false. */
1139 /* If the Etype of this node is not the same as that of the Entity, then
1140 something went wrong, probably in generic instantiation. However, this
1141 does not apply to types. Since we sometime have strange Ekind's, just
1142 do this test for objects, except for discriminants because their type
1143 may have been changed to a subtype by Exp_Ch3.Adjust_Discriminants. */
1144 gcc_assert (!Is_Object (gnat_entity
)
1145 || Ekind (gnat_entity
) == E_Discriminant
1146 || Etype (gnat_node
) == Etype (gnat_entity
)
1147 || Gigi_Types_Compatible (Etype (gnat_node
),
1148 Etype (gnat_entity
)));
1150 /* If this is a reference to a deferred constant whose partial view is of
1151 unconstrained private type, the proper type is on the full view of the
1152 constant, not on the full view of the type which may be unconstrained. */
1153 const Entity_Id gnat_full_view
= Full_View_Of_Private_Constant (gnat_entity
);
1154 if (Present (gnat_full_view
))
1156 gnat_entity
= gnat_full_view
;
1157 gnat_result_type
= Etype (gnat_entity
);
1161 /* We use the Actual_Subtype only if it has already been elaborated,
1162 as we may be invoked precisely during its elaboration, otherwise
1163 the Etype. Avoid using it for packed arrays to simplify things,
1164 except in a return statement because we need the actual size and
1165 the front-end does not make it explicit in this case. */
1166 if ((Ekind (gnat_entity
) == E_Constant
1167 || Ekind (gnat_entity
) == E_Variable
1168 || Is_Formal (gnat_entity
))
1169 && !(Is_Array_Type (Etype (gnat_entity
))
1170 && Present (Packed_Array_Impl_Type (Etype (gnat_entity
)))
1171 && Nkind (Parent (gnat_node
)) != N_Simple_Return_Statement
)
1172 && Present (Actual_Subtype (gnat_entity
))
1173 && present_gnu_tree (Actual_Subtype (gnat_entity
)))
1174 gnat_result_type
= Actual_Subtype (gnat_entity
);
1176 gnat_result_type
= Etype (gnat_node
);
1179 /* Expand the type of this identifier first if it is needed, in case it is an
1180 enumeral literal, which only get made when the type is expanded. There is
1181 no order-of-elaboration issue here. */
1182 if (Is_Subprogram (gnat_entity
))
1183 gnu_result_type
= NULL_TREE
;
1185 gnu_result_type
= get_unpadded_type (gnat_result_type
);
1187 /* If this is a non-imported elementary constant with an address clause,
1188 retrieve the value instead of a pointer to be dereferenced unless
1189 an lvalue is required. This is generally more efficient and actually
1190 required if this is a static expression because it might be used
1191 in a context where a dereference is inappropriate, such as a case
1192 statement alternative or a record discriminant. There is no possible
1193 volatile-ness short-circuit here since Volatile constants must be
1194 imported per C.6. */
1195 if (Ekind (gnat_entity
) == E_Constant
1196 && Is_Elementary_Type (gnat_result_type
)
1197 && !Is_Imported (gnat_entity
)
1198 && Present (Address_Clause (gnat_entity
)))
1201 = lvalue_required_p (gnat_node
, gnu_result_type
, true, false);
1202 use_constant_initializer
= !require_lvalue
;
1206 require_lvalue
= -1;
1207 use_constant_initializer
= false;
1210 /* Fetch the initialization value of a constant if requested. */
1211 if (use_constant_initializer
)
1213 /* If this is a deferred constant, the initializer is attached to
1215 if (Present (Full_View (gnat_entity
)))
1216 gnat_entity
= Full_View (gnat_entity
);
1218 gnu_result
= gnat_to_gnu (Expression (Declaration_Node (gnat_entity
)));
1221 gnu_result
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, false);
1223 /* Some objects (such as parameters passed by reference, globals of
1224 variable size, and renamed objects) actually represent the address
1225 of the object. In that case, we must do the dereference. Likewise,
1226 deal with parameters to foreign convention subprograms. */
1227 if (DECL_P (gnu_result
)
1228 && (DECL_BY_REF_P (gnu_result
)
1229 || (TREE_CODE (gnu_result
) == PARM_DECL
1230 && DECL_BY_COMPONENT_PTR_P (gnu_result
))))
1232 const bool read_only
= DECL_POINTS_TO_READONLY_P (gnu_result
);
1234 /* If it's a PARM_DECL to foreign convention subprogram, convert it. */
1235 if (TREE_CODE (gnu_result
) == PARM_DECL
1236 && DECL_BY_COMPONENT_PTR_P (gnu_result
))
1238 = convert (build_pointer_type (gnu_result_type
), gnu_result
);
1240 /* If it's a CONST_DECL, return the underlying constant like below. */
1241 else if (TREE_CODE (gnu_result
) == CONST_DECL
1242 && !(DECL_CONST_ADDRESS_P (gnu_result
)
1243 && lvalue_required_p (gnat_node
, gnu_result_type
, true,
1245 gnu_result
= DECL_INITIAL (gnu_result
);
1247 /* Do the final dereference. */
1248 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result
);
1250 if ((INDIRECT_REF_P (gnu_result
)
1251 || TREE_CODE (gnu_result
) == UNCONSTRAINED_ARRAY_REF
)
1252 && No (Address_Clause (gnat_entity
)))
1253 TREE_THIS_NOTRAP (gnu_result
) = 1;
1256 TREE_READONLY (gnu_result
) = 1;
1259 /* If we have a constant declaration and its initializer, try to return the
1260 latter to avoid the need to call fold in lots of places and the need for
1261 elaboration code if this identifier is used as an initializer itself. */
1262 if (constant_decl_with_initializer_p (gnu_result
))
1264 bool constant_only
= (TREE_CODE (gnu_result
) == CONST_DECL
1265 && !DECL_CONST_CORRESPONDING_VAR (gnu_result
));
1266 bool address_of_constant
= (TREE_CODE (gnu_result
) == CONST_DECL
1267 && DECL_CONST_ADDRESS_P (gnu_result
));
1269 /* If there is a (corresponding) variable or this is the address of a
1270 constant, we only want to return the initializer if an lvalue isn't
1271 required. Evaluate this now if we have not already done so. */
1272 if ((!constant_only
|| address_of_constant
) && require_lvalue
< 0)
1274 = lvalue_required_p (gnat_node
, gnu_result_type
, true,
1275 address_of_constant
)
1276 || (AGGREGATE_TYPE_P (gnu_result_type
)
1277 && lvalue_for_aggregate_p (gnat_node
, gnu_result_type
));
1279 /* Finally retrieve the initializer if this is deemed valid. */
1280 if ((constant_only
&& !address_of_constant
) || !require_lvalue
)
1281 gnu_result
= DECL_INITIAL (gnu_result
);
1284 /* But for a constant renaming we couldn't do that incrementally for its
1285 definition because of the need to return an lvalue so, if the present
1286 context doesn't itself require an lvalue, we try again here. */
1287 else if (Ekind (gnat_entity
) == E_Constant
1288 && Is_Elementary_Type (gnat_result_type
)
1289 && Present (Renamed_Object (gnat_entity
)))
1291 if (require_lvalue
< 0)
1293 = lvalue_required_p (gnat_node
, gnu_result_type
, true, false);
1294 if (!require_lvalue
)
1295 gnu_result
= fold_constant_decl_in_expr (gnu_result
);
1298 /* The GNAT tree has the type of a function set to its result type, so we
1299 adjust here. Also use the type of the result if the Etype is a subtype
1300 that is nominally unconstrained. Likewise if this is a deferred constant
1301 of a discriminated type whose full view can be elaborated statically, to
1302 avoid problematic conversions to the nominal subtype. But remove any
1303 padding from the resulting type. */
1304 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_result
))
1305 || Is_Constr_Subt_For_UN_Aliased (gnat_result_type
)
1306 || (Ekind (gnat_entity
) == E_Constant
1307 && Present (Full_View (gnat_entity
))
1308 && Has_Discriminants (gnat_result_type
)
1309 && TREE_CODE (gnu_result
) == CONSTRUCTOR
))
1311 gnu_result_type
= TREE_TYPE (gnu_result
);
1312 if (TYPE_IS_PADDING_P (gnu_result_type
))
1313 gnu_result_type
= TREE_TYPE (TYPE_FIELDS (gnu_result_type
));
1316 *gnu_result_type_p
= gnu_result_type
;
1321 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Pragma, to a GCC
1322 tree, which is returned. */
1325 Pragma_to_gnu (Node_Id gnat_node
)
1327 tree gnu_result
= alloc_stmt_list ();
1330 /* Check for (and ignore) unrecognized pragmas. */
1331 if (!Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node
))))
1335 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)));
1337 /* Save the expression of pragma Compile_Time_{Error|Warning} for later. */
1338 if (id
== Pragma_Compile_Time_Error
|| id
== Pragma_Compile_Time_Warning
)
1340 gnat_temp
= First (Pragma_Argument_Associations (gnat_node
));
1341 gnat_compile_time_expr_list
.safe_push (Expression (gnat_temp
));
1345 /* Stop there if we are just annotating types. */
1346 if (type_annotate_only
)
1351 case Pragma_Inspection_Point
:
1352 /* Do nothing at top level: all such variables are already viewable. */
1353 if (global_bindings_p ())
1356 for (gnat_temp
= First (Pragma_Argument_Associations (gnat_node
));
1357 Present (gnat_temp
);
1358 gnat_temp
= Next (gnat_temp
))
1360 Node_Id gnat_expr
= Expression (gnat_temp
);
1361 tree gnu_expr
= gnat_to_gnu (gnat_expr
);
1362 tree asm_constraint
= NULL_TREE
;
1363 #ifdef ASM_COMMENT_START
1366 gnu_expr
= maybe_unconstrained_array (gnu_expr
);
1367 if (TREE_CODE (gnu_expr
) == CONST_DECL
1368 && DECL_CONST_CORRESPONDING_VAR (gnu_expr
))
1369 gnu_expr
= DECL_CONST_CORRESPONDING_VAR (gnu_expr
);
1370 gnat_mark_addressable (gnu_expr
);
1372 #ifdef ASM_COMMENT_START
1373 comment
= concat (ASM_COMMENT_START
,
1374 " inspection point: ",
1375 Get_Name_String (Chars (gnat_expr
)),
1378 asm_constraint
= build_string (strlen (comment
), comment
);
1381 gnu_expr
= build5 (ASM_EXPR
, void_type_node
,
1385 (build_tree_list (NULL_TREE
,
1386 build_string (1, "m")),
1387 gnu_expr
, NULL_TREE
),
1388 NULL_TREE
, NULL_TREE
);
1389 ASM_VOLATILE_P (gnu_expr
) = 1;
1390 set_expr_location_from_node (gnu_expr
, gnat_node
);
1391 append_to_statement_list (gnu_expr
, &gnu_result
);
1395 case Pragma_Loop_Optimize
:
1396 for (gnat_temp
= First (Pragma_Argument_Associations (gnat_node
));
1397 Present (gnat_temp
);
1398 gnat_temp
= Next (gnat_temp
))
1400 tree gnu_loop_stmt
= gnu_loop_stack
->last ()->stmt
;
1402 switch (Chars (Expression (gnat_temp
)))
1405 LOOP_STMT_IVDEP (gnu_loop_stmt
) = 1;
1408 case Name_No_Unroll
:
1409 LOOP_STMT_NO_UNROLL (gnu_loop_stmt
) = 1;
1413 LOOP_STMT_UNROLL (gnu_loop_stmt
) = 1;
1416 case Name_No_Vector
:
1417 LOOP_STMT_NO_VECTOR (gnu_loop_stmt
) = 1;
1421 LOOP_STMT_VECTOR (gnu_loop_stmt
) = 1;
1430 case Pragma_Optimize
:
1431 switch (Chars (Expression
1432 (First (Pragma_Argument_Associations (gnat_node
)))))
1436 post_error ("must specify -O0??", gnat_node
);
1441 post_error ("must specify -Os??", gnat_node
);
1446 post_error ("insufficient -O value??", gnat_node
);
1454 case Pragma_Reviewable
:
1455 if (write_symbols
== NO_DEBUG
)
1456 post_error ("must specify -g??", gnat_node
);
1459 case Pragma_Warning_As_Error
:
1460 case Pragma_Warnings
:
1463 /* Preserve the location of the pragma. */
1464 const location_t location
= input_location
;
1465 struct cl_option_handlers handlers
;
1466 unsigned int option_index
;
1470 gnat_temp
= First (Pragma_Argument_Associations (gnat_node
));
1472 /* This is the String form: pragma Warning{s|_As_Error}(String). */
1473 if (Nkind (Expression (gnat_temp
)) == N_String_Literal
)
1477 case Pragma_Warning_As_Error
:
1482 case Pragma_Warnings
:
1491 gnat_expr
= Expression (gnat_temp
);
1494 /* This is the On/Off form: pragma Warnings (On | Off [,String]). */
1495 else if (Nkind (Expression (gnat_temp
)) == N_Identifier
)
1497 switch (Chars (Expression (gnat_temp
)))
1511 /* Deal with optional pattern (but ignore Reason => "..."). */
1512 if (Present (Next (gnat_temp
))
1513 && Chars (Next (gnat_temp
)) != Name_Reason
)
1515 /* pragma Warnings (On | Off, Name) is handled differently. */
1516 if (Nkind (Expression (Next (gnat_temp
))) != N_String_Literal
)
1519 gnat_expr
= Expression (Next (gnat_temp
));
1525 /* For pragma Warnings (Off), we save the current state... */
1526 if (kind
== DK_IGNORED
)
1527 diagnostic_push_diagnostics (global_dc
, location
);
1529 /* ...so that, for pragma Warnings (On), we do not enable all
1530 the warnings but just restore the previous state. */
1533 diagnostic_pop_diagnostics (global_dc
, location
);
1544 /* This is the same implementation as in the C family of compilers. */
1545 const unsigned int lang_mask
= CL_Ada
| CL_COMMON
;
1546 const char *arg
= NULL
;
1547 if (Present (gnat_expr
))
1549 tree gnu_expr
= gnat_to_gnu (gnat_expr
);
1550 const char *option_string
= TREE_STRING_POINTER (gnu_expr
);
1551 const int len
= TREE_STRING_LENGTH (gnu_expr
);
1552 if (len
< 3 || option_string
[0] != '-' || option_string
[1] != 'W')
1554 option_index
= find_opt (option_string
+ 1, lang_mask
);
1555 if (option_index
== OPT_SPECIAL_unknown
)
1557 post_error ("unknown -W switch??", gnat_node
);
1560 else if (!(cl_options
[option_index
].flags
& CL_WARNING
))
1562 post_error ("-W switch does not control warning??", gnat_node
);
1565 else if (!(cl_options
[option_index
].flags
& lang_mask
))
1567 post_error ("-W switch not valid for Ada??", gnat_node
);
1570 if (cl_options
[option_index
].flags
& CL_JOINED
)
1571 arg
= option_string
+ 1 + cl_options
[option_index
].opt_len
;
1576 set_default_handlers (&handlers
, NULL
);
1577 control_warning_option (option_index
, (int) kind
, arg
, imply
, location
,
1578 lang_mask
, &handlers
, &global_options
,
1579 &global_options_set
, global_dc
);
1590 /* Check the inline status of nested function FNDECL wrt its parent function.
1592 If a non-inline nested function is referenced from an inline external
1593 function, we cannot honor both requests at the same time without cloning
1594 the nested function in the current unit since it is private to its unit.
1595 We could inline it as well but it's probably better to err on the side
1596 of too little inlining.
1598 This must be done only on nested functions present in the source code
1599 and not on nested functions generated by the compiler, e.g. finalizers,
1600 because they may be not marked inline and we don't want them to block
1601 the inlining of the parent function. */
1604 check_inlining_for_nested_subprog (tree fndecl
)
1606 if (DECL_IGNORED_P (current_function_decl
) || DECL_IGNORED_P (fndecl
))
1609 if (DECL_DECLARED_INLINE_P (fndecl
))
1612 tree parent_decl
= decl_function_context (fndecl
);
1613 if (DECL_EXTERNAL (parent_decl
) && DECL_DECLARED_INLINE_P (parent_decl
))
1615 const location_t loc1
= DECL_SOURCE_LOCATION (fndecl
);
1616 const location_t loc2
= DECL_SOURCE_LOCATION (parent_decl
);
1618 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (parent_decl
)))
1620 error_at (loc1
, "subprogram %q+F not marked %<Inline_Always%>",
1622 error_at (loc2
, "parent subprogram cannot be inlined");
1626 warning_at (loc1
, OPT_Winline
, "subprogram %q+F not marked %<Inline%>",
1628 warning_at (loc2
, OPT_Winline
, "parent subprogram cannot be inlined");
1631 DECL_DECLARED_INLINE_P (parent_decl
) = 0;
1632 DECL_UNINLINABLE (parent_decl
) = 1;
1636 /* Return an expression for the length of TYPE, an integral type, computed in
1637 RESULT_TYPE, another integral type.
1639 We used to compute the length as MAX (hb - lb + 1, 0) which could overflow
1640 when lb == TYPE'First. We now compute it as (hb >= lb) ? hb - lb + 1 : 0
1641 which would only overflow in much rarer cases, for extremely large arrays
1642 we expect never to encounter in practice. Besides, the former computation
1643 required the use of potentially constraining signed arithmetics while the
1644 latter does not. Note that the comparison must be done in the original
1645 base index type in order to avoid any overflow during the conversion. */
1648 get_type_length (tree type
, tree result_type
)
1650 tree comp_type
= get_base_type (result_type
);
1651 tree base_type
= maybe_character_type (get_base_type (type
));
1652 tree lb
= convert (base_type
, TYPE_MIN_VALUE (type
));
1653 tree hb
= convert (base_type
, TYPE_MAX_VALUE (type
));
1655 = build_binary_op (PLUS_EXPR
, comp_type
,
1656 build_binary_op (MINUS_EXPR
, comp_type
,
1657 convert (comp_type
, hb
),
1658 convert (comp_type
, lb
)),
1659 build_int_cst (comp_type
, 1));
1661 = build_cond_expr (result_type
,
1662 build_binary_op (GE_EXPR
, boolean_type_node
, hb
, lb
),
1663 convert (result_type
, length
),
1664 build_int_cst (result_type
, 0));
1668 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node, to a
1669 GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to where we
1670 should place the result type. ATTRIBUTE is the attribute ID. */
1673 Attribute_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
,
1674 Attribute_Id attribute
)
1676 const Node_Id gnat_prefix
= Prefix (gnat_node
);
1677 tree gnu_prefix
= gnat_to_gnu (gnat_prefix
);
1678 tree gnu_type
= TREE_TYPE (gnu_prefix
);
1679 tree gnu_expr
, gnu_result_type
, gnu_result
= error_mark_node
;
1680 bool prefix_unused
= false;
1683 /* If the input is a NULL_EXPR, make a new one. */
1684 if (TREE_CODE (gnu_prefix
) == NULL_EXPR
)
1686 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1687 *gnu_result_type_p
= gnu_result_type
;
1688 return build1 (NULL_EXPR
, gnu_result_type
, TREE_OPERAND (gnu_prefix
, 0));
1691 /* If the input is a LOAD_EXPR of an unconstrained array type, the second
1692 operand contains the storage model object. */
1693 if (TREE_CODE (gnu_prefix
) == LOAD_EXPR
1694 && TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
1695 gnat_smo
= tree_to_shwi (TREE_OPERAND (gnu_prefix
, 1));
1703 /* These just add or subtract the constant 1 since representation
1704 clauses for enumeration types are handled in the front-end. */
1705 gnu_expr
= gnat_to_gnu (First (Expressions (gnat_node
)));
1706 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1707 gnu_type
= maybe_character_type (gnu_result_type
);
1708 if (TREE_TYPE (gnu_expr
) != gnu_type
)
1709 gnu_expr
= convert (gnu_type
, gnu_expr
);
1711 = build_binary_op (attribute
== Attr_Pred
? MINUS_EXPR
: PLUS_EXPR
,
1712 gnu_type
, gnu_expr
, build_int_cst (gnu_type
, 1));
1716 case Attr_Unrestricted_Access
:
1717 /* Conversions don't change the address of references but can cause
1718 build_unary_op to miss the references below so strip them off.
1720 Also remove the conversions applied to declarations as the intent is
1721 to take the decls' address, not that of the copies that the
1722 conversions may create.
1724 On the contrary, if the address-of operation causes a temporary
1725 to be created, then it must be created with the proper type. */
1726 gnu_expr
= remove_conversions (gnu_prefix
,
1727 !Must_Be_Byte_Aligned (gnat_node
));
1728 if (REFERENCE_CLASS_P (gnu_expr
) || DECL_P (gnu_expr
))
1729 gnu_prefix
= gnu_expr
;
1731 /* If we are taking 'Address of an unconstrained object, this is the
1732 pointer to the underlying array. */
1733 if (attribute
== Attr_Address
)
1734 gnu_prefix
= maybe_unconstrained_array (gnu_prefix
);
1736 /* If we are building a static dispatch table, we have to honor
1737 TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible
1738 with the C++ ABI. We do it in the non-static case as well,
1739 see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */
1740 else if (TARGET_VTABLE_USES_DESCRIPTORS
1741 && Is_Dispatch_Table_Entity (Etype (gnat_node
)))
1744 /* Descriptors can only be built here for top-level functions. */
1745 bool build_descriptor
= (global_bindings_p () != 0);
1747 vec
<constructor_elt
, va_gc
> *gnu_vec
= NULL
;
1748 constructor_elt
*elt
;
1750 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1752 /* If we're not going to build the descriptor, we have to retrieve
1753 the one which will be built by the linker (or by the compiler
1754 later if a static chain is requested). */
1755 if (!build_descriptor
)
1757 gnu_result
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_prefix
);
1758 gnu_result
= fold_convert (build_pointer_type (gnu_result_type
),
1760 gnu_result
= build1 (INDIRECT_REF
, gnu_result_type
, gnu_result
);
1763 vec_safe_grow (gnu_vec
, TARGET_VTABLE_USES_DESCRIPTORS
, true);
1764 elt
= (gnu_vec
->address () + TARGET_VTABLE_USES_DESCRIPTORS
- 1);
1765 for (gnu_field
= TYPE_FIELDS (gnu_result_type
), i
= 0;
1766 i
< TARGET_VTABLE_USES_DESCRIPTORS
;
1767 gnu_field
= DECL_CHAIN (gnu_field
), i
++)
1769 if (build_descriptor
)
1771 t
= build2 (FDESC_EXPR
, TREE_TYPE (gnu_field
), gnu_prefix
,
1772 build_int_cst (NULL_TREE
, i
));
1773 TREE_CONSTANT (t
) = 1;
1776 t
= build3 (COMPONENT_REF
, ptr_void_ftype
, gnu_result
,
1777 gnu_field
, NULL_TREE
);
1779 elt
->index
= gnu_field
;
1784 gnu_result
= gnat_build_constructor (gnu_result_type
, gnu_vec
);
1788 /* ... fall through ... */
1791 case Attr_Unchecked_Access
:
1792 case Attr_Code_Address
:
1793 /* Taking the address of a type does not make sense. */
1794 gcc_assert (TREE_CODE (gnu_prefix
) != TYPE_DECL
);
1796 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1798 = build_unary_op (((attribute
== Attr_Address
1799 || attribute
== Attr_Unrestricted_Access
)
1800 && !Must_Be_Byte_Aligned (gnat_node
))
1801 ? ATTR_ADDR_EXPR
: ADDR_EXPR
,
1802 gnu_result_type
, gnu_prefix
);
1804 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
1805 don't try to build a trampoline. */
1806 if (attribute
== Attr_Code_Address
)
1808 gnu_expr
= remove_conversions (gnu_result
, false);
1810 if (TREE_CODE (gnu_expr
) == ADDR_EXPR
)
1811 TREE_NO_TRAMPOLINE (gnu_expr
) = TREE_CONSTANT (gnu_expr
) = 1;
1813 /* On targets for which function symbols denote a descriptor, the
1814 code address is stored within the first slot of the descriptor
1815 so we do an additional dereference:
1816 result = *((result_type *) result)
1817 where we expect result to be of some pointer type already. */
1818 if (targetm
.calls
.custom_function_descriptors
== 0)
1820 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
1821 convert (build_pointer_type (gnu_result_type
),
1825 /* For 'Access, issue an error message if the prefix is a C++ method
1826 since it can use a special calling convention on some platforms,
1827 which cannot be propagated to the access type. */
1828 else if (attribute
== Attr_Access
1829 && TREE_CODE (TREE_TYPE (gnu_prefix
)) == METHOD_TYPE
)
1830 post_error ("access to C++ constructor or member function not allowed",
1833 /* For other address attributes applied to a nested function,
1834 find an inner ADDR_EXPR and annotate it so that we can issue
1835 a useful warning with -Wtrampolines. */
1836 else if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_prefix
))
1837 && (gnu_expr
= remove_conversions (gnu_result
, false))
1838 && TREE_CODE (gnu_expr
) == ADDR_EXPR
1839 && decl_function_context (TREE_OPERAND (gnu_expr
, 0)))
1841 set_expr_location_from_node (gnu_expr
, gnat_node
);
1843 /* Also check the inlining status. */
1844 check_inlining_for_nested_subprog (TREE_OPERAND (gnu_expr
, 0));
1846 /* Moreover, for 'Access or 'Unrestricted_Access with non-
1847 foreign-compatible representation, mark the ADDR_EXPR so
1848 that we can build a descriptor instead of a trampoline. */
1849 if ((attribute
== Attr_Access
1850 || attribute
== Attr_Unrestricted_Access
)
1851 && targetm
.calls
.custom_function_descriptors
> 0
1852 && Can_Use_Internal_Rep (Underlying_Type (Etype (gnat_node
))))
1853 FUNC_ADDR_BY_DESCRIPTOR (gnu_expr
) = 1;
1855 /* Otherwise, we need to check that we are not violating the
1856 No_Implicit_Dynamic_Code restriction. */
1857 else if (targetm
.calls
.custom_function_descriptors
!= 0)
1858 Check_Implicit_Dynamic_Code_Allowed (gnat_node
);
1862 case Attr_Pool_Address
:
1864 tree gnu_ptr
= gnu_prefix
;
1867 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1869 /* If this is fat pointer, the object must have been allocated with the
1870 template in front of the array. So compute the template address; do
1871 it by converting to a thin pointer. */
1872 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr
)))
1874 = convert (build_pointer_type
1875 (TYPE_OBJECT_RECORD_TYPE
1876 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr
)))),
1879 gnu_obj_type
= TREE_TYPE (TREE_TYPE (gnu_ptr
));
1881 /* If this is a thin pointer, the object must have been allocated with
1882 the template in front of the array. So compute the template address
1884 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr
)))
1886 = build_binary_op (POINTER_PLUS_EXPR
, TREE_TYPE (gnu_ptr
),
1888 fold_build1 (NEGATE_EXPR
, sizetype
,
1891 TYPE_FIELDS ((gnu_obj_type
)))));
1893 gnu_result
= convert (gnu_result_type
, gnu_ptr
);
1898 case Attr_Object_Size
:
1899 case Attr_Value_Size
:
1900 case Attr_Max_Size_In_Storage_Elements
:
1901 /* Strip NOPs, conversions between original and packable versions, and
1902 unpadding from GNU_PREFIX. Note that we cannot simply strip every
1903 VIEW_CONVERT_EXPR because some of them may give the actual size, e.g.
1904 for nominally unconstrained packed array. We use GNU_EXPR to see
1905 if a COMPONENT_REF was involved. */
1906 while (CONVERT_EXPR_P (gnu_prefix
)
1907 || TREE_CODE (gnu_prefix
) == NON_LVALUE_EXPR
1908 || (TREE_CODE (gnu_prefix
) == VIEW_CONVERT_EXPR
1909 && TREE_CODE (TREE_TYPE (gnu_prefix
)) == RECORD_TYPE
1910 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0)))
1912 && TYPE_NAME (TREE_TYPE (gnu_prefix
))
1913 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0)))))
1914 gnu_prefix
= TREE_OPERAND (gnu_prefix
, 0);
1915 gnu_expr
= gnu_prefix
;
1916 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
1917 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0))))
1918 gnu_prefix
= TREE_OPERAND (gnu_prefix
, 0);
1919 prefix_unused
= true;
1920 gnu_type
= TREE_TYPE (gnu_prefix
);
1922 /* Replace an unconstrained array type with the type of the underlying
1923 array, except for 'Max_Size_In_Storage_Elements because we need to
1924 return the (maximum) size requested for an allocator. */
1925 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
1927 gnu_type
= TYPE_OBJECT_RECORD_TYPE (gnu_type
);
1928 if (attribute
!= Attr_Max_Size_In_Storage_Elements
)
1929 gnu_type
= TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
)));
1932 /* The type must be frozen at this point. */
1933 gcc_assert (COMPLETE_TYPE_P (gnu_type
));
1935 /* If we're looking for the size of a field, return the field size. */
1936 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
1937 gnu_result
= DECL_SIZE (TREE_OPERAND (gnu_prefix
, 1));
1939 /* Otherwise, if the prefix is an object, or if we are looking for
1940 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the
1941 GCC size of the type. We make an exception for padded objects,
1942 as we do not take into account alignment promotions for the size.
1943 This is in keeping with the object case of gnat_to_gnu_entity. */
1944 else if ((TREE_CODE (gnu_prefix
) != TYPE_DECL
1945 && !(TYPE_IS_PADDING_P (gnu_type
)
1946 && TREE_CODE (gnu_expr
) == COMPONENT_REF
1947 && pad_type_has_rm_size (gnu_type
)))
1948 || attribute
== Attr_Object_Size
1949 || attribute
== Attr_Max_Size_In_Storage_Elements
)
1951 /* If this is a dereference and we have a special dynamic constrained
1952 subtype on the prefix, use it to compute the size; otherwise, use
1953 the designated subtype. */
1954 if (Nkind (gnat_prefix
) == N_Explicit_Dereference
1955 && Present (Actual_Designated_Subtype (gnat_prefix
)))
1957 tree gnu_actual_obj_type
1958 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_prefix
));
1960 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_prefix
)));
1962 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type
))
1964 = build_unc_object_type_from_ptr (gnu_ptr_type
,
1965 gnu_actual_obj_type
,
1966 get_identifier ("SIZE"),
1970 gnu_result
= TYPE_SIZE (gnu_type
);
1973 /* Otherwise, the result is the RM size of the type. */
1975 gnu_result
= rm_size (gnu_type
);
1977 /* Deal with a self-referential size by qualifying the size with the
1978 object or returning the maximum size for a type. */
1979 if (TREE_CODE (gnu_prefix
) != TYPE_DECL
)
1981 gnu_result
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result
, gnu_prefix
);
1982 if (Present (gnat_smo
)
1983 && Present (Storage_Model_Copy_From (gnat_smo
)))
1984 gnu_result
= INSTANTIATE_LOAD_IN_EXPR (gnu_result
, gnat_smo
);
1986 else if (CONTAINS_PLACEHOLDER_P (gnu_result
))
1987 gnu_result
= max_size (gnu_result
, true);
1989 /* If the type contains a template, subtract the padded size of the
1990 template, except for 'Max_Size_In_Storage_Elements because we need
1991 to return the (maximum) size requested for an allocator. */
1992 if (TREE_CODE (gnu_type
) == RECORD_TYPE
1993 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
)
1994 && attribute
!= Attr_Max_Size_In_Storage_Elements
)
1996 = size_binop (MINUS_EXPR
, gnu_result
,
1997 bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type
))));
1999 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
2000 if (attribute
== Attr_Max_Size_In_Storage_Elements
)
2001 gnu_result
= size_binop (CEIL_DIV_EXPR
, gnu_result
, bitsize_unit_node
);
2003 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2006 case Attr_Alignment
:
2010 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
2011 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0))))
2012 gnu_prefix
= TREE_OPERAND (gnu_prefix
, 0);
2014 gnu_type
= TREE_TYPE (gnu_prefix
);
2015 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2016 prefix_unused
= true;
2018 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
2019 align
= DECL_ALIGN (TREE_OPERAND (gnu_prefix
, 1)) / BITS_PER_UNIT
;
2022 Entity_Id gnat_type
= Etype (gnat_prefix
);
2023 unsigned int double_align
;
2024 bool is_capped_double
, align_clause
;
2026 /* If the default alignment of "double" or larger scalar types is
2027 specifically capped and there is an alignment clause neither
2028 on the type nor on the prefix itself, return the cap. */
2029 if ((double_align
= double_float_alignment
) > 0)
2031 = is_double_float_or_array (gnat_type
, &align_clause
);
2032 else if ((double_align
= double_scalar_alignment
) > 0)
2034 = is_double_scalar_or_array (gnat_type
, &align_clause
);
2036 is_capped_double
= align_clause
= false;
2038 if (is_capped_double
2039 && Nkind (gnat_prefix
) == N_Identifier
2040 && Present (Alignment_Clause (Entity (gnat_prefix
))))
2041 align_clause
= true;
2043 if (is_capped_double
&& !align_clause
)
2044 align
= double_align
;
2046 align
= TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
;
2049 gnu_result
= size_int (align
);
2055 case Attr_Range_Length
:
2056 prefix_unused
= true;
2058 if (INTEGRAL_TYPE_P (gnu_type
) || SCALAR_FLOAT_TYPE_P (gnu_type
))
2060 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2062 if (attribute
== Attr_First
)
2063 gnu_result
= TYPE_MIN_VALUE (gnu_type
);
2064 else if (attribute
== Attr_Last
)
2065 gnu_result
= TYPE_MAX_VALUE (gnu_type
);
2067 gnu_result
= get_type_length (gnu_type
, gnu_result_type
);
2071 /* ... fall through ... */
2075 int Dimension
= (Present (Expressions (gnat_node
))
2076 ? UI_To_Int (Intval (First (Expressions (gnat_node
))))
2078 struct parm_attr_d
*pa
= NULL
;
2079 Entity_Id gnat_param
= Empty
;
2080 bool unconstrained_ptr_deref
= false;
2082 gnu_prefix
= maybe_padded_object (gnu_prefix
);
2083 gnu_prefix
= maybe_unconstrained_array (gnu_prefix
);
2085 /* We treat unconstrained array In parameters specially. We also note
2086 whether we are dereferencing a pointer to unconstrained array. */
2087 if (!Is_Constrained (Etype (gnat_prefix
)))
2088 switch (Nkind (gnat_prefix
))
2091 /* This is the direct case. */
2092 if (Ekind (Entity (gnat_prefix
)) == E_In_Parameter
)
2093 gnat_param
= Entity (gnat_prefix
);
2096 case N_Explicit_Dereference
:
2097 /* This is the indirect case. Note that we need to be sure that
2098 the access value cannot be null as we'll hoist the load. */
2099 if (Nkind (Prefix (gnat_prefix
)) == N_Identifier
2100 && Ekind (Entity (Prefix (gnat_prefix
))) == E_In_Parameter
)
2102 if (Can_Never_Be_Null (Entity (Prefix (gnat_prefix
))))
2103 gnat_param
= Entity (Prefix (gnat_prefix
));
2106 unconstrained_ptr_deref
= true;
2113 /* If the prefix is the view conversion of a constrained array to an
2114 unconstrained form, we retrieve the constrained array because we
2115 might not be able to substitute the PLACEHOLDER_EXPR coming from
2116 the conversion. This can occur with the 'Old attribute applied
2117 to a parameter with an unconstrained type, which gets rewritten
2118 into a constrained local variable very late in the game. */
2119 if (TREE_CODE (gnu_prefix
) == VIEW_CONVERT_EXPR
2120 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (gnu_prefix
)))
2121 && !CONTAINS_PLACEHOLDER_P
2122 (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0)))))
2123 gnu_type
= TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0));
2125 gnu_type
= TREE_TYPE (gnu_prefix
);
2127 prefix_unused
= true;
2128 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2130 if (TYPE_CONVENTION_FORTRAN_P (gnu_type
))
2135 for (ndim
= 1, gnu_type_temp
= gnu_type
;
2136 TREE_CODE (TREE_TYPE (gnu_type_temp
)) == ARRAY_TYPE
2137 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp
));
2138 ndim
++, gnu_type_temp
= TREE_TYPE (gnu_type_temp
))
2141 Dimension
= ndim
+ 1 - Dimension
;
2144 for (i
= 1; i
< Dimension
; i
++)
2145 gnu_type
= TREE_TYPE (gnu_type
);
2147 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
2149 /* When not optimizing, look up the slot associated with the parameter
2150 and the dimension in the cache and create a new one on failure.
2151 Don't do this when the actual subtype needs debug info (this happens
2152 with -gnatD): in elaborate_expression_1, we create variables that
2153 hold the bounds, so caching attributes isn't very interesting and
2154 causes dependency issues between these variables and cached
2157 && Present (gnat_param
)
2158 && !(Present (Actual_Subtype (gnat_param
))
2159 && Needs_Debug_Info (Actual_Subtype (gnat_param
))))
2161 FOR_EACH_VEC_SAFE_ELT (f_parm_attr_cache
, i
, pa
)
2162 if (pa
->id
== gnat_param
&& pa
->dim
== Dimension
)
2167 pa
= ggc_cleared_alloc
<parm_attr_d
> ();
2168 pa
->id
= gnat_param
;
2169 pa
->dim
= Dimension
;
2170 vec_safe_push (f_parm_attr_cache
, pa
);
2174 /* Return the cached expression or build a new one. */
2175 if (attribute
== Attr_First
)
2177 if (pa
&& pa
->first
)
2179 gnu_result
= pa
->first
;
2184 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)));
2187 else if (attribute
== Attr_Last
)
2191 gnu_result
= pa
->last
;
2196 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)));
2199 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
2201 if (pa
&& pa
->length
)
2203 gnu_result
= pa
->length
;
2208 = get_type_length (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)),
2212 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2213 handling. Note that these attributes could not have been used on
2214 an unconstrained array type. */
2215 gnu_result
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result
, gnu_prefix
);
2216 if (Present (gnat_smo
)
2217 && Present (Storage_Model_Copy_From (gnat_smo
)))
2218 gnu_result
= INSTANTIATE_LOAD_IN_EXPR (gnu_result
, gnat_smo
);
2220 /* Cache the expression we have just computed. Since we want to do it
2221 at run time, we force the use of a SAVE_EXPR and let the gimplifier
2222 create the temporary in the outermost binding level. We will make
2223 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
2224 paths by forcing its evaluation on entry of the function. */
2228 = build1 (SAVE_EXPR
, TREE_TYPE (gnu_result
), gnu_result
);
2232 pa
->first
= gnu_result
;
2236 pa
->last
= gnu_result
;
2240 case Attr_Range_Length
:
2241 pa
->length
= gnu_result
;
2249 /* Otherwise, evaluate it each time it is referenced. */
2255 /* If we are dereferencing a pointer to unconstrained array, we
2256 need to capture the value because the pointed-to bounds may
2257 subsequently be released. */
2258 if (unconstrained_ptr_deref
)
2260 = build1 (SAVE_EXPR
, TREE_TYPE (gnu_result
), gnu_result
);
2264 case Attr_Range_Length
:
2265 /* Set the source location onto the predicate of the condition
2266 but not if the expression is cached to avoid messing up the
2268 if (TREE_CODE (gnu_result
) == COND_EXPR
2269 && EXPR_P (TREE_OPERAND (gnu_result
, 0)))
2270 set_expr_location_from_node (TREE_OPERAND (gnu_result
, 0),
2281 case Attr_Bit_Position
:
2283 case Attr_First_Bit
:
2290 tree gnu_field_bitpos
;
2291 tree gnu_field_offset
;
2294 int unsignedp
, reversep
, volatilep
;
2296 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2297 gnu_prefix
= remove_conversions (gnu_prefix
, true);
2298 prefix_unused
= true;
2300 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
2301 the result is 0. Don't allow 'Bit on a bare component, though. */
2302 if (attribute
== Attr_Bit
2303 && TREE_CODE (gnu_prefix
) != COMPONENT_REF
2304 && TREE_CODE (gnu_prefix
) != FIELD_DECL
)
2306 gnu_result
= integer_zero_node
;
2311 gcc_assert (TREE_CODE (gnu_prefix
) == COMPONENT_REF
2312 || (attribute
== Attr_Bit_Position
2313 && TREE_CODE (gnu_prefix
) == FIELD_DECL
));
2315 get_inner_reference (gnu_prefix
, &bitsize
, &bitpos
, &gnu_offset
,
2316 &mode
, &unsignedp
, &reversep
, &volatilep
);
2318 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
2320 gnu_field_bitpos
= bit_position (TREE_OPERAND (gnu_prefix
, 1));
2321 gnu_field_offset
= byte_position (TREE_OPERAND (gnu_prefix
, 1));
2323 for (gnu_inner
= TREE_OPERAND (gnu_prefix
, 0);
2324 TREE_CODE (gnu_inner
) == COMPONENT_REF
2325 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner
, 1));
2326 gnu_inner
= TREE_OPERAND (gnu_inner
, 0))
2329 = size_binop (PLUS_EXPR
, gnu_field_bitpos
,
2330 bit_position (TREE_OPERAND (gnu_inner
, 1)));
2332 = size_binop (PLUS_EXPR
, gnu_field_offset
,
2333 byte_position (TREE_OPERAND (gnu_inner
, 1)));
2336 else if (TREE_CODE (gnu_prefix
) == FIELD_DECL
)
2338 gnu_field_bitpos
= bit_position (gnu_prefix
);
2339 gnu_field_offset
= byte_position (gnu_prefix
);
2343 gnu_field_bitpos
= bitsize_zero_node
;
2344 gnu_field_offset
= size_zero_node
;
2350 gnu_result
= gnu_field_offset
;
2353 case Attr_First_Bit
:
2355 gnu_result
= size_int (num_trailing_bits (bitpos
));
2359 gnu_result
= bitsize_int (num_trailing_bits (bitpos
));
2360 gnu_result
= size_binop (PLUS_EXPR
, gnu_result
,
2361 TYPE_SIZE (TREE_TYPE (gnu_prefix
)));
2362 /* ??? Avoid a large unsigned result that will overflow when
2363 converted to the signed universal_integer. */
2364 if (integer_zerop (gnu_result
))
2365 gnu_result
= integer_minus_one_node
;
2368 = size_binop (MINUS_EXPR
, gnu_result
, bitsize_one_node
);
2371 case Attr_Bit_Position
:
2372 gnu_result
= gnu_field_bitpos
;
2375 /* -Wswitch warning avoidance. */
2380 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
2382 gnu_result
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result
, gnu_prefix
);
2383 if (Present (gnat_smo
)
2384 && Present (Storage_Model_Copy_From (gnat_smo
)))
2385 gnu_result
= INSTANTIATE_LOAD_IN_EXPR (gnu_result
, gnat_smo
);
2392 tree gnu_lhs
= gnat_to_gnu (First (Expressions (gnat_node
)));
2393 tree gnu_rhs
= gnat_to_gnu (Next (First (Expressions (gnat_node
))));
2395 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2397 /* The result of {MIN,MAX}_EXPR is unspecified if either operand is
2398 a NaN so we implement the semantics of C99 f{min,max} to make it
2399 predictable in this case: if either operand is a NaN, the other
2400 is returned; if both operands are NaN's, a NaN is returned. */
2401 if (SCALAR_FLOAT_TYPE_P (gnu_result_type
)
2402 && !Machine_Overflows_On_Target
)
2404 const bool lhs_side_effects_p
= TREE_SIDE_EFFECTS (gnu_lhs
);
2405 const bool rhs_side_effects_p
= TREE_SIDE_EFFECTS (gnu_rhs
);
2406 tree t
= builtin_decl_explicit (BUILT_IN_ISNAN
);
2407 tree lhs_is_nan
, rhs_is_nan
;
2409 /* If the operands have side-effects, they need to be evaluated
2410 only once in spite of the multiple references in the result. */
2411 if (lhs_side_effects_p
)
2412 gnu_lhs
= gnat_protect_expr (gnu_lhs
);
2413 if (rhs_side_effects_p
)
2414 gnu_rhs
= gnat_protect_expr (gnu_rhs
);
2416 lhs_is_nan
= fold_build2 (NE_EXPR
, boolean_type_node
,
2417 build_call_expr (t
, 1, gnu_lhs
),
2420 rhs_is_nan
= fold_build2 (NE_EXPR
, boolean_type_node
,
2421 build_call_expr (t
, 1, gnu_rhs
),
2424 gnu_result
= build_binary_op (attribute
== Attr_Min
2425 ? MIN_EXPR
: MAX_EXPR
,
2426 gnu_result_type
, gnu_lhs
, gnu_rhs
);
2427 gnu_result
= fold_build3 (COND_EXPR
, gnu_result_type
,
2428 rhs_is_nan
, gnu_lhs
, gnu_result
);
2429 gnu_result
= fold_build3 (COND_EXPR
, gnu_result_type
,
2430 lhs_is_nan
, gnu_rhs
, gnu_result
);
2432 /* If the operands have side-effects, they need to be evaluated
2433 before doing the tests above since the place they otherwise
2434 would end up being evaluated at run time could be wrong. */
2435 if (lhs_side_effects_p
)
2437 = build2 (COMPOUND_EXPR
, gnu_result_type
, gnu_lhs
, gnu_result
);
2439 if (rhs_side_effects_p
)
2441 = build2 (COMPOUND_EXPR
, gnu_result_type
, gnu_rhs
, gnu_result
);
2444 gnu_result
= build_binary_op (attribute
== Attr_Min
2445 ? MIN_EXPR
: MAX_EXPR
,
2446 gnu_result_type
, gnu_lhs
, gnu_rhs
);
2450 case Attr_Passed_By_Reference
:
2451 gnu_result
= size_int (default_pass_by_ref (gnu_type
)
2452 || must_pass_by_ref (gnu_type
));
2453 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2456 case Attr_Component_Size
:
2457 gnu_prefix
= maybe_padded_object (gnu_prefix
);
2458 gnu_type
= TREE_TYPE (gnu_prefix
);
2460 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
2461 gnu_type
= TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type
))));
2463 while (TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
2464 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
)))
2465 gnu_type
= TREE_TYPE (gnu_type
);
2467 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
2469 /* Note this size cannot be self-referential. */
2470 gnu_result
= TYPE_SIZE (TREE_TYPE (gnu_type
));
2471 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2472 prefix_unused
= true;
2475 case Attr_Descriptor_Size
:
2476 gnu_type
= TREE_TYPE (gnu_prefix
);
2477 gcc_assert (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
);
2479 /* Return the padded size of the template in the object record type. */
2480 gnu_type
= TYPE_OBJECT_RECORD_TYPE (gnu_type
);
2481 gnu_result
= bit_position (DECL_CHAIN (TYPE_FIELDS (gnu_type
)));
2482 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2483 prefix_unused
= true;
2486 case Attr_Null_Parameter
:
2487 /* This is just a zero cast to the pointer type for our prefix and
2489 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2491 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
2492 convert (build_pointer_type (gnu_result_type
),
2493 integer_zero_node
));
2496 case Attr_Mechanism_Code
:
2498 Entity_Id gnat_obj
= Entity (gnat_prefix
);
2501 prefix_unused
= true;
2502 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2503 if (Present (Expressions (gnat_node
)))
2505 int i
= UI_To_Int (Intval (First (Expressions (gnat_node
))));
2507 for (gnat_obj
= First_Formal (gnat_obj
); i
> 1;
2508 i
--, gnat_obj
= Next_Formal (gnat_obj
))
2512 code
= Mechanism (gnat_obj
);
2513 if (code
== Default
)
2514 code
= ((present_gnu_tree (gnat_obj
)
2515 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj
))
2516 || ((TREE_CODE (get_gnu_tree (gnat_obj
))
2518 && (DECL_BY_COMPONENT_PTR_P
2519 (get_gnu_tree (gnat_obj
))))))
2520 ? By_Reference
: By_Copy
);
2521 gnu_result
= convert (gnu_result_type
, size_int (- code
));
2526 /* We treat Model as identical to Machine. This is true for at least
2527 IEEE and some other nice floating-point systems. */
2529 /* ... fall through ... */
2532 /* The trick is to force the compiler to store the result in memory so
2533 that we do not have extra precision used. But do this only when this
2534 is necessary, i.e. if FP_ARITH_MAY_WIDEN is true and the precision of
2535 the type is lower than that of the longest floating-point type. */
2536 prefix_unused
= true;
2537 gnu_expr
= gnat_to_gnu (First (Expressions (gnat_node
)));
2538 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2539 gnu_result
= convert (gnu_result_type
, gnu_expr
);
2541 if (TREE_CODE (gnu_result
) != REAL_CST
2542 && fp_arith_may_widen
2543 && TYPE_PRECISION (gnu_result_type
)
2544 < TYPE_PRECISION (longest_float_type_node
))
2546 tree rec_type
= make_node (RECORD_TYPE
);
2548 = create_field_decl (get_identifier ("OBJ"), gnu_result_type
,
2549 rec_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2550 tree rec_val
, asm_expr
;
2552 finish_record_type (rec_type
, field
, 0, false);
2554 rec_val
= build_constructor_single (rec_type
, field
, gnu_result
);
2555 rec_val
= build1 (SAVE_EXPR
, rec_type
, rec_val
);
2558 = build5 (ASM_EXPR
, void_type_node
,
2559 build_string (0, ""),
2560 tree_cons (build_tree_list (NULL_TREE
,
2561 build_string (2, "=m")),
2562 rec_val
, NULL_TREE
),
2563 tree_cons (build_tree_list (NULL_TREE
,
2564 build_string (1, "m")),
2565 rec_val
, NULL_TREE
),
2566 NULL_TREE
, NULL_TREE
);
2567 ASM_VOLATILE_P (asm_expr
) = 1;
2570 = build_compound_expr (gnu_result_type
, asm_expr
,
2571 build_component_ref (rec_val
, field
,
2577 prefix_unused
= true;
2578 gnu_expr
= gnat_to_gnu (First (Expressions (gnat_node
)));
2579 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
2580 /* This can be a random address so build an alias-all pointer type. */
2582 = convert (build_pointer_type_for_mode (gnu_result_type
, ptr_mode
,
2585 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_expr
);
2589 /* This abort means that we have an unimplemented attribute. */
2593 /* If this is an attribute where the prefix was unused, force a use of it if
2594 it has a side-effect. But don't do it if the prefix is just an entity
2595 name. However, if an access check is needed, we must do it. See second
2596 example in AARM 11.6(5.e). */
2598 && TREE_SIDE_EFFECTS (gnu_prefix
)
2599 && !Is_Entity_Name (gnat_prefix
))
2601 = build_compound_expr (TREE_TYPE (gnu_result
), gnu_prefix
, gnu_result
);
2603 *gnu_result_type_p
= gnu_result_type
;
2607 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Case_Statement, to a
2608 GCC tree, which is returned. */
2611 Case_Statement_to_gnu (Node_Id gnat_node
)
2613 tree gnu_result
, gnu_expr
, gnu_type
, gnu_label
;
2615 location_t end_locus
;
2616 bool may_fallthru
= false;
2618 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
2619 gnu_expr
= convert (get_base_type (TREE_TYPE (gnu_expr
)), gnu_expr
);
2620 gnu_expr
= maybe_character_value (gnu_expr
);
2621 gnu_type
= TREE_TYPE (gnu_expr
);
2623 /* We build a SWITCH_EXPR that contains the code with interspersed
2624 CASE_LABEL_EXPRs for each label. */
2625 if (!Sloc_to_locus (End_Location (gnat_node
), &end_locus
))
2626 end_locus
= input_location
;
2627 gnu_label
= create_artificial_label (end_locus
);
2628 start_stmt_group ();
2630 for (gnat_when
= First_Non_Pragma (Alternatives (gnat_node
));
2631 Present (gnat_when
);
2632 gnat_when
= Next_Non_Pragma (gnat_when
))
2634 bool choices_added_p
= false;
2635 Node_Id gnat_choice
;
2637 /* First compile all the different case choices for the current WHEN
2639 for (gnat_choice
= First (Discrete_Choices (gnat_when
));
2640 Present (gnat_choice
);
2641 gnat_choice
= Next (gnat_choice
))
2643 tree gnu_low
= NULL_TREE
, gnu_high
= NULL_TREE
;
2644 tree label
= create_artificial_label (input_location
);
2646 switch (Nkind (gnat_choice
))
2649 gnu_low
= gnat_to_gnu (Low_Bound (gnat_choice
));
2650 gnu_high
= gnat_to_gnu (High_Bound (gnat_choice
));
2653 case N_Subtype_Indication
:
2654 gnu_low
= gnat_to_gnu (Low_Bound (Range_Expression
2655 (Constraint (gnat_choice
))));
2656 gnu_high
= gnat_to_gnu (High_Bound (Range_Expression
2657 (Constraint (gnat_choice
))));
2661 case N_Expanded_Name
:
2662 /* This represents either a subtype range or a static value of
2663 some kind; Ekind says which. */
2664 if (Is_Type (Entity (gnat_choice
)))
2666 tree gnu_type
= get_unpadded_type (Entity (gnat_choice
));
2668 gnu_low
= TYPE_MIN_VALUE (gnu_type
);
2669 gnu_high
= TYPE_MAX_VALUE (gnu_type
);
2673 /* ... fall through ... */
2675 case N_Character_Literal
:
2676 case N_Integer_Literal
:
2677 gnu_low
= gnat_to_gnu (gnat_choice
);
2680 case N_Others_Choice
:
2687 /* Everything should be folded into constants at this point. */
2688 gcc_assert (!gnu_low
|| TREE_CODE (gnu_low
) == INTEGER_CST
);
2689 gcc_assert (!gnu_high
|| TREE_CODE (gnu_high
) == INTEGER_CST
);
2691 if (gnu_low
&& TREE_TYPE (gnu_low
) != gnu_type
)
2692 gnu_low
= convert (gnu_type
, gnu_low
);
2693 if (gnu_high
&& TREE_TYPE (gnu_high
) != gnu_type
)
2694 gnu_high
= convert (gnu_type
, gnu_high
);
2696 add_stmt_with_node (build_case_label (gnu_low
, gnu_high
, label
),
2698 choices_added_p
= true;
2701 /* This construct doesn't define a scope so we shouldn't push a binding
2702 level around the statement list. Except that we have always done so
2703 historically and this makes it possible to reduce stack usage. As a
2704 compromise, we keep doing it for case statements, for which this has
2705 never been problematic, but not for case expressions in Ada 2012. */
2706 if (choices_added_p
)
2708 const bool case_expr_p
= From_Conditional_Expression (gnat_node
);
2709 tree group
= build_stmt_group (Statements (gnat_when
), !case_expr_p
);
2710 const bool group_may_fallthru
= block_may_fallthru (group
);
2712 if (group_may_fallthru
)
2714 tree stmt
= build1 (GOTO_EXPR
, void_type_node
, gnu_label
);
2715 SET_EXPR_LOCATION (stmt
, end_locus
);
2717 may_fallthru
= true;
2722 /* Now emit a definition of the label the cases branch to, if any. */
2724 add_stmt (build1 (LABEL_EXPR
, void_type_node
, gnu_label
));
2725 gnu_result
= build2 (SWITCH_EXPR
, gnu_type
, gnu_expr
, end_stmt_group ());
2730 /* Return true if we are in the body of a loop. */
2733 inside_loop_p (void)
2735 return !vec_safe_is_empty (gnu_loop_stack
);
2738 /* Find out whether EXPR is a simple additive expression based on the iteration
2739 variable of some enclosing loop in the current function. If so, return the
2740 loop and set *DISP to the displacement and *NEG_P to true if this is for a
2741 subtraction; otherwise, return NULL. */
2743 static struct loop_info_d
*
2744 find_loop_for (tree expr
, tree
*disp
, bool *neg_p
)
2748 struct loop_info_d
*iter
= NULL
;
2751 if (is_simple_additive_expression (expr
, &add
, &cst
, &minus_p
))
2768 var
= remove_conversions (var
, false);
2770 if (TREE_CODE (var
) != VAR_DECL
)
2773 gcc_checking_assert (vec_safe_length (gnu_loop_stack
) > 0);
2775 FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack
, i
, iter
)
2776 if (iter
->loop_var
== var
&& iter
->fndecl
== current_function_decl
)
2782 /* Return the innermost enclosing loop in the current function. */
2784 static struct loop_info_d
*
2787 struct loop_info_d
*iter
= NULL
;
2790 gcc_checking_assert (vec_safe_length (gnu_loop_stack
) > 0);
2792 FOR_EACH_VEC_ELT_REVERSE (*gnu_loop_stack
, i
, iter
)
2793 if (iter
->fndecl
== current_function_decl
)
2799 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2800 false, or the maximum value if MAX is true, of TYPE. */
2803 can_equal_min_or_max_val_p (tree val
, tree type
, bool max
)
2805 tree min_or_max_val
= (max
? TYPE_MAX_VALUE (type
) : TYPE_MIN_VALUE (type
));
2807 if (TREE_CODE (min_or_max_val
) != INTEGER_CST
)
2810 if (TREE_CODE (val
) == NOP_EXPR
)
2812 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val
, 0)))
2813 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val
, 0))));
2815 if (TREE_CODE (val
) != INTEGER_CST
)
2819 return tree_int_cst_lt (val
, min_or_max_val
) == 0;
2821 return tree_int_cst_lt (min_or_max_val
, val
) == 0;
2824 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2825 If REVERSE is true, minimum value is taken as maximum value. */
2828 can_equal_min_val_p (tree val
, tree type
, bool reverse
)
2830 return can_equal_min_or_max_val_p (val
, type
, reverse
);
2833 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2834 If REVERSE is true, maximum value is taken as minimum value. */
2837 can_equal_max_val_p (tree val
, tree type
, bool reverse
)
2839 return can_equal_min_or_max_val_p (val
, type
, !reverse
);
2842 /* Replace EXPR1 and EXPR2 by invariant expressions if possible. Return
2843 true if both expressions have been replaced and false otherwise. */
2846 make_invariant (tree
*expr1
, tree
*expr2
)
2848 tree inv_expr1
= gnat_invariant_expr (*expr1
);
2849 tree inv_expr2
= gnat_invariant_expr (*expr2
);
2857 return inv_expr1
&& inv_expr2
;
2860 /* Helper function for walk_tree, used by independent_iterations_p below. */
2863 scan_rhs_r (tree
*tp
, int *walk_subtrees
, void *data
)
2865 bitmap
*params
= (bitmap
*)data
;
2868 /* No need to walk into types or decls. */
2869 if (IS_TYPE_OR_DECL_P (t
))
2872 if (TREE_CODE (t
) == PARM_DECL
&& bitmap_bit_p (*params
, DECL_UID (t
)))
2878 /* Return true if STMT_LIST generates independent iterations in a loop. */
2881 independent_iterations_p (tree stmt_list
)
2883 tree_stmt_iterator tsi
;
2884 bitmap params
= BITMAP_GGC_ALLOC();
2885 auto_vec
<tree
, 16> rhs
;
2889 if (TREE_CODE (stmt_list
) == BIND_EXPR
)
2890 stmt_list
= BIND_EXPR_BODY (stmt_list
);
2892 /* Scan the list and return false on anything that is not either a check
2893 or an assignment to a parameter with restricted aliasing. */
2894 for (tsi
= tsi_start (stmt_list
); !tsi_end_p (tsi
); tsi_next (&tsi
))
2896 tree stmt
= tsi_stmt (tsi
);
2898 switch (TREE_CODE (stmt
))
2902 if (COND_EXPR_ELSE (stmt
))
2904 if (TREE_CODE (COND_EXPR_THEN (stmt
)) != CALL_EXPR
)
2906 tree func
= get_callee_fndecl (COND_EXPR_THEN (stmt
));
2907 if (!(func
&& TREE_THIS_VOLATILE (func
)))
2914 tree lhs
= TREE_OPERAND (stmt
, 0);
2915 while (handled_component_p (lhs
))
2916 lhs
= TREE_OPERAND (lhs
, 0);
2917 if (TREE_CODE (lhs
) != INDIRECT_REF
)
2919 lhs
= TREE_OPERAND (lhs
, 0);
2920 if (!(TREE_CODE (lhs
) == PARM_DECL
2921 && DECL_RESTRICTED_ALIASING_P (lhs
)))
2923 bitmap_set_bit (params
, DECL_UID (lhs
));
2924 rhs
.safe_push (TREE_OPERAND (stmt
, 1));
2933 /* At this point we know that the list contains only statements that will
2934 modify parameters with restricted aliasing. Check that the statements
2935 don't at the time read from these parameters. */
2936 FOR_EACH_VEC_ELT (rhs
, i
, iter
)
2937 if (walk_tree_without_duplicates (&iter
, scan_rhs_r
, ¶ms
))
2943 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Loop_Statement, to a
2944 GCC tree, which is returned. */
2947 Loop_Statement_to_gnu (Node_Id gnat_node
)
2949 const Node_Id gnat_iter_scheme
= Iteration_Scheme (gnat_node
);
2950 struct loop_info_d
*gnu_loop_info
= ggc_cleared_alloc
<loop_info_d
> ();
2951 tree gnu_loop_stmt
= build4 (LOOP_STMT
, void_type_node
, NULL_TREE
,
2952 NULL_TREE
, NULL_TREE
, NULL_TREE
);
2953 tree gnu_loop_label
= create_artificial_label (input_location
);
2954 tree gnu_cond_expr
= NULL_TREE
, gnu_low
= NULL_TREE
, gnu_high
= NULL_TREE
;
2957 /* Push the loop_info structure associated with the LOOP_STMT. */
2958 gnu_loop_info
->fndecl
= current_function_decl
;
2959 gnu_loop_info
->stmt
= gnu_loop_stmt
;
2960 vec_safe_push (gnu_loop_stack
, gnu_loop_info
);
2962 /* Set location information for statement and end label. */
2963 set_expr_location_from_node (gnu_loop_stmt
, gnat_node
);
2964 Sloc_to_locus (Sloc (End_Label (gnat_node
)),
2965 &DECL_SOURCE_LOCATION (gnu_loop_label
));
2966 LOOP_STMT_LABEL (gnu_loop_stmt
) = gnu_loop_label
;
2968 /* Set the condition under which the loop must keep going. If we have an
2969 explicit condition, use it to set the location information throughout
2970 the translation of the loop statement to avoid having multiple SLOCs.
2972 For the case "LOOP .... END LOOP;" the condition is always true. */
2973 if (No (gnat_iter_scheme
))
2976 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2977 else if (Present (Condition (gnat_iter_scheme
)))
2979 LOOP_STMT_COND (gnu_loop_stmt
)
2980 = gnat_to_gnu (Condition (gnat_iter_scheme
));
2982 set_expr_location_from_node (gnu_loop_stmt
, gnat_iter_scheme
);
2985 /* Otherwise we have an iteration scheme and the condition is given by the
2986 bounds of the subtype of the iteration variable. */
2989 Node_Id gnat_loop_spec
= Loop_Parameter_Specification (gnat_iter_scheme
);
2990 Entity_Id gnat_loop_var
= Defining_Entity (gnat_loop_spec
);
2991 Entity_Id gnat_type
= Etype (gnat_loop_var
);
2992 tree gnu_type
= get_unpadded_type (gnat_type
);
2993 tree gnu_base_type
= maybe_character_type (get_base_type (gnu_type
));
2994 tree gnu_one_node
= build_int_cst (gnu_base_type
, 1);
2995 tree gnu_loop_var
, gnu_loop_iv
, gnu_first
, gnu_last
, gnu_stmt
;
2996 enum tree_code update_code
, test_code
, shift_code
;
2997 bool reverse
= Reverse_Present (gnat_loop_spec
), use_iv
= false;
2999 gnu_low
= convert (gnu_base_type
, TYPE_MIN_VALUE (gnu_type
));
3000 gnu_high
= convert (gnu_base_type
, TYPE_MAX_VALUE (gnu_type
));
3002 /* We must disable modulo reduction for the iteration variable, if any,
3003 in order for the loop comparison to be effective. */
3006 gnu_first
= gnu_high
;
3008 update_code
= MINUS_NOMOD_EXPR
;
3009 test_code
= GE_EXPR
;
3010 shift_code
= PLUS_NOMOD_EXPR
;
3014 gnu_first
= gnu_low
;
3015 gnu_last
= gnu_high
;
3016 update_code
= PLUS_NOMOD_EXPR
;
3017 test_code
= LE_EXPR
;
3018 shift_code
= MINUS_NOMOD_EXPR
;
3021 /* We use two different strategies to translate the loop, depending on
3022 whether optimization is enabled.
3024 If it is, we generate the canonical loop form expected by the loop
3025 optimizer and the loop vectorizer, which is the do-while form:
3034 This avoids an implicit dependency on loop header copying and makes
3035 it possible to turn BOTTOM_COND into an inequality test.
3037 If optimization is disabled, loop header copying doesn't come into
3038 play and we try to generate the loop form with the fewer conditional
3039 branches. First, the default form, which is:
3047 It should catch most loops with constant ending point. Then, if we
3048 cannot, we try to generate the shifted form:
3056 which should catch loops with constant starting point. Otherwise, if
3057 we cannot, we generate the fallback form:
3066 which works in all cases. */
3068 if (optimize
&& !optimize_debug
)
3070 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
3072 if (!can_equal_min_val_p (gnu_first
, gnu_base_type
, reverse
))
3075 /* Otherwise, use the do-while form with the help of a special
3076 induction variable in the unsigned version of the base type
3077 or the unsigned version of the size type, whichever is the
3078 largest, in order to have wrap-around arithmetics for it. */
3081 if (TYPE_PRECISION (gnu_base_type
)
3082 > TYPE_PRECISION (size_type_node
))
3084 = gnat_type_for_size (TYPE_PRECISION (gnu_base_type
), 1);
3086 gnu_base_type
= size_type_node
;
3088 gnu_first
= convert (gnu_base_type
, gnu_first
);
3089 gnu_last
= convert (gnu_base_type
, gnu_last
);
3090 gnu_one_node
= build_int_cst (gnu_base_type
, 1);
3095 = build_binary_op (shift_code
, gnu_base_type
, gnu_first
,
3097 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt
) = 1;
3098 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt
) = 1;
3102 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
3103 if (!can_equal_max_val_p (gnu_last
, gnu_base_type
, reverse
))
3106 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
3108 else if (!can_equal_min_val_p (gnu_first
, gnu_base_type
, reverse
)
3109 && !can_equal_min_val_p (gnu_last
, gnu_base_type
, reverse
))
3112 = build_binary_op (shift_code
, gnu_base_type
, gnu_first
,
3115 = build_binary_op (shift_code
, gnu_base_type
, gnu_last
,
3117 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt
) = 1;
3120 /* Otherwise, use the fallback form. */
3122 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt
) = 1;
3125 /* If we use the BOTTOM_COND, we can turn the test into an inequality
3126 test but we have to add ENTRY_COND to protect the empty loop. */
3127 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt
))
3129 test_code
= NE_EXPR
;
3131 = build3 (COND_EXPR
, void_type_node
,
3132 build_binary_op (LE_EXPR
, boolean_type_node
,
3134 NULL_TREE
, alloc_stmt_list ());
3135 set_expr_location_from_node (gnu_cond_expr
, gnat_iter_scheme
);
3138 /* Open a new nesting level that will surround the loop to declare the
3139 iteration variable. */
3140 start_stmt_group ();
3143 /* If we use the special induction variable, create it and set it to
3144 its initial value. Morever, the regular iteration variable cannot
3145 itself be initialized, lest the initial value wrapped around. */
3149 = create_init_temporary ("I", gnu_first
, &gnu_stmt
, gnat_loop_var
);
3150 add_stmt (gnu_stmt
);
3151 gnu_first
= NULL_TREE
;
3154 gnu_loop_iv
= NULL_TREE
;
3156 /* Declare the iteration variable and set it to its initial value. */
3157 gnu_loop_var
= gnat_to_gnu_entity (gnat_loop_var
, gnu_first
, true);
3158 if (DECL_BY_REF_P (gnu_loop_var
))
3159 gnu_loop_var
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_loop_var
);
3162 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var
));
3163 SET_DECL_INDUCTION_VAR (gnu_loop_var
, gnu_loop_iv
);
3165 gnu_loop_info
->loop_var
= gnu_loop_var
;
3166 gnu_loop_info
->low_bound
= gnu_low
;
3167 gnu_loop_info
->high_bound
= gnu_high
;
3169 /* Do all the arithmetics in the base type. */
3170 gnu_loop_var
= convert (gnu_base_type
, gnu_loop_var
);
3172 /* Set either the top or bottom exit condition. */
3174 LOOP_STMT_COND (gnu_loop_stmt
)
3175 = build_binary_op (test_code
, boolean_type_node
, gnu_loop_iv
,
3178 LOOP_STMT_COND (gnu_loop_stmt
)
3179 = build_binary_op (test_code
, boolean_type_node
, gnu_loop_var
,
3182 /* Set either the top or bottom update statement and give it the source
3183 location of the iteration for better coverage info. */
3187 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_loop_iv
,
3188 build_binary_op (update_code
, gnu_base_type
,
3189 gnu_loop_iv
, gnu_one_node
));
3190 set_expr_location_from_node (gnu_stmt
, gnat_iter_scheme
);
3191 append_to_statement_list (gnu_stmt
,
3192 &LOOP_STMT_UPDATE (gnu_loop_stmt
));
3194 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_loop_var
,
3196 set_expr_location_from_node (gnu_stmt
, gnat_iter_scheme
);
3197 append_to_statement_list (gnu_stmt
,
3198 &LOOP_STMT_UPDATE (gnu_loop_stmt
));
3203 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_loop_var
,
3204 build_binary_op (update_code
, gnu_base_type
,
3205 gnu_loop_var
, gnu_one_node
));
3206 set_expr_location_from_node (gnu_stmt
, gnat_iter_scheme
);
3207 LOOP_STMT_UPDATE (gnu_loop_stmt
) = gnu_stmt
;
3210 set_expr_location_from_node (gnu_loop_stmt
, gnat_iter_scheme
);
3213 /* If the loop was named, have the name point to this loop. In this case,
3214 the association is not a DECL node, but the end label of the loop. */
3215 if (Present (Identifier (gnat_node
)))
3216 save_gnu_tree (Entity (Identifier (gnat_node
)), gnu_loop_label
, true);
3218 /* Make the loop body into its own block, so any allocated storage will be
3219 released every iteration. This is needed for stack allocation. */
3220 LOOP_STMT_BODY (gnu_loop_stmt
)
3221 = build_stmt_group (Statements (gnat_node
), true);
3222 TREE_SIDE_EFFECTS (gnu_loop_stmt
) = 1;
3224 /* If we have an iteration scheme, then we are in a statement group. Add
3225 the LOOP_STMT to it, finish it and make it the "loop". */
3226 if (Present (gnat_iter_scheme
) && No (Condition (gnat_iter_scheme
)))
3228 /* First, if we have computed invariant conditions for range (or index)
3229 checks applied to the iteration variable, find out whether they can
3230 be evaluated to false at compile time; otherwise, if there are not
3231 too many of them, combine them with the original checks. If loop
3232 unswitching is enabled, do not require the loop bounds to be also
3233 invariant, as their evaluation will still be ahead of the loop. */
3234 if (vec_safe_length (gnu_loop_info
->checks
) > 0
3235 && (make_invariant (&gnu_low
, &gnu_high
) || optimize
>= 3))
3237 struct range_check_info_d
*rci
;
3238 unsigned int i
, n_remaining_checks
= 0;
3240 FOR_EACH_VEC_ELT (*gnu_loop_info
->checks
, i
, rci
)
3242 tree low_ok
, high_ok
;
3246 tree gnu_adjusted_low
= convert (rci
->type
, gnu_low
);
3249 = fold_build2 (rci
->neg_p
? MINUS_EXPR
: PLUS_EXPR
,
3250 rci
->type
, gnu_adjusted_low
, rci
->disp
);
3252 = build_binary_op (GE_EXPR
, boolean_type_node
,
3253 gnu_adjusted_low
, rci
->low_bound
);
3256 low_ok
= boolean_true_node
;
3258 if (rci
->high_bound
)
3260 tree gnu_adjusted_high
= convert (rci
->type
, gnu_high
);
3263 = fold_build2 (rci
->neg_p
? MINUS_EXPR
: PLUS_EXPR
,
3264 rci
->type
, gnu_adjusted_high
, rci
->disp
);
3266 = build_binary_op (LE_EXPR
, boolean_type_node
,
3267 gnu_adjusted_high
, rci
->high_bound
);
3270 high_ok
= boolean_true_node
;
3273 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
3277 = build_unary_op (TRUTH_NOT_EXPR
, boolean_type_node
, range_ok
);
3279 if (rci
->invariant_cond
== boolean_false_node
)
3280 TREE_OPERAND (rci
->inserted_cond
, 0) = rci
->invariant_cond
;
3282 n_remaining_checks
++;
3285 /* Note that loop unswitching can only be applied a small number of
3286 times to a given loop (PARAM_MAX_UNSWITCH_LEVEL default to 3). */
3287 if (IN_RANGE (n_remaining_checks
, 1, 3)
3290 FOR_EACH_VEC_ELT (*gnu_loop_info
->checks
, i
, rci
)
3291 if (rci
->invariant_cond
!= boolean_false_node
)
3293 TREE_OPERAND (rci
->inserted_cond
, 0) = rci
->invariant_cond
;
3296 add_stmt_with_node_force (rci
->inserted_cond
, gnat_node
);
3300 /* Second, if we have recorded invariants to be hoisted, emit them. */
3301 if (vec_safe_length (gnu_loop_info
->invariants
) > 0)
3305 FOR_EACH_VEC_ELT (*gnu_loop_info
->invariants
, i
, iter
)
3306 add_stmt_with_node_force (*iter
, gnat_node
);
3309 /* Third, if loop vectorization is enabled and the iterations of the
3310 loop can easily be proved as independent, mark the loop. */
3312 && independent_iterations_p (LOOP_STMT_BODY (gnu_loop_stmt
)))
3313 LOOP_STMT_IVDEP (gnu_loop_stmt
) = 1;
3315 add_stmt (gnu_loop_stmt
);
3317 gnu_loop_stmt
= end_stmt_group ();
3320 /* If we have an outer COND_EXPR, that's our result and this loop is its
3321 "true" statement. Otherwise, the result is the LOOP_STMT. */
3324 COND_EXPR_THEN (gnu_cond_expr
) = gnu_loop_stmt
;
3325 TREE_SIDE_EFFECTS (gnu_cond_expr
) = 1;
3326 gnu_result
= gnu_cond_expr
;
3329 gnu_result
= gnu_loop_stmt
;
3331 gnu_loop_stack
->pop ();
3336 /* This page implements a form of Named Return Value optimization modeled
3337 on the C++ optimization of the same name. The main difference is that
3338 we disregard any semantical considerations when applying it here, the
3339 counterpart being that we don't try to apply it to semantically loaded
3340 return types, i.e. types with the TYPE_BY_REFERENCE_P flag set.
3342 We consider a function body of the following GENERIC form:
3346 RETURN_EXPR [<retval> = ...]
3348 RETURN_EXPR [<retval> = R1]
3352 RETURN_EXPR [<retval> = ...]
3354 RETURN_EXPR [<retval> = Ri]
3357 where the Ri are not addressable and we try to fulfill a simple criterion
3358 that would make it possible to replace one or several Ri variables by the
3359 single RESULT_DECL of the function.
3361 The first observation is that RETURN_EXPRs that don't directly reference
3362 any of the Ri variables on the RHS of their assignment are transparent wrt
3363 the optimization. This is because the Ri variables aren't addressable so
3364 any transformation applied to them doesn't affect the RHS; moreover, the
3365 assignment writes the full <retval> object so existing values are entirely
3368 This property can be extended to some forms of RETURN_EXPRs that reference
3369 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
3370 case, in particular when function calls are involved.
3372 Therefore the algorithm is as follows:
3374 1. Collect the list of candidates for a Named Return Value (Ri variables
3375 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
3376 other expressions on the RHS of such assignments.
3378 2. Prune the members of the first list (candidates) that are referenced
3379 by a member of the second list (expressions).
3381 3. Extract a set of candidates with non-overlapping live ranges from the
3382 first list. These are the Named Return Values.
3384 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
3385 Named Return Values in the function with the RESULT_DECL.
3387 If the function returns an unconstrained type, things are a bit different
3388 because the anonymous return object is allocated on the secondary stack
3389 and RESULT_DECL is only a pointer to it. Each return object can be of a
3390 different size and is allocated separately so we need not care about the
3391 addressability and the aforementioned overlapping issues. Therefore, we
3392 don't collect the other expressions and skip step #2 in the algorithm. */
3399 hash_set
<tree
> *visited
;
3402 /* Return true if T is a Named Return Value. */
3405 is_nrv_p (bitmap nrv
, tree t
)
3407 return VAR_P (t
) && bitmap_bit_p (nrv
, DECL_UID (t
));
3410 /* Helper function for walk_tree, used by finalize_nrv below. */
3413 prune_nrv_r (tree
*tp
, int *walk_subtrees
, void *data
)
3415 struct nrv_data
*dp
= (struct nrv_data
*)data
;
3418 /* No need to walk into types or decls. */
3419 if (IS_TYPE_OR_DECL_P (t
))
3422 if (is_nrv_p (dp
->nrv
, t
))
3423 bitmap_clear_bit (dp
->nrv
, DECL_UID (t
));
3428 /* Prune Named Return Values in BLOCK and return true if there is still a
3429 Named Return Value in BLOCK or one of its sub-blocks. */
3432 prune_nrv_in_block (bitmap nrv
, tree block
)
3434 bool has_nrv
= false;
3437 /* First recurse on the sub-blocks. */
3438 for (t
= BLOCK_SUBBLOCKS (block
); t
; t
= BLOCK_CHAIN (t
))
3439 has_nrv
|= prune_nrv_in_block (nrv
, t
);
3441 /* Then make sure to keep at most one NRV per block. */
3442 for (t
= BLOCK_VARS (block
); t
; t
= DECL_CHAIN (t
))
3443 if (is_nrv_p (nrv
, t
))
3446 bitmap_clear_bit (nrv
, DECL_UID (t
));
3454 /* Helper function for walk_tree, used by finalize_nrv below. */
3457 finalize_nrv_r (tree
*tp
, int *walk_subtrees
, void *data
)
3459 struct nrv_data
*dp
= (struct nrv_data
*)data
;
3462 /* No need to walk into types. */
3466 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
3467 nop, but differs from using NULL_TREE in that it indicates that we care
3468 about the value of the RESULT_DECL. */
3469 else if (TREE_CODE (t
) == RETURN_EXPR
3470 && TREE_CODE (TREE_OPERAND (t
, 0)) == INIT_EXPR
)
3472 tree ret_val
= TREE_OPERAND (TREE_OPERAND (t
, 0), 1);
3474 /* Strip useless conversions around the return value. */
3475 if (gnat_useless_type_conversion (ret_val
))
3476 ret_val
= TREE_OPERAND (ret_val
, 0);
3478 if (is_nrv_p (dp
->nrv
, ret_val
))
3479 TREE_OPERAND (t
, 0) = dp
->result
;
3482 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
3484 else if (TREE_CODE (t
) == DECL_EXPR
3485 && is_nrv_p (dp
->nrv
, DECL_EXPR_DECL (t
)))
3487 tree var
= DECL_EXPR_DECL (t
), init
;
3489 if (DECL_INITIAL (var
))
3491 init
= build_binary_op (INIT_EXPR
, NULL_TREE
, dp
->result
,
3492 DECL_INITIAL (var
));
3493 SET_EXPR_LOCATION (init
, EXPR_LOCATION (t
));
3494 DECL_INITIAL (var
) = NULL_TREE
;
3497 init
= build_empty_stmt (EXPR_LOCATION (t
));
3500 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
3501 SET_DECL_VALUE_EXPR (var
, dp
->result
);
3502 DECL_HAS_VALUE_EXPR_P (var
) = 1;
3503 /* ??? Kludge to avoid an assertion failure during inlining. */
3504 DECL_SIZE (var
) = bitsize_unit_node
;
3505 DECL_SIZE_UNIT (var
) = size_one_node
;
3508 /* And replace all uses of NRVs with the RESULT_DECL. */
3509 else if (is_nrv_p (dp
->nrv
, t
))
3510 *tp
= convert (TREE_TYPE (t
), dp
->result
);
3512 /* Avoid walking into the same tree more than once. Unfortunately, we
3513 can't just use walk_tree_without_duplicates because it would only
3514 call us for the first occurrence of NRVs in the function body. */
3515 if (dp
->visited
->add (*tp
))
3521 /* Likewise, but used when the function returns an unconstrained type. */
3524 finalize_nrv_unc_r (tree
*tp
, int *walk_subtrees
, void *data
)
3526 struct nrv_data
*dp
= (struct nrv_data
*)data
;
3529 /* No need to walk into types. */
3533 /* We need to see the DECL_EXPR of NRVs before any other references so we
3534 walk the body of BIND_EXPR before walking its variables. */
3535 else if (TREE_CODE (t
) == BIND_EXPR
)
3536 walk_tree (&BIND_EXPR_BODY (t
), finalize_nrv_unc_r
, data
, NULL
);
3538 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
3539 return value built by the allocator instead of the whole construct. */
3540 else if (TREE_CODE (t
) == RETURN_EXPR
3541 && TREE_CODE (TREE_OPERAND (t
, 0)) == INIT_EXPR
)
3543 tree ret_val
= TREE_OPERAND (TREE_OPERAND (t
, 0), 1);
3545 /* This is the construct returned by the allocator. */
3546 if (TREE_CODE (ret_val
) == COMPOUND_EXPR
3547 && TREE_CODE (TREE_OPERAND (ret_val
, 0)) == INIT_EXPR
)
3549 tree rhs
= TREE_OPERAND (TREE_OPERAND (ret_val
, 0), 1);
3551 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val
)))
3552 ret_val
= CONSTRUCTOR_ELT (rhs
, 1)->value
;
3557 /* Strip useless conversions around the return value. */
3558 if (gnat_useless_type_conversion (ret_val
)
3559 || TREE_CODE (ret_val
) == VIEW_CONVERT_EXPR
)
3560 ret_val
= TREE_OPERAND (ret_val
, 0);
3562 /* Strip unpadding around the return value. */
3563 if (TREE_CODE (ret_val
) == COMPONENT_REF
3564 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val
, 0))))
3565 ret_val
= TREE_OPERAND (ret_val
, 0);
3567 /* Assign the new return value to the RESULT_DECL. */
3568 if (is_nrv_p (dp
->nrv
, ret_val
))
3569 TREE_OPERAND (TREE_OPERAND (t
, 0), 1)
3570 = TREE_OPERAND (DECL_INITIAL (ret_val
), 0);
3573 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
3574 into a new variable. */
3575 else if (TREE_CODE (t
) == DECL_EXPR
3576 && is_nrv_p (dp
->nrv
, DECL_EXPR_DECL (t
)))
3578 tree saved_current_function_decl
= current_function_decl
;
3579 tree var
= DECL_EXPR_DECL (t
);
3580 tree alloc
, p_array
, new_var
, new_ret
;
3581 vec
<constructor_elt
, va_gc
> *v
;
3584 /* Create an artificial context to build the allocation. */
3585 current_function_decl
= decl_function_context (var
);
3586 start_stmt_group ();
3589 /* This will return a COMPOUND_EXPR with the allocation in the first
3590 arm and the final return value in the second arm. */
3591 alloc
= build_allocator (TREE_TYPE (var
), DECL_INITIAL (var
),
3592 TREE_TYPE (dp
->result
),
3593 Procedure_To_Call (dp
->gnat_ret
),
3594 Storage_Pool (dp
->gnat_ret
),
3597 /* The new variable is built as a reference to the allocated space. */
3599 = build_decl (DECL_SOURCE_LOCATION (var
), VAR_DECL
, DECL_NAME (var
),
3600 build_reference_type (TREE_TYPE (var
)));
3601 DECL_BY_REFERENCE (new_var
) = 1;
3603 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc
)))
3605 tree cst
= TREE_OPERAND (alloc
, 1);
3607 /* The new initial value is a COMPOUND_EXPR with the allocation in
3608 the first arm and the value of P_ARRAY in the second arm. */
3609 DECL_INITIAL (new_var
)
3610 = build2 (COMPOUND_EXPR
, TREE_TYPE (new_var
),
3611 TREE_OPERAND (alloc
, 0),
3612 CONSTRUCTOR_ELT (cst
, 0)->value
);
3614 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
3615 p_array
= TYPE_FIELDS (TREE_TYPE (alloc
));
3616 CONSTRUCTOR_APPEND_ELT (v
, p_array
,
3617 fold_convert (TREE_TYPE (p_array
), new_var
));
3618 CONSTRUCTOR_APPEND_ELT (v
, DECL_CHAIN (p_array
),
3619 CONSTRUCTOR_ELT (cst
, 1)->value
);
3620 new_ret
= build_constructor (TREE_TYPE (alloc
), v
);
3624 /* The new initial value is just the allocation. */
3625 DECL_INITIAL (new_var
) = alloc
;
3626 new_ret
= fold_convert (TREE_TYPE (alloc
), new_var
);
3629 gnat_pushdecl (new_var
, Empty
);
3631 /* Destroy the artificial context and insert the new statements. */
3633 *tp
= end_stmt_group ();
3634 current_function_decl
= saved_current_function_decl
;
3636 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
3637 DECL_CHAIN (new_var
) = DECL_CHAIN (var
);
3638 DECL_CHAIN (var
) = new_var
;
3639 DECL_IGNORED_P (var
) = 1;
3641 /* Save the new return value and the dereference of NEW_VAR. */
3643 = build2 (COMPOUND_EXPR
, TREE_TYPE (var
), new_ret
,
3644 build1 (INDIRECT_REF
, TREE_TYPE (var
), new_var
));
3645 /* ??? Kludge to avoid messing up during inlining. */
3646 DECL_CONTEXT (var
) = NULL_TREE
;
3649 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
3650 else if (is_nrv_p (dp
->nrv
, t
))
3651 *tp
= TREE_OPERAND (DECL_INITIAL (t
), 1);
3653 /* Avoid walking into the same tree more than once. Unfortunately, we
3654 can't just use walk_tree_without_duplicates because it would only
3655 call us for the first occurrence of NRVs in the function body. */
3656 if (dp
->visited
->add (*tp
))
3662 /* Apply FUNC to all the sub-trees of nested functions in NODE. FUNC is called
3663 with the DATA and the address of each sub-tree. If FUNC returns a non-NULL
3664 value, the traversal is stopped. */
3667 walk_nesting_tree (struct cgraph_node
*node
, walk_tree_fn func
, void *data
)
3669 for (node
= first_nested_function (node
);
3670 node
; node
= next_nested_function (node
))
3672 walk_tree_without_duplicates (&DECL_SAVED_TREE (node
->decl
), func
, data
);
3673 walk_nesting_tree (node
, func
, data
);
3677 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
3678 contains the candidates for Named Return Value and OTHER is a list of
3679 the other return values. GNAT_RET is a representative return node. */
3682 finalize_nrv (tree fndecl
, bitmap nrv
, vec
<tree
, va_gc
> *other
, Node_Id gnat_ret
)
3684 struct nrv_data data
;
3689 /* We shouldn't be applying the optimization to return types that we aren't
3690 allowed to manipulate freely. */
3691 gcc_assert (!TYPE_IS_BY_REFERENCE_P (TREE_TYPE (TREE_TYPE (fndecl
))));
3693 /* Prune the candidates that are referenced by other return values. */
3695 data
.result
= NULL_TREE
;
3696 data
.gnat_ret
= Empty
;
3697 data
.visited
= NULL
;
3698 FOR_EACH_VEC_SAFE_ELT (other
, i
, iter
)
3699 walk_tree_without_duplicates (&iter
, prune_nrv_r
, &data
);
3700 if (bitmap_empty_p (nrv
))
3703 /* Prune also the candidates that are referenced by nested functions. */
3704 walk_nesting_tree (cgraph_node::get_create (fndecl
), prune_nrv_r
, &data
);
3705 if (bitmap_empty_p (nrv
))
3708 /* Extract a set of NRVs with non-overlapping live ranges. */
3709 if (!prune_nrv_in_block (nrv
, DECL_INITIAL (fndecl
)))
3712 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3714 data
.result
= DECL_RESULT (fndecl
);
3715 data
.gnat_ret
= gnat_ret
;
3716 data
.visited
= new hash_set
<tree
>;
3717 if (TYPE_RETURN_BY_DIRECT_REF_P (TREE_TYPE (fndecl
)))
3718 func
= finalize_nrv_unc_r
;
3720 func
= finalize_nrv_r
;
3721 walk_tree (&DECL_SAVED_TREE (fndecl
), func
, &data
, NULL
);
3722 delete data
.visited
;
3725 /* Return true if RET_VAL can be used as a Named Return Value for the
3726 anonymous return object RET_OBJ. */
3729 return_value_ok_for_nrv_p (tree ret_obj
, tree ret_val
)
3731 if (TREE_CODE (ret_val
) != VAR_DECL
)
3734 if (TREE_THIS_VOLATILE (ret_val
))
3737 if (DECL_CONTEXT (ret_val
) != current_function_decl
)
3740 if (TREE_STATIC (ret_val
))
3743 /* For the constrained case, test for addressability. */
3744 if (ret_obj
&& TREE_ADDRESSABLE (ret_val
))
3747 /* For the constrained case, test for overalignment. */
3748 if (ret_obj
&& DECL_ALIGN (ret_val
) > DECL_ALIGN (ret_obj
))
3751 /* For the unconstrained case, test for bogus initialization. */
3753 && DECL_INITIAL (ret_val
)
3754 && TREE_CODE (DECL_INITIAL (ret_val
)) == NULL_EXPR
)
3760 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3761 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3762 around RESULT_OBJ, which may be null in this case. */
3765 build_return_expr (tree ret_obj
, tree ret_val
)
3771 /* The gimplifier explicitly enforces the following invariant:
3780 As a consequence, type consistency dictates that we use the type
3781 of the RET_OBJ as the operation type. */
3782 tree operation_type
= TREE_TYPE (ret_obj
);
3784 /* Convert the right operand to the operation type. Note that this is
3785 the transformation applied in the INIT_EXPR case of build_binary_op,
3786 with the assumption that the type cannot involve a placeholder. */
3787 if (operation_type
!= TREE_TYPE (ret_val
))
3788 ret_val
= convert (operation_type
, ret_val
);
3790 /* We always can use an INIT_EXPR for the return object. */
3791 result_expr
= build2 (INIT_EXPR
, void_type_node
, ret_obj
, ret_val
);
3793 /* If the function returns an aggregate type, find out whether this is
3794 a candidate for Named Return Value. If so, record it. Otherwise,
3795 if this is an expression of some kind, record it elsewhere. */
3798 && AGGREGATE_TYPE_P (operation_type
)
3799 && !TYPE_IS_FAT_POINTER_P (operation_type
)
3800 && TYPE_MODE (operation_type
) == BLKmode
3801 && aggregate_value_p (operation_type
, current_function_decl
))
3803 /* Strip useless conversions around the return value. */
3804 if (gnat_useless_type_conversion (ret_val
))
3805 ret_val
= TREE_OPERAND (ret_val
, 0);
3807 /* Now apply the test to the return value. */
3808 if (return_value_ok_for_nrv_p (ret_obj
, ret_val
))
3810 if (!f_named_ret_val
)
3811 f_named_ret_val
= BITMAP_GGC_ALLOC ();
3812 bitmap_set_bit (f_named_ret_val
, DECL_UID (ret_val
));
3815 /* Note that we need not care about CONSTRUCTORs here, as they are
3816 totally transparent given the read-compose-write semantics of
3817 assignments from CONSTRUCTORs. */
3818 else if (EXPR_P (ret_val
))
3819 vec_safe_push (f_other_ret_val
, ret_val
);
3823 result_expr
= ret_obj
;
3825 return build1 (RETURN_EXPR
, void_type_node
, result_expr
);
3828 /* Subroutine of gnat_to_gnu to translate the At_End_Proc of GNAT_NODE, an
3829 N_Block_Statement or N_Handled_Sequence_Of_Statements or N_*_Body node.
3831 To invoked the GCC mechanism, we call add_cleanup and when we leave the
3832 group, end_stmt_group will create the TRY_FINALLY_EXPR construct. */
3835 At_End_Proc_to_gnu (Node_Id gnat_node
)
3837 tree proc_decl
= gnat_to_gnu (At_End_Proc (gnat_node
));
3838 Node_Id gnat_end_label
;
3840 /* When not optimizing, disable inlining of finalizers as this can
3841 create a more complex CFG in the parent function. */
3842 if (!optimize
|| optimize_debug
)
3843 DECL_DECLARED_INLINE_P (proc_decl
) = 0;
3845 /* Retrieve the end label attached to the node, if any. */
3846 if (Nkind (gnat_node
) == N_Handled_Sequence_Of_Statements
)
3847 gnat_end_label
= End_Label (gnat_node
);
3848 else if (Present (Handled_Statement_Sequence (gnat_node
)))
3849 gnat_end_label
= End_Label (Handled_Statement_Sequence (gnat_node
));
3851 gnat_end_label
= Empty
;
3853 /* If there is no end label attached, we use the location of the At_End
3854 procedure because Expand_Cleanup_Actions might reset the location of
3855 the enclosing construct to that of an inner statement. */
3856 add_cleanup (build_call_n_expr (proc_decl
, 0),
3857 Present (gnat_end_label
)
3858 ? gnat_end_label
: At_End_Proc (gnat_node
));
3861 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Subprogram_Body. */
3864 Subprogram_Body_to_gnu (Node_Id gnat_node
)
3866 /* The defining identifier for the subprogram body. Note that if a
3867 specification has appeared before for this body, then the identifier
3868 occurring in that specification will also be a defining identifier
3869 and calls to this subprogram will point to that specification. */
3870 Entity_Id gnat_subprog
3871 = (Present (Corresponding_Spec (gnat_node
))
3872 ? Corresponding_Spec (gnat_node
) : Defining_Entity (gnat_node
));
3873 /* The FUNCTION_DECL node corresponding to the defining identifier. */
3875 /* Its RESULT_DECL node. */
3876 tree gnu_result_decl
;
3877 /* Its FUNCTION_TYPE node. */
3878 tree gnu_subprog_type
;
3879 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3881 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3882 tree gnu_return_var_elmt
;
3883 /* Its source location. */
3886 /* If this is a generic subprogram or it has been eliminated, ignore it. */
3887 if (Is_Generic_Subprogram (gnat_subprog
) || Is_Eliminated (gnat_subprog
))
3890 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3891 the already-elaborated tree node. However, if this subprogram had its
3892 elaboration deferred, we will already have made a tree node for it. So
3893 treat it as not being defined in that case. Such a subprogram cannot
3894 have an address clause or a freeze node, so this test is safe, though it
3895 does disable some otherwise-useful error checking. */
3897 = gnat_to_gnu_entity (gnat_subprog
, NULL_TREE
,
3898 Acts_As_Spec (gnat_node
)
3899 && !present_gnu_tree (gnat_subprog
));
3900 DECL_FUNCTION_IS_DEF (gnu_subprog
) = true;
3901 gnu_result_decl
= DECL_RESULT (gnu_subprog
);
3902 gnu_subprog_type
= TREE_TYPE (gnu_subprog
);
3903 gnu_cico_list
= TYPE_CI_CO_LIST (gnu_subprog_type
);
3904 if (gnu_cico_list
&& TREE_VALUE (gnu_cico_list
) == void_type_node
)
3905 gnu_return_var_elmt
= gnu_cico_list
;
3907 gnu_return_var_elmt
= NULL_TREE
;
3909 /* If the function returns by invisible reference, make it explicit in the
3910 function body, but beware that maybe_make_gnu_thunk may already have done
3911 it if the function is inlined across units. See gnat_to_gnu_subprog_type
3912 for more details. */
3913 if (TREE_ADDRESSABLE (gnu_subprog_type
)
3914 && TREE_CODE (TREE_TYPE (gnu_result_decl
)) != REFERENCE_TYPE
)
3916 TREE_TYPE (gnu_result_decl
)
3917 = build_reference_type (TREE_TYPE (gnu_result_decl
));
3918 relayout_decl (gnu_result_decl
);
3921 /* Set the line number in the decl to correspond to that of the body. */
3922 if (DECL_IGNORED_P (gnu_subprog
))
3923 locus
= UNKNOWN_LOCATION
;
3924 else if (!Sloc_to_locus (Sloc (gnat_node
), &locus
, false, gnu_subprog
))
3925 locus
= input_location
;
3926 DECL_SOURCE_LOCATION (gnu_subprog
) = locus
;
3928 /* Try to create a bona-fide thunk and hand it over to the middle-end. */
3929 if (Is_Thunk (gnat_subprog
)
3930 && !Is_Secondary_Stack_Thunk (gnat_subprog
)
3931 && maybe_make_gnu_thunk (gnat_subprog
, gnu_subprog
))
3934 /* Initialize the information structure for the function. */
3935 allocate_struct_function (gnu_subprog
, false);
3936 language_function
*gnu_subprog_lang
= ggc_cleared_alloc
<language_function
> ();
3937 DECL_STRUCT_FUNCTION (gnu_subprog
)->language
= gnu_subprog_lang
;
3938 DECL_STRUCT_FUNCTION (gnu_subprog
)->function_start_locus
= locus
;
3941 begin_subprog_body (gnu_subprog
);
3943 /* If there are copy-in/copy-out parameters, we need to ensure that they are
3944 properly copied out by the return statement. We do this by making a new
3945 block and converting any return into a goto to a label at the end of the
3949 tree gnu_return_var
;
3951 vec_safe_push (gnu_return_label_stack
,
3952 create_artificial_label (input_location
));
3954 start_stmt_group ();
3957 /* If this is a function with copy-in/copy-out parameters and which does
3958 not return by invisible reference, we also need a variable for the
3959 return value to be placed. */
3960 if (gnu_return_var_elmt
&& !TREE_ADDRESSABLE (gnu_subprog_type
))
3962 tree gnu_return_type
3963 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt
));
3966 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE
,
3967 gnu_return_type
, NULL_TREE
,
3968 false, false, false, false, false,
3969 true, false, NULL
, gnat_subprog
);
3970 TREE_VALUE (gnu_return_var_elmt
) = gnu_return_var
;
3973 gnu_return_var
= NULL_TREE
;
3975 vec_safe_push (gnu_return_var_stack
, gnu_return_var
);
3977 /* See whether there are parameters for which we don't have a GCC tree
3978 yet. These must be Out parameters. Make a VAR_DECL for them and
3979 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3980 We can match up the entries because TYPE_CI_CO_LIST is in the order
3981 of the parameters. */
3982 for (Entity_Id gnat_param
= First_Formal_With_Extras (gnat_subprog
);
3983 Present (gnat_param
);
3984 gnat_param
= Next_Formal_With_Extras (gnat_param
))
3985 if (!present_gnu_tree (gnat_param
))
3987 tree gnu_cico_entry
= gnu_cico_list
;
3990 /* Skip any entries that have been already filled in; they must
3991 correspond to In Out parameters. */
3992 while (gnu_cico_entry
&& TREE_VALUE (gnu_cico_entry
))
3993 gnu_cico_entry
= TREE_CHAIN (gnu_cico_entry
);
3995 /* Do any needed dereferences for by-ref objects. */
3996 gnu_decl
= gnat_to_gnu_entity (gnat_param
, NULL_TREE
, true);
3997 gcc_assert (DECL_P (gnu_decl
));
3998 if (DECL_BY_REF_P (gnu_decl
))
3999 gnu_decl
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_decl
);
4001 /* Do any needed references for padded types. */
4002 TREE_VALUE (gnu_cico_entry
)
4003 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry
)), gnu_decl
);
4007 vec_safe_push (gnu_return_label_stack
, NULL_TREE
);
4009 /* Get a tree corresponding to the code for the subprogram. */
4010 start_stmt_group ();
4013 /* First translate the declarations of the subprogram. */
4014 process_decls (Declarations (gnat_node
), Empty
, true, true);
4016 /* Then generate the code of the subprogram itself. A return statement will
4017 be present and any Out parameters will be handled there. */
4018 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
4020 /* Process the At_End_Proc, if any. */
4021 if (Present (At_End_Proc (gnat_node
)))
4022 At_End_Proc_to_gnu (gnat_node
);
4025 tree gnu_result
= end_stmt_group ();
4027 /* Attempt setting the end_locus of our GCC body tree, typically a BIND_EXPR,
4028 then the end_locus of our GCC subprogram declaration tree. */
4029 set_end_locus_from_node (gnu_result
, gnat_node
);
4030 set_end_locus_from_node (gnu_subprog
, gnat_node
);
4032 /* If we populated the parameter attributes cache, we need to make sure that
4033 the cached expressions are evaluated on all the possible paths leading to
4034 their uses. So we force their evaluation on entry of the function. */
4035 vec
<parm_attr
, va_gc
> *cache
= gnu_subprog_lang
->parm_attr_cache
;
4038 struct parm_attr_d
*pa
;
4041 start_stmt_group ();
4043 FOR_EACH_VEC_ELT (*cache
, i
, pa
)
4046 add_stmt_with_node_force (pa
->first
, gnat_node
);
4048 add_stmt_with_node_force (pa
->last
, gnat_node
);
4050 add_stmt_with_node_force (pa
->length
, gnat_node
);
4053 add_stmt (gnu_result
);
4054 gnu_result
= end_stmt_group ();
4056 gnu_subprog_lang
->parm_attr_cache
= NULL
;
4059 /* If we are dealing with a return from an Ada procedure with parameters
4060 passed by copy-in/copy-out, we need to return a record containing the
4061 final values of these parameters. If the list contains only one entry,
4062 return just that entry though.
4064 For a full description of the copy-in/copy-out parameter mechanism, see
4065 the part of the gnat_to_gnu_entity routine dealing with the translation
4068 We need to make a block that contains the definition of that label and
4069 the copying of the return value. It first contains the function, then
4070 the label and copy statement. */
4073 const Node_Id gnat_end_label
4074 = End_Label (Handled_Statement_Sequence (gnat_node
));
4076 gnu_return_var_stack
->pop ();
4078 add_stmt (gnu_result
);
4079 add_stmt (build1 (LABEL_EXPR
, void_type_node
,
4080 gnu_return_label_stack
->last ()));
4082 /* If this is a function which returns by invisible reference, the
4083 return value has already been dealt with at the return statements,
4084 so we only need to indirectly copy out the parameters. */
4085 if (TREE_ADDRESSABLE (gnu_subprog_type
))
4088 = build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result_decl
);
4091 gcc_assert (TREE_VALUE (gnu_cico_list
) == void_type_node
);
4093 for (t
= TREE_CHAIN (gnu_cico_list
); t
; t
= TREE_CHAIN (t
))
4095 tree gnu_field_deref
4096 = build_component_ref (gnu_ret_deref
, TREE_PURPOSE (t
), true);
4097 gnu_result
= build2 (MODIFY_EXPR
, void_type_node
,
4098 gnu_field_deref
, TREE_VALUE (t
));
4099 add_stmt_with_node (gnu_result
, gnat_end_label
);
4103 /* Otherwise, if this is a procedure or a function which does not return
4104 by invisible reference, we can do a direct block-copy out. */
4109 if (list_length (gnu_cico_list
) == 1)
4110 gnu_retval
= TREE_VALUE (gnu_cico_list
);
4113 = build_constructor_from_list (TREE_TYPE (gnu_subprog_type
),
4116 gnu_result
= build_return_expr (gnu_result_decl
, gnu_retval
);
4117 add_stmt_with_node (gnu_result
, gnat_end_label
);
4121 gnu_result
= end_stmt_group ();
4124 gnu_return_label_stack
->pop ();
4126 /* On SEH targets, install an exception handler around the main entry
4127 point to catch unhandled exceptions. */
4128 if (DECL_NAME (gnu_subprog
) == main_identifier_node
4129 && targetm_common
.except_unwind_info (&global_options
) == UI_SEH
)
4134 t
= build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
4135 1, integer_zero_node
);
4136 t
= build_call_n_expr (unhandled_except_decl
, 1, t
);
4138 etype
= build_unary_op (ADDR_EXPR
, NULL_TREE
, unhandled_others_decl
);
4139 etype
= tree_cons (NULL_TREE
, etype
, NULL_TREE
);
4141 t
= build2 (CATCH_EXPR
, void_type_node
, etype
, t
);
4142 gnu_result
= build2 (TRY_CATCH_EXPR
, TREE_TYPE (gnu_result
),
4146 end_subprog_body (gnu_result
);
4148 /* Finally annotate the parameters and disconnect the trees for parameters
4149 that we have turned into variables since they are now unusable. */
4150 for (Entity_Id gnat_param
= First_Formal_With_Extras (gnat_subprog
);
4151 Present (gnat_param
);
4152 gnat_param
= Next_Formal_With_Extras (gnat_param
))
4154 tree gnu_param
= get_gnu_tree (gnat_param
);
4155 bool is_var_decl
= VAR_P (gnu_param
);
4157 annotate_object (gnat_param
, TREE_TYPE (gnu_param
), NULL_TREE
,
4158 DECL_BY_REF_P (gnu_param
));
4161 save_gnu_tree (gnat_param
, NULL_TREE
, false);
4164 /* Disconnect the variable created for the return value. */
4165 if (gnu_return_var_elmt
)
4166 TREE_VALUE (gnu_return_var_elmt
) = void_type_node
;
4168 /* If the function returns an aggregate type and we have candidates for
4169 a Named Return Value, finalize the optimization. */
4170 if (optimize
&& !optimize_debug
&& gnu_subprog_lang
->named_ret_val
)
4172 finalize_nrv (gnu_subprog
,
4173 gnu_subprog_lang
->named_ret_val
,
4174 gnu_subprog_lang
->other_ret_val
,
4175 gnu_subprog_lang
->gnat_ret
);
4176 gnu_subprog_lang
->named_ret_val
= NULL
;
4177 gnu_subprog_lang
->other_ret_val
= NULL
;
4180 /* If this is an inlined external function that has been marked uninlinable,
4181 drop the body and stop there. Otherwise compile the body. */
4182 if (DECL_EXTERNAL (gnu_subprog
) && DECL_UNINLINABLE (gnu_subprog
))
4183 DECL_SAVED_TREE (gnu_subprog
) = NULL_TREE
;
4185 rest_of_subprog_body_compilation (gnu_subprog
);
4188 /* The type of an atomic access. */
4190 typedef enum { NOT_ATOMIC
, SIMPLE_ATOMIC
, OUTER_ATOMIC
} atomic_acces_t
;
4192 /* Return true if GNAT_NODE references an Atomic entity. This is modeled on
4193 the Is_Atomic_Object predicate of the front-end, but additionally handles
4194 explicit dereferences. */
4197 node_is_atomic (Node_Id gnat_node
)
4199 Entity_Id gnat_entity
;
4201 switch (Nkind (gnat_node
))
4204 case N_Expanded_Name
:
4205 gnat_entity
= Entity (gnat_node
);
4206 if (!Is_Object (gnat_entity
))
4208 return Is_Atomic (gnat_entity
)
4209 || (Is_Atomic (Etype (gnat_entity
))
4210 && !simple_constant_p (gnat_entity
));
4212 case N_Selected_Component
:
4213 return Is_Atomic (Etype (gnat_node
))
4214 || Is_Atomic (Entity (Selector_Name (gnat_node
)));
4216 case N_Indexed_Component
:
4217 return Is_Atomic (Etype (gnat_node
))
4218 || Has_Atomic_Components (Etype (Prefix (gnat_node
)))
4219 || (Is_Entity_Name (Prefix (gnat_node
))
4220 && Has_Atomic_Components (Entity (Prefix (gnat_node
))));
4222 case N_Explicit_Dereference
:
4223 return Is_Atomic (Etype (gnat_node
));
4232 /* Return true if GNAT_NODE references a Volatile_Full_Access entity. This is
4233 modeled on the Is_Volatile_Full_Access_Object predicate of the front-end,
4234 but additionally handles explicit dereferences. */
4237 node_is_volatile_full_access (Node_Id gnat_node
)
4239 Entity_Id gnat_entity
;
4241 switch (Nkind (gnat_node
))
4244 case N_Expanded_Name
:
4245 gnat_entity
= Entity (gnat_node
);
4246 if (!Is_Object (gnat_entity
))
4248 return Is_Volatile_Full_Access (gnat_entity
)
4249 || (Is_Volatile_Full_Access (Etype (gnat_entity
))
4250 && !simple_constant_p (gnat_entity
));
4252 case N_Selected_Component
:
4253 return Is_Volatile_Full_Access (Etype (gnat_node
))
4254 || Is_Volatile_Full_Access (Entity (Selector_Name (gnat_node
)));
4256 case N_Indexed_Component
:
4257 case N_Explicit_Dereference
:
4258 return Is_Volatile_Full_Access (Etype (gnat_node
));
4267 /* Return true if GNAT_NODE references a component of a larger object. */
4270 node_is_component (Node_Id gnat_node
)
4272 const Node_Kind k
= Nkind (gnat_node
);
4273 return k
== N_Indexed_Component
|| k
== N_Selected_Component
|| k
== N_Slice
;
4276 /* Return true if GNAT_NODE is a type conversion. */
4279 node_is_type_conversion (Node_Id gnat_node
)
4281 const Node_Kind k
= Nkind (gnat_node
);
4282 return k
== N_Type_Conversion
|| k
== N_Unchecked_Type_Conversion
;
4285 /* Compute whether GNAT_NODE requires atomic access and set TYPE to the type
4286 of access and SYNC according to the associated synchronization setting.
4288 We implement 3 different semantics of atomicity in this function:
4290 1. the Ada 95/2005/2012 semantics of the Atomic aspect/pragma,
4291 2. the Ada 2022 semantics of the Atomic aspect/pragma,
4292 3. the semantics of the Volatile_Full_Access GNAT aspect/pragma.
4294 They are mutually exclusive and the FE should have rejected conflicts. */
4297 get_atomic_access (Node_Id gnat_node
, atomic_acces_t
*type
, bool *sync
)
4299 Node_Id gnat_parent
, gnat_temp
;
4300 Attribute_Id attr_id
;
4302 /* First, scan the parent to filter out irrelevant cases. */
4303 gnat_parent
= Parent (gnat_node
);
4304 switch (Nkind (gnat_parent
))
4306 case N_Attribute_Reference
:
4307 attr_id
= Get_Attribute_Id (Attribute_Name (gnat_parent
));
4308 /* Do not mess up machine code insertions. */
4309 if (attr_id
== Attr_Asm_Input
|| attr_id
== Attr_Asm_Output
)
4312 /* Nothing to do if we are the prefix of an attribute, since we do not
4313 want an atomic access for things like 'Size. */
4315 /* ... fall through ... */
4318 /* The N_Reference node is like an attribute. */
4319 if (Prefix (gnat_parent
) == gnat_node
)
4323 case N_Object_Renaming_Declaration
:
4324 /* Nothing to do for the identifier in an object renaming declaration,
4325 the renaming itself does not need atomic access. */
4332 /* Now strip any type conversion from GNAT_NODE. */
4333 if (node_is_type_conversion (gnat_node
))
4334 gnat_node
= Expression (gnat_node
);
4336 /* Up to Ada 2012, for Atomic itself, only reads and updates of the object as
4337 a whole require atomic access (RM C.6(15)). But, starting with Ada 2022,
4338 reads of or writes to a nonatomic subcomponent of the object also require
4339 atomic access (RM C.6(19)). */
4340 if (node_is_atomic (gnat_node
))
4342 bool as_a_whole
= true;
4344 /* If we are the prefix of the parent, then the access is partial. */
4345 for (gnat_temp
= gnat_node
, gnat_parent
= Parent (gnat_temp
);
4346 node_is_component (gnat_parent
) && Prefix (gnat_parent
) == gnat_temp
;
4347 gnat_temp
= gnat_parent
, gnat_parent
= Parent (gnat_temp
))
4348 if (Ada_Version
< Ada_2022
|| node_is_atomic (gnat_parent
))
4353 /* We consider that partial accesses are not sequential actions and,
4354 therefore, do not require synchronization. */
4355 *type
= SIMPLE_ATOMIC
;
4356 *sync
= as_a_whole
? Atomic_Sync_Required (gnat_node
) : false;
4360 /* Look for an outer atomic access of a nonatomic subcomponent. Note that,
4361 for VFA, we do this before looking at the node itself because we need to
4362 access the outermost VFA object atomically, unlike for Atomic where it is
4363 the innermost atomic object (RM C.6(19)). */
4364 for (gnat_temp
= gnat_node
;
4365 node_is_component (gnat_temp
);
4366 gnat_temp
= Prefix (gnat_temp
))
4367 if ((Ada_Version
>= Ada_2022
&& node_is_atomic (Prefix (gnat_temp
)))
4368 || node_is_volatile_full_access (Prefix (gnat_temp
)))
4370 *type
= OUTER_ATOMIC
;
4375 /* Unlike Atomic, accessing a VFA object always requires atomic access. */
4376 if (node_is_volatile_full_access (gnat_node
))
4378 *type
= SIMPLE_ATOMIC
;
4388 /* Return true if GNAT_NODE requires simple atomic access and, if so, set SYNC
4389 according to the associated synchronization setting. */
4392 simple_atomic_access_required_p (Node_Id gnat_node
, bool *sync
)
4394 atomic_acces_t type
;
4395 get_atomic_access (gnat_node
, &type
, sync
);
4396 return type
== SIMPLE_ATOMIC
;
4399 /* Return the storage model specified by GNAT_NODE, or else Empty. */
4402 get_storage_model (Node_Id gnat_node
)
4404 if (Nkind (gnat_node
) == N_Explicit_Dereference
4405 && Has_Designated_Storage_Model_Aspect (Etype (Prefix (gnat_node
))))
4406 return Storage_Model_Object (Etype (Prefix (gnat_node
)));
4411 /* Compute whether GNAT_NODE requires storage model access and set GNAT_SMO to
4412 the storage model object to be used for it if it does, or else Empty. */
4415 get_storage_model_access (Node_Id gnat_node
, Entity_Id
*gnat_smo
)
4417 const Node_Id gnat_parent
= Parent (gnat_node
);
4420 switch (Nkind (gnat_parent
))
4422 case N_Attribute_Reference
:
4423 /* If the parent is an attribute reference that requires an lvalue and
4424 gnat_node is the Prefix (i.e. not a parameter), we do not need to
4425 actually access any storage. */
4426 if (lvalue_required_for_attribute_p (gnat_parent
)
4427 && Prefix (gnat_parent
) == gnat_node
)
4431 case N_Object_Renaming_Declaration
:
4432 /* Nothing to do for the identifier in an object renaming declaration,
4433 the renaming itself does not need storage model access. */
4440 /* If we are the prefix of the parent, then the access is above us. */
4441 if ((node_is_component (gnat_parent
) && Prefix (gnat_parent
) == gnat_node
)
4442 || (node_is_type_conversion (gnat_parent
)
4443 && node_is_component (Parent (gnat_parent
))
4444 && Prefix (Parent (gnat_parent
)) == gnat_parent
))
4447 /* Find the innermost prefix in GNAT_NODE, stripping any type conversion. */
4448 if (node_is_type_conversion (gnat_node
))
4449 gnat_node
= Expression (gnat_node
);
4450 while (node_is_component (gnat_node
))
4452 gnat_node
= Prefix (gnat_node
);
4453 if (node_is_type_conversion (gnat_node
))
4454 gnat_node
= Expression (gnat_node
);
4457 *gnat_smo
= get_storage_model (gnat_node
);
4460 /* Return true if GNAT_NODE requires storage model access and, if so, set
4461 GNAT_SMO to the storage model object to be used for it. */
4464 storage_model_access_required_p (Node_Id gnat_node
, Entity_Id
*gnat_smo
)
4466 get_storage_model_access (gnat_node
, gnat_smo
);
4467 return Present (*gnat_smo
);
4470 /* Create a temporary variable with PREFIX and TYPE, and return it. */
4473 create_temporary (const char *prefix
, tree type
)
4476 = create_var_decl (create_tmp_var_name (prefix
), NULL_TREE
,
4478 false, false, false, false, false,
4479 true, false, NULL
, Empty
);
4483 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
4484 Put the initialization statement into GNU_INIT_STMT and annotate it with
4485 the SLOC of GNAT_NODE. Return the temporary variable. */
4488 create_init_temporary (const char *prefix
, tree gnu_init
, tree
*gnu_init_stmt
,
4491 tree gnu_temp
= create_temporary (prefix
, TREE_TYPE (gnu_init
));
4493 *gnu_init_stmt
= build_binary_op (INIT_EXPR
, NULL_TREE
, gnu_temp
, gnu_init
);
4494 set_expr_location_from_node (*gnu_init_stmt
, gnat_node
);
4499 /* Return true if TYPE is an array of scalar type. */
4502 is_array_of_scalar_type (tree type
)
4504 if (TREE_CODE (type
) != ARRAY_TYPE
)
4507 type
= TREE_TYPE (type
);
4509 return !AGGREGATE_TYPE_P (type
) && !POINTER_TYPE_P (type
);
4512 /* Helper function for walk_tree, used by return_slot_opt_for_pure_call_p. */
4515 find_decls_r (tree
*tp
, int *walk_subtrees
, void *data
)
4517 bitmap decls
= (bitmap
) data
;
4522 else if (DECL_P (*tp
))
4523 bitmap_set_bit (decls
, DECL_UID (*tp
));
4528 /* Return whether the assignment TARGET = CALL can be subject to the return
4529 slot optimization, under the assumption that the called function be pure
4530 in the Ada sense and return an array of scalar type. */
4533 return_slot_opt_for_pure_call_p (tree target
, tree call
)
4535 /* Check that the target is a DECL. */
4536 if (!DECL_P (target
))
4539 const bitmap decls
= BITMAP_GGC_ALLOC ();
4540 call_expr_arg_iterator iter
;
4543 /* Check that all the arguments have either a scalar type (we assume that
4544 this means by-copy passing mechanism) or array of scalar type. */
4545 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, call
)
4547 tree arg_type
= TREE_TYPE (arg
);
4548 if (TREE_CODE (arg_type
) == REFERENCE_TYPE
)
4549 arg_type
= TREE_TYPE (arg_type
);
4551 if (is_array_of_scalar_type (arg_type
))
4552 walk_tree_without_duplicates (&arg
, find_decls_r
, decls
);
4554 else if (AGGREGATE_TYPE_P (arg_type
) || POINTER_TYPE_P (arg_type
))
4558 /* Check that the target is not referenced by the non-scalar arguments. */
4559 return !bitmap_bit_p (decls
, DECL_UID (target
));
4562 /* Elaborate types referenced in the profile (FIRST_FORMAL, RESULT_TYPE). */
4565 elaborate_profile (Entity_Id first_formal
, Entity_Id result_type
)
4569 for (formal
= first_formal
;
4571 formal
= Next_Formal_With_Extras (formal
))
4572 (void) gnat_to_gnu_type (Etype (formal
));
4574 if (Present (result_type
) && Ekind (result_type
) != E_Void
)
4575 (void) gnat_to_gnu_type (result_type
);
4578 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Function_Call
4579 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
4580 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
4581 If GNU_TARGET is non-null, this must be a function call on the RHS of a
4582 N_Assignment_Statement and the result is to be placed into that object.
4583 ATOMIC_ACCESS is the type of atomic access to be used for the assignment
4584 to GNU_TARGET. If, in addition, ATOMIC_SYNC is true, then the assignment
4585 to GNU_TARGET requires atomic synchronization. GNAT_SMO is the storage
4586 model object to be used for the assignment to GNU_TARGET or Empty if there
4590 Call_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
, tree gnu_target
,
4591 atomic_acces_t atomic_access
, bool atomic_sync
, Entity_Id gnat_smo
)
4593 const bool function_call
= (Nkind (gnat_node
) == N_Function_Call
);
4594 const bool returning_value
= (function_call
&& !gnu_target
);
4595 /* The GCC node corresponding to the GNAT subprogram name. This can either
4596 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
4597 or an indirect reference expression (an INDIRECT_REF node) pointing to a
4599 const Node_Id gnat_subprog
= Name (gnat_node
);
4600 tree gnu_subprog
= gnat_to_gnu (gnat_subprog
);
4601 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
4602 tree gnu_subprog_type
= TREE_TYPE (gnu_subprog
);
4603 /* The return type of the FUNCTION_TYPE. */
4604 tree gnu_result_type
;
4605 const bool frontend_builtin
4606 = (TREE_CODE (gnu_subprog
) == FUNCTION_DECL
4607 && DECL_BUILT_IN_CLASS (gnu_subprog
) == BUILT_IN_FRONTEND
);
4608 auto_vec
<tree
, 16> gnu_actual_vec
;
4609 tree gnu_name_list
= NULL_TREE
;
4610 tree gnu_stmt_list
= NULL_TREE
;
4611 tree gnu_after_list
= NULL_TREE
;
4612 tree gnu_retval
= NULL_TREE
;
4613 tree gnu_call
, gnu_result
;
4614 bool went_into_elab_proc
;
4615 bool pushed_binding_level
;
4618 Entity_Id gnat_formal
;
4619 Entity_Id gnat_result_type
;
4620 Node_Id gnat_actual
;
4621 atomic_acces_t aa_type
;
4624 /* The only way we can make a call via an access type is if GNAT_NAME is an
4625 explicit dereference. In that case, get the list of formal args from the
4626 type the access type is pointing to. Otherwise, get the formals from the
4627 entity being called. */
4628 if (Nkind (gnat_subprog
) == N_Explicit_Dereference
)
4630 const Entity_Id gnat_prefix_type
4631 = Underlying_Type (Etype (Prefix (gnat_subprog
)));
4633 gnat_formal
= First_Formal_With_Extras (Etype (gnat_subprog
));
4634 gnat_result_type
= Etype (Etype (gnat_subprog
));
4635 variadic
= IN (Convention (gnat_prefix_type
), Convention_C_Variadic
);
4637 /* If the access type doesn't require foreign-compatible representation,
4638 be prepared for descriptors. */
4640 = targetm
.calls
.custom_function_descriptors
> 0
4641 && Can_Use_Internal_Rep (gnat_prefix_type
);
4644 else if (Nkind (gnat_subprog
) == N_Attribute_Reference
)
4646 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
4647 gnat_formal
= Empty
;
4648 gnat_result_type
= Empty
;
4650 by_descriptor
= false;
4655 gcc_checking_assert (Is_Entity_Name (gnat_subprog
));
4657 gnat_formal
= First_Formal_With_Extras (Entity (gnat_subprog
));
4658 gnat_result_type
= Etype (Entity_Id (gnat_subprog
));
4659 variadic
= IN (Convention (Entity (gnat_subprog
)), Convention_C_Variadic
);
4660 by_descriptor
= false;
4662 /* If we are calling a stubbed function, then raise Program_Error, but
4663 elaborate all our args first. */
4664 if (Convention (Entity (gnat_subprog
)) == Convention_Stubbed
)
4666 tree call_expr
= build_call_raise (PE_Stubbed_Subprogram_Called
,
4667 gnat_node
, N_Raise_Program_Error
);
4669 for (gnat_actual
= First_Actual (gnat_node
);
4670 Present (gnat_actual
);
4671 gnat_actual
= Next_Actual (gnat_actual
))
4672 add_stmt (gnat_to_gnu (gnat_actual
));
4674 if (returning_value
)
4676 gnu_result_type
= TREE_TYPE (gnu_subprog_type
);
4677 *gnu_result_type_p
= gnu_result_type
;
4678 return build1 (NULL_EXPR
, gnu_result_type
, call_expr
);
4685 /* We must elaborate the entire profile now because, if it references types
4686 that were initially incomplete, their elaboration changes the contents
4687 of GNU_SUBPROG_TYPE and, in particular, may change the result type. */
4688 elaborate_profile (gnat_formal
, gnat_result_type
);
4690 gcc_assert (FUNC_OR_METHOD_TYPE_P (gnu_subprog_type
));
4691 gnu_result_type
= TREE_TYPE (gnu_subprog_type
);
4693 if (TREE_CODE (gnu_subprog
) == FUNCTION_DECL
)
4695 /* For a call to a nested function, check the inlining status. */
4696 if (decl_function_context (gnu_subprog
))
4697 check_inlining_for_nested_subprog (gnu_subprog
);
4699 /* For a recursive call, avoid explosion due to recursive inlining. */
4700 if (gnu_subprog
== current_function_decl
)
4701 DECL_DISREGARD_INLINE_LIMITS (gnu_subprog
) = 0;
4704 /* The lifetime of the temporaries created for the call ends right after the
4705 return value is copied, so we can give them the scope of the elaboration
4706 routine at top level. */
4707 if (!current_function_decl
)
4709 current_function_decl
= get_elaboration_procedure ();
4710 went_into_elab_proc
= true;
4713 went_into_elab_proc
= false;
4715 /* First, create the temporary for the return value when:
4717 1. There is no target and the function has copy-in/copy-out parameters,
4718 because we need to preserve the return value before copying back the
4721 2. There is no target and the call is made for neither the declaration
4722 of an object (regular or renaming), nor a return statement, nor an
4723 allocator, nor an aggregate, and the return type has variable size
4724 because in this case the gimplifier cannot create the temporary, or
4725 more generally is an aggregate type, because the gimplifier would
4726 create the temporary in the outermost scope instead of locally here.
4727 But there is an exception for an allocator of unconstrained record
4728 type with default discriminant because we allocate the actual size
4729 in this case, unlike in the other cases, so we need a temporary to
4730 fetch the discriminant and we create it here.
4732 3. There is a target and it is a slice or an array with fixed size,
4733 and the return type has variable size, because the gimplifier
4734 doesn't handle these cases.
4736 4. There is a target which is a bit-field and the function returns an
4737 unconstrained record type with default discriminant, because the
4738 return may copy more data than the bit-field can contain.
4740 5. There is a target which needs to be accessed with a storage model.
4742 6. There is no target and we have misaligned In Out or Out parameters
4743 passed by reference, because we need to preserve the return value
4744 before copying back the parameters. However, in this case, we'll
4745 defer creating the temporary, see below.
4747 This must be done before we push a binding level around the call, since
4748 we will pop it before copying the return value. */
4750 && ((!gnu_target
&& TYPE_CI_CO_LIST (gnu_subprog_type
))
4752 && Nkind (Parent (gnat_node
)) != N_Object_Declaration
4753 && Nkind (Parent (gnat_node
)) != N_Object_Renaming_Declaration
4754 && Nkind (Parent (gnat_node
)) != N_Simple_Return_Statement
4755 && (!(Nkind (Parent (gnat_node
)) == N_Qualified_Expression
4756 && Nkind (Parent (Parent (gnat_node
))) == N_Allocator
)
4757 || type_is_padding_self_referential (gnu_result_type
))
4758 && Nkind (Parent (gnat_node
)) != N_Aggregate
4759 && AGGREGATE_TYPE_P (gnu_result_type
)
4760 && !TYPE_IS_FAT_POINTER_P (gnu_result_type
))
4762 && (TREE_CODE (gnu_target
) == ARRAY_RANGE_REF
4763 || (TREE_CODE (TREE_TYPE (gnu_target
)) == ARRAY_TYPE
4764 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target
)))
4766 && TREE_CODE (TYPE_SIZE (gnu_result_type
)) != INTEGER_CST
)
4768 && TREE_CODE (gnu_target
) == COMPONENT_REF
4769 && DECL_BIT_FIELD (TREE_OPERAND (gnu_target
, 1))
4770 && DECL_SIZE (TREE_OPERAND (gnu_target
, 1))
4771 != TYPE_SIZE (TREE_TYPE (gnu_target
))
4772 && type_is_padding_self_referential (gnu_result_type
))
4774 && Present (gnat_smo
)
4775 && Present (Storage_Model_Copy_To (gnat_smo
)))))
4777 gnu_retval
= create_temporary ("R", gnu_result_type
);
4778 DECL_RETURN_VALUE_P (gnu_retval
) = 1;
4781 /* If we don't need a value or have already created it, push a binding level
4782 around the call. This will narrow the lifetime of the temporaries we may
4783 need to make when translating the parameters as much as possible. */
4784 if (!returning_value
|| gnu_retval
)
4786 start_stmt_group ();
4788 pushed_binding_level
= true;
4791 pushed_binding_level
= false;
4793 /* Create the list of the actual parameters as GCC expects it, namely a
4794 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
4795 is an expression and the TREE_PURPOSE field is null. But skip Out
4796 parameters not passed by reference and that need not be copied in. */
4797 for (gnat_actual
= First_Actual (gnat_node
);
4798 Present (gnat_actual
);
4799 gnat_formal
= Next_Formal_With_Extras (gnat_formal
),
4800 gnat_actual
= Next_Actual (gnat_actual
))
4802 Entity_Id gnat_formal_type
= Etype (gnat_formal
);
4803 tree gnu_formal_type
= gnat_to_gnu_type (gnat_formal_type
);
4804 tree gnu_formal
= present_gnu_tree (gnat_formal
)
4805 ? get_gnu_tree (gnat_formal
) : NULL_TREE
;
4806 const bool in_param
= (Ekind (gnat_formal
) == E_In_Parameter
);
4807 const bool is_true_formal_parm
4808 = gnu_formal
&& TREE_CODE (gnu_formal
) == PARM_DECL
;
4809 const bool is_by_ref_formal_parm
4810 = is_true_formal_parm
4811 && (DECL_BY_REF_P (gnu_formal
)
4812 || DECL_BY_COMPONENT_PTR_P (gnu_formal
));
4813 /* In the In Out or Out case, we must suppress conversions that yield
4814 an lvalue but can nevertheless cause the creation of a temporary,
4815 because we need the real object in this case, either to pass its
4816 address if it's passed by reference or as target of the back copy
4817 done after the call if it uses the copy-in/copy-out mechanism.
4818 We do it in the In case too, except for an unchecked conversion
4819 to an elementary type or a constrained composite type because it
4820 alone can cause the actual to be misaligned and the addressability
4821 test is applied to the real object. */
4822 const bool suppress_type_conversion
4823 = ((Nkind (gnat_actual
) == N_Unchecked_Type_Conversion
4825 || !is_by_ref_formal_parm
4826 || (Is_Composite_Type (Underlying_Type (gnat_formal_type
))
4827 && !Is_Constrained (Underlying_Type (gnat_formal_type
)))))
4828 || (Nkind (gnat_actual
) == N_Type_Conversion
4829 && Is_Composite_Type (Underlying_Type (gnat_formal_type
))));
4830 Node_Id gnat_name
= suppress_type_conversion
4831 ? Expression (gnat_actual
) : gnat_actual
;
4832 tree gnu_name
= gnat_to_gnu (gnat_name
), gnu_name_type
;
4834 /* If it's possible we may need to use this expression twice, make sure
4835 that any side-effects are handled via SAVE_EXPRs; likewise if we need
4836 to force side-effects before the call. */
4837 if (!in_param
&& !is_by_ref_formal_parm
)
4839 tree init
= NULL_TREE
;
4840 gnu_name
= gnat_stabilize_reference (gnu_name
, true, &init
);
4843 = build_compound_expr (TREE_TYPE (gnu_name
), init
, gnu_name
);
4846 /* If we are passing a non-addressable parameter by reference, pass the
4847 address of a copy. In the In Out or Out case, set up to copy back
4848 out after the call. */
4849 if (is_by_ref_formal_parm
4850 && (gnu_name_type
= gnat_to_gnu_type (Etype (gnat_name
)))
4851 && !addressable_p (gnu_name
, gnu_name_type
))
4853 tree gnu_orig
= gnu_name
, gnu_temp
, gnu_stmt
;
4855 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
4856 but sort of an instantiation for them. */
4857 if (TREE_CODE (remove_conversions (gnu_name
, true)) == CONSTRUCTOR
)
4860 /* If the formal is passed by reference, a copy is not allowed. */
4861 else if (TYPE_IS_BY_REFERENCE_P (gnu_formal_type
)
4862 || Is_Aliased (gnat_formal
))
4863 post_error ("misaligned actual cannot be passed by reference",
4866 /* If the mechanism was forced to by-ref, a copy is not allowed but
4867 we issue only a warning because this case is not strict Ada. */
4868 else if (DECL_FORCED_BY_REF_P (gnu_formal
))
4869 post_error ("misaligned actual cannot be passed by reference??",
4872 /* If the actual type of the object is already the nominal type,
4873 we have nothing to do, except if the size is self-referential
4874 in which case we'll remove the unpadding below. */
4875 if (TREE_TYPE (gnu_name
) == gnu_name_type
4876 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type
)))
4879 /* Otherwise remove the unpadding from all the objects. */
4880 else if (TREE_CODE (gnu_name
) == COMPONENT_REF
4881 && TYPE_IS_PADDING_P
4882 (TREE_TYPE (TREE_OPERAND (gnu_name
, 0))))
4883 gnu_orig
= gnu_name
= TREE_OPERAND (gnu_name
, 0);
4885 /* Otherwise convert to the nominal type of the object if needed.
4886 There are several cases in which we need to make the temporary
4887 using this type instead of the actual type of the object when
4888 they are distinct, because the expectations of the callee would
4889 otherwise not be met:
4890 - if it's a justified modular type,
4891 - if the actual type is a smaller form of it,
4892 - if it's a smaller form of the actual type. */
4893 else if ((TREE_CODE (gnu_name_type
) == RECORD_TYPE
4894 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type
)
4895 || smaller_form_type_p (TREE_TYPE (gnu_name
),
4897 || (INTEGRAL_TYPE_P (gnu_name_type
)
4898 && smaller_form_type_p (gnu_name_type
,
4899 TREE_TYPE (gnu_name
))))
4900 gnu_name
= convert (gnu_name_type
, gnu_name
);
4902 /* If this is an In Out or Out parameter and we're returning a value,
4903 we need to create a temporary for the return value because we must
4904 preserve it before copying back at the very end. */
4905 if (!in_param
&& returning_value
&& !gnu_retval
)
4907 gnu_retval
= create_temporary ("R", gnu_result_type
);
4908 DECL_RETURN_VALUE_P (gnu_retval
) = 1;
4911 /* If we haven't pushed a binding level, push it now. This will
4912 narrow the lifetime of the temporary we are about to make as
4913 much as possible. */
4914 if (!pushed_binding_level
&& (!returning_value
|| gnu_retval
))
4916 start_stmt_group ();
4918 pushed_binding_level
= true;
4921 /* Create an explicit temporary holding the copy. */
4923 /* Do not initialize it for the _Init parameter of an initialization
4924 procedure since no data is meant to be passed in. */
4925 if (Ekind (gnat_formal
) == E_Out_Parameter
4926 && Is_Entity_Name (gnat_subprog
)
4927 && Is_Init_Proc (Entity (gnat_subprog
)))
4928 gnu_name
= gnu_temp
= create_temporary ("A", TREE_TYPE (gnu_name
));
4930 /* Initialize it on the fly like for an implicit temporary in the
4931 other cases, as we don't necessarily have a statement list. */
4934 gnu_temp
= create_init_temporary ("A", gnu_name
, &gnu_stmt
,
4936 gnu_name
= build_compound_expr (TREE_TYPE (gnu_name
), gnu_stmt
,
4940 /* Set up to move the copy back to the original if needed. */
4943 /* If the original is a COND_EXPR whose first arm isn't meant to
4944 be further used, just deal with the second arm. This is very
4945 likely the conditional expression built for a check. */
4946 if (TREE_CODE (gnu_orig
) == COND_EXPR
4947 && TREE_CODE (TREE_OPERAND (gnu_orig
, 1)) == COMPOUND_EXPR
4949 (TREE_OPERAND (TREE_OPERAND (gnu_orig
, 1), 1)))
4950 gnu_orig
= TREE_OPERAND (gnu_orig
, 2);
4953 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_orig
, gnu_temp
);
4954 set_expr_location_from_node (gnu_stmt
, gnat_node
);
4956 append_to_statement_list (gnu_stmt
, &gnu_after_list
);
4960 /* Start from the real object and build the actual. */
4961 tree gnu_actual
= gnu_name
;
4963 /* If atomic access is required for an In or In Out actual parameter,
4964 build the atomic load. */
4965 if (is_true_formal_parm
4966 && !is_by_ref_formal_parm
4967 && Ekind (gnat_formal
) != E_Out_Parameter
4968 && simple_atomic_access_required_p (gnat_actual
, &aa_sync
))
4969 gnu_actual
= build_atomic_load (gnu_actual
, aa_sync
);
4971 /* If this was a procedure call, we may not have removed any padding.
4972 So do it here for the part we will use as an input, if any. */
4973 if (Ekind (gnat_formal
) != E_Out_Parameter
4974 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual
)))
4976 = convert (get_unpadded_type (Etype (gnat_actual
)), gnu_actual
);
4978 /* Put back the conversion we suppressed above in the computation of the
4979 real object. And even if we didn't suppress any conversion there, we
4980 may have suppressed a conversion to the Etype of the actual earlier,
4981 since the parent is a procedure call, so put it back here. Note that
4982 we might have a dummy type here if the actual is the dereference of a
4983 pointer to it, but that's OK when the formal is passed by reference.
4984 We also do not put back a conversion between an actual and a formal
4985 that are unconstrained array types to avoid creating local bounds. */
4986 tree gnu_actual_type
= get_unpadded_type (Etype (gnat_actual
));
4987 if (TYPE_IS_DUMMY_P (gnu_actual_type
))
4988 gcc_assert (is_true_formal_parm
&& DECL_BY_REF_P (gnu_formal
));
4989 else if (suppress_type_conversion
4990 && Nkind (gnat_actual
) == N_Unchecked_Type_Conversion
)
4991 gnu_actual
= unchecked_convert (gnu_actual_type
, gnu_actual
,
4992 No_Truncation (gnat_actual
));
4993 else if ((TREE_CODE (TREE_TYPE (gnu_actual
)) == UNCONSTRAINED_ARRAY_TYPE
4994 || (TREE_CODE (TREE_TYPE (gnu_actual
)) == RECORD_TYPE
4995 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual
))))
4996 && TREE_CODE (gnu_formal_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4999 gnu_actual
= convert (gnu_actual_type
, gnu_actual
);
5001 gigi_checking_assert (!Do_Range_Check (gnat_actual
));
5003 /* First see if the parameter is passed by reference. */
5004 if (is_true_formal_parm
&& DECL_BY_REF_P (gnu_formal
))
5008 /* In Out or Out parameters passed by reference don't use the
5009 copy-in/copy-out mechanism so the address of the real object
5010 must be passed to the function. */
5011 gnu_actual
= gnu_name
;
5013 /* If we have a padded type, be sure we've removed padding. */
5014 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual
)))
5015 gnu_actual
= convert (get_unpadded_type (Etype (gnat_actual
)),
5018 /* If we have the constructed subtype of an aliased object
5019 with an unconstrained nominal subtype, the type of the
5020 actual includes the template, although it is formally
5021 constrained. So we need to convert it back to the real
5022 constructed subtype to retrieve the constrained part
5023 and takes its address. */
5024 if (TREE_CODE (TREE_TYPE (gnu_actual
)) == RECORD_TYPE
5025 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual
))
5026 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual
))
5027 && Is_Array_Type (Underlying_Type (Etype (gnat_actual
))))
5028 gnu_actual
= convert (gnu_actual_type
, gnu_actual
);
5031 /* There is no need to convert the actual to the formal's type before
5032 taking its address. The only exception is for unconstrained array
5033 types because of the way we build fat pointers. */
5034 if (TREE_CODE (gnu_formal_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5036 /* Put back the conversion we suppressed above for In Out or Out
5037 parameters, since it may set the bounds of the actual. */
5038 if (!in_param
&& suppress_type_conversion
)
5039 gnu_actual
= convert (gnu_actual_type
, gnu_actual
);
5040 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
5043 /* Take the address of the object and convert to the proper pointer
5045 gnu_formal_type
= TREE_TYPE (gnu_formal
);
5046 gnu_actual
= build_unary_op (ADDR_EXPR
, gnu_formal_type
, gnu_actual
);
5049 /* Then see if the parameter is an array passed to a foreign convention
5051 else if (is_true_formal_parm
&& DECL_BY_COMPONENT_PTR_P (gnu_formal
))
5053 gnu_actual
= maybe_padded_object (gnu_actual
);
5054 gnu_actual
= maybe_unconstrained_array (gnu_actual
);
5056 /* Take the address of the object and convert to the proper pointer
5057 type. We'd like to actually compute the address of the beginning
5058 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
5059 possibility that the ARRAY_REF might return a constant and we'd be
5060 getting the wrong address. Neither approach is exactly correct,
5061 but this is the most likely to work in all cases. */
5062 gnu_formal_type
= TREE_TYPE (gnu_formal
);
5063 gnu_actual
= build_unary_op (ADDR_EXPR
, gnu_formal_type
, gnu_actual
);
5066 /* Then see if the parameter is passed by copy. */
5067 else if (is_true_formal_parm
)
5070 gnu_name_list
= tree_cons (NULL_TREE
, gnu_name
, gnu_name_list
);
5072 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
5074 /* If this is a front-end built-in function, there is no need to
5075 convert to the type used to pass the argument. */
5076 if (!frontend_builtin
)
5077 gnu_actual
= convert (DECL_ARG_TYPE (gnu_formal
), gnu_actual
);
5080 /* Then see if this is an unnamed parameter in a variadic C function. */
5083 /* This is based on the processing done in gnat_to_gnu_param, but
5084 we expect the mechanism to be set in (almost) all cases. */
5085 const Mechanism_Type mech
= Mechanism (gnat_formal
);
5087 /* Strip off possible padding type. */
5088 if (TYPE_IS_PADDING_P (gnu_formal_type
))
5089 gnu_formal_type
= TREE_TYPE (TYPE_FIELDS (gnu_formal_type
));
5091 /* Arrays are passed as pointers to element type. First check for
5092 unconstrained array and get the underlying array. */
5093 if (TREE_CODE (gnu_formal_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5096 (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_formal_type
))));
5098 /* Arrays are passed as pointers to element type. */
5099 if (mech
!= By_Copy
&& TREE_CODE (gnu_formal_type
) == ARRAY_TYPE
)
5101 gnu_actual
= maybe_padded_object (gnu_actual
);
5102 gnu_actual
= maybe_unconstrained_array (gnu_actual
);
5104 /* Strip off any multi-dimensional entries, then strip
5105 off the last array to get the component type. */
5106 while (TREE_CODE (TREE_TYPE (gnu_formal_type
)) == ARRAY_TYPE
5107 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_formal_type
)))
5108 gnu_formal_type
= TREE_TYPE (gnu_formal_type
);
5110 gnu_formal_type
= TREE_TYPE (gnu_formal_type
);
5111 gnu_formal_type
= build_pointer_type (gnu_formal_type
);
5113 = build_unary_op (ADDR_EXPR
, gnu_formal_type
, gnu_actual
);
5116 /* Fat pointers are passed as thin pointers. */
5117 else if (TYPE_IS_FAT_POINTER_P (gnu_formal_type
))
5119 = make_type_from_size (gnu_formal_type
,
5120 size_int (POINTER_SIZE
), 0);
5122 /* If we were requested or muss pass by reference, do so.
5123 If we were requested to pass by copy, do so.
5124 Otherwise, pass In Out or Out parameters or aggregates by
5126 else if (mech
== By_Reference
5127 || must_pass_by_ref (gnu_formal_type
)
5129 && (!in_param
|| AGGREGATE_TYPE_P (gnu_formal_type
))))
5131 gnu_formal_type
= build_reference_type (gnu_formal_type
);
5133 = build_unary_op (ADDR_EXPR
, gnu_formal_type
, gnu_actual
);
5136 /* Otherwise pass by copy after applying default C promotions. */
5139 if (INTEGRAL_TYPE_P (gnu_formal_type
)
5140 && TYPE_PRECISION (gnu_formal_type
)
5141 < TYPE_PRECISION (integer_type_node
))
5142 gnu_formal_type
= integer_type_node
;
5144 else if (SCALAR_FLOAT_TYPE_P (gnu_formal_type
)
5145 && TYPE_PRECISION (gnu_formal_type
)
5146 < TYPE_PRECISION (double_type_node
))
5147 gnu_formal_type
= double_type_node
;
5150 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
5153 /* If we didn't create a PARM_DECL for the formal, this means that
5154 it is an Out parameter not passed by reference and that need not
5155 be copied in. In this case, the value of the actual need not be
5156 read. However, we still need to make sure that its side-effects
5157 are evaluated before the call, so we evaluate its address. */
5161 gnu_name_list
= tree_cons (NULL_TREE
, gnu_name
, gnu_name_list
);
5163 if (TREE_SIDE_EFFECTS (gnu_name
))
5165 tree addr
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_name
);
5166 append_to_statement_list (addr
, &gnu_stmt_list
);
5172 gnu_actual_vec
.safe_push (gnu_actual
);
5175 if (frontend_builtin
)
5177 tree pred_cst
= build_int_cst (integer_type_node
, PRED_BUILTIN_EXPECT
);
5178 enum internal_fn icode
= IFN_BUILTIN_EXPECT
;
5180 switch (DECL_FE_FUNCTION_CODE (gnu_subprog
))
5182 case BUILT_IN_EXPECT
:
5184 case BUILT_IN_LIKELY
:
5185 gnu_actual_vec
.safe_push (boolean_true_node
);
5187 case BUILT_IN_UNLIKELY
:
5188 gnu_actual_vec
.safe_push (boolean_false_node
);
5194 gnu_actual_vec
.safe_push (pred_cst
);
5197 = build_call_expr_internal_loc_array (UNKNOWN_LOCATION
,
5200 gnu_actual_vec
.length (),
5201 gnu_actual_vec
.begin ());
5206 = build_call_array_loc (UNKNOWN_LOCATION
,
5208 build_unary_op (ADDR_EXPR
, NULL_TREE
,
5210 gnu_actual_vec
.length (),
5211 gnu_actual_vec
.begin ());
5212 CALL_EXPR_BY_DESCRIPTOR (gnu_call
) = by_descriptor
;
5215 set_expr_location_from_node (gnu_call
, gnat_node
);
5217 /* If we have created a temporary for the return value, initialize it. */
5221 = build_binary_op (INIT_EXPR
, NULL_TREE
, gnu_retval
, gnu_call
);
5222 set_expr_location_from_node (gnu_stmt
, gnat_node
);
5223 append_to_statement_list (gnu_stmt
, &gnu_stmt_list
);
5224 gnu_call
= gnu_retval
;
5227 /* If this is a subprogram with copy-in/copy-out parameters, we need to
5228 unpack the valued returned from the function into the In Out or Out
5229 parameters. We deal with the function return (if this is an Ada
5231 if (TYPE_CI_CO_LIST (gnu_subprog_type
))
5233 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
5234 copy-out parameters. */
5235 tree gnu_cico_list
= TYPE_CI_CO_LIST (gnu_subprog_type
);
5236 const int length
= list_length (gnu_cico_list
);
5238 /* The call sequence must contain one and only one call, even though the
5239 function is pure. Save the result into a temporary if needed. */
5246 = create_init_temporary ("P", gnu_call
, &gnu_stmt
, gnat_node
);
5247 append_to_statement_list (gnu_stmt
, &gnu_stmt_list
);
5250 gnu_name_list
= nreverse (gnu_name_list
);
5253 /* The first entry is for the actual return value if this is a
5254 function, so skip it. */
5256 gnu_cico_list
= TREE_CHAIN (gnu_cico_list
);
5258 if (Nkind (gnat_subprog
) == N_Explicit_Dereference
)
5259 gnat_formal
= First_Formal_With_Extras (Etype (gnat_subprog
));
5261 gnat_formal
= First_Formal_With_Extras (Entity (gnat_subprog
));
5263 for (gnat_actual
= First_Actual (gnat_node
);
5264 Present (gnat_actual
);
5265 gnat_formal
= Next_Formal_With_Extras (gnat_formal
),
5266 gnat_actual
= Next_Actual (gnat_actual
))
5267 /* If we are dealing with a copy-in/copy-out parameter, we must
5268 retrieve its value from the record returned in the call. */
5269 if (!(present_gnu_tree (gnat_formal
)
5270 && TREE_CODE (get_gnu_tree (gnat_formal
)) == PARM_DECL
5271 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal
))
5272 || DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal
))))
5273 && Ekind (gnat_formal
) != E_In_Parameter
)
5275 /* Get the value to assign to this In Out or Out parameter. It is
5276 either the result of the function if there is only a single such
5277 parameter or the appropriate field from the record returned. */
5281 : build_component_ref (gnu_call
, TREE_PURPOSE (gnu_cico_list
),
5284 /* If the actual is a conversion, get the inner expression, which
5285 will be the real destination, and convert the result to the
5286 type of the actual parameter. */
5288 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list
));
5290 /* If the result is padded, remove the padding. */
5291 gnu_result
= maybe_padded_object (gnu_result
);
5293 /* If the actual is a type conversion, the real target object is
5294 denoted by the inner Expression and we need to convert the
5295 result to the associated type.
5296 We also need to convert our gnu assignment target to this type
5297 if the corresponding GNU_NAME was constructed from the GNAT
5298 conversion node and not from the inner Expression. */
5299 if (Nkind (gnat_actual
) == N_Type_Conversion
)
5301 const Node_Id gnat_expr
= Expression (gnat_actual
);
5303 gigi_checking_assert (!Do_Range_Check (gnat_expr
));
5306 = convert_with_check (Etype (gnat_expr
), gnu_result
,
5307 Do_Overflow_Check (gnat_actual
),
5308 Float_Truncate (gnat_actual
),
5311 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal
))))
5312 gnu_actual
= convert (TREE_TYPE (gnu_result
), gnu_actual
);
5315 /* Unchecked conversions as actuals for Out parameters are not
5316 allowed in user code because they are not variables, but do
5317 occur in front-end expansions. The associated GNU_NAME is
5318 always obtained from the inner expression in such cases. */
5319 else if (Nkind (gnat_actual
) == N_Unchecked_Type_Conversion
)
5320 gnu_result
= unchecked_convert (TREE_TYPE (gnu_actual
),
5322 No_Truncation (gnat_actual
));
5325 gigi_checking_assert (!Do_Range_Check (gnat_actual
));
5327 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual
)))
5328 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result
)))))
5329 gnu_result
= convert (TREE_TYPE (gnu_actual
), gnu_result
);
5332 get_atomic_access (gnat_actual
, &aa_type
, &aa_sync
);
5334 /* If an outer atomic access is required for an actual parameter,
5335 build the load-modify-store sequence. */
5336 if (aa_type
== OUTER_ATOMIC
)
5338 = build_load_modify_store (gnu_actual
, gnu_result
, gnat_node
);
5340 /* Or else, if a simple atomic access is required, build the atomic
5342 else if (aa_type
== SIMPLE_ATOMIC
)
5344 = build_atomic_store (gnu_actual
, gnu_result
, aa_sync
);
5346 /* Otherwise build a regular assignment. */
5348 gnu_result
= build_binary_op (MODIFY_EXPR
, NULL_TREE
,
5349 gnu_actual
, gnu_result
);
5351 if (EXPR_P (gnu_result
))
5352 set_expr_location_from_node (gnu_result
, gnat_node
);
5353 append_to_statement_list (gnu_result
, &gnu_stmt_list
);
5354 gnu_cico_list
= TREE_CHAIN (gnu_cico_list
);
5355 gnu_name_list
= TREE_CHAIN (gnu_name_list
);
5359 /* If this is a function call, the result is the call expression unless a
5360 target is specified, in which case we copy the result into the target
5361 and return the assignment statement. */
5364 /* If this is a function with copy-in/copy-out parameters, extract the
5365 return value from it and update the return type. */
5366 if (TYPE_CI_CO_LIST (gnu_subprog_type
))
5368 tree gnu_elmt
= TYPE_CI_CO_LIST (gnu_subprog_type
);
5370 = build_component_ref (gnu_call
, TREE_PURPOSE (gnu_elmt
), false);
5371 gnu_result_type
= TREE_TYPE (gnu_call
);
5374 /* If the function returns by direct reference, we have to dereference
5376 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type
))
5377 gnu_call
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_call
);
5381 Node_Id gnat_parent
= Parent (gnat_node
);
5382 enum tree_code op_code
;
5384 gigi_checking_assert (!Do_Range_Check (gnat_node
));
5386 /* ??? If the return type has variable size, then force the return
5387 slot optimization as we would not be able to create a temporary.
5388 That's what has been done historically. */
5389 if (return_type_with_variable_size_p (gnu_result_type
))
5390 op_code
= INIT_EXPR
;
5392 /* If this is a call to a pure function returning an array of scalar
5393 type, try to apply the return slot optimization. */
5394 else if ((TYPE_READONLY (gnu_subprog_type
)
5395 || TYPE_RESTRICT (gnu_subprog_type
))
5396 && is_array_of_scalar_type (gnu_result_type
)
5397 && TYPE_MODE (gnu_result_type
) == BLKmode
5398 && aggregate_value_p (gnu_result_type
, gnu_subprog_type
)
5399 && return_slot_opt_for_pure_call_p (gnu_target
, gnu_call
))
5400 op_code
= INIT_EXPR
;
5402 /* If this is the initialization of a return object in a function
5403 returning by invisible reference, we can always use the return
5404 slot optimization. */
5405 else if (TREE_CODE (gnu_target
) == INDIRECT_REF
5406 && TREE_CODE (TREE_OPERAND (gnu_target
, 0)) == RESULT_DECL
5407 && current_function_decl
5408 && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl
)))
5409 op_code
= INIT_EXPR
;
5412 op_code
= MODIFY_EXPR
;
5414 /* Use the required method to move the result to the target. */
5415 if (atomic_access
== OUTER_ATOMIC
)
5417 = build_load_modify_store (gnu_target
, gnu_call
, gnat_node
);
5418 else if (atomic_access
== SIMPLE_ATOMIC
)
5419 gnu_call
= build_atomic_store (gnu_target
, gnu_call
, atomic_sync
);
5420 else if (Present (gnat_smo
)
5421 && Present (Storage_Model_Copy_To (gnat_smo
)))
5423 = build_storage_model_store (gnat_smo
, gnu_target
, gnu_call
);
5426 = build_binary_op (op_code
, NULL_TREE
, gnu_target
, gnu_call
);
5428 if (EXPR_P (gnu_call
))
5429 set_expr_location_from_node (gnu_call
, gnat_parent
);
5430 append_to_statement_list (gnu_call
, &gnu_stmt_list
);
5433 *gnu_result_type_p
= get_unpadded_type (Etype (gnat_node
));
5436 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
5437 parameters, the result is just the call statement. */
5438 else if (!TYPE_CI_CO_LIST (gnu_subprog_type
))
5439 append_to_statement_list (gnu_call
, &gnu_stmt_list
);
5441 /* Finally, add the copy back statements, if any. */
5442 append_to_statement_list (gnu_after_list
, &gnu_stmt_list
);
5444 if (went_into_elab_proc
)
5445 current_function_decl
= NULL_TREE
;
5447 /* If we have pushed a binding level, pop it and finish up the enclosing
5449 if (pushed_binding_level
)
5451 add_stmt (gnu_stmt_list
);
5453 gnu_result
= end_stmt_group ();
5456 /* Otherwise, retrieve the statement list, if any. */
5457 else if (gnu_stmt_list
)
5458 gnu_result
= gnu_stmt_list
;
5460 /* Otherwise, just return the call expression. */
5464 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
5465 But first simplify if we have only one statement in the list. */
5466 if (returning_value
)
5468 tree first
= expr_first (gnu_result
), last
= expr_last (gnu_result
);
5472 = build_compound_expr (TREE_TYPE (gnu_call
), gnu_result
, gnu_call
);
5478 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an
5479 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
5482 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node
)
5484 /* If just annotating, ignore all EH and cleanups. */
5486 = !type_annotate_only
&& Present (Exception_Handlers (gnat_node
));
5487 const bool at_end
= !type_annotate_only
&& Present (At_End_Proc (gnat_node
));
5491 /* The exception handling mechanism can handle both ZCX and SJLJ schemes, and
5492 is exposed through the TRY_CATCH_EXPR construct that we build manually.
5494 ??? The region level calls down there have been specifically put in place
5495 for a ZCX context and currently the order in which things are emitted
5496 (region/handlers) is different from the SJLJ case. Instead of putting
5497 other calls with different conditions at other places for the SJLJ case,
5498 it seems cleaner to reorder things for the SJLJ case and generalize the
5499 condition to make it not ZCX specific. */
5501 /* First build the tree for the statements inside the sequence. */
5502 start_stmt_group ();
5504 for (gnat_temp
= First (Statements (gnat_node
));
5505 Present (gnat_temp
);
5506 gnat_temp
= Next (gnat_temp
))
5507 add_stmt (gnat_to_gnu (gnat_temp
));
5509 gnu_result
= end_stmt_group ();
5511 /* Then process the exception handlers, if any. */
5517 /* First make a group containing the handlers. */
5518 start_stmt_group ();
5519 for (gnat_temp
= First_Non_Pragma (Exception_Handlers (gnat_node
));
5520 Present (gnat_temp
);
5521 gnat_temp
= Next_Non_Pragma (gnat_temp
))
5522 add_stmt (gnat_to_gnu (gnat_temp
));
5523 gnu_handlers
= end_stmt_group ();
5525 /* Now make the TRY_CATCH_EXPR for the group. */
5527 = build2 (TRY_CATCH_EXPR
, void_type_node
, gnu_result
, gnu_handlers
);
5529 /* Set a location. We need to find a unique location for the dispatching
5530 code, otherwise we can get coverage or debugging issues. Try with
5531 the location of the end label. */
5532 if (Present (End_Label (gnat_node
))
5533 && Sloc_to_locus (Sloc (End_Label (gnat_node
)), &locus
))
5534 SET_EXPR_LOCATION (gnu_result
, locus
);
5536 /* Clear column information so that the exception handler of an
5537 implicit transient block does not incorrectly inherit the slocs
5538 of a decision, which would otherwise confuse control flow based
5539 coverage analysis tools. */
5540 set_expr_location_from_node (gnu_result
, gnat_node
, true);
5543 /* Process the At_End_Proc, if any. */
5546 start_stmt_group ();
5547 add_stmt (gnu_result
);
5548 At_End_Proc_to_gnu (gnat_node
);
5549 gnu_result
= end_stmt_group ();
5555 /* Return true if no statement in GNAT_LIST can alter the control flow. */
5558 stmt_list_cannot_alter_control_flow_p (List_Id gnat_list
)
5563 /* This is very conservative, we reject everything except for simple
5564 assignments between identifiers or literals. */
5565 for (Node_Id gnat_node
= First (gnat_list
);
5566 Present (gnat_node
);
5567 gnat_node
= Next (gnat_node
))
5569 if (Nkind (gnat_node
) != N_Assignment_Statement
)
5572 if (Nkind (Name (gnat_node
)) != N_Identifier
)
5575 Node_Kind nkind
= Nkind (Expression (gnat_node
));
5576 if (nkind
!= N_Identifier
5577 && nkind
!= N_Integer_Literal
5578 && nkind
!= N_Real_Literal
)
5585 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Exception_Handler,
5586 to a GCC tree, which is returned. */
5589 Exception_Handler_to_gnu (Node_Id gnat_node
)
5591 tree gnu_etypes_list
= NULL_TREE
;
5593 /* We build a TREE_LIST of nodes representing what exception types this
5594 handler can catch, with special cases for others and all others cases.
5596 Each exception type is actually identified by a pointer to the exception
5597 id, or to a dummy object for "others" and "all others". */
5598 for (Node_Id gnat_temp
= First (Exception_Choices (gnat_node
));
5600 gnat_temp
= Next (gnat_temp
))
5602 tree gnu_expr
, gnu_etype
;
5604 if (Nkind (gnat_temp
) == N_Others_Choice
)
5606 gnu_expr
= All_Others (gnat_temp
) ? all_others_decl
: others_decl
;
5607 gnu_etype
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
5609 else if (Nkind (gnat_temp
) == N_Identifier
5610 || Nkind (gnat_temp
) == N_Expanded_Name
)
5612 Entity_Id gnat_ex_id
= Entity (gnat_temp
);
5614 /* Exception may be a renaming. Recover original exception which is
5615 the one elaborated and registered. */
5616 if (Present (Renamed_Object (gnat_ex_id
)))
5617 gnat_ex_id
= Renamed_Object (gnat_ex_id
);
5619 gnu_expr
= gnat_to_gnu_entity (gnat_ex_id
, NULL_TREE
, false);
5620 gnu_etype
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
5625 /* The GCC interface expects NULL to be passed for catch all handlers, so
5626 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
5627 is integer_zero_node. It would not work, however, because GCC's
5628 notion of "catch all" is stronger than our notion of "others". Until
5629 we correctly use the cleanup interface as well, doing that would
5630 prevent the "all others" handlers from being seen, because nothing
5631 can be caught beyond a catch all from GCC's point of view. */
5632 gnu_etypes_list
= tree_cons (NULL_TREE
, gnu_etype
, gnu_etypes_list
);
5635 start_stmt_group ();
5637 /* Expand a call to the begin_handler hook at the beginning of the
5638 handler, and arrange for a call to the end_handler hook to occur
5639 on every possible exit path. GDB sets a breakpoint in the
5640 begin_handler for catchpoints.
5642 A v1 begin handler saves the cleanup from the exception object,
5643 and marks the exception as in use, so that it will not be
5644 released by other handlers. A v1 end handler restores the
5645 cleanup and releases the exception object, unless it is still
5646 claimed, or the exception is being propagated (reraised).
5648 __builtin_eh_pointer references the exception occurrence being
5649 handled or propagated. Within the handler region, it is the
5650 former, but within the else branch of the EH_ELSE_EXPR, i.e. the
5651 exceptional cleanup path, it is the latter, so we must save the
5652 occurrence being handled early on, so that, should an exception
5653 be (re)raised, we can release the current exception, or figure
5654 out we're not to release it because we're propagating a reraise
5657 We use local variables to retrieve the incoming value at handler
5658 entry time (EXPTR), the saved cleanup (EXCLN) and the token
5659 (EXVTK), and reuse them to feed the end_handler hook's argument
5662 /* CODE: void *EXPTR = __builtin_eh_pointer (0); */
5663 tree gnu_current_exc_ptr
5664 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
5665 1, integer_zero_node
);
5667 = create_var_decl (get_identifier ("EXPTR"), NULL_TREE
,
5668 ptr_type_node
, gnu_current_exc_ptr
,
5669 true, false, false, false, false, true, true,
5672 tree prev_gnu_incoming_exc_ptr
= gnu_incoming_exc_ptr
;
5673 gnu_incoming_exc_ptr
= exc_ptr
;
5675 /* begin_handler_decl must not throw, so we can use it as an
5676 initializer for a variable used in cleanups.
5678 CODE: void *EXCLN = __gnat_begin_handler_v1 (EXPTR); */
5680 = create_var_decl (get_identifier ("EXCLN"), NULL_TREE
,
5682 build_call_n_expr (begin_handler_decl
, 1,
5684 true, false, false, false, false,
5685 true, true, NULL
, gnat_node
);
5687 /* Declare and initialize the choice parameter, if present. */
5688 if (Present (Choice_Parameter (gnat_node
)))
5691 = gnat_to_gnu_entity (Choice_Parameter (gnat_node
), NULL_TREE
, true);
5693 /* CODE: __gnat_set_exception_parameter (&choice_param, EXPTR); */
5694 add_stmt (build_call_n_expr
5695 (set_exception_parameter_decl
, 2,
5696 build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_param
),
5697 gnu_incoming_exc_ptr
));
5700 /* CODE: <handler proper> */
5701 add_stmt_list (Statements (gnat_node
));
5703 tree call
= build_call_n_expr (end_handler_decl
, 3,
5707 /* If the handler can only end by falling off the end, don't bother
5709 if (stmt_list_cannot_alter_control_flow_p (Statements (gnat_node
)))
5710 /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, NULL); */
5711 add_stmt_with_node (call
, gnat_node
);
5712 /* Otherwise, all of the above is after
5715 The call above will appear after
5718 And the code below will appear after
5721 The else block to a finally block is taken instead of the finally
5722 block when an exception propagates out of the try block. */
5725 start_stmt_group ();
5727 /* CODE: void *EXPRP = __builtin_eh_handler (0); */
5729 = create_var_decl (get_identifier ("EXPRP"), NULL_TREE
,
5731 build_call_expr (builtin_decl_explicit
5732 (BUILT_IN_EH_POINTER
),
5733 1, integer_zero_node
),
5734 true, false, false, false, false,
5735 true, true, NULL
, gnat_node
);
5737 /* CODE: __gnat_end_handler_v1 (EXPTR, EXCLN, EXPRP); */
5738 tree ecall
= build_call_n_expr (end_handler_decl
, 3,
5743 add_stmt_with_node (ecall
, gnat_node
);
5746 tree eblk
= end_stmt_group ();
5747 tree ehls
= build2 (EH_ELSE_EXPR
, void_type_node
, call
, eblk
);
5748 add_cleanup (ehls
, gnat_node
);
5751 gnu_incoming_exc_ptr
= prev_gnu_incoming_exc_ptr
;
5754 build2 (CATCH_EXPR
, void_type_node
, gnu_etypes_list
, end_stmt_group ());
5757 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Compilation_Unit. */
5760 Compilation_Unit_to_gnu (Node_Id gnat_node
)
5762 const Node_Id gnat_unit
= Unit (gnat_node
);
5763 const bool body_p
= (Nkind (gnat_unit
) == N_Package_Body
5764 || Nkind (gnat_unit
) == N_Subprogram_Body
);
5765 const Entity_Id gnat_unit_entity
= Defining_Entity (gnat_unit
);
5766 Entity_Id gnat_entity
;
5767 Node_Id gnat_pragma
, gnat_iter
;
5768 /* Make the decl for the elaboration procedure. Emit debug info for it, so
5769 that users can break into their elaboration code in debuggers. Kludge:
5770 don't consider it as a definition so that we have a line map for its
5771 body, but no subprogram description in debug info. In addition, don't
5772 qualify it as artificial, even though it is not a user subprogram per se,
5773 in particular for specs. Unlike, say, clones created internally by the
5774 compiler, this subprogram materializes specific user code and flagging it
5775 artificial would take elab code away from gcov's analysis. */
5776 tree gnu_elab_proc_decl
5777 = create_subprog_decl
5778 (create_concat_name (gnat_unit_entity
, body_p
? "elabb" : "elabs"),
5779 NULL_TREE
, void_ftype
, NULL_TREE
,
5780 is_default
, true, false, false, true, false, NULL
, gnat_unit
);
5781 struct elab_info
*info
;
5783 vec_safe_push (gnu_elab_proc_stack
, gnu_elab_proc_decl
);
5784 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl
) = 1;
5786 /* Initialize the information structure for the function. */
5787 allocate_struct_function (gnu_elab_proc_decl
, false);
5790 current_function_decl
= NULL_TREE
;
5792 start_stmt_group ();
5795 /* For a body, first process the spec if there is one. */
5796 if (Nkind (gnat_unit
) == N_Package_Body
5797 || (Nkind (gnat_unit
) == N_Subprogram_Body
&& !Acts_As_Spec (gnat_node
)))
5798 add_stmt (gnat_to_gnu (Library_Unit (gnat_node
)));
5800 if (type_annotate_only
&& gnat_node
== Cunit (Main_Unit
))
5802 elaborate_all_entities (gnat_node
);
5804 if (Nkind (gnat_unit
) == N_Subprogram_Declaration
5805 || Nkind (gnat_unit
) == N_Generic_Package_Declaration
5806 || Nkind (gnat_unit
) == N_Generic_Subprogram_Declaration
)
5810 /* Then process any pragmas and declarations preceding the unit. */
5811 for (gnat_pragma
= First (Context_Items (gnat_node
));
5812 Present (gnat_pragma
);
5813 gnat_pragma
= Next (gnat_pragma
))
5814 if (Nkind (gnat_pragma
) == N_Pragma
)
5815 add_stmt (gnat_to_gnu (gnat_pragma
));
5816 process_decls (Declarations (Aux_Decls_Node (gnat_node
)), Empty
,
5819 /* Process the unit itself. */
5820 add_stmt (gnat_to_gnu (gnat_unit
));
5822 /* Generate code for all the inlined subprograms. */
5823 for (gnat_entity
= First_Inlined_Subprogram (gnat_node
);
5824 Present (gnat_entity
);
5825 gnat_entity
= Next_Inlined_Subprogram (gnat_entity
))
5829 /* Without optimization, process only the required subprograms. */
5830 if (!optimize
&& !Has_Pragma_Inline_Always (gnat_entity
))
5833 /* The set of inlined subprograms is computed from data recorded early
5834 during expansion and it can be a strict superset of the final set
5835 computed after semantic analysis, for example if a call to such a
5836 subprogram occurs in a pragma Assert and assertions are disabled.
5837 In that case, semantic analysis resets Is_Public to false but the
5838 entry for the subprogram in the inlining tables is stalled. */
5839 if (!Is_Public (gnat_entity
))
5842 gnat_body
= Parent (Declaration_Node (gnat_entity
));
5843 if (Nkind (gnat_body
) != N_Subprogram_Body
)
5845 /* ??? This happens when only the spec of a package is provided. */
5846 if (No (Corresponding_Body (gnat_body
)))
5850 = Parent (Declaration_Node (Corresponding_Body (gnat_body
)));
5853 /* Define the entity first so we set DECL_EXTERNAL. */
5854 gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, false);
5855 add_stmt (gnat_to_gnu (gnat_body
));
5858 /* Process any pragmas and actions following the unit. */
5859 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node
)));
5860 add_stmt_list (Actions (Aux_Decls_Node (gnat_node
)));
5861 finalize_from_limited_with ();
5863 /* Then process the expressions of pragma Compile_Time_{Error|Warning} to
5864 annotate types referenced therein if they have not been annotated. */
5865 for (int i
= 0; gnat_compile_time_expr_list
.iterate (i
, &gnat_iter
); i
++)
5866 (void) gnat_to_gnu_external (gnat_iter
);
5867 gnat_compile_time_expr_list
.release ();
5869 /* Save away what we've made so far and finish it up. */
5870 set_current_block_context (gnu_elab_proc_decl
);
5872 DECL_SAVED_TREE (gnu_elab_proc_decl
) = end_stmt_group ();
5873 set_end_locus_from_node (gnu_elab_proc_decl
, gnat_unit
);
5874 gnu_elab_proc_stack
->pop ();
5876 /* Record this potential elaboration procedure for later processing. */
5877 info
= ggc_alloc
<elab_info
> ();
5878 info
->next
= elab_info_list
;
5879 info
->elab_proc
= gnu_elab_proc_decl
;
5880 info
->gnat_node
= gnat_node
;
5881 elab_info_list
= info
;
5883 /* Force the processing for all nodes that remain in the queue. */
5884 process_deferred_decl_context (true);
5887 /* Mark COND, a boolean expression, as predicating a call to a noreturn
5888 function, i.e. predict that it is very likely false, and return it.
5890 The compiler will automatically predict the last edge leading to a call
5891 to a noreturn function as very unlikely taken. This function makes it
5892 possible to extend the prediction to predecessors in case the condition
5893 is made up of several short-circuit operators. */
5896 build_noreturn_cond (tree cond
)
5898 tree pred_cst
= build_int_cst (integer_type_node
, PRED_NORETURN
);
5900 build_call_expr_internal_loc (UNKNOWN_LOCATION
, IFN_BUILTIN_EXPECT
,
5901 boolean_type_node
, 3, cond
,
5902 boolean_false_node
, pred_cst
);
5905 /* Subroutine of gnat_to_gnu to translate GNAT_RANGE, a node representing a
5906 range of values, into GNU_LOW and GNU_HIGH bounds. */
5909 Range_to_gnu (Node_Id gnat_range
, tree
*gnu_low
, tree
*gnu_high
)
5911 /* GNAT_RANGE is either an N_Range or an identifier denoting a subtype. */
5912 switch (Nkind (gnat_range
))
5915 *gnu_low
= gnat_to_gnu (Low_Bound (gnat_range
));
5916 *gnu_high
= gnat_to_gnu (High_Bound (gnat_range
));
5919 case N_Expanded_Name
:
5922 tree gnu_range_type
= get_unpadded_type (Entity (gnat_range
));
5923 tree gnu_range_base_type
= get_base_type (gnu_range_type
);
5926 = convert (gnu_range_base_type
, TYPE_MIN_VALUE (gnu_range_type
));
5928 = convert (gnu_range_base_type
, TYPE_MAX_VALUE (gnu_range_type
));
5937 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Raise_xxx_Error,
5938 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
5939 where we should place the result type. */
5942 Raise_Error_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
)
5944 const Node_Kind kind
= Nkind (gnat_node
);
5945 const Node_Id gnat_cond
= Condition (gnat_node
);
5946 const int reason
= UI_To_Int (Reason (gnat_node
));
5947 const bool with_extra_info
5948 = Exception_Extra_Info
5949 && !No_Exception_Handlers_Set ()
5950 && No (get_exception_label (kind
));
5951 tree gnu_result
= NULL_TREE
, gnu_cond
= NULL_TREE
;
5954 /* The following processing is not required for correctness. Its purpose is
5955 to give more precise error messages and to record some information. */
5958 case CE_Access_Check_Failed
:
5959 if (with_extra_info
)
5960 gnu_result
= build_call_raise_column (reason
, gnat_node
, kind
);
5963 case CE_Index_Check_Failed
:
5964 case CE_Range_Check_Failed
:
5965 case CE_Invalid_Data
:
5966 if (No (gnat_cond
) || Nkind (gnat_cond
) != N_Op_Not
)
5968 gnat_rcond
= Right_Opnd (gnat_cond
);
5969 if (Nkind (gnat_rcond
) == N_In
5970 || Nkind (gnat_rcond
) == N_Op_Ge
5971 || Nkind (gnat_rcond
) == N_Op_Le
)
5973 const Node_Id gnat_index
= Left_Opnd (gnat_rcond
);
5974 const Node_Id gnat_type
= Etype (gnat_index
);
5975 tree gnu_index
= gnat_to_gnu (gnat_index
);
5976 tree gnu_type
= get_unpadded_type (gnat_type
);
5977 tree gnu_low_bound
, gnu_high_bound
, disp
;
5978 struct loop_info_d
*loop
;
5981 switch (Nkind (gnat_rcond
))
5984 Range_to_gnu (Right_Opnd (gnat_rcond
),
5985 &gnu_low_bound
, &gnu_high_bound
);
5989 gnu_low_bound
= gnat_to_gnu (Right_Opnd (gnat_rcond
));
5990 gnu_high_bound
= TYPE_MAX_VALUE (gnu_type
);
5994 gnu_low_bound
= TYPE_MIN_VALUE (gnu_type
);
5995 gnu_high_bound
= gnat_to_gnu (Right_Opnd (gnat_rcond
));
6002 gnu_type
= maybe_character_type (gnu_type
);
6003 if (TREE_TYPE (gnu_index
) != gnu_type
)
6005 gnu_low_bound
= convert (gnu_type
, gnu_low_bound
);
6006 gnu_high_bound
= convert (gnu_type
, gnu_high_bound
);
6007 gnu_index
= convert (gnu_type
, gnu_index
);
6011 && Known_Esize (gnat_type
)
6012 && UI_To_Int (Esize (gnat_type
)) <= 32)
6014 = build_call_raise_range (reason
, gnat_node
, kind
, gnu_index
,
6015 gnu_low_bound
, gnu_high_bound
);
6017 /* If optimization is enabled and we are inside a loop, we try to
6018 compute invariant conditions for checks applied to the iteration
6019 variable, i.e. conditions that are independent of the variable
6020 and necessary in order for the checks to fail in the course of
6021 some iteration. If we succeed, we consider an alternative:
6023 1. If loop unswitching is enabled, we prepend these conditions
6024 to the original conditions of the checks. This will make it
6025 possible for the loop unswitching pass to replace the loop
6026 with two loops, one of which has the checks eliminated and
6027 the other has the original checks reinstated, and a prologue
6028 implementing a run-time selection. The former loop will be
6029 for example suitable for vectorization.
6031 2. Otherwise, we instead append the conditions to the original
6032 conditions of the checks. At worse, if the conditions cannot
6033 be evaluated at compile time, they will be evaluated as true
6034 at run time only when the checks have already failed, thus
6035 contributing negatively only to the size of the executable.
6036 But the hope is that these invariant conditions be evaluated
6037 at compile time to false, thus taking away the entire checks
6042 || (gnu_low_bound
= gnat_invariant_expr (gnu_low_bound
)))
6044 || (gnu_high_bound
= gnat_invariant_expr (gnu_high_bound
)))
6045 && (loop
= find_loop_for (gnu_index
, &disp
, &neg_p
)))
6047 struct range_check_info_d
*rci
= ggc_alloc
<range_check_info_d
> ();
6048 rci
->low_bound
= gnu_low_bound
;
6049 rci
->high_bound
= gnu_high_bound
;
6052 rci
->type
= gnu_type
;
6054 = build1 (SAVE_EXPR
, boolean_type_node
, boolean_true_node
);
6055 vec_safe_push (loop
->checks
, rci
);
6056 gnu_cond
= build_noreturn_cond (gnat_to_gnu (gnat_cond
));
6058 gnu_cond
= build_binary_op (TRUTH_ANDIF_EXPR
,
6063 gnu_cond
= build_binary_op (TRUTH_ANDIF_EXPR
,
6066 rci
->inserted_cond
);
6075 /* The following processing does the real work, but we must nevertheless make
6076 sure not to override the result of the previous processing. */
6078 gnu_result
= build_call_raise (reason
, gnat_node
, kind
);
6079 set_expr_location_from_node (gnu_result
, gnat_node
);
6081 *gnu_result_type_p
= get_unpadded_type (Etype (gnat_node
));
6083 /* If the type is VOID, this is a statement, so we need to generate the code
6084 for the call. Handle a condition, if there is one. */
6085 if (VOID_TYPE_P (*gnu_result_type_p
))
6087 if (Present (gnat_cond
))
6090 gnu_cond
= gnat_to_gnu (gnat_cond
);
6091 if (integer_zerop (gnu_cond
))
6092 return alloc_stmt_list ();
6093 gnu_result
= build3 (COND_EXPR
, void_type_node
, gnu_cond
, gnu_result
,
6094 alloc_stmt_list ());
6099 /* The condition field must not be present when the node is used as an
6101 gigi_checking_assert (No (gnat_cond
));
6102 gnu_result
= build1 (NULL_EXPR
, *gnu_result_type_p
, gnu_result
);
6108 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
6109 parameter of a call. */
6112 lhs_or_actual_p (Node_Id gnat_node
)
6114 const Node_Id gnat_parent
= Parent (gnat_node
);
6115 const Node_Kind kind
= Nkind (gnat_parent
);
6117 if (kind
== N_Assignment_Statement
&& Name (gnat_parent
) == gnat_node
)
6120 if ((kind
== N_Procedure_Call_Statement
|| kind
== N_Function_Call
)
6121 && Name (gnat_parent
) != gnat_node
)
6124 if (kind
== N_Parameter_Association
)
6130 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
6131 of an assignment or an actual parameter of a call. */
6134 present_in_lhs_or_actual_p (Node_Id gnat_node
)
6136 return lhs_or_actual_p (gnat_node
)
6137 || (node_is_type_conversion (Parent (gnat_node
))
6138 && lhs_or_actual_p (Parent (gnat_node
)));
6141 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
6142 as gigi is concerned. This is used to avoid conversions on the LHS. */
6145 unchecked_conversion_nop (Node_Id gnat_node
)
6147 Entity_Id from_type
, to_type
;
6149 /* The conversion must be on the LHS of an assignment or an actual parameter
6150 of a call. Otherwise, even if the conversion was essentially a no-op, it
6151 could de facto ensure type consistency and this should be preserved. */
6152 if (!lhs_or_actual_p (gnat_node
))
6155 from_type
= Etype (Expression (gnat_node
));
6157 /* We're interested in artificial conversions generated by the front-end
6158 to make private types explicit, e.g. in Expand_Assign_Array. */
6159 if (!Is_Private_Type (from_type
))
6162 from_type
= Underlying_Type (from_type
);
6163 to_type
= Etype (gnat_node
);
6165 /* The direct conversion to the underlying type is a no-op. */
6166 if (to_type
== from_type
)
6169 /* For an array subtype, the conversion to the PAIT is a no-op. */
6170 if (Ekind (from_type
) == E_Array_Subtype
6171 && to_type
== Packed_Array_Impl_Type (from_type
))
6174 /* For a record subtype, the conversion to the type is a no-op. */
6175 if (Ekind (from_type
) == E_Record_Subtype
6176 && to_type
== Etype (from_type
))
6182 /* Return true if GNAT_NODE represents a statement. */
6185 statement_node_p (Node_Id gnat_node
)
6187 const Node_Kind kind
= Nkind (gnat_node
);
6189 if (kind
== N_Label
)
6192 if (IN (kind
, N_Statement_Other_Than_Procedure_Call
))
6195 if (kind
== N_Procedure_Call_Statement
)
6198 if (IN (kind
, N_Raise_xxx_Error
) && Ekind (Etype (gnat_node
)) == E_Void
)
6204 /* This function is the driver of the GNAT to GCC tree transformation process.
6205 It is the entry point of the tree transformer. GNAT_NODE is the root of
6206 some GNAT tree. Return the root of the corresponding GCC tree. If this
6207 is an expression, return the GCC equivalent of the expression. If this
6208 is a statement, return the statement or add it to the current statement
6209 group, in which case anything returned is to be interpreted as occurring
6210 after anything added. */
6213 gnat_to_gnu (Node_Id gnat_node
)
6215 const Node_Kind kind
= Nkind (gnat_node
);
6216 tree gnu_result
= error_mark_node
; /* Default to no value. */
6217 tree gnu_result_type
= void_type_node
;
6218 tree gnu_expr
, gnu_lhs
, gnu_rhs
;
6220 atomic_acces_t aa_type
;
6221 bool went_into_elab_proc
;
6225 /* Save node number for error message and set location information. */
6226 if (Sloc (gnat_node
) > No_Location
)
6227 Current_Error_Node
= gnat_node
;
6228 Sloc_to_locus (Sloc (gnat_node
), &input_location
);
6230 /* If we are only annotating types and this node is a statement, return
6231 an empty statement list. */
6232 if (type_annotate_only
&& statement_node_p (gnat_node
))
6233 return alloc_stmt_list ();
6235 /* If we are only annotating types and this node is a subexpression, return
6236 a NULL_EXPR, but filter out nodes appearing in the expressions attached
6237 to packed array implementation types. */
6238 if (type_annotate_only
6239 && IN (kind
, N_Subexpr
)
6240 && !(((IN (kind
, N_Op
) && kind
!= N_Op_Expon
)
6241 || kind
== N_Type_Conversion
)
6242 && Is_Integer_Type (Etype (gnat_node
)))
6243 && !(kind
== N_Attribute_Reference
6244 && (Get_Attribute_Id (Attribute_Name (gnat_node
)) == Attr_Length
6245 || Get_Attribute_Id (Attribute_Name (gnat_node
)) == Attr_Size
)
6246 && Is_Constrained (Etype (Prefix (gnat_node
)))
6247 && !Is_Constr_Subt_For_U_Nominal (Etype (Prefix (gnat_node
))))
6248 && kind
!= N_Expanded_Name
6249 && kind
!= N_Identifier
6250 && !Compile_Time_Known_Value (gnat_node
))
6251 return build1 (NULL_EXPR
, get_unpadded_type (Etype (gnat_node
)),
6252 build_call_raise (CE_Range_Check_Failed
, gnat_node
,
6253 N_Raise_Constraint_Error
));
6255 /* If this is a statement and we are at top level, it must be part of the
6256 elaboration procedure, so mark us as being in that procedure. */
6257 if ((statement_node_p (gnat_node
)
6258 || kind
== N_Handled_Sequence_Of_Statements
6259 || kind
== N_Implicit_Label_Declaration
)
6260 && !current_function_decl
)
6262 current_function_decl
= get_elaboration_procedure ();
6263 went_into_elab_proc
= true;
6266 went_into_elab_proc
= false;
6270 /********************************/
6271 /* Chapter 2: Lexical Elements */
6272 /********************************/
6275 case N_Expanded_Name
:
6276 case N_Operator_Symbol
:
6277 case N_Defining_Identifier
:
6278 case N_Defining_Operator_Symbol
:
6279 gnu_result
= Identifier_to_gnu (gnat_node
, &gnu_result_type
);
6281 /* If atomic access is required on the RHS, build the atomic load. */
6282 if (simple_atomic_access_required_p (gnat_node
, &aa_sync
)
6283 && !present_in_lhs_or_actual_p (gnat_node
))
6284 gnu_result
= build_atomic_load (gnu_result
, aa_sync
);
6287 case N_Integer_Literal
:
6291 /* Get the type of the result, looking inside any padding and
6292 justified modular types. Then get the value in that type. */
6293 gnu_type
= gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6295 if (TREE_CODE (gnu_type
) == RECORD_TYPE
6296 && TYPE_JUSTIFIED_MODULAR_P (gnu_type
))
6297 gnu_type
= TREE_TYPE (TYPE_FIELDS (gnu_type
));
6299 gnu_result
= UI_To_gnu (Intval (gnat_node
), gnu_type
);
6301 /* If the result overflows (meaning it doesn't fit in its base type),
6302 abort, unless this is for a named number because that's not fatal.
6303 We would like to check that the value is within the range of the
6304 subtype, but that causes problems with subtypes whose usage will
6305 raise Constraint_Error and also with biased representation. */
6306 if (TREE_OVERFLOW (gnu_result
))
6308 if (Nkind (Parent (gnat_node
)) == N_Number_Declaration
)
6309 gnu_result
= error_mark_node
;
6316 case N_Character_Literal
:
6317 /* If a Entity is present, it means that this was one of the
6318 literals in a user-defined character type. In that case,
6319 just return the value in the CONST_DECL. Otherwise, use the
6320 character code. In that case, the base type should be an
6321 INTEGER_TYPE, but we won't bother checking for that. */
6322 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6323 if (Present (Entity (gnat_node
)))
6324 gnu_result
= DECL_INITIAL (get_gnu_tree (Entity (gnat_node
)));
6327 = build_int_cst (gnu_result_type
,
6328 UI_To_CC (Char_Literal_Value (gnat_node
)));
6331 case N_Real_Literal
:
6332 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6334 /* If this is of a fixed-point type, the value we want is the value of
6335 the corresponding integer. */
6336 if (Is_Fixed_Point_Type (Underlying_Type (Etype (gnat_node
))))
6338 gnu_result
= UI_To_gnu (Corresponding_Integer_Value (gnat_node
),
6340 gcc_assert (!TREE_OVERFLOW (gnu_result
));
6345 Ureal ur_realval
= Realval (gnat_node
);
6347 /* First convert the value to a machine number if it isn't already.
6348 That will force the base to 2 for non-zero values and simplify
6349 the rest of the logic. */
6350 if (!Is_Machine_Number (gnat_node
))
6352 = Machine (Base_Type (Underlying_Type (Etype (gnat_node
))),
6353 ur_realval
, Round_Even
, gnat_node
);
6355 if (UR_Is_Zero (ur_realval
))
6356 gnu_result
= build_real (gnu_result_type
, dconst0
);
6359 REAL_VALUE_TYPE tmp
;
6361 gnu_result
= UI_To_gnu (Numerator (ur_realval
), gnu_result_type
);
6363 /* The base must be 2 as Machine guarantees this, so we scale
6364 the value, which we know can fit in the mantissa of the type
6365 (hence the use of that type above). */
6366 gcc_assert (Rbase (ur_realval
) == 2);
6367 real_ldexp (&tmp
, &TREE_REAL_CST (gnu_result
),
6368 - UI_To_Int (Denominator (ur_realval
)));
6369 gnu_result
= build_real (gnu_result_type
, tmp
);
6372 /* Now see if we need to negate the result. Do it this way to
6373 properly handle -0. */
6374 if (UR_Is_Negative (Realval (gnat_node
)))
6376 = build_unary_op (NEGATE_EXPR
, get_base_type (gnu_result_type
),
6382 case N_String_Literal
:
6383 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6384 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type
)) == HOST_BITS_PER_CHAR
)
6386 String_Id gnat_string
= Strval (gnat_node
);
6387 int length
= String_Length (gnat_string
);
6390 if (length
>= ALLOCA_THRESHOLD
)
6391 string
= XNEWVEC (char, length
);
6393 string
= (char *) alloca (length
);
6395 /* Build the string with the characters in the literal. Note
6396 that Ada strings are 1-origin. */
6397 for (i
= 0; i
< length
; i
++)
6398 string
[i
] = Get_String_Char (gnat_string
, i
+ 1);
6400 gnu_result
= build_string (length
, string
);
6402 /* Strings in GCC don't normally have types, but we want
6403 this to not be converted to the array type. */
6404 TREE_TYPE (gnu_result
) = gnu_result_type
;
6406 if (length
>= ALLOCA_THRESHOLD
)
6411 /* Build a list consisting of each character, then make
6413 String_Id gnat_string
= Strval (gnat_node
);
6414 int length
= String_Length (gnat_string
);
6416 tree gnu_idx
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type
));
6417 tree gnu_one_node
= convert (TREE_TYPE (gnu_idx
), integer_one_node
);
6418 vec
<constructor_elt
, va_gc
> *gnu_vec
;
6419 vec_alloc (gnu_vec
, length
);
6421 for (i
= 0; i
< length
; i
++)
6423 tree t
= build_int_cst (TREE_TYPE (gnu_result_type
),
6424 Get_String_Char (gnat_string
, i
+ 1));
6426 CONSTRUCTOR_APPEND_ELT (gnu_vec
, gnu_idx
, t
);
6427 gnu_idx
= int_const_binop (PLUS_EXPR
, gnu_idx
, gnu_one_node
);
6430 gnu_result
= gnat_build_constructor (gnu_result_type
, gnu_vec
);
6435 gnu_result
= Pragma_to_gnu (gnat_node
);
6438 /**************************************/
6439 /* Chapter 3: Declarations and Types */
6440 /**************************************/
6442 case N_Subtype_Declaration
:
6443 case N_Full_Type_Declaration
:
6444 case N_Incomplete_Type_Declaration
:
6445 case N_Private_Type_Declaration
:
6446 case N_Private_Extension_Declaration
:
6447 case N_Task_Type_Declaration
:
6448 process_type (Defining_Entity (gnat_node
));
6449 gnu_result
= alloc_stmt_list ();
6452 case N_Object_Declaration
:
6453 case N_Number_Declaration
:
6454 case N_Exception_Declaration
:
6455 gnat_temp
= Defining_Entity (gnat_node
);
6456 gnu_result
= alloc_stmt_list ();
6458 /* If we are just annotating types and this object has an unconstrained
6459 or task type, don't elaborate it. */
6460 if (type_annotate_only
6461 && (((Is_Array_Type (Etype (gnat_temp
))
6462 || Is_Record_Type (Etype (gnat_temp
)))
6463 && !Is_Constrained (Etype (gnat_temp
)))
6464 || Is_Concurrent_Type (Etype (gnat_temp
))))
6467 /* If this is a constant related to a return initialized by a reference
6468 to a function call in a function returning by invisible reference:
6470 type Ann is access all Result_Type;
6471 Rnn : constant Ann := Func'reference;
6475 then elide the temporary by forwarding the return object to Func:
6477 result_type *Rnn = (result_type *) <retval>;
6478 *<retval> = Func (); [return slot optimization]
6482 That's necessary if the result type needs finalization because the
6483 temporary would never be adjusted as Expand_Simple_Function_Return
6484 also elides the temporary in this case. */
6485 if (Ekind (gnat_temp
) == E_Constant
6486 && Is_Related_To_Func_Return (gnat_temp
)
6487 && Nkind (Expression (gnat_node
)) == N_Reference
6488 && Nkind (Prefix (Expression (gnat_node
))) == N_Function_Call
6489 && current_function_decl
6490 && TREE_ADDRESSABLE (TREE_TYPE (current_function_decl
)))
6492 gnat_to_gnu_entity (gnat_temp
,
6493 DECL_RESULT (current_function_decl
),
6496 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
6497 DECL_RESULT (current_function_decl
));
6499 = Call_to_gnu (Prefix (Expression (gnat_node
)),
6500 &gnu_result_type
, gnu_result
,
6501 NOT_ATOMIC
, false, Empty
);
6505 if (Present (Expression (gnat_node
))
6506 && !(kind
== N_Object_Declaration
&& No_Initialization (gnat_node
))
6507 && (!type_annotate_only
6508 || Compile_Time_Known_Value (Expression (gnat_node
))))
6510 gigi_checking_assert (!Do_Range_Check (Expression (gnat_node
)));
6512 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
6514 /* First deal with erroneous expressions. */
6515 if (TREE_CODE (gnu_expr
) == ERROR_MARK
)
6517 /* If this is a named number for which we cannot manipulate
6518 the value, just skip the declaration altogether. */
6519 if (kind
== N_Number_Declaration
)
6521 else if (type_annotate_only
)
6522 gnu_expr
= NULL_TREE
;
6525 /* Then a special case: we do not want the SLOC of the expression
6526 of the tag to pop up every time it is referenced somewhere. */
6527 else if (EXPR_P (gnu_expr
) && Is_Tag (gnat_temp
))
6528 SET_EXPR_LOCATION (gnu_expr
, UNKNOWN_LOCATION
);
6531 gnu_expr
= NULL_TREE
;
6533 /* If this is a deferred constant with an address clause, we ignore the
6534 full view since the clause is on the partial view and we cannot have
6535 2 different GCC trees for the object. The only bits of the full view
6536 we will use is the initializer, but it will be directly fetched. */
6537 if (Ekind (gnat_temp
) == E_Constant
6538 && Present (Address_Clause (gnat_temp
))
6539 && Present (Full_View (gnat_temp
)))
6540 save_gnu_tree (Full_View (gnat_temp
), error_mark_node
, true);
6542 /* If this object has its elaboration delayed, we must force evaluation
6543 of GNU_EXPR now and save it for the freeze point. Note that we need
6544 not do anything special at the global level since the lifetime of the
6545 temporary is fully contained within the elaboration routine. */
6546 if (Present (Freeze_Node (gnat_temp
)))
6550 gnu_result
= gnat_save_expr (gnu_expr
);
6551 save_gnu_tree (gnat_node
, gnu_result
, true);
6555 gnat_to_gnu_entity (gnat_temp
, gnu_expr
, true);
6558 case N_Object_Renaming_Declaration
:
6559 gnat_temp
= Defining_Entity (gnat_node
);
6560 gnu_result
= alloc_stmt_list ();
6562 /* Don't do anything if this renaming is handled by the front end and it
6563 does not need debug info. Note that we consider renamings don't need
6564 debug info when optimizing: our way to describe them has a
6565 memory/elaboration footprint.
6567 Don't do anything neither if we are just annotating types and this
6568 object has a composite or task type, don't elaborate it. */
6569 if ((!Is_Renaming_Of_Object (gnat_temp
)
6570 || (Needs_Debug_Info (gnat_temp
)
6572 && can_materialize_object_renaming_p
6573 (Renamed_Object (gnat_temp
))))
6574 && ! (type_annotate_only
6575 && (Is_Array_Type (Etype (gnat_temp
))
6576 || Is_Record_Type (Etype (gnat_temp
))
6577 || Is_Concurrent_Type (Etype (gnat_temp
)))))
6578 gnat_to_gnu_entity (gnat_temp
,
6579 gnat_to_gnu (Renamed_Object (gnat_temp
)),
6583 case N_Exception_Renaming_Declaration
:
6584 gnat_temp
= Defining_Entity (gnat_node
);
6585 gnu_result
= alloc_stmt_list ();
6587 if (Present (Renamed_Entity (gnat_temp
)))
6588 gnat_to_gnu_entity (gnat_temp
,
6589 gnat_to_gnu (Renamed_Entity (gnat_temp
)),
6593 case N_Subprogram_Renaming_Declaration
:
6595 const Node_Id gnat_renaming
= Defining_Entity (gnat_node
);
6596 const Node_Id gnat_renamed
= Renamed_Entity (gnat_renaming
);
6598 gnu_result
= alloc_stmt_list ();
6600 /* Materializing renamed subprograms will only benefit the debugging
6601 information as they aren't referenced in the generated code. So
6602 skip them when they aren't needed. Avoid doing this if:
6604 - there is a freeze node: in this case the renamed entity is not
6606 - the renamed subprogram is intrinsic: it will not be available in
6607 the debugging information (note that both or only one of the
6608 renaming and the renamed subprograms can be intrinsic). */
6609 if (!type_annotate_only
6610 && Needs_Debug_Info (gnat_renaming
)
6611 && No (Freeze_Node (gnat_renaming
))
6612 && Present (gnat_renamed
)
6613 && (Ekind (gnat_renamed
) == E_Function
6614 || Ekind (gnat_renamed
) == E_Procedure
)
6615 && !Is_Intrinsic_Subprogram (gnat_renaming
)
6616 && !Is_Intrinsic_Subprogram (gnat_renamed
))
6617 gnat_to_gnu_entity (gnat_renaming
, gnat_to_gnu (gnat_renamed
), true);
6621 case N_Implicit_Label_Declaration
:
6622 gnat_to_gnu_entity (Defining_Entity (gnat_node
), NULL_TREE
, true);
6623 gnu_result
= alloc_stmt_list ();
6626 case N_Package_Renaming_Declaration
:
6627 /* These are fully handled in the front end. */
6628 /* ??? For package renamings, find a way to use GENERIC namespaces so
6629 that we get proper debug information for them. */
6630 gnu_result
= alloc_stmt_list ();
6633 /*************************************/
6634 /* Chapter 4: Names and Expressions */
6635 /*************************************/
6637 case N_Explicit_Dereference
:
6638 /* Make sure the designated type is complete before dereferencing. */
6639 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6640 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
6641 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result
);
6643 /* If atomic access is required on the RHS, build the atomic load. */
6644 if (simple_atomic_access_required_p (gnat_node
, &aa_sync
)
6645 && !present_in_lhs_or_actual_p (gnat_node
))
6646 gnu_result
= build_atomic_load (gnu_result
, aa_sync
);
6648 /* If storage model access is required on the RHS, build the load. */
6649 else if (storage_model_access_required_p (gnat_node
, &gnat_smo
)
6650 && Present (Storage_Model_Copy_From (gnat_smo
))
6651 && !present_in_lhs_or_actual_p (gnat_node
))
6652 gnu_result
= build_storage_model_load (gnat_smo
, gnu_result
);
6655 case N_Indexed_Component
:
6657 const Entity_Id gnat_array_object
= Prefix (gnat_node
);
6658 tree gnu_array_object
= gnat_to_gnu (gnat_array_object
);
6661 Node_Id
*gnat_expr_array
;
6663 /* Get the storage model of the array. */
6664 gnat_smo
= get_storage_model (gnat_array_object
);
6666 gnu_array_object
= maybe_padded_object (gnu_array_object
);
6667 gnu_array_object
= maybe_unconstrained_array (gnu_array_object
);
6669 /* Convert vector inputs to their representative array type, to fit
6670 what the code below expects. */
6671 if (VECTOR_TYPE_P (TREE_TYPE (gnu_array_object
)))
6673 if (present_in_lhs_or_actual_p (gnat_node
))
6674 gnat_mark_addressable (gnu_array_object
);
6675 gnu_array_object
= maybe_vector_array (gnu_array_object
);
6678 /* The failure of this assertion will very likely come from a missing
6679 expansion for a packed array access. */
6680 gcc_assert (TREE_CODE (TREE_TYPE (gnu_array_object
)) == ARRAY_TYPE
);
6682 /* First compute the number of dimensions of the array, then
6683 fill the expression array, the order depending on whether
6684 this is a Convention_Fortran array or not. */
6685 for (ndim
= 1, gnu_type
= TREE_TYPE (gnu_array_object
);
6686 TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
6687 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
));
6688 ndim
++, gnu_type
= TREE_TYPE (gnu_type
))
6691 gnat_expr_array
= XALLOCAVEC (Node_Id
, ndim
);
6693 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object
)))
6694 for (i
= ndim
- 1, gnat_temp
= First (Expressions (gnat_node
));
6696 i
--, gnat_temp
= Next (gnat_temp
))
6697 gnat_expr_array
[i
] = gnat_temp
;
6699 for (i
= 0, gnat_temp
= First (Expressions (gnat_node
));
6701 i
++, gnat_temp
= Next (gnat_temp
))
6702 gnat_expr_array
[i
] = gnat_temp
;
6704 /* Start with the prefix and build the successive references. */
6705 gnu_result
= gnu_array_object
;
6707 for (i
= 0, gnu_type
= TREE_TYPE (gnu_array_object
);
6709 i
++, gnu_type
= TREE_TYPE (gnu_type
))
6711 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
6712 gnat_temp
= gnat_expr_array
[i
];
6713 gnu_expr
= maybe_character_value (gnat_to_gnu (gnat_temp
));
6716 = build_binary_op (ARRAY_REF
, NULL_TREE
, gnu_result
, gnu_expr
);
6718 if (Present (gnat_smo
)
6719 && Present (Storage_Model_Copy_From (gnat_smo
)))
6720 instantiate_load_in_array_ref (gnu_result
, gnat_smo
);
6723 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6725 /* If atomic access is required on the RHS, build the atomic load. */
6726 if (simple_atomic_access_required_p (gnat_node
, &aa_sync
)
6727 && !present_in_lhs_or_actual_p (gnat_node
))
6728 gnu_result
= build_atomic_load (gnu_result
, aa_sync
);
6730 /* If storage model access is required on the RHS, build the load. */
6731 else if (storage_model_access_required_p (gnat_node
, &gnat_smo
)
6732 && Present (Storage_Model_Copy_From (gnat_smo
))
6733 && !present_in_lhs_or_actual_p (gnat_node
))
6734 gnu_result
= build_storage_model_load (gnat_smo
, gnu_result
);
6740 const Entity_Id gnat_array_object
= Prefix (gnat_node
);
6741 tree gnu_array_object
= gnat_to_gnu (gnat_array_object
);
6743 /* Get the storage model of the array. */
6744 gnat_smo
= get_storage_model (gnat_array_object
);
6746 gnu_array_object
= maybe_padded_object (gnu_array_object
);
6747 gnu_array_object
= maybe_unconstrained_array (gnu_array_object
);
6749 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6751 gnu_expr
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type
));
6752 gnu_expr
= maybe_character_value (gnu_expr
);
6754 /* If this is a slice with non-constant size of an array with constant
6755 size, set the maximum size for the allocation of temporaries. */
6756 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type
))
6757 && TREE_CONSTANT (TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object
))))
6758 TYPE_ARRAY_MAX_SIZE (gnu_result_type
)
6759 = TYPE_SIZE_UNIT (TREE_TYPE (gnu_array_object
));
6761 gnu_result
= build_binary_op (ARRAY_RANGE_REF
, gnu_result_type
,
6762 gnu_array_object
, gnu_expr
);
6764 if (Present (gnat_smo
)
6765 && Present (Storage_Model_Copy_From (gnat_smo
)))
6766 instantiate_load_in_array_ref (gnu_result
, gnat_smo
);
6768 /* If storage model access is required on the RHS, build the load. */
6769 if (storage_model_access_required_p (gnat_node
, &gnat_smo
)
6770 && Present (Storage_Model_Copy_From (gnat_smo
))
6771 && !present_in_lhs_or_actual_p (gnat_node
))
6772 gnu_result
= build_storage_model_load (gnat_smo
, gnu_result
);
6776 case N_Selected_Component
:
6778 const Entity_Id gnat_prefix
= Prefix (gnat_node
);
6779 Entity_Id gnat_field
= Entity (Selector_Name (gnat_node
));
6780 tree gnu_prefix
= gnat_to_gnu (gnat_prefix
);
6782 gnu_prefix
= maybe_padded_object (gnu_prefix
);
6784 /* gnat_to_gnu_entity does not save the GNU tree made for renamed
6785 discriminants so avoid making recursive calls on each reference
6786 to them by following the appropriate link directly here. */
6787 if (Ekind (gnat_field
) == E_Discriminant
)
6789 /* For discriminant references in tagged types always substitute
6790 the corresponding discriminant as the actual component. */
6791 if (Is_Tagged_Type (Underlying_Type (Etype (gnat_prefix
))))
6792 while (Present (Corresponding_Discriminant (gnat_field
)))
6793 gnat_field
= Corresponding_Discriminant (gnat_field
);
6795 /* For discriminant references in untagged types always substitute
6796 the corresponding stored discriminant. */
6797 else if (Present (Corresponding_Discriminant (gnat_field
)))
6798 gnat_field
= Original_Record_Component (gnat_field
);
6801 /* Handle extracting the real or imaginary part of a complex.
6802 The real part is the first field and the imaginary the last. */
6803 if (TREE_CODE (TREE_TYPE (gnu_prefix
)) == COMPLEX_TYPE
)
6804 gnu_result
= build_unary_op (Present (Next_Entity (gnat_field
))
6805 ? REALPART_EXPR
: IMAGPART_EXPR
,
6806 NULL_TREE
, gnu_prefix
);
6809 tree gnu_field
= gnat_to_gnu_field_decl (gnat_field
);
6811 struct loop_info_d
*loop
;
6814 = build_component_ref (gnu_prefix
, gnu_field
,
6815 (Nkind (Parent (gnat_node
))
6816 == N_Attribute_Reference
)
6817 && lvalue_required_for_attribute_p
6818 (Parent (gnat_node
)));
6820 /* If optimization is enabled and we are inside a loop, we try to
6821 hoist nonconstant but invariant offset computations outside of
6822 the loop, since they very likely contain loads that could turn
6823 out to be hard to move if they end up in active EH regions. */
6826 && TREE_CODE (gnu_result
) == COMPONENT_REF
6827 && (gnu_offset
= component_ref_field_offset (gnu_result
))
6828 && !TREE_CONSTANT (gnu_offset
)
6829 && (gnu_offset
= gnat_invariant_expr (gnu_offset
))
6830 && (loop
= find_loop ()))
6833 = build1 (SAVE_EXPR
, TREE_TYPE (gnu_offset
), gnu_offset
);
6834 vec_safe_push (loop
->invariants
, invariant
);
6835 tree field
= TREE_OPERAND (gnu_result
, 1);
6837 = size_int (DECL_OFFSET_ALIGN (field
) / BITS_PER_UNIT
);
6838 /* Divide the offset by its alignment. */
6839 TREE_OPERAND (gnu_result
, 2)
6840 = size_binop (EXACT_DIV_EXPR
, invariant
, factor
);
6844 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6846 /* If atomic access is required on the RHS, build the atomic load. */
6847 if (simple_atomic_access_required_p (gnat_node
, &aa_sync
)
6848 && !present_in_lhs_or_actual_p (gnat_node
))
6849 gnu_result
= build_atomic_load (gnu_result
, aa_sync
);
6851 /* If storage model access is required on the RHS, build the load. */
6852 else if (storage_model_access_required_p (gnat_node
, &gnat_smo
)
6853 && Present (Storage_Model_Copy_From (gnat_smo
))
6854 && !present_in_lhs_or_actual_p (gnat_node
))
6855 gnu_result
= build_storage_model_load (gnat_smo
, gnu_result
);
6859 case N_Attribute_Reference
:
6861 /* The attribute designator. */
6862 const Attribute_Id attr
= Get_Attribute_Id (Attribute_Name (gnat_node
));
6864 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
6865 is a unit, not an object with a GCC equivalent. */
6866 if (attr
== Attr_Elab_Spec
|| attr
== Attr_Elab_Body
)
6868 create_subprog_decl (create_concat_name
6869 (Entity (Prefix (gnat_node
)),
6870 attr
== Attr_Elab_Body
? "elabb" : "elabs"),
6871 NULL_TREE
, void_ftype
, NULL_TREE
, is_default
,
6872 true, true, true, true, false, NULL
,
6875 gnu_result
= Attribute_to_gnu (gnat_node
, &gnu_result_type
, attr
);
6880 /* Like 'Access as far as we are concerned. */
6881 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
6882 gnu_result
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_result
);
6883 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6887 case N_Extension_Aggregate
:
6891 /* Check that this aggregate has not slipped through the cracks. */
6892 gcc_assert (!Expansion_Delayed (gnat_node
));
6894 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6896 if (TREE_CODE (gnu_result_type
) == RECORD_TYPE
6897 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type
))
6899 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type
)));
6900 else if (VECTOR_TYPE_P (gnu_result_type
))
6901 gnu_aggr_type
= TYPE_REPRESENTATIVE_ARRAY (gnu_result_type
);
6903 gnu_aggr_type
= gnu_result_type
;
6905 if (Null_Record_Present (gnat_node
))
6906 gnu_result
= gnat_build_constructor (gnu_aggr_type
, NULL
);
6908 else if (TREE_CODE (gnu_aggr_type
) == RECORD_TYPE
6909 || TREE_CODE (gnu_aggr_type
) == UNION_TYPE
)
6911 = assoc_to_constructor (Etype (gnat_node
),
6912 First (Component_Associations (gnat_node
)),
6914 else if (TREE_CODE (gnu_aggr_type
) == ARRAY_TYPE
)
6915 gnu_result
= pos_to_constructor (First (Expressions (gnat_node
)),
6917 else if (TREE_CODE (gnu_aggr_type
) == COMPLEX_TYPE
)
6920 (COMPLEX_EXPR
, gnu_aggr_type
,
6921 gnat_to_gnu (Expression (First
6922 (Component_Associations (gnat_node
)))),
6923 gnat_to_gnu (Expression
6925 (First (Component_Associations (gnat_node
))))));
6929 gnu_result
= convert (gnu_result_type
, gnu_result
);
6934 if (TARGET_VTABLE_USES_DESCRIPTORS
6935 && Ekind (Etype (gnat_node
)) == E_Access_Subprogram_Type
6936 && Is_Dispatch_Table_Entity (Etype (gnat_node
)))
6937 gnu_result
= null_fdesc_node
;
6939 gnu_result
= null_pointer_node
;
6940 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6943 case N_Type_Conversion
:
6944 case N_Qualified_Expression
:
6945 gnu_expr
= maybe_character_value (gnat_to_gnu (Expression (gnat_node
)));
6946 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6948 /* If this is a qualified expression for a tagged type, we mark the type
6949 as used. Because of polymorphism, this might be the only reference to
6950 the tagged type in the program while objects have it as dynamic type.
6951 The debugger needs to see it to display these objects properly. */
6952 if (kind
== N_Qualified_Expression
&& Is_Tagged_Type (Etype (gnat_node
)))
6953 used_types_insert (gnu_result_type
);
6955 gigi_checking_assert (!Do_Range_Check (Expression (gnat_node
)));
6958 = convert_with_check (Etype (gnat_node
), gnu_expr
,
6959 Do_Overflow_Check (gnat_node
),
6960 kind
== N_Type_Conversion
6961 && Float_Truncate (gnat_node
), gnat_node
);
6964 case N_Unchecked_Type_Conversion
:
6965 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6966 gnu_expr
= maybe_character_value (gnat_to_gnu (Expression (gnat_node
)));
6968 /* Skip further processing if the conversion is deemed a no-op. */
6969 if (unchecked_conversion_nop (gnat_node
))
6971 gnu_result
= gnu_expr
;
6972 gnu_result_type
= TREE_TYPE (gnu_result
);
6976 /* If the result is a pointer type, see if we are improperly
6977 converting to a stricter alignment. */
6978 if (STRICT_ALIGNMENT
&& POINTER_TYPE_P (gnu_result_type
)
6979 && Is_Access_Type (Etype (gnat_node
)))
6981 unsigned int align
= known_alignment (gnu_expr
);
6982 tree gnu_obj_type
= TREE_TYPE (gnu_result_type
);
6983 unsigned int oalign
= TYPE_ALIGN (gnu_obj_type
);
6985 if (align
!= 0 && align
< oalign
&& !TYPE_ALIGN_OK (gnu_obj_type
))
6986 post_error_ne_tree_2
6987 ("??source alignment (^) '< alignment of & (^)",
6988 gnat_node
, Designated_Type (Etype (gnat_node
)),
6989 size_int (align
/ BITS_PER_UNIT
), oalign
/ BITS_PER_UNIT
);
6992 /* If we are converting a descriptor to a function pointer, first
6993 build the pointer. */
6994 if (TARGET_VTABLE_USES_DESCRIPTORS
6995 && TREE_TYPE (gnu_expr
) == fdesc_type_node
6996 && POINTER_TYPE_P (gnu_result_type
))
6997 gnu_expr
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
6999 gnu_result
= unchecked_convert (gnu_result_type
, gnu_expr
,
7000 No_Truncation (gnat_node
));
7006 tree gnu_obj
= gnat_to_gnu (Left_Opnd (gnat_node
));
7007 tree gnu_low
, gnu_high
;
7009 Range_to_gnu (Right_Opnd (gnat_node
), &gnu_low
, &gnu_high
);
7010 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7012 tree gnu_op_type
= maybe_character_type (TREE_TYPE (gnu_obj
));
7013 if (TREE_TYPE (gnu_obj
) != gnu_op_type
)
7015 gnu_obj
= convert (gnu_op_type
, gnu_obj
);
7016 gnu_low
= convert (gnu_op_type
, gnu_low
);
7017 gnu_high
= convert (gnu_op_type
, gnu_high
);
7020 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
7021 ensure that GNU_OBJ is evaluated only once and perform a full range
7023 if (operand_equal_p (gnu_low
, gnu_high
, 0))
7025 = build_binary_op (EQ_EXPR
, gnu_result_type
, gnu_obj
, gnu_low
);
7029 gnu_obj
= gnat_protect_expr (gnu_obj
);
7030 t1
= build_binary_op (GE_EXPR
, gnu_result_type
, gnu_obj
, gnu_low
);
7032 set_expr_location_from_node (t1
, gnat_node
);
7033 t2
= build_binary_op (LE_EXPR
, gnu_result_type
, gnu_obj
, gnu_high
);
7035 set_expr_location_from_node (t2
, gnat_node
);
7037 = build_binary_op (TRUTH_ANDIF_EXPR
, gnu_result_type
, t1
, t2
);
7040 if (kind
== N_Not_In
)
7042 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result
), gnu_result
);
7047 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
7048 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
7049 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7050 gnu_result
= build_binary_op (FLOAT_TYPE_P (gnu_result_type
)
7052 : (Rounded_Result (gnat_node
)
7053 ? ROUND_DIV_EXPR
: TRUNC_DIV_EXPR
),
7054 gnu_result_type
, gnu_lhs
, gnu_rhs
);
7055 /* If the result type is larger than a word, then declare the dependence
7056 on the libgcc routine. */
7057 if (INTEGRAL_TYPE_P (gnu_result_type
)
7058 && TYPE_PRECISION (gnu_result_type
) > BITS_PER_WORD
)
7059 Check_Restriction_No_Dependence_On_System (Name_Gcc
, gnat_node
);
7073 case N_Op_Rotate_Left
:
7074 case N_Op_Rotate_Right
:
7075 case N_Op_Shift_Left
:
7076 case N_Op_Shift_Right
:
7077 case N_Op_Shift_Right_Arithmetic
:
7084 enum tree_code code
= gnu_codes
[kind
];
7085 bool ignore_lhs_overflow
= false;
7086 location_t saved_location
= input_location
;
7087 tree gnu_type
, gnu_max_shift
= NULL_TREE
;
7089 /* Fix operations set up for boolean types in GNU_CODES above. */
7090 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node
))))
7094 code
= BIT_AND_EXPR
;
7097 code
= BIT_IOR_EXPR
;
7100 code
= BIT_XOR_EXPR
;
7106 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
7107 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
7108 gnu_type
= gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7110 /* If this is a shift, take the count as unsigned since that is what
7111 most machines do and will generate simpler adjustments below. */
7112 if (IN (kind
, N_Op_Shift
))
7115 = gnat_unsigned_type_for (get_base_type (TREE_TYPE (gnu_rhs
)));
7116 gnu_rhs
= convert (gnu_count_type
, gnu_rhs
);
7118 = convert (TREE_TYPE (gnu_rhs
), TYPE_SIZE (gnu_type
));
7119 /* If the result type is larger than a word, then declare the
7120 dependence on the libgcc routine. */
7121 if (TYPE_PRECISION (gnu_type
) > BITS_PER_WORD
)
7122 Check_Restriction_No_Dependence_On_System (Name_Gcc
, gnat_node
);
7125 /* If this is a comparison between (potentially) large aggregates, then
7126 declare the dependence on the memcmp routine. */
7127 else if ((kind
== N_Op_Eq
|| kind
== N_Op_Ne
)
7128 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs
))
7129 && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs
)))
7130 || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs
)),
7131 2 * BITS_PER_WORD
) > 0))
7132 Check_Restriction_No_Dependence_On_System (Name_Memory_Compare
,
7135 /* If this is a modulo/remainder and the result type is larger than a
7136 word, then declare the dependence on the libgcc routine. */
7137 else if ((kind
== N_Op_Mod
||kind
== N_Op_Rem
)
7138 && TYPE_PRECISION (gnu_type
) > BITS_PER_WORD
)
7139 Check_Restriction_No_Dependence_On_System (Name_Gcc
, gnat_node
);
7141 /* Pending generic support for efficient vector logical operations in
7142 GCC, convert vectors to their representative array type view. */
7143 gnu_lhs
= maybe_vector_array (gnu_lhs
);
7144 gnu_rhs
= maybe_vector_array (gnu_rhs
);
7146 /* If this is a comparison operator, convert any references to an
7147 unconstrained array value into a reference to the actual array. */
7148 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
7150 gnu_lhs
= maybe_unconstrained_array (gnu_lhs
);
7151 gnu_rhs
= maybe_unconstrained_array (gnu_rhs
);
7153 tree gnu_op_type
= maybe_character_type (TREE_TYPE (gnu_lhs
));
7154 if (TREE_TYPE (gnu_lhs
) != gnu_op_type
)
7156 gnu_lhs
= convert (gnu_op_type
, gnu_lhs
);
7157 gnu_rhs
= convert (gnu_op_type
, gnu_rhs
);
7161 /* If this is a shift whose count is not guaranteed to be correct,
7162 we need to adjust the shift count. */
7163 if ((kind
== N_Op_Rotate_Left
|| kind
== N_Op_Rotate_Right
)
7164 && !Shift_Count_OK (gnat_node
))
7165 gnu_rhs
= build_binary_op (TRUNC_MOD_EXPR
, TREE_TYPE (gnu_rhs
),
7166 gnu_rhs
, gnu_max_shift
);
7167 else if (kind
== N_Op_Shift_Right_Arithmetic
7168 && !Shift_Count_OK (gnat_node
))
7170 = build_binary_op (MIN_EXPR
, TREE_TYPE (gnu_rhs
),
7171 build_binary_op (MINUS_EXPR
,
7172 TREE_TYPE (gnu_rhs
),
7175 (TREE_TYPE (gnu_rhs
), 1)),
7178 /* For right shifts, the type says what kind of shift to do,
7179 so we may need to choose a different type. In this case,
7180 we have to ignore integer overflow lest it propagates all
7181 the way down and causes a CE to be explicitly raised. */
7182 if (kind
== N_Op_Shift_Right
&& !TYPE_UNSIGNED (gnu_type
))
7184 gnu_type
= gnat_unsigned_type_for (gnu_type
);
7185 ignore_lhs_overflow
= true;
7187 else if (kind
== N_Op_Shift_Right_Arithmetic
7188 && TYPE_UNSIGNED (gnu_type
))
7190 gnu_type
= gnat_signed_type_for (gnu_type
);
7191 ignore_lhs_overflow
= true;
7194 if (gnu_type
!= gnu_result_type
)
7196 tree gnu_old_lhs
= gnu_lhs
;
7197 gnu_lhs
= convert (gnu_type
, gnu_lhs
);
7198 if (TREE_CODE (gnu_lhs
) == INTEGER_CST
&& ignore_lhs_overflow
)
7199 TREE_OVERFLOW (gnu_lhs
) = TREE_OVERFLOW (gnu_old_lhs
);
7200 gnu_rhs
= convert (gnu_type
, gnu_rhs
);
7202 gnu_max_shift
= convert (gnu_type
, gnu_max_shift
);
7205 /* For signed integer addition, subtraction and multiplication, do an
7206 overflow check if required. */
7207 if (Do_Overflow_Check (gnat_node
)
7208 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
|| code
== MULT_EXPR
)
7209 && !TYPE_UNSIGNED (gnu_type
)
7210 && !FLOAT_TYPE_P (gnu_type
))
7212 = build_binary_op_trapv (code
, gnu_type
, gnu_lhs
, gnu_rhs
,
7216 /* Some operations, e.g. comparisons of arrays, generate complex
7217 trees that need to be annotated while they are being built. */
7218 input_location
= saved_location
;
7219 gnu_result
= build_binary_op (code
, gnu_type
, gnu_lhs
, gnu_rhs
);
7222 /* If this is a logical shift with the shift count not verified,
7223 we must return zero if it is too large. We cannot compensate
7224 beforehand in this case. */
7225 if ((kind
== N_Op_Shift_Left
|| kind
== N_Op_Shift_Right
)
7226 && !Shift_Count_OK (gnat_node
))
7228 = build_cond_expr (gnu_type
,
7229 build_binary_op (GE_EXPR
, boolean_type_node
,
7230 gnu_rhs
, gnu_max_shift
),
7231 build_int_cst (gnu_type
, 0),
7236 case N_If_Expression
:
7238 tree gnu_cond
= gnat_to_gnu (First (Expressions (gnat_node
)));
7239 tree gnu_true
= gnat_to_gnu (Next (First (Expressions (gnat_node
))));
7241 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node
)))));
7243 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7245 = build_cond_expr (gnu_result_type
, gnu_cond
, gnu_true
, gnu_false
);
7250 gnu_result
= gnat_to_gnu (Right_Opnd (gnat_node
));
7251 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7255 /* This case can apply to a boolean or a modular type.
7256 Fall through for a boolean operand since GNU_CODES is set
7257 up to handle this. */
7258 if (Is_Modular_Integer_Type (Underlying_Type (Etype (gnat_node
))))
7260 gnu_expr
= gnat_to_gnu (Right_Opnd (gnat_node
));
7261 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7262 gnu_result
= build_unary_op (BIT_NOT_EXPR
, gnu_result_type
,
7267 /* ... fall through ... */
7271 gnu_expr
= gnat_to_gnu (Right_Opnd (gnat_node
));
7272 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7274 /* For signed integer negation and absolute value, do an overflow check
7276 if (Do_Overflow_Check (gnat_node
)
7277 && !TYPE_UNSIGNED (gnu_result_type
)
7278 && !FLOAT_TYPE_P (gnu_result_type
))
7280 = build_unary_op_trapv (gnu_codes
[kind
], gnu_result_type
, gnu_expr
,
7284 = build_unary_op (gnu_codes
[kind
], gnu_result_type
, gnu_expr
);
7289 tree gnu_type
, gnu_init
;
7290 bool ignore_init_type
;
7292 gnat_temp
= Expression (gnat_node
);
7294 /* The expression can be either an N_Identifier or an Expanded_Name,
7295 which must represent a type, or a N_Qualified_Expression, which
7296 contains both the type and an initial value for the object. */
7297 if (Nkind (gnat_temp
) == N_Identifier
7298 || Nkind (gnat_temp
) == N_Expanded_Name
)
7300 ignore_init_type
= false;
7301 gnu_init
= NULL_TREE
;
7302 gnu_type
= gnat_to_gnu_type (Entity (gnat_temp
));
7305 else if (Nkind (gnat_temp
) == N_Qualified_Expression
)
7307 const Entity_Id gnat_desig_type
7308 = Designated_Type (Underlying_Type (Etype (gnat_node
)));
7310 ignore_init_type
= Has_Constrained_Partial_View (gnat_desig_type
);
7312 gnu_init
= gnat_to_gnu (Expression (gnat_temp
));
7313 gnu_init
= maybe_unconstrained_array (gnu_init
);
7315 gigi_checking_assert (!Do_Range_Check (Expression (gnat_temp
)));
7317 if (Is_Elementary_Type (gnat_desig_type
)
7318 || Is_Constrained (gnat_desig_type
))
7319 gnu_type
= gnat_to_gnu_type (gnat_desig_type
);
7322 gnu_type
= gnat_to_gnu_type (Etype (Expression (gnat_temp
)));
7323 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
7324 gnu_type
= TREE_TYPE (gnu_init
);
7327 /* See the N_Qualified_Expression case for the rationale. */
7328 if (Is_Tagged_Type (gnat_desig_type
))
7329 used_types_insert (gnu_type
);
7331 gnu_init
= convert (gnu_type
, gnu_init
);
7336 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
7337 return build_allocator (gnu_type
, gnu_init
, gnu_result_type
,
7338 Procedure_To_Call (gnat_node
),
7339 Storage_Pool (gnat_node
), gnat_node
,
7344 /**************************/
7345 /* Chapter 5: Statements */
7346 /**************************/
7349 gnu_result
= build1 (LABEL_EXPR
, void_type_node
,
7350 gnat_to_gnu (Identifier (gnat_node
)));
7353 case N_Null_Statement
:
7354 /* When not optimizing, turn null statements from source into gotos to
7355 the next statement that the middle-end knows how to preserve. */
7356 if (!optimize
&& Comes_From_Source (gnat_node
))
7358 tree stmt
, label
= create_label_decl (NULL_TREE
, gnat_node
);
7359 DECL_IGNORED_P (label
) = 1;
7360 start_stmt_group ();
7361 stmt
= build1 (GOTO_EXPR
, void_type_node
, label
);
7362 set_expr_location_from_node (stmt
, gnat_node
);
7364 stmt
= build1 (LABEL_EXPR
, void_type_node
, label
);
7365 set_expr_location_from_node (stmt
, gnat_node
);
7367 gnu_result
= end_stmt_group ();
7370 gnu_result
= alloc_stmt_list ();
7373 case N_Assignment_Statement
:
7374 /* First get the LHS of the statement and convert any reference to an
7375 unconstrained array into a reference to the underlying array. */
7376 gnu_lhs
= maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node
)));
7378 /* If the type has a size that overflows, convert this into raise of
7379 Storage_Error: execution shouldn't have gotten here anyway. */
7380 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs
))) == INTEGER_CST
7381 && !valid_constant_size_p (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs
))))
7382 gnu_result
= build_call_raise (SE_Object_Too_Large
, gnat_node
,
7383 N_Raise_Storage_Error
);
7385 /* If the RHS is a function call, let Call_to_gnu do the entire work. */
7386 else if (Nkind (Expression (gnat_node
)) == N_Function_Call
)
7388 get_atomic_access (Name (gnat_node
), &aa_type
, &aa_sync
);
7389 get_storage_model_access (Name (gnat_node
), &gnat_smo
);
7391 = Call_to_gnu (Expression (gnat_node
), &gnu_result_type
, gnu_lhs
,
7392 aa_type
, aa_sync
, gnat_smo
);
7395 /* Otherwise we need to build the assignment statement manually. */
7398 const Node_Id gnat_name
= Name (gnat_node
);
7399 const Node_Id gnat_expr
= Expression (gnat_node
);
7400 const Node_Id gnat_inner
7401 = Nkind (gnat_expr
) == N_Qualified_Expression
7402 ? Expression (gnat_expr
)
7404 const Entity_Id gnat_type
= Underlying_Type (Etype (gnat_name
));
7405 const bool use_memset_p
7406 = Is_Array_Type (gnat_type
)
7407 && Nkind (gnat_inner
) == N_Aggregate
7408 && Is_Single_Aggregate (gnat_inner
);
7410 /* If we use memset, we need to find the innermost expression. */
7413 gnat_temp
= gnat_inner
;
7416 = Expression (First (Component_Associations (gnat_temp
)));
7417 } while (Nkind (gnat_temp
) == N_Aggregate
7418 && Is_Single_Aggregate (gnat_temp
));
7419 gnu_rhs
= gnat_to_gnu (gnat_temp
);
7422 /* Otherwise get the RHS of the statement and do the same processing
7423 as for the LHS above. */
7425 gnu_rhs
= maybe_unconstrained_array (gnat_to_gnu (gnat_expr
));
7427 gigi_checking_assert (!Do_Range_Check (gnat_expr
));
7429 get_atomic_access (gnat_name
, &aa_type
, &aa_sync
);
7430 get_storage_model_access (gnat_name
, &gnat_smo
);
7432 /* If an outer atomic access is required on the LHS, build the load-
7433 modify-store sequence. */
7434 if (aa_type
== OUTER_ATOMIC
)
7435 gnu_result
= build_load_modify_store (gnu_lhs
, gnu_rhs
, gnat_node
);
7437 /* Or else, if a simple atomic access is required, build the atomic
7439 else if (aa_type
== SIMPLE_ATOMIC
)
7440 gnu_result
= build_atomic_store (gnu_lhs
, gnu_rhs
, aa_sync
);
7442 /* Or else, if a storage model access is required, build the special
7444 else if (Present (gnat_smo
)
7445 && Present (Storage_Model_Copy_To (gnat_smo
)))
7449 /* We obviously cannot use memset in this case. */
7450 gcc_assert (!use_memset_p
);
7452 /* If this is a dereference with a special dynamic constrained
7453 subtype on the node, use it to compute the size. */
7454 if (Nkind (gnat_name
) == N_Explicit_Dereference
7455 && Present (Actual_Designated_Subtype (gnat_name
)))
7457 tree gnu_actual_obj_type
7458 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_name
));
7459 gnu_size
= TYPE_SIZE_UNIT (gnu_actual_obj_type
);
7462 gnu_size
= NULL_TREE
;
7465 = build_storage_model_store (gnat_smo
, gnu_lhs
, gnu_rhs
,
7469 /* Or else, use memset when the conditions are met. This has already
7470 been validated by Aggr_Assignment_OK_For_Backend in the front-end
7471 and the RHS is thus guaranteed to be of the appropriate form. */
7472 else if (use_memset_p
)
7475 = real_zerop (gnu_rhs
)
7477 : fold_convert (integer_type_node
, gnu_rhs
);
7478 tree dest
= build_fold_addr_expr (gnu_lhs
);
7479 tree t
= builtin_decl_explicit (BUILT_IN_MEMSET
);
7480 /* Be extra careful not to write too much data. */
7482 if (TREE_CODE (gnu_lhs
) == COMPONENT_REF
)
7483 size
= DECL_SIZE_UNIT (TREE_OPERAND (gnu_lhs
, 1));
7484 else if (DECL_P (gnu_lhs
))
7485 size
= DECL_SIZE_UNIT (gnu_lhs
);
7487 size
= TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs
));
7488 size
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, gnu_lhs
);
7489 if (TREE_CODE (value
) == INTEGER_CST
&& !integer_zerop (value
))
7492 = build_int_cst (integer_type_node
,
7493 ((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
7494 value
= int_const_binop (BIT_AND_EXPR
, value
, mask
);
7496 gnu_result
= build_call_expr (t
, 3, dest
, value
, size
);
7497 Check_Restriction_No_Dependence_On_System (Name_Memory_Set
,
7503 tree t
= remove_conversions (gnu_rhs
, false);
7505 /* If a storage model load is present on the RHS, then elide the
7506 temporary associated with it. */
7507 if (TREE_CODE (t
) == LOAD_EXPR
)
7509 gnu_result
= TREE_OPERAND (t
, 1);
7510 gcc_assert (TREE_CODE (gnu_result
) == CALL_EXPR
);
7512 tree arg
= CALL_EXPR_ARG (gnu_result
, 1);
7513 CALL_EXPR_ARG (gnu_result
, 1)
7514 = build_unary_op (ADDR_EXPR
, TREE_TYPE (arg
), gnu_lhs
);
7517 /* Otherwise build a regular assignment. */
7520 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_lhs
, gnu_rhs
);
7523 /* If the assignment type is a regular array and the two sides are
7524 not completely disjoint, play safe and use memmove. But don't do
7525 it for a bit-packed array as it might not be byte-aligned. */
7526 if (TREE_CODE (gnu_result
) == MODIFY_EXPR
7527 && Is_Array_Type (gnat_type
)
7528 && !Is_Bit_Packed_Array (gnat_type
)
7529 && !(Forwards_OK (gnat_node
) && Backwards_OK (gnat_node
)))
7531 tree to
= TREE_OPERAND (gnu_result
, 0);
7532 tree from
= TREE_OPERAND (gnu_result
, 1);
7533 tree type
= TREE_TYPE (from
);
7535 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (type
), from
);
7536 tree to_ptr
= build_fold_addr_expr (to
);
7537 tree from_ptr
= build_fold_addr_expr (from
);
7538 tree t
= builtin_decl_explicit (BUILT_IN_MEMMOVE
);
7539 gnu_result
= build_call_expr (t
, 3, to_ptr
, from_ptr
, size
);
7540 Check_Restriction_No_Dependence_On_System (Name_Memory_Move
,
7544 /* If this is an assignment between (potentially) large aggregates,
7545 then declare the dependence on the memcpy routine. */
7546 else if (AGGREGATE_TYPE_P (TREE_TYPE (gnu_lhs
))
7547 && (!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_lhs
)))
7548 || compare_tree_int (TYPE_SIZE (TREE_TYPE (gnu_lhs
)),
7549 2 * BITS_PER_WORD
) > 0))
7550 Check_Restriction_No_Dependence_On_System (Name_Memory_Copy
,
7555 case N_If_Statement
:
7557 tree
*gnu_else_ptr
; /* Point to put next "else if" or "else". */
7559 /* Make the outer COND_EXPR. Avoid non-determinism. */
7560 gnu_result
= build3 (COND_EXPR
, void_type_node
,
7561 gnat_to_gnu (Condition (gnat_node
)),
7562 NULL_TREE
, NULL_TREE
);
7563 COND_EXPR_THEN (gnu_result
)
7564 = build_stmt_group (Then_Statements (gnat_node
), false);
7565 TREE_SIDE_EFFECTS (gnu_result
) = 1;
7566 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_result
);
7568 /* Now make a COND_EXPR for each of the "else if" parts. Put each
7569 into the previous "else" part and point to where to put any
7570 outer "else". Also avoid non-determinism. */
7571 if (Present (Elsif_Parts (gnat_node
)))
7572 for (gnat_temp
= First (Elsif_Parts (gnat_node
));
7573 Present (gnat_temp
); gnat_temp
= Next (gnat_temp
))
7575 gnu_expr
= build3 (COND_EXPR
, void_type_node
,
7576 gnat_to_gnu (Condition (gnat_temp
)),
7577 NULL_TREE
, NULL_TREE
);
7578 COND_EXPR_THEN (gnu_expr
)
7579 = build_stmt_group (Then_Statements (gnat_temp
), false);
7580 TREE_SIDE_EFFECTS (gnu_expr
) = 1;
7581 set_expr_location_from_node (gnu_expr
, gnat_temp
);
7582 *gnu_else_ptr
= gnu_expr
;
7583 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_expr
);
7586 *gnu_else_ptr
= build_stmt_group (Else_Statements (gnat_node
), false);
7590 case N_Case_Statement
:
7591 gnu_result
= Case_Statement_to_gnu (gnat_node
);
7594 case N_Loop_Statement
:
7595 gnu_result
= Loop_Statement_to_gnu (gnat_node
);
7598 case N_Block_Statement
:
7599 /* The only way to enter the block is to fall through to it. */
7600 if (stmt_group_may_fallthru ())
7602 start_stmt_group ();
7604 process_decls (Declarations (gnat_node
), Empty
, true, true);
7605 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
7606 if (Present (At_End_Proc (gnat_node
)))
7607 At_End_Proc_to_gnu (gnat_node
);
7609 gnu_result
= end_stmt_group ();
7612 gnu_result
= alloc_stmt_list ();
7615 case N_Exit_Statement
:
7617 = build2 (EXIT_STMT
, void_type_node
,
7618 (Present (Condition (gnat_node
))
7619 ? gnat_to_gnu (Condition (gnat_node
)) : NULL_TREE
),
7620 (Present (Name (gnat_node
))
7621 ? get_gnu_tree (Entity (Name (gnat_node
)))
7622 : LOOP_STMT_LABEL (gnu_loop_stack
->last ()->stmt
)));
7625 case N_Simple_Return_Statement
:
7627 tree gnu_ret_obj
, gnu_ret_val
;
7629 /* If the subprogram is a function, we must return the expression. */
7630 if (Present (Expression (gnat_node
)))
7632 tree gnu_subprog_type
= TREE_TYPE (current_function_decl
);
7634 /* If this function has copy-in/copy-out parameters parameters and
7635 doesn't return by invisible reference, get the real object for
7636 the return. See Subprogram_Body_to_gnu. */
7637 if (TYPE_CI_CO_LIST (gnu_subprog_type
)
7638 && !TREE_ADDRESSABLE (gnu_subprog_type
))
7639 gnu_ret_obj
= gnu_return_var_stack
->last ();
7641 gnu_ret_obj
= DECL_RESULT (current_function_decl
);
7643 /* Get the GCC tree for the expression to be returned. */
7644 gnu_ret_val
= gnat_to_gnu (Expression (gnat_node
));
7646 /* Do not remove the padding from GNU_RET_VAL if the inner type is
7647 self-referential since we want to allocate the fixed size. */
7648 if (TREE_CODE (gnu_ret_val
) == COMPONENT_REF
7649 && type_is_padding_self_referential
7650 (TREE_TYPE (TREE_OPERAND (gnu_ret_val
, 0))))
7651 gnu_ret_val
= TREE_OPERAND (gnu_ret_val
, 0);
7653 /* If the function returns by direct reference, return a pointer
7654 to the return value, possibly after allocating it. */
7655 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type
))
7657 if (Present (Storage_Pool (gnat_node
)))
7659 gnu_ret_val
= maybe_unconstrained_array (gnu_ret_val
);
7661 /* And find out whether it is a candidate for Named Return
7662 Value. If so, record it. */
7665 && !TYPE_CI_CO_LIST (gnu_subprog_type
))
7667 tree ret_val
= gnu_ret_val
;
7669 /* Strip conversions around the return value. */
7670 if (gnat_useless_type_conversion (ret_val
))
7671 ret_val
= TREE_OPERAND (ret_val
, 0);
7673 /* Strip unpadding around the return value. */
7674 if (TREE_CODE (ret_val
) == COMPONENT_REF
7675 && TYPE_IS_PADDING_P
7676 (TREE_TYPE (TREE_OPERAND (ret_val
, 0))))
7677 ret_val
= TREE_OPERAND (ret_val
, 0);
7679 /* Now apply the test to the return value. */
7680 if (return_value_ok_for_nrv_p (NULL_TREE
, ret_val
))
7682 if (!f_named_ret_val
)
7683 f_named_ret_val
= BITMAP_GGC_ALLOC ();
7684 bitmap_set_bit (f_named_ret_val
,
7685 DECL_UID (ret_val
));
7687 f_gnat_ret
= gnat_node
;
7692 = build_allocator (TREE_TYPE (gnu_ret_val
),
7694 TREE_TYPE (gnu_ret_obj
),
7695 Procedure_To_Call (gnat_node
),
7696 Storage_Pool (gnat_node
),
7703 = build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_ret_val
);
7706 /* Otherwise, if it returns by invisible reference, dereference
7707 the pointer it is passed using the type of the return value
7708 and build the copy operation manually. This ensures that we
7709 don't copy too much data, for example if the return type is
7710 unconstrained with a maximum size. */
7711 else if (TREE_ADDRESSABLE (gnu_subprog_type
))
7714 = build_unary_op (INDIRECT_REF
, TREE_TYPE (gnu_ret_val
),
7716 gnu_result
= build2 (INIT_EXPR
, void_type_node
,
7717 gnu_ret_deref
, gnu_ret_val
);
7718 /* Avoid a useless copy with __builtin_return_slot. */
7719 if (INDIRECT_REF_P (gnu_ret_val
))
7721 = build3 (COND_EXPR
, void_type_node
,
7722 fold_build2 (NE_EXPR
, boolean_type_node
,
7723 TREE_OPERAND (gnu_ret_val
, 0),
7725 gnu_result
, NULL_TREE
);
7726 add_stmt_with_node (gnu_result
, gnat_node
);
7727 gnu_ret_val
= NULL_TREE
;
7732 gnu_ret_obj
= gnu_ret_val
= NULL_TREE
;
7734 /* If we have a return label defined, convert this into a branch to
7735 that label. The return proper will be handled elsewhere. */
7736 if (gnu_return_label_stack
->last ())
7739 add_stmt_with_node (build_binary_op (MODIFY_EXPR
,
7740 NULL_TREE
, gnu_ret_obj
,
7744 gnu_result
= build1 (GOTO_EXPR
, void_type_node
,
7745 gnu_return_label_stack
->last ());
7747 /* When not optimizing, make sure the return is preserved. */
7748 if (!optimize
&& Comes_From_Source (gnat_node
))
7749 DECL_ARTIFICIAL (gnu_return_label_stack
->last ()) = 0;
7752 /* Otherwise, build a regular return. */
7754 gnu_result
= build_return_expr (gnu_ret_obj
, gnu_ret_val
);
7758 case N_Goto_Statement
:
7759 gnu_expr
= gnat_to_gnu (Name (gnat_node
));
7760 gnu_result
= build1 (GOTO_EXPR
, void_type_node
, gnu_expr
);
7761 TREE_USED (gnu_expr
) = 1;
7764 /***************************/
7765 /* Chapter 6: Subprograms */
7766 /***************************/
7768 case N_Subprogram_Declaration
:
7769 /* Unless there is a freeze node, declare the entity. We consider
7770 this a definition even though we're not generating code for the
7771 subprogram because we will be making the corresponding GCC node.
7772 When there is a freeze node, it is considered the definition of
7773 the subprogram and we do nothing until after it is encountered.
7774 That's an efficiency issue: the types involved in the profile
7775 are far more likely to be frozen between the declaration and
7776 the freeze node than before the declaration, so we save some
7777 updates of the GCC node by waiting until the freeze node.
7778 The counterpart is that we assume that there is no reference
7779 to the subprogram between the declaration and the freeze node
7780 in the expanded code; otherwise, it will be interpreted as an
7781 external reference and very likely give rise to a link failure. */
7782 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node
)))))
7783 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node
)),
7785 gnu_result
= alloc_stmt_list ();
7788 case N_Abstract_Subprogram_Declaration
:
7789 /* This subprogram doesn't exist for code generation purposes, but we
7790 have to elaborate the types of any parameters and result, unless
7791 they are imported types (nothing to generate in this case).
7793 The parameter list may contain types with freeze nodes, e.g. not null
7794 subtypes, so the subprogram itself may carry a freeze node, in which
7795 case its elaboration must be deferred. */
7797 /* Process the parameter types first. */
7798 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node
)))))
7800 = First_Formal_With_Extras
7801 (Defining_Entity (Specification (gnat_node
)));
7802 Present (gnat_temp
);
7803 gnat_temp
= Next_Formal_With_Extras (gnat_temp
))
7804 if (Is_Itype (Etype (gnat_temp
))
7805 && !From_Limited_With (Etype (gnat_temp
)))
7806 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, false);
7808 /* Then the result type, set to Standard_Void_Type for procedures. */
7810 Entity_Id gnat_temp_type
7811 = Etype (Defining_Entity (Specification (gnat_node
)));
7813 if (Is_Itype (gnat_temp_type
) && !From_Limited_With (gnat_temp_type
))
7814 gnat_to_gnu_entity (Etype (gnat_temp_type
), NULL_TREE
, false);
7817 gnu_result
= alloc_stmt_list ();
7820 case N_Defining_Program_Unit_Name
:
7821 /* For a child unit identifier go up a level to get the specification.
7822 We get this when we try to find the spec of a child unit package
7823 that is the compilation unit being compiled. */
7824 gnu_result
= gnat_to_gnu (Parent (gnat_node
));
7827 case N_Subprogram_Body
:
7828 Subprogram_Body_to_gnu (gnat_node
);
7829 gnu_result
= alloc_stmt_list ();
7832 case N_Function_Call
:
7833 case N_Procedure_Call_Statement
:
7834 gnu_result
= Call_to_gnu (gnat_node
, &gnu_result_type
, NULL_TREE
,
7835 NOT_ATOMIC
, false, Empty
);
7838 /************************/
7839 /* Chapter 7: Packages */
7840 /************************/
7842 case N_Package_Declaration
:
7843 gnu_result
= gnat_to_gnu (Specification (gnat_node
));
7846 case N_Package_Specification
:
7847 start_stmt_group ();
7848 process_decls (Visible_Declarations (gnat_node
),
7849 Private_Declarations (gnat_node
),
7851 gnu_result
= end_stmt_group ();
7854 case N_Package_Body
:
7855 /* If this is the body of a generic package - do nothing. */
7856 if (Ekind (Corresponding_Spec (gnat_node
)) == E_Generic_Package
)
7858 gnu_result
= alloc_stmt_list ();
7862 start_stmt_group ();
7863 process_decls (Declarations (gnat_node
), Empty
, true, true);
7864 if (Present (Handled_Statement_Sequence (gnat_node
)))
7865 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
7866 if (Present (At_End_Proc (gnat_node
)))
7867 At_End_Proc_to_gnu (gnat_node
);
7868 gnu_result
= end_stmt_group ();
7871 /********************************/
7872 /* Chapter 8: Visibility Rules */
7873 /********************************/
7875 case N_Use_Package_Clause
:
7876 case N_Use_Type_Clause
:
7877 /* Nothing to do here - but these may appear in list of declarations. */
7878 gnu_result
= alloc_stmt_list ();
7881 /*********************/
7882 /* Chapter 9: Tasks */
7883 /*********************/
7885 case N_Protected_Type_Declaration
:
7886 gnu_result
= alloc_stmt_list ();
7889 case N_Single_Task_Declaration
:
7890 gnat_to_gnu_entity (Defining_Entity (gnat_node
), NULL_TREE
, true);
7891 gnu_result
= alloc_stmt_list ();
7894 /*********************************************************/
7895 /* Chapter 10: Program Structure and Compilation Issues */
7896 /*********************************************************/
7898 case N_Compilation_Unit
:
7899 /* This is not called for the main unit on which gigi is invoked. */
7900 Compilation_Unit_to_gnu (gnat_node
);
7901 gnu_result
= alloc_stmt_list ();
7905 gnu_result
= gnat_to_gnu (Proper_Body (gnat_node
));
7909 case N_Protected_Body
:
7911 /* These nodes should only be present when annotating types. */
7912 gcc_assert (type_annotate_only
);
7913 process_decls (Declarations (gnat_node
), Empty
, true, true);
7914 gnu_result
= alloc_stmt_list ();
7917 case N_Subprogram_Body_Stub
:
7918 case N_Package_Body_Stub
:
7919 case N_Protected_Body_Stub
:
7920 case N_Task_Body_Stub
:
7921 /* Simply process whatever unit is being inserted. */
7922 if (Present (Library_Unit (gnat_node
)))
7923 gnu_result
= gnat_to_gnu (Unit (Library_Unit (gnat_node
)));
7926 gcc_assert (type_annotate_only
);
7927 gnu_result
= alloc_stmt_list ();
7931 /***************************/
7932 /* Chapter 11: Exceptions */
7933 /***************************/
7935 case N_Handled_Sequence_Of_Statements
:
7936 gnu_result
= Handled_Sequence_Of_Statements_to_gnu (gnat_node
);
7939 case N_Exception_Handler
:
7940 gnu_result
= Exception_Handler_to_gnu (gnat_node
);
7943 case N_Raise_Statement
:
7944 /* Only for reraise in back-end exceptions mode. */
7945 gcc_assert (No (Name (gnat_node
)));
7947 start_stmt_group ();
7949 add_stmt_with_node (build_call_n_expr (reraise_zcx_decl
, 1,
7950 gnu_incoming_exc_ptr
),
7953 gnu_result
= end_stmt_group ();
7956 case N_Push_Constraint_Error_Label
:
7957 gnu_constraint_error_label_stack
.safe_push (Exception_Label (gnat_node
));
7960 case N_Push_Storage_Error_Label
:
7961 gnu_storage_error_label_stack
.safe_push (Exception_Label (gnat_node
));
7964 case N_Push_Program_Error_Label
:
7965 gnu_program_error_label_stack
.safe_push (Exception_Label (gnat_node
));
7968 case N_Pop_Constraint_Error_Label
:
7969 gnat_temp
= gnu_constraint_error_label_stack
.pop ();
7970 if (Present (gnat_temp
)
7971 && !TREE_USED (gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, false))
7972 && No_Exception_Propagation_Active ())
7973 Warn_If_No_Local_Raise (gnat_temp
);
7976 case N_Pop_Storage_Error_Label
:
7977 gnat_temp
= gnu_storage_error_label_stack
.pop ();
7978 if (Present (gnat_temp
)
7979 && !TREE_USED (gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, false))
7980 && No_Exception_Propagation_Active ())
7981 Warn_If_No_Local_Raise (gnat_temp
);
7984 case N_Pop_Program_Error_Label
:
7985 gnat_temp
= gnu_program_error_label_stack
.pop ();
7986 if (Present (gnat_temp
)
7987 && !TREE_USED (gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, false))
7988 && No_Exception_Propagation_Active ())
7989 Warn_If_No_Local_Raise (gnat_temp
);
7992 /******************************/
7993 /* Chapter 12: Generic Units */
7994 /******************************/
7996 case N_Generic_Function_Renaming_Declaration
:
7997 case N_Generic_Package_Renaming_Declaration
:
7998 case N_Generic_Procedure_Renaming_Declaration
:
7999 case N_Generic_Package_Declaration
:
8000 case N_Generic_Subprogram_Declaration
:
8001 case N_Package_Instantiation
:
8002 case N_Procedure_Instantiation
:
8003 case N_Function_Instantiation
:
8004 /* These nodes can appear on a declaration list but there is nothing to
8005 to be done with them. */
8006 gnu_result
= alloc_stmt_list ();
8009 /**************************************************/
8010 /* Chapter 13: Representation Clauses and */
8011 /* Implementation-Dependent Features */
8012 /**************************************************/
8014 case N_Attribute_Definition_Clause
:
8015 gnu_result
= alloc_stmt_list ();
8017 /* The only one we need to deal with is 'Address since, for the others,
8018 the front-end puts the information elsewhere. */
8019 if (Get_Attribute_Id (Chars (gnat_node
)) != Attr_Address
)
8022 /* And we only deal with 'Address if the object has a Freeze node. */
8023 gnat_temp
= Entity (Name (gnat_node
));
8024 if (Freeze_Node (gnat_temp
))
8026 tree gnu_address
= gnat_to_gnu (Expression (gnat_node
)), gnu_temp
;
8028 /* Get the value to use as the address and save it as the equivalent
8029 for the object; when it is frozen, gnat_to_gnu_entity will do the
8030 right thing. For a subprogram, put the naked address but build a
8031 meaningfull expression for an object in case its address is taken
8032 before the Freeze node is encountered; this can happen if the type
8033 of the object is limited and it is initialized with the result of
8035 if (Is_Subprogram (gnat_temp
))
8036 gnu_temp
= gnu_address
;
8039 tree gnu_type
= gnat_to_gnu_type (Etype (gnat_temp
));
8040 /* Drop atomic and volatile qualifiers for the expression. */
8041 gnu_type
= TYPE_MAIN_VARIANT (gnu_type
);
8043 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
8044 gnu_address
= convert (gnu_type
, gnu_address
);
8046 = build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_address
);
8049 save_gnu_tree (gnat_temp
, gnu_temp
, true);
8053 case N_Enumeration_Representation_Clause
:
8054 case N_Record_Representation_Clause
:
8056 /* We do nothing with these. SEM puts the information elsewhere. */
8057 gnu_result
= alloc_stmt_list ();
8060 case N_Code_Statement
:
8061 if (!type_annotate_only
)
8063 tree gnu_template
= gnat_to_gnu (Asm_Template (gnat_node
));
8064 tree gnu_inputs
= NULL_TREE
, gnu_outputs
= NULL_TREE
;
8065 tree gnu_clobbers
= NULL_TREE
, tail
;
8066 bool allows_mem
, allows_reg
, fake
;
8067 int ninputs
, noutputs
, i
;
8068 const char **oconstraints
;
8069 const char *constraint
;
8072 /* First retrieve the 3 operand lists built by the front-end. */
8073 Setup_Asm_Outputs (gnat_node
);
8074 while (Present (gnat_temp
= Asm_Output_Variable ()))
8076 tree gnu_value
= gnat_to_gnu (gnat_temp
);
8077 tree gnu_constr
= build_tree_list (NULL_TREE
, gnat_to_gnu
8078 (Asm_Output_Constraint ()));
8080 gnu_outputs
= tree_cons (gnu_constr
, gnu_value
, gnu_outputs
);
8084 Setup_Asm_Inputs (gnat_node
);
8085 while (Present (gnat_temp
= Asm_Input_Value ()))
8087 tree gnu_value
= gnat_to_gnu (gnat_temp
);
8088 tree gnu_constr
= build_tree_list (NULL_TREE
, gnat_to_gnu
8089 (Asm_Input_Constraint ()));
8091 gnu_inputs
= tree_cons (gnu_constr
, gnu_value
, gnu_inputs
);
8095 Clobber_Setup (gnat_node
);
8096 while ((clobber
= (char *) Clobber_Get_Next ()))
8098 = tree_cons (NULL_TREE
,
8099 build_string (strlen (clobber
) + 1, clobber
),
8102 /* Then perform some standard checking and processing on the
8103 operands. In particular, mark them addressable if needed. */
8104 gnu_outputs
= nreverse (gnu_outputs
);
8105 noutputs
= list_length (gnu_outputs
);
8106 gnu_inputs
= nreverse (gnu_inputs
);
8107 ninputs
= list_length (gnu_inputs
);
8108 oconstraints
= XALLOCAVEC (const char *, noutputs
);
8110 for (i
= 0, tail
= gnu_outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8112 tree output
= TREE_VALUE (tail
);
8114 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8115 oconstraints
[i
] = constraint
;
8117 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
8118 &allows_mem
, &allows_reg
, &fake
))
8120 /* If the operand is going to end up in memory,
8121 mark it addressable. Note that we don't test
8122 allows_mem like in the input case below; this
8123 is modeled on the C front-end. */
8126 output
= remove_conversions (output
, false);
8127 if (TREE_CODE (output
) == CONST_DECL
8128 && DECL_CONST_CORRESPONDING_VAR (output
))
8129 output
= DECL_CONST_CORRESPONDING_VAR (output
);
8130 if (!gnat_mark_addressable (output
))
8131 output
= error_mark_node
;
8135 output
= error_mark_node
;
8137 TREE_VALUE (tail
) = output
;
8140 for (i
= 0, tail
= gnu_inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
8142 tree input
= TREE_VALUE (tail
);
8144 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
8146 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
,
8148 &allows_mem
, &allows_reg
))
8150 /* If the operand is going to end up in memory,
8151 mark it addressable. */
8152 if (!allows_reg
&& allows_mem
)
8154 input
= remove_conversions (input
, false);
8155 if (TREE_CODE (input
) == CONST_DECL
8156 && DECL_CONST_CORRESPONDING_VAR (input
))
8157 input
= DECL_CONST_CORRESPONDING_VAR (input
);
8158 if (!gnat_mark_addressable (input
))
8159 input
= error_mark_node
;
8163 input
= error_mark_node
;
8165 TREE_VALUE (tail
) = input
;
8168 gnu_result
= build5 (ASM_EXPR
, void_type_node
,
8169 gnu_template
, gnu_outputs
,
8170 gnu_inputs
, gnu_clobbers
, NULL_TREE
);
8171 ASM_VOLATILE_P (gnu_result
) = Is_Asm_Volatile (gnat_node
);
8174 gnu_result
= alloc_stmt_list ();
8182 /* Markers are created by the ABE mechanism to capture information which
8183 is either unavailable of expensive to recompute. Markers do not have
8184 and runtime semantics, and should be ignored. */
8187 case N_Variable_Reference_Marker
:
8188 gnu_result
= alloc_stmt_list ();
8191 case N_Expression_With_Actions
:
8192 /* This construct doesn't define a scope so we don't push a binding
8193 level around the statement list, but we wrap it in a SAVE_EXPR to
8194 protect it from unsharing. Elaborate the expression as part of the
8195 same statement group as the actions so that the type declaration
8196 gets inserted there as well. This ensures that the type elaboration
8197 code is issued past the actions computing values on which it might
8199 start_stmt_group ();
8200 add_stmt_list (Actions (gnat_node
));
8201 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
8202 gnu_result
= end_stmt_group ();
8204 gnu_result
= build1 (SAVE_EXPR
, void_type_node
, gnu_result
);
8205 TREE_SIDE_EFFECTS (gnu_result
) = 1;
8208 = build_compound_expr (TREE_TYPE (gnu_expr
), gnu_result
, gnu_expr
);
8209 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
8212 case N_Freeze_Entity
:
8213 start_stmt_group ();
8214 process_freeze_entity (gnat_node
);
8215 process_decls (Actions (gnat_node
), Empty
, true, true);
8216 gnu_result
= end_stmt_group ();
8219 case N_Freeze_Generic_Entity
:
8220 gnu_result
= alloc_stmt_list ();
8223 case N_Itype_Reference
:
8224 if (!present_gnu_tree (Itype (gnat_node
)))
8225 process_type (Itype (gnat_node
));
8226 gnu_result
= alloc_stmt_list ();
8229 case N_Free_Statement
:
8230 gnat_temp
= Expression (gnat_node
);
8232 if (!type_annotate_only
)
8234 const Entity_Id gnat_desig_type
8235 = Designated_Type (Underlying_Type (Etype (gnat_temp
)));
8236 const Entity_Id gnat_pool
= Storage_Pool (gnat_node
);
8237 const bool pool_is_storage_model
8238 = Present (gnat_pool
)
8239 && Has_Storage_Model_Type_Aspect (Etype (gnat_pool
))
8240 && Present (Storage_Model_Copy_From (gnat_pool
));
8241 tree gnu_ptr
, gnu_ptr_type
, gnu_obj_type
, gnu_actual_obj_type
;
8243 /* Make sure the designated type is complete before dereferencing,
8244 in case it is a Taft Amendment type. */
8245 (void) gnat_to_gnu_entity (gnat_desig_type
, NULL_TREE
, false);
8247 gnu_ptr
= gnat_to_gnu (gnat_temp
);
8248 gnu_ptr_type
= TREE_TYPE (gnu_ptr
);
8250 /* If this is a thin pointer, we must first dereference it to create
8251 a fat pointer, then go back below to a thin pointer. The reason
8252 for this is that we need to have a fat pointer someplace in order
8253 to properly compute the size. */
8254 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr
)))
8255 gnu_ptr
= build_unary_op (ADDR_EXPR
, NULL_TREE
,
8256 build_unary_op (INDIRECT_REF
, NULL_TREE
,
8259 /* If this is a fat pointer, the object must have been allocated with
8260 the template in front of the array. So pass the template address,
8261 and get the total size; do it by converting to a thin pointer. */
8262 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr
)))
8264 = convert (build_pointer_type
8265 (TYPE_OBJECT_RECORD_TYPE
8266 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr
)))),
8269 gnu_obj_type
= TREE_TYPE (TREE_TYPE (gnu_ptr
));
8271 /* If this is a thin pointer, the object must have been allocated with
8272 the template in front of the array. So pass the template address,
8273 and get the total size. */
8274 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr
)))
8276 = build_binary_op (POINTER_PLUS_EXPR
, TREE_TYPE (gnu_ptr
),
8278 fold_build1 (NEGATE_EXPR
, sizetype
,
8281 TYPE_FIELDS ((gnu_obj_type
)))));
8283 /* If we have a special dynamic constrained subtype on the node, use
8284 it to compute the size; otherwise, use the designated subtype. */
8285 if (Present (Actual_Designated_Subtype (gnat_node
)))
8288 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node
));
8290 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type
))
8292 = build_unc_object_type_from_ptr (gnu_ptr_type
,
8293 gnu_actual_obj_type
,
8294 get_identifier ("DEALLOC"),
8298 gnu_actual_obj_type
= gnu_obj_type
;
8300 tree gnu_size
= TYPE_SIZE_UNIT (gnu_actual_obj_type
);
8301 gnu_size
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_size
, gnu_ptr
);
8302 if (pool_is_storage_model
)
8303 gnu_size
= INSTANTIATE_LOAD_IN_EXPR (gnu_size
, gnat_pool
);
8306 = build_call_alloc_dealloc (gnu_ptr
, gnu_size
, gnu_obj_type
,
8307 Procedure_To_Call (gnat_node
),
8308 gnat_pool
, gnat_node
);
8312 case N_Raise_Constraint_Error
:
8313 case N_Raise_Program_Error
:
8314 case N_Raise_Storage_Error
:
8315 if (type_annotate_only
)
8316 gnu_result
= alloc_stmt_list ();
8318 gnu_result
= Raise_Error_to_gnu (gnat_node
, &gnu_result_type
);
8321 case N_Validate_Unchecked_Conversion
:
8322 /* The only validation we currently do on an unchecked conversion is
8323 that of aliasing assumptions. */
8324 if (flag_strict_aliasing
)
8325 gnat_validate_uc_list
.safe_push (gnat_node
);
8326 gnu_result
= alloc_stmt_list ();
8329 case N_Function_Specification
:
8330 case N_Procedure_Specification
:
8332 case N_Component_Association
:
8333 /* These nodes should only be present when annotating types. */
8334 gcc_assert (type_annotate_only
);
8335 gnu_result
= alloc_stmt_list ();
8339 /* Other nodes are not supposed to reach here. */
8343 /* If we are in the elaboration procedure, check if we are violating the
8344 No_Elaboration_Code restriction by having a non-empty statement. */
8345 if (statement_node_p (gnat_node
)
8346 && !(TREE_CODE (gnu_result
) == STATEMENT_LIST
8347 && empty_stmt_list_p (gnu_result
))
8348 && current_function_decl
== get_elaboration_procedure ())
8349 Check_Elaboration_Code_Allowed (gnat_node
);
8351 /* If we pushed the processing of the elaboration routine, pop it back. */
8352 if (went_into_elab_proc
)
8353 current_function_decl
= NULL_TREE
;
8355 /* When not optimizing, turn boolean rvalues B into B != false tests
8356 so that we can put the location information of the reference to B on
8357 the inequality operator for better debug info. */
8359 && TREE_CODE (gnu_result
) != INTEGER_CST
8360 && TREE_CODE (gnu_result
) != TYPE_DECL
8361 && (kind
== N_Identifier
8362 || kind
== N_Expanded_Name
8363 || kind
== N_Explicit_Dereference
8364 || kind
== N_Indexed_Component
8365 || kind
== N_Selected_Component
)
8366 && TREE_CODE (get_base_type (gnu_result_type
)) == BOOLEAN_TYPE
8367 && Nkind (Parent (gnat_node
)) != N_Attribute_Reference
8368 && Nkind (Parent (gnat_node
)) != N_Pragma_Argument_Association
8369 && Nkind (Parent (gnat_node
)) != N_Variant_Part
8370 && !lvalue_required_p (gnat_node
, gnu_result_type
, false, false))
8373 = build_binary_op (NE_EXPR
, gnu_result_type
,
8374 convert (gnu_result_type
, gnu_result
),
8375 convert (gnu_result_type
, boolean_false_node
));
8376 if (TREE_CODE (gnu_result
) != INTEGER_CST
)
8377 set_gnu_expr_location_from_node (gnu_result
, gnat_node
);
8380 /* Set the location information on the result if it's not a simple name
8381 or something that contains a simple name, for example a tag, because
8382 we don"t want all the references to get the location of the first use.
8383 Note that we may have no result if we tried to build a CALL_EXPR node
8384 to a procedure with no side-effects and optimization is enabled. */
8385 else if (kind
!= N_Identifier
8386 && !(kind
== N_Selected_Component
8387 && Chars (Selector_Name (gnat_node
)) == Name_uTag
)
8389 && EXPR_P (gnu_result
))
8390 set_gnu_expr_location_from_node (gnu_result
, gnat_node
);
8392 /* If we're supposed to return something of void_type, it means we have
8393 something we're elaborating for effect, so just return. */
8394 if (VOID_TYPE_P (gnu_result_type
))
8397 /* If the result is a constant that overflowed, raise Constraint_Error. */
8398 if (TREE_CODE (gnu_result
) == INTEGER_CST
&& TREE_OVERFLOW (gnu_result
))
8400 post_error ("??Constraint_Error will be raised at run time", gnat_node
);
8402 = build1 (NULL_EXPR
, gnu_result_type
,
8403 build_call_raise (CE_Overflow_Check_Failed
, gnat_node
,
8404 N_Raise_Constraint_Error
));
8407 /* If the result has side-effects and is of an unconstrained type, protect
8408 the expression in case it will be referenced multiple times, i.e. for
8409 its value and to compute the size of an object. But do it neither for
8410 an object nor a renaming declaration, nor a return statement of a call
8411 to a function that returns an unconstrained record type with default
8412 discriminant, because there is no size to be computed in these cases
8413 and this will create a useless temporary. We must do this before any
8415 if (TREE_SIDE_EFFECTS (gnu_result
)
8416 && (TREE_CODE (gnu_result_type
) == UNCONSTRAINED_ARRAY_TYPE
8417 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type
)))
8418 && !(TREE_CODE (gnu_result
) == CALL_EXPR
8419 && type_is_padding_self_referential (TREE_TYPE (gnu_result
))
8420 && (Nkind (Parent (gnat_node
)) == N_Object_Declaration
8421 || Nkind (Parent (gnat_node
)) == N_Object_Renaming_Declaration
8422 || Nkind (Parent (gnat_node
)) == N_Simple_Return_Statement
)))
8423 gnu_result
= gnat_protect_expr (gnu_result
);
8425 /* Now convert the result to the result type, unless we are in one of the
8428 1. If this is the LHS of an assignment or an actual parameter of a
8429 call, return the result almost unmodified since the RHS will have
8430 to be converted to our type in that case, unless the result type
8431 has a simpler size or for array types because this size might be
8432 changed in-between. Likewise if there is just a no-op unchecked
8433 conversion in-between. Similarly, don't convert integral types
8434 that are the operands of an unchecked conversion since we need
8435 to ignore those conversions (for 'Valid).
8437 2. If we have a label (which doesn't have any well-defined type), a
8438 field or an error, return the result almost unmodified. Similarly,
8439 if the two types are record types with the same name, don't convert.
8440 This will be the case when we are converting from a packable version
8441 of a type to its original type and we need those conversions to be
8442 NOPs in order for assignments into these types to work properly.
8444 3. If the type is void or if we have no result, return error_mark_node
8445 to show we have no result.
8447 4. If this is a call to a function that returns with variable size and
8448 the call is used as the expression in either an object or a renaming
8449 declaration, return the result unmodified because we want to use the
8450 return slot optimization in this case.
8452 5. If this is a reference to an unconstrained array which is used either
8453 as the prefix of an attribute reference that requires an lvalue or in
8454 a return statement, then return the result unmodified because we want
8455 to return the original bounds.
8457 6. Finally, if the type of the result is already correct. */
8459 if (Present (Parent (gnat_node
))
8460 && (lhs_or_actual_p (gnat_node
)
8461 || (Nkind (Parent (gnat_node
)) == N_Unchecked_Type_Conversion
8462 && unchecked_conversion_nop (Parent (gnat_node
)))
8463 || (Nkind (Parent (gnat_node
)) == N_Unchecked_Type_Conversion
8464 && !AGGREGATE_TYPE_P (gnu_result_type
)
8465 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result
))))
8466 && !(TYPE_SIZE (gnu_result_type
)
8467 && TYPE_SIZE (TREE_TYPE (gnu_result
))
8468 && AGGREGATE_TYPE_P (gnu_result_type
)
8469 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result
))
8470 && ((TREE_CODE (TYPE_SIZE (gnu_result_type
)) == INTEGER_CST
8471 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result
)))
8473 || (TREE_CODE (TYPE_SIZE (gnu_result_type
)) != INTEGER_CST
8474 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type
))
8475 && (CONTAINS_PLACEHOLDER_P
8476 (TYPE_SIZE (TREE_TYPE (gnu_result
)))))
8477 || (TREE_CODE (gnu_result_type
) == ARRAY_TYPE
8478 && TREE_CODE (TREE_TYPE (gnu_result
)) == ARRAY_TYPE
))
8479 && !(TREE_CODE (gnu_result_type
) == RECORD_TYPE
8480 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type
))))
8482 /* Remove padding only if the inner object is of self-referential
8483 size: in that case it must be an object of unconstrained type
8484 with a default discriminant and we want to avoid copying too
8485 much data. But do not remove it if it is already too small. */
8486 if (type_is_padding_self_referential (TREE_TYPE (gnu_result
))
8487 && !(TREE_CODE (gnu_result
) == COMPONENT_REF
8488 && DECL_BIT_FIELD (TREE_OPERAND (gnu_result
, 1))
8489 && DECL_SIZE (TREE_OPERAND (gnu_result
, 1))
8490 != TYPE_SIZE (TREE_TYPE (gnu_result
))))
8491 gnu_result
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result
))),
8495 else if (TREE_CODE (gnu_result
) == LABEL_DECL
8496 || TREE_CODE (gnu_result
) == FIELD_DECL
8497 || TREE_CODE (gnu_result
) == ERROR_MARK
8498 || (TYPE_NAME (gnu_result_type
)
8499 == TYPE_NAME (TREE_TYPE (gnu_result
))
8500 && TREE_CODE (gnu_result_type
) == RECORD_TYPE
8501 && TREE_CODE (TREE_TYPE (gnu_result
)) == RECORD_TYPE
))
8503 /* Remove any padding. */
8504 gnu_result
= maybe_padded_object (gnu_result
);
8507 else if (gnu_result
== error_mark_node
|| gnu_result_type
== void_type_node
)
8508 gnu_result
= error_mark_node
;
8510 else if (TREE_CODE (gnu_result
) == CALL_EXPR
8511 && Present (Parent (gnat_node
))
8512 && (Nkind (Parent (gnat_node
)) == N_Object_Declaration
8513 || Nkind (Parent (gnat_node
)) == N_Object_Renaming_Declaration
)
8514 && return_type_with_variable_size_p (TREE_TYPE (gnu_result
)))
8517 else if (TREE_CODE (TREE_TYPE (gnu_result
)) == UNCONSTRAINED_ARRAY_TYPE
8518 && Present (Parent (gnat_node
))
8519 && ((Nkind (Parent (gnat_node
)) == N_Attribute_Reference
8520 && lvalue_required_for_attribute_p (Parent (gnat_node
)))
8521 || Nkind (Parent (gnat_node
)) == N_Simple_Return_Statement
))
8524 else if (TREE_TYPE (gnu_result
) != gnu_result_type
)
8525 gnu_result
= convert (gnu_result_type
, gnu_result
);
8527 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
8528 while ((TREE_CODE (gnu_result
) == NOP_EXPR
8529 || TREE_CODE (gnu_result
) == NON_LVALUE_EXPR
)
8530 && TREE_TYPE (TREE_OPERAND (gnu_result
, 0)) == TREE_TYPE (gnu_result
))
8531 gnu_result
= TREE_OPERAND (gnu_result
, 0);
8536 /* Similar to gnat_to_gnu, but discard any object that might be created in
8537 the course of the translation of GNAT_NODE, which must be an "external"
8538 expression in the sense that it will be elaborated elsewhere. */
8541 gnat_to_gnu_external (Node_Id gnat_node
)
8543 const int save_force_global
= force_global
;
8544 bool went_into_elab_proc
;
8546 /* Force the local context and create a fake scope that we zap
8547 at the end so declarations will not be stuck either in the
8548 global varpool or in the current scope. */
8549 if (!current_function_decl
)
8551 current_function_decl
= get_elaboration_procedure ();
8552 went_into_elab_proc
= true;
8555 went_into_elab_proc
= false;
8559 tree gnu_result
= gnat_to_gnu (gnat_node
);
8562 force_global
= save_force_global
;
8563 if (went_into_elab_proc
)
8564 current_function_decl
= NULL_TREE
;
8566 /* Do not import locations from external units. */
8567 if (CAN_HAVE_LOCATION_P (gnu_result
))
8568 SET_EXPR_LOCATION (gnu_result
, UNKNOWN_LOCATION
);
8573 /* Return true if the statement list STMT_LIST is empty. */
8576 empty_stmt_list_p (tree stmt_list
)
8578 tree_stmt_iterator tsi
;
8580 for (tsi
= tsi_start (stmt_list
); !tsi_end_p (tsi
); tsi_next (&tsi
))
8582 tree stmt
= tsi_stmt (tsi
);
8584 /* Anything else than an empty STMT_STMT counts as something. */
8585 if (TREE_CODE (stmt
) != STMT_STMT
|| STMT_STMT_STMT (stmt
))
8592 /* Record the current code position in GNAT_NODE. */
8595 record_code_position (Node_Id gnat_node
)
8597 tree stmt_stmt
= build1 (STMT_STMT
, void_type_node
, NULL_TREE
);
8599 add_stmt_with_node (stmt_stmt
, gnat_node
);
8600 save_gnu_tree (gnat_node
, stmt_stmt
, true);
8603 /* Insert the code for GNAT_NODE at the position saved for that node. */
8606 insert_code_for (Node_Id gnat_node
)
8608 tree code
= gnat_to_gnu (gnat_node
);
8610 /* It's too late to remove the STMT_STMT itself at this point. */
8611 if (!empty_stmt_list_p (code
))
8612 STMT_STMT_STMT (get_gnu_tree (gnat_node
)) = code
;
8614 save_gnu_tree (gnat_node
, NULL_TREE
, true);
8617 /* Start a new statement group chained to the previous group. */
8620 start_stmt_group (void)
8622 struct stmt_group
*group
= stmt_group_free_list
;
8624 /* First see if we can get one from the free list. */
8626 stmt_group_free_list
= group
->previous
;
8628 group
= ggc_alloc
<stmt_group
> ();
8630 group
->previous
= current_stmt_group
;
8631 group
->stmt_list
= group
->block
= group
->cleanups
= NULL_TREE
;
8632 current_stmt_group
= group
;
8635 /* Add GNU_STMT to the current statement group. If it is an expression with
8636 no effects, it is ignored. */
8639 add_stmt (tree gnu_stmt
)
8641 append_to_statement_list (gnu_stmt
, ¤t_stmt_group
->stmt_list
);
8644 /* Similar, but the statement is always added, regardless of side-effects. */
8647 add_stmt_force (tree gnu_stmt
)
8649 append_to_statement_list_force (gnu_stmt
, ¤t_stmt_group
->stmt_list
);
8652 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
8655 add_stmt_with_node (tree gnu_stmt
, Node_Id gnat_node
)
8657 if (Present (gnat_node
))
8658 set_expr_location_from_node (gnu_stmt
, gnat_node
);
8659 add_stmt (gnu_stmt
);
8662 /* Similar, but the statement is always added, regardless of side-effects. */
8665 add_stmt_with_node_force (tree gnu_stmt
, Node_Id gnat_node
)
8667 if (Present (gnat_node
))
8668 set_expr_location_from_node (gnu_stmt
, gnat_node
);
8669 add_stmt_force (gnu_stmt
);
8672 /* Add a declaration statement for GNU_DECL to the current statement group.
8673 Get the SLOC to be put onto the statement from GNAT_NODE. */
8676 add_decl_expr (tree gnu_decl
, Node_Id gnat_node
)
8678 tree type
= TREE_TYPE (gnu_decl
);
8679 tree gnu_stmt
, gnu_init
;
8681 /* If this is a variable that Gigi is to ignore, we may have been given
8682 an ERROR_MARK. So test for it. We also might have been given a
8683 reference for a renaming. So only do something for a decl. Also
8684 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
8685 if (!DECL_P (gnu_decl
)
8686 || (TREE_CODE (gnu_decl
) == TYPE_DECL
8687 && TREE_CODE (type
) == UNCONSTRAINED_ARRAY_TYPE
))
8690 gnu_stmt
= build1 (DECL_EXPR
, void_type_node
, gnu_decl
);
8692 /* If we are external or global, we don't want to output the DECL_EXPR for
8693 this DECL node since we already have evaluated the expressions in the
8694 sizes and positions as globals and doing it again would be wrong. */
8695 if (DECL_EXTERNAL (gnu_decl
) || global_bindings_p ())
8697 /* Mark everything as used to prevent node sharing with subprograms.
8698 Note that walk_tree knows how to deal with TYPE_DECL, but neither
8699 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
8700 MARK_VISITED (gnu_stmt
);
8701 if (VAR_P (gnu_decl
)
8702 || TREE_CODE (gnu_decl
) == CONST_DECL
)
8704 MARK_VISITED (DECL_SIZE (gnu_decl
));
8705 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl
));
8706 MARK_VISITED (DECL_INITIAL (gnu_decl
));
8710 add_stmt_with_node (gnu_stmt
, gnat_node
);
8712 /* Mark our TYPE_ADA_SIZE field now since it will not be gimplified. */
8713 if (TREE_CODE (gnu_decl
) == TYPE_DECL
8714 && RECORD_OR_UNION_TYPE_P (type
)
8715 && !TYPE_FAT_POINTER_P (type
))
8716 MARK_VISITED (TYPE_ADA_SIZE (type
));
8718 if (VAR_P (gnu_decl
) && (gnu_init
= DECL_INITIAL (gnu_decl
)))
8720 /* If this is a variable and an initializer is attached to it, it must be
8721 valid for the context. Similar to init_const in create_var_decl. */
8722 if (!gnat_types_compatible_p (type
, TREE_TYPE (gnu_init
))
8723 || (TREE_STATIC (gnu_decl
)
8724 && !initializer_constant_valid_p (gnu_init
,
8725 TREE_TYPE (gnu_init
))))
8727 DECL_INITIAL (gnu_decl
) = NULL_TREE
;
8728 if (TREE_READONLY (gnu_decl
))
8730 TREE_READONLY (gnu_decl
) = 0;
8731 DECL_READONLY_ONCE_ELAB (gnu_decl
) = 1;
8734 /* Remove any padding so the assignment is done properly. */
8735 gnu_decl
= maybe_padded_object (gnu_decl
);
8738 = build_binary_op (INIT_EXPR
, NULL_TREE
, gnu_decl
, gnu_init
);
8739 add_stmt_with_node (gnu_stmt
, gnat_node
);
8742 /* If this is the initialization of a (potentially) large aggregate, then
8743 declare the dependence on the memcpy routine. */
8744 if (AGGREGATE_TYPE_P (type
)
8745 && (!TREE_CONSTANT (TYPE_SIZE (type
))
8746 || compare_tree_int (TYPE_SIZE (type
), 2 * BITS_PER_WORD
) > 0))
8747 Check_Restriction_No_Dependence_On_System (Name_Memory_Copy
,
8752 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
8755 mark_visited_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
8759 if (TREE_VISITED (t
))
8762 /* Don't mark a dummy type as visited because we want to mark its sizes
8763 and fields once it's filled in. */
8764 else if (!TYPE_IS_DUMMY_P (t
))
8765 TREE_VISITED (t
) = 1;
8767 /* The test in gimplify_type_sizes is on the main variant. */
8769 TYPE_SIZES_GIMPLIFIED (TYPE_MAIN_VARIANT (t
)) = 1;
8774 /* Mark nodes rooted at T with TREE_VISITED and types as having their
8775 sized gimplified. We use this to indicate all variable sizes and
8776 positions in global types may not be shared by any subprogram. */
8779 mark_visited (tree t
)
8781 walk_tree (&t
, mark_visited_r
, NULL
, NULL
);
8784 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
8785 set its location to that of GNAT_NODE if present, but with column info
8786 cleared so that conditional branches generated as part of the cleanup
8787 code do not interfere with coverage analysis tools. */
8790 add_cleanup (tree gnu_cleanup
, Node_Id gnat_node
)
8792 if (Present (gnat_node
))
8793 set_expr_location_from_node (gnu_cleanup
, gnat_node
, true);
8795 /* An EH_ELSE_EXPR must be by itself, and that's all we need when we
8796 use it. The assert below makes sure that is so. Should we ever
8797 need more than that, we could combine EH_ELSE_EXPRs, and copy
8798 non-EH_ELSE_EXPR stmts into both cleanup paths of an
8800 if (TREE_CODE (gnu_cleanup
) == EH_ELSE_EXPR
)
8802 gcc_assert (!current_stmt_group
->cleanups
);
8803 current_stmt_group
->cleanups
= gnu_cleanup
;
8807 gcc_assert (!current_stmt_group
->cleanups
8808 || (TREE_CODE (current_stmt_group
->cleanups
)
8810 append_to_statement_list (gnu_cleanup
, ¤t_stmt_group
->cleanups
);
8814 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
8817 set_block_for_group (tree gnu_block
)
8819 gcc_assert (!current_stmt_group
->block
);
8820 current_stmt_group
->block
= gnu_block
;
8823 /* Return code corresponding to the current code group. It is normally
8824 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
8825 BLOCK or cleanups were set. */
8828 end_stmt_group (void)
8830 struct stmt_group
*group
= current_stmt_group
;
8831 tree gnu_retval
= group
->stmt_list
;
8833 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
8834 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
8835 make a BIND_EXPR. Note that we nest in that because the cleanup may
8836 reference variables in the block. */
8838 gnu_retval
= alloc_stmt_list ();
8840 if (group
->cleanups
)
8841 gnu_retval
= build2 (TRY_FINALLY_EXPR
, void_type_node
, gnu_retval
,
8844 if (current_stmt_group
->block
)
8845 gnu_retval
= build3 (BIND_EXPR
, void_type_node
, BLOCK_VARS (group
->block
),
8846 gnu_retval
, group
->block
);
8848 /* Remove this group from the stack and add it to the free list. */
8849 current_stmt_group
= group
->previous
;
8850 group
->previous
= stmt_group_free_list
;
8851 stmt_group_free_list
= group
;
8856 /* Return whether the current statement group may fall through. */
8859 stmt_group_may_fallthru (void)
8861 if (current_stmt_group
->stmt_list
)
8862 return block_may_fallthru (current_stmt_group
->stmt_list
);
8867 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
8871 add_stmt_list (List_Id gnat_list
)
8875 if (Present (gnat_list
))
8876 for (gnat_node
= First (gnat_list
); Present (gnat_node
);
8877 gnat_node
= Next (gnat_node
))
8878 add_stmt (gnat_to_gnu (gnat_node
));
8881 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
8882 If BINDING_P is true, push and pop a binding level around the list. */
8885 build_stmt_group (List_Id gnat_list
, bool binding_p
)
8887 start_stmt_group ();
8892 add_stmt_list (gnat_list
);
8897 return end_stmt_group ();
8900 /* Generate GIMPLE in place for the expression at *EXPR_P. */
8903 gnat_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
,
8904 gimple_seq
*post_p ATTRIBUTE_UNUSED
)
8906 tree expr
= *expr_p
;
8907 tree type
= TREE_TYPE (expr
);
8910 if (IS_ADA_STMT (expr
))
8911 return gnat_gimplify_stmt (expr_p
);
8913 switch (TREE_CODE (expr
))
8916 op
= TREE_OPERAND (expr
, 0);
8918 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
8919 is put into static memory. We know that it's going to be read-only
8920 given the semantics we have and it must be in static memory when the
8921 reference is in an elaboration procedure. */
8922 if (TREE_CODE (op
) == CONSTRUCTOR
&& TREE_CONSTANT (op
))
8924 tree addr
= build_fold_addr_expr (tree_output_constant_def (op
));
8925 *expr_p
= fold_convert (type
, addr
);
8929 /* Replace atomic loads with their first argument. That's necessary
8930 because the gimplifier would create a temporary otherwise. */
8931 if (TREE_SIDE_EFFECTS (op
))
8932 while (handled_component_p (op
) || CONVERT_EXPR_P (op
))
8934 tree inner
= TREE_OPERAND (op
, 0);
8935 if (TREE_CODE (inner
) == CALL_EXPR
&& call_is_atomic_load (inner
))
8937 tree t
= CALL_EXPR_ARG (inner
, 0);
8938 if (TREE_CODE (t
) == NOP_EXPR
)
8939 t
= TREE_OPERAND (t
, 0);
8940 if (TREE_CODE (t
) == ADDR_EXPR
)
8941 TREE_OPERAND (op
, 0) = TREE_OPERAND (t
, 0);
8943 TREE_OPERAND (op
, 0) = build_fold_indirect_ref (t
);
8951 /* If we are passing a constant fat pointer CONSTRUCTOR, make sure it is
8952 put into static memory; this performs a restricted version of constant
8953 propagation on fat pointers in calls. But do not do it for strings to
8954 avoid blocking concatenation in the caller when it is inlined. */
8955 for (int i
= 0; i
< call_expr_nargs (expr
); i
++)
8957 tree arg
= CALL_EXPR_ARG (expr
, i
);
8959 if (TREE_CODE (arg
) == CONSTRUCTOR
8960 && TREE_CONSTANT (arg
)
8961 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (arg
)))
8963 tree t
= CONSTRUCTOR_ELT (arg
, 0)->value
;
8964 if (TREE_CODE (t
) == NOP_EXPR
)
8965 t
= TREE_OPERAND (t
, 0);
8966 if (TREE_CODE (t
) == ADDR_EXPR
)
8967 t
= TREE_OPERAND (t
, 0);
8968 if (TREE_CODE (t
) != STRING_CST
)
8969 CALL_EXPR_ARG (expr
, i
) = tree_output_constant_def (arg
);
8975 op
= DECL_EXPR_DECL (expr
);
8977 /* The expressions for the RM bounds must be gimplified to ensure that
8978 they are properly elaborated. See gimplify_decl_expr. */
8979 if ((TREE_CODE (op
) == TYPE_DECL
|| VAR_P (op
))
8980 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op
))
8981 && (INTEGRAL_TYPE_P (TREE_TYPE (op
))
8982 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (op
))))
8984 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (op
)), t
, val
;
8986 val
= TYPE_RM_MIN_VALUE (type
);
8989 gimplify_one_sizepos (&val
, pre_p
);
8990 for (t
= type
; t
; t
= TYPE_NEXT_VARIANT (t
))
8991 SET_TYPE_RM_MIN_VALUE (t
, val
);
8994 val
= TYPE_RM_MAX_VALUE (type
);
8997 gimplify_one_sizepos (&val
, pre_p
);
8998 for (t
= type
; t
; t
= TYPE_NEXT_VARIANT (t
))
8999 SET_TYPE_RM_MAX_VALUE (t
, val
);
9005 /* If this is an aggregate type, build a null pointer of the appropriate
9006 type and dereference it. */
9007 if (AGGREGATE_TYPE_P (type
)
9008 || TREE_CODE (type
) == UNCONSTRAINED_ARRAY_TYPE
)
9009 *expr_p
= build_unary_op (INDIRECT_REF
, NULL_TREE
,
9010 convert (build_pointer_type (type
),
9011 null_pointer_node
));
9013 /* Otherwise, just make a VAR_DECL. */
9016 *expr_p
= create_tmp_var (type
, NULL
);
9017 suppress_warning (*expr_p
);
9020 gimplify_and_add (TREE_OPERAND (expr
, 0), pre_p
);
9024 op
= TREE_OPERAND (expr
, 0);
9026 /* Propagate TREE_NO_WARNING from expression to temporary by using the
9027 SAVE_EXPR itself as an intermediate step. See gimplify_save_expr. */
9028 if (type
== void_type_node
)
9030 else if (SAVE_EXPR_RESOLVED_P (expr
))
9031 TREE_NO_WARNING (op
) = TREE_NO_WARNING (expr
);
9033 TREE_NO_WARNING (expr
) = TREE_NO_WARNING (op
);
9038 tree new_var
= create_tmp_var (type
, "L");
9039 TREE_ADDRESSABLE (new_var
) = 1;
9041 tree init
= TREE_OPERAND (expr
, 1);
9042 gcc_assert (TREE_CODE (init
) == CALL_EXPR
);
9043 tree arg
= CALL_EXPR_ARG (init
, 1);
9044 CALL_EXPR_ARG (init
, 1)
9045 = build_unary_op (ADDR_EXPR
, TREE_TYPE (arg
), new_var
);
9046 gimplify_and_add (init
, pre_p
);
9052 case VIEW_CONVERT_EXPR
:
9053 op
= TREE_OPERAND (expr
, 0);
9055 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
9056 type to a scalar one, explicitly create the local temporary. That's
9057 required if the type is passed by reference. */
9058 if ((TREE_CODE (op
) == CONSTRUCTOR
|| TREE_CODE (op
) == CALL_EXPR
)
9059 && AGGREGATE_TYPE_P (TREE_TYPE (op
))
9060 && !AGGREGATE_TYPE_P (type
))
9062 tree new_var
= create_tmp_var_raw (TREE_TYPE (op
), "C");
9063 gimple_add_tmp_var (new_var
);
9065 tree mod
= build2 (INIT_EXPR
, TREE_TYPE (new_var
), new_var
, op
);
9066 gimplify_and_add (mod
, pre_p
);
9068 TREE_OPERAND (expr
, 0) = new_var
;
9073 case UNCONSTRAINED_ARRAY_REF
:
9074 /* We should only do this if we are just elaborating for side effects,
9075 but we can't know that yet. */
9076 *expr_p
= TREE_OPERAND (expr
, 0);
9083 return GS_UNHANDLED
;
9086 /* Generate GIMPLE in place for the statement at *STMT_P. */
9088 static enum gimplify_status
9089 gnat_gimplify_stmt (tree
*stmt_p
)
9091 tree stmt
= *stmt_p
;
9093 switch (TREE_CODE (stmt
))
9096 *stmt_p
= STMT_STMT_STMT (stmt
);
9101 tree gnu_start_label
= create_artificial_label (input_location
);
9102 tree gnu_cond
= LOOP_STMT_COND (stmt
);
9103 tree gnu_update
= LOOP_STMT_UPDATE (stmt
);
9104 tree gnu_end_label
= LOOP_STMT_LABEL (stmt
);
9106 /* Build the condition expression from the test, if any. */
9109 /* Deal with the optimization hints. */
9110 if (LOOP_STMT_IVDEP (stmt
))
9111 gnu_cond
= build3 (ANNOTATE_EXPR
, TREE_TYPE (gnu_cond
), gnu_cond
,
9112 build_int_cst (integer_type_node
,
9113 annot_expr_ivdep_kind
),
9115 if (LOOP_STMT_NO_UNROLL (stmt
))
9116 gnu_cond
= build3 (ANNOTATE_EXPR
, TREE_TYPE (gnu_cond
), gnu_cond
,
9117 build_int_cst (integer_type_node
,
9118 annot_expr_unroll_kind
),
9120 if (LOOP_STMT_UNROLL (stmt
))
9121 gnu_cond
= build3 (ANNOTATE_EXPR
, TREE_TYPE (gnu_cond
), gnu_cond
,
9122 build_int_cst (integer_type_node
,
9123 annot_expr_unroll_kind
),
9124 build_int_cst (NULL_TREE
, USHRT_MAX
));
9125 if (LOOP_STMT_NO_VECTOR (stmt
))
9126 gnu_cond
= build3 (ANNOTATE_EXPR
, TREE_TYPE (gnu_cond
), gnu_cond
,
9127 build_int_cst (integer_type_node
,
9128 annot_expr_no_vector_kind
),
9130 if (LOOP_STMT_VECTOR (stmt
))
9131 gnu_cond
= build3 (ANNOTATE_EXPR
, TREE_TYPE (gnu_cond
), gnu_cond
,
9132 build_int_cst (integer_type_node
,
9133 annot_expr_vector_kind
),
9137 = build3 (COND_EXPR
, void_type_node
, gnu_cond
, NULL_TREE
,
9138 build1 (GOTO_EXPR
, void_type_node
, gnu_end_label
));
9141 /* Set to emit the statements of the loop. */
9142 *stmt_p
= NULL_TREE
;
9144 /* We first emit the start label and then a conditional jump to the
9145 end label if there's a top condition, then the update if it's at
9146 the top, then the body of the loop, then a conditional jump to
9147 the end label if there's a bottom condition, then the update if
9148 it's at the bottom, and finally a jump to the start label and the
9149 definition of the end label. */
9150 append_to_statement_list (build1 (LABEL_EXPR
, void_type_node
,
9154 if (gnu_cond
&& !LOOP_STMT_BOTTOM_COND_P (stmt
))
9155 append_to_statement_list (gnu_cond
, stmt_p
);
9157 if (gnu_update
&& LOOP_STMT_TOP_UPDATE_P (stmt
))
9158 append_to_statement_list (gnu_update
, stmt_p
);
9160 append_to_statement_list (LOOP_STMT_BODY (stmt
), stmt_p
);
9162 if (gnu_cond
&& LOOP_STMT_BOTTOM_COND_P (stmt
))
9163 append_to_statement_list (gnu_cond
, stmt_p
);
9165 if (gnu_update
&& !LOOP_STMT_TOP_UPDATE_P (stmt
))
9166 append_to_statement_list (gnu_update
, stmt_p
);
9168 tree t
= build1 (GOTO_EXPR
, void_type_node
, gnu_start_label
);
9169 SET_EXPR_LOCATION (t
, DECL_SOURCE_LOCATION (gnu_end_label
));
9170 append_to_statement_list (t
, stmt_p
);
9172 append_to_statement_list (build1 (LABEL_EXPR
, void_type_node
,
9179 /* Build a statement to jump to the corresponding end label, then
9180 see if it needs to be conditional. */
9181 *stmt_p
= build1 (GOTO_EXPR
, void_type_node
, EXIT_STMT_LABEL (stmt
));
9182 if (EXIT_STMT_COND (stmt
))
9183 *stmt_p
= build3 (COND_EXPR
, void_type_node
,
9184 EXIT_STMT_COND (stmt
), *stmt_p
, alloc_stmt_list ());
9192 /* Force a reference to each of the entities in GNAT_PACKAGE recursively.
9194 This routine is exclusively called in type_annotate mode, to compute DDA
9195 information for types in withed units, for ASIS use. */
9198 elaborate_all_entities_for_package (Entity_Id gnat_package
)
9200 Entity_Id gnat_entity
;
9202 for (gnat_entity
= First_Entity (gnat_package
);
9203 Present (gnat_entity
);
9204 gnat_entity
= Next_Entity (gnat_entity
))
9206 const Entity_Kind kind
= Ekind (gnat_entity
);
9208 /* We are interested only in entities visible from the main unit. */
9209 if (!Is_Public (gnat_entity
))
9212 /* Skip stuff internal to the compiler. */
9213 if (Is_Intrinsic_Subprogram (gnat_entity
))
9215 if (kind
== E_Operator
)
9217 if (IN (kind
, Subprogram_Kind
)
9218 && (Present (Alias (gnat_entity
))
9219 || Is_Intrinsic_Subprogram (gnat_entity
)))
9221 if (Is_Itype (gnat_entity
))
9224 /* Skip named numbers. */
9225 if (IN (kind
, Named_Kind
))
9228 /* Skip generic declarations. */
9229 if (IN (kind
, Generic_Unit_Kind
))
9232 /* Skip formal objects. */
9233 if (IN (kind
, Formal_Object_Kind
))
9236 /* Skip package bodies. */
9237 if (kind
== E_Package_Body
)
9240 /* Skip limited views that point back to the main unit. */
9241 if (IN (kind
, Incomplete_Kind
)
9242 && From_Limited_With (gnat_entity
)
9243 && In_Extended_Main_Code_Unit (Non_Limited_View (gnat_entity
)))
9246 /* Skip types that aren't frozen. */
9247 if (IN (kind
, Type_Kind
) && !Is_Frozen (gnat_entity
))
9250 /* Recurse on real packages that aren't in the main unit. */
9251 if (kind
== E_Package
)
9253 if (No (Renamed_Entity (gnat_entity
))
9254 && !In_Extended_Main_Code_Unit (gnat_entity
))
9255 elaborate_all_entities_for_package (gnat_entity
);
9258 gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, false);
9262 /* Force a reference to each of the entities in packages withed by GNAT_NODE.
9263 Operate recursively but check that we aren't elaborating something more
9266 This routine is exclusively called in type_annotate mode, to compute DDA
9267 information for types in withed units, for ASIS use. */
9270 elaborate_all_entities (Node_Id gnat_node
)
9272 Entity_Id gnat_with_clause
;
9274 /* Process each unit only once. As we trace the context of all relevant
9275 units transitively, including generic bodies, we may encounter the
9276 same generic unit repeatedly. */
9277 if (!present_gnu_tree (gnat_node
))
9278 save_gnu_tree (gnat_node
, integer_zero_node
, true);
9280 /* Save entities in all context units. A body may have an implicit_with
9281 on its own spec, if the context includes a child unit, so don't save
9283 for (gnat_with_clause
= First (Context_Items (gnat_node
));
9284 Present (gnat_with_clause
);
9285 gnat_with_clause
= Next (gnat_with_clause
))
9286 if (Nkind (gnat_with_clause
) == N_With_Clause
9287 && !present_gnu_tree (Library_Unit (gnat_with_clause
))
9288 && Library_Unit (gnat_with_clause
) != Library_Unit (Cunit (Main_Unit
)))
9290 Node_Id gnat_unit
= Library_Unit (gnat_with_clause
);
9291 Entity_Id gnat_entity
= Entity (Name (gnat_with_clause
));
9293 elaborate_all_entities (gnat_unit
);
9295 if (Ekind (gnat_entity
) == E_Package
9296 && No (Renamed_Entity (gnat_entity
)))
9297 elaborate_all_entities_for_package (gnat_entity
);
9299 else if (Ekind (gnat_entity
) == E_Generic_Package
)
9301 Node_Id gnat_body
= Corresponding_Body (Unit (gnat_unit
));
9303 /* Retrieve compilation unit node of generic body. */
9304 while (Present (gnat_body
)
9305 && Nkind (gnat_body
) != N_Compilation_Unit
)
9306 gnat_body
= Parent (gnat_body
);
9308 /* If body is available, elaborate its context. */
9309 if (Present (gnat_body
))
9310 elaborate_all_entities (gnat_body
);
9314 if (Nkind (Unit (gnat_node
)) == N_Package_Body
)
9315 elaborate_all_entities (Library_Unit (gnat_node
));
9318 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
9321 process_freeze_entity (Node_Id gnat_node
)
9323 const Entity_Id gnat_entity
= Entity (gnat_node
);
9324 const Entity_Kind kind
= Ekind (gnat_entity
);
9325 tree gnu_old
, gnu_new
;
9327 /* If this is a package, generate code for the package body, if any. */
9328 if (kind
== E_Package
)
9330 const Node_Id gnat_decl
= Parent (Declaration_Node (gnat_entity
));
9331 if (Present (Corresponding_Body (gnat_decl
)))
9332 insert_code_for (Parent (Corresponding_Body (gnat_decl
)));
9336 /* Don't do anything for class-wide types as they are always transformed
9337 into their root type. */
9338 if (kind
== E_Class_Wide_Type
)
9341 /* Likewise for the entities internally used by the front-end to register
9342 primitives covering abstract interfaces, see Expand_N_Freeze_Entity. */
9343 if (Is_Subprogram (gnat_entity
) && Present (Interface_Alias (gnat_entity
)))
9346 /* Check for an old definition if this isn't an object with address clause,
9347 since the saved GCC tree is the address expression in that case. */
9349 = present_gnu_tree (gnat_entity
) && No (Address_Clause (gnat_entity
))
9350 ? get_gnu_tree (gnat_entity
) : NULL_TREE
;
9352 /* Don't do anything for subprograms that may have been elaborated before
9353 their freeze nodes. This can happen, for example, because of an inner
9354 call in an instance body or because of previous compilation of a spec
9355 for inlining purposes. */
9357 && ((TREE_CODE (gnu_old
) == FUNCTION_DECL
9358 && (kind
== E_Function
|| kind
== E_Procedure
))
9359 || (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (gnu_old
))
9360 && kind
== E_Subprogram_Type
)))
9363 /* If we have a non-dummy type old tree, we have nothing to do, except for
9364 aborting, since this node was never delayed as it should have been. We
9365 let this happen for concurrent types and their Corresponding_Record_Type,
9366 however, because each might legitimately be elaborated before its own
9367 freeze node, e.g. while processing the other. */
9369 && !(TREE_CODE (gnu_old
) == TYPE_DECL
9370 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old
))))
9372 gcc_assert (Is_Concurrent_Type (gnat_entity
)
9373 || (Is_Record_Type (gnat_entity
)
9374 && Is_Concurrent_Record_Type (gnat_entity
)));
9378 /* Reset the saved tree, if any, and elaborate the object or type for real.
9379 If there is a full view, elaborate it and use the result. And, if this
9380 is the root type of a class-wide type, reuse it for the latter. */
9383 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
9385 if (Is_Incomplete_Or_Private_Type (gnat_entity
)
9386 && Present (Full_View (gnat_entity
)))
9388 Entity_Id full_view
= Full_View (gnat_entity
);
9390 save_gnu_tree (full_view
, NULL_TREE
, false);
9392 if (Is_Private_Type (full_view
)
9393 && Present (Underlying_Full_View (full_view
)))
9395 full_view
= Underlying_Full_View (full_view
);
9396 save_gnu_tree (full_view
, NULL_TREE
, false);
9400 if (Is_Type (gnat_entity
)
9401 && Present (Class_Wide_Type (gnat_entity
))
9402 && Root_Type (Class_Wide_Type (gnat_entity
)) == gnat_entity
)
9403 save_gnu_tree (Class_Wide_Type (gnat_entity
), NULL_TREE
, false);
9406 if (Is_Incomplete_Or_Private_Type (gnat_entity
)
9407 && Present (Full_View (gnat_entity
)))
9409 Entity_Id full_view
= Full_View (gnat_entity
);
9411 if (Is_Private_Type (full_view
)
9412 && Present (Underlying_Full_View (full_view
)))
9413 full_view
= Underlying_Full_View (full_view
);
9415 gnu_new
= gnat_to_gnu_entity (full_view
, NULL_TREE
, true);
9417 /* Propagate back-annotations from full view to partial view. */
9418 if (!Known_Alignment (gnat_entity
))
9419 Copy_Alignment (gnat_entity
, full_view
);
9421 if (!Known_Esize (gnat_entity
))
9422 Copy_Esize (gnat_entity
, full_view
);
9424 if (!Known_RM_Size (gnat_entity
))
9425 Copy_RM_Size (gnat_entity
, full_view
);
9427 /* The above call may have defined this entity (the simplest example
9428 of this is when we have a private enumeral type since the bounds
9429 will have the public view). */
9430 if (!present_gnu_tree (gnat_entity
))
9431 save_gnu_tree (gnat_entity
, gnu_new
, false);
9436 = (Nkind (Declaration_Node (gnat_entity
)) == N_Object_Declaration
9437 && present_gnu_tree (Declaration_Node (gnat_entity
)))
9438 ? get_gnu_tree (Declaration_Node (gnat_entity
)) : NULL_TREE
;
9440 gnu_new
= gnat_to_gnu_entity (gnat_entity
, gnu_init
, true);
9443 if (Is_Type (gnat_entity
)
9444 && Present (Class_Wide_Type (gnat_entity
))
9445 && Root_Type (Class_Wide_Type (gnat_entity
)) == gnat_entity
)
9446 save_gnu_tree (Class_Wide_Type (gnat_entity
), gnu_new
, false);
9448 /* If we have an old type and we've made pointers to this type, update those
9449 pointers. If this is a Taft amendment type in the main unit, we need to
9450 mark the type as used since other units referencing it don't see the full
9451 declaration and, therefore, cannot mark it as used themselves. */
9454 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old
)),
9455 TREE_TYPE (gnu_new
));
9456 if (TYPE_DUMMY_IN_PROFILE_P (TREE_TYPE (gnu_old
)))
9457 update_profiles_with (TREE_TYPE (gnu_old
));
9458 if (DECL_TAFT_TYPE_P (gnu_old
))
9459 used_types_insert (TREE_TYPE (gnu_new
));
9463 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
9464 We make two passes, one to elaborate anything other than bodies (but
9465 we declare a function if there was no spec). The second pass
9466 elaborates the bodies.
9468 We make a complete pass through both lists if PASS1P is true, then make
9469 the second pass over both lists if PASS2P is true. The lists usually
9470 correspond to the public and private parts of a package. */
9473 process_decls (List_Id gnat_decls
, List_Id gnat_decls2
,
9474 bool pass1p
, bool pass2p
)
9476 List_Id gnat_decl_array
[2];
9480 gnat_decl_array
[0] = gnat_decls
, gnat_decl_array
[1] = gnat_decls2
;
9483 for (i
= 0; i
<= 1; i
++)
9484 if (Present (gnat_decl_array
[i
]))
9485 for (gnat_decl
= First (gnat_decl_array
[i
]);
9486 Present (gnat_decl
);
9487 gnat_decl
= Next (gnat_decl
))
9489 /* For package specs, we recurse inside the declarations,
9490 thus taking the two pass approach inside the boundary. */
9491 if (Nkind (gnat_decl
) == N_Package_Declaration
9492 && (Nkind (Specification (gnat_decl
)
9493 == N_Package_Specification
)))
9494 process_decls (Visible_Declarations (Specification (gnat_decl
)),
9495 Private_Declarations (Specification (gnat_decl
)),
9498 /* Similarly for any declarations in the actions of a
9500 else if (Nkind (gnat_decl
) == N_Freeze_Entity
)
9502 process_freeze_entity (gnat_decl
);
9503 process_decls (Actions (gnat_decl
), Empty
, true, false);
9506 /* Package bodies with freeze nodes get their elaboration deferred
9507 until the freeze node, but the code must be placed in the right
9508 place, so record the code position now. */
9509 else if (Nkind (gnat_decl
) == N_Package_Body
9510 && Present (Freeze_Node (Corresponding_Spec (gnat_decl
))))
9511 record_code_position (gnat_decl
);
9513 else if (Nkind (gnat_decl
) == N_Package_Body_Stub
9514 && Present (Library_Unit (gnat_decl
))
9515 && Present (Freeze_Node
9518 (Library_Unit (gnat_decl
)))))))
9519 record_code_position
9520 (Proper_Body (Unit (Library_Unit (gnat_decl
))));
9522 /* We defer most subprogram bodies to the second pass. For bodies
9523 that act as their own specs and stubs, the entity itself must be
9524 elaborated in the first pass, because it may be used in other
9526 else if (Nkind (gnat_decl
) == N_Subprogram_Body
)
9528 if (Acts_As_Spec (gnat_decl
))
9530 Entity_Id gnat_subprog
= Defining_Entity (gnat_decl
);
9532 if (!Is_Generic_Subprogram (gnat_subprog
))
9533 gnat_to_gnu_entity (gnat_subprog
, NULL_TREE
, true);
9537 else if (Nkind (gnat_decl
) == N_Subprogram_Body_Stub
)
9539 Entity_Id gnat_subprog
9540 = Defining_Entity (Specification (gnat_decl
));
9542 if (!Is_Generic_Subprogram (gnat_subprog
)
9543 && Ekind (gnat_subprog
) != E_Subprogram_Body
)
9544 gnat_to_gnu_entity (gnat_subprog
, NULL_TREE
, true);
9547 /* Concurrent stubs stand for the corresponding subprogram bodies,
9548 which are deferred like other bodies. */
9549 else if (Nkind (gnat_decl
) == N_Task_Body_Stub
9550 || Nkind (gnat_decl
) == N_Protected_Body_Stub
)
9553 /* Renamed subprograms may not be elaborated yet at this point
9554 since renamings do not trigger freezing. Wait for the second
9555 pass to take care of them. */
9556 else if (Nkind (gnat_decl
) == N_Subprogram_Renaming_Declaration
)
9560 add_stmt (gnat_to_gnu (gnat_decl
));
9563 /* Here we elaborate everything we deferred above except for package bodies,
9564 which are elaborated at their freeze nodes. Note that we must also
9565 go inside things (package specs and freeze nodes) the first pass did. */
9567 for (i
= 0; i
<= 1; i
++)
9568 if (Present (gnat_decl_array
[i
]))
9569 for (gnat_decl
= First (gnat_decl_array
[i
]);
9570 Present (gnat_decl
);
9571 gnat_decl
= Next (gnat_decl
))
9573 if (Nkind (gnat_decl
) == N_Subprogram_Body
9574 || Nkind (gnat_decl
) == N_Subprogram_Body_Stub
9575 || Nkind (gnat_decl
) == N_Task_Body_Stub
9576 || Nkind (gnat_decl
) == N_Protected_Body_Stub
)
9577 add_stmt (gnat_to_gnu (gnat_decl
));
9579 else if (Nkind (gnat_decl
) == N_Package_Declaration
9580 && (Nkind (Specification (gnat_decl
)
9581 == N_Package_Specification
)))
9582 process_decls (Visible_Declarations (Specification (gnat_decl
)),
9583 Private_Declarations (Specification (gnat_decl
)),
9586 else if (Nkind (gnat_decl
) == N_Freeze_Entity
)
9587 process_decls (Actions (gnat_decl
), Empty
, false, true);
9589 else if (Nkind (gnat_decl
) == N_Subprogram_Renaming_Declaration
)
9590 add_stmt (gnat_to_gnu (gnat_decl
));
9594 /* Make a unary operation of kind CODE using build_unary_op, but guard
9595 the operation by an overflow check. CODE can be one of NEGATE_EXPR
9596 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
9597 the operation is to be performed in that type. GNAT_NODE is the gnat
9598 node conveying the source location for which the error should be
9602 build_unary_op_trapv (enum tree_code code
, tree gnu_type
, tree operand
,
9605 gcc_assert (code
== NEGATE_EXPR
|| code
== ABS_EXPR
);
9607 operand
= gnat_protect_expr (operand
);
9609 return emit_check (build_binary_op (EQ_EXPR
, boolean_type_node
,
9610 operand
, TYPE_MIN_VALUE (gnu_type
)),
9611 build_unary_op (code
, gnu_type
, operand
),
9612 CE_Overflow_Check_Failed
, gnat_node
);
9615 /* Make a binary operation of kind CODE using build_binary_op, but guard
9616 the operation by an overflow check. CODE can be one of PLUS_EXPR,
9617 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
9618 Usually the operation is to be performed in that type. GNAT_NODE is
9619 the GNAT node conveying the source location for which the error should
9623 build_binary_op_trapv (enum tree_code code
, tree gnu_type
, tree left
,
9624 tree right
, Node_Id gnat_node
)
9626 const unsigned int precision
= TYPE_PRECISION (gnu_type
);
9627 tree lhs
= gnat_protect_expr (left
);
9628 tree rhs
= gnat_protect_expr (right
);
9629 tree type_max
= TYPE_MAX_VALUE (gnu_type
);
9630 tree type_min
= TYPE_MIN_VALUE (gnu_type
);
9631 tree gnu_expr
, check
;
9634 /* Assert that the precision is a power of 2. */
9635 gcc_assert ((precision
& (precision
- 1)) == 0);
9637 /* Prefer a constant on the RHS to simplify checks. */
9638 if (TREE_CODE (rhs
) != INTEGER_CST
9639 && TREE_CODE (lhs
) == INTEGER_CST
9640 && (code
== PLUS_EXPR
|| code
== MULT_EXPR
))
9647 gnu_expr
= build_binary_op (code
, gnu_type
, lhs
, rhs
);
9649 /* If we can fold the expression to a constant, just return it.
9650 The caller will deal with overflow, no need to generate a check. */
9651 if (TREE_CODE (gnu_expr
) == INTEGER_CST
)
9654 /* If no operand is a constant, we use the generic implementation. */
9655 if (TREE_CODE (lhs
) != INTEGER_CST
&& TREE_CODE (rhs
) != INTEGER_CST
)
9657 /* First convert the operands to the result type like build_binary_op.
9658 This is where the bias is made explicit for biased types. */
9659 lhs
= convert (gnu_type
, lhs
);
9660 rhs
= convert (gnu_type
, rhs
);
9662 /* Never inline a 64-bit mult for a 32-bit target, it's way too long. */
9663 if (code
== MULT_EXPR
&& precision
== 64 && BITS_PER_WORD
< 64)
9665 tree int64
= gnat_type_for_size (64, 0);
9666 Check_Restriction_No_Dependence_On_System (Name_Arith_64
, gnat_node
);
9667 return convert (gnu_type
, build_call_n_expr (mulv64_decl
, 2,
9668 convert (int64
, lhs
),
9669 convert (int64
, rhs
)));
9672 /* Likewise for a 128-bit mult and a 64-bit target. */
9673 else if (code
== MULT_EXPR
&& precision
== 128 && BITS_PER_WORD
< 128)
9675 tree int128
= gnat_type_for_size (128, 0);
9676 Check_Restriction_No_Dependence_On_System (Name_Arith_128
, gnat_node
);
9677 return convert (gnu_type
, build_call_n_expr (mulv128_decl
, 2,
9678 convert (int128
, lhs
),
9679 convert (int128
, rhs
)));
9682 enum internal_fn icode
;
9687 icode
= IFN_ADD_OVERFLOW
;
9690 icode
= IFN_SUB_OVERFLOW
;
9693 icode
= IFN_MUL_OVERFLOW
;
9699 tree gnu_ctype
= build_complex_type (gnu_type
);
9701 = build_call_expr_internal_loc (UNKNOWN_LOCATION
, icode
, gnu_ctype
, 2,
9703 tree tgt
= save_expr (call
);
9704 gnu_expr
= build1 (REALPART_EXPR
, gnu_type
, tgt
);
9705 check
= fold_build2 (NE_EXPR
, boolean_type_node
,
9706 build1 (IMAGPART_EXPR
, gnu_type
, tgt
),
9707 build_int_cst (gnu_type
, 0));
9709 emit_check (check
, gnu_expr
, CE_Overflow_Check_Failed
, gnat_node
);
9712 /* If one operand is a constant, we expose the overflow condition to enable
9713 a subsequent simplication or even elimination. */
9717 sgn
= tree_int_cst_sgn (rhs
);
9719 /* When rhs > 0, overflow when lhs > type_max - rhs. */
9720 check
= build_binary_op (GT_EXPR
, boolean_type_node
, lhs
,
9721 build_binary_op (MINUS_EXPR
, gnu_type
,
9724 /* When rhs < 0, overflow when lhs < type_min - rhs. */
9725 check
= build_binary_op (LT_EXPR
, boolean_type_node
, lhs
,
9726 build_binary_op (MINUS_EXPR
, gnu_type
,
9733 if (TREE_CODE (lhs
) == INTEGER_CST
)
9735 sgn
= tree_int_cst_sgn (lhs
);
9737 /* When lhs > 0, overflow when rhs < lhs - type_max. */
9738 check
= build_binary_op (LT_EXPR
, boolean_type_node
, rhs
,
9739 build_binary_op (MINUS_EXPR
, gnu_type
,
9742 /* When lhs < 0, overflow when rhs > lhs - type_min. */
9743 check
= build_binary_op (GT_EXPR
, boolean_type_node
, rhs
,
9744 build_binary_op (MINUS_EXPR
, gnu_type
,
9751 sgn
= tree_int_cst_sgn (rhs
);
9753 /* When rhs > 0, overflow when lhs < type_min + rhs. */
9754 check
= build_binary_op (LT_EXPR
, boolean_type_node
, lhs
,
9755 build_binary_op (PLUS_EXPR
, gnu_type
,
9758 /* When rhs < 0, overflow when lhs > type_max + rhs. */
9759 check
= build_binary_op (GT_EXPR
, boolean_type_node
, lhs
,
9760 build_binary_op (PLUS_EXPR
, gnu_type
,
9768 sgn
= tree_int_cst_sgn (rhs
);
9771 if (integer_onep (rhs
))
9774 tree lb
= build_binary_op (TRUNC_DIV_EXPR
, gnu_type
, type_min
, rhs
);
9775 tree ub
= build_binary_op (TRUNC_DIV_EXPR
, gnu_type
, type_max
, rhs
);
9777 /* When rhs > 1, overflow outside [type_min/rhs; type_max/rhs]. */
9779 = build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
9780 build_binary_op (LT_EXPR
, boolean_type_node
,
9782 build_binary_op (GT_EXPR
, boolean_type_node
,
9787 tree lb
= build_binary_op (TRUNC_DIV_EXPR
, gnu_type
, type_max
, rhs
);
9788 tree ub
= build_binary_op (TRUNC_DIV_EXPR
, gnu_type
, type_min
, rhs
);
9790 if (integer_minus_onep (rhs
))
9791 /* When rhs == -1, overflow if lhs == type_min. */
9793 = build_binary_op (EQ_EXPR
, boolean_type_node
, lhs
, type_min
);
9795 /* When rhs < -1, overflow outside [type_max/rhs; type_min/rhs]. */
9797 = build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
9798 build_binary_op (LT_EXPR
, boolean_type_node
,
9800 build_binary_op (GT_EXPR
, boolean_type_node
,
9811 return emit_check (check
, gnu_expr
, CE_Overflow_Check_Failed
, gnat_node
);
9814 /* GNU_COND contains the condition corresponding to an index, overflow or
9815 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR
9816 if GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
9817 REASON is the code that says why the exception is raised. GNAT_NODE is
9818 the node conveying the source location for which the error should be
9821 We used to propagate TREE_SIDE_EFFECTS from GNU_EXPR to the COND_EXPR,
9822 overwriting the setting inherited from the call statement, on the ground
9823 that the expression need not be evaluated just for the check. However
9824 that's incorrect because, in the GCC type system, its value is presumed
9825 to be valid so its comparison against the type bounds always yields true
9826 and, therefore, could be done without evaluating it; given that it can
9827 be a computation that overflows the bounds, the language may require the
9828 check to fail and thus the expression to be evaluated in this case. */
9831 emit_check (tree gnu_cond
, tree gnu_expr
, int reason
, Node_Id gnat_node
)
9834 = build_call_raise (reason
, gnat_node
, N_Raise_Constraint_Error
);
9836 fold_build3 (COND_EXPR
, TREE_TYPE (gnu_expr
), gnu_cond
,
9837 build2 (COMPOUND_EXPR
, TREE_TYPE (gnu_expr
), gnu_call
,
9838 SCALAR_FLOAT_TYPE_P (TREE_TYPE (gnu_expr
))
9839 ? build_real (TREE_TYPE (gnu_expr
), dconst0
)
9840 : build_int_cst (TREE_TYPE (gnu_expr
), 0)),
9844 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
9845 checks if OVERFLOW_P is true. If TRUNCATE_P is true, do a fp-to-integer
9846 conversion with truncation, otherwise round. GNAT_NODE is the GNAT node
9847 conveying the source location for which the error should be signaled. */
9850 convert_with_check (Entity_Id gnat_type
, tree gnu_expr
, bool overflow_p
,
9851 bool truncate_p
, Node_Id gnat_node
)
9853 tree gnu_type
= get_unpadded_type (gnat_type
);
9854 tree gnu_base_type
= get_base_type (gnu_type
);
9855 tree gnu_in_type
= TREE_TYPE (gnu_expr
);
9856 tree gnu_in_base_type
= get_base_type (gnu_in_type
);
9857 tree gnu_result
= gnu_expr
;
9859 /* If we are not doing any checks, the output is an integral type and the
9860 input is not a floating-point type, just do the conversion. This is
9861 required for packed array types and is simpler in all cases anyway. */
9863 && INTEGRAL_TYPE_P (gnu_base_type
)
9864 && !FLOAT_TYPE_P (gnu_in_base_type
))
9865 return convert (gnu_type
, gnu_expr
);
9867 /* If the mode of the input base type is larger, then converting to it below
9868 may pessimize the final conversion step, for example generate a libcall
9869 instead of a simple instruction, so use a narrower type in this case. */
9870 if (TYPE_MODE (gnu_in_base_type
) != TYPE_MODE (gnu_in_type
)
9871 && !(TREE_CODE (gnu_in_type
) == INTEGER_TYPE
9872 && TYPE_BIASED_REPRESENTATION_P (gnu_in_type
)))
9873 gnu_in_base_type
= gnat_type_for_mode (TYPE_MODE (gnu_in_type
),
9874 TYPE_UNSIGNED (gnu_in_type
));
9876 /* First convert the expression to the base type. This will never generate
9877 code, but makes the tests below simpler. But don't do this if converting
9878 from an integer type to an unconstrained array type since then we need to
9879 get the bounds from the original (unpacked) type. */
9880 if (TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
)
9881 gnu_result
= convert (gnu_in_base_type
, gnu_result
);
9883 /* If overflow checks are requested, we need to be sure the result will fit
9884 in the output base type. But don't do this if the input is integer and
9885 the output floating-point. */
9887 && !(FLOAT_TYPE_P (gnu_base_type
) && INTEGRAL_TYPE_P (gnu_in_base_type
)))
9889 /* Ensure GNU_EXPR only gets evaluated once. */
9890 tree gnu_input
= gnat_protect_expr (gnu_result
);
9891 tree gnu_cond
= boolean_false_node
;
9892 tree gnu_in_lb
= TYPE_MIN_VALUE (gnu_in_base_type
);
9893 tree gnu_in_ub
= TYPE_MAX_VALUE (gnu_in_base_type
);
9894 tree gnu_out_lb
= TYPE_MIN_VALUE (gnu_base_type
);
9896 = (TREE_CODE (gnu_base_type
) == INTEGER_TYPE
9897 && TYPE_MODULAR_P (gnu_base_type
))
9898 ? fold_build2 (MINUS_EXPR
, gnu_base_type
,
9899 TYPE_MODULUS (gnu_base_type
),
9900 build_int_cst (gnu_base_type
, 1))
9901 : TYPE_MAX_VALUE (gnu_base_type
);
9903 /* Convert the lower bounds to signed types, so we're sure we're
9904 comparing them properly. Likewise, convert the upper bounds
9905 to unsigned types. */
9906 if (INTEGRAL_TYPE_P (gnu_in_base_type
)
9907 && TYPE_UNSIGNED (gnu_in_base_type
))
9909 = convert (gnat_signed_type_for (gnu_in_base_type
), gnu_in_lb
);
9911 if (INTEGRAL_TYPE_P (gnu_in_base_type
)
9912 && !TYPE_UNSIGNED (gnu_in_base_type
))
9914 = convert (gnat_unsigned_type_for (gnu_in_base_type
), gnu_in_ub
);
9916 if (INTEGRAL_TYPE_P (gnu_base_type
) && TYPE_UNSIGNED (gnu_base_type
))
9918 = convert (gnat_signed_type_for (gnu_base_type
), gnu_out_lb
);
9920 if (INTEGRAL_TYPE_P (gnu_base_type
) && !TYPE_UNSIGNED (gnu_base_type
))
9922 = convert (gnat_unsigned_type_for (gnu_base_type
), gnu_out_ub
);
9924 /* Check each bound separately and only if the result bound
9925 is tighter than the bound on the input type. Note that all the
9926 types are base types, so the bounds must be constant. Also,
9927 the comparison is done in the base type of the input, which
9928 always has the proper signedness. First check for input
9929 integer (which means output integer), output float (which means
9930 both float), or mixed, in which case we always compare.
9931 Note that we have to do the comparison which would *fail* in the
9932 case of an error since if it's an FP comparison and one of the
9933 values is a NaN or Inf, the comparison will fail. */
9934 if (INTEGRAL_TYPE_P (gnu_in_base_type
)
9935 ? tree_int_cst_lt (gnu_in_lb
, gnu_out_lb
)
9936 : (FLOAT_TYPE_P (gnu_base_type
)
9937 ? real_less (&TREE_REAL_CST (gnu_in_lb
),
9938 &TREE_REAL_CST (gnu_out_lb
))
9942 (build_binary_op (GE_EXPR
, boolean_type_node
,
9943 gnu_input
, convert (gnu_in_base_type
,
9946 if (INTEGRAL_TYPE_P (gnu_in_base_type
)
9947 ? tree_int_cst_lt (gnu_out_ub
, gnu_in_ub
)
9948 : (FLOAT_TYPE_P (gnu_base_type
)
9949 ? real_less (&TREE_REAL_CST (gnu_out_ub
),
9950 &TREE_REAL_CST (gnu_in_ub
))
9953 = build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
, gnu_cond
,
9955 (build_binary_op (LE_EXPR
, boolean_type_node
,
9957 convert (gnu_in_base_type
,
9960 if (!integer_zerop (gnu_cond
))
9961 gnu_result
= emit_check (gnu_cond
, gnu_input
,
9962 CE_Overflow_Check_Failed
, gnat_node
);
9965 /* Now convert to the result base type. If this is a non-truncating
9966 float-to-integer conversion, round. */
9967 if (INTEGRAL_TYPE_P (gnu_base_type
)
9968 && FLOAT_TYPE_P (gnu_in_base_type
)
9971 REAL_VALUE_TYPE half_minus_pred_half
, pred_half
;
9972 tree gnu_conv
, gnu_zero
, gnu_comp
, calc_type
;
9973 tree gnu_pred_half
, gnu_add_pred_half
, gnu_subtract_pred_half
;
9974 const struct real_format
*fmt
;
9976 /* The following calculations depend on proper rounding to even
9977 of each arithmetic operation. In order to prevent excess
9978 precision from spoiling this property, use the widest hardware
9979 floating-point type if FP_ARITH_MAY_WIDEN is true. */
9981 = fp_arith_may_widen
? longest_float_type_node
: gnu_in_base_type
;
9983 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
9984 fmt
= REAL_MODE_FORMAT (TYPE_MODE (calc_type
));
9985 real_2expN (&half_minus_pred_half
, -(fmt
->p
) - 1, TYPE_MODE (calc_type
));
9986 real_arithmetic (&pred_half
, MINUS_EXPR
, &dconsthalf
,
9987 &half_minus_pred_half
);
9988 gnu_pred_half
= build_real (calc_type
, pred_half
);
9990 /* If the input is strictly negative, subtract this value
9991 and otherwise add it from the input. For 0.5, the result
9992 is exactly between 1.0 and the machine number preceding 1.0
9993 (for calc_type). Since the last bit of 1.0 is even, this 0.5
9994 will round to 1.0, while all other number with an absolute
9995 value less than 0.5 round to 0.0. For larger numbers exactly
9996 halfway between integers, rounding will always be correct as
9997 the true mathematical result will be closer to the higher
9998 integer compared to the lower one. So, this constant works
9999 for all floating-point numbers.
10001 The reason to use the same constant with subtract/add instead
10002 of a positive and negative constant is to allow the comparison
10003 to be scheduled in parallel with retrieval of the constant and
10004 conversion of the input to the calc_type (if necessary). */
10006 gnu_zero
= build_real (gnu_in_base_type
, dconst0
);
10007 gnu_result
= gnat_protect_expr (gnu_result
);
10008 gnu_conv
= convert (calc_type
, gnu_result
);
10010 = fold_build2 (GE_EXPR
, boolean_type_node
, gnu_result
, gnu_zero
);
10012 = fold_build2 (PLUS_EXPR
, calc_type
, gnu_conv
, gnu_pred_half
);
10013 gnu_subtract_pred_half
10014 = fold_build2 (MINUS_EXPR
, calc_type
, gnu_conv
, gnu_pred_half
);
10015 gnu_result
= fold_build3 (COND_EXPR
, calc_type
, gnu_comp
,
10016 gnu_add_pred_half
, gnu_subtract_pred_half
);
10019 if (TREE_CODE (gnu_base_type
) == INTEGER_TYPE
10020 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type
)
10021 && TREE_CODE (gnu_result
) == UNCONSTRAINED_ARRAY_REF
)
10022 gnu_result
= unchecked_convert (gnu_base_type
, gnu_result
, false);
10024 gnu_result
= convert (gnu_base_type
, gnu_result
);
10026 /* If this is a conversion between an integer type larger than a word and a
10027 floating-point type, then declare the dependence on the libgcc routine. */
10028 if ((INTEGRAL_TYPE_P (gnu_in_base_type
)
10029 && TYPE_PRECISION (gnu_in_base_type
) > BITS_PER_WORD
10030 && FLOAT_TYPE_P (gnu_base_type
))
10031 || (FLOAT_TYPE_P (gnu_in_base_type
)
10032 && INTEGRAL_TYPE_P (gnu_base_type
)
10033 && TYPE_PRECISION (gnu_base_type
) > BITS_PER_WORD
))
10034 Check_Restriction_No_Dependence_On_System (Name_Gcc
, gnat_node
);
10036 return convert (gnu_type
, gnu_result
);
10039 /* Return true if GNU_EXPR can be directly addressed. This is the case
10040 unless it is an expression involving computation or if it involves a
10041 reference to a bitfield or to an object not sufficiently aligned for
10042 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
10043 be directly addressed as an object of this type.
10045 *** Notes on addressability issues in the Ada compiler ***
10047 This predicate is necessary in order to bridge the gap between Gigi
10048 and the middle-end about addressability of GENERIC trees. A tree
10049 is said to be addressable if it can be directly addressed, i.e. if
10050 its address can be taken, is a multiple of the type's alignment on
10051 strict-alignment architectures and returns the first storage unit
10052 assigned to the object represented by the tree.
10054 In the C family of languages, everything is in practice addressable
10055 at the language level, except for bit-fields. This means that these
10056 compilers will take the address of any tree that doesn't represent
10057 a bit-field reference and expect the result to be the first storage
10058 unit assigned to the object. Even in cases where this will result
10059 in unaligned accesses at run time, nothing is supposed to be done
10060 and the program is considered as erroneous instead (see PR c/18287).
10062 The implicit assumptions made in the middle-end are in keeping with
10063 the C viewpoint described above:
10064 - the address of a bit-field reference is supposed to be never
10065 taken; the compiler (generally) will stop on such a construct,
10066 - any other tree is addressable if it is formally addressable,
10067 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
10069 In Ada, the viewpoint is the opposite one: nothing is addressable
10070 at the language level unless explicitly declared so. This means
10071 that the compiler will both make sure that the trees representing
10072 references to addressable ("aliased" in Ada parlance) objects are
10073 addressable and make no real attempts at ensuring that the trees
10074 representing references to non-addressable objects are addressable.
10076 In the first case, Ada is effectively equivalent to C and handing
10077 down the direct result of applying ADDR_EXPR to these trees to the
10078 middle-end works flawlessly. In the second case, Ada cannot afford
10079 to consider the program as erroneous if the address of trees that
10080 are not addressable is requested for technical reasons, unlike C;
10081 as a consequence, the Ada compiler must arrange for either making
10082 sure that this address is not requested in the middle-end or for
10083 compensating by inserting temporaries if it is requested in Gigi.
10085 The first goal can be achieved because the middle-end should not
10086 request the address of non-addressable trees on its own; the only
10087 exception is for the invocation of low-level block operations like
10088 memcpy, for which the addressability requirements are lower since
10089 the type's alignment can be disregarded. In practice, this means
10090 that Gigi must make sure that such operations cannot be applied to
10091 non-BLKmode bit-fields.
10093 The second goal is achieved by means of the addressable_p predicate,
10094 which computes whether a temporary must be inserted by Gigi when the
10095 address of a tree is requested; if so, the address of the temporary
10096 will be used in lieu of that of the original tree and some glue code
10097 generated to connect everything together. */
10100 addressable_p (tree gnu_expr
, tree gnu_type
)
10102 /* For an integral type, the size of the actual type of the object may not
10103 be greater than that of the expected type, otherwise an indirect access
10104 in the latter type wouldn't correctly set all the bits of the object. */
10106 && INTEGRAL_TYPE_P (gnu_type
)
10107 && smaller_form_type_p (gnu_type
, TREE_TYPE (gnu_expr
)))
10110 /* The size of the actual type of the object may not be smaller than that
10111 of the expected type, otherwise an indirect access in the latter type
10112 would be larger than the object. But only record types need to be
10113 considered in practice for this case. */
10115 && TREE_CODE (gnu_type
) == RECORD_TYPE
10116 && smaller_form_type_p (TREE_TYPE (gnu_expr
), gnu_type
))
10119 switch (TREE_CODE (gnu_expr
))
10123 case FUNCTION_DECL
:
10125 /* All DECLs are addressable: if they are in a register, we can force
10129 case UNCONSTRAINED_ARRAY_REF
:
10131 /* Taking the address of a dereference yields the original pointer. */
10137 /* Taking the address yields a pointer to the constant pool. */
10141 /* Taking the address of a static constructor yields a pointer to the
10142 tree constant pool. */
10143 return TREE_STATIC (gnu_expr
) ? true : false;
10155 /* All rvalues are deemed addressable since taking their address will
10156 force a temporary to be created by the middle-end. */
10159 case COMPOUND_EXPR
:
10160 /* The address of a compound expression is that of its 2nd operand. */
10161 return addressable_p (TREE_OPERAND (gnu_expr
, 1), gnu_type
);
10164 /* We accept &COND_EXPR as soon as both operands are addressable and
10165 expect the outcome to be the address of the selected operand. */
10166 return (addressable_p (TREE_OPERAND (gnu_expr
, 1), NULL_TREE
)
10167 && addressable_p (TREE_OPERAND (gnu_expr
, 2), NULL_TREE
));
10169 case COMPONENT_REF
:
10170 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr
, 1))
10171 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
10172 the field is sufficiently aligned, in case it is subject
10173 to a pragma Component_Alignment. But we don't need to
10174 check the alignment of the containing record, as it is
10175 guaranteed to be not smaller than that of its most
10176 aligned field that is not a bit-field. */
10177 && (!STRICT_ALIGNMENT
10178 || DECL_ALIGN (TREE_OPERAND (gnu_expr
, 1))
10179 >= TYPE_ALIGN (TREE_TYPE (gnu_expr
))))
10180 /* The field of a padding record is always addressable. */
10181 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0))))
10182 && addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
));
10184 case ARRAY_REF
: case ARRAY_RANGE_REF
:
10185 case REALPART_EXPR
: case IMAGPART_EXPR
:
10187 return addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
);
10190 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr
))
10191 && addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
));
10193 case VIEW_CONVERT_EXPR
:
10195 /* This is addressable if we can avoid a copy. */
10196 tree type
= TREE_TYPE (gnu_expr
);
10197 tree inner_type
= TREE_TYPE (TREE_OPERAND (gnu_expr
, 0));
10198 return (((TYPE_MODE (type
) == TYPE_MODE (inner_type
)
10199 && (!STRICT_ALIGNMENT
10200 || TYPE_ALIGN (type
) <= TYPE_ALIGN (inner_type
)
10201 || TYPE_ALIGN (inner_type
) >= BIGGEST_ALIGNMENT
))
10202 || ((TYPE_MODE (type
) == BLKmode
10203 || TYPE_MODE (inner_type
) == BLKmode
)
10204 && (!STRICT_ALIGNMENT
10205 || TYPE_ALIGN (type
) <= TYPE_ALIGN (inner_type
)
10206 || TYPE_ALIGN (inner_type
) >= BIGGEST_ALIGNMENT
10207 || TYPE_ALIGN_OK (type
)
10208 || TYPE_ALIGN_OK (inner_type
))))
10209 && addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
));
10217 /* Do the processing for the declaration of a GNAT_ENTITY, a type or subtype.
10218 If a Freeze node exists for the entity, delay the bulk of the processing.
10219 Otherwise make a GCC type for GNAT_ENTITY and set up the correspondence. */
10222 process_type (Entity_Id gnat_entity
)
10225 = present_gnu_tree (gnat_entity
) ? get_gnu_tree (gnat_entity
) : NULL_TREE
;
10227 /* If we are to delay elaboration of this type, just do any elaboration
10228 needed for expressions within the declaration and make a dummy node
10229 for it and its Full_View (if any), in case something points to it.
10230 Do not do this if it has already been done (the only way that can
10231 happen is if the private completion is also delayed). */
10232 if (Present (Freeze_Node (gnat_entity
)))
10234 elaborate_entity (gnat_entity
);
10238 tree gnu_decl
= TYPE_STUB_DECL (make_dummy_type (gnat_entity
));
10239 save_gnu_tree (gnat_entity
, gnu_decl
, false);
10240 if (Is_Incomplete_Or_Private_Type (gnat_entity
)
10241 && Present (Full_View (gnat_entity
)))
10243 if (Has_Completion_In_Body (gnat_entity
))
10244 DECL_TAFT_TYPE_P (gnu_decl
) = 1;
10245 save_gnu_tree (Full_View (gnat_entity
), gnu_decl
, false);
10252 /* If we saved away a dummy type for this node, it means that this made the
10253 type that corresponds to the full type of an incomplete type. Clear that
10254 type for now and then update the type in the pointers below. But, if the
10255 saved type is not dummy, it very likely means that we have a use before
10256 declaration for the type in the tree, what we really cannot handle. */
10259 gcc_assert (TREE_CODE (gnu_old
) == TYPE_DECL
10260 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old
)));
10262 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
10265 /* Now fully elaborate the type. */
10266 tree gnu_new
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, true);
10267 gcc_assert (TREE_CODE (gnu_new
) == TYPE_DECL
);
10269 /* If we have an old type and we've made pointers to this type, update those
10270 pointers. If this is a Taft amendment type in the main unit, we need to
10271 mark the type as used since other units referencing it don't see the full
10272 declaration and, therefore, cannot mark it as used themselves. */
10275 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old
)),
10276 TREE_TYPE (gnu_new
));
10277 if (DECL_TAFT_TYPE_P (gnu_old
))
10278 used_types_insert (TREE_TYPE (gnu_new
));
10281 /* If this is a record type corresponding to a task or protected type
10282 that is a completion of an incomplete type, perform a similar update
10283 on the type. ??? Including protected types here is a guess. */
10284 if (Is_Record_Type (gnat_entity
)
10285 && Is_Concurrent_Record_Type (gnat_entity
)
10286 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
)))
10289 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
));
10291 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
),
10293 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
),
10296 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old
)),
10297 TREE_TYPE (gnu_new
));
10301 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
10302 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting of the
10303 associations that are from RECORD_TYPE. If we see an internal record, make
10304 a recursive call to fill it in as well. */
10307 extract_values (tree values
, tree record_type
)
10309 vec
<constructor_elt
, va_gc
> *v
= NULL
;
10312 for (field
= TYPE_FIELDS (record_type
); field
; field
= DECL_CHAIN (field
))
10314 tree tem
, value
= NULL_TREE
;
10316 /* _Parent is an internal field, but may have values in the aggregate,
10317 so check for values first. */
10318 if ((tem
= purpose_member (field
, values
)))
10320 value
= TREE_VALUE (tem
);
10321 TREE_ADDRESSABLE (tem
) = 1;
10324 else if (DECL_INTERNAL_P (field
))
10326 value
= extract_values (values
, TREE_TYPE (field
));
10327 if (TREE_CODE (value
) == CONSTRUCTOR
10328 && vec_safe_is_empty (CONSTRUCTOR_ELTS (value
)))
10332 /* If we have a record subtype, the names will match, but not the
10333 actual FIELD_DECLs. */
10334 for (tem
= values
; tem
; tem
= TREE_CHAIN (tem
))
10335 if (DECL_NAME (TREE_PURPOSE (tem
)) == DECL_NAME (field
))
10337 value
= convert (TREE_TYPE (field
), TREE_VALUE (tem
));
10338 TREE_ADDRESSABLE (tem
) = 1;
10344 CONSTRUCTOR_APPEND_ELT (v
, field
, value
);
10347 return gnat_build_constructor (record_type
, v
);
10350 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
10351 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
10352 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
10355 assoc_to_constructor (Entity_Id gnat_entity
, Node_Id gnat_assoc
, tree gnu_type
)
10357 tree gnu_list
= NULL_TREE
, gnu_result
;
10359 /* We test for GNU_FIELD being empty in the case where a variant
10360 was the last thing since we don't take things off GNAT_ASSOC in
10361 that case. We check GNAT_ASSOC in case we have a variant, but it
10364 for (; Present (gnat_assoc
); gnat_assoc
= Next (gnat_assoc
))
10366 const Node_Id gnat_field
= First (Choices (gnat_assoc
));
10367 const Node_Id gnat_expr
= Expression (gnat_assoc
);
10368 tree gnu_field
= gnat_to_gnu_field_decl (Entity (gnat_field
));
10369 tree gnu_expr
= gnat_to_gnu (Expression (gnat_assoc
));
10371 /* The expander is supposed to put a single component selector name
10372 in every record component association. */
10373 gcc_assert (No (Next (gnat_field
)));
10375 /* Ignore discriminants that have Corresponding_Discriminants in tagged
10376 types since we'll be setting those fields in the parent subtype. */
10377 if (Ekind (Entity (gnat_field
)) == E_Discriminant
10378 && Present (Corresponding_Discriminant (Entity (gnat_field
)))
10379 && Is_Tagged_Type (Scope (Entity (gnat_field
))))
10382 /* Also ignore discriminants of Unchecked_Unions. */
10383 if (Ekind (Entity (gnat_field
)) == E_Discriminant
10384 && Is_Unchecked_Union (gnat_entity
))
10387 gigi_checking_assert (!Do_Range_Check (gnat_expr
));
10389 /* Convert to the type of the field. */
10390 gnu_expr
= convert (TREE_TYPE (gnu_field
), gnu_expr
);
10392 /* Add the field and expression to the list. */
10393 gnu_list
= tree_cons (gnu_field
, gnu_expr
, gnu_list
);
10396 gnu_result
= extract_values (gnu_list
, gnu_type
);
10400 /* Verify that every entry in GNU_LIST was used. */
10401 for (; gnu_list
; gnu_list
= TREE_CHAIN (gnu_list
))
10402 gcc_assert (TREE_ADDRESSABLE (gnu_list
));
10408 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
10409 the first element of an array aggregate. It may itself be an aggregate.
10410 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate. */
10413 pos_to_constructor (Node_Id gnat_expr
, tree gnu_array_type
)
10415 tree gnu_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type
));
10416 vec
<constructor_elt
, va_gc
> *gnu_expr_vec
= NULL
;
10418 for (; Present (gnat_expr
); gnat_expr
= Next (gnat_expr
))
10422 /* If the expression is itself an array aggregate then first build the
10423 innermost constructor if it is part of our array (multi-dimensional
10425 if (Nkind (gnat_expr
) == N_Aggregate
10426 && TREE_CODE (TREE_TYPE (gnu_array_type
)) == ARRAY_TYPE
10427 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type
)))
10428 gnu_expr
= pos_to_constructor (First (Expressions (gnat_expr
)),
10429 TREE_TYPE (gnu_array_type
));
10432 /* If the expression is a conversion to an unconstrained array type,
10433 skip it to avoid spilling to memory. */
10434 if (Nkind (gnat_expr
) == N_Type_Conversion
10435 && Is_Array_Type (Etype (gnat_expr
))
10436 && !Is_Constrained (Etype (gnat_expr
)))
10437 gnu_expr
= gnat_to_gnu (Expression (gnat_expr
));
10439 gnu_expr
= gnat_to_gnu (gnat_expr
);
10441 gigi_checking_assert (!Do_Range_Check (gnat_expr
));
10444 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec
, gnu_index
,
10445 convert (TREE_TYPE (gnu_array_type
), gnu_expr
));
10447 gnu_index
= int_const_binop (PLUS_EXPR
, gnu_index
,
10448 convert (TREE_TYPE (gnu_index
),
10449 integer_one_node
));
10452 return gnat_build_constructor (gnu_array_type
, gnu_expr_vec
);
10455 /* Process a N_Validate_Unchecked_Conversion node. */
10458 validate_unchecked_conversion (Node_Id gnat_node
)
10460 tree gnu_source_type
= gnat_to_gnu_type (Source_Type (gnat_node
));
10461 tree gnu_target_type
= gnat_to_gnu_type (Target_Type (gnat_node
));
10463 /* If the target is a pointer type, see if we are either converting from a
10464 non-pointer or from a pointer to a type with a different alias set and
10465 warn if so, unless the pointer has been marked to alias everything. */
10466 if (POINTER_TYPE_P (gnu_target_type
)
10467 && !TYPE_REF_CAN_ALIAS_ALL (gnu_target_type
))
10469 tree gnu_source_desig_type
= POINTER_TYPE_P (gnu_source_type
)
10470 ? TREE_TYPE (gnu_source_type
)
10472 tree gnu_target_desig_type
= TREE_TYPE (gnu_target_type
);
10473 alias_set_type target_alias_set
= get_alias_set (gnu_target_desig_type
);
10475 if (target_alias_set
!= 0
10476 && (!POINTER_TYPE_P (gnu_source_type
)
10477 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type
),
10478 target_alias_set
)))
10480 post_error_ne ("??possible aliasing problem for type&",
10481 gnat_node
, Target_Type (gnat_node
));
10482 post_error ("\\?use -fno-strict-aliasing switch for references",
10484 post_error_ne ("\\?or use `pragma No_Strict_Aliasing (&);`",
10485 gnat_node
, Target_Type (gnat_node
));
10489 /* Likewise if the target is a fat pointer type, but we have no mechanism to
10490 mitigate the problem in this case, so we unconditionally warn. */
10491 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type
))
10493 tree gnu_source_desig_type
10494 = TYPE_IS_FAT_POINTER_P (gnu_source_type
)
10495 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type
)))
10497 tree gnu_target_desig_type
10498 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type
)));
10499 alias_set_type target_alias_set
= get_alias_set (gnu_target_desig_type
);
10501 if (target_alias_set
!= 0
10502 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type
)
10503 || !alias_sets_conflict_p (get_alias_set (gnu_source_desig_type
),
10504 target_alias_set
)))
10506 post_error_ne ("??possible aliasing problem for type&",
10507 gnat_node
, Target_Type (gnat_node
));
10508 post_error ("\\?use -fno-strict-aliasing switch for references",
10514 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a
10515 source code location and false if it doesn't. If CLEAR_COLUMN is
10516 true, set the column information to 0. If DECL is given and SLOC
10517 refers to a File with an instance, map DECL to that instance. */
10520 Sloc_to_locus (Source_Ptr Sloc
, location_t
*locus
, bool clear_column
,
10523 if (Sloc
== No_Location
)
10526 if (Sloc
<= Standard_Location
)
10528 *locus
= BUILTINS_LOCATION
;
10532 Source_File_Index file
= Get_Source_File_Index (Sloc
);
10533 Line_Number_Type line
= Get_Logical_Line_Number (Sloc
);
10534 Column_Number_Type column
= (clear_column
? 0 : Get_Column_Number (Sloc
));
10535 line_map_ordinary
*map
= LINEMAPS_ORDINARY_MAP_AT (line_table
, file
- 1);
10537 /* We can have zero if pragma Source_Reference is in effect. */
10541 /* Translate the location. */
10543 = linemap_position_for_line_and_column (line_table
, map
, line
, column
);
10545 if (decl
&& file_map
&& file_map
[file
- 1].Instance
)
10546 decl_to_instance_map
->put (decl
, file_map
[file
- 1].Instance
);
10551 /* Return whether GNAT_NODE is a defining identifier for a renaming that comes
10552 from the parameter association for the instantiation of a generic. We do
10553 not want to emit source location for them: the code generated for their
10554 initialization is likely to disturb debugging. */
10557 renaming_from_instantiation_p (Node_Id gnat_node
)
10559 if (Nkind (gnat_node
) != N_Defining_Identifier
10560 || !Is_Object (gnat_node
)
10561 || Comes_From_Source (gnat_node
)
10562 || !Present (Renamed_Object (gnat_node
)))
10565 /* Get the object declaration of the renamed object, if any and if the
10566 renamed object is a mere identifier. */
10567 gnat_node
= Renamed_Object (gnat_node
);
10568 if (Nkind (gnat_node
) != N_Identifier
)
10571 gnat_node
= Parent (Entity (gnat_node
));
10572 return (Present (gnat_node
)
10573 && Nkind (gnat_node
) == N_Object_Declaration
10574 && Present (Corresponding_Generic_Association (gnat_node
)));
10577 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
10578 don't do anything if it doesn't correspond to a source location. And,
10579 if CLEAR_COLUMN is true, set the column information to 0. */
10582 set_expr_location_from_node (tree node
, Node_Id gnat_node
, bool clear_column
)
10586 /* Do not set a location for constructs likely to disturb debugging. */
10587 if (Nkind (gnat_node
) == N_Defining_Identifier
)
10589 if (Is_Type (gnat_node
) && Is_Actual_Subtype (gnat_node
))
10592 if (renaming_from_instantiation_p (gnat_node
))
10596 if (!Sloc_to_locus (Sloc (gnat_node
), &locus
, clear_column
))
10599 SET_EXPR_LOCATION (node
, locus
);
10602 /* More elaborate version of set_expr_location_from_node to be used in more
10603 general contexts, for example the result of the translation of a generic
10607 set_gnu_expr_location_from_node (tree node
, Node_Id gnat_node
)
10609 /* Set the location information on the node if it is a real expression.
10610 References can be reused for multiple GNAT nodes and they would get
10611 the location information of their last use. Also make sure not to
10612 overwrite an existing location as it is probably more precise. */
10614 switch (TREE_CODE (node
))
10617 case NON_LVALUE_EXPR
:
10621 case COMPOUND_EXPR
:
10622 if (EXPR_P (TREE_OPERAND (node
, 1)))
10623 set_gnu_expr_location_from_node (TREE_OPERAND (node
, 1), gnat_node
);
10625 /* ... fall through ... */
10628 if (!REFERENCE_CLASS_P (node
) && !EXPR_HAS_LOCATION (node
))
10630 set_expr_location_from_node (node
, gnat_node
);
10631 set_end_locus_from_node (node
, gnat_node
);
10637 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
10638 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
10639 most sense. Return true if a sensible assignment was performed. */
10642 set_end_locus_from_node (tree gnu_node
, Node_Id gnat_node
)
10644 Node_Id gnat_end_label
;
10645 location_t end_locus
;
10647 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
10648 end_locus when there is one. We consider only GNAT nodes with a possible
10649 End_Label attached. If the End_Label actually was unassigned, fallback
10650 on the original node. We'd better assign an explicit sloc associated with
10651 the outer construct in any case. */
10653 switch (Nkind (gnat_node
))
10655 case N_Package_Body
:
10656 case N_Subprogram_Body
:
10657 case N_Block_Statement
:
10658 if (Present (Handled_Statement_Sequence (gnat_node
)))
10659 gnat_end_label
= End_Label (Handled_Statement_Sequence (gnat_node
));
10661 gnat_end_label
= Empty
;
10664 case N_Package_Declaration
:
10665 gcc_checking_assert (Present (Specification (gnat_node
)));
10666 gnat_end_label
= End_Label (Specification (gnat_node
));
10673 if (Present (gnat_end_label
))
10674 gnat_node
= gnat_end_label
;
10676 /* Some expanded subprograms have neither an End_Label nor a Sloc
10677 attached. Notify that to callers. For a block statement with no
10678 End_Label, clear column information, so that the tree for a
10679 transient block does not receive the sloc of a source condition. */
10680 if (!Sloc_to_locus (Sloc (gnat_node
), &end_locus
,
10681 No (gnat_end_label
)
10682 && Nkind (gnat_node
) == N_Block_Statement
))
10685 switch (TREE_CODE (gnu_node
))
10688 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node
)) = end_locus
;
10691 case FUNCTION_DECL
:
10692 DECL_STRUCT_FUNCTION (gnu_node
)->function_end_locus
= end_locus
;
10700 /* Post an error message. MSG is the error message, properly annotated.
10701 NODE is the node at which to post the error and the node to use for the
10702 '&' substitution. */
10705 post_error (const char *msg
, Node_Id node
)
10707 String_Template temp
;
10713 temp
.Low_Bound
= 1;
10714 temp
.High_Bound
= strlen (msg
);
10717 Error_Msg_N (sp
, node
);
10720 /* Similar to post_error, but NODE is the node at which to post the error and
10721 ENT is the node to use for the '&' substitution. */
10724 post_error_ne (const char *msg
, Node_Id node
, Entity_Id ent
)
10726 String_Template temp
;
10732 temp
.Low_Bound
= 1;
10733 temp
.High_Bound
= strlen (msg
);
10736 Error_Msg_NE (sp
, node
, ent
);
10739 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
10742 post_error_ne_num (const char *msg
, Node_Id node
, Entity_Id ent
, int num
)
10744 Error_Msg_Uint_1
= UI_From_Int (num
);
10745 post_error_ne (msg
, node
, ent
);
10748 /* Similar to post_error_ne, but T is a GCC tree representing the number to
10749 write. If T represents a constant, the text inside curly brackets in
10750 MSG will be output (presumably including a '^'). Otherwise it will not
10751 be output and the text inside square brackets will be output instead. */
10754 post_error_ne_tree (const char *msg
, Node_Id node
, Entity_Id ent
, tree t
)
10756 char *new_msg
= XALLOCAVEC (char, strlen (msg
) + 1);
10757 char start_yes
, end_yes
, start_no
, end_no
;
10761 if (TREE_CODE (t
) == INTEGER_CST
)
10763 Error_Msg_Uint_1
= UI_From_gnu (t
);
10764 start_yes
= '{', end_yes
= '}', start_no
= '[', end_no
= ']';
10767 start_yes
= '[', end_yes
= ']', start_no
= '{', end_no
= '}';
10769 for (p
= msg
, q
= new_msg
; *p
; p
++)
10771 if (*p
== start_yes
)
10772 for (p
++; *p
!= end_yes
; p
++)
10774 else if (*p
== start_no
)
10775 for (p
++; *p
!= end_no
; p
++)
10783 post_error_ne (new_msg
, node
, ent
);
10786 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
10789 post_error_ne_tree_2 (const char *msg
, Node_Id node
, Entity_Id ent
, tree t
,
10792 Error_Msg_Uint_2
= UI_From_Int (num
);
10793 post_error_ne_tree (msg
, node
, ent
, t
);
10796 /* Return a label to branch to for the exception type in KIND or Empty
10800 get_exception_label (char kind
)
10804 case N_Raise_Constraint_Error
:
10805 return gnu_constraint_error_label_stack
.last ();
10807 case N_Raise_Storage_Error
:
10808 return gnu_storage_error_label_stack
.last ();
10810 case N_Raise_Program_Error
:
10811 return gnu_program_error_label_stack
.last ();
10817 gcc_unreachable ();
10820 /* Return the decl for the current elaboration procedure. */
10823 get_elaboration_procedure (void)
10825 return gnu_elab_proc_stack
->last ();
10828 /* Return the controlling type of a dispatching subprogram. */
10831 get_controlling_type (Entity_Id subprog
)
10833 /* This is modeled on Expand_Interface_Thunk. */
10834 Entity_Id controlling_type
= Etype (First_Formal (subprog
));
10835 if (Is_Access_Type (controlling_type
))
10836 controlling_type
= Directly_Designated_Type (controlling_type
);
10837 controlling_type
= Underlying_Type (controlling_type
);
10838 if (Is_Concurrent_Type (controlling_type
))
10839 controlling_type
= Corresponding_Record_Type (controlling_type
);
10840 controlling_type
= Base_Type (controlling_type
);
10841 return controlling_type
;
10844 /* Return whether we should use an alias for the TARGET of a thunk
10845 in order to make the call generated in the thunk local. */
10848 use_alias_for_thunk_p (tree target
)
10850 /* We cannot generate a local call in this case. */
10851 if (DECL_EXTERNAL (target
))
10854 /* The call is already local in this case. */
10855 if (TREE_CODE (DECL_CONTEXT (target
)) == FUNCTION_DECL
)
10858 return TARGET_USE_LOCAL_THUNK_ALIAS_P (target
);
10861 static GTY(()) unsigned long thunk_labelno
= 0;
10863 /* Create an alias for TARGET to be used as the target of a thunk. */
10866 make_alias_for_thunk (tree target
)
10869 targetm
.asm_out
.generate_internal_label (buf
, "LTHUNK", thunk_labelno
++);
10871 tree alias
= build_decl (DECL_SOURCE_LOCATION (target
), TREE_CODE (target
),
10872 get_identifier (buf
), TREE_TYPE (target
));
10874 DECL_LANG_SPECIFIC (alias
) = DECL_LANG_SPECIFIC (target
);
10875 DECL_CONTEXT (alias
) = DECL_CONTEXT (target
);
10876 TREE_READONLY (alias
) = TREE_READONLY (target
);
10877 TREE_THIS_VOLATILE (alias
) = TREE_THIS_VOLATILE (target
);
10878 DECL_ARTIFICIAL (alias
) = 1;
10879 DECL_INITIAL (alias
) = error_mark_node
;
10880 DECL_ARGUMENTS (alias
) = copy_list (DECL_ARGUMENTS (target
));
10881 TREE_ADDRESSABLE (alias
) = 1;
10882 SET_DECL_ASSEMBLER_NAME (alias
, DECL_NAME (alias
));
10884 cgraph_node
*n
= cgraph_node::create_same_body_alias (alias
, target
);
10890 /* Create the local covariant part of {GNAT,GNU}_THUNK. */
10893 make_covariant_thunk (Entity_Id gnat_thunk
, tree gnu_thunk
)
10895 tree gnu_name
= create_concat_name (gnat_thunk
, "CV");
10897 = build_decl (DECL_SOURCE_LOCATION (gnu_thunk
), TREE_CODE (gnu_thunk
),
10898 gnu_name
, TREE_TYPE (gnu_thunk
));
10900 DECL_ARGUMENTS (gnu_cv_thunk
) = copy_list (DECL_ARGUMENTS (gnu_thunk
));
10901 for (tree param_decl
= DECL_ARGUMENTS (gnu_cv_thunk
);
10903 param_decl
= DECL_CHAIN (param_decl
))
10904 DECL_CONTEXT (param_decl
) = gnu_cv_thunk
;
10906 DECL_RESULT (gnu_cv_thunk
) = copy_node (DECL_RESULT (gnu_thunk
));
10907 DECL_CONTEXT (DECL_RESULT (gnu_cv_thunk
)) = gnu_cv_thunk
;
10909 DECL_LANG_SPECIFIC (gnu_cv_thunk
) = DECL_LANG_SPECIFIC (gnu_thunk
);
10910 DECL_CONTEXT (gnu_cv_thunk
) = DECL_CONTEXT (gnu_thunk
);
10911 TREE_READONLY (gnu_cv_thunk
) = TREE_READONLY (gnu_thunk
);
10912 TREE_THIS_VOLATILE (gnu_cv_thunk
) = TREE_THIS_VOLATILE (gnu_thunk
);
10913 DECL_ARTIFICIAL (gnu_cv_thunk
) = 1;
10915 return gnu_cv_thunk
;
10918 /* Try to create a GNU thunk for {GNAT,GNU}_THUNK and return true on success.
10920 GNU thunks are more efficient than GNAT thunks because they don't call into
10921 the runtime to retrieve the offset used in the displacement operation, but
10922 they are tailored to C++ and thus too limited to support the full range of
10923 thunks generated in Ada. Here's the complete list of limitations:
10925 1. Multi-controlling thunks, i.e thunks with more than one controlling
10926 parameter, are simply not supported.
10928 2. Covariant thunks, i.e. thunks for which the result is also controlling,
10929 are split into a pair of (this, covariant-only) thunks.
10931 3. Variable-offset thunks, i.e. thunks for which the offset depends on the
10932 object and not only on its type, are supported as 2nd class citizens.
10934 4. External thunks, i.e. thunks for which the target is not declared in
10935 the same unit as the thunk, are supported as 2nd class citizens.
10937 5. Local thunks, i.e. thunks generated for a local type, are supported as
10938 2nd class citizens. */
10941 maybe_make_gnu_thunk (Entity_Id gnat_thunk
, tree gnu_thunk
)
10943 /* We use the Thunk_Target to compute the properties of the thunk. */
10944 const Entity_Id gnat_target
= Thunk_Target (gnat_thunk
);
10946 /* Check that the first formal of the target is the only controlling one. */
10947 Entity_Id gnat_formal
= First_Formal (gnat_target
);
10948 if (!Is_Controlling_Formal (gnat_formal
))
10950 for (gnat_formal
= Next_Formal (gnat_formal
);
10951 Present (gnat_formal
);
10952 gnat_formal
= Next_Formal (gnat_formal
))
10953 if (Is_Controlling_Formal (gnat_formal
))
10956 /* Look for the types that control the target and the thunk. */
10957 const Entity_Id gnat_controlling_type
= get_controlling_type (gnat_target
);
10958 const Entity_Id gnat_interface_type
= get_controlling_type (gnat_thunk
);
10960 /* We must have an interface type at this point. */
10961 gcc_assert (Is_Interface (gnat_interface_type
));
10963 /* Now compute whether the former covers the latter. */
10964 const Entity_Id gnat_interface_tag
10965 = Find_Interface_Tag (gnat_controlling_type
, gnat_interface_type
);
10966 tree gnu_interface_tag
10967 = Present (gnat_interface_tag
)
10968 ? gnat_to_gnu_field_decl (gnat_interface_tag
)
10970 tree gnu_interface_offset
10971 = gnu_interface_tag
? byte_position (gnu_interface_tag
) : NULL_TREE
;
10973 /* There are three ways to retrieve the offset between the interface view
10974 and the base object. Either the controlling type covers the interface
10975 type and the offset of the corresponding tag is fixed, in which case it
10976 can be statically encoded in the thunk (see FIXED_OFFSET below). Or the
10977 controlling type doesn't cover the interface type but is of fixed size,
10978 in which case the offset is stored in the dispatch table, two pointers
10979 above the dispatch table address (see VIRTUAL_VALUE below). Otherwise,
10980 the offset is variable and is stored right after the tag in every object
10981 (see INDIRECT_OFFSET below). See also a-tags.ads for more details. */
10982 HOST_WIDE_INT fixed_offset
, virtual_value
, indirect_offset
;
10983 tree virtual_offset
;
10985 if (gnu_interface_offset
&& TREE_CODE (gnu_interface_offset
) == INTEGER_CST
)
10987 fixed_offset
= - tree_to_shwi (gnu_interface_offset
);
10989 virtual_offset
= NULL_TREE
;
10990 indirect_offset
= 0;
10992 else if (!gnu_interface_offset
10993 && !Is_Variable_Size_Record (gnat_controlling_type
))
10996 virtual_value
= - 2 * (HOST_WIDE_INT
) (POINTER_SIZE
/ BITS_PER_UNIT
);
10997 virtual_offset
= build_int_cst (integer_type_node
, virtual_value
);
10998 indirect_offset
= 0;
11002 /* Covariant thunks with variable offset are not supported. */
11003 if (Has_Controlling_Result (gnat_target
))
11008 virtual_offset
= NULL_TREE
;
11009 indirect_offset
= (HOST_WIDE_INT
) (POINTER_SIZE
/ BITS_PER_UNIT
);
11012 /* But we generate a call to the Thunk_Entity in the thunk. */
11014 = gnat_to_gnu_entity (Thunk_Entity (gnat_thunk
), NULL_TREE
, false);
11016 /* If the target is local, then thunk and target must have the same context
11017 because cgraph_node::expand_thunk can only forward the static chain. */
11018 if (DECL_STATIC_CHAIN (gnu_target
)
11019 && DECL_CONTEXT (gnu_thunk
) != DECL_CONTEXT (gnu_target
))
11022 /* If the target returns by invisible reference and is external, apply the
11023 same transformation as Subprogram_Body_to_gnu here. */
11024 if (TREE_ADDRESSABLE (TREE_TYPE (gnu_target
))
11025 && DECL_EXTERNAL (gnu_target
)
11026 && TREE_CODE (TREE_TYPE (DECL_RESULT (gnu_target
))) != REFERENCE_TYPE
)
11028 TREE_TYPE (DECL_RESULT (gnu_target
))
11029 = build_reference_type (TREE_TYPE (DECL_RESULT (gnu_target
)));
11030 relayout_decl (DECL_RESULT (gnu_target
));
11033 /* The thunk expander requires the return types of thunk and target to be
11034 compatible, which is not fully the case with the CICO mechanism. */
11035 if (TYPE_CI_CO_LIST (TREE_TYPE (gnu_thunk
)))
11037 tree gnu_target_type
= TREE_TYPE (gnu_target
);
11038 gcc_assert (TYPE_CI_CO_LIST (gnu_target_type
));
11039 TYPE_CANONICAL (TREE_TYPE (TREE_TYPE (gnu_thunk
)))
11040 = TYPE_CANONICAL (TREE_TYPE (gnu_target_type
));
11043 cgraph_node
*target_node
= cgraph_node::get_create (gnu_target
);
11045 /* We may also need to create an alias for the target in order to make
11046 the call local, depending on the linkage of the target. */
11047 tree gnu_alias
= use_alias_for_thunk_p (gnu_target
)
11048 ? make_alias_for_thunk (gnu_target
)
11051 /* If the return type of the target is a controlling type, then we need
11052 both an usual this thunk and a covariant thunk in this order:
11054 this thunk --> covariant thunk --> target
11056 For covariant thunks, we can only handle a fixed offset. */
11057 if (Has_Controlling_Result (gnat_target
))
11059 gcc_assert (fixed_offset
< 0);
11060 tree gnu_cv_thunk
= make_covariant_thunk (gnat_thunk
, gnu_thunk
);
11061 target_node
->create_thunk (gnu_cv_thunk
, gnu_target
, false,
11062 - fixed_offset
, 0, 0,
11063 NULL_TREE
, gnu_alias
);
11065 gnu_alias
= gnu_target
= gnu_cv_thunk
;
11068 target_node
->create_thunk (gnu_thunk
, gnu_target
, true,
11069 fixed_offset
, virtual_value
, indirect_offset
,
11070 virtual_offset
, gnu_alias
);
11075 /* Initialize the table that maps GNAT codes to GCC codes for simple
11076 binary and unary operations. */
11079 init_code_table (void)
11081 gnu_codes
[N_Op_And
] = TRUTH_AND_EXPR
;
11082 gnu_codes
[N_Op_Or
] = TRUTH_OR_EXPR
;
11083 gnu_codes
[N_Op_Xor
] = TRUTH_XOR_EXPR
;
11084 gnu_codes
[N_Op_Eq
] = EQ_EXPR
;
11085 gnu_codes
[N_Op_Ne
] = NE_EXPR
;
11086 gnu_codes
[N_Op_Lt
] = LT_EXPR
;
11087 gnu_codes
[N_Op_Le
] = LE_EXPR
;
11088 gnu_codes
[N_Op_Gt
] = GT_EXPR
;
11089 gnu_codes
[N_Op_Ge
] = GE_EXPR
;
11090 gnu_codes
[N_Op_Add
] = PLUS_EXPR
;
11091 gnu_codes
[N_Op_Subtract
] = MINUS_EXPR
;
11092 gnu_codes
[N_Op_Multiply
] = MULT_EXPR
;
11093 gnu_codes
[N_Op_Mod
] = FLOOR_MOD_EXPR
;
11094 gnu_codes
[N_Op_Rem
] = TRUNC_MOD_EXPR
;
11095 gnu_codes
[N_Op_Minus
] = NEGATE_EXPR
;
11096 gnu_codes
[N_Op_Abs
] = ABS_EXPR
;
11097 gnu_codes
[N_Op_Not
] = TRUTH_NOT_EXPR
;
11098 gnu_codes
[N_Op_Rotate_Left
] = LROTATE_EXPR
;
11099 gnu_codes
[N_Op_Rotate_Right
] = RROTATE_EXPR
;
11100 gnu_codes
[N_Op_Shift_Left
] = LSHIFT_EXPR
;
11101 gnu_codes
[N_Op_Shift_Right
] = RSHIFT_EXPR
;
11102 gnu_codes
[N_Op_Shift_Right_Arithmetic
] = RSHIFT_EXPR
;
11103 gnu_codes
[N_And_Then
] = TRUTH_ANDIF_EXPR
;
11104 gnu_codes
[N_Or_Else
] = TRUTH_ORIF_EXPR
;
11107 #include "gt-ada-trans.h"