1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2016, 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 along with GCC; see the 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"
31 #include "stringpool.h"
32 #include "diagnostic-core.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tree-inline.h"
54 /* "stdcall" and "thiscall" conventions should be processed in a specific way
55 on 32-bit x86/Windows only. The macros below are helpers to avoid having
56 to check for a Windows specific attribute throughout this unit. */
58 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
60 #define Has_Stdcall_Convention(E) \
61 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
62 #define Has_Thiscall_Convention(E) \
63 (!TARGET_64BIT && is_cplusplus_method (E))
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
69 #define Has_Stdcall_Convention(E) 0
70 #define Has_Thiscall_Convention(E) 0
73 #define STDCALL_PREFIX "_imp__"
75 /* Stack realignment is necessary for functions with foreign conventions when
76 the ABI doesn't mandate as much as what the compiler assumes - that is, up
77 to PREFERRED_STACK_BOUNDARY.
79 Such realignment can be requested with a dedicated function type attribute
80 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
81 characterize the situations where the attribute should be set. We rely on
82 compiler configuration settings for 'main' to decide. */
84 #ifdef MAIN_STACK_BOUNDARY
85 #define FOREIGN_FORCE_REALIGN_STACK \
86 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
88 #define FOREIGN_FORCE_REALIGN_STACK 0
93 struct incomplete
*next
;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing a record, an array or a subprogram type. */
100 static int defer_incomplete_level
= 0;
101 static struct incomplete
*defer_incomplete_list
;
103 /* This variable is used to delay expanding From_Limited_With types until the
105 static struct incomplete
*defer_limited_with_list
;
107 typedef struct subst_pair_d
{
113 typedef struct variant_desc_d
{
114 /* The type of the variant. */
117 /* The associated field. */
120 /* The value of the qualifier. */
123 /* The type of the variant after transformation. */
128 /* A map used to cache the result of annotate_value. */
129 struct value_annotation_hasher
: ggc_cache_ptr_hash
<tree_int_map
>
131 static inline hashval_t
132 hash (tree_int_map
*m
)
134 return htab_hash_pointer (m
->base
.from
);
138 equal (tree_int_map
*a
, tree_int_map
*b
)
140 return a
->base
.from
== b
->base
.from
;
144 keep_cache_entry (tree_int_map
*&m
)
146 return ggc_marked_p (m
->base
.from
);
150 static GTY ((cache
)) hash_table
<value_annotation_hasher
> *annotate_value_cache
;
152 /* A map used to associate a dummy type with a list of subprogram entities. */
153 struct GTY((for_user
)) tree_entity_vec_map
155 struct tree_map_base base
;
156 vec
<Entity_Id
, va_gc_atomic
> *to
;
160 gt_pch_nx (Entity_Id
&)
165 gt_pch_nx (Entity_Id
*x
, gt_pointer_operator op
, void *cookie
)
170 struct dummy_type_hasher
: ggc_cache_ptr_hash
<tree_entity_vec_map
>
172 static inline hashval_t
173 hash (tree_entity_vec_map
*m
)
175 return htab_hash_pointer (m
->base
.from
);
179 equal (tree_entity_vec_map
*a
, tree_entity_vec_map
*b
)
181 return a
->base
.from
== b
->base
.from
;
185 keep_cache_entry (tree_entity_vec_map
*&m
)
187 return ggc_marked_p (m
->base
.from
);
191 static GTY ((cache
)) hash_table
<dummy_type_hasher
> *dummy_to_subprog_map
;
193 static void prepend_one_attribute (struct attrib
**,
194 enum attrib_type
, tree
, tree
, Node_Id
);
195 static void prepend_one_attribute_pragma (struct attrib
**, Node_Id
);
196 static void prepend_attributes (struct attrib
**, Entity_Id
);
197 static tree
elaborate_expression (Node_Id
, Entity_Id
, const char *, bool, bool,
199 static bool type_has_variable_size (tree
);
200 static tree
elaborate_expression_1 (tree
, Entity_Id
, const char *, bool, bool);
201 static tree
elaborate_expression_2 (tree
, Entity_Id
, const char *, bool, bool,
203 static tree
elaborate_reference (tree
, Entity_Id
, bool, tree
*);
204 static tree
gnat_to_gnu_component_type (Entity_Id
, bool, bool);
205 static tree
gnat_to_gnu_subprog_type (Entity_Id
, bool, bool, tree
*);
206 static tree
gnat_to_gnu_field (Entity_Id
, tree
, int, bool, bool);
207 static tree
change_qualified_type (tree
, int);
208 static bool same_discriminant_p (Entity_Id
, Entity_Id
);
209 static bool array_type_has_nonaliased_component (tree
, Entity_Id
);
210 static bool compile_time_known_address_p (Node_Id
);
211 static bool cannot_be_superflat (Node_Id
);
212 static bool constructor_address_p (tree
);
213 static bool allocatable_size_p (tree
, bool);
214 static bool initial_value_needs_conversion (tree
, tree
);
215 static int compare_field_bitpos (const PTR
, const PTR
);
216 static bool components_to_record (tree
, Node_Id
, tree
, int, bool, bool, bool,
217 bool, bool, bool, bool, bool, tree
, tree
*);
218 static Uint
annotate_value (tree
);
219 static void annotate_rep (Entity_Id
, tree
);
220 static tree
build_position_list (tree
, bool, tree
, tree
, unsigned int, tree
);
221 static vec
<subst_pair
> build_subst_list (Entity_Id
, Entity_Id
, bool);
222 static vec
<variant_desc
> build_variant_list (tree
,
225 static tree
validate_size (Uint
, tree
, Entity_Id
, enum tree_code
, bool, bool);
226 static void set_rm_size (Uint
, tree
, Entity_Id
);
227 static unsigned int validate_alignment (Uint
, Entity_Id
, unsigned int);
228 static void check_ok_for_atomic_type (tree
, Entity_Id
, bool);
229 static tree
create_field_decl_from (tree
, tree
, tree
, tree
, tree
,
231 static tree
create_rep_part (tree
, tree
, tree
);
232 static tree
get_rep_part (tree
);
233 static tree
create_variant_part_from (tree
, vec
<variant_desc
> , tree
,
234 tree
, vec
<subst_pair
> );
235 static void copy_and_substitute_in_size (tree
, tree
, vec
<subst_pair
> );
236 static void associate_original_type_to_packed_array (tree
, Entity_Id
);
237 static const char *get_entity_char (Entity_Id
);
239 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
240 to pass around calls performing profile compatibility checks. */
243 Entity_Id gnat_entity
; /* The Ada subprogram entity. */
244 tree ada_fntype
; /* The corresponding GCC type node. */
245 tree btin_fntype
; /* The GCC builtin function type node. */
248 static bool intrin_profiles_compatible_p (intrin_binding_t
*);
250 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
251 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
252 and associate the ..._DECL node with the input GNAT defining identifier.
254 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
255 initial value (in GCC tree form). This is optional for a variable. For
256 a renamed entity, GNU_EXPR gives the object being renamed.
258 DEFINITION is true if this call is intended for a definition. This is used
259 for separate compilation where it is necessary to know whether an external
260 declaration or a definition must be created if the GCC equivalent was not
261 created previously. */
264 gnat_to_gnu_entity (Entity_Id gnat_entity
, tree gnu_expr
, bool definition
)
266 /* Contains the kind of the input GNAT node. */
267 const Entity_Kind kind
= Ekind (gnat_entity
);
268 /* True if this is a type. */
269 const bool is_type
= IN (kind
, Type_Kind
);
270 /* True if this is an artificial entity. */
271 const bool artificial_p
= !Comes_From_Source (gnat_entity
);
272 /* True if debug info is requested for this entity. */
273 const bool debug_info_p
= Needs_Debug_Info (gnat_entity
);
274 /* True if this entity is to be considered as imported. */
275 const bool imported_p
276 = (Is_Imported (gnat_entity
) && No (Address_Clause (gnat_entity
)));
277 /* For a type, contains the equivalent GNAT node to be used in gigi. */
278 Entity_Id gnat_equiv_type
= Empty
;
279 /* Temporary used to walk the GNAT tree. */
281 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
282 This node will be associated with the GNAT node by calling at the end
283 of the `switch' statement. */
284 tree gnu_decl
= NULL_TREE
;
285 /* Contains the GCC type to be used for the GCC node. */
286 tree gnu_type
= NULL_TREE
;
287 /* Contains the GCC size tree to be used for the GCC node. */
288 tree gnu_size
= NULL_TREE
;
289 /* Contains the GCC name to be used for the GCC node. */
290 tree gnu_entity_name
;
291 /* True if we have already saved gnu_decl as a GNAT association. */
293 /* True if we incremented defer_incomplete_level. */
294 bool this_deferred
= false;
295 /* True if we incremented force_global. */
296 bool this_global
= false;
297 /* True if we should check to see if elaborated during processing. */
298 bool maybe_present
= false;
299 /* True if we made GNU_DECL and its type here. */
300 bool this_made_decl
= false;
301 /* Size and alignment of the GCC node, if meaningful. */
302 unsigned int esize
= 0, align
= 0;
303 /* Contains the list of attributes directly attached to the entity. */
304 struct attrib
*attr_list
= NULL
;
306 /* Since a use of an Itype is a definition, process it as such if it
307 is not in a with'ed unit. */
310 && Is_Itype (gnat_entity
)
311 && !present_gnu_tree (gnat_entity
)
312 && In_Extended_Main_Code_Unit (gnat_entity
))
314 /* Ensure that we are in a subprogram mentioned in the Scope chain of
315 this entity, our current scope is global, or we encountered a task
316 or entry (where we can't currently accurately check scoping). */
317 if (!current_function_decl
318 || DECL_ELABORATION_PROC_P (current_function_decl
))
320 process_type (gnat_entity
);
321 return get_gnu_tree (gnat_entity
);
324 for (gnat_temp
= Scope (gnat_entity
);
326 gnat_temp
= Scope (gnat_temp
))
328 if (Is_Type (gnat_temp
))
329 gnat_temp
= Underlying_Type (gnat_temp
);
331 if (Ekind (gnat_temp
) == E_Subprogram_Body
)
333 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp
)));
335 if (IN (Ekind (gnat_temp
), Subprogram_Kind
)
336 && Present (Protected_Body_Subprogram (gnat_temp
)))
337 gnat_temp
= Protected_Body_Subprogram (gnat_temp
);
339 if (Ekind (gnat_temp
) == E_Entry
340 || Ekind (gnat_temp
) == E_Entry_Family
341 || Ekind (gnat_temp
) == E_Task_Type
342 || (IN (Ekind (gnat_temp
), Subprogram_Kind
)
343 && present_gnu_tree (gnat_temp
)
344 && (current_function_decl
345 == gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, false))))
347 process_type (gnat_entity
);
348 return get_gnu_tree (gnat_entity
);
352 /* This abort means the Itype has an incorrect scope, i.e. that its
353 scope does not correspond to the subprogram it is declared in. */
357 /* If we've already processed this entity, return what we got last time.
358 If we are defining the node, we should not have already processed it.
359 In that case, we will abort below when we try to save a new GCC tree
360 for this object. We also need to handle the case of getting a dummy
361 type when a Full_View exists but be careful so as not to trigger its
362 premature elaboration. */
363 if ((!definition
|| (is_type
&& imported_p
))
364 && present_gnu_tree (gnat_entity
))
366 gnu_decl
= get_gnu_tree (gnat_entity
);
368 if (TREE_CODE (gnu_decl
) == TYPE_DECL
369 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
))
370 && IN (kind
, Incomplete_Or_Private_Kind
)
371 && Present (Full_View (gnat_entity
))
372 && (present_gnu_tree (Full_View (gnat_entity
))
373 || No (Freeze_Node (Full_View (gnat_entity
)))))
376 = gnat_to_gnu_entity (Full_View (gnat_entity
), NULL_TREE
, false);
377 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
378 save_gnu_tree (gnat_entity
, gnu_decl
, false);
384 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
385 must be specified unless it was specified by the programmer. Exceptions
386 are for access-to-protected-subprogram types and all access subtypes, as
387 another GNAT type is used to lay out the GCC type for them. */
388 gcc_assert (!Unknown_Esize (gnat_entity
)
389 || Has_Size_Clause (gnat_entity
)
390 || (!IN (kind
, Numeric_Kind
)
391 && !IN (kind
, Enumeration_Kind
)
392 && (!IN (kind
, Access_Kind
)
393 || kind
== E_Access_Protected_Subprogram_Type
394 || kind
== E_Anonymous_Access_Protected_Subprogram_Type
395 || kind
== E_Access_Subtype
396 || type_annotate_only
)));
398 /* The RM size must be specified for all discrete and fixed-point types. */
399 gcc_assert (!(IN (kind
, Discrete_Or_Fixed_Point_Kind
)
400 && Unknown_RM_Size (gnat_entity
)));
402 /* If we get here, it means we have not yet done anything with this entity.
403 If we are not defining it, it must be a type or an entity that is defined
404 elsewhere or externally, otherwise we should have defined it already. */
405 gcc_assert (definition
406 || type_annotate_only
408 || kind
== E_Discriminant
409 || kind
== E_Component
411 || (kind
== E_Constant
&& Present (Full_View (gnat_entity
)))
412 || Is_Public (gnat_entity
));
414 /* Get the name of the entity and set up the line number and filename of
415 the original definition for use in any decl we make. Make sure we do not
416 inherit another source location. */
417 gnu_entity_name
= get_entity_name (gnat_entity
);
418 if (Sloc (gnat_entity
) != No_Location
419 && !renaming_from_generic_instantiation_p (gnat_entity
))
420 Sloc_to_locus (Sloc (gnat_entity
), &input_location
);
422 /* For cases when we are not defining (i.e., we are referencing from
423 another compilation unit) public entities, show we are at global level
424 for the purpose of computing scopes. Don't do this for components or
425 discriminants since the relevant test is whether or not the record is
428 && kind
!= E_Component
429 && kind
!= E_Discriminant
430 && Is_Public (gnat_entity
)
431 && !Is_Statically_Allocated (gnat_entity
))
432 force_global
++, this_global
= true;
434 /* Handle any attributes directly attached to the entity. */
435 if (Has_Gigi_Rep_Item (gnat_entity
))
436 prepend_attributes (&attr_list
, gnat_entity
);
438 /* Do some common processing for types. */
441 /* Compute the equivalent type to be used in gigi. */
442 gnat_equiv_type
= Gigi_Equivalent_Type (gnat_entity
);
444 /* Machine_Attributes on types are expected to be propagated to
445 subtypes. The corresponding Gigi_Rep_Items are only attached
446 to the first subtype though, so we handle the propagation here. */
447 if (Base_Type (gnat_entity
) != gnat_entity
448 && !Is_First_Subtype (gnat_entity
)
449 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity
))))
450 prepend_attributes (&attr_list
,
451 First_Subtype (Base_Type (gnat_entity
)));
453 /* Compute a default value for the size of an elementary type. */
454 if (Known_Esize (gnat_entity
) && Is_Elementary_Type (gnat_entity
))
456 unsigned int max_esize
;
458 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity
)));
459 esize
= UI_To_Int (Esize (gnat_entity
));
461 if (IN (kind
, Float_Kind
))
462 max_esize
= fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE
);
463 else if (IN (kind
, Access_Kind
))
464 max_esize
= POINTER_SIZE
* 2;
466 max_esize
= LONG_LONG_TYPE_SIZE
;
468 if (esize
> max_esize
)
478 /* The GNAT record where the component was defined. */
479 Entity_Id gnat_record
= Underlying_Type (Scope (gnat_entity
));
481 /* If the entity is a discriminant of an extended tagged type used to
482 rename a discriminant of the parent type, return the latter. */
483 if (Is_Tagged_Type (gnat_record
)
484 && Present (Corresponding_Discriminant (gnat_entity
)))
487 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity
),
488 gnu_expr
, definition
);
493 /* If the entity is an inherited component (in the case of extended
494 tagged record types), just return the original entity, which must
495 be a FIELD_DECL. Likewise for discriminants. If the entity is a
496 non-girder discriminant (in the case of derived untagged record
497 types), return the stored discriminant it renames. */
498 else if (Present (Original_Record_Component (gnat_entity
))
499 && Original_Record_Component (gnat_entity
) != gnat_entity
)
502 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity
),
503 gnu_expr
, definition
);
508 /* Otherwise, if we are not defining this and we have no GCC type
509 for the containing record, make one for it. Then we should
510 have made our own equivalent. */
511 else if (!definition
&& !present_gnu_tree (gnat_record
))
513 /* ??? If this is in a record whose scope is a protected
514 type and we have an Original_Record_Component, use it.
515 This is a workaround for major problems in protected type
517 Entity_Id Scop
= Scope (Scope (gnat_entity
));
518 if (Is_Protected_Type (Underlying_Type (Scop
))
519 && Present (Original_Record_Component (gnat_entity
)))
522 = gnat_to_gnu_entity (Original_Record_Component
529 gnat_to_gnu_entity (Scope (gnat_entity
), NULL_TREE
, false);
530 gnu_decl
= get_gnu_tree (gnat_entity
);
536 /* Here we have no GCC type and this is a reference rather than a
537 definition. This should never happen. Most likely the cause is
538 reference before declaration in the GNAT tree for gnat_entity. */
543 /* Ignore constant definitions already marked with the error node. See
544 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
546 && present_gnu_tree (gnat_entity
)
547 && get_gnu_tree (gnat_entity
) == error_mark_node
)
549 maybe_present
= true;
553 /* Ignore deferred constant definitions without address clause since
554 they are processed fully in the front-end. If No_Initialization
555 is set, this is not a deferred constant but a constant whose value
556 is built manually. And constants that are renamings are handled
560 && No (Address_Clause (gnat_entity
))
561 && !No_Initialization (Declaration_Node (gnat_entity
))
562 && No (Renamed_Object (gnat_entity
)))
564 gnu_decl
= error_mark_node
;
569 /* If this is a use of a deferred constant without address clause,
570 get its full definition. */
572 && No (Address_Clause (gnat_entity
))
573 && Present (Full_View (gnat_entity
)))
576 = gnat_to_gnu_entity (Full_View (gnat_entity
), gnu_expr
, false);
581 /* If we have a constant that we are not defining, get the expression it
582 was defined to represent. This is necessary to avoid generating dumb
583 elaboration code in simple cases, but we may throw it away later if it
584 is not a constant. But do not retrieve it if it is an allocator since
585 the designated type might still be dummy at this point. */
587 && !No_Initialization (Declaration_Node (gnat_entity
))
588 && Present (Expression (Declaration_Node (gnat_entity
)))
589 && Nkind (Expression (Declaration_Node (gnat_entity
)))
591 /* The expression may contain N_Expression_With_Actions nodes and
592 thus object declarations from other units. Discard them. */
594 = gnat_to_gnu_external (Expression (Declaration_Node (gnat_entity
)));
596 /* ... fall through ... */
599 case E_Loop_Parameter
:
600 case E_Out_Parameter
:
603 /* Always create a variable for volatile objects and variables seen
604 constant but with a Linker_Section pragma. */
606 = ((kind
== E_Constant
|| kind
== E_Variable
)
607 && Is_True_Constant (gnat_entity
)
608 && !(kind
== E_Variable
609 && Present (Linker_Section_Pragma (gnat_entity
)))
610 && !Treat_As_Volatile (gnat_entity
)
611 && (((Nkind (Declaration_Node (gnat_entity
))
612 == N_Object_Declaration
)
613 && Present (Expression (Declaration_Node (gnat_entity
))))
614 || Present (Renamed_Object (gnat_entity
))
616 bool inner_const_flag
= const_flag
;
617 bool static_flag
= Is_Statically_Allocated (gnat_entity
);
618 /* We implement RM 13.3(19) for exported and imported (non-constant)
619 objects by making them volatile. */
621 = (Treat_As_Volatile (gnat_entity
)
622 || (!const_flag
&& (Is_Exported (gnat_entity
) || imported_p
)));
623 bool mutable_p
= false;
624 bool used_by_ref
= false;
625 tree gnu_ext_name
= NULL_TREE
;
626 tree renamed_obj
= NULL_TREE
;
627 tree gnu_object_size
;
629 /* We need to translate the renamed object even though we are only
630 referencing the renaming. But it may contain a call for which
631 we'll generate a temporary to hold the return value and which
632 is part of the definition of the renaming, so discard it. */
633 if (Present (Renamed_Object (gnat_entity
)) && !definition
)
635 if (kind
== E_Exception
)
636 gnu_expr
= gnat_to_gnu_entity (Renamed_Entity (gnat_entity
),
639 gnu_expr
= gnat_to_gnu_external (Renamed_Object (gnat_entity
));
642 /* Get the type after elaborating the renamed object. */
643 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
645 /* If this is a standard exception definition, then use the standard
646 exception type. This is necessary to make sure that imported and
647 exported views of exceptions are properly merged in LTO mode. */
648 if (TREE_CODE (TYPE_NAME (gnu_type
)) == TYPE_DECL
649 && DECL_NAME (TYPE_NAME (gnu_type
)) == exception_data_name_id
)
650 gnu_type
= except_type_node
;
652 /* For a debug renaming declaration, build a debug-only entity. */
653 if (Present (Debug_Renaming_Link (gnat_entity
)))
655 /* Force a non-null value to make sure the symbol is retained. */
656 tree value
= build1 (INDIRECT_REF
, gnu_type
,
658 build_pointer_type (gnu_type
),
659 integer_minus_one_node
));
660 gnu_decl
= build_decl (input_location
,
661 VAR_DECL
, gnu_entity_name
, gnu_type
);
662 SET_DECL_VALUE_EXPR (gnu_decl
, value
);
663 DECL_HAS_VALUE_EXPR_P (gnu_decl
) = 1;
664 gnat_pushdecl (gnu_decl
, gnat_entity
);
668 /* If this is a loop variable, its type should be the base type.
669 This is because the code for processing a loop determines whether
670 a normal loop end test can be done by comparing the bounds of the
671 loop against those of the base type, which is presumed to be the
672 size used for computation. But this is not correct when the size
673 of the subtype is smaller than the type. */
674 if (kind
== E_Loop_Parameter
)
675 gnu_type
= get_base_type (gnu_type
);
677 /* Reject non-renamed objects whose type is an unconstrained array or
678 any object whose type is a dummy type or void. */
679 if ((TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
680 && No (Renamed_Object (gnat_entity
)))
681 || TYPE_IS_DUMMY_P (gnu_type
)
682 || TREE_CODE (gnu_type
) == VOID_TYPE
)
684 gcc_assert (type_annotate_only
);
687 return error_mark_node
;
690 /* If an alignment is specified, use it if valid. Note that exceptions
691 are objects but don't have an alignment. We must do this before we
692 validate the size, since the alignment can affect the size. */
693 if (kind
!= E_Exception
&& Known_Alignment (gnat_entity
))
695 gcc_assert (Present (Alignment (gnat_entity
)));
697 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
698 TYPE_ALIGN (gnu_type
));
700 /* No point in changing the type if there is an address clause
701 as the final type of the object will be a reference type. */
702 if (Present (Address_Clause (gnat_entity
)))
706 tree orig_type
= gnu_type
;
709 = maybe_pad_type (gnu_type
, NULL_TREE
, align
, gnat_entity
,
710 false, false, definition
, true);
712 /* If a padding record was made, declare it now since it will
713 never be declared otherwise. This is necessary to ensure
714 that its subtrees are properly marked. */
715 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
716 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true,
717 debug_info_p
, gnat_entity
);
721 /* If we are defining the object, see if it has a Size and validate it
722 if so. If we are not defining the object and a Size clause applies,
723 simply retrieve the value. We don't want to ignore the clause and
724 it is expected to have been validated already. Then get the new
727 gnu_size
= validate_size (Esize (gnat_entity
), gnu_type
,
728 gnat_entity
, VAR_DECL
, false,
729 Has_Size_Clause (gnat_entity
));
730 else if (Has_Size_Clause (gnat_entity
))
731 gnu_size
= UI_To_gnu (Esize (gnat_entity
), bitsizetype
);
736 = make_type_from_size (gnu_type
, gnu_size
,
737 Has_Biased_Representation (gnat_entity
));
739 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0))
740 gnu_size
= NULL_TREE
;
743 /* If this object has self-referential size, it must be a record with
744 a default discriminant. We are supposed to allocate an object of
745 the maximum size in this case, unless it is a constant with an
746 initializing expression, in which case we can get the size from
747 that. Note that the resulting size may still be a variable, so
748 this may end up with an indirect allocation. */
749 if (No (Renamed_Object (gnat_entity
))
750 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
752 if (gnu_expr
&& kind
== E_Constant
)
754 tree size
= TYPE_SIZE (TREE_TYPE (gnu_expr
));
755 if (CONTAINS_PLACEHOLDER_P (size
))
757 /* If the initializing expression is itself a constant,
758 despite having a nominal type with self-referential
759 size, we can get the size directly from it. */
760 if (TREE_CODE (gnu_expr
) == COMPONENT_REF
762 (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0)))
763 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == VAR_DECL
764 && (TREE_READONLY (TREE_OPERAND (gnu_expr
, 0))
765 || DECL_READONLY_ONCE_ELAB
766 (TREE_OPERAND (gnu_expr
, 0))))
767 gnu_size
= DECL_SIZE (TREE_OPERAND (gnu_expr
, 0));
770 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, gnu_expr
);
775 /* We may have no GNU_EXPR because No_Initialization is
776 set even though there's an Expression. */
777 else if (kind
== E_Constant
778 && (Nkind (Declaration_Node (gnat_entity
))
779 == N_Object_Declaration
)
780 && Present (Expression (Declaration_Node (gnat_entity
))))
782 = TYPE_SIZE (gnat_to_gnu_type
784 (Expression (Declaration_Node (gnat_entity
)))));
787 gnu_size
= max_size (TYPE_SIZE (gnu_type
), true);
791 /* If we are at global level and the size isn't constant, call
792 elaborate_expression_1 to make a variable for it rather than
793 calculating it each time. */
794 if (global_bindings_p () && !TREE_CONSTANT (gnu_size
))
795 gnu_size
= elaborate_expression_1 (gnu_size
, gnat_entity
,
796 "SIZE", definition
, false);
799 /* If the size is zero byte, make it one byte since some linkers have
800 troubles with zero-sized objects. If the object will have a
801 template, that will make it nonzero so don't bother. Also avoid
802 doing that for an object renaming or an object with an address
803 clause, as we would lose useful information on the view size
804 (e.g. for null array slices) and we are not allocating the object
807 && integer_zerop (gnu_size
)
808 && !TREE_OVERFLOW (gnu_size
))
809 || (TYPE_SIZE (gnu_type
)
810 && integer_zerop (TYPE_SIZE (gnu_type
))
811 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type
))))
812 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
813 && No (Renamed_Object (gnat_entity
))
814 && No (Address_Clause (gnat_entity
)))
815 gnu_size
= bitsize_unit_node
;
817 /* If this is an object with no specified size and alignment, and
818 if either it is atomic or we are not optimizing alignment for
819 space and it is composite and not an exception, an Out parameter
820 or a reference to another object, and the size of its type is a
821 constant, set the alignment to the smallest one which is not
822 smaller than the size, with an appropriate cap. */
823 if (!gnu_size
&& align
== 0
824 && (Is_Atomic_Or_VFA (gnat_entity
)
825 || (!Optimize_Alignment_Space (gnat_entity
)
826 && kind
!= E_Exception
827 && kind
!= E_Out_Parameter
828 && Is_Composite_Type (Etype (gnat_entity
))
829 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
830 && !Is_Exported (gnat_entity
)
832 && No (Renamed_Object (gnat_entity
))
833 && No (Address_Clause (gnat_entity
))))
834 && TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
)
836 unsigned int size_cap
, align_cap
;
838 /* No point in promoting the alignment if this doesn't prevent
839 BLKmode access to the object, in particular block copy, as
840 this will for example disable the NRV optimization for it.
841 No point in jumping through all the hoops needed in order
842 to support BIGGEST_ALIGNMENT if we don't really have to.
843 So we cap to the smallest alignment that corresponds to
844 a known efficient memory access pattern of the target. */
845 if (Is_Atomic_Or_VFA (gnat_entity
))
848 align_cap
= BIGGEST_ALIGNMENT
;
852 size_cap
= MAX_FIXED_MODE_SIZE
;
853 align_cap
= get_mode_alignment (ptr_mode
);
856 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type
))
857 || compare_tree_int (TYPE_SIZE (gnu_type
), size_cap
) > 0)
859 else if (compare_tree_int (TYPE_SIZE (gnu_type
), align_cap
) > 0)
862 align
= ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type
)));
864 /* But make sure not to under-align the object. */
865 if (align
<= TYPE_ALIGN (gnu_type
))
868 /* And honor the minimum valid atomic alignment, if any. */
869 #ifdef MINIMUM_ATOMIC_ALIGNMENT
870 else if (align
< MINIMUM_ATOMIC_ALIGNMENT
)
871 align
= MINIMUM_ATOMIC_ALIGNMENT
;
875 /* If the object is set to have atomic components, find the component
876 type and validate it.
878 ??? Note that we ignore Has_Volatile_Components on objects; it's
879 not at all clear what to do in that case. */
880 if (Has_Atomic_Components (gnat_entity
))
882 tree gnu_inner
= (TREE_CODE (gnu_type
) == ARRAY_TYPE
883 ? TREE_TYPE (gnu_type
) : gnu_type
);
885 while (TREE_CODE (gnu_inner
) == ARRAY_TYPE
886 && TYPE_MULTI_ARRAY_P (gnu_inner
))
887 gnu_inner
= TREE_TYPE (gnu_inner
);
889 check_ok_for_atomic_type (gnu_inner
, gnat_entity
, true);
892 /* If this is an aliased object with an unconstrained array nominal
893 subtype, make a type that includes the template. We will either
894 allocate or create a variable of that type, see below. */
895 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
896 && Is_Array_Type (Underlying_Type (Etype (gnat_entity
)))
897 && !type_annotate_only
)
900 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
902 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array
),
904 concat_name (gnu_entity_name
,
909 /* ??? If this is an object of CW type initialized to a value, try to
910 ensure that the object is sufficient aligned for this value, but
911 without pessimizing the allocation. This is a kludge necessary
912 because we don't support dynamic alignment. */
914 && Ekind (Etype (gnat_entity
)) == E_Class_Wide_Subtype
915 && No (Renamed_Object (gnat_entity
))
916 && No (Address_Clause (gnat_entity
)))
917 align
= get_target_system_allocator_alignment () * BITS_PER_UNIT
;
919 #ifdef MINIMUM_ATOMIC_ALIGNMENT
920 /* If the size is a constant and no alignment is specified, force
921 the alignment to be the minimum valid atomic alignment. The
922 restriction on constant size avoids problems with variable-size
923 temporaries; if the size is variable, there's no issue with
924 atomic access. Also don't do this for a constant, since it isn't
925 necessary and can interfere with constant replacement. Finally,
926 do not do it for Out parameters since that creates an
927 size inconsistency with In parameters. */
929 && MINIMUM_ATOMIC_ALIGNMENT
> TYPE_ALIGN (gnu_type
)
930 && !FLOAT_TYPE_P (gnu_type
)
931 && !const_flag
&& No (Renamed_Object (gnat_entity
))
932 && !imported_p
&& No (Address_Clause (gnat_entity
))
933 && kind
!= E_Out_Parameter
934 && (gnu_size
? TREE_CODE (gnu_size
) == INTEGER_CST
935 : TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
))
936 align
= MINIMUM_ATOMIC_ALIGNMENT
;
939 /* Make a new type with the desired size and alignment, if needed.
940 But do not take into account alignment promotions to compute the
941 size of the object. */
942 gnu_object_size
= gnu_size
? gnu_size
: TYPE_SIZE (gnu_type
);
943 if (gnu_size
|| align
> 0)
945 tree orig_type
= gnu_type
;
947 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
948 false, false, definition
, true);
950 /* If a padding record was made, declare it now since it will
951 never be declared otherwise. This is necessary to ensure
952 that its subtrees are properly marked. */
953 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
954 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true,
955 debug_info_p
, gnat_entity
);
958 /* Now check if the type of the object allows atomic access. */
959 if (Is_Atomic_Or_VFA (gnat_entity
))
960 check_ok_for_atomic_type (gnu_type
, gnat_entity
, false);
962 /* If this is a renaming, avoid as much as possible to create a new
963 object. However, in some cases, creating it is required because
964 renaming can be applied to objects that are not names in Ada.
965 This processing needs to be applied to the raw expression so as
966 to make it more likely to rename the underlying object. */
967 if (Present (Renamed_Object (gnat_entity
)))
969 /* If the renamed object had padding, strip off the reference to
970 the inner object and reset our type. */
971 if ((TREE_CODE (gnu_expr
) == COMPONENT_REF
972 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0))))
973 /* Strip useless conversions around the object. */
974 || gnat_useless_type_conversion (gnu_expr
))
976 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
977 gnu_type
= TREE_TYPE (gnu_expr
);
980 /* Or else, if the renamed object has an unconstrained type with
981 default discriminant, use the padded type. */
982 else if (type_is_padding_self_referential (TREE_TYPE (gnu_expr
)))
983 gnu_type
= TREE_TYPE (gnu_expr
);
985 /* Case 1: if this is a constant renaming stemming from a function
986 call, treat it as a normal object whose initial value is what
987 is being renamed. RM 3.3 says that the result of evaluating a
988 function call is a constant object. Therefore, it can be the
989 inner object of a constant renaming and the renaming must be
990 fully instantiated, i.e. it cannot be a reference to (part of)
991 an existing object. And treat other rvalues (addresses, null
992 expressions, constructors and literals) the same way. */
993 tree inner
= gnu_expr
;
994 while (handled_component_p (inner
) || CONVERT_EXPR_P (inner
))
995 inner
= TREE_OPERAND (inner
, 0);
996 /* Expand_Dispatching_Call can prepend a comparison of the tags
997 before the call to "=". */
998 if (TREE_CODE (inner
) == TRUTH_ANDIF_EXPR
999 || TREE_CODE (inner
) == COMPOUND_EXPR
)
1000 inner
= TREE_OPERAND (inner
, 1);
1001 if ((TREE_CODE (inner
) == CALL_EXPR
1002 && !call_is_atomic_load (inner
))
1003 || TREE_CODE (inner
) == ADDR_EXPR
1004 || TREE_CODE (inner
) == NULL_EXPR
1005 || TREE_CODE (inner
) == CONSTRUCTOR
1006 || CONSTANT_CLASS_P (inner
)
1007 /* We need to detect the case where a temporary is created to
1008 hold the return value, since we cannot safely rename it at
1009 top level as it lives only in the elaboration routine. */
1010 || (TREE_CODE (inner
) == VAR_DECL
1011 && DECL_RETURN_VALUE_P (inner
))
1012 /* We also need to detect the case where the front-end creates
1013 a dangling 'reference to a function call at top level and
1014 substitutes it in the renaming, for example:
1016 q__b : boolean renames r__f.e (1);
1018 can be rewritten into:
1020 q__R1s : constant q__A2s := r__f'reference;
1022 q__b : boolean renames q__R1s.all.e (1);
1024 We cannot safely rename the rewritten expression since the
1025 underlying object lives only in the elaboration routine. */
1026 || (TREE_CODE (inner
) == INDIRECT_REF
1028 = remove_conversions (TREE_OPERAND (inner
, 0), true))
1029 && TREE_CODE (inner
) == VAR_DECL
1030 && DECL_RETURN_VALUE_P (inner
)))
1033 /* Case 2: if the renaming entity need not be materialized, use
1034 the elaborated renamed expression for the renaming. But this
1035 means that the caller is responsible for evaluating the address
1036 of the renaming in the correct place for the definition case to
1037 instantiate the SAVE_EXPRs. */
1038 else if (!Materialize_Entity (gnat_entity
))
1040 tree init
= NULL_TREE
;
1043 = elaborate_reference (gnu_expr
, gnat_entity
, definition
,
1046 /* We cannot evaluate the first arm of a COMPOUND_EXPR in the
1047 correct place for this case. */
1050 /* No DECL_EXPR will be created so the expression needs to be
1051 marked manually because it will likely be shared. */
1052 if (global_bindings_p ())
1053 MARK_VISITED (gnu_decl
);
1055 /* This assertion will fail if the renamed object isn't aligned
1056 enough as to make it possible to honor the alignment set on
1060 unsigned int ralign
= DECL_P (gnu_decl
)
1061 ? DECL_ALIGN (gnu_decl
)
1062 : TYPE_ALIGN (TREE_TYPE (gnu_decl
));
1063 gcc_assert (ralign
>= align
);
1066 save_gnu_tree (gnat_entity
, gnu_decl
, true);
1068 annotate_object (gnat_entity
, gnu_type
, NULL_TREE
, false);
1072 /* Case 3: otherwise, make a constant pointer to the object we
1073 are renaming and attach the object to the pointer after it is
1074 elaborated. The object will be referenced directly instead
1075 of indirectly via the pointer to avoid aliasing problems with
1076 non-addressable entities. The pointer is called a "renaming"
1077 pointer in this case. Note that we also need to preserve the
1078 volatility of the renamed object through the indirection. */
1081 tree init
= NULL_TREE
;
1083 if (TREE_THIS_VOLATILE (gnu_expr
) && !TYPE_VOLATILE (gnu_type
))
1085 = change_qualified_type (gnu_type
, TYPE_QUAL_VOLATILE
);
1086 gnu_type
= build_reference_type (gnu_type
);
1089 volatile_flag
= false;
1090 inner_const_flag
= TREE_READONLY (gnu_expr
);
1091 gnu_size
= NULL_TREE
;
1094 = elaborate_reference (gnu_expr
, gnat_entity
, definition
,
1097 /* The expression needs to be marked manually because it will
1098 likely be shared, even for a definition since the ADDR_EXPR
1099 built below can cause the first few nodes to be folded. */
1100 if (global_bindings_p ())
1101 MARK_VISITED (renamed_obj
);
1103 if (type_annotate_only
1104 && TREE_CODE (renamed_obj
) == ERROR_MARK
)
1105 gnu_expr
= NULL_TREE
;
1109 = build_unary_op (ADDR_EXPR
, gnu_type
, renamed_obj
);
1112 = build_compound_expr (TREE_TYPE (gnu_expr
), init
,
1118 /* If we are defining an aliased object whose nominal subtype is
1119 unconstrained, the object is a record that contains both the
1120 template and the object. If there is an initializer, it will
1121 have already been converted to the right type, but we need to
1122 create the template if there is no initializer. */
1125 && TREE_CODE (gnu_type
) == RECORD_TYPE
1126 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type
)
1127 /* Beware that padding might have been introduced above. */
1128 || (TYPE_PADDING_P (gnu_type
)
1129 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1131 && TYPE_CONTAINS_TEMPLATE_P
1132 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))))
1135 = TYPE_PADDING_P (gnu_type
)
1136 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1137 : TYPE_FIELDS (gnu_type
);
1138 vec
<constructor_elt
, va_gc
> *v
;
1140 tree t
= build_template (TREE_TYPE (template_field
),
1141 TREE_TYPE (DECL_CHAIN (template_field
)),
1143 CONSTRUCTOR_APPEND_ELT (v
, template_field
, t
);
1144 gnu_expr
= gnat_build_constructor (gnu_type
, v
);
1147 /* Convert the expression to the type of the object if need be. */
1148 if (gnu_expr
&& initial_value_needs_conversion (gnu_type
, gnu_expr
))
1149 gnu_expr
= convert (gnu_type
, gnu_expr
);
1151 /* If this is a pointer that doesn't have an initializing expression,
1152 initialize it to NULL, unless the object is declared imported as
1155 && (POINTER_TYPE_P (gnu_type
) || TYPE_IS_FAT_POINTER_P (gnu_type
))
1157 && !Is_Imported (gnat_entity
))
1158 gnu_expr
= integer_zero_node
;
1160 /* If we are defining the object and it has an Address clause, we must
1161 either get the address expression from the saved GCC tree for the
1162 object if it has a Freeze node, or elaborate the address expression
1163 here since the front-end has guaranteed that the elaboration has no
1164 effects in this case. */
1165 if (definition
&& Present (Address_Clause (gnat_entity
)))
1167 const Node_Id gnat_clause
= Address_Clause (gnat_entity
);
1168 Node_Id gnat_address
= Expression (gnat_clause
);
1170 = present_gnu_tree (gnat_entity
)
1171 ? get_gnu_tree (gnat_entity
) : gnat_to_gnu (gnat_address
);
1173 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
1175 /* Convert the type of the object to a reference type that can
1176 alias everything as per RM 13.3(19). */
1177 if (volatile_flag
&& !TYPE_VOLATILE (gnu_type
))
1178 gnu_type
= change_qualified_type (gnu_type
, TYPE_QUAL_VOLATILE
);
1180 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1181 gnu_address
= convert (gnu_type
, gnu_address
);
1184 = (!Is_Public (gnat_entity
)
1185 || compile_time_known_address_p (gnat_address
));
1186 volatile_flag
= false;
1187 gnu_size
= NULL_TREE
;
1189 /* If this is an aliased object with an unconstrained array nominal
1190 subtype, then it can overlay only another aliased object with an
1191 unconstrained array nominal subtype and compatible template. */
1192 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
1193 && Is_Array_Type (Underlying_Type (Etype (gnat_entity
)))
1194 && !type_annotate_only
)
1196 tree rec_type
= TREE_TYPE (gnu_type
);
1197 tree off
= byte_position (DECL_CHAIN (TYPE_FIELDS (rec_type
)));
1199 /* This is the pattern built for a regular object. */
1200 if (TREE_CODE (gnu_address
) == POINTER_PLUS_EXPR
1201 && TREE_OPERAND (gnu_address
, 1) == off
)
1202 gnu_address
= TREE_OPERAND (gnu_address
, 0);
1203 /* This is the pattern built for an overaligned object. */
1204 else if (TREE_CODE (gnu_address
) == POINTER_PLUS_EXPR
1205 && TREE_CODE (TREE_OPERAND (gnu_address
, 1))
1207 && TREE_OPERAND (TREE_OPERAND (gnu_address
, 1), 1)
1210 = build2 (POINTER_PLUS_EXPR
, gnu_type
,
1211 TREE_OPERAND (gnu_address
, 0),
1212 TREE_OPERAND (TREE_OPERAND (gnu_address
, 1), 0));
1215 post_error_ne ("aliased object& with unconstrained array "
1216 "nominal subtype", gnat_clause
,
1218 post_error ("\\can overlay only aliased object with "
1219 "compatible subtype", gnat_clause
);
1223 /* If we don't have an initializing expression for the underlying
1224 variable, the initializing expression for the pointer is the
1225 specified address. Otherwise, we have to make a COMPOUND_EXPR
1226 to assign both the address and the initial value. */
1228 gnu_expr
= gnu_address
;
1231 = build2 (COMPOUND_EXPR
, gnu_type
,
1232 build_binary_op (INIT_EXPR
, NULL_TREE
,
1233 build_unary_op (INDIRECT_REF
,
1240 /* If it has an address clause and we are not defining it, mark it
1241 as an indirect object. Likewise for Stdcall objects that are
1243 if ((!definition
&& Present (Address_Clause (gnat_entity
)))
1244 || (imported_p
&& Has_Stdcall_Convention (gnat_entity
)))
1246 /* Convert the type of the object to a reference type that can
1247 alias everything as per RM 13.3(19). */
1248 if (volatile_flag
&& !TYPE_VOLATILE (gnu_type
))
1249 gnu_type
= change_qualified_type (gnu_type
, TYPE_QUAL_VOLATILE
);
1251 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1254 volatile_flag
= false;
1255 gnu_size
= NULL_TREE
;
1257 /* No point in taking the address of an initializing expression
1258 that isn't going to be used. */
1259 gnu_expr
= NULL_TREE
;
1261 /* If it has an address clause whose value is known at compile
1262 time, make the object a CONST_DECL. This will avoid a
1263 useless dereference. */
1264 if (Present (Address_Clause (gnat_entity
)))
1266 Node_Id gnat_address
1267 = Expression (Address_Clause (gnat_entity
));
1269 if (compile_time_known_address_p (gnat_address
))
1271 gnu_expr
= gnat_to_gnu (gnat_address
);
1277 /* If we are at top level and this object is of variable size,
1278 make the actual type a hidden pointer to the real type and
1279 make the initializer be a memory allocation and initialization.
1280 Likewise for objects we aren't defining (presumed to be
1281 external references from other packages), but there we do
1282 not set up an initialization.
1284 If the object's size overflows, make an allocator too, so that
1285 Storage_Error gets raised. Note that we will never free
1286 such memory, so we presume it never will get allocated. */
1287 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type
),
1288 global_bindings_p ()
1292 && !allocatable_size_p (convert (sizetype
,
1294 (CEIL_DIV_EXPR
, gnu_size
,
1295 bitsize_unit_node
)),
1296 global_bindings_p ()
1300 if (volatile_flag
&& !TYPE_VOLATILE (gnu_type
))
1301 gnu_type
= change_qualified_type (gnu_type
, TYPE_QUAL_VOLATILE
);
1302 gnu_type
= build_reference_type (gnu_type
);
1305 volatile_flag
= false;
1306 gnu_size
= NULL_TREE
;
1308 /* In case this was a aliased object whose nominal subtype is
1309 unconstrained, the pointer above will be a thin pointer and
1310 build_allocator will automatically make the template.
1312 If we have a template initializer only (that we made above),
1313 pretend there is none and rely on what build_allocator creates
1314 again anyway. Otherwise (if we have a full initializer), get
1315 the data part and feed that to build_allocator.
1317 If we are elaborating a mutable object, tell build_allocator to
1318 ignore a possibly simpler size from the initializer, if any, as
1319 we must allocate the maximum possible size in this case. */
1320 if (definition
&& !imported_p
)
1322 tree gnu_alloc_type
= TREE_TYPE (gnu_type
);
1324 if (TREE_CODE (gnu_alloc_type
) == RECORD_TYPE
1325 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type
))
1328 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type
)));
1330 if (TREE_CODE (gnu_expr
) == CONSTRUCTOR
1331 && vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr
)) == 1)
1332 gnu_expr
= NULL_TREE
;
1335 = build_component_ref
1337 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr
))),
1341 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type
)) == INTEGER_CST
1342 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type
)))
1343 post_error ("?`Storage_Error` will be raised at run time!",
1347 = build_allocator (gnu_alloc_type
, gnu_expr
, gnu_type
,
1348 Empty
, Empty
, gnat_entity
, mutable_p
);
1351 gnu_expr
= NULL_TREE
;
1354 /* If this object would go into the stack and has an alignment larger
1355 than the largest stack alignment the back-end can honor, resort to
1356 a variable of "aligning type". */
1358 && !global_bindings_p ()
1361 && TYPE_ALIGN (gnu_type
) > BIGGEST_ALIGNMENT
)
1363 /* Create the new variable. No need for extra room before the
1364 aligned field as this is in automatic storage. */
1366 = make_aligning_type (gnu_type
, TYPE_ALIGN (gnu_type
),
1367 TYPE_SIZE_UNIT (gnu_type
),
1368 BIGGEST_ALIGNMENT
, 0, gnat_entity
);
1370 = create_var_decl (create_concat_name (gnat_entity
, "ALIGN"),
1371 NULL_TREE
, gnu_new_type
, NULL_TREE
,
1372 false, false, false, false, false,
1373 true, debug_info_p
, NULL
, gnat_entity
);
1375 /* Initialize the aligned field if we have an initializer. */
1378 (build_binary_op (INIT_EXPR
, NULL_TREE
,
1380 (gnu_new_var
, TYPE_FIELDS (gnu_new_type
),
1385 /* And setup this entity as a reference to the aligned field. */
1386 gnu_type
= build_reference_type (gnu_type
);
1389 (ADDR_EXPR
, NULL_TREE
,
1390 build_component_ref (gnu_new_var
, TYPE_FIELDS (gnu_new_type
),
1392 TREE_CONSTANT (gnu_expr
) = 1;
1396 volatile_flag
= false;
1397 gnu_size
= NULL_TREE
;
1400 /* If this is an aliased object with an unconstrained array nominal
1401 subtype, we make its type a thin reference, i.e. the reference
1402 counterpart of a thin pointer, so it points to the array part.
1403 This is aimed to make it easier for the debugger to decode the
1404 object. Note that we have to do it this late because of the
1405 couple of allocation adjustments that might be made above. */
1406 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
1407 && Is_Array_Type (Underlying_Type (Etype (gnat_entity
)))
1408 && !type_annotate_only
)
1410 /* In case the object with the template has already been allocated
1411 just above, we have nothing to do here. */
1412 if (!TYPE_IS_THIN_POINTER_P (gnu_type
))
1414 /* This variable is a GNAT encoding used by Workbench: let it
1415 go through the debugging information but mark it as
1416 artificial: users are not interested in it. */
1418 = create_var_decl (concat_name (gnu_entity_name
, "UNC"),
1419 NULL_TREE
, gnu_type
, gnu_expr
,
1420 const_flag
, Is_Public (gnat_entity
),
1421 imported_p
|| !definition
, static_flag
,
1422 volatile_flag
, true, debug_info_p
,
1424 gnu_expr
= build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_unc_var
);
1425 TREE_CONSTANT (gnu_expr
) = 1;
1429 volatile_flag
= false;
1430 inner_const_flag
= TREE_READONLY (gnu_unc_var
);
1431 gnu_size
= NULL_TREE
;
1435 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
1437 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array
));
1440 /* Convert the expression to the type of the object if need be. */
1441 if (gnu_expr
&& initial_value_needs_conversion (gnu_type
, gnu_expr
))
1442 gnu_expr
= convert (gnu_type
, gnu_expr
);
1444 /* If this name is external or a name was specified, use it, but don't
1445 use the Interface_Name with an address clause (see cd30005). */
1446 if ((Is_Public (gnat_entity
) && !Is_Imported (gnat_entity
))
1447 || (Present (Interface_Name (gnat_entity
))
1448 && No (Address_Clause (gnat_entity
))))
1449 gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
1451 /* If this is an aggregate constant initialized to a constant, force it
1452 to be statically allocated. This saves an initialization copy. */
1455 && gnu_expr
&& TREE_CONSTANT (gnu_expr
)
1456 && AGGREGATE_TYPE_P (gnu_type
)
1457 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type
))
1458 && !(TYPE_IS_PADDING_P (gnu_type
)
1459 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1460 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))))
1463 /* Deal with a pragma Linker_Section on a constant or variable. */
1464 if ((kind
== E_Constant
|| kind
== E_Variable
)
1465 && Present (Linker_Section_Pragma (gnat_entity
)))
1466 prepend_one_attribute_pragma (&attr_list
,
1467 Linker_Section_Pragma (gnat_entity
));
1469 /* Now create the variable or the constant and set various flags. */
1471 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
1472 gnu_expr
, const_flag
, Is_Public (gnat_entity
),
1473 imported_p
|| !definition
, static_flag
,
1474 volatile_flag
, artificial_p
, debug_info_p
,
1475 attr_list
, gnat_entity
, !renamed_obj
);
1476 DECL_BY_REF_P (gnu_decl
) = used_by_ref
;
1477 DECL_POINTS_TO_READONLY_P (gnu_decl
) = used_by_ref
&& inner_const_flag
;
1478 DECL_CAN_NEVER_BE_NULL_P (gnu_decl
) = Can_Never_Be_Null (gnat_entity
);
1480 /* If we are defining an Out parameter and optimization isn't enabled,
1481 create a fake PARM_DECL for debugging purposes and make it point to
1482 the VAR_DECL. Suppress debug info for the latter but make sure it
1483 will live in memory so that it can be accessed from within the
1484 debugger through the PARM_DECL. */
1485 if (kind
== E_Out_Parameter
1489 && !flag_generate_lto
)
1491 tree param
= create_param_decl (gnu_entity_name
, gnu_type
);
1492 gnat_pushdecl (param
, gnat_entity
);
1493 SET_DECL_VALUE_EXPR (param
, gnu_decl
);
1494 DECL_HAS_VALUE_EXPR_P (param
) = 1;
1495 DECL_IGNORED_P (gnu_decl
) = 1;
1496 TREE_ADDRESSABLE (gnu_decl
) = 1;
1499 /* If this is a loop parameter, set the corresponding flag. */
1500 else if (kind
== E_Loop_Parameter
)
1501 DECL_LOOP_PARM_P (gnu_decl
) = 1;
1503 /* If this is a renaming pointer, attach the renamed object to it. */
1505 SET_DECL_RENAMED_OBJECT (gnu_decl
, renamed_obj
);
1507 /* If this is a constant and we are defining it or it generates a real
1508 symbol at the object level and we are referencing it, we may want
1509 or need to have a true variable to represent it:
1510 - if optimization isn't enabled, for debugging purposes,
1511 - if the constant is public and not overlaid on something else,
1512 - if its address is taken,
1513 - if either itself or its type is aliased. */
1514 if (TREE_CODE (gnu_decl
) == CONST_DECL
1515 && (definition
|| Sloc (gnat_entity
) > Standard_Location
)
1516 && ((!optimize
&& debug_info_p
)
1517 || (Is_Public (gnat_entity
)
1518 && No (Address_Clause (gnat_entity
)))
1519 || Address_Taken (gnat_entity
)
1520 || Is_Aliased (gnat_entity
)
1521 || Is_Aliased (Etype (gnat_entity
))))
1524 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
1525 gnu_expr
, true, Is_Public (gnat_entity
),
1526 !definition
, static_flag
, volatile_flag
,
1527 artificial_p
, debug_info_p
, attr_list
,
1528 gnat_entity
, false);
1530 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl
, gnu_corr_var
);
1533 /* If this is a constant, even if we don't need a true variable, we
1534 may need to avoid returning the initializer in every case. That
1535 can happen for the address of a (constant) constructor because,
1536 upon dereferencing it, the constructor will be reinjected in the
1537 tree, which may not be valid in every case; see lvalue_required_p
1538 for more details. */
1539 if (TREE_CODE (gnu_decl
) == CONST_DECL
)
1540 DECL_CONST_ADDRESS_P (gnu_decl
) = constructor_address_p (gnu_expr
);
1542 /* If this object is declared in a block that contains a block with an
1543 exception handler, and we aren't using the GCC exception mechanism,
1544 we must force this variable in memory in order to avoid an invalid
1546 if (Front_End_Exceptions ()
1547 && Has_Nested_Block_With_Handler (Scope (gnat_entity
)))
1548 TREE_ADDRESSABLE (gnu_decl
) = 1;
1550 /* If this is a local variable with non-BLKmode and aggregate type,
1551 and optimization isn't enabled, then force it in memory so that
1552 a register won't be allocated to it with possible subparts left
1553 uninitialized and reaching the register allocator. */
1554 else if (TREE_CODE (gnu_decl
) == VAR_DECL
1555 && !DECL_EXTERNAL (gnu_decl
)
1556 && !TREE_STATIC (gnu_decl
)
1557 && DECL_MODE (gnu_decl
) != BLKmode
1558 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl
))
1559 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl
))
1561 TREE_ADDRESSABLE (gnu_decl
) = 1;
1563 /* If we are defining an object with variable size or an object with
1564 fixed size that will be dynamically allocated, and we are using the
1565 front-end setjmp/longjmp exception mechanism, update the setjmp
1568 && Exception_Mechanism
== Front_End_SJLJ
1569 && get_block_jmpbuf_decl ()
1570 && DECL_SIZE_UNIT (gnu_decl
)
1571 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl
)) != INTEGER_CST
1572 || (flag_stack_check
== GENERIC_STACK_CHECK
1573 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl
),
1574 STACK_CHECK_MAX_VAR_SIZE
) > 0)))
1575 add_stmt_with_node (build_call_n_expr
1576 (update_setjmp_buf_decl
, 1,
1577 build_unary_op (ADDR_EXPR
, NULL_TREE
,
1578 get_block_jmpbuf_decl ())),
1581 /* Back-annotate Esize and Alignment of the object if not already
1582 known. Note that we pick the values of the type, not those of
1583 the object, to shield ourselves from low-level platform-dependent
1584 adjustments like alignment promotion. This is both consistent with
1585 all the treatment above, where alignment and size are set on the
1586 type of the object and not on the object directly, and makes it
1587 possible to support all confirming representation clauses. */
1588 annotate_object (gnat_entity
, TREE_TYPE (gnu_decl
), gnu_object_size
,
1594 /* Return a TYPE_DECL for "void" that we previously made. */
1595 gnu_decl
= TYPE_NAME (void_type_node
);
1598 case E_Enumeration_Type
:
1599 /* A special case: for the types Character and Wide_Character in
1600 Standard, we do not list all the literals. So if the literals
1601 are not specified, make this an integer type. */
1602 if (No (First_Literal (gnat_entity
)))
1604 if (esize
== CHAR_TYPE_SIZE
&& flag_signed_char
)
1605 gnu_type
= make_signed_type (CHAR_TYPE_SIZE
);
1607 gnu_type
= make_unsigned_type (esize
);
1608 TYPE_NAME (gnu_type
) = gnu_entity_name
;
1610 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1611 This is needed by the DWARF-2 back-end to distinguish between
1612 unsigned integer types and character types. */
1613 TYPE_STRING_FLAG (gnu_type
) = 1;
1615 /* This flag is needed by the call just below. */
1616 TYPE_ARTIFICIAL (gnu_type
) = artificial_p
;
1618 finish_character_type (gnu_type
);
1622 /* We have a list of enumeral constants in First_Literal. We make a
1623 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1624 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1625 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1626 value of the literal. But when we have a regular boolean type, we
1627 simplify this a little by using a BOOLEAN_TYPE. */
1628 const bool is_boolean
= Is_Boolean_Type (gnat_entity
)
1629 && !Has_Non_Standard_Rep (gnat_entity
);
1630 const bool is_unsigned
= Is_Unsigned_Type (gnat_entity
);
1631 tree gnu_list
= NULL_TREE
;
1632 Entity_Id gnat_literal
;
1634 gnu_type
= make_node (is_boolean
? BOOLEAN_TYPE
: ENUMERAL_TYPE
);
1635 TYPE_PRECISION (gnu_type
) = esize
;
1636 TYPE_UNSIGNED (gnu_type
) = is_unsigned
;
1637 set_min_and_max_values_for_integral_type (gnu_type
, esize
,
1638 TYPE_SIGN (gnu_type
));
1639 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
1640 layout_type (gnu_type
);
1642 for (gnat_literal
= First_Literal (gnat_entity
);
1643 Present (gnat_literal
);
1644 gnat_literal
= Next_Literal (gnat_literal
))
1647 = UI_To_gnu (Enumeration_Rep (gnat_literal
), gnu_type
);
1648 /* Do not generate debug info for individual enumerators. */
1650 = create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
1651 gnu_type
, gnu_value
, true, false, false,
1652 false, false, artificial_p
, false,
1653 NULL
, gnat_literal
);
1654 save_gnu_tree (gnat_literal
, gnu_literal
, false);
1656 = tree_cons (DECL_NAME (gnu_literal
), gnu_value
, gnu_list
);
1660 TYPE_VALUES (gnu_type
) = nreverse (gnu_list
);
1662 /* Note that the bounds are updated at the end of this function
1663 to avoid an infinite recursion since they refer to the type. */
1668 case E_Signed_Integer_Type
:
1669 /* For integer types, just make a signed type the appropriate number
1671 gnu_type
= make_signed_type (esize
);
1674 case E_Ordinary_Fixed_Point_Type
:
1675 case E_Decimal_Fixed_Point_Type
:
1677 /* Small_Value is the scale factor. */
1678 const Ureal gnat_small_value
= Small_Value (gnat_entity
);
1679 tree scale_factor
= NULL_TREE
;
1681 gnu_type
= make_signed_type (esize
);
1683 /* Try to decode the scale factor and to save it for the fixed-point
1684 types debug hook. */
1686 /* There are various ways to describe the scale factor, however there
1687 are cases where back-end internals cannot hold it. In such cases,
1688 we output invalid scale factor for such cases (i.e. the 0/0
1689 rational constant) but we expect GNAT to output GNAT encodings,
1690 then. Thus, keep this in sync with
1691 Exp_Dbug.Is_Handled_Scale_Factor. */
1693 /* When encoded as 1/2**N or 1/10**N, describe the scale factor as a
1694 binary or decimal scale: it is easier to read for humans. */
1695 if (UI_Eq (Numerator (gnat_small_value
), Uint_1
)
1696 && (Rbase (gnat_small_value
) == 2
1697 || Rbase (gnat_small_value
) == 10))
1699 /* Given RM restrictions on 'Small values, we assume here that
1700 the denominator fits in an int. */
1701 const tree base
= build_int_cst (integer_type_node
,
1702 Rbase (gnat_small_value
));
1704 = build_int_cst (integer_type_node
,
1705 UI_To_Int (Denominator (gnat_small_value
)));
1707 = build2 (RDIV_EXPR
, integer_type_node
,
1709 build2 (POWER_EXPR
, integer_type_node
,
1713 /* Default to arbitrary scale factors descriptions. */
1716 const Uint num
= Norm_Num (gnat_small_value
);
1717 const Uint den
= Norm_Den (gnat_small_value
);
1719 if (UI_Is_In_Int_Range (num
) && UI_Is_In_Int_Range (den
))
1722 = build_int_cst (integer_type_node
,
1723 UI_To_Int (Norm_Num (gnat_small_value
)));
1725 = build_int_cst (integer_type_node
,
1726 UI_To_Int (Norm_Den (gnat_small_value
)));
1727 scale_factor
= build2 (RDIV_EXPR
, integer_type_node
,
1731 /* If compiler internals cannot represent arbitrary scale
1732 factors, output an invalid scale factor so that debugger
1733 don't try to handle them but so that we still have a type
1734 in the output. Note that GNAT */
1735 scale_factor
= integer_zero_node
;
1738 TYPE_FIXED_POINT_P (gnu_type
) = 1;
1739 SET_TYPE_SCALE_FACTOR (gnu_type
, scale_factor
);
1743 case E_Modular_Integer_Type
:
1745 /* For modular types, make the unsigned type of the proper number
1746 of bits and then set up the modulus, if required. */
1747 tree gnu_modulus
, gnu_high
= NULL_TREE
;
1749 /* Packed Array Impl. Types are supposed to be subtypes only. */
1750 gcc_assert (!Is_Packed_Array_Impl_Type (gnat_entity
));
1752 gnu_type
= make_unsigned_type (esize
);
1754 /* Get the modulus in this type. If it overflows, assume it is because
1755 it is equal to 2**Esize. Note that there is no overflow checking
1756 done on unsigned type, so we detect the overflow by looking for
1757 a modulus of zero, which is otherwise invalid. */
1758 gnu_modulus
= UI_To_gnu (Modulus (gnat_entity
), gnu_type
);
1760 if (!integer_zerop (gnu_modulus
))
1762 TYPE_MODULAR_P (gnu_type
) = 1;
1763 SET_TYPE_MODULUS (gnu_type
, gnu_modulus
);
1764 gnu_high
= fold_build2 (MINUS_EXPR
, gnu_type
, gnu_modulus
,
1765 build_int_cst (gnu_type
, 1));
1768 /* If the upper bound is not maximal, make an extra subtype. */
1770 && !tree_int_cst_equal (gnu_high
, TYPE_MAX_VALUE (gnu_type
)))
1772 tree gnu_subtype
= make_unsigned_type (esize
);
1773 SET_TYPE_RM_MAX_VALUE (gnu_subtype
, gnu_high
);
1774 TREE_TYPE (gnu_subtype
) = gnu_type
;
1775 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
1776 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "UMT");
1777 gnu_type
= gnu_subtype
;
1782 case E_Signed_Integer_Subtype
:
1783 case E_Enumeration_Subtype
:
1784 case E_Modular_Integer_Subtype
:
1785 case E_Ordinary_Fixed_Point_Subtype
:
1786 case E_Decimal_Fixed_Point_Subtype
:
1788 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1789 not want to call create_range_type since we would like each subtype
1790 node to be distinct. ??? Historically this was in preparation for
1791 when memory aliasing is implemented, but that's obsolete now given
1792 the call to relate_alias_sets below.
1794 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1795 this fact is used by the arithmetic conversion functions.
1797 We elaborate the Ancestor_Subtype if it is not in the current unit
1798 and one of our bounds is non-static. We do this to ensure consistent
1799 naming in the case where several subtypes share the same bounds, by
1800 elaborating the first such subtype first, thus using its name. */
1803 && Present (Ancestor_Subtype (gnat_entity
))
1804 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1805 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1806 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
1807 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
), gnu_expr
, false);
1809 /* Set the precision to the Esize except for bit-packed arrays. */
1810 if (Is_Packed_Array_Impl_Type (gnat_entity
)
1811 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1812 esize
= UI_To_Int (RM_Size (gnat_entity
));
1814 /* First subtypes of Character are treated as Character; otherwise
1815 this should be an unsigned type if the base type is unsigned or
1816 if the lower bound is constant and non-negative or if the type
1817 is biased. However, even if the lower bound is constant and
1818 non-negative, we use a signed type for a subtype with the same
1819 size as its signed base type, because this eliminates useless
1820 conversions to it and gives more leeway to the optimizer; but
1821 this means that we will need to explicitly test for this case
1822 when we change the representation based on the RM size. */
1823 if (kind
== E_Enumeration_Subtype
1824 && No (First_Literal (Etype (gnat_entity
)))
1825 && Esize (gnat_entity
) == RM_Size (gnat_entity
)
1826 && esize
== CHAR_TYPE_SIZE
1827 && flag_signed_char
)
1828 gnu_type
= make_signed_type (CHAR_TYPE_SIZE
);
1829 else if (Is_Unsigned_Type (Etype (gnat_entity
))
1830 || (Esize (Etype (gnat_entity
)) != Esize (gnat_entity
)
1831 && Is_Unsigned_Type (gnat_entity
))
1832 || Has_Biased_Representation (gnat_entity
))
1833 gnu_type
= make_unsigned_type (esize
);
1835 gnu_type
= make_signed_type (esize
);
1836 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
1838 SET_TYPE_RM_MIN_VALUE
1839 (gnu_type
, elaborate_expression (Type_Low_Bound (gnat_entity
),
1840 gnat_entity
, "L", definition
, true,
1843 SET_TYPE_RM_MAX_VALUE
1844 (gnu_type
, elaborate_expression (Type_High_Bound (gnat_entity
),
1845 gnat_entity
, "U", definition
, true,
1848 TYPE_BIASED_REPRESENTATION_P (gnu_type
)
1849 = Has_Biased_Representation (gnat_entity
);
1851 /* Set TYPE_STRING_FLAG for Character and Wide_Character subtypes. */
1852 TYPE_STRING_FLAG (gnu_type
) = TYPE_STRING_FLAG (TREE_TYPE (gnu_type
));
1854 /* Inherit our alias set from what we're a subtype of. Subtypes
1855 are not different types and a pointer can designate any instance
1856 within a subtype hierarchy. */
1857 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
1859 /* One of the above calls might have caused us to be elaborated,
1860 so don't blow up if so. */
1861 if (present_gnu_tree (gnat_entity
))
1863 maybe_present
= true;
1867 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1868 TYPE_STUB_DECL (gnu_type
)
1869 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
1871 /* For a packed array, make the original array type a parallel/debug
1873 if (debug_info_p
&& Is_Packed_Array_Impl_Type (gnat_entity
))
1874 associate_original_type_to_packed_array (gnu_type
, gnat_entity
);
1878 /* We have to handle clauses that under-align the type specially. */
1879 if ((Present (Alignment_Clause (gnat_entity
))
1880 || (Is_Packed_Array_Impl_Type (gnat_entity
)
1882 (Alignment_Clause (Original_Array_Type (gnat_entity
)))))
1883 && UI_Is_In_Int_Range (Alignment (gnat_entity
)))
1885 align
= UI_To_Int (Alignment (gnat_entity
)) * BITS_PER_UNIT
;
1886 if (align
>= TYPE_ALIGN (gnu_type
))
1890 /* If the type we are dealing with represents a bit-packed array,
1891 we need to have the bits left justified on big-endian targets
1892 and right justified on little-endian targets. We also need to
1893 ensure that when the value is read (e.g. for comparison of two
1894 such values), we only get the good bits, since the unused bits
1895 are uninitialized. Both goals are accomplished by wrapping up
1896 the modular type in an enclosing record type. */
1897 if (Is_Packed_Array_Impl_Type (gnat_entity
)
1898 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1900 tree gnu_field_type
, gnu_field
;
1902 /* Set the RM size before wrapping up the original type. */
1903 SET_TYPE_RM_SIZE (gnu_type
,
1904 UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
));
1905 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
) = 1;
1907 /* Strip the ___XP suffix for standard DWARF. */
1908 if (gnat_encodings
== DWARF_GNAT_ENCODINGS_MINIMAL
)
1909 gnu_entity_name
= TYPE_NAME (gnu_type
);
1911 /* Create a stripped-down declaration, mainly for debugging. */
1912 create_type_decl (gnu_entity_name
, gnu_type
, true, debug_info_p
,
1915 /* Now save it and build the enclosing record type. */
1916 gnu_field_type
= gnu_type
;
1918 gnu_type
= make_node (RECORD_TYPE
);
1919 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "JM");
1920 TYPE_PACKED (gnu_type
) = 1;
1921 TYPE_SIZE (gnu_type
) = TYPE_SIZE (gnu_field_type
);
1922 TYPE_SIZE_UNIT (gnu_type
) = TYPE_SIZE_UNIT (gnu_field_type
);
1923 SET_TYPE_ADA_SIZE (gnu_type
, TYPE_RM_SIZE (gnu_field_type
));
1925 /* Propagate the alignment of the modular type to the record type,
1926 unless there is an alignment clause that under-aligns the type.
1927 This means that bit-packed arrays are given "ceil" alignment for
1928 their size by default, which may seem counter-intuitive but makes
1929 it possible to overlay them on modular types easily. */
1930 SET_TYPE_ALIGN (gnu_type
,
1931 align
> 0 ? align
: TYPE_ALIGN (gnu_field_type
));
1933 /* Propagate the reverse storage order flag to the record type so
1934 that the required byte swapping is performed when retrieving the
1935 enclosed modular value. */
1936 TYPE_REVERSE_STORAGE_ORDER (gnu_type
)
1937 = Reverse_Storage_Order (Original_Array_Type (gnat_entity
));
1939 relate_alias_sets (gnu_type
, gnu_field_type
, ALIAS_SET_COPY
);
1941 /* Don't declare the field as addressable since we won't be taking
1942 its address and this would prevent create_field_decl from making
1945 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type
,
1946 gnu_type
, NULL_TREE
, bitsize_zero_node
, 1, 0);
1948 /* We will output additional debug info manually below. */
1949 finish_record_type (gnu_type
, gnu_field
, 2, false);
1950 compute_record_mode (gnu_type
);
1951 TYPE_JUSTIFIED_MODULAR_P (gnu_type
) = 1;
1955 /* Make the original array type a parallel/debug type. */
1956 associate_original_type_to_packed_array (gnu_type
, gnat_entity
);
1958 /* Since GNU_TYPE is a padding type around the packed array
1959 implementation type, the padded type is its debug type. */
1960 if (gnat_encodings
== DWARF_GNAT_ENCODINGS_MINIMAL
)
1961 SET_TYPE_DEBUG_TYPE (gnu_type
, gnu_field_type
);
1965 /* If the type we are dealing with has got a smaller alignment than the
1966 natural one, we need to wrap it up in a record type and misalign the
1967 latter; we reuse the padding machinery for this purpose. */
1970 tree gnu_size
= UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
);
1972 /* Set the RM size before wrapping the type. */
1973 SET_TYPE_RM_SIZE (gnu_type
, gnu_size
);
1976 = maybe_pad_type (gnu_type
, TYPE_SIZE (gnu_type
), align
,
1977 gnat_entity
, false, true, definition
, false);
1979 TYPE_PACKED (gnu_type
) = 1;
1980 SET_TYPE_ADA_SIZE (gnu_type
, gnu_size
);
1985 case E_Floating_Point_Type
:
1986 /* The type of the Low and High bounds can be our type if this is
1987 a type from Standard, so set them at the end of the function. */
1988 gnu_type
= make_node (REAL_TYPE
);
1989 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
1990 layout_type (gnu_type
);
1993 case E_Floating_Point_Subtype
:
1994 /* See the E_Signed_Integer_Subtype case for the rationale. */
1996 && Present (Ancestor_Subtype (gnat_entity
))
1997 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1998 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1999 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
2000 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
), gnu_expr
, false);
2002 gnu_type
= make_node (REAL_TYPE
);
2003 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
2004 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
2005 TYPE_GCC_MIN_VALUE (gnu_type
)
2006 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type
));
2007 TYPE_GCC_MAX_VALUE (gnu_type
)
2008 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type
));
2009 layout_type (gnu_type
);
2011 SET_TYPE_RM_MIN_VALUE
2012 (gnu_type
, elaborate_expression (Type_Low_Bound (gnat_entity
),
2013 gnat_entity
, "L", definition
, true,
2016 SET_TYPE_RM_MAX_VALUE
2017 (gnu_type
, elaborate_expression (Type_High_Bound (gnat_entity
),
2018 gnat_entity
, "U", definition
, true,
2021 /* Inherit our alias set from what we're a subtype of, as for
2022 integer subtypes. */
2023 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
2025 /* One of the above calls might have caused us to be elaborated,
2026 so don't blow up if so. */
2027 maybe_present
= true;
2030 /* Array Types and Subtypes
2032 Unconstrained array types are represented by E_Array_Type and
2033 constrained array types are represented by E_Array_Subtype. There
2034 are no actual objects of an unconstrained array type; all we have
2035 are pointers to that type.
2037 The following fields are defined on array types and subtypes:
2039 Component_Type Component type of the array.
2040 Number_Dimensions Number of dimensions (an int).
2041 First_Index Type of first index. */
2045 const bool convention_fortran_p
2046 = (Convention (gnat_entity
) == Convention_Fortran
);
2047 const int ndim
= Number_Dimensions (gnat_entity
);
2048 tree gnu_template_type
;
2049 tree gnu_ptr_template
;
2050 tree gnu_template_reference
, gnu_template_fields
, gnu_fat_type
;
2051 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2052 tree
*gnu_temp_fields
= XALLOCAVEC (tree
, ndim
);
2053 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
, tem
, t
;
2054 Entity_Id gnat_index
, gnat_name
;
2058 /* Create the type for the component now, as it simplifies breaking
2059 type reference loops. */
2061 = gnat_to_gnu_component_type (gnat_entity
, definition
, debug_info_p
);
2062 if (present_gnu_tree (gnat_entity
))
2064 /* As a side effect, the type may have been translated. */
2065 maybe_present
= true;
2069 /* We complete an existing dummy fat pointer type in place. This both
2070 avoids further complex adjustments in update_pointer_to and yields
2071 better debugging information in DWARF by leveraging the support for
2072 incomplete declarations of "tagged" types in the DWARF back-end. */
2073 gnu_type
= get_dummy_type (gnat_entity
);
2074 if (gnu_type
&& TYPE_POINTER_TO (gnu_type
))
2076 gnu_fat_type
= TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type
));
2077 TYPE_NAME (gnu_fat_type
) = NULL_TREE
;
2078 /* Save the contents of the dummy type for update_pointer_to. */
2079 TYPE_POINTER_TO (gnu_type
) = copy_type (gnu_fat_type
);
2081 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type
)));
2082 gnu_template_type
= TREE_TYPE (gnu_ptr_template
);
2086 gnu_fat_type
= make_node (RECORD_TYPE
);
2087 gnu_template_type
= make_node (RECORD_TYPE
);
2088 gnu_ptr_template
= build_pointer_type (gnu_template_type
);
2091 /* Make a node for the array. If we are not defining the array
2092 suppress expanding incomplete types. */
2093 gnu_type
= make_node (UNCONSTRAINED_ARRAY_TYPE
);
2097 defer_incomplete_level
++;
2098 this_deferred
= true;
2101 /* Build the fat pointer type. Use a "void *" object instead of
2102 a pointer to the array type since we don't have the array type
2103 yet (it will reference the fat pointer via the bounds). */
2105 = create_field_decl (get_identifier ("P_ARRAY"), ptr_type_node
,
2106 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2108 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template
,
2109 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2111 if (COMPLETE_TYPE_P (gnu_fat_type
))
2113 /* We are going to lay it out again so reset the alias set. */
2114 alias_set_type alias_set
= TYPE_ALIAS_SET (gnu_fat_type
);
2115 TYPE_ALIAS_SET (gnu_fat_type
) = -1;
2116 finish_fat_pointer_type (gnu_fat_type
, tem
);
2117 TYPE_ALIAS_SET (gnu_fat_type
) = alias_set
;
2118 for (t
= gnu_fat_type
; t
; t
= TYPE_NEXT_VARIANT (t
))
2120 TYPE_FIELDS (t
) = tem
;
2121 SET_TYPE_UNCONSTRAINED_ARRAY (t
, gnu_type
);
2126 finish_fat_pointer_type (gnu_fat_type
, tem
);
2127 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type
, gnu_type
);
2130 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2131 is the fat pointer. This will be used to access the individual
2132 fields once we build them. */
2133 tem
= build3 (COMPONENT_REF
, gnu_ptr_template
,
2134 build0 (PLACEHOLDER_EXPR
, gnu_fat_type
),
2135 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type
)), NULL_TREE
);
2136 gnu_template_reference
2137 = build_unary_op (INDIRECT_REF
, gnu_template_type
, tem
);
2138 TREE_READONLY (gnu_template_reference
) = 1;
2139 TREE_THIS_NOTRAP (gnu_template_reference
) = 1;
2141 /* Now create the GCC type for each index and add the fields for that
2142 index to the template. */
2143 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2144 gnat_index
= First_Index (gnat_entity
);
2145 0 <= index
&& index
< ndim
;
2146 index
+= (convention_fortran_p
? - 1 : 1),
2147 gnat_index
= Next_Index (gnat_index
))
2149 char field_name
[16];
2150 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2151 tree gnu_index_base_type
2152 = maybe_character_type (get_base_type (gnu_index_type
));
2153 tree gnu_lb_field
, gnu_hb_field
, gnu_orig_min
, gnu_orig_max
;
2154 tree gnu_min
, gnu_max
, gnu_high
;
2156 /* Make the FIELD_DECLs for the low and high bounds of this
2157 type and then make extractions of these fields from the
2159 sprintf (field_name
, "LB%d", index
);
2160 gnu_lb_field
= create_field_decl (get_identifier (field_name
),
2161 gnu_index_base_type
,
2162 gnu_template_type
, NULL_TREE
,
2164 Sloc_to_locus (Sloc (gnat_entity
),
2165 &DECL_SOURCE_LOCATION (gnu_lb_field
));
2167 field_name
[0] = 'U';
2168 gnu_hb_field
= create_field_decl (get_identifier (field_name
),
2169 gnu_index_base_type
,
2170 gnu_template_type
, NULL_TREE
,
2172 Sloc_to_locus (Sloc (gnat_entity
),
2173 &DECL_SOURCE_LOCATION (gnu_hb_field
));
2175 gnu_temp_fields
[index
] = chainon (gnu_lb_field
, gnu_hb_field
);
2177 /* We can't use build_component_ref here since the template type
2178 isn't complete yet. */
2179 gnu_orig_min
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2180 gnu_template_reference
, gnu_lb_field
,
2182 gnu_orig_max
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2183 gnu_template_reference
, gnu_hb_field
,
2185 TREE_READONLY (gnu_orig_min
) = TREE_READONLY (gnu_orig_max
) = 1;
2187 gnu_min
= convert (sizetype
, gnu_orig_min
);
2188 gnu_max
= convert (sizetype
, gnu_orig_max
);
2190 /* Compute the size of this dimension. See the E_Array_Subtype
2191 case below for the rationale. */
2193 = build3 (COND_EXPR
, sizetype
,
2194 build2 (GE_EXPR
, boolean_type_node
,
2195 gnu_orig_max
, gnu_orig_min
),
2197 size_binop (MINUS_EXPR
, gnu_min
, size_one_node
));
2199 /* Make a range type with the new range in the Ada base type.
2200 Then make an index type with the size range in sizetype. */
2201 gnu_index_types
[index
]
2202 = create_index_type (gnu_min
, gnu_high
,
2203 create_range_type (gnu_index_base_type
,
2208 /* Update the maximum size of the array in elements. */
2212 = convert (sizetype
, TYPE_MIN_VALUE (gnu_index_type
));
2214 = convert (sizetype
, TYPE_MAX_VALUE (gnu_index_type
));
2216 = size_binop (PLUS_EXPR
, size_one_node
,
2217 size_binop (MINUS_EXPR
, gnu_max
, gnu_min
));
2219 if (TREE_CODE (gnu_this_max
) == INTEGER_CST
2220 && TREE_OVERFLOW (gnu_this_max
))
2221 gnu_max_size
= NULL_TREE
;
2224 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2227 TYPE_NAME (gnu_index_types
[index
])
2228 = create_concat_name (gnat_entity
, field_name
);
2231 /* Install all the fields into the template. */
2232 TYPE_NAME (gnu_template_type
)
2233 = create_concat_name (gnat_entity
, "XUB");
2234 gnu_template_fields
= NULL_TREE
;
2235 for (index
= 0; index
< ndim
; index
++)
2237 = chainon (gnu_template_fields
, gnu_temp_fields
[index
]);
2238 finish_record_type (gnu_template_type
, gnu_template_fields
, 0,
2240 TYPE_READONLY (gnu_template_type
) = 1;
2242 /* If Component_Size is not already specified, annotate it with the
2243 size of the component. */
2244 if (Unknown_Component_Size (gnat_entity
))
2245 Set_Component_Size (gnat_entity
,
2246 annotate_value (TYPE_SIZE (comp_type
)));
2248 /* Compute the maximum size of the array in units and bits. */
2251 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2252 TYPE_SIZE_UNIT (comp_type
));
2253 gnu_max_size
= size_binop (MULT_EXPR
,
2254 convert (bitsizetype
, gnu_max_size
),
2255 TYPE_SIZE (comp_type
));
2258 gnu_max_size_unit
= NULL_TREE
;
2260 /* Now build the array type. */
2262 for (index
= ndim
- 1; index
>= 0; index
--)
2264 tem
= build_nonshared_array_type (tem
, gnu_index_types
[index
]);
2265 if (index
== ndim
- 1)
2266 TYPE_REVERSE_STORAGE_ORDER (tem
)
2267 = Reverse_Storage_Order (gnat_entity
);
2268 TYPE_MULTI_ARRAY_P (tem
) = (index
> 0);
2269 if (array_type_has_nonaliased_component (tem
, gnat_entity
))
2270 TYPE_NONALIASED_COMPONENT (tem
) = 1;
2273 /* If an alignment is specified, use it if valid. But ignore it
2274 for the original type of packed array types. If the alignment
2275 was requested with an explicit alignment clause, state so. */
2276 if (No (Packed_Array_Impl_Type (gnat_entity
))
2277 && Known_Alignment (gnat_entity
))
2279 SET_TYPE_ALIGN (tem
,
2280 validate_alignment (Alignment (gnat_entity
),
2283 if (Present (Alignment_Clause (gnat_entity
)))
2284 TYPE_USER_ALIGN (tem
) = 1;
2287 TYPE_CONVENTION_FORTRAN_P (tem
) = convention_fortran_p
;
2289 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2290 implementation types as such so that the debug information back-end
2291 can output the appropriate description for them. */
2293 = (Is_Packed (gnat_entity
)
2294 || Is_Packed_Array_Impl_Type (gnat_entity
));
2296 if (Treat_As_Volatile (gnat_entity
))
2297 tem
= change_qualified_type (tem
, TYPE_QUAL_VOLATILE
);
2299 /* Adjust the type of the pointer-to-array field of the fat pointer
2300 and record the aliasing relationships if necessary. */
2301 TREE_TYPE (TYPE_FIELDS (gnu_fat_type
)) = build_pointer_type (tem
);
2302 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type
))
2303 record_component_aliases (gnu_fat_type
);
2305 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2306 corresponding fat pointer. */
2307 TREE_TYPE (gnu_type
) = gnu_fat_type
;
2308 TYPE_POINTER_TO (gnu_type
) = gnu_fat_type
;
2309 TYPE_REFERENCE_TO (gnu_type
) = gnu_fat_type
;
2310 SET_TYPE_MODE (gnu_type
, BLKmode
);
2311 SET_TYPE_ALIGN (gnu_type
, TYPE_ALIGN (tem
));
2313 /* If the maximum size doesn't overflow, use it. */
2315 && TREE_CODE (gnu_max_size
) == INTEGER_CST
2316 && !TREE_OVERFLOW (gnu_max_size
)
2317 && TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2318 && !TREE_OVERFLOW (gnu_max_size_unit
))
2320 TYPE_SIZE (tem
) = size_binop (MIN_EXPR
, gnu_max_size
,
2322 TYPE_SIZE_UNIT (tem
) = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2323 TYPE_SIZE_UNIT (tem
));
2326 create_type_decl (create_concat_name (gnat_entity
, "XUA"), tem
,
2327 artificial_p
, debug_info_p
, gnat_entity
);
2329 /* If told to generate GNAT encodings for them (GDB rely on them at the
2330 moment): give the fat pointer type a name. If this is a packed
2331 array, tell the debugger how to interpret the underlying bits. */
2332 if (Present (Packed_Array_Impl_Type (gnat_entity
)))
2333 gnat_name
= Packed_Array_Impl_Type (gnat_entity
);
2335 gnat_name
= gnat_entity
;
2336 if (gnat_encodings
!= DWARF_GNAT_ENCODINGS_MINIMAL
)
2337 gnu_entity_name
= create_concat_name (gnat_name
, "XUP");
2338 create_type_decl (gnu_entity_name
, gnu_fat_type
, artificial_p
,
2339 debug_info_p
, gnat_entity
);
2341 /* Create the type to be designated by thin pointers: a record type for
2342 the array and its template. We used to shift the fields to have the
2343 template at a negative offset, but this was somewhat of a kludge; we
2344 now shift thin pointer values explicitly but only those which have a
2345 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE.
2346 Note that GDB can handle standard DWARF information for them, so we
2347 don't have to name them as a GNAT encoding, except if specifically
2349 if (gnat_encodings
!= DWARF_GNAT_ENCODINGS_MINIMAL
)
2350 gnu_entity_name
= create_concat_name (gnat_name
, "XUT");
2352 gnu_entity_name
= get_entity_name (gnat_name
);
2353 tem
= build_unc_object_type (gnu_template_type
, tem
, gnu_entity_name
,
2356 SET_TYPE_UNCONSTRAINED_ARRAY (tem
, gnu_type
);
2357 TYPE_OBJECT_RECORD_TYPE (gnu_type
) = tem
;
2361 case E_Array_Subtype
:
2363 /* This is the actual data type for array variables. Multidimensional
2364 arrays are implemented as arrays of arrays. Note that arrays which
2365 have sparse enumeration subtypes as index components create sparse
2366 arrays, which is obviously space inefficient but so much easier to
2369 Also note that the subtype never refers to the unconstrained array
2370 type, which is somewhat at variance with Ada semantics.
2372 First check to see if this is simply a renaming of the array type.
2373 If so, the result is the array type. */
2375 gnu_type
= TYPE_MAIN_VARIANT (gnat_to_gnu_type (Etype (gnat_entity
)));
2376 if (!Is_Constrained (gnat_entity
))
2380 Entity_Id gnat_index
, gnat_base_index
;
2381 const bool convention_fortran_p
2382 = (Convention (gnat_entity
) == Convention_Fortran
);
2383 const int ndim
= Number_Dimensions (gnat_entity
);
2384 tree gnu_base_type
= gnu_type
;
2385 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2386 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
;
2387 bool need_index_type_struct
= false;
2390 /* First create the GCC type for each index and find out whether
2391 special types are needed for debugging information. */
2392 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2393 gnat_index
= First_Index (gnat_entity
),
2395 = First_Index (Implementation_Base_Type (gnat_entity
));
2396 0 <= index
&& index
< ndim
;
2397 index
+= (convention_fortran_p
? - 1 : 1),
2398 gnat_index
= Next_Index (gnat_index
),
2399 gnat_base_index
= Next_Index (gnat_base_index
))
2401 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2402 tree gnu_index_base_type
2403 = maybe_character_type (get_base_type (gnu_index_type
));
2405 = convert (gnu_index_base_type
,
2406 TYPE_MIN_VALUE (gnu_index_type
));
2408 = convert (gnu_index_base_type
,
2409 TYPE_MAX_VALUE (gnu_index_type
));
2410 tree gnu_min
= convert (sizetype
, gnu_orig_min
);
2411 tree gnu_max
= convert (sizetype
, gnu_orig_max
);
2412 tree gnu_base_index_type
2413 = get_unpadded_type (Etype (gnat_base_index
));
2414 tree gnu_base_index_base_type
2415 = maybe_character_type (get_base_type (gnu_base_index_type
));
2416 tree gnu_base_orig_min
2417 = convert (gnu_base_index_base_type
,
2418 TYPE_MIN_VALUE (gnu_base_index_type
));
2419 tree gnu_base_orig_max
2420 = convert (gnu_base_index_base_type
,
2421 TYPE_MAX_VALUE (gnu_base_index_type
));
2424 /* See if the base array type is already flat. If it is, we
2425 are probably compiling an ACATS test but it will cause the
2426 code below to malfunction if we don't handle it specially. */
2427 if (TREE_CODE (gnu_base_orig_min
) == INTEGER_CST
2428 && TREE_CODE (gnu_base_orig_max
) == INTEGER_CST
2429 && tree_int_cst_lt (gnu_base_orig_max
, gnu_base_orig_min
))
2431 gnu_min
= size_one_node
;
2432 gnu_max
= size_zero_node
;
2436 /* Similarly, if one of the values overflows in sizetype and the
2437 range is null, use 1..0 for the sizetype bounds. */
2438 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2439 && TREE_CODE (gnu_max
) == INTEGER_CST
2440 && (TREE_OVERFLOW (gnu_min
) || TREE_OVERFLOW (gnu_max
))
2441 && tree_int_cst_lt (gnu_orig_max
, gnu_orig_min
))
2443 gnu_min
= size_one_node
;
2444 gnu_max
= size_zero_node
;
2448 /* If the minimum and maximum values both overflow in sizetype,
2449 but the difference in the original type does not overflow in
2450 sizetype, ignore the overflow indication. */
2451 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2452 && TREE_CODE (gnu_max
) == INTEGER_CST
2453 && TREE_OVERFLOW (gnu_min
) && TREE_OVERFLOW (gnu_max
)
2456 fold_build2 (MINUS_EXPR
, gnu_index_type
,
2460 TREE_OVERFLOW (gnu_min
) = 0;
2461 TREE_OVERFLOW (gnu_max
) = 0;
2465 /* Compute the size of this dimension in the general case. We
2466 need to provide GCC with an upper bound to use but have to
2467 deal with the "superflat" case. There are three ways to do
2468 this. If we can prove that the array can never be superflat,
2469 we can just use the high bound of the index type. */
2470 else if ((Nkind (gnat_index
) == N_Range
2471 && cannot_be_superflat (gnat_index
))
2472 /* Bit-Packed Array Impl. Types are never superflat. */
2473 || (Is_Packed_Array_Impl_Type (gnat_entity
)
2474 && Is_Bit_Packed_Array
2475 (Original_Array_Type (gnat_entity
))))
2478 /* Otherwise, if the high bound is constant but the low bound is
2479 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2480 lower bound. Note that the comparison must be done in the
2481 original type to avoid any overflow during the conversion. */
2482 else if (TREE_CODE (gnu_max
) == INTEGER_CST
2483 && TREE_CODE (gnu_min
) != INTEGER_CST
)
2487 = build_cond_expr (sizetype
,
2488 build_binary_op (GE_EXPR
,
2493 int_const_binop (PLUS_EXPR
, gnu_max
,
2497 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2498 in all the other cases. Note that, here as well as above,
2499 the condition used in the comparison must be equivalent to
2500 the condition (length != 0). This is relied upon in order
2501 to optimize array comparisons in compare_arrays. Moreover
2502 we use int_const_binop for the shift by 1 if the bound is
2503 constant to avoid any unwanted overflow. */
2506 = build_cond_expr (sizetype
,
2507 build_binary_op (GE_EXPR
,
2512 TREE_CODE (gnu_min
) == INTEGER_CST
2513 ? int_const_binop (MINUS_EXPR
, gnu_min
,
2515 : size_binop (MINUS_EXPR
, gnu_min
,
2518 /* Reuse the index type for the range type. Then make an index
2519 type with the size range in sizetype. */
2520 gnu_index_types
[index
]
2521 = create_index_type (gnu_min
, gnu_high
, gnu_index_type
,
2524 /* Update the maximum size of the array in elements. Here we
2525 see if any constraint on the index type of the base type
2526 can be used in the case of self-referential bound on the
2527 index type of the subtype. We look for a non-"infinite"
2528 and non-self-referential bound from any type involved and
2529 handle each bound separately. */
2532 tree gnu_base_min
= convert (sizetype
, gnu_base_orig_min
);
2533 tree gnu_base_max
= convert (sizetype
, gnu_base_orig_max
);
2534 tree gnu_base_base_min
2535 = convert (sizetype
,
2536 TYPE_MIN_VALUE (gnu_base_index_base_type
));
2537 tree gnu_base_base_max
2538 = convert (sizetype
,
2539 TYPE_MAX_VALUE (gnu_base_index_base_type
));
2541 if (!CONTAINS_PLACEHOLDER_P (gnu_min
)
2542 || !(TREE_CODE (gnu_base_min
) == INTEGER_CST
2543 && !TREE_OVERFLOW (gnu_base_min
)))
2544 gnu_base_min
= gnu_min
;
2546 if (!CONTAINS_PLACEHOLDER_P (gnu_max
)
2547 || !(TREE_CODE (gnu_base_max
) == INTEGER_CST
2548 && !TREE_OVERFLOW (gnu_base_max
)))
2549 gnu_base_max
= gnu_max
;
2551 if ((TREE_CODE (gnu_base_min
) == INTEGER_CST
2552 && TREE_OVERFLOW (gnu_base_min
))
2553 || operand_equal_p (gnu_base_min
, gnu_base_base_min
, 0)
2554 || (TREE_CODE (gnu_base_max
) == INTEGER_CST
2555 && TREE_OVERFLOW (gnu_base_max
))
2556 || operand_equal_p (gnu_base_max
, gnu_base_base_max
, 0))
2557 gnu_max_size
= NULL_TREE
;
2562 /* Use int_const_binop if the bounds are constant to
2563 avoid any unwanted overflow. */
2564 if (TREE_CODE (gnu_base_min
) == INTEGER_CST
2565 && TREE_CODE (gnu_base_max
) == INTEGER_CST
)
2567 = int_const_binop (PLUS_EXPR
, size_one_node
,
2568 int_const_binop (MINUS_EXPR
,
2573 = size_binop (PLUS_EXPR
, size_one_node
,
2574 size_binop (MINUS_EXPR
,
2579 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2583 /* We need special types for debugging information to point to
2584 the index types if they have variable bounds, are not integer
2585 types, are biased or are wider than sizetype. These are GNAT
2586 encodings, so we have to include them only when all encodings
2588 if ((TREE_CODE (gnu_orig_min
) != INTEGER_CST
2589 || TREE_CODE (gnu_orig_max
) != INTEGER_CST
2590 || TREE_CODE (gnu_index_type
) != INTEGER_TYPE
2591 || (TREE_TYPE (gnu_index_type
)
2592 && TREE_CODE (TREE_TYPE (gnu_index_type
))
2594 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type
))
2595 && gnat_encodings
!= DWARF_GNAT_ENCODINGS_MINIMAL
)
2596 need_index_type_struct
= true;
2599 /* Then flatten: create the array of arrays. For an array type
2600 used to implement a packed array, get the component type from
2601 the original array type since the representation clauses that
2602 can affect it are on the latter. */
2603 if (Is_Packed_Array_Impl_Type (gnat_entity
)
2604 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
2606 gnu_type
= gnat_to_gnu_type (Original_Array_Type (gnat_entity
));
2607 for (index
= ndim
- 1; index
>= 0; index
--)
2608 gnu_type
= TREE_TYPE (gnu_type
);
2610 /* One of the above calls might have caused us to be elaborated,
2611 so don't blow up if so. */
2612 if (present_gnu_tree (gnat_entity
))
2614 maybe_present
= true;
2620 gnu_type
= gnat_to_gnu_component_type (gnat_entity
, definition
,
2623 /* One of the above calls might have caused us to be elaborated,
2624 so don't blow up if so. */
2625 if (present_gnu_tree (gnat_entity
))
2627 maybe_present
= true;
2632 /* Compute the maximum size of the array in units and bits. */
2635 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2636 TYPE_SIZE_UNIT (gnu_type
));
2637 gnu_max_size
= size_binop (MULT_EXPR
,
2638 convert (bitsizetype
, gnu_max_size
),
2639 TYPE_SIZE (gnu_type
));
2642 gnu_max_size_unit
= NULL_TREE
;
2644 /* Now build the array type. */
2645 for (index
= ndim
- 1; index
>= 0; index
--)
2647 gnu_type
= build_nonshared_array_type (gnu_type
,
2648 gnu_index_types
[index
]);
2649 if (index
== ndim
- 1)
2650 TYPE_REVERSE_STORAGE_ORDER (gnu_type
)
2651 = Reverse_Storage_Order (gnat_entity
);
2652 TYPE_MULTI_ARRAY_P (gnu_type
) = (index
> 0);
2653 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2654 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2657 /* Strip the ___XP suffix for standard DWARF. */
2658 if (Is_Packed_Array_Impl_Type (gnat_entity
)
2659 && gnat_encodings
== DWARF_GNAT_ENCODINGS_MINIMAL
)
2661 Entity_Id gnat_original_array_type
2662 = Underlying_Type (Original_Array_Type (gnat_entity
));
2665 = get_entity_name (gnat_original_array_type
);
2668 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2669 TYPE_STUB_DECL (gnu_type
)
2670 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
2672 /* If we are at file level and this is a multi-dimensional array,
2673 we need to make a variable corresponding to the stride of the
2674 inner dimensions. */
2675 if (global_bindings_p () && ndim
> 1)
2679 for (gnu_arr_type
= TREE_TYPE (gnu_type
), index
= 1;
2680 TREE_CODE (gnu_arr_type
) == ARRAY_TYPE
;
2681 gnu_arr_type
= TREE_TYPE (gnu_arr_type
), index
++)
2683 tree eltype
= TREE_TYPE (gnu_arr_type
);
2684 char stride_name
[32];
2686 sprintf (stride_name
, "ST%d", index
);
2687 TYPE_SIZE (gnu_arr_type
)
2688 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type
),
2689 gnat_entity
, stride_name
,
2692 /* ??? For now, store the size as a multiple of the
2693 alignment of the element type in bytes so that we
2694 can see the alignment from the tree. */
2695 sprintf (stride_name
, "ST%d_A_UNIT", index
);
2696 TYPE_SIZE_UNIT (gnu_arr_type
)
2697 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type
),
2698 gnat_entity
, stride_name
,
2700 TYPE_ALIGN (eltype
));
2702 /* ??? create_type_decl is not invoked on the inner types so
2703 the MULT_EXPR node built above will never be marked. */
2704 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type
));
2708 /* If we need to write out a record type giving the names of the
2709 bounds for debugging purposes, do it now and make the record
2710 type a parallel type. This is not needed for a packed array
2711 since the bounds are conveyed by the original array type. */
2712 if (need_index_type_struct
2714 && !Is_Packed_Array_Impl_Type (gnat_entity
))
2716 tree gnu_bound_rec
= make_node (RECORD_TYPE
);
2717 tree gnu_field_list
= NULL_TREE
;
2720 TYPE_NAME (gnu_bound_rec
)
2721 = create_concat_name (gnat_entity
, "XA");
2723 for (index
= ndim
- 1; index
>= 0; index
--)
2725 tree gnu_index
= TYPE_INDEX_TYPE (gnu_index_types
[index
]);
2726 tree gnu_index_name
= TYPE_IDENTIFIER (gnu_index
);
2728 /* Make sure to reference the types themselves, and not just
2729 their names, as the debugger may fall back on them. */
2730 gnu_field
= create_field_decl (gnu_index_name
, gnu_index
,
2731 gnu_bound_rec
, NULL_TREE
,
2733 DECL_CHAIN (gnu_field
) = gnu_field_list
;
2734 gnu_field_list
= gnu_field
;
2737 finish_record_type (gnu_bound_rec
, gnu_field_list
, 0, true);
2738 add_parallel_type (gnu_type
, gnu_bound_rec
);
2741 /* If this is a packed array type, make the original array type a
2742 parallel/debug type. Otherwise, if such GNAT encodings are
2743 required, do it for the base array type if it isn't artificial to
2744 make sure it is kept in the debug info. */
2747 if (Is_Packed_Array_Impl_Type (gnat_entity
))
2748 associate_original_type_to_packed_array (gnu_type
,
2753 = gnat_to_gnu_entity (Etype (gnat_entity
), NULL_TREE
,
2755 if (!DECL_ARTIFICIAL (gnu_base_decl
)
2756 && gnat_encodings
!= DWARF_GNAT_ENCODINGS_MINIMAL
)
2757 add_parallel_type (gnu_type
,
2758 TREE_TYPE (TREE_TYPE (gnu_base_decl
)));
2762 TYPE_CONVENTION_FORTRAN_P (gnu_type
) = convention_fortran_p
;
2763 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
)
2764 = (Is_Packed_Array_Impl_Type (gnat_entity
)
2765 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)));
2767 /* Tag top-level ARRAY_TYPE nodes for packed arrays and their
2768 implementation types as such so that the debug information back-end
2769 can output the appropriate description for them. */
2770 TYPE_PACKED (gnu_type
)
2771 = (Is_Packed (gnat_entity
)
2772 || Is_Packed_Array_Impl_Type (gnat_entity
));
2774 /* If the size is self-referential and the maximum size doesn't
2775 overflow, use it. */
2776 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
2778 && !(TREE_CODE (gnu_max_size
) == INTEGER_CST
2779 && TREE_OVERFLOW (gnu_max_size
))
2780 && !(TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2781 && TREE_OVERFLOW (gnu_max_size_unit
)))
2783 TYPE_SIZE (gnu_type
) = size_binop (MIN_EXPR
, gnu_max_size
,
2784 TYPE_SIZE (gnu_type
));
2785 TYPE_SIZE_UNIT (gnu_type
)
2786 = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2787 TYPE_SIZE_UNIT (gnu_type
));
2790 /* Set our alias set to that of our base type. This gives all
2791 array subtypes the same alias set. */
2792 relate_alias_sets (gnu_type
, gnu_base_type
, ALIAS_SET_COPY
);
2794 /* If this is a packed type, make this type the same as the packed
2795 array type, but do some adjusting in the type first. */
2796 if (Present (Packed_Array_Impl_Type (gnat_entity
)))
2798 Entity_Id gnat_index
;
2801 /* First finish the type we had been making so that we output
2802 debugging information for it. */
2803 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
2804 if (Treat_As_Volatile (gnat_entity
))
2807 = TYPE_QUAL_VOLATILE
2808 | (Is_Atomic_Or_VFA (gnat_entity
) ? TYPE_QUAL_ATOMIC
: 0);
2809 gnu_type
= change_qualified_type (gnu_type
, quals
);
2811 /* Make it artificial only if the base type was artificial too.
2812 That's sort of "morally" true and will make it possible for
2813 the debugger to look it up by name in DWARF, which is needed
2814 in order to decode the packed array type. */
2816 = create_type_decl (gnu_entity_name
, gnu_type
,
2817 !Comes_From_Source (Etype (gnat_entity
))
2818 && artificial_p
, debug_info_p
,
2821 /* Save it as our equivalent in case the call below elaborates
2823 save_gnu_tree (gnat_entity
, gnu_decl
, false);
2826 = gnat_to_gnu_entity (Packed_Array_Impl_Type (gnat_entity
),
2828 this_made_decl
= true;
2829 gnu_type
= TREE_TYPE (gnu_decl
);
2831 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
2833 gnu_inner
= gnu_type
;
2834 while (TREE_CODE (gnu_inner
) == RECORD_TYPE
2835 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner
)
2836 || TYPE_PADDING_P (gnu_inner
)))
2837 gnu_inner
= TREE_TYPE (TYPE_FIELDS (gnu_inner
));
2839 /* We need to attach the index type to the type we just made so
2840 that the actual bounds can later be put into a template. */
2841 if ((TREE_CODE (gnu_inner
) == ARRAY_TYPE
2842 && !TYPE_ACTUAL_BOUNDS (gnu_inner
))
2843 || (TREE_CODE (gnu_inner
) == INTEGER_TYPE
2844 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
)))
2846 if (TREE_CODE (gnu_inner
) == INTEGER_TYPE
)
2848 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2849 TYPE_MODULUS for modular types so we make an extra
2850 subtype if necessary. */
2851 if (TYPE_MODULAR_P (gnu_inner
))
2854 = make_unsigned_type (TYPE_PRECISION (gnu_inner
));
2855 TREE_TYPE (gnu_subtype
) = gnu_inner
;
2856 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
2857 SET_TYPE_RM_MIN_VALUE (gnu_subtype
,
2858 TYPE_MIN_VALUE (gnu_inner
));
2859 SET_TYPE_RM_MAX_VALUE (gnu_subtype
,
2860 TYPE_MAX_VALUE (gnu_inner
));
2861 gnu_inner
= gnu_subtype
;
2864 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
) = 1;
2866 /* Check for other cases of overloading. */
2867 gcc_checking_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner
));
2870 for (gnat_index
= First_Index (gnat_entity
);
2871 Present (gnat_index
);
2872 gnat_index
= Next_Index (gnat_index
))
2873 SET_TYPE_ACTUAL_BOUNDS
2875 tree_cons (NULL_TREE
,
2876 get_unpadded_type (Etype (gnat_index
)),
2877 TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2879 if (Convention (gnat_entity
) != Convention_Fortran
)
2880 SET_TYPE_ACTUAL_BOUNDS
2881 (gnu_inner
, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2883 if (TREE_CODE (gnu_type
) == RECORD_TYPE
2884 && TYPE_JUSTIFIED_MODULAR_P (gnu_type
))
2885 TREE_TYPE (TYPE_FIELDS (gnu_type
)) = gnu_inner
;
2891 case E_String_Literal_Subtype
:
2892 /* Create the type for a string literal. */
2894 Entity_Id gnat_full_type
2895 = (IN (Ekind (Etype (gnat_entity
)), Private_Kind
)
2896 && Present (Full_View (Etype (gnat_entity
)))
2897 ? Full_View (Etype (gnat_entity
)) : Etype (gnat_entity
));
2898 tree gnu_string_type
= get_unpadded_type (gnat_full_type
);
2899 tree gnu_string_array_type
2900 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type
))));
2901 tree gnu_string_index_type
2902 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2903 (TYPE_DOMAIN (gnu_string_array_type
))));
2904 tree gnu_lower_bound
2905 = convert (gnu_string_index_type
,
2906 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity
)));
2908 = UI_To_gnu (String_Literal_Length (gnat_entity
),
2909 gnu_string_index_type
);
2910 tree gnu_upper_bound
2911 = build_binary_op (PLUS_EXPR
, gnu_string_index_type
,
2913 int_const_binop (MINUS_EXPR
, gnu_length
,
2914 convert (gnu_string_index_type
,
2915 integer_one_node
)));
2917 = create_index_type (convert (sizetype
, gnu_lower_bound
),
2918 convert (sizetype
, gnu_upper_bound
),
2919 create_range_type (gnu_string_index_type
,
2925 = build_nonshared_array_type (gnat_to_gnu_type
2926 (Component_Type (gnat_entity
)),
2928 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2929 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2930 relate_alias_sets (gnu_type
, gnu_string_type
, ALIAS_SET_COPY
);
2934 /* Record Types and Subtypes
2936 The following fields are defined on record types:
2938 Has_Discriminants True if the record has discriminants
2939 First_Discriminant Points to head of list of discriminants
2940 First_Entity Points to head of list of fields
2941 Is_Tagged_Type True if the record is tagged
2943 Implementation of Ada records and discriminated records:
2945 A record type definition is transformed into the equivalent of a C
2946 struct definition. The fields that are the discriminants which are
2947 found in the Full_Type_Declaration node and the elements of the
2948 Component_List found in the Record_Type_Definition node. The
2949 Component_List can be a recursive structure since each Variant of
2950 the Variant_Part of the Component_List has a Component_List.
2952 Processing of a record type definition comprises starting the list of
2953 field declarations here from the discriminants and the calling the
2954 function components_to_record to add the rest of the fields from the
2955 component list and return the gnu type node. The function
2956 components_to_record will call itself recursively as it traverses
2960 if (Has_Complex_Representation (gnat_entity
))
2963 = build_complex_type
2965 (Etype (Defining_Entity
2966 (First (Component_Items
2969 (Declaration_Node (gnat_entity
)))))))));
2975 Node_Id full_definition
= Declaration_Node (gnat_entity
);
2976 Node_Id record_definition
= Type_Definition (full_definition
);
2977 Node_Id gnat_constr
;
2978 Entity_Id gnat_field
;
2979 tree gnu_field
, gnu_field_list
= NULL_TREE
;
2980 tree gnu_get_parent
;
2981 /* Set PACKED in keeping with gnat_to_gnu_field. */
2983 = Is_Packed (gnat_entity
)
2985 : Component_Alignment (gnat_entity
) == Calign_Storage_Unit
2988 const bool has_align
= Known_Alignment (gnat_entity
);
2989 const bool has_discr
= Has_Discriminants (gnat_entity
);
2990 const bool has_rep
= Has_Specified_Layout (gnat_entity
);
2991 const bool is_extension
2992 = (Is_Tagged_Type (gnat_entity
)
2993 && Nkind (record_definition
) == N_Derived_Type_Definition
);
2994 const bool is_unchecked_union
= Is_Unchecked_Union (gnat_entity
);
2995 bool all_rep
= has_rep
;
2997 /* See if all fields have a rep clause. Stop when we find one
3000 for (gnat_field
= First_Entity (gnat_entity
);
3001 Present (gnat_field
);
3002 gnat_field
= Next_Entity (gnat_field
))
3003 if ((Ekind (gnat_field
) == E_Component
3004 || Ekind (gnat_field
) == E_Discriminant
)
3005 && No (Component_Clause (gnat_field
)))
3011 /* If this is a record extension, go a level further to find the
3012 record definition. Also, verify we have a Parent_Subtype. */
3015 if (!type_annotate_only
3016 || Present (Record_Extension_Part (record_definition
)))
3017 record_definition
= Record_Extension_Part (record_definition
);
3019 gcc_assert (type_annotate_only
3020 || Present (Parent_Subtype (gnat_entity
)));
3023 /* Make a node for the record. If we are not defining the record,
3024 suppress expanding incomplete types. */
3025 gnu_type
= make_node (tree_code_for_record_type (gnat_entity
));
3026 TYPE_NAME (gnu_type
) = gnu_entity_name
;
3027 TYPE_PACKED (gnu_type
) = (packed
!= 0) || has_align
|| has_rep
;
3028 TYPE_REVERSE_STORAGE_ORDER (gnu_type
)
3029 = Reverse_Storage_Order (gnat_entity
);
3030 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
3034 defer_incomplete_level
++;
3035 this_deferred
= true;
3038 /* If both a size and rep clause were specified, put the size on
3039 the record type now so that it can get the proper layout. */
3040 if (has_rep
&& Known_RM_Size (gnat_entity
))
3041 TYPE_SIZE (gnu_type
)
3042 = UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
);
3044 /* Always set the alignment on the record type here so that it can
3045 get the proper layout. */
3047 SET_TYPE_ALIGN (gnu_type
,
3048 validate_alignment (Alignment (gnat_entity
),
3052 SET_TYPE_ALIGN (gnu_type
, 0);
3054 /* If a type needs strict alignment, the minimum size will be the
3055 type size instead of the RM size (see validate_size). Cap the
3056 alignment lest it causes this type size to become too large. */
3057 if (Strict_Alignment (gnat_entity
) && Known_RM_Size (gnat_entity
))
3059 unsigned int max_size
= UI_To_Int (RM_Size (gnat_entity
));
3060 unsigned int max_align
= max_size
& -max_size
;
3061 if (max_align
< BIGGEST_ALIGNMENT
)
3062 TYPE_MAX_ALIGN (gnu_type
) = max_align
;
3066 /* If we have a Parent_Subtype, make a field for the parent. If
3067 this record has rep clauses, force the position to zero. */
3068 if (Present (Parent_Subtype (gnat_entity
)))
3070 Entity_Id gnat_parent
= Parent_Subtype (gnat_entity
);
3071 tree gnu_dummy_parent_type
= make_node (RECORD_TYPE
);
3074 /* A major complexity here is that the parent subtype will
3075 reference our discriminants in its Stored_Constraint list.
3076 But those must reference the parent component of this record
3077 which is precisely of the parent subtype we have not built yet!
3078 To break the circle we first build a dummy COMPONENT_REF which
3079 represents the "get to the parent" operation and initialize
3080 each of those discriminants to a COMPONENT_REF of the above
3081 dummy parent referencing the corresponding discriminant of the
3082 base type of the parent subtype. */
3083 gnu_get_parent
= build3 (COMPONENT_REF
, gnu_dummy_parent_type
,
3084 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3085 build_decl (input_location
,
3086 FIELD_DECL
, NULL_TREE
,
3087 gnu_dummy_parent_type
),
3091 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3092 Present (gnat_field
);
3093 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3094 if (Present (Corresponding_Discriminant (gnat_field
)))
3097 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3101 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3102 gnu_get_parent
, gnu_field
, NULL_TREE
),
3106 /* Then we build the parent subtype. If it has discriminants but
3107 the type itself has unknown discriminants, this means that it
3108 doesn't contain information about how the discriminants are
3109 derived from those of the ancestor type, so it cannot be used
3110 directly. Instead it is built by cloning the parent subtype
3111 of the underlying record view of the type, for which the above
3112 derivation of discriminants has been made explicit. */
3113 if (Has_Discriminants (gnat_parent
)
3114 && Has_Unknown_Discriminants (gnat_entity
))
3116 Entity_Id gnat_uview
= Underlying_Record_View (gnat_entity
);
3118 /* If we are defining the type, the underlying record
3119 view must already have been elaborated at this point.
3120 Otherwise do it now as its parent subtype cannot be
3121 technically elaborated on its own. */
3123 gcc_assert (present_gnu_tree (gnat_uview
));
3125 gnat_to_gnu_entity (gnat_uview
, NULL_TREE
, false);
3127 gnu_parent
= gnat_to_gnu_type (Parent_Subtype (gnat_uview
));
3129 /* Substitute the "get to the parent" of the type for that
3130 of its underlying record view in the cloned type. */
3131 for (gnat_field
= First_Stored_Discriminant (gnat_uview
);
3132 Present (gnat_field
);
3133 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3134 if (Present (Corresponding_Discriminant (gnat_field
)))
3136 tree gnu_field
= gnat_to_gnu_field_decl (gnat_field
);
3138 = build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3139 gnu_get_parent
, gnu_field
, NULL_TREE
);
3141 = substitute_in_type (gnu_parent
, gnu_field
, gnu_ref
);
3145 gnu_parent
= gnat_to_gnu_type (gnat_parent
);
3147 /* The parent field needs strict alignment so, if it is to
3148 be created with a component clause below, then we need
3149 to apply the same adjustment as in gnat_to_gnu_field. */
3150 if (has_rep
&& TYPE_ALIGN (gnu_type
) < TYPE_ALIGN (gnu_parent
))
3151 SET_TYPE_ALIGN (gnu_type
, TYPE_ALIGN (gnu_parent
));
3153 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3154 initially built. The discriminants must reference the fields
3155 of the parent subtype and not those of its base type for the
3156 placeholder machinery to properly work. */
3159 /* The actual parent subtype is the full view. */
3160 if (IN (Ekind (gnat_parent
), Private_Kind
))
3162 if (Present (Full_View (gnat_parent
)))
3163 gnat_parent
= Full_View (gnat_parent
);
3165 gnat_parent
= Underlying_Full_View (gnat_parent
);
3168 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3169 Present (gnat_field
);
3170 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3171 if (Present (Corresponding_Discriminant (gnat_field
)))
3174 for (field
= First_Stored_Discriminant (gnat_parent
);
3176 field
= Next_Stored_Discriminant (field
))
3177 if (same_discriminant_p (gnat_field
, field
))
3179 gcc_assert (Present (field
));
3180 TREE_OPERAND (get_gnu_tree (gnat_field
), 1)
3181 = gnat_to_gnu_field_decl (field
);
3185 /* The "get to the parent" COMPONENT_REF must be given its
3187 TREE_TYPE (gnu_get_parent
) = gnu_parent
;
3189 /* ...and reference the _Parent field of this record. */
3191 = create_field_decl (parent_name_id
,
3192 gnu_parent
, gnu_type
,
3194 ? TYPE_SIZE (gnu_parent
) : NULL_TREE
,
3196 ? bitsize_zero_node
: NULL_TREE
,
3198 DECL_INTERNAL_P (gnu_field
) = 1;
3199 TREE_OPERAND (gnu_get_parent
, 1) = gnu_field
;
3200 TYPE_FIELDS (gnu_type
) = gnu_field
;
3203 /* Make the fields for the discriminants and put them into the record
3204 unless it's an Unchecked_Union. */
3206 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3207 Present (gnat_field
);
3208 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3210 /* If this is a record extension and this discriminant is the
3211 renaming of another discriminant, we've handled it above. */
3212 if (Present (Parent_Subtype (gnat_entity
))
3213 && Present (Corresponding_Discriminant (gnat_field
)))
3216 /* However, if we are just annotating types, the Parent_Subtype
3217 doesn't exist so we need skip the discriminant altogether. */
3218 if (type_annotate_only
3219 && Is_Tagged_Type (gnat_entity
)
3220 && Is_Derived_Type (gnat_entity
)
3221 && Present (Corresponding_Discriminant (gnat_field
)))
3225 = gnat_to_gnu_field (gnat_field
, gnu_type
, packed
, definition
,
3228 /* Make an expression using a PLACEHOLDER_EXPR from the
3229 FIELD_DECL node just created and link that with the
3230 corresponding GNAT defining identifier. */
3231 save_gnu_tree (gnat_field
,
3232 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3233 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3234 gnu_field
, NULL_TREE
),
3237 if (!is_unchecked_union
)
3239 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3240 gnu_field_list
= gnu_field
;
3244 /* If we have a derived untagged type that renames discriminants in
3245 the root type, the (stored) discriminants are a just copy of the
3246 discriminants of the root type. This means that any constraints
3247 added by the renaming in the derivation are disregarded as far
3248 as the layout of the derived type is concerned. To rescue them,
3249 we change the type of the (stored) discriminants to a subtype
3250 with the bounds of the type of the visible discriminants. */
3253 && Stored_Constraint (gnat_entity
) != No_Elist
)
3254 for (gnat_constr
= First_Elmt (Stored_Constraint (gnat_entity
));
3255 gnat_constr
!= No_Elmt
;
3256 gnat_constr
= Next_Elmt (gnat_constr
))
3257 if (Nkind (Node (gnat_constr
)) == N_Identifier
3258 /* Ignore access discriminants. */
3259 && !Is_Access_Type (Etype (Node (gnat_constr
)))
3260 && Ekind (Entity (Node (gnat_constr
))) == E_Discriminant
)
3262 Entity_Id gnat_discr
= Entity (Node (gnat_constr
));
3263 tree gnu_discr_type
, gnu_ref
;
3265 /* If the scope of the discriminant is not the record type,
3266 this means that we're processing the implicit full view
3267 of a type derived from a private discriminated type: in
3268 this case, the Stored_Constraint list is simply copied
3269 from the partial view, see Build_Derived_Private_Type.
3270 So we need to retrieve the corresponding discriminant
3271 of the implicit full view, otherwise we will abort. */
3272 if (Scope (gnat_discr
) != gnat_entity
)
3275 for (field
= First_Entity (gnat_entity
);
3277 field
= Next_Entity (field
))
3278 if (Ekind (field
) == E_Discriminant
3279 && same_discriminant_p (gnat_discr
, field
))
3281 gcc_assert (Present (field
));
3285 gnu_discr_type
= gnat_to_gnu_type (Etype (gnat_discr
));
3287 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr
),
3290 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3291 just above for one of the stored discriminants. */
3292 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref
, 0)) == gnu_type
);
3294 if (gnu_discr_type
!= TREE_TYPE (gnu_ref
))
3296 const unsigned prec
= TYPE_PRECISION (TREE_TYPE (gnu_ref
));
3298 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref
))
3299 ? make_unsigned_type (prec
) : make_signed_type (prec
);
3300 TREE_TYPE (gnu_subtype
) = TREE_TYPE (gnu_ref
);
3301 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
3302 SET_TYPE_RM_MIN_VALUE (gnu_subtype
,
3303 TYPE_MIN_VALUE (gnu_discr_type
));
3304 SET_TYPE_RM_MAX_VALUE (gnu_subtype
,
3305 TYPE_MAX_VALUE (gnu_discr_type
));
3307 = TREE_TYPE (TREE_OPERAND (gnu_ref
, 1)) = gnu_subtype
;
3311 /* Add the fields into the record type and finish it up. */
3312 components_to_record (gnu_type
, Component_List (record_definition
),
3313 gnu_field_list
, packed
, definition
, false,
3314 all_rep
, is_unchecked_union
,
3315 artificial_p
, debug_info_p
,
3316 false, OK_To_Reorder_Components (gnat_entity
),
3317 all_rep
? NULL_TREE
: bitsize_zero_node
, NULL
);
3319 /* Fill in locations of fields. */
3320 annotate_rep (gnat_entity
, gnu_type
);
3322 /* If there are any entities in the chain corresponding to components
3323 that we did not elaborate, ensure we elaborate their types if they
3325 for (gnat_temp
= First_Entity (gnat_entity
);
3326 Present (gnat_temp
);
3327 gnat_temp
= Next_Entity (gnat_temp
))
3328 if ((Ekind (gnat_temp
) == E_Component
3329 || Ekind (gnat_temp
) == E_Discriminant
)
3330 && Is_Itype (Etype (gnat_temp
))
3331 && !present_gnu_tree (gnat_temp
))
3332 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, false);
3334 /* If this is a record type associated with an exception definition,
3335 equate its fields to those of the standard exception type. This
3336 will make it possible to convert between them. */
3337 if (gnu_entity_name
== exception_data_name_id
)
3340 for (gnu_field
= TYPE_FIELDS (gnu_type
),
3341 gnu_std_field
= TYPE_FIELDS (except_type_node
);
3343 gnu_field
= DECL_CHAIN (gnu_field
),
3344 gnu_std_field
= DECL_CHAIN (gnu_std_field
))
3345 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field
, gnu_std_field
);
3346 gcc_assert (!gnu_std_field
);
3351 case E_Class_Wide_Subtype
:
3352 /* If an equivalent type is present, that is what we should use.
3353 Otherwise, fall through to handle this like a record subtype
3354 since it may have constraints. */
3355 if (gnat_equiv_type
!= gnat_entity
)
3357 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, false);
3358 maybe_present
= true;
3362 /* ... fall through ... */
3364 case E_Record_Subtype
:
3365 /* If Cloned_Subtype is Present it means this record subtype has
3366 identical layout to that type or subtype and we should use
3367 that GCC type for this one. The front end guarantees that
3368 the component list is shared. */
3369 if (Present (Cloned_Subtype (gnat_entity
)))
3371 gnu_decl
= gnat_to_gnu_entity (Cloned_Subtype (gnat_entity
),
3373 maybe_present
= true;
3377 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3378 changing the type, make a new type with each field having the type of
3379 the field in the new subtype but the position computed by transforming
3380 every discriminant reference according to the constraints. We don't
3381 see any difference between private and non-private type here since
3382 derivations from types should have been deferred until the completion
3383 of the private type. */
3386 Entity_Id gnat_base_type
= Implementation_Base_Type (gnat_entity
);
3391 defer_incomplete_level
++;
3392 this_deferred
= true;
3396 = TYPE_MAIN_VARIANT (gnat_to_gnu_type (gnat_base_type
));
3398 if (present_gnu_tree (gnat_entity
))
3400 maybe_present
= true;
3404 /* If this is a record subtype associated with a dispatch table,
3405 strip the suffix. This is necessary to make sure 2 different
3406 subtypes associated with the imported and exported views of a
3407 dispatch table are properly merged in LTO mode. */
3408 if (Is_Dispatch_Table_Entity (gnat_entity
))
3411 Get_Encoded_Name (gnat_entity
);
3412 p
= strchr (Name_Buffer
, '_');
3414 strcpy (p
+2, "dtS");
3415 gnu_entity_name
= get_identifier (Name_Buffer
);
3418 /* When the subtype has discriminants and these discriminants affect
3419 the initial shape it has inherited, factor them in. But for an
3420 Unchecked_Union (it must be an Itype), just return the type.
3421 We can't just test Is_Constrained because private subtypes without
3422 discriminants of types with discriminants with default expressions
3423 are Is_Constrained but aren't constrained! */
3424 if (IN (Ekind (gnat_base_type
), Record_Kind
)
3425 && !Is_Unchecked_Union (gnat_base_type
)
3426 && !Is_For_Access_Subtype (gnat_entity
)
3427 && Has_Discriminants (gnat_entity
)
3428 && Is_Constrained (gnat_entity
)
3429 && Stored_Constraint (gnat_entity
) != No_Elist
)
3431 vec
<subst_pair
> gnu_subst_list
3432 = build_subst_list (gnat_entity
, gnat_base_type
, definition
);
3433 tree gnu_unpad_base_type
, gnu_rep_part
, gnu_variant_part
;
3434 tree gnu_pos_list
, gnu_field_list
= NULL_TREE
;
3435 bool selected_variant
= false, all_constant_pos
= true;
3436 Entity_Id gnat_field
;
3437 vec
<variant_desc
> gnu_variant_list
;
3439 gnu_type
= make_node (RECORD_TYPE
);
3440 TYPE_NAME (gnu_type
) = gnu_entity_name
;
3441 if (gnat_encodings
== DWARF_GNAT_ENCODINGS_MINIMAL
)
3442 SET_TYPE_DEBUG_TYPE (gnu_type
, gnu_base_type
);
3443 TYPE_PACKED (gnu_type
) = TYPE_PACKED (gnu_base_type
);
3444 TYPE_REVERSE_STORAGE_ORDER (gnu_type
)
3445 = Reverse_Storage_Order (gnat_entity
);
3446 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
3448 /* Set the size, alignment and alias set of the new type to
3449 match that of the old one, doing required substitutions. */
3450 copy_and_substitute_in_size (gnu_type
, gnu_base_type
,
3453 if (TYPE_IS_PADDING_P (gnu_base_type
))
3454 gnu_unpad_base_type
= TREE_TYPE (TYPE_FIELDS (gnu_base_type
));
3456 gnu_unpad_base_type
= gnu_base_type
;
3458 /* Look for REP and variant parts in the base type. */
3459 gnu_rep_part
= get_rep_part (gnu_unpad_base_type
);
3460 gnu_variant_part
= get_variant_part (gnu_unpad_base_type
);
3462 /* If there is a variant part, we must compute whether the
3463 constraints statically select a particular variant. If
3464 so, we simply drop the qualified union and flatten the
3465 list of fields. Otherwise we'll build a new qualified
3466 union for the variants that are still relevant. */
3467 if (gnu_variant_part
)
3473 = build_variant_list (TREE_TYPE (gnu_variant_part
),
3477 /* If all the qualifiers are unconditionally true, the
3478 innermost variant is statically selected. */
3479 selected_variant
= true;
3480 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3481 if (!integer_onep (v
->qual
))
3483 selected_variant
= false;
3487 /* Otherwise, create the new variants. */
3488 if (!selected_variant
)
3489 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3491 tree old_variant
= v
->type
;
3492 tree new_variant
= make_node (RECORD_TYPE
);
3494 = concat_name (DECL_NAME (gnu_variant_part
),
3496 (DECL_NAME (v
->field
)));
3497 TYPE_NAME (new_variant
)
3498 = concat_name (TYPE_NAME (gnu_type
),
3499 IDENTIFIER_POINTER (suffix
));
3500 TYPE_REVERSE_STORAGE_ORDER (new_variant
)
3501 = TYPE_REVERSE_STORAGE_ORDER (gnu_type
);
3502 copy_and_substitute_in_size (new_variant
, old_variant
,
3504 v
->new_type
= new_variant
;
3509 gnu_variant_list
.create (0);
3510 selected_variant
= false;
3513 /* Make a list of fields and their position in the base type. */
3515 = build_position_list (gnu_unpad_base_type
,
3516 gnu_variant_list
.exists ()
3517 && !selected_variant
,
3518 size_zero_node
, bitsize_zero_node
,
3519 BIGGEST_ALIGNMENT
, NULL_TREE
);
3521 /* Now go down every component in the subtype and compute its
3522 size and position from those of the component in the base
3523 type and from the constraints of the subtype. */
3524 for (gnat_field
= First_Entity (gnat_entity
);
3525 Present (gnat_field
);
3526 gnat_field
= Next_Entity (gnat_field
))
3527 if ((Ekind (gnat_field
) == E_Component
3528 || Ekind (gnat_field
) == E_Discriminant
)
3529 && !(Present (Corresponding_Discriminant (gnat_field
))
3530 && Is_Tagged_Type (gnat_base_type
))
3532 (Scope (Original_Record_Component (gnat_field
)))
3535 Name_Id gnat_name
= Chars (gnat_field
);
3536 Entity_Id gnat_old_field
3537 = Original_Record_Component (gnat_field
);
3539 = gnat_to_gnu_field_decl (gnat_old_field
);
3540 tree gnu_context
= DECL_CONTEXT (gnu_old_field
);
3541 tree gnu_field
, gnu_field_type
, gnu_size
, gnu_pos
;
3542 tree gnu_cont_type
, gnu_last
= NULL_TREE
;
3544 /* If the type is the same, retrieve the GCC type from the
3545 old field to take into account possible adjustments. */
3546 if (Etype (gnat_field
) == Etype (gnat_old_field
))
3547 gnu_field_type
= TREE_TYPE (gnu_old_field
);
3549 gnu_field_type
= gnat_to_gnu_type (Etype (gnat_field
));
3551 /* If there was a component clause, the field types must be
3552 the same for the type and subtype, so copy the data from
3553 the old field to avoid recomputation here. Also if the
3554 field is justified modular and the optimization in
3555 gnat_to_gnu_field was applied. */
3556 if (Present (Component_Clause (gnat_old_field
))
3557 || (TREE_CODE (gnu_field_type
) == RECORD_TYPE
3558 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type
)
3559 && TREE_TYPE (TYPE_FIELDS (gnu_field_type
))
3560 == TREE_TYPE (gnu_old_field
)))
3562 gnu_size
= DECL_SIZE (gnu_old_field
);
3563 gnu_field_type
= TREE_TYPE (gnu_old_field
);
3566 /* If the old field was packed and of constant size, we
3567 have to get the old size here, as it might differ from
3568 what the Etype conveys and the latter might overlap
3569 onto the following field. Try to arrange the type for
3570 possible better packing along the way. */
3571 else if (DECL_PACKED (gnu_old_field
)
3572 && TREE_CODE (DECL_SIZE (gnu_old_field
))
3575 gnu_size
= DECL_SIZE (gnu_old_field
);
3576 if (RECORD_OR_UNION_TYPE_P (gnu_field_type
)
3577 && !TYPE_FAT_POINTER_P (gnu_field_type
)
3578 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type
)))
3580 = make_packable_type (gnu_field_type
, true);
3584 gnu_size
= TYPE_SIZE (gnu_field_type
);
3586 /* If the context of the old field is the base type or its
3587 REP part (if any), put the field directly in the new
3588 type; otherwise look up the context in the variant list
3589 and put the field either in the new type if there is a
3590 selected variant or in one of the new variants. */
3591 if (gnu_context
== gnu_unpad_base_type
3593 && gnu_context
== TREE_TYPE (gnu_rep_part
)))
3594 gnu_cont_type
= gnu_type
;
3601 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3602 if (gnu_context
== v
->type
3603 || ((rep_part
= get_rep_part (v
->type
))
3604 && gnu_context
== TREE_TYPE (rep_part
)))
3608 if (selected_variant
)
3609 gnu_cont_type
= gnu_type
;
3611 gnu_cont_type
= v
->new_type
;
3614 /* The front-end may pass us "ghost" components if
3615 it fails to recognize that a constrained subtype
3616 is statically constrained. Discard them. */
3620 /* Now create the new field modeled on the old one. */
3622 = create_field_decl_from (gnu_old_field
, gnu_field_type
,
3623 gnu_cont_type
, gnu_size
,
3624 gnu_pos_list
, gnu_subst_list
);
3625 gnu_pos
= DECL_FIELD_OFFSET (gnu_field
);
3627 /* Put it in one of the new variants directly. */
3628 if (gnu_cont_type
!= gnu_type
)
3630 DECL_CHAIN (gnu_field
) = TYPE_FIELDS (gnu_cont_type
);
3631 TYPE_FIELDS (gnu_cont_type
) = gnu_field
;
3634 /* To match the layout crafted in components_to_record,
3635 if this is the _Tag or _Parent field, put it before
3636 any other fields. */
3637 else if (gnat_name
== Name_uTag
3638 || gnat_name
== Name_uParent
)
3639 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
3641 /* Similarly, if this is the _Controller field, put
3642 it before the other fields except for the _Tag or
3644 else if (gnat_name
== Name_uController
&& gnu_last
)
3646 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
3647 DECL_CHAIN (gnu_last
) = gnu_field
;
3650 /* Otherwise, if this is a regular field, put it after
3651 the other fields. */
3654 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3655 gnu_field_list
= gnu_field
;
3657 gnu_last
= gnu_field
;
3658 if (TREE_CODE (gnu_pos
) != INTEGER_CST
)
3659 all_constant_pos
= false;
3662 save_gnu_tree (gnat_field
, gnu_field
, false);
3665 /* If there is a variant list, a selected variant and the fields
3666 all have a constant position, put them in order of increasing
3667 position to match that of constant CONSTRUCTORs. Likewise if
3668 there is no variant list but a REP part, since the latter has
3669 been flattened in the process. */
3670 if (((gnu_variant_list
.exists () && selected_variant
)
3671 || (!gnu_variant_list
.exists () && gnu_rep_part
))
3672 && all_constant_pos
)
3674 const int len
= list_length (gnu_field_list
);
3675 tree
*field_arr
= XALLOCAVEC (tree
, len
), t
;
3678 for (t
= gnu_field_list
, i
= 0; t
; t
= DECL_CHAIN (t
), i
++)
3681 qsort (field_arr
, len
, sizeof (tree
), compare_field_bitpos
);
3683 gnu_field_list
= NULL_TREE
;
3684 for (i
= 0; i
< len
; i
++)
3686 DECL_CHAIN (field_arr
[i
]) = gnu_field_list
;
3687 gnu_field_list
= field_arr
[i
];
3691 /* If there is a variant list and no selected variant, we need
3692 to create the nest of variant parts from the old nest. */
3693 else if (gnu_variant_list
.exists () && !selected_variant
)
3695 tree new_variant_part
3696 = create_variant_part_from (gnu_variant_part
,
3697 gnu_variant_list
, gnu_type
,
3698 gnu_pos_list
, gnu_subst_list
);
3699 DECL_CHAIN (new_variant_part
) = gnu_field_list
;
3700 gnu_field_list
= new_variant_part
;
3703 /* Now go through the entities again looking for Itypes that
3704 we have not elaborated but should (e.g., Etypes of fields
3705 that have Original_Components). */
3706 for (gnat_field
= First_Entity (gnat_entity
);
3707 Present (gnat_field
); gnat_field
= Next_Entity (gnat_field
))
3708 if ((Ekind (gnat_field
) == E_Discriminant
3709 || Ekind (gnat_field
) == E_Component
)
3710 && !present_gnu_tree (Etype (gnat_field
)))
3711 gnat_to_gnu_entity (Etype (gnat_field
), NULL_TREE
, false);
3713 /* We will output additional debug info manually below. */
3714 finish_record_type (gnu_type
, nreverse (gnu_field_list
), 2,
3716 compute_record_mode (gnu_type
);
3718 /* Fill in locations of fields. */
3719 annotate_rep (gnat_entity
, gnu_type
);
3721 /* If debugging information is being written for the type and if
3722 we are asked to output such encodings, write a record that
3723 shows what we are a subtype of and also make a variable that
3724 indicates our size, if still variable. */
3725 if (gnat_encodings
!= DWARF_GNAT_ENCODINGS_MINIMAL
)
3727 tree gnu_subtype_marker
= make_node (RECORD_TYPE
);
3728 tree gnu_unpad_base_name
3729 = TYPE_IDENTIFIER (gnu_unpad_base_type
);
3730 tree gnu_size_unit
= TYPE_SIZE_UNIT (gnu_type
);
3732 TYPE_NAME (gnu_subtype_marker
)
3733 = create_concat_name (gnat_entity
, "XVS");
3734 finish_record_type (gnu_subtype_marker
,
3735 create_field_decl (gnu_unpad_base_name
,
3736 build_reference_type
3737 (gnu_unpad_base_type
),
3739 NULL_TREE
, NULL_TREE
,
3743 add_parallel_type (gnu_type
, gnu_subtype_marker
);
3746 && TREE_CODE (gnu_size_unit
) != INTEGER_CST
3747 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit
))
3748 TYPE_SIZE_UNIT (gnu_subtype_marker
)
3749 = create_var_decl (create_concat_name (gnat_entity
,
3751 NULL_TREE
, sizetype
, gnu_size_unit
,
3752 false, false, false, false, false,
3757 gnu_variant_list
.release ();
3758 gnu_subst_list
.release ();
3761 /* Otherwise, go down all the components in the new type and make
3762 them equivalent to those in the base type. */
3765 gnu_type
= gnu_base_type
;
3767 for (gnat_temp
= First_Entity (gnat_entity
);
3768 Present (gnat_temp
);
3769 gnat_temp
= Next_Entity (gnat_temp
))
3770 if ((Ekind (gnat_temp
) == E_Discriminant
3771 && !Is_Unchecked_Union (gnat_base_type
))
3772 || Ekind (gnat_temp
) == E_Component
)
3773 save_gnu_tree (gnat_temp
,
3774 gnat_to_gnu_field_decl
3775 (Original_Record_Component (gnat_temp
)),
3781 case E_Access_Subprogram_Type
:
3782 case E_Anonymous_Access_Subprogram_Type
:
3783 /* Use the special descriptor type for dispatch tables if needed,
3784 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3785 Note that we are only required to do so for static tables in
3786 order to be compatible with the C++ ABI, but Ada 2005 allows
3787 to extend library level tagged types at the local level so
3788 we do it in the non-static case as well. */
3789 if (TARGET_VTABLE_USES_DESCRIPTORS
3790 && Is_Dispatch_Table_Entity (gnat_entity
))
3792 gnu_type
= fdesc_type_node
;
3793 gnu_size
= TYPE_SIZE (gnu_type
);
3797 /* ... fall through ... */
3799 case E_Allocator_Type
:
3801 case E_Access_Attribute_Type
:
3802 case E_Anonymous_Access_Type
:
3803 case E_General_Access_Type
:
3805 /* The designated type and its equivalent type for gigi. */
3806 Entity_Id gnat_desig_type
= Directly_Designated_Type (gnat_entity
);
3807 Entity_Id gnat_desig_equiv
= Gigi_Equivalent_Type (gnat_desig_type
);
3808 /* Whether it comes from a limited with. */
3809 const bool is_from_limited_with
3810 = (IN (Ekind (gnat_desig_equiv
), Incomplete_Kind
)
3811 && From_Limited_With (gnat_desig_equiv
));
3812 /* The "full view" of the designated type. If this is an incomplete
3813 entity from a limited with, treat its non-limited view as the full
3814 view. Otherwise, if this is an incomplete or private type, use the
3815 full view. In the former case, we might point to a private type,
3816 in which case, we need its full view. Also, we want to look at the
3817 actual type used for the representation, so this takes a total of
3819 Entity_Id gnat_desig_full_direct_first
3820 = (is_from_limited_with
3821 ? Non_Limited_View (gnat_desig_equiv
)
3822 : (IN (Ekind (gnat_desig_equiv
), Incomplete_Or_Private_Kind
)
3823 ? Full_View (gnat_desig_equiv
) : Empty
));
3824 Entity_Id gnat_desig_full_direct
3825 = ((is_from_limited_with
3826 && Present (gnat_desig_full_direct_first
)
3827 && IN (Ekind (gnat_desig_full_direct_first
), Private_Kind
))
3828 ? Full_View (gnat_desig_full_direct_first
)
3829 : gnat_desig_full_direct_first
);
3830 Entity_Id gnat_desig_full
3831 = Gigi_Equivalent_Type (gnat_desig_full_direct
);
3832 /* The type actually used to represent the designated type, either
3833 gnat_desig_full or gnat_desig_equiv. */
3834 Entity_Id gnat_desig_rep
;
3835 /* We want to know if we'll be seeing the freeze node for any
3836 incomplete type we may be pointing to. */
3837 const bool in_main_unit
3838 = (Present (gnat_desig_full
)
3839 ? In_Extended_Main_Code_Unit (gnat_desig_full
)
3840 : In_Extended_Main_Code_Unit (gnat_desig_type
));
3841 /* True if we make a dummy type here. */
3842 bool made_dummy
= false;
3843 /* The mode to be used for the pointer type. */
3844 machine_mode p_mode
= mode_for_size (esize
, MODE_INT
, 0);
3845 /* The GCC type used for the designated type. */
3846 tree gnu_desig_type
= NULL_TREE
;
3848 if (!targetm
.valid_pointer_mode (p_mode
))
3851 /* If either the designated type or its full view is an unconstrained
3852 array subtype, replace it with the type it's a subtype of. This
3853 avoids problems with multiple copies of unconstrained array types.
3854 Likewise, if the designated type is a subtype of an incomplete
3855 record type, use the parent type to avoid order of elaboration
3856 issues. This can lose some code efficiency, but there is no
3858 if (Ekind (gnat_desig_equiv
) == E_Array_Subtype
3859 && !Is_Constrained (gnat_desig_equiv
))
3860 gnat_desig_equiv
= Etype (gnat_desig_equiv
);
3861 if (Present (gnat_desig_full
)
3862 && ((Ekind (gnat_desig_full
) == E_Array_Subtype
3863 && !Is_Constrained (gnat_desig_full
))
3864 || (Ekind (gnat_desig_full
) == E_Record_Subtype
3865 && Ekind (Etype (gnat_desig_full
)) == E_Record_Type
)))
3866 gnat_desig_full
= Etype (gnat_desig_full
);
3868 /* Set the type that's the representation of the designated type. */
3870 = Present (gnat_desig_full
) ? gnat_desig_full
: gnat_desig_equiv
;
3872 /* If we already know what the full type is, use it. */
3873 if (Present (gnat_desig_full
) && present_gnu_tree (gnat_desig_full
))
3874 gnu_desig_type
= TREE_TYPE (get_gnu_tree (gnat_desig_full
));
3876 /* Get the type of the thing we are to point to and build a pointer to
3877 it. If it is a reference to an incomplete or private type with a
3878 full view that is a record or an array, make a dummy type node and
3879 get the actual type later when we have verified it is safe. */
3880 else if ((!in_main_unit
3881 && !present_gnu_tree (gnat_desig_equiv
)
3882 && Present (gnat_desig_full
)
3883 && (Is_Record_Type (gnat_desig_full
)
3884 || Is_Array_Type (gnat_desig_full
)))
3885 /* Likewise if this is a reference to a record, an array or a
3886 subprogram type and we are to defer elaborating incomplete
3887 types. We do this because this access type may be the full
3888 view of a private type. */
3889 || ((!in_main_unit
|| imported_p
)
3890 && defer_incomplete_level
!= 0
3891 && !present_gnu_tree (gnat_desig_equiv
)
3892 && (Is_Record_Type (gnat_desig_rep
)
3893 || Is_Array_Type (gnat_desig_rep
)
3894 || Ekind (gnat_desig_rep
) == E_Subprogram_Type
))
3895 /* If this is a reference from a limited_with type back to our
3896 main unit and there's a freeze node for it, either we have
3897 already processed the declaration and made the dummy type,
3898 in which case we just reuse the latter, or we have not yet,
3899 in which case we make the dummy type and it will be reused
3900 when the declaration is finally processed. In both cases,
3901 the pointer eventually created below will be automatically
3902 adjusted when the freeze node is processed. */
3904 && is_from_limited_with
3905 && Present (Freeze_Node (gnat_desig_rep
))))
3907 gnu_desig_type
= make_dummy_type (gnat_desig_equiv
);
3911 /* Otherwise handle the case of a pointer to itself. */
3912 else if (gnat_desig_equiv
== gnat_entity
)
3915 = build_pointer_type_for_mode (void_type_node
, p_mode
,
3916 No_Strict_Aliasing (gnat_entity
));
3917 TREE_TYPE (gnu_type
) = TYPE_POINTER_TO (gnu_type
) = gnu_type
;
3920 /* If expansion is disabled, the equivalent type of a concurrent type
3921 is absent, so build a dummy pointer type. */
3922 else if (type_annotate_only
&& No (gnat_desig_equiv
))
3923 gnu_type
= ptr_type_node
;
3925 /* Finally, handle the default case where we can just elaborate our
3928 gnu_desig_type
= gnat_to_gnu_type (gnat_desig_equiv
);
3930 /* It is possible that a call to gnat_to_gnu_type above resolved our
3931 type. If so, just return it. */
3932 if (present_gnu_tree (gnat_entity
))
3934 maybe_present
= true;
3938 /* Access-to-unconstrained-array types need a special treatment. */
3939 if (Is_Array_Type (gnat_desig_rep
) && !Is_Constrained (gnat_desig_rep
))
3941 /* If the processing above got something that has a pointer, then
3942 we are done. This could have happened either because the type
3943 was elaborated or because somebody else executed the code. */
3944 if (!TYPE_POINTER_TO (gnu_desig_type
))
3945 build_dummy_unc_pointer_types (gnat_desig_equiv
, gnu_desig_type
);
3947 gnu_type
= TYPE_POINTER_TO (gnu_desig_type
);
3950 /* If we haven't done it yet, build the pointer type the usual way. */
3953 /* Modify the designated type if we are pointing only to constant
3954 objects, but don't do it for a dummy type. */
3955 if (Is_Access_Constant (gnat_entity
)
3956 && !TYPE_IS_DUMMY_P (gnu_desig_type
))
3958 = change_qualified_type (gnu_desig_type
, TYPE_QUAL_CONST
);
3961 = build_pointer_type_for_mode (gnu_desig_type
, p_mode
,
3962 No_Strict_Aliasing (gnat_entity
));
3965 /* If the designated type is not declared in the main unit and we made
3966 a dummy node for it, save our definition, elaborate the actual type
3967 and replace the dummy type we made with the actual one. But if we
3968 are to defer actually looking up the actual type, make an entry in
3969 the deferred list instead. If this is from a limited with, we may
3970 have to defer until the end of the current unit. */
3971 if (!in_main_unit
&& made_dummy
)
3973 if (TYPE_IS_FAT_POINTER_P (gnu_type
) && esize
== POINTER_SIZE
)
3975 = build_pointer_type (TYPE_OBJECT_RECORD_TYPE (gnu_desig_type
));
3977 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
3978 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
3979 artificial_p
, debug_info_p
,
3981 this_made_decl
= true;
3982 gnu_type
= TREE_TYPE (gnu_decl
);
3983 save_gnu_tree (gnat_entity
, gnu_decl
, false);
3986 if (defer_incomplete_level
== 0 && !is_from_limited_with
)
3988 update_pointer_to (TYPE_MAIN_VARIANT (gnu_desig_type
),
3989 gnat_to_gnu_type (gnat_desig_equiv
));
3993 struct incomplete
*p
= XNEW (struct incomplete
);
3994 struct incomplete
**head
3995 = (is_from_limited_with
3996 ? &defer_limited_with_list
: &defer_incomplete_list
);
3998 p
->old_type
= gnu_desig_type
;
3999 p
->full_type
= gnat_desig_equiv
;
4007 case E_Access_Protected_Subprogram_Type
:
4008 case E_Anonymous_Access_Protected_Subprogram_Type
:
4009 /* The run-time representation is the equivalent type. */
4010 if (type_annotate_only
&& No (gnat_equiv_type
))
4011 gnu_type
= ptr_type_node
;
4014 gnu_type
= gnat_to_gnu_type (gnat_equiv_type
);
4015 maybe_present
= true;
4018 /* The designated subtype must be elaborated as well, if it does
4019 not have its own freeze node. */
4020 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
4021 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
4022 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
)))
4023 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity
))))
4024 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
4029 case E_Access_Subtype
:
4030 /* We treat this as identical to its base type; any constraint is
4031 meaningful only to the front-end. */
4032 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
4034 /* The designated subtype must be elaborated as well, if it does
4035 not have its own freeze node. But designated subtypes created
4036 for constrained components of records with discriminants are
4037 not frozen by the front-end and not elaborated here, because
4038 their use may appear before the base type is frozen and it is
4039 not clear that they are needed in gigi. With the current model,
4040 there is no correct place where they could be elaborated. */
4041 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
4042 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
4043 && Is_Frozen (Directly_Designated_Type (gnat_entity
))
4044 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
))))
4046 /* If we are to defer elaborating incomplete types, make a dummy
4047 type node and elaborate it later. */
4048 if (defer_incomplete_level
!= 0)
4050 struct incomplete
*p
= XNEW (struct incomplete
);
4053 = make_dummy_type (Directly_Designated_Type (gnat_entity
));
4054 p
->full_type
= Directly_Designated_Type (gnat_entity
);
4055 p
->next
= defer_incomplete_list
;
4056 defer_incomplete_list
= p
;
4058 else if (!IN (Ekind (Base_Type
4059 (Directly_Designated_Type (gnat_entity
))),
4060 Incomplete_Or_Private_Kind
))
4061 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
4065 maybe_present
= true;
4068 /* Subprogram Entities
4070 The following access functions are defined for subprograms:
4072 Etype Return type or Standard_Void_Type.
4073 First_Formal The first formal parameter.
4074 Is_Imported Indicates that the subprogram has appeared in
4075 an INTERFACE or IMPORT pragma. For now we
4076 assume that the external language is C.
4077 Is_Exported Likewise but for an EXPORT pragma.
4078 Is_Inlined True if the subprogram is to be inlined.
4080 Each parameter is first checked by calling must_pass_by_ref on its
4081 type to determine if it is passed by reference. For parameters which
4082 are copied in, if they are Ada In Out or Out parameters, their return
4083 value becomes part of a record which becomes the return type of the
4084 function (C function - note that this applies only to Ada procedures
4085 so there is no Ada return type). Additional code to store back the
4086 parameters will be generated on the caller side. This transformation
4087 is done here, not in the front-end.
4089 The intended result of the transformation can be seen from the
4090 equivalent source rewritings that follow:
4092 struct temp {int a,b};
4093 procedure P (A,B: In Out ...) is temp P (int A,B)
4096 end P; return {A,B};
4103 For subprogram types we need to perform mainly the same conversions to
4104 GCC form that are needed for procedures and function declarations. The
4105 only difference is that at the end, we make a type declaration instead
4106 of a function declaration. */
4108 case E_Subprogram_Type
:
4112 tree gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
4113 enum inline_status_t inline_status
4114 = Has_Pragma_No_Inline (gnat_entity
)
4116 : Has_Pragma_Inline_Always (gnat_entity
)
4118 : (Is_Inlined (gnat_entity
) ? is_enabled
: is_disabled
);
4119 bool public_flag
= Is_Public (gnat_entity
) || imported_p
;
4120 /* Subprograms marked both Intrinsic and Always_Inline need not
4121 have a body of their own. */
4123 = ((Is_Public (gnat_entity
) && !definition
)
4125 || (Convention (gnat_entity
) == Convention_Intrinsic
4126 && Has_Pragma_Inline_Always (gnat_entity
)));
4127 tree gnu_param_list
;
4129 /* A parameter may refer to this type, so defer completion of any
4130 incomplete types. */
4131 if (kind
== E_Subprogram_Type
&& !definition
)
4133 defer_incomplete_level
++;
4134 this_deferred
= true;
4137 /* If the subprogram has an alias, it is probably inherited, so
4138 we can use the original one. If the original "subprogram"
4139 is actually an enumeration literal, it may be the first use
4140 of its type, so we must elaborate that type now. */
4141 if (Present (Alias (gnat_entity
)))
4143 const Entity_Id gnat_renamed
= Renamed_Object (gnat_entity
);
4145 if (Ekind (Alias (gnat_entity
)) == E_Enumeration_Literal
)
4146 gnat_to_gnu_entity (Etype (Alias (gnat_entity
)), NULL_TREE
,
4150 = gnat_to_gnu_entity (Alias (gnat_entity
), gnu_expr
, false);
4152 /* Elaborate any Itypes in the parameters of this entity. */
4153 for (gnat_temp
= First_Formal_With_Extras (gnat_entity
);
4154 Present (gnat_temp
);
4155 gnat_temp
= Next_Formal_With_Extras (gnat_temp
))
4156 if (Is_Itype (Etype (gnat_temp
)))
4157 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, false);
4159 /* Materialize renamed subprograms in the debugging information
4160 when the renamed object is compile time known. We can consider
4161 such renamings as imported declarations.
4163 Because the parameters in generics instantiation are generally
4164 materialized as renamings, we ofter end up having both the
4165 renamed subprogram and the renaming in the same context and with
4166 the same name: in this case, renaming is both useless debug-wise
4167 and potentially harmful as name resolution in the debugger could
4168 return twice the same entity! So avoid this case. */
4169 if (debug_info_p
&& !artificial_p
4170 && !(get_debug_scope (gnat_entity
, NULL
)
4171 == get_debug_scope (gnat_renamed
, NULL
)
4172 && Name_Equals (Chars (gnat_entity
),
4173 Chars (gnat_renamed
)))
4174 && Present (gnat_renamed
)
4175 && (Ekind (gnat_renamed
) == E_Function
4176 || Ekind (gnat_renamed
) == E_Procedure
)
4178 && TREE_CODE (gnu_decl
) == FUNCTION_DECL
)
4180 tree decl
= build_decl (input_location
, IMPORTED_DECL
,
4181 gnu_entity_name
, void_type_node
);
4182 IMPORTED_DECL_ASSOCIATED_DECL (decl
) = gnu_decl
;
4183 gnat_pushdecl (decl
, gnat_entity
);
4189 /* Get the GCC tree for the (underlying) subprogram type. If the
4190 entity is an actual subprogram, also get the parameter list. */
4192 = gnat_to_gnu_subprog_type (gnat_entity
, definition
, debug_info_p
,
4195 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4196 corresponding DECL node and check the parameter association. */
4197 if (Convention (gnat_entity
) == Convention_Intrinsic
4198 && Present (Interface_Name (gnat_entity
)))
4200 tree gnu_builtin_decl
= builtin_decl_for (gnu_ext_name
);
4202 /* If we have a builtin DECL for that function, use it. Check if
4203 the profiles are compatible and warn if they are not. Note that
4204 the checker is expected to post diagnostics in this case. */
4205 if (gnu_builtin_decl
)
4207 intrin_binding_t inb
4208 = { gnat_entity
, gnu_type
, TREE_TYPE (gnu_builtin_decl
) };
4210 if (!intrin_profiles_compatible_p (&inb
))
4212 ("?profile of& doesn''t match the builtin it binds!",
4215 gnu_decl
= gnu_builtin_decl
;
4216 gnu_type
= TREE_TYPE (gnu_builtin_decl
);
4220 /* Inability to find the builtin DECL most often indicates a
4221 genuine mistake, but imports of unregistered intrinsics are
4222 sometimes issued on purpose to allow hooking in alternate
4223 bodies. We post a warning conditioned on Wshadow in this case,
4224 to let developers be notified on demand without risking false
4225 positives with common default sets of options. */
4226 else if (warn_shadow
)
4227 post_error ("?gcc intrinsic not found for&!", gnat_entity
);
4230 /* If there was no specified Interface_Name and the external and
4231 internal names of the subprogram are the same, only use the
4232 internal name to allow disambiguation of nested subprograms. */
4233 if (No (Interface_Name (gnat_entity
))
4234 && gnu_ext_name
== gnu_entity_name
)
4235 gnu_ext_name
= NULL_TREE
;
4237 /* Deal with platform-specific calling conventions. */
4238 if (Has_Stdcall_Convention (gnat_entity
))
4239 prepend_one_attribute
4240 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4241 get_identifier ("stdcall"), NULL_TREE
,
4243 else if (Has_Thiscall_Convention (gnat_entity
))
4244 prepend_one_attribute
4245 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4246 get_identifier ("thiscall"), NULL_TREE
,
4249 /* If we should request stack realignment for a foreign convention
4250 subprogram, do so. Note that this applies to task entry points
4252 if (FOREIGN_FORCE_REALIGN_STACK
4253 && Has_Foreign_Convention (gnat_entity
))
4254 prepend_one_attribute
4255 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4256 get_identifier ("force_align_arg_pointer"), NULL_TREE
,
4259 /* Deal with a pragma Linker_Section on a subprogram. */
4260 if ((kind
== E_Function
|| kind
== E_Procedure
)
4261 && Present (Linker_Section_Pragma (gnat_entity
)))
4262 prepend_one_attribute_pragma (&attr_list
,
4263 Linker_Section_Pragma (gnat_entity
));
4265 /* If we are defining the subprogram and it has an Address clause
4266 we must get the address expression from the saved GCC tree for the
4267 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4268 the address expression here since the front-end has guaranteed
4269 in that case that the elaboration has no effects. If there is
4270 an Address clause and we are not defining the object, just
4271 make it a constant. */
4272 if (Present (Address_Clause (gnat_entity
)))
4274 tree gnu_address
= NULL_TREE
;
4278 = (present_gnu_tree (gnat_entity
)
4279 ? get_gnu_tree (gnat_entity
)
4280 : gnat_to_gnu (Expression (Address_Clause (gnat_entity
))));
4282 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
4284 /* Convert the type of the object to a reference type that can
4285 alias everything as per RM 13.3(19). */
4287 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
4289 gnu_address
= convert (gnu_type
, gnu_address
);
4292 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4293 gnu_address
, false, Is_Public (gnat_entity
),
4294 extern_flag
, false, false, artificial_p
,
4295 debug_info_p
, NULL
, gnat_entity
);
4296 DECL_BY_REF_P (gnu_decl
) = 1;
4299 else if (kind
== E_Subprogram_Type
)
4301 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
4304 = create_type_decl (gnu_entity_name
, gnu_type
, artificial_p
,
4305 debug_info_p
, gnat_entity
);
4311 = create_subprog_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4312 gnu_param_list
, inline_status
,
4313 public_flag
, extern_flag
,
4314 artificial_p
, debug_info_p
,
4315 attr_list
, gnat_entity
);
4317 DECL_STUBBED_P (gnu_decl
)
4318 = (Convention (gnat_entity
) == Convention_Stubbed
);
4323 case E_Incomplete_Type
:
4324 case E_Incomplete_Subtype
:
4325 case E_Private_Type
:
4326 case E_Private_Subtype
:
4327 case E_Limited_Private_Type
:
4328 case E_Limited_Private_Subtype
:
4329 case E_Record_Type_With_Private
:
4330 case E_Record_Subtype_With_Private
:
4332 const bool is_from_limited_with
4333 = (IN (kind
, Incomplete_Kind
) && From_Limited_With (gnat_entity
));
4334 /* Get the "full view" of this entity. If this is an incomplete
4335 entity from a limited with, treat its non-limited view as the
4336 full view. Otherwise, use either the full view or the underlying
4337 full view, whichever is present. This is used in all the tests
4339 const Entity_Id full_view
4340 = is_from_limited_with
4341 ? Non_Limited_View (gnat_entity
)
4342 : Present (Full_View (gnat_entity
))
4343 ? Full_View (gnat_entity
)
4344 : IN (kind
, Private_Kind
)
4345 ? Underlying_Full_View (gnat_entity
)
4348 /* If this is an incomplete type with no full view, it must be a Taft
4349 Amendment type, in which case we return a dummy type. Otherwise,
4350 just get the type from its Etype. */
4353 if (kind
== E_Incomplete_Type
)
4355 gnu_type
= make_dummy_type (gnat_entity
);
4356 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4361 = gnat_to_gnu_entity (Etype (gnat_entity
), NULL_TREE
, false);
4362 maybe_present
= true;
4366 /* Or else, if we already made a type for the full view, reuse it. */
4367 else if (present_gnu_tree (full_view
))
4368 gnu_decl
= get_gnu_tree (full_view
);
4370 /* Or else, if we are not defining the type or there is no freeze
4371 node on it, get the type for the full view. Likewise if this is
4372 a limited_with'ed type not declared in the main unit, which can
4373 happen for incomplete formal types instantiated on a type coming
4374 from a limited_with clause. */
4375 else if (!definition
4376 || No (Freeze_Node (full_view
))
4377 || (is_from_limited_with
4378 && !In_Extended_Main_Code_Unit (full_view
)))
4380 gnu_decl
= gnat_to_gnu_entity (full_view
, NULL_TREE
, false);
4381 maybe_present
= true;
4384 /* Otherwise, make a dummy type entry which will be replaced later.
4385 Save it as the full declaration's type so we can do any needed
4386 updates when we see it. */
4389 gnu_type
= make_dummy_type (gnat_entity
);
4390 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4391 if (Has_Completion_In_Body (gnat_entity
))
4392 DECL_TAFT_TYPE_P (gnu_decl
) = 1;
4393 save_gnu_tree (full_view
, gnu_decl
, 0);
4398 case E_Class_Wide_Type
:
4399 /* Class-wide types are always transformed into their root type. */
4400 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, false);
4401 maybe_present
= true;
4404 case E_Protected_Type
:
4405 case E_Protected_Subtype
:
4407 case E_Task_Subtype
:
4408 /* If we are just annotating types and have no equivalent record type,
4409 just return void_type, except for root types that have discriminants
4410 because the discriminants will very likely be used in the declarative
4411 part of the associated body so they need to be translated. */
4412 if (type_annotate_only
&& No (gnat_equiv_type
))
4414 if (Has_Discriminants (gnat_entity
)
4415 && Root_Type (gnat_entity
) == gnat_entity
)
4417 tree gnu_field_list
= NULL_TREE
;
4418 Entity_Id gnat_field
;
4420 /* This is a minimal version of the E_Record_Type handling. */
4421 gnu_type
= make_node (RECORD_TYPE
);
4422 TYPE_NAME (gnu_type
) = gnu_entity_name
;
4424 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
4425 Present (gnat_field
);
4426 gnat_field
= Next_Stored_Discriminant (gnat_field
))
4429 = gnat_to_gnu_field (gnat_field
, gnu_type
, false,
4430 definition
, debug_info_p
);
4432 save_gnu_tree (gnat_field
,
4433 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
4434 build0 (PLACEHOLDER_EXPR
, gnu_type
),
4435 gnu_field
, NULL_TREE
),
4438 DECL_CHAIN (gnu_field
) = gnu_field_list
;
4439 gnu_field_list
= gnu_field
;
4442 finish_record_type (gnu_type
, nreverse (gnu_field_list
), 0,
4446 gnu_type
= void_type_node
;
4449 /* Concurrent types are always transformed into their record type. */
4451 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, false);
4452 maybe_present
= true;
4456 gnu_decl
= create_label_decl (gnu_entity_name
, gnat_entity
);
4461 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4462 we've already saved it, so we don't try to. */
4463 gnu_decl
= error_mark_node
;
4467 case E_Abstract_State
:
4468 /* This is a SPARK annotation that only reaches here when compiling in
4470 gcc_assert (type_annotate_only
);
4471 gnu_decl
= error_mark_node
;
4479 /* If we had a case where we evaluated another type and it might have
4480 defined this one, handle it here. */
4481 if (maybe_present
&& present_gnu_tree (gnat_entity
))
4483 gnu_decl
= get_gnu_tree (gnat_entity
);
4487 /* If we are processing a type and there is either no decl for it or
4488 we just made one, do some common processing for the type, such as
4489 handling alignment and possible padding. */
4490 if (is_type
&& (!gnu_decl
|| this_made_decl
))
4492 /* Process the attributes, if not already done. Note that the type is
4493 already defined so we cannot pass true for IN_PLACE here. */
4494 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
4496 /* Tell the middle-end that objects of tagged types are guaranteed to
4497 be properly aligned. This is necessary because conversions to the
4498 class-wide type are translated into conversions to the root type,
4499 which can be less aligned than some of its derived types. */
4500 if (Is_Tagged_Type (gnat_entity
)
4501 || Is_Class_Wide_Equivalent_Type (gnat_entity
))
4502 TYPE_ALIGN_OK (gnu_type
) = 1;
4504 /* Record whether the type is passed by reference. */
4505 if (!VOID_TYPE_P (gnu_type
) && Is_By_Reference_Type (gnat_entity
))
4506 TYPE_BY_REFERENCE_P (gnu_type
) = 1;
4508 /* ??? Don't set the size for a String_Literal since it is either
4509 confirming or we don't handle it properly (if the low bound is
4511 if (!gnu_size
&& kind
!= E_String_Literal_Subtype
)
4513 Uint gnat_size
= Known_Esize (gnat_entity
)
4514 ? Esize (gnat_entity
) : RM_Size (gnat_entity
);
4516 = validate_size (gnat_size
, gnu_type
, gnat_entity
, TYPE_DECL
,
4517 false, Has_Size_Clause (gnat_entity
));
4520 /* If a size was specified, see if we can make a new type of that size
4521 by rearranging the type, for example from a fat to a thin pointer. */
4525 = make_type_from_size (gnu_type
, gnu_size
,
4526 Has_Biased_Representation (gnat_entity
));
4528 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0)
4529 && operand_equal_p (rm_size (gnu_type
), gnu_size
, 0))
4530 gnu_size
= NULL_TREE
;
4533 /* If the alignment has not already been processed and this is not
4534 an unconstrained array type, see if an alignment is specified.
4535 If not, we pick a default alignment for atomic objects. */
4536 if (align
!= 0 || TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4538 else if (Known_Alignment (gnat_entity
))
4540 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
4541 TYPE_ALIGN (gnu_type
));
4543 /* Warn on suspiciously large alignments. This should catch
4544 errors about the (alignment,byte)/(size,bit) discrepancy. */
4545 if (align
> BIGGEST_ALIGNMENT
&& Has_Alignment_Clause (gnat_entity
))
4549 /* If a size was specified, take it into account. Otherwise
4550 use the RM size for records or unions as the type size has
4551 already been adjusted to the alignment. */
4554 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
4555 && !TYPE_FAT_POINTER_P (gnu_type
))
4556 size
= rm_size (gnu_type
);
4558 size
= TYPE_SIZE (gnu_type
);
4560 /* Consider an alignment as suspicious if the alignment/size
4561 ratio is greater or equal to the byte/bit ratio. */
4562 if (tree_fits_uhwi_p (size
)
4563 && align
>= tree_to_uhwi (size
) * BITS_PER_UNIT
)
4564 post_error_ne ("?suspiciously large alignment specified for&",
4565 Expression (Alignment_Clause (gnat_entity
)),
4569 else if (Is_Atomic_Or_VFA (gnat_entity
) && !gnu_size
4570 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type
))
4571 && integer_pow2p (TYPE_SIZE (gnu_type
)))
4572 align
= MIN (BIGGEST_ALIGNMENT
,
4573 tree_to_uhwi (TYPE_SIZE (gnu_type
)));
4574 else if (Is_Atomic_Or_VFA (gnat_entity
) && gnu_size
4575 && tree_fits_uhwi_p (gnu_size
)
4576 && integer_pow2p (gnu_size
))
4577 align
= MIN (BIGGEST_ALIGNMENT
, tree_to_uhwi (gnu_size
));
4579 /* See if we need to pad the type. If we did, and made a record,
4580 the name of the new type may be changed. So get it back for
4581 us when we make the new TYPE_DECL below. */
4582 if (gnu_size
|| align
> 0)
4583 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
4584 false, !gnu_decl
, definition
, false);
4586 if (TYPE_IS_PADDING_P (gnu_type
))
4587 gnu_entity_name
= TYPE_IDENTIFIER (gnu_type
);
4589 /* Now set the RM size of the type. We cannot do it before padding
4590 because we need to accept arbitrary RM sizes on integral types. */
4591 set_rm_size (RM_Size (gnat_entity
), gnu_type
, gnat_entity
);
4593 /* If we are at global level, GCC will have applied variable_size to
4594 the type, but that won't have done anything. So, if it's not
4595 a constant or self-referential, call elaborate_expression_1 to
4596 make a variable for the size rather than calculating it each time.
4597 Handle both the RM size and the actual size. */
4598 if (global_bindings_p ()
4599 && TYPE_SIZE (gnu_type
)
4600 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
))
4601 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
4603 tree size
= TYPE_SIZE (gnu_type
);
4605 TYPE_SIZE (gnu_type
)
4606 = elaborate_expression_1 (size
, gnat_entity
, "SIZE", definition
,
4609 /* ??? For now, store the size as a multiple of the alignment in
4610 bytes so that we can see the alignment from the tree. */
4611 TYPE_SIZE_UNIT (gnu_type
)
4612 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type
), gnat_entity
,
4613 "SIZE_A_UNIT", definition
, false,
4614 TYPE_ALIGN (gnu_type
));
4616 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4617 may not be marked by the call to create_type_decl below. */
4618 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type
));
4620 if (TREE_CODE (gnu_type
) == RECORD_TYPE
)
4622 tree variant_part
= get_variant_part (gnu_type
);
4623 tree ada_size
= TYPE_ADA_SIZE (gnu_type
);
4627 tree union_type
= TREE_TYPE (variant_part
);
4628 tree offset
= DECL_FIELD_OFFSET (variant_part
);
4630 /* If the position of the variant part is constant, subtract
4631 it from the size of the type of the parent to get the new
4632 size. This manual CSE reduces the data size. */
4633 if (TREE_CODE (offset
) == INTEGER_CST
)
4635 tree bitpos
= DECL_FIELD_BIT_OFFSET (variant_part
);
4636 TYPE_SIZE (union_type
)
4637 = size_binop (MINUS_EXPR
, TYPE_SIZE (gnu_type
),
4638 bit_from_pos (offset
, bitpos
));
4639 TYPE_SIZE_UNIT (union_type
)
4640 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (gnu_type
),
4641 byte_from_pos (offset
, bitpos
));
4645 TYPE_SIZE (union_type
)
4646 = elaborate_expression_1 (TYPE_SIZE (union_type
),
4647 gnat_entity
, "VSIZE",
4650 /* ??? For now, store the size as a multiple of the
4651 alignment in bytes so that we can see the alignment
4653 TYPE_SIZE_UNIT (union_type
)
4654 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type
),
4655 gnat_entity
, "VSIZE_A_UNIT",
4657 TYPE_ALIGN (union_type
));
4659 /* ??? For now, store the offset as a multiple of the
4660 alignment in bytes so that we can see the alignment
4662 DECL_FIELD_OFFSET (variant_part
)
4663 = elaborate_expression_2 (offset
, gnat_entity
,
4664 "VOFFSET", definition
, false,
4669 DECL_SIZE (variant_part
) = TYPE_SIZE (union_type
);
4670 DECL_SIZE_UNIT (variant_part
) = TYPE_SIZE_UNIT (union_type
);
4673 if (operand_equal_p (ada_size
, size
, 0))
4674 ada_size
= TYPE_SIZE (gnu_type
);
4677 = elaborate_expression_1 (ada_size
, gnat_entity
, "RM_SIZE",
4679 SET_TYPE_ADA_SIZE (gnu_type
, ada_size
);
4683 /* If this is a record type or subtype, call elaborate_expression_2 on
4684 any field position. Do this for both global and local types.
4685 Skip any fields that we haven't made trees for to avoid problems with
4686 class wide types. */
4687 if (IN (kind
, Record_Kind
))
4688 for (gnat_temp
= First_Entity (gnat_entity
); Present (gnat_temp
);
4689 gnat_temp
= Next_Entity (gnat_temp
))
4690 if (Ekind (gnat_temp
) == E_Component
&& present_gnu_tree (gnat_temp
))
4692 tree gnu_field
= get_gnu_tree (gnat_temp
);
4694 /* ??? For now, store the offset as a multiple of the alignment
4695 in bytes so that we can see the alignment from the tree. */
4696 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field
)))
4698 DECL_FIELD_OFFSET (gnu_field
)
4699 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field
),
4700 gnat_temp
, "OFFSET", definition
,
4702 DECL_OFFSET_ALIGN (gnu_field
));
4704 /* ??? The context of gnu_field is not necessarily gnu_type
4705 so the MULT_EXPR node built above may not be marked by
4706 the call to create_type_decl below. */
4707 if (global_bindings_p ())
4708 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field
));
4712 if (Is_Atomic_Or_VFA (gnat_entity
))
4713 check_ok_for_atomic_type (gnu_type
, gnat_entity
, false);
4715 /* If this is not an unconstrained array type, set some flags. */
4716 if (TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
)
4718 if (Present (Alignment_Clause (gnat_entity
)))
4719 TYPE_USER_ALIGN (gnu_type
) = 1;
4721 if (Universal_Aliasing (gnat_entity
) && !TYPE_IS_DUMMY_P (gnu_type
))
4722 TYPE_UNIVERSAL_ALIASING_P (gnu_type
) = 1;
4724 /* If it is passed by reference, force BLKmode to ensure that
4725 objects of this type will always be put in memory. */
4726 if (TYPE_MODE (gnu_type
) != BLKmode
4727 && AGGREGATE_TYPE_P (gnu_type
)
4728 && TYPE_BY_REFERENCE_P (gnu_type
))
4729 SET_TYPE_MODE (gnu_type
, BLKmode
);
4731 if (Treat_As_Volatile (gnat_entity
))
4734 = TYPE_QUAL_VOLATILE
4735 | (Is_Atomic_Or_VFA (gnat_entity
) ? TYPE_QUAL_ATOMIC
: 0);
4736 gnu_type
= change_qualified_type (gnu_type
, quals
);
4741 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
4742 artificial_p
, debug_info_p
,
4746 TREE_TYPE (gnu_decl
) = gnu_type
;
4747 TYPE_STUB_DECL (gnu_type
) = gnu_decl
;
4751 if (is_type
&& !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
)))
4753 gnu_type
= TREE_TYPE (gnu_decl
);
4755 /* If this is a derived type, relate its alias set to that of its parent
4756 to avoid troubles when a call to an inherited primitive is inlined in
4757 a context where a derived object is accessed. The inlined code works
4758 on the parent view so the resulting code may access the same object
4759 using both the parent and the derived alias sets, which thus have to
4760 conflict. As the same issue arises with component references, the
4761 parent alias set also has to conflict with composite types enclosing
4762 derived components. For instance, if we have:
4769 we want T to conflict with both D and R, in addition to R being a
4770 superset of D by record/component construction.
4772 One way to achieve this is to perform an alias set copy from the
4773 parent to the derived type. This is not quite appropriate, though,
4774 as we don't want separate derived types to conflict with each other:
4776 type I1 is new Integer;
4777 type I2 is new Integer;
4779 We want I1 and I2 to both conflict with Integer but we do not want
4780 I1 to conflict with I2, and an alias set copy on derivation would
4783 The option chosen is to make the alias set of the derived type a
4784 superset of that of its parent type. It trivially fulfills the
4785 simple requirement for the Integer derivation example above, and
4786 the component case as well by superset transitivity:
4789 R ----------> D ----------> T
4791 However, for composite types, conversions between derived types are
4792 translated into VIEW_CONVERT_EXPRs so a sequence like:
4794 type Comp1 is new Comp;
4795 type Comp2 is new Comp;
4796 procedure Proc (C : Comp1);
4804 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
4806 and gimplified into:
4813 i.e. generates code involving type punning. Therefore, Comp1 needs
4814 to conflict with Comp2 and an alias set copy is required.
4816 The language rules ensure the parent type is already frozen here. */
4817 if (kind
!= E_Subprogram_Type
4818 && Is_Derived_Type (gnat_entity
)
4819 && !type_annotate_only
)
4821 Entity_Id gnat_parent_type
= Underlying_Type (Etype (gnat_entity
));
4822 /* For constrained packed array subtypes, the implementation type is
4823 used instead of the nominal type. */
4824 if (kind
== E_Array_Subtype
4825 && Is_Constrained (gnat_entity
)
4826 && Present (Packed_Array_Impl_Type (gnat_parent_type
)))
4827 gnat_parent_type
= Packed_Array_Impl_Type (gnat_parent_type
);
4828 relate_alias_sets (gnu_type
, gnat_to_gnu_type (gnat_parent_type
),
4829 Is_Composite_Type (gnat_entity
)
4830 ? ALIAS_SET_COPY
: ALIAS_SET_SUPERSET
);
4833 /* Back-annotate the Alignment of the type if not already in the
4834 tree. Likewise for sizes. */
4835 if (Unknown_Alignment (gnat_entity
))
4837 unsigned int double_align
, align
;
4838 bool is_capped_double
, align_clause
;
4840 /* If the default alignment of "double" or larger scalar types is
4841 specifically capped and this is not an array with an alignment
4842 clause on the component type, return the cap. */
4843 if ((double_align
= double_float_alignment
) > 0)
4845 = is_double_float_or_array (gnat_entity
, &align_clause
);
4846 else if ((double_align
= double_scalar_alignment
) > 0)
4848 = is_double_scalar_or_array (gnat_entity
, &align_clause
);
4850 is_capped_double
= align_clause
= false;
4852 if (is_capped_double
&& !align_clause
)
4853 align
= double_align
;
4855 align
= TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
;
4857 Set_Alignment (gnat_entity
, UI_From_Int (align
));
4860 if (Unknown_Esize (gnat_entity
) && TYPE_SIZE (gnu_type
))
4862 tree gnu_size
= TYPE_SIZE (gnu_type
);
4864 /* If the size is self-referential, annotate the maximum value. */
4865 if (CONTAINS_PLACEHOLDER_P (gnu_size
))
4866 gnu_size
= max_size (gnu_size
, true);
4868 /* If we are just annotating types and the type is tagged, the tag
4869 and the parent components are not generated by the front-end so
4870 alignment and sizes must be adjusted if there is no rep clause. */
4871 if (type_annotate_only
4872 && Is_Tagged_Type (gnat_entity
)
4873 && Unknown_RM_Size (gnat_entity
)
4874 && !VOID_TYPE_P (gnu_type
)
4875 && (!TYPE_FIELDS (gnu_type
)
4876 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type
)))))
4880 if (Is_Derived_Type (gnat_entity
))
4882 Entity_Id gnat_parent
= Etype (Base_Type (gnat_entity
));
4883 offset
= UI_To_gnu (Esize (gnat_parent
), bitsizetype
);
4884 Set_Alignment (gnat_entity
, Alignment (gnat_parent
));
4889 = MAX (TYPE_ALIGN (gnu_type
), POINTER_SIZE
) / BITS_PER_UNIT
;
4890 offset
= bitsize_int (POINTER_SIZE
);
4891 Set_Alignment (gnat_entity
, UI_From_Int (align
));
4894 if (TYPE_FIELDS (gnu_type
))
4896 = round_up (offset
, DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
4898 gnu_size
= size_binop (PLUS_EXPR
, gnu_size
, offset
);
4899 gnu_size
= round_up (gnu_size
, POINTER_SIZE
);
4900 Uint uint_size
= annotate_value (gnu_size
);
4901 Set_RM_Size (gnat_entity
, uint_size
);
4902 Set_Esize (gnat_entity
, uint_size
);
4905 /* If there is a rep clause, only adjust alignment and Esize. */
4906 else if (type_annotate_only
&& Is_Tagged_Type (gnat_entity
))
4909 = MAX (TYPE_ALIGN (gnu_type
), POINTER_SIZE
) / BITS_PER_UNIT
;
4910 Set_Alignment (gnat_entity
, UI_From_Int (align
));
4911 gnu_size
= round_up (gnu_size
, POINTER_SIZE
);
4912 Set_Esize (gnat_entity
, annotate_value (gnu_size
));
4915 /* Otherwise no adjustment is needed. */
4917 Set_Esize (gnat_entity
, annotate_value (gnu_size
));
4920 if (Unknown_RM_Size (gnat_entity
) && rm_size (gnu_type
))
4921 Set_RM_Size (gnat_entity
, annotate_value (rm_size (gnu_type
)));
4924 /* If we haven't already, associate the ..._DECL node that we just made with
4925 the input GNAT entity node. */
4927 save_gnu_tree (gnat_entity
, gnu_decl
, false);
4929 /* Now we are sure gnat_entity has a corresponding ..._DECL node,
4930 eliminate as many deferred computations as possible. */
4931 process_deferred_decl_context (false);
4933 /* If this is an enumeration or floating-point type, we were not able to set
4934 the bounds since they refer to the type. These are always static. */
4935 if ((kind
== E_Enumeration_Type
&& Present (First_Literal (gnat_entity
)))
4936 || (kind
== E_Floating_Point_Type
))
4938 tree gnu_scalar_type
= gnu_type
;
4939 tree gnu_low_bound
, gnu_high_bound
;
4941 /* If this is a padded type, we need to use the underlying type. */
4942 if (TYPE_IS_PADDING_P (gnu_scalar_type
))
4943 gnu_scalar_type
= TREE_TYPE (TYPE_FIELDS (gnu_scalar_type
));
4945 /* If this is a floating point type and we haven't set a floating
4946 point type yet, use this in the evaluation of the bounds. */
4947 if (!longest_float_type_node
&& kind
== E_Floating_Point_Type
)
4948 longest_float_type_node
= gnu_scalar_type
;
4950 gnu_low_bound
= gnat_to_gnu (Type_Low_Bound (gnat_entity
));
4951 gnu_high_bound
= gnat_to_gnu (Type_High_Bound (gnat_entity
));
4953 if (kind
== E_Enumeration_Type
)
4955 /* Enumeration types have specific RM bounds. */
4956 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type
, gnu_low_bound
);
4957 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type
, gnu_high_bound
);
4961 /* Floating-point types don't have specific RM bounds. */
4962 TYPE_GCC_MIN_VALUE (gnu_scalar_type
) = gnu_low_bound
;
4963 TYPE_GCC_MAX_VALUE (gnu_scalar_type
) = gnu_high_bound
;
4967 /* If we deferred processing of incomplete types, re-enable it. If there
4968 were no other disables and we have deferred types to process, do so. */
4970 && --defer_incomplete_level
== 0
4971 && defer_incomplete_list
)
4973 struct incomplete
*p
, *next
;
4975 /* We are back to level 0 for the deferring of incomplete types.
4976 But processing these incomplete types below may itself require
4977 deferring, so preserve what we have and restart from scratch. */
4978 p
= defer_incomplete_list
;
4979 defer_incomplete_list
= NULL
;
4986 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
4987 gnat_to_gnu_type (p
->full_type
));
4992 /* If we are not defining this type, see if it's on one of the lists of
4993 incomplete types. If so, handle the list entry now. */
4994 if (is_type
&& !definition
)
4996 struct incomplete
*p
;
4998 for (p
= defer_incomplete_list
; p
; p
= p
->next
)
4999 if (p
->old_type
&& p
->full_type
== gnat_entity
)
5001 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5002 TREE_TYPE (gnu_decl
));
5003 p
->old_type
= NULL_TREE
;
5006 for (p
= defer_limited_with_list
; p
; p
= p
->next
)
5007 if (p
->old_type
&& Non_Limited_View (p
->full_type
) == gnat_entity
)
5009 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5010 TREE_TYPE (gnu_decl
));
5011 p
->old_type
= NULL_TREE
;
5018 /* If this is a packed array type whose original array type is itself
5019 an Itype without freeze node, make sure the latter is processed. */
5020 if (Is_Packed_Array_Impl_Type (gnat_entity
)
5021 && Is_Itype (Original_Array_Type (gnat_entity
))
5022 && No (Freeze_Node (Original_Array_Type (gnat_entity
)))
5023 && !present_gnu_tree (Original_Array_Type (gnat_entity
)))
5024 gnat_to_gnu_entity (Original_Array_Type (gnat_entity
), NULL_TREE
, false);
5029 /* Similar, but if the returned value is a COMPONENT_REF, return the
5033 gnat_to_gnu_field_decl (Entity_Id gnat_entity
)
5035 tree gnu_field
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, false);
5037 if (TREE_CODE (gnu_field
) == COMPONENT_REF
)
5038 gnu_field
= TREE_OPERAND (gnu_field
, 1);
5043 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5044 the GCC type corresponding to that entity. */
5047 gnat_to_gnu_type (Entity_Id gnat_entity
)
5051 /* The back end never attempts to annotate generic types. */
5052 if (Is_Generic_Type (gnat_entity
) && type_annotate_only
)
5053 return void_type_node
;
5055 gnu_decl
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, false);
5056 gcc_assert (TREE_CODE (gnu_decl
) == TYPE_DECL
);
5058 return TREE_TYPE (gnu_decl
);
5061 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5062 the unpadded version of the GCC type corresponding to that entity. */
5065 get_unpadded_type (Entity_Id gnat_entity
)
5067 tree type
= gnat_to_gnu_type (gnat_entity
);
5069 if (TYPE_IS_PADDING_P (type
))
5070 type
= TREE_TYPE (TYPE_FIELDS (type
));
5075 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5076 a C++ imported method or equivalent.
5078 We use the predicate on 32-bit x86/Windows to find out whether we need to
5079 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5080 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5083 is_cplusplus_method (Entity_Id gnat_entity
)
5085 /* Check that the subprogram has C++ convention. */
5086 if (Convention (gnat_entity
) != Convention_CPP
)
5089 /* A constructor is a method on the C++ side. We deal with it now because
5090 it is declared without the 'this' parameter in the sources and, although
5091 the front-end will create a version with the 'this' parameter for code
5092 generation purposes, we want to return true for both versions. */
5093 if (Is_Constructor (gnat_entity
))
5096 /* And that the type of the first parameter (indirectly) has it too. */
5097 Entity_Id gnat_first
= First_Formal (gnat_entity
);
5098 if (No (gnat_first
))
5101 Entity_Id gnat_type
= Etype (gnat_first
);
5102 if (Is_Access_Type (gnat_type
))
5103 gnat_type
= Directly_Designated_Type (gnat_type
);
5104 if (Convention (gnat_type
) != Convention_CPP
)
5107 /* This is the main case: C++ method imported as a primitive operation.
5108 Note that a C++ class with no virtual functions can be imported as a
5109 limited record type so the operation is not necessarily dispatching. */
5110 if (Is_Primitive (gnat_entity
))
5113 /* A thunk needs to be handled like its associated primitive operation. */
5114 if (Is_Subprogram (gnat_entity
) && Is_Thunk (gnat_entity
))
5117 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5118 if (Is_Dispatch_Table_Entity (gnat_entity
))
5124 /* Finalize the processing of From_Limited_With incomplete types. */
5127 finalize_from_limited_with (void)
5129 struct incomplete
*p
, *next
;
5131 p
= defer_limited_with_list
;
5132 defer_limited_with_list
= NULL
;
5140 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5141 gnat_to_gnu_type (p
->full_type
));
5142 if (TYPE_DUMMY_IN_PROFILE_P (p
->old_type
))
5143 update_profiles_with (p
->old_type
);
5150 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5151 kind of type (such E_Task_Type) that has a different type which Gigi
5152 uses for its representation. If the type does not have a special type
5153 for its representation, return GNAT_ENTITY. If a type is supposed to
5154 exist, but does not, abort unless annotating types, in which case
5155 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5158 Gigi_Equivalent_Type (Entity_Id gnat_entity
)
5160 Entity_Id gnat_equiv
= gnat_entity
;
5162 if (No (gnat_entity
))
5165 switch (Ekind (gnat_entity
))
5167 case E_Class_Wide_Subtype
:
5168 if (Present (Equivalent_Type (gnat_entity
)))
5169 gnat_equiv
= Equivalent_Type (gnat_entity
);
5172 case E_Access_Protected_Subprogram_Type
:
5173 case E_Anonymous_Access_Protected_Subprogram_Type
:
5174 gnat_equiv
= Equivalent_Type (gnat_entity
);
5177 case E_Class_Wide_Type
:
5178 gnat_equiv
= Root_Type (gnat_entity
);
5182 case E_Task_Subtype
:
5183 case E_Protected_Type
:
5184 case E_Protected_Subtype
:
5185 gnat_equiv
= Corresponding_Record_Type (gnat_entity
);
5192 gcc_assert (Present (gnat_equiv
) || type_annotate_only
);
5197 /* Return a GCC tree for a type corresponding to the component type of the
5198 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5199 is for an array being defined. DEBUG_INFO_P is true if we need to write
5200 debug information for other types that we may create in the process. */
5203 gnat_to_gnu_component_type (Entity_Id gnat_array
, bool definition
,
5206 const Entity_Id gnat_type
= Component_Type (gnat_array
);
5207 tree gnu_type
= gnat_to_gnu_type (gnat_type
);
5209 unsigned int max_align
;
5211 /* If an alignment is specified, use it as a cap on the component type
5212 so that it can be honored for the whole type. But ignore it for the
5213 original type of packed array types. */
5214 if (No (Packed_Array_Impl_Type (gnat_array
))
5215 && Known_Alignment (gnat_array
))
5216 max_align
= validate_alignment (Alignment (gnat_array
), gnat_array
, 0);
5220 /* Try to get a smaller form of the component if needed. */
5221 if ((Is_Packed (gnat_array
) || Has_Component_Size_Clause (gnat_array
))
5222 && !Is_Bit_Packed_Array (gnat_array
)
5223 && !Has_Aliased_Components (gnat_array
)
5224 && !Strict_Alignment (gnat_type
)
5225 && RECORD_OR_UNION_TYPE_P (gnu_type
)
5226 && !TYPE_FAT_POINTER_P (gnu_type
)
5227 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type
)))
5228 gnu_type
= make_packable_type (gnu_type
, false, max_align
);
5230 if (Has_Atomic_Components (gnat_array
))
5231 check_ok_for_atomic_type (gnu_type
, gnat_array
, true);
5233 /* Get and validate any specified Component_Size. */
5235 = validate_size (Component_Size (gnat_array
), gnu_type
, gnat_array
,
5236 Is_Bit_Packed_Array (gnat_array
) ? TYPE_DECL
: VAR_DECL
,
5237 true, Has_Component_Size_Clause (gnat_array
));
5239 /* If the array has aliased components and the component size can be zero,
5240 force at least unit size to ensure that the components have distinct
5243 && Has_Aliased_Components (gnat_array
)
5244 && (integer_zerop (TYPE_SIZE (gnu_type
))
5245 || (TREE_CODE (gnu_type
) == ARRAY_TYPE
5246 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
)))))
5248 = size_binop (MAX_EXPR
, TYPE_SIZE (gnu_type
), bitsize_unit_node
);
5250 /* If the component type is a RECORD_TYPE that has a self-referential size,
5251 then use the maximum size for the component size. */
5253 && TREE_CODE (gnu_type
) == RECORD_TYPE
5254 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
5255 gnu_comp_size
= max_size (TYPE_SIZE (gnu_type
), true);
5257 /* Honor the component size. This is not needed for bit-packed arrays. */
5258 if (gnu_comp_size
&& !Is_Bit_Packed_Array (gnat_array
))
5260 tree orig_type
= gnu_type
;
5262 gnu_type
= make_type_from_size (gnu_type
, gnu_comp_size
, false);
5263 if (max_align
> 0 && TYPE_ALIGN (gnu_type
) > max_align
)
5264 gnu_type
= orig_type
;
5266 orig_type
= gnu_type
;
5268 gnu_type
= maybe_pad_type (gnu_type
, gnu_comp_size
, 0, gnat_array
,
5269 true, false, definition
, true);
5271 /* If a padding record was made, declare it now since it will never be
5272 declared otherwise. This is necessary to ensure that its subtrees
5273 are properly marked. */
5274 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
5275 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true, debug_info_p
,
5279 /* If the component type is a padded type made for a non-bit-packed array
5280 of scalars with reverse storage order, we need to propagate the reverse
5281 storage order to the padding type since it is the innermost enclosing
5282 aggregate type around the scalar. */
5283 if (TYPE_IS_PADDING_P (gnu_type
)
5284 && Reverse_Storage_Order (gnat_array
)
5285 && !Is_Bit_Packed_Array (gnat_array
)
5286 && Is_Scalar_Type (gnat_type
))
5287 gnu_type
= set_reverse_storage_order_on_pad_type (gnu_type
);
5289 if (Has_Volatile_Components (gnat_array
))
5292 = TYPE_QUAL_VOLATILE
5293 | (Has_Atomic_Components (gnat_array
) ? TYPE_QUAL_ATOMIC
: 0);
5294 gnu_type
= change_qualified_type (gnu_type
, quals
);
5300 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM, to be placed
5301 in the parameter list built for GNAT_SUBPROG. FIRST is true if GNAT_PARAM
5302 is the first parameter in the list. Also set CICO to true if the parameter
5303 must use the copy-in copy-out implementation mechanism.
5305 The returned tree is a PARM_DECL, except for those cases where no
5306 parameter needs to be actually passed to the subprogram; the type
5307 of this "shadow" parameter is then returned instead. */
5310 gnat_to_gnu_param (Entity_Id gnat_param
, bool first
, Entity_Id gnat_subprog
,
5313 Entity_Id gnat_param_type
= Etype (gnat_param
);
5314 Mechanism_Type mech
= Mechanism (gnat_param
);
5315 tree gnu_param_name
= get_entity_name (gnat_param
);
5316 tree gnu_param_type
= gnat_to_gnu_type (gnat_param_type
);
5317 bool foreign
= Has_Foreign_Convention (gnat_subprog
);
5318 bool in_param
= (Ekind (gnat_param
) == E_In_Parameter
);
5319 /* The parameter can be indirectly modified if its address is taken. */
5320 bool ro_param
= in_param
&& !Address_Taken (gnat_param
);
5321 bool by_return
= false, by_component_ptr
= false;
5322 bool by_ref
= false;
5323 bool restricted_aliasing_p
= false;
5326 /* Builtins are expanded inline and there is no real call sequence involved.
5327 So the type expected by the underlying expander is always the type of the
5328 argument "as is". */
5329 if (Convention (gnat_subprog
) == Convention_Intrinsic
5330 && Present (Interface_Name (gnat_subprog
)))
5333 /* Handle the first parameter of a valued procedure specially: it's a copy
5334 mechanism for which the parameter is never allocated. */
5335 else if (first
&& Is_Valued_Procedure (gnat_subprog
))
5337 gcc_assert (Ekind (gnat_param
) == E_Out_Parameter
);
5342 /* Or else, see if a Mechanism was supplied that forced this parameter
5343 to be passed one way or another. */
5344 else if (mech
== Default
|| mech
== By_Copy
|| mech
== By_Reference
)
5347 /* Positive mechanism means by copy for sufficiently small parameters. */
5350 if (TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
5351 || TREE_CODE (TYPE_SIZE (gnu_param_type
)) != INTEGER_CST
5352 || compare_tree_int (TYPE_SIZE (gnu_param_type
), mech
) > 0)
5353 mech
= By_Reference
;
5358 /* Otherwise, it's an unsupported mechanism so error out. */
5361 post_error ("unsupported mechanism for&", gnat_param
);
5365 /* If this is either a foreign function or if the underlying type won't
5366 be passed by reference and is as aligned as the original type, strip
5367 off possible padding type. */
5368 if (TYPE_IS_PADDING_P (gnu_param_type
))
5370 tree unpadded_type
= TREE_TYPE (TYPE_FIELDS (gnu_param_type
));
5373 || (!must_pass_by_ref (unpadded_type
)
5374 && mech
!= By_Reference
5375 && (mech
== By_Copy
|| !default_pass_by_ref (unpadded_type
))
5376 && TYPE_ALIGN (unpadded_type
) >= TYPE_ALIGN (gnu_param_type
)))
5377 gnu_param_type
= unpadded_type
;
5380 /* If this is a read-only parameter, make a variant of the type that is
5381 read-only. ??? However, if this is an unconstrained array, that type
5382 can be very complex, so skip it for now. Likewise for any other
5383 self-referential type. */
5385 && TREE_CODE (gnu_param_type
) != UNCONSTRAINED_ARRAY_TYPE
5386 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type
)))
5387 gnu_param_type
= change_qualified_type (gnu_param_type
, TYPE_QUAL_CONST
);
5389 /* For foreign conventions, pass arrays as pointers to the element type.
5390 First check for unconstrained array and get the underlying array. */
5391 if (foreign
&& TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5393 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type
))));
5395 /* For GCC builtins, pass Address integer types as (void *) */
5396 if (Convention (gnat_subprog
) == Convention_Intrinsic
5397 && Present (Interface_Name (gnat_subprog
))
5398 && Is_Descendant_Of_Address (gnat_param_type
))
5399 gnu_param_type
= ptr_type_node
;
5401 /* Arrays are passed as pointers to element type for foreign conventions. */
5402 if (foreign
&& mech
!= By_Copy
&& TREE_CODE (gnu_param_type
) == ARRAY_TYPE
)
5404 /* Strip off any multi-dimensional entries, then strip
5405 off the last array to get the component type. */
5406 while (TREE_CODE (TREE_TYPE (gnu_param_type
)) == ARRAY_TYPE
5407 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type
)))
5408 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5410 by_component_ptr
= true;
5411 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5415 = change_qualified_type (gnu_param_type
, TYPE_QUAL_CONST
);
5417 gnu_param_type
= build_pointer_type (gnu_param_type
);
5420 /* Fat pointers are passed as thin pointers for foreign conventions. */
5421 else if (foreign
&& TYPE_IS_FAT_POINTER_P (gnu_param_type
))
5423 = make_type_from_size (gnu_param_type
, size_int (POINTER_SIZE
), 0);
5425 /* If we were requested or muss pass by reference, do so.
5426 If we were requested to pass by copy, do so.
5427 Otherwise, for foreign conventions, pass In Out or Out parameters
5428 or aggregates by reference. For COBOL and Fortran, pass all
5429 integer and FP types that way too. For Convention Ada, use
5430 the standard Ada default. */
5431 else if (mech
== By_Reference
5432 || must_pass_by_ref (gnu_param_type
)
5435 && (!in_param
|| AGGREGATE_TYPE_P (gnu_param_type
)))
5437 && (Convention (gnat_subprog
) == Convention_Fortran
5438 || Convention (gnat_subprog
) == Convention_COBOL
)
5439 && (INTEGRAL_TYPE_P (gnu_param_type
)
5440 || FLOAT_TYPE_P (gnu_param_type
)))
5442 && default_pass_by_ref (gnu_param_type
)))))
5444 /* We take advantage of 6.2(12) by considering that references built for
5445 parameters whose type isn't by-ref and for which the mechanism hasn't
5446 been forced to by-ref allow only a restricted form of aliasing. */
5447 restricted_aliasing_p
5448 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type
) && mech
!= By_Reference
;
5449 gnu_param_type
= build_reference_type (gnu_param_type
);
5453 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5457 if (mech
== By_Copy
&& (by_ref
|| by_component_ptr
))
5458 post_error ("?cannot pass & by copy", gnat_param
);
5460 /* If this is an Out parameter that isn't passed by reference and isn't
5461 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5462 it will be a VAR_DECL created when we process the procedure, so just
5463 return its type. For the special parameter of a valued procedure,
5466 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5467 Out parameters with discriminants or implicit initial values to be
5468 handled like In Out parameters. These type are normally built as
5469 aggregates, hence passed by reference, except for some packed arrays
5470 which end up encoded in special integer types. Note that scalars can
5471 be given implicit initial values using the Default_Value aspect.
5473 The exception we need to make is then for packed arrays of records
5474 with discriminants or implicit initial values. We have no light/easy
5475 way to check for the latter case, so we merely check for packed arrays
5476 of records. This may lead to useless copy-in operations, but in very
5477 rare cases only, as these would be exceptions in a set of already
5478 exceptional situations. */
5479 if (Ekind (gnat_param
) == E_Out_Parameter
5482 || (!POINTER_TYPE_P (gnu_param_type
)
5483 && !AGGREGATE_TYPE_P (gnu_param_type
)
5484 && !Has_Default_Aspect (gnat_param_type
)))
5485 && !(Is_Array_Type (gnat_param_type
)
5486 && Is_Packed (gnat_param_type
)
5487 && Is_Composite_Type (Component_Type (gnat_param_type
))))
5488 return gnu_param_type
;
5490 gnu_param
= create_param_decl (gnu_param_name
, gnu_param_type
);
5491 TREE_READONLY (gnu_param
) = ro_param
|| by_ref
|| by_component_ptr
;
5492 DECL_BY_REF_P (gnu_param
) = by_ref
;
5493 DECL_BY_COMPONENT_PTR_P (gnu_param
) = by_component_ptr
;
5494 DECL_POINTS_TO_READONLY_P (gnu_param
)
5495 = (ro_param
&& (by_ref
|| by_component_ptr
));
5496 DECL_CAN_NEVER_BE_NULL_P (gnu_param
) = Can_Never_Be_Null (gnat_param
);
5497 DECL_RESTRICTED_ALIASING_P (gnu_param
) = restricted_aliasing_p
;
5498 Sloc_to_locus (Sloc (gnat_param
), &DECL_SOURCE_LOCATION (gnu_param
));
5500 /* If no Mechanism was specified, indicate what we're using, then
5501 back-annotate it. */
5502 if (mech
== Default
)
5503 mech
= (by_ref
|| by_component_ptr
) ? By_Reference
: By_Copy
;
5505 Set_Mechanism (gnat_param
, mech
);
5509 /* Associate GNAT_SUBPROG with GNU_TYPE, which must be a dummy type, so that
5510 GNAT_SUBPROG is updated when TYPE is completed. */
5513 associate_subprog_with_dummy_type (Entity_Id gnat_subprog
, tree gnu_type
)
5515 gcc_assert (TYPE_IS_DUMMY_P (gnu_type
));
5517 struct tree_entity_vec_map in
;
5518 in
.base
.from
= gnu_type
;
5519 struct tree_entity_vec_map
**slot
5520 = dummy_to_subprog_map
->find_slot (&in
, INSERT
);
5523 tree_entity_vec_map
*e
= ggc_alloc
<tree_entity_vec_map
> ();
5524 e
->base
.from
= gnu_type
;
5527 TYPE_DUMMY_IN_PROFILE_P (gnu_type
) = 1;
5529 vec
<Entity_Id
, va_gc_atomic
> *v
= (*slot
)->to
;
5531 /* Make sure GNAT_SUBPROG is not associated twice with the same dummy type,
5532 since this would mean updating twice its profile. */
5535 const unsigned len
= v
->length ();
5536 unsigned int l
= 0, u
= len
;
5538 /* Entity_Id is a simple integer so we can implement a stable order on
5539 the vector with an ordered insertion scheme and binary search. */
5542 unsigned int m
= (l
+ u
) / 2;
5543 int diff
= (int) (*v
)[m
] - (int) gnat_subprog
;
5552 /* l == u and therefore is the insertion point. */
5553 vec_safe_insert (v
, l
, gnat_subprog
);
5556 vec_safe_push (v
, gnat_subprog
);
5561 /* Update the GCC tree previously built for the profile of GNAT_SUBPROG. */
5564 update_profile (Entity_Id gnat_subprog
)
5566 tree gnu_param_list
;
5567 tree gnu_type
= gnat_to_gnu_subprog_type (gnat_subprog
, true,
5568 Needs_Debug_Info (gnat_subprog
),
5570 tree gnu_subprog
= get_gnu_tree (gnat_subprog
);
5572 TREE_TYPE (gnu_subprog
) = gnu_type
;
5574 /* If GNAT_SUBPROG is an actual subprogram, GNU_SUBPROG is a FUNCTION_DECL
5575 and needs to be adjusted too. */
5576 if (Ekind (gnat_subprog
) != E_Subprogram_Type
)
5578 DECL_ARGUMENTS (gnu_subprog
) = gnu_param_list
;
5579 finish_subprog_decl (gnu_subprog
, gnu_type
);
5583 /* Update the GCC trees previously built for the profiles involving GNU_TYPE,
5584 a dummy type which appears in profiles. */
5587 update_profiles_with (tree gnu_type
)
5589 struct tree_entity_vec_map in
;
5590 in
.base
.from
= gnu_type
;
5591 struct tree_entity_vec_map
*e
= dummy_to_subprog_map
->find (&in
);
5593 vec
<Entity_Id
, va_gc_atomic
> *v
= e
->to
;
5595 TYPE_DUMMY_IN_PROFILE_P (gnu_type
) = 0;
5599 FOR_EACH_VEC_ELT (*v
, i
, iter
)
5600 update_profile (*iter
);
5605 /* Return the GCC tree for GNAT_TYPE present in the profile of a subprogram.
5607 Ada 2012 (AI05-0151) says that incomplete types coming from a limited
5608 context may now appear as parameter and result types. As a consequence,
5609 we may need to defer their translation until after a freeze node is seen
5610 or to the end of the current unit. We also aim at handling temporarily
5611 incomplete types created by the usual delayed elaboration scheme. */
5614 gnat_to_gnu_profile_type (Entity_Id gnat_type
)
5616 /* This is the same logic as the E_Access_Type case of gnat_to_gnu_entity
5617 so the rationale is exposed in that place. These processings probably
5618 ought to be merged at some point. */
5619 Entity_Id gnat_equiv
= Gigi_Equivalent_Type (gnat_type
);
5620 const bool is_from_limited_with
5621 = (IN (Ekind (gnat_equiv
), Incomplete_Kind
)
5622 && From_Limited_With (gnat_equiv
));
5623 Entity_Id gnat_full_direct_first
5624 = (is_from_limited_with
5625 ? Non_Limited_View (gnat_equiv
)
5626 : (IN (Ekind (gnat_equiv
), Incomplete_Or_Private_Kind
)
5627 ? Full_View (gnat_equiv
) : Empty
));
5628 Entity_Id gnat_full_direct
5629 = ((is_from_limited_with
5630 && Present (gnat_full_direct_first
)
5631 && IN (Ekind (gnat_full_direct_first
), Private_Kind
))
5632 ? Full_View (gnat_full_direct_first
)
5633 : gnat_full_direct_first
);
5634 Entity_Id gnat_full
= Gigi_Equivalent_Type (gnat_full_direct
);
5635 Entity_Id gnat_rep
= Present (gnat_full
) ? gnat_full
: gnat_equiv
;
5636 const bool in_main_unit
= In_Extended_Main_Code_Unit (gnat_rep
);
5639 if (Present (gnat_full
) && present_gnu_tree (gnat_full
))
5640 gnu_type
= TREE_TYPE (get_gnu_tree (gnat_full
));
5642 else if (is_from_limited_with
5644 && !present_gnu_tree (gnat_equiv
)
5645 && Present (gnat_full
)
5646 && (Is_Record_Type (gnat_full
) || Is_Array_Type (gnat_full
)))
5647 || (in_main_unit
&& Present (Freeze_Node (gnat_rep
)))))
5649 gnu_type
= make_dummy_type (gnat_equiv
);
5653 struct incomplete
*p
= XNEW (struct incomplete
);
5655 p
->old_type
= gnu_type
;
5656 p
->full_type
= gnat_equiv
;
5657 p
->next
= defer_limited_with_list
;
5658 defer_limited_with_list
= p
;
5662 else if (type_annotate_only
&& No (gnat_equiv
))
5663 gnu_type
= void_type_node
;
5666 gnu_type
= gnat_to_gnu_type (gnat_equiv
);
5668 /* Access-to-unconstrained-array types need a special treatment. */
5669 if (Is_Array_Type (gnat_rep
) && !Is_Constrained (gnat_rep
))
5671 if (!TYPE_POINTER_TO (gnu_type
))
5672 build_dummy_unc_pointer_types (gnat_equiv
, gnu_type
);
5678 /* Return a GCC tree for a subprogram type corresponding to GNAT_SUBPROG.
5679 DEFINITION is true if this is for a subprogram being defined. DEBUG_INFO_P
5680 is true if we need to write debug information for other types that we may
5681 create in the process. Also set PARAM_LIST to the list of parameters. */
5684 gnat_to_gnu_subprog_type (Entity_Id gnat_subprog
, bool definition
,
5685 bool debug_info_p
, tree
*param_list
)
5687 const Entity_Kind kind
= Ekind (gnat_subprog
);
5688 Entity_Id gnat_return_type
= Etype (gnat_subprog
);
5689 Entity_Id gnat_param
;
5690 tree gnu_return_type
;
5691 tree gnu_param_type_list
= NULL_TREE
;
5692 tree gnu_param_list
= NULL_TREE
;
5693 /* Non-null for subprograms containing parameters passed by copy-in copy-out
5694 (In Out or Out parameters not passed by reference), in which case it is
5695 the list of nodes used to specify the values of the In Out/Out parameters
5696 that are returned as a record upon procedure return. The TREE_PURPOSE of
5697 an element of this list is a FIELD_DECL of the record and the TREE_VALUE
5698 is the PARM_DECL corresponding to that field. This list will be saved in
5699 the TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
5700 tree gnu_cico_list
= NULL_TREE
;
5701 /* Fields in return type of procedure with copy-in copy-out parameters. */
5702 tree gnu_field_list
= NULL_TREE
;
5703 /* The semantics of "pure" in Ada essentially matches that of "const"
5704 in the back-end. In particular, both properties are orthogonal to
5705 the "nothrow" property if the EH circuitry is explicit in the
5706 internal representation of the back-end. If we are to completely
5707 hide the EH circuitry from it, we need to declare that calls to pure
5708 Ada subprograms that can throw have side effects since they can
5709 trigger an "abnormal" transfer of control flow; thus they can be
5710 neither "const" nor "pure" in the back-end sense. */
5711 bool const_flag
= (Back_End_Exceptions () && Is_Pure (gnat_subprog
));
5712 bool return_by_direct_ref_p
= false;
5713 bool return_by_invisi_ref_p
= false;
5714 bool return_unconstrained_p
= false;
5715 bool incomplete_profile_p
= false;
5718 /* Look into the return type and get its associated GCC tree. If it is not
5719 void, compute various flags for the subprogram type. */
5720 if (Ekind (gnat_return_type
) == E_Void
)
5721 gnu_return_type
= void_type_node
;
5724 gnu_return_type
= gnat_to_gnu_profile_type (gnat_return_type
);
5726 /* If this function returns by reference, make the actual return type
5727 the reference type and make a note of that. */
5728 if (Returns_By_Ref (gnat_subprog
))
5730 gnu_return_type
= build_reference_type (gnu_return_type
);
5731 return_by_direct_ref_p
= true;
5734 /* If the return type is an unconstrained array type, the return value
5735 will be allocated on the secondary stack so the actual return type
5736 is the fat pointer type. */
5737 else if (TREE_CODE (gnu_return_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5739 gnu_return_type
= TYPE_REFERENCE_TO (gnu_return_type
);
5740 return_unconstrained_p
= true;
5743 /* This is the same unconstrained array case, but for a dummy type. */
5744 else if (TYPE_REFERENCE_TO (gnu_return_type
)
5745 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_return_type
)))
5747 gnu_return_type
= TYPE_REFERENCE_TO (gnu_return_type
);
5748 return_unconstrained_p
= true;
5751 /* Likewise, if the return type requires a transient scope, the return
5752 value will also be allocated on the secondary stack so the actual
5753 return type is the reference type. */
5754 else if (Requires_Transient_Scope (gnat_return_type
))
5756 gnu_return_type
= build_reference_type (gnu_return_type
);
5757 return_unconstrained_p
= true;
5760 /* If the Mechanism is By_Reference, ensure this function uses the
5761 target's by-invisible-reference mechanism, which may not be the
5762 same as above (e.g. it might be passing an extra parameter). */
5763 else if (kind
== E_Function
&& Mechanism (gnat_subprog
) == By_Reference
)
5764 return_by_invisi_ref_p
= true;
5766 /* Likewise, if the return type is itself By_Reference. */
5767 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type
))
5768 return_by_invisi_ref_p
= true;
5770 /* If the type is a padded type and the underlying type would not be
5771 passed by reference or the function has a foreign convention, return
5772 the underlying type. */
5773 else if (TYPE_IS_PADDING_P (gnu_return_type
)
5774 && (!default_pass_by_ref
5775 (TREE_TYPE (TYPE_FIELDS (gnu_return_type
)))
5776 || Has_Foreign_Convention (gnat_subprog
)))
5777 gnu_return_type
= TREE_TYPE (TYPE_FIELDS (gnu_return_type
));
5779 /* If the return type is unconstrained, it must have a maximum size.
5780 Use the padded type as the effective return type. And ensure the
5781 function uses the target's by-invisible-reference mechanism to
5782 avoid copying too much data when it returns. */
5783 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type
)))
5785 tree orig_type
= gnu_return_type
;
5786 tree max_return_size
= max_size (TYPE_SIZE (gnu_return_type
), true);
5788 /* If the size overflows to 0, set it to an arbitrary positive
5789 value so that assignments in the type are preserved. Their
5790 actual size is independent of this positive value. */
5791 if (TREE_CODE (max_return_size
) == INTEGER_CST
5792 && TREE_OVERFLOW (max_return_size
)
5793 && integer_zerop (max_return_size
))
5795 max_return_size
= copy_node (bitsize_unit_node
);
5796 TREE_OVERFLOW (max_return_size
) = 1;
5799 gnu_return_type
= maybe_pad_type (gnu_return_type
, max_return_size
,
5800 0, gnat_subprog
, false, false,
5803 /* Declare it now since it will never be declared otherwise. This
5804 is necessary to ensure that its subtrees are properly marked. */
5805 if (gnu_return_type
!= orig_type
5806 && !DECL_P (TYPE_NAME (gnu_return_type
)))
5807 create_type_decl (TYPE_NAME (gnu_return_type
), gnu_return_type
,
5808 true, debug_info_p
, gnat_subprog
);
5810 return_by_invisi_ref_p
= true;
5813 /* If the return type has a size that overflows, we usually cannot have
5814 a function that returns that type. This usage doesn't really make
5815 sense anyway, so issue an error here. */
5816 if (!return_by_invisi_ref_p
5817 && TYPE_SIZE_UNIT (gnu_return_type
)
5818 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type
)) == INTEGER_CST
5819 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type
)))
5821 post_error ("cannot return type whose size overflows", gnat_subprog
);
5822 gnu_return_type
= copy_type (gnu_return_type
);
5823 TYPE_SIZE (gnu_return_type
) = bitsize_zero_node
;
5824 TYPE_SIZE_UNIT (gnu_return_type
) = size_zero_node
;
5827 /* If the return type is incomplete, there are 2 cases: if the function
5828 returns by reference, then the return type is only linked indirectly
5829 in the profile, so the profile can be seen as complete since it need
5830 not be further modified, only the reference types need be adjusted;
5831 otherwise the profile itself is incomplete and need be adjusted. */
5832 if (TYPE_IS_DUMMY_P (gnu_return_type
))
5834 associate_subprog_with_dummy_type (gnat_subprog
, gnu_return_type
);
5835 incomplete_profile_p
= true;
5838 if (kind
== E_Function
)
5839 Set_Mechanism (gnat_subprog
, return_unconstrained_p
5840 || return_by_direct_ref_p
5841 || return_by_invisi_ref_p
5842 ? By_Reference
: By_Copy
);
5845 /* A procedure (something that doesn't return anything) shouldn't be
5846 considered const since there would be no reason for calling such a
5847 subprogram. Note that procedures with Out (or In Out) parameters
5848 have already been converted into a function with a return type.
5849 Similarly, if the function returns an unconstrained type, then the
5850 function will allocate the return value on the secondary stack and
5851 thus calls to it cannot be CSE'ed, lest the stack be reclaimed. */
5852 if (TREE_CODE (gnu_return_type
) == VOID_TYPE
|| return_unconstrained_p
)
5855 /* Loop over the parameters and get their associated GCC tree. While doing
5856 this, build a copy-in copy-out structure if we need one. */
5857 for (gnat_param
= First_Formal_With_Extras (gnat_subprog
), num
= 0;
5858 Present (gnat_param
);
5859 gnat_param
= Next_Formal_With_Extras (gnat_param
), num
++)
5861 Entity_Id gnat_param_type
= Etype (gnat_param
);
5862 tree gnu_param_name
= get_entity_name (gnat_param
);
5863 tree gnu_param_type
= gnat_to_gnu_profile_type (gnat_param_type
);
5864 tree gnu_param
, gnu_field
;
5867 /* If the parameter type is incomplete, there are 2 cases: if it is
5868 passed by reference, then the type is only linked indirectly in
5869 the profile, so the profile can be seen as complete since it need
5870 not be further modified, only the reference types need be adjusted;
5871 otherwise the profile itself is incomplete and need be adjusted. */
5872 if (TYPE_IS_DUMMY_P (gnu_param_type
))
5876 if (Mechanism (gnat_param
) == By_Reference
5877 || (TYPE_REFERENCE_TO (gnu_param_type
)
5878 && TYPE_IS_FAT_POINTER_P (TYPE_REFERENCE_TO (gnu_param_type
)))
5879 || TYPE_IS_BY_REFERENCE_P (gnu_param_type
))
5881 gnu_param_type
= build_reference_type (gnu_param_type
);
5882 gnu_param
= create_param_decl (gnu_param_name
, gnu_param_type
);
5883 TREE_READONLY (gnu_param
) = 1;
5884 DECL_BY_REF_P (gnu_param
) = 1;
5885 DECL_POINTS_TO_READONLY_P (gnu_param
)
5886 = (Ekind (gnat_param
) == E_In_Parameter
5887 && !Address_Taken (gnat_param
));
5888 Set_Mechanism (gnat_param
, By_Reference
);
5889 Sloc_to_locus (Sloc (gnat_param
),
5890 &DECL_SOURCE_LOCATION (gnu_param
));
5893 /* ??? This is a kludge to support null procedures in spec taking a
5894 parameter with an untagged incomplete type coming from a limited
5895 context. The front-end creates a body without knowing anything
5896 about the non-limited view, which is illegal Ada and cannot be
5897 reasonably supported. Create a parameter with a fake type. */
5898 else if (kind
== E_Procedure
5899 && (gnat_decl
= Parent (gnat_subprog
))
5900 && Nkind (gnat_decl
) == N_Procedure_Specification
5901 && Null_Present (gnat_decl
)
5902 && IN (Ekind (gnat_param_type
), Incomplete_Kind
))
5903 gnu_param
= create_param_decl (gnu_param_name
, ptr_type_node
);
5907 gnu_param
= create_param_decl (gnu_param_name
, gnu_param_type
);
5908 associate_subprog_with_dummy_type (gnat_subprog
, gnu_param_type
);
5909 incomplete_profile_p
= true;
5916 = gnat_to_gnu_param (gnat_param
, num
== 0, gnat_subprog
, &cico
);
5918 /* We are returned either a PARM_DECL or a type if no parameter
5919 needs to be passed; in either case, adjust the type. */
5920 if (DECL_P (gnu_param
))
5921 gnu_param_type
= TREE_TYPE (gnu_param
);
5924 gnu_param_type
= gnu_param
;
5925 gnu_param
= NULL_TREE
;
5929 /* If we built a GCC tree for the parameter, register it. */
5933 = tree_cons (NULL_TREE
, gnu_param_type
, gnu_param_type_list
);
5934 gnu_param_list
= chainon (gnu_param
, gnu_param_list
);
5935 save_gnu_tree (gnat_param
, NULL_TREE
, false);
5936 save_gnu_tree (gnat_param
, gnu_param
, false);
5938 /* If a parameter is a pointer, a function may modify memory through
5939 it and thus shouldn't be considered a const function. Also, the
5940 memory may be modified between two calls, so they can't be CSE'ed.
5941 The latter case also handles by-ref parameters. */
5942 if (POINTER_TYPE_P (gnu_param_type
)
5943 || TYPE_IS_FAT_POINTER_P (gnu_param_type
))
5947 /* If the parameter uses the copy-in copy-out mechanism, allocate a field
5948 for it in the return type and register the association. */
5949 if (cico
&& !incomplete_profile_p
)
5953 tree gnu_new_ret_type
= make_node (RECORD_TYPE
);
5955 /* If this is a function, we also need a field for the
5956 return value to be placed. */
5957 if (TREE_CODE (gnu_return_type
) != VOID_TYPE
)
5960 = create_field_decl (get_identifier ("RETVAL"),
5962 gnu_new_ret_type
, NULL_TREE
,
5964 Sloc_to_locus (Sloc (gnat_subprog
),
5965 &DECL_SOURCE_LOCATION (gnu_field
));
5966 gnu_field_list
= gnu_field
;
5968 = tree_cons (gnu_field
, void_type_node
, NULL_TREE
);
5971 gnu_return_type
= gnu_new_ret_type
;
5972 TYPE_NAME (gnu_return_type
) = get_identifier ("RETURN");
5973 /* Set a default alignment to speed up accesses. But we should
5974 not increase the size of the structure too much, lest it does
5975 not fit in return registers anymore. */
5976 SET_TYPE_ALIGN (gnu_return_type
, get_mode_alignment (ptr_mode
));
5980 = create_field_decl (gnu_param_name
, gnu_param_type
,
5981 gnu_return_type
, NULL_TREE
, NULL_TREE
, 0, 0);
5982 Sloc_to_locus (Sloc (gnat_param
),
5983 &DECL_SOURCE_LOCATION (gnu_field
));
5984 DECL_CHAIN (gnu_field
) = gnu_field_list
;
5985 gnu_field_list
= gnu_field
;
5986 gnu_cico_list
= tree_cons (gnu_field
, gnu_param
, gnu_cico_list
);
5990 /* If the subprogram uses the copy-in copy-out mechanism, possibly adjust
5991 and finish up the return type. */
5992 if (gnu_cico_list
&& !incomplete_profile_p
)
5994 /* If we have a CICO list but it has only one entry, we convert
5995 this function into a function that returns this object. */
5996 if (list_length (gnu_cico_list
) == 1)
5997 gnu_return_type
= TREE_TYPE (TREE_PURPOSE (gnu_cico_list
));
5999 /* Do not finalize the return type if the subprogram is stubbed
6000 since structures are incomplete for the back-end. */
6001 else if (Convention (gnat_subprog
) != Convention_Stubbed
)
6003 finish_record_type (gnu_return_type
, nreverse (gnu_field_list
), 0,
6006 /* Try to promote the mode of the return type if it is passed
6007 in registers, again to speed up accesses. */
6008 if (TYPE_MODE (gnu_return_type
) == BLKmode
6009 && !targetm
.calls
.return_in_memory (gnu_return_type
, NULL_TREE
))
6012 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type
));
6013 unsigned int i
= BITS_PER_UNIT
;
6018 mode
= mode_for_size (i
, MODE_INT
, 0);
6019 if (mode
!= BLKmode
)
6021 SET_TYPE_MODE (gnu_return_type
, mode
);
6022 SET_TYPE_ALIGN (gnu_return_type
, GET_MODE_ALIGNMENT (mode
));
6023 TYPE_SIZE (gnu_return_type
)
6024 = bitsize_int (GET_MODE_BITSIZE (mode
));
6025 TYPE_SIZE_UNIT (gnu_return_type
)
6026 = size_int (GET_MODE_SIZE (mode
));
6031 rest_of_record_type_compilation (gnu_return_type
);
6035 /* The lists have been built in reverse. */
6036 gnu_param_type_list
= nreverse (gnu_param_type_list
);
6037 gnu_param_type_list
= chainon (gnu_param_type_list
, void_list_node
);
6038 *param_list
= nreverse (gnu_param_list
);
6039 gnu_cico_list
= nreverse (gnu_cico_list
);
6041 /* If the profile is incomplete, we only set the (temporary) return and
6042 parameter types; otherwise, we build the full type. In either case,
6043 we reuse an already existing GCC tree that we built previously here. */
6044 tree gnu_type
= present_gnu_tree (gnat_subprog
)
6045 ? TREE_TYPE (get_gnu_tree (gnat_subprog
)) : NULL_TREE
;
6047 if (incomplete_profile_p
)
6049 if (gnu_type
&& TREE_CODE (gnu_type
) == FUNCTION_TYPE
)
6052 gnu_type
= make_node (FUNCTION_TYPE
);
6053 TREE_TYPE (gnu_type
) = gnu_return_type
;
6054 TYPE_ARG_TYPES (gnu_type
) = gnu_param_type_list
;
6058 if (gnu_type
&& TREE_CODE (gnu_type
) == FUNCTION_TYPE
)
6060 TREE_TYPE (gnu_type
) = gnu_return_type
;
6061 TYPE_ARG_TYPES (gnu_type
) = gnu_param_type_list
;
6062 TYPE_CI_CO_LIST (gnu_type
) = gnu_cico_list
;
6063 TYPE_RETURN_UNCONSTRAINED_P (gnu_type
) = return_unconstrained_p
;
6064 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type
) = return_by_direct_ref_p
;
6065 TREE_ADDRESSABLE (gnu_type
) = return_by_invisi_ref_p
;
6066 TYPE_CANONICAL (gnu_type
) = gnu_type
;
6067 layout_type (gnu_type
);
6072 = build_function_type (gnu_return_type
, gnu_param_type_list
);
6074 /* GNU_TYPE may be shared since GCC hashes types. Unshare it if it
6075 has a different TYPE_CI_CO_LIST or flags. */
6076 if (!fntype_same_flags_p (gnu_type
, gnu_cico_list
,
6077 return_unconstrained_p
,
6078 return_by_direct_ref_p
,
6079 return_by_invisi_ref_p
))
6081 gnu_type
= copy_type (gnu_type
);
6082 TYPE_CI_CO_LIST (gnu_type
) = gnu_cico_list
;
6083 TYPE_RETURN_UNCONSTRAINED_P (gnu_type
) = return_unconstrained_p
;
6084 TYPE_RETURN_BY_DIRECT_REF_P (gnu_type
) = return_by_direct_ref_p
;
6085 TREE_ADDRESSABLE (gnu_type
) = return_by_invisi_ref_p
;
6090 gnu_type
= change_qualified_type (gnu_type
, TYPE_QUAL_CONST
);
6092 if (No_Return (gnat_subprog
))
6093 gnu_type
= change_qualified_type (gnu_type
, TYPE_QUAL_VOLATILE
);
6099 /* Like build_qualified_type, but TYPE_QUALS is added to the existing
6100 qualifiers on TYPE. */
6103 change_qualified_type (tree type
, int type_quals
)
6105 return build_qualified_type (type
, TYPE_QUALS (type
) | type_quals
);
6108 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
6111 same_discriminant_p (Entity_Id discr1
, Entity_Id discr2
)
6113 while (Present (Corresponding_Discriminant (discr1
)))
6114 discr1
= Corresponding_Discriminant (discr1
);
6116 while (Present (Corresponding_Discriminant (discr2
)))
6117 discr2
= Corresponding_Discriminant (discr2
);
6120 Original_Record_Component (discr1
) == Original_Record_Component (discr2
);
6123 /* Return true if the array type GNU_TYPE, which represents a dimension of
6124 GNAT_TYPE, has a non-aliased component in the back-end sense. */
6127 array_type_has_nonaliased_component (tree gnu_type
, Entity_Id gnat_type
)
6129 /* If the array type is not the innermost dimension of the GNAT type,
6130 then it has a non-aliased component. */
6131 if (TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
6132 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
)))
6135 /* If the array type has an aliased component in the front-end sense,
6136 then it also has an aliased component in the back-end sense. */
6137 if (Has_Aliased_Components (gnat_type
))
6140 /* If this is a derived type, then it has a non-aliased component if
6141 and only if its parent type also has one. */
6142 if (Is_Derived_Type (gnat_type
))
6144 tree gnu_parent_type
= gnat_to_gnu_type (Etype (gnat_type
));
6146 if (TREE_CODE (gnu_parent_type
) == UNCONSTRAINED_ARRAY_TYPE
)
6148 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type
))));
6149 for (index
= Number_Dimensions (gnat_type
) - 1; index
> 0; index
--)
6150 gnu_parent_type
= TREE_TYPE (gnu_parent_type
);
6151 return TYPE_NONALIASED_COMPONENT (gnu_parent_type
);
6154 /* Otherwise, rely exclusively on properties of the element type. */
6155 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type
));
6158 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
6161 compile_time_known_address_p (Node_Id gnat_address
)
6163 /* Catch System'To_Address. */
6164 if (Nkind (gnat_address
) == N_Unchecked_Type_Conversion
)
6165 gnat_address
= Expression (gnat_address
);
6167 return Compile_Time_Known_Value (gnat_address
);
6170 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
6171 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
6174 cannot_be_superflat (Node_Id gnat_range
)
6176 Node_Id gnat_lb
= Low_Bound (gnat_range
), gnat_hb
= High_Bound (gnat_range
);
6177 Node_Id scalar_range
;
6178 tree gnu_lb
, gnu_hb
, gnu_lb_minus_one
;
6180 /* If the low bound is not constant, try to find an upper bound. */
6181 while (Nkind (gnat_lb
) != N_Integer_Literal
6182 && (Ekind (Etype (gnat_lb
)) == E_Signed_Integer_Subtype
6183 || Ekind (Etype (gnat_lb
)) == E_Modular_Integer_Subtype
)
6184 && (scalar_range
= Scalar_Range (Etype (gnat_lb
)))
6185 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
6186 || Nkind (scalar_range
) == N_Range
))
6187 gnat_lb
= High_Bound (scalar_range
);
6189 /* If the high bound is not constant, try to find a lower bound. */
6190 while (Nkind (gnat_hb
) != N_Integer_Literal
6191 && (Ekind (Etype (gnat_hb
)) == E_Signed_Integer_Subtype
6192 || Ekind (Etype (gnat_hb
)) == E_Modular_Integer_Subtype
)
6193 && (scalar_range
= Scalar_Range (Etype (gnat_hb
)))
6194 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
6195 || Nkind (scalar_range
) == N_Range
))
6196 gnat_hb
= Low_Bound (scalar_range
);
6198 /* If we have failed to find constant bounds, punt. */
6199 if (Nkind (gnat_lb
) != N_Integer_Literal
6200 || Nkind (gnat_hb
) != N_Integer_Literal
)
6203 /* We need at least a signed 64-bit type to catch most cases. */
6204 gnu_lb
= UI_To_gnu (Intval (gnat_lb
), sbitsizetype
);
6205 gnu_hb
= UI_To_gnu (Intval (gnat_hb
), sbitsizetype
);
6206 if (TREE_OVERFLOW (gnu_lb
) || TREE_OVERFLOW (gnu_hb
))
6209 /* If the low bound is the smallest integer, nothing can be smaller. */
6210 gnu_lb_minus_one
= size_binop (MINUS_EXPR
, gnu_lb
, sbitsize_one_node
);
6211 if (TREE_OVERFLOW (gnu_lb_minus_one
))
6214 return !tree_int_cst_lt (gnu_hb
, gnu_lb_minus_one
);
6217 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
6220 constructor_address_p (tree gnu_expr
)
6222 while (TREE_CODE (gnu_expr
) == NOP_EXPR
6223 || TREE_CODE (gnu_expr
) == CONVERT_EXPR
6224 || TREE_CODE (gnu_expr
) == NON_LVALUE_EXPR
)
6225 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
6227 return (TREE_CODE (gnu_expr
) == ADDR_EXPR
6228 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == CONSTRUCTOR
);
6231 /* Return true if the size in units represented by GNU_SIZE can be handled by
6232 an allocation. If STATIC_P is true, consider only what can be done with a
6233 static allocation. */
6236 allocatable_size_p (tree gnu_size
, bool static_p
)
6238 /* We can allocate a fixed size if it is a valid for the middle-end. */
6239 if (TREE_CODE (gnu_size
) == INTEGER_CST
)
6240 return valid_constant_size_p (gnu_size
);
6242 /* We can allocate a variable size if this isn't a static allocation. */
6247 /* Return true if GNU_EXPR needs a conversion to GNU_TYPE when used as the
6248 initial value of an object of GNU_TYPE. */
6251 initial_value_needs_conversion (tree gnu_type
, tree gnu_expr
)
6253 /* Do not convert if the object's type is unconstrained because this would
6254 generate useless evaluations of the CONSTRUCTOR to compute the size. */
6255 if (TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
6256 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
6259 /* Do not convert if the object's type is a padding record whose field is of
6260 self-referential size because we want to copy only the actual data. */
6261 if (type_is_padding_self_referential (gnu_type
))
6264 /* Do not convert a call to a function that returns with variable size since
6265 we want to use the return slot optimization in this case. */
6266 if (TREE_CODE (gnu_expr
) == CALL_EXPR
6267 && return_type_with_variable_size_p (TREE_TYPE (gnu_expr
)))
6270 /* Do not convert to a record type with a variant part from a record type
6271 without one, to keep the object simpler. */
6272 if (TREE_CODE (gnu_type
) == RECORD_TYPE
6273 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
6274 && get_variant_part (gnu_type
)
6275 && !get_variant_part (TREE_TYPE (gnu_expr
)))
6278 /* In all the other cases, convert the expression to the object's type. */
6282 /* Given GNAT_ENTITY, elaborate all expressions that are required to
6283 be elaborated at the point of its definition, but do nothing else. */
6286 elaborate_entity (Entity_Id gnat_entity
)
6288 switch (Ekind (gnat_entity
))
6290 case E_Signed_Integer_Subtype
:
6291 case E_Modular_Integer_Subtype
:
6292 case E_Enumeration_Subtype
:
6293 case E_Ordinary_Fixed_Point_Subtype
:
6294 case E_Decimal_Fixed_Point_Subtype
:
6295 case E_Floating_Point_Subtype
:
6297 Node_Id gnat_lb
= Type_Low_Bound (gnat_entity
);
6298 Node_Id gnat_hb
= Type_High_Bound (gnat_entity
);
6300 /* ??? Tests to avoid Constraint_Error in static expressions
6301 are needed until after the front stops generating bogus
6302 conversions on bounds of real types. */
6303 if (!Raises_Constraint_Error (gnat_lb
))
6304 elaborate_expression (gnat_lb
, gnat_entity
, "L", true, false,
6305 Needs_Debug_Info (gnat_entity
));
6306 if (!Raises_Constraint_Error (gnat_hb
))
6307 elaborate_expression (gnat_hb
, gnat_entity
, "U", true, false,
6308 Needs_Debug_Info (gnat_entity
));
6312 case E_Record_Subtype
:
6313 case E_Private_Subtype
:
6314 case E_Limited_Private_Subtype
:
6315 case E_Record_Subtype_With_Private
:
6316 if (Has_Discriminants (gnat_entity
) && Is_Constrained (gnat_entity
))
6318 Node_Id gnat_discriminant_expr
;
6319 Entity_Id gnat_field
;
6322 = First_Discriminant (Implementation_Base_Type (gnat_entity
)),
6323 gnat_discriminant_expr
6324 = First_Elmt (Discriminant_Constraint (gnat_entity
));
6325 Present (gnat_field
);
6326 gnat_field
= Next_Discriminant (gnat_field
),
6327 gnat_discriminant_expr
= Next_Elmt (gnat_discriminant_expr
))
6328 /* Ignore access discriminants. */
6329 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr
))))
6330 elaborate_expression (Node (gnat_discriminant_expr
),
6331 gnat_entity
, get_entity_char (gnat_field
),
6332 true, false, false);
6339 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6340 NAME, ARGS and ERROR_POINT. */
6343 prepend_one_attribute (struct attrib
**attr_list
,
6344 enum attrib_type attrib_type
,
6347 Node_Id attr_error_point
)
6349 struct attrib
* attr
= (struct attrib
*) xmalloc (sizeof (struct attrib
));
6351 attr
->type
= attrib_type
;
6352 attr
->name
= attr_name
;
6353 attr
->args
= attr_args
;
6354 attr
->error_point
= attr_error_point
;
6356 attr
->next
= *attr_list
;
6360 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6363 prepend_one_attribute_pragma (struct attrib
**attr_list
, Node_Id gnat_pragma
)
6365 const Node_Id gnat_arg
= Pragma_Argument_Associations (gnat_pragma
);
6366 tree gnu_arg0
= NULL_TREE
, gnu_arg1
= NULL_TREE
;
6367 enum attrib_type etype
;
6369 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6370 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma
))))
6372 case Pragma_Machine_Attribute
:
6373 etype
= ATTR_MACHINE_ATTRIBUTE
;
6376 case Pragma_Linker_Alias
:
6377 etype
= ATTR_LINK_ALIAS
;
6380 case Pragma_Linker_Section
:
6381 etype
= ATTR_LINK_SECTION
;
6384 case Pragma_Linker_Constructor
:
6385 etype
= ATTR_LINK_CONSTRUCTOR
;
6388 case Pragma_Linker_Destructor
:
6389 etype
= ATTR_LINK_DESTRUCTOR
;
6392 case Pragma_Weak_External
:
6393 etype
= ATTR_WEAK_EXTERNAL
;
6396 case Pragma_Thread_Local_Storage
:
6397 etype
= ATTR_THREAD_LOCAL_STORAGE
;
6404 /* See what arguments we have and turn them into GCC trees for attribute
6405 handlers. These expect identifier for strings. We handle at most two
6406 arguments and static expressions only. */
6407 if (Present (gnat_arg
) && Present (First (gnat_arg
)))
6409 Node_Id gnat_arg0
= Next (First (gnat_arg
));
6410 Node_Id gnat_arg1
= Empty
;
6412 if (Present (gnat_arg0
)
6413 && Is_OK_Static_Expression (Expression (gnat_arg0
)))
6415 gnu_arg0
= gnat_to_gnu (Expression (gnat_arg0
));
6417 if (TREE_CODE (gnu_arg0
) == STRING_CST
)
6419 gnu_arg0
= get_identifier (TREE_STRING_POINTER (gnu_arg0
));
6420 if (IDENTIFIER_LENGTH (gnu_arg0
) == 0)
6424 gnat_arg1
= Next (gnat_arg0
);
6427 if (Present (gnat_arg1
)
6428 && Is_OK_Static_Expression (Expression (gnat_arg1
)))
6430 gnu_arg1
= gnat_to_gnu (Expression (gnat_arg1
));
6432 if (TREE_CODE (gnu_arg1
) == STRING_CST
)
6433 gnu_arg1
= get_identifier (TREE_STRING_POINTER (gnu_arg1
));
6437 /* Prepend to the list. Make a list of the argument we might have, as GCC
6439 prepend_one_attribute (attr_list
, etype
, gnu_arg0
,
6441 ? build_tree_list (NULL_TREE
, gnu_arg1
) : NULL_TREE
,
6442 Present (Next (First (gnat_arg
)))
6443 ? Expression (Next (First (gnat_arg
))) : gnat_pragma
);
6446 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6449 prepend_attributes (struct attrib
**attr_list
, Entity_Id gnat_entity
)
6453 /* Attributes are stored as Representation Item pragmas. */
6454 for (gnat_temp
= First_Rep_Item (gnat_entity
);
6455 Present (gnat_temp
);
6456 gnat_temp
= Next_Rep_Item (gnat_temp
))
6457 if (Nkind (gnat_temp
) == N_Pragma
)
6458 prepend_one_attribute_pragma (attr_list
, gnat_temp
);
6461 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6462 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6463 return the GCC tree to use for that expression. S is the suffix to use
6464 if a variable needs to be created and DEFINITION is true if this is done
6465 for a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6466 otherwise, we are just elaborating the expression for side-effects. If
6467 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6468 isn't needed for code generation. */
6471 elaborate_expression (Node_Id gnat_expr
, Entity_Id gnat_entity
, const char *s
,
6472 bool definition
, bool need_value
, bool need_debug
)
6476 /* If we already elaborated this expression (e.g. it was involved
6477 in the definition of a private type), use the old value. */
6478 if (present_gnu_tree (gnat_expr
))
6479 return get_gnu_tree (gnat_expr
);
6481 /* If we don't need a value and this is static or a discriminant,
6482 we don't need to do anything. */
6484 && (Is_OK_Static_Expression (gnat_expr
)
6485 || (Nkind (gnat_expr
) == N_Identifier
6486 && Ekind (Entity (gnat_expr
)) == E_Discriminant
)))
6489 /* If it's a static expression, we don't need a variable for debugging. */
6490 if (need_debug
&& Is_OK_Static_Expression (gnat_expr
))
6493 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6494 gnu_expr
= elaborate_expression_1 (gnat_to_gnu (gnat_expr
), gnat_entity
, s
,
6495 definition
, need_debug
);
6497 /* Save the expression in case we try to elaborate this entity again. Since
6498 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6499 if (!CONTAINS_PLACEHOLDER_P (gnu_expr
))
6500 save_gnu_tree (gnat_expr
, gnu_expr
, true);
6502 return need_value
? gnu_expr
: error_mark_node
;
6505 /* Similar, but take a GNU expression and always return a result. */
6508 elaborate_expression_1 (tree gnu_expr
, Entity_Id gnat_entity
, const char *s
,
6509 bool definition
, bool need_debug
)
6511 const bool expr_public_p
= Is_Public (gnat_entity
);
6512 const bool expr_global_p
= expr_public_p
|| global_bindings_p ();
6513 bool expr_variable_p
, use_variable
;
6515 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6516 that an expression cannot contain both a discriminant and a variable. */
6517 if (CONTAINS_PLACEHOLDER_P (gnu_expr
))
6520 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6521 a variable that is initialized to contain the expression when the package
6522 containing the definition is elaborated. If this entity is defined at top
6523 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6524 if this is necessary. */
6525 if (TREE_CONSTANT (gnu_expr
))
6526 expr_variable_p
= false;
6529 /* Skip any conversions and simple constant arithmetics to see if the
6530 expression is based on a read-only variable. */
6531 tree inner
= remove_conversions (gnu_expr
, true);
6533 inner
= skip_simple_constant_arithmetic (inner
);
6535 if (handled_component_p (inner
))
6536 inner
= get_inner_constant_reference (inner
);
6540 && TREE_CODE (inner
) == VAR_DECL
6541 && (TREE_READONLY (inner
) || DECL_READONLY_ONCE_ELAB (inner
)));
6544 /* We only need to use the variable if we are in a global context since GCC
6545 can do the right thing in the local case. However, when not optimizing,
6546 use it for bounds of loop iteration scheme to avoid code duplication. */
6547 use_variable
= expr_variable_p
6551 && Is_Itype (gnat_entity
)
6552 && Nkind (Associated_Node_For_Itype (gnat_entity
))
6553 == N_Loop_Parameter_Specification
));
6555 /* Now create it, possibly only for debugging purposes. */
6556 if (use_variable
|| need_debug
)
6558 /* The following variable creation can happen when processing the body
6559 of subprograms that are defined out of the extended main unit and
6560 inlined. In this case, we are not at the global scope, and thus the
6561 new variable must not be tagged "external", as we used to do here as
6562 soon as DEFINITION was false. */
6564 = create_var_decl (create_concat_name (gnat_entity
, s
), NULL_TREE
,
6565 TREE_TYPE (gnu_expr
), gnu_expr
, true,
6566 expr_public_p
, !definition
&& expr_global_p
,
6567 expr_global_p
, false, true, need_debug
,
6570 /* Using this variable at debug time (if need_debug is true) requires a
6571 proper location. The back-end will compute a location for this
6572 variable only if the variable is used by the generated code.
6573 Returning the variable ensures the caller will use it in generated
6574 code. Note that there is no need for a location if the debug info
6575 contains an integer constant.
6576 TODO: when the encoding-based debug scheme is dropped, move this
6577 condition to the top-level IF block: we will not need to create a
6578 variable anymore in such cases, then. */
6579 if (use_variable
|| (need_debug
&& !TREE_CONSTANT (gnu_expr
)))
6583 return expr_variable_p
? gnat_save_expr (gnu_expr
) : gnu_expr
;
6586 /* Similar, but take an alignment factor and make it explicit in the tree. */
6589 elaborate_expression_2 (tree gnu_expr
, Entity_Id gnat_entity
, const char *s
,
6590 bool definition
, bool need_debug
, unsigned int align
)
6592 tree unit_align
= size_int (align
/ BITS_PER_UNIT
);
6594 size_binop (MULT_EXPR
,
6595 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR
,
6598 gnat_entity
, s
, definition
,
6603 /* Structure to hold internal data for elaborate_reference. */
6612 /* Wrapper function around elaborate_expression_1 for elaborate_reference. */
6615 elaborate_reference_1 (tree ref
, void *data
)
6617 struct er_data
*er
= (struct er_data
*)data
;
6620 /* This is what elaborate_expression_1 does if NEED_DEBUG is false. */
6621 if (TREE_CONSTANT (ref
))
6624 /* If this is a COMPONENT_REF of a fat pointer, elaborate the entire fat
6625 pointer. This may be more efficient, but will also allow us to more
6626 easily find the match for the PLACEHOLDER_EXPR. */
6627 if (TREE_CODE (ref
) == COMPONENT_REF
6628 && TYPE_IS_FAT_POINTER_P (TREE_TYPE (TREE_OPERAND (ref
, 0))))
6629 return build3 (COMPONENT_REF
, TREE_TYPE (ref
),
6630 elaborate_reference_1 (TREE_OPERAND (ref
, 0), data
),
6631 TREE_OPERAND (ref
, 1), NULL_TREE
);
6633 sprintf (suffix
, "EXP%d", ++er
->n
);
6635 elaborate_expression_1 (ref
, er
->entity
, suffix
, er
->definition
, false);
6638 /* Elaborate the reference REF to be used as renamed object for GNAT_ENTITY.
6639 DEFINITION is true if this is done for a definition of GNAT_ENTITY and
6640 INIT is set to the first arm of a COMPOUND_EXPR present in REF, if any. */
6643 elaborate_reference (tree ref
, Entity_Id gnat_entity
, bool definition
,
6646 struct er_data er
= { gnat_entity
, definition
, 0 };
6647 return gnat_rewrite_reference (ref
, elaborate_reference_1
, &er
, init
);
6650 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6651 the value passed against the list of choices. */
6654 choices_to_gnu (tree operand
, Node_Id choices
)
6658 tree result
= boolean_false_node
;
6659 tree this_test
, low
= 0, high
= 0, single
= 0;
6661 for (choice
= First (choices
); Present (choice
); choice
= Next (choice
))
6663 switch (Nkind (choice
))
6666 low
= gnat_to_gnu (Low_Bound (choice
));
6667 high
= gnat_to_gnu (High_Bound (choice
));
6670 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6671 build_binary_op (GE_EXPR
, boolean_type_node
,
6673 build_binary_op (LE_EXPR
, boolean_type_node
,
6678 case N_Subtype_Indication
:
6679 gnat_temp
= Range_Expression (Constraint (choice
));
6680 low
= gnat_to_gnu (Low_Bound (gnat_temp
));
6681 high
= gnat_to_gnu (High_Bound (gnat_temp
));
6684 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6685 build_binary_op (GE_EXPR
, boolean_type_node
,
6687 build_binary_op (LE_EXPR
, boolean_type_node
,
6692 case N_Expanded_Name
:
6693 /* This represents either a subtype range, an enumeration
6694 literal, or a constant Ekind says which. If an enumeration
6695 literal or constant, fall through to the next case. */
6696 if (Ekind (Entity (choice
)) != E_Enumeration_Literal
6697 && Ekind (Entity (choice
)) != E_Constant
)
6699 tree type
= gnat_to_gnu_type (Entity (choice
));
6701 low
= TYPE_MIN_VALUE (type
);
6702 high
= TYPE_MAX_VALUE (type
);
6705 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6706 build_binary_op (GE_EXPR
, boolean_type_node
,
6708 build_binary_op (LE_EXPR
, boolean_type_node
,
6713 /* ... fall through ... */
6715 case N_Character_Literal
:
6716 case N_Integer_Literal
:
6717 single
= gnat_to_gnu (choice
);
6718 this_test
= build_binary_op (EQ_EXPR
, boolean_type_node
, operand
,
6722 case N_Others_Choice
:
6723 this_test
= boolean_true_node
;
6730 result
= build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
, result
,
6737 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6738 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6741 adjust_packed (tree field_type
, tree record_type
, int packed
)
6743 /* If the field contains an item of variable size, we cannot pack it
6744 because we cannot create temporaries of non-fixed size in case
6745 we need to take the address of the field. See addressable_p and
6746 the notes on the addressability issues for further details. */
6747 if (type_has_variable_size (field_type
))
6750 /* In the other cases, we can honor the packing. */
6754 /* If the alignment of the record is specified and the field type
6755 is over-aligned, request Storage_Unit alignment for the field. */
6756 if (TYPE_ALIGN (record_type
)
6757 && TYPE_ALIGN (field_type
) > TYPE_ALIGN (record_type
))
6760 /* Likewise if the maximum alignment of the record is specified. */
6761 if (TYPE_MAX_ALIGN (record_type
)
6762 && TYPE_ALIGN (field_type
) > TYPE_MAX_ALIGN (record_type
))
6768 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6769 placed in GNU_RECORD_TYPE.
6771 PACKED is 1 if the enclosing record is packed or -1 if the enclosing
6772 record has Component_Alignment of Storage_Unit.
6774 DEFINITION is true if this field is for a record being defined.
6776 DEBUG_INFO_P is true if we need to write debug information for types
6777 that we may create in the process. */
6780 gnat_to_gnu_field (Entity_Id gnat_field
, tree gnu_record_type
, int packed
,
6781 bool definition
, bool debug_info_p
)
6783 const Entity_Id gnat_field_type
= Etype (gnat_field
);
6784 const bool is_aliased
6785 = Is_Aliased (gnat_field
);
6786 const bool is_atomic
6787 = (Is_Atomic_Or_VFA (gnat_field
) || Is_Atomic_Or_VFA (gnat_field_type
));
6788 const bool is_independent
6789 = (Is_Independent (gnat_field
) || Is_Independent (gnat_field_type
));
6790 const bool is_volatile
6791 = (Treat_As_Volatile (gnat_field
) || Treat_As_Volatile (gnat_field_type
));
6792 const bool needs_strict_alignment
6796 || Strict_Alignment (gnat_field_type
));
6797 tree gnu_field_type
= gnat_to_gnu_type (gnat_field_type
);
6798 tree gnu_field_id
= get_entity_name (gnat_field
);
6799 tree gnu_field
, gnu_size
, gnu_pos
;
6801 /* If this field requires strict alignment, we cannot pack it because
6802 it would very likely be under-aligned in the record. */
6803 if (needs_strict_alignment
)
6806 packed
= adjust_packed (gnu_field_type
, gnu_record_type
, packed
);
6808 /* If a size is specified, use it. Otherwise, if the record type is packed,
6809 use the official RM size. See "Handling of Type'Size Values" in Einfo
6810 for further details. */
6811 if (Known_Esize (gnat_field
))
6812 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6813 gnat_field
, FIELD_DECL
, false, true);
6814 else if (packed
== 1)
6815 gnu_size
= validate_size (RM_Size (gnat_field_type
), gnu_field_type
,
6816 gnat_field
, FIELD_DECL
, false, true);
6818 gnu_size
= NULL_TREE
;
6820 /* If we have a specified size that is smaller than that of the field's type,
6821 or a position is specified, and the field's type is a record that doesn't
6822 require strict alignment, see if we can get either an integral mode form
6823 of the type or a smaller form. If we can, show a size was specified for
6824 the field if there wasn't one already, so we know to make this a bitfield
6825 and avoid making things wider.
6827 Changing to an integral mode form is useful when the record is packed as
6828 we can then place the field at a non-byte-aligned position and so achieve
6829 tighter packing. This is in addition required if the field shares a byte
6830 with another field and the front-end lets the back-end handle the access
6831 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6833 Changing to a smaller form is required if the specified size is smaller
6834 than that of the field's type and the type contains sub-fields that are
6835 padded, in order to avoid generating accesses to these sub-fields that
6836 are wider than the field.
6838 We avoid the transformation if it is not required or potentially useful,
6839 as it might entail an increase of the field's alignment and have ripple
6840 effects on the outer record type. A typical case is a field known to be
6841 byte-aligned and not to share a byte with another field. */
6842 if (!needs_strict_alignment
6843 && RECORD_OR_UNION_TYPE_P (gnu_field_type
)
6844 && !TYPE_FAT_POINTER_P (gnu_field_type
)
6845 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type
))
6848 && (tree_int_cst_lt (gnu_size
, TYPE_SIZE (gnu_field_type
))
6849 || (Present (Component_Clause (gnat_field
))
6850 && !(UI_To_Int (Component_Bit_Offset (gnat_field
))
6851 % BITS_PER_UNIT
== 0
6852 && value_factor_p (gnu_size
, BITS_PER_UNIT
)))))))
6854 tree gnu_packable_type
= make_packable_type (gnu_field_type
, true);
6855 if (gnu_packable_type
!= gnu_field_type
)
6857 gnu_field_type
= gnu_packable_type
;
6859 gnu_size
= rm_size (gnu_field_type
);
6863 if (Is_Atomic_Or_VFA (gnat_field
))
6864 check_ok_for_atomic_type (gnu_field_type
, gnat_field
, false);
6866 if (Present (Component_Clause (gnat_field
)))
6868 Node_Id gnat_clause
= Component_Clause (gnat_field
);
6869 Entity_Id gnat_parent
6870 = Parent_Subtype (Underlying_Type (Scope (gnat_field
)));
6872 gnu_pos
= UI_To_gnu (Component_Bit_Offset (gnat_field
), bitsizetype
);
6873 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6874 gnat_field
, FIELD_DECL
, false, true);
6876 /* Ensure the position does not overlap with the parent subtype, if there
6877 is one. This test is omitted if the parent of the tagged type has a
6878 full rep clause since, in this case, component clauses are allowed to
6879 overlay the space allocated for the parent type and the front-end has
6880 checked that there are no overlapping components. */
6881 if (Present (gnat_parent
) && !Is_Fully_Repped_Tagged_Type (gnat_parent
))
6883 tree gnu_parent
= gnat_to_gnu_type (gnat_parent
);
6885 if (TREE_CODE (TYPE_SIZE (gnu_parent
)) == INTEGER_CST
6886 && tree_int_cst_lt (gnu_pos
, TYPE_SIZE (gnu_parent
)))
6888 ("offset of& must be beyond parent{, minimum allowed is ^}",
6889 Position (gnat_clause
), gnat_field
, TYPE_SIZE_UNIT (gnu_parent
));
6892 /* If this field needs strict alignment, make sure that the record is
6893 sufficiently aligned and that the position and size are consistent
6894 with the type. But don't do it if we are just annotating types and
6895 the field's type is tagged, since tagged types aren't fully laid out
6896 in this mode. Also, note that atomic implies volatile so the inner
6897 test sequences ordering is significant here. */
6898 if (needs_strict_alignment
6899 && !(type_annotate_only
&& Is_Tagged_Type (gnat_field_type
)))
6901 const unsigned int type_align
= TYPE_ALIGN (gnu_field_type
);
6903 if (TYPE_ALIGN (gnu_record_type
) < type_align
)
6904 SET_TYPE_ALIGN (gnu_record_type
, type_align
);
6906 /* If the position is not a multiple of the alignment of the type,
6907 then error out and reset the position. */
6908 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR
, gnu_pos
,
6909 bitsize_int (type_align
))))
6914 s
= "position of atomic field& must be multiple of ^ bits";
6915 else if (is_aliased
)
6916 s
= "position of aliased field& must be multiple of ^ bits";
6917 else if (is_independent
)
6918 s
= "position of independent field& must be multiple of ^ bits";
6919 else if (is_volatile
)
6920 s
= "position of volatile field& must be multiple of ^ bits";
6921 else if (Strict_Alignment (gnat_field_type
))
6922 s
= "position of & with aliased or tagged part must be"
6923 " multiple of ^ bits";
6927 post_error_ne_num (s
, First_Bit (gnat_clause
), gnat_field
,
6929 gnu_pos
= NULL_TREE
;
6934 tree gnu_type_size
= TYPE_SIZE (gnu_field_type
);
6935 const int cmp
= tree_int_cst_compare (gnu_size
, gnu_type_size
);
6937 /* If the size is lower than that of the type, or greater for
6938 atomic and aliased, then error out and reset the size. */
6939 if (cmp
< 0 || (cmp
> 0 && (is_atomic
|| is_aliased
)))
6944 s
= "size of atomic field& must be ^ bits";
6945 else if (is_aliased
)
6946 s
= "size of aliased field& must be ^ bits";
6947 else if (is_independent
)
6948 s
= "size of independent field& must be at least ^ bits";
6949 else if (is_volatile
)
6950 s
= "size of volatile field& must be at least ^ bits";
6951 else if (Strict_Alignment (gnat_field_type
))
6952 s
= "size of & with aliased or tagged part must be"
6957 post_error_ne_tree (s
, Last_Bit (gnat_clause
), gnat_field
,
6959 gnu_size
= NULL_TREE
;
6962 /* Likewise if the size is not a multiple of a byte, */
6963 else if (!integer_zerop (size_binop (TRUNC_MOD_EXPR
, gnu_size
,
6964 bitsize_unit_node
)))
6969 s
= "size of independent field& must be multiple of"
6971 else if (is_volatile
)
6972 s
= "size of volatile field& must be multiple of"
6974 else if (Strict_Alignment (gnat_field_type
))
6975 s
= "size of & with aliased or tagged part must be"
6976 " multiple of Storage_Unit";
6980 post_error_ne (s
, Last_Bit (gnat_clause
), gnat_field
);
6981 gnu_size
= NULL_TREE
;
6987 /* If the record has rep clauses and this is the tag field, make a rep
6988 clause for it as well. */
6989 else if (Has_Specified_Layout (Scope (gnat_field
))
6990 && Chars (gnat_field
) == Name_uTag
)
6992 gnu_pos
= bitsize_zero_node
;
6993 gnu_size
= TYPE_SIZE (gnu_field_type
);
6998 gnu_pos
= NULL_TREE
;
7000 /* If we are packing the record and the field is BLKmode, round the
7001 size up to a byte boundary. */
7002 if (packed
&& TYPE_MODE (gnu_field_type
) == BLKmode
&& gnu_size
)
7003 gnu_size
= round_up (gnu_size
, BITS_PER_UNIT
);
7006 /* We need to make the size the maximum for the type if it is
7007 self-referential and an unconstrained type. In that case, we can't
7008 pack the field since we can't make a copy to align it. */
7009 if (TREE_CODE (gnu_field_type
) == RECORD_TYPE
7011 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type
))
7012 && !Is_Constrained (Underlying_Type (gnat_field_type
)))
7014 gnu_size
= max_size (TYPE_SIZE (gnu_field_type
), true);
7018 /* If a size is specified, adjust the field's type to it. */
7021 tree orig_field_type
;
7023 /* If the field's type is justified modular, we would need to remove
7024 the wrapper to (better) meet the layout requirements. However we
7025 can do so only if the field is not aliased to preserve the unique
7026 layout and if the prescribed size is not greater than that of the
7027 packed array to preserve the justification. */
7028 if (!needs_strict_alignment
7029 && TREE_CODE (gnu_field_type
) == RECORD_TYPE
7030 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type
)
7031 && tree_int_cst_compare (gnu_size
, TYPE_ADA_SIZE (gnu_field_type
))
7033 gnu_field_type
= TREE_TYPE (TYPE_FIELDS (gnu_field_type
));
7035 /* Similarly if the field's type is a misaligned integral type, but
7036 there is no restriction on the size as there is no justification. */
7037 if (!needs_strict_alignment
7038 && TYPE_IS_PADDING_P (gnu_field_type
)
7039 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type
))))
7040 gnu_field_type
= TREE_TYPE (TYPE_FIELDS (gnu_field_type
));
7043 = make_type_from_size (gnu_field_type
, gnu_size
,
7044 Has_Biased_Representation (gnat_field
));
7046 orig_field_type
= gnu_field_type
;
7047 gnu_field_type
= maybe_pad_type (gnu_field_type
, gnu_size
, 0, gnat_field
,
7048 false, false, definition
, true);
7050 /* If a padding record was made, declare it now since it will never be
7051 declared otherwise. This is necessary to ensure that its subtrees
7052 are properly marked. */
7053 if (gnu_field_type
!= orig_field_type
7054 && !DECL_P (TYPE_NAME (gnu_field_type
)))
7055 create_type_decl (TYPE_NAME (gnu_field_type
), gnu_field_type
, true,
7056 debug_info_p
, gnat_field
);
7059 /* Otherwise (or if there was an error), don't specify a position. */
7061 gnu_pos
= NULL_TREE
;
7063 /* If the field's type is a padded type made for a scalar field of a record
7064 type with reverse storage order, we need to propagate the reverse storage
7065 order to the padding type since it is the innermost enclosing aggregate
7066 type around the scalar. */
7067 if (TYPE_IS_PADDING_P (gnu_field_type
)
7068 && TYPE_REVERSE_STORAGE_ORDER (gnu_record_type
)
7069 && Is_Scalar_Type (gnat_field_type
))
7070 gnu_field_type
= set_reverse_storage_order_on_pad_type (gnu_field_type
);
7072 gcc_assert (TREE_CODE (gnu_field_type
) != RECORD_TYPE
7073 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type
));
7075 /* Now create the decl for the field. */
7077 = create_field_decl (gnu_field_id
, gnu_field_type
, gnu_record_type
,
7078 gnu_size
, gnu_pos
, packed
, Is_Aliased (gnat_field
));
7079 Sloc_to_locus (Sloc (gnat_field
), &DECL_SOURCE_LOCATION (gnu_field
));
7080 DECL_ALIASED_P (gnu_field
) = Is_Aliased (gnat_field
);
7081 TREE_SIDE_EFFECTS (gnu_field
) = TREE_THIS_VOLATILE (gnu_field
) = is_volatile
;
7083 if (Ekind (gnat_field
) == E_Discriminant
)
7085 DECL_INVARIANT_P (gnu_field
)
7086 = No (Discriminant_Default_Value (gnat_field
));
7087 DECL_DISCRIMINANT_NUMBER (gnu_field
)
7088 = UI_To_gnu (Discriminant_Number (gnat_field
), sizetype
);
7094 /* Return true if at least one member of COMPONENT_LIST needs strict
7098 components_need_strict_alignment (Node_Id component_list
)
7100 Node_Id component_decl
;
7102 for (component_decl
= First_Non_Pragma (Component_Items (component_list
));
7103 Present (component_decl
);
7104 component_decl
= Next_Non_Pragma (component_decl
))
7106 Entity_Id gnat_field
= Defining_Entity (component_decl
);
7108 if (Is_Aliased (gnat_field
))
7111 if (Strict_Alignment (Etype (gnat_field
)))
7118 /* Return true if TYPE is a type with variable size or a padding type with a
7119 field of variable size or a record that has a field with such a type. */
7122 type_has_variable_size (tree type
)
7126 if (!TREE_CONSTANT (TYPE_SIZE (type
)))
7129 if (TYPE_IS_PADDING_P (type
)
7130 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type
))))
7133 if (!RECORD_OR_UNION_TYPE_P (type
))
7136 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
7137 if (type_has_variable_size (TREE_TYPE (field
)))
7143 /* Return true if FIELD is an artificial field. */
7146 field_is_artificial (tree field
)
7148 /* These fields are generated by the front-end proper. */
7149 if (IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == '_')
7152 /* These fields are generated by gigi. */
7153 if (DECL_INTERNAL_P (field
))
7159 /* Return true if FIELD is a non-artificial aliased field. */
7162 field_is_aliased (tree field
)
7164 if (field_is_artificial (field
))
7167 return DECL_ALIASED_P (field
);
7170 /* Return true if FIELD is a non-artificial field with self-referential
7174 field_has_self_size (tree field
)
7176 if (field_is_artificial (field
))
7179 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
7182 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field
)));
7185 /* Return true if FIELD is a non-artificial field with variable size. */
7188 field_has_variable_size (tree field
)
7190 if (field_is_artificial (field
))
7193 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
7196 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field
))) != INTEGER_CST
;
7199 /* qsort comparer for the bit positions of two record components. */
7202 compare_field_bitpos (const PTR rt1
, const PTR rt2
)
7204 const_tree
const field1
= * (const_tree
const *) rt1
;
7205 const_tree
const field2
= * (const_tree
const *) rt2
;
7207 = tree_int_cst_compare (bit_position (field1
), bit_position (field2
));
7209 return ret
? ret
: (int) (DECL_UID (field1
) - DECL_UID (field2
));
7212 /* Structure holding information for a given variant. */
7213 typedef struct vinfo
7215 /* The record type of the variant. */
7218 /* The name of the variant. */
7221 /* The qualifier of the variant. */
7224 /* Whether the variant has a rep clause. */
7227 /* Whether the variant is packed. */
7232 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
7233 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
7234 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
7235 When called from gnat_to_gnu_entity during the processing of a record type
7236 definition, the GCC node for the parent, if any, will be the single field
7237 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
7238 GNU_FIELD_LIST. The other calls to this function are recursive calls for
7239 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
7241 PACKED is 1 if this is for a packed record or -1 if this is for a record
7242 with Component_Alignment of Storage_Unit.
7244 DEFINITION is true if we are defining this record type.
7246 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
7247 out the record. This means the alignment only serves to force fields to
7248 be bitfields, but not to require the record to be that aligned. This is
7251 ALL_REP is true if a rep clause is present for all the fields.
7253 UNCHECKED_UNION is true if we are building this type for a record with a
7254 Pragma Unchecked_Union.
7256 ARTIFICIAL is true if this is a type that was generated by the compiler.
7258 DEBUG_INFO is true if we need to write debug information about the type.
7260 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
7261 mean that its contents may be unused as well, only the container itself.
7263 REORDER is true if we are permitted to reorder components of this type.
7265 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
7266 the outer record type down to this variant level. It is nonzero only if
7267 all the fields down to this level have a rep clause and ALL_REP is false.
7269 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
7270 with a rep clause is to be added; in this case, that is all that should
7271 be done with such fields and the return value will be false. */
7274 components_to_record (tree gnu_record_type
, Node_Id gnat_component_list
,
7275 tree gnu_field_list
, int packed
, bool definition
,
7276 bool cancel_alignment
, bool all_rep
,
7277 bool unchecked_union
, bool artificial
,
7278 bool debug_info
, bool maybe_unused
, bool reorder
,
7279 tree first_free_pos
, tree
*p_gnu_rep_list
)
7281 const bool needs_xv_encodings
7282 = debug_info
&& gnat_encodings
!= DWARF_GNAT_ENCODINGS_MINIMAL
;
7283 bool all_rep_and_size
= all_rep
&& TYPE_SIZE (gnu_record_type
);
7284 bool variants_have_rep
= all_rep
;
7285 bool layout_with_rep
= false;
7286 bool has_self_field
= false;
7287 bool has_aliased_after_self_field
= false;
7288 Node_Id component_decl
, variant_part
;
7289 tree gnu_field
, gnu_next
, gnu_last
;
7290 tree gnu_variant_part
= NULL_TREE
;
7291 tree gnu_rep_list
= NULL_TREE
;
7292 tree gnu_var_list
= NULL_TREE
;
7293 tree gnu_self_list
= NULL_TREE
;
7294 tree gnu_zero_list
= NULL_TREE
;
7296 /* For each component referenced in a component declaration create a GCC
7297 field and add it to the list, skipping pragmas in the GNAT list. */
7298 gnu_last
= tree_last (gnu_field_list
);
7299 if (Present (Component_Items (gnat_component_list
)))
7301 = First_Non_Pragma (Component_Items (gnat_component_list
));
7302 Present (component_decl
);
7303 component_decl
= Next_Non_Pragma (component_decl
))
7305 Entity_Id gnat_field
= Defining_Entity (component_decl
);
7306 Name_Id gnat_name
= Chars (gnat_field
);
7308 /* If present, the _Parent field must have been created as the single
7309 field of the record type. Put it before any other fields. */
7310 if (gnat_name
== Name_uParent
)
7312 gnu_field
= TYPE_FIELDS (gnu_record_type
);
7313 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
7317 gnu_field
= gnat_to_gnu_field (gnat_field
, gnu_record_type
, packed
,
7318 definition
, debug_info
);
7320 /* If this is the _Tag field, put it before any other fields. */
7321 if (gnat_name
== Name_uTag
)
7322 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
7324 /* If this is the _Controller field, put it before the other
7325 fields except for the _Tag or _Parent field. */
7326 else if (gnat_name
== Name_uController
&& gnu_last
)
7328 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
7329 DECL_CHAIN (gnu_last
) = gnu_field
;
7332 /* If this is a regular field, put it after the other fields. */
7335 DECL_CHAIN (gnu_field
) = gnu_field_list
;
7336 gnu_field_list
= gnu_field
;
7338 gnu_last
= gnu_field
;
7340 /* And record information for the final layout. */
7341 if (field_has_self_size (gnu_field
))
7342 has_self_field
= true;
7343 else if (has_self_field
&& field_is_aliased (gnu_field
))
7344 has_aliased_after_self_field
= true;
7348 save_gnu_tree (gnat_field
, gnu_field
, false);
7351 /* At the end of the component list there may be a variant part. */
7352 variant_part
= Variant_Part (gnat_component_list
);
7354 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7355 mutually exclusive and should go in the same memory. To do this we need
7356 to treat each variant as a record whose elements are created from the
7357 component list for the variant. So here we create the records from the
7358 lists for the variants and put them all into the QUAL_UNION_TYPE.
7359 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7360 use GNU_RECORD_TYPE if there are no fields so far. */
7361 if (Present (variant_part
))
7363 Node_Id gnat_discr
= Name (variant_part
), variant
;
7364 tree gnu_discr
= gnat_to_gnu (gnat_discr
);
7365 tree gnu_name
= TYPE_IDENTIFIER (gnu_record_type
);
7367 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr
))),
7369 tree gnu_union_type
, gnu_union_name
;
7370 tree this_first_free_pos
, gnu_variant_list
= NULL_TREE
;
7371 bool union_field_needs_strict_alignment
= false;
7372 auto_vec
<vinfo_t
, 16> variant_types
;
7373 vinfo_t
*gnu_variant
;
7374 unsigned int variants_align
= 0;
7378 = concat_name (gnu_name
, IDENTIFIER_POINTER (gnu_var_name
));
7380 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7381 are all in the variant part, to match the layout of C unions. There
7382 is an associated check below. */
7383 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
)
7384 gnu_union_type
= gnu_record_type
;
7388 = make_node (unchecked_union
? UNION_TYPE
: QUAL_UNION_TYPE
);
7390 TYPE_NAME (gnu_union_type
) = gnu_union_name
;
7391 SET_TYPE_ALIGN (gnu_union_type
, 0);
7392 TYPE_PACKED (gnu_union_type
) = TYPE_PACKED (gnu_record_type
);
7393 TYPE_REVERSE_STORAGE_ORDER (gnu_union_type
)
7394 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type
);
7397 /* If all the fields down to this level have a rep clause, find out
7398 whether all the fields at this level also have one. If so, then
7399 compute the new first free position to be passed downward. */
7400 this_first_free_pos
= first_free_pos
;
7401 if (this_first_free_pos
)
7403 for (gnu_field
= gnu_field_list
;
7405 gnu_field
= DECL_CHAIN (gnu_field
))
7406 if (DECL_FIELD_OFFSET (gnu_field
))
7408 tree pos
= bit_position (gnu_field
);
7409 if (!tree_int_cst_lt (pos
, this_first_free_pos
))
7411 = size_binop (PLUS_EXPR
, pos
, DECL_SIZE (gnu_field
));
7415 this_first_free_pos
= NULL_TREE
;
7420 /* We build the variants in two passes. The bulk of the work is done in
7421 the first pass, that is to say translating the GNAT nodes, building
7422 the container types and computing the associated properties. However
7423 we cannot finish up the container types during this pass because we
7424 don't know where the variant part will be placed until the end. */
7425 for (variant
= First_Non_Pragma (Variants (variant_part
));
7427 variant
= Next_Non_Pragma (variant
))
7429 tree gnu_variant_type
= make_node (RECORD_TYPE
);
7430 tree gnu_inner_name
, gnu_qual
;
7435 Get_Variant_Encoding (variant
);
7436 gnu_inner_name
= get_identifier_with_length (Name_Buffer
, Name_Len
);
7437 TYPE_NAME (gnu_variant_type
)
7438 = concat_name (gnu_union_name
,
7439 IDENTIFIER_POINTER (gnu_inner_name
));
7441 /* Set the alignment of the inner type in case we need to make
7442 inner objects into bitfields, but then clear it out so the
7443 record actually gets only the alignment required. */
7444 SET_TYPE_ALIGN (gnu_variant_type
, TYPE_ALIGN (gnu_record_type
));
7445 TYPE_PACKED (gnu_variant_type
) = TYPE_PACKED (gnu_record_type
);
7446 TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type
)
7447 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type
);
7449 /* Similarly, if the outer record has a size specified and all
7450 the fields have a rep clause, we can propagate the size. */
7451 if (all_rep_and_size
)
7453 TYPE_SIZE (gnu_variant_type
) = TYPE_SIZE (gnu_record_type
);
7454 TYPE_SIZE_UNIT (gnu_variant_type
)
7455 = TYPE_SIZE_UNIT (gnu_record_type
);
7458 /* Add the fields into the record type for the variant. Note that
7459 we aren't sure to really use it at this point, see below. */
7461 = components_to_record (gnu_variant_type
, Component_List (variant
),
7462 NULL_TREE
, packed
, definition
,
7463 !all_rep_and_size
, all_rep
,
7465 true, needs_xv_encodings
, true, reorder
,
7466 this_first_free_pos
,
7467 all_rep
|| this_first_free_pos
7468 ? NULL
: &gnu_rep_list
);
7470 /* Translate the qualifier and annotate the GNAT node. */
7471 gnu_qual
= choices_to_gnu (gnu_discr
, Discrete_Choices (variant
));
7472 Set_Present_Expr (variant
, annotate_value (gnu_qual
));
7474 /* Deal with packedness like in gnat_to_gnu_field. */
7475 if (components_need_strict_alignment (Component_List (variant
)))
7478 union_field_needs_strict_alignment
= true;
7482 = adjust_packed (gnu_variant_type
, gnu_record_type
, packed
);
7484 /* Push this variant onto the stack for the second pass. */
7485 vinfo
.type
= gnu_variant_type
;
7486 vinfo
.name
= gnu_inner_name
;
7487 vinfo
.qual
= gnu_qual
;
7488 vinfo
.has_rep
= has_rep
;
7489 vinfo
.packed
= field_packed
;
7490 variant_types
.safe_push (vinfo
);
7492 /* Compute the global properties that will determine the placement of
7493 the variant part. */
7494 variants_have_rep
|= has_rep
;
7495 if (!field_packed
&& TYPE_ALIGN (gnu_variant_type
) > variants_align
)
7496 variants_align
= TYPE_ALIGN (gnu_variant_type
);
7499 /* Round up the first free position to the alignment of the variant part
7500 for the variants without rep clause. This will guarantee a consistent
7501 layout independently of the placement of the variant part. */
7502 if (variants_have_rep
&& variants_align
> 0 && this_first_free_pos
)
7503 this_first_free_pos
= round_up (this_first_free_pos
, variants_align
);
7505 /* In the second pass, the container types are adjusted if necessary and
7506 finished up, then the corresponding fields of the variant part are
7507 built with their qualifier, unless this is an unchecked union. */
7508 FOR_EACH_VEC_ELT (variant_types
, i
, gnu_variant
)
7510 tree gnu_variant_type
= gnu_variant
->type
;
7511 tree gnu_field_list
= TYPE_FIELDS (gnu_variant_type
);
7513 /* If this is an Unchecked_Union whose fields are all in the variant
7514 part and we have a single field with no representation clause or
7515 placed at offset zero, use the field directly to match the layout
7517 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
7519 && !DECL_CHAIN (gnu_field_list
)
7520 && (!DECL_FIELD_OFFSET (gnu_field_list
)
7521 || integer_zerop (bit_position (gnu_field_list
))))
7523 gnu_field
= gnu_field_list
;
7524 DECL_CONTEXT (gnu_field
) = gnu_record_type
;
7528 /* Finalize the variant type now. We used to throw away empty
7529 record types but we no longer do that because we need them to
7530 generate complete debug info for the variant; otherwise, the
7531 union type definition will be lacking the fields associated
7532 with these empty variants. */
7533 if (gnu_field_list
&& variants_have_rep
&& !gnu_variant
->has_rep
)
7535 /* The variant part will be at offset 0 so we need to ensure
7536 that the fields are laid out starting from the first free
7537 position at this level. */
7538 tree gnu_rep_type
= make_node (RECORD_TYPE
);
7540 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type
)
7541 = TYPE_REVERSE_STORAGE_ORDER (gnu_variant_type
);
7542 finish_record_type (gnu_rep_type
, NULL_TREE
, 0, debug_info
);
7544 = create_rep_part (gnu_rep_type
, gnu_variant_type
,
7545 this_first_free_pos
);
7546 DECL_CHAIN (gnu_rep_part
) = gnu_field_list
;
7547 gnu_field_list
= gnu_rep_part
;
7548 finish_record_type (gnu_variant_type
, gnu_field_list
, 0,
7553 rest_of_record_type_compilation (gnu_variant_type
);
7554 create_type_decl (TYPE_NAME (gnu_variant_type
), gnu_variant_type
,
7555 true, needs_xv_encodings
, gnat_component_list
);
7558 = create_field_decl (gnu_variant
->name
, gnu_variant_type
,
7561 ? TYPE_SIZE (gnu_variant_type
) : 0,
7562 variants_have_rep
? bitsize_zero_node
: 0,
7563 gnu_variant
->packed
, 0);
7565 DECL_INTERNAL_P (gnu_field
) = 1;
7567 if (!unchecked_union
)
7568 DECL_QUALIFIER (gnu_field
) = gnu_variant
->qual
;
7571 DECL_CHAIN (gnu_field
) = gnu_variant_list
;
7572 gnu_variant_list
= gnu_field
;
7575 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7576 if (gnu_variant_list
)
7578 int union_field_packed
;
7580 if (all_rep_and_size
)
7582 TYPE_SIZE (gnu_union_type
) = TYPE_SIZE (gnu_record_type
);
7583 TYPE_SIZE_UNIT (gnu_union_type
)
7584 = TYPE_SIZE_UNIT (gnu_record_type
);
7587 finish_record_type (gnu_union_type
, nreverse (gnu_variant_list
),
7588 all_rep_and_size
? 1 : 0, needs_xv_encodings
);
7590 /* If GNU_UNION_TYPE is our record type, it means we must have an
7591 Unchecked_Union with no fields. Verify that and, if so, just
7593 if (gnu_union_type
== gnu_record_type
)
7595 gcc_assert (unchecked_union
7598 return variants_have_rep
;
7601 create_type_decl (TYPE_NAME (gnu_union_type
), gnu_union_type
, true,
7602 needs_xv_encodings
, gnat_component_list
);
7604 /* Deal with packedness like in gnat_to_gnu_field. */
7605 if (union_field_needs_strict_alignment
)
7606 union_field_packed
= 0;
7609 = adjust_packed (gnu_union_type
, gnu_record_type
, packed
);
7612 = create_field_decl (gnu_var_name
, gnu_union_type
, gnu_record_type
,
7614 ? TYPE_SIZE (gnu_union_type
) : 0,
7615 variants_have_rep
? bitsize_zero_node
: 0,
7616 union_field_packed
, 0);
7618 DECL_INTERNAL_P (gnu_variant_part
) = 1;
7622 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7623 permitted to reorder components, self-referential sizes or variable sizes.
7624 If they do, pull them out and put them onto the appropriate list. We have
7625 to do this in a separate pass since we want to handle the discriminants
7626 but can't play with them until we've used them in debugging data above.
7628 Similarly, pull out the fields with zero size and no rep clause, as they
7629 would otherwise modify the layout and thus very likely run afoul of the
7630 Ada semantics, which are different from those of C here.
7632 ??? If we reorder them, debugging information will be wrong but there is
7633 nothing that can be done about this at the moment. */
7634 gnu_last
= NULL_TREE
;
7636 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7639 DECL_CHAIN (gnu_last) = gnu_next; \
7641 gnu_field_list = gnu_next; \
7643 DECL_CHAIN (gnu_field) = (LIST); \
7644 (LIST) = gnu_field; \
7647 for (gnu_field
= gnu_field_list
; gnu_field
; gnu_field
= gnu_next
)
7649 gnu_next
= DECL_CHAIN (gnu_field
);
7651 if (DECL_FIELD_OFFSET (gnu_field
))
7653 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list
);
7657 if ((reorder
|| has_aliased_after_self_field
)
7658 && field_has_self_size (gnu_field
))
7660 MOVE_FROM_FIELD_LIST_TO (gnu_self_list
);
7664 if (reorder
&& field_has_variable_size (gnu_field
))
7666 MOVE_FROM_FIELD_LIST_TO (gnu_var_list
);
7670 if (DECL_SIZE (gnu_field
) && integer_zerop (DECL_SIZE (gnu_field
)))
7672 DECL_FIELD_OFFSET (gnu_field
) = size_zero_node
;
7673 SET_DECL_OFFSET_ALIGN (gnu_field
, BIGGEST_ALIGNMENT
);
7674 DECL_FIELD_BIT_OFFSET (gnu_field
) = bitsize_zero_node
;
7675 if (field_is_aliased (gnu_field
))
7676 SET_TYPE_ALIGN (gnu_record_type
,
7677 MAX (TYPE_ALIGN (gnu_record_type
),
7678 TYPE_ALIGN (TREE_TYPE (gnu_field
))));
7679 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list
);
7683 gnu_last
= gnu_field
;
7686 #undef MOVE_FROM_FIELD_LIST_TO
7688 gnu_field_list
= nreverse (gnu_field_list
);
7690 /* If permitted, we reorder the fields as follows:
7692 1) all fixed length fields,
7693 2) all fields whose length doesn't depend on discriminants,
7694 3) all fields whose length depends on discriminants,
7695 4) the variant part,
7697 within the record and within each variant recursively. */
7700 = chainon (gnu_field_list
, chainon (gnu_var_list
, gnu_self_list
));
7702 /* Otherwise, if there is an aliased field placed after a field whose length
7703 depends on discriminants, we put all the fields of the latter sort, last.
7704 We need to do this in case an object of this record type is mutable. */
7705 else if (has_aliased_after_self_field
)
7706 gnu_field_list
= chainon (gnu_field_list
, gnu_self_list
);
7708 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7709 in our REP list to the previous level because this level needs them in
7710 order to do a correct layout, i.e. avoid having overlapping fields. */
7711 if (p_gnu_rep_list
&& gnu_rep_list
)
7712 *p_gnu_rep_list
= chainon (*p_gnu_rep_list
, gnu_rep_list
);
7714 /* Deal with the annoying case of an extension of a record with variable size
7715 and partial rep clause, for which the _Parent field is forced at offset 0
7716 and has variable size, which we do not support below. Note that we cannot
7717 do it if the field has fixed size because we rely on the presence of the
7718 REP part built below to trigger the reordering of the fields in a derived
7719 record type when all the fields have a fixed position. */
7720 else if (gnu_rep_list
7721 && !DECL_CHAIN (gnu_rep_list
)
7722 && TREE_CODE (DECL_SIZE (gnu_rep_list
)) != INTEGER_CST
7723 && !variants_have_rep
7725 && integer_zerop (first_free_pos
)
7726 && integer_zerop (bit_position (gnu_rep_list
)))
7728 DECL_CHAIN (gnu_rep_list
) = gnu_field_list
;
7729 gnu_field_list
= gnu_rep_list
;
7730 gnu_rep_list
= NULL_TREE
;
7733 /* Otherwise, sort the fields by bit position and put them into their own
7734 record, before the others, if we also have fields without rep clause. */
7735 else if (gnu_rep_list
)
7737 tree gnu_rep_type
, gnu_rep_part
;
7738 int i
, len
= list_length (gnu_rep_list
);
7739 tree
*gnu_arr
= XALLOCAVEC (tree
, len
);
7741 /* If all the fields have a rep clause, we can do a flat layout. */
7742 layout_with_rep
= !gnu_field_list
7743 && (!gnu_variant_part
|| variants_have_rep
);
7745 = layout_with_rep
? gnu_record_type
: make_node (RECORD_TYPE
);
7747 for (gnu_field
= gnu_rep_list
, i
= 0;
7749 gnu_field
= DECL_CHAIN (gnu_field
), i
++)
7750 gnu_arr
[i
] = gnu_field
;
7752 qsort (gnu_arr
, len
, sizeof (tree
), compare_field_bitpos
);
7754 /* Put the fields in the list in order of increasing position, which
7755 means we start from the end. */
7756 gnu_rep_list
= NULL_TREE
;
7757 for (i
= len
- 1; i
>= 0; i
--)
7759 DECL_CHAIN (gnu_arr
[i
]) = gnu_rep_list
;
7760 gnu_rep_list
= gnu_arr
[i
];
7761 DECL_CONTEXT (gnu_arr
[i
]) = gnu_rep_type
;
7764 if (layout_with_rep
)
7765 gnu_field_list
= gnu_rep_list
;
7768 TYPE_REVERSE_STORAGE_ORDER (gnu_rep_type
)
7769 = TYPE_REVERSE_STORAGE_ORDER (gnu_record_type
);
7770 finish_record_type (gnu_rep_type
, gnu_rep_list
, 1, debug_info
);
7772 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7773 without rep clause are laid out starting from this position.
7774 Therefore, we force it as a minimal size on the REP part. */
7776 = create_rep_part (gnu_rep_type
, gnu_record_type
, first_free_pos
);
7778 /* Chain the REP part at the beginning of the field list. */
7779 DECL_CHAIN (gnu_rep_part
) = gnu_field_list
;
7780 gnu_field_list
= gnu_rep_part
;
7784 /* Chain the variant part at the end of the field list. */
7785 if (gnu_variant_part
)
7786 gnu_field_list
= chainon (gnu_field_list
, gnu_variant_part
);
7788 if (cancel_alignment
)
7789 SET_TYPE_ALIGN (gnu_record_type
, 0);
7791 TYPE_ARTIFICIAL (gnu_record_type
) = artificial
;
7793 finish_record_type (gnu_record_type
, gnu_field_list
, layout_with_rep
? 1 : 0,
7794 debug_info
&& !maybe_unused
);
7796 /* Chain the fields with zero size at the beginning of the field list. */
7798 TYPE_FIELDS (gnu_record_type
)
7799 = chainon (gnu_zero_list
, TYPE_FIELDS (gnu_record_type
));
7801 return (gnu_rep_list
&& !p_gnu_rep_list
) || variants_have_rep
;
7804 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7805 placed into an Esize, Component_Bit_Offset, or Component_Size value
7806 in the GNAT tree. */
7809 annotate_value (tree gnu_size
)
7812 Node_Ref_Or_Val ops
[3], ret
, pre_op1
= No_Uint
;
7813 struct tree_int_map in
;
7816 /* See if we've already saved the value for this node. */
7817 if (EXPR_P (gnu_size
))
7819 struct tree_int_map
*e
;
7821 in
.base
.from
= gnu_size
;
7822 e
= annotate_value_cache
->find (&in
);
7825 return (Node_Ref_Or_Val
) e
->to
;
7828 in
.base
.from
= NULL_TREE
;
7830 /* If we do not return inside this switch, TCODE will be set to the
7831 code to use for a Create_Node operand and LEN (set above) will be
7832 the number of recursive calls for us to make. */
7834 switch (TREE_CODE (gnu_size
))
7837 return TREE_OVERFLOW (gnu_size
) ? No_Uint
: UI_From_gnu (gnu_size
);
7840 /* The only case we handle here is a simple discriminant reference. */
7841 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size
, 1)))
7843 tree n
= DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size
, 1));
7845 /* Climb up the chain of successive extensions, if any. */
7846 while (TREE_CODE (TREE_OPERAND (gnu_size
, 0)) == COMPONENT_REF
7847 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size
, 0), 1))
7849 gnu_size
= TREE_OPERAND (gnu_size
, 0);
7851 if (TREE_CODE (TREE_OPERAND (gnu_size
, 0)) == PLACEHOLDER_EXPR
)
7853 Create_Node (Discrim_Val
, annotate_value (n
), No_Uint
, No_Uint
);
7858 CASE_CONVERT
: case NON_LVALUE_EXPR
:
7859 return annotate_value (TREE_OPERAND (gnu_size
, 0));
7861 /* Now just list the operations we handle. */
7862 case COND_EXPR
: tcode
= Cond_Expr
; break;
7863 case PLUS_EXPR
: tcode
= Plus_Expr
; break;
7864 case MINUS_EXPR
: tcode
= Minus_Expr
; break;
7865 case MULT_EXPR
: tcode
= Mult_Expr
; break;
7866 case TRUNC_DIV_EXPR
: tcode
= Trunc_Div_Expr
; break;
7867 case CEIL_DIV_EXPR
: tcode
= Ceil_Div_Expr
; break;
7868 case FLOOR_DIV_EXPR
: tcode
= Floor_Div_Expr
; break;
7869 case TRUNC_MOD_EXPR
: tcode
= Trunc_Mod_Expr
; break;
7870 case CEIL_MOD_EXPR
: tcode
= Ceil_Mod_Expr
; break;
7871 case FLOOR_MOD_EXPR
: tcode
= Floor_Mod_Expr
; break;
7872 case EXACT_DIV_EXPR
: tcode
= Exact_Div_Expr
; break;
7873 case NEGATE_EXPR
: tcode
= Negate_Expr
; break;
7874 case MIN_EXPR
: tcode
= Min_Expr
; break;
7875 case MAX_EXPR
: tcode
= Max_Expr
; break;
7876 case ABS_EXPR
: tcode
= Abs_Expr
; break;
7877 case TRUTH_ANDIF_EXPR
: tcode
= Truth_Andif_Expr
; break;
7878 case TRUTH_ORIF_EXPR
: tcode
= Truth_Orif_Expr
; break;
7879 case TRUTH_AND_EXPR
: tcode
= Truth_And_Expr
; break;
7880 case TRUTH_OR_EXPR
: tcode
= Truth_Or_Expr
; break;
7881 case TRUTH_XOR_EXPR
: tcode
= Truth_Xor_Expr
; break;
7882 case TRUTH_NOT_EXPR
: tcode
= Truth_Not_Expr
; break;
7883 case LT_EXPR
: tcode
= Lt_Expr
; break;
7884 case LE_EXPR
: tcode
= Le_Expr
; break;
7885 case GT_EXPR
: tcode
= Gt_Expr
; break;
7886 case GE_EXPR
: tcode
= Ge_Expr
; break;
7887 case EQ_EXPR
: tcode
= Eq_Expr
; break;
7888 case NE_EXPR
: tcode
= Ne_Expr
; break;
7891 tcode
= Bit_And_Expr
;
7892 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7893 Such values appear in expressions with aligning patterns. Note that,
7894 since sizetype is unsigned, we have to jump through some hoops. */
7895 if (TREE_CODE (TREE_OPERAND (gnu_size
, 1)) == INTEGER_CST
)
7897 tree op1
= TREE_OPERAND (gnu_size
, 1);
7898 wide_int signed_op1
= wi::sext (op1
, TYPE_PRECISION (sizetype
));
7899 if (wi::neg_p (signed_op1
))
7901 op1
= wide_int_to_tree (sizetype
, wi::neg (signed_op1
));
7902 pre_op1
= annotate_value (build1 (NEGATE_EXPR
, sizetype
, op1
));
7908 /* In regular mode, inline back only if symbolic annotation is requested
7909 in order to avoid memory explosion on big discriminated record types.
7910 But not in ASIS mode, as symbolic annotation is required for DDA. */
7911 if (List_Representation_Info
== 3 || type_annotate_only
)
7913 tree t
= maybe_inline_call_in_expr (gnu_size
);
7915 return annotate_value (t
);
7918 return Uint_Minus_1
;
7920 /* Fall through... */
7926 /* Now get each of the operands that's relevant for this code. If any
7927 cannot be expressed as a repinfo node, say we can't. */
7928 for (i
= 0; i
< 3; i
++)
7931 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (gnu_size
)); i
++)
7933 if (i
== 1 && pre_op1
!= No_Uint
)
7936 ops
[i
] = annotate_value (TREE_OPERAND (gnu_size
, i
));
7937 if (ops
[i
] == No_Uint
)
7941 ret
= Create_Node (tcode
, ops
[0], ops
[1], ops
[2]);
7943 /* Save the result in the cache. */
7946 struct tree_int_map
**h
;
7947 /* We can't assume the hash table data hasn't moved since the initial
7948 look up, so we have to search again. Allocating and inserting an
7949 entry at that point would be an alternative, but then we'd better
7950 discard the entry if we decided not to cache it. */
7951 h
= annotate_value_cache
->find_slot (&in
, INSERT
);
7953 *h
= ggc_alloc
<tree_int_map
> ();
7954 (*h
)->base
.from
= gnu_size
;
7961 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7962 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7963 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7964 BY_REF is true if the object is used by reference. */
7967 annotate_object (Entity_Id gnat_entity
, tree gnu_type
, tree size
, bool by_ref
)
7971 if (TYPE_IS_FAT_POINTER_P (gnu_type
))
7972 gnu_type
= TYPE_UNCONSTRAINED_ARRAY (gnu_type
);
7974 gnu_type
= TREE_TYPE (gnu_type
);
7977 if (Unknown_Esize (gnat_entity
))
7979 if (TREE_CODE (gnu_type
) == RECORD_TYPE
7980 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
7981 size
= TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
))));
7983 size
= TYPE_SIZE (gnu_type
);
7986 Set_Esize (gnat_entity
, annotate_value (size
));
7989 if (Unknown_Alignment (gnat_entity
))
7990 Set_Alignment (gnat_entity
,
7991 UI_From_Int (TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
));
7994 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7995 Return NULL_TREE if there is no such element in the list. */
7998 purpose_member_field (const_tree elem
, tree list
)
8002 tree field
= TREE_PURPOSE (list
);
8003 if (SAME_FIELD_P (field
, elem
))
8005 list
= TREE_CHAIN (list
);
8010 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
8011 set Component_Bit_Offset and Esize of the components to the position and
8012 size used by Gigi. */
8015 annotate_rep (Entity_Id gnat_entity
, tree gnu_type
)
8017 Entity_Id gnat_field
;
8020 /* We operate by first making a list of all fields and their position (we
8021 can get the size easily) and then update all the sizes in the tree. */
8023 = build_position_list (gnu_type
, false, size_zero_node
, bitsize_zero_node
,
8024 BIGGEST_ALIGNMENT
, NULL_TREE
);
8026 for (gnat_field
= First_Entity (gnat_entity
);
8027 Present (gnat_field
);
8028 gnat_field
= Next_Entity (gnat_field
))
8029 if (Ekind (gnat_field
) == E_Component
8030 || (Ekind (gnat_field
) == E_Discriminant
8031 && !Is_Unchecked_Union (Scope (gnat_field
))))
8033 tree t
= purpose_member_field (gnat_to_gnu_field_decl (gnat_field
),
8039 /* If we are just annotating types and the type is tagged, the tag
8040 and the parent components are not generated by the front-end so
8041 we need to add the appropriate offset to each component without
8042 representation clause. */
8043 if (type_annotate_only
8044 && Is_Tagged_Type (gnat_entity
)
8045 && No (Component_Clause (gnat_field
)))
8047 /* For a component appearing in the current extension, the
8048 offset is the size of the parent. */
8049 if (Is_Derived_Type (gnat_entity
)
8050 && Original_Record_Component (gnat_field
) == gnat_field
)
8052 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity
))),
8055 parent_offset
= bitsize_int (POINTER_SIZE
);
8057 if (TYPE_FIELDS (gnu_type
))
8059 = round_up (parent_offset
,
8060 DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
8063 parent_offset
= bitsize_zero_node
;
8065 Set_Component_Bit_Offset
8068 (size_binop (PLUS_EXPR
,
8069 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t
), 0),
8070 TREE_VEC_ELT (TREE_VALUE (t
), 2)),
8073 Set_Esize (gnat_field
,
8074 annotate_value (DECL_SIZE (TREE_PURPOSE (t
))));
8076 else if (Is_Tagged_Type (gnat_entity
) && Is_Derived_Type (gnat_entity
))
8078 /* If there is no entry, this is an inherited component whose
8079 position is the same as in the parent type. */
8080 Entity_Id gnat_orig_field
= Original_Record_Component (gnat_field
);
8082 /* If we are just annotating types, discriminants renaming those of
8083 the parent have no entry so deal with them specifically. */
8084 if (type_annotate_only
8085 && gnat_orig_field
== gnat_field
8086 && Ekind (gnat_field
) == E_Discriminant
)
8087 gnat_orig_field
= Corresponding_Discriminant (gnat_field
);
8089 Set_Component_Bit_Offset (gnat_field
,
8090 Component_Bit_Offset (gnat_orig_field
));
8092 Set_Esize (gnat_field
, Esize (gnat_orig_field
));
8097 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
8098 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
8099 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
8100 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
8101 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
8102 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
8103 pre-existing list to be chained to the newly created entries. */
8106 build_position_list (tree gnu_type
, bool do_not_flatten_variant
, tree gnu_pos
,
8107 tree gnu_bitpos
, unsigned int offset_align
, tree gnu_list
)
8111 for (gnu_field
= TYPE_FIELDS (gnu_type
);
8113 gnu_field
= DECL_CHAIN (gnu_field
))
8115 tree gnu_our_bitpos
= size_binop (PLUS_EXPR
, gnu_bitpos
,
8116 DECL_FIELD_BIT_OFFSET (gnu_field
));
8117 tree gnu_our_offset
= size_binop (PLUS_EXPR
, gnu_pos
,
8118 DECL_FIELD_OFFSET (gnu_field
));
8119 unsigned int our_offset_align
8120 = MIN (offset_align
, DECL_OFFSET_ALIGN (gnu_field
));
8121 tree v
= make_tree_vec (3);
8123 TREE_VEC_ELT (v
, 0) = gnu_our_offset
;
8124 TREE_VEC_ELT (v
, 1) = size_int (our_offset_align
);
8125 TREE_VEC_ELT (v
, 2) = gnu_our_bitpos
;
8126 gnu_list
= tree_cons (gnu_field
, v
, gnu_list
);
8128 /* Recurse on internal fields, flattening the nested fields except for
8129 those in the variant part, if requested. */
8130 if (DECL_INTERNAL_P (gnu_field
))
8132 tree gnu_field_type
= TREE_TYPE (gnu_field
);
8133 if (do_not_flatten_variant
8134 && TREE_CODE (gnu_field_type
) == QUAL_UNION_TYPE
)
8136 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
8137 size_zero_node
, bitsize_zero_node
,
8138 BIGGEST_ALIGNMENT
, gnu_list
);
8141 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
8142 gnu_our_offset
, gnu_our_bitpos
,
8143 our_offset_align
, gnu_list
);
8150 /* Return a list describing the substitutions needed to reflect the
8151 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
8152 be in any order. The values in an element of the list are in the form
8153 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
8154 a definition of GNAT_SUBTYPE. */
8156 static vec
<subst_pair
>
8157 build_subst_list (Entity_Id gnat_subtype
, Entity_Id gnat_type
, bool definition
)
8159 vec
<subst_pair
> gnu_list
= vNULL
;
8160 Entity_Id gnat_discrim
;
8161 Node_Id gnat_constr
;
8163 for (gnat_discrim
= First_Stored_Discriminant (gnat_type
),
8164 gnat_constr
= First_Elmt (Stored_Constraint (gnat_subtype
));
8165 Present (gnat_discrim
);
8166 gnat_discrim
= Next_Stored_Discriminant (gnat_discrim
),
8167 gnat_constr
= Next_Elmt (gnat_constr
))
8168 /* Ignore access discriminants. */
8169 if (!Is_Access_Type (Etype (Node (gnat_constr
))))
8171 tree gnu_field
= gnat_to_gnu_field_decl (gnat_discrim
);
8172 tree replacement
= convert (TREE_TYPE (gnu_field
),
8173 elaborate_expression
8174 (Node (gnat_constr
), gnat_subtype
,
8175 get_entity_char (gnat_discrim
),
8176 definition
, true, false));
8177 subst_pair s
= {gnu_field
, replacement
};
8178 gnu_list
.safe_push (s
);
8184 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
8185 variants of QUAL_UNION_TYPE that are still relevant after applying
8186 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
8187 list to be prepended to the newly created entries. */
8189 static vec
<variant_desc
>
8190 build_variant_list (tree qual_union_type
, vec
<subst_pair
> subst_list
,
8191 vec
<variant_desc
> gnu_list
)
8195 for (gnu_field
= TYPE_FIELDS (qual_union_type
);
8197 gnu_field
= DECL_CHAIN (gnu_field
))
8199 tree qual
= DECL_QUALIFIER (gnu_field
);
8203 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8204 qual
= SUBSTITUTE_IN_EXPR (qual
, s
->discriminant
, s
->replacement
);
8206 /* If the new qualifier is not unconditionally false, its variant may
8207 still be accessed. */
8208 if (!integer_zerop (qual
))
8210 tree variant_type
= TREE_TYPE (gnu_field
), variant_subpart
;
8211 variant_desc v
= {variant_type
, gnu_field
, qual
, NULL_TREE
};
8213 gnu_list
.safe_push (v
);
8215 /* Recurse on the variant subpart of the variant, if any. */
8216 variant_subpart
= get_variant_part (variant_type
);
8217 if (variant_subpart
)
8218 gnu_list
= build_variant_list (TREE_TYPE (variant_subpart
),
8219 subst_list
, gnu_list
);
8221 /* If the new qualifier is unconditionally true, the subsequent
8222 variants cannot be accessed. */
8223 if (integer_onep (qual
))
8231 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
8232 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
8233 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
8234 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
8235 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
8236 true if we are being called to process the Component_Size of GNAT_OBJECT;
8237 this is used only for error messages. ZERO_OK is true if a size of zero
8238 is permitted; if ZERO_OK is false, it means that a size of zero should be
8239 treated as an unspecified size. */
8242 validate_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_object
,
8243 enum tree_code kind
, bool component_p
, bool zero_ok
)
8245 Node_Id gnat_error_node
;
8246 tree type_size
, size
;
8248 /* Return 0 if no size was specified. */
8249 if (uint_size
== No_Uint
)
8252 /* Ignore a negative size since that corresponds to our back-annotation. */
8253 if (UI_Lt (uint_size
, Uint_0
))
8256 /* Find the node to use for error messages. */
8257 if ((Ekind (gnat_object
) == E_Component
8258 || Ekind (gnat_object
) == E_Discriminant
)
8259 && Present (Component_Clause (gnat_object
)))
8260 gnat_error_node
= Last_Bit (Component_Clause (gnat_object
));
8261 else if (Present (Size_Clause (gnat_object
)))
8262 gnat_error_node
= Expression (Size_Clause (gnat_object
));
8264 gnat_error_node
= gnat_object
;
8266 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8267 but cannot be represented in bitsizetype. */
8268 size
= UI_To_gnu (uint_size
, bitsizetype
);
8269 if (TREE_OVERFLOW (size
))
8272 post_error_ne ("component size for& is too large", gnat_error_node
,
8275 post_error_ne ("size for& is too large", gnat_error_node
,
8280 /* Ignore a zero size if it is not permitted. */
8281 if (!zero_ok
&& integer_zerop (size
))
8284 /* The size of objects is always a multiple of a byte. */
8285 if (kind
== VAR_DECL
8286 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
, size
, bitsize_unit_node
)))
8289 post_error_ne ("component size for& is not a multiple of Storage_Unit",
8290 gnat_error_node
, gnat_object
);
8292 post_error_ne ("size for& is not a multiple of Storage_Unit",
8293 gnat_error_node
, gnat_object
);
8297 /* If this is an integral type or a packed array type, the front-end has
8298 already verified the size, so we need not do it here (which would mean
8299 checking against the bounds). However, if this is an aliased object,
8300 it may not be smaller than the type of the object. */
8301 if ((INTEGRAL_TYPE_P (gnu_type
) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type
))
8302 && !(kind
== VAR_DECL
&& Is_Aliased (gnat_object
)))
8305 /* If the object is a record that contains a template, add the size of the
8306 template to the specified size. */
8307 if (TREE_CODE (gnu_type
) == RECORD_TYPE
8308 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
8309 size
= size_binop (PLUS_EXPR
, DECL_SIZE (TYPE_FIELDS (gnu_type
)), size
);
8311 if (kind
== VAR_DECL
8312 /* If a type needs strict alignment, a component of this type in
8313 a packed record cannot be packed and thus uses the type size. */
8314 || (kind
== TYPE_DECL
&& Strict_Alignment (gnat_object
)))
8315 type_size
= TYPE_SIZE (gnu_type
);
8317 type_size
= rm_size (gnu_type
);
8319 /* Modify the size of a discriminated type to be the maximum size. */
8320 if (type_size
&& CONTAINS_PLACEHOLDER_P (type_size
))
8321 type_size
= max_size (type_size
, true);
8323 /* If this is an access type or a fat pointer, the minimum size is that given
8324 by the smallest integral mode that's valid for pointers. */
8325 if (TREE_CODE (gnu_type
) == POINTER_TYPE
|| TYPE_IS_FAT_POINTER_P (gnu_type
))
8327 machine_mode p_mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
8328 while (!targetm
.valid_pointer_mode (p_mode
))
8329 p_mode
= GET_MODE_WIDER_MODE (p_mode
);
8330 type_size
= bitsize_int (GET_MODE_BITSIZE (p_mode
));
8333 /* Issue an error either if the default size of the object isn't a constant
8334 or if the new size is smaller than it. */
8335 if (TREE_CODE (type_size
) != INTEGER_CST
8336 || TREE_OVERFLOW (type_size
)
8337 || tree_int_cst_lt (size
, type_size
))
8341 ("component size for& too small{, minimum allowed is ^}",
8342 gnat_error_node
, gnat_object
, type_size
);
8345 ("size for& too small{, minimum allowed is ^}",
8346 gnat_error_node
, gnat_object
, type_size
);
8353 /* Similarly, but both validate and process a value of RM size. This routine
8354 is only called for types. */
8357 set_rm_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_entity
)
8359 Node_Id gnat_attr_node
;
8360 tree old_size
, size
;
8362 /* Do nothing if no size was specified. */
8363 if (uint_size
== No_Uint
)
8366 /* Ignore a negative size since that corresponds to our back-annotation. */
8367 if (UI_Lt (uint_size
, Uint_0
))
8370 /* Only issue an error if a Value_Size clause was explicitly given.
8371 Otherwise, we'd be duplicating an error on the Size clause. */
8373 = Get_Attribute_Definition_Clause (gnat_entity
, Attr_Value_Size
);
8375 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
8376 but cannot be represented in bitsizetype. */
8377 size
= UI_To_gnu (uint_size
, bitsizetype
);
8378 if (TREE_OVERFLOW (size
))
8380 if (Present (gnat_attr_node
))
8381 post_error_ne ("Value_Size for& is too large", gnat_attr_node
,
8386 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8387 exists, or this is an integer type, in which case the front-end will
8388 have always set it. */
8389 if (No (gnat_attr_node
)
8390 && integer_zerop (size
)
8391 && !Has_Size_Clause (gnat_entity
)
8392 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
8395 old_size
= rm_size (gnu_type
);
8397 /* If the old size is self-referential, get the maximum size. */
8398 if (CONTAINS_PLACEHOLDER_P (old_size
))
8399 old_size
= max_size (old_size
, true);
8401 /* Issue an error either if the old size of the object isn't a constant or
8402 if the new size is smaller than it. The front-end has already verified
8403 this for scalar and packed array types. */
8404 if (TREE_CODE (old_size
) != INTEGER_CST
8405 || TREE_OVERFLOW (old_size
)
8406 || (AGGREGATE_TYPE_P (gnu_type
)
8407 && !(TREE_CODE (gnu_type
) == ARRAY_TYPE
8408 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type
))
8409 && !(TYPE_IS_PADDING_P (gnu_type
)
8410 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
))) == ARRAY_TYPE
8411 && TYPE_PACKED_ARRAY_TYPE_P
8412 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))
8413 && tree_int_cst_lt (size
, old_size
)))
8415 if (Present (gnat_attr_node
))
8417 ("Value_Size for& too small{, minimum allowed is ^}",
8418 gnat_attr_node
, gnat_entity
, old_size
);
8422 /* Otherwise, set the RM size proper for integral types... */
8423 if ((TREE_CODE (gnu_type
) == INTEGER_TYPE
8424 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
8425 || (TREE_CODE (gnu_type
) == ENUMERAL_TYPE
8426 || TREE_CODE (gnu_type
) == BOOLEAN_TYPE
))
8427 SET_TYPE_RM_SIZE (gnu_type
, size
);
8429 /* ...or the Ada size for record and union types. */
8430 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
8431 && !TYPE_FAT_POINTER_P (gnu_type
))
8432 SET_TYPE_ADA_SIZE (gnu_type
, size
);
8435 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8436 a type or object whose present alignment is ALIGN. If this alignment is
8437 valid, return it. Otherwise, give an error and return ALIGN. */
8440 validate_alignment (Uint alignment
, Entity_Id gnat_entity
, unsigned int align
)
8442 unsigned int max_allowed_alignment
= get_target_maximum_allowed_alignment ();
8443 unsigned int new_align
;
8444 Node_Id gnat_error_node
;
8446 /* Don't worry about checking alignment if alignment was not specified
8447 by the source program and we already posted an error for this entity. */
8448 if (Error_Posted (gnat_entity
) && !Has_Alignment_Clause (gnat_entity
))
8451 /* Post the error on the alignment clause if any. Note, for the implicit
8452 base type of an array type, the alignment clause is on the first
8454 if (Present (Alignment_Clause (gnat_entity
)))
8455 gnat_error_node
= Expression (Alignment_Clause (gnat_entity
));
8457 else if (Is_Itype (gnat_entity
)
8458 && Is_Array_Type (gnat_entity
)
8459 && Etype (gnat_entity
) == gnat_entity
8460 && Present (Alignment_Clause (First_Subtype (gnat_entity
))))
8462 Expression (Alignment_Clause (First_Subtype (gnat_entity
)));
8465 gnat_error_node
= gnat_entity
;
8467 /* Within GCC, an alignment is an integer, so we must make sure a value is
8468 specified that fits in that range. Also, there is an upper bound to
8469 alignments we can support/allow. */
8470 if (!UI_Is_In_Int_Range (alignment
)
8471 || ((new_align
= UI_To_Int (alignment
)) > max_allowed_alignment
))
8472 post_error_ne_num ("largest supported alignment for& is ^",
8473 gnat_error_node
, gnat_entity
, max_allowed_alignment
);
8474 else if (!(Present (Alignment_Clause (gnat_entity
))
8475 && From_At_Mod (Alignment_Clause (gnat_entity
)))
8476 && new_align
* BITS_PER_UNIT
< align
)
8478 unsigned int double_align
;
8479 bool is_capped_double
, align_clause
;
8481 /* If the default alignment of "double" or larger scalar types is
8482 specifically capped and the new alignment is above the cap, do
8483 not post an error and change the alignment only if there is an
8484 alignment clause; this makes it possible to have the associated
8485 GCC type overaligned by default for performance reasons. */
8486 if ((double_align
= double_float_alignment
) > 0)
8489 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
8491 = is_double_float_or_array (gnat_type
, &align_clause
);
8493 else if ((double_align
= double_scalar_alignment
) > 0)
8496 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
8498 = is_double_scalar_or_array (gnat_type
, &align_clause
);
8501 is_capped_double
= align_clause
= false;
8503 if (is_capped_double
&& new_align
>= double_align
)
8506 align
= new_align
* BITS_PER_UNIT
;
8510 if (is_capped_double
)
8511 align
= double_align
* BITS_PER_UNIT
;
8513 post_error_ne_num ("alignment for& must be at least ^",
8514 gnat_error_node
, gnat_entity
,
8515 align
/ BITS_PER_UNIT
);
8520 new_align
= (new_align
> 0 ? new_align
* BITS_PER_UNIT
: 1);
8521 if (new_align
> align
)
8528 /* Verify that TYPE is something we can implement atomically. If not, issue
8529 an error for GNAT_ENTITY. COMPONENT_P is true if we are being called to
8530 process a component type. */
8533 check_ok_for_atomic_type (tree type
, Entity_Id gnat_entity
, bool component_p
)
8535 Node_Id gnat_error_point
= gnat_entity
;
8538 enum mode_class mclass
;
8542 /* If this is an anonymous base type, nothing to check, the error will be
8543 reported on the source type if need be. */
8544 if (!Comes_From_Source (gnat_entity
))
8547 mode
= TYPE_MODE (type
);
8548 mclass
= GET_MODE_CLASS (mode
);
8549 align
= TYPE_ALIGN (type
);
8550 size
= TYPE_SIZE (type
);
8552 /* Consider all aligned floating-point types atomic and any aligned types
8553 that are represented by integers no wider than a machine word. */
8554 if ((mclass
== MODE_FLOAT
8555 || ((mclass
== MODE_INT
|| mclass
== MODE_PARTIAL_INT
)
8556 && GET_MODE_BITSIZE (mode
) <= BITS_PER_WORD
))
8557 && align
>= GET_MODE_ALIGNMENT (mode
))
8560 /* For the moment, also allow anything that has an alignment equal to its
8561 size and which is smaller than a word. */
8563 && TREE_CODE (size
) == INTEGER_CST
8564 && compare_tree_int (size
, align
) == 0
8565 && align
<= BITS_PER_WORD
)
8568 for (gnat_node
= First_Rep_Item (gnat_entity
);
8569 Present (gnat_node
);
8570 gnat_node
= Next_Rep_Item (gnat_node
))
8571 if (Nkind (gnat_node
) == N_Pragma
)
8573 unsigned char pragma_id
8574 = Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)));
8576 if ((pragma_id
== Pragma_Atomic
&& !component_p
)
8577 || (pragma_id
== Pragma_Atomic_Components
&& component_p
))
8579 gnat_error_point
= First (Pragma_Argument_Associations (gnat_node
));
8585 post_error_ne ("atomic access to component of & cannot be guaranteed",
8586 gnat_error_point
, gnat_entity
);
8587 else if (Is_Volatile_Full_Access (gnat_entity
))
8588 post_error_ne ("volatile full access to & cannot be guaranteed",
8589 gnat_error_point
, gnat_entity
);
8591 post_error_ne ("atomic access to & cannot be guaranteed",
8592 gnat_error_point
, gnat_entity
);
8596 /* Helper for the intrin compatibility checks family. Evaluate whether
8597 two types are definitely incompatible. */
8600 intrin_types_incompatible_p (tree t1
, tree t2
)
8602 enum tree_code code
;
8604 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
8607 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
8610 if (TREE_CODE (t1
) != TREE_CODE (t2
))
8613 code
= TREE_CODE (t1
);
8619 return TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
);
8622 case REFERENCE_TYPE
:
8623 /* Assume designated types are ok. We'd need to account for char * and
8624 void * variants to do better, which could rapidly get messy and isn't
8625 clearly worth the effort. */
8635 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8636 on the Ada/builtin argument lists for the INB binding. */
8639 intrin_arglists_compatible_p (intrin_binding_t
* inb
)
8641 function_args_iterator ada_iter
, btin_iter
;
8643 function_args_iter_init (&ada_iter
, inb
->ada_fntype
);
8644 function_args_iter_init (&btin_iter
, inb
->btin_fntype
);
8646 /* Sequence position of the last argument we checked. */
8651 tree ada_type
= function_args_iter_cond (&ada_iter
);
8652 tree btin_type
= function_args_iter_cond (&btin_iter
);
8654 /* If we've exhausted both lists simultaneously, we're done. */
8655 if (!ada_type
&& !btin_type
)
8658 /* If one list is shorter than the other, they fail to match. */
8659 if (!ada_type
|| !btin_type
)
8662 /* If we're done with the Ada args and not with the internal builtin
8663 args, or the other way around, complain. */
8664 if (ada_type
== void_type_node
8665 && btin_type
!= void_type_node
)
8667 post_error ("?Ada arguments list too short!", inb
->gnat_entity
);
8671 if (btin_type
== void_type_node
8672 && ada_type
!= void_type_node
)
8674 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8675 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8679 /* Otherwise, check that types match for the current argument. */
8681 if (intrin_types_incompatible_p (ada_type
, btin_type
))
8683 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8684 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8689 function_args_iter_next (&ada_iter
);
8690 function_args_iter_next (&btin_iter
);
8696 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8697 on the Ada/builtin return values for the INB binding. */
8700 intrin_return_compatible_p (intrin_binding_t
* inb
)
8702 tree ada_return_type
= TREE_TYPE (inb
->ada_fntype
);
8703 tree btin_return_type
= TREE_TYPE (inb
->btin_fntype
);
8705 /* Accept function imported as procedure, common and convenient. */
8706 if (VOID_TYPE_P (ada_return_type
)
8707 && !VOID_TYPE_P (btin_return_type
))
8710 /* If return type is Address (integer type), map it to void *. */
8711 if (Is_Descendant_Of_Address (Etype (inb
->gnat_entity
)))
8712 ada_return_type
= ptr_type_node
;
8714 /* Check return types compatibility otherwise. Note that this
8715 handles void/void as well. */
8716 if (intrin_types_incompatible_p (btin_return_type
, ada_return_type
))
8718 post_error ("?intrinsic binding type mismatch on return value!",
8726 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8727 compatible. Issue relevant warnings when they are not.
8729 This is intended as a light check to diagnose the most obvious cases, not
8730 as a full fledged type compatibility predicate. It is the programmer's
8731 responsibility to ensure correctness of the Ada declarations in Imports,
8732 especially when binding straight to a compiler internal. */
8735 intrin_profiles_compatible_p (intrin_binding_t
* inb
)
8737 /* Check compatibility on return values and argument lists, each responsible
8738 for posting warnings as appropriate. Ensure use of the proper sloc for
8741 bool arglists_compatible_p
, return_compatible_p
;
8742 location_t saved_location
= input_location
;
8744 Sloc_to_locus (Sloc (inb
->gnat_entity
), &input_location
);
8746 return_compatible_p
= intrin_return_compatible_p (inb
);
8747 arglists_compatible_p
= intrin_arglists_compatible_p (inb
);
8749 input_location
= saved_location
;
8751 return return_compatible_p
&& arglists_compatible_p
;
8754 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8755 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8756 specified size for this field. POS_LIST is a position list describing
8757 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8761 create_field_decl_from (tree old_field
, tree field_type
, tree record_type
,
8762 tree size
, tree pos_list
,
8763 vec
<subst_pair
> subst_list
)
8765 tree t
= TREE_VALUE (purpose_member (old_field
, pos_list
));
8766 tree pos
= TREE_VEC_ELT (t
, 0), bitpos
= TREE_VEC_ELT (t
, 2);
8767 unsigned int offset_align
= tree_to_uhwi (TREE_VEC_ELT (t
, 1));
8768 tree new_pos
, new_field
;
8772 if (CONTAINS_PLACEHOLDER_P (pos
))
8773 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8774 pos
= SUBSTITUTE_IN_EXPR (pos
, s
->discriminant
, s
->replacement
);
8776 /* If the position is now a constant, we can set it as the position of the
8777 field when we make it. Otherwise, we need to deal with it specially. */
8778 if (TREE_CONSTANT (pos
))
8779 new_pos
= bit_from_pos (pos
, bitpos
);
8781 new_pos
= NULL_TREE
;
8784 = create_field_decl (DECL_NAME (old_field
), field_type
, record_type
,
8785 size
, new_pos
, DECL_PACKED (old_field
),
8786 !DECL_NONADDRESSABLE_P (old_field
));
8790 normalize_offset (&pos
, &bitpos
, offset_align
);
8791 /* Finalize the position. */
8792 DECL_FIELD_OFFSET (new_field
) = variable_size (pos
);
8793 DECL_FIELD_BIT_OFFSET (new_field
) = bitpos
;
8794 SET_DECL_OFFSET_ALIGN (new_field
, offset_align
);
8795 DECL_SIZE (new_field
) = size
;
8796 DECL_SIZE_UNIT (new_field
)
8797 = convert (sizetype
,
8798 size_binop (CEIL_DIV_EXPR
, size
, bitsize_unit_node
));
8799 layout_decl (new_field
, DECL_OFFSET_ALIGN (new_field
));
8802 DECL_INTERNAL_P (new_field
) = DECL_INTERNAL_P (old_field
);
8803 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, old_field
);
8804 DECL_DISCRIMINANT_NUMBER (new_field
) = DECL_DISCRIMINANT_NUMBER (old_field
);
8805 TREE_THIS_VOLATILE (new_field
) = TREE_THIS_VOLATILE (old_field
);
8810 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8811 it is the minimal size the REP_PART must have. */
8814 create_rep_part (tree rep_type
, tree record_type
, tree min_size
)
8818 if (min_size
&& !tree_int_cst_lt (TYPE_SIZE (rep_type
), min_size
))
8819 min_size
= NULL_TREE
;
8821 field
= create_field_decl (get_identifier ("REP"), rep_type
, record_type
,
8822 min_size
, NULL_TREE
, 0, 1);
8823 DECL_INTERNAL_P (field
) = 1;
8828 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8831 get_rep_part (tree record_type
)
8833 tree field
= TYPE_FIELDS (record_type
);
8835 /* The REP part is the first field, internal, another record, and its name
8836 starts with an 'R'. */
8838 && DECL_INTERNAL_P (field
)
8839 && TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
8840 && IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == 'R')
8846 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8849 get_variant_part (tree record_type
)
8853 /* The variant part is the only internal field that is a qualified union. */
8854 for (field
= TYPE_FIELDS (record_type
); field
; field
= DECL_CHAIN (field
))
8855 if (DECL_INTERNAL_P (field
)
8856 && TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
)
8862 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8863 the list of variants to be used and RECORD_TYPE is the type of the parent.
8864 POS_LIST is a position list describing the layout of fields present in
8865 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8869 create_variant_part_from (tree old_variant_part
,
8870 vec
<variant_desc
> variant_list
,
8871 tree record_type
, tree pos_list
,
8872 vec
<subst_pair
> subst_list
)
8874 tree offset
= DECL_FIELD_OFFSET (old_variant_part
);
8875 tree old_union_type
= TREE_TYPE (old_variant_part
);
8876 tree new_union_type
, new_variant_part
;
8877 tree union_field_list
= NULL_TREE
;
8881 /* First create the type of the variant part from that of the old one. */
8882 new_union_type
= make_node (QUAL_UNION_TYPE
);
8883 TYPE_NAME (new_union_type
)
8884 = concat_name (TYPE_NAME (record_type
),
8885 IDENTIFIER_POINTER (DECL_NAME (old_variant_part
)));
8887 /* If the position of the variant part is constant, subtract it from the
8888 size of the type of the parent to get the new size. This manual CSE
8889 reduces the code size when not optimizing. */
8890 if (TREE_CODE (offset
) == INTEGER_CST
)
8892 tree bitpos
= DECL_FIELD_BIT_OFFSET (old_variant_part
);
8893 tree first_bit
= bit_from_pos (offset
, bitpos
);
8894 TYPE_SIZE (new_union_type
)
8895 = size_binop (MINUS_EXPR
, TYPE_SIZE (record_type
), first_bit
);
8896 TYPE_SIZE_UNIT (new_union_type
)
8897 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (record_type
),
8898 byte_from_pos (offset
, bitpos
));
8899 SET_TYPE_ADA_SIZE (new_union_type
,
8900 size_binop (MINUS_EXPR
, TYPE_ADA_SIZE (record_type
),
8902 SET_TYPE_ALIGN (new_union_type
, TYPE_ALIGN (old_union_type
));
8903 relate_alias_sets (new_union_type
, old_union_type
, ALIAS_SET_COPY
);
8906 copy_and_substitute_in_size (new_union_type
, old_union_type
, subst_list
);
8908 /* Now finish up the new variants and populate the union type. */
8909 FOR_EACH_VEC_ELT_REVERSE (variant_list
, i
, v
)
8911 tree old_field
= v
->field
, new_field
;
8912 tree old_variant
, old_variant_subpart
, new_variant
, field_list
;
8914 /* Skip variants that don't belong to this nesting level. */
8915 if (DECL_CONTEXT (old_field
) != old_union_type
)
8918 /* Retrieve the list of fields already added to the new variant. */
8919 new_variant
= v
->new_type
;
8920 field_list
= TYPE_FIELDS (new_variant
);
8922 /* If the old variant had a variant subpart, we need to create a new
8923 variant subpart and add it to the field list. */
8924 old_variant
= v
->type
;
8925 old_variant_subpart
= get_variant_part (old_variant
);
8926 if (old_variant_subpart
)
8928 tree new_variant_subpart
8929 = create_variant_part_from (old_variant_subpart
, variant_list
,
8930 new_variant
, pos_list
, subst_list
);
8931 DECL_CHAIN (new_variant_subpart
) = field_list
;
8932 field_list
= new_variant_subpart
;
8935 /* Finish up the new variant and create the field. No need for debug
8936 info thanks to the XVS type. */
8937 finish_record_type (new_variant
, nreverse (field_list
), 2, false);
8938 compute_record_mode (new_variant
);
8939 create_type_decl (TYPE_NAME (new_variant
), new_variant
, true, false,
8943 = create_field_decl_from (old_field
, new_variant
, new_union_type
,
8944 TYPE_SIZE (new_variant
),
8945 pos_list
, subst_list
);
8946 DECL_QUALIFIER (new_field
) = v
->qual
;
8947 DECL_INTERNAL_P (new_field
) = 1;
8948 DECL_CHAIN (new_field
) = union_field_list
;
8949 union_field_list
= new_field
;
8952 /* Finish up the union type and create the variant part. No need for debug
8953 info thanks to the XVS type. Note that we don't reverse the field list
8954 because VARIANT_LIST has been traversed in reverse order. */
8955 finish_record_type (new_union_type
, union_field_list
, 2, false);
8956 compute_record_mode (new_union_type
);
8957 create_type_decl (TYPE_NAME (new_union_type
), new_union_type
, true, false,
8961 = create_field_decl_from (old_variant_part
, new_union_type
, record_type
,
8962 TYPE_SIZE (new_union_type
),
8963 pos_list
, subst_list
);
8964 DECL_INTERNAL_P (new_variant_part
) = 1;
8966 /* With multiple discriminants it is possible for an inner variant to be
8967 statically selected while outer ones are not; in this case, the list
8968 of fields of the inner variant is not flattened and we end up with a
8969 qualified union with a single member. Drop the useless container. */
8970 if (!DECL_CHAIN (union_field_list
))
8972 DECL_CONTEXT (union_field_list
) = record_type
;
8973 DECL_FIELD_OFFSET (union_field_list
)
8974 = DECL_FIELD_OFFSET (new_variant_part
);
8975 DECL_FIELD_BIT_OFFSET (union_field_list
)
8976 = DECL_FIELD_BIT_OFFSET (new_variant_part
);
8977 SET_DECL_OFFSET_ALIGN (union_field_list
,
8978 DECL_OFFSET_ALIGN (new_variant_part
));
8979 new_variant_part
= union_field_list
;
8982 return new_variant_part
;
8985 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8986 which are both RECORD_TYPE, after applying the substitutions described
8990 copy_and_substitute_in_size (tree new_type
, tree old_type
,
8991 vec
<subst_pair
> subst_list
)
8996 TYPE_SIZE (new_type
) = TYPE_SIZE (old_type
);
8997 TYPE_SIZE_UNIT (new_type
) = TYPE_SIZE_UNIT (old_type
);
8998 SET_TYPE_ADA_SIZE (new_type
, TYPE_ADA_SIZE (old_type
));
8999 SET_TYPE_ALIGN (new_type
, TYPE_ALIGN (old_type
));
9000 relate_alias_sets (new_type
, old_type
, ALIAS_SET_COPY
);
9002 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type
)))
9003 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
9004 TYPE_SIZE (new_type
)
9005 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type
),
9006 s
->discriminant
, s
->replacement
);
9008 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type
)))
9009 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
9010 TYPE_SIZE_UNIT (new_type
)
9011 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type
),
9012 s
->discriminant
, s
->replacement
);
9014 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type
)))
9015 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
9017 (new_type
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type
),
9018 s
->discriminant
, s
->replacement
));
9020 /* Finalize the size. */
9021 TYPE_SIZE (new_type
) = variable_size (TYPE_SIZE (new_type
));
9022 TYPE_SIZE_UNIT (new_type
) = variable_size (TYPE_SIZE_UNIT (new_type
));
9025 /* Associate to GNU_TYPE, the translation of GNAT_ENTITY, which is
9026 the implementation type of a packed array type (Is_Packed_Array_Impl_Type),
9027 the original array type if it has been translated. This association is a
9028 parallel type for GNAT encodings or a debug type for standard DWARF. Note
9029 that for standard DWARF, we also want to get the original type name. */
9032 associate_original_type_to_packed_array (tree gnu_type
, Entity_Id gnat_entity
)
9034 Entity_Id gnat_original_array_type
9035 = Underlying_Type (Original_Array_Type (gnat_entity
));
9036 tree gnu_original_array_type
;
9038 if (!present_gnu_tree (gnat_original_array_type
))
9041 gnu_original_array_type
= gnat_to_gnu_type (gnat_original_array_type
);
9043 if (TYPE_IS_DUMMY_P (gnu_original_array_type
))
9046 if (gnat_encodings
== DWARF_GNAT_ENCODINGS_MINIMAL
)
9048 tree original_name
= TYPE_NAME (gnu_original_array_type
);
9050 if (TREE_CODE (original_name
) == TYPE_DECL
)
9051 original_name
= DECL_NAME (original_name
);
9053 SET_TYPE_ORIGINAL_PACKED_ARRAY (gnu_type
, gnu_original_array_type
);
9054 TYPE_NAME (gnu_type
) = original_name
;
9057 add_parallel_type (gnu_type
, gnu_original_array_type
);
9060 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
9061 type with all size expressions that contain F in a PLACEHOLDER_EXPR
9062 updated by replacing F with R.
9064 The function doesn't update the layout of the type, i.e. it assumes
9065 that the substitution is purely formal. That's why the replacement
9066 value R must itself contain a PLACEHOLDER_EXPR. */
9069 substitute_in_type (tree t
, tree f
, tree r
)
9073 gcc_assert (CONTAINS_PLACEHOLDER_P (r
));
9075 switch (TREE_CODE (t
))
9082 /* First the domain types of arrays. */
9083 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t
))
9084 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t
)))
9086 tree low
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t
), f
, r
);
9087 tree high
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t
), f
, r
);
9089 if (low
== TYPE_GCC_MIN_VALUE (t
) && high
== TYPE_GCC_MAX_VALUE (t
))
9093 TYPE_GCC_MIN_VALUE (nt
) = low
;
9094 TYPE_GCC_MAX_VALUE (nt
) = high
;
9096 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_INDEX_TYPE (t
))
9098 (nt
, substitute_in_type (TYPE_INDEX_TYPE (t
), f
, r
));
9103 /* Then the subtypes. */
9104 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t
))
9105 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t
)))
9107 tree low
= SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t
), f
, r
);
9108 tree high
= SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t
), f
, r
);
9110 if (low
== TYPE_RM_MIN_VALUE (t
) && high
== TYPE_RM_MAX_VALUE (t
))
9114 SET_TYPE_RM_MIN_VALUE (nt
, low
);
9115 SET_TYPE_RM_MAX_VALUE (nt
, high
);
9123 nt
= substitute_in_type (TREE_TYPE (t
), f
, r
);
9124 if (nt
== TREE_TYPE (t
))
9127 return build_complex_type (nt
);
9130 /* These should never show up here. */
9135 tree component
= substitute_in_type (TREE_TYPE (t
), f
, r
);
9136 tree domain
= substitute_in_type (TYPE_DOMAIN (t
), f
, r
);
9138 if (component
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
9141 nt
= build_nonshared_array_type (component
, domain
);
9142 SET_TYPE_ALIGN (nt
, TYPE_ALIGN (t
));
9143 TYPE_USER_ALIGN (nt
) = TYPE_USER_ALIGN (t
);
9144 SET_TYPE_MODE (nt
, TYPE_MODE (t
));
9145 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
9146 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
9147 TYPE_NONALIASED_COMPONENT (nt
) = TYPE_NONALIASED_COMPONENT (t
);
9148 TYPE_MULTI_ARRAY_P (nt
) = TYPE_MULTI_ARRAY_P (t
);
9149 TYPE_CONVENTION_FORTRAN_P (nt
) = TYPE_CONVENTION_FORTRAN_P (t
);
9155 case QUAL_UNION_TYPE
:
9157 bool changed_field
= false;
9160 /* Start out with no fields, make new fields, and chain them
9161 in. If we haven't actually changed the type of any field,
9162 discard everything we've done and return the old type. */
9164 TYPE_FIELDS (nt
) = NULL_TREE
;
9166 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
9168 tree new_field
= copy_node (field
), new_n
;
9170 new_n
= substitute_in_type (TREE_TYPE (field
), f
, r
);
9171 if (new_n
!= TREE_TYPE (field
))
9173 TREE_TYPE (new_field
) = new_n
;
9174 changed_field
= true;
9177 new_n
= SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field
), f
, r
);
9178 if (new_n
!= DECL_FIELD_OFFSET (field
))
9180 DECL_FIELD_OFFSET (new_field
) = new_n
;
9181 changed_field
= true;
9184 /* Do the substitution inside the qualifier, if any. */
9185 if (TREE_CODE (t
) == QUAL_UNION_TYPE
)
9187 new_n
= SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field
), f
, r
);
9188 if (new_n
!= DECL_QUALIFIER (field
))
9190 DECL_QUALIFIER (new_field
) = new_n
;
9191 changed_field
= true;
9195 DECL_CONTEXT (new_field
) = nt
;
9196 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, field
);
9198 DECL_CHAIN (new_field
) = TYPE_FIELDS (nt
);
9199 TYPE_FIELDS (nt
) = new_field
;
9205 TYPE_FIELDS (nt
) = nreverse (TYPE_FIELDS (nt
));
9206 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
9207 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
9208 SET_TYPE_ADA_SIZE (nt
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t
), f
, r
));
9217 /* Return the RM size of GNU_TYPE. This is the actual number of bits
9218 needed to represent the object. */
9221 rm_size (tree gnu_type
)
9223 /* For integral types, we store the RM size explicitly. */
9224 if (INTEGRAL_TYPE_P (gnu_type
) && TYPE_RM_SIZE (gnu_type
))
9225 return TYPE_RM_SIZE (gnu_type
);
9227 /* Return the RM size of the actual data plus the size of the template. */
9228 if (TREE_CODE (gnu_type
) == RECORD_TYPE
9229 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
9231 size_binop (PLUS_EXPR
,
9232 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
)))),
9233 DECL_SIZE (TYPE_FIELDS (gnu_type
)));
9235 /* For record or union types, we store the size explicitly. */
9236 if (RECORD_OR_UNION_TYPE_P (gnu_type
)
9237 && !TYPE_FAT_POINTER_P (gnu_type
)
9238 && TYPE_ADA_SIZE (gnu_type
))
9239 return TYPE_ADA_SIZE (gnu_type
);
9241 /* For other types, this is just the size. */
9242 return TYPE_SIZE (gnu_type
);
9245 /* Return the name to be used for GNAT_ENTITY. If a type, create a
9246 fully-qualified name, possibly with type information encoding.
9247 Otherwise, return the name. */
9250 get_entity_char (Entity_Id gnat_entity
)
9252 Get_Encoded_Name (gnat_entity
);
9253 return ggc_strdup (Name_Buffer
);
9257 get_entity_name (Entity_Id gnat_entity
)
9259 Get_Encoded_Name (gnat_entity
);
9260 return get_identifier_with_length (Name_Buffer
, Name_Len
);
9263 /* Return an identifier representing the external name to be used for
9264 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
9265 and the specified suffix. */
9268 create_concat_name (Entity_Id gnat_entity
, const char *suffix
)
9270 const Entity_Kind kind
= Ekind (gnat_entity
);
9271 const bool has_suffix
= (suffix
!= NULL
);
9272 String_Template temp
= {1, has_suffix
? strlen (suffix
) : 0};
9273 String_Pointer sp
= {suffix
, &temp
};
9275 Get_External_Name (gnat_entity
, has_suffix
, sp
);
9277 /* A variable using the Stdcall convention lives in a DLL. We adjust
9278 its name to use the jump table, the _imp__NAME contains the address
9279 for the NAME variable. */
9280 if ((kind
== E_Variable
|| kind
== E_Constant
)
9281 && Has_Stdcall_Convention (gnat_entity
))
9283 const int len
= strlen (STDCALL_PREFIX
) + Name_Len
;
9284 char *new_name
= (char *) alloca (len
+ 1);
9285 strcpy (new_name
, STDCALL_PREFIX
);
9286 strcat (new_name
, Name_Buffer
);
9287 return get_identifier_with_length (new_name
, len
);
9290 return get_identifier_with_length (Name_Buffer
, Name_Len
);
9293 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
9294 string, return a new IDENTIFIER_NODE that is the concatenation of
9295 the name followed by "___" and the specified suffix. */
9298 concat_name (tree gnu_name
, const char *suffix
)
9300 const int len
= IDENTIFIER_LENGTH (gnu_name
) + 3 + strlen (suffix
);
9301 char *new_name
= (char *) alloca (len
+ 1);
9302 strcpy (new_name
, IDENTIFIER_POINTER (gnu_name
));
9303 strcat (new_name
, "___");
9304 strcat (new_name
, suffix
);
9305 return get_identifier_with_length (new_name
, len
);
9308 /* Initialize data structures of the decl.c module. */
9311 init_gnat_decl (void)
9313 /* Initialize the cache of annotated values. */
9314 annotate_value_cache
= hash_table
<value_annotation_hasher
>::create_ggc (512);
9316 /* Initialize the association of dummy types with subprograms. */
9317 dummy_to_subprog_map
= hash_table
<dummy_type_hasher
>::create_ggc (512);
9320 /* Destroy data structures of the decl.c module. */
9323 destroy_gnat_decl (void)
9325 /* Destroy the cache of annotated values. */
9326 annotate_value_cache
->empty ();
9327 annotate_value_cache
= NULL
;
9329 /* Destroy the association of dummy types with subprograms. */
9330 dummy_to_subprog_map
->empty ();
9331 dummy_to_subprog_map
= NULL
;
9334 #include "gt-ada-decl.h"