1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2012, 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"
34 #include "libfuncs.h" /* For set_stack_check_libfunc. */
35 #include "tree-iterator.h"
41 #include "adadecode.h"
58 /* We should avoid allocating more than ALLOCA_THRESHOLD bytes via alloca,
59 for fear of running out of stack space. If we need more, we use xmalloc
61 #define ALLOCA_THRESHOLD 1000
63 /* Let code below know whether we are targetting VMS without need of
64 intrusive preprocessor directives. */
65 #ifndef TARGET_ABI_OPEN_VMS
66 #define TARGET_ABI_OPEN_VMS 0
69 /* In configurations where blocks have no end_locus attached, just
70 sink assignments into a dummy global. */
71 #ifndef BLOCK_SOURCE_END_LOCATION
72 static location_t block_end_locus_sink
;
73 #define BLOCK_SOURCE_END_LOCATION(BLOCK) block_end_locus_sink
76 /* For efficient float-to-int rounding, it is necessary to know whether
77 floating-point arithmetic may use wider intermediate results. When
78 FP_ARITH_MAY_WIDEN is not defined, be conservative and only assume
79 that arithmetic does not widen if double precision is emulated. */
80 #ifndef FP_ARITH_MAY_WIDEN
81 #if defined(HAVE_extendsfdf2)
82 #define FP_ARITH_MAY_WIDEN HAVE_extendsfdf2
84 #define FP_ARITH_MAY_WIDEN 0
88 /* Pointers to front-end tables accessed through macros. */
89 struct Node
*Nodes_Ptr
;
90 Node_Id
*Next_Node_Ptr
;
91 Node_Id
*Prev_Node_Ptr
;
92 struct Elist_Header
*Elists_Ptr
;
93 struct Elmt_Item
*Elmts_Ptr
;
94 struct String_Entry
*Strings_Ptr
;
95 Char_Code
*String_Chars_Ptr
;
96 struct List_Header
*List_Headers_Ptr
;
98 /* Highest number in the front-end node table. */
101 /* Current node being treated, in case abort called. */
102 Node_Id error_gnat_node
;
104 /* True when gigi is being called on an analyzed but unexpanded
105 tree, and the only purpose of the call is to properly annotate
106 types with representation information. */
107 bool type_annotate_only
;
109 /* Current filename without path. */
110 const char *ref_filename
;
112 /* When not optimizing, we cache the 'First, 'Last and 'Length attributes
113 of unconstrained array IN parameters to avoid emitting a great deal of
114 redundant instructions to recompute them each time. */
115 struct GTY (()) parm_attr_d
{
116 int id
; /* GTY doesn't like Entity_Id. */
123 typedef struct parm_attr_d
*parm_attr
;
125 DEF_VEC_P(parm_attr
);
126 DEF_VEC_ALLOC_P(parm_attr
,gc
);
128 struct GTY(()) language_function
{
129 VEC(parm_attr
,gc
) *parm_attr_cache
;
130 bitmap named_ret_val
;
131 VEC(tree
,gc
) *other_ret_val
;
135 #define f_parm_attr_cache \
136 DECL_STRUCT_FUNCTION (current_function_decl)->language->parm_attr_cache
138 #define f_named_ret_val \
139 DECL_STRUCT_FUNCTION (current_function_decl)->language->named_ret_val
141 #define f_other_ret_val \
142 DECL_STRUCT_FUNCTION (current_function_decl)->language->other_ret_val
145 DECL_STRUCT_FUNCTION (current_function_decl)->language->gnat_ret
147 /* A structure used to gather together information about a statement group.
148 We use this to gather related statements, for example the "then" part
149 of a IF. In the case where it represents a lexical scope, we may also
150 have a BLOCK node corresponding to it and/or cleanups. */
152 struct GTY((chain_next ("%h.previous"))) stmt_group
{
153 struct stmt_group
*previous
; /* Previous code group. */
154 tree stmt_list
; /* List of statements for this code group. */
155 tree block
; /* BLOCK for this code group, if any. */
156 tree cleanups
; /* Cleanups for this code group, if any. */
159 static GTY(()) struct stmt_group
*current_stmt_group
;
161 /* List of unused struct stmt_group nodes. */
162 static GTY((deletable
)) struct stmt_group
*stmt_group_free_list
;
164 /* A structure used to record information on elaboration procedures
165 we've made and need to process.
167 ??? gnat_node should be Node_Id, but gengtype gets confused. */
169 struct GTY((chain_next ("%h.next"))) elab_info
{
170 struct elab_info
*next
; /* Pointer to next in chain. */
171 tree elab_proc
; /* Elaboration procedure. */
172 int gnat_node
; /* The N_Compilation_Unit. */
175 static GTY(()) struct elab_info
*elab_info_list
;
177 /* Stack of exception pointer variables. Each entry is the VAR_DECL
178 that stores the address of the raised exception. Nonzero means we
179 are in an exception handler. Not used in the zero-cost case. */
180 static GTY(()) VEC(tree
,gc
) *gnu_except_ptr_stack
;
182 /* In ZCX case, current exception pointer. Used to re-raise it. */
183 static GTY(()) tree gnu_incoming_exc_ptr
;
185 /* Stack for storing the current elaboration procedure decl. */
186 static GTY(()) VEC(tree
,gc
) *gnu_elab_proc_stack
;
188 /* Stack of labels to be used as a goto target instead of a return in
189 some functions. See processing for N_Subprogram_Body. */
190 static GTY(()) VEC(tree
,gc
) *gnu_return_label_stack
;
192 /* Stack of variable for the return value of a function with copy-in/copy-out
193 parameters. See processing for N_Subprogram_Body. */
194 static GTY(()) VEC(tree
,gc
) *gnu_return_var_stack
;
196 /* Structure used to record information for a range check. */
197 struct GTY(()) range_check_info_d
{
204 typedef struct range_check_info_d
*range_check_info
;
206 DEF_VEC_P(range_check_info
);
207 DEF_VEC_ALLOC_P(range_check_info
,gc
);
209 /* Structure used to record information for a loop. */
210 struct GTY(()) loop_info_d
{
213 VEC(range_check_info
,gc
) *checks
;
216 typedef struct loop_info_d
*loop_info
;
218 DEF_VEC_P(loop_info
);
219 DEF_VEC_ALLOC_P(loop_info
,gc
);
221 /* Stack of loop_info structures associated with LOOP_STMT nodes. */
222 static GTY(()) VEC(loop_info
,gc
) *gnu_loop_stack
;
224 /* The stacks for N_{Push,Pop}_*_Label. */
225 static GTY(()) VEC(tree
,gc
) *gnu_constraint_error_label_stack
;
226 static GTY(()) VEC(tree
,gc
) *gnu_storage_error_label_stack
;
227 static GTY(()) VEC(tree
,gc
) *gnu_program_error_label_stack
;
229 /* Map GNAT tree codes to GCC tree codes for simple expressions. */
230 static enum tree_code gnu_codes
[Number_Node_Kinds
];
232 static void init_code_table (void);
233 static void Compilation_Unit_to_gnu (Node_Id
);
234 static void record_code_position (Node_Id
);
235 static void insert_code_for (Node_Id
);
236 static void add_cleanup (tree
, Node_Id
);
237 static void add_stmt_list (List_Id
);
238 static void push_exception_label_stack (VEC(tree
,gc
) **, Entity_Id
);
239 static tree
build_stmt_group (List_Id
, bool);
240 static enum gimplify_status
gnat_gimplify_stmt (tree
*);
241 static void elaborate_all_entities (Node_Id
);
242 static void process_freeze_entity (Node_Id
);
243 static void process_decls (List_Id
, List_Id
, Node_Id
, bool, bool);
244 static tree
emit_range_check (tree
, Node_Id
, Node_Id
);
245 static tree
emit_index_check (tree
, tree
, tree
, tree
, Node_Id
);
246 static tree
emit_check (tree
, tree
, int, Node_Id
);
247 static tree
build_unary_op_trapv (enum tree_code
, tree
, tree
, Node_Id
);
248 static tree
build_binary_op_trapv (enum tree_code
, tree
, tree
, tree
, Node_Id
);
249 static tree
convert_with_check (Entity_Id
, tree
, bool, bool, bool, Node_Id
);
250 static bool addressable_p (tree
, tree
);
251 static tree
assoc_to_constructor (Entity_Id
, Node_Id
, tree
);
252 static tree
extract_values (tree
, tree
);
253 static tree
pos_to_constructor (Node_Id
, tree
, Entity_Id
);
254 static tree
maybe_implicit_deref (tree
);
255 static void set_expr_location_from_node (tree
, Node_Id
);
256 static bool set_end_locus_from_node (tree
, Node_Id
);
257 static void set_gnu_expr_location_from_node (tree
, Node_Id
);
258 static int lvalue_required_p (Node_Id
, tree
, bool, bool, bool);
259 static tree
build_raise_check (int, enum exception_info_kind
);
260 static tree
create_init_temporary (const char *, tree
, tree
*, Node_Id
);
262 /* Hooks for debug info back-ends, only supported and used in a restricted set
263 of configurations. */
264 static const char *extract_encoding (const char *) ATTRIBUTE_UNUSED
;
265 static const char *decode_name (const char *) ATTRIBUTE_UNUSED
;
267 /* This is the main program of the back-end. It sets up all the table
268 structures and then generates code. */
271 gigi (Node_Id gnat_root
, int max_gnat_node
, int number_name ATTRIBUTE_UNUSED
,
272 struct Node
*nodes_ptr
, Node_Id
*next_node_ptr
, Node_Id
*prev_node_ptr
,
273 struct Elist_Header
*elists_ptr
, struct Elmt_Item
*elmts_ptr
,
274 struct String_Entry
*strings_ptr
, Char_Code
*string_chars_ptr
,
275 struct List_Header
*list_headers_ptr
, Nat number_file
,
276 struct File_Info_Type
*file_info_ptr
,
277 Entity_Id standard_boolean
, Entity_Id standard_integer
,
278 Entity_Id standard_character
, Entity_Id standard_long_long_float
,
279 Entity_Id standard_exception_type
, Int gigi_operating_mode
)
281 Entity_Id gnat_literal
;
282 tree long_long_float_type
, exception_type
, t
, ftype
;
283 tree int64_type
= gnat_type_for_size (64, 0);
284 struct elab_info
*info
;
287 max_gnat_nodes
= max_gnat_node
;
289 Nodes_Ptr
= nodes_ptr
;
290 Next_Node_Ptr
= next_node_ptr
;
291 Prev_Node_Ptr
= prev_node_ptr
;
292 Elists_Ptr
= elists_ptr
;
293 Elmts_Ptr
= elmts_ptr
;
294 Strings_Ptr
= strings_ptr
;
295 String_Chars_Ptr
= string_chars_ptr
;
296 List_Headers_Ptr
= list_headers_ptr
;
298 type_annotate_only
= (gigi_operating_mode
== 1);
300 for (i
= 0; i
< number_file
; i
++)
302 /* Use the identifier table to make a permanent copy of the filename as
303 the name table gets reallocated after Gigi returns but before all the
304 debugging information is output. The __gnat_to_canonical_file_spec
305 call translates filenames from pragmas Source_Reference that contain
306 host style syntax not understood by gdb. */
310 (__gnat_to_canonical_file_spec
311 (Get_Name_String (file_info_ptr
[i
].File_Name
))));
313 /* We rely on the order isomorphism between files and line maps. */
314 gcc_assert ((int) LINEMAPS_ORDINARY_USED (line_table
) == i
);
316 /* We create the line map for a source file at once, with a fixed number
317 of columns chosen to avoid jumping over the next power of 2. */
318 linemap_add (line_table
, LC_ENTER
, 0, filename
, 1);
319 linemap_line_start (line_table
, file_info_ptr
[i
].Num_Source_Lines
, 252);
320 linemap_position_for_column (line_table
, 252 - 1);
321 linemap_add (line_table
, LC_LEAVE
, 0, NULL
, 0);
324 gcc_assert (Nkind (gnat_root
) == N_Compilation_Unit
);
326 /* Declare the name of the compilation unit as the first global
327 name in order to make the middle-end fully deterministic. */
328 t
= create_concat_name (Defining_Entity (Unit (gnat_root
)), NULL
);
329 first_global_object_name
= ggc_strdup (IDENTIFIER_POINTER (t
));
331 /* Initialize ourselves. */
336 /* If we are just annotating types, give VOID_TYPE zero sizes to avoid
338 if (type_annotate_only
)
340 TYPE_SIZE (void_type_node
) = bitsize_zero_node
;
341 TYPE_SIZE_UNIT (void_type_node
) = size_zero_node
;
344 /* Enable GNAT stack checking method if needed */
345 if (!Stack_Check_Probes_On_Target
)
346 set_stack_check_libfunc ("_gnat_stack_check");
348 /* Retrieve alignment settings. */
349 double_float_alignment
= get_target_double_float_alignment ();
350 double_scalar_alignment
= get_target_double_scalar_alignment ();
352 /* Record the builtin types. Define `integer' and `character' first so that
353 dbx will output them first. */
354 record_builtin_type ("integer", integer_type_node
, false);
355 record_builtin_type ("character", unsigned_char_type_node
, false);
356 record_builtin_type ("boolean", boolean_type_node
, false);
357 record_builtin_type ("void", void_type_node
, false);
359 /* Save the type we made for integer as the type for Standard.Integer. */
360 save_gnu_tree (Base_Type (standard_integer
),
361 TYPE_NAME (integer_type_node
),
364 /* Likewise for character as the type for Standard.Character. */
365 save_gnu_tree (Base_Type (standard_character
),
366 TYPE_NAME (unsigned_char_type_node
),
369 /* Likewise for boolean as the type for Standard.Boolean. */
370 save_gnu_tree (Base_Type (standard_boolean
),
371 TYPE_NAME (boolean_type_node
),
373 gnat_literal
= First_Literal (Base_Type (standard_boolean
));
374 t
= UI_To_gnu (Enumeration_Rep (gnat_literal
), boolean_type_node
);
375 gcc_assert (t
== boolean_false_node
);
376 t
= create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
377 boolean_type_node
, t
, true, false, false, false,
379 DECL_IGNORED_P (t
) = 1;
380 save_gnu_tree (gnat_literal
, t
, false);
381 gnat_literal
= Next_Literal (gnat_literal
);
382 t
= UI_To_gnu (Enumeration_Rep (gnat_literal
), boolean_type_node
);
383 gcc_assert (t
== boolean_true_node
);
384 t
= create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
385 boolean_type_node
, t
, true, false, false, false,
387 DECL_IGNORED_P (t
) = 1;
388 save_gnu_tree (gnat_literal
, t
, false);
390 void_ftype
= build_function_type_list (void_type_node
, NULL_TREE
);
391 ptr_void_ftype
= build_pointer_type (void_ftype
);
393 /* Now declare run-time functions. */
394 ftype
= build_function_type_list (ptr_void_type_node
, sizetype
, NULL_TREE
);
396 /* malloc is a function declaration tree for a function to allocate
399 = create_subprog_decl (get_identifier ("__gnat_malloc"), NULL_TREE
,
400 ftype
, NULL_TREE
, false, true, true, true, NULL
,
402 DECL_IS_MALLOC (malloc_decl
) = 1;
404 /* malloc32 is a function declaration tree for a function to allocate
405 32-bit memory on a 64-bit system. Needed only on 64-bit VMS. */
407 = create_subprog_decl (get_identifier ("__gnat_malloc32"), NULL_TREE
,
408 ftype
, NULL_TREE
, false, true, true, true, NULL
,
410 DECL_IS_MALLOC (malloc32_decl
) = 1;
412 /* free is a function declaration tree for a function to free memory. */
414 = create_subprog_decl (get_identifier ("__gnat_free"), NULL_TREE
,
415 build_function_type_list (void_type_node
,
418 NULL_TREE
, false, true, true, true, NULL
, Empty
);
420 /* This is used for 64-bit multiplication with overflow checking. */
422 = create_subprog_decl (get_identifier ("__gnat_mulv64"), NULL_TREE
,
423 build_function_type_list (int64_type
, int64_type
,
424 int64_type
, NULL_TREE
),
425 NULL_TREE
, false, true, true, true, NULL
, Empty
);
427 /* Name of the _Parent field in tagged record types. */
428 parent_name_id
= get_identifier (Get_Name_String (Name_uParent
));
430 /* Name of the Exception_Data type defined in System.Standard_Library. */
431 exception_data_name_id
432 = get_identifier ("system__standard_library__exception_data");
434 /* Make the types and functions used for exception processing. */
436 = build_array_type (gnat_type_for_mode (Pmode
, 0),
437 build_index_type (size_int (5)));
438 record_builtin_type ("JMPBUF_T", jmpbuf_type
, true);
439 jmpbuf_ptr_type
= build_pointer_type (jmpbuf_type
);
441 /* Functions to get and set the jumpbuf pointer for the current thread. */
443 = create_subprog_decl
444 (get_identifier ("system__soft_links__get_jmpbuf_address_soft"),
445 NULL_TREE
, build_function_type_list (jmpbuf_ptr_type
, NULL_TREE
),
446 NULL_TREE
, false, true, true, true, NULL
, Empty
);
447 DECL_IGNORED_P (get_jmpbuf_decl
) = 1;
450 = create_subprog_decl
451 (get_identifier ("system__soft_links__set_jmpbuf_address_soft"),
452 NULL_TREE
, build_function_type_list (void_type_node
, jmpbuf_ptr_type
,
454 NULL_TREE
, false, true, true, true, NULL
, Empty
);
455 DECL_IGNORED_P (set_jmpbuf_decl
) = 1;
457 /* setjmp returns an integer and has one operand, which is a pointer to
460 = create_subprog_decl
461 (get_identifier ("__builtin_setjmp"), NULL_TREE
,
462 build_function_type_list (integer_type_node
, jmpbuf_ptr_type
,
464 NULL_TREE
, false, true, true, true, NULL
, Empty
);
465 DECL_BUILT_IN_CLASS (setjmp_decl
) = BUILT_IN_NORMAL
;
466 DECL_FUNCTION_CODE (setjmp_decl
) = BUILT_IN_SETJMP
;
468 /* update_setjmp_buf updates a setjmp buffer from the current stack pointer
470 update_setjmp_buf_decl
471 = create_subprog_decl
472 (get_identifier ("__builtin_update_setjmp_buf"), NULL_TREE
,
473 build_function_type_list (void_type_node
, jmpbuf_ptr_type
, NULL_TREE
),
474 NULL_TREE
, false, true, true, true, NULL
, Empty
);
475 DECL_BUILT_IN_CLASS (update_setjmp_buf_decl
) = BUILT_IN_NORMAL
;
476 DECL_FUNCTION_CODE (update_setjmp_buf_decl
) = BUILT_IN_UPDATE_SETJMP_BUF
;
478 /* Hooks to call when entering/leaving an exception handler. */
480 = build_function_type_list (void_type_node
, ptr_void_type_node
, NULL_TREE
);
483 = create_subprog_decl (get_identifier ("__gnat_begin_handler"), NULL_TREE
,
484 ftype
, NULL_TREE
, false, true, true, true, NULL
,
486 DECL_IGNORED_P (begin_handler_decl
) = 1;
489 = create_subprog_decl (get_identifier ("__gnat_end_handler"), NULL_TREE
,
490 ftype
, NULL_TREE
, false, true, true, true, NULL
,
492 DECL_IGNORED_P (end_handler_decl
) = 1;
495 = create_subprog_decl (get_identifier ("__gnat_reraise_zcx"), NULL_TREE
,
496 ftype
, NULL_TREE
, false, true, true, true, NULL
,
498 DECL_IGNORED_P (reraise_zcx_decl
) = 1;
500 /* If in no exception handlers mode, all raise statements are redirected to
501 __gnat_last_chance_handler. No need to redefine raise_nodefer_decl since
502 this procedure will never be called in this mode. */
503 if (No_Exception_Handlers_Set ())
506 = create_subprog_decl
507 (get_identifier ("__gnat_last_chance_handler"), NULL_TREE
,
508 build_function_type_list (void_type_node
,
510 (unsigned_char_type_node
),
511 integer_type_node
, NULL_TREE
),
512 NULL_TREE
, false, true, true, true, NULL
, Empty
);
513 TREE_THIS_VOLATILE (decl
) = 1;
514 TREE_SIDE_EFFECTS (decl
) = 1;
516 = build_qualified_type (TREE_TYPE (decl
), TYPE_QUAL_VOLATILE
);
517 for (i
= 0; i
< (int) ARRAY_SIZE (gnat_raise_decls
); i
++)
518 gnat_raise_decls
[i
] = decl
;
522 /* Otherwise, make one decl for each exception reason. */
523 for (i
= 0; i
< (int) ARRAY_SIZE (gnat_raise_decls
); i
++)
524 gnat_raise_decls
[i
] = build_raise_check (i
, exception_simple
);
525 for (i
= 0; i
< (int) ARRAY_SIZE (gnat_raise_decls_ext
); i
++)
526 gnat_raise_decls_ext
[i
]
527 = build_raise_check (i
,
528 i
== CE_Index_Check_Failed
529 || i
== CE_Range_Check_Failed
530 || i
== CE_Invalid_Data
531 ? exception_range
: exception_column
);
534 /* Set the types that GCC and Gigi use from the front end. */
536 = gnat_to_gnu_entity (Base_Type (standard_exception_type
), NULL_TREE
, 0);
537 except_type_node
= TREE_TYPE (exception_type
);
539 /* Make other functions used for exception processing. */
541 = create_subprog_decl
542 (get_identifier ("system__soft_links__get_gnat_exception"), NULL_TREE
,
543 build_function_type_list (build_pointer_type (except_type_node
),
545 NULL_TREE
, false, true, true, true, NULL
, Empty
);
548 = create_subprog_decl
549 (get_identifier ("__gnat_raise_nodefer_with_msg"), NULL_TREE
,
550 build_function_type_list (void_type_node
,
551 build_pointer_type (except_type_node
),
553 NULL_TREE
, false, true, true, true, NULL
, Empty
);
555 /* Indicate that it never returns. */
556 TREE_THIS_VOLATILE (raise_nodefer_decl
) = 1;
557 TREE_SIDE_EFFECTS (raise_nodefer_decl
) = 1;
558 TREE_TYPE (raise_nodefer_decl
)
559 = build_qualified_type (TREE_TYPE (raise_nodefer_decl
),
562 /* Build the special descriptor type and its null node if needed. */
563 if (TARGET_VTABLE_USES_DESCRIPTORS
)
565 tree null_node
= fold_convert (ptr_void_ftype
, null_pointer_node
);
566 tree field_list
= NULL_TREE
;
568 VEC(constructor_elt
,gc
) *null_vec
= NULL
;
569 constructor_elt
*elt
;
571 fdesc_type_node
= make_node (RECORD_TYPE
);
572 VEC_safe_grow (constructor_elt
, gc
, null_vec
,
573 TARGET_VTABLE_USES_DESCRIPTORS
);
574 elt
= (VEC_address (constructor_elt
,null_vec
)
575 + TARGET_VTABLE_USES_DESCRIPTORS
- 1);
577 for (j
= 0; j
< TARGET_VTABLE_USES_DESCRIPTORS
; j
++)
580 = create_field_decl (NULL_TREE
, ptr_void_ftype
, fdesc_type_node
,
581 NULL_TREE
, NULL_TREE
, 0, 1);
582 DECL_CHAIN (field
) = field_list
;
585 elt
->value
= null_node
;
589 finish_record_type (fdesc_type_node
, nreverse (field_list
), 0, false);
590 record_builtin_type ("descriptor", fdesc_type_node
, true);
591 null_fdesc_node
= gnat_build_constructor (fdesc_type_node
, null_vec
);
595 = gnat_to_gnu_entity (Base_Type (standard_long_long_float
), NULL_TREE
, 0);
597 if (TREE_CODE (TREE_TYPE (long_long_float_type
)) == INTEGER_TYPE
)
599 /* In this case, the builtin floating point types are VAX float,
600 so make up a type for use. */
601 longest_float_type_node
= make_node (REAL_TYPE
);
602 TYPE_PRECISION (longest_float_type_node
) = LONG_DOUBLE_TYPE_SIZE
;
603 layout_type (longest_float_type_node
);
604 record_builtin_type ("longest float type", longest_float_type_node
,
608 longest_float_type_node
= TREE_TYPE (long_long_float_type
);
610 /* Dummy objects to materialize "others" and "all others" in the exception
611 tables. These are exported by a-exexpr-gcc.adb, so see this unit for
614 = create_var_decl (get_identifier ("OTHERS"),
615 get_identifier ("__gnat_others_value"),
616 integer_type_node
, NULL_TREE
, true, false, true, false,
620 = create_var_decl (get_identifier ("ALL_OTHERS"),
621 get_identifier ("__gnat_all_others_value"),
622 integer_type_node
, NULL_TREE
, true, false, true, false,
625 main_identifier_node
= get_identifier ("main");
627 /* Install the builtins we might need, either internally or as
628 user available facilities for Intrinsic imports. */
629 gnat_install_builtins ();
631 VEC_safe_push (tree
, gc
, gnu_except_ptr_stack
, NULL_TREE
);
632 VEC_safe_push (tree
, gc
, gnu_constraint_error_label_stack
, NULL_TREE
);
633 VEC_safe_push (tree
, gc
, gnu_storage_error_label_stack
, NULL_TREE
);
634 VEC_safe_push (tree
, gc
, gnu_program_error_label_stack
, NULL_TREE
);
636 /* Process any Pragma Ident for the main unit. */
637 #ifdef ASM_OUTPUT_IDENT
638 if (Present (Ident_String (Main_Unit
)))
641 TREE_STRING_POINTER (gnat_to_gnu (Ident_String (Main_Unit
))));
644 /* If we are using the GCC exception mechanism, let GCC know. */
645 if (Exception_Mechanism
== Back_End_Exceptions
)
648 /* Now translate the compilation unit proper. */
649 Compilation_Unit_to_gnu (gnat_root
);
651 /* Finally see if we have any elaboration procedures to deal with. */
652 for (info
= elab_info_list
; info
; info
= info
->next
)
654 tree gnu_body
= DECL_SAVED_TREE (info
->elab_proc
), gnu_stmts
;
656 /* We should have a BIND_EXPR but it may not have any statements in it.
657 If it doesn't have any, we have nothing to do except for setting the
658 flag on the GNAT node. Otherwise, process the function as others. */
659 gnu_stmts
= gnu_body
;
660 if (TREE_CODE (gnu_stmts
) == BIND_EXPR
)
661 gnu_stmts
= BIND_EXPR_BODY (gnu_stmts
);
662 if (!gnu_stmts
|| !STATEMENT_LIST_HEAD (gnu_stmts
))
663 Set_Has_No_Elaboration_Code (info
->gnat_node
, 1);
666 begin_subprog_body (info
->elab_proc
);
667 end_subprog_body (gnu_body
);
668 rest_of_subprog_body_compilation (info
->elab_proc
);
672 /* We cannot track the location of errors past this point. */
673 error_gnat_node
= Empty
;
676 /* Return a subprogram decl corresponding to __gnat_rcheck_xx for the given
677 CHECK if KIND is EXCEPTION_SIMPLE, or else to __gnat_rcheck_xx_ext. */
680 build_raise_check (int check
, enum exception_info_kind kind
)
685 if (kind
== exception_simple
)
687 sprintf (name
, "__gnat_rcheck_%.2d", check
);
689 = build_function_type_list (void_type_node
,
691 (unsigned_char_type_node
),
692 integer_type_node
, NULL_TREE
);
696 tree t
= (kind
== exception_column
? NULL_TREE
: integer_type_node
);
697 sprintf (name
, "__gnat_rcheck_%.2d_ext", check
);
699 = build_function_type_list (void_type_node
,
701 (unsigned_char_type_node
),
702 integer_type_node
, integer_type_node
,
707 = create_subprog_decl (get_identifier (name
), NULL_TREE
, ftype
, NULL_TREE
,
708 false, true, true, true, NULL
, Empty
);
710 /* Indicate that it never returns. */
711 TREE_THIS_VOLATILE (result
) = 1;
712 TREE_SIDE_EFFECTS (result
) = 1;
714 = build_qualified_type (TREE_TYPE (result
), TYPE_QUAL_VOLATILE
);
719 /* Return a positive value if an lvalue is required for GNAT_NODE, which is
720 an N_Attribute_Reference. */
723 lvalue_required_for_attribute_p (Node_Id gnat_node
)
725 switch (Get_Attribute_Id (Attribute_Name (gnat_node
)))
733 case Attr_Range_Length
:
735 case Attr_Object_Size
:
736 case Attr_Value_Size
:
737 case Attr_Component_Size
:
738 case Attr_Max_Size_In_Storage_Elements
:
741 case Attr_Null_Parameter
:
742 case Attr_Passed_By_Reference
:
743 case Attr_Mechanism_Code
:
748 case Attr_Unchecked_Access
:
749 case Attr_Unrestricted_Access
:
750 case Attr_Code_Address
:
751 case Attr_Pool_Address
:
754 case Attr_Bit_Position
:
760 case Attr_Asm_Output
:
766 /* Return a positive value if an lvalue is required for GNAT_NODE. GNU_TYPE
767 is the type that will be used for GNAT_NODE in the translated GNU tree.
768 CONSTANT indicates whether the underlying object represented by GNAT_NODE
769 is constant in the Ada sense. If it is, ADDRESS_OF_CONSTANT indicates
770 whether its value is the address of a constant and ALIASED whether it is
771 aliased. If it isn't, ADDRESS_OF_CONSTANT and ALIASED are ignored.
773 The function climbs up the GNAT tree starting from the node and returns 1
774 upon encountering a node that effectively requires an lvalue downstream.
775 It returns int instead of bool to facilitate usage in non-purely binary
779 lvalue_required_p (Node_Id gnat_node
, tree gnu_type
, bool constant
,
780 bool address_of_constant
, bool aliased
)
782 Node_Id gnat_parent
= Parent (gnat_node
), gnat_temp
;
784 switch (Nkind (gnat_parent
))
789 case N_Attribute_Reference
:
790 return lvalue_required_for_attribute_p (gnat_parent
);
792 case N_Parameter_Association
:
793 case N_Function_Call
:
794 case N_Procedure_Call_Statement
:
795 /* If the parameter is by reference, an lvalue is required. */
797 || must_pass_by_ref (gnu_type
)
798 || default_pass_by_ref (gnu_type
));
800 case N_Indexed_Component
:
801 /* Only the array expression can require an lvalue. */
802 if (Prefix (gnat_parent
) != gnat_node
)
805 /* ??? Consider that referencing an indexed component with a
806 non-constant index forces the whole aggregate to memory.
807 Note that N_Integer_Literal is conservative, any static
808 expression in the RM sense could probably be accepted. */
809 for (gnat_temp
= First (Expressions (gnat_parent
));
811 gnat_temp
= Next (gnat_temp
))
812 if (Nkind (gnat_temp
) != N_Integer_Literal
)
815 /* ... fall through ... */
818 /* Only the array expression can require an lvalue. */
819 if (Prefix (gnat_parent
) != gnat_node
)
822 aliased
|= Has_Aliased_Components (Etype (gnat_node
));
823 return lvalue_required_p (gnat_parent
, gnu_type
, constant
,
824 address_of_constant
, aliased
);
826 case N_Selected_Component
:
827 aliased
|= Is_Aliased (Entity (Selector_Name (gnat_parent
)));
828 return lvalue_required_p (gnat_parent
, gnu_type
, constant
,
829 address_of_constant
, aliased
);
831 case N_Object_Renaming_Declaration
:
832 /* We need to make a real renaming only if the constant object is
833 aliased or if we may use a renaming pointer; otherwise we can
834 optimize and return the rvalue. We make an exception if the object
835 is an identifier since in this case the rvalue can be propagated
836 attached to the CONST_DECL. */
839 /* This should match the constant case of the renaming code. */
841 (Underlying_Type (Etype (Name (gnat_parent
))))
842 || Nkind (Name (gnat_parent
)) == N_Identifier
);
844 case N_Object_Declaration
:
845 /* We cannot use a constructor if this is an atomic object because
846 the actual assignment might end up being done component-wise. */
848 ||(Is_Composite_Type (Underlying_Type (Etype (gnat_node
)))
849 && Is_Atomic (Defining_Entity (gnat_parent
)))
850 /* We don't use a constructor if this is a class-wide object
851 because the effective type of the object is the equivalent
852 type of the class-wide subtype and it smashes most of the
853 data into an array of bytes to which we cannot convert. */
854 || Ekind ((Etype (Defining_Entity (gnat_parent
))))
855 == E_Class_Wide_Subtype
);
857 case N_Assignment_Statement
:
858 /* We cannot use a constructor if the LHS is an atomic object because
859 the actual assignment might end up being done component-wise. */
861 || Name (gnat_parent
) == gnat_node
862 || (Is_Composite_Type (Underlying_Type (Etype (gnat_node
)))
863 && Is_Atomic (Entity (Name (gnat_parent
)))));
865 case N_Unchecked_Type_Conversion
:
869 /* ... fall through ... */
871 case N_Type_Conversion
:
872 case N_Qualified_Expression
:
873 /* We must look through all conversions because we may need to bypass
874 an intermediate conversion that is meant to be purely formal. */
875 return lvalue_required_p (gnat_parent
,
876 get_unpadded_type (Etype (gnat_parent
)),
877 constant
, address_of_constant
, aliased
);
880 /* We should only reach here through the N_Qualified_Expression case.
881 Force an lvalue for composite types since a block-copy to the newly
882 allocated area of memory is made. */
883 return Is_Composite_Type (Underlying_Type (Etype (gnat_node
)));
885 case N_Explicit_Dereference
:
886 /* We look through dereferences for address of constant because we need
887 to handle the special cases listed above. */
888 if (constant
&& address_of_constant
)
889 return lvalue_required_p (gnat_parent
,
890 get_unpadded_type (Etype (gnat_parent
)),
893 /* ... fall through ... */
902 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Identifier,
903 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer
904 to where we should place the result type. */
907 Identifier_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
)
909 Node_Id gnat_temp
, gnat_temp_type
;
910 tree gnu_result
, gnu_result_type
;
912 /* Whether we should require an lvalue for GNAT_NODE. Needed in
913 specific circumstances only, so evaluated lazily. < 0 means
914 unknown, > 0 means known true, 0 means known false. */
915 int require_lvalue
= -1;
917 /* If GNAT_NODE is a constant, whether we should use the initialization
918 value instead of the constant entity, typically for scalars with an
919 address clause when the parent doesn't require an lvalue. */
920 bool use_constant_initializer
= false;
922 /* If the Etype of this node does not equal the Etype of the Entity,
923 something is wrong with the entity map, probably in generic
924 instantiation. However, this does not apply to types. Since we sometime
925 have strange Ekind's, just do this test for objects. Also, if the Etype of
926 the Entity is private, the Etype of the N_Identifier is allowed to be the
927 full type and also we consider a packed array type to be the same as the
928 original type. Similarly, a class-wide type is equivalent to a subtype of
929 itself. Finally, if the types are Itypes, one may be a copy of the other,
930 which is also legal. */
931 gnat_temp
= (Nkind (gnat_node
) == N_Defining_Identifier
932 ? gnat_node
: Entity (gnat_node
));
933 gnat_temp_type
= Etype (gnat_temp
);
935 gcc_assert (Etype (gnat_node
) == gnat_temp_type
936 || (Is_Packed (gnat_temp_type
)
937 && Etype (gnat_node
) == Packed_Array_Type (gnat_temp_type
))
938 || (Is_Class_Wide_Type (Etype (gnat_node
)))
939 || (IN (Ekind (gnat_temp_type
), Private_Kind
)
940 && Present (Full_View (gnat_temp_type
))
941 && ((Etype (gnat_node
) == Full_View (gnat_temp_type
))
942 || (Is_Packed (Full_View (gnat_temp_type
))
943 && (Etype (gnat_node
)
944 == Packed_Array_Type (Full_View
945 (gnat_temp_type
))))))
946 || (Is_Itype (Etype (gnat_node
)) && Is_Itype (gnat_temp_type
))
947 || !(Ekind (gnat_temp
) == E_Variable
948 || Ekind (gnat_temp
) == E_Component
949 || Ekind (gnat_temp
) == E_Constant
950 || Ekind (gnat_temp
) == E_Loop_Parameter
951 || IN (Ekind (gnat_temp
), Formal_Kind
)));
953 /* If this is a reference to a deferred constant whose partial view is an
954 unconstrained private type, the proper type is on the full view of the
955 constant, not on the full view of the type, which may be unconstrained.
957 This may be a reference to a type, for example in the prefix of the
958 attribute Position, generated for dispatching code (see Make_DT in
959 exp_disp,adb). In that case we need the type itself, not is parent,
960 in particular if it is a derived type */
961 if (Ekind (gnat_temp
) == E_Constant
962 && Is_Private_Type (gnat_temp_type
)
963 && (Has_Unknown_Discriminants (gnat_temp_type
)
964 || (Present (Full_View (gnat_temp_type
))
965 && Has_Discriminants (Full_View (gnat_temp_type
))))
966 && Present (Full_View (gnat_temp
)))
968 gnat_temp
= Full_View (gnat_temp
);
969 gnat_temp_type
= Etype (gnat_temp
);
973 /* We want to use the Actual_Subtype if it has already been elaborated,
974 otherwise the Etype. Avoid using Actual_Subtype for packed arrays to
976 if ((Ekind (gnat_temp
) == E_Constant
977 || Ekind (gnat_temp
) == E_Variable
|| Is_Formal (gnat_temp
))
978 && !(Is_Array_Type (Etype (gnat_temp
))
979 && Present (Packed_Array_Type (Etype (gnat_temp
))))
980 && Present (Actual_Subtype (gnat_temp
))
981 && present_gnu_tree (Actual_Subtype (gnat_temp
)))
982 gnat_temp_type
= Actual_Subtype (gnat_temp
);
984 gnat_temp_type
= Etype (gnat_node
);
987 /* Expand the type of this identifier first, in case it is an enumeral
988 literal, which only get made when the type is expanded. There is no
989 order-of-elaboration issue here. */
990 gnu_result_type
= get_unpadded_type (gnat_temp_type
);
992 /* If this is a non-imported scalar constant with an address clause,
993 retrieve the value instead of a pointer to be dereferenced unless
994 an lvalue is required. This is generally more efficient and actually
995 required if this is a static expression because it might be used
996 in a context where a dereference is inappropriate, such as a case
997 statement alternative or a record discriminant. There is no possible
998 volatile-ness short-circuit here since Volatile constants must be
1000 if (Ekind (gnat_temp
) == E_Constant
1001 && Is_Scalar_Type (gnat_temp_type
)
1002 && !Is_Imported (gnat_temp
)
1003 && Present (Address_Clause (gnat_temp
)))
1005 require_lvalue
= lvalue_required_p (gnat_node
, gnu_result_type
, true,
1006 false, Is_Aliased (gnat_temp
));
1007 use_constant_initializer
= !require_lvalue
;
1010 if (use_constant_initializer
)
1012 /* If this is a deferred constant, the initializer is attached to
1014 if (Present (Full_View (gnat_temp
)))
1015 gnat_temp
= Full_View (gnat_temp
);
1017 gnu_result
= gnat_to_gnu (Expression (Declaration_Node (gnat_temp
)));
1020 gnu_result
= gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, 0);
1022 /* Some objects (such as parameters passed by reference, globals of
1023 variable size, and renamed objects) actually represent the address
1024 of the object. In that case, we must do the dereference. Likewise,
1025 deal with parameters to foreign convention subprograms. */
1026 if (DECL_P (gnu_result
)
1027 && (DECL_BY_REF_P (gnu_result
)
1028 || (TREE_CODE (gnu_result
) == PARM_DECL
1029 && DECL_BY_COMPONENT_PTR_P (gnu_result
))))
1031 const bool read_only
= DECL_POINTS_TO_READONLY_P (gnu_result
);
1033 /* First do the first dereference if needed. */
1034 if (TREE_CODE (gnu_result
) == PARM_DECL
1035 && DECL_BY_DOUBLE_REF_P (gnu_result
))
1037 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result
);
1038 if (TREE_CODE (gnu_result
) == INDIRECT_REF
)
1039 TREE_THIS_NOTRAP (gnu_result
) = 1;
1041 /* The first reference, in case of a double reference, always points
1042 to read-only, see gnat_to_gnu_param for the rationale. */
1043 TREE_READONLY (gnu_result
) = 1;
1046 /* If it's a PARM_DECL to foreign convention subprogram, convert it. */
1047 if (TREE_CODE (gnu_result
) == PARM_DECL
1048 && DECL_BY_COMPONENT_PTR_P (gnu_result
))
1050 = convert (build_pointer_type (gnu_result_type
), gnu_result
);
1052 /* If it's a CONST_DECL, return the underlying constant like below. */
1053 else if (TREE_CODE (gnu_result
) == CONST_DECL
)
1054 gnu_result
= DECL_INITIAL (gnu_result
);
1056 /* If it's a renaming pointer and we are at the right binding level,
1057 we can reference the renamed object directly, since the renamed
1058 expression has been protected against multiple evaluations. */
1059 if (TREE_CODE (gnu_result
) == VAR_DECL
1060 && !DECL_LOOP_PARM_P (gnu_result
)
1061 && DECL_RENAMED_OBJECT (gnu_result
)
1062 && (!DECL_RENAMING_GLOBAL_P (gnu_result
) || global_bindings_p ()))
1063 gnu_result
= DECL_RENAMED_OBJECT (gnu_result
);
1065 /* Otherwise, do the final dereference. */
1068 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result
);
1070 if ((TREE_CODE (gnu_result
) == INDIRECT_REF
1071 || TREE_CODE (gnu_result
) == UNCONSTRAINED_ARRAY_REF
)
1072 && No (Address_Clause (gnat_temp
)))
1073 TREE_THIS_NOTRAP (gnu_result
) = 1;
1076 TREE_READONLY (gnu_result
) = 1;
1080 /* The GNAT tree has the type of a function as the type of its result. Also
1081 use the type of the result if the Etype is a subtype which is nominally
1082 unconstrained. But remove any padding from the resulting type. */
1083 if (TREE_CODE (TREE_TYPE (gnu_result
)) == FUNCTION_TYPE
1084 || Is_Constr_Subt_For_UN_Aliased (gnat_temp_type
))
1086 gnu_result_type
= TREE_TYPE (gnu_result
);
1087 if (TYPE_IS_PADDING_P (gnu_result_type
))
1088 gnu_result_type
= TREE_TYPE (TYPE_FIELDS (gnu_result_type
));
1091 /* If we have a constant declaration and its initializer, try to return the
1092 latter to avoid the need to call fold in lots of places and the need for
1093 elaboration code if this identifier is used as an initializer itself.
1094 Don't do it for aggregate types that contain a placeholder since their
1095 initializers cannot be manipulated easily. */
1096 if (TREE_CONSTANT (gnu_result
)
1097 && DECL_P (gnu_result
)
1098 && DECL_INITIAL (gnu_result
)
1099 && !(AGGREGATE_TYPE_P (TREE_TYPE (gnu_result
))
1100 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_result
))
1101 && type_contains_placeholder_p (TREE_TYPE (gnu_result
))))
1103 bool constant_only
= (TREE_CODE (gnu_result
) == CONST_DECL
1104 && !DECL_CONST_CORRESPONDING_VAR (gnu_result
));
1105 bool address_of_constant
= (TREE_CODE (gnu_result
) == CONST_DECL
1106 && DECL_CONST_ADDRESS_P (gnu_result
));
1108 /* If there is a (corresponding) variable or this is the address of a
1109 constant, we only want to return the initializer if an lvalue isn't
1110 required. Evaluate this now if we have not already done so. */
1111 if ((!constant_only
|| address_of_constant
) && require_lvalue
< 0)
1113 = lvalue_required_p (gnat_node
, gnu_result_type
, true,
1114 address_of_constant
, Is_Aliased (gnat_temp
));
1116 /* ??? We need to unshare the initializer if the object is external
1117 as such objects are not marked for unsharing if we are not at the
1118 global level. This should be fixed in add_decl_expr. */
1119 if ((constant_only
&& !address_of_constant
) || !require_lvalue
)
1120 gnu_result
= unshare_expr (DECL_INITIAL (gnu_result
));
1123 *gnu_result_type_p
= gnu_result_type
;
1128 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Pragma. Return
1129 any statements we generate. */
1132 Pragma_to_gnu (Node_Id gnat_node
)
1135 tree gnu_result
= alloc_stmt_list ();
1137 /* Check for (and ignore) unrecognized pragma and do nothing if we are just
1138 annotating types. */
1139 if (type_annotate_only
1140 || !Is_Pragma_Name (Chars (Pragma_Identifier (gnat_node
))))
1143 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
))))
1145 case Pragma_Inspection_Point
:
1146 /* Do nothing at top level: all such variables are already viewable. */
1147 if (global_bindings_p ())
1150 for (gnat_temp
= First (Pragma_Argument_Associations (gnat_node
));
1151 Present (gnat_temp
);
1152 gnat_temp
= Next (gnat_temp
))
1154 Node_Id gnat_expr
= Expression (gnat_temp
);
1155 tree gnu_expr
= gnat_to_gnu (gnat_expr
);
1157 enum machine_mode mode
;
1158 tree asm_constraint
= NULL_TREE
;
1159 #ifdef ASM_COMMENT_START
1163 if (TREE_CODE (gnu_expr
) == UNCONSTRAINED_ARRAY_REF
)
1164 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
1166 /* Use the value only if it fits into a normal register,
1167 otherwise use the address. */
1168 mode
= TYPE_MODE (TREE_TYPE (gnu_expr
));
1169 use_address
= ((GET_MODE_CLASS (mode
) != MODE_INT
1170 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
1171 || GET_MODE_SIZE (mode
) > UNITS_PER_WORD
);
1174 gnu_expr
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
1176 #ifdef ASM_COMMENT_START
1177 comment
= concat (ASM_COMMENT_START
,
1178 " inspection point: ",
1179 Get_Name_String (Chars (gnat_expr
)),
1180 use_address
? " address" : "",
1183 asm_constraint
= build_string (strlen (comment
), comment
);
1186 gnu_expr
= build5 (ASM_EXPR
, void_type_node
,
1190 (build_tree_list (NULL_TREE
,
1191 build_string (1, "g")),
1192 gnu_expr
, NULL_TREE
),
1193 NULL_TREE
, NULL_TREE
);
1194 ASM_VOLATILE_P (gnu_expr
) = 1;
1195 set_expr_location_from_node (gnu_expr
, gnat_node
);
1196 append_to_statement_list (gnu_expr
, &gnu_result
);
1200 case Pragma_Optimize
:
1201 switch (Chars (Expression
1202 (First (Pragma_Argument_Associations (gnat_node
)))))
1204 case Name_Time
: case Name_Space
:
1206 post_error ("insufficient -O value?", gnat_node
);
1211 post_error ("must specify -O0?", gnat_node
);
1219 case Pragma_Reviewable
:
1220 if (write_symbols
== NO_DEBUG
)
1221 post_error ("must specify -g?", gnat_node
);
1228 /* Subroutine of gnat_to_gnu to translate GNAT_NODE, an N_Attribute node,
1229 to a GCC tree, which is returned. GNU_RESULT_TYPE_P is a pointer to
1230 where we should place the result type. ATTRIBUTE is the attribute ID. */
1233 Attribute_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
, int attribute
)
1235 tree gnu_prefix
= gnat_to_gnu (Prefix (gnat_node
));
1236 tree gnu_type
= TREE_TYPE (gnu_prefix
);
1237 tree gnu_expr
, gnu_result_type
, gnu_result
= error_mark_node
;
1238 bool prefix_unused
= false;
1240 /* If the input is a NULL_EXPR, make a new one. */
1241 if (TREE_CODE (gnu_prefix
) == NULL_EXPR
)
1243 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1244 *gnu_result_type_p
= gnu_result_type
;
1245 return build1 (NULL_EXPR
, gnu_result_type
, TREE_OPERAND (gnu_prefix
, 0));
1252 /* These are just conversions since representation clauses for
1253 enumeration types are handled in the front-end. */
1255 bool checkp
= Do_Range_Check (First (Expressions (gnat_node
)));
1256 gnu_result
= gnat_to_gnu (First (Expressions (gnat_node
)));
1257 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1258 gnu_result
= convert_with_check (Etype (gnat_node
), gnu_result
,
1259 checkp
, checkp
, true, gnat_node
);
1265 /* These just add or subtract the constant 1 since representation
1266 clauses for enumeration types are handled in the front-end. */
1267 gnu_expr
= gnat_to_gnu (First (Expressions (gnat_node
)));
1268 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1270 if (Do_Range_Check (First (Expressions (gnat_node
))))
1272 gnu_expr
= gnat_protect_expr (gnu_expr
);
1275 (build_binary_op (EQ_EXPR
, boolean_type_node
,
1277 attribute
== Attr_Pred
1278 ? TYPE_MIN_VALUE (gnu_result_type
)
1279 : TYPE_MAX_VALUE (gnu_result_type
)),
1280 gnu_expr
, CE_Range_Check_Failed
, gnat_node
);
1284 = build_binary_op (attribute
== Attr_Pred
? MINUS_EXPR
: PLUS_EXPR
,
1285 gnu_result_type
, gnu_expr
,
1286 convert (gnu_result_type
, integer_one_node
));
1290 case Attr_Unrestricted_Access
:
1291 /* Conversions don't change addresses but can cause us to miss the
1292 COMPONENT_REF case below, so strip them off. */
1293 gnu_prefix
= remove_conversions (gnu_prefix
,
1294 !Must_Be_Byte_Aligned (gnat_node
));
1296 /* If we are taking 'Address of an unconstrained object, this is the
1297 pointer to the underlying array. */
1298 if (attribute
== Attr_Address
)
1299 gnu_prefix
= maybe_unconstrained_array (gnu_prefix
);
1301 /* If we are building a static dispatch table, we have to honor
1302 TARGET_VTABLE_USES_DESCRIPTORS if we want to be compatible
1303 with the C++ ABI. We do it in the non-static case as well,
1304 see gnat_to_gnu_entity, case E_Access_Subprogram_Type. */
1305 else if (TARGET_VTABLE_USES_DESCRIPTORS
1306 && Is_Dispatch_Table_Entity (Etype (gnat_node
)))
1309 /* Descriptors can only be built here for top-level functions. */
1310 bool build_descriptor
= (global_bindings_p () != 0);
1312 VEC(constructor_elt
,gc
) *gnu_vec
= NULL
;
1313 constructor_elt
*elt
;
1315 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1317 /* If we're not going to build the descriptor, we have to retrieve
1318 the one which will be built by the linker (or by the compiler
1319 later if a static chain is requested). */
1320 if (!build_descriptor
)
1322 gnu_result
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_prefix
);
1323 gnu_result
= fold_convert (build_pointer_type (gnu_result_type
),
1325 gnu_result
= build1 (INDIRECT_REF
, gnu_result_type
, gnu_result
);
1328 VEC_safe_grow (constructor_elt
, gc
, gnu_vec
,
1329 TARGET_VTABLE_USES_DESCRIPTORS
);
1330 elt
= (VEC_address (constructor_elt
, gnu_vec
)
1331 + TARGET_VTABLE_USES_DESCRIPTORS
- 1);
1332 for (gnu_field
= TYPE_FIELDS (gnu_result_type
), i
= 0;
1333 i
< TARGET_VTABLE_USES_DESCRIPTORS
;
1334 gnu_field
= DECL_CHAIN (gnu_field
), i
++)
1336 if (build_descriptor
)
1338 t
= build2 (FDESC_EXPR
, TREE_TYPE (gnu_field
), gnu_prefix
,
1339 build_int_cst (NULL_TREE
, i
));
1340 TREE_CONSTANT (t
) = 1;
1343 t
= build3 (COMPONENT_REF
, ptr_void_ftype
, gnu_result
,
1344 gnu_field
, NULL_TREE
);
1346 elt
->index
= gnu_field
;
1351 gnu_result
= gnat_build_constructor (gnu_result_type
, gnu_vec
);
1355 /* ... fall through ... */
1358 case Attr_Unchecked_Access
:
1359 case Attr_Code_Address
:
1360 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1362 = build_unary_op (((attribute
== Attr_Address
1363 || attribute
== Attr_Unrestricted_Access
)
1364 && !Must_Be_Byte_Aligned (gnat_node
))
1365 ? ATTR_ADDR_EXPR
: ADDR_EXPR
,
1366 gnu_result_type
, gnu_prefix
);
1368 /* For 'Code_Address, find an inner ADDR_EXPR and mark it so that we
1369 don't try to build a trampoline. */
1370 if (attribute
== Attr_Code_Address
)
1372 gnu_expr
= remove_conversions (gnu_result
, false);
1374 if (TREE_CODE (gnu_expr
) == ADDR_EXPR
)
1375 TREE_NO_TRAMPOLINE (gnu_expr
) = TREE_CONSTANT (gnu_expr
) = 1;
1378 /* For other address attributes applied to a nested function,
1379 find an inner ADDR_EXPR and annotate it so that we can issue
1380 a useful warning with -Wtrampolines. */
1381 else if (TREE_CODE (TREE_TYPE (gnu_prefix
)) == FUNCTION_TYPE
)
1383 gnu_expr
= remove_conversions (gnu_result
, false);
1385 if (TREE_CODE (gnu_expr
) == ADDR_EXPR
1386 && decl_function_context (TREE_OPERAND (gnu_expr
, 0)))
1388 set_expr_location_from_node (gnu_expr
, gnat_node
);
1390 /* Check that we're not violating the No_Implicit_Dynamic_Code
1391 restriction. Be conservative if we don't know anything
1392 about the trampoline strategy for the target. */
1393 Check_Implicit_Dynamic_Code_Allowed (gnat_node
);
1398 case Attr_Pool_Address
:
1401 tree gnu_ptr
= gnu_prefix
;
1403 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1405 /* If this is an unconstrained array, we know the object has been
1406 allocated with the template in front of the object. So compute
1407 the template address. */
1408 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr
)))
1410 = convert (build_pointer_type
1411 (TYPE_OBJECT_RECORD_TYPE
1412 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr
)))),
1415 gnu_obj_type
= TREE_TYPE (TREE_TYPE (gnu_ptr
));
1416 if (TREE_CODE (gnu_obj_type
) == RECORD_TYPE
1417 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type
))
1419 tree gnu_char_ptr_type
1420 = build_pointer_type (unsigned_char_type_node
);
1421 tree gnu_pos
= byte_position (TYPE_FIELDS (gnu_obj_type
));
1422 gnu_ptr
= convert (gnu_char_ptr_type
, gnu_ptr
);
1423 gnu_ptr
= build_binary_op (POINTER_PLUS_EXPR
, gnu_char_ptr_type
,
1427 gnu_result
= convert (gnu_result_type
, gnu_ptr
);
1432 case Attr_Object_Size
:
1433 case Attr_Value_Size
:
1434 case Attr_Max_Size_In_Storage_Elements
:
1435 gnu_expr
= gnu_prefix
;
1437 /* Remove NOPs and conversions between original and packable version
1438 from GNU_EXPR, and conversions from GNU_PREFIX. We use GNU_EXPR
1439 to see if a COMPONENT_REF was involved. */
1440 while (TREE_CODE (gnu_expr
) == NOP_EXPR
1441 || (TREE_CODE (gnu_expr
) == VIEW_CONVERT_EXPR
1442 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
1443 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0)))
1445 && TYPE_NAME (TREE_TYPE (gnu_expr
))
1446 == TYPE_NAME (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0)))))
1447 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
1449 gnu_prefix
= remove_conversions (gnu_prefix
, true);
1450 prefix_unused
= true;
1451 gnu_type
= TREE_TYPE (gnu_prefix
);
1453 /* Replace an unconstrained array type with the type of the underlying
1454 array. We can't do this with a call to maybe_unconstrained_array
1455 since we may have a TYPE_DECL. For 'Max_Size_In_Storage_Elements,
1456 use the record type that will be used to allocate the object and its
1458 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
1460 gnu_type
= TYPE_OBJECT_RECORD_TYPE (gnu_type
);
1461 if (attribute
!= Attr_Max_Size_In_Storage_Elements
)
1462 gnu_type
= TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
)));
1465 /* If we're looking for the size of a field, return the field size.
1466 Otherwise, if the prefix is an object, or if we're looking for
1467 'Object_Size or 'Max_Size_In_Storage_Elements, the result is the
1468 GCC size of the type. Otherwise, it is the RM size of the type. */
1469 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
1470 gnu_result
= DECL_SIZE (TREE_OPERAND (gnu_prefix
, 1));
1471 else if (TREE_CODE (gnu_prefix
) != TYPE_DECL
1472 || attribute
== Attr_Object_Size
1473 || attribute
== Attr_Max_Size_In_Storage_Elements
)
1475 /* If the prefix is an object of a padded type, the GCC size isn't
1476 relevant to the programmer. Normally what we want is the RM size,
1477 which was set from the specified size, but if it was not set, we
1478 want the size of the field. Using the MAX of those two produces
1479 the right result in all cases. Don't use the size of the field
1480 if it's self-referential, since that's never what's wanted. */
1481 if (TREE_CODE (gnu_prefix
) != TYPE_DECL
1482 && TYPE_IS_PADDING_P (gnu_type
)
1483 && TREE_CODE (gnu_expr
) == COMPONENT_REF
)
1485 gnu_result
= rm_size (gnu_type
);
1486 if (!CONTAINS_PLACEHOLDER_P
1487 (DECL_SIZE (TREE_OPERAND (gnu_expr
, 1))))
1489 = size_binop (MAX_EXPR
, gnu_result
,
1490 DECL_SIZE (TREE_OPERAND (gnu_expr
, 1)));
1492 else if (Nkind (Prefix (gnat_node
)) == N_Explicit_Dereference
)
1494 Node_Id gnat_deref
= Prefix (gnat_node
);
1495 Node_Id gnat_actual_subtype
1496 = Actual_Designated_Subtype (gnat_deref
);
1498 = TREE_TYPE (gnat_to_gnu (Prefix (gnat_deref
)));
1500 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type
)
1501 && Present (gnat_actual_subtype
))
1503 tree gnu_actual_obj_type
1504 = gnat_to_gnu_type (gnat_actual_subtype
);
1506 = build_unc_object_type_from_ptr (gnu_ptr_type
,
1507 gnu_actual_obj_type
,
1508 get_identifier ("SIZE"),
1512 gnu_result
= TYPE_SIZE (gnu_type
);
1515 gnu_result
= TYPE_SIZE (gnu_type
);
1518 gnu_result
= rm_size (gnu_type
);
1520 /* Deal with a self-referential size by returning the maximum size for
1521 a type and by qualifying the size with the object otherwise. */
1522 if (CONTAINS_PLACEHOLDER_P (gnu_result
))
1524 if (TREE_CODE (gnu_prefix
) == TYPE_DECL
)
1525 gnu_result
= max_size (gnu_result
, true);
1527 gnu_result
= substitute_placeholder_in_expr (gnu_result
, gnu_expr
);
1530 /* If the type contains a template, subtract its size. */
1531 if (TREE_CODE (gnu_type
) == RECORD_TYPE
1532 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
1533 gnu_result
= size_binop (MINUS_EXPR
, gnu_result
,
1534 DECL_SIZE (TYPE_FIELDS (gnu_type
)));
1536 /* For 'Max_Size_In_Storage_Elements, adjust the unit. */
1537 if (attribute
== Attr_Max_Size_In_Storage_Elements
)
1538 gnu_result
= size_binop (CEIL_DIV_EXPR
, gnu_result
, bitsize_unit_node
);
1540 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1543 case Attr_Alignment
:
1547 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
1548 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0))))
1549 gnu_prefix
= TREE_OPERAND (gnu_prefix
, 0);
1551 gnu_type
= TREE_TYPE (gnu_prefix
);
1552 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1553 prefix_unused
= true;
1555 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
1556 align
= DECL_ALIGN (TREE_OPERAND (gnu_prefix
, 1)) / BITS_PER_UNIT
;
1559 Node_Id gnat_prefix
= Prefix (gnat_node
);
1560 Entity_Id gnat_type
= Etype (gnat_prefix
);
1561 unsigned int double_align
;
1562 bool is_capped_double
, align_clause
;
1564 /* If the default alignment of "double" or larger scalar types is
1565 specifically capped and there is an alignment clause neither
1566 on the type nor on the prefix itself, return the cap. */
1567 if ((double_align
= double_float_alignment
) > 0)
1569 = is_double_float_or_array (gnat_type
, &align_clause
);
1570 else if ((double_align
= double_scalar_alignment
) > 0)
1572 = is_double_scalar_or_array (gnat_type
, &align_clause
);
1574 is_capped_double
= align_clause
= false;
1576 if (is_capped_double
1577 && Nkind (gnat_prefix
) == N_Identifier
1578 && Present (Alignment_Clause (Entity (gnat_prefix
))))
1579 align_clause
= true;
1581 if (is_capped_double
&& !align_clause
)
1582 align
= double_align
;
1584 align
= TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
;
1587 gnu_result
= size_int (align
);
1593 case Attr_Range_Length
:
1594 prefix_unused
= true;
1596 if (INTEGRAL_TYPE_P (gnu_type
) || TREE_CODE (gnu_type
) == REAL_TYPE
)
1598 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1600 if (attribute
== Attr_First
)
1601 gnu_result
= TYPE_MIN_VALUE (gnu_type
);
1602 else if (attribute
== Attr_Last
)
1603 gnu_result
= TYPE_MAX_VALUE (gnu_type
);
1607 (MAX_EXPR
, get_base_type (gnu_result_type
),
1609 (PLUS_EXPR
, get_base_type (gnu_result_type
),
1610 build_binary_op (MINUS_EXPR
,
1611 get_base_type (gnu_result_type
),
1612 convert (gnu_result_type
,
1613 TYPE_MAX_VALUE (gnu_type
)),
1614 convert (gnu_result_type
,
1615 TYPE_MIN_VALUE (gnu_type
))),
1616 convert (gnu_result_type
, integer_one_node
)),
1617 convert (gnu_result_type
, integer_zero_node
));
1622 /* ... fall through ... */
1626 int Dimension
= (Present (Expressions (gnat_node
))
1627 ? UI_To_Int (Intval (First (Expressions (gnat_node
))))
1629 struct parm_attr_d
*pa
= NULL
;
1630 Entity_Id gnat_param
= Empty
;
1632 /* Make sure any implicit dereference gets done. */
1633 gnu_prefix
= maybe_implicit_deref (gnu_prefix
);
1634 gnu_prefix
= maybe_unconstrained_array (gnu_prefix
);
1636 /* We treat unconstrained array In parameters specially. */
1637 if (!Is_Constrained (Etype (Prefix (gnat_node
))))
1639 Node_Id gnat_prefix
= Prefix (gnat_node
);
1641 /* This is the direct case. */
1642 if (Nkind (gnat_prefix
) == N_Identifier
1643 && Ekind (Entity (gnat_prefix
)) == E_In_Parameter
)
1644 gnat_param
= Entity (gnat_prefix
);
1646 /* This is the indirect case. Note that we need to be sure that
1647 the access value cannot be null as we'll hoist the load. */
1648 if (Nkind (gnat_prefix
) == N_Explicit_Dereference
1649 && Nkind (Prefix (gnat_prefix
)) == N_Identifier
1650 && Ekind (Entity (Prefix (gnat_prefix
))) == E_In_Parameter
1651 && Can_Never_Be_Null (Entity (Prefix (gnat_prefix
))))
1652 gnat_param
= Entity (Prefix (gnat_prefix
));
1655 gnu_type
= TREE_TYPE (gnu_prefix
);
1656 prefix_unused
= true;
1657 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1659 if (TYPE_CONVENTION_FORTRAN_P (gnu_type
))
1664 for (ndim
= 1, gnu_type_temp
= gnu_type
;
1665 TREE_CODE (TREE_TYPE (gnu_type_temp
)) == ARRAY_TYPE
1666 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type_temp
));
1667 ndim
++, gnu_type_temp
= TREE_TYPE (gnu_type_temp
))
1670 Dimension
= ndim
+ 1 - Dimension
;
1673 for (i
= 1; i
< Dimension
; i
++)
1674 gnu_type
= TREE_TYPE (gnu_type
);
1676 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
1678 /* When not optimizing, look up the slot associated with the parameter
1679 and the dimension in the cache and create a new one on failure. */
1680 if (!optimize
&& Present (gnat_param
))
1682 FOR_EACH_VEC_ELT (parm_attr
, f_parm_attr_cache
, i
, pa
)
1683 if (pa
->id
== gnat_param
&& pa
->dim
== Dimension
)
1688 pa
= ggc_alloc_cleared_parm_attr_d ();
1689 pa
->id
= gnat_param
;
1690 pa
->dim
= Dimension
;
1691 VEC_safe_push (parm_attr
, gc
, f_parm_attr_cache
, pa
);
1695 /* Return the cached expression or build a new one. */
1696 if (attribute
== Attr_First
)
1698 if (pa
&& pa
->first
)
1700 gnu_result
= pa
->first
;
1705 = TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)));
1708 else if (attribute
== Attr_Last
)
1712 gnu_result
= pa
->last
;
1717 = TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
)));
1720 else /* attribute == Attr_Range_Length || attribute == Attr_Length */
1722 if (pa
&& pa
->length
)
1724 gnu_result
= pa
->length
;
1729 /* We used to compute the length as max (hb - lb + 1, 0),
1730 which could overflow for some cases of empty arrays, e.g.
1731 when lb == index_type'first. We now compute the length as
1732 (hb >= lb) ? hb - lb + 1 : 0, which would only overflow in
1733 much rarer cases, for extremely large arrays we expect
1734 never to encounter in practice. In addition, the former
1735 computation required the use of potentially constraining
1736 signed arithmetic while the latter doesn't. Note that
1737 the comparison must be done in the original index type,
1738 to avoid any overflow during the conversion. */
1739 tree comp_type
= get_base_type (gnu_result_type
);
1740 tree index_type
= TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
));
1741 tree lb
= TYPE_MIN_VALUE (index_type
);
1742 tree hb
= TYPE_MAX_VALUE (index_type
);
1744 = build_binary_op (PLUS_EXPR
, comp_type
,
1745 build_binary_op (MINUS_EXPR
,
1747 convert (comp_type
, hb
),
1748 convert (comp_type
, lb
)),
1749 convert (comp_type
, integer_one_node
));
1751 = build_cond_expr (comp_type
,
1752 build_binary_op (GE_EXPR
,
1756 convert (comp_type
, integer_zero_node
));
1760 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
1761 handling. Note that these attributes could not have been used on
1762 an unconstrained array type. */
1763 gnu_result
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result
, gnu_prefix
);
1765 /* Cache the expression we have just computed. Since we want to do it
1766 at run time, we force the use of a SAVE_EXPR and let the gimplifier
1767 create the temporary in the outermost binding level. We will make
1768 sure in Subprogram_Body_to_gnu that it is evaluated on all possible
1769 paths by forcing its evaluation on entry of the function. */
1773 = build1 (SAVE_EXPR
, TREE_TYPE (gnu_result
), gnu_result
);
1774 if (attribute
== Attr_First
)
1775 pa
->first
= gnu_result
;
1776 else if (attribute
== Attr_Last
)
1777 pa
->last
= gnu_result
;
1779 pa
->length
= gnu_result
;
1782 /* Set the source location onto the predicate of the condition in the
1783 'Length case but do not do it if the expression is cached to avoid
1784 messing up the debug info. */
1785 else if ((attribute
== Attr_Range_Length
|| attribute
== Attr_Length
)
1786 && TREE_CODE (gnu_result
) == COND_EXPR
1787 && EXPR_P (TREE_OPERAND (gnu_result
, 0)))
1788 set_expr_location_from_node (TREE_OPERAND (gnu_result
, 0),
1794 case Attr_Bit_Position
:
1796 case Attr_First_Bit
:
1800 HOST_WIDE_INT bitsize
;
1801 HOST_WIDE_INT bitpos
;
1803 tree gnu_field_bitpos
;
1804 tree gnu_field_offset
;
1806 enum machine_mode mode
;
1807 int unsignedp
, volatilep
;
1809 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1810 gnu_prefix
= remove_conversions (gnu_prefix
, true);
1811 prefix_unused
= true;
1813 /* We can have 'Bit on any object, but if it isn't a COMPONENT_REF,
1814 the result is 0. Don't allow 'Bit on a bare component, though. */
1815 if (attribute
== Attr_Bit
1816 && TREE_CODE (gnu_prefix
) != COMPONENT_REF
1817 && TREE_CODE (gnu_prefix
) != FIELD_DECL
)
1819 gnu_result
= integer_zero_node
;
1824 gcc_assert (TREE_CODE (gnu_prefix
) == COMPONENT_REF
1825 || (attribute
== Attr_Bit_Position
1826 && TREE_CODE (gnu_prefix
) == FIELD_DECL
));
1828 get_inner_reference (gnu_prefix
, &bitsize
, &bitpos
, &gnu_offset
,
1829 &mode
, &unsignedp
, &volatilep
, false);
1831 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
)
1833 gnu_field_bitpos
= bit_position (TREE_OPERAND (gnu_prefix
, 1));
1834 gnu_field_offset
= byte_position (TREE_OPERAND (gnu_prefix
, 1));
1836 for (gnu_inner
= TREE_OPERAND (gnu_prefix
, 0);
1837 TREE_CODE (gnu_inner
) == COMPONENT_REF
1838 && DECL_INTERNAL_P (TREE_OPERAND (gnu_inner
, 1));
1839 gnu_inner
= TREE_OPERAND (gnu_inner
, 0))
1842 = size_binop (PLUS_EXPR
, gnu_field_bitpos
,
1843 bit_position (TREE_OPERAND (gnu_inner
, 1)));
1845 = size_binop (PLUS_EXPR
, gnu_field_offset
,
1846 byte_position (TREE_OPERAND (gnu_inner
, 1)));
1849 else if (TREE_CODE (gnu_prefix
) == FIELD_DECL
)
1851 gnu_field_bitpos
= bit_position (gnu_prefix
);
1852 gnu_field_offset
= byte_position (gnu_prefix
);
1856 gnu_field_bitpos
= bitsize_zero_node
;
1857 gnu_field_offset
= size_zero_node
;
1863 gnu_result
= gnu_field_offset
;
1866 case Attr_First_Bit
:
1868 gnu_result
= size_int (bitpos
% BITS_PER_UNIT
);
1872 gnu_result
= bitsize_int (bitpos
% BITS_PER_UNIT
);
1873 gnu_result
= size_binop (PLUS_EXPR
, gnu_result
,
1874 TYPE_SIZE (TREE_TYPE (gnu_prefix
)));
1875 gnu_result
= size_binop (MINUS_EXPR
, gnu_result
,
1879 case Attr_Bit_Position
:
1880 gnu_result
= gnu_field_bitpos
;
1884 /* If this has a PLACEHOLDER_EXPR, qualify it by the object we are
1886 gnu_result
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_result
, gnu_prefix
);
1893 tree gnu_lhs
= gnat_to_gnu (First (Expressions (gnat_node
)));
1894 tree gnu_rhs
= gnat_to_gnu (Next (First (Expressions (gnat_node
))));
1896 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1897 gnu_result
= build_binary_op (attribute
== Attr_Min
1898 ? MIN_EXPR
: MAX_EXPR
,
1899 gnu_result_type
, gnu_lhs
, gnu_rhs
);
1903 case Attr_Passed_By_Reference
:
1904 gnu_result
= size_int (default_pass_by_ref (gnu_type
)
1905 || must_pass_by_ref (gnu_type
));
1906 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1909 case Attr_Component_Size
:
1910 if (TREE_CODE (gnu_prefix
) == COMPONENT_REF
1911 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_prefix
, 0))))
1912 gnu_prefix
= TREE_OPERAND (gnu_prefix
, 0);
1914 gnu_prefix
= maybe_implicit_deref (gnu_prefix
);
1915 gnu_type
= TREE_TYPE (gnu_prefix
);
1917 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
1918 gnu_type
= TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_type
))));
1920 while (TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
1921 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
)))
1922 gnu_type
= TREE_TYPE (gnu_type
);
1924 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
1926 /* Note this size cannot be self-referential. */
1927 gnu_result
= TYPE_SIZE (TREE_TYPE (gnu_type
));
1928 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1929 prefix_unused
= true;
1932 case Attr_Descriptor_Size
:
1933 gnu_type
= TREE_TYPE (gnu_prefix
);
1934 gcc_assert (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
);
1936 /* What we want is the offset of the ARRAY field in the record that the
1937 thin pointer designates, but the components have been shifted so this
1938 is actually the opposite of the offset of the BOUNDS field. */
1939 gnu_type
= TYPE_OBJECT_RECORD_TYPE (gnu_type
);
1940 gnu_result
= size_binop (MINUS_EXPR
, bitsize_zero_node
,
1941 bit_position (TYPE_FIELDS (gnu_type
)));
1942 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1943 prefix_unused
= true;
1946 case Attr_Null_Parameter
:
1947 /* This is just a zero cast to the pointer type for our prefix and
1949 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1951 = build_unary_op (INDIRECT_REF
, NULL_TREE
,
1952 convert (build_pointer_type (gnu_result_type
),
1953 integer_zero_node
));
1954 TREE_PRIVATE (gnu_result
) = 1;
1957 case Attr_Mechanism_Code
:
1960 Entity_Id gnat_obj
= Entity (Prefix (gnat_node
));
1962 prefix_unused
= true;
1963 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1964 if (Present (Expressions (gnat_node
)))
1966 int i
= UI_To_Int (Intval (First (Expressions (gnat_node
))));
1968 for (gnat_obj
= First_Formal (gnat_obj
); i
> 1;
1969 i
--, gnat_obj
= Next_Formal (gnat_obj
))
1973 code
= Mechanism (gnat_obj
);
1974 if (code
== Default
)
1975 code
= ((present_gnu_tree (gnat_obj
)
1976 && (DECL_BY_REF_P (get_gnu_tree (gnat_obj
))
1977 || ((TREE_CODE (get_gnu_tree (gnat_obj
))
1979 && (DECL_BY_COMPONENT_PTR_P
1980 (get_gnu_tree (gnat_obj
))))))
1981 ? By_Reference
: By_Copy
);
1982 gnu_result
= convert (gnu_result_type
, size_int (- code
));
1987 /* Say we have an unimplemented attribute. Then set the value to be
1988 returned to be a zero and hope that's something we can convert to
1989 the type of this attribute. */
1990 post_error ("unimplemented attribute", gnat_node
);
1991 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
1992 gnu_result
= integer_zero_node
;
1996 /* If this is an attribute where the prefix was unused, force a use of it if
1997 it has a side-effect. But don't do it if the prefix is just an entity
1998 name. However, if an access check is needed, we must do it. See second
1999 example in AARM 11.6(5.e). */
2000 if (prefix_unused
&& TREE_SIDE_EFFECTS (gnu_prefix
)
2001 && !Is_Entity_Name (Prefix (gnat_node
)))
2002 gnu_result
= build_compound_expr (TREE_TYPE (gnu_result
), gnu_prefix
,
2005 *gnu_result_type_p
= gnu_result_type
;
2009 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Case_Statement,
2010 to a GCC tree, which is returned. */
2013 Case_Statement_to_gnu (Node_Id gnat_node
)
2015 tree gnu_result
, gnu_expr
, gnu_label
;
2017 location_t end_locus
;
2018 bool may_fallthru
= false;
2020 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
2021 gnu_expr
= convert (get_base_type (TREE_TYPE (gnu_expr
)), gnu_expr
);
2023 /* The range of values in a case statement is determined by the rules in
2024 RM 5.4(7-9). In almost all cases, this range is represented by the Etype
2025 of the expression. One exception arises in the case of a simple name that
2026 is parenthesized. This still has the Etype of the name, but since it is
2027 not a name, para 7 does not apply, and we need to go to the base type.
2028 This is the only case where parenthesization affects the dynamic
2029 semantics (i.e. the range of possible values at run time that is covered
2030 by the others alternative).
2032 Another exception is if the subtype of the expression is non-static. In
2033 that case, we also have to use the base type. */
2034 if (Paren_Count (Expression (gnat_node
)) != 0
2035 || !Is_OK_Static_Subtype (Underlying_Type
2036 (Etype (Expression (gnat_node
)))))
2037 gnu_expr
= convert (get_base_type (TREE_TYPE (gnu_expr
)), gnu_expr
);
2039 /* We build a SWITCH_EXPR that contains the code with interspersed
2040 CASE_LABEL_EXPRs for each label. */
2041 if (!Sloc_to_locus (Sloc (gnat_node
) + UI_To_Int (End_Span (gnat_node
)),
2043 end_locus
= input_location
;
2044 gnu_label
= create_artificial_label (end_locus
);
2045 start_stmt_group ();
2047 for (gnat_when
= First_Non_Pragma (Alternatives (gnat_node
));
2048 Present (gnat_when
);
2049 gnat_when
= Next_Non_Pragma (gnat_when
))
2051 bool choices_added_p
= false;
2052 Node_Id gnat_choice
;
2054 /* First compile all the different case choices for the current WHEN
2056 for (gnat_choice
= First (Discrete_Choices (gnat_when
));
2057 Present (gnat_choice
); gnat_choice
= Next (gnat_choice
))
2059 tree gnu_low
= NULL_TREE
, gnu_high
= NULL_TREE
;
2061 switch (Nkind (gnat_choice
))
2064 gnu_low
= gnat_to_gnu (Low_Bound (gnat_choice
));
2065 gnu_high
= gnat_to_gnu (High_Bound (gnat_choice
));
2068 case N_Subtype_Indication
:
2069 gnu_low
= gnat_to_gnu (Low_Bound (Range_Expression
2070 (Constraint (gnat_choice
))));
2071 gnu_high
= gnat_to_gnu (High_Bound (Range_Expression
2072 (Constraint (gnat_choice
))));
2076 case N_Expanded_Name
:
2077 /* This represents either a subtype range or a static value of
2078 some kind; Ekind says which. */
2079 if (IN (Ekind (Entity (gnat_choice
)), Type_Kind
))
2081 tree gnu_type
= get_unpadded_type (Entity (gnat_choice
));
2083 gnu_low
= fold (TYPE_MIN_VALUE (gnu_type
));
2084 gnu_high
= fold (TYPE_MAX_VALUE (gnu_type
));
2088 /* ... fall through ... */
2090 case N_Character_Literal
:
2091 case N_Integer_Literal
:
2092 gnu_low
= gnat_to_gnu (gnat_choice
);
2095 case N_Others_Choice
:
2102 /* If the case value is a subtype that raises Constraint_Error at
2103 run time because of a wrong bound, then gnu_low or gnu_high is
2104 not translated into an INTEGER_CST. In such a case, we need
2105 to ensure that the when statement is not added in the tree,
2106 otherwise it will crash the gimplifier. */
2107 if ((!gnu_low
|| TREE_CODE (gnu_low
) == INTEGER_CST
)
2108 && (!gnu_high
|| TREE_CODE (gnu_high
) == INTEGER_CST
))
2110 add_stmt_with_node (build_case_label
2112 create_artificial_label (input_location
)),
2114 choices_added_p
= true;
2118 /* Push a binding level here in case variables are declared as we want
2119 them to be local to this set of statements instead of to the block
2120 containing the Case statement. */
2121 if (choices_added_p
)
2123 tree group
= build_stmt_group (Statements (gnat_when
), true);
2124 bool group_may_fallthru
= block_may_fallthru (group
);
2126 if (group_may_fallthru
)
2128 tree stmt
= build1 (GOTO_EXPR
, void_type_node
, gnu_label
);
2129 SET_EXPR_LOCATION (stmt
, end_locus
);
2131 may_fallthru
= true;
2136 /* Now emit a definition of the label the cases branch to, if any. */
2138 add_stmt (build1 (LABEL_EXPR
, void_type_node
, gnu_label
));
2139 gnu_result
= build3 (SWITCH_EXPR
, TREE_TYPE (gnu_expr
), gnu_expr
,
2140 end_stmt_group (), NULL_TREE
);
2145 /* Find out whether VAR is an iteration variable of an enclosing loop in the
2146 current function. If so, push a range_check_info structure onto the stack
2147 of this enclosing loop and return it. Otherwise, return NULL. */
2149 static struct range_check_info_d
*
2150 push_range_check_info (tree var
)
2152 struct loop_info_d
*iter
= NULL
;
2155 if (VEC_empty (loop_info
, gnu_loop_stack
))
2158 var
= remove_conversions (var
, false);
2160 if (TREE_CODE (var
) != VAR_DECL
)
2163 if (decl_function_context (var
) != current_function_decl
)
2166 for (i
= VEC_length (loop_info
, gnu_loop_stack
) - 1;
2167 VEC_iterate (loop_info
, gnu_loop_stack
, i
, iter
);
2169 if (var
== iter
->loop_var
)
2174 struct range_check_info_d
*rci
= ggc_alloc_range_check_info_d ();
2175 VEC_safe_push (range_check_info
, gc
, iter
->checks
, rci
);
2182 /* Return true if VAL (of type TYPE) can equal the minimum value if MAX is
2183 false, or the maximum value if MAX is true, of TYPE. */
2186 can_equal_min_or_max_val_p (tree val
, tree type
, bool max
)
2188 tree min_or_max_val
= (max
? TYPE_MAX_VALUE (type
) : TYPE_MIN_VALUE (type
));
2190 if (TREE_CODE (min_or_max_val
) != INTEGER_CST
)
2193 if (TREE_CODE (val
) == NOP_EXPR
)
2195 ? TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val
, 0)))
2196 : TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val
, 0))));
2198 if (TREE_CODE (val
) != INTEGER_CST
)
2201 return tree_int_cst_equal (val
, min_or_max_val
) == 1;
2204 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.
2205 If REVERSE is true, minimum value is taken as maximum value. */
2208 can_equal_min_val_p (tree val
, tree type
, bool reverse
)
2210 return can_equal_min_or_max_val_p (val
, type
, reverse
);
2213 /* Return true if VAL (of type TYPE) can equal the maximum value of TYPE.
2214 If REVERSE is true, maximum value is taken as minimum value. */
2217 can_equal_max_val_p (tree val
, tree type
, bool reverse
)
2219 return can_equal_min_or_max_val_p (val
, type
, !reverse
);
2222 /* Return true if VAL1 can be lower than VAL2. */
2225 can_be_lower_p (tree val1
, tree val2
)
2227 if (TREE_CODE (val1
) == NOP_EXPR
)
2228 val1
= TYPE_MIN_VALUE (TREE_TYPE (TREE_OPERAND (val1
, 0)));
2230 if (TREE_CODE (val1
) != INTEGER_CST
)
2233 if (TREE_CODE (val2
) == NOP_EXPR
)
2234 val2
= TYPE_MAX_VALUE (TREE_TYPE (TREE_OPERAND (val2
, 0)));
2236 if (TREE_CODE (val2
) != INTEGER_CST
)
2239 return tree_int_cst_lt (val1
, val2
);
2242 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Loop_Statement,
2243 to a GCC tree, which is returned. */
2246 Loop_Statement_to_gnu (Node_Id gnat_node
)
2248 const Node_Id gnat_iter_scheme
= Iteration_Scheme (gnat_node
);
2249 struct loop_info_d
*gnu_loop_info
= ggc_alloc_cleared_loop_info_d ();
2250 tree gnu_loop_stmt
= build4 (LOOP_STMT
, void_type_node
, NULL_TREE
,
2251 NULL_TREE
, NULL_TREE
, NULL_TREE
);
2252 tree gnu_loop_label
= create_artificial_label (input_location
);
2253 tree gnu_cond_expr
= NULL_TREE
, gnu_low
= NULL_TREE
, gnu_high
= NULL_TREE
;
2256 /* Push the loop_info structure associated with the LOOP_STMT. */
2257 VEC_safe_push (loop_info
, gc
, gnu_loop_stack
, gnu_loop_info
);
2259 /* Set location information for statement and end label. */
2260 set_expr_location_from_node (gnu_loop_stmt
, gnat_node
);
2261 Sloc_to_locus (Sloc (End_Label (gnat_node
)),
2262 &DECL_SOURCE_LOCATION (gnu_loop_label
));
2263 LOOP_STMT_LABEL (gnu_loop_stmt
) = gnu_loop_label
;
2265 /* Save the label so that a corresponding N_Exit_Statement can find it. */
2266 gnu_loop_info
->label
= gnu_loop_label
;
2268 /* Set the condition under which the loop must keep going.
2269 For the case "LOOP .... END LOOP;" the condition is always true. */
2270 if (No (gnat_iter_scheme
))
2273 /* For the case "WHILE condition LOOP ..... END LOOP;" it's immediate. */
2274 else if (Present (Condition (gnat_iter_scheme
)))
2275 LOOP_STMT_COND (gnu_loop_stmt
)
2276 = gnat_to_gnu (Condition (gnat_iter_scheme
));
2278 /* Otherwise we have an iteration scheme and the condition is given by the
2279 bounds of the subtype of the iteration variable. */
2282 Node_Id gnat_loop_spec
= Loop_Parameter_Specification (gnat_iter_scheme
);
2283 Entity_Id gnat_loop_var
= Defining_Entity (gnat_loop_spec
);
2284 Entity_Id gnat_type
= Etype (gnat_loop_var
);
2285 tree gnu_type
= get_unpadded_type (gnat_type
);
2286 tree gnu_base_type
= get_base_type (gnu_type
);
2287 tree gnu_one_node
= convert (gnu_base_type
, integer_one_node
);
2288 tree gnu_loop_var
, gnu_loop_iv
, gnu_first
, gnu_last
, gnu_stmt
;
2289 enum tree_code update_code
, test_code
, shift_code
;
2290 bool reverse
= Reverse_Present (gnat_loop_spec
), use_iv
= false;
2292 gnu_low
= TYPE_MIN_VALUE (gnu_type
);
2293 gnu_high
= TYPE_MAX_VALUE (gnu_type
);
2295 /* We must disable modulo reduction for the iteration variable, if any,
2296 in order for the loop comparison to be effective. */
2299 gnu_first
= gnu_high
;
2301 update_code
= MINUS_NOMOD_EXPR
;
2302 test_code
= GE_EXPR
;
2303 shift_code
= PLUS_NOMOD_EXPR
;
2307 gnu_first
= gnu_low
;
2308 gnu_last
= gnu_high
;
2309 update_code
= PLUS_NOMOD_EXPR
;
2310 test_code
= LE_EXPR
;
2311 shift_code
= MINUS_NOMOD_EXPR
;
2314 /* We use two different strategies to translate the loop, depending on
2315 whether optimization is enabled.
2317 If it is, we generate the canonical loop form expected by the loop
2318 optimizer and the loop vectorizer, which is the do-while form:
2327 This avoids an implicit dependency on loop header copying and makes
2328 it possible to turn BOTTOM_COND into an inequality test.
2330 If optimization is disabled, loop header copying doesn't come into
2331 play and we try to generate the loop form with the fewer conditional
2332 branches. First, the default form, which is:
2340 It should catch most loops with constant ending point. Then, if we
2341 cannot, we try to generate the shifted form:
2349 which should catch loops with constant starting point. Otherwise, if
2350 we cannot, we generate the fallback form:
2359 which works in all cases. */
2363 /* We can use the do-while form directly if GNU_FIRST-1 doesn't
2365 if (!can_equal_min_val_p (gnu_first
, gnu_base_type
, reverse
))
2368 /* Otherwise, use the do-while form with the help of a special
2369 induction variable in the unsigned version of the base type
2370 or the unsigned version of the size type, whichever is the
2371 largest, in order to have wrap-around arithmetics for it. */
2374 if (TYPE_PRECISION (gnu_base_type
)
2375 > TYPE_PRECISION (size_type_node
))
2376 gnu_base_type
= gnat_unsigned_type (gnu_base_type
);
2378 gnu_base_type
= size_type_node
;
2380 gnu_first
= convert (gnu_base_type
, gnu_first
);
2381 gnu_last
= convert (gnu_base_type
, gnu_last
);
2382 gnu_one_node
= convert (gnu_base_type
, integer_one_node
);
2387 = build_binary_op (shift_code
, gnu_base_type
, gnu_first
,
2389 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt
) = 1;
2390 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt
) = 1;
2394 /* We can use the default form if GNU_LAST+1 doesn't overflow. */
2395 if (!can_equal_max_val_p (gnu_last
, gnu_base_type
, reverse
))
2398 /* Otherwise, we can use the shifted form if neither GNU_FIRST-1 nor
2400 else if (!can_equal_min_val_p (gnu_first
, gnu_base_type
, reverse
)
2401 && !can_equal_min_val_p (gnu_last
, gnu_base_type
, reverse
))
2404 = build_binary_op (shift_code
, gnu_base_type
, gnu_first
,
2407 = build_binary_op (shift_code
, gnu_base_type
, gnu_last
,
2409 LOOP_STMT_TOP_UPDATE_P (gnu_loop_stmt
) = 1;
2412 /* Otherwise, use the fallback form. */
2414 LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt
) = 1;
2417 /* If we use the BOTTOM_COND, we can turn the test into an inequality
2418 test but we may have to add ENTRY_COND to protect the empty loop. */
2419 if (LOOP_STMT_BOTTOM_COND_P (gnu_loop_stmt
))
2421 test_code
= NE_EXPR
;
2422 if (can_be_lower_p (gnu_high
, gnu_low
))
2425 = build3 (COND_EXPR
, void_type_node
,
2426 build_binary_op (LE_EXPR
, boolean_type_node
,
2428 NULL_TREE
, alloc_stmt_list ());
2429 set_expr_location_from_node (gnu_cond_expr
, gnat_loop_spec
);
2433 /* Open a new nesting level that will surround the loop to declare the
2434 iteration variable. */
2435 start_stmt_group ();
2438 /* If we use the special induction variable, create it and set it to
2439 its initial value. Morever, the regular iteration variable cannot
2440 itself be initialized, lest the initial value wrapped around. */
2444 = create_init_temporary ("I", gnu_first
, &gnu_stmt
, gnat_loop_var
);
2445 add_stmt (gnu_stmt
);
2446 gnu_first
= NULL_TREE
;
2449 gnu_loop_iv
= NULL_TREE
;
2451 /* Declare the iteration variable and set it to its initial value. */
2452 gnu_loop_var
= gnat_to_gnu_entity (gnat_loop_var
, gnu_first
, 1);
2453 if (DECL_BY_REF_P (gnu_loop_var
))
2454 gnu_loop_var
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_loop_var
);
2457 gcc_assert (DECL_LOOP_PARM_P (gnu_loop_var
));
2458 SET_DECL_INDUCTION_VAR (gnu_loop_var
, gnu_loop_iv
);
2460 gnu_loop_info
->loop_var
= gnu_loop_var
;
2462 /* Do all the arithmetics in the base type. */
2463 gnu_loop_var
= convert (gnu_base_type
, gnu_loop_var
);
2465 /* Set either the top or bottom exit condition. */
2467 LOOP_STMT_COND (gnu_loop_stmt
)
2468 = build_binary_op (test_code
, boolean_type_node
, gnu_loop_iv
,
2471 LOOP_STMT_COND (gnu_loop_stmt
)
2472 = build_binary_op (test_code
, boolean_type_node
, gnu_loop_var
,
2475 /* Set either the top or bottom update statement and give it the source
2476 location of the iteration for better coverage info. */
2480 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_loop_iv
,
2481 build_binary_op (update_code
, gnu_base_type
,
2482 gnu_loop_iv
, gnu_one_node
));
2483 set_expr_location_from_node (gnu_stmt
, gnat_iter_scheme
);
2484 append_to_statement_list (gnu_stmt
,
2485 &LOOP_STMT_UPDATE (gnu_loop_stmt
));
2487 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_loop_var
,
2489 set_expr_location_from_node (gnu_stmt
, gnat_iter_scheme
);
2490 append_to_statement_list (gnu_stmt
,
2491 &LOOP_STMT_UPDATE (gnu_loop_stmt
));
2496 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_loop_var
,
2497 build_binary_op (update_code
, gnu_base_type
,
2498 gnu_loop_var
, gnu_one_node
));
2499 set_expr_location_from_node (gnu_stmt
, gnat_iter_scheme
);
2500 LOOP_STMT_UPDATE (gnu_loop_stmt
) = gnu_stmt
;
2504 /* If the loop was named, have the name point to this loop. In this case,
2505 the association is not a DECL node, but the end label of the loop. */
2506 if (Present (Identifier (gnat_node
)))
2507 save_gnu_tree (Entity (Identifier (gnat_node
)), gnu_loop_label
, true);
2509 /* Make the loop body into its own block, so any allocated storage will be
2510 released every iteration. This is needed for stack allocation. */
2511 LOOP_STMT_BODY (gnu_loop_stmt
)
2512 = build_stmt_group (Statements (gnat_node
), true);
2513 TREE_SIDE_EFFECTS (gnu_loop_stmt
) = 1;
2515 /* If we have an iteration scheme, then we are in a statement group. Add
2516 the LOOP_STMT to it, finish it and make it the "loop". */
2517 if (Present (gnat_iter_scheme
) && No (Condition (gnat_iter_scheme
)))
2519 struct range_check_info_d
*rci
;
2520 unsigned n_checks
= VEC_length (range_check_info
, gnu_loop_info
->checks
);
2523 /* First, if we have computed a small number of invariant conditions for
2524 range checks applied to the iteration variable, then initialize these
2525 conditions in front of the loop. Otherwise, leave them set to True.
2527 ??? The heuristics need to be improved, by taking into account the
2528 following datapoints:
2529 - loop unswitching is disabled for big loops. The cap is the
2530 parameter PARAM_MAX_UNSWITCH_INSNS (50).
2531 - loop unswitching can only be applied a small number of times
2532 to a given loop. The cap is PARAM_MAX_UNSWITCH_LEVEL (3).
2533 - the front-end quickly generates useless or redundant checks
2534 that can be entirely optimized away in the end. */
2535 if (1 <= n_checks
&& n_checks
<= 4)
2537 VEC_iterate (range_check_info
, gnu_loop_info
->checks
, i
, rci
);
2541 = build_binary_op (GE_EXPR
, boolean_type_node
,
2542 convert (rci
->type
, gnu_low
),
2545 = build_binary_op (LE_EXPR
, boolean_type_node
,
2546 convert (rci
->type
, gnu_high
),
2549 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
2552 TREE_OPERAND (rci
->invariant_cond
, 0)
2553 = build_unary_op (TRUTH_NOT_EXPR
, boolean_type_node
, range_ok
);
2555 add_stmt_with_node_force (rci
->invariant_cond
, gnat_node
);
2558 add_stmt (gnu_loop_stmt
);
2560 gnu_loop_stmt
= end_stmt_group ();
2563 /* If we have an outer COND_EXPR, that's our result and this loop is its
2564 "true" statement. Otherwise, the result is the LOOP_STMT. */
2567 COND_EXPR_THEN (gnu_cond_expr
) = gnu_loop_stmt
;
2568 gnu_result
= gnu_cond_expr
;
2569 recalculate_side_effects (gnu_cond_expr
);
2572 gnu_result
= gnu_loop_stmt
;
2574 VEC_pop (loop_info
, gnu_loop_stack
);
2579 /* Emit statements to establish __gnat_handle_vms_condition as a VMS condition
2580 handler for the current function. */
2582 /* This is implemented by issuing a call to the appropriate VMS specific
2583 builtin. To avoid having VMS specific sections in the global gigi decls
2584 array, we maintain the decls of interest here. We can't declare them
2585 inside the function because we must mark them never to be GC'd, which we
2586 can only do at the global level. */
2588 static GTY(()) tree vms_builtin_establish_handler_decl
= NULL_TREE
;
2589 static GTY(()) tree gnat_vms_condition_handler_decl
= NULL_TREE
;
2592 establish_gnat_vms_condition_handler (void)
2594 tree establish_stmt
;
2596 /* Elaborate the required decls on the first call. Check on the decl for
2597 the gnat condition handler to decide, as this is one we create so we are
2598 sure that it will be non null on subsequent calls. The builtin decl is
2599 looked up so remains null on targets where it is not implemented yet. */
2600 if (gnat_vms_condition_handler_decl
== NULL_TREE
)
2602 vms_builtin_establish_handler_decl
2604 (get_identifier ("__builtin_establish_vms_condition_handler"));
2606 gnat_vms_condition_handler_decl
2607 = create_subprog_decl (get_identifier ("__gnat_handle_vms_condition"),
2609 build_function_type_list (boolean_type_node
,
2613 NULL_TREE
, false, true, true, true, NULL
,
2616 /* ??? DECL_CONTEXT shouldn't have been set because of DECL_EXTERNAL. */
2617 DECL_CONTEXT (gnat_vms_condition_handler_decl
) = NULL_TREE
;
2620 /* Do nothing if the establish builtin is not available, which might happen
2621 on targets where the facility is not implemented. */
2622 if (vms_builtin_establish_handler_decl
== NULL_TREE
)
2626 = build_call_n_expr (vms_builtin_establish_handler_decl
, 1,
2628 (ADDR_EXPR
, NULL_TREE
,
2629 gnat_vms_condition_handler_decl
));
2631 add_stmt (establish_stmt
);
2634 /* This page implements a form of Named Return Value optimization modelled
2635 on the C++ optimization of the same name. The main difference is that
2636 we disregard any semantical considerations when applying it here, the
2637 counterpart being that we don't try to apply it to semantically loaded
2638 return types, i.e. types with the TREE_ADDRESSABLE flag set.
2640 We consider a function body of the following GENERIC form:
2644 RETURN_EXPR [<retval> = ...]
2646 RETURN_EXPR [<retval> = R1]
2650 RETURN_EXPR [<retval> = ...]
2652 RETURN_EXPR [<retval> = Ri]
2655 and we try to fulfill a simple criterion that would make it possible to
2656 replace one or several Ri variables with the RESULT_DECL of the function.
2658 The first observation is that RETURN_EXPRs that don't directly reference
2659 any of the Ri variables on the RHS of their assignment are transparent wrt
2660 the optimization. This is because the Ri variables aren't addressable so
2661 any transformation applied to them doesn't affect the RHS; moreover, the
2662 assignment writes the full <retval> object so existing values are entirely
2665 This property can be extended to some forms of RETURN_EXPRs that reference
2666 the Ri variables, for example CONSTRUCTORs, but isn't true in the general
2667 case, in particular when function calls are involved.
2669 Therefore the algorithm is as follows:
2671 1. Collect the list of candidates for a Named Return Value (Ri variables
2672 on the RHS of assignments of RETURN_EXPRs) as well as the list of the
2673 other expressions on the RHS of such assignments.
2675 2. Prune the members of the first list (candidates) that are referenced
2676 by a member of the second list (expressions).
2678 3. Extract a set of candidates with non-overlapping live ranges from the
2679 first list. These are the Named Return Values.
2681 4. Adjust the relevant RETURN_EXPRs and replace the occurrences of the
2682 Named Return Values in the function with the RESULT_DECL.
2684 If the function returns an unconstrained type, things are a bit different
2685 because the anonymous return object is allocated on the secondary stack
2686 and RESULT_DECL is only a pointer to it. Each return object can be of a
2687 different size and is allocated separately so we need not care about the
2688 aforementioned overlapping issues. Therefore, we don't collect the other
2689 expressions and skip step #2 in the algorithm. */
2696 struct pointer_set_t
*visited
;
2699 /* Return true if T is a Named Return Value. */
2702 is_nrv_p (bitmap nrv
, tree t
)
2704 return TREE_CODE (t
) == VAR_DECL
&& bitmap_bit_p (nrv
, DECL_UID (t
));
2707 /* Helper function for walk_tree, used by finalize_nrv below. */
2710 prune_nrv_r (tree
*tp
, int *walk_subtrees
, void *data
)
2712 struct nrv_data
*dp
= (struct nrv_data
*)data
;
2715 /* No need to walk into types or decls. */
2716 if (IS_TYPE_OR_DECL_P (t
))
2719 if (is_nrv_p (dp
->nrv
, t
))
2720 bitmap_clear_bit (dp
->nrv
, DECL_UID (t
));
2725 /* Prune Named Return Values in BLOCK and return true if there is still a
2726 Named Return Value in BLOCK or one of its sub-blocks. */
2729 prune_nrv_in_block (bitmap nrv
, tree block
)
2731 bool has_nrv
= false;
2734 /* First recurse on the sub-blocks. */
2735 for (t
= BLOCK_SUBBLOCKS (block
); t
; t
= BLOCK_CHAIN (t
))
2736 has_nrv
|= prune_nrv_in_block (nrv
, t
);
2738 /* Then make sure to keep at most one NRV per block. */
2739 for (t
= BLOCK_VARS (block
); t
; t
= DECL_CHAIN (t
))
2740 if (is_nrv_p (nrv
, t
))
2743 bitmap_clear_bit (nrv
, DECL_UID (t
));
2751 /* Helper function for walk_tree, used by finalize_nrv below. */
2754 finalize_nrv_r (tree
*tp
, int *walk_subtrees
, void *data
)
2756 struct nrv_data
*dp
= (struct nrv_data
*)data
;
2759 /* No need to walk into types. */
2763 /* Change RETURN_EXPRs of NRVs to just refer to the RESULT_DECL; this is a
2764 nop, but differs from using NULL_TREE in that it indicates that we care
2765 about the value of the RESULT_DECL. */
2766 else if (TREE_CODE (t
) == RETURN_EXPR
2767 && TREE_CODE (TREE_OPERAND (t
, 0)) == MODIFY_EXPR
)
2769 tree ret_val
= TREE_OPERAND (TREE_OPERAND (t
, 0), 1), init_expr
;
2771 /* If this is the temporary created for a return value with variable
2772 size in call_to_gnu, we replace the RHS with the init expression. */
2773 if (TREE_CODE (ret_val
) == COMPOUND_EXPR
2774 && TREE_CODE (TREE_OPERAND (ret_val
, 0)) == INIT_EXPR
2775 && TREE_OPERAND (TREE_OPERAND (ret_val
, 0), 0)
2776 == TREE_OPERAND (ret_val
, 1))
2778 init_expr
= TREE_OPERAND (TREE_OPERAND (ret_val
, 0), 1);
2779 ret_val
= TREE_OPERAND (ret_val
, 1);
2782 init_expr
= NULL_TREE
;
2784 /* Strip useless conversions around the return value. */
2785 if (gnat_useless_type_conversion (ret_val
))
2786 ret_val
= TREE_OPERAND (ret_val
, 0);
2788 if (is_nrv_p (dp
->nrv
, ret_val
))
2791 TREE_OPERAND (TREE_OPERAND (t
, 0), 1) = init_expr
;
2793 TREE_OPERAND (t
, 0) = dp
->result
;
2797 /* Replace the DECL_EXPR of NRVs with an initialization of the RESULT_DECL,
2799 else if (TREE_CODE (t
) == DECL_EXPR
2800 && is_nrv_p (dp
->nrv
, DECL_EXPR_DECL (t
)))
2802 tree var
= DECL_EXPR_DECL (t
), init
;
2804 if (DECL_INITIAL (var
))
2806 init
= build_binary_op (INIT_EXPR
, NULL_TREE
, dp
->result
,
2807 DECL_INITIAL (var
));
2808 SET_EXPR_LOCATION (init
, EXPR_LOCATION (t
));
2809 DECL_INITIAL (var
) = NULL_TREE
;
2812 init
= build_empty_stmt (EXPR_LOCATION (t
));
2815 /* Identify the NRV to the RESULT_DECL for debugging purposes. */
2816 SET_DECL_VALUE_EXPR (var
, dp
->result
);
2817 DECL_HAS_VALUE_EXPR_P (var
) = 1;
2818 /* ??? Kludge to avoid an assertion failure during inlining. */
2819 DECL_SIZE (var
) = bitsize_unit_node
;
2820 DECL_SIZE_UNIT (var
) = size_one_node
;
2823 /* And replace all uses of NRVs with the RESULT_DECL. */
2824 else if (is_nrv_p (dp
->nrv
, t
))
2825 *tp
= convert (TREE_TYPE (t
), dp
->result
);
2827 /* Avoid walking into the same tree more than once. Unfortunately, we
2828 can't just use walk_tree_without_duplicates because it would only
2829 call us for the first occurrence of NRVs in the function body. */
2830 if (pointer_set_insert (dp
->visited
, *tp
))
2836 /* Likewise, but used when the function returns an unconstrained type. */
2839 finalize_nrv_unc_r (tree
*tp
, int *walk_subtrees
, void *data
)
2841 struct nrv_data
*dp
= (struct nrv_data
*)data
;
2844 /* No need to walk into types. */
2848 /* We need to see the DECL_EXPR of NRVs before any other references so we
2849 walk the body of BIND_EXPR before walking its variables. */
2850 else if (TREE_CODE (t
) == BIND_EXPR
)
2851 walk_tree (&BIND_EXPR_BODY (t
), finalize_nrv_unc_r
, data
, NULL
);
2853 /* Change RETURN_EXPRs of NRVs to assign to the RESULT_DECL only the final
2854 return value built by the allocator instead of the whole construct. */
2855 else if (TREE_CODE (t
) == RETURN_EXPR
2856 && TREE_CODE (TREE_OPERAND (t
, 0)) == MODIFY_EXPR
)
2858 tree ret_val
= TREE_OPERAND (TREE_OPERAND (t
, 0), 1);
2860 /* This is the construct returned by the allocator. */
2861 if (TREE_CODE (ret_val
) == COMPOUND_EXPR
2862 && TREE_CODE (TREE_OPERAND (ret_val
, 0)) == INIT_EXPR
)
2864 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (ret_val
)))
2866 = VEC_index (constructor_elt
,
2868 (TREE_OPERAND (TREE_OPERAND (ret_val
, 0), 1)),
2871 ret_val
= TREE_OPERAND (TREE_OPERAND (ret_val
, 0), 1);
2874 /* Strip useless conversions around the return value. */
2875 if (gnat_useless_type_conversion (ret_val
)
2876 || TREE_CODE (ret_val
) == VIEW_CONVERT_EXPR
)
2877 ret_val
= TREE_OPERAND (ret_val
, 0);
2879 /* Strip unpadding around the return value. */
2880 if (TREE_CODE (ret_val
) == COMPONENT_REF
2881 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (ret_val
, 0))))
2882 ret_val
= TREE_OPERAND (ret_val
, 0);
2884 /* Assign the new return value to the RESULT_DECL. */
2885 if (is_nrv_p (dp
->nrv
, ret_val
))
2886 TREE_OPERAND (TREE_OPERAND (t
, 0), 1)
2887 = TREE_OPERAND (DECL_INITIAL (ret_val
), 0);
2890 /* Adjust the DECL_EXPR of NRVs to call the allocator and save the result
2891 into a new variable. */
2892 else if (TREE_CODE (t
) == DECL_EXPR
2893 && is_nrv_p (dp
->nrv
, DECL_EXPR_DECL (t
)))
2895 tree saved_current_function_decl
= current_function_decl
;
2896 tree var
= DECL_EXPR_DECL (t
);
2897 tree alloc
, p_array
, new_var
, new_ret
;
2898 VEC(constructor_elt
,gc
) *v
= VEC_alloc (constructor_elt
, gc
, 2);
2900 /* Create an artificial context to build the allocation. */
2901 current_function_decl
= decl_function_context (var
);
2902 start_stmt_group ();
2905 /* This will return a COMPOUND_EXPR with the allocation in the first
2906 arm and the final return value in the second arm. */
2907 alloc
= build_allocator (TREE_TYPE (var
), DECL_INITIAL (var
),
2908 TREE_TYPE (dp
->result
),
2909 Procedure_To_Call (dp
->gnat_ret
),
2910 Storage_Pool (dp
->gnat_ret
),
2913 /* The new variable is built as a reference to the allocated space. */
2915 = build_decl (DECL_SOURCE_LOCATION (var
), VAR_DECL
, DECL_NAME (var
),
2916 build_reference_type (TREE_TYPE (var
)));
2917 DECL_BY_REFERENCE (new_var
) = 1;
2919 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (alloc
)))
2921 /* The new initial value is a COMPOUND_EXPR with the allocation in
2922 the first arm and the value of P_ARRAY in the second arm. */
2923 DECL_INITIAL (new_var
)
2924 = build2 (COMPOUND_EXPR
, TREE_TYPE (new_var
),
2925 TREE_OPERAND (alloc
, 0),
2926 VEC_index (constructor_elt
,
2927 CONSTRUCTOR_ELTS (TREE_OPERAND (alloc
, 1)),
2930 /* Build a modified CONSTRUCTOR that references NEW_VAR. */
2931 p_array
= TYPE_FIELDS (TREE_TYPE (alloc
));
2932 CONSTRUCTOR_APPEND_ELT (v
, p_array
,
2933 fold_convert (TREE_TYPE (p_array
), new_var
));
2934 CONSTRUCTOR_APPEND_ELT (v
, DECL_CHAIN (p_array
),
2935 VEC_index (constructor_elt
,
2937 (TREE_OPERAND (alloc
, 1)),
2939 new_ret
= build_constructor (TREE_TYPE (alloc
), v
);
2943 /* The new initial value is just the allocation. */
2944 DECL_INITIAL (new_var
) = alloc
;
2945 new_ret
= fold_convert (TREE_TYPE (alloc
), new_var
);
2948 gnat_pushdecl (new_var
, Empty
);
2950 /* Destroy the artificial context and insert the new statements. */
2952 *tp
= end_stmt_group ();
2953 current_function_decl
= saved_current_function_decl
;
2955 /* Chain NEW_VAR immediately after VAR and ignore the latter. */
2956 DECL_CHAIN (new_var
) = DECL_CHAIN (var
);
2957 DECL_CHAIN (var
) = new_var
;
2958 DECL_IGNORED_P (var
) = 1;
2960 /* Save the new return value and the dereference of NEW_VAR. */
2962 = build2 (COMPOUND_EXPR
, TREE_TYPE (var
), new_ret
,
2963 build1 (INDIRECT_REF
, TREE_TYPE (var
), new_var
));
2964 /* ??? Kludge to avoid messing up during inlining. */
2965 DECL_CONTEXT (var
) = NULL_TREE
;
2968 /* And replace all uses of NRVs with the dereference of NEW_VAR. */
2969 else if (is_nrv_p (dp
->nrv
, t
))
2970 *tp
= TREE_OPERAND (DECL_INITIAL (t
), 1);
2972 /* Avoid walking into the same tree more than once. Unfortunately, we
2973 can't just use walk_tree_without_duplicates because it would only
2974 call us for the first occurrence of NRVs in the function body. */
2975 if (pointer_set_insert (dp
->visited
, *tp
))
2981 /* Finalize the Named Return Value optimization for FNDECL. The NRV bitmap
2982 contains the candidates for Named Return Value and OTHER is a list of
2983 the other return values. GNAT_RET is a representative return node. */
2986 finalize_nrv (tree fndecl
, bitmap nrv
, VEC(tree
,gc
) *other
, Node_Id gnat_ret
)
2988 struct cgraph_node
*node
;
2989 struct nrv_data data
;
2994 /* We shouldn't be applying the optimization to return types that we aren't
2995 allowed to manipulate freely. */
2996 gcc_assert (!TREE_ADDRESSABLE (TREE_TYPE (TREE_TYPE (fndecl
))));
2998 /* Prune the candidates that are referenced by other return values. */
3000 data
.result
= NULL_TREE
;
3001 data
.visited
= NULL
;
3002 for (i
= 0; VEC_iterate(tree
, other
, i
, iter
); i
++)
3003 walk_tree_without_duplicates (&iter
, prune_nrv_r
, &data
);
3004 if (bitmap_empty_p (nrv
))
3007 /* Prune also the candidates that are referenced by nested functions. */
3008 node
= cgraph_get_create_node (fndecl
);
3009 for (node
= node
->nested
; node
; node
= node
->next_nested
)
3010 walk_tree_without_duplicates (&DECL_SAVED_TREE (node
->decl
), prune_nrv_r
,
3012 if (bitmap_empty_p (nrv
))
3015 /* Extract a set of NRVs with non-overlapping live ranges. */
3016 if (!prune_nrv_in_block (nrv
, DECL_INITIAL (fndecl
)))
3019 /* Adjust the relevant RETURN_EXPRs and replace the occurrences of NRVs. */
3021 data
.result
= DECL_RESULT (fndecl
);
3022 data
.gnat_ret
= gnat_ret
;
3023 data
.visited
= pointer_set_create ();
3024 if (TYPE_RETURN_UNCONSTRAINED_P (TREE_TYPE (fndecl
)))
3025 func
= finalize_nrv_unc_r
;
3027 func
= finalize_nrv_r
;
3028 walk_tree (&DECL_SAVED_TREE (fndecl
), func
, &data
, NULL
);
3029 pointer_set_destroy (data
.visited
);
3032 /* Return true if RET_VAL can be used as a Named Return Value for the
3033 anonymous return object RET_OBJ. */
3036 return_value_ok_for_nrv_p (tree ret_obj
, tree ret_val
)
3038 if (TREE_CODE (ret_val
) != VAR_DECL
)
3041 if (TREE_THIS_VOLATILE (ret_val
))
3044 if (DECL_CONTEXT (ret_val
) != current_function_decl
)
3047 if (TREE_STATIC (ret_val
))
3050 if (TREE_ADDRESSABLE (ret_val
))
3053 if (ret_obj
&& DECL_ALIGN (ret_val
) > DECL_ALIGN (ret_obj
))
3059 /* Build a RETURN_EXPR. If RET_VAL is non-null, build a RETURN_EXPR around
3060 the assignment of RET_VAL to RET_OBJ. Otherwise build a bare RETURN_EXPR
3061 around RESULT_OBJ, which may be null in this case. */
3064 build_return_expr (tree ret_obj
, tree ret_val
)
3070 /* The gimplifier explicitly enforces the following invariant:
3079 As a consequence, type consistency dictates that we use the type
3080 of the RET_OBJ as the operation type. */
3081 tree operation_type
= TREE_TYPE (ret_obj
);
3083 /* Convert the right operand to the operation type. Note that it's the
3084 same transformation as in the MODIFY_EXPR case of build_binary_op,
3085 with the assumption that the type cannot involve a placeholder. */
3086 if (operation_type
!= TREE_TYPE (ret_val
))
3087 ret_val
= convert (operation_type
, ret_val
);
3089 result_expr
= build2 (MODIFY_EXPR
, void_type_node
, ret_obj
, ret_val
);
3091 /* If the function returns an aggregate type, find out whether this is
3092 a candidate for Named Return Value. If so, record it. Otherwise,
3093 if this is an expression of some kind, record it elsewhere. */
3095 && AGGREGATE_TYPE_P (operation_type
)
3096 && !TYPE_IS_FAT_POINTER_P (operation_type
)
3097 && aggregate_value_p (operation_type
, current_function_decl
))
3099 /* Recognize the temporary created for a return value with variable
3100 size in call_to_gnu. We want to eliminate it if possible. */
3101 if (TREE_CODE (ret_val
) == COMPOUND_EXPR
3102 && TREE_CODE (TREE_OPERAND (ret_val
, 0)) == INIT_EXPR
3103 && TREE_OPERAND (TREE_OPERAND (ret_val
, 0), 0)
3104 == TREE_OPERAND (ret_val
, 1))
3105 ret_val
= TREE_OPERAND (ret_val
, 1);
3107 /* Strip useless conversions around the return value. */
3108 if (gnat_useless_type_conversion (ret_val
))
3109 ret_val
= TREE_OPERAND (ret_val
, 0);
3111 /* Now apply the test to the return value. */
3112 if (return_value_ok_for_nrv_p (ret_obj
, ret_val
))
3114 if (!f_named_ret_val
)
3115 f_named_ret_val
= BITMAP_GGC_ALLOC ();
3116 bitmap_set_bit (f_named_ret_val
, DECL_UID (ret_val
));
3119 /* Note that we need not care about CONSTRUCTORs here, as they are
3120 totally transparent given the read-compose-write semantics of
3121 assignments from CONSTRUCTORs. */
3122 else if (EXPR_P (ret_val
))
3123 VEC_safe_push (tree
, gc
, f_other_ret_val
, ret_val
);
3127 result_expr
= ret_obj
;
3129 return build1 (RETURN_EXPR
, void_type_node
, result_expr
);
3132 /* Build a stub for the subprogram specified by the GCC tree GNU_SUBPROG
3133 and the GNAT node GNAT_SUBPROG. */
3136 build_function_stub (tree gnu_subprog
, Entity_Id gnat_subprog
)
3138 tree gnu_subprog_type
, gnu_subprog_addr
, gnu_subprog_call
;
3139 tree gnu_subprog_param
, gnu_stub_param
, gnu_param
;
3140 tree gnu_stub_decl
= DECL_FUNCTION_STUB (gnu_subprog
);
3141 VEC(tree
,gc
) *gnu_param_vec
= NULL
;
3143 gnu_subprog_type
= TREE_TYPE (gnu_subprog
);
3145 /* Initialize the information structure for the function. */
3146 allocate_struct_function (gnu_stub_decl
, false);
3149 begin_subprog_body (gnu_stub_decl
);
3151 start_stmt_group ();
3154 /* Loop over the parameters of the stub and translate any of them
3155 passed by descriptor into a by reference one. */
3156 for (gnu_stub_param
= DECL_ARGUMENTS (gnu_stub_decl
),
3157 gnu_subprog_param
= DECL_ARGUMENTS (gnu_subprog
);
3159 gnu_stub_param
= DECL_CHAIN (gnu_stub_param
),
3160 gnu_subprog_param
= DECL_CHAIN (gnu_subprog_param
))
3162 if (DECL_BY_DESCRIPTOR_P (gnu_stub_param
))
3164 gcc_assert (DECL_BY_REF_P (gnu_subprog_param
));
3166 = convert_vms_descriptor (TREE_TYPE (gnu_subprog_param
),
3168 DECL_PARM_ALT_TYPE (gnu_stub_param
),
3169 DECL_BY_DOUBLE_REF_P (gnu_subprog_param
),
3173 gnu_param
= gnu_stub_param
;
3175 VEC_safe_push (tree
, gc
, gnu_param_vec
, gnu_param
);
3178 /* Invoke the internal subprogram. */
3179 gnu_subprog_addr
= build1 (ADDR_EXPR
, build_pointer_type (gnu_subprog_type
),
3181 gnu_subprog_call
= build_call_vec (TREE_TYPE (gnu_subprog_type
),
3182 gnu_subprog_addr
, gnu_param_vec
);
3184 /* Propagate the return value, if any. */
3185 if (VOID_TYPE_P (TREE_TYPE (gnu_subprog_type
)))
3186 add_stmt (gnu_subprog_call
);
3188 add_stmt (build_return_expr (DECL_RESULT (gnu_stub_decl
),
3192 end_subprog_body (end_stmt_group ());
3193 rest_of_subprog_body_compilation (gnu_stub_decl
);
3196 /* Subroutine of gnat_to_gnu to process gnat_node, an N_Subprogram_Body. We
3197 don't return anything. */
3200 Subprogram_Body_to_gnu (Node_Id gnat_node
)
3202 /* Defining identifier of a parameter to the subprogram. */
3203 Entity_Id gnat_param
;
3204 /* The defining identifier for the subprogram body. Note that if a
3205 specification has appeared before for this body, then the identifier
3206 occurring in that specification will also be a defining identifier and all
3207 the calls to this subprogram will point to that specification. */
3208 Entity_Id gnat_subprog_id
3209 = (Present (Corresponding_Spec (gnat_node
))
3210 ? Corresponding_Spec (gnat_node
) : Defining_Entity (gnat_node
));
3211 /* The FUNCTION_DECL node corresponding to the subprogram spec. */
3212 tree gnu_subprog_decl
;
3213 /* Its RESULT_DECL node. */
3214 tree gnu_result_decl
;
3215 /* Its FUNCTION_TYPE node. */
3216 tree gnu_subprog_type
;
3217 /* The TYPE_CI_CO_LIST of its FUNCTION_TYPE node, if any. */
3219 /* The entry in the CI_CO_LIST that represents a function return, if any. */
3220 tree gnu_return_var_elmt
= NULL_TREE
;
3222 struct language_function
*gnu_subprog_language
;
3223 VEC(parm_attr
,gc
) *cache
;
3225 /* If this is a generic object or if it has been eliminated,
3227 if (Ekind (gnat_subprog_id
) == E_Generic_Procedure
3228 || Ekind (gnat_subprog_id
) == E_Generic_Function
3229 || Is_Eliminated (gnat_subprog_id
))
3232 /* If this subprogram acts as its own spec, define it. Otherwise, just get
3233 the already-elaborated tree node. However, if this subprogram had its
3234 elaboration deferred, we will already have made a tree node for it. So
3235 treat it as not being defined in that case. Such a subprogram cannot
3236 have an address clause or a freeze node, so this test is safe, though it
3237 does disable some otherwise-useful error checking. */
3239 = gnat_to_gnu_entity (gnat_subprog_id
, NULL_TREE
,
3240 Acts_As_Spec (gnat_node
)
3241 && !present_gnu_tree (gnat_subprog_id
));
3242 gnu_result_decl
= DECL_RESULT (gnu_subprog_decl
);
3243 gnu_subprog_type
= TREE_TYPE (gnu_subprog_decl
);
3244 gnu_cico_list
= TYPE_CI_CO_LIST (gnu_subprog_type
);
3246 gnu_return_var_elmt
= value_member (void_type_node
, gnu_cico_list
);
3248 /* If the function returns by invisible reference, make it explicit in the
3249 function body. See gnat_to_gnu_entity, E_Subprogram_Type case.
3250 Handle the explicit case here and the copy-in/copy-out case below. */
3251 if (TREE_ADDRESSABLE (gnu_subprog_type
) && !gnu_return_var_elmt
)
3253 TREE_TYPE (gnu_result_decl
)
3254 = build_reference_type (TREE_TYPE (gnu_result_decl
));
3255 relayout_decl (gnu_result_decl
);
3258 /* Set the line number in the decl to correspond to that of the body so that
3259 the line number notes are written correctly. */
3260 Sloc_to_locus (Sloc (gnat_node
), &DECL_SOURCE_LOCATION (gnu_subprog_decl
));
3262 /* Initialize the information structure for the function. */
3263 allocate_struct_function (gnu_subprog_decl
, false);
3264 gnu_subprog_language
= ggc_alloc_cleared_language_function ();
3265 DECL_STRUCT_FUNCTION (gnu_subprog_decl
)->language
= gnu_subprog_language
;
3268 begin_subprog_body (gnu_subprog_decl
);
3270 /* If there are In Out or Out parameters, we need to ensure that the return
3271 statement properly copies them out. We do this by making a new block and
3272 converting any return into a goto to a label at the end of the block. */
3275 tree gnu_return_var
= NULL_TREE
;
3277 VEC_safe_push (tree
, gc
, gnu_return_label_stack
,
3278 create_artificial_label (input_location
));
3280 start_stmt_group ();
3283 /* If this is a function with In Out or Out parameters, we also need a
3284 variable for the return value to be placed. */
3285 if (gnu_return_var_elmt
)
3287 tree gnu_return_type
3288 = TREE_TYPE (TREE_PURPOSE (gnu_return_var_elmt
));
3290 /* If the function returns by invisible reference, make it
3291 explicit in the function body. See gnat_to_gnu_entity,
3292 E_Subprogram_Type case. */
3293 if (TREE_ADDRESSABLE (gnu_subprog_type
))
3294 gnu_return_type
= build_reference_type (gnu_return_type
);
3297 = create_var_decl (get_identifier ("RETVAL"), NULL_TREE
,
3298 gnu_return_type
, NULL_TREE
, false, false,
3299 false, false, NULL
, gnat_subprog_id
);
3300 TREE_VALUE (gnu_return_var_elmt
) = gnu_return_var
;
3303 VEC_safe_push (tree
, gc
, gnu_return_var_stack
, gnu_return_var
);
3305 /* See whether there are parameters for which we don't have a GCC tree
3306 yet. These must be Out parameters. Make a VAR_DECL for them and
3307 put it into TYPE_CI_CO_LIST, which must contain an empty entry too.
3308 We can match up the entries because TYPE_CI_CO_LIST is in the order
3309 of the parameters. */
3310 for (gnat_param
= First_Formal_With_Extras (gnat_subprog_id
);
3311 Present (gnat_param
);
3312 gnat_param
= Next_Formal_With_Extras (gnat_param
))
3313 if (!present_gnu_tree (gnat_param
))
3315 tree gnu_cico_entry
= gnu_cico_list
;
3317 /* Skip any entries that have been already filled in; they must
3318 correspond to In Out parameters. */
3319 while (gnu_cico_entry
&& TREE_VALUE (gnu_cico_entry
))
3320 gnu_cico_entry
= TREE_CHAIN (gnu_cico_entry
);
3322 /* Do any needed references for padded types. */
3323 TREE_VALUE (gnu_cico_entry
)
3324 = convert (TREE_TYPE (TREE_PURPOSE (gnu_cico_entry
)),
3325 gnat_to_gnu_entity (gnat_param
, NULL_TREE
, 1));
3329 VEC_safe_push (tree
, gc
, gnu_return_label_stack
, NULL_TREE
);
3331 /* Get a tree corresponding to the code for the subprogram. */
3332 start_stmt_group ();
3335 /* On VMS, establish our condition handler to possibly turn a condition into
3336 the corresponding exception if the subprogram has a foreign convention or
3339 To ensure proper execution of local finalizations on condition instances,
3340 we must turn a condition into the corresponding exception even if there
3341 is no applicable Ada handler, and need at least one condition handler per
3342 possible call chain involving GNAT code. OTOH, establishing the handler
3343 has a cost so we want to minimize the number of subprograms into which
3344 this happens. The foreign or exported condition is expected to satisfy
3345 all the constraints. */
3346 if (TARGET_ABI_OPEN_VMS
3347 && (Has_Foreign_Convention (gnat_subprog_id
)
3348 || Is_Exported (gnat_subprog_id
)))
3349 establish_gnat_vms_condition_handler ();
3351 process_decls (Declarations (gnat_node
), Empty
, Empty
, true, true);
3353 /* Generate the code of the subprogram itself. A return statement will be
3354 present and any Out parameters will be handled there. */
3355 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
3357 gnu_result
= end_stmt_group ();
3359 /* If we populated the parameter attributes cache, we need to make sure that
3360 the cached expressions are evaluated on all the possible paths leading to
3361 their uses. So we force their evaluation on entry of the function. */
3362 cache
= gnu_subprog_language
->parm_attr_cache
;
3365 struct parm_attr_d
*pa
;
3368 start_stmt_group ();
3370 FOR_EACH_VEC_ELT (parm_attr
, cache
, i
, pa
)
3373 add_stmt_with_node_force (pa
->first
, gnat_node
);
3375 add_stmt_with_node_force (pa
->last
, gnat_node
);
3377 add_stmt_with_node_force (pa
->length
, gnat_node
);
3380 add_stmt (gnu_result
);
3381 gnu_result
= end_stmt_group ();
3383 gnu_subprog_language
->parm_attr_cache
= NULL
;
3386 /* If we are dealing with a return from an Ada procedure with parameters
3387 passed by copy-in/copy-out, we need to return a record containing the
3388 final values of these parameters. If the list contains only one entry,
3389 return just that entry though.
3391 For a full description of the copy-in/copy-out parameter mechanism, see
3392 the part of the gnat_to_gnu_entity routine dealing with the translation
3395 We need to make a block that contains the definition of that label and
3396 the copying of the return value. It first contains the function, then
3397 the label and copy statement. */
3402 add_stmt (gnu_result
);
3403 add_stmt (build1 (LABEL_EXPR
, void_type_node
,
3404 VEC_last (tree
, gnu_return_label_stack
)));
3406 if (list_length (gnu_cico_list
) == 1)
3407 gnu_retval
= TREE_VALUE (gnu_cico_list
);
3409 gnu_retval
= build_constructor_from_list (TREE_TYPE (gnu_subprog_type
),
3412 add_stmt_with_node (build_return_expr (gnu_result_decl
, gnu_retval
),
3413 End_Label (Handled_Statement_Sequence (gnat_node
)));
3415 gnu_result
= end_stmt_group ();
3418 VEC_pop (tree
, gnu_return_label_stack
);
3420 /* Attempt setting the end_locus of our GCC body tree, typically a
3421 BIND_EXPR or STATEMENT_LIST, then the end_locus of our GCC subprogram
3422 declaration tree. */
3423 set_end_locus_from_node (gnu_result
, gnat_node
);
3424 set_end_locus_from_node (gnu_subprog_decl
, gnat_node
);
3426 end_subprog_body (gnu_result
);
3428 /* Finally annotate the parameters and disconnect the trees for parameters
3429 that we have turned into variables since they are now unusable. */
3430 for (gnat_param
= First_Formal_With_Extras (gnat_subprog_id
);
3431 Present (gnat_param
);
3432 gnat_param
= Next_Formal_With_Extras (gnat_param
))
3434 tree gnu_param
= get_gnu_tree (gnat_param
);
3435 bool is_var_decl
= (TREE_CODE (gnu_param
) == VAR_DECL
);
3437 annotate_object (gnat_param
, TREE_TYPE (gnu_param
), NULL_TREE
,
3438 DECL_BY_REF_P (gnu_param
),
3439 !is_var_decl
&& DECL_BY_DOUBLE_REF_P (gnu_param
));
3442 save_gnu_tree (gnat_param
, NULL_TREE
, false);
3445 /* Disconnect the variable created for the return value. */
3446 if (gnu_return_var_elmt
)
3447 TREE_VALUE (gnu_return_var_elmt
) = void_type_node
;
3449 /* If the function returns an aggregate type and we have candidates for
3450 a Named Return Value, finalize the optimization. */
3451 if (optimize
&& gnu_subprog_language
->named_ret_val
)
3453 finalize_nrv (gnu_subprog_decl
,
3454 gnu_subprog_language
->named_ret_val
,
3455 gnu_subprog_language
->other_ret_val
,
3456 gnu_subprog_language
->gnat_ret
);
3457 gnu_subprog_language
->named_ret_val
= NULL
;
3458 gnu_subprog_language
->other_ret_val
= NULL
;
3461 rest_of_subprog_body_compilation (gnu_subprog_decl
);
3463 /* If there is a stub associated with the function, build it now. */
3464 if (DECL_FUNCTION_STUB (gnu_subprog_decl
))
3465 build_function_stub (gnu_subprog_decl
, gnat_subprog_id
);
3467 mark_out_of_scope (Defining_Unit_Name (Specification (gnat_node
)));
3470 /* Return true if GNAT_NODE requires atomic synchronization. */
3473 atomic_sync_required_p (Node_Id gnat_node
)
3475 const Node_Id gnat_parent
= Parent (gnat_node
);
3477 unsigned char attr_id
;
3479 /* First, scan the node to find the Atomic_Sync_Required flag. */
3480 kind
= Nkind (gnat_node
);
3481 if (kind
== N_Type_Conversion
|| kind
== N_Unchecked_Type_Conversion
)
3483 gnat_node
= Expression (gnat_node
);
3484 kind
= Nkind (gnat_node
);
3489 case N_Expanded_Name
:
3490 case N_Explicit_Dereference
:
3492 case N_Indexed_Component
:
3493 case N_Selected_Component
:
3494 if (!Atomic_Sync_Required (gnat_node
))
3502 /* Then, scan the parent to find out cases where the flag is irrelevant. */
3503 kind
= Nkind (gnat_parent
);
3506 case N_Attribute_Reference
:
3507 attr_id
= Get_Attribute_Id (Attribute_Name (gnat_parent
));
3508 /* Do not mess up machine code insertions. */
3509 if (attr_id
== Attr_Asm_Input
|| attr_id
== Attr_Asm_Output
)
3513 case N_Object_Renaming_Declaration
:
3514 /* Do not generate a function call as a renamed object. */
3524 /* Create a temporary variable with PREFIX and TYPE, and return it. */
3527 create_temporary (const char *prefix
, tree type
)
3529 tree gnu_temp
= create_var_decl (create_tmp_var_name (prefix
), NULL_TREE
,
3530 type
, NULL_TREE
, false, false, false, false,
3532 DECL_ARTIFICIAL (gnu_temp
) = 1;
3533 DECL_IGNORED_P (gnu_temp
) = 1;
3538 /* Create a temporary variable with PREFIX and initialize it with GNU_INIT.
3539 Put the initialization statement into GNU_INIT_STMT and annotate it with
3540 the SLOC of GNAT_NODE. Return the temporary variable. */
3543 create_init_temporary (const char *prefix
, tree gnu_init
, tree
*gnu_init_stmt
,
3546 tree gnu_temp
= create_temporary (prefix
, TREE_TYPE (gnu_init
));
3548 *gnu_init_stmt
= build_binary_op (INIT_EXPR
, NULL_TREE
, gnu_temp
, gnu_init
);
3549 set_expr_location_from_node (*gnu_init_stmt
, gnat_node
);
3554 /* Subroutine of gnat_to_gnu to translate gnat_node, either an N_Function_Call
3555 or an N_Procedure_Call_Statement, to a GCC tree, which is returned.
3556 GNU_RESULT_TYPE_P is a pointer to where we should place the result type.
3557 If GNU_TARGET is non-null, this must be a function call on the RHS of a
3558 N_Assignment_Statement and the result is to be placed into that object.
3559 If, in addition, ATOMIC_SYNC is true, then the assignment to GNU_TARGET
3560 requires atomic synchronization. */
3563 call_to_gnu (Node_Id gnat_node
, tree
*gnu_result_type_p
, tree gnu_target
,
3566 const bool function_call
= (Nkind (gnat_node
) == N_Function_Call
);
3567 const bool returning_value
= (function_call
&& !gnu_target
);
3568 /* The GCC node corresponding to the GNAT subprogram name. This can either
3569 be a FUNCTION_DECL node if we are dealing with a standard subprogram call,
3570 or an indirect reference expression (an INDIRECT_REF node) pointing to a
3572 tree gnu_subprog
= gnat_to_gnu (Name (gnat_node
));
3573 /* The FUNCTION_TYPE node giving the GCC type of the subprogram. */
3574 tree gnu_subprog_type
= TREE_TYPE (gnu_subprog
);
3575 /* The return type of the FUNCTION_TYPE. */
3576 tree gnu_result_type
= TREE_TYPE (gnu_subprog_type
);
3577 tree gnu_subprog_addr
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_subprog
);
3578 VEC(tree
,gc
) *gnu_actual_vec
= NULL
;
3579 tree gnu_name_list
= NULL_TREE
;
3580 tree gnu_stmt_list
= NULL_TREE
;
3581 tree gnu_after_list
= NULL_TREE
;
3582 tree gnu_retval
= NULL_TREE
;
3583 tree gnu_call
, gnu_result
;
3584 bool went_into_elab_proc
= false;
3585 bool pushed_binding_level
= false;
3586 Entity_Id gnat_formal
;
3587 Node_Id gnat_actual
;
3589 gcc_assert (TREE_CODE (gnu_subprog_type
) == FUNCTION_TYPE
);
3591 /* If we are calling a stubbed function, raise Program_Error, but Elaborate
3592 all our args first. */
3593 if (TREE_CODE (gnu_subprog
) == FUNCTION_DECL
&& DECL_STUBBED_P (gnu_subprog
))
3595 tree call_expr
= build_call_raise (PE_Stubbed_Subprogram_Called
,
3596 gnat_node
, N_Raise_Program_Error
);
3598 for (gnat_actual
= First_Actual (gnat_node
);
3599 Present (gnat_actual
);
3600 gnat_actual
= Next_Actual (gnat_actual
))
3601 add_stmt (gnat_to_gnu (gnat_actual
));
3603 if (returning_value
)
3605 *gnu_result_type_p
= gnu_result_type
;
3606 return build1 (NULL_EXPR
, gnu_result_type
, call_expr
);
3612 /* The only way we can be making a call via an access type is if Name is an
3613 explicit dereference. In that case, get the list of formal args from the
3614 type the access type is pointing to. Otherwise, get the formals from the
3615 entity being called. */
3616 if (Nkind (Name (gnat_node
)) == N_Explicit_Dereference
)
3617 gnat_formal
= First_Formal_With_Extras (Etype (Name (gnat_node
)));
3618 else if (Nkind (Name (gnat_node
)) == N_Attribute_Reference
)
3619 /* Assume here that this must be 'Elab_Body or 'Elab_Spec. */
3620 gnat_formal
= Empty
;
3622 gnat_formal
= First_Formal_With_Extras (Entity (Name (gnat_node
)));
3624 /* The lifetime of the temporaries created for the call ends right after the
3625 return value is copied, so we can give them the scope of the elaboration
3626 routine at top level. */
3627 if (!current_function_decl
)
3629 current_function_decl
= get_elaboration_procedure ();
3630 went_into_elab_proc
= true;
3633 /* First, create the temporary for the return value if we need it: for a
3634 variable-sized return type if there is no target and this is not an
3635 object declaration, or else there is a target and it is a slice or an
3636 array with fixed size, as the gimplifier doesn't handle these cases;
3637 otherwise for a function with copy-in/copy-out parameters if there is
3638 no target, because we need to preserve the return value before copying
3639 back the parameters. This must be done before we push a binding level
3640 around the call as we will pop it before copying the return value. */
3642 && ((TREE_CODE (TYPE_SIZE (gnu_result_type
)) != INTEGER_CST
3644 && Nkind (Parent (gnat_node
)) != N_Object_Declaration
)
3646 && (TREE_CODE (gnu_target
) == ARRAY_RANGE_REF
3647 || (TREE_CODE (TREE_TYPE (gnu_target
)) == ARRAY_TYPE
3648 && TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_target
)))
3650 || (!gnu_target
&& TYPE_CI_CO_LIST (gnu_subprog_type
))))
3651 gnu_retval
= create_temporary ("R", gnu_result_type
);
3653 /* Create the list of the actual parameters as GCC expects it, namely a
3654 chain of TREE_LIST nodes in which the TREE_VALUE field of each node
3655 is an expression and the TREE_PURPOSE field is null. But skip Out
3656 parameters not passed by reference and that need not be copied in. */
3657 for (gnat_actual
= First_Actual (gnat_node
);
3658 Present (gnat_actual
);
3659 gnat_formal
= Next_Formal_With_Extras (gnat_formal
),
3660 gnat_actual
= Next_Actual (gnat_actual
))
3662 tree gnu_formal
= present_gnu_tree (gnat_formal
)
3663 ? get_gnu_tree (gnat_formal
) : NULL_TREE
;
3664 tree gnu_formal_type
= gnat_to_gnu_type (Etype (gnat_formal
));
3665 const bool is_true_formal_parm
3666 = gnu_formal
&& TREE_CODE (gnu_formal
) == PARM_DECL
;
3667 const bool is_by_ref_formal_parm
3668 = is_true_formal_parm
3669 && (DECL_BY_REF_P (gnu_formal
)
3670 || DECL_BY_COMPONENT_PTR_P (gnu_formal
)
3671 || DECL_BY_DESCRIPTOR_P (gnu_formal
));
3672 /* In the Out or In Out case, we must suppress conversions that yield
3673 an lvalue but can nevertheless cause the creation of a temporary,
3674 because we need the real object in this case, either to pass its
3675 address if it's passed by reference or as target of the back copy
3676 done after the call if it uses the copy-in/copy-out mechanism.
3677 We do it in the In case too, except for an unchecked conversion
3678 because it alone can cause the actual to be misaligned and the
3679 addressability test is applied to the real object. */
3680 const bool suppress_type_conversion
3681 = ((Nkind (gnat_actual
) == N_Unchecked_Type_Conversion
3682 && Ekind (gnat_formal
) != E_In_Parameter
)
3683 || (Nkind (gnat_actual
) == N_Type_Conversion
3684 && Is_Composite_Type (Underlying_Type (Etype (gnat_formal
)))));
3685 Node_Id gnat_name
= suppress_type_conversion
3686 ? Expression (gnat_actual
) : gnat_actual
;
3687 tree gnu_name
= gnat_to_gnu (gnat_name
), gnu_name_type
;
3690 /* If it's possible we may need to use this expression twice, make sure
3691 that any side-effects are handled via SAVE_EXPRs; likewise if we need
3692 to force side-effects before the call.
3693 ??? This is more conservative than we need since we don't need to do
3694 this for pass-by-ref with no conversion. */
3695 if (Ekind (gnat_formal
) != E_In_Parameter
)
3696 gnu_name
= gnat_stabilize_reference (gnu_name
, true, NULL
);
3698 /* If we are passing a non-addressable parameter by reference, pass the
3699 address of a copy. In the Out or In Out case, set up to copy back
3700 out after the call. */
3701 if (is_by_ref_formal_parm
3702 && (gnu_name_type
= gnat_to_gnu_type (Etype (gnat_name
)))
3703 && !addressable_p (gnu_name
, gnu_name_type
))
3705 bool in_param
= (Ekind (gnat_formal
) == E_In_Parameter
);
3706 tree gnu_orig
= gnu_name
, gnu_temp
, gnu_stmt
;
3708 /* Do not issue warnings for CONSTRUCTORs since this is not a copy
3709 but sort of an instantiation for them. */
3710 if (TREE_CODE (gnu_name
) == CONSTRUCTOR
)
3713 /* If the type is passed by reference, a copy is not allowed. */
3714 else if (TREE_ADDRESSABLE (gnu_formal_type
))
3715 post_error ("misaligned actual cannot be passed by reference",
3718 /* For users of Starlet we issue a warning because the interface
3719 apparently assumes that by-ref parameters outlive the procedure
3720 invocation. The code still will not work as intended, but we
3721 cannot do much better since low-level parts of the back-end
3722 would allocate temporaries at will because of the misalignment
3723 if we did not do so here. */
3724 else if (Is_Valued_Procedure (Entity (Name (gnat_node
))))
3727 ("?possible violation of implicit assumption", gnat_actual
);
3729 ("?made by pragma Import_Valued_Procedure on &", gnat_actual
,
3730 Entity (Name (gnat_node
)));
3731 post_error_ne ("?because of misalignment of &", gnat_actual
,
3735 /* If the actual type of the object is already the nominal type,
3736 we have nothing to do, except if the size is self-referential
3737 in which case we'll remove the unpadding below. */
3738 if (TREE_TYPE (gnu_name
) == gnu_name_type
3739 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_name_type
)))
3742 /* Otherwise remove the unpadding from all the objects. */
3743 else if (TREE_CODE (gnu_name
) == COMPONENT_REF
3744 && TYPE_IS_PADDING_P
3745 (TREE_TYPE (TREE_OPERAND (gnu_name
, 0))))
3746 gnu_orig
= gnu_name
= TREE_OPERAND (gnu_name
, 0);
3748 /* Otherwise convert to the nominal type of the object if needed.
3749 There are several cases in which we need to make the temporary
3750 using this type instead of the actual type of the object when
3751 they are distinct, because the expectations of the callee would
3752 otherwise not be met:
3753 - if it's a justified modular type,
3754 - if the actual type is a smaller form of it,
3755 - if it's a smaller form of the actual type. */
3756 else if ((TREE_CODE (gnu_name_type
) == RECORD_TYPE
3757 && (TYPE_JUSTIFIED_MODULAR_P (gnu_name_type
)
3758 || smaller_form_type_p (TREE_TYPE (gnu_name
),
3760 || (INTEGRAL_TYPE_P (gnu_name_type
)
3761 && smaller_form_type_p (gnu_name_type
,
3762 TREE_TYPE (gnu_name
))))
3763 gnu_name
= convert (gnu_name_type
, gnu_name
);
3765 /* If this is an In Out or Out parameter and we're returning a value,
3766 we need to create a temporary for the return value because we must
3767 preserve it before copying back at the very end. */
3768 if (!in_param
&& returning_value
&& !gnu_retval
)
3769 gnu_retval
= create_temporary ("R", gnu_result_type
);
3771 /* If we haven't pushed a binding level, push a new one. This will
3772 narrow the lifetime of the temporary we are about to make as much
3773 as possible. The drawback is that we'd need to create a temporary
3774 for the return value, if any (see comment before the loop). So do
3775 it only when this temporary was already created just above. */
3776 if (!pushed_binding_level
&& !(in_param
&& returning_value
))
3778 start_stmt_group ();
3780 pushed_binding_level
= true;
3783 /* Create an explicit temporary holding the copy. */
3785 = create_init_temporary ("A", gnu_name
, &gnu_stmt
, gnat_actual
);
3787 /* But initialize it on the fly like for an implicit temporary as
3788 we aren't necessarily having a statement list. */
3789 gnu_name
= build_compound_expr (TREE_TYPE (gnu_name
), gnu_stmt
,
3792 /* Set up to move the copy back to the original if needed. */
3795 gnu_stmt
= build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_orig
,
3797 set_expr_location_from_node (gnu_stmt
, gnat_node
);
3798 append_to_statement_list (gnu_stmt
, &gnu_after_list
);
3802 /* Start from the real object and build the actual. */
3803 gnu_actual
= gnu_name
;
3805 /* If this is an atomic access of an In or In Out parameter for which
3806 synchronization is required, build the atomic load. */
3807 if (is_true_formal_parm
3808 && !is_by_ref_formal_parm
3809 && Ekind (gnat_formal
) != E_Out_Parameter
3810 && atomic_sync_required_p (gnat_actual
))
3811 gnu_actual
= build_atomic_load (gnu_actual
);
3813 /* If this was a procedure call, we may not have removed any padding.
3814 So do it here for the part we will use as an input, if any. */
3815 if (Ekind (gnat_formal
) != E_Out_Parameter
3816 && TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual
)))
3818 = convert (get_unpadded_type (Etype (gnat_actual
)), gnu_actual
);
3820 /* Put back the conversion we suppressed above in the computation of the
3821 real object. And even if we didn't suppress any conversion there, we
3822 may have suppressed a conversion to the Etype of the actual earlier,
3823 since the parent is a procedure call, so put it back here. */
3824 if (suppress_type_conversion
3825 && Nkind (gnat_actual
) == N_Unchecked_Type_Conversion
)
3827 = unchecked_convert (gnat_to_gnu_type (Etype (gnat_actual
)),
3828 gnu_actual
, No_Truncation (gnat_actual
));
3831 = convert (gnat_to_gnu_type (Etype (gnat_actual
)), gnu_actual
);
3833 /* Make sure that the actual is in range of the formal's type. */
3834 if (Ekind (gnat_formal
) != E_Out_Parameter
3835 && Do_Range_Check (gnat_actual
))
3837 = emit_range_check (gnu_actual
, Etype (gnat_formal
), gnat_actual
);
3839 /* Unless this is an In parameter, we must remove any justified modular
3840 building from GNU_NAME to get an lvalue. */
3841 if (Ekind (gnat_formal
) != E_In_Parameter
3842 && TREE_CODE (gnu_name
) == CONSTRUCTOR
3843 && TREE_CODE (TREE_TYPE (gnu_name
)) == RECORD_TYPE
3844 && TYPE_JUSTIFIED_MODULAR_P (TREE_TYPE (gnu_name
)))
3846 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_name
))), gnu_name
);
3848 /* If we have not saved a GCC object for the formal, it means it is an
3849 Out parameter not passed by reference and that need not be copied in.
3850 Otherwise, first see if the parameter is passed by reference. */
3851 if (is_true_formal_parm
&& DECL_BY_REF_P (gnu_formal
))
3853 if (Ekind (gnat_formal
) != E_In_Parameter
)
3855 /* In Out or Out parameters passed by reference don't use the
3856 copy-in/copy-out mechanism so the address of the real object
3857 must be passed to the function. */
3858 gnu_actual
= gnu_name
;
3860 /* If we have a padded type, be sure we've removed padding. */
3861 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_actual
)))
3862 gnu_actual
= convert (get_unpadded_type (Etype (gnat_actual
)),
3865 /* If we have the constructed subtype of an aliased object
3866 with an unconstrained nominal subtype, the type of the
3867 actual includes the template, although it is formally
3868 constrained. So we need to convert it back to the real
3869 constructed subtype to retrieve the constrained part
3870 and takes its address. */
3871 if (TREE_CODE (TREE_TYPE (gnu_actual
)) == RECORD_TYPE
3872 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_actual
))
3873 && Is_Constr_Subt_For_UN_Aliased (Etype (gnat_actual
))
3874 && Is_Array_Type (Etype (gnat_actual
)))
3875 gnu_actual
= convert (gnat_to_gnu_type (Etype (gnat_actual
)),
3879 /* There is no need to convert the actual to the formal's type before
3880 taking its address. The only exception is for unconstrained array
3881 types because of the way we build fat pointers. */
3882 if (TREE_CODE (gnu_formal_type
) == UNCONSTRAINED_ARRAY_TYPE
)
3884 /* Put back a view conversion for In Out or Out parameters. */
3885 if (Ekind (gnat_formal
) != E_In_Parameter
)
3886 gnu_actual
= convert (gnat_to_gnu_type (Etype (gnat_actual
)),
3888 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
3891 /* The symmetry of the paths to the type of an entity is broken here
3892 since arguments don't know that they will be passed by ref. */
3893 gnu_formal_type
= TREE_TYPE (gnu_formal
);
3895 if (DECL_BY_DOUBLE_REF_P (gnu_formal
))
3897 = build_unary_op (ADDR_EXPR
, TREE_TYPE (gnu_formal_type
),
3900 gnu_actual
= build_unary_op (ADDR_EXPR
, gnu_formal_type
, gnu_actual
);
3902 else if (is_true_formal_parm
&& DECL_BY_COMPONENT_PTR_P (gnu_formal
))
3904 gnu_formal_type
= TREE_TYPE (gnu_formal
);
3905 gnu_actual
= maybe_implicit_deref (gnu_actual
);
3906 gnu_actual
= maybe_unconstrained_array (gnu_actual
);
3908 if (TYPE_IS_PADDING_P (gnu_formal_type
))
3910 gnu_formal_type
= TREE_TYPE (TYPE_FIELDS (gnu_formal_type
));
3911 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
3914 /* Take the address of the object and convert to the proper pointer
3915 type. We'd like to actually compute the address of the beginning
3916 of the array using an ADDR_EXPR of an ARRAY_REF, but there's a
3917 possibility that the ARRAY_REF might return a constant and we'd be
3918 getting the wrong address. Neither approach is exactly correct,
3919 but this is the most likely to work in all cases. */
3920 gnu_actual
= build_unary_op (ADDR_EXPR
, gnu_formal_type
, gnu_actual
);
3922 else if (is_true_formal_parm
&& DECL_BY_DESCRIPTOR_P (gnu_formal
))
3924 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
3926 /* If this is 'Null_Parameter, pass a zero descriptor. */
3927 if ((TREE_CODE (gnu_actual
) == INDIRECT_REF
3928 || TREE_CODE (gnu_actual
) == UNCONSTRAINED_ARRAY_REF
)
3929 && TREE_PRIVATE (gnu_actual
))
3931 = convert (DECL_ARG_TYPE (gnu_formal
), integer_zero_node
);
3933 gnu_actual
= build_unary_op (ADDR_EXPR
, NULL_TREE
,
3935 (TREE_TYPE (TREE_TYPE (gnu_formal
)),
3936 gnu_actual
, gnat_actual
));
3942 if (Ekind (gnat_formal
) != E_In_Parameter
)
3943 gnu_name_list
= tree_cons (NULL_TREE
, gnu_name
, gnu_name_list
);
3945 if (!is_true_formal_parm
)
3947 /* Make sure side-effects are evaluated before the call. */
3948 if (TREE_SIDE_EFFECTS (gnu_name
))
3949 append_to_statement_list (gnu_name
, &gnu_stmt_list
);
3953 gnu_actual
= convert (gnu_formal_type
, gnu_actual
);
3955 /* If this is 'Null_Parameter, pass a zero even though we are
3956 dereferencing it. */
3957 if (TREE_CODE (gnu_actual
) == INDIRECT_REF
3958 && TREE_PRIVATE (gnu_actual
)
3959 && (gnu_size
= TYPE_SIZE (TREE_TYPE (gnu_actual
)))
3960 && TREE_CODE (gnu_size
) == INTEGER_CST
3961 && compare_tree_int (gnu_size
, BITS_PER_WORD
) <= 0)
3963 = unchecked_convert (DECL_ARG_TYPE (gnu_formal
),
3964 convert (gnat_type_for_size
3965 (TREE_INT_CST_LOW (gnu_size
), 1),
3969 gnu_actual
= convert (DECL_ARG_TYPE (gnu_formal
), gnu_actual
);
3972 VEC_safe_push (tree
, gc
, gnu_actual_vec
, gnu_actual
);
3976 = build_call_vec (gnu_result_type
, gnu_subprog_addr
, gnu_actual_vec
);
3977 set_expr_location_from_node (gnu_call
, gnat_node
);
3979 /* If we have created a temporary for the return value, initialize it. */
3983 = build_binary_op (INIT_EXPR
, NULL_TREE
, gnu_retval
, gnu_call
);
3984 set_expr_location_from_node (gnu_stmt
, gnat_node
);
3985 append_to_statement_list (gnu_stmt
, &gnu_stmt_list
);
3986 gnu_call
= gnu_retval
;
3989 /* If this is a subprogram with copy-in/copy-out parameters, we need to
3990 unpack the valued returned from the function into the In Out or Out
3991 parameters. We deal with the function return (if this is an Ada
3993 if (TYPE_CI_CO_LIST (gnu_subprog_type
))
3995 /* List of FIELD_DECLs associated with the PARM_DECLs of the copy-in/
3996 copy-out parameters. */
3997 tree gnu_cico_list
= TYPE_CI_CO_LIST (gnu_subprog_type
);
3998 const int length
= list_length (gnu_cico_list
);
4000 /* The call sequence must contain one and only one call, even though the
4001 function is pure. Save the result into a temporary if needed. */
4007 /* If we haven't pushed a binding level, push a new one. This
4008 will narrow the lifetime of the temporary we are about to
4009 make as much as possible. */
4010 if (!pushed_binding_level
)
4012 start_stmt_group ();
4014 pushed_binding_level
= true;
4017 = create_init_temporary ("P", gnu_call
, &gnu_stmt
, gnat_node
);
4018 append_to_statement_list (gnu_stmt
, &gnu_stmt_list
);
4021 gnu_name_list
= nreverse (gnu_name_list
);
4024 /* The first entry is for the actual return value if this is a
4025 function, so skip it. */
4026 if (TREE_VALUE (gnu_cico_list
) == void_type_node
)
4027 gnu_cico_list
= TREE_CHAIN (gnu_cico_list
);
4029 if (Nkind (Name (gnat_node
)) == N_Explicit_Dereference
)
4030 gnat_formal
= First_Formal_With_Extras (Etype (Name (gnat_node
)));
4032 gnat_formal
= First_Formal_With_Extras (Entity (Name (gnat_node
)));
4034 for (gnat_actual
= First_Actual (gnat_node
);
4035 Present (gnat_actual
);
4036 gnat_formal
= Next_Formal_With_Extras (gnat_formal
),
4037 gnat_actual
= Next_Actual (gnat_actual
))
4038 /* If we are dealing with a copy-in/copy-out parameter, we must
4039 retrieve its value from the record returned in the call. */
4040 if (!(present_gnu_tree (gnat_formal
)
4041 && TREE_CODE (get_gnu_tree (gnat_formal
)) == PARM_DECL
4042 && (DECL_BY_REF_P (get_gnu_tree (gnat_formal
))
4043 || (TREE_CODE (get_gnu_tree (gnat_formal
)) == PARM_DECL
4044 && ((DECL_BY_COMPONENT_PTR_P (get_gnu_tree (gnat_formal
))
4045 || (DECL_BY_DESCRIPTOR_P
4046 (get_gnu_tree (gnat_formal
))))))))
4047 && Ekind (gnat_formal
) != E_In_Parameter
)
4049 /* Get the value to assign to this Out or In Out parameter. It is
4050 either the result of the function if there is only a single such
4051 parameter or the appropriate field from the record returned. */
4055 : build_component_ref (gnu_call
, NULL_TREE
,
4056 TREE_PURPOSE (gnu_cico_list
), false);
4058 /* If the actual is a conversion, get the inner expression, which
4059 will be the real destination, and convert the result to the
4060 type of the actual parameter. */
4062 = maybe_unconstrained_array (TREE_VALUE (gnu_name_list
));
4064 /* If the result is a padded type, remove the padding. */
4065 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result
)))
4067 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result
))),
4070 /* If the actual is a type conversion, the real target object is
4071 denoted by the inner Expression and we need to convert the
4072 result to the associated type.
4073 We also need to convert our gnu assignment target to this type
4074 if the corresponding GNU_NAME was constructed from the GNAT
4075 conversion node and not from the inner Expression. */
4076 if (Nkind (gnat_actual
) == N_Type_Conversion
)
4079 = convert_with_check
4080 (Etype (Expression (gnat_actual
)), gnu_result
,
4081 Do_Overflow_Check (gnat_actual
),
4082 Do_Range_Check (Expression (gnat_actual
)),
4083 Float_Truncate (gnat_actual
), gnat_actual
);
4085 if (!Is_Composite_Type (Underlying_Type (Etype (gnat_formal
))))
4086 gnu_actual
= convert (TREE_TYPE (gnu_result
), gnu_actual
);
4089 /* Unchecked conversions as actuals for Out parameters are not
4090 allowed in user code because they are not variables, but do
4091 occur in front-end expansions. The associated GNU_NAME is
4092 always obtained from the inner expression in such cases. */
4093 else if (Nkind (gnat_actual
) == N_Unchecked_Type_Conversion
)
4094 gnu_result
= unchecked_convert (TREE_TYPE (gnu_actual
),
4096 No_Truncation (gnat_actual
));
4099 if (Do_Range_Check (gnat_actual
))
4101 = emit_range_check (gnu_result
, Etype (gnat_actual
),
4104 if (!(!TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_actual
)))
4105 && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (gnu_result
)))))
4106 gnu_result
= convert (TREE_TYPE (gnu_actual
), gnu_result
);
4109 if (atomic_sync_required_p (gnat_actual
))
4110 gnu_result
= build_atomic_store (gnu_actual
, gnu_result
);
4112 gnu_result
= build_binary_op (MODIFY_EXPR
, NULL_TREE
,
4113 gnu_actual
, gnu_result
);
4114 set_expr_location_from_node (gnu_result
, gnat_node
);
4115 append_to_statement_list (gnu_result
, &gnu_stmt_list
);
4116 gnu_cico_list
= TREE_CHAIN (gnu_cico_list
);
4117 gnu_name_list
= TREE_CHAIN (gnu_name_list
);
4121 /* If this is a function call, the result is the call expression unless a
4122 target is specified, in which case we copy the result into the target
4123 and return the assignment statement. */
4126 /* If this is a function with copy-in/copy-out parameters, extract the
4127 return value from it and update the return type. */
4128 if (TYPE_CI_CO_LIST (gnu_subprog_type
))
4130 tree gnu_elmt
= value_member (void_type_node
,
4131 TYPE_CI_CO_LIST (gnu_subprog_type
));
4132 gnu_call
= build_component_ref (gnu_call
, NULL_TREE
,
4133 TREE_PURPOSE (gnu_elmt
), false);
4134 gnu_result_type
= TREE_TYPE (gnu_call
);
4137 /* If the function returns an unconstrained array or by direct reference,
4138 we have to dereference the pointer. */
4139 if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type
)
4140 || TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type
))
4141 gnu_call
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_call
);
4145 Node_Id gnat_parent
= Parent (gnat_node
);
4146 enum tree_code op_code
;
4148 /* If range check is needed, emit code to generate it. */
4149 if (Do_Range_Check (gnat_node
))
4151 = emit_range_check (gnu_call
, Etype (Name (gnat_parent
)),
4154 /* ??? If the return type has variable size, then force the return
4155 slot optimization as we would not be able to create a temporary.
4156 Likewise if it was unconstrained as we would copy too much data.
4157 That's what has been done historically. */
4158 if (TREE_CODE (TYPE_SIZE (gnu_result_type
)) != INTEGER_CST
4159 || (TYPE_IS_PADDING_P (gnu_result_type
)
4160 && CONTAINS_PLACEHOLDER_P
4161 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_result_type
))))))
4162 op_code
= INIT_EXPR
;
4164 op_code
= MODIFY_EXPR
;
4167 gnu_call
= build_atomic_store (gnu_target
, gnu_call
);
4170 = build_binary_op (op_code
, NULL_TREE
, gnu_target
, gnu_call
);
4171 set_expr_location_from_node (gnu_call
, gnat_parent
);
4172 append_to_statement_list (gnu_call
, &gnu_stmt_list
);
4175 *gnu_result_type_p
= get_unpadded_type (Etype (gnat_node
));
4178 /* Otherwise, if this is a procedure call statement without copy-in/copy-out
4179 parameters, the result is just the call statement. */
4180 else if (!TYPE_CI_CO_LIST (gnu_subprog_type
))
4181 append_to_statement_list (gnu_call
, &gnu_stmt_list
);
4183 /* Finally, add the copy back statements, if any. */
4184 append_to_statement_list (gnu_after_list
, &gnu_stmt_list
);
4186 if (went_into_elab_proc
)
4187 current_function_decl
= NULL_TREE
;
4189 /* If we have pushed a binding level, pop it and finish up the enclosing
4191 if (pushed_binding_level
)
4193 add_stmt (gnu_stmt_list
);
4195 gnu_result
= end_stmt_group ();
4198 /* Otherwise, retrieve the statement list, if any. */
4199 else if (gnu_stmt_list
)
4200 gnu_result
= gnu_stmt_list
;
4202 /* Otherwise, just return the call expression. */
4206 /* If we nevertheless need a value, make a COMPOUND_EXPR to return it.
4207 But first simplify if we have only one statement in the list. */
4208 if (returning_value
)
4210 tree first
= expr_first (gnu_result
), last
= expr_last (gnu_result
);
4214 = build_compound_expr (TREE_TYPE (gnu_call
), gnu_result
, gnu_call
);
4220 /* Subroutine of gnat_to_gnu to translate gnat_node, an
4221 N_Handled_Sequence_Of_Statements, to a GCC tree, which is returned. */
4224 Handled_Sequence_Of_Statements_to_gnu (Node_Id gnat_node
)
4226 tree gnu_jmpsave_decl
= NULL_TREE
;
4227 tree gnu_jmpbuf_decl
= NULL_TREE
;
4228 /* If just annotating, ignore all EH and cleanups. */
4229 bool gcc_zcx
= (!type_annotate_only
4230 && Present (Exception_Handlers (gnat_node
))
4231 && Exception_Mechanism
== Back_End_Exceptions
);
4233 = (!type_annotate_only
&& Present (Exception_Handlers (gnat_node
))
4234 && Exception_Mechanism
== Setjmp_Longjmp
);
4235 bool at_end
= !type_annotate_only
&& Present (At_End_Proc (gnat_node
));
4236 bool binding_for_block
= (at_end
|| gcc_zcx
|| setjmp_longjmp
);
4237 tree gnu_inner_block
; /* The statement(s) for the block itself. */
4242 /* The GCC exception handling mechanism can handle both ZCX and SJLJ schemes
4243 and we have our own SJLJ mechanism. To call the GCC mechanism, we call
4244 add_cleanup, and when we leave the binding, end_stmt_group will create
4245 the TRY_FINALLY_EXPR.
4247 ??? The region level calls down there have been specifically put in place
4248 for a ZCX context and currently the order in which things are emitted
4249 (region/handlers) is different from the SJLJ case. Instead of putting
4250 other calls with different conditions at other places for the SJLJ case,
4251 it seems cleaner to reorder things for the SJLJ case and generalize the
4252 condition to make it not ZCX specific.
4254 If there are any exceptions or cleanup processing involved, we need an
4255 outer statement group (for Setjmp_Longjmp) and binding level. */
4256 if (binding_for_block
)
4258 start_stmt_group ();
4262 /* If using setjmp_longjmp, make the variables for the setjmp buffer and save
4263 area for address of previous buffer. Do this first since we need to have
4264 the setjmp buf known for any decls in this block. */
4268 = create_var_decl (get_identifier ("JMPBUF_SAVE"), NULL_TREE
,
4270 build_call_n_expr (get_jmpbuf_decl
, 0),
4271 false, false, false, false, NULL
, gnat_node
);
4272 DECL_ARTIFICIAL (gnu_jmpsave_decl
) = 1;
4274 /* The __builtin_setjmp receivers will immediately reinstall it. Now
4275 because of the unstructured form of EH used by setjmp_longjmp, there
4276 might be forward edges going to __builtin_setjmp receivers on which
4277 it is uninitialized, although they will never be actually taken. */
4278 TREE_NO_WARNING (gnu_jmpsave_decl
) = 1;
4280 = create_var_decl (get_identifier ("JMP_BUF"), NULL_TREE
,
4283 false, false, false, false, NULL
, gnat_node
);
4284 DECL_ARTIFICIAL (gnu_jmpbuf_decl
) = 1;
4286 set_block_jmpbuf_decl (gnu_jmpbuf_decl
);
4288 /* When we exit this block, restore the saved value. */
4289 add_cleanup (build_call_n_expr (set_jmpbuf_decl
, 1, gnu_jmpsave_decl
),
4290 End_Label (gnat_node
));
4293 /* If we are to call a function when exiting this block, add a cleanup
4294 to the binding level we made above. Note that add_cleanup is FIFO
4295 so we must register this cleanup after the EH cleanup just above. */
4297 add_cleanup (build_call_n_expr (gnat_to_gnu (At_End_Proc (gnat_node
)), 0),
4298 End_Label (gnat_node
));
4300 /* Now build the tree for the declarations and statements inside this block.
4301 If this is SJLJ, set our jmp_buf as the current buffer. */
4302 start_stmt_group ();
4305 add_stmt (build_call_n_expr (set_jmpbuf_decl
, 1,
4306 build_unary_op (ADDR_EXPR
, NULL_TREE
,
4309 if (Present (First_Real_Statement (gnat_node
)))
4310 process_decls (Statements (gnat_node
), Empty
,
4311 First_Real_Statement (gnat_node
), true, true);
4313 /* Generate code for each statement in the block. */
4314 for (gnat_temp
= (Present (First_Real_Statement (gnat_node
))
4315 ? First_Real_Statement (gnat_node
)
4316 : First (Statements (gnat_node
)));
4317 Present (gnat_temp
); gnat_temp
= Next (gnat_temp
))
4318 add_stmt (gnat_to_gnu (gnat_temp
));
4319 gnu_inner_block
= end_stmt_group ();
4321 /* Now generate code for the two exception models, if either is relevant for
4325 tree
*gnu_else_ptr
= 0;
4328 /* Make a binding level for the exception handling declarations and code
4329 and set up gnu_except_ptr_stack for the handlers to use. */
4330 start_stmt_group ();
4333 VEC_safe_push (tree
, gc
, gnu_except_ptr_stack
,
4334 create_var_decl (get_identifier ("EXCEPT_PTR"), NULL_TREE
,
4335 build_pointer_type (except_type_node
),
4336 build_call_n_expr (get_excptr_decl
, 0),
4337 false, false, false, false,
4340 /* Generate code for each handler. The N_Exception_Handler case does the
4341 real work and returns a COND_EXPR for each handler, which we chain
4343 for (gnat_temp
= First_Non_Pragma (Exception_Handlers (gnat_node
));
4344 Present (gnat_temp
); gnat_temp
= Next_Non_Pragma (gnat_temp
))
4346 gnu_expr
= gnat_to_gnu (gnat_temp
);
4348 /* If this is the first one, set it as the outer one. Otherwise,
4349 point the "else" part of the previous handler to us. Then point
4350 to our "else" part. */
4352 add_stmt (gnu_expr
);
4354 *gnu_else_ptr
= gnu_expr
;
4356 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_expr
);
4359 /* If none of the exception handlers did anything, re-raise but do not
4361 gnu_expr
= build_call_n_expr (raise_nodefer_decl
, 1,
4362 VEC_last (tree
, gnu_except_ptr_stack
));
4363 set_expr_location_from_node
4365 Present (End_Label (gnat_node
)) ? End_Label (gnat_node
) : gnat_node
);
4368 *gnu_else_ptr
= gnu_expr
;
4370 add_stmt (gnu_expr
);
4372 /* End the binding level dedicated to the exception handlers and get the
4373 whole statement group. */
4374 VEC_pop (tree
, gnu_except_ptr_stack
);
4376 gnu_handler
= end_stmt_group ();
4378 /* If the setjmp returns 1, we restore our incoming longjmp value and
4379 then check the handlers. */
4380 start_stmt_group ();
4381 add_stmt_with_node (build_call_n_expr (set_jmpbuf_decl
, 1,
4384 add_stmt (gnu_handler
);
4385 gnu_handler
= end_stmt_group ();
4387 /* This block is now "if (setjmp) ... <handlers> else <block>". */
4388 gnu_result
= build3 (COND_EXPR
, void_type_node
,
4391 build_unary_op (ADDR_EXPR
, NULL_TREE
,
4393 gnu_handler
, gnu_inner_block
);
4399 /* First make a block containing the handlers. */
4400 start_stmt_group ();
4401 for (gnat_temp
= First_Non_Pragma (Exception_Handlers (gnat_node
));
4402 Present (gnat_temp
);
4403 gnat_temp
= Next_Non_Pragma (gnat_temp
))
4404 add_stmt (gnat_to_gnu (gnat_temp
));
4405 gnu_handlers
= end_stmt_group ();
4407 /* Now make the TRY_CATCH_EXPR for the block. */
4408 gnu_result
= build2 (TRY_CATCH_EXPR
, void_type_node
,
4409 gnu_inner_block
, gnu_handlers
);
4412 gnu_result
= gnu_inner_block
;
4414 /* Now close our outer block, if we had to make one. */
4415 if (binding_for_block
)
4417 add_stmt (gnu_result
);
4419 gnu_result
= end_stmt_group ();
4425 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4426 to a GCC tree, which is returned. This is the variant for Setjmp_Longjmp
4427 exception handling. */
4430 Exception_Handler_to_gnu_sjlj (Node_Id gnat_node
)
4432 /* Unless this is "Others" or the special "Non-Ada" exception for Ada, make
4433 an "if" statement to select the proper exceptions. For "Others", exclude
4434 exceptions where Handled_By_Others is nonzero unless the All_Others flag
4435 is set. For "Non-ada", accept an exception if "Lang" is 'V'. */
4436 tree gnu_choice
= boolean_false_node
;
4437 tree gnu_body
= build_stmt_group (Statements (gnat_node
), false);
4440 for (gnat_temp
= First (Exception_Choices (gnat_node
));
4441 gnat_temp
; gnat_temp
= Next (gnat_temp
))
4445 if (Nkind (gnat_temp
) == N_Others_Choice
)
4447 if (All_Others (gnat_temp
))
4448 this_choice
= boolean_true_node
;
4452 (EQ_EXPR
, boolean_type_node
,
4457 (INDIRECT_REF
, NULL_TREE
,
4458 VEC_last (tree
, gnu_except_ptr_stack
)),
4459 get_identifier ("not_handled_by_others"), NULL_TREE
,
4464 else if (Nkind (gnat_temp
) == N_Identifier
4465 || Nkind (gnat_temp
) == N_Expanded_Name
)
4467 Entity_Id gnat_ex_id
= Entity (gnat_temp
);
4470 /* Exception may be a renaming. Recover original exception which is
4471 the one elaborated and registered. */
4472 if (Present (Renamed_Object (gnat_ex_id
)))
4473 gnat_ex_id
= Renamed_Object (gnat_ex_id
);
4475 gnu_expr
= gnat_to_gnu_entity (gnat_ex_id
, NULL_TREE
, 0);
4479 (EQ_EXPR
, boolean_type_node
,
4480 VEC_last (tree
, gnu_except_ptr_stack
),
4481 convert (TREE_TYPE (VEC_last (tree
, gnu_except_ptr_stack
)),
4482 build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
)));
4484 /* If this is the distinguished exception "Non_Ada_Error" (and we are
4485 in VMS mode), also allow a non-Ada exception (a VMS condition) t
4487 if (Is_Non_Ada_Error (Entity (gnat_temp
)))
4490 = build_component_ref
4491 (build_unary_op (INDIRECT_REF
, NULL_TREE
,
4492 VEC_last (tree
, gnu_except_ptr_stack
)),
4493 get_identifier ("lang"), NULL_TREE
, false);
4497 (TRUTH_ORIF_EXPR
, boolean_type_node
,
4498 build_binary_op (EQ_EXPR
, boolean_type_node
, gnu_comp
,
4499 build_int_cst (TREE_TYPE (gnu_comp
), 'V')),
4506 gnu_choice
= build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
4507 gnu_choice
, this_choice
);
4510 return build3 (COND_EXPR
, void_type_node
, gnu_choice
, gnu_body
, NULL_TREE
);
4513 /* Subroutine of gnat_to_gnu to translate gnat_node, an N_Exception_Handler,
4514 to a GCC tree, which is returned. This is the variant for ZCX. */
4517 Exception_Handler_to_gnu_zcx (Node_Id gnat_node
)
4519 tree gnu_etypes_list
= NULL_TREE
;
4522 tree gnu_current_exc_ptr
;
4523 tree prev_gnu_incoming_exc_ptr
;
4526 /* We build a TREE_LIST of nodes representing what exception types this
4527 handler can catch, with special cases for others and all others cases.
4529 Each exception type is actually identified by a pointer to the exception
4530 id, or to a dummy object for "others" and "all others". */
4531 for (gnat_temp
= First (Exception_Choices (gnat_node
));
4532 gnat_temp
; gnat_temp
= Next (gnat_temp
))
4534 if (Nkind (gnat_temp
) == N_Others_Choice
)
4537 = All_Others (gnat_temp
) ? all_others_decl
: others_decl
;
4540 = build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
4542 else if (Nkind (gnat_temp
) == N_Identifier
4543 || Nkind (gnat_temp
) == N_Expanded_Name
)
4545 Entity_Id gnat_ex_id
= Entity (gnat_temp
);
4547 /* Exception may be a renaming. Recover original exception which is
4548 the one elaborated and registered. */
4549 if (Present (Renamed_Object (gnat_ex_id
)))
4550 gnat_ex_id
= Renamed_Object (gnat_ex_id
);
4552 gnu_expr
= gnat_to_gnu_entity (gnat_ex_id
, NULL_TREE
, 0);
4553 gnu_etype
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_expr
);
4555 /* The Non_Ada_Error case for VMS exceptions is handled
4556 by the personality routine. */
4561 /* The GCC interface expects NULL to be passed for catch all handlers, so
4562 it would be quite tempting to set gnu_etypes_list to NULL if gnu_etype
4563 is integer_zero_node. It would not work, however, because GCC's
4564 notion of "catch all" is stronger than our notion of "others". Until
4565 we correctly use the cleanup interface as well, doing that would
4566 prevent the "all others" handlers from being seen, because nothing
4567 can be caught beyond a catch all from GCC's point of view. */
4568 gnu_etypes_list
= tree_cons (NULL_TREE
, gnu_etype
, gnu_etypes_list
);
4571 start_stmt_group ();
4574 /* Expand a call to the begin_handler hook at the beginning of the handler,
4575 and arrange for a call to the end_handler hook to occur on every possible
4578 The hooks expect a pointer to the low level occurrence. This is required
4579 for our stack management scheme because a raise inside the handler pushes
4580 a new occurrence on top of the stack, which means that this top does not
4581 necessarily match the occurrence this handler was dealing with.
4583 __builtin_eh_pointer references the exception occurrence being
4584 propagated. Upon handler entry, this is the exception for which the
4585 handler is triggered. This might not be the case upon handler exit,
4586 however, as we might have a new occurrence propagated by the handler's
4587 body, and the end_handler hook called as a cleanup in this context.
4589 We use a local variable to retrieve the incoming value at handler entry
4590 time, and reuse it to feed the end_handler hook's argument at exit. */
4593 = build_call_expr (builtin_decl_explicit (BUILT_IN_EH_POINTER
),
4594 1, integer_zero_node
);
4595 prev_gnu_incoming_exc_ptr
= gnu_incoming_exc_ptr
;
4596 gnu_incoming_exc_ptr
= create_var_decl (get_identifier ("EXPTR"), NULL_TREE
,
4597 ptr_type_node
, gnu_current_exc_ptr
,
4598 false, false, false, false,
4601 add_stmt_with_node (build_call_n_expr (begin_handler_decl
, 1,
4602 gnu_incoming_exc_ptr
),
4604 /* ??? We don't seem to have an End_Label at hand to set the location. */
4605 add_cleanup (build_call_n_expr (end_handler_decl
, 1, gnu_incoming_exc_ptr
),
4607 add_stmt_list (Statements (gnat_node
));
4610 gnu_incoming_exc_ptr
= prev_gnu_incoming_exc_ptr
;
4612 return build2 (CATCH_EXPR
, void_type_node
, gnu_etypes_list
,
4616 /* Subroutine of gnat_to_gnu to generate code for an N_Compilation unit. */
4619 Compilation_Unit_to_gnu (Node_Id gnat_node
)
4621 const Node_Id gnat_unit
= Unit (gnat_node
);
4622 const bool body_p
= (Nkind (gnat_unit
) == N_Package_Body
4623 || Nkind (gnat_unit
) == N_Subprogram_Body
);
4624 const Entity_Id gnat_unit_entity
= Defining_Entity (gnat_unit
);
4625 /* Make the decl for the elaboration procedure. */
4626 tree gnu_elab_proc_decl
4627 = create_subprog_decl
4628 (create_concat_name (gnat_unit_entity
, body_p
? "elabb" : "elabs"),
4629 NULL_TREE
, void_ftype
, NULL_TREE
, false, true, false, true, NULL
,
4631 struct elab_info
*info
;
4633 VEC_safe_push (tree
, gc
, gnu_elab_proc_stack
, gnu_elab_proc_decl
);
4634 DECL_ELABORATION_PROC_P (gnu_elab_proc_decl
) = 1;
4636 /* Initialize the information structure for the function. */
4637 allocate_struct_function (gnu_elab_proc_decl
, false);
4640 current_function_decl
= NULL_TREE
;
4642 start_stmt_group ();
4645 /* For a body, first process the spec if there is one. */
4646 if (Nkind (gnat_unit
) == N_Package_Body
4647 || (Nkind (gnat_unit
) == N_Subprogram_Body
&& !Acts_As_Spec (gnat_node
)))
4648 add_stmt (gnat_to_gnu (Library_Unit (gnat_node
)));
4650 if (type_annotate_only
&& gnat_node
== Cunit (Main_Unit
))
4652 elaborate_all_entities (gnat_node
);
4654 if (Nkind (gnat_unit
) == N_Subprogram_Declaration
4655 || Nkind (gnat_unit
) == N_Generic_Package_Declaration
4656 || Nkind (gnat_unit
) == N_Generic_Subprogram_Declaration
)
4660 process_decls (Declarations (Aux_Decls_Node (gnat_node
)), Empty
, Empty
,
4662 add_stmt (gnat_to_gnu (gnat_unit
));
4664 /* If we can inline, generate code for all the inlined subprograms. */
4667 Entity_Id gnat_entity
;
4669 for (gnat_entity
= First_Inlined_Subprogram (gnat_node
);
4670 Present (gnat_entity
);
4671 gnat_entity
= Next_Inlined_Subprogram (gnat_entity
))
4673 Node_Id gnat_body
= Parent (Declaration_Node (gnat_entity
));
4675 if (Nkind (gnat_body
) != N_Subprogram_Body
)
4677 /* ??? This really should always be present. */
4678 if (No (Corresponding_Body (gnat_body
)))
4681 = Parent (Declaration_Node (Corresponding_Body (gnat_body
)));
4684 if (Present (gnat_body
))
4686 /* Define the entity first so we set DECL_EXTERNAL. */
4687 gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
4688 add_stmt (gnat_to_gnu (gnat_body
));
4693 /* Process any pragmas and actions following the unit. */
4694 add_stmt_list (Pragmas_After (Aux_Decls_Node (gnat_node
)));
4695 add_stmt_list (Actions (Aux_Decls_Node (gnat_node
)));
4696 finalize_from_with_types ();
4698 /* Save away what we've made so far and record this potential elaboration
4700 info
= ggc_alloc_elab_info ();
4701 set_current_block_context (gnu_elab_proc_decl
);
4703 DECL_SAVED_TREE (gnu_elab_proc_decl
) = end_stmt_group ();
4705 set_end_locus_from_node (gnu_elab_proc_decl
, gnat_unit
);
4707 info
->next
= elab_info_list
;
4708 info
->elab_proc
= gnu_elab_proc_decl
;
4709 info
->gnat_node
= gnat_node
;
4710 elab_info_list
= info
;
4712 /* Generate elaboration code for this unit, if necessary, and say whether
4714 VEC_pop (tree
, gnu_elab_proc_stack
);
4716 /* Invalidate the global renaming pointers. This is necessary because
4717 stabilization of the renamed entities may create SAVE_EXPRs which
4718 have been tied to a specific elaboration routine just above. */
4719 invalidate_global_renaming_pointers ();
4722 /* Return true if GNAT_NODE is on the LHS of an assignment or an actual
4723 parameter of a call. */
4726 lhs_or_actual_p (Node_Id gnat_node
)
4728 Node_Id gnat_parent
= Parent (gnat_node
);
4729 Node_Kind kind
= Nkind (gnat_parent
);
4731 if (kind
== N_Assignment_Statement
&& Name (gnat_parent
) == gnat_node
)
4734 if ((kind
== N_Procedure_Call_Statement
|| kind
== N_Function_Call
)
4735 && Name (gnat_parent
) != gnat_node
)
4738 if (kind
== N_Parameter_Association
)
4744 /* Return true if either GNAT_NODE or a view of GNAT_NODE is on the LHS
4745 of an assignment or an actual parameter of a call. */
4748 present_in_lhs_or_actual_p (Node_Id gnat_node
)
4752 if (lhs_or_actual_p (gnat_node
))
4755 kind
= Nkind (Parent (gnat_node
));
4757 if ((kind
== N_Type_Conversion
|| kind
== N_Unchecked_Type_Conversion
)
4758 && lhs_or_actual_p (Parent (gnat_node
)))
4764 /* Return true if GNAT_NODE, an unchecked type conversion, is a no-op as far
4765 as gigi is concerned. This is used to avoid conversions on the LHS. */
4768 unchecked_conversion_nop (Node_Id gnat_node
)
4770 Entity_Id from_type
, to_type
;
4772 /* The conversion must be on the LHS of an assignment or an actual parameter
4773 of a call. Otherwise, even if the conversion was essentially a no-op, it
4774 could de facto ensure type consistency and this should be preserved. */
4775 if (!lhs_or_actual_p (gnat_node
))
4778 from_type
= Etype (Expression (gnat_node
));
4780 /* We're interested in artificial conversions generated by the front-end
4781 to make private types explicit, e.g. in Expand_Assign_Array. */
4782 if (!Is_Private_Type (from_type
))
4785 from_type
= Underlying_Type (from_type
);
4786 to_type
= Etype (gnat_node
);
4788 /* The direct conversion to the underlying type is a no-op. */
4789 if (to_type
== from_type
)
4792 /* For an array subtype, the conversion to the PAT is a no-op. */
4793 if (Ekind (from_type
) == E_Array_Subtype
4794 && to_type
== Packed_Array_Type (from_type
))
4797 /* For a record subtype, the conversion to the type is a no-op. */
4798 if (Ekind (from_type
) == E_Record_Subtype
4799 && to_type
== Etype (from_type
))
4805 /* This function is the driver of the GNAT to GCC tree transformation process.
4806 It is the entry point of the tree transformer. GNAT_NODE is the root of
4807 some GNAT tree. Return the root of the corresponding GCC tree. If this
4808 is an expression, return the GCC equivalent of the expression. If this
4809 is a statement, return the statement or add it to the current statement
4810 group, in which case anything returned is to be interpreted as occurring
4811 after anything added. */
4814 gnat_to_gnu (Node_Id gnat_node
)
4816 const Node_Kind kind
= Nkind (gnat_node
);
4817 bool went_into_elab_proc
= false;
4818 tree gnu_result
= error_mark_node
; /* Default to no value. */
4819 tree gnu_result_type
= void_type_node
;
4820 tree gnu_expr
, gnu_lhs
, gnu_rhs
;
4823 /* Save node number for error message and set location information. */
4824 error_gnat_node
= gnat_node
;
4825 Sloc_to_locus (Sloc (gnat_node
), &input_location
);
4827 /* If this node is a statement and we are only annotating types, return an
4828 empty statement list. */
4829 if (type_annotate_only
&& IN (kind
, N_Statement_Other_Than_Procedure_Call
))
4830 return alloc_stmt_list ();
4832 /* If this node is a non-static subexpression and we are only annotating
4833 types, make this into a NULL_EXPR. */
4834 if (type_annotate_only
4835 && IN (kind
, N_Subexpr
)
4836 && kind
!= N_Identifier
4837 && !Compile_Time_Known_Value (gnat_node
))
4838 return build1 (NULL_EXPR
, get_unpadded_type (Etype (gnat_node
)),
4839 build_call_raise (CE_Range_Check_Failed
, gnat_node
,
4840 N_Raise_Constraint_Error
));
4842 if ((IN (kind
, N_Statement_Other_Than_Procedure_Call
)
4843 && kind
!= N_Null_Statement
)
4844 || kind
== N_Procedure_Call_Statement
4846 || kind
== N_Implicit_Label_Declaration
4847 || kind
== N_Handled_Sequence_Of_Statements
4848 || (IN (kind
, N_Raise_xxx_Error
) && Ekind (Etype (gnat_node
)) == E_Void
))
4850 tree current_elab_proc
= get_elaboration_procedure ();
4852 /* If this is a statement and we are at top level, it must be part of
4853 the elaboration procedure, so mark us as being in that procedure. */
4854 if (!current_function_decl
)
4856 current_function_decl
= current_elab_proc
;
4857 went_into_elab_proc
= true;
4860 /* If we are in the elaboration procedure, check if we are violating a
4861 No_Elaboration_Code restriction by having a statement there. Don't
4862 check for a possible No_Elaboration_Code restriction violation on
4863 N_Handled_Sequence_Of_Statements, as we want to signal an error on
4864 every nested real statement instead. This also avoids triggering
4865 spurious errors on dummy (empty) sequences created by the front-end
4866 for package bodies in some cases. */
4867 if (current_function_decl
== current_elab_proc
4868 && kind
!= N_Handled_Sequence_Of_Statements
)
4869 Check_Elaboration_Code_Allowed (gnat_node
);
4874 /********************************/
4875 /* Chapter 2: Lexical Elements */
4876 /********************************/
4879 case N_Expanded_Name
:
4880 case N_Operator_Symbol
:
4881 case N_Defining_Identifier
:
4882 gnu_result
= Identifier_to_gnu (gnat_node
, &gnu_result_type
);
4884 /* If this is an atomic access on the RHS for which synchronization is
4885 required, build the atomic load. */
4886 if (atomic_sync_required_p (gnat_node
)
4887 && !present_in_lhs_or_actual_p (gnat_node
))
4888 gnu_result
= build_atomic_load (gnu_result
);
4891 case N_Integer_Literal
:
4895 /* Get the type of the result, looking inside any padding and
4896 justified modular types. Then get the value in that type. */
4897 gnu_type
= gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
4899 if (TREE_CODE (gnu_type
) == RECORD_TYPE
4900 && TYPE_JUSTIFIED_MODULAR_P (gnu_type
))
4901 gnu_type
= TREE_TYPE (TYPE_FIELDS (gnu_type
));
4903 gnu_result
= UI_To_gnu (Intval (gnat_node
), gnu_type
);
4905 /* If the result overflows (meaning it doesn't fit in its base type),
4906 abort. We would like to check that the value is within the range
4907 of the subtype, but that causes problems with subtypes whose usage
4908 will raise Constraint_Error and with biased representation, so
4910 gcc_assert (!TREE_OVERFLOW (gnu_result
));
4914 case N_Character_Literal
:
4915 /* If a Entity is present, it means that this was one of the
4916 literals in a user-defined character type. In that case,
4917 just return the value in the CONST_DECL. Otherwise, use the
4918 character code. In that case, the base type should be an
4919 INTEGER_TYPE, but we won't bother checking for that. */
4920 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
4921 if (Present (Entity (gnat_node
)))
4922 gnu_result
= DECL_INITIAL (get_gnu_tree (Entity (gnat_node
)));
4925 = build_int_cst_type
4926 (gnu_result_type
, UI_To_CC (Char_Literal_Value (gnat_node
)));
4929 case N_Real_Literal
:
4930 /* If this is of a fixed-point type, the value we want is the
4931 value of the corresponding integer. */
4932 if (IN (Ekind (Underlying_Type (Etype (gnat_node
))), Fixed_Point_Kind
))
4934 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
4935 gnu_result
= UI_To_gnu (Corresponding_Integer_Value (gnat_node
),
4937 gcc_assert (!TREE_OVERFLOW (gnu_result
));
4940 /* We should never see a Vax_Float type literal, since the front end
4941 is supposed to transform these using appropriate conversions. */
4942 else if (Vax_Float (Underlying_Type (Etype (gnat_node
))))
4947 Ureal ur_realval
= Realval (gnat_node
);
4949 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
4951 /* If the real value is zero, so is the result. Otherwise,
4952 convert it to a machine number if it isn't already. That
4953 forces BASE to 0 or 2 and simplifies the rest of our logic. */
4954 if (UR_Is_Zero (ur_realval
))
4955 gnu_result
= convert (gnu_result_type
, integer_zero_node
);
4958 if (!Is_Machine_Number (gnat_node
))
4960 = Machine (Base_Type (Underlying_Type (Etype (gnat_node
))),
4961 ur_realval
, Round_Even
, gnat_node
);
4964 = UI_To_gnu (Numerator (ur_realval
), gnu_result_type
);
4966 /* If we have a base of zero, divide by the denominator.
4967 Otherwise, the base must be 2 and we scale the value, which
4968 we know can fit in the mantissa of the type (hence the use
4969 of that type above). */
4970 if (No (Rbase (ur_realval
)))
4972 = build_binary_op (RDIV_EXPR
,
4973 get_base_type (gnu_result_type
),
4975 UI_To_gnu (Denominator (ur_realval
),
4979 REAL_VALUE_TYPE tmp
;
4981 gcc_assert (Rbase (ur_realval
) == 2);
4982 real_ldexp (&tmp
, &TREE_REAL_CST (gnu_result
),
4983 - UI_To_Int (Denominator (ur_realval
)));
4984 gnu_result
= build_real (gnu_result_type
, tmp
);
4988 /* Now see if we need to negate the result. Do it this way to
4989 properly handle -0. */
4990 if (UR_Is_Negative (Realval (gnat_node
)))
4992 = build_unary_op (NEGATE_EXPR
, get_base_type (gnu_result_type
),
4998 case N_String_Literal
:
4999 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5000 if (TYPE_PRECISION (TREE_TYPE (gnu_result_type
)) == HOST_BITS_PER_CHAR
)
5002 String_Id gnat_string
= Strval (gnat_node
);
5003 int length
= String_Length (gnat_string
);
5006 if (length
>= ALLOCA_THRESHOLD
)
5007 string
= XNEWVEC (char, length
+ 1);
5009 string
= (char *) alloca (length
+ 1);
5011 /* Build the string with the characters in the literal. Note
5012 that Ada strings are 1-origin. */
5013 for (i
= 0; i
< length
; i
++)
5014 string
[i
] = Get_String_Char (gnat_string
, i
+ 1);
5016 /* Put a null at the end of the string in case it's in a context
5017 where GCC will want to treat it as a C string. */
5020 gnu_result
= build_string (length
, string
);
5022 /* Strings in GCC don't normally have types, but we want
5023 this to not be converted to the array type. */
5024 TREE_TYPE (gnu_result
) = gnu_result_type
;
5026 if (length
>= ALLOCA_THRESHOLD
)
5031 /* Build a list consisting of each character, then make
5033 String_Id gnat_string
= Strval (gnat_node
);
5034 int length
= String_Length (gnat_string
);
5036 tree gnu_idx
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type
));
5037 VEC(constructor_elt
,gc
) *gnu_vec
5038 = VEC_alloc (constructor_elt
, gc
, length
);
5040 for (i
= 0; i
< length
; i
++)
5042 tree t
= build_int_cst (TREE_TYPE (gnu_result_type
),
5043 Get_String_Char (gnat_string
, i
+ 1));
5045 CONSTRUCTOR_APPEND_ELT (gnu_vec
, gnu_idx
, t
);
5046 gnu_idx
= int_const_binop (PLUS_EXPR
, gnu_idx
, integer_one_node
);
5049 gnu_result
= gnat_build_constructor (gnu_result_type
, gnu_vec
);
5054 gnu_result
= Pragma_to_gnu (gnat_node
);
5057 /**************************************/
5058 /* Chapter 3: Declarations and Types */
5059 /**************************************/
5061 case N_Subtype_Declaration
:
5062 case N_Full_Type_Declaration
:
5063 case N_Incomplete_Type_Declaration
:
5064 case N_Private_Type_Declaration
:
5065 case N_Private_Extension_Declaration
:
5066 case N_Task_Type_Declaration
:
5067 process_type (Defining_Entity (gnat_node
));
5068 gnu_result
= alloc_stmt_list ();
5071 case N_Object_Declaration
:
5072 case N_Exception_Declaration
:
5073 gnat_temp
= Defining_Entity (gnat_node
);
5074 gnu_result
= alloc_stmt_list ();
5076 /* If we are just annotating types and this object has an unconstrained
5077 or task type, don't elaborate it. */
5078 if (type_annotate_only
5079 && (((Is_Array_Type (Etype (gnat_temp
))
5080 || Is_Record_Type (Etype (gnat_temp
)))
5081 && !Is_Constrained (Etype (gnat_temp
)))
5082 || Is_Concurrent_Type (Etype (gnat_temp
))))
5085 if (Present (Expression (gnat_node
))
5086 && !(kind
== N_Object_Declaration
&& No_Initialization (gnat_node
))
5087 && (!type_annotate_only
5088 || Compile_Time_Known_Value (Expression (gnat_node
))))
5090 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
5091 if (Do_Range_Check (Expression (gnat_node
)))
5093 = emit_range_check (gnu_expr
, Etype (gnat_temp
), gnat_node
);
5095 /* If this object has its elaboration delayed, we must force
5096 evaluation of GNU_EXPR right now and save it for when the object
5098 if (Present (Freeze_Node (gnat_temp
)))
5100 if (TREE_CONSTANT (gnu_expr
))
5102 else if (global_bindings_p ())
5104 = create_var_decl (create_concat_name (gnat_temp
, "init"),
5105 NULL_TREE
, TREE_TYPE (gnu_expr
), gnu_expr
,
5106 false, false, false, false,
5109 gnu_expr
= gnat_save_expr (gnu_expr
);
5111 save_gnu_tree (gnat_node
, gnu_expr
, true);
5115 gnu_expr
= NULL_TREE
;
5117 if (type_annotate_only
&& gnu_expr
&& TREE_CODE (gnu_expr
) == ERROR_MARK
)
5118 gnu_expr
= NULL_TREE
;
5120 /* If this is a deferred constant with an address clause, we ignore the
5121 full view since the clause is on the partial view and we cannot have
5122 2 different GCC trees for the object. The only bits of the full view
5123 we will use is the initializer, but it will be directly fetched. */
5124 if (Ekind(gnat_temp
) == E_Constant
5125 && Present (Address_Clause (gnat_temp
))
5126 && Present (Full_View (gnat_temp
)))
5127 save_gnu_tree (Full_View (gnat_temp
), error_mark_node
, true);
5129 if (No (Freeze_Node (gnat_temp
)))
5130 gnat_to_gnu_entity (gnat_temp
, gnu_expr
, 1);
5133 case N_Object_Renaming_Declaration
:
5134 gnat_temp
= Defining_Entity (gnat_node
);
5136 /* Don't do anything if this renaming is handled by the front end or if
5137 we are just annotating types and this object has a composite or task
5138 type, don't elaborate it. We return the result in case it has any
5139 SAVE_EXPRs in it that need to be evaluated here. */
5140 if (!Is_Renaming_Of_Object (gnat_temp
)
5141 && ! (type_annotate_only
5142 && (Is_Array_Type (Etype (gnat_temp
))
5143 || Is_Record_Type (Etype (gnat_temp
))
5144 || Is_Concurrent_Type (Etype (gnat_temp
)))))
5146 = gnat_to_gnu_entity (gnat_temp
,
5147 gnat_to_gnu (Renamed_Object (gnat_temp
)), 1);
5149 gnu_result
= alloc_stmt_list ();
5152 case N_Implicit_Label_Declaration
:
5153 gnat_to_gnu_entity (Defining_Entity (gnat_node
), NULL_TREE
, 1);
5154 gnu_result
= alloc_stmt_list ();
5157 case N_Exception_Renaming_Declaration
:
5158 case N_Number_Declaration
:
5159 case N_Package_Renaming_Declaration
:
5160 case N_Subprogram_Renaming_Declaration
:
5161 /* These are fully handled in the front end. */
5162 gnu_result
= alloc_stmt_list ();
5165 /*************************************/
5166 /* Chapter 4: Names and Expressions */
5167 /*************************************/
5169 case N_Explicit_Dereference
:
5170 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
5171 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5172 gnu_result
= build_unary_op (INDIRECT_REF
, NULL_TREE
, gnu_result
);
5174 /* If this is an atomic access on the RHS for which synchronization is
5175 required, build the atomic load. */
5176 if (atomic_sync_required_p (gnat_node
)
5177 && !present_in_lhs_or_actual_p (gnat_node
))
5178 gnu_result
= build_atomic_load (gnu_result
);
5181 case N_Indexed_Component
:
5183 tree gnu_array_object
= gnat_to_gnu (Prefix (gnat_node
));
5187 Node_Id
*gnat_expr_array
;
5189 gnu_array_object
= maybe_implicit_deref (gnu_array_object
);
5191 /* Convert vector inputs to their representative array type, to fit
5192 what the code below expects. */
5193 gnu_array_object
= maybe_vector_array (gnu_array_object
);
5195 gnu_array_object
= maybe_unconstrained_array (gnu_array_object
);
5197 /* If we got a padded type, remove it too. */
5198 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_array_object
)))
5200 = convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_array_object
))),
5203 gnu_result
= gnu_array_object
;
5205 /* First compute the number of dimensions of the array, then
5206 fill the expression array, the order depending on whether
5207 this is a Convention_Fortran array or not. */
5208 for (ndim
= 1, gnu_type
= TREE_TYPE (gnu_array_object
);
5209 TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
5210 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
));
5211 ndim
++, gnu_type
= TREE_TYPE (gnu_type
))
5214 gnat_expr_array
= XALLOCAVEC (Node_Id
, ndim
);
5216 if (TYPE_CONVENTION_FORTRAN_P (TREE_TYPE (gnu_array_object
)))
5217 for (i
= ndim
- 1, gnat_temp
= First (Expressions (gnat_node
));
5219 i
--, gnat_temp
= Next (gnat_temp
))
5220 gnat_expr_array
[i
] = gnat_temp
;
5222 for (i
= 0, gnat_temp
= First (Expressions (gnat_node
));
5224 i
++, gnat_temp
= Next (gnat_temp
))
5225 gnat_expr_array
[i
] = gnat_temp
;
5227 for (i
= 0, gnu_type
= TREE_TYPE (gnu_array_object
);
5228 i
< ndim
; i
++, gnu_type
= TREE_TYPE (gnu_type
))
5230 gcc_assert (TREE_CODE (gnu_type
) == ARRAY_TYPE
);
5231 gnat_temp
= gnat_expr_array
[i
];
5232 gnu_expr
= gnat_to_gnu (gnat_temp
);
5234 if (Do_Range_Check (gnat_temp
))
5237 (gnu_array_object
, gnu_expr
,
5238 TYPE_MIN_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))),
5239 TYPE_MAX_VALUE (TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
))),
5242 gnu_result
= build_binary_op (ARRAY_REF
, NULL_TREE
,
5243 gnu_result
, gnu_expr
);
5246 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5248 /* If this is an atomic access on the RHS for which synchronization is
5249 required, build the atomic load. */
5250 if (atomic_sync_required_p (gnat_node
)
5251 && !present_in_lhs_or_actual_p (gnat_node
))
5252 gnu_result
= build_atomic_load (gnu_result
);
5258 Node_Id gnat_range_node
= Discrete_Range (gnat_node
);
5261 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
5262 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5264 /* Do any implicit dereferences of the prefix and do any needed
5266 gnu_result
= maybe_implicit_deref (gnu_result
);
5267 gnu_result
= maybe_unconstrained_array (gnu_result
);
5268 gnu_type
= TREE_TYPE (gnu_result
);
5269 if (Do_Range_Check (gnat_range_node
))
5271 /* Get the bounds of the slice. */
5273 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_result_type
));
5274 tree gnu_min_expr
= TYPE_MIN_VALUE (gnu_index_type
);
5275 tree gnu_max_expr
= TYPE_MAX_VALUE (gnu_index_type
);
5276 /* Get the permitted bounds. */
5277 tree gnu_base_index_type
5278 = TYPE_INDEX_TYPE (TYPE_DOMAIN (gnu_type
));
5279 tree gnu_base_min_expr
= SUBSTITUTE_PLACEHOLDER_IN_EXPR
5280 (TYPE_MIN_VALUE (gnu_base_index_type
), gnu_result
);
5281 tree gnu_base_max_expr
= SUBSTITUTE_PLACEHOLDER_IN_EXPR
5282 (TYPE_MAX_VALUE (gnu_base_index_type
), gnu_result
);
5283 tree gnu_expr_l
, gnu_expr_h
, gnu_expr_type
;
5285 gnu_min_expr
= gnat_protect_expr (gnu_min_expr
);
5286 gnu_max_expr
= gnat_protect_expr (gnu_max_expr
);
5288 /* Derive a good type to convert everything to. */
5289 gnu_expr_type
= get_base_type (gnu_index_type
);
5291 /* Test whether the minimum slice value is too small. */
5292 gnu_expr_l
= build_binary_op (LT_EXPR
, boolean_type_node
,
5293 convert (gnu_expr_type
,
5295 convert (gnu_expr_type
,
5296 gnu_base_min_expr
));
5298 /* Test whether the maximum slice value is too large. */
5299 gnu_expr_h
= build_binary_op (GT_EXPR
, boolean_type_node
,
5300 convert (gnu_expr_type
,
5302 convert (gnu_expr_type
,
5303 gnu_base_max_expr
));
5305 /* Build a slice index check that returns the low bound,
5306 assuming the slice is not empty. */
5307 gnu_expr
= emit_check
5308 (build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
5309 gnu_expr_l
, gnu_expr_h
),
5310 gnu_min_expr
, CE_Index_Check_Failed
, gnat_node
);
5312 /* Build a conditional expression that does the index checks and
5313 returns the low bound if the slice is not empty (max >= min),
5314 and returns the naked low bound otherwise (max < min), unless
5315 it is non-constant and the high bound is; this prevents VRP
5316 from inferring bogus ranges on the unlikely path. */
5317 gnu_expr
= fold_build3 (COND_EXPR
, gnu_expr_type
,
5318 build_binary_op (GE_EXPR
, gnu_expr_type
,
5319 convert (gnu_expr_type
,
5321 convert (gnu_expr_type
,
5324 TREE_CODE (gnu_min_expr
) != INTEGER_CST
5325 && TREE_CODE (gnu_max_expr
) == INTEGER_CST
5326 ? gnu_max_expr
: gnu_min_expr
);
5329 /* Simply return the naked low bound. */
5330 gnu_expr
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_result_type
));
5332 /* If this is a slice with non-constant size of an array with constant
5333 size, set the maximum size for the allocation of temporaries. */
5334 if (!TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_result_type
))
5335 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_type
)))
5336 TYPE_ARRAY_MAX_SIZE (gnu_result_type
) = TYPE_SIZE_UNIT (gnu_type
);
5338 gnu_result
= build_binary_op (ARRAY_RANGE_REF
, gnu_result_type
,
5339 gnu_result
, gnu_expr
);
5343 case N_Selected_Component
:
5345 tree gnu_prefix
= gnat_to_gnu (Prefix (gnat_node
));
5346 Entity_Id gnat_field
= Entity (Selector_Name (gnat_node
));
5347 Entity_Id gnat_pref_type
= Etype (Prefix (gnat_node
));
5350 while (IN (Ekind (gnat_pref_type
), Incomplete_Or_Private_Kind
)
5351 || IN (Ekind (gnat_pref_type
), Access_Kind
))
5353 if (IN (Ekind (gnat_pref_type
), Incomplete_Or_Private_Kind
))
5354 gnat_pref_type
= Underlying_Type (gnat_pref_type
);
5355 else if (IN (Ekind (gnat_pref_type
), Access_Kind
))
5356 gnat_pref_type
= Designated_Type (gnat_pref_type
);
5359 gnu_prefix
= maybe_implicit_deref (gnu_prefix
);
5361 /* For discriminant references in tagged types always substitute the
5362 corresponding discriminant as the actual selected component. */
5363 if (Is_Tagged_Type (gnat_pref_type
))
5364 while (Present (Corresponding_Discriminant (gnat_field
)))
5365 gnat_field
= Corresponding_Discriminant (gnat_field
);
5367 /* For discriminant references of untagged types always substitute the
5368 corresponding stored discriminant. */
5369 else if (Present (Corresponding_Discriminant (gnat_field
)))
5370 gnat_field
= Original_Record_Component (gnat_field
);
5372 /* Handle extracting the real or imaginary part of a complex.
5373 The real part is the first field and the imaginary the last. */
5374 if (TREE_CODE (TREE_TYPE (gnu_prefix
)) == COMPLEX_TYPE
)
5375 gnu_result
= build_unary_op (Present (Next_Entity (gnat_field
))
5376 ? REALPART_EXPR
: IMAGPART_EXPR
,
5377 NULL_TREE
, gnu_prefix
);
5380 gnu_field
= gnat_to_gnu_field_decl (gnat_field
);
5382 /* If there are discriminants, the prefix might be evaluated more
5383 than once, which is a problem if it has side-effects. */
5384 if (Has_Discriminants (Is_Access_Type (Etype (Prefix (gnat_node
)))
5385 ? Designated_Type (Etype
5386 (Prefix (gnat_node
)))
5387 : Etype (Prefix (gnat_node
))))
5388 gnu_prefix
= gnat_stabilize_reference (gnu_prefix
, false, NULL
);
5391 = build_component_ref (gnu_prefix
, NULL_TREE
, gnu_field
,
5392 (Nkind (Parent (gnat_node
))
5393 == N_Attribute_Reference
)
5394 && lvalue_required_for_attribute_p
5395 (Parent (gnat_node
)));
5398 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5400 /* If this is an atomic access on the RHS for which synchronization is
5401 required, build the atomic load. */
5402 if (atomic_sync_required_p (gnat_node
)
5403 && !present_in_lhs_or_actual_p (gnat_node
))
5404 gnu_result
= build_atomic_load (gnu_result
);
5408 case N_Attribute_Reference
:
5410 /* The attribute designator. */
5411 const int attr
= Get_Attribute_Id (Attribute_Name (gnat_node
));
5413 /* The Elab_Spec and Elab_Body attributes are special in that Prefix
5414 is a unit, not an object with a GCC equivalent. */
5415 if (attr
== Attr_Elab_Spec
|| attr
== Attr_Elab_Body
)
5417 create_subprog_decl (create_concat_name
5418 (Entity (Prefix (gnat_node
)),
5419 attr
== Attr_Elab_Body
? "elabb" : "elabs"),
5420 NULL_TREE
, void_ftype
, NULL_TREE
, false,
5421 true, true, true, NULL
, gnat_node
);
5423 gnu_result
= Attribute_to_gnu (gnat_node
, &gnu_result_type
, attr
);
5428 /* Like 'Access as far as we are concerned. */
5429 gnu_result
= gnat_to_gnu (Prefix (gnat_node
));
5430 gnu_result
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_result
);
5431 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5435 case N_Extension_Aggregate
:
5439 /* ??? It is wrong to evaluate the type now, but there doesn't
5440 seem to be any other practical way of doing it. */
5442 gcc_assert (!Expansion_Delayed (gnat_node
));
5444 gnu_aggr_type
= gnu_result_type
5445 = get_unpadded_type (Etype (gnat_node
));
5447 if (TREE_CODE (gnu_result_type
) == RECORD_TYPE
5448 && TYPE_CONTAINS_TEMPLATE_P (gnu_result_type
))
5450 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_result_type
)));
5451 else if (TREE_CODE (gnu_result_type
) == VECTOR_TYPE
)
5452 gnu_aggr_type
= TYPE_REPRESENTATIVE_ARRAY (gnu_result_type
);
5454 if (Null_Record_Present (gnat_node
))
5455 gnu_result
= gnat_build_constructor (gnu_aggr_type
, NULL
);
5457 else if (TREE_CODE (gnu_aggr_type
) == RECORD_TYPE
5458 || TREE_CODE (gnu_aggr_type
) == UNION_TYPE
)
5460 = assoc_to_constructor (Etype (gnat_node
),
5461 First (Component_Associations (gnat_node
)),
5463 else if (TREE_CODE (gnu_aggr_type
) == ARRAY_TYPE
)
5464 gnu_result
= pos_to_constructor (First (Expressions (gnat_node
)),
5466 Component_Type (Etype (gnat_node
)));
5467 else if (TREE_CODE (gnu_aggr_type
) == COMPLEX_TYPE
)
5470 (COMPLEX_EXPR
, gnu_aggr_type
,
5471 gnat_to_gnu (Expression (First
5472 (Component_Associations (gnat_node
)))),
5473 gnat_to_gnu (Expression
5475 (First (Component_Associations (gnat_node
))))));
5479 gnu_result
= convert (gnu_result_type
, gnu_result
);
5484 if (TARGET_VTABLE_USES_DESCRIPTORS
5485 && Ekind (Etype (gnat_node
)) == E_Access_Subprogram_Type
5486 && Is_Dispatch_Table_Entity (Etype (gnat_node
)))
5487 gnu_result
= null_fdesc_node
;
5489 gnu_result
= null_pointer_node
;
5490 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5493 case N_Type_Conversion
:
5494 case N_Qualified_Expression
:
5495 /* Get the operand expression. */
5496 gnu_result
= gnat_to_gnu (Expression (gnat_node
));
5497 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5500 = convert_with_check (Etype (gnat_node
), gnu_result
,
5501 Do_Overflow_Check (gnat_node
),
5502 Do_Range_Check (Expression (gnat_node
)),
5503 kind
== N_Type_Conversion
5504 && Float_Truncate (gnat_node
), gnat_node
);
5507 case N_Unchecked_Type_Conversion
:
5508 gnu_result
= gnat_to_gnu (Expression (gnat_node
));
5510 /* Skip further processing if the conversion is deemed a no-op. */
5511 if (unchecked_conversion_nop (gnat_node
))
5513 gnu_result_type
= TREE_TYPE (gnu_result
);
5517 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5519 /* If the result is a pointer type, see if we are improperly
5520 converting to a stricter alignment. */
5521 if (STRICT_ALIGNMENT
&& POINTER_TYPE_P (gnu_result_type
)
5522 && IN (Ekind (Etype (gnat_node
)), Access_Kind
))
5524 unsigned int align
= known_alignment (gnu_result
);
5525 tree gnu_obj_type
= TREE_TYPE (gnu_result_type
);
5526 unsigned int oalign
= TYPE_ALIGN (gnu_obj_type
);
5528 if (align
!= 0 && align
< oalign
&& !TYPE_ALIGN_OK (gnu_obj_type
))
5529 post_error_ne_tree_2
5530 ("?source alignment (^) '< alignment of & (^)",
5531 gnat_node
, Designated_Type (Etype (gnat_node
)),
5532 size_int (align
/ BITS_PER_UNIT
), oalign
/ BITS_PER_UNIT
);
5535 /* If we are converting a descriptor to a function pointer, first
5536 build the pointer. */
5537 if (TARGET_VTABLE_USES_DESCRIPTORS
5538 && TREE_TYPE (gnu_result
) == fdesc_type_node
5539 && POINTER_TYPE_P (gnu_result_type
))
5540 gnu_result
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_result
);
5542 gnu_result
= unchecked_convert (gnu_result_type
, gnu_result
,
5543 No_Truncation (gnat_node
));
5549 tree gnu_obj
= gnat_to_gnu (Left_Opnd (gnat_node
));
5550 Node_Id gnat_range
= Right_Opnd (gnat_node
);
5551 tree gnu_low
, gnu_high
;
5553 /* GNAT_RANGE is either an N_Range node or an identifier denoting a
5555 if (Nkind (gnat_range
) == N_Range
)
5557 gnu_low
= gnat_to_gnu (Low_Bound (gnat_range
));
5558 gnu_high
= gnat_to_gnu (High_Bound (gnat_range
));
5560 else if (Nkind (gnat_range
) == N_Identifier
5561 || Nkind (gnat_range
) == N_Expanded_Name
)
5563 tree gnu_range_type
= get_unpadded_type (Entity (gnat_range
));
5565 gnu_low
= TYPE_MIN_VALUE (gnu_range_type
);
5566 gnu_high
= TYPE_MAX_VALUE (gnu_range_type
);
5571 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5573 /* If LOW and HIGH are identical, perform an equality test. Otherwise,
5574 ensure that GNU_OBJ is evaluated only once and perform a full range
5576 if (operand_equal_p (gnu_low
, gnu_high
, 0))
5578 = build_binary_op (EQ_EXPR
, gnu_result_type
, gnu_obj
, gnu_low
);
5582 gnu_obj
= gnat_protect_expr (gnu_obj
);
5583 t1
= build_binary_op (GE_EXPR
, gnu_result_type
, gnu_obj
, gnu_low
);
5585 set_expr_location_from_node (t1
, gnat_node
);
5586 t2
= build_binary_op (LE_EXPR
, gnu_result_type
, gnu_obj
, gnu_high
);
5588 set_expr_location_from_node (t2
, gnat_node
);
5590 = build_binary_op (TRUTH_ANDIF_EXPR
, gnu_result_type
, t1
, t2
);
5593 if (kind
== N_Not_In
)
5595 = invert_truthvalue_loc (EXPR_LOCATION (gnu_result
), gnu_result
);
5600 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
5601 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
5602 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5603 gnu_result
= build_binary_op (FLOAT_TYPE_P (gnu_result_type
)
5605 : (Rounded_Result (gnat_node
)
5606 ? ROUND_DIV_EXPR
: TRUNC_DIV_EXPR
),
5607 gnu_result_type
, gnu_lhs
, gnu_rhs
);
5610 case N_Op_Or
: case N_Op_And
: case N_Op_Xor
:
5611 /* These can either be operations on booleans or on modular types.
5612 Fall through for boolean types since that's the way GNU_CODES is
5614 if (IN (Ekind (Underlying_Type (Etype (gnat_node
))),
5615 Modular_Integer_Kind
))
5618 = (kind
== N_Op_Or
? BIT_IOR_EXPR
5619 : kind
== N_Op_And
? BIT_AND_EXPR
5622 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
5623 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
5624 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5625 gnu_result
= build_binary_op (code
, gnu_result_type
,
5630 /* ... fall through ... */
5632 case N_Op_Eq
: case N_Op_Ne
: case N_Op_Lt
:
5633 case N_Op_Le
: case N_Op_Gt
: case N_Op_Ge
:
5634 case N_Op_Add
: case N_Op_Subtract
: case N_Op_Multiply
:
5635 case N_Op_Mod
: case N_Op_Rem
:
5636 case N_Op_Rotate_Left
:
5637 case N_Op_Rotate_Right
:
5638 case N_Op_Shift_Left
:
5639 case N_Op_Shift_Right
:
5640 case N_Op_Shift_Right_Arithmetic
:
5641 case N_And_Then
: case N_Or_Else
:
5643 enum tree_code code
= gnu_codes
[kind
];
5644 bool ignore_lhs_overflow
= false;
5645 location_t saved_location
= input_location
;
5648 gnu_lhs
= gnat_to_gnu (Left_Opnd (gnat_node
));
5649 gnu_rhs
= gnat_to_gnu (Right_Opnd (gnat_node
));
5650 gnu_type
= gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5652 /* Pending generic support for efficient vector logical operations in
5653 GCC, convert vectors to their representative array type view and
5655 gnu_lhs
= maybe_vector_array (gnu_lhs
);
5656 gnu_rhs
= maybe_vector_array (gnu_rhs
);
5658 /* If this is a comparison operator, convert any references to
5659 an unconstrained array value into a reference to the
5661 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
5663 gnu_lhs
= maybe_unconstrained_array (gnu_lhs
);
5664 gnu_rhs
= maybe_unconstrained_array (gnu_rhs
);
5667 /* If the result type is a private type, its full view may be a
5668 numeric subtype. The representation we need is that of its base
5669 type, given that it is the result of an arithmetic operation. */
5670 else if (Is_Private_Type (Etype (gnat_node
)))
5671 gnu_type
= gnu_result_type
5672 = get_unpadded_type (Base_Type (Full_View (Etype (gnat_node
))));
5674 /* If this is a shift whose count is not guaranteed to be correct,
5675 we need to adjust the shift count. */
5676 if (IN (kind
, N_Op_Shift
) && !Shift_Count_OK (gnat_node
))
5678 tree gnu_count_type
= get_base_type (TREE_TYPE (gnu_rhs
));
5680 = convert (gnu_count_type
, TYPE_SIZE (gnu_type
));
5682 if (kind
== N_Op_Rotate_Left
|| kind
== N_Op_Rotate_Right
)
5683 gnu_rhs
= build_binary_op (TRUNC_MOD_EXPR
, gnu_count_type
,
5684 gnu_rhs
, gnu_max_shift
);
5685 else if (kind
== N_Op_Shift_Right_Arithmetic
)
5688 (MIN_EXPR
, gnu_count_type
,
5689 build_binary_op (MINUS_EXPR
,
5692 convert (gnu_count_type
,
5697 /* For right shifts, the type says what kind of shift to do,
5698 so we may need to choose a different type. In this case,
5699 we have to ignore integer overflow lest it propagates all
5700 the way down and causes a CE to be explicitly raised. */
5701 if (kind
== N_Op_Shift_Right
&& !TYPE_UNSIGNED (gnu_type
))
5703 gnu_type
= gnat_unsigned_type (gnu_type
);
5704 ignore_lhs_overflow
= true;
5706 else if (kind
== N_Op_Shift_Right_Arithmetic
5707 && TYPE_UNSIGNED (gnu_type
))
5709 gnu_type
= gnat_signed_type (gnu_type
);
5710 ignore_lhs_overflow
= true;
5713 if (gnu_type
!= gnu_result_type
)
5715 tree gnu_old_lhs
= gnu_lhs
;
5716 gnu_lhs
= convert (gnu_type
, gnu_lhs
);
5717 if (TREE_CODE (gnu_lhs
) == INTEGER_CST
&& ignore_lhs_overflow
)
5718 TREE_OVERFLOW (gnu_lhs
) = TREE_OVERFLOW (gnu_old_lhs
);
5719 gnu_rhs
= convert (gnu_type
, gnu_rhs
);
5722 /* Instead of expanding overflow checks for addition, subtraction
5723 and multiplication itself, the front end will leave this to
5724 the back end when Backend_Overflow_Checks_On_Target is set.
5725 As the GCC back end itself does not know yet how to properly
5726 do overflow checking, do it here. The goal is to push
5727 the expansions further into the back end over time. */
5728 if (Do_Overflow_Check (gnat_node
) && Backend_Overflow_Checks_On_Target
5729 && (kind
== N_Op_Add
5730 || kind
== N_Op_Subtract
5731 || kind
== N_Op_Multiply
)
5732 && !TYPE_UNSIGNED (gnu_type
)
5733 && !FLOAT_TYPE_P (gnu_type
))
5734 gnu_result
= build_binary_op_trapv (code
, gnu_type
,
5735 gnu_lhs
, gnu_rhs
, gnat_node
);
5738 /* Some operations, e.g. comparisons of arrays, generate complex
5739 trees that need to be annotated while they are being built. */
5740 input_location
= saved_location
;
5741 gnu_result
= build_binary_op (code
, gnu_type
, gnu_lhs
, gnu_rhs
);
5744 /* If this is a logical shift with the shift count not verified,
5745 we must return zero if it is too large. We cannot compensate
5746 above in this case. */
5747 if ((kind
== N_Op_Shift_Left
|| kind
== N_Op_Shift_Right
)
5748 && !Shift_Count_OK (gnat_node
))
5752 build_binary_op (GE_EXPR
, boolean_type_node
,
5754 convert (TREE_TYPE (gnu_rhs
),
5755 TYPE_SIZE (gnu_type
))),
5756 convert (gnu_type
, integer_zero_node
),
5761 case N_Conditional_Expression
:
5763 tree gnu_cond
= gnat_to_gnu (First (Expressions (gnat_node
)));
5764 tree gnu_true
= gnat_to_gnu (Next (First (Expressions (gnat_node
))));
5766 = gnat_to_gnu (Next (Next (First (Expressions (gnat_node
)))));
5768 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5770 = build_cond_expr (gnu_result_type
, gnu_cond
, gnu_true
, gnu_false
);
5775 gnu_result
= gnat_to_gnu (Right_Opnd (gnat_node
));
5776 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5780 /* This case can apply to a boolean or a modular type.
5781 Fall through for a boolean operand since GNU_CODES is set
5782 up to handle this. */
5783 if (Is_Modular_Integer_Type (Etype (gnat_node
))
5784 || (Ekind (Etype (gnat_node
)) == E_Private_Type
5785 && Is_Modular_Integer_Type (Full_View (Etype (gnat_node
)))))
5787 gnu_expr
= gnat_to_gnu (Right_Opnd (gnat_node
));
5788 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5789 gnu_result
= build_unary_op (BIT_NOT_EXPR
, gnu_result_type
,
5794 /* ... fall through ... */
5796 case N_Op_Minus
: case N_Op_Abs
:
5797 gnu_expr
= gnat_to_gnu (Right_Opnd (gnat_node
));
5799 if (Ekind (Etype (gnat_node
)) != E_Private_Type
)
5800 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5802 gnu_result_type
= get_unpadded_type (Base_Type
5803 (Full_View (Etype (gnat_node
))));
5805 if (Do_Overflow_Check (gnat_node
)
5806 && !TYPE_UNSIGNED (gnu_result_type
)
5807 && !FLOAT_TYPE_P (gnu_result_type
))
5809 = build_unary_op_trapv (gnu_codes
[kind
],
5810 gnu_result_type
, gnu_expr
, gnat_node
);
5812 gnu_result
= build_unary_op (gnu_codes
[kind
],
5813 gnu_result_type
, gnu_expr
);
5820 bool ignore_init_type
= false;
5822 gnat_temp
= Expression (gnat_node
);
5824 /* The Expression operand can either be an N_Identifier or
5825 Expanded_Name, which must represent a type, or a
5826 N_Qualified_Expression, which contains both the object type and an
5827 initial value for the object. */
5828 if (Nkind (gnat_temp
) == N_Identifier
5829 || Nkind (gnat_temp
) == N_Expanded_Name
)
5830 gnu_type
= gnat_to_gnu_type (Entity (gnat_temp
));
5831 else if (Nkind (gnat_temp
) == N_Qualified_Expression
)
5833 Entity_Id gnat_desig_type
5834 = Designated_Type (Underlying_Type (Etype (gnat_node
)));
5836 ignore_init_type
= Has_Constrained_Partial_View (gnat_desig_type
);
5837 gnu_init
= gnat_to_gnu (Expression (gnat_temp
));
5839 gnu_init
= maybe_unconstrained_array (gnu_init
);
5840 if (Do_Range_Check (Expression (gnat_temp
)))
5842 = emit_range_check (gnu_init
, gnat_desig_type
, gnat_temp
);
5844 if (Is_Elementary_Type (gnat_desig_type
)
5845 || Is_Constrained (gnat_desig_type
))
5847 gnu_type
= gnat_to_gnu_type (gnat_desig_type
);
5848 gnu_init
= convert (gnu_type
, gnu_init
);
5852 gnu_type
= gnat_to_gnu_type (Etype (Expression (gnat_temp
)));
5853 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5854 gnu_type
= TREE_TYPE (gnu_init
);
5856 gnu_init
= convert (gnu_type
, gnu_init
);
5862 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
5863 return build_allocator (gnu_type
, gnu_init
, gnu_result_type
,
5864 Procedure_To_Call (gnat_node
),
5865 Storage_Pool (gnat_node
), gnat_node
,
5870 /**************************/
5871 /* Chapter 5: Statements */
5872 /**************************/
5875 gnu_result
= build1 (LABEL_EXPR
, void_type_node
,
5876 gnat_to_gnu (Identifier (gnat_node
)));
5879 case N_Null_Statement
:
5880 /* When not optimizing, turn null statements from source into gotos to
5881 the next statement that the middle-end knows how to preserve. */
5882 if (!optimize
&& Comes_From_Source (gnat_node
))
5884 tree stmt
, label
= create_label_decl (NULL_TREE
);
5885 start_stmt_group ();
5886 stmt
= build1 (GOTO_EXPR
, void_type_node
, label
);
5887 set_expr_location_from_node (stmt
, gnat_node
);
5889 stmt
= build1 (LABEL_EXPR
, void_type_node
, label
);
5890 set_expr_location_from_node (stmt
, gnat_node
);
5892 gnu_result
= end_stmt_group ();
5895 gnu_result
= alloc_stmt_list ();
5898 case N_Assignment_Statement
:
5899 /* Get the LHS and RHS of the statement and convert any reference to an
5900 unconstrained array into a reference to the underlying array. */
5901 gnu_lhs
= maybe_unconstrained_array (gnat_to_gnu (Name (gnat_node
)));
5903 /* If the type has a size that overflows, convert this into raise of
5904 Storage_Error: execution shouldn't have gotten here anyway. */
5905 if (TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs
))) == INTEGER_CST
5906 && TREE_OVERFLOW (TYPE_SIZE_UNIT (TREE_TYPE (gnu_lhs
))))
5907 gnu_result
= build_call_raise (SE_Object_Too_Large
, gnat_node
,
5908 N_Raise_Storage_Error
);
5909 else if (Nkind (Expression (gnat_node
)) == N_Function_Call
)
5911 = call_to_gnu (Expression (gnat_node
), &gnu_result_type
, gnu_lhs
,
5912 atomic_sync_required_p (Name (gnat_node
)));
5916 = maybe_unconstrained_array (gnat_to_gnu (Expression (gnat_node
)));
5918 /* If range check is needed, emit code to generate it. */
5919 if (Do_Range_Check (Expression (gnat_node
)))
5920 gnu_rhs
= emit_range_check (gnu_rhs
, Etype (Name (gnat_node
)),
5923 if (atomic_sync_required_p (Name (gnat_node
)))
5924 gnu_result
= build_atomic_store (gnu_lhs
, gnu_rhs
);
5927 = build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_lhs
, gnu_rhs
);
5929 /* If the type being assigned is an array type and the two sides are
5930 not completely disjoint, play safe and use memmove. But don't do
5931 it for a bit-packed array as it might not be byte-aligned. */
5932 if (TREE_CODE (gnu_result
) == MODIFY_EXPR
5933 && Is_Array_Type (Etype (Name (gnat_node
)))
5934 && !Is_Bit_Packed_Array (Etype (Name (gnat_node
)))
5935 && !(Forwards_OK (gnat_node
) && Backwards_OK (gnat_node
)))
5937 tree to
, from
, size
, to_ptr
, from_ptr
, t
;
5939 to
= TREE_OPERAND (gnu_result
, 0);
5940 from
= TREE_OPERAND (gnu_result
, 1);
5942 size
= TYPE_SIZE_UNIT (TREE_TYPE (from
));
5943 size
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, from
);
5945 to_ptr
= build_fold_addr_expr (to
);
5946 from_ptr
= build_fold_addr_expr (from
);
5948 t
= builtin_decl_implicit (BUILT_IN_MEMMOVE
);
5949 gnu_result
= build_call_expr (t
, 3, to_ptr
, from_ptr
, size
);
5954 case N_If_Statement
:
5956 tree
*gnu_else_ptr
; /* Point to put next "else if" or "else". */
5958 /* Make the outer COND_EXPR. Avoid non-determinism. */
5959 gnu_result
= build3 (COND_EXPR
, void_type_node
,
5960 gnat_to_gnu (Condition (gnat_node
)),
5961 NULL_TREE
, NULL_TREE
);
5962 COND_EXPR_THEN (gnu_result
)
5963 = build_stmt_group (Then_Statements (gnat_node
), false);
5964 TREE_SIDE_EFFECTS (gnu_result
) = 1;
5965 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_result
);
5967 /* Now make a COND_EXPR for each of the "else if" parts. Put each
5968 into the previous "else" part and point to where to put any
5969 outer "else". Also avoid non-determinism. */
5970 if (Present (Elsif_Parts (gnat_node
)))
5971 for (gnat_temp
= First (Elsif_Parts (gnat_node
));
5972 Present (gnat_temp
); gnat_temp
= Next (gnat_temp
))
5974 gnu_expr
= build3 (COND_EXPR
, void_type_node
,
5975 gnat_to_gnu (Condition (gnat_temp
)),
5976 NULL_TREE
, NULL_TREE
);
5977 COND_EXPR_THEN (gnu_expr
)
5978 = build_stmt_group (Then_Statements (gnat_temp
), false);
5979 TREE_SIDE_EFFECTS (gnu_expr
) = 1;
5980 set_expr_location_from_node (gnu_expr
, gnat_temp
);
5981 *gnu_else_ptr
= gnu_expr
;
5982 gnu_else_ptr
= &COND_EXPR_ELSE (gnu_expr
);
5985 *gnu_else_ptr
= build_stmt_group (Else_Statements (gnat_node
), false);
5989 case N_Case_Statement
:
5990 gnu_result
= Case_Statement_to_gnu (gnat_node
);
5993 case N_Loop_Statement
:
5994 gnu_result
= Loop_Statement_to_gnu (gnat_node
);
5997 case N_Block_Statement
:
5998 start_stmt_group ();
6000 process_decls (Declarations (gnat_node
), Empty
, Empty
, true, true);
6001 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
6003 gnu_result
= end_stmt_group ();
6005 if (Present (Identifier (gnat_node
)))
6006 mark_out_of_scope (Entity (Identifier (gnat_node
)));
6009 case N_Exit_Statement
:
6011 = build2 (EXIT_STMT
, void_type_node
,
6012 (Present (Condition (gnat_node
))
6013 ? gnat_to_gnu (Condition (gnat_node
)) : NULL_TREE
),
6014 (Present (Name (gnat_node
))
6015 ? get_gnu_tree (Entity (Name (gnat_node
)))
6016 : VEC_last (loop_info
, gnu_loop_stack
)->label
));
6019 case N_Return_Statement
:
6021 tree gnu_ret_obj
, gnu_ret_val
;
6023 /* If the subprogram is a function, we must return the expression. */
6024 if (Present (Expression (gnat_node
)))
6026 tree gnu_subprog_type
= TREE_TYPE (current_function_decl
);
6028 /* If this function has copy-in/copy-out parameters, get the real
6029 object for the return. See Subprogram_to_gnu. */
6030 if (TYPE_CI_CO_LIST (gnu_subprog_type
))
6031 gnu_ret_obj
= VEC_last (tree
, gnu_return_var_stack
);
6033 gnu_ret_obj
= DECL_RESULT (current_function_decl
);
6035 /* Get the GCC tree for the expression to be returned. */
6036 gnu_ret_val
= gnat_to_gnu (Expression (gnat_node
));
6038 /* Do not remove the padding from GNU_RET_VAL if the inner type is
6039 self-referential since we want to allocate the fixed size. */
6040 if (TREE_CODE (gnu_ret_val
) == COMPONENT_REF
6041 && TYPE_IS_PADDING_P
6042 (TREE_TYPE (TREE_OPERAND (gnu_ret_val
, 0)))
6043 && CONTAINS_PLACEHOLDER_P
6044 (TYPE_SIZE (TREE_TYPE (gnu_ret_val
))))
6045 gnu_ret_val
= TREE_OPERAND (gnu_ret_val
, 0);
6047 /* If the function returns by direct reference, return a pointer
6048 to the return value. */
6049 if (TYPE_RETURN_BY_DIRECT_REF_P (gnu_subprog_type
)
6050 || By_Ref (gnat_node
))
6051 gnu_ret_val
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_ret_val
);
6053 /* Otherwise, if it returns an unconstrained array, we have to
6054 allocate a new version of the result and return it. */
6055 else if (TYPE_RETURN_UNCONSTRAINED_P (gnu_subprog_type
))
6057 gnu_ret_val
= maybe_unconstrained_array (gnu_ret_val
);
6059 /* And find out whether this is a candidate for Named Return
6060 Value. If so, record it. */
6061 if (!TYPE_CI_CO_LIST (gnu_subprog_type
) && optimize
)
6063 tree ret_val
= gnu_ret_val
;
6065 /* Strip useless conversions around the return value. */
6066 if (gnat_useless_type_conversion (ret_val
))
6067 ret_val
= TREE_OPERAND (ret_val
, 0);
6069 /* Strip unpadding around the return value. */
6070 if (TREE_CODE (ret_val
) == COMPONENT_REF
6071 && TYPE_IS_PADDING_P
6072 (TREE_TYPE (TREE_OPERAND (ret_val
, 0))))
6073 ret_val
= TREE_OPERAND (ret_val
, 0);
6075 /* Now apply the test to the return value. */
6076 if (return_value_ok_for_nrv_p (NULL_TREE
, ret_val
))
6078 if (!f_named_ret_val
)
6079 f_named_ret_val
= BITMAP_GGC_ALLOC ();
6080 bitmap_set_bit (f_named_ret_val
, DECL_UID (ret_val
));
6082 f_gnat_ret
= gnat_node
;
6086 gnu_ret_val
= build_allocator (TREE_TYPE (gnu_ret_val
),
6088 TREE_TYPE (gnu_ret_obj
),
6089 Procedure_To_Call (gnat_node
),
6090 Storage_Pool (gnat_node
),
6094 /* Otherwise, if it returns by invisible reference, dereference
6095 the pointer it is passed using the type of the return value
6096 and build the copy operation manually. This ensures that we
6097 don't copy too much data, for example if the return type is
6098 unconstrained with a maximum size. */
6099 else if (TREE_ADDRESSABLE (gnu_subprog_type
))
6102 = build_unary_op (INDIRECT_REF
, TREE_TYPE (gnu_ret_val
),
6104 gnu_result
= build_binary_op (MODIFY_EXPR
, NULL_TREE
,
6105 gnu_ret_deref
, gnu_ret_val
);
6106 add_stmt_with_node (gnu_result
, gnat_node
);
6107 gnu_ret_val
= NULL_TREE
;
6112 gnu_ret_obj
= gnu_ret_val
= NULL_TREE
;
6114 /* If we have a return label defined, convert this into a branch to
6115 that label. The return proper will be handled elsewhere. */
6116 if (VEC_last (tree
, gnu_return_label_stack
))
6119 add_stmt (build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_ret_obj
,
6122 gnu_result
= build1 (GOTO_EXPR
, void_type_node
,
6123 VEC_last (tree
, gnu_return_label_stack
));
6125 /* When not optimizing, make sure the return is preserved. */
6126 if (!optimize
&& Comes_From_Source (gnat_node
))
6127 DECL_ARTIFICIAL (VEC_last (tree
, gnu_return_label_stack
)) = 0;
6130 /* Otherwise, build a regular return. */
6132 gnu_result
= build_return_expr (gnu_ret_obj
, gnu_ret_val
);
6136 case N_Goto_Statement
:
6138 = build1 (GOTO_EXPR
, void_type_node
, gnat_to_gnu (Name (gnat_node
)));
6141 /***************************/
6142 /* Chapter 6: Subprograms */
6143 /***************************/
6145 case N_Subprogram_Declaration
:
6146 /* Unless there is a freeze node, declare the subprogram. We consider
6147 this a "definition" even though we're not generating code for
6148 the subprogram because we will be making the corresponding GCC
6151 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node
)))))
6152 gnat_to_gnu_entity (Defining_Entity (Specification (gnat_node
)),
6154 gnu_result
= alloc_stmt_list ();
6157 case N_Abstract_Subprogram_Declaration
:
6158 /* This subprogram doesn't exist for code generation purposes, but we
6159 have to elaborate the types of any parameters and result, unless
6160 they are imported types (nothing to generate in this case).
6162 The parameter list may contain types with freeze nodes, e.g. not null
6163 subtypes, so the subprogram itself may carry a freeze node, in which
6164 case its elaboration must be deferred. */
6166 /* Process the parameter types first. */
6167 if (No (Freeze_Node (Defining_Entity (Specification (gnat_node
)))))
6169 = First_Formal_With_Extras
6170 (Defining_Entity (Specification (gnat_node
)));
6171 Present (gnat_temp
);
6172 gnat_temp
= Next_Formal_With_Extras (gnat_temp
))
6173 if (Is_Itype (Etype (gnat_temp
))
6174 && !From_With_Type (Etype (gnat_temp
)))
6175 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
6177 /* Then the result type, set to Standard_Void_Type for procedures. */
6179 Entity_Id gnat_temp_type
6180 = Etype (Defining_Entity (Specification (gnat_node
)));
6182 if (Is_Itype (gnat_temp_type
) && !From_With_Type (gnat_temp_type
))
6183 gnat_to_gnu_entity (Etype (gnat_temp_type
), NULL_TREE
, 0);
6186 gnu_result
= alloc_stmt_list ();
6189 case N_Defining_Program_Unit_Name
:
6190 /* For a child unit identifier go up a level to get the specification.
6191 We get this when we try to find the spec of a child unit package
6192 that is the compilation unit being compiled. */
6193 gnu_result
= gnat_to_gnu (Parent (gnat_node
));
6196 case N_Subprogram_Body
:
6197 Subprogram_Body_to_gnu (gnat_node
);
6198 gnu_result
= alloc_stmt_list ();
6201 case N_Function_Call
:
6202 case N_Procedure_Call_Statement
:
6203 gnu_result
= call_to_gnu (gnat_node
, &gnu_result_type
, NULL_TREE
, false);
6206 /************************/
6207 /* Chapter 7: Packages */
6208 /************************/
6210 case N_Package_Declaration
:
6211 gnu_result
= gnat_to_gnu (Specification (gnat_node
));
6214 case N_Package_Specification
:
6216 start_stmt_group ();
6217 process_decls (Visible_Declarations (gnat_node
),
6218 Private_Declarations (gnat_node
), Empty
, true, true);
6219 gnu_result
= end_stmt_group ();
6222 case N_Package_Body
:
6224 /* If this is the body of a generic package - do nothing. */
6225 if (Ekind (Corresponding_Spec (gnat_node
)) == E_Generic_Package
)
6227 gnu_result
= alloc_stmt_list ();
6231 start_stmt_group ();
6232 process_decls (Declarations (gnat_node
), Empty
, Empty
, true, true);
6234 if (Present (Handled_Statement_Sequence (gnat_node
)))
6235 add_stmt (gnat_to_gnu (Handled_Statement_Sequence (gnat_node
)));
6237 gnu_result
= end_stmt_group ();
6240 /********************************/
6241 /* Chapter 8: Visibility Rules */
6242 /********************************/
6244 case N_Use_Package_Clause
:
6245 case N_Use_Type_Clause
:
6246 /* Nothing to do here - but these may appear in list of declarations. */
6247 gnu_result
= alloc_stmt_list ();
6250 /*********************/
6251 /* Chapter 9: Tasks */
6252 /*********************/
6254 case N_Protected_Type_Declaration
:
6255 gnu_result
= alloc_stmt_list ();
6258 case N_Single_Task_Declaration
:
6259 gnat_to_gnu_entity (Defining_Entity (gnat_node
), NULL_TREE
, 1);
6260 gnu_result
= alloc_stmt_list ();
6263 /*********************************************************/
6264 /* Chapter 10: Program Structure and Compilation Issues */
6265 /*********************************************************/
6267 case N_Compilation_Unit
:
6268 /* This is not called for the main unit on which gigi is invoked. */
6269 Compilation_Unit_to_gnu (gnat_node
);
6270 gnu_result
= alloc_stmt_list ();
6273 case N_Subprogram_Body_Stub
:
6274 case N_Package_Body_Stub
:
6275 case N_Protected_Body_Stub
:
6276 case N_Task_Body_Stub
:
6277 /* Simply process whatever unit is being inserted. */
6278 gnu_result
= gnat_to_gnu (Unit (Library_Unit (gnat_node
)));
6282 gnu_result
= gnat_to_gnu (Proper_Body (gnat_node
));
6285 /***************************/
6286 /* Chapter 11: Exceptions */
6287 /***************************/
6289 case N_Handled_Sequence_Of_Statements
:
6290 /* If there is an At_End procedure attached to this node, and the EH
6291 mechanism is SJLJ, we must have at least a corresponding At_End
6292 handler, unless the No_Exception_Handlers restriction is set. */
6293 gcc_assert (type_annotate_only
6294 || Exception_Mechanism
!= Setjmp_Longjmp
6295 || No (At_End_Proc (gnat_node
))
6296 || Present (Exception_Handlers (gnat_node
))
6297 || No_Exception_Handlers_Set ());
6299 gnu_result
= Handled_Sequence_Of_Statements_to_gnu (gnat_node
);
6302 case N_Exception_Handler
:
6303 if (Exception_Mechanism
== Setjmp_Longjmp
)
6304 gnu_result
= Exception_Handler_to_gnu_sjlj (gnat_node
);
6305 else if (Exception_Mechanism
== Back_End_Exceptions
)
6306 gnu_result
= Exception_Handler_to_gnu_zcx (gnat_node
);
6311 case N_Raise_Statement
:
6312 /* Only for reraise in back-end exceptions mode. */
6313 gcc_assert (No (Name (gnat_node
))
6314 && Exception_Mechanism
== Back_End_Exceptions
);
6316 start_stmt_group ();
6319 /* Clear the current exception pointer so that the occurrence won't be
6321 gnu_expr
= create_var_decl (get_identifier ("SAVED_EXPTR"), NULL_TREE
,
6322 ptr_type_node
, gnu_incoming_exc_ptr
,
6323 false, false, false, false, NULL
, gnat_node
);
6325 add_stmt (build_binary_op (MODIFY_EXPR
, NULL_TREE
, gnu_incoming_exc_ptr
,
6326 convert (ptr_type_node
, integer_zero_node
)));
6327 add_stmt (build_call_n_expr (reraise_zcx_decl
, 1, gnu_expr
));
6329 gnu_result
= end_stmt_group ();
6332 case N_Push_Constraint_Error_Label
:
6333 push_exception_label_stack (&gnu_constraint_error_label_stack
,
6334 Exception_Label (gnat_node
));
6337 case N_Push_Storage_Error_Label
:
6338 push_exception_label_stack (&gnu_storage_error_label_stack
,
6339 Exception_Label (gnat_node
));
6342 case N_Push_Program_Error_Label
:
6343 push_exception_label_stack (&gnu_program_error_label_stack
,
6344 Exception_Label (gnat_node
));
6347 case N_Pop_Constraint_Error_Label
:
6348 VEC_pop (tree
, gnu_constraint_error_label_stack
);
6351 case N_Pop_Storage_Error_Label
:
6352 VEC_pop (tree
, gnu_storage_error_label_stack
);
6355 case N_Pop_Program_Error_Label
:
6356 VEC_pop (tree
, gnu_program_error_label_stack
);
6359 /******************************/
6360 /* Chapter 12: Generic Units */
6361 /******************************/
6363 case N_Generic_Function_Renaming_Declaration
:
6364 case N_Generic_Package_Renaming_Declaration
:
6365 case N_Generic_Procedure_Renaming_Declaration
:
6366 case N_Generic_Package_Declaration
:
6367 case N_Generic_Subprogram_Declaration
:
6368 case N_Package_Instantiation
:
6369 case N_Procedure_Instantiation
:
6370 case N_Function_Instantiation
:
6371 /* These nodes can appear on a declaration list but there is nothing to
6372 to be done with them. */
6373 gnu_result
= alloc_stmt_list ();
6376 /**************************************************/
6377 /* Chapter 13: Representation Clauses and */
6378 /* Implementation-Dependent Features */
6379 /**************************************************/
6381 case N_Attribute_Definition_Clause
:
6382 gnu_result
= alloc_stmt_list ();
6384 /* The only one we need to deal with is 'Address since, for the others,
6385 the front-end puts the information elsewhere. */
6386 if (Get_Attribute_Id (Chars (gnat_node
)) != Attr_Address
)
6389 /* And we only deal with 'Address if the object has a Freeze node. */
6390 gnat_temp
= Entity (Name (gnat_node
));
6391 if (No (Freeze_Node (gnat_temp
)))
6394 /* Get the value to use as the address and save it as the equivalent
6395 for the object. When it is frozen, gnat_to_gnu_entity will do the
6397 save_gnu_tree (gnat_temp
, gnat_to_gnu (Expression (gnat_node
)), true);
6400 case N_Enumeration_Representation_Clause
:
6401 case N_Record_Representation_Clause
:
6403 /* We do nothing with these. SEM puts the information elsewhere. */
6404 gnu_result
= alloc_stmt_list ();
6407 case N_Code_Statement
:
6408 if (!type_annotate_only
)
6410 tree gnu_template
= gnat_to_gnu (Asm_Template (gnat_node
));
6411 tree gnu_inputs
= NULL_TREE
, gnu_outputs
= NULL_TREE
;
6412 tree gnu_clobbers
= NULL_TREE
, tail
;
6413 bool allows_mem
, allows_reg
, fake
;
6414 int ninputs
, noutputs
, i
;
6415 const char **oconstraints
;
6416 const char *constraint
;
6419 /* First retrieve the 3 operand lists built by the front-end. */
6420 Setup_Asm_Outputs (gnat_node
);
6421 while (Present (gnat_temp
= Asm_Output_Variable ()))
6423 tree gnu_value
= gnat_to_gnu (gnat_temp
);
6424 tree gnu_constr
= build_tree_list (NULL_TREE
, gnat_to_gnu
6425 (Asm_Output_Constraint ()));
6427 gnu_outputs
= tree_cons (gnu_constr
, gnu_value
, gnu_outputs
);
6431 Setup_Asm_Inputs (gnat_node
);
6432 while (Present (gnat_temp
= Asm_Input_Value ()))
6434 tree gnu_value
= gnat_to_gnu (gnat_temp
);
6435 tree gnu_constr
= build_tree_list (NULL_TREE
, gnat_to_gnu
6436 (Asm_Input_Constraint ()));
6438 gnu_inputs
= tree_cons (gnu_constr
, gnu_value
, gnu_inputs
);
6442 Clobber_Setup (gnat_node
);
6443 while ((clobber
= Clobber_Get_Next ()))
6445 = tree_cons (NULL_TREE
,
6446 build_string (strlen (clobber
) + 1, clobber
),
6449 /* Then perform some standard checking and processing on the
6450 operands. In particular, mark them addressable if needed. */
6451 gnu_outputs
= nreverse (gnu_outputs
);
6452 noutputs
= list_length (gnu_outputs
);
6453 gnu_inputs
= nreverse (gnu_inputs
);
6454 ninputs
= list_length (gnu_inputs
);
6455 oconstraints
= XALLOCAVEC (const char *, noutputs
);
6457 for (i
= 0, tail
= gnu_outputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
6459 tree output
= TREE_VALUE (tail
);
6461 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
6462 oconstraints
[i
] = constraint
;
6464 if (parse_output_constraint (&constraint
, i
, ninputs
, noutputs
,
6465 &allows_mem
, &allows_reg
, &fake
))
6467 /* If the operand is going to end up in memory,
6468 mark it addressable. Note that we don't test
6469 allows_mem like in the input case below; this
6470 is modelled on the C front-end. */
6473 output
= remove_conversions (output
, false);
6474 if (TREE_CODE (output
) == CONST_DECL
6475 && DECL_CONST_CORRESPONDING_VAR (output
))
6476 output
= DECL_CONST_CORRESPONDING_VAR (output
);
6477 if (!gnat_mark_addressable (output
))
6478 output
= error_mark_node
;
6482 output
= error_mark_node
;
6484 TREE_VALUE (tail
) = output
;
6487 for (i
= 0, tail
= gnu_inputs
; tail
; ++i
, tail
= TREE_CHAIN (tail
))
6489 tree input
= TREE_VALUE (tail
);
6491 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail
)));
6493 if (parse_input_constraint (&constraint
, i
, ninputs
, noutputs
,
6495 &allows_mem
, &allows_reg
))
6497 /* If the operand is going to end up in memory,
6498 mark it addressable. */
6499 if (!allows_reg
&& allows_mem
)
6501 input
= remove_conversions (input
, false);
6502 if (TREE_CODE (input
) == CONST_DECL
6503 && DECL_CONST_CORRESPONDING_VAR (input
))
6504 input
= DECL_CONST_CORRESPONDING_VAR (input
);
6505 if (!gnat_mark_addressable (input
))
6506 input
= error_mark_node
;
6510 input
= error_mark_node
;
6512 TREE_VALUE (tail
) = input
;
6515 gnu_result
= build5 (ASM_EXPR
, void_type_node
,
6516 gnu_template
, gnu_outputs
,
6517 gnu_inputs
, gnu_clobbers
, NULL_TREE
);
6518 ASM_VOLATILE_P (gnu_result
) = Is_Asm_Volatile (gnat_node
);
6521 gnu_result
= alloc_stmt_list ();
6529 case N_Expression_With_Actions
:
6530 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6531 /* This construct doesn't define a scope so we don't wrap the statement
6532 list in a BIND_EXPR; however, we wrap it in a SAVE_EXPR to protect it
6534 gnu_result
= build_stmt_group (Actions (gnat_node
), false);
6535 gnu_result
= build1 (SAVE_EXPR
, void_type_node
, gnu_result
);
6536 TREE_SIDE_EFFECTS (gnu_result
) = 1;
6537 gnu_expr
= gnat_to_gnu (Expression (gnat_node
));
6539 = build_compound_expr (TREE_TYPE (gnu_expr
), gnu_result
, gnu_expr
);
6542 case N_Freeze_Entity
:
6543 start_stmt_group ();
6544 process_freeze_entity (gnat_node
);
6545 process_decls (Actions (gnat_node
), Empty
, Empty
, true, true);
6546 gnu_result
= end_stmt_group ();
6549 case N_Itype_Reference
:
6550 if (!present_gnu_tree (Itype (gnat_node
)))
6551 process_type (Itype (gnat_node
));
6553 gnu_result
= alloc_stmt_list ();
6556 case N_Free_Statement
:
6557 if (!type_annotate_only
)
6559 tree gnu_ptr
= gnat_to_gnu (Expression (gnat_node
));
6560 tree gnu_ptr_type
= TREE_TYPE (gnu_ptr
);
6562 tree gnu_actual_obj_type
= 0;
6565 /* If this is a thin pointer, we must dereference it to create
6566 a fat pointer, then go back below to a thin pointer. The
6567 reason for this is that we need a fat pointer someplace in
6568 order to properly compute the size. */
6569 if (TYPE_IS_THIN_POINTER_P (TREE_TYPE (gnu_ptr
)))
6570 gnu_ptr
= build_unary_op (ADDR_EXPR
, NULL_TREE
,
6571 build_unary_op (INDIRECT_REF
, NULL_TREE
,
6574 /* If this is an unconstrained array, we know the object must
6575 have been allocated with the template in front of the object.
6576 So pass the template address, but get the total size. Do this
6577 by converting to a thin pointer. */
6578 if (TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_ptr
)))
6580 = convert (build_pointer_type
6581 (TYPE_OBJECT_RECORD_TYPE
6582 (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (gnu_ptr
)))),
6585 gnu_obj_type
= TREE_TYPE (TREE_TYPE (gnu_ptr
));
6587 if (Present (Actual_Designated_Subtype (gnat_node
)))
6590 = gnat_to_gnu_type (Actual_Designated_Subtype (gnat_node
));
6592 if (TYPE_IS_FAT_OR_THIN_POINTER_P (gnu_ptr_type
))
6594 = build_unc_object_type_from_ptr (gnu_ptr_type
,
6595 gnu_actual_obj_type
,
6596 get_identifier ("DEALLOC"),
6600 gnu_actual_obj_type
= gnu_obj_type
;
6602 gnu_obj_size
= TYPE_SIZE_UNIT (gnu_actual_obj_type
);
6604 if (TREE_CODE (gnu_obj_type
) == RECORD_TYPE
6605 && TYPE_CONTAINS_TEMPLATE_P (gnu_obj_type
))
6607 tree gnu_char_ptr_type
6608 = build_pointer_type (unsigned_char_type_node
);
6609 tree gnu_pos
= byte_position (TYPE_FIELDS (gnu_obj_type
));
6610 gnu_ptr
= convert (gnu_char_ptr_type
, gnu_ptr
);
6611 gnu_ptr
= build_binary_op (POINTER_PLUS_EXPR
, gnu_char_ptr_type
,
6616 = build_call_alloc_dealloc (gnu_ptr
, gnu_obj_size
, gnu_obj_type
,
6617 Procedure_To_Call (gnat_node
),
6618 Storage_Pool (gnat_node
),
6623 case N_Raise_Constraint_Error
:
6624 case N_Raise_Program_Error
:
6625 case N_Raise_Storage_Error
:
6627 const int reason
= UI_To_Int (Reason (gnat_node
));
6628 const Node_Id gnat_cond
= Condition (gnat_node
);
6629 const bool with_extra_info
= Exception_Extra_Info
6630 && !No_Exception_Handlers_Set ()
6631 && !get_exception_label (kind
);
6632 tree gnu_cond
= NULL_TREE
;
6634 if (type_annotate_only
)
6636 gnu_result
= alloc_stmt_list ();
6640 gnu_result_type
= get_unpadded_type (Etype (gnat_node
));
6644 case CE_Access_Check_Failed
:
6645 if (with_extra_info
)
6646 gnu_result
= build_call_raise_column (reason
, gnat_node
);
6649 case CE_Index_Check_Failed
:
6650 case CE_Range_Check_Failed
:
6651 case CE_Invalid_Data
:
6652 if (Present (gnat_cond
)
6653 && Nkind (gnat_cond
) == N_Op_Not
6654 && Nkind (Right_Opnd (gnat_cond
)) == N_In
6655 && Nkind (Right_Opnd (Right_Opnd (gnat_cond
))) == N_Range
)
6657 Node_Id gnat_index
= Left_Opnd (Right_Opnd (gnat_cond
));
6658 Node_Id gnat_type
= Etype (gnat_index
);
6659 Node_Id gnat_range
= Right_Opnd (Right_Opnd (gnat_cond
));
6660 tree gnu_index
= gnat_to_gnu (gnat_index
);
6661 tree gnu_low_bound
= gnat_to_gnu (Low_Bound (gnat_range
));
6662 tree gnu_high_bound
= gnat_to_gnu (High_Bound (gnat_range
));
6663 struct range_check_info_d
*rci
;
6666 && Known_Esize (gnat_type
)
6667 && UI_To_Int (Esize (gnat_type
)) <= 32)
6669 = build_call_raise_range (reason
, gnat_node
, gnu_index
,
6670 gnu_low_bound
, gnu_high_bound
);
6672 /* If loop unswitching is enabled, we try to compute invariant
6673 conditions for checks applied to iteration variables, i.e.
6674 conditions that are both independent of the variable and
6675 necessary in order for the check to fail in the course of
6676 some iteration, and prepend them to the original condition
6677 of the checks. This will make it possible later for the
6678 loop unswitching pass to replace the loop with two loops,
6679 one of which has the checks eliminated and the other has
6680 the original checks reinstated, and a run time selection.
6681 The former loop will be suitable for vectorization. */
6682 if (flag_unswitch_loops
6683 && (gnu_low_bound
= gnat_invariant_expr (gnu_low_bound
))
6684 && (gnu_high_bound
= gnat_invariant_expr (gnu_high_bound
))
6685 && (rci
= push_range_check_info (gnu_index
)))
6687 rci
->low_bound
= gnu_low_bound
;
6688 rci
->high_bound
= gnu_high_bound
;
6689 rci
->type
= gnat_to_gnu_type (gnat_type
);
6690 rci
->invariant_cond
= build1 (SAVE_EXPR
, boolean_type_node
,
6692 gnu_cond
= build_binary_op (TRUTH_ANDIF_EXPR
,
6694 rci
->invariant_cond
,
6695 gnat_to_gnu (gnat_cond
));
6704 if (gnu_result
== error_mark_node
)
6705 gnu_result
= build_call_raise (reason
, gnat_node
, kind
);
6707 set_expr_location_from_node (gnu_result
, gnat_node
);
6709 /* If the type is VOID, this is a statement, so we need to generate
6710 the code for the call. Handle a condition, if there is one. */
6711 if (VOID_TYPE_P (gnu_result_type
))
6713 if (Present (gnat_cond
))
6716 gnu_cond
= gnat_to_gnu (gnat_cond
);
6718 = build3 (COND_EXPR
, void_type_node
, gnu_cond
, gnu_result
,
6719 alloc_stmt_list ());
6723 gnu_result
= build1 (NULL_EXPR
, gnu_result_type
, gnu_result
);
6727 case N_Validate_Unchecked_Conversion
:
6729 Entity_Id gnat_target_type
= Target_Type (gnat_node
);
6730 tree gnu_source_type
= gnat_to_gnu_type (Source_Type (gnat_node
));
6731 tree gnu_target_type
= gnat_to_gnu_type (gnat_target_type
);
6733 /* No need for any warning in this case. */
6734 if (!flag_strict_aliasing
)
6737 /* If the result is a pointer type, see if we are either converting
6738 from a non-pointer or from a pointer to a type with a different
6739 alias set and warn if so. If the result is defined in the same
6740 unit as this unchecked conversion, we can allow this because we
6741 can know to make the pointer type behave properly. */
6742 else if (POINTER_TYPE_P (gnu_target_type
)
6743 && !In_Same_Source_Unit (gnat_target_type
, gnat_node
)
6744 && !No_Strict_Aliasing (Underlying_Type (gnat_target_type
)))
6746 tree gnu_source_desig_type
= POINTER_TYPE_P (gnu_source_type
)
6747 ? TREE_TYPE (gnu_source_type
)
6749 tree gnu_target_desig_type
= TREE_TYPE (gnu_target_type
);
6751 if ((TYPE_DUMMY_P (gnu_target_desig_type
)
6752 || get_alias_set (gnu_target_desig_type
) != 0)
6753 && (!POINTER_TYPE_P (gnu_source_type
)
6754 || (TYPE_DUMMY_P (gnu_source_desig_type
)
6755 != TYPE_DUMMY_P (gnu_target_desig_type
))
6756 || (TYPE_DUMMY_P (gnu_source_desig_type
)
6757 && gnu_source_desig_type
!= gnu_target_desig_type
)
6758 || !alias_sets_conflict_p
6759 (get_alias_set (gnu_source_desig_type
),
6760 get_alias_set (gnu_target_desig_type
))))
6763 ("?possible aliasing problem for type&",
6764 gnat_node
, Target_Type (gnat_node
));
6766 ("\\?use -fno-strict-aliasing switch for references",
6769 ("\\?or use `pragma No_Strict_Aliasing (&);`",
6770 gnat_node
, Target_Type (gnat_node
));
6774 /* But if the result is a fat pointer type, we have no mechanism to
6775 do that, so we unconditionally warn in problematic cases. */
6776 else if (TYPE_IS_FAT_POINTER_P (gnu_target_type
))
6778 tree gnu_source_array_type
6779 = TYPE_IS_FAT_POINTER_P (gnu_source_type
)
6780 ? TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_source_type
)))
6782 tree gnu_target_array_type
6783 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (gnu_target_type
)));
6785 if ((TYPE_DUMMY_P (gnu_target_array_type
)
6786 || get_alias_set (gnu_target_array_type
) != 0)
6787 && (!TYPE_IS_FAT_POINTER_P (gnu_source_type
)
6788 || (TYPE_DUMMY_P (gnu_source_array_type
)
6789 != TYPE_DUMMY_P (gnu_target_array_type
))
6790 || (TYPE_DUMMY_P (gnu_source_array_type
)
6791 && gnu_source_array_type
!= gnu_target_array_type
)
6792 || !alias_sets_conflict_p
6793 (get_alias_set (gnu_source_array_type
),
6794 get_alias_set (gnu_target_array_type
))))
6797 ("?possible aliasing problem for type&",
6798 gnat_node
, Target_Type (gnat_node
));
6800 ("\\?use -fno-strict-aliasing switch for references",
6805 gnu_result
= alloc_stmt_list ();
6809 /* SCIL nodes require no processing for GCC. Other nodes should only
6810 be present when annotating types. */
6811 gcc_assert (IN (kind
, N_SCIL_Node
) || type_annotate_only
);
6812 gnu_result
= alloc_stmt_list ();
6815 /* If we pushed the processing of the elaboration routine, pop it back. */
6816 if (went_into_elab_proc
)
6817 current_function_decl
= NULL_TREE
;
6819 /* When not optimizing, turn boolean rvalues B into B != false tests
6820 so that the code just below can put the location information of the
6821 reference to B on the inequality operator for better debug info. */
6823 && TREE_CODE (gnu_result
) != INTEGER_CST
6824 && (kind
== N_Identifier
6825 || kind
== N_Expanded_Name
6826 || kind
== N_Explicit_Dereference
6827 || kind
== N_Function_Call
6828 || kind
== N_Indexed_Component
6829 || kind
== N_Selected_Component
)
6830 && TREE_CODE (get_base_type (gnu_result_type
)) == BOOLEAN_TYPE
6831 && !lvalue_required_p (gnat_node
, gnu_result_type
, false, false, false))
6832 gnu_result
= build_binary_op (NE_EXPR
, gnu_result_type
,
6833 convert (gnu_result_type
, gnu_result
),
6834 convert (gnu_result_type
,
6835 boolean_false_node
));
6837 /* Set the location information on the result. Note that we may have
6838 no result if we tried to build a CALL_EXPR node to a procedure with
6839 no side-effects and optimization is enabled. */
6840 if (gnu_result
&& EXPR_P (gnu_result
))
6841 set_gnu_expr_location_from_node (gnu_result
, gnat_node
);
6843 /* If we're supposed to return something of void_type, it means we have
6844 something we're elaborating for effect, so just return. */
6845 if (TREE_CODE (gnu_result_type
) == VOID_TYPE
)
6848 /* If the result is a constant that overflowed, raise Constraint_Error. */
6849 if (TREE_CODE (gnu_result
) == INTEGER_CST
&& TREE_OVERFLOW (gnu_result
))
6851 post_error ("?`Constraint_Error` will be raised at run time", gnat_node
);
6853 = build1 (NULL_EXPR
, gnu_result_type
,
6854 build_call_raise (CE_Overflow_Check_Failed
, gnat_node
,
6855 N_Raise_Constraint_Error
));
6858 /* If our result has side-effects and is of an unconstrained type,
6859 make a SAVE_EXPR so that we can be sure it will only be referenced
6860 once. Note we must do this before any conversions. */
6861 if (TREE_SIDE_EFFECTS (gnu_result
)
6862 && (TREE_CODE (gnu_result_type
) == UNCONSTRAINED_ARRAY_TYPE
6863 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type
))))
6864 gnu_result
= gnat_stabilize_reference (gnu_result
, false, NULL
);
6866 /* Now convert the result to the result type, unless we are in one of the
6869 1. If this is the LHS of an assignment or an actual parameter of a
6870 call, return the result almost unmodified since the RHS will have
6871 to be converted to our type in that case, unless the result type
6872 has a simpler size. Likewise if there is just a no-op unchecked
6873 conversion in-between. Similarly, don't convert integral types
6874 that are the operands of an unchecked conversion since we need
6875 to ignore those conversions (for 'Valid).
6877 2. If we have a label (which doesn't have any well-defined type), a
6878 field or an error, return the result almost unmodified. Similarly,
6879 if the two types are record types with the same name, don't convert.
6880 This will be the case when we are converting from a packable version
6881 of a type to its original type and we need those conversions to be
6882 NOPs in order for assignments into these types to work properly.
6884 3. If the type is void or if we have no result, return error_mark_node
6885 to show we have no result.
6887 4. Finally, if the type of the result is already correct. */
6889 if (Present (Parent (gnat_node
))
6890 && (lhs_or_actual_p (gnat_node
)
6891 || (Nkind (Parent (gnat_node
)) == N_Unchecked_Type_Conversion
6892 && unchecked_conversion_nop (Parent (gnat_node
)))
6893 || (Nkind (Parent (gnat_node
)) == N_Unchecked_Type_Conversion
6894 && !AGGREGATE_TYPE_P (gnu_result_type
)
6895 && !AGGREGATE_TYPE_P (TREE_TYPE (gnu_result
))))
6896 && !(TYPE_SIZE (gnu_result_type
)
6897 && TYPE_SIZE (TREE_TYPE (gnu_result
))
6898 && (AGGREGATE_TYPE_P (gnu_result_type
)
6899 == AGGREGATE_TYPE_P (TREE_TYPE (gnu_result
)))
6900 && ((TREE_CODE (TYPE_SIZE (gnu_result_type
)) == INTEGER_CST
6901 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (gnu_result
)))
6903 || (TREE_CODE (TYPE_SIZE (gnu_result_type
)) != INTEGER_CST
6904 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_result_type
))
6905 && (CONTAINS_PLACEHOLDER_P
6906 (TYPE_SIZE (TREE_TYPE (gnu_result
))))))
6907 && !(TREE_CODE (gnu_result_type
) == RECORD_TYPE
6908 && TYPE_JUSTIFIED_MODULAR_P (gnu_result_type
))))
6910 /* Remove padding only if the inner object is of self-referential
6911 size: in that case it must be an object of unconstrained type
6912 with a default discriminant and we want to avoid copying too
6914 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result
))
6915 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS
6916 (TREE_TYPE (gnu_result
))))))
6917 gnu_result
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result
))),
6921 else if (TREE_CODE (gnu_result
) == LABEL_DECL
6922 || TREE_CODE (gnu_result
) == FIELD_DECL
6923 || TREE_CODE (gnu_result
) == ERROR_MARK
6924 || (TYPE_NAME (gnu_result_type
)
6925 == TYPE_NAME (TREE_TYPE (gnu_result
))
6926 && TREE_CODE (gnu_result_type
) == RECORD_TYPE
6927 && TREE_CODE (TREE_TYPE (gnu_result
)) == RECORD_TYPE
))
6929 /* Remove any padding. */
6930 if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_result
)))
6931 gnu_result
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_result
))),
6935 else if (gnu_result
== error_mark_node
|| gnu_result_type
== void_type_node
)
6936 gnu_result
= error_mark_node
;
6938 else if (gnu_result_type
!= TREE_TYPE (gnu_result
))
6939 gnu_result
= convert (gnu_result_type
, gnu_result
);
6941 /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on the result. */
6942 while ((TREE_CODE (gnu_result
) == NOP_EXPR
6943 || TREE_CODE (gnu_result
) == NON_LVALUE_EXPR
)
6944 && TREE_TYPE (TREE_OPERAND (gnu_result
, 0)) == TREE_TYPE (gnu_result
))
6945 gnu_result
= TREE_OPERAND (gnu_result
, 0);
6950 /* Subroutine of above to push the exception label stack. GNU_STACK is
6951 a pointer to the stack to update and GNAT_LABEL, if present, is the
6952 label to push onto the stack. */
6955 push_exception_label_stack (VEC(tree
,gc
) **gnu_stack
, Entity_Id gnat_label
)
6957 tree gnu_label
= (Present (gnat_label
)
6958 ? gnat_to_gnu_entity (gnat_label
, NULL_TREE
, 0)
6961 VEC_safe_push (tree
, gc
, *gnu_stack
, gnu_label
);
6964 /* Record the current code position in GNAT_NODE. */
6967 record_code_position (Node_Id gnat_node
)
6969 tree stmt_stmt
= build1 (STMT_STMT
, void_type_node
, NULL_TREE
);
6971 add_stmt_with_node (stmt_stmt
, gnat_node
);
6972 save_gnu_tree (gnat_node
, stmt_stmt
, true);
6975 /* Insert the code for GNAT_NODE at the position saved for that node. */
6978 insert_code_for (Node_Id gnat_node
)
6980 STMT_STMT_STMT (get_gnu_tree (gnat_node
)) = gnat_to_gnu (gnat_node
);
6981 save_gnu_tree (gnat_node
, NULL_TREE
, true);
6984 /* Start a new statement group chained to the previous group. */
6987 start_stmt_group (void)
6989 struct stmt_group
*group
= stmt_group_free_list
;
6991 /* First see if we can get one from the free list. */
6993 stmt_group_free_list
= group
->previous
;
6995 group
= ggc_alloc_stmt_group ();
6997 group
->previous
= current_stmt_group
;
6998 group
->stmt_list
= group
->block
= group
->cleanups
= NULL_TREE
;
6999 current_stmt_group
= group
;
7002 /* Add GNU_STMT to the current statement group. If it is an expression with
7003 no effects, it is ignored. */
7006 add_stmt (tree gnu_stmt
)
7008 append_to_statement_list (gnu_stmt
, ¤t_stmt_group
->stmt_list
);
7011 /* Similar, but the statement is always added, regardless of side-effects. */
7014 add_stmt_force (tree gnu_stmt
)
7016 append_to_statement_list_force (gnu_stmt
, ¤t_stmt_group
->stmt_list
);
7019 /* Like add_stmt, but set the location of GNU_STMT to that of GNAT_NODE. */
7022 add_stmt_with_node (tree gnu_stmt
, Node_Id gnat_node
)
7024 if (Present (gnat_node
))
7025 set_expr_location_from_node (gnu_stmt
, gnat_node
);
7026 add_stmt (gnu_stmt
);
7029 /* Similar, but the statement is always added, regardless of side-effects. */
7032 add_stmt_with_node_force (tree gnu_stmt
, Node_Id gnat_node
)
7034 if (Present (gnat_node
))
7035 set_expr_location_from_node (gnu_stmt
, gnat_node
);
7036 add_stmt_force (gnu_stmt
);
7039 /* Add a declaration statement for GNU_DECL to the current statement group.
7040 Get SLOC from Entity_Id. */
7043 add_decl_expr (tree gnu_decl
, Entity_Id gnat_entity
)
7045 tree type
= TREE_TYPE (gnu_decl
);
7046 tree gnu_stmt
, gnu_init
, t
;
7048 /* If this is a variable that Gigi is to ignore, we may have been given
7049 an ERROR_MARK. So test for it. We also might have been given a
7050 reference for a renaming. So only do something for a decl. Also
7051 ignore a TYPE_DECL for an UNCONSTRAINED_ARRAY_TYPE. */
7052 if (!DECL_P (gnu_decl
)
7053 || (TREE_CODE (gnu_decl
) == TYPE_DECL
7054 && TREE_CODE (type
) == UNCONSTRAINED_ARRAY_TYPE
))
7057 gnu_stmt
= build1 (DECL_EXPR
, void_type_node
, gnu_decl
);
7059 /* If we are global, we don't want to actually output the DECL_EXPR for
7060 this decl since we already have evaluated the expressions in the
7061 sizes and positions as globals and doing it again would be wrong. */
7062 if (global_bindings_p ())
7064 /* Mark everything as used to prevent node sharing with subprograms.
7065 Note that walk_tree knows how to deal with TYPE_DECL, but neither
7066 VAR_DECL nor CONST_DECL. This appears to be somewhat arbitrary. */
7067 MARK_VISITED (gnu_stmt
);
7068 if (TREE_CODE (gnu_decl
) == VAR_DECL
7069 || TREE_CODE (gnu_decl
) == CONST_DECL
)
7071 MARK_VISITED (DECL_SIZE (gnu_decl
));
7072 MARK_VISITED (DECL_SIZE_UNIT (gnu_decl
));
7073 MARK_VISITED (DECL_INITIAL (gnu_decl
));
7075 /* In any case, we have to deal with our own TYPE_ADA_SIZE field. */
7076 else if (TREE_CODE (gnu_decl
) == TYPE_DECL
7077 && RECORD_OR_UNION_TYPE_P (type
)
7078 && !TYPE_FAT_POINTER_P (type
))
7079 MARK_VISITED (TYPE_ADA_SIZE (type
));
7081 else if (!DECL_EXTERNAL (gnu_decl
))
7082 add_stmt_with_node (gnu_stmt
, gnat_entity
);
7084 /* If this is a variable and an initializer is attached to it, it must be
7085 valid for the context. Similar to init_const in create_var_decl_1. */
7086 if (TREE_CODE (gnu_decl
) == VAR_DECL
7087 && (gnu_init
= DECL_INITIAL (gnu_decl
)) != NULL_TREE
7088 && (!gnat_types_compatible_p (type
, TREE_TYPE (gnu_init
))
7089 || (TREE_STATIC (gnu_decl
)
7090 && !initializer_constant_valid_p (gnu_init
,
7091 TREE_TYPE (gnu_init
)))))
7093 /* If GNU_DECL has a padded type, convert it to the unpadded
7094 type so the assignment is done properly. */
7095 if (TYPE_IS_PADDING_P (type
))
7096 t
= convert (TREE_TYPE (TYPE_FIELDS (type
)), gnu_decl
);
7100 gnu_stmt
= build_binary_op (INIT_EXPR
, NULL_TREE
, t
, gnu_init
);
7102 DECL_INITIAL (gnu_decl
) = NULL_TREE
;
7103 if (TREE_READONLY (gnu_decl
))
7105 TREE_READONLY (gnu_decl
) = 0;
7106 DECL_READONLY_ONCE_ELAB (gnu_decl
) = 1;
7109 add_stmt_with_node (gnu_stmt
, gnat_entity
);
7113 /* Callback for walk_tree to mark the visited trees rooted at *TP. */
7116 mark_visited_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
7120 if (TREE_VISITED (t
))
7123 /* Don't mark a dummy type as visited because we want to mark its sizes
7124 and fields once it's filled in. */
7125 else if (!TYPE_IS_DUMMY_P (t
))
7126 TREE_VISITED (t
) = 1;
7129 TYPE_SIZES_GIMPLIFIED (t
) = 1;
7134 /* Mark nodes rooted at T with TREE_VISITED and types as having their
7135 sized gimplified. We use this to indicate all variable sizes and
7136 positions in global types may not be shared by any subprogram. */
7139 mark_visited (tree t
)
7141 walk_tree (&t
, mark_visited_r
, NULL
, NULL
);
7144 /* Add GNU_CLEANUP, a cleanup action, to the current code group and
7145 set its location to that of GNAT_NODE if present. */
7148 add_cleanup (tree gnu_cleanup
, Node_Id gnat_node
)
7150 if (Present (gnat_node
))
7151 set_expr_location_from_node (gnu_cleanup
, gnat_node
);
7152 append_to_statement_list (gnu_cleanup
, ¤t_stmt_group
->cleanups
);
7155 /* Set the BLOCK node corresponding to the current code group to GNU_BLOCK. */
7158 set_block_for_group (tree gnu_block
)
7160 gcc_assert (!current_stmt_group
->block
);
7161 current_stmt_group
->block
= gnu_block
;
7164 /* Return code corresponding to the current code group. It is normally
7165 a STATEMENT_LIST, but may also be a BIND_EXPR or TRY_FINALLY_EXPR if
7166 BLOCK or cleanups were set. */
7169 end_stmt_group (void)
7171 struct stmt_group
*group
= current_stmt_group
;
7172 tree gnu_retval
= group
->stmt_list
;
7174 /* If this is a null list, allocate a new STATEMENT_LIST. Then, if there
7175 are cleanups, make a TRY_FINALLY_EXPR. Last, if there is a BLOCK,
7176 make a BIND_EXPR. Note that we nest in that because the cleanup may
7177 reference variables in the block. */
7178 if (gnu_retval
== NULL_TREE
)
7179 gnu_retval
= alloc_stmt_list ();
7181 if (group
->cleanups
)
7182 gnu_retval
= build2 (TRY_FINALLY_EXPR
, void_type_node
, gnu_retval
,
7185 if (current_stmt_group
->block
)
7186 gnu_retval
= build3 (BIND_EXPR
, void_type_node
, BLOCK_VARS (group
->block
),
7187 gnu_retval
, group
->block
);
7189 /* Remove this group from the stack and add it to the free list. */
7190 current_stmt_group
= group
->previous
;
7191 group
->previous
= stmt_group_free_list
;
7192 stmt_group_free_list
= group
;
7197 /* Add a list of statements from GNAT_LIST, a possibly-empty list of
7201 add_stmt_list (List_Id gnat_list
)
7205 if (Present (gnat_list
))
7206 for (gnat_node
= First (gnat_list
); Present (gnat_node
);
7207 gnat_node
= Next (gnat_node
))
7208 add_stmt (gnat_to_gnu (gnat_node
));
7211 /* Build a tree from GNAT_LIST, a possibly-empty list of statements.
7212 If BINDING_P is true, push and pop a binding level around the list. */
7215 build_stmt_group (List_Id gnat_list
, bool binding_p
)
7217 start_stmt_group ();
7221 add_stmt_list (gnat_list
);
7225 return end_stmt_group ();
7228 /* Generate GIMPLE in place for the expression at *EXPR_P. */
7231 gnat_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
,
7232 gimple_seq
*post_p ATTRIBUTE_UNUSED
)
7234 tree expr
= *expr_p
;
7237 if (IS_ADA_STMT (expr
))
7238 return gnat_gimplify_stmt (expr_p
);
7240 switch (TREE_CODE (expr
))
7243 /* If this is for a scalar, just make a VAR_DECL for it. If for
7244 an aggregate, get a null pointer of the appropriate type and
7246 if (AGGREGATE_TYPE_P (TREE_TYPE (expr
)))
7247 *expr_p
= build1 (INDIRECT_REF
, TREE_TYPE (expr
),
7248 convert (build_pointer_type (TREE_TYPE (expr
)),
7249 integer_zero_node
));
7252 *expr_p
= create_tmp_var (TREE_TYPE (expr
), NULL
);
7253 TREE_NO_WARNING (*expr_p
) = 1;
7256 gimplify_and_add (TREE_OPERAND (expr
, 0), pre_p
);
7259 case UNCONSTRAINED_ARRAY_REF
:
7260 /* We should only do this if we are just elaborating for side-effects,
7261 but we can't know that yet. */
7262 *expr_p
= TREE_OPERAND (*expr_p
, 0);
7266 op
= TREE_OPERAND (expr
, 0);
7268 /* If we are taking the address of a constant CONSTRUCTOR, make sure it
7269 is put into static memory. We know that it's going to be read-only
7270 given the semantics we have and it must be in static memory when the
7271 reference is in an elaboration procedure. */
7272 if (TREE_CODE (op
) == CONSTRUCTOR
&& TREE_CONSTANT (op
))
7274 tree addr
= build_fold_addr_expr (tree_output_constant_def (op
));
7275 *expr_p
= fold_convert (TREE_TYPE (expr
), addr
);
7279 /* Otherwise, if we are taking the address of a non-constant CONSTRUCTOR
7280 or of a call, explicitly create the local temporary. That's required
7281 if the type is passed by reference. */
7282 if (TREE_CODE (op
) == CONSTRUCTOR
|| TREE_CODE (op
) == CALL_EXPR
)
7284 tree mod
, new_var
= create_tmp_var_raw (TREE_TYPE (op
), "C");
7285 TREE_ADDRESSABLE (new_var
) = 1;
7286 gimple_add_tmp_var (new_var
);
7288 mod
= build2 (INIT_EXPR
, TREE_TYPE (new_var
), new_var
, op
);
7289 gimplify_and_add (mod
, pre_p
);
7291 TREE_OPERAND (expr
, 0) = new_var
;
7292 recompute_tree_invariant_for_addr_expr (expr
);
7296 return GS_UNHANDLED
;
7298 case VIEW_CONVERT_EXPR
:
7299 op
= TREE_OPERAND (expr
, 0);
7301 /* If we are view-converting a CONSTRUCTOR or a call from an aggregate
7302 type to a scalar one, explicitly create the local temporary. That's
7303 required if the type is passed by reference. */
7304 if ((TREE_CODE (op
) == CONSTRUCTOR
|| TREE_CODE (op
) == CALL_EXPR
)
7305 && AGGREGATE_TYPE_P (TREE_TYPE (op
))
7306 && !AGGREGATE_TYPE_P (TREE_TYPE (expr
)))
7308 tree mod
, new_var
= create_tmp_var_raw (TREE_TYPE (op
), "C");
7309 gimple_add_tmp_var (new_var
);
7311 mod
= build2 (INIT_EXPR
, TREE_TYPE (new_var
), new_var
, op
);
7312 gimplify_and_add (mod
, pre_p
);
7314 TREE_OPERAND (expr
, 0) = new_var
;
7318 return GS_UNHANDLED
;
7321 op
= DECL_EXPR_DECL (expr
);
7323 /* The expressions for the RM bounds must be gimplified to ensure that
7324 they are properly elaborated. See gimplify_decl_expr. */
7325 if ((TREE_CODE (op
) == TYPE_DECL
|| TREE_CODE (op
) == VAR_DECL
)
7326 && !TYPE_SIZES_GIMPLIFIED (TREE_TYPE (op
)))
7327 switch (TREE_CODE (TREE_TYPE (op
)))
7334 tree type
= TYPE_MAIN_VARIANT (TREE_TYPE (op
)), t
, val
;
7336 val
= TYPE_RM_MIN_VALUE (type
);
7339 gimplify_one_sizepos (&val
, pre_p
);
7340 for (t
= type
; t
; t
= TYPE_NEXT_VARIANT (t
))
7341 SET_TYPE_RM_MIN_VALUE (t
, val
);
7344 val
= TYPE_RM_MAX_VALUE (type
);
7347 gimplify_one_sizepos (&val
, pre_p
);
7348 for (t
= type
; t
; t
= TYPE_NEXT_VARIANT (t
))
7349 SET_TYPE_RM_MAX_VALUE (t
, val
);
7359 /* ... fall through ... */
7362 return GS_UNHANDLED
;
7366 /* Generate GIMPLE in place for the statement at *STMT_P. */
7368 static enum gimplify_status
7369 gnat_gimplify_stmt (tree
*stmt_p
)
7371 tree stmt
= *stmt_p
;
7373 switch (TREE_CODE (stmt
))
7376 *stmt_p
= STMT_STMT_STMT (stmt
);
7381 tree gnu_start_label
= create_artificial_label (input_location
);
7382 tree gnu_cond
= LOOP_STMT_COND (stmt
);
7383 tree gnu_update
= LOOP_STMT_UPDATE (stmt
);
7384 tree gnu_end_label
= LOOP_STMT_LABEL (stmt
);
7387 /* Build the condition expression from the test, if any. */
7390 = build3 (COND_EXPR
, void_type_node
, gnu_cond
, alloc_stmt_list (),
7391 build1 (GOTO_EXPR
, void_type_node
, gnu_end_label
));
7393 /* Set to emit the statements of the loop. */
7394 *stmt_p
= NULL_TREE
;
7396 /* We first emit the start label and then a conditional jump to the
7397 end label if there's a top condition, then the update if it's at
7398 the top, then the body of the loop, then a conditional jump to
7399 the end label if there's a bottom condition, then the update if
7400 it's at the bottom, and finally a jump to the start label and the
7401 definition of the end label. */
7402 append_to_statement_list (build1 (LABEL_EXPR
, void_type_node
,
7406 if (gnu_cond
&& !LOOP_STMT_BOTTOM_COND_P (stmt
))
7407 append_to_statement_list (gnu_cond
, stmt_p
);
7409 if (gnu_update
&& LOOP_STMT_TOP_UPDATE_P (stmt
))
7410 append_to_statement_list (gnu_update
, stmt_p
);
7412 append_to_statement_list (LOOP_STMT_BODY (stmt
), stmt_p
);
7414 if (gnu_cond
&& LOOP_STMT_BOTTOM_COND_P (stmt
))
7415 append_to_statement_list (gnu_cond
, stmt_p
);
7417 if (gnu_update
&& !LOOP_STMT_TOP_UPDATE_P (stmt
))
7418 append_to_statement_list (gnu_update
, stmt_p
);
7420 t
= build1 (GOTO_EXPR
, void_type_node
, gnu_start_label
);
7421 SET_EXPR_LOCATION (t
, DECL_SOURCE_LOCATION (gnu_end_label
));
7422 append_to_statement_list (t
, stmt_p
);
7424 append_to_statement_list (build1 (LABEL_EXPR
, void_type_node
,
7431 /* Build a statement to jump to the corresponding end label, then
7432 see if it needs to be conditional. */
7433 *stmt_p
= build1 (GOTO_EXPR
, void_type_node
, EXIT_STMT_LABEL (stmt
));
7434 if (EXIT_STMT_COND (stmt
))
7435 *stmt_p
= build3 (COND_EXPR
, void_type_node
,
7436 EXIT_STMT_COND (stmt
), *stmt_p
, alloc_stmt_list ());
7444 /* Force references to each of the entities in packages withed by GNAT_NODE.
7445 Operate recursively but check that we aren't elaborating something more
7448 This routine is exclusively called in type_annotate mode, to compute DDA
7449 information for types in withed units, for ASIS use. */
7452 elaborate_all_entities (Node_Id gnat_node
)
7454 Entity_Id gnat_with_clause
, gnat_entity
;
7456 /* Process each unit only once. As we trace the context of all relevant
7457 units transitively, including generic bodies, we may encounter the
7458 same generic unit repeatedly. */
7459 if (!present_gnu_tree (gnat_node
))
7460 save_gnu_tree (gnat_node
, integer_zero_node
, true);
7462 /* Save entities in all context units. A body may have an implicit_with
7463 on its own spec, if the context includes a child unit, so don't save
7465 for (gnat_with_clause
= First (Context_Items (gnat_node
));
7466 Present (gnat_with_clause
);
7467 gnat_with_clause
= Next (gnat_with_clause
))
7468 if (Nkind (gnat_with_clause
) == N_With_Clause
7469 && !present_gnu_tree (Library_Unit (gnat_with_clause
))
7470 && Library_Unit (gnat_with_clause
) != Library_Unit (Cunit (Main_Unit
)))
7472 elaborate_all_entities (Library_Unit (gnat_with_clause
));
7474 if (Ekind (Entity (Name (gnat_with_clause
))) == E_Package
)
7476 for (gnat_entity
= First_Entity (Entity (Name (gnat_with_clause
)));
7477 Present (gnat_entity
);
7478 gnat_entity
= Next_Entity (gnat_entity
))
7479 if (Is_Public (gnat_entity
)
7480 && Convention (gnat_entity
) != Convention_Intrinsic
7481 && Ekind (gnat_entity
) != E_Package
7482 && Ekind (gnat_entity
) != E_Package_Body
7483 && Ekind (gnat_entity
) != E_Operator
7484 && !(IN (Ekind (gnat_entity
), Type_Kind
)
7485 && !Is_Frozen (gnat_entity
))
7486 && !((Ekind (gnat_entity
) == E_Procedure
7487 || Ekind (gnat_entity
) == E_Function
)
7488 && Is_Intrinsic_Subprogram (gnat_entity
))
7489 && !IN (Ekind (gnat_entity
), Named_Kind
)
7490 && !IN (Ekind (gnat_entity
), Generic_Unit_Kind
))
7491 gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
7493 else if (Ekind (Entity (Name (gnat_with_clause
))) == E_Generic_Package
)
7496 = Corresponding_Body (Unit (Library_Unit (gnat_with_clause
)));
7498 /* Retrieve compilation unit node of generic body. */
7499 while (Present (gnat_body
)
7500 && Nkind (gnat_body
) != N_Compilation_Unit
)
7501 gnat_body
= Parent (gnat_body
);
7503 /* If body is available, elaborate its context. */
7504 if (Present (gnat_body
))
7505 elaborate_all_entities (gnat_body
);
7509 if (Nkind (Unit (gnat_node
)) == N_Package_Body
)
7510 elaborate_all_entities (Library_Unit (gnat_node
));
7513 /* Do the processing of GNAT_NODE, an N_Freeze_Entity. */
7516 process_freeze_entity (Node_Id gnat_node
)
7518 const Entity_Id gnat_entity
= Entity (gnat_node
);
7519 const Entity_Kind kind
= Ekind (gnat_entity
);
7520 tree gnu_old
, gnu_new
;
7522 /* If this is a package, we need to generate code for the package. */
7523 if (kind
== E_Package
)
7526 (Parent (Corresponding_Body
7527 (Parent (Declaration_Node (gnat_entity
)))));
7531 /* Don't do anything for class-wide types as they are always transformed
7532 into their root type. */
7533 if (kind
== E_Class_Wide_Type
)
7536 /* Check for an old definition. This freeze node might be for an Itype. */
7538 = present_gnu_tree (gnat_entity
) ? get_gnu_tree (gnat_entity
) : NULL_TREE
;
7540 /* If this entity has an address representation clause, GNU_OLD is the
7541 address, so discard it here. */
7542 if (Present (Address_Clause (gnat_entity
)))
7543 gnu_old
= NULL_TREE
;
7545 /* Don't do anything for subprograms that may have been elaborated before
7546 their freeze nodes. This can happen, for example, because of an inner
7547 call in an instance body or because of previous compilation of a spec
7548 for inlining purposes. */
7550 && ((TREE_CODE (gnu_old
) == FUNCTION_DECL
7551 && (kind
== E_Function
|| kind
== E_Procedure
))
7552 || (TREE_CODE (TREE_TYPE (gnu_old
)) == FUNCTION_TYPE
7553 && kind
== E_Subprogram_Type
)))
7556 /* If we have a non-dummy type old tree, we have nothing to do, except
7557 aborting if this is the public view of a private type whose full view was
7558 not delayed, as this node was never delayed as it should have been. We
7559 let this happen for concurrent types and their Corresponding_Record_Type,
7560 however, because each might legitimately be elaborated before its own
7561 freeze node, e.g. while processing the other. */
7563 && !(TREE_CODE (gnu_old
) == TYPE_DECL
7564 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old
))))
7566 gcc_assert ((IN (kind
, Incomplete_Or_Private_Kind
)
7567 && Present (Full_View (gnat_entity
))
7568 && No (Freeze_Node (Full_View (gnat_entity
))))
7569 || Is_Concurrent_Type (gnat_entity
)
7570 || (IN (kind
, Record_Kind
)
7571 && Is_Concurrent_Record_Type (gnat_entity
)));
7575 /* Reset the saved tree, if any, and elaborate the object or type for real.
7576 If there is a full view, elaborate it and use the result. And, if this
7577 is the root type of a class-wide type, reuse it for the latter. */
7580 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
7581 if (IN (kind
, Incomplete_Or_Private_Kind
)
7582 && Present (Full_View (gnat_entity
))
7583 && present_gnu_tree (Full_View (gnat_entity
)))
7584 save_gnu_tree (Full_View (gnat_entity
), NULL_TREE
, false);
7585 if (IN (kind
, Type_Kind
)
7586 && Present (Class_Wide_Type (gnat_entity
))
7587 && Root_Type (Class_Wide_Type (gnat_entity
)) == gnat_entity
)
7588 save_gnu_tree (Class_Wide_Type (gnat_entity
), NULL_TREE
, false);
7591 if (IN (kind
, Incomplete_Or_Private_Kind
)
7592 && Present (Full_View (gnat_entity
)))
7594 gnu_new
= gnat_to_gnu_entity (Full_View (gnat_entity
), NULL_TREE
, 1);
7596 /* Propagate back-annotations from full view to partial view. */
7597 if (Unknown_Alignment (gnat_entity
))
7598 Set_Alignment (gnat_entity
, Alignment (Full_View (gnat_entity
)));
7600 if (Unknown_Esize (gnat_entity
))
7601 Set_Esize (gnat_entity
, Esize (Full_View (gnat_entity
)));
7603 if (Unknown_RM_Size (gnat_entity
))
7604 Set_RM_Size (gnat_entity
, RM_Size (Full_View (gnat_entity
)));
7606 /* The above call may have defined this entity (the simplest example
7607 of this is when we have a private enumeral type since the bounds
7608 will have the public view). */
7609 if (!present_gnu_tree (gnat_entity
))
7610 save_gnu_tree (gnat_entity
, gnu_new
, false);
7615 = (Nkind (Declaration_Node (gnat_entity
)) == N_Object_Declaration
7616 && present_gnu_tree (Declaration_Node (gnat_entity
)))
7617 ? get_gnu_tree (Declaration_Node (gnat_entity
)) : NULL_TREE
;
7619 gnu_new
= gnat_to_gnu_entity (gnat_entity
, gnu_init
, 1);
7622 if (IN (kind
, Type_Kind
)
7623 && Present (Class_Wide_Type (gnat_entity
))
7624 && Root_Type (Class_Wide_Type (gnat_entity
)) == gnat_entity
)
7625 save_gnu_tree (Class_Wide_Type (gnat_entity
), gnu_new
, false);
7627 /* If we have an old type and we've made pointers to this type, update those
7628 pointers. If this is a Taft amendment type in the main unit, we need to
7629 mark the type as used since other units referencing it don't see the full
7630 declaration and, therefore, cannot mark it as used themselves. */
7633 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old
)),
7634 TREE_TYPE (gnu_new
));
7635 if (DECL_TAFT_TYPE_P (gnu_old
))
7636 used_types_insert (TREE_TYPE (gnu_new
));
7640 /* Elaborate decls in the lists GNAT_DECLS and GNAT_DECLS2, if present.
7641 We make two passes, one to elaborate anything other than bodies (but
7642 we declare a function if there was no spec). The second pass
7643 elaborates the bodies.
7645 GNAT_END_LIST gives the element in the list past the end. Normally,
7646 this is Empty, but can be First_Real_Statement for a
7647 Handled_Sequence_Of_Statements.
7649 We make a complete pass through both lists if PASS1P is true, then make
7650 the second pass over both lists if PASS2P is true. The lists usually
7651 correspond to the public and private parts of a package. */
7654 process_decls (List_Id gnat_decls
, List_Id gnat_decls2
,
7655 Node_Id gnat_end_list
, bool pass1p
, bool pass2p
)
7657 List_Id gnat_decl_array
[2];
7661 gnat_decl_array
[0] = gnat_decls
, gnat_decl_array
[1] = gnat_decls2
;
7664 for (i
= 0; i
<= 1; i
++)
7665 if (Present (gnat_decl_array
[i
]))
7666 for (gnat_decl
= First (gnat_decl_array
[i
]);
7667 gnat_decl
!= gnat_end_list
; gnat_decl
= Next (gnat_decl
))
7669 /* For package specs, we recurse inside the declarations,
7670 thus taking the two pass approach inside the boundary. */
7671 if (Nkind (gnat_decl
) == N_Package_Declaration
7672 && (Nkind (Specification (gnat_decl
)
7673 == N_Package_Specification
)))
7674 process_decls (Visible_Declarations (Specification (gnat_decl
)),
7675 Private_Declarations (Specification (gnat_decl
)),
7676 Empty
, true, false);
7678 /* Similarly for any declarations in the actions of a
7680 else if (Nkind (gnat_decl
) == N_Freeze_Entity
)
7682 process_freeze_entity (gnat_decl
);
7683 process_decls (Actions (gnat_decl
), Empty
, Empty
, true, false);
7686 /* Package bodies with freeze nodes get their elaboration deferred
7687 until the freeze node, but the code must be placed in the right
7688 place, so record the code position now. */
7689 else if (Nkind (gnat_decl
) == N_Package_Body
7690 && Present (Freeze_Node (Corresponding_Spec (gnat_decl
))))
7691 record_code_position (gnat_decl
);
7693 else if (Nkind (gnat_decl
) == N_Package_Body_Stub
7694 && Present (Library_Unit (gnat_decl
))
7695 && Present (Freeze_Node
7698 (Library_Unit (gnat_decl
)))))))
7699 record_code_position
7700 (Proper_Body (Unit (Library_Unit (gnat_decl
))));
7702 /* We defer most subprogram bodies to the second pass. */
7703 else if (Nkind (gnat_decl
) == N_Subprogram_Body
)
7705 if (Acts_As_Spec (gnat_decl
))
7707 Node_Id gnat_subprog_id
= Defining_Entity (gnat_decl
);
7709 if (Ekind (gnat_subprog_id
) != E_Generic_Procedure
7710 && Ekind (gnat_subprog_id
) != E_Generic_Function
)
7711 gnat_to_gnu_entity (gnat_subprog_id
, NULL_TREE
, 1);
7715 /* For bodies and stubs that act as their own specs, the entity
7716 itself must be elaborated in the first pass, because it may
7717 be used in other declarations. */
7718 else if (Nkind (gnat_decl
) == N_Subprogram_Body_Stub
)
7720 Node_Id gnat_subprog_id
7721 = Defining_Entity (Specification (gnat_decl
));
7723 if (Ekind (gnat_subprog_id
) != E_Subprogram_Body
7724 && Ekind (gnat_subprog_id
) != E_Generic_Procedure
7725 && Ekind (gnat_subprog_id
) != E_Generic_Function
)
7726 gnat_to_gnu_entity (gnat_subprog_id
, NULL_TREE
, 1);
7729 /* Concurrent stubs stand for the corresponding subprogram bodies,
7730 which are deferred like other bodies. */
7731 else if (Nkind (gnat_decl
) == N_Task_Body_Stub
7732 || Nkind (gnat_decl
) == N_Protected_Body_Stub
)
7736 add_stmt (gnat_to_gnu (gnat_decl
));
7739 /* Here we elaborate everything we deferred above except for package bodies,
7740 which are elaborated at their freeze nodes. Note that we must also
7741 go inside things (package specs and freeze nodes) the first pass did. */
7743 for (i
= 0; i
<= 1; i
++)
7744 if (Present (gnat_decl_array
[i
]))
7745 for (gnat_decl
= First (gnat_decl_array
[i
]);
7746 gnat_decl
!= gnat_end_list
; gnat_decl
= Next (gnat_decl
))
7748 if (Nkind (gnat_decl
) == N_Subprogram_Body
7749 || Nkind (gnat_decl
) == N_Subprogram_Body_Stub
7750 || Nkind (gnat_decl
) == N_Task_Body_Stub
7751 || Nkind (gnat_decl
) == N_Protected_Body_Stub
)
7752 add_stmt (gnat_to_gnu (gnat_decl
));
7754 else if (Nkind (gnat_decl
) == N_Package_Declaration
7755 && (Nkind (Specification (gnat_decl
)
7756 == N_Package_Specification
)))
7757 process_decls (Visible_Declarations (Specification (gnat_decl
)),
7758 Private_Declarations (Specification (gnat_decl
)),
7759 Empty
, false, true);
7761 else if (Nkind (gnat_decl
) == N_Freeze_Entity
)
7762 process_decls (Actions (gnat_decl
), Empty
, Empty
, false, true);
7766 /* Make a unary operation of kind CODE using build_unary_op, but guard
7767 the operation by an overflow check. CODE can be one of NEGATE_EXPR
7768 or ABS_EXPR. GNU_TYPE is the type desired for the result. Usually
7769 the operation is to be performed in that type. GNAT_NODE is the gnat
7770 node conveying the source location for which the error should be
7774 build_unary_op_trapv (enum tree_code code
, tree gnu_type
, tree operand
,
7777 gcc_assert (code
== NEGATE_EXPR
|| code
== ABS_EXPR
);
7779 operand
= gnat_protect_expr (operand
);
7781 return emit_check (build_binary_op (EQ_EXPR
, boolean_type_node
,
7782 operand
, TYPE_MIN_VALUE (gnu_type
)),
7783 build_unary_op (code
, gnu_type
, operand
),
7784 CE_Overflow_Check_Failed
, gnat_node
);
7787 /* Make a binary operation of kind CODE using build_binary_op, but guard
7788 the operation by an overflow check. CODE can be one of PLUS_EXPR,
7789 MINUS_EXPR or MULT_EXPR. GNU_TYPE is the type desired for the result.
7790 Usually the operation is to be performed in that type. GNAT_NODE is
7791 the GNAT node conveying the source location for which the error should
7795 build_binary_op_trapv (enum tree_code code
, tree gnu_type
, tree left
,
7796 tree right
, Node_Id gnat_node
)
7798 tree lhs
= gnat_protect_expr (left
);
7799 tree rhs
= gnat_protect_expr (right
);
7800 tree type_max
= TYPE_MAX_VALUE (gnu_type
);
7801 tree type_min
= TYPE_MIN_VALUE (gnu_type
);
7804 tree zero
= convert (gnu_type
, integer_zero_node
);
7809 int precision
= TYPE_PRECISION (gnu_type
);
7811 gcc_assert (!(precision
& (precision
- 1))); /* ensure power of 2 */
7813 /* Prefer a constant or known-positive rhs to simplify checks. */
7814 if (!TREE_CONSTANT (rhs
)
7815 && commutative_tree_code (code
)
7816 && (TREE_CONSTANT (lhs
) || (!tree_expr_nonnegative_p (rhs
)
7817 && tree_expr_nonnegative_p (lhs
))))
7824 rhs_lt_zero
= tree_expr_nonnegative_p (rhs
)
7825 ? boolean_false_node
7826 : build_binary_op (LT_EXPR
, boolean_type_node
, rhs
, zero
);
7828 /* ??? Should use more efficient check for operand_equal_p (lhs, rhs, 0) */
7830 /* Try a few strategies that may be cheaper than the general
7831 code at the end of the function, if the rhs is not known.
7833 - Call library function for 64-bit multiplication (complex)
7834 - Widen, if input arguments are sufficiently small
7835 - Determine overflow using wrapped result for addition/subtraction. */
7837 if (!TREE_CONSTANT (rhs
))
7839 /* Even for add/subtract double size to get another base type. */
7840 int needed_precision
= precision
* 2;
7842 if (code
== MULT_EXPR
&& precision
== 64)
7844 tree int_64
= gnat_type_for_size (64, 0);
7846 return convert (gnu_type
, build_call_n_expr (mulv64_decl
, 2,
7847 convert (int_64
, lhs
),
7848 convert (int_64
, rhs
)));
7851 else if (needed_precision
<= BITS_PER_WORD
7852 || (code
== MULT_EXPR
7853 && needed_precision
<= LONG_LONG_TYPE_SIZE
))
7855 tree wide_type
= gnat_type_for_size (needed_precision
, 0);
7857 tree wide_result
= build_binary_op (code
, wide_type
,
7858 convert (wide_type
, lhs
),
7859 convert (wide_type
, rhs
));
7861 tree check
= build_binary_op
7862 (TRUTH_ORIF_EXPR
, boolean_type_node
,
7863 build_binary_op (LT_EXPR
, boolean_type_node
, wide_result
,
7864 convert (wide_type
, type_min
)),
7865 build_binary_op (GT_EXPR
, boolean_type_node
, wide_result
,
7866 convert (wide_type
, type_max
)));
7868 tree result
= convert (gnu_type
, wide_result
);
7871 emit_check (check
, result
, CE_Overflow_Check_Failed
, gnat_node
);
7874 else if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
7876 tree unsigned_type
= gnat_type_for_size (precision
, 1);
7877 tree wrapped_expr
= convert
7878 (gnu_type
, build_binary_op (code
, unsigned_type
,
7879 convert (unsigned_type
, lhs
),
7880 convert (unsigned_type
, rhs
)));
7882 tree result
= convert
7883 (gnu_type
, build_binary_op (code
, gnu_type
, lhs
, rhs
));
7885 /* Overflow when (rhs < 0) ^ (wrapped_expr < lhs)), for addition
7886 or when (rhs < 0) ^ (wrapped_expr > lhs) for subtraction. */
7887 tree check
= build_binary_op
7888 (TRUTH_XOR_EXPR
, boolean_type_node
, rhs_lt_zero
,
7889 build_binary_op (code
== PLUS_EXPR
? LT_EXPR
: GT_EXPR
,
7890 boolean_type_node
, wrapped_expr
, lhs
));
7893 emit_check (check
, result
, CE_Overflow_Check_Failed
, gnat_node
);
7900 /* When rhs >= 0, overflow when lhs > type_max - rhs. */
7901 check_pos
= build_binary_op (GT_EXPR
, boolean_type_node
, lhs
,
7902 build_binary_op (MINUS_EXPR
, gnu_type
,
7905 /* When rhs < 0, overflow when lhs < type_min - rhs. */
7906 check_neg
= build_binary_op (LT_EXPR
, boolean_type_node
, lhs
,
7907 build_binary_op (MINUS_EXPR
, gnu_type
,
7912 /* When rhs >= 0, overflow when lhs < type_min + rhs. */
7913 check_pos
= build_binary_op (LT_EXPR
, boolean_type_node
, lhs
,
7914 build_binary_op (PLUS_EXPR
, gnu_type
,
7917 /* When rhs < 0, overflow when lhs > type_max + rhs. */
7918 check_neg
= build_binary_op (GT_EXPR
, boolean_type_node
, lhs
,
7919 build_binary_op (PLUS_EXPR
, gnu_type
,
7924 /* The check here is designed to be efficient if the rhs is constant,
7925 but it will work for any rhs by using integer division.
7926 Four different check expressions determine whether X * C overflows,
7929 C > 0 => X > type_max / C || X < type_min / C
7930 C == -1 => X == type_min
7931 C < -1 => X > type_min / C || X < type_max / C */
7933 tmp1
= build_binary_op (TRUNC_DIV_EXPR
, gnu_type
, type_max
, rhs
);
7934 tmp2
= build_binary_op (TRUNC_DIV_EXPR
, gnu_type
, type_min
, rhs
);
7937 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
7938 build_binary_op (NE_EXPR
, boolean_type_node
, zero
,
7940 build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
7941 build_binary_op (GT_EXPR
,
7944 build_binary_op (LT_EXPR
,
7949 = fold_build3 (COND_EXPR
, boolean_type_node
,
7950 build_binary_op (EQ_EXPR
, boolean_type_node
, rhs
,
7951 build_int_cst (gnu_type
, -1)),
7952 build_binary_op (EQ_EXPR
, boolean_type_node
, lhs
,
7954 build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
7955 build_binary_op (GT_EXPR
,
7958 build_binary_op (LT_EXPR
,
7967 gnu_expr
= build_binary_op (code
, gnu_type
, lhs
, rhs
);
7969 /* If we can fold the expression to a constant, just return it.
7970 The caller will deal with overflow, no need to generate a check. */
7971 if (TREE_CONSTANT (gnu_expr
))
7974 check
= fold_build3 (COND_EXPR
, boolean_type_node
, rhs_lt_zero
, check_neg
,
7977 return emit_check (check
, gnu_expr
, CE_Overflow_Check_Failed
, gnat_node
);
7980 /* Emit code for a range check. GNU_EXPR is the expression to be checked,
7981 GNAT_RANGE_TYPE the gnat type or subtype containing the bounds against
7982 which we have to check. GNAT_NODE is the GNAT node conveying the source
7983 location for which the error should be signaled. */
7986 emit_range_check (tree gnu_expr
, Entity_Id gnat_range_type
, Node_Id gnat_node
)
7988 tree gnu_range_type
= get_unpadded_type (gnat_range_type
);
7989 tree gnu_low
= TYPE_MIN_VALUE (gnu_range_type
);
7990 tree gnu_high
= TYPE_MAX_VALUE (gnu_range_type
);
7991 tree gnu_compare_type
= get_base_type (TREE_TYPE (gnu_expr
));
7993 /* If GNU_EXPR has GNAT_RANGE_TYPE as its base type, no check is needed.
7994 This can for example happen when translating 'Val or 'Value. */
7995 if (gnu_compare_type
== gnu_range_type
)
7998 /* If GNU_EXPR has an integral type that is narrower than GNU_RANGE_TYPE,
7999 we can't do anything since we might be truncating the bounds. No
8000 check is needed in this case. */
8001 if (INTEGRAL_TYPE_P (TREE_TYPE (gnu_expr
))
8002 && (TYPE_PRECISION (gnu_compare_type
)
8003 < TYPE_PRECISION (get_base_type (gnu_range_type
))))
8006 /* Checked expressions must be evaluated only once. */
8007 gnu_expr
= gnat_protect_expr (gnu_expr
);
8009 /* Note that the form of the check is
8010 (not (expr >= lo)) or (not (expr <= hi))
8011 the reason for this slightly convoluted form is that NaNs
8012 are not considered to be in range in the float case. */
8014 (build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
8016 (build_binary_op (GE_EXPR
, boolean_type_node
,
8017 convert (gnu_compare_type
, gnu_expr
),
8018 convert (gnu_compare_type
, gnu_low
))),
8020 (build_binary_op (LE_EXPR
, boolean_type_node
,
8021 convert (gnu_compare_type
, gnu_expr
),
8022 convert (gnu_compare_type
,
8024 gnu_expr
, CE_Range_Check_Failed
, gnat_node
);
8027 /* Emit code for an index check. GNU_ARRAY_OBJECT is the array object which
8028 we are about to index, GNU_EXPR is the index expression to be checked,
8029 GNU_LOW and GNU_HIGH are the lower and upper bounds against which GNU_EXPR
8030 has to be checked. Note that for index checking we cannot simply use the
8031 emit_range_check function (although very similar code needs to be generated
8032 in both cases) since for index checking the array type against which we are
8033 checking the indices may be unconstrained and consequently we need to get
8034 the actual index bounds from the array object itself (GNU_ARRAY_OBJECT).
8035 The place where we need to do that is in subprograms having unconstrained
8036 array formal parameters. GNAT_NODE is the GNAT node conveying the source
8037 location for which the error should be signaled. */
8040 emit_index_check (tree gnu_array_object
, tree gnu_expr
, tree gnu_low
,
8041 tree gnu_high
, Node_Id gnat_node
)
8043 tree gnu_expr_check
;
8045 /* Checked expressions must be evaluated only once. */
8046 gnu_expr
= gnat_protect_expr (gnu_expr
);
8048 /* Must do this computation in the base type in case the expression's
8049 type is an unsigned subtypes. */
8050 gnu_expr_check
= convert (get_base_type (TREE_TYPE (gnu_expr
)), gnu_expr
);
8052 /* If GNU_LOW or GNU_HIGH are a PLACEHOLDER_EXPR, qualify them by
8053 the object we are handling. */
8054 gnu_low
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_low
, gnu_array_object
);
8055 gnu_high
= SUBSTITUTE_PLACEHOLDER_IN_EXPR (gnu_high
, gnu_array_object
);
8058 (build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
,
8059 build_binary_op (LT_EXPR
, boolean_type_node
,
8061 convert (TREE_TYPE (gnu_expr_check
),
8063 build_binary_op (GT_EXPR
, boolean_type_node
,
8065 convert (TREE_TYPE (gnu_expr_check
),
8067 gnu_expr
, CE_Index_Check_Failed
, gnat_node
);
8070 /* GNU_COND contains the condition corresponding to an access, discriminant or
8071 range check of value GNU_EXPR. Build a COND_EXPR that returns GNU_EXPR if
8072 GNU_COND is false and raises a CONSTRAINT_ERROR if GNU_COND is true.
8073 REASON is the code that says why the exception was raised. GNAT_NODE is
8074 the GNAT node conveying the source location for which the error should be
8078 emit_check (tree gnu_cond
, tree gnu_expr
, int reason
, Node_Id gnat_node
)
8081 = build_call_raise (reason
, gnat_node
, N_Raise_Constraint_Error
);
8083 = fold_build3 (COND_EXPR
, TREE_TYPE (gnu_expr
), gnu_cond
,
8084 build2 (COMPOUND_EXPR
, TREE_TYPE (gnu_expr
), gnu_call
,
8085 convert (TREE_TYPE (gnu_expr
), integer_zero_node
)),
8088 /* GNU_RESULT has side effects if and only if GNU_EXPR has:
8089 we don't need to evaluate it just for the check. */
8090 TREE_SIDE_EFFECTS (gnu_result
) = TREE_SIDE_EFFECTS (gnu_expr
);
8095 /* Return an expression that converts GNU_EXPR to GNAT_TYPE, doing overflow
8096 checks if OVERFLOW_P is true and range checks if RANGE_P is true.
8097 GNAT_TYPE is known to be an integral type. If TRUNCATE_P true, do a
8098 float to integer conversion with truncation; otherwise round.
8099 GNAT_NODE is the GNAT node conveying the source location for which the
8100 error should be signaled. */
8103 convert_with_check (Entity_Id gnat_type
, tree gnu_expr
, bool overflowp
,
8104 bool rangep
, bool truncatep
, Node_Id gnat_node
)
8106 tree gnu_type
= get_unpadded_type (gnat_type
);
8107 tree gnu_in_type
= TREE_TYPE (gnu_expr
);
8108 tree gnu_in_basetype
= get_base_type (gnu_in_type
);
8109 tree gnu_base_type
= get_base_type (gnu_type
);
8110 tree gnu_result
= gnu_expr
;
8112 /* If we are not doing any checks, the output is an integral type, and
8113 the input is not a floating type, just do the conversion. This
8114 shortcut is required to avoid problems with packed array types
8115 and simplifies code in all cases anyway. */
8116 if (!rangep
&& !overflowp
&& INTEGRAL_TYPE_P (gnu_base_type
)
8117 && !FLOAT_TYPE_P (gnu_in_type
))
8118 return convert (gnu_type
, gnu_expr
);
8120 /* First convert the expression to its base type. This
8121 will never generate code, but makes the tests below much simpler.
8122 But don't do this if converting from an integer type to an unconstrained
8123 array type since then we need to get the bounds from the original
8125 if (TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
)
8126 gnu_result
= convert (gnu_in_basetype
, gnu_result
);
8128 /* If overflow checks are requested, we need to be sure the result will
8129 fit in the output base type. But don't do this if the input
8130 is integer and the output floating-point. */
8132 && !(FLOAT_TYPE_P (gnu_base_type
) && INTEGRAL_TYPE_P (gnu_in_basetype
)))
8134 /* Ensure GNU_EXPR only gets evaluated once. */
8135 tree gnu_input
= gnat_protect_expr (gnu_result
);
8136 tree gnu_cond
= boolean_false_node
;
8137 tree gnu_in_lb
= TYPE_MIN_VALUE (gnu_in_basetype
);
8138 tree gnu_in_ub
= TYPE_MAX_VALUE (gnu_in_basetype
);
8139 tree gnu_out_lb
= TYPE_MIN_VALUE (gnu_base_type
);
8140 tree gnu_out_ub
= TYPE_MAX_VALUE (gnu_base_type
);
8142 /* Convert the lower bounds to signed types, so we're sure we're
8143 comparing them properly. Likewise, convert the upper bounds
8144 to unsigned types. */
8145 if (INTEGRAL_TYPE_P (gnu_in_basetype
) && TYPE_UNSIGNED (gnu_in_basetype
))
8146 gnu_in_lb
= convert (gnat_signed_type (gnu_in_basetype
), gnu_in_lb
);
8148 if (INTEGRAL_TYPE_P (gnu_in_basetype
)
8149 && !TYPE_UNSIGNED (gnu_in_basetype
))
8150 gnu_in_ub
= convert (gnat_unsigned_type (gnu_in_basetype
), gnu_in_ub
);
8152 if (INTEGRAL_TYPE_P (gnu_base_type
) && TYPE_UNSIGNED (gnu_base_type
))
8153 gnu_out_lb
= convert (gnat_signed_type (gnu_base_type
), gnu_out_lb
);
8155 if (INTEGRAL_TYPE_P (gnu_base_type
) && !TYPE_UNSIGNED (gnu_base_type
))
8156 gnu_out_ub
= convert (gnat_unsigned_type (gnu_base_type
), gnu_out_ub
);
8158 /* Check each bound separately and only if the result bound
8159 is tighter than the bound on the input type. Note that all the
8160 types are base types, so the bounds must be constant. Also,
8161 the comparison is done in the base type of the input, which
8162 always has the proper signedness. First check for input
8163 integer (which means output integer), output float (which means
8164 both float), or mixed, in which case we always compare.
8165 Note that we have to do the comparison which would *fail* in the
8166 case of an error since if it's an FP comparison and one of the
8167 values is a NaN or Inf, the comparison will fail. */
8168 if (INTEGRAL_TYPE_P (gnu_in_basetype
)
8169 ? tree_int_cst_lt (gnu_in_lb
, gnu_out_lb
)
8170 : (FLOAT_TYPE_P (gnu_base_type
)
8171 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_in_lb
),
8172 TREE_REAL_CST (gnu_out_lb
))
8176 (build_binary_op (GE_EXPR
, boolean_type_node
,
8177 gnu_input
, convert (gnu_in_basetype
,
8180 if (INTEGRAL_TYPE_P (gnu_in_basetype
)
8181 ? tree_int_cst_lt (gnu_out_ub
, gnu_in_ub
)
8182 : (FLOAT_TYPE_P (gnu_base_type
)
8183 ? REAL_VALUES_LESS (TREE_REAL_CST (gnu_out_ub
),
8184 TREE_REAL_CST (gnu_in_lb
))
8187 = build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
, gnu_cond
,
8189 (build_binary_op (LE_EXPR
, boolean_type_node
,
8191 convert (gnu_in_basetype
,
8194 if (!integer_zerop (gnu_cond
))
8195 gnu_result
= emit_check (gnu_cond
, gnu_input
,
8196 CE_Overflow_Check_Failed
, gnat_node
);
8199 /* Now convert to the result base type. If this is a non-truncating
8200 float-to-integer conversion, round. */
8201 if (INTEGRAL_TYPE_P (gnu_base_type
) && FLOAT_TYPE_P (gnu_in_basetype
)
8204 REAL_VALUE_TYPE half_minus_pred_half
, pred_half
;
8205 tree gnu_conv
, gnu_zero
, gnu_comp
, calc_type
;
8206 tree gnu_pred_half
, gnu_add_pred_half
, gnu_subtract_pred_half
;
8207 const struct real_format
*fmt
;
8209 /* The following calculations depend on proper rounding to even
8210 of each arithmetic operation. In order to prevent excess
8211 precision from spoiling this property, use the widest hardware
8212 floating-point type if FP_ARITH_MAY_WIDEN is true. */
8214 = FP_ARITH_MAY_WIDEN
? longest_float_type_node
: gnu_in_basetype
;
8216 /* FIXME: Should not have padding in the first place. */
8217 if (TYPE_IS_PADDING_P (calc_type
))
8218 calc_type
= TREE_TYPE (TYPE_FIELDS (calc_type
));
8220 /* Compute the exact value calc_type'Pred (0.5) at compile time. */
8221 fmt
= REAL_MODE_FORMAT (TYPE_MODE (calc_type
));
8222 real_2expN (&half_minus_pred_half
, -(fmt
->p
) - 1, TYPE_MODE (calc_type
));
8223 REAL_ARITHMETIC (pred_half
, MINUS_EXPR
, dconsthalf
,
8224 half_minus_pred_half
);
8225 gnu_pred_half
= build_real (calc_type
, pred_half
);
8227 /* If the input is strictly negative, subtract this value
8228 and otherwise add it from the input. For 0.5, the result
8229 is exactly between 1.0 and the machine number preceding 1.0
8230 (for calc_type). Since the last bit of 1.0 is even, this 0.5
8231 will round to 1.0, while all other number with an absolute
8232 value less than 0.5 round to 0.0. For larger numbers exactly
8233 halfway between integers, rounding will always be correct as
8234 the true mathematical result will be closer to the higher
8235 integer compared to the lower one. So, this constant works
8236 for all floating-point numbers.
8238 The reason to use the same constant with subtract/add instead
8239 of a positive and negative constant is to allow the comparison
8240 to be scheduled in parallel with retrieval of the constant and
8241 conversion of the input to the calc_type (if necessary). */
8243 gnu_zero
= convert (gnu_in_basetype
, integer_zero_node
);
8244 gnu_result
= gnat_protect_expr (gnu_result
);
8245 gnu_conv
= convert (calc_type
, gnu_result
);
8247 = fold_build2 (GE_EXPR
, boolean_type_node
, gnu_result
, gnu_zero
);
8249 = fold_build2 (PLUS_EXPR
, calc_type
, gnu_conv
, gnu_pred_half
);
8250 gnu_subtract_pred_half
8251 = fold_build2 (MINUS_EXPR
, calc_type
, gnu_conv
, gnu_pred_half
);
8252 gnu_result
= fold_build3 (COND_EXPR
, calc_type
, gnu_comp
,
8253 gnu_add_pred_half
, gnu_subtract_pred_half
);
8256 if (TREE_CODE (gnu_base_type
) == INTEGER_TYPE
8257 && TYPE_HAS_ACTUAL_BOUNDS_P (gnu_base_type
)
8258 && TREE_CODE (gnu_result
) == UNCONSTRAINED_ARRAY_REF
)
8259 gnu_result
= unchecked_convert (gnu_base_type
, gnu_result
, false);
8261 gnu_result
= convert (gnu_base_type
, gnu_result
);
8263 /* Finally, do the range check if requested. Note that if the result type
8264 is a modular type, the range check is actually an overflow check. */
8266 || (TREE_CODE (gnu_base_type
) == INTEGER_TYPE
8267 && TYPE_MODULAR_P (gnu_base_type
) && overflowp
))
8268 gnu_result
= emit_range_check (gnu_result
, gnat_type
, gnat_node
);
8270 return convert (gnu_type
, gnu_result
);
8273 /* Return true if GNU_EXPR can be directly addressed. This is the case
8274 unless it is an expression involving computation or if it involves a
8275 reference to a bitfield or to an object not sufficiently aligned for
8276 its type. If GNU_TYPE is non-null, return true only if GNU_EXPR can
8277 be directly addressed as an object of this type.
8279 *** Notes on addressability issues in the Ada compiler ***
8281 This predicate is necessary in order to bridge the gap between Gigi
8282 and the middle-end about addressability of GENERIC trees. A tree
8283 is said to be addressable if it can be directly addressed, i.e. if
8284 its address can be taken, is a multiple of the type's alignment on
8285 strict-alignment architectures and returns the first storage unit
8286 assigned to the object represented by the tree.
8288 In the C family of languages, everything is in practice addressable
8289 at the language level, except for bit-fields. This means that these
8290 compilers will take the address of any tree that doesn't represent
8291 a bit-field reference and expect the result to be the first storage
8292 unit assigned to the object. Even in cases where this will result
8293 in unaligned accesses at run time, nothing is supposed to be done
8294 and the program is considered as erroneous instead (see PR c/18287).
8296 The implicit assumptions made in the middle-end are in keeping with
8297 the C viewpoint described above:
8298 - the address of a bit-field reference is supposed to be never
8299 taken; the compiler (generally) will stop on such a construct,
8300 - any other tree is addressable if it is formally addressable,
8301 i.e. if it is formally allowed to be the operand of ADDR_EXPR.
8303 In Ada, the viewpoint is the opposite one: nothing is addressable
8304 at the language level unless explicitly declared so. This means
8305 that the compiler will both make sure that the trees representing
8306 references to addressable ("aliased" in Ada parlance) objects are
8307 addressable and make no real attempts at ensuring that the trees
8308 representing references to non-addressable objects are addressable.
8310 In the first case, Ada is effectively equivalent to C and handing
8311 down the direct result of applying ADDR_EXPR to these trees to the
8312 middle-end works flawlessly. In the second case, Ada cannot afford
8313 to consider the program as erroneous if the address of trees that
8314 are not addressable is requested for technical reasons, unlike C;
8315 as a consequence, the Ada compiler must arrange for either making
8316 sure that this address is not requested in the middle-end or for
8317 compensating by inserting temporaries if it is requested in Gigi.
8319 The first goal can be achieved because the middle-end should not
8320 request the address of non-addressable trees on its own; the only
8321 exception is for the invocation of low-level block operations like
8322 memcpy, for which the addressability requirements are lower since
8323 the type's alignment can be disregarded. In practice, this means
8324 that Gigi must make sure that such operations cannot be applied to
8325 non-BLKmode bit-fields.
8327 The second goal is achieved by means of the addressable_p predicate,
8328 which computes whether a temporary must be inserted by Gigi when the
8329 address of a tree is requested; if so, the address of the temporary
8330 will be used in lieu of that of the original tree and some glue code
8331 generated to connect everything together. */
8334 addressable_p (tree gnu_expr
, tree gnu_type
)
8336 /* For an integral type, the size of the actual type of the object may not
8337 be greater than that of the expected type, otherwise an indirect access
8338 in the latter type wouldn't correctly set all the bits of the object. */
8340 && INTEGRAL_TYPE_P (gnu_type
)
8341 && smaller_form_type_p (gnu_type
, TREE_TYPE (gnu_expr
)))
8344 /* The size of the actual type of the object may not be smaller than that
8345 of the expected type, otherwise an indirect access in the latter type
8346 would be larger than the object. But only record types need to be
8347 considered in practice for this case. */
8349 && TREE_CODE (gnu_type
) == RECORD_TYPE
8350 && smaller_form_type_p (TREE_TYPE (gnu_expr
), gnu_type
))
8353 switch (TREE_CODE (gnu_expr
))
8359 /* All DECLs are addressable: if they are in a register, we can force
8363 case UNCONSTRAINED_ARRAY_REF
:
8365 /* Taking the address of a dereference yields the original pointer. */
8370 /* Taking the address yields a pointer to the constant pool. */
8374 /* Taking the address of a static constructor yields a pointer to the
8375 tree constant pool. */
8376 return TREE_STATIC (gnu_expr
) ? true : false;
8387 /* All rvalues are deemed addressable since taking their address will
8388 force a temporary to be created by the middle-end. */
8392 /* The address of a compound expression is that of its 2nd operand. */
8393 return addressable_p (TREE_OPERAND (gnu_expr
, 1), gnu_type
);
8396 /* We accept &COND_EXPR as soon as both operands are addressable and
8397 expect the outcome to be the address of the selected operand. */
8398 return (addressable_p (TREE_OPERAND (gnu_expr
, 1), NULL_TREE
)
8399 && addressable_p (TREE_OPERAND (gnu_expr
, 2), NULL_TREE
));
8402 return (((!DECL_BIT_FIELD (TREE_OPERAND (gnu_expr
, 1))
8403 /* Even with DECL_BIT_FIELD cleared, we have to ensure that
8404 the field is sufficiently aligned, in case it is subject
8405 to a pragma Component_Alignment. But we don't need to
8406 check the alignment of the containing record, as it is
8407 guaranteed to be not smaller than that of its most
8408 aligned field that is not a bit-field. */
8409 && (!STRICT_ALIGNMENT
8410 || DECL_ALIGN (TREE_OPERAND (gnu_expr
, 1))
8411 >= TYPE_ALIGN (TREE_TYPE (gnu_expr
))))
8412 /* The field of a padding record is always addressable. */
8413 || TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0))))
8414 && addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
));
8416 case ARRAY_REF
: case ARRAY_RANGE_REF
:
8417 case REALPART_EXPR
: case IMAGPART_EXPR
:
8419 return addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
);
8422 return (AGGREGATE_TYPE_P (TREE_TYPE (gnu_expr
))
8423 && addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
));
8425 case VIEW_CONVERT_EXPR
:
8427 /* This is addressable if we can avoid a copy. */
8428 tree type
= TREE_TYPE (gnu_expr
);
8429 tree inner_type
= TREE_TYPE (TREE_OPERAND (gnu_expr
, 0));
8430 return (((TYPE_MODE (type
) == TYPE_MODE (inner_type
)
8431 && (!STRICT_ALIGNMENT
8432 || TYPE_ALIGN (type
) <= TYPE_ALIGN (inner_type
)
8433 || TYPE_ALIGN (inner_type
) >= BIGGEST_ALIGNMENT
))
8434 || ((TYPE_MODE (type
) == BLKmode
8435 || TYPE_MODE (inner_type
) == BLKmode
)
8436 && (!STRICT_ALIGNMENT
8437 || TYPE_ALIGN (type
) <= TYPE_ALIGN (inner_type
)
8438 || TYPE_ALIGN (inner_type
) >= BIGGEST_ALIGNMENT
8439 || TYPE_ALIGN_OK (type
)
8440 || TYPE_ALIGN_OK (inner_type
))))
8441 && addressable_p (TREE_OPERAND (gnu_expr
, 0), NULL_TREE
));
8449 /* Do the processing for the declaration of a GNAT_ENTITY, a type. If
8450 a separate Freeze node exists, delay the bulk of the processing. Otherwise
8451 make a GCC type for GNAT_ENTITY and set up the correspondence. */
8454 process_type (Entity_Id gnat_entity
)
8457 = present_gnu_tree (gnat_entity
) ? get_gnu_tree (gnat_entity
) : 0;
8460 /* If we are to delay elaboration of this type, just do any
8461 elaborations needed for expressions within the declaration and
8462 make a dummy type entry for this node and its Full_View (if
8463 any) in case something points to it. Don't do this if it
8464 has already been done (the only way that can happen is if
8465 the private completion is also delayed). */
8466 if (Present (Freeze_Node (gnat_entity
))
8467 || (IN (Ekind (gnat_entity
), Incomplete_Or_Private_Kind
)
8468 && Present (Full_View (gnat_entity
))
8469 && Freeze_Node (Full_View (gnat_entity
))
8470 && !present_gnu_tree (Full_View (gnat_entity
))))
8472 elaborate_entity (gnat_entity
);
8476 tree gnu_decl
= TYPE_STUB_DECL (make_dummy_type (gnat_entity
));
8477 save_gnu_tree (gnat_entity
, gnu_decl
, false);
8478 if (IN (Ekind (gnat_entity
), Incomplete_Or_Private_Kind
)
8479 && Present (Full_View (gnat_entity
)))
8481 if (Has_Completion_In_Body (gnat_entity
))
8482 DECL_TAFT_TYPE_P (gnu_decl
) = 1;
8483 save_gnu_tree (Full_View (gnat_entity
), gnu_decl
, false);
8490 /* If we saved away a dummy type for this node it means that this
8491 made the type that corresponds to the full type of an incomplete
8492 type. Clear that type for now and then update the type in the
8496 gcc_assert (TREE_CODE (gnu_old
) == TYPE_DECL
8497 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_old
)));
8499 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
8502 /* Now fully elaborate the type. */
8503 gnu_new
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 1);
8504 gcc_assert (TREE_CODE (gnu_new
) == TYPE_DECL
);
8506 /* If we have an old type and we've made pointers to this type, update those
8507 pointers. If this is a Taft amendment type in the main unit, we need to
8508 mark the type as used since other units referencing it don't see the full
8509 declaration and, therefore, cannot mark it as used themselves. */
8512 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_old
)),
8513 TREE_TYPE (gnu_new
));
8514 if (DECL_TAFT_TYPE_P (gnu_old
))
8515 used_types_insert (TREE_TYPE (gnu_new
));
8518 /* If this is a record type corresponding to a task or protected type
8519 that is a completion of an incomplete type, perform a similar update
8520 on the type. ??? Including protected types here is a guess. */
8521 if (IN (Ekind (gnat_entity
), Record_Kind
)
8522 && Is_Concurrent_Record_Type (gnat_entity
)
8523 && present_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
)))
8526 = get_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
));
8528 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
),
8530 save_gnu_tree (Corresponding_Concurrent_Type (gnat_entity
),
8533 update_pointer_to (TYPE_MAIN_VARIANT (TREE_TYPE (gnu_task_old
)),
8534 TREE_TYPE (gnu_new
));
8538 /* GNAT_ENTITY is the type of the resulting constructor, GNAT_ASSOC is the
8539 front of the Component_Associations of an N_Aggregate and GNU_TYPE is the
8540 GCC type of the corresponding record type. Return the CONSTRUCTOR. */
8543 assoc_to_constructor (Entity_Id gnat_entity
, Node_Id gnat_assoc
, tree gnu_type
)
8545 tree gnu_list
= NULL_TREE
, gnu_result
;
8547 /* We test for GNU_FIELD being empty in the case where a variant
8548 was the last thing since we don't take things off GNAT_ASSOC in
8549 that case. We check GNAT_ASSOC in case we have a variant, but it
8552 for (; Present (gnat_assoc
); gnat_assoc
= Next (gnat_assoc
))
8554 Node_Id gnat_field
= First (Choices (gnat_assoc
));
8555 tree gnu_field
= gnat_to_gnu_field_decl (Entity (gnat_field
));
8556 tree gnu_expr
= gnat_to_gnu (Expression (gnat_assoc
));
8558 /* The expander is supposed to put a single component selector name
8559 in every record component association. */
8560 gcc_assert (No (Next (gnat_field
)));
8562 /* Ignore fields that have Corresponding_Discriminants since we'll
8563 be setting that field in the parent. */
8564 if (Present (Corresponding_Discriminant (Entity (gnat_field
)))
8565 && Is_Tagged_Type (Scope (Entity (gnat_field
))))
8568 /* Also ignore discriminants of Unchecked_Unions. */
8569 if (Is_Unchecked_Union (gnat_entity
)
8570 && Ekind (Entity (gnat_field
)) == E_Discriminant
)
8573 /* Before assigning a value in an aggregate make sure range checks
8574 are done if required. Then convert to the type of the field. */
8575 if (Do_Range_Check (Expression (gnat_assoc
)))
8576 gnu_expr
= emit_range_check (gnu_expr
, Etype (gnat_field
), Empty
);
8578 gnu_expr
= convert (TREE_TYPE (gnu_field
), gnu_expr
);
8580 /* Add the field and expression to the list. */
8581 gnu_list
= tree_cons (gnu_field
, gnu_expr
, gnu_list
);
8584 gnu_result
= extract_values (gnu_list
, gnu_type
);
8586 #ifdef ENABLE_CHECKING
8587 /* Verify that every entry in GNU_LIST was used. */
8588 for (; gnu_list
; gnu_list
= TREE_CHAIN (gnu_list
))
8589 gcc_assert (TREE_ADDRESSABLE (gnu_list
));
8595 /* Build a possibly nested constructor for array aggregates. GNAT_EXPR is
8596 the first element of an array aggregate. It may itself be an aggregate.
8597 GNU_ARRAY_TYPE is the GCC type corresponding to the array aggregate.
8598 GNAT_COMPONENT_TYPE is the type of the array component; it is needed
8599 for range checking. */
8602 pos_to_constructor (Node_Id gnat_expr
, tree gnu_array_type
,
8603 Entity_Id gnat_component_type
)
8605 tree gnu_index
= TYPE_MIN_VALUE (TYPE_DOMAIN (gnu_array_type
));
8607 VEC(constructor_elt
,gc
) *gnu_expr_vec
= NULL
;
8609 for ( ; Present (gnat_expr
); gnat_expr
= Next (gnat_expr
))
8611 /* If the expression is itself an array aggregate then first build the
8612 innermost constructor if it is part of our array (multi-dimensional
8614 if (Nkind (gnat_expr
) == N_Aggregate
8615 && TREE_CODE (TREE_TYPE (gnu_array_type
)) == ARRAY_TYPE
8616 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_array_type
)))
8617 gnu_expr
= pos_to_constructor (First (Expressions (gnat_expr
)),
8618 TREE_TYPE (gnu_array_type
),
8619 gnat_component_type
);
8622 gnu_expr
= gnat_to_gnu (gnat_expr
);
8624 /* Before assigning the element to the array, make sure it is
8626 if (Do_Range_Check (gnat_expr
))
8627 gnu_expr
= emit_range_check (gnu_expr
, gnat_component_type
, Empty
);
8630 CONSTRUCTOR_APPEND_ELT (gnu_expr_vec
, gnu_index
,
8631 convert (TREE_TYPE (gnu_array_type
), gnu_expr
));
8633 gnu_index
= int_const_binop (PLUS_EXPR
, gnu_index
, integer_one_node
);
8636 return gnat_build_constructor (gnu_array_type
, gnu_expr_vec
);
8639 /* Subroutine of assoc_to_constructor: VALUES is a list of field associations,
8640 some of which are from RECORD_TYPE. Return a CONSTRUCTOR consisting
8641 of the associations that are from RECORD_TYPE. If we see an internal
8642 record, make a recursive call to fill it in as well. */
8645 extract_values (tree values
, tree record_type
)
8648 VEC(constructor_elt
,gc
) *v
= NULL
;
8650 for (field
= TYPE_FIELDS (record_type
); field
; field
= DECL_CHAIN (field
))
8654 /* _Parent is an internal field, but may have values in the aggregate,
8655 so check for values first. */
8656 if ((tem
= purpose_member (field
, values
)))
8658 value
= TREE_VALUE (tem
);
8659 TREE_ADDRESSABLE (tem
) = 1;
8662 else if (DECL_INTERNAL_P (field
))
8664 value
= extract_values (values
, TREE_TYPE (field
));
8665 if (TREE_CODE (value
) == CONSTRUCTOR
8666 && VEC_empty (constructor_elt
, CONSTRUCTOR_ELTS (value
)))
8670 /* If we have a record subtype, the names will match, but not the
8671 actual FIELD_DECLs. */
8672 for (tem
= values
; tem
; tem
= TREE_CHAIN (tem
))
8673 if (DECL_NAME (TREE_PURPOSE (tem
)) == DECL_NAME (field
))
8675 value
= convert (TREE_TYPE (field
), TREE_VALUE (tem
));
8676 TREE_ADDRESSABLE (tem
) = 1;
8682 CONSTRUCTOR_APPEND_ELT (v
, field
, value
);
8685 return gnat_build_constructor (record_type
, v
);
8688 /* EXP is to be treated as an array or record. Handle the cases when it is
8689 an access object and perform the required dereferences. */
8692 maybe_implicit_deref (tree exp
)
8694 /* If the type is a pointer, dereference it. */
8695 if (POINTER_TYPE_P (TREE_TYPE (exp
))
8696 || TYPE_IS_FAT_POINTER_P (TREE_TYPE (exp
)))
8697 exp
= build_unary_op (INDIRECT_REF
, NULL_TREE
, exp
);
8699 /* If we got a padded type, remove it too. */
8700 if (TYPE_IS_PADDING_P (TREE_TYPE (exp
)))
8701 exp
= convert (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (exp
))), exp
);
8706 /* Convert SLOC into LOCUS. Return true if SLOC corresponds to a source code
8707 location and false if it doesn't. In the former case, set the Gigi global
8708 variable REF_FILENAME to the simple debug file name as given by sinput. */
8711 Sloc_to_locus (Source_Ptr Sloc
, location_t
*locus
)
8713 if (Sloc
== No_Location
)
8716 if (Sloc
<= Standard_Location
)
8718 *locus
= BUILTINS_LOCATION
;
8723 Source_File_Index file
= Get_Source_File_Index (Sloc
);
8724 Logical_Line_Number line
= Get_Logical_Line_Number (Sloc
);
8725 Column_Number column
= Get_Column_Number (Sloc
);
8726 struct line_map
*map
= LINEMAPS_ORDINARY_MAP_AT (line_table
, file
- 1);
8728 /* We can have zero if pragma Source_Reference is in effect. */
8732 /* Translate the location. */
8733 *locus
= linemap_position_for_line_and_column (map
, line
, column
);
8737 = IDENTIFIER_POINTER
8739 (Get_Name_String (Debug_Source_Name (Get_Source_File_Index (Sloc
)))));;
8744 /* Similar to set_expr_location, but start with the Sloc of GNAT_NODE and
8745 don't do anything if it doesn't correspond to a source location. */
8748 set_expr_location_from_node (tree node
, Node_Id gnat_node
)
8752 if (!Sloc_to_locus (Sloc (gnat_node
), &locus
))
8755 SET_EXPR_LOCATION (node
, locus
);
8758 /* More elaborate version of set_expr_location_from_node to be used in more
8759 general contexts, for example the result of the translation of a generic
8763 set_gnu_expr_location_from_node (tree node
, Node_Id gnat_node
)
8765 /* Set the location information on the node if it is a real expression.
8766 References can be reused for multiple GNAT nodes and they would get
8767 the location information of their last use. Also make sure not to
8768 overwrite an existing location as it is probably more precise. */
8770 switch (TREE_CODE (node
))
8773 case NON_LVALUE_EXPR
:
8777 if (EXPR_P (TREE_OPERAND (node
, 1)))
8778 set_gnu_expr_location_from_node (TREE_OPERAND (node
, 1), gnat_node
);
8780 /* ... fall through ... */
8783 if (!REFERENCE_CLASS_P (node
) && !EXPR_HAS_LOCATION (node
))
8785 set_expr_location_from_node (node
, gnat_node
);
8786 set_end_locus_from_node (node
, gnat_node
);
8792 /* Return a colon-separated list of encodings contained in encoded Ada
8796 extract_encoding (const char *name
)
8798 char *encoding
= (char *) ggc_alloc_atomic (strlen (name
));
8799 get_encoding (name
, encoding
);
8803 /* Extract the Ada name from an encoded name. */
8806 decode_name (const char *name
)
8808 char *decoded
= (char *) ggc_alloc_atomic (strlen (name
) * 2 + 60);
8809 __gnat_decode (name
, decoded
, 0);
8813 /* Post an error message. MSG is the error message, properly annotated.
8814 NODE is the node at which to post the error and the node to use for the
8815 '&' substitution. */
8818 post_error (const char *msg
, Node_Id node
)
8820 String_Template temp
;
8823 temp
.Low_Bound
= 1, temp
.High_Bound
= strlen (msg
);
8824 fp
.Array
= msg
, fp
.Bounds
= &temp
;
8826 Error_Msg_N (fp
, node
);
8829 /* Similar to post_error, but NODE is the node at which to post the error and
8830 ENT is the node to use for the '&' substitution. */
8833 post_error_ne (const char *msg
, Node_Id node
, Entity_Id ent
)
8835 String_Template temp
;
8838 temp
.Low_Bound
= 1, temp
.High_Bound
= strlen (msg
);
8839 fp
.Array
= msg
, fp
.Bounds
= &temp
;
8841 Error_Msg_NE (fp
, node
, ent
);
8844 /* Similar to post_error_ne, but NUM is the number to use for the '^'. */
8847 post_error_ne_num (const char *msg
, Node_Id node
, Entity_Id ent
, int num
)
8849 Error_Msg_Uint_1
= UI_From_Int (num
);
8850 post_error_ne (msg
, node
, ent
);
8853 /* Set the end_locus information for GNU_NODE, if any, from an explicit end
8854 location associated with GNAT_NODE or GNAT_NODE itself, whichever makes
8855 most sense. Return true if a sensible assignment was performed. */
8858 set_end_locus_from_node (tree gnu_node
, Node_Id gnat_node
)
8860 Node_Id gnat_end_label
= Empty
;
8861 location_t end_locus
;
8863 /* Pick the GNAT node of which we'll take the sloc to assign to the GCC node
8864 end_locus when there is one. We consider only GNAT nodes with a possible
8865 End_Label attached. If the End_Label actually was unassigned, fallback
8866 on the orginal node. We'd better assign an explicit sloc associated with
8867 the outer construct in any case. */
8869 switch (Nkind (gnat_node
))
8871 case N_Package_Body
:
8872 case N_Subprogram_Body
:
8873 case N_Block_Statement
:
8874 gnat_end_label
= End_Label (Handled_Statement_Sequence (gnat_node
));
8877 case N_Package_Declaration
:
8878 gnat_end_label
= End_Label (Specification (gnat_node
));
8885 gnat_node
= Present (gnat_end_label
) ? gnat_end_label
: gnat_node
;
8887 /* Some expanded subprograms have neither an End_Label nor a Sloc
8888 attached. Notify that to callers. */
8890 if (!Sloc_to_locus (Sloc (gnat_node
), &end_locus
))
8893 switch (TREE_CODE (gnu_node
))
8896 BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (gnu_node
)) = end_locus
;
8900 DECL_STRUCT_FUNCTION (gnu_node
)->function_end_locus
= end_locus
;
8908 /* Similar to post_error_ne, but T is a GCC tree representing the number to
8909 write. If T represents a constant, the text inside curly brackets in
8910 MSG will be output (presumably including a '^'). Otherwise it will not
8911 be output and the text inside square brackets will be output instead. */
8914 post_error_ne_tree (const char *msg
, Node_Id node
, Entity_Id ent
, tree t
)
8916 char *new_msg
= XALLOCAVEC (char, strlen (msg
) + 1);
8917 char start_yes
, end_yes
, start_no
, end_no
;
8921 if (TREE_CODE (t
) == INTEGER_CST
)
8923 Error_Msg_Uint_1
= UI_From_gnu (t
);
8924 start_yes
= '{', end_yes
= '}', start_no
= '[', end_no
= ']';
8927 start_yes
= '[', end_yes
= ']', start_no
= '{', end_no
= '}';
8929 for (p
= msg
, q
= new_msg
; *p
; p
++)
8931 if (*p
== start_yes
)
8932 for (p
++; *p
!= end_yes
; p
++)
8934 else if (*p
== start_no
)
8935 for (p
++; *p
!= end_no
; p
++)
8943 post_error_ne (new_msg
, node
, ent
);
8946 /* Similar to post_error_ne_tree, but NUM is a second integer to write. */
8949 post_error_ne_tree_2 (const char *msg
, Node_Id node
, Entity_Id ent
, tree t
,
8952 Error_Msg_Uint_2
= UI_From_Int (num
);
8953 post_error_ne_tree (msg
, node
, ent
, t
);
8956 /* Initialize the table that maps GNAT codes to GCC codes for simple
8957 binary and unary operations. */
8960 init_code_table (void)
8962 gnu_codes
[N_And_Then
] = TRUTH_ANDIF_EXPR
;
8963 gnu_codes
[N_Or_Else
] = TRUTH_ORIF_EXPR
;
8965 gnu_codes
[N_Op_And
] = TRUTH_AND_EXPR
;
8966 gnu_codes
[N_Op_Or
] = TRUTH_OR_EXPR
;
8967 gnu_codes
[N_Op_Xor
] = TRUTH_XOR_EXPR
;
8968 gnu_codes
[N_Op_Eq
] = EQ_EXPR
;
8969 gnu_codes
[N_Op_Ne
] = NE_EXPR
;
8970 gnu_codes
[N_Op_Lt
] = LT_EXPR
;
8971 gnu_codes
[N_Op_Le
] = LE_EXPR
;
8972 gnu_codes
[N_Op_Gt
] = GT_EXPR
;
8973 gnu_codes
[N_Op_Ge
] = GE_EXPR
;
8974 gnu_codes
[N_Op_Add
] = PLUS_EXPR
;
8975 gnu_codes
[N_Op_Subtract
] = MINUS_EXPR
;
8976 gnu_codes
[N_Op_Multiply
] = MULT_EXPR
;
8977 gnu_codes
[N_Op_Mod
] = FLOOR_MOD_EXPR
;
8978 gnu_codes
[N_Op_Rem
] = TRUNC_MOD_EXPR
;
8979 gnu_codes
[N_Op_Minus
] = NEGATE_EXPR
;
8980 gnu_codes
[N_Op_Abs
] = ABS_EXPR
;
8981 gnu_codes
[N_Op_Not
] = TRUTH_NOT_EXPR
;
8982 gnu_codes
[N_Op_Rotate_Left
] = LROTATE_EXPR
;
8983 gnu_codes
[N_Op_Rotate_Right
] = RROTATE_EXPR
;
8984 gnu_codes
[N_Op_Shift_Left
] = LSHIFT_EXPR
;
8985 gnu_codes
[N_Op_Shift_Right
] = RSHIFT_EXPR
;
8986 gnu_codes
[N_Op_Shift_Right_Arithmetic
] = RSHIFT_EXPR
;
8989 /* Return a label to branch to for the exception type in KIND or NULL_TREE
8993 get_exception_label (char kind
)
8995 if (kind
== N_Raise_Constraint_Error
)
8996 return VEC_last (tree
, gnu_constraint_error_label_stack
);
8997 else if (kind
== N_Raise_Storage_Error
)
8998 return VEC_last (tree
, gnu_storage_error_label_stack
);
8999 else if (kind
== N_Raise_Program_Error
)
9000 return VEC_last (tree
, gnu_program_error_label_stack
);
9005 /* Return the decl for the current elaboration procedure. */
9008 get_elaboration_procedure (void)
9010 return VEC_last (tree
, gnu_elab_proc_stack
);
9013 #include "gt-ada-trans.h"