1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2012, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License 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"
35 #include "tree-inline.h"
53 /* "stdcall" and "thiscall" conventions should be processed in a specific way
54 on 32-bit x86/Windows only. The macros below are helpers to avoid having
55 to check for a Windows specific attribute throughout this unit. */
57 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
59 #define Has_Stdcall_Convention(E) \
60 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
61 #define Has_Thiscall_Convention(E) \
62 (!TARGET_64BIT && is_cplusplus_method (E))
64 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
65 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
68 #define Has_Stdcall_Convention(E) 0
69 #define Has_Thiscall_Convention(E) 0
72 /* Stack realignment is necessary for functions with foreign conventions when
73 the ABI doesn't mandate as much as what the compiler assumes - that is, up
74 to PREFERRED_STACK_BOUNDARY.
76 Such realignment can be requested with a dedicated function type attribute
77 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
78 characterize the situations where the attribute should be set. We rely on
79 compiler configuration settings for 'main' to decide. */
81 #ifdef MAIN_STACK_BOUNDARY
82 #define FOREIGN_FORCE_REALIGN_STACK \
83 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
85 #define FOREIGN_FORCE_REALIGN_STACK 0
90 struct incomplete
*next
;
95 /* These variables are used to defer recursively expanding incomplete types
96 while we are processing an array, a record or a subprogram type. */
97 static int defer_incomplete_level
= 0;
98 static struct incomplete
*defer_incomplete_list
;
100 /* This variable is used to delay expanding From_With_Type types until the
102 static struct incomplete
*defer_limited_with
;
104 typedef struct subst_pair_d
{
109 DEF_VEC_O(subst_pair
);
110 DEF_VEC_ALLOC_O(subst_pair
,heap
);
112 typedef struct variant_desc_d
{
113 /* The type of the variant. */
116 /* The associated field. */
119 /* The value of the qualifier. */
122 /* The type of the variant after transformation. */
126 DEF_VEC_O(variant_desc
);
127 DEF_VEC_ALLOC_O(variant_desc
,heap
);
129 /* A hash table used to cache the result of annotate_value. */
130 static GTY ((if_marked ("tree_int_map_marked_p"),
131 param_is (struct tree_int_map
))) htab_t annotate_value_cache
;
133 static bool allocatable_size_p (tree
, bool);
134 static void prepend_one_attribute_to (struct attrib
**,
135 enum attr_type
, tree
, tree
, Node_Id
);
136 static void prepend_attributes (Entity_Id
, struct attrib
**);
137 static tree
elaborate_expression (Node_Id
, Entity_Id
, tree
, bool, bool, bool);
138 static bool type_has_variable_size (tree
);
139 static tree
elaborate_expression_1 (tree
, Entity_Id
, tree
, bool, bool);
140 static tree
elaborate_expression_2 (tree
, Entity_Id
, tree
, bool, bool,
142 static tree
gnat_to_gnu_component_type (Entity_Id
, bool, bool);
143 static tree
gnat_to_gnu_param (Entity_Id
, Mechanism_Type
, Entity_Id
, bool,
145 static tree
gnat_to_gnu_field (Entity_Id
, tree
, int, bool, bool);
146 static bool same_discriminant_p (Entity_Id
, Entity_Id
);
147 static bool array_type_has_nonaliased_component (tree
, Entity_Id
);
148 static bool compile_time_known_address_p (Node_Id
);
149 static bool cannot_be_superflat_p (Node_Id
);
150 static bool constructor_address_p (tree
);
151 static void components_to_record (tree
, Node_Id
, tree
, int, bool, bool, bool,
152 bool, bool, bool, bool, bool, tree
, tree
*);
153 static Uint
annotate_value (tree
);
154 static void annotate_rep (Entity_Id
, tree
);
155 static tree
build_position_list (tree
, bool, tree
, tree
, unsigned int, tree
);
156 static VEC(subst_pair
,heap
) *build_subst_list (Entity_Id
, Entity_Id
, bool);
157 static VEC(variant_desc
,heap
) *build_variant_list (tree
,
158 VEC(subst_pair
,heap
) *,
159 VEC(variant_desc
,heap
) *);
160 static tree
validate_size (Uint
, tree
, Entity_Id
, enum tree_code
, bool, bool);
161 static void set_rm_size (Uint
, tree
, Entity_Id
);
162 static unsigned int validate_alignment (Uint
, Entity_Id
, unsigned int);
163 static void check_ok_for_atomic (tree
, Entity_Id
, bool);
164 static tree
create_field_decl_from (tree
, tree
, tree
, tree
, tree
,
165 VEC(subst_pair
,heap
) *);
166 static tree
create_rep_part (tree
, tree
, tree
);
167 static tree
get_rep_part (tree
);
168 static tree
create_variant_part_from (tree
, VEC(variant_desc
,heap
) *, tree
,
169 tree
, VEC(subst_pair
,heap
) *);
170 static void copy_and_substitute_in_size (tree
, tree
, VEC(subst_pair
,heap
) *);
172 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
173 to pass around calls performing profile compatibility checks. */
176 Entity_Id gnat_entity
; /* The Ada subprogram entity. */
177 tree ada_fntype
; /* The corresponding GCC type node. */
178 tree btin_fntype
; /* The GCC builtin function type node. */
181 static bool intrin_profiles_compatible_p (intrin_binding_t
*);
183 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
184 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
185 and associate the ..._DECL node with the input GNAT defining identifier.
187 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
188 initial value (in GCC tree form). This is optional for a variable. For
189 a renamed entity, GNU_EXPR gives the object being renamed.
191 DEFINITION is nonzero if this call is intended for a definition. This is
192 used for separate compilation where it is necessary to know whether an
193 external declaration or a definition must be created if the GCC equivalent
194 was not created previously. The value of 1 is normally used for a nonzero
195 DEFINITION, but a value of 2 is used in special circumstances, defined in
199 gnat_to_gnu_entity (Entity_Id gnat_entity
, tree gnu_expr
, int definition
)
201 /* Contains the kind of the input GNAT node. */
202 const Entity_Kind kind
= Ekind (gnat_entity
);
203 /* True if this is a type. */
204 const bool is_type
= IN (kind
, Type_Kind
);
205 /* True if debug info is requested for this entity. */
206 const bool debug_info_p
= Needs_Debug_Info (gnat_entity
);
207 /* True if this entity is to be considered as imported. */
208 const bool imported_p
209 = (Is_Imported (gnat_entity
) && No (Address_Clause (gnat_entity
)));
210 /* For a type, contains the equivalent GNAT node to be used in gigi. */
211 Entity_Id gnat_equiv_type
= Empty
;
212 /* Temporary used to walk the GNAT tree. */
214 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
215 This node will be associated with the GNAT node by calling at the end
216 of the `switch' statement. */
217 tree gnu_decl
= NULL_TREE
;
218 /* Contains the GCC type to be used for the GCC node. */
219 tree gnu_type
= NULL_TREE
;
220 /* Contains the GCC size tree to be used for the GCC node. */
221 tree gnu_size
= NULL_TREE
;
222 /* Contains the GCC name to be used for the GCC node. */
223 tree gnu_entity_name
;
224 /* True if we have already saved gnu_decl as a GNAT association. */
226 /* True if we incremented defer_incomplete_level. */
227 bool this_deferred
= false;
228 /* True if we incremented force_global. */
229 bool this_global
= false;
230 /* True if we should check to see if elaborated during processing. */
231 bool maybe_present
= false;
232 /* True if we made GNU_DECL and its type here. */
233 bool this_made_decl
= false;
234 /* Size and alignment of the GCC node, if meaningful. */
235 unsigned int esize
= 0, align
= 0;
236 /* Contains the list of attributes directly attached to the entity. */
237 struct attrib
*attr_list
= NULL
;
239 /* Since a use of an Itype is a definition, process it as such if it
240 is not in a with'ed unit. */
243 && Is_Itype (gnat_entity
)
244 && !present_gnu_tree (gnat_entity
)
245 && In_Extended_Main_Code_Unit (gnat_entity
))
247 /* Ensure that we are in a subprogram mentioned in the Scope chain of
248 this entity, our current scope is global, or we encountered a task
249 or entry (where we can't currently accurately check scoping). */
250 if (!current_function_decl
251 || DECL_ELABORATION_PROC_P (current_function_decl
))
253 process_type (gnat_entity
);
254 return get_gnu_tree (gnat_entity
);
257 for (gnat_temp
= Scope (gnat_entity
);
259 gnat_temp
= Scope (gnat_temp
))
261 if (Is_Type (gnat_temp
))
262 gnat_temp
= Underlying_Type (gnat_temp
);
264 if (Ekind (gnat_temp
) == E_Subprogram_Body
)
266 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp
)));
268 if (IN (Ekind (gnat_temp
), Subprogram_Kind
)
269 && Present (Protected_Body_Subprogram (gnat_temp
)))
270 gnat_temp
= Protected_Body_Subprogram (gnat_temp
);
272 if (Ekind (gnat_temp
) == E_Entry
273 || Ekind (gnat_temp
) == E_Entry_Family
274 || Ekind (gnat_temp
) == E_Task_Type
275 || (IN (Ekind (gnat_temp
), Subprogram_Kind
)
276 && present_gnu_tree (gnat_temp
)
277 && (current_function_decl
278 == gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, 0))))
280 process_type (gnat_entity
);
281 return get_gnu_tree (gnat_entity
);
285 /* This abort means the Itype has an incorrect scope, i.e. that its
286 scope does not correspond to the subprogram it is declared in. */
290 /* If we've already processed this entity, return what we got last time.
291 If we are defining the node, we should not have already processed it.
292 In that case, we will abort below when we try to save a new GCC tree
293 for this object. We also need to handle the case of getting a dummy
294 type when a Full_View exists. */
295 if ((!definition
|| (is_type
&& imported_p
))
296 && present_gnu_tree (gnat_entity
))
298 gnu_decl
= get_gnu_tree (gnat_entity
);
300 if (TREE_CODE (gnu_decl
) == TYPE_DECL
301 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
))
302 && IN (kind
, Incomplete_Or_Private_Kind
)
303 && Present (Full_View (gnat_entity
)))
306 = gnat_to_gnu_entity (Full_View (gnat_entity
), NULL_TREE
, 0);
307 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
308 save_gnu_tree (gnat_entity
, gnu_decl
, false);
314 /* If this is a numeric or enumeral type, or an access type, a nonzero
315 Esize must be specified unless it was specified by the programmer. */
316 gcc_assert (!Unknown_Esize (gnat_entity
)
317 || Has_Size_Clause (gnat_entity
)
318 || (!IN (kind
, Numeric_Kind
)
319 && !IN (kind
, Enumeration_Kind
)
320 && (!IN (kind
, Access_Kind
)
321 || kind
== E_Access_Protected_Subprogram_Type
322 || kind
== E_Anonymous_Access_Protected_Subprogram_Type
323 || kind
== E_Access_Subtype
)));
325 /* The RM size must be specified for all discrete and fixed-point types. */
326 gcc_assert (!(IN (kind
, Discrete_Or_Fixed_Point_Kind
)
327 && Unknown_RM_Size (gnat_entity
)));
329 /* If we get here, it means we have not yet done anything with this entity.
330 If we are not defining it, it must be a type or an entity that is defined
331 elsewhere or externally, otherwise we should have defined it already. */
332 gcc_assert (definition
333 || type_annotate_only
335 || kind
== E_Discriminant
336 || kind
== E_Component
338 || (kind
== E_Constant
&& Present (Full_View (gnat_entity
)))
339 || Is_Public (gnat_entity
));
341 /* Get the name of the entity and set up the line number and filename of
342 the original definition for use in any decl we make. */
343 gnu_entity_name
= get_entity_name (gnat_entity
);
344 Sloc_to_locus (Sloc (gnat_entity
), &input_location
);
346 /* For cases when we are not defining (i.e., we are referencing from
347 another compilation unit) public entities, show we are at global level
348 for the purpose of computing scopes. Don't do this for components or
349 discriminants since the relevant test is whether or not the record is
352 && kind
!= E_Component
353 && kind
!= E_Discriminant
354 && Is_Public (gnat_entity
)
355 && !Is_Statically_Allocated (gnat_entity
))
356 force_global
++, this_global
= true;
358 /* Handle any attributes directly attached to the entity. */
359 if (Has_Gigi_Rep_Item (gnat_entity
))
360 prepend_attributes (gnat_entity
, &attr_list
);
362 /* Do some common processing for types. */
365 /* Compute the equivalent type to be used in gigi. */
366 gnat_equiv_type
= Gigi_Equivalent_Type (gnat_entity
);
368 /* Machine_Attributes on types are expected to be propagated to
369 subtypes. The corresponding Gigi_Rep_Items are only attached
370 to the first subtype though, so we handle the propagation here. */
371 if (Base_Type (gnat_entity
) != gnat_entity
372 && !Is_First_Subtype (gnat_entity
)
373 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity
))))
374 prepend_attributes (First_Subtype (Base_Type (gnat_entity
)),
377 /* Compute a default value for the size of an elementary type. */
378 if (Known_Esize (gnat_entity
) && Is_Elementary_Type (gnat_entity
))
380 unsigned int max_esize
;
382 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity
)));
383 esize
= UI_To_Int (Esize (gnat_entity
));
385 if (IN (kind
, Float_Kind
))
386 max_esize
= fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE
);
387 else if (IN (kind
, Access_Kind
))
388 max_esize
= POINTER_SIZE
* 2;
390 max_esize
= LONG_LONG_TYPE_SIZE
;
392 if (esize
> max_esize
)
400 /* If this is a use of a deferred constant without address clause,
401 get its full definition. */
403 && No (Address_Clause (gnat_entity
))
404 && Present (Full_View (gnat_entity
)))
407 = gnat_to_gnu_entity (Full_View (gnat_entity
), gnu_expr
, 0);
412 /* If we have an external constant that we are not defining, get the
413 expression that is was defined to represent. We may throw it away
414 later if it is not a constant. But do not retrieve the expression
415 if it is an allocator because the designated type might be dummy
418 && !No_Initialization (Declaration_Node (gnat_entity
))
419 && Present (Expression (Declaration_Node (gnat_entity
)))
420 && Nkind (Expression (Declaration_Node (gnat_entity
)))
423 bool went_into_elab_proc
= false;
424 int save_force_global
= force_global
;
426 /* The expression may contain N_Expression_With_Actions nodes and
427 thus object declarations from other units. In this case, even
428 though the expression will eventually be discarded since not a
429 constant, the declarations would be stuck either in the global
430 varpool or in the current scope. Therefore we force the local
431 context and create a fake scope that we'll zap at the end. */
432 if (!current_function_decl
)
434 current_function_decl
= get_elaboration_procedure ();
435 went_into_elab_proc
= true;
440 gnu_expr
= gnat_to_gnu (Expression (Declaration_Node (gnat_entity
)));
443 force_global
= save_force_global
;
444 if (went_into_elab_proc
)
445 current_function_decl
= NULL_TREE
;
448 /* Ignore deferred constant definitions without address clause since
449 they are processed fully in the front-end. If No_Initialization
450 is set, this is not a deferred constant but a constant whose value
451 is built manually. And constants that are renamings are handled
455 && No (Address_Clause (gnat_entity
))
456 && !No_Initialization (Declaration_Node (gnat_entity
))
457 && No (Renamed_Object (gnat_entity
)))
459 gnu_decl
= error_mark_node
;
464 /* Ignore constant definitions already marked with the error node. See
465 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
468 && present_gnu_tree (gnat_entity
)
469 && get_gnu_tree (gnat_entity
) == error_mark_node
)
471 maybe_present
= true;
478 /* We used to special case VMS exceptions here to directly map them to
479 their associated condition code. Since this code had to be masked
480 dynamically to strip off the severity bits, this caused trouble in
481 the GCC/ZCX case because the "type" pointers we store in the tables
482 have to be static. We now don't special case here anymore, and let
483 the regular processing take place, which leaves us with a regular
484 exception data object for VMS exceptions too. The condition code
485 mapping is taken care of by the front end and the bitmasking by the
492 /* The GNAT record where the component was defined. */
493 Entity_Id gnat_record
= Underlying_Type (Scope (gnat_entity
));
495 /* If the variable is an inherited record component (in the case of
496 extended record types), just return the inherited entity, which
497 must be a FIELD_DECL. Likewise for discriminants.
498 For discriminants of untagged records which have explicit
499 stored discriminants, return the entity for the corresponding
500 stored discriminant. Also use Original_Record_Component
501 if the record has a private extension. */
502 if (Present (Original_Record_Component (gnat_entity
))
503 && Original_Record_Component (gnat_entity
) != gnat_entity
)
506 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity
),
507 gnu_expr
, definition
);
512 /* If the enclosing record has explicit stored discriminants,
513 then it is an untagged record. If the Corresponding_Discriminant
514 is not empty then this must be a renamed discriminant and its
515 Original_Record_Component must point to the corresponding explicit
516 stored discriminant (i.e. we should have taken the previous
518 else if (Present (Corresponding_Discriminant (gnat_entity
))
519 && Is_Tagged_Type (gnat_record
))
521 /* A tagged record has no explicit stored discriminants. */
522 gcc_assert (First_Discriminant (gnat_record
)
523 == First_Stored_Discriminant (gnat_record
));
525 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity
),
526 gnu_expr
, definition
);
531 else if (Present (CR_Discriminant (gnat_entity
))
532 && type_annotate_only
)
534 gnu_decl
= gnat_to_gnu_entity (CR_Discriminant (gnat_entity
),
535 gnu_expr
, definition
);
540 /* If the enclosing record has explicit stored discriminants, then
541 it is an untagged record. If the Corresponding_Discriminant
542 is not empty then this must be a renamed discriminant and its
543 Original_Record_Component must point to the corresponding explicit
544 stored discriminant (i.e. we should have taken the first
546 else if (Present (Corresponding_Discriminant (gnat_entity
))
547 && (First_Discriminant (gnat_record
)
548 != First_Stored_Discriminant (gnat_record
)))
551 /* Otherwise, if we are not defining this and we have no GCC type
552 for the containing record, make one for it. Then we should
553 have made our own equivalent. */
554 else if (!definition
&& !present_gnu_tree (gnat_record
))
556 /* ??? If this is in a record whose scope is a protected
557 type and we have an Original_Record_Component, use it.
558 This is a workaround for major problems in protected type
560 Entity_Id Scop
= Scope (Scope (gnat_entity
));
561 if ((Is_Protected_Type (Scop
)
562 || (Is_Private_Type (Scop
)
563 && Present (Full_View (Scop
))
564 && Is_Protected_Type (Full_View (Scop
))))
565 && Present (Original_Record_Component (gnat_entity
)))
568 = gnat_to_gnu_entity (Original_Record_Component
575 gnat_to_gnu_entity (Scope (gnat_entity
), NULL_TREE
, 0);
576 gnu_decl
= get_gnu_tree (gnat_entity
);
582 /* Here we have no GCC type and this is a reference rather than a
583 definition. This should never happen. Most likely the cause is
584 reference before declaration in the gnat tree for gnat_entity. */
588 case E_Loop_Parameter
:
589 case E_Out_Parameter
:
592 /* Simple variables, loop variables, Out parameters and exceptions. */
596 = ((kind
== E_Constant
|| kind
== E_Variable
)
597 && Is_True_Constant (gnat_entity
)
598 && !Treat_As_Volatile (gnat_entity
)
599 && (((Nkind (Declaration_Node (gnat_entity
))
600 == N_Object_Declaration
)
601 && Present (Expression (Declaration_Node (gnat_entity
))))
602 || Present (Renamed_Object (gnat_entity
))
604 bool inner_const_flag
= const_flag
;
605 bool static_p
= Is_Statically_Allocated (gnat_entity
);
606 bool mutable_p
= false;
607 bool used_by_ref
= false;
608 tree gnu_ext_name
= NULL_TREE
;
609 tree renamed_obj
= NULL_TREE
;
610 tree gnu_object_size
;
612 if (Present (Renamed_Object (gnat_entity
)) && !definition
)
614 if (kind
== E_Exception
)
615 gnu_expr
= gnat_to_gnu_entity (Renamed_Entity (gnat_entity
),
618 gnu_expr
= gnat_to_gnu (Renamed_Object (gnat_entity
));
621 /* Get the type after elaborating the renamed object. */
622 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
624 /* If this is a standard exception definition, then use the standard
625 exception type. This is necessary to make sure that imported and
626 exported views of exceptions are properly merged in LTO mode. */
627 if (TREE_CODE (TYPE_NAME (gnu_type
)) == TYPE_DECL
628 && DECL_NAME (TYPE_NAME (gnu_type
)) == exception_data_name_id
)
629 gnu_type
= except_type_node
;
631 /* For a debug renaming declaration, build a debug-only entity. */
632 if (Present (Debug_Renaming_Link (gnat_entity
)))
634 /* Force a non-null value to make sure the symbol is retained. */
635 tree value
= build1 (INDIRECT_REF
, gnu_type
,
637 build_pointer_type (gnu_type
),
638 integer_minus_one_node
));
639 gnu_decl
= build_decl (input_location
,
640 VAR_DECL
, gnu_entity_name
, gnu_type
);
641 SET_DECL_VALUE_EXPR (gnu_decl
, value
);
642 DECL_HAS_VALUE_EXPR_P (gnu_decl
) = 1;
643 gnat_pushdecl (gnu_decl
, gnat_entity
);
647 /* If this is a loop variable, its type should be the base type.
648 This is because the code for processing a loop determines whether
649 a normal loop end test can be done by comparing the bounds of the
650 loop against those of the base type, which is presumed to be the
651 size used for computation. But this is not correct when the size
652 of the subtype is smaller than the type. */
653 if (kind
== E_Loop_Parameter
)
654 gnu_type
= get_base_type (gnu_type
);
656 /* Reject non-renamed objects whose type is an unconstrained array or
657 any object whose type is a dummy type or void. */
658 if ((TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
659 && No (Renamed_Object (gnat_entity
)))
660 || TYPE_IS_DUMMY_P (gnu_type
)
661 || TREE_CODE (gnu_type
) == VOID_TYPE
)
663 gcc_assert (type_annotate_only
);
666 return error_mark_node
;
669 /* If an alignment is specified, use it if valid. Note that exceptions
670 are objects but don't have an alignment. We must do this before we
671 validate the size, since the alignment can affect the size. */
672 if (kind
!= E_Exception
&& Known_Alignment (gnat_entity
))
674 gcc_assert (Present (Alignment (gnat_entity
)));
676 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
677 TYPE_ALIGN (gnu_type
));
679 /* No point in changing the type if there is an address clause
680 as the final type of the object will be a reference type. */
681 if (Present (Address_Clause (gnat_entity
)))
685 tree orig_type
= gnu_type
;
688 = maybe_pad_type (gnu_type
, NULL_TREE
, align
, gnat_entity
,
689 false, false, definition
, true);
691 /* If a padding record was made, declare it now since it will
692 never be declared otherwise. This is necessary to ensure
693 that its subtrees are properly marked. */
694 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
695 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, NULL
, true,
696 debug_info_p
, gnat_entity
);
700 /* If we are defining the object, see if it has a Size and validate it
701 if so. If we are not defining the object and a Size clause applies,
702 simply retrieve the value. We don't want to ignore the clause and
703 it is expected to have been validated already. Then get the new
706 gnu_size
= validate_size (Esize (gnat_entity
), gnu_type
,
707 gnat_entity
, VAR_DECL
, false,
708 Has_Size_Clause (gnat_entity
));
709 else if (Has_Size_Clause (gnat_entity
))
710 gnu_size
= UI_To_gnu (Esize (gnat_entity
), bitsizetype
);
715 = make_type_from_size (gnu_type
, gnu_size
,
716 Has_Biased_Representation (gnat_entity
));
718 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0))
719 gnu_size
= NULL_TREE
;
722 /* If this object has self-referential size, it must be a record with
723 a default discriminant. We are supposed to allocate an object of
724 the maximum size in this case, unless it is a constant with an
725 initializing expression, in which case we can get the size from
726 that. Note that the resulting size may still be a variable, so
727 this may end up with an indirect allocation. */
728 if (No (Renamed_Object (gnat_entity
))
729 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
731 if (gnu_expr
&& kind
== E_Constant
)
733 tree size
= TYPE_SIZE (TREE_TYPE (gnu_expr
));
734 if (CONTAINS_PLACEHOLDER_P (size
))
736 /* If the initializing expression is itself a constant,
737 despite having a nominal type with self-referential
738 size, we can get the size directly from it. */
739 if (TREE_CODE (gnu_expr
) == COMPONENT_REF
741 (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0)))
742 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == VAR_DECL
743 && (TREE_READONLY (TREE_OPERAND (gnu_expr
, 0))
744 || DECL_READONLY_ONCE_ELAB
745 (TREE_OPERAND (gnu_expr
, 0))))
746 gnu_size
= DECL_SIZE (TREE_OPERAND (gnu_expr
, 0));
749 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, gnu_expr
);
754 /* We may have no GNU_EXPR because No_Initialization is
755 set even though there's an Expression. */
756 else if (kind
== E_Constant
757 && (Nkind (Declaration_Node (gnat_entity
))
758 == N_Object_Declaration
)
759 && Present (Expression (Declaration_Node (gnat_entity
))))
761 = TYPE_SIZE (gnat_to_gnu_type
763 (Expression (Declaration_Node (gnat_entity
)))));
766 gnu_size
= max_size (TYPE_SIZE (gnu_type
), true);
770 /* If we are at global level and the size isn't constant, call
771 elaborate_expression_1 to make a variable for it rather than
772 calculating it each time. */
773 if (global_bindings_p () && !TREE_CONSTANT (gnu_size
))
774 gnu_size
= elaborate_expression_1 (gnu_size
, gnat_entity
,
775 get_identifier ("SIZE"),
779 /* If the size is zero byte, make it one byte since some linkers have
780 troubles with zero-sized objects. If the object will have a
781 template, that will make it nonzero so don't bother. Also avoid
782 doing that for an object renaming or an object with an address
783 clause, as we would lose useful information on the view size
784 (e.g. for null array slices) and we are not allocating the object
787 && integer_zerop (gnu_size
)
788 && !TREE_OVERFLOW (gnu_size
))
789 || (TYPE_SIZE (gnu_type
)
790 && integer_zerop (TYPE_SIZE (gnu_type
))
791 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type
))))
792 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
793 || !Is_Array_Type (Etype (gnat_entity
)))
794 && No (Renamed_Object (gnat_entity
))
795 && No (Address_Clause (gnat_entity
)))
796 gnu_size
= bitsize_unit_node
;
798 /* If this is an object with no specified size and alignment, and
799 if either it is atomic or we are not optimizing alignment for
800 space and it is composite and not an exception, an Out parameter
801 or a reference to another object, and the size of its type is a
802 constant, set the alignment to the smallest one which is not
803 smaller than the size, with an appropriate cap. */
804 if (!gnu_size
&& align
== 0
805 && (Is_Atomic (gnat_entity
)
806 || (!Optimize_Alignment_Space (gnat_entity
)
807 && kind
!= E_Exception
808 && kind
!= E_Out_Parameter
809 && Is_Composite_Type (Etype (gnat_entity
))
810 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
811 && !Is_Exported (gnat_entity
)
813 && No (Renamed_Object (gnat_entity
))
814 && No (Address_Clause (gnat_entity
))))
815 && TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
)
817 unsigned int size_cap
, align_cap
;
819 /* No point in promoting the alignment if this doesn't prevent
820 BLKmode access to the object, in particular block copy, as
821 this will for example disable the NRV optimization for it.
822 No point in jumping through all the hoops needed in order
823 to support BIGGEST_ALIGNMENT if we don't really have to.
824 So we cap to the smallest alignment that corresponds to
825 a known efficient memory access pattern of the target. */
826 if (Is_Atomic (gnat_entity
))
829 align_cap
= BIGGEST_ALIGNMENT
;
833 size_cap
= MAX_FIXED_MODE_SIZE
;
834 align_cap
= get_mode_alignment (ptr_mode
);
837 if (!host_integerp (TYPE_SIZE (gnu_type
), 1)
838 || compare_tree_int (TYPE_SIZE (gnu_type
), size_cap
) > 0)
840 else if (compare_tree_int (TYPE_SIZE (gnu_type
), align_cap
) > 0)
843 align
= ceil_pow2 (tree_low_cst (TYPE_SIZE (gnu_type
), 1));
845 /* But make sure not to under-align the object. */
846 if (align
<= TYPE_ALIGN (gnu_type
))
849 /* And honor the minimum valid atomic alignment, if any. */
850 #ifdef MINIMUM_ATOMIC_ALIGNMENT
851 else if (align
< MINIMUM_ATOMIC_ALIGNMENT
)
852 align
= MINIMUM_ATOMIC_ALIGNMENT
;
856 /* If the object is set to have atomic components, find the component
857 type and validate it.
859 ??? Note that we ignore Has_Volatile_Components on objects; it's
860 not at all clear what to do in that case. */
861 if (Has_Atomic_Components (gnat_entity
))
863 tree gnu_inner
= (TREE_CODE (gnu_type
) == ARRAY_TYPE
864 ? TREE_TYPE (gnu_type
) : gnu_type
);
866 while (TREE_CODE (gnu_inner
) == ARRAY_TYPE
867 && TYPE_MULTI_ARRAY_P (gnu_inner
))
868 gnu_inner
= TREE_TYPE (gnu_inner
);
870 check_ok_for_atomic (gnu_inner
, gnat_entity
, true);
873 /* Now check if the type of the object allows atomic access. Note
874 that we must test the type, even if this object has size and
875 alignment to allow such access, because we will be going inside
876 the padded record to assign to the object. We could fix this by
877 always copying via an intermediate value, but it's not clear it's
879 if (Is_Atomic (gnat_entity
))
880 check_ok_for_atomic (gnu_type
, gnat_entity
, false);
882 /* If this is an aliased object with an unconstrained nominal subtype,
883 make a type that includes the template. */
884 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
885 && Is_Array_Type (Etype (gnat_entity
))
886 && !type_annotate_only
)
889 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
891 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array
),
893 concat_name (gnu_entity_name
,
898 /* ??? If this is an object of CW type initialized to a value, try to
899 ensure that the object is sufficient aligned for this value, but
900 without pessimizing the allocation. This is a kludge necessary
901 because we don't support dynamic alignment. */
903 && Ekind (Etype (gnat_entity
)) == E_Class_Wide_Subtype
904 && No (Renamed_Object (gnat_entity
))
905 && No (Address_Clause (gnat_entity
)))
906 align
= get_target_system_allocator_alignment () * BITS_PER_UNIT
;
908 #ifdef MINIMUM_ATOMIC_ALIGNMENT
909 /* If the size is a constant and no alignment is specified, force
910 the alignment to be the minimum valid atomic alignment. The
911 restriction on constant size avoids problems with variable-size
912 temporaries; if the size is variable, there's no issue with
913 atomic access. Also don't do this for a constant, since it isn't
914 necessary and can interfere with constant replacement. Finally,
915 do not do it for Out parameters since that creates an
916 size inconsistency with In parameters. */
918 && MINIMUM_ATOMIC_ALIGNMENT
> TYPE_ALIGN (gnu_type
)
919 && !FLOAT_TYPE_P (gnu_type
)
920 && !const_flag
&& No (Renamed_Object (gnat_entity
))
921 && !imported_p
&& No (Address_Clause (gnat_entity
))
922 && kind
!= E_Out_Parameter
923 && (gnu_size
? TREE_CODE (gnu_size
) == INTEGER_CST
924 : TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
))
925 align
= MINIMUM_ATOMIC_ALIGNMENT
;
928 /* Make a new type with the desired size and alignment, if needed.
929 But do not take into account alignment promotions to compute the
930 size of the object. */
931 gnu_object_size
= gnu_size
? gnu_size
: TYPE_SIZE (gnu_type
);
932 if (gnu_size
|| align
> 0)
934 tree orig_type
= gnu_type
;
936 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
937 false, false, definition
, true);
939 /* If a padding record was made, declare it now since it will
940 never be declared otherwise. This is necessary to ensure
941 that its subtrees are properly marked. */
942 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
943 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, NULL
, true,
944 debug_info_p
, gnat_entity
);
947 /* If this is a renaming, avoid as much as possible to create a new
948 object. However, in several cases, creating it is required.
949 This processing needs to be applied to the raw expression so
950 as to make it more likely to rename the underlying object. */
951 if (Present (Renamed_Object (gnat_entity
)))
953 bool create_normal_object
= false;
955 /* If the renamed object had padding, strip off the reference
956 to the inner object and reset our type. */
957 if ((TREE_CODE (gnu_expr
) == COMPONENT_REF
958 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0))))
959 /* Strip useless conversions around the object. */
960 || gnat_useless_type_conversion (gnu_expr
))
962 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
963 gnu_type
= TREE_TYPE (gnu_expr
);
966 /* Or else, if the renamed object has an unconstrained type with
967 default discriminant, use the padded type. */
968 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr
))
969 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr
)))
971 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
972 gnu_type
= TREE_TYPE (gnu_expr
);
974 /* Case 1: If this is a constant renaming stemming from a function
975 call, treat it as a normal object whose initial value is what
976 is being renamed. RM 3.3 says that the result of evaluating a
977 function call is a constant object. As a consequence, it can
978 be the inner object of a constant renaming. In this case, the
979 renaming must be fully instantiated, i.e. it cannot be a mere
980 reference to (part of) an existing object. */
983 tree inner_object
= gnu_expr
;
984 while (handled_component_p (inner_object
))
985 inner_object
= TREE_OPERAND (inner_object
, 0);
986 if (TREE_CODE (inner_object
) == CALL_EXPR
)
987 create_normal_object
= true;
990 /* Otherwise, see if we can proceed with a stabilized version of
991 the renamed entity or if we need to make a new object. */
992 if (!create_normal_object
)
994 tree maybe_stable_expr
= NULL_TREE
;
997 /* Case 2: If the renaming entity need not be materialized and
998 the renamed expression is something we can stabilize, use
999 that for the renaming. At the global level, we can only do
1000 this if we know no SAVE_EXPRs need be made, because the
1001 expression we return might be used in arbitrary conditional
1002 branches so we must force the evaluation of the SAVE_EXPRs
1003 immediately and this requires a proper function context.
1004 Note that an external constant is at the global level. */
1005 if (!Materialize_Entity (gnat_entity
)
1006 && (!((!definition
&& kind
== E_Constant
)
1007 || global_bindings_p ())
1008 || (staticp (gnu_expr
)
1009 && !TREE_SIDE_EFFECTS (gnu_expr
))))
1012 = gnat_stabilize_reference (gnu_expr
, true, &stable
);
1016 /* ??? No DECL_EXPR is created so we need to mark
1017 the expression manually lest it is shared. */
1018 if ((!definition
&& kind
== E_Constant
)
1019 || global_bindings_p ())
1020 MARK_VISITED (maybe_stable_expr
);
1021 gnu_decl
= maybe_stable_expr
;
1022 save_gnu_tree (gnat_entity
, gnu_decl
, true);
1024 annotate_object (gnat_entity
, gnu_type
, NULL_TREE
,
1026 /* This assertion will fail if the renamed object
1027 isn't aligned enough as to make it possible to
1028 honor the alignment set on the renaming. */
1031 unsigned int renamed_align
1033 ? DECL_ALIGN (gnu_decl
)
1034 : TYPE_ALIGN (TREE_TYPE (gnu_decl
));
1035 gcc_assert (renamed_align
>= align
);
1040 /* The stabilization failed. Keep maybe_stable_expr
1041 untouched here to let the pointer case below know
1042 about that failure. */
1045 /* Case 3: If this is a constant renaming and creating a
1046 new object is allowed and cheap, treat it as a normal
1047 object whose initial value is what is being renamed. */
1049 && !Is_Composite_Type
1050 (Underlying_Type (Etype (gnat_entity
))))
1053 /* Case 4: Make this into a constant pointer to the object we
1054 are to rename and attach the object to the pointer if it is
1055 something we can stabilize.
1057 From the proper scope, attached objects will be referenced
1058 directly instead of indirectly via the pointer to avoid
1059 subtle aliasing problems with non-addressable entities.
1060 They have to be stable because we must not evaluate the
1061 variables in the expression every time the renaming is used.
1062 The pointer is called a "renaming" pointer in this case.
1064 In the rare cases where we cannot stabilize the renamed
1065 object, we just make a "bare" pointer, and the renamed
1066 entity is always accessed indirectly through it. */
1069 /* We need to preserve the volatileness of the renamed
1070 object through the indirection. */
1071 if (TREE_THIS_VOLATILE (gnu_expr
)
1072 && !TYPE_VOLATILE (gnu_type
))
1074 = build_qualified_type (gnu_type
,
1075 (TYPE_QUALS (gnu_type
)
1076 | TYPE_QUAL_VOLATILE
));
1077 gnu_type
= build_reference_type (gnu_type
);
1078 inner_const_flag
= TREE_READONLY (gnu_expr
);
1081 /* If the previous attempt at stabilizing failed, there
1082 is no point in trying again and we reuse the result
1083 without attaching it to the pointer. In this case it
1084 will only be used as the initializing expression of
1085 the pointer and thus needs no special treatment with
1086 regard to multiple evaluations. */
1087 if (maybe_stable_expr
)
1090 /* Otherwise, try to stabilize and attach the expression
1091 to the pointer if the stabilization succeeds.
1093 Note that this might introduce SAVE_EXPRs and we don't
1094 check whether we're at the global level or not. This
1095 is fine since we are building a pointer initializer and
1096 neither the pointer nor the initializing expression can
1097 be accessed before the pointer elaboration has taken
1098 place in a correct program.
1100 These SAVE_EXPRs will be evaluated at the right place
1101 by either the evaluation of the initializer for the
1102 non-global case or the elaboration code for the global
1103 case, and will be attached to the elaboration procedure
1104 in the latter case. */
1108 = gnat_stabilize_reference (gnu_expr
, true, &stable
);
1111 renamed_obj
= maybe_stable_expr
;
1113 /* Attaching is actually performed downstream, as soon
1114 as we have a VAR_DECL for the pointer we make. */
1117 gnu_expr
= build_unary_op (ADDR_EXPR
, gnu_type
,
1120 gnu_size
= NULL_TREE
;
1126 /* Make a volatile version of this object's type if we are to make
1127 the object volatile. We also interpret 13.3(19) conservatively
1128 and disallow any optimizations for such a non-constant object. */
1129 if ((Treat_As_Volatile (gnat_entity
)
1131 && gnu_type
!= except_type_node
1132 && (Is_Exported (gnat_entity
)
1134 || Present (Address_Clause (gnat_entity
)))))
1135 && !TYPE_VOLATILE (gnu_type
))
1136 gnu_type
= build_qualified_type (gnu_type
,
1137 (TYPE_QUALS (gnu_type
)
1138 | TYPE_QUAL_VOLATILE
));
1140 /* If we are defining an aliased object whose nominal subtype is
1141 unconstrained, the object is a record that contains both the
1142 template and the object. If there is an initializer, it will
1143 have already been converted to the right type, but we need to
1144 create the template if there is no initializer. */
1147 && TREE_CODE (gnu_type
) == RECORD_TYPE
1148 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type
)
1149 /* Beware that padding might have been introduced above. */
1150 || (TYPE_PADDING_P (gnu_type
)
1151 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1153 && TYPE_CONTAINS_TEMPLATE_P
1154 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))))
1157 = TYPE_PADDING_P (gnu_type
)
1158 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1159 : TYPE_FIELDS (gnu_type
);
1160 VEC(constructor_elt
,gc
) *v
= VEC_alloc (constructor_elt
, gc
, 1);
1161 tree t
= build_template (TREE_TYPE (template_field
),
1162 TREE_TYPE (DECL_CHAIN (template_field
)),
1164 CONSTRUCTOR_APPEND_ELT (v
, template_field
, t
);
1165 gnu_expr
= gnat_build_constructor (gnu_type
, v
);
1168 /* Convert the expression to the type of the object except in the
1169 case where the object's type is unconstrained or the object's type
1170 is a padded record whose field is of self-referential size. In
1171 the former case, converting will generate unnecessary evaluations
1172 of the CONSTRUCTOR to compute the size and in the latter case, we
1173 want to only copy the actual data. Also don't convert to a record
1174 type with a variant part from a record type without one, to keep
1175 the object simpler. */
1177 && TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
1178 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
1179 && !(TYPE_IS_PADDING_P (gnu_type
)
1180 && CONTAINS_PLACEHOLDER_P
1181 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))))
1182 && !(TREE_CODE (gnu_type
) == RECORD_TYPE
1183 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
1184 && get_variant_part (gnu_type
) != NULL_TREE
1185 && get_variant_part (TREE_TYPE (gnu_expr
)) == NULL_TREE
))
1186 gnu_expr
= convert (gnu_type
, gnu_expr
);
1188 /* If this is a pointer that doesn't have an initializing expression,
1189 initialize it to NULL, unless the object is imported. */
1191 && (POINTER_TYPE_P (gnu_type
) || TYPE_IS_FAT_POINTER_P (gnu_type
))
1193 && !Is_Imported (gnat_entity
))
1194 gnu_expr
= integer_zero_node
;
1196 /* If we are defining the object and it has an Address clause, we must
1197 either get the address expression from the saved GCC tree for the
1198 object if it has a Freeze node, or elaborate the address expression
1199 here since the front-end has guaranteed that the elaboration has no
1200 effects in this case. */
1201 if (definition
&& Present (Address_Clause (gnat_entity
)))
1203 Node_Id gnat_expr
= Expression (Address_Clause (gnat_entity
));
1205 = present_gnu_tree (gnat_entity
)
1206 ? get_gnu_tree (gnat_entity
) : gnat_to_gnu (gnat_expr
);
1208 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
1210 /* Ignore the size. It's either meaningless or was handled
1212 gnu_size
= NULL_TREE
;
1213 /* Convert the type of the object to a reference type that can
1214 alias everything as per 13.3(19). */
1216 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1217 gnu_address
= convert (gnu_type
, gnu_address
);
1220 = !Is_Public (gnat_entity
)
1221 || compile_time_known_address_p (gnat_expr
);
1223 /* If this is a deferred constant, the initializer is attached to
1225 if (kind
== E_Constant
&& Present (Full_View (gnat_entity
)))
1228 (Expression (Declaration_Node (Full_View (gnat_entity
))));
1230 /* If we don't have an initializing expression for the underlying
1231 variable, the initializing expression for the pointer is the
1232 specified address. Otherwise, we have to make a COMPOUND_EXPR
1233 to assign both the address and the initial value. */
1235 gnu_expr
= gnu_address
;
1238 = build2 (COMPOUND_EXPR
, gnu_type
,
1240 (MODIFY_EXPR
, NULL_TREE
,
1241 build_unary_op (INDIRECT_REF
, NULL_TREE
,
1247 /* If it has an address clause and we are not defining it, mark it
1248 as an indirect object. Likewise for Stdcall objects that are
1250 if ((!definition
&& Present (Address_Clause (gnat_entity
)))
1251 || (Is_Imported (gnat_entity
)
1252 && Has_Stdcall_Convention (gnat_entity
)))
1254 /* Convert the type of the object to a reference type that can
1255 alias everything as per 13.3(19). */
1257 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1258 gnu_size
= NULL_TREE
;
1260 /* No point in taking the address of an initializing expression
1261 that isn't going to be used. */
1262 gnu_expr
= NULL_TREE
;
1264 /* If it has an address clause whose value is known at compile
1265 time, make the object a CONST_DECL. This will avoid a
1266 useless dereference. */
1267 if (Present (Address_Clause (gnat_entity
)))
1269 Node_Id gnat_address
1270 = Expression (Address_Clause (gnat_entity
));
1272 if (compile_time_known_address_p (gnat_address
))
1274 gnu_expr
= gnat_to_gnu (gnat_address
);
1282 /* If we are at top level and this object is of variable size,
1283 make the actual type a hidden pointer to the real type and
1284 make the initializer be a memory allocation and initialization.
1285 Likewise for objects we aren't defining (presumed to be
1286 external references from other packages), but there we do
1287 not set up an initialization.
1289 If the object's size overflows, make an allocator too, so that
1290 Storage_Error gets raised. Note that we will never free
1291 such memory, so we presume it never will get allocated. */
1292 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type
),
1293 global_bindings_p ()
1297 && !allocatable_size_p (convert (sizetype
,
1299 (CEIL_DIV_EXPR
, gnu_size
,
1300 bitsize_unit_node
)),
1301 global_bindings_p ()
1305 gnu_type
= build_reference_type (gnu_type
);
1306 gnu_size
= NULL_TREE
;
1309 /* In case this was a aliased object whose nominal subtype is
1310 unconstrained, the pointer above will be a thin pointer and
1311 build_allocator will automatically make the template.
1313 If we have a template initializer only (that we made above),
1314 pretend there is none and rely on what build_allocator creates
1315 again anyway. Otherwise (if we have a full initializer), get
1316 the data part and feed that to build_allocator.
1318 If we are elaborating a mutable object, tell build_allocator to
1319 ignore a possibly simpler size from the initializer, if any, as
1320 we must allocate the maximum possible size in this case. */
1321 if (definition
&& !imported_p
)
1323 tree gnu_alloc_type
= TREE_TYPE (gnu_type
);
1325 if (TREE_CODE (gnu_alloc_type
) == RECORD_TYPE
1326 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type
))
1329 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type
)));
1331 if (TREE_CODE (gnu_expr
) == CONSTRUCTOR
1332 && 1 == VEC_length (constructor_elt
,
1333 CONSTRUCTOR_ELTS (gnu_expr
)))
1337 = build_component_ref
1338 (gnu_expr
, NULL_TREE
,
1339 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr
))),
1343 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type
)) == INTEGER_CST
1344 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type
)))
1345 post_error ("?`Storage_Error` will be raised at run time!",
1349 = build_allocator (gnu_alloc_type
, gnu_expr
, gnu_type
,
1350 Empty
, Empty
, gnat_entity
, mutable_p
);
1355 gnu_expr
= NULL_TREE
;
1360 /* If this object would go into the stack and has an alignment larger
1361 than the largest stack alignment the back-end can honor, resort to
1362 a variable of "aligning type". */
1363 if (!global_bindings_p () && !static_p
&& definition
1364 && !imported_p
&& TYPE_ALIGN (gnu_type
) > BIGGEST_ALIGNMENT
)
1366 /* Create the new variable. No need for extra room before the
1367 aligned field as this is in automatic storage. */
1369 = make_aligning_type (gnu_type
, TYPE_ALIGN (gnu_type
),
1370 TYPE_SIZE_UNIT (gnu_type
),
1371 BIGGEST_ALIGNMENT
, 0);
1373 = create_var_decl (create_concat_name (gnat_entity
, "ALIGN"),
1374 NULL_TREE
, gnu_new_type
, NULL_TREE
, false,
1375 false, false, false, NULL
, gnat_entity
);
1377 /* Initialize the aligned field if we have an initializer. */
1380 (build_binary_op (MODIFY_EXPR
, NULL_TREE
,
1382 (gnu_new_var
, NULL_TREE
,
1383 TYPE_FIELDS (gnu_new_type
), false),
1387 /* And setup this entity as a reference to the aligned field. */
1388 gnu_type
= build_reference_type (gnu_type
);
1391 (ADDR_EXPR
, gnu_type
,
1392 build_component_ref (gnu_new_var
, NULL_TREE
,
1393 TYPE_FIELDS (gnu_new_type
), false));
1395 gnu_size
= NULL_TREE
;
1400 /* If this is an aliased object with an unconstrained nominal subtype,
1401 we make its type a thin reference, i.e. the reference counterpart
1402 of a thin pointer, so that it points to the array part. This is
1403 aimed at making it easier for the debugger to decode the object.
1404 Note that we have to do that this late because of the couple of
1405 allocation adjustments that might be made just above. */
1406 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
1407 && Is_Array_Type (Etype (gnat_entity
))
1408 && !type_annotate_only
)
1411 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
1413 /* In case the object with the template has already been allocated
1414 just above, we have nothing to do here. */
1415 if (!TYPE_IS_THIN_POINTER_P (gnu_type
))
1417 gnu_size
= NULL_TREE
;
1420 if (definition
&& !imported_p
)
1423 = create_var_decl (concat_name (gnu_entity_name
, "UNC"),
1424 NULL_TREE
, gnu_type
, gnu_expr
,
1425 const_flag
, Is_Public (gnat_entity
),
1426 false, static_p
, NULL
, gnat_entity
);
1428 = build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_unc_var
);
1429 TREE_CONSTANT (gnu_expr
) = 1;
1434 gnu_expr
= NULL_TREE
;
1440 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array
));
1444 gnu_type
= build_qualified_type (gnu_type
, (TYPE_QUALS (gnu_type
)
1445 | TYPE_QUAL_CONST
));
1447 /* Convert the expression to the type of the object except in the
1448 case where the object's type is unconstrained or the object's type
1449 is a padded record whose field is of self-referential size. In
1450 the former case, converting will generate unnecessary evaluations
1451 of the CONSTRUCTOR to compute the size and in the latter case, we
1452 want to only copy the actual data. Also don't convert to a record
1453 type with a variant part from a record type without one, to keep
1454 the object simpler. */
1456 && TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
1457 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
1458 && !(TYPE_IS_PADDING_P (gnu_type
)
1459 && CONTAINS_PLACEHOLDER_P
1460 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))))
1461 && !(TREE_CODE (gnu_type
) == RECORD_TYPE
1462 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
1463 && get_variant_part (gnu_type
) != NULL_TREE
1464 && get_variant_part (TREE_TYPE (gnu_expr
)) == NULL_TREE
))
1465 gnu_expr
= convert (gnu_type
, gnu_expr
);
1467 /* If this name is external or there was a name specified, use it,
1468 unless this is a VMS exception object since this would conflict
1469 with the symbol we need to export in addition. Don't use the
1470 Interface_Name if there is an address clause (see CD30005). */
1471 if (!Is_VMS_Exception (gnat_entity
)
1472 && ((Present (Interface_Name (gnat_entity
))
1473 && No (Address_Clause (gnat_entity
)))
1474 || (Is_Public (gnat_entity
)
1475 && (!Is_Imported (gnat_entity
)
1476 || Is_Exported (gnat_entity
)))))
1477 gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
1479 /* If this is an aggregate constant initialized to a constant, force it
1480 to be statically allocated. This saves an initialization copy. */
1483 && gnu_expr
&& TREE_CONSTANT (gnu_expr
)
1484 && AGGREGATE_TYPE_P (gnu_type
)
1485 && host_integerp (TYPE_SIZE_UNIT (gnu_type
), 1)
1486 && !(TYPE_IS_PADDING_P (gnu_type
)
1487 && !host_integerp (TYPE_SIZE_UNIT
1488 (TREE_TYPE (TYPE_FIELDS (gnu_type
))), 1)))
1491 /* Now create the variable or the constant and set various flags. */
1493 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
1494 gnu_expr
, const_flag
, Is_Public (gnat_entity
),
1495 imported_p
|| !definition
, static_p
, attr_list
,
1497 DECL_BY_REF_P (gnu_decl
) = used_by_ref
;
1498 DECL_POINTS_TO_READONLY_P (gnu_decl
) = used_by_ref
&& inner_const_flag
;
1499 DECL_CAN_NEVER_BE_NULL_P (gnu_decl
) = Can_Never_Be_Null (gnat_entity
);
1501 /* If we are defining an Out parameter and optimization isn't enabled,
1502 create a fake PARM_DECL for debugging purposes and make it point to
1503 the VAR_DECL. Suppress debug info for the latter but make sure it
1504 will live on the stack so that it can be accessed from within the
1505 debugger through the PARM_DECL. */
1506 if (kind
== E_Out_Parameter
&& definition
&& !optimize
&& debug_info_p
)
1508 tree param
= create_param_decl (gnu_entity_name
, gnu_type
, false);
1509 gnat_pushdecl (param
, gnat_entity
);
1510 SET_DECL_VALUE_EXPR (param
, gnu_decl
);
1511 DECL_HAS_VALUE_EXPR_P (param
) = 1;
1512 DECL_IGNORED_P (gnu_decl
) = 1;
1513 TREE_ADDRESSABLE (gnu_decl
) = 1;
1516 /* If this is a loop parameter, set the corresponding flag. */
1517 else if (kind
== E_Loop_Parameter
)
1518 DECL_LOOP_PARM_P (gnu_decl
) = 1;
1520 /* If this is a renaming pointer, attach the renamed object to it and
1521 register it if we are at the global level. Note that an external
1522 constant is at the global level. */
1523 else if (TREE_CODE (gnu_decl
) == VAR_DECL
&& renamed_obj
)
1525 SET_DECL_RENAMED_OBJECT (gnu_decl
, renamed_obj
);
1526 if ((!definition
&& kind
== E_Constant
) || global_bindings_p ())
1528 DECL_RENAMING_GLOBAL_P (gnu_decl
) = 1;
1529 record_global_renaming_pointer (gnu_decl
);
1533 /* If this is a constant and we are defining it or it generates a real
1534 symbol at the object level and we are referencing it, we may want
1535 or need to have a true variable to represent it:
1536 - if optimization isn't enabled, for debugging purposes,
1537 - if the constant is public and not overlaid on something else,
1538 - if its address is taken,
1539 - if either itself or its type is aliased. */
1540 if (TREE_CODE (gnu_decl
) == CONST_DECL
1541 && (definition
|| Sloc (gnat_entity
) > Standard_Location
)
1542 && ((!optimize
&& debug_info_p
)
1543 || (Is_Public (gnat_entity
)
1544 && No (Address_Clause (gnat_entity
)))
1545 || Address_Taken (gnat_entity
)
1546 || Is_Aliased (gnat_entity
)
1547 || Is_Aliased (Etype (gnat_entity
))))
1550 = create_true_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
1551 gnu_expr
, true, Is_Public (gnat_entity
),
1552 !definition
, static_p
, attr_list
,
1555 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl
, gnu_corr_var
);
1557 /* As debugging information will be generated for the variable,
1558 do not generate debugging information for the constant. */
1560 DECL_IGNORED_P (gnu_decl
) = 1;
1562 DECL_IGNORED_P (gnu_corr_var
) = 1;
1565 /* If this is a constant, even if we don't need a true variable, we
1566 may need to avoid returning the initializer in every case. That
1567 can happen for the address of a (constant) constructor because,
1568 upon dereferencing it, the constructor will be reinjected in the
1569 tree, which may not be valid in every case; see lvalue_required_p
1570 for more details. */
1571 if (TREE_CODE (gnu_decl
) == CONST_DECL
)
1572 DECL_CONST_ADDRESS_P (gnu_decl
) = constructor_address_p (gnu_expr
);
1574 /* If this object is declared in a block that contains a block with an
1575 exception handler, and we aren't using the GCC exception mechanism,
1576 we must force this variable in memory in order to avoid an invalid
1578 if (Exception_Mechanism
!= Back_End_Exceptions
1579 && Has_Nested_Block_With_Handler (Scope (gnat_entity
)))
1580 TREE_ADDRESSABLE (gnu_decl
) = 1;
1582 /* If we are defining an object with variable size or an object with
1583 fixed size that will be dynamically allocated, and we are using the
1584 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1586 && Exception_Mechanism
== Setjmp_Longjmp
1587 && get_block_jmpbuf_decl ()
1588 && DECL_SIZE_UNIT (gnu_decl
)
1589 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl
)) != INTEGER_CST
1590 || (flag_stack_check
== GENERIC_STACK_CHECK
1591 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl
),
1592 STACK_CHECK_MAX_VAR_SIZE
) > 0)))
1593 add_stmt_with_node (build_call_n_expr
1594 (update_setjmp_buf_decl
, 1,
1595 build_unary_op (ADDR_EXPR
, NULL_TREE
,
1596 get_block_jmpbuf_decl ())),
1599 /* Back-annotate Esize and Alignment of the object if not already
1600 known. Note that we pick the values of the type, not those of
1601 the object, to shield ourselves from low-level platform-dependent
1602 adjustments like alignment promotion. This is both consistent with
1603 all the treatment above, where alignment and size are set on the
1604 type of the object and not on the object directly, and makes it
1605 possible to support all confirming representation clauses. */
1606 annotate_object (gnat_entity
, TREE_TYPE (gnu_decl
), gnu_object_size
,
1607 used_by_ref
, false);
1612 /* Return a TYPE_DECL for "void" that we previously made. */
1613 gnu_decl
= TYPE_NAME (void_type_node
);
1616 case E_Enumeration_Type
:
1617 /* A special case: for the types Character and Wide_Character in
1618 Standard, we do not list all the literals. So if the literals
1619 are not specified, make this an unsigned type. */
1620 if (No (First_Literal (gnat_entity
)))
1622 gnu_type
= make_unsigned_type (esize
);
1623 TYPE_NAME (gnu_type
) = gnu_entity_name
;
1625 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1626 This is needed by the DWARF-2 back-end to distinguish between
1627 unsigned integer types and character types. */
1628 TYPE_STRING_FLAG (gnu_type
) = 1;
1633 /* We have a list of enumeral constants in First_Literal. We make a
1634 CONST_DECL for each one and build into GNU_LITERAL_LIST the list to
1635 be placed into TYPE_FIELDS. Each node in the list is a TREE_LIST
1636 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1637 value of the literal. But when we have a regular boolean type, we
1638 simplify this a little by using a BOOLEAN_TYPE. */
1639 bool is_boolean
= Is_Boolean_Type (gnat_entity
)
1640 && !Has_Non_Standard_Rep (gnat_entity
);
1641 tree gnu_literal_list
= NULL_TREE
;
1642 Entity_Id gnat_literal
;
1644 if (Is_Unsigned_Type (gnat_entity
))
1645 gnu_type
= make_unsigned_type (esize
);
1647 gnu_type
= make_signed_type (esize
);
1649 TREE_SET_CODE (gnu_type
, is_boolean
? BOOLEAN_TYPE
: ENUMERAL_TYPE
);
1651 for (gnat_literal
= First_Literal (gnat_entity
);
1652 Present (gnat_literal
);
1653 gnat_literal
= Next_Literal (gnat_literal
))
1656 = UI_To_gnu (Enumeration_Rep (gnat_literal
), gnu_type
);
1658 = create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
1659 gnu_type
, gnu_value
, true, false, false,
1660 false, NULL
, gnat_literal
);
1661 /* Do not generate debug info for individual enumerators. */
1662 DECL_IGNORED_P (gnu_literal
) = 1;
1663 save_gnu_tree (gnat_literal
, gnu_literal
, false);
1664 gnu_literal_list
= tree_cons (DECL_NAME (gnu_literal
),
1665 gnu_value
, gnu_literal_list
);
1669 TYPE_VALUES (gnu_type
) = nreverse (gnu_literal_list
);
1671 /* Note that the bounds are updated at the end of this function
1672 to avoid an infinite recursion since they refer to the type. */
1676 case E_Signed_Integer_Type
:
1677 case E_Ordinary_Fixed_Point_Type
:
1678 case E_Decimal_Fixed_Point_Type
:
1679 /* For integer types, just make a signed type the appropriate number
1681 gnu_type
= make_signed_type (esize
);
1684 case E_Modular_Integer_Type
:
1686 /* For modular types, make the unsigned type of the proper number
1687 of bits and then set up the modulus, if required. */
1688 tree gnu_modulus
, gnu_high
= NULL_TREE
;
1690 /* Packed array types are supposed to be subtypes only. */
1691 gcc_assert (!Is_Packed_Array_Type (gnat_entity
));
1693 gnu_type
= make_unsigned_type (esize
);
1695 /* Get the modulus in this type. If it overflows, assume it is because
1696 it is equal to 2**Esize. Note that there is no overflow checking
1697 done on unsigned type, so we detect the overflow by looking for
1698 a modulus of zero, which is otherwise invalid. */
1699 gnu_modulus
= UI_To_gnu (Modulus (gnat_entity
), gnu_type
);
1701 if (!integer_zerop (gnu_modulus
))
1703 TYPE_MODULAR_P (gnu_type
) = 1;
1704 SET_TYPE_MODULUS (gnu_type
, gnu_modulus
);
1705 gnu_high
= fold_build2 (MINUS_EXPR
, gnu_type
, gnu_modulus
,
1706 convert (gnu_type
, integer_one_node
));
1709 /* If the upper bound is not maximal, make an extra subtype. */
1711 && !tree_int_cst_equal (gnu_high
, TYPE_MAX_VALUE (gnu_type
)))
1713 tree gnu_subtype
= make_unsigned_type (esize
);
1714 SET_TYPE_RM_MAX_VALUE (gnu_subtype
, gnu_high
);
1715 TREE_TYPE (gnu_subtype
) = gnu_type
;
1716 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
1717 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "UMT");
1718 gnu_type
= gnu_subtype
;
1723 case E_Signed_Integer_Subtype
:
1724 case E_Enumeration_Subtype
:
1725 case E_Modular_Integer_Subtype
:
1726 case E_Ordinary_Fixed_Point_Subtype
:
1727 case E_Decimal_Fixed_Point_Subtype
:
1729 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1730 not want to call create_range_type since we would like each subtype
1731 node to be distinct. ??? Historically this was in preparation for
1732 when memory aliasing is implemented, but that's obsolete now given
1733 the call to relate_alias_sets below.
1735 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1736 this fact is used by the arithmetic conversion functions.
1738 We elaborate the Ancestor_Subtype if it is not in the current unit
1739 and one of our bounds is non-static. We do this to ensure consistent
1740 naming in the case where several subtypes share the same bounds, by
1741 elaborating the first such subtype first, thus using its name. */
1744 && Present (Ancestor_Subtype (gnat_entity
))
1745 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1746 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1747 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
1748 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
), gnu_expr
, 0);
1750 /* Set the precision to the Esize except for bit-packed arrays. */
1751 if (Is_Packed_Array_Type (gnat_entity
)
1752 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1753 esize
= UI_To_Int (RM_Size (gnat_entity
));
1755 /* This should be an unsigned type if the base type is unsigned or
1756 if the lower bound is constant and non-negative or if the type
1758 if (Is_Unsigned_Type (Etype (gnat_entity
))
1759 || Is_Unsigned_Type (gnat_entity
)
1760 || Has_Biased_Representation (gnat_entity
))
1761 gnu_type
= make_unsigned_type (esize
);
1763 gnu_type
= make_signed_type (esize
);
1764 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
1766 SET_TYPE_RM_MIN_VALUE
1768 convert (TREE_TYPE (gnu_type
),
1769 elaborate_expression (Type_Low_Bound (gnat_entity
),
1770 gnat_entity
, get_identifier ("L"),
1772 Needs_Debug_Info (gnat_entity
))));
1774 SET_TYPE_RM_MAX_VALUE
1776 convert (TREE_TYPE (gnu_type
),
1777 elaborate_expression (Type_High_Bound (gnat_entity
),
1778 gnat_entity
, get_identifier ("U"),
1780 Needs_Debug_Info (gnat_entity
))));
1782 /* One of the above calls might have caused us to be elaborated,
1783 so don't blow up if so. */
1784 if (present_gnu_tree (gnat_entity
))
1786 maybe_present
= true;
1790 TYPE_BIASED_REPRESENTATION_P (gnu_type
)
1791 = Has_Biased_Representation (gnat_entity
);
1793 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1794 TYPE_STUB_DECL (gnu_type
)
1795 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
1797 /* Inherit our alias set from what we're a subtype of. Subtypes
1798 are not different types and a pointer can designate any instance
1799 within a subtype hierarchy. */
1800 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
1802 /* For a packed array, make the original array type a parallel type. */
1804 && Is_Packed_Array_Type (gnat_entity
)
1805 && present_gnu_tree (Original_Array_Type (gnat_entity
)))
1806 add_parallel_type (gnu_type
,
1808 (Original_Array_Type (gnat_entity
)));
1812 /* We have to handle clauses that under-align the type specially. */
1813 if ((Present (Alignment_Clause (gnat_entity
))
1814 || (Is_Packed_Array_Type (gnat_entity
)
1816 (Alignment_Clause (Original_Array_Type (gnat_entity
)))))
1817 && UI_Is_In_Int_Range (Alignment (gnat_entity
)))
1819 align
= UI_To_Int (Alignment (gnat_entity
)) * BITS_PER_UNIT
;
1820 if (align
>= TYPE_ALIGN (gnu_type
))
1824 /* If the type we are dealing with represents a bit-packed array,
1825 we need to have the bits left justified on big-endian targets
1826 and right justified on little-endian targets. We also need to
1827 ensure that when the value is read (e.g. for comparison of two
1828 such values), we only get the good bits, since the unused bits
1829 are uninitialized. Both goals are accomplished by wrapping up
1830 the modular type in an enclosing record type. */
1831 if (Is_Packed_Array_Type (gnat_entity
)
1832 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1834 tree gnu_field_type
, gnu_field
;
1836 /* Set the RM size before wrapping up the original type. */
1837 SET_TYPE_RM_SIZE (gnu_type
,
1838 UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
));
1839 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
) = 1;
1841 /* Create a stripped-down declaration, mainly for debugging. */
1842 create_type_decl (gnu_entity_name
, gnu_type
, NULL
, true,
1843 debug_info_p
, gnat_entity
);
1845 /* Now save it and build the enclosing record type. */
1846 gnu_field_type
= gnu_type
;
1848 gnu_type
= make_node (RECORD_TYPE
);
1849 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "JM");
1850 TYPE_PACKED (gnu_type
) = 1;
1851 TYPE_SIZE (gnu_type
) = TYPE_SIZE (gnu_field_type
);
1852 TYPE_SIZE_UNIT (gnu_type
) = TYPE_SIZE_UNIT (gnu_field_type
);
1853 SET_TYPE_ADA_SIZE (gnu_type
, TYPE_RM_SIZE (gnu_field_type
));
1855 /* Propagate the alignment of the modular type to the record type,
1856 unless there is an alignment clause that under-aligns the type.
1857 This means that bit-packed arrays are given "ceil" alignment for
1858 their size by default, which may seem counter-intuitive but makes
1859 it possible to overlay them on modular types easily. */
1860 TYPE_ALIGN (gnu_type
)
1861 = align
> 0 ? align
: TYPE_ALIGN (gnu_field_type
);
1863 relate_alias_sets (gnu_type
, gnu_field_type
, ALIAS_SET_COPY
);
1865 /* Don't declare the field as addressable since we won't be taking
1866 its address and this would prevent create_field_decl from making
1869 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type
,
1870 gnu_type
, NULL_TREE
, bitsize_zero_node
, 1, 0);
1872 /* Do not emit debug info until after the parallel type is added. */
1873 finish_record_type (gnu_type
, gnu_field
, 2, false);
1874 compute_record_mode (gnu_type
);
1875 TYPE_JUSTIFIED_MODULAR_P (gnu_type
) = 1;
1879 /* Make the original array type a parallel type. */
1880 if (present_gnu_tree (Original_Array_Type (gnat_entity
)))
1881 add_parallel_type (gnu_type
,
1883 (Original_Array_Type (gnat_entity
)));
1885 rest_of_record_type_compilation (gnu_type
);
1889 /* If the type we are dealing with has got a smaller alignment than the
1890 natural one, we need to wrap it up in a record type and under-align
1891 the latter. We reuse the padding machinery for this purpose. */
1894 tree gnu_field_type
, gnu_field
;
1896 /* Set the RM size before wrapping up the type. */
1897 SET_TYPE_RM_SIZE (gnu_type
,
1898 UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
));
1900 /* Create a stripped-down declaration, mainly for debugging. */
1901 create_type_decl (gnu_entity_name
, gnu_type
, NULL
, true,
1902 debug_info_p
, gnat_entity
);
1904 /* Now save it and build the enclosing record type. */
1905 gnu_field_type
= gnu_type
;
1907 gnu_type
= make_node (RECORD_TYPE
);
1908 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "PAD");
1909 TYPE_PACKED (gnu_type
) = 1;
1910 TYPE_SIZE (gnu_type
) = TYPE_SIZE (gnu_field_type
);
1911 TYPE_SIZE_UNIT (gnu_type
) = TYPE_SIZE_UNIT (gnu_field_type
);
1912 SET_TYPE_ADA_SIZE (gnu_type
, TYPE_RM_SIZE (gnu_field_type
));
1913 TYPE_ALIGN (gnu_type
) = align
;
1914 relate_alias_sets (gnu_type
, gnu_field_type
, ALIAS_SET_COPY
);
1916 /* Don't declare the field as addressable since we won't be taking
1917 its address and this would prevent create_field_decl from making
1920 = create_field_decl (get_identifier ("F"), gnu_field_type
,
1921 gnu_type
, NULL_TREE
, bitsize_zero_node
, 1, 0);
1923 finish_record_type (gnu_type
, gnu_field
, 2, debug_info_p
);
1924 compute_record_mode (gnu_type
);
1925 TYPE_PADDING_P (gnu_type
) = 1;
1930 case E_Floating_Point_Type
:
1931 /* If this is a VAX floating-point type, use an integer of the proper
1932 size. All the operations will be handled with ASM statements. */
1933 if (Vax_Float (gnat_entity
))
1935 gnu_type
= make_signed_type (esize
);
1936 TYPE_VAX_FLOATING_POINT_P (gnu_type
) = 1;
1937 SET_TYPE_DIGITS_VALUE (gnu_type
,
1938 UI_To_gnu (Digits_Value (gnat_entity
),
1943 /* The type of the Low and High bounds can be our type if this is
1944 a type from Standard, so set them at the end of the function. */
1945 gnu_type
= make_node (REAL_TYPE
);
1946 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
1947 layout_type (gnu_type
);
1950 case E_Floating_Point_Subtype
:
1951 if (Vax_Float (gnat_entity
))
1953 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
1959 && Present (Ancestor_Subtype (gnat_entity
))
1960 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1961 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1962 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
1963 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
),
1966 gnu_type
= make_node (REAL_TYPE
);
1967 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
1968 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
1969 TYPE_GCC_MIN_VALUE (gnu_type
)
1970 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type
));
1971 TYPE_GCC_MAX_VALUE (gnu_type
)
1972 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type
));
1973 layout_type (gnu_type
);
1975 SET_TYPE_RM_MIN_VALUE
1977 convert (TREE_TYPE (gnu_type
),
1978 elaborate_expression (Type_Low_Bound (gnat_entity
),
1979 gnat_entity
, get_identifier ("L"),
1981 Needs_Debug_Info (gnat_entity
))));
1983 SET_TYPE_RM_MAX_VALUE
1985 convert (TREE_TYPE (gnu_type
),
1986 elaborate_expression (Type_High_Bound (gnat_entity
),
1987 gnat_entity
, get_identifier ("U"),
1989 Needs_Debug_Info (gnat_entity
))));
1991 /* One of the above calls might have caused us to be elaborated,
1992 so don't blow up if so. */
1993 if (present_gnu_tree (gnat_entity
))
1995 maybe_present
= true;
1999 /* Inherit our alias set from what we're a subtype of, as for
2000 integer subtypes. */
2001 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
2005 /* Array and String Types and Subtypes
2007 Unconstrained array types are represented by E_Array_Type and
2008 constrained array types are represented by E_Array_Subtype. There
2009 are no actual objects of an unconstrained array type; all we have
2010 are pointers to that type.
2012 The following fields are defined on array types and subtypes:
2014 Component_Type Component type of the array.
2015 Number_Dimensions Number of dimensions (an int).
2016 First_Index Type of first index. */
2021 const bool convention_fortran_p
2022 = (Convention (gnat_entity
) == Convention_Fortran
);
2023 const int ndim
= Number_Dimensions (gnat_entity
);
2024 tree gnu_template_type
;
2025 tree gnu_ptr_template
;
2026 tree gnu_template_reference
, gnu_template_fields
, gnu_fat_type
;
2027 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2028 tree
*gnu_temp_fields
= XALLOCAVEC (tree
, ndim
);
2029 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
, tem
, t
;
2030 Entity_Id gnat_index
, gnat_name
;
2034 /* Create the type for the component now, as it simplifies breaking
2035 type reference loops. */
2037 = gnat_to_gnu_component_type (gnat_entity
, definition
, debug_info_p
);
2038 if (present_gnu_tree (gnat_entity
))
2040 /* As a side effect, the type may have been translated. */
2041 maybe_present
= true;
2045 /* We complete an existing dummy fat pointer type in place. This both
2046 avoids further complex adjustments in update_pointer_to and yields
2047 better debugging information in DWARF by leveraging the support for
2048 incomplete declarations of "tagged" types in the DWARF back-end. */
2049 gnu_type
= get_dummy_type (gnat_entity
);
2050 if (gnu_type
&& TYPE_POINTER_TO (gnu_type
))
2052 gnu_fat_type
= TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type
));
2053 TYPE_NAME (gnu_fat_type
) = NULL_TREE
;
2054 /* Save the contents of the dummy type for update_pointer_to. */
2055 TYPE_POINTER_TO (gnu_type
) = copy_type (gnu_fat_type
);
2057 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type
)));
2058 gnu_template_type
= TREE_TYPE (gnu_ptr_template
);
2062 gnu_fat_type
= make_node (RECORD_TYPE
);
2063 gnu_template_type
= make_node (RECORD_TYPE
);
2064 gnu_ptr_template
= build_pointer_type (gnu_template_type
);
2067 /* Make a node for the array. If we are not defining the array
2068 suppress expanding incomplete types. */
2069 gnu_type
= make_node (UNCONSTRAINED_ARRAY_TYPE
);
2073 defer_incomplete_level
++;
2074 this_deferred
= true;
2077 /* Build the fat pointer type. Use a "void *" object instead of
2078 a pointer to the array type since we don't have the array type
2079 yet (it will reference the fat pointer via the bounds). */
2081 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node
,
2082 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2084 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template
,
2085 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2087 if (COMPLETE_TYPE_P (gnu_fat_type
))
2089 /* We are going to lay it out again so reset the alias set. */
2090 alias_set_type alias_set
= TYPE_ALIAS_SET (gnu_fat_type
);
2091 TYPE_ALIAS_SET (gnu_fat_type
) = -1;
2092 finish_fat_pointer_type (gnu_fat_type
, tem
);
2093 TYPE_ALIAS_SET (gnu_fat_type
) = alias_set
;
2094 for (t
= gnu_fat_type
; t
; t
= TYPE_NEXT_VARIANT (t
))
2096 TYPE_FIELDS (t
) = tem
;
2097 SET_TYPE_UNCONSTRAINED_ARRAY (t
, gnu_type
);
2102 finish_fat_pointer_type (gnu_fat_type
, tem
);
2103 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type
, gnu_type
);
2106 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2107 is the fat pointer. This will be used to access the individual
2108 fields once we build them. */
2109 tem
= build3 (COMPONENT_REF
, gnu_ptr_template
,
2110 build0 (PLACEHOLDER_EXPR
, gnu_fat_type
),
2111 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type
)), NULL_TREE
);
2112 gnu_template_reference
2113 = build_unary_op (INDIRECT_REF
, gnu_template_type
, tem
);
2114 TREE_READONLY (gnu_template_reference
) = 1;
2115 TREE_THIS_NOTRAP (gnu_template_reference
) = 1;
2117 /* Now create the GCC type for each index and add the fields for that
2118 index to the template. */
2119 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2120 gnat_index
= First_Index (gnat_entity
);
2121 0 <= index
&& index
< ndim
;
2122 index
+= (convention_fortran_p
? - 1 : 1),
2123 gnat_index
= Next_Index (gnat_index
))
2125 char field_name
[16];
2126 tree gnu_index_base_type
2127 = get_unpadded_type (Base_Type (Etype (gnat_index
)));
2128 tree gnu_lb_field
, gnu_hb_field
, gnu_orig_min
, gnu_orig_max
;
2129 tree gnu_min
, gnu_max
, gnu_high
;
2131 /* Make the FIELD_DECLs for the low and high bounds of this
2132 type and then make extractions of these fields from the
2134 sprintf (field_name
, "LB%d", index
);
2135 gnu_lb_field
= create_field_decl (get_identifier (field_name
),
2136 gnu_index_base_type
,
2137 gnu_template_type
, NULL_TREE
,
2139 Sloc_to_locus (Sloc (gnat_entity
),
2140 &DECL_SOURCE_LOCATION (gnu_lb_field
));
2142 field_name
[0] = 'U';
2143 gnu_hb_field
= create_field_decl (get_identifier (field_name
),
2144 gnu_index_base_type
,
2145 gnu_template_type
, NULL_TREE
,
2147 Sloc_to_locus (Sloc (gnat_entity
),
2148 &DECL_SOURCE_LOCATION (gnu_hb_field
));
2150 gnu_temp_fields
[index
] = chainon (gnu_lb_field
, gnu_hb_field
);
2152 /* We can't use build_component_ref here since the template type
2153 isn't complete yet. */
2154 gnu_orig_min
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2155 gnu_template_reference
, gnu_lb_field
,
2157 gnu_orig_max
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2158 gnu_template_reference
, gnu_hb_field
,
2160 TREE_READONLY (gnu_orig_min
) = TREE_READONLY (gnu_orig_max
) = 1;
2162 gnu_min
= convert (sizetype
, gnu_orig_min
);
2163 gnu_max
= convert (sizetype
, gnu_orig_max
);
2165 /* Compute the size of this dimension. See the E_Array_Subtype
2166 case below for the rationale. */
2168 = build3 (COND_EXPR
, sizetype
,
2169 build2 (GE_EXPR
, boolean_type_node
,
2170 gnu_orig_max
, gnu_orig_min
),
2172 size_binop (MINUS_EXPR
, gnu_min
, size_one_node
));
2174 /* Make a range type with the new range in the Ada base type.
2175 Then make an index type with the size range in sizetype. */
2176 gnu_index_types
[index
]
2177 = create_index_type (gnu_min
, gnu_high
,
2178 create_range_type (gnu_index_base_type
,
2183 /* Update the maximum size of the array in elements. */
2186 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2188 = convert (sizetype
, TYPE_MIN_VALUE (gnu_index_type
));
2190 = convert (sizetype
, TYPE_MAX_VALUE (gnu_index_type
));
2192 = size_binop (MAX_EXPR
,
2193 size_binop (PLUS_EXPR
, size_one_node
,
2194 size_binop (MINUS_EXPR
,
2198 if (TREE_CODE (gnu_this_max
) == INTEGER_CST
2199 && TREE_OVERFLOW (gnu_this_max
))
2200 gnu_max_size
= NULL_TREE
;
2203 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2206 TYPE_NAME (gnu_index_types
[index
])
2207 = create_concat_name (gnat_entity
, field_name
);
2210 /* Install all the fields into the template. */
2211 TYPE_NAME (gnu_template_type
)
2212 = create_concat_name (gnat_entity
, "XUB");
2213 gnu_template_fields
= NULL_TREE
;
2214 for (index
= 0; index
< ndim
; index
++)
2216 = chainon (gnu_template_fields
, gnu_temp_fields
[index
]);
2217 finish_record_type (gnu_template_type
, gnu_template_fields
, 0,
2219 TYPE_READONLY (gnu_template_type
) = 1;
2221 /* If Component_Size is not already specified, annotate it with the
2222 size of the component. */
2223 if (Unknown_Component_Size (gnat_entity
))
2224 Set_Component_Size (gnat_entity
,
2225 annotate_value (TYPE_SIZE (comp_type
)));
2227 /* Compute the maximum size of the array in units and bits. */
2230 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2231 TYPE_SIZE_UNIT (comp_type
));
2232 gnu_max_size
= size_binop (MULT_EXPR
,
2233 convert (bitsizetype
, gnu_max_size
),
2234 TYPE_SIZE (comp_type
));
2237 gnu_max_size_unit
= NULL_TREE
;
2239 /* Now build the array type. */
2241 for (index
= ndim
- 1; index
>= 0; index
--)
2243 tem
= build_nonshared_array_type (tem
, gnu_index_types
[index
]);
2244 TYPE_MULTI_ARRAY_P (tem
) = (index
> 0);
2245 if (array_type_has_nonaliased_component (tem
, gnat_entity
))
2246 TYPE_NONALIASED_COMPONENT (tem
) = 1;
2249 /* If an alignment is specified, use it if valid. But ignore it
2250 for the original type of packed array types. If the alignment
2251 was requested with an explicit alignment clause, state so. */
2252 if (No (Packed_Array_Type (gnat_entity
))
2253 && Known_Alignment (gnat_entity
))
2256 = validate_alignment (Alignment (gnat_entity
), gnat_entity
,
2258 if (Present (Alignment_Clause (gnat_entity
)))
2259 TYPE_USER_ALIGN (tem
) = 1;
2262 TYPE_CONVENTION_FORTRAN_P (tem
) = convention_fortran_p
;
2264 /* Adjust the type of the pointer-to-array field of the fat pointer
2265 and record the aliasing relationships if necessary. */
2266 TREE_TYPE (TYPE_FIELDS (gnu_fat_type
)) = build_pointer_type (tem
);
2267 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type
))
2268 record_component_aliases (gnu_fat_type
);
2270 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2271 corresponding fat pointer. */
2272 TREE_TYPE (gnu_type
) = gnu_fat_type
;
2273 TYPE_POINTER_TO (gnu_type
) = gnu_fat_type
;
2274 TYPE_REFERENCE_TO (gnu_type
) = gnu_fat_type
;
2275 SET_TYPE_MODE (gnu_type
, BLKmode
);
2276 TYPE_ALIGN (gnu_type
) = TYPE_ALIGN (tem
);
2278 /* If the maximum size doesn't overflow, use it. */
2280 && TREE_CODE (gnu_max_size
) == INTEGER_CST
2281 && !TREE_OVERFLOW (gnu_max_size
)
2282 && TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2283 && !TREE_OVERFLOW (gnu_max_size_unit
))
2285 TYPE_SIZE (tem
) = size_binop (MIN_EXPR
, gnu_max_size
,
2287 TYPE_SIZE_UNIT (tem
) = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2288 TYPE_SIZE_UNIT (tem
));
2291 create_type_decl (create_concat_name (gnat_entity
, "XUA"),
2292 tem
, NULL
, !Comes_From_Source (gnat_entity
),
2293 debug_info_p
, gnat_entity
);
2295 /* Give the fat pointer type a name. If this is a packed type, tell
2296 the debugger how to interpret the underlying bits. */
2297 if (Present (Packed_Array_Type (gnat_entity
)))
2298 gnat_name
= Packed_Array_Type (gnat_entity
);
2300 gnat_name
= gnat_entity
;
2301 create_type_decl (create_concat_name (gnat_name
, "XUP"),
2302 gnu_fat_type
, NULL
, !Comes_From_Source (gnat_entity
),
2303 debug_info_p
, gnat_entity
);
2305 /* Create the type to be designated by thin pointers: a record type for
2306 the array and its template. We used to shift the fields to have the
2307 template at a negative offset, but this was somewhat of a kludge; we
2308 now shift thin pointer values explicitly but only those which have a
2309 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2310 tem
= build_unc_object_type (gnu_template_type
, tem
,
2311 create_concat_name (gnat_name
, "XUT"),
2314 SET_TYPE_UNCONSTRAINED_ARRAY (tem
, gnu_type
);
2315 TYPE_OBJECT_RECORD_TYPE (gnu_type
) = tem
;
2319 case E_String_Subtype
:
2320 case E_Array_Subtype
:
2322 /* This is the actual data type for array variables. Multidimensional
2323 arrays are implemented as arrays of arrays. Note that arrays which
2324 have sparse enumeration subtypes as index components create sparse
2325 arrays, which is obviously space inefficient but so much easier to
2328 Also note that the subtype never refers to the unconstrained array
2329 type, which is somewhat at variance with Ada semantics.
2331 First check to see if this is simply a renaming of the array type.
2332 If so, the result is the array type. */
2334 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
2335 if (!Is_Constrained (gnat_entity
))
2339 Entity_Id gnat_index
, gnat_base_index
;
2340 const bool convention_fortran_p
2341 = (Convention (gnat_entity
) == Convention_Fortran
);
2342 const int ndim
= Number_Dimensions (gnat_entity
);
2343 tree gnu_base_type
= gnu_type
;
2344 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2345 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
;
2346 bool need_index_type_struct
= false;
2349 /* First create the GCC type for each index and find out whether
2350 special types are needed for debugging information. */
2351 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2352 gnat_index
= First_Index (gnat_entity
),
2354 = First_Index (Implementation_Base_Type (gnat_entity
));
2355 0 <= index
&& index
< ndim
;
2356 index
+= (convention_fortran_p
? - 1 : 1),
2357 gnat_index
= Next_Index (gnat_index
),
2358 gnat_base_index
= Next_Index (gnat_base_index
))
2360 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2361 tree gnu_orig_min
= TYPE_MIN_VALUE (gnu_index_type
);
2362 tree gnu_orig_max
= TYPE_MAX_VALUE (gnu_index_type
);
2363 tree gnu_min
= convert (sizetype
, gnu_orig_min
);
2364 tree gnu_max
= convert (sizetype
, gnu_orig_max
);
2365 tree gnu_base_index_type
2366 = get_unpadded_type (Etype (gnat_base_index
));
2367 tree gnu_base_orig_min
= TYPE_MIN_VALUE (gnu_base_index_type
);
2368 tree gnu_base_orig_max
= TYPE_MAX_VALUE (gnu_base_index_type
);
2371 /* See if the base array type is already flat. If it is, we
2372 are probably compiling an ACATS test but it will cause the
2373 code below to malfunction if we don't handle it specially. */
2374 if (TREE_CODE (gnu_base_orig_min
) == INTEGER_CST
2375 && TREE_CODE (gnu_base_orig_max
) == INTEGER_CST
2376 && tree_int_cst_lt (gnu_base_orig_max
, gnu_base_orig_min
))
2378 gnu_min
= size_one_node
;
2379 gnu_max
= size_zero_node
;
2383 /* Similarly, if one of the values overflows in sizetype and the
2384 range is null, use 1..0 for the sizetype bounds. */
2385 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2386 && TREE_CODE (gnu_max
) == INTEGER_CST
2387 && (TREE_OVERFLOW (gnu_min
) || TREE_OVERFLOW (gnu_max
))
2388 && tree_int_cst_lt (gnu_orig_max
, gnu_orig_min
))
2390 gnu_min
= size_one_node
;
2391 gnu_max
= size_zero_node
;
2395 /* If the minimum and maximum values both overflow in sizetype,
2396 but the difference in the original type does not overflow in
2397 sizetype, ignore the overflow indication. */
2398 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2399 && TREE_CODE (gnu_max
) == INTEGER_CST
2400 && TREE_OVERFLOW (gnu_min
) && TREE_OVERFLOW (gnu_max
)
2403 fold_build2 (MINUS_EXPR
, gnu_index_type
,
2407 TREE_OVERFLOW (gnu_min
) = 0;
2408 TREE_OVERFLOW (gnu_max
) = 0;
2412 /* Compute the size of this dimension in the general case. We
2413 need to provide GCC with an upper bound to use but have to
2414 deal with the "superflat" case. There are three ways to do
2415 this. If we can prove that the array can never be superflat,
2416 we can just use the high bound of the index type. */
2417 else if ((Nkind (gnat_index
) == N_Range
2418 && cannot_be_superflat_p (gnat_index
))
2419 /* Packed Array Types are never superflat. */
2420 || Is_Packed_Array_Type (gnat_entity
))
2423 /* Otherwise, if the high bound is constant but the low bound is
2424 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2425 lower bound. Note that the comparison must be done in the
2426 original type to avoid any overflow during the conversion. */
2427 else if (TREE_CODE (gnu_max
) == INTEGER_CST
2428 && TREE_CODE (gnu_min
) != INTEGER_CST
)
2432 = build_cond_expr (sizetype
,
2433 build_binary_op (GE_EXPR
,
2438 size_binop (PLUS_EXPR
, gnu_max
,
2442 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2443 in all the other cases. Note that, here as well as above,
2444 the condition used in the comparison must be equivalent to
2445 the condition (length != 0). This is relied upon in order
2446 to optimize array comparisons in compare_arrays. */
2449 = build_cond_expr (sizetype
,
2450 build_binary_op (GE_EXPR
,
2455 size_binop (MINUS_EXPR
, gnu_min
,
2458 /* Reuse the index type for the range type. Then make an index
2459 type with the size range in sizetype. */
2460 gnu_index_types
[index
]
2461 = create_index_type (gnu_min
, gnu_high
, gnu_index_type
,
2464 /* Update the maximum size of the array in elements. Here we
2465 see if any constraint on the index type of the base type
2466 can be used in the case of self-referential bound on the
2467 index type of the subtype. We look for a non-"infinite"
2468 and non-self-referential bound from any type involved and
2469 handle each bound separately. */
2472 tree gnu_base_min
= convert (sizetype
, gnu_base_orig_min
);
2473 tree gnu_base_max
= convert (sizetype
, gnu_base_orig_max
);
2474 tree gnu_base_index_base_type
2475 = get_base_type (gnu_base_index_type
);
2476 tree gnu_base_base_min
2477 = convert (sizetype
,
2478 TYPE_MIN_VALUE (gnu_base_index_base_type
));
2479 tree gnu_base_base_max
2480 = convert (sizetype
,
2481 TYPE_MAX_VALUE (gnu_base_index_base_type
));
2483 if (!CONTAINS_PLACEHOLDER_P (gnu_min
)
2484 || !(TREE_CODE (gnu_base_min
) == INTEGER_CST
2485 && !TREE_OVERFLOW (gnu_base_min
)))
2486 gnu_base_min
= gnu_min
;
2488 if (!CONTAINS_PLACEHOLDER_P (gnu_max
)
2489 || !(TREE_CODE (gnu_base_max
) == INTEGER_CST
2490 && !TREE_OVERFLOW (gnu_base_max
)))
2491 gnu_base_max
= gnu_max
;
2493 if ((TREE_CODE (gnu_base_min
) == INTEGER_CST
2494 && TREE_OVERFLOW (gnu_base_min
))
2495 || operand_equal_p (gnu_base_min
, gnu_base_base_min
, 0)
2496 || (TREE_CODE (gnu_base_max
) == INTEGER_CST
2497 && TREE_OVERFLOW (gnu_base_max
))
2498 || operand_equal_p (gnu_base_max
, gnu_base_base_max
, 0))
2499 gnu_max_size
= NULL_TREE
;
2503 = size_binop (MAX_EXPR
,
2504 size_binop (PLUS_EXPR
, size_one_node
,
2505 size_binop (MINUS_EXPR
,
2510 if (TREE_CODE (gnu_this_max
) == INTEGER_CST
2511 && TREE_OVERFLOW (gnu_this_max
))
2512 gnu_max_size
= NULL_TREE
;
2515 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2519 /* We need special types for debugging information to point to
2520 the index types if they have variable bounds, are not integer
2521 types, are biased or are wider than sizetype. */
2522 if (!integer_onep (gnu_orig_min
)
2523 || TREE_CODE (gnu_orig_max
) != INTEGER_CST
2524 || TREE_CODE (gnu_index_type
) != INTEGER_TYPE
2525 || (TREE_TYPE (gnu_index_type
)
2526 && TREE_CODE (TREE_TYPE (gnu_index_type
))
2528 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type
)
2529 || compare_tree_int (rm_size (gnu_index_type
),
2530 TYPE_PRECISION (sizetype
)) > 0)
2531 need_index_type_struct
= true;
2534 /* Then flatten: create the array of arrays. For an array type
2535 used to implement a packed array, get the component type from
2536 the original array type since the representation clauses that
2537 can affect it are on the latter. */
2538 if (Is_Packed_Array_Type (gnat_entity
)
2539 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
2541 gnu_type
= gnat_to_gnu_type (Original_Array_Type (gnat_entity
));
2542 for (index
= ndim
- 1; index
>= 0; index
--)
2543 gnu_type
= TREE_TYPE (gnu_type
);
2545 /* One of the above calls might have caused us to be elaborated,
2546 so don't blow up if so. */
2547 if (present_gnu_tree (gnat_entity
))
2549 maybe_present
= true;
2555 gnu_type
= gnat_to_gnu_component_type (gnat_entity
, definition
,
2558 /* One of the above calls might have caused us to be elaborated,
2559 so don't blow up if so. */
2560 if (present_gnu_tree (gnat_entity
))
2562 maybe_present
= true;
2567 /* Compute the maximum size of the array in units and bits. */
2570 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2571 TYPE_SIZE_UNIT (gnu_type
));
2572 gnu_max_size
= size_binop (MULT_EXPR
,
2573 convert (bitsizetype
, gnu_max_size
),
2574 TYPE_SIZE (gnu_type
));
2577 gnu_max_size_unit
= NULL_TREE
;
2579 /* Now build the array type. */
2580 for (index
= ndim
- 1; index
>= 0; index
--)
2582 gnu_type
= build_nonshared_array_type (gnu_type
,
2583 gnu_index_types
[index
]);
2584 TYPE_MULTI_ARRAY_P (gnu_type
) = (index
> 0);
2585 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2586 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2589 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2590 TYPE_STUB_DECL (gnu_type
)
2591 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
2593 /* If we are at file level and this is a multi-dimensional array,
2594 we need to make a variable corresponding to the stride of the
2595 inner dimensions. */
2596 if (global_bindings_p () && ndim
> 1)
2598 tree gnu_st_name
= get_identifier ("ST");
2601 for (gnu_arr_type
= TREE_TYPE (gnu_type
);
2602 TREE_CODE (gnu_arr_type
) == ARRAY_TYPE
;
2603 gnu_arr_type
= TREE_TYPE (gnu_arr_type
),
2604 gnu_st_name
= concat_name (gnu_st_name
, "ST"))
2606 tree eltype
= TREE_TYPE (gnu_arr_type
);
2608 TYPE_SIZE (gnu_arr_type
)
2609 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type
),
2610 gnat_entity
, gnu_st_name
,
2613 /* ??? For now, store the size as a multiple of the
2614 alignment of the element type in bytes so that we
2615 can see the alignment from the tree. */
2616 TYPE_SIZE_UNIT (gnu_arr_type
)
2617 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type
),
2619 concat_name (gnu_st_name
, "A_U"),
2621 TYPE_ALIGN (eltype
));
2623 /* ??? create_type_decl is not invoked on the inner types so
2624 the MULT_EXPR node built above will never be marked. */
2625 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type
));
2629 /* If we need to write out a record type giving the names of the
2630 bounds for debugging purposes, do it now and make the record
2631 type a parallel type. This is not needed for a packed array
2632 since the bounds are conveyed by the original array type. */
2633 if (need_index_type_struct
2635 && !Is_Packed_Array_Type (gnat_entity
))
2637 tree gnu_bound_rec
= make_node (RECORD_TYPE
);
2638 tree gnu_field_list
= NULL_TREE
;
2641 TYPE_NAME (gnu_bound_rec
)
2642 = create_concat_name (gnat_entity
, "XA");
2644 for (index
= ndim
- 1; index
>= 0; index
--)
2646 tree gnu_index
= TYPE_INDEX_TYPE (gnu_index_types
[index
]);
2647 tree gnu_index_name
= TYPE_NAME (gnu_index
);
2649 if (TREE_CODE (gnu_index_name
) == TYPE_DECL
)
2650 gnu_index_name
= DECL_NAME (gnu_index_name
);
2652 /* Make sure to reference the types themselves, and not just
2653 their names, as the debugger may fall back on them. */
2654 gnu_field
= create_field_decl (gnu_index_name
, gnu_index
,
2655 gnu_bound_rec
, NULL_TREE
,
2657 DECL_CHAIN (gnu_field
) = gnu_field_list
;
2658 gnu_field_list
= gnu_field
;
2661 finish_record_type (gnu_bound_rec
, gnu_field_list
, 0, true);
2662 add_parallel_type (gnu_type
, gnu_bound_rec
);
2665 /* If this is a packed array type, make the original array type a
2666 parallel type. Otherwise, do it for the base array type if it
2667 isn't artificial to make sure it is kept in the debug info. */
2670 if (Is_Packed_Array_Type (gnat_entity
)
2671 && present_gnu_tree (Original_Array_Type (gnat_entity
)))
2672 add_parallel_type (gnu_type
,
2674 (Original_Array_Type (gnat_entity
)));
2678 = gnat_to_gnu_entity (Etype (gnat_entity
), NULL_TREE
, 0);
2679 if (!DECL_ARTIFICIAL (gnu_base_decl
))
2680 add_parallel_type (gnu_type
,
2681 TREE_TYPE (TREE_TYPE (gnu_base_decl
)));
2685 TYPE_CONVENTION_FORTRAN_P (gnu_type
) = convention_fortran_p
;
2686 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
)
2687 = (Is_Packed_Array_Type (gnat_entity
)
2688 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)));
2690 /* If the size is self-referential and the maximum size doesn't
2691 overflow, use it. */
2692 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
2694 && !(TREE_CODE (gnu_max_size
) == INTEGER_CST
2695 && TREE_OVERFLOW (gnu_max_size
))
2696 && !(TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2697 && TREE_OVERFLOW (gnu_max_size_unit
)))
2699 TYPE_SIZE (gnu_type
) = size_binop (MIN_EXPR
, gnu_max_size
,
2700 TYPE_SIZE (gnu_type
));
2701 TYPE_SIZE_UNIT (gnu_type
)
2702 = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2703 TYPE_SIZE_UNIT (gnu_type
));
2706 /* Set our alias set to that of our base type. This gives all
2707 array subtypes the same alias set. */
2708 relate_alias_sets (gnu_type
, gnu_base_type
, ALIAS_SET_COPY
);
2710 /* If this is a packed type, make this type the same as the packed
2711 array type, but do some adjusting in the type first. */
2712 if (Present (Packed_Array_Type (gnat_entity
)))
2714 Entity_Id gnat_index
;
2717 /* First finish the type we had been making so that we output
2718 debugging information for it. */
2719 if (Treat_As_Volatile (gnat_entity
))
2721 = build_qualified_type (gnu_type
,
2722 TYPE_QUALS (gnu_type
)
2723 | TYPE_QUAL_VOLATILE
);
2725 /* Make it artificial only if the base type was artificial too.
2726 That's sort of "morally" true and will make it possible for
2727 the debugger to look it up by name in DWARF, which is needed
2728 in order to decode the packed array type. */
2730 = create_type_decl (gnu_entity_name
, gnu_type
, attr_list
,
2731 !Comes_From_Source (Etype (gnat_entity
))
2732 && !Comes_From_Source (gnat_entity
),
2733 debug_info_p
, gnat_entity
);
2735 /* Save it as our equivalent in case the call below elaborates
2737 save_gnu_tree (gnat_entity
, gnu_decl
, false);
2739 gnu_decl
= gnat_to_gnu_entity (Packed_Array_Type (gnat_entity
),
2741 this_made_decl
= true;
2742 gnu_type
= TREE_TYPE (gnu_decl
);
2743 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
2745 gnu_inner
= gnu_type
;
2746 while (TREE_CODE (gnu_inner
) == RECORD_TYPE
2747 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner
)
2748 || TYPE_PADDING_P (gnu_inner
)))
2749 gnu_inner
= TREE_TYPE (TYPE_FIELDS (gnu_inner
));
2751 /* We need to attach the index type to the type we just made so
2752 that the actual bounds can later be put into a template. */
2753 if ((TREE_CODE (gnu_inner
) == ARRAY_TYPE
2754 && !TYPE_ACTUAL_BOUNDS (gnu_inner
))
2755 || (TREE_CODE (gnu_inner
) == INTEGER_TYPE
2756 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
)))
2758 if (TREE_CODE (gnu_inner
) == INTEGER_TYPE
)
2760 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2761 TYPE_MODULUS for modular types so we make an extra
2762 subtype if necessary. */
2763 if (TYPE_MODULAR_P (gnu_inner
))
2766 = make_unsigned_type (TYPE_PRECISION (gnu_inner
));
2767 TREE_TYPE (gnu_subtype
) = gnu_inner
;
2768 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
2769 SET_TYPE_RM_MIN_VALUE (gnu_subtype
,
2770 TYPE_MIN_VALUE (gnu_inner
));
2771 SET_TYPE_RM_MAX_VALUE (gnu_subtype
,
2772 TYPE_MAX_VALUE (gnu_inner
));
2773 gnu_inner
= gnu_subtype
;
2776 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
) = 1;
2778 #ifdef ENABLE_CHECKING
2779 /* Check for other cases of overloading. */
2780 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner
));
2784 for (gnat_index
= First_Index (gnat_entity
);
2785 Present (gnat_index
);
2786 gnat_index
= Next_Index (gnat_index
))
2787 SET_TYPE_ACTUAL_BOUNDS
2789 tree_cons (NULL_TREE
,
2790 get_unpadded_type (Etype (gnat_index
)),
2791 TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2793 if (Convention (gnat_entity
) != Convention_Fortran
)
2794 SET_TYPE_ACTUAL_BOUNDS
2795 (gnu_inner
, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2797 if (TREE_CODE (gnu_type
) == RECORD_TYPE
2798 && TYPE_JUSTIFIED_MODULAR_P (gnu_type
))
2799 TREE_TYPE (TYPE_FIELDS (gnu_type
)) = gnu_inner
;
2804 /* Abort if packed array with no Packed_Array_Type field set. */
2805 gcc_assert (!Is_Packed (gnat_entity
));
2809 case E_String_Literal_Subtype
:
2810 /* Create the type for a string literal. */
2812 Entity_Id gnat_full_type
2813 = (IN (Ekind (Etype (gnat_entity
)), Private_Kind
)
2814 && Present (Full_View (Etype (gnat_entity
)))
2815 ? Full_View (Etype (gnat_entity
)) : Etype (gnat_entity
));
2816 tree gnu_string_type
= get_unpadded_type (gnat_full_type
);
2817 tree gnu_string_array_type
2818 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type
))));
2819 tree gnu_string_index_type
2820 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2821 (TYPE_DOMAIN (gnu_string_array_type
))));
2822 tree gnu_lower_bound
2823 = convert (gnu_string_index_type
,
2824 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity
)));
2826 = UI_To_gnu (String_Literal_Length (gnat_entity
),
2827 gnu_string_index_type
);
2828 tree gnu_upper_bound
2829 = build_binary_op (PLUS_EXPR
, gnu_string_index_type
,
2831 int_const_binop (MINUS_EXPR
, gnu_length
,
2834 = create_index_type (convert (sizetype
, gnu_lower_bound
),
2835 convert (sizetype
, gnu_upper_bound
),
2836 create_range_type (gnu_string_index_type
,
2842 = build_nonshared_array_type (gnat_to_gnu_type
2843 (Component_Type (gnat_entity
)),
2845 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2846 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2847 relate_alias_sets (gnu_type
, gnu_string_type
, ALIAS_SET_COPY
);
2851 /* Record Types and Subtypes
2853 The following fields are defined on record types:
2855 Has_Discriminants True if the record has discriminants
2856 First_Discriminant Points to head of list of discriminants
2857 First_Entity Points to head of list of fields
2858 Is_Tagged_Type True if the record is tagged
2860 Implementation of Ada records and discriminated records:
2862 A record type definition is transformed into the equivalent of a C
2863 struct definition. The fields that are the discriminants which are
2864 found in the Full_Type_Declaration node and the elements of the
2865 Component_List found in the Record_Type_Definition node. The
2866 Component_List can be a recursive structure since each Variant of
2867 the Variant_Part of the Component_List has a Component_List.
2869 Processing of a record type definition comprises starting the list of
2870 field declarations here from the discriminants and the calling the
2871 function components_to_record to add the rest of the fields from the
2872 component list and return the gnu type node. The function
2873 components_to_record will call itself recursively as it traverses
2877 if (Has_Complex_Representation (gnat_entity
))
2880 = build_complex_type
2882 (Etype (Defining_Entity
2883 (First (Component_Items
2886 (Declaration_Node (gnat_entity
)))))))));
2892 Node_Id full_definition
= Declaration_Node (gnat_entity
);
2893 Node_Id record_definition
= Type_Definition (full_definition
);
2894 Entity_Id gnat_field
;
2895 tree gnu_field
, gnu_field_list
= NULL_TREE
, gnu_get_parent
;
2896 /* Set PACKED in keeping with gnat_to_gnu_field. */
2898 = Is_Packed (gnat_entity
)
2900 : Component_Alignment (gnat_entity
) == Calign_Storage_Unit
2902 : (Known_Alignment (gnat_entity
)
2903 || (Strict_Alignment (gnat_entity
)
2904 && Known_RM_Size (gnat_entity
)))
2907 bool has_discr
= Has_Discriminants (gnat_entity
);
2908 bool has_rep
= Has_Specified_Layout (gnat_entity
);
2909 bool all_rep
= has_rep
;
2911 = (Is_Tagged_Type (gnat_entity
)
2912 && Nkind (record_definition
) == N_Derived_Type_Definition
);
2913 bool is_unchecked_union
= Is_Unchecked_Union (gnat_entity
);
2915 /* See if all fields have a rep clause. Stop when we find one
2918 for (gnat_field
= First_Entity (gnat_entity
);
2919 Present (gnat_field
);
2920 gnat_field
= Next_Entity (gnat_field
))
2921 if ((Ekind (gnat_field
) == E_Component
2922 || Ekind (gnat_field
) == E_Discriminant
)
2923 && No (Component_Clause (gnat_field
)))
2929 /* If this is a record extension, go a level further to find the
2930 record definition. Also, verify we have a Parent_Subtype. */
2933 if (!type_annotate_only
2934 || Present (Record_Extension_Part (record_definition
)))
2935 record_definition
= Record_Extension_Part (record_definition
);
2937 gcc_assert (type_annotate_only
2938 || Present (Parent_Subtype (gnat_entity
)));
2941 /* Make a node for the record. If we are not defining the record,
2942 suppress expanding incomplete types. */
2943 gnu_type
= make_node (tree_code_for_record_type (gnat_entity
));
2944 TYPE_NAME (gnu_type
) = gnu_entity_name
;
2945 TYPE_PACKED (gnu_type
) = (packed
!= 0) || has_rep
;
2949 defer_incomplete_level
++;
2950 this_deferred
= true;
2953 /* If both a size and rep clause was specified, put the size in
2954 the record type now so that it can get the proper mode. */
2955 if (has_rep
&& Known_RM_Size (gnat_entity
))
2956 TYPE_SIZE (gnu_type
)
2957 = UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
);
2959 /* Always set the alignment here so that it can be used to
2960 set the mode, if it is making the alignment stricter. If
2961 it is invalid, it will be checked again below. If this is to
2962 be Atomic, choose a default alignment of a word unless we know
2963 the size and it's smaller. */
2964 if (Known_Alignment (gnat_entity
))
2965 TYPE_ALIGN (gnu_type
)
2966 = validate_alignment (Alignment (gnat_entity
), gnat_entity
, 0);
2967 else if (Is_Atomic (gnat_entity
) && Known_Esize (gnat_entity
))
2969 unsigned int size
= UI_To_Int (Esize (gnat_entity
));
2970 TYPE_ALIGN (gnu_type
)
2971 = size
>= BITS_PER_WORD
? BITS_PER_WORD
: ceil_pow2 (size
);
2973 /* If a type needs strict alignment, the minimum size will be the
2974 type size instead of the RM size (see validate_size). Cap the
2975 alignment, lest it causes this type size to become too large. */
2976 else if (Strict_Alignment (gnat_entity
) && Known_RM_Size (gnat_entity
))
2978 unsigned int raw_size
= UI_To_Int (RM_Size (gnat_entity
));
2979 unsigned int raw_align
= raw_size
& -raw_size
;
2980 if (raw_align
< BIGGEST_ALIGNMENT
)
2981 TYPE_ALIGN (gnu_type
) = raw_align
;
2984 TYPE_ALIGN (gnu_type
) = 0;
2986 /* If we have a Parent_Subtype, make a field for the parent. If
2987 this record has rep clauses, force the position to zero. */
2988 if (Present (Parent_Subtype (gnat_entity
)))
2990 Entity_Id gnat_parent
= Parent_Subtype (gnat_entity
);
2991 tree gnu_dummy_parent_type
= make_node (RECORD_TYPE
);
2994 /* A major complexity here is that the parent subtype will
2995 reference our discriminants in its Discriminant_Constraint
2996 list. But those must reference the parent component of this
2997 record which is of the parent subtype we have not built yet!
2998 To break the circle we first build a dummy COMPONENT_REF which
2999 represents the "get to the parent" operation and initialize
3000 each of those discriminants to a COMPONENT_REF of the above
3001 dummy parent referencing the corresponding discriminant of the
3002 base type of the parent subtype. */
3003 gnu_get_parent
= build3 (COMPONENT_REF
, gnu_dummy_parent_type
,
3004 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3005 build_decl (input_location
,
3006 FIELD_DECL
, NULL_TREE
,
3007 gnu_dummy_parent_type
),
3011 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3012 Present (gnat_field
);
3013 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3014 if (Present (Corresponding_Discriminant (gnat_field
)))
3017 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3021 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3022 gnu_get_parent
, gnu_field
, NULL_TREE
),
3026 /* Then we build the parent subtype. If it has discriminants but
3027 the type itself has unknown discriminants, this means that it
3028 doesn't contain information about how the discriminants are
3029 derived from those of the ancestor type, so it cannot be used
3030 directly. Instead it is built by cloning the parent subtype
3031 of the underlying record view of the type, for which the above
3032 derivation of discriminants has been made explicit. */
3033 if (Has_Discriminants (gnat_parent
)
3034 && Has_Unknown_Discriminants (gnat_entity
))
3036 Entity_Id gnat_uview
= Underlying_Record_View (gnat_entity
);
3038 /* If we are defining the type, the underlying record
3039 view must already have been elaborated at this point.
3040 Otherwise do it now as its parent subtype cannot be
3041 technically elaborated on its own. */
3043 gcc_assert (present_gnu_tree (gnat_uview
));
3045 gnat_to_gnu_entity (gnat_uview
, NULL_TREE
, 0);
3047 gnu_parent
= gnat_to_gnu_type (Parent_Subtype (gnat_uview
));
3049 /* Substitute the "get to the parent" of the type for that
3050 of its underlying record view in the cloned type. */
3051 for (gnat_field
= First_Stored_Discriminant (gnat_uview
);
3052 Present (gnat_field
);
3053 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3054 if (Present (Corresponding_Discriminant (gnat_field
)))
3056 tree gnu_field
= gnat_to_gnu_field_decl (gnat_field
);
3058 = build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3059 gnu_get_parent
, gnu_field
, NULL_TREE
);
3061 = substitute_in_type (gnu_parent
, gnu_field
, gnu_ref
);
3065 gnu_parent
= gnat_to_gnu_type (gnat_parent
);
3067 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3068 initially built. The discriminants must reference the fields
3069 of the parent subtype and not those of its base type for the
3070 placeholder machinery to properly work. */
3073 /* The actual parent subtype is the full view. */
3074 if (IN (Ekind (gnat_parent
), Private_Kind
))
3076 if (Present (Full_View (gnat_parent
)))
3077 gnat_parent
= Full_View (gnat_parent
);
3079 gnat_parent
= Underlying_Full_View (gnat_parent
);
3082 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3083 Present (gnat_field
);
3084 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3085 if (Present (Corresponding_Discriminant (gnat_field
)))
3087 Entity_Id field
= Empty
;
3088 for (field
= First_Stored_Discriminant (gnat_parent
);
3090 field
= Next_Stored_Discriminant (field
))
3091 if (same_discriminant_p (gnat_field
, field
))
3093 gcc_assert (Present (field
));
3094 TREE_OPERAND (get_gnu_tree (gnat_field
), 1)
3095 = gnat_to_gnu_field_decl (field
);
3099 /* The "get to the parent" COMPONENT_REF must be given its
3101 TREE_TYPE (gnu_get_parent
) = gnu_parent
;
3103 /* ...and reference the _Parent field of this record. */
3105 = create_field_decl (parent_name_id
,
3106 gnu_parent
, gnu_type
,
3108 ? TYPE_SIZE (gnu_parent
) : NULL_TREE
,
3110 ? bitsize_zero_node
: NULL_TREE
,
3112 DECL_INTERNAL_P (gnu_field
) = 1;
3113 TREE_OPERAND (gnu_get_parent
, 1) = gnu_field
;
3114 TYPE_FIELDS (gnu_type
) = gnu_field
;
3117 /* Make the fields for the discriminants and put them into the record
3118 unless it's an Unchecked_Union. */
3120 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3121 Present (gnat_field
);
3122 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3124 /* If this is a record extension and this discriminant is the
3125 renaming of another discriminant, we've handled it above. */
3126 if (Present (Parent_Subtype (gnat_entity
))
3127 && Present (Corresponding_Discriminant (gnat_field
)))
3131 = gnat_to_gnu_field (gnat_field
, gnu_type
, packed
, definition
,
3134 /* Make an expression using a PLACEHOLDER_EXPR from the
3135 FIELD_DECL node just created and link that with the
3136 corresponding GNAT defining identifier. */
3137 save_gnu_tree (gnat_field
,
3138 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3139 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3140 gnu_field
, NULL_TREE
),
3143 if (!is_unchecked_union
)
3145 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3146 gnu_field_list
= gnu_field
;
3150 /* Add the fields into the record type and finish it up. */
3151 components_to_record (gnu_type
, Component_List (record_definition
),
3152 gnu_field_list
, packed
, definition
, false,
3153 all_rep
, is_unchecked_union
,
3154 !Comes_From_Source (gnat_entity
), debug_info_p
,
3155 false, OK_To_Reorder_Components (gnat_entity
),
3156 all_rep
? NULL_TREE
: bitsize_zero_node
, NULL
);
3158 /* If it is passed by reference, force BLKmode to ensure that objects
3159 of this type will always be put in memory. */
3160 if (Is_By_Reference_Type (gnat_entity
))
3161 SET_TYPE_MODE (gnu_type
, BLKmode
);
3163 /* We used to remove the associations of the discriminants and _Parent
3164 for validity checking but we may need them if there's a Freeze_Node
3165 for a subtype used in this record. */
3166 TYPE_VOLATILE (gnu_type
) = Treat_As_Volatile (gnat_entity
);
3168 /* Fill in locations of fields. */
3169 annotate_rep (gnat_entity
, gnu_type
);
3171 /* If there are any entities in the chain corresponding to components
3172 that we did not elaborate, ensure we elaborate their types if they
3174 for (gnat_temp
= First_Entity (gnat_entity
);
3175 Present (gnat_temp
);
3176 gnat_temp
= Next_Entity (gnat_temp
))
3177 if ((Ekind (gnat_temp
) == E_Component
3178 || Ekind (gnat_temp
) == E_Discriminant
)
3179 && Is_Itype (Etype (gnat_temp
))
3180 && !present_gnu_tree (gnat_temp
))
3181 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
3183 /* If this is a record type associated with an exception definition,
3184 equate its fields to those of the standard exception type. This
3185 will make it possible to convert between them. */
3186 if (gnu_entity_name
== exception_data_name_id
)
3189 for (gnu_field
= TYPE_FIELDS (gnu_type
),
3190 gnu_std_field
= TYPE_FIELDS (except_type_node
);
3192 gnu_field
= DECL_CHAIN (gnu_field
),
3193 gnu_std_field
= DECL_CHAIN (gnu_std_field
))
3194 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field
, gnu_std_field
);
3195 gcc_assert (!gnu_std_field
);
3200 case E_Class_Wide_Subtype
:
3201 /* If an equivalent type is present, that is what we should use.
3202 Otherwise, fall through to handle this like a record subtype
3203 since it may have constraints. */
3204 if (gnat_equiv_type
!= gnat_entity
)
3206 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
3207 maybe_present
= true;
3211 /* ... fall through ... */
3213 case E_Record_Subtype
:
3214 /* If Cloned_Subtype is Present it means this record subtype has
3215 identical layout to that type or subtype and we should use
3216 that GCC type for this one. The front end guarantees that
3217 the component list is shared. */
3218 if (Present (Cloned_Subtype (gnat_entity
)))
3220 gnu_decl
= gnat_to_gnu_entity (Cloned_Subtype (gnat_entity
),
3222 maybe_present
= true;
3226 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3227 changing the type, make a new type with each field having the type of
3228 the field in the new subtype but the position computed by transforming
3229 every discriminant reference according to the constraints. We don't
3230 see any difference between private and non-private type here since
3231 derivations from types should have been deferred until the completion
3232 of the private type. */
3235 Entity_Id gnat_base_type
= Implementation_Base_Type (gnat_entity
);
3240 defer_incomplete_level
++;
3241 this_deferred
= true;
3244 gnu_base_type
= gnat_to_gnu_type (gnat_base_type
);
3246 if (present_gnu_tree (gnat_entity
))
3248 maybe_present
= true;
3252 /* If this is a record subtype associated with a dispatch table,
3253 strip the suffix. This is necessary to make sure 2 different
3254 subtypes associated with the imported and exported views of a
3255 dispatch table are properly merged in LTO mode. */
3256 if (Is_Dispatch_Table_Entity (gnat_entity
))
3259 Get_Encoded_Name (gnat_entity
);
3260 p
= strchr (Name_Buffer
, '_');
3262 strcpy (p
+2, "dtS");
3263 gnu_entity_name
= get_identifier (Name_Buffer
);
3266 /* When the subtype has discriminants and these discriminants affect
3267 the initial shape it has inherited, factor them in. But for an
3268 Unchecked_Union (it must be an Itype), just return the type.
3269 We can't just test Is_Constrained because private subtypes without
3270 discriminants of types with discriminants with default expressions
3271 are Is_Constrained but aren't constrained! */
3272 if (IN (Ekind (gnat_base_type
), Record_Kind
)
3273 && !Is_Unchecked_Union (gnat_base_type
)
3274 && !Is_For_Access_Subtype (gnat_entity
)
3275 && Is_Constrained (gnat_entity
)
3276 && Has_Discriminants (gnat_entity
)
3277 && Present (Discriminant_Constraint (gnat_entity
))
3278 && Stored_Constraint (gnat_entity
) != No_Elist
)
3280 VEC(subst_pair
,heap
) *gnu_subst_list
3281 = build_subst_list (gnat_entity
, gnat_base_type
, definition
);
3282 tree gnu_unpad_base_type
, gnu_rep_part
, gnu_variant_part
, t
;
3283 tree gnu_pos_list
, gnu_field_list
= NULL_TREE
;
3284 bool selected_variant
= false;
3285 Entity_Id gnat_field
;
3286 VEC(variant_desc
,heap
) *gnu_variant_list
;
3288 gnu_type
= make_node (RECORD_TYPE
);
3289 TYPE_NAME (gnu_type
) = gnu_entity_name
;
3290 TYPE_PACKED (gnu_type
) = TYPE_PACKED (gnu_base_type
);
3292 /* Set the size, alignment and alias set of the new type to
3293 match that of the old one, doing required substitutions. */
3294 copy_and_substitute_in_size (gnu_type
, gnu_base_type
,
3297 if (TYPE_IS_PADDING_P (gnu_base_type
))
3298 gnu_unpad_base_type
= TREE_TYPE (TYPE_FIELDS (gnu_base_type
));
3300 gnu_unpad_base_type
= gnu_base_type
;
3302 /* Look for a variant part in the base type. */
3303 gnu_variant_part
= get_variant_part (gnu_unpad_base_type
);
3305 /* If there is a variant part, we must compute whether the
3306 constraints statically select a particular variant. If
3307 so, we simply drop the qualified union and flatten the
3308 list of fields. Otherwise we'll build a new qualified
3309 union for the variants that are still relevant. */
3310 if (gnu_variant_part
)
3316 = build_variant_list (TREE_TYPE (gnu_variant_part
),
3317 gnu_subst_list
, NULL
);
3319 /* If all the qualifiers are unconditionally true, the
3320 innermost variant is statically selected. */
3321 selected_variant
= true;
3322 FOR_EACH_VEC_ELT (variant_desc
, gnu_variant_list
, i
, v
)
3323 if (!integer_onep (v
->qual
))
3325 selected_variant
= false;
3329 /* Otherwise, create the new variants. */
3330 if (!selected_variant
)
3331 FOR_EACH_VEC_ELT (variant_desc
, gnu_variant_list
, i
, v
)
3333 tree old_variant
= v
->type
;
3334 tree new_variant
= make_node (RECORD_TYPE
);
3336 = concat_name (DECL_NAME (gnu_variant_part
),
3338 (DECL_NAME (v
->field
)));
3339 TYPE_NAME (new_variant
)
3340 = concat_name (TYPE_NAME (gnu_type
),
3341 IDENTIFIER_POINTER (suffix
));
3342 copy_and_substitute_in_size (new_variant
, old_variant
,
3344 v
->new_type
= new_variant
;
3349 gnu_variant_list
= NULL
;
3350 selected_variant
= false;
3354 = build_position_list (gnu_unpad_base_type
,
3355 gnu_variant_list
&& !selected_variant
,
3356 size_zero_node
, bitsize_zero_node
,
3357 BIGGEST_ALIGNMENT
, NULL_TREE
);
3359 for (gnat_field
= First_Entity (gnat_entity
);
3360 Present (gnat_field
);
3361 gnat_field
= Next_Entity (gnat_field
))
3362 if ((Ekind (gnat_field
) == E_Component
3363 || Ekind (gnat_field
) == E_Discriminant
)
3364 && !(Present (Corresponding_Discriminant (gnat_field
))
3365 && Is_Tagged_Type (gnat_base_type
))
3366 && Underlying_Type (Scope (Original_Record_Component
3370 Name_Id gnat_name
= Chars (gnat_field
);
3371 Entity_Id gnat_old_field
3372 = Original_Record_Component (gnat_field
);
3374 = gnat_to_gnu_field_decl (gnat_old_field
);
3375 tree gnu_context
= DECL_CONTEXT (gnu_old_field
);
3376 tree gnu_field
, gnu_field_type
, gnu_size
;
3377 tree gnu_cont_type
, gnu_last
= NULL_TREE
;
3379 /* If the type is the same, retrieve the GCC type from the
3380 old field to take into account possible adjustments. */
3381 if (Etype (gnat_field
) == Etype (gnat_old_field
))
3382 gnu_field_type
= TREE_TYPE (gnu_old_field
);
3384 gnu_field_type
= gnat_to_gnu_type (Etype (gnat_field
));
3386 /* If there was a component clause, the field types must be
3387 the same for the type and subtype, so copy the data from
3388 the old field to avoid recomputation here. Also if the
3389 field is justified modular and the optimization in
3390 gnat_to_gnu_field was applied. */
3391 if (Present (Component_Clause (gnat_old_field
))
3392 || (TREE_CODE (gnu_field_type
) == RECORD_TYPE
3393 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type
)
3394 && TREE_TYPE (TYPE_FIELDS (gnu_field_type
))
3395 == TREE_TYPE (gnu_old_field
)))
3397 gnu_size
= DECL_SIZE (gnu_old_field
);
3398 gnu_field_type
= TREE_TYPE (gnu_old_field
);
3401 /* If the old field was packed and of constant size, we
3402 have to get the old size here, as it might differ from
3403 what the Etype conveys and the latter might overlap
3404 onto the following field. Try to arrange the type for
3405 possible better packing along the way. */
3406 else if (DECL_PACKED (gnu_old_field
)
3407 && TREE_CODE (DECL_SIZE (gnu_old_field
))
3410 gnu_size
= DECL_SIZE (gnu_old_field
);
3411 if (RECORD_OR_UNION_TYPE_P (gnu_field_type
)
3412 && !TYPE_FAT_POINTER_P (gnu_field_type
)
3413 && host_integerp (TYPE_SIZE (gnu_field_type
), 1))
3415 = make_packable_type (gnu_field_type
, true);
3419 gnu_size
= TYPE_SIZE (gnu_field_type
);
3421 /* If the context of the old field is the base type or its
3422 REP part (if any), put the field directly in the new
3423 type; otherwise look up the context in the variant list
3424 and put the field either in the new type if there is a
3425 selected variant or in one of the new variants. */
3426 if (gnu_context
== gnu_unpad_base_type
3427 || ((gnu_rep_part
= get_rep_part (gnu_unpad_base_type
))
3428 && gnu_context
== TREE_TYPE (gnu_rep_part
)))
3429 gnu_cont_type
= gnu_type
;
3436 FOR_EACH_VEC_ELT (variant_desc
, gnu_variant_list
, i
, v
)
3437 if (gnu_context
== v
->type
3438 || ((gnu_rep_part
= get_rep_part (v
->type
))
3439 && gnu_context
== TREE_TYPE (gnu_rep_part
)))
3446 if (selected_variant
)
3447 gnu_cont_type
= gnu_type
;
3449 gnu_cont_type
= v
->new_type
;
3452 /* The front-end may pass us "ghost" components if
3453 it fails to recognize that a constrained subtype
3454 is statically constrained. Discard them. */
3458 /* Now create the new field modeled on the old one. */
3460 = create_field_decl_from (gnu_old_field
, gnu_field_type
,
3461 gnu_cont_type
, gnu_size
,
3462 gnu_pos_list
, gnu_subst_list
);
3464 /* Put it in one of the new variants directly. */
3465 if (gnu_cont_type
!= gnu_type
)
3467 DECL_CHAIN (gnu_field
) = TYPE_FIELDS (gnu_cont_type
);
3468 TYPE_FIELDS (gnu_cont_type
) = gnu_field
;
3471 /* To match the layout crafted in components_to_record,
3472 if this is the _Tag or _Parent field, put it before
3473 any other fields. */
3474 else if (gnat_name
== Name_uTag
3475 || gnat_name
== Name_uParent
)
3476 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
3478 /* Similarly, if this is the _Controller field, put
3479 it before the other fields except for the _Tag or
3481 else if (gnat_name
== Name_uController
&& gnu_last
)
3483 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
3484 DECL_CHAIN (gnu_last
) = gnu_field
;
3487 /* Otherwise, if this is a regular field, put it after
3488 the other fields. */
3491 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3492 gnu_field_list
= gnu_field
;
3494 gnu_last
= gnu_field
;
3497 save_gnu_tree (gnat_field
, gnu_field
, false);
3500 /* If there is a variant list and no selected variant, we need
3501 to create the nest of variant parts from the old nest. */
3502 if (gnu_variant_list
&& !selected_variant
)
3504 tree new_variant_part
3505 = create_variant_part_from (gnu_variant_part
,
3506 gnu_variant_list
, gnu_type
,
3507 gnu_pos_list
, gnu_subst_list
);
3508 DECL_CHAIN (new_variant_part
) = gnu_field_list
;
3509 gnu_field_list
= new_variant_part
;
3512 /* Now go through the entities again looking for Itypes that
3513 we have not elaborated but should (e.g., Etypes of fields
3514 that have Original_Components). */
3515 for (gnat_field
= First_Entity (gnat_entity
);
3516 Present (gnat_field
); gnat_field
= Next_Entity (gnat_field
))
3517 if ((Ekind (gnat_field
) == E_Discriminant
3518 || Ekind (gnat_field
) == E_Component
)
3519 && !present_gnu_tree (Etype (gnat_field
)))
3520 gnat_to_gnu_entity (Etype (gnat_field
), NULL_TREE
, 0);
3522 /* Do not emit debug info for the type yet since we're going to
3524 finish_record_type (gnu_type
, nreverse (gnu_field_list
), 2,
3527 /* See the E_Record_Type case for the rationale. */
3528 if (Is_By_Reference_Type (gnat_entity
))
3529 SET_TYPE_MODE (gnu_type
, BLKmode
);
3531 compute_record_mode (gnu_type
);
3533 TYPE_VOLATILE (gnu_type
) = Treat_As_Volatile (gnat_entity
);
3535 /* Fill in locations of fields. */
3536 annotate_rep (gnat_entity
, gnu_type
);
3538 /* If debugging information is being written for the type, write
3539 a record that shows what we are a subtype of and also make a
3540 variable that indicates our size, if still variable. */
3543 tree gnu_subtype_marker
= make_node (RECORD_TYPE
);
3544 tree gnu_unpad_base_name
= TYPE_NAME (gnu_unpad_base_type
);
3545 tree gnu_size_unit
= TYPE_SIZE_UNIT (gnu_type
);
3547 if (TREE_CODE (gnu_unpad_base_name
) == TYPE_DECL
)
3548 gnu_unpad_base_name
= DECL_NAME (gnu_unpad_base_name
);
3550 TYPE_NAME (gnu_subtype_marker
)
3551 = create_concat_name (gnat_entity
, "XVS");
3552 finish_record_type (gnu_subtype_marker
,
3553 create_field_decl (gnu_unpad_base_name
,
3554 build_reference_type
3555 (gnu_unpad_base_type
),
3557 NULL_TREE
, NULL_TREE
,
3561 add_parallel_type (gnu_type
, gnu_subtype_marker
);
3564 && TREE_CODE (gnu_size_unit
) != INTEGER_CST
3565 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit
))
3566 TYPE_SIZE_UNIT (gnu_subtype_marker
)
3567 = create_var_decl (create_concat_name (gnat_entity
,
3569 NULL_TREE
, sizetype
, gnu_size_unit
,
3570 false, false, false, false, NULL
,
3574 VEC_free (variant_desc
, heap
, gnu_variant_list
);
3575 VEC_free (subst_pair
, heap
, gnu_subst_list
);
3577 /* Now we can finalize it. */
3578 rest_of_record_type_compilation (gnu_type
);
3581 /* Otherwise, go down all the components in the new type and make
3582 them equivalent to those in the base type. */
3585 gnu_type
= gnu_base_type
;
3587 for (gnat_temp
= First_Entity (gnat_entity
);
3588 Present (gnat_temp
);
3589 gnat_temp
= Next_Entity (gnat_temp
))
3590 if ((Ekind (gnat_temp
) == E_Discriminant
3591 && !Is_Unchecked_Union (gnat_base_type
))
3592 || Ekind (gnat_temp
) == E_Component
)
3593 save_gnu_tree (gnat_temp
,
3594 gnat_to_gnu_field_decl
3595 (Original_Record_Component (gnat_temp
)),
3601 case E_Access_Subprogram_Type
:
3602 /* Use the special descriptor type for dispatch tables if needed,
3603 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3604 Note that we are only required to do so for static tables in
3605 order to be compatible with the C++ ABI, but Ada 2005 allows
3606 to extend library level tagged types at the local level so
3607 we do it in the non-static case as well. */
3608 if (TARGET_VTABLE_USES_DESCRIPTORS
3609 && Is_Dispatch_Table_Entity (gnat_entity
))
3611 gnu_type
= fdesc_type_node
;
3612 gnu_size
= TYPE_SIZE (gnu_type
);
3616 /* ... fall through ... */
3618 case E_Anonymous_Access_Subprogram_Type
:
3619 /* If we are not defining this entity, and we have incomplete
3620 entities being processed above us, make a dummy type and
3621 fill it in later. */
3622 if (!definition
&& defer_incomplete_level
!= 0)
3624 struct incomplete
*p
= XNEW (struct incomplete
);
3627 = build_pointer_type
3628 (make_dummy_type (Directly_Designated_Type (gnat_entity
)));
3629 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
, attr_list
,
3630 !Comes_From_Source (gnat_entity
),
3631 debug_info_p
, gnat_entity
);
3632 this_made_decl
= true;
3633 gnu_type
= TREE_TYPE (gnu_decl
);
3634 save_gnu_tree (gnat_entity
, gnu_decl
, false);
3637 p
->old_type
= TREE_TYPE (gnu_type
);
3638 p
->full_type
= Directly_Designated_Type (gnat_entity
);
3639 p
->next
= defer_incomplete_list
;
3640 defer_incomplete_list
= p
;
3644 /* ... fall through ... */
3646 case E_Allocator_Type
:
3648 case E_Access_Attribute_Type
:
3649 case E_Anonymous_Access_Type
:
3650 case E_General_Access_Type
:
3652 /* The designated type and its equivalent type for gigi. */
3653 Entity_Id gnat_desig_type
= Directly_Designated_Type (gnat_entity
);
3654 Entity_Id gnat_desig_equiv
= Gigi_Equivalent_Type (gnat_desig_type
);
3655 /* Whether it comes from a limited with. */
3656 bool is_from_limited_with
3657 = (IN (Ekind (gnat_desig_equiv
), Incomplete_Kind
)
3658 && From_With_Type (gnat_desig_equiv
));
3659 /* The "full view" of the designated type. If this is an incomplete
3660 entity from a limited with, treat its non-limited view as the full
3661 view. Otherwise, if this is an incomplete or private type, use the
3662 full view. In the former case, we might point to a private type,
3663 in which case, we need its full view. Also, we want to look at the
3664 actual type used for the representation, so this takes a total of
3666 Entity_Id gnat_desig_full_direct_first
3667 = (is_from_limited_with
3668 ? Non_Limited_View (gnat_desig_equiv
)
3669 : (IN (Ekind (gnat_desig_equiv
), Incomplete_Or_Private_Kind
)
3670 ? Full_View (gnat_desig_equiv
) : Empty
));
3671 Entity_Id gnat_desig_full_direct
3672 = ((is_from_limited_with
3673 && Present (gnat_desig_full_direct_first
)
3674 && IN (Ekind (gnat_desig_full_direct_first
), Private_Kind
))
3675 ? Full_View (gnat_desig_full_direct_first
)
3676 : gnat_desig_full_direct_first
);
3677 Entity_Id gnat_desig_full
3678 = Gigi_Equivalent_Type (gnat_desig_full_direct
);
3679 /* The type actually used to represent the designated type, either
3680 gnat_desig_full or gnat_desig_equiv. */
3681 Entity_Id gnat_desig_rep
;
3682 /* True if this is a pointer to an unconstrained array. */
3683 bool is_unconstrained_array
;
3684 /* We want to know if we'll be seeing the freeze node for any
3685 incomplete type we may be pointing to. */
3687 = (Present (gnat_desig_full
)
3688 ? In_Extended_Main_Code_Unit (gnat_desig_full
)
3689 : In_Extended_Main_Code_Unit (gnat_desig_type
));
3690 /* True if we make a dummy type here. */
3691 bool made_dummy
= false;
3692 /* The mode to be used for the pointer type. */
3693 enum machine_mode p_mode
= mode_for_size (esize
, MODE_INT
, 0);
3694 /* The GCC type used for the designated type. */
3695 tree gnu_desig_type
= NULL_TREE
;
3697 if (!targetm
.valid_pointer_mode (p_mode
))
3700 /* If either the designated type or its full view is an unconstrained
3701 array subtype, replace it with the type it's a subtype of. This
3702 avoids problems with multiple copies of unconstrained array types.
3703 Likewise, if the designated type is a subtype of an incomplete
3704 record type, use the parent type to avoid order of elaboration
3705 issues. This can lose some code efficiency, but there is no
3707 if (Ekind (gnat_desig_equiv
) == E_Array_Subtype
3708 && !Is_Constrained (gnat_desig_equiv
))
3709 gnat_desig_equiv
= Etype (gnat_desig_equiv
);
3710 if (Present (gnat_desig_full
)
3711 && ((Ekind (gnat_desig_full
) == E_Array_Subtype
3712 && !Is_Constrained (gnat_desig_full
))
3713 || (Ekind (gnat_desig_full
) == E_Record_Subtype
3714 && Ekind (Etype (gnat_desig_full
)) == E_Record_Type
)))
3715 gnat_desig_full
= Etype (gnat_desig_full
);
3717 /* Set the type that's actually the representation of the designated
3718 type and also flag whether we have a unconstrained array. */
3720 = Present (gnat_desig_full
) ? gnat_desig_full
: gnat_desig_equiv
;
3721 is_unconstrained_array
3722 = Is_Array_Type (gnat_desig_rep
) && !Is_Constrained (gnat_desig_rep
);
3724 /* If we are pointing to an incomplete type whose completion is an
3725 unconstrained array, make dummy fat and thin pointer types to it.
3726 Likewise if the type itself is dummy or an unconstrained array. */
3727 if (is_unconstrained_array
3728 && (Present (gnat_desig_full
)
3729 || (present_gnu_tree (gnat_desig_equiv
)
3731 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv
))))
3733 && defer_incomplete_level
!= 0
3734 && !present_gnu_tree (gnat_desig_equiv
))
3736 && is_from_limited_with
3737 && Present (Freeze_Node (gnat_desig_equiv
)))))
3739 if (present_gnu_tree (gnat_desig_rep
))
3740 gnu_desig_type
= TREE_TYPE (get_gnu_tree (gnat_desig_rep
));
3743 gnu_desig_type
= make_dummy_type (gnat_desig_rep
);
3747 /* If the call above got something that has a pointer, the pointer
3748 is our type. This could have happened either because the type
3749 was elaborated or because somebody else executed the code. */
3750 if (!TYPE_POINTER_TO (gnu_desig_type
))
3751 build_dummy_unc_pointer_types (gnat_desig_equiv
, gnu_desig_type
);
3752 gnu_type
= TYPE_POINTER_TO (gnu_desig_type
);
3755 /* If we already know what the full type is, use it. */
3756 else if (Present (gnat_desig_full
)
3757 && present_gnu_tree (gnat_desig_full
))
3758 gnu_desig_type
= TREE_TYPE (get_gnu_tree (gnat_desig_full
));
3760 /* Get the type of the thing we are to point to and build a pointer to
3761 it. If it is a reference to an incomplete or private type with a
3762 full view that is a record, make a dummy type node and get the
3763 actual type later when we have verified it is safe. */
3764 else if ((!in_main_unit
3765 && !present_gnu_tree (gnat_desig_equiv
)
3766 && Present (gnat_desig_full
)
3767 && !present_gnu_tree (gnat_desig_full
)
3768 && Is_Record_Type (gnat_desig_full
))
3769 /* Likewise if we are pointing to a record or array and we are
3770 to defer elaborating incomplete types. We do this as this
3771 access type may be the full view of a private type. Note
3772 that the unconstrained array case is handled above. */
3773 || ((!in_main_unit
|| imported_p
)
3774 && defer_incomplete_level
!= 0
3775 && !present_gnu_tree (gnat_desig_equiv
)
3776 && (Is_Record_Type (gnat_desig_rep
)
3777 || Is_Array_Type (gnat_desig_rep
)))
3778 /* If this is a reference from a limited_with type back to our
3779 main unit and there's a freeze node for it, either we have
3780 already processed the declaration and made the dummy type,
3781 in which case we just reuse the latter, or we have not yet,
3782 in which case we make the dummy type and it will be reused
3783 when the declaration is finally processed. In both cases,
3784 the pointer eventually created below will be automatically
3785 adjusted when the freeze node is processed. Note that the
3786 unconstrained array case is handled above. */
3788 && is_from_limited_with
3789 && Present (Freeze_Node (gnat_desig_rep
))))
3791 gnu_desig_type
= make_dummy_type (gnat_desig_equiv
);
3795 /* Otherwise handle the case of a pointer to itself. */
3796 else if (gnat_desig_equiv
== gnat_entity
)
3799 = build_pointer_type_for_mode (void_type_node
, p_mode
,
3800 No_Strict_Aliasing (gnat_entity
));
3801 TREE_TYPE (gnu_type
) = TYPE_POINTER_TO (gnu_type
) = gnu_type
;
3804 /* If expansion is disabled, the equivalent type of a concurrent type
3805 is absent, so build a dummy pointer type. */
3806 else if (type_annotate_only
&& No (gnat_desig_equiv
))
3807 gnu_type
= ptr_void_type_node
;
3809 /* Finally, handle the default case where we can just elaborate our
3812 gnu_desig_type
= gnat_to_gnu_type (gnat_desig_equiv
);
3814 /* It is possible that a call to gnat_to_gnu_type above resolved our
3815 type. If so, just return it. */
3816 if (present_gnu_tree (gnat_entity
))
3818 maybe_present
= true;
3822 /* If we haven't done it yet, build the pointer type the usual way. */
3825 /* Modify the designated type if we are pointing only to constant
3826 objects, but don't do it for unconstrained arrays. */
3827 if (Is_Access_Constant (gnat_entity
)
3828 && TREE_CODE (gnu_desig_type
) != UNCONSTRAINED_ARRAY_TYPE
)
3831 = build_qualified_type
3833 TYPE_QUALS (gnu_desig_type
) | TYPE_QUAL_CONST
);
3835 /* Some extra processing is required if we are building a
3836 pointer to an incomplete type (in the GCC sense). We might
3837 have such a type if we just made a dummy, or directly out
3838 of the call to gnat_to_gnu_type above if we are processing
3839 an access type for a record component designating the
3840 record type itself. */
3841 if (TYPE_MODE (gnu_desig_type
) == VOIDmode
)
3843 /* We must ensure that the pointer to variant we make will
3844 be processed by update_pointer_to when the initial type
3845 is completed. Pretend we made a dummy and let further
3846 processing act as usual. */
3849 /* We must ensure that update_pointer_to will not retrieve
3850 the dummy variant when building a properly qualified
3851 version of the complete type. We take advantage of the
3852 fact that get_qualified_type is requiring TYPE_NAMEs to
3853 match to influence build_qualified_type and then also
3854 update_pointer_to here. */
3855 TYPE_NAME (gnu_desig_type
)
3856 = create_concat_name (gnat_desig_type
, "INCOMPLETE_CST");
3861 = build_pointer_type_for_mode (gnu_desig_type
, p_mode
,
3862 No_Strict_Aliasing (gnat_entity
));
3865 /* If we are not defining this object and we have made a dummy pointer,
3866 save our current definition, evaluate the actual type, and replace
3867 the tentative type we made with the actual one. If we are to defer
3868 actually looking up the actual type, make an entry in the deferred
3869 list. If this is from a limited with, we may have to defer to the
3870 end of the current unit. */
3871 if ((!in_main_unit
|| is_from_limited_with
) && made_dummy
)
3873 tree gnu_old_desig_type
;
3875 if (TYPE_IS_FAT_POINTER_P (gnu_type
))
3877 gnu_old_desig_type
= TYPE_UNCONSTRAINED_ARRAY (gnu_type
);
3878 if (esize
== POINTER_SIZE
)
3879 gnu_type
= build_pointer_type
3880 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type
));
3883 gnu_old_desig_type
= TREE_TYPE (gnu_type
);
3885 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
, attr_list
,
3886 !Comes_From_Source (gnat_entity
),
3887 debug_info_p
, gnat_entity
);
3888 this_made_decl
= true;
3889 gnu_type
= TREE_TYPE (gnu_decl
);
3890 save_gnu_tree (gnat_entity
, gnu_decl
, false);
3893 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3894 update gnu_old_desig_type directly, in which case it will not be
3895 a dummy type any more when we get into update_pointer_to.
3897 This can happen e.g. when the designated type is a record type,
3898 because their elaboration starts with an initial node from
3899 make_dummy_type, which may be the same node as the one we got.
3901 Besides, variants of this non-dummy type might have been created
3902 along the way. update_pointer_to is expected to properly take
3903 care of those situations. */
3904 if (defer_incomplete_level
== 0 && !is_from_limited_with
)
3906 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type
),
3907 gnat_to_gnu_type (gnat_desig_equiv
));
3911 struct incomplete
*p
= XNEW (struct incomplete
);
3912 struct incomplete
**head
3913 = (is_from_limited_with
3914 ? &defer_limited_with
: &defer_incomplete_list
);
3915 p
->old_type
= gnu_old_desig_type
;
3916 p
->full_type
= gnat_desig_equiv
;
3924 case E_Access_Protected_Subprogram_Type
:
3925 case E_Anonymous_Access_Protected_Subprogram_Type
:
3926 if (type_annotate_only
&& No (gnat_equiv_type
))
3927 gnu_type
= ptr_void_type_node
;
3930 /* The run-time representation is the equivalent type. */
3931 gnu_type
= gnat_to_gnu_type (gnat_equiv_type
);
3932 maybe_present
= true;
3935 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
3936 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
3937 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
)))
3938 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity
))))
3939 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
3944 case E_Access_Subtype
:
3946 /* We treat this as identical to its base type; any constraint is
3947 meaningful only to the front-end.
3949 The designated type must be elaborated as well, if it does
3950 not have its own freeze node. Designated (sub)types created
3951 for constrained components of records with discriminants are
3952 not frozen by the front-end and thus not elaborated by gigi,
3953 because their use may appear before the base type is frozen,
3954 and because it is not clear that they are needed anywhere in
3955 gigi. With the current model, there is no correct place where
3956 they could be elaborated. */
3958 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
3959 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
3960 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
3961 && Is_Frozen (Directly_Designated_Type (gnat_entity
))
3962 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
))))
3964 /* If we are not defining this entity, and we have incomplete
3965 entities being processed above us, make a dummy type and
3966 elaborate it later. */
3967 if (!definition
&& defer_incomplete_level
!= 0)
3969 struct incomplete
*p
= XNEW (struct incomplete
);
3972 = make_dummy_type (Directly_Designated_Type (gnat_entity
));
3973 p
->full_type
= Directly_Designated_Type (gnat_entity
);
3974 p
->next
= defer_incomplete_list
;
3975 defer_incomplete_list
= p
;
3977 else if (!IN (Ekind (Base_Type
3978 (Directly_Designated_Type (gnat_entity
))),
3979 Incomplete_Or_Private_Kind
))
3980 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
3984 maybe_present
= true;
3987 /* Subprogram Entities
3989 The following access functions are defined for subprograms:
3991 Etype Return type or Standard_Void_Type.
3992 First_Formal The first formal parameter.
3993 Is_Imported Indicates that the subprogram has appeared in
3994 an INTERFACE or IMPORT pragma. For now we
3995 assume that the external language is C.
3996 Is_Exported Likewise but for an EXPORT pragma.
3997 Is_Inlined True if the subprogram is to be inlined.
3999 Each parameter is first checked by calling must_pass_by_ref on its
4000 type to determine if it is passed by reference. For parameters which
4001 are copied in, if they are Ada In Out or Out parameters, their return
4002 value becomes part of a record which becomes the return type of the
4003 function (C function - note that this applies only to Ada procedures
4004 so there is no Ada return type). Additional code to store back the
4005 parameters will be generated on the caller side. This transformation
4006 is done here, not in the front-end.
4008 The intended result of the transformation can be seen from the
4009 equivalent source rewritings that follow:
4011 struct temp {int a,b};
4012 procedure P (A,B: In Out ...) is temp P (int A,B)
4015 end P; return {A,B};
4022 For subprogram types we need to perform mainly the same conversions to
4023 GCC form that are needed for procedures and function declarations. The
4024 only difference is that at the end, we make a type declaration instead
4025 of a function declaration. */
4027 case E_Subprogram_Type
:
4031 /* The type returned by a function or else Standard_Void_Type for a
4033 Entity_Id gnat_return_type
= Etype (gnat_entity
);
4034 tree gnu_return_type
;
4035 /* The first GCC parameter declaration (a PARM_DECL node). The
4036 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4037 actually is the head of this parameter list. */
4038 tree gnu_param_list
= NULL_TREE
;
4039 /* Likewise for the stub associated with an exported procedure. */
4040 tree gnu_stub_param_list
= NULL_TREE
;
4041 /* Non-null for subprograms containing parameters passed by copy-in
4042 copy-out (Ada In Out or Out parameters not passed by reference),
4043 in which case it is the list of nodes used to specify the values
4044 of the In Out/Out parameters that are returned as a record upon
4045 procedure return. The TREE_PURPOSE of an element of this list is
4046 a field of the record and the TREE_VALUE is the PARM_DECL
4047 corresponding to that field. This list will be saved in the
4048 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4049 tree gnu_cico_list
= NULL_TREE
;
4050 /* List of fields in return type of procedure with copy-in copy-out
4052 tree gnu_field_list
= NULL_TREE
;
4053 /* If an import pragma asks to map this subprogram to a GCC builtin,
4054 this is the builtin DECL node. */
4055 tree gnu_builtin_decl
= NULL_TREE
;
4056 /* For the stub associated with an exported procedure. */
4057 tree gnu_stub_type
= NULL_TREE
, gnu_stub_name
= NULL_TREE
;
4058 tree gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
4059 Entity_Id gnat_param
;
4060 bool inline_flag
= Is_Inlined (gnat_entity
);
4061 bool public_flag
= Is_Public (gnat_entity
) || imported_p
;
4063 = (Is_Public (gnat_entity
) && !definition
) || imported_p
;
4064 bool artificial_flag
= !Comes_From_Source (gnat_entity
);
4065 /* The semantics of "pure" in Ada essentially matches that of "const"
4066 in the back-end. In particular, both properties are orthogonal to
4067 the "nothrow" property if the EH circuitry is explicit in the
4068 internal representation of the back-end. If we are to completely
4069 hide the EH circuitry from it, we need to declare that calls to pure
4070 Ada subprograms that can throw have side effects since they can
4071 trigger an "abnormal" transfer of control flow; thus they can be
4072 neither "const" nor "pure" in the back-end sense. */
4074 = (Exception_Mechanism
== Back_End_Exceptions
4075 && Is_Pure (gnat_entity
));
4076 bool volatile_flag
= No_Return (gnat_entity
);
4077 bool return_by_direct_ref_p
= false;
4078 bool return_by_invisi_ref_p
= false;
4079 bool return_unconstrained_p
= false;
4080 bool has_stub
= false;
4083 /* A parameter may refer to this type, so defer completion of any
4084 incomplete types. */
4085 if (kind
== E_Subprogram_Type
&& !definition
)
4087 defer_incomplete_level
++;
4088 this_deferred
= true;
4091 /* If the subprogram has an alias, it is probably inherited, so
4092 we can use the original one. If the original "subprogram"
4093 is actually an enumeration literal, it may be the first use
4094 of its type, so we must elaborate that type now. */
4095 if (Present (Alias (gnat_entity
)))
4097 if (Ekind (Alias (gnat_entity
)) == E_Enumeration_Literal
)
4098 gnat_to_gnu_entity (Etype (Alias (gnat_entity
)), NULL_TREE
, 0);
4100 gnu_decl
= gnat_to_gnu_entity (Alias (gnat_entity
), gnu_expr
, 0);
4102 /* Elaborate any Itypes in the parameters of this entity. */
4103 for (gnat_temp
= First_Formal_With_Extras (gnat_entity
);
4104 Present (gnat_temp
);
4105 gnat_temp
= Next_Formal_With_Extras (gnat_temp
))
4106 if (Is_Itype (Etype (gnat_temp
)))
4107 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
4112 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4113 corresponding DECL node. Proper generation of calls later on need
4114 proper parameter associations so we don't "break;" here. */
4115 if (Convention (gnat_entity
) == Convention_Intrinsic
4116 && Present (Interface_Name (gnat_entity
)))
4118 gnu_builtin_decl
= builtin_decl_for (gnu_ext_name
);
4120 /* Inability to find the builtin decl most often indicates a
4121 genuine mistake, but imports of unregistered intrinsics are
4122 sometimes issued on purpose to allow hooking in alternate
4123 bodies. We post a warning conditioned on Wshadow in this case,
4124 to let developers be notified on demand without risking false
4125 positives with common default sets of options. */
4127 if (gnu_builtin_decl
== NULL_TREE
&& warn_shadow
)
4128 post_error ("?gcc intrinsic not found for&!", gnat_entity
);
4131 /* ??? What if we don't find the builtin node above ? warn ? err ?
4132 In the current state we neither warn nor err, and calls will just
4133 be handled as for regular subprograms. */
4135 /* Look into the return type and get its associated GCC tree. If it
4136 is not void, compute various flags for the subprogram type. */
4137 if (Ekind (gnat_return_type
) == E_Void
)
4138 gnu_return_type
= void_type_node
;
4141 gnu_return_type
= gnat_to_gnu_type (gnat_return_type
);
4143 /* If this function returns by reference, make the actual return
4144 type the pointer type and make a note of that. */
4145 if (Returns_By_Ref (gnat_entity
))
4147 gnu_return_type
= build_pointer_type (gnu_return_type
);
4148 return_by_direct_ref_p
= true;
4151 /* If we are supposed to return an unconstrained array type, make
4152 the actual return type the fat pointer type. */
4153 else if (TREE_CODE (gnu_return_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4155 gnu_return_type
= TREE_TYPE (gnu_return_type
);
4156 return_unconstrained_p
= true;
4159 /* Likewise, if the return type requires a transient scope, the
4160 return value will be allocated on the secondary stack so the
4161 actual return type is the pointer type. */
4162 else if (Requires_Transient_Scope (gnat_return_type
))
4164 gnu_return_type
= build_pointer_type (gnu_return_type
);
4165 return_unconstrained_p
= true;
4168 /* If the Mechanism is By_Reference, ensure this function uses the
4169 target's by-invisible-reference mechanism, which may not be the
4170 same as above (e.g. it might be passing an extra parameter). */
4171 else if (kind
== E_Function
4172 && Mechanism (gnat_entity
) == By_Reference
)
4173 return_by_invisi_ref_p
= true;
4175 /* Likewise, if the return type is itself By_Reference. */
4176 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type
))
4177 return_by_invisi_ref_p
= true;
4179 /* If the type is a padded type and the underlying type would not
4180 be passed by reference or the function has a foreign convention,
4181 return the underlying type. */
4182 else if (TYPE_IS_PADDING_P (gnu_return_type
)
4183 && (!default_pass_by_ref
4184 (TREE_TYPE (TYPE_FIELDS (gnu_return_type
)))
4185 || Has_Foreign_Convention (gnat_entity
)))
4186 gnu_return_type
= TREE_TYPE (TYPE_FIELDS (gnu_return_type
));
4188 /* If the return type is unconstrained, that means it must have a
4189 maximum size. Use the padded type as the effective return type.
4190 And ensure the function uses the target's by-invisible-reference
4191 mechanism to avoid copying too much data when it returns. */
4192 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type
)))
4194 tree orig_type
= gnu_return_type
;
4197 = maybe_pad_type (gnu_return_type
,
4198 max_size (TYPE_SIZE (gnu_return_type
),
4200 0, gnat_entity
, false, false, false, true);
4202 /* Declare it now since it will never be declared otherwise.
4203 This is necessary to ensure that its subtrees are properly
4205 if (gnu_return_type
!= orig_type
4206 && !DECL_P (TYPE_NAME (gnu_return_type
)))
4207 create_type_decl (TYPE_NAME (gnu_return_type
),
4208 gnu_return_type
, NULL
, true,
4209 debug_info_p
, gnat_entity
);
4211 return_by_invisi_ref_p
= true;
4214 /* If the return type has a size that overflows, we cannot have
4215 a function that returns that type. This usage doesn't make
4216 sense anyway, so give an error here. */
4217 if (TYPE_SIZE_UNIT (gnu_return_type
)
4218 && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type
))
4219 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type
)))
4221 post_error ("cannot return type whose size overflows",
4223 gnu_return_type
= copy_node (gnu_return_type
);
4224 TYPE_SIZE (gnu_return_type
) = bitsize_zero_node
;
4225 TYPE_SIZE_UNIT (gnu_return_type
) = size_zero_node
;
4226 TYPE_MAIN_VARIANT (gnu_return_type
) = gnu_return_type
;
4227 TYPE_NEXT_VARIANT (gnu_return_type
) = NULL_TREE
;
4231 /* Loop over the parameters and get their associated GCC tree. While
4232 doing this, build a copy-in copy-out structure if we need one. */
4233 for (gnat_param
= First_Formal_With_Extras (gnat_entity
), parmnum
= 0;
4234 Present (gnat_param
);
4235 gnat_param
= Next_Formal_With_Extras (gnat_param
), parmnum
++)
4237 tree gnu_param_name
= get_entity_name (gnat_param
);
4238 tree gnu_param_type
= gnat_to_gnu_type (Etype (gnat_param
));
4239 tree gnu_param
, gnu_field
;
4240 bool copy_in_copy_out
= false;
4241 Mechanism_Type mech
= Mechanism (gnat_param
);
4243 /* Builtins are expanded inline and there is no real call sequence
4244 involved. So the type expected by the underlying expander is
4245 always the type of each argument "as is". */
4246 if (gnu_builtin_decl
)
4248 /* Handle the first parameter of a valued procedure specially. */
4249 else if (Is_Valued_Procedure (gnat_entity
) && parmnum
== 0)
4250 mech
= By_Copy_Return
;
4251 /* Otherwise, see if a Mechanism was supplied that forced this
4252 parameter to be passed one way or another. */
4253 else if (mech
== Default
4254 || mech
== By_Copy
|| mech
== By_Reference
)
4256 else if (By_Descriptor_Last
<= mech
&& mech
<= By_Descriptor
)
4257 mech
= By_Descriptor
;
4259 else if (By_Short_Descriptor_Last
<= mech
&&
4260 mech
<= By_Short_Descriptor
)
4261 mech
= By_Short_Descriptor
;
4265 if (TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
4266 || TREE_CODE (TYPE_SIZE (gnu_param_type
)) != INTEGER_CST
4267 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type
),
4269 mech
= By_Reference
;
4275 post_error ("unsupported mechanism for&", gnat_param
);
4280 = gnat_to_gnu_param (gnat_param
, mech
, gnat_entity
,
4281 Has_Foreign_Convention (gnat_entity
),
4284 /* We are returned either a PARM_DECL or a type if no parameter
4285 needs to be passed; in either case, adjust the type. */
4286 if (DECL_P (gnu_param
))
4287 gnu_param_type
= TREE_TYPE (gnu_param
);
4290 gnu_param_type
= gnu_param
;
4291 gnu_param
= NULL_TREE
;
4294 /* The failure of this assertion will very likely come from an
4295 order of elaboration issue for the type of the parameter. */
4296 gcc_assert (kind
== E_Subprogram_Type
4297 || !TYPE_IS_DUMMY_P (gnu_param_type
)
4298 || type_annotate_only
);
4302 /* If it's an exported subprogram, we build a parameter list
4303 in parallel, in case we need to emit a stub for it. */
4304 if (Is_Exported (gnat_entity
))
4307 = chainon (gnu_param
, gnu_stub_param_list
);
4308 /* Change By_Descriptor parameter to By_Reference for
4309 the internal version of an exported subprogram. */
4310 if (mech
== By_Descriptor
|| mech
== By_Short_Descriptor
)
4313 = gnat_to_gnu_param (gnat_param
, By_Reference
,
4319 gnu_param
= copy_node (gnu_param
);
4322 gnu_param_list
= chainon (gnu_param
, gnu_param_list
);
4323 Sloc_to_locus (Sloc (gnat_param
),
4324 &DECL_SOURCE_LOCATION (gnu_param
));
4325 save_gnu_tree (gnat_param
, gnu_param
, false);
4327 /* If a parameter is a pointer, this function may modify
4328 memory through it and thus shouldn't be considered
4329 a const function. Also, the memory may be modified
4330 between two calls, so they can't be CSE'ed. The latter
4331 case also handles by-ref parameters. */
4332 if (POINTER_TYPE_P (gnu_param_type
)
4333 || TYPE_IS_FAT_POINTER_P (gnu_param_type
))
4337 if (copy_in_copy_out
)
4341 tree gnu_new_ret_type
= make_node (RECORD_TYPE
);
4343 /* If this is a function, we also need a field for the
4344 return value to be placed. */
4345 if (TREE_CODE (gnu_return_type
) != VOID_TYPE
)
4348 = create_field_decl (get_identifier ("RETVAL"),
4350 gnu_new_ret_type
, NULL_TREE
,
4352 Sloc_to_locus (Sloc (gnat_entity
),
4353 &DECL_SOURCE_LOCATION (gnu_field
));
4354 gnu_field_list
= gnu_field
;
4356 = tree_cons (gnu_field
, void_type_node
, NULL_TREE
);
4359 gnu_return_type
= gnu_new_ret_type
;
4360 TYPE_NAME (gnu_return_type
) = get_identifier ("RETURN");
4361 /* Set a default alignment to speed up accesses. But we
4362 shouldn't increase the size of the structure too much,
4363 lest it doesn't fit in return registers anymore. */
4364 TYPE_ALIGN (gnu_return_type
)
4365 = get_mode_alignment (ptr_mode
);
4369 = create_field_decl (gnu_param_name
, gnu_param_type
,
4370 gnu_return_type
, NULL_TREE
, NULL_TREE
,
4372 Sloc_to_locus (Sloc (gnat_param
),
4373 &DECL_SOURCE_LOCATION (gnu_field
));
4374 DECL_CHAIN (gnu_field
) = gnu_field_list
;
4375 gnu_field_list
= gnu_field
;
4377 = tree_cons (gnu_field
, gnu_param
, gnu_cico_list
);
4383 /* If we have a CICO list but it has only one entry, we convert
4384 this function into a function that returns this object. */
4385 if (list_length (gnu_cico_list
) == 1)
4386 gnu_return_type
= TREE_TYPE (TREE_PURPOSE (gnu_cico_list
));
4388 /* Do not finalize the return type if the subprogram is stubbed
4389 since structures are incomplete for the back-end. */
4390 else if (Convention (gnat_entity
) != Convention_Stubbed
)
4392 finish_record_type (gnu_return_type
, nreverse (gnu_field_list
),
4395 /* Try to promote the mode of the return type if it is passed
4396 in registers, again to speed up accesses. */
4397 if (TYPE_MODE (gnu_return_type
) == BLKmode
4398 && !targetm
.calls
.return_in_memory (gnu_return_type
,
4402 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type
));
4403 unsigned int i
= BITS_PER_UNIT
;
4404 enum machine_mode mode
;
4408 mode
= mode_for_size (i
, MODE_INT
, 0);
4409 if (mode
!= BLKmode
)
4411 SET_TYPE_MODE (gnu_return_type
, mode
);
4412 TYPE_ALIGN (gnu_return_type
)
4413 = GET_MODE_ALIGNMENT (mode
);
4414 TYPE_SIZE (gnu_return_type
)
4415 = bitsize_int (GET_MODE_BITSIZE (mode
));
4416 TYPE_SIZE_UNIT (gnu_return_type
)
4417 = size_int (GET_MODE_SIZE (mode
));
4422 rest_of_record_type_compilation (gnu_return_type
);
4426 if (Has_Stdcall_Convention (gnat_entity
))
4427 prepend_one_attribute_to
4428 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4429 get_identifier ("stdcall"), NULL_TREE
,
4431 else if (Has_Thiscall_Convention (gnat_entity
))
4432 prepend_one_attribute_to
4433 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4434 get_identifier ("thiscall"), NULL_TREE
,
4437 /* If we should request stack realignment for a foreign convention
4438 subprogram, do so. Note that this applies to task entry points in
4440 if (FOREIGN_FORCE_REALIGN_STACK
4441 && Has_Foreign_Convention (gnat_entity
))
4442 prepend_one_attribute_to
4443 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4444 get_identifier ("force_align_arg_pointer"), NULL_TREE
,
4447 /* The lists have been built in reverse. */
4448 gnu_param_list
= nreverse (gnu_param_list
);
4450 gnu_stub_param_list
= nreverse (gnu_stub_param_list
);
4451 gnu_cico_list
= nreverse (gnu_cico_list
);
4453 if (kind
== E_Function
)
4454 Set_Mechanism (gnat_entity
, return_unconstrained_p
4455 || return_by_direct_ref_p
4456 || return_by_invisi_ref_p
4457 ? By_Reference
: By_Copy
);
4459 = create_subprog_type (gnu_return_type
, gnu_param_list
,
4460 gnu_cico_list
, return_unconstrained_p
,
4461 return_by_direct_ref_p
,
4462 return_by_invisi_ref_p
);
4466 = create_subprog_type (gnu_return_type
, gnu_stub_param_list
,
4467 gnu_cico_list
, return_unconstrained_p
,
4468 return_by_direct_ref_p
,
4469 return_by_invisi_ref_p
);
4471 /* A subprogram (something that doesn't return anything) shouldn't
4472 be considered const since there would be no reason for such a
4473 subprogram. Note that procedures with Out (or In Out) parameters
4474 have already been converted into a function with a return type. */
4475 if (TREE_CODE (gnu_return_type
) == VOID_TYPE
)
4479 = build_qualified_type (gnu_type
,
4480 TYPE_QUALS (gnu_type
)
4481 | (TYPE_QUAL_CONST
* const_flag
)
4482 | (TYPE_QUAL_VOLATILE
* volatile_flag
));
4486 = build_qualified_type (gnu_stub_type
,
4487 TYPE_QUALS (gnu_stub_type
)
4488 | (TYPE_QUAL_CONST
* const_flag
)
4489 | (TYPE_QUAL_VOLATILE
* volatile_flag
));
4491 /* If we have a builtin decl for that function, use it. Check if the
4492 profiles are compatible and warn if they are not. The checker is
4493 expected to post extra diagnostics in this case. */
4494 if (gnu_builtin_decl
)
4496 intrin_binding_t inb
;
4498 inb
.gnat_entity
= gnat_entity
;
4499 inb
.ada_fntype
= gnu_type
;
4500 inb
.btin_fntype
= TREE_TYPE (gnu_builtin_decl
);
4502 if (!intrin_profiles_compatible_p (&inb
))
4504 ("?profile of& doesn''t match the builtin it binds!",
4507 gnu_decl
= gnu_builtin_decl
;
4508 gnu_type
= TREE_TYPE (gnu_builtin_decl
);
4512 /* If there was no specified Interface_Name and the external and
4513 internal names of the subprogram are the same, only use the
4514 internal name to allow disambiguation of nested subprograms. */
4515 if (No (Interface_Name (gnat_entity
))
4516 && gnu_ext_name
== gnu_entity_name
)
4517 gnu_ext_name
= NULL_TREE
;
4519 /* If we are defining the subprogram and it has an Address clause
4520 we must get the address expression from the saved GCC tree for the
4521 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4522 the address expression here since the front-end has guaranteed
4523 in that case that the elaboration has no effects. If there is
4524 an Address clause and we are not defining the object, just
4525 make it a constant. */
4526 if (Present (Address_Clause (gnat_entity
)))
4528 tree gnu_address
= NULL_TREE
;
4532 = (present_gnu_tree (gnat_entity
)
4533 ? get_gnu_tree (gnat_entity
)
4534 : gnat_to_gnu (Expression (Address_Clause (gnat_entity
))));
4536 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
4538 /* Convert the type of the object to a reference type that can
4539 alias everything as per 13.3(19). */
4541 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
4543 gnu_address
= convert (gnu_type
, gnu_address
);
4546 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4547 gnu_address
, false, Is_Public (gnat_entity
),
4548 extern_flag
, false, NULL
, gnat_entity
);
4549 DECL_BY_REF_P (gnu_decl
) = 1;
4552 else if (kind
== E_Subprogram_Type
)
4554 = create_type_decl (gnu_entity_name
, gnu_type
, attr_list
,
4555 artificial_flag
, debug_info_p
, gnat_entity
);
4560 gnu_stub_name
= gnu_ext_name
;
4561 gnu_ext_name
= create_concat_name (gnat_entity
, "internal");
4562 public_flag
= false;
4563 artificial_flag
= true;
4567 = create_subprog_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4568 gnu_param_list
, inline_flag
, public_flag
,
4569 extern_flag
, artificial_flag
, attr_list
,
4574 = create_subprog_decl (gnu_entity_name
, gnu_stub_name
,
4575 gnu_stub_type
, gnu_stub_param_list
,
4576 inline_flag
, true, extern_flag
,
4577 false, attr_list
, gnat_entity
);
4578 SET_DECL_FUNCTION_STUB (gnu_decl
, gnu_stub_decl
);
4581 /* This is unrelated to the stub built right above. */
4582 DECL_STUBBED_P (gnu_decl
)
4583 = Convention (gnat_entity
) == Convention_Stubbed
;
4588 case E_Incomplete_Type
:
4589 case E_Incomplete_Subtype
:
4590 case E_Private_Type
:
4591 case E_Private_Subtype
:
4592 case E_Limited_Private_Type
:
4593 case E_Limited_Private_Subtype
:
4594 case E_Record_Type_With_Private
:
4595 case E_Record_Subtype_With_Private
:
4597 /* Get the "full view" of this entity. If this is an incomplete
4598 entity from a limited with, treat its non-limited view as the
4599 full view. Otherwise, use either the full view or the underlying
4600 full view, whichever is present. This is used in all the tests
4603 = (IN (kind
, Incomplete_Kind
) && From_With_Type (gnat_entity
))
4604 ? Non_Limited_View (gnat_entity
)
4605 : Present (Full_View (gnat_entity
))
4606 ? Full_View (gnat_entity
)
4607 : Underlying_Full_View (gnat_entity
);
4609 /* If this is an incomplete type with no full view, it must be a Taft
4610 Amendment type, in which case we return a dummy type. Otherwise,
4611 just get the type from its Etype. */
4614 if (kind
== E_Incomplete_Type
)
4616 gnu_type
= make_dummy_type (gnat_entity
);
4617 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4621 gnu_decl
= gnat_to_gnu_entity (Etype (gnat_entity
),
4623 maybe_present
= true;
4628 /* If we already made a type for the full view, reuse it. */
4629 else if (present_gnu_tree (full_view
))
4631 gnu_decl
= get_gnu_tree (full_view
);
4635 /* Otherwise, if we are not defining the type now, get the type
4636 from the full view. But always get the type from the full view
4637 for define on use types, since otherwise we won't see them! */
4638 else if (!definition
4639 || (Is_Itype (full_view
)
4640 && No (Freeze_Node (gnat_entity
)))
4641 || (Is_Itype (gnat_entity
)
4642 && No (Freeze_Node (full_view
))))
4644 gnu_decl
= gnat_to_gnu_entity (full_view
, NULL_TREE
, 0);
4645 maybe_present
= true;
4649 /* For incomplete types, make a dummy type entry which will be
4650 replaced later. Save it as the full declaration's type so
4651 we can do any needed updates when we see it. */
4652 gnu_type
= make_dummy_type (gnat_entity
);
4653 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4654 if (Has_Completion_In_Body (gnat_entity
))
4655 DECL_TAFT_TYPE_P (gnu_decl
) = 1;
4656 save_gnu_tree (full_view
, gnu_decl
, 0);
4660 case E_Class_Wide_Type
:
4661 /* Class-wide types are always transformed into their root type. */
4662 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
4663 maybe_present
= true;
4667 case E_Task_Subtype
:
4668 case E_Protected_Type
:
4669 case E_Protected_Subtype
:
4670 /* Concurrent types are always transformed into their record type. */
4671 if (type_annotate_only
&& No (gnat_equiv_type
))
4672 gnu_type
= void_type_node
;
4674 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
4675 maybe_present
= true;
4679 gnu_decl
= create_label_decl (gnu_entity_name
, gnat_entity
);
4684 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4685 we've already saved it, so we don't try to. */
4686 gnu_decl
= error_mark_node
;
4694 /* If we had a case where we evaluated another type and it might have
4695 defined this one, handle it here. */
4696 if (maybe_present
&& present_gnu_tree (gnat_entity
))
4698 gnu_decl
= get_gnu_tree (gnat_entity
);
4702 /* If we are processing a type and there is either no decl for it or
4703 we just made one, do some common processing for the type, such as
4704 handling alignment and possible padding. */
4705 if (is_type
&& (!gnu_decl
|| this_made_decl
))
4707 /* Tell the middle-end that objects of tagged types are guaranteed to
4708 be properly aligned. This is necessary because conversions to the
4709 class-wide type are translated into conversions to the root type,
4710 which can be less aligned than some of its derived types. */
4711 if (Is_Tagged_Type (gnat_entity
)
4712 || Is_Class_Wide_Equivalent_Type (gnat_entity
))
4713 TYPE_ALIGN_OK (gnu_type
) = 1;
4715 /* Record whether the type is passed by reference. */
4716 if (!VOID_TYPE_P (gnu_type
) && Is_By_Reference_Type (gnat_entity
))
4717 TYPE_BY_REFERENCE_P (gnu_type
) = 1;
4719 /* ??? Don't set the size for a String_Literal since it is either
4720 confirming or we don't handle it properly (if the low bound is
4722 if (!gnu_size
&& kind
!= E_String_Literal_Subtype
)
4724 Uint gnat_size
= Known_Esize (gnat_entity
)
4725 ? Esize (gnat_entity
) : RM_Size (gnat_entity
);
4727 = validate_size (gnat_size
, gnu_type
, gnat_entity
, TYPE_DECL
,
4728 false, Has_Size_Clause (gnat_entity
));
4731 /* If a size was specified, see if we can make a new type of that size
4732 by rearranging the type, for example from a fat to a thin pointer. */
4736 = make_type_from_size (gnu_type
, gnu_size
,
4737 Has_Biased_Representation (gnat_entity
));
4739 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0)
4740 && operand_equal_p (rm_size (gnu_type
), gnu_size
, 0))
4741 gnu_size
= NULL_TREE
;
4744 /* If the alignment hasn't already been processed and this is
4745 not an unconstrained array, see if an alignment is specified.
4746 If not, we pick a default alignment for atomic objects. */
4747 if (align
!= 0 || TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4749 else if (Known_Alignment (gnat_entity
))
4751 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
4752 TYPE_ALIGN (gnu_type
));
4754 /* Warn on suspiciously large alignments. This should catch
4755 errors about the (alignment,byte)/(size,bit) discrepancy. */
4756 if (align
> BIGGEST_ALIGNMENT
&& Has_Alignment_Clause (gnat_entity
))
4760 /* If a size was specified, take it into account. Otherwise
4761 use the RM size for records or unions as the type size has
4762 already been adjusted to the alignment. */
4765 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
4766 && !TYPE_FAT_POINTER_P (gnu_type
))
4767 size
= rm_size (gnu_type
);
4769 size
= TYPE_SIZE (gnu_type
);
4771 /* Consider an alignment as suspicious if the alignment/size
4772 ratio is greater or equal to the byte/bit ratio. */
4773 if (host_integerp (size
, 1)
4774 && align
>= TREE_INT_CST_LOW (size
) * BITS_PER_UNIT
)
4775 post_error_ne ("?suspiciously large alignment specified for&",
4776 Expression (Alignment_Clause (gnat_entity
)),
4780 else if (Is_Atomic (gnat_entity
) && !gnu_size
4781 && host_integerp (TYPE_SIZE (gnu_type
), 1)
4782 && integer_pow2p (TYPE_SIZE (gnu_type
)))
4783 align
= MIN (BIGGEST_ALIGNMENT
,
4784 tree_low_cst (TYPE_SIZE (gnu_type
), 1));
4785 else if (Is_Atomic (gnat_entity
) && gnu_size
4786 && host_integerp (gnu_size
, 1)
4787 && integer_pow2p (gnu_size
))
4788 align
= MIN (BIGGEST_ALIGNMENT
, tree_low_cst (gnu_size
, 1));
4790 /* See if we need to pad the type. If we did, and made a record,
4791 the name of the new type may be changed. So get it back for
4792 us when we make the new TYPE_DECL below. */
4793 if (gnu_size
|| align
> 0)
4794 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
4795 false, !gnu_decl
, definition
, false);
4797 if (TYPE_IS_PADDING_P (gnu_type
))
4799 gnu_entity_name
= TYPE_NAME (gnu_type
);
4800 if (TREE_CODE (gnu_entity_name
) == TYPE_DECL
)
4801 gnu_entity_name
= DECL_NAME (gnu_entity_name
);
4804 /* Now set the RM size of the type. We cannot do it before padding
4805 because we need to accept arbitrary RM sizes on integral types. */
4806 set_rm_size (RM_Size (gnat_entity
), gnu_type
, gnat_entity
);
4808 /* If we are at global level, GCC will have applied variable_size to
4809 the type, but that won't have done anything. So, if it's not
4810 a constant or self-referential, call elaborate_expression_1 to
4811 make a variable for the size rather than calculating it each time.
4812 Handle both the RM size and the actual size. */
4813 if (global_bindings_p ()
4814 && TYPE_SIZE (gnu_type
)
4815 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
))
4816 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
4818 tree size
= TYPE_SIZE (gnu_type
);
4820 TYPE_SIZE (gnu_type
)
4821 = elaborate_expression_1 (size
, gnat_entity
,
4822 get_identifier ("SIZE"),
4825 /* ??? For now, store the size as a multiple of the alignment in
4826 bytes so that we can see the alignment from the tree. */
4827 TYPE_SIZE_UNIT (gnu_type
)
4828 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type
), gnat_entity
,
4829 get_identifier ("SIZE_A_UNIT"),
4831 TYPE_ALIGN (gnu_type
));
4833 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4834 may not be marked by the call to create_type_decl below. */
4835 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type
));
4837 if (TREE_CODE (gnu_type
) == RECORD_TYPE
)
4839 tree variant_part
= get_variant_part (gnu_type
);
4840 tree ada_size
= TYPE_ADA_SIZE (gnu_type
);
4844 tree union_type
= TREE_TYPE (variant_part
);
4845 tree offset
= DECL_FIELD_OFFSET (variant_part
);
4847 /* If the position of the variant part is constant, subtract
4848 it from the size of the type of the parent to get the new
4849 size. This manual CSE reduces the data size. */
4850 if (TREE_CODE (offset
) == INTEGER_CST
)
4852 tree bitpos
= DECL_FIELD_BIT_OFFSET (variant_part
);
4853 TYPE_SIZE (union_type
)
4854 = size_binop (MINUS_EXPR
, TYPE_SIZE (gnu_type
),
4855 bit_from_pos (offset
, bitpos
));
4856 TYPE_SIZE_UNIT (union_type
)
4857 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (gnu_type
),
4858 byte_from_pos (offset
, bitpos
));
4862 TYPE_SIZE (union_type
)
4863 = elaborate_expression_1 (TYPE_SIZE (union_type
),
4865 get_identifier ("VSIZE"),
4868 /* ??? For now, store the size as a multiple of the
4869 alignment in bytes so that we can see the alignment
4871 TYPE_SIZE_UNIT (union_type
)
4872 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type
),
4877 TYPE_ALIGN (union_type
));
4879 /* ??? For now, store the offset as a multiple of the
4880 alignment in bytes so that we can see the alignment
4882 DECL_FIELD_OFFSET (variant_part
)
4883 = elaborate_expression_2 (offset
,
4885 get_identifier ("VOFFSET"),
4891 DECL_SIZE (variant_part
) = TYPE_SIZE (union_type
);
4892 DECL_SIZE_UNIT (variant_part
) = TYPE_SIZE_UNIT (union_type
);
4895 if (operand_equal_p (ada_size
, size
, 0))
4896 ada_size
= TYPE_SIZE (gnu_type
);
4899 = elaborate_expression_1 (ada_size
, gnat_entity
,
4900 get_identifier ("RM_SIZE"),
4902 SET_TYPE_ADA_SIZE (gnu_type
, ada_size
);
4906 /* If this is a record type or subtype, call elaborate_expression_1 on
4907 any field position. Do this for both global and local types.
4908 Skip any fields that we haven't made trees for to avoid problems with
4909 class wide types. */
4910 if (IN (kind
, Record_Kind
))
4911 for (gnat_temp
= First_Entity (gnat_entity
); Present (gnat_temp
);
4912 gnat_temp
= Next_Entity (gnat_temp
))
4913 if (Ekind (gnat_temp
) == E_Component
&& present_gnu_tree (gnat_temp
))
4915 tree gnu_field
= get_gnu_tree (gnat_temp
);
4917 /* ??? For now, store the offset as a multiple of the alignment
4918 in bytes so that we can see the alignment from the tree. */
4919 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field
)))
4921 DECL_FIELD_OFFSET (gnu_field
)
4922 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field
),
4924 get_identifier ("OFFSET"),
4926 DECL_OFFSET_ALIGN (gnu_field
));
4928 /* ??? The context of gnu_field is not necessarily gnu_type
4929 so the MULT_EXPR node built above may not be marked by
4930 the call to create_type_decl below. */
4931 if (global_bindings_p ())
4932 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field
));
4936 if (Treat_As_Volatile (gnat_entity
))
4938 = build_qualified_type (gnu_type
,
4939 TYPE_QUALS (gnu_type
) | TYPE_QUAL_VOLATILE
);
4941 if (Is_Atomic (gnat_entity
))
4942 check_ok_for_atomic (gnu_type
, gnat_entity
, false);
4944 if (Present (Alignment_Clause (gnat_entity
)))
4945 TYPE_USER_ALIGN (gnu_type
) = 1;
4947 if (Universal_Aliasing (gnat_entity
))
4948 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type
)) = 1;
4951 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
, attr_list
,
4952 !Comes_From_Source (gnat_entity
),
4953 debug_info_p
, gnat_entity
);
4956 TREE_TYPE (gnu_decl
) = gnu_type
;
4957 TYPE_STUB_DECL (gnu_type
) = gnu_decl
;
4961 if (is_type
&& !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
)))
4963 gnu_type
= TREE_TYPE (gnu_decl
);
4965 /* If this is a derived type, relate its alias set to that of its parent
4966 to avoid troubles when a call to an inherited primitive is inlined in
4967 a context where a derived object is accessed. The inlined code works
4968 on the parent view so the resulting code may access the same object
4969 using both the parent and the derived alias sets, which thus have to
4970 conflict. As the same issue arises with component references, the
4971 parent alias set also has to conflict with composite types enclosing
4972 derived components. For instance, if we have:
4979 we want T to conflict with both D and R, in addition to R being a
4980 superset of D by record/component construction.
4982 One way to achieve this is to perform an alias set copy from the
4983 parent to the derived type. This is not quite appropriate, though,
4984 as we don't want separate derived types to conflict with each other:
4986 type I1 is new Integer;
4987 type I2 is new Integer;
4989 We want I1 and I2 to both conflict with Integer but we do not want
4990 I1 to conflict with I2, and an alias set copy on derivation would
4993 The option chosen is to make the alias set of the derived type a
4994 superset of that of its parent type. It trivially fulfills the
4995 simple requirement for the Integer derivation example above, and
4996 the component case as well by superset transitivity:
4999 R ----------> D ----------> T
5001 However, for composite types, conversions between derived types are
5002 translated into VIEW_CONVERT_EXPRs so a sequence like:
5004 type Comp1 is new Comp;
5005 type Comp2 is new Comp;
5006 procedure Proc (C : Comp1);
5014 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5016 and gimplified into:
5023 i.e. generates code involving type punning. Therefore, Comp1 needs
5024 to conflict with Comp2 and an alias set copy is required.
5026 The language rules ensure the parent type is already frozen here. */
5027 if (Is_Derived_Type (gnat_entity
))
5029 tree gnu_parent_type
= gnat_to_gnu_type (Etype (gnat_entity
));
5030 relate_alias_sets (gnu_type
, gnu_parent_type
,
5031 Is_Composite_Type (gnat_entity
)
5032 ? ALIAS_SET_COPY
: ALIAS_SET_SUPERSET
);
5035 /* Back-annotate the Alignment of the type if not already in the
5036 tree. Likewise for sizes. */
5037 if (Unknown_Alignment (gnat_entity
))
5039 unsigned int double_align
, align
;
5040 bool is_capped_double
, align_clause
;
5042 /* If the default alignment of "double" or larger scalar types is
5043 specifically capped and this is not an array with an alignment
5044 clause on the component type, return the cap. */
5045 if ((double_align
= double_float_alignment
) > 0)
5047 = is_double_float_or_array (gnat_entity
, &align_clause
);
5048 else if ((double_align
= double_scalar_alignment
) > 0)
5050 = is_double_scalar_or_array (gnat_entity
, &align_clause
);
5052 is_capped_double
= align_clause
= false;
5054 if (is_capped_double
&& !align_clause
)
5055 align
= double_align
;
5057 align
= TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
;
5059 Set_Alignment (gnat_entity
, UI_From_Int (align
));
5062 if (Unknown_Esize (gnat_entity
) && TYPE_SIZE (gnu_type
))
5064 tree gnu_size
= TYPE_SIZE (gnu_type
);
5066 /* If the size is self-referential, annotate the maximum value. */
5067 if (CONTAINS_PLACEHOLDER_P (gnu_size
))
5068 gnu_size
= max_size (gnu_size
, true);
5070 /* If we are just annotating types and the type is tagged, the tag
5071 and the parent components are not generated by the front-end so
5072 sizes must be adjusted if there is no representation clause. */
5073 if (type_annotate_only
5074 && Is_Tagged_Type (gnat_entity
)
5075 && !VOID_TYPE_P (gnu_type
)
5076 && (!TYPE_FIELDS (gnu_type
)
5077 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type
)))))
5079 tree pointer_size
= bitsize_int (POINTER_SIZE
), offset
;
5082 if (Is_Derived_Type (gnat_entity
))
5084 Entity_Id gnat_parent
= Etype (Base_Type (gnat_entity
));
5085 offset
= UI_To_gnu (Esize (gnat_parent
), bitsizetype
);
5086 Set_Alignment (gnat_entity
, Alignment (gnat_parent
));
5089 offset
= pointer_size
;
5091 if (TYPE_FIELDS (gnu_type
))
5093 = round_up (offset
, DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
5095 gnu_size
= size_binop (PLUS_EXPR
, gnu_size
, offset
);
5096 gnu_size
= round_up (gnu_size
, POINTER_SIZE
);
5097 uint_size
= annotate_value (gnu_size
);
5098 Set_Esize (gnat_entity
, uint_size
);
5099 Set_RM_Size (gnat_entity
, uint_size
);
5102 Set_Esize (gnat_entity
, annotate_value (gnu_size
));
5105 if (Unknown_RM_Size (gnat_entity
) && rm_size (gnu_type
))
5106 Set_RM_Size (gnat_entity
, annotate_value (rm_size (gnu_type
)));
5109 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5110 cannot do so if we are reusing the ..._DECL node made for an equivalent
5111 type or an alias or a renamed object as the predicates don't apply to it
5112 but to GNAT_ENTITY. */
5113 if (DECL_P (gnu_decl
)
5114 && !(is_type
&& gnat_equiv_type
!= gnat_entity
)
5115 && !Present (Alias (gnat_entity
))
5116 && !(Present (Renamed_Object (gnat_entity
)) && saved
))
5118 if (!Comes_From_Source (gnat_entity
))
5119 DECL_ARTIFICIAL (gnu_decl
) = 1;
5122 DECL_IGNORED_P (gnu_decl
) = 1;
5125 /* If we haven't already, associate the ..._DECL node that we just made with
5126 the input GNAT entity node. */
5128 save_gnu_tree (gnat_entity
, gnu_decl
, false);
5130 /* If this is an enumeration or floating-point type, we were not able to set
5131 the bounds since they refer to the type. These are always static. */
5132 if ((kind
== E_Enumeration_Type
&& Present (First_Literal (gnat_entity
)))
5133 || (kind
== E_Floating_Point_Type
&& !Vax_Float (gnat_entity
)))
5135 tree gnu_scalar_type
= gnu_type
;
5136 tree gnu_low_bound
, gnu_high_bound
;
5138 /* If this is a padded type, we need to use the underlying type. */
5139 if (TYPE_IS_PADDING_P (gnu_scalar_type
))
5140 gnu_scalar_type
= TREE_TYPE (TYPE_FIELDS (gnu_scalar_type
));
5142 /* If this is a floating point type and we haven't set a floating
5143 point type yet, use this in the evaluation of the bounds. */
5144 if (!longest_float_type_node
&& kind
== E_Floating_Point_Type
)
5145 longest_float_type_node
= gnu_scalar_type
;
5147 gnu_low_bound
= gnat_to_gnu (Type_Low_Bound (gnat_entity
));
5148 gnu_high_bound
= gnat_to_gnu (Type_High_Bound (gnat_entity
));
5150 if (kind
== E_Enumeration_Type
)
5152 /* Enumeration types have specific RM bounds. */
5153 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type
, gnu_low_bound
);
5154 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type
, gnu_high_bound
);
5158 /* Floating-point types don't have specific RM bounds. */
5159 TYPE_GCC_MIN_VALUE (gnu_scalar_type
) = gnu_low_bound
;
5160 TYPE_GCC_MAX_VALUE (gnu_scalar_type
) = gnu_high_bound
;
5164 /* If we deferred processing of incomplete types, re-enable it. If there
5165 were no other disables and we have deferred types to process, do so. */
5167 && --defer_incomplete_level
== 0
5168 && defer_incomplete_list
)
5170 struct incomplete
*p
, *next
;
5172 /* We are back to level 0 for the deferring of incomplete types.
5173 But processing these incomplete types below may itself require
5174 deferring, so preserve what we have and restart from scratch. */
5175 p
= defer_incomplete_list
;
5176 defer_incomplete_list
= NULL
;
5183 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5184 gnat_to_gnu_type (p
->full_type
));
5189 /* If we are not defining this type, see if it's on one of the lists of
5190 incomplete types. If so, handle the list entry now. */
5191 if (is_type
&& !definition
)
5193 struct incomplete
*p
;
5195 for (p
= defer_incomplete_list
; p
; p
= p
->next
)
5196 if (p
->old_type
&& p
->full_type
== gnat_entity
)
5198 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5199 TREE_TYPE (gnu_decl
));
5200 p
->old_type
= NULL_TREE
;
5203 for (p
= defer_limited_with
; p
; p
= p
->next
)
5204 if (p
->old_type
&& Non_Limited_View (p
->full_type
) == gnat_entity
)
5206 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5207 TREE_TYPE (gnu_decl
));
5208 p
->old_type
= NULL_TREE
;
5215 /* If this is a packed array type whose original array type is itself
5216 an Itype without freeze node, make sure the latter is processed. */
5217 if (Is_Packed_Array_Type (gnat_entity
)
5218 && Is_Itype (Original_Array_Type (gnat_entity
))
5219 && No (Freeze_Node (Original_Array_Type (gnat_entity
)))
5220 && !present_gnu_tree (Original_Array_Type (gnat_entity
)))
5221 gnat_to_gnu_entity (Original_Array_Type (gnat_entity
), NULL_TREE
, 0);
5226 /* Similar, but if the returned value is a COMPONENT_REF, return the
5230 gnat_to_gnu_field_decl (Entity_Id gnat_entity
)
5232 tree gnu_field
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
5234 if (TREE_CODE (gnu_field
) == COMPONENT_REF
)
5235 gnu_field
= TREE_OPERAND (gnu_field
, 1);
5240 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5241 the GCC type corresponding to that entity. */
5244 gnat_to_gnu_type (Entity_Id gnat_entity
)
5248 /* The back end never attempts to annotate generic types. */
5249 if (Is_Generic_Type (gnat_entity
) && type_annotate_only
)
5250 return void_type_node
;
5252 gnu_decl
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
5253 gcc_assert (TREE_CODE (gnu_decl
) == TYPE_DECL
);
5255 return TREE_TYPE (gnu_decl
);
5258 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5259 the unpadded version of the GCC type corresponding to that entity. */
5262 get_unpadded_type (Entity_Id gnat_entity
)
5264 tree type
= gnat_to_gnu_type (gnat_entity
);
5266 if (TYPE_IS_PADDING_P (type
))
5267 type
= TREE_TYPE (TYPE_FIELDS (type
));
5272 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5273 type has been changed to that of the parameterless procedure, except if an
5274 alias is already present, in which case it is returned instead. */
5277 get_minimal_subprog_decl (Entity_Id gnat_entity
)
5279 tree gnu_entity_name
, gnu_ext_name
;
5280 struct attrib
*attr_list
= NULL
;
5282 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5283 of the handling applied here. */
5285 while (Present (Alias (gnat_entity
)))
5287 gnat_entity
= Alias (gnat_entity
);
5288 if (present_gnu_tree (gnat_entity
))
5289 return get_gnu_tree (gnat_entity
);
5292 gnu_entity_name
= get_entity_name (gnat_entity
);
5293 gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
5295 if (Has_Stdcall_Convention (gnat_entity
))
5296 prepend_one_attribute_to (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
5297 get_identifier ("stdcall"), NULL_TREE
,
5299 else if (Has_Thiscall_Convention (gnat_entity
))
5300 prepend_one_attribute_to (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
5301 get_identifier ("thiscall"), NULL_TREE
,
5304 if (No (Interface_Name (gnat_entity
)) && gnu_ext_name
== gnu_entity_name
)
5305 gnu_ext_name
= NULL_TREE
;
5308 create_subprog_decl (gnu_entity_name
, gnu_ext_name
, void_ftype
, NULL_TREE
,
5309 false, true, true, true, attr_list
, gnat_entity
);
5312 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5313 a C++ imported method or equivalent.
5315 We use the predicate on 32-bit x86/Windows to find out whether we need to
5316 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5317 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5320 is_cplusplus_method (Entity_Id gnat_entity
)
5322 if (Convention (gnat_entity
) != Convention_CPP
)
5325 /* This is the main case: C++ method imported as a primitive operation. */
5326 if (Is_Dispatching_Operation (gnat_entity
))
5329 /* A thunk needs to be handled like its associated primitive operation. */
5330 if (Is_Subprogram (gnat_entity
) && Is_Thunk (gnat_entity
))
5333 /* C++ classes with no virtual functions can be imported as limited
5334 record types, but we need to return true for the constructors. */
5335 if (Is_Constructor (gnat_entity
))
5338 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5339 if (Is_Dispatch_Table_Entity (gnat_entity
))
5345 /* Finalize the processing of From_With_Type incomplete types. */
5348 finalize_from_with_types (void)
5350 struct incomplete
*p
, *next
;
5352 p
= defer_limited_with
;
5353 defer_limited_with
= NULL
;
5360 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5361 gnat_to_gnu_type (p
->full_type
));
5366 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5367 kind of type (such E_Task_Type) that has a different type which Gigi
5368 uses for its representation. If the type does not have a special type
5369 for its representation, return GNAT_ENTITY. If a type is supposed to
5370 exist, but does not, abort unless annotating types, in which case
5371 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5374 Gigi_Equivalent_Type (Entity_Id gnat_entity
)
5376 Entity_Id gnat_equiv
= gnat_entity
;
5378 if (No (gnat_entity
))
5381 switch (Ekind (gnat_entity
))
5383 case E_Class_Wide_Subtype
:
5384 if (Present (Equivalent_Type (gnat_entity
)))
5385 gnat_equiv
= Equivalent_Type (gnat_entity
);
5388 case E_Access_Protected_Subprogram_Type
:
5389 case E_Anonymous_Access_Protected_Subprogram_Type
:
5390 gnat_equiv
= Equivalent_Type (gnat_entity
);
5393 case E_Class_Wide_Type
:
5394 gnat_equiv
= Root_Type (gnat_entity
);
5398 case E_Task_Subtype
:
5399 case E_Protected_Type
:
5400 case E_Protected_Subtype
:
5401 gnat_equiv
= Corresponding_Record_Type (gnat_entity
);
5408 gcc_assert (Present (gnat_equiv
) || type_annotate_only
);
5413 /* Return a GCC tree for a type corresponding to the component type of the
5414 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5415 is for an array being defined. DEBUG_INFO_P is true if we need to write
5416 debug information for other types that we may create in the process. */
5419 gnat_to_gnu_component_type (Entity_Id gnat_array
, bool definition
,
5422 const Entity_Id gnat_type
= Component_Type (gnat_array
);
5423 tree gnu_type
= gnat_to_gnu_type (gnat_type
);
5426 /* Try to get a smaller form of the component if needed. */
5427 if ((Is_Packed (gnat_array
)
5428 || Has_Component_Size_Clause (gnat_array
))
5429 && !Is_Bit_Packed_Array (gnat_array
)
5430 && !Has_Aliased_Components (gnat_array
)
5431 && !Strict_Alignment (gnat_type
)
5432 && RECORD_OR_UNION_TYPE_P (gnu_type
)
5433 && !TYPE_FAT_POINTER_P (gnu_type
)
5434 && host_integerp (TYPE_SIZE (gnu_type
), 1))
5435 gnu_type
= make_packable_type (gnu_type
, false);
5437 if (Has_Atomic_Components (gnat_array
))
5438 check_ok_for_atomic (gnu_type
, gnat_array
, true);
5440 /* Get and validate any specified Component_Size. */
5442 = validate_size (Component_Size (gnat_array
), gnu_type
, gnat_array
,
5443 Is_Bit_Packed_Array (gnat_array
) ? TYPE_DECL
: VAR_DECL
,
5444 true, Has_Component_Size_Clause (gnat_array
));
5446 /* If the array has aliased components and the component size can be zero,
5447 force at least unit size to ensure that the components have distinct
5450 && Has_Aliased_Components (gnat_array
)
5451 && (integer_zerop (TYPE_SIZE (gnu_type
))
5452 || (TREE_CODE (gnu_type
) == ARRAY_TYPE
5453 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
)))))
5455 = size_binop (MAX_EXPR
, TYPE_SIZE (gnu_type
), bitsize_unit_node
);
5457 /* If the component type is a RECORD_TYPE that has a self-referential size,
5458 then use the maximum size for the component size. */
5460 && TREE_CODE (gnu_type
) == RECORD_TYPE
5461 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
5462 gnu_comp_size
= max_size (TYPE_SIZE (gnu_type
), true);
5464 /* Honor the component size. This is not needed for bit-packed arrays. */
5465 if (gnu_comp_size
&& !Is_Bit_Packed_Array (gnat_array
))
5467 tree orig_type
= gnu_type
;
5468 unsigned int max_align
;
5470 /* If an alignment is specified, use it as a cap on the component type
5471 so that it can be honored for the whole type. But ignore it for the
5472 original type of packed array types. */
5473 if (No (Packed_Array_Type (gnat_array
)) && Known_Alignment (gnat_array
))
5474 max_align
= validate_alignment (Alignment (gnat_array
), gnat_array
, 0);
5478 gnu_type
= make_type_from_size (gnu_type
, gnu_comp_size
, false);
5479 if (max_align
> 0 && TYPE_ALIGN (gnu_type
) > max_align
)
5480 gnu_type
= orig_type
;
5482 orig_type
= gnu_type
;
5484 gnu_type
= maybe_pad_type (gnu_type
, gnu_comp_size
, 0, gnat_array
,
5485 true, false, definition
, true);
5487 /* If a padding record was made, declare it now since it will never be
5488 declared otherwise. This is necessary to ensure that its subtrees
5489 are properly marked. */
5490 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
5491 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, NULL
, true,
5492 debug_info_p
, gnat_array
);
5495 if (Has_Volatile_Components (gnat_array
))
5497 = build_qualified_type (gnu_type
,
5498 TYPE_QUALS (gnu_type
) | TYPE_QUAL_VOLATILE
);
5503 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5504 using MECH as its passing mechanism, to be placed in the parameter
5505 list built for GNAT_SUBPROG. Assume a foreign convention for the
5506 latter if FOREIGN is true. Also set CICO to true if the parameter
5507 must use the copy-in copy-out implementation mechanism.
5509 The returned tree is a PARM_DECL, except for those cases where no
5510 parameter needs to be actually passed to the subprogram; the type
5511 of this "shadow" parameter is then returned instead. */
5514 gnat_to_gnu_param (Entity_Id gnat_param
, Mechanism_Type mech
,
5515 Entity_Id gnat_subprog
, bool foreign
, bool *cico
)
5517 tree gnu_param_name
= get_entity_name (gnat_param
);
5518 tree gnu_param_type
= gnat_to_gnu_type (Etype (gnat_param
));
5519 tree gnu_param_type_alt
= NULL_TREE
;
5520 bool in_param
= (Ekind (gnat_param
) == E_In_Parameter
);
5521 /* The parameter can be indirectly modified if its address is taken. */
5522 bool ro_param
= in_param
&& !Address_Taken (gnat_param
);
5523 bool by_return
= false, by_component_ptr
= false;
5524 bool by_ref
= false, by_double_ref
= false;
5527 /* Copy-return is used only for the first parameter of a valued procedure.
5528 It's a copy mechanism for which a parameter is never allocated. */
5529 if (mech
== By_Copy_Return
)
5531 gcc_assert (Ekind (gnat_param
) == E_Out_Parameter
);
5536 /* If this is either a foreign function or if the underlying type won't
5537 be passed by reference, strip off possible padding type. */
5538 if (TYPE_IS_PADDING_P (gnu_param_type
))
5540 tree unpadded_type
= TREE_TYPE (TYPE_FIELDS (gnu_param_type
));
5542 if (mech
== By_Reference
5544 || (!must_pass_by_ref (unpadded_type
)
5545 && (mech
== By_Copy
|| !default_pass_by_ref (unpadded_type
))))
5546 gnu_param_type
= unpadded_type
;
5549 /* If this is a read-only parameter, make a variant of the type that is
5550 read-only. ??? However, if this is an unconstrained array, that type
5551 can be very complex, so skip it for now. Likewise for any other
5552 self-referential type. */
5554 && TREE_CODE (gnu_param_type
) != UNCONSTRAINED_ARRAY_TYPE
5555 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type
)))
5556 gnu_param_type
= build_qualified_type (gnu_param_type
,
5557 (TYPE_QUALS (gnu_param_type
)
5558 | TYPE_QUAL_CONST
));
5560 /* For foreign conventions, pass arrays as pointers to the element type.
5561 First check for unconstrained array and get the underlying array. */
5562 if (foreign
&& TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5564 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type
))));
5566 /* For GCC builtins, pass Address integer types as (void *) */
5567 if (Convention (gnat_subprog
) == Convention_Intrinsic
5568 && Present (Interface_Name (gnat_subprog
))
5569 && Is_Descendent_Of_Address (Etype (gnat_param
)))
5570 gnu_param_type
= ptr_void_type_node
;
5572 /* VMS descriptors are themselves passed by reference. */
5573 if (mech
== By_Short_Descriptor
||
5574 (mech
== By_Descriptor
&& TARGET_ABI_OPEN_VMS
&& !flag_vms_malloc64
))
5576 = build_pointer_type (build_vms_descriptor32 (gnu_param_type
,
5577 Mechanism (gnat_param
),
5579 else if (mech
== By_Descriptor
)
5581 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5582 chosen in fill_vms_descriptor. */
5584 = build_pointer_type (build_vms_descriptor32 (gnu_param_type
,
5585 Mechanism (gnat_param
),
5588 = build_pointer_type (build_vms_descriptor (gnu_param_type
,
5589 Mechanism (gnat_param
),
5593 /* Arrays are passed as pointers to element type for foreign conventions. */
5596 && TREE_CODE (gnu_param_type
) == ARRAY_TYPE
)
5598 /* Strip off any multi-dimensional entries, then strip
5599 off the last array to get the component type. */
5600 while (TREE_CODE (TREE_TYPE (gnu_param_type
)) == ARRAY_TYPE
5601 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type
)))
5602 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5604 by_component_ptr
= true;
5605 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5608 gnu_param_type
= build_qualified_type (gnu_param_type
,
5609 (TYPE_QUALS (gnu_param_type
)
5610 | TYPE_QUAL_CONST
));
5612 gnu_param_type
= build_pointer_type (gnu_param_type
);
5615 /* Fat pointers are passed as thin pointers for foreign conventions. */
5616 else if (foreign
&& TYPE_IS_FAT_POINTER_P (gnu_param_type
))
5618 = make_type_from_size (gnu_param_type
, size_int (POINTER_SIZE
), 0);
5620 /* If we must pass or were requested to pass by reference, do so.
5621 If we were requested to pass by copy, do so.
5622 Otherwise, for foreign conventions, pass In Out or Out parameters
5623 or aggregates by reference. For COBOL and Fortran, pass all
5624 integer and FP types that way too. For Convention Ada, use
5625 the standard Ada default. */
5626 else if (must_pass_by_ref (gnu_param_type
)
5627 || mech
== By_Reference
5630 && (!in_param
|| AGGREGATE_TYPE_P (gnu_param_type
)))
5632 && (Convention (gnat_subprog
) == Convention_Fortran
5633 || Convention (gnat_subprog
) == Convention_COBOL
)
5634 && (INTEGRAL_TYPE_P (gnu_param_type
)
5635 || FLOAT_TYPE_P (gnu_param_type
)))
5637 && default_pass_by_ref (gnu_param_type
)))))
5639 /* We take advantage of 6.2(12) by considering that references built for
5640 parameters whose type isn't by-ref and for which the mechanism hasn't
5641 been forced to by-ref are restrict-qualified in the C sense. */
5643 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type
) && mech
!= By_Reference
;
5644 gnu_param_type
= build_reference_type (gnu_param_type
);
5647 = build_qualified_type (gnu_param_type
, TYPE_QUAL_RESTRICT
);
5650 /* In some ABIs, e.g. SPARC 32-bit, fat pointer types are themselves
5651 passed by reference. Pass them by explicit reference, this will
5652 generate more debuggable code at -O0. */
5653 if (TYPE_IS_FAT_POINTER_P (gnu_param_type
)
5654 && targetm
.calls
.pass_by_reference (pack_cumulative_args (NULL
),
5655 TYPE_MODE (gnu_param_type
),
5659 gnu_param_type
= build_reference_type (gnu_param_type
);
5660 by_double_ref
= true;
5664 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5668 if (mech
== By_Copy
&& (by_ref
|| by_component_ptr
))
5669 post_error ("?cannot pass & by copy", gnat_param
);
5671 /* If this is an Out parameter that isn't passed by reference and isn't
5672 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5673 it will be a VAR_DECL created when we process the procedure, so just
5674 return its type. For the special parameter of a valued procedure,
5677 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5678 Out parameters with discriminants or implicit initial values to be
5679 handled like In Out parameters. These type are normally built as
5680 aggregates, hence passed by reference, except for some packed arrays
5681 which end up encoded in special integer types.
5683 The exception we need to make is then for packed arrays of records
5684 with discriminants or implicit initial values. We have no light/easy
5685 way to check for the latter case, so we merely check for packed arrays
5686 of records. This may lead to useless copy-in operations, but in very
5687 rare cases only, as these would be exceptions in a set of already
5688 exceptional situations. */
5689 if (Ekind (gnat_param
) == E_Out_Parameter
5692 || (mech
!= By_Descriptor
5693 && mech
!= By_Short_Descriptor
5694 && !POINTER_TYPE_P (gnu_param_type
)
5695 && !AGGREGATE_TYPE_P (gnu_param_type
)))
5696 && !(Is_Array_Type (Etype (gnat_param
))
5697 && Is_Packed (Etype (gnat_param
))
5698 && Is_Composite_Type (Component_Type (Etype (gnat_param
)))))
5699 return gnu_param_type
;
5701 gnu_param
= create_param_decl (gnu_param_name
, gnu_param_type
,
5702 ro_param
|| by_ref
|| by_component_ptr
);
5703 DECL_BY_REF_P (gnu_param
) = by_ref
;
5704 DECL_BY_DOUBLE_REF_P (gnu_param
) = by_double_ref
;
5705 DECL_BY_COMPONENT_PTR_P (gnu_param
) = by_component_ptr
;
5706 DECL_BY_DESCRIPTOR_P (gnu_param
) = (mech
== By_Descriptor
||
5707 mech
== By_Short_Descriptor
);
5708 /* Note that, in case of a parameter passed by double reference, the
5709 DECL_POINTS_TO_READONLY_P flag is meant for the second reference.
5710 The first reference always points to read-only, as it points to
5711 the second reference, i.e. the reference to the actual parameter. */
5712 DECL_POINTS_TO_READONLY_P (gnu_param
)
5713 = (ro_param
&& (by_ref
|| by_component_ptr
));
5714 DECL_CAN_NEVER_BE_NULL_P (gnu_param
) = Can_Never_Be_Null (gnat_param
);
5716 /* Save the alternate descriptor type, if any. */
5717 if (gnu_param_type_alt
)
5718 SET_DECL_PARM_ALT_TYPE (gnu_param
, gnu_param_type_alt
);
5720 /* If no Mechanism was specified, indicate what we're using, then
5721 back-annotate it. */
5722 if (mech
== Default
)
5723 mech
= (by_ref
|| by_component_ptr
) ? By_Reference
: By_Copy
;
5725 Set_Mechanism (gnat_param
, mech
);
5729 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5732 same_discriminant_p (Entity_Id discr1
, Entity_Id discr2
)
5734 while (Present (Corresponding_Discriminant (discr1
)))
5735 discr1
= Corresponding_Discriminant (discr1
);
5737 while (Present (Corresponding_Discriminant (discr2
)))
5738 discr2
= Corresponding_Discriminant (discr2
);
5741 Original_Record_Component (discr1
) == Original_Record_Component (discr2
);
5744 /* Return true if the array type GNU_TYPE, which represents a dimension of
5745 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5748 array_type_has_nonaliased_component (tree gnu_type
, Entity_Id gnat_type
)
5750 /* If the array type is not the innermost dimension of the GNAT type,
5751 then it has a non-aliased component. */
5752 if (TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
5753 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
)))
5756 /* If the array type has an aliased component in the front-end sense,
5757 then it also has an aliased component in the back-end sense. */
5758 if (Has_Aliased_Components (gnat_type
))
5761 /* If this is a derived type, then it has a non-aliased component if
5762 and only if its parent type also has one. */
5763 if (Is_Derived_Type (gnat_type
))
5765 tree gnu_parent_type
= gnat_to_gnu_type (Etype (gnat_type
));
5767 if (TREE_CODE (gnu_parent_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5769 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type
))));
5770 for (index
= Number_Dimensions (gnat_type
) - 1; index
> 0; index
--)
5771 gnu_parent_type
= TREE_TYPE (gnu_parent_type
);
5772 return TYPE_NONALIASED_COMPONENT (gnu_parent_type
);
5775 /* Otherwise, rely exclusively on properties of the element type. */
5776 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type
));
5779 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5782 compile_time_known_address_p (Node_Id gnat_address
)
5784 /* Catch System'To_Address. */
5785 if (Nkind (gnat_address
) == N_Unchecked_Type_Conversion
)
5786 gnat_address
= Expression (gnat_address
);
5788 return Compile_Time_Known_Value (gnat_address
);
5791 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5792 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5795 cannot_be_superflat_p (Node_Id gnat_range
)
5797 Node_Id gnat_lb
= Low_Bound (gnat_range
), gnat_hb
= High_Bound (gnat_range
);
5798 Node_Id scalar_range
;
5799 tree gnu_lb
, gnu_hb
, gnu_lb_minus_one
;
5801 /* If the low bound is not constant, try to find an upper bound. */
5802 while (Nkind (gnat_lb
) != N_Integer_Literal
5803 && (Ekind (Etype (gnat_lb
)) == E_Signed_Integer_Subtype
5804 || Ekind (Etype (gnat_lb
)) == E_Modular_Integer_Subtype
)
5805 && (scalar_range
= Scalar_Range (Etype (gnat_lb
)))
5806 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
5807 || Nkind (scalar_range
) == N_Range
))
5808 gnat_lb
= High_Bound (scalar_range
);
5810 /* If the high bound is not constant, try to find a lower bound. */
5811 while (Nkind (gnat_hb
) != N_Integer_Literal
5812 && (Ekind (Etype (gnat_hb
)) == E_Signed_Integer_Subtype
5813 || Ekind (Etype (gnat_hb
)) == E_Modular_Integer_Subtype
)
5814 && (scalar_range
= Scalar_Range (Etype (gnat_hb
)))
5815 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
5816 || Nkind (scalar_range
) == N_Range
))
5817 gnat_hb
= Low_Bound (scalar_range
);
5819 /* If we have failed to find constant bounds, punt. */
5820 if (Nkind (gnat_lb
) != N_Integer_Literal
5821 || Nkind (gnat_hb
) != N_Integer_Literal
)
5824 /* We need at least a signed 64-bit type to catch most cases. */
5825 gnu_lb
= UI_To_gnu (Intval (gnat_lb
), sbitsizetype
);
5826 gnu_hb
= UI_To_gnu (Intval (gnat_hb
), sbitsizetype
);
5827 if (TREE_OVERFLOW (gnu_lb
) || TREE_OVERFLOW (gnu_hb
))
5830 /* If the low bound is the smallest integer, nothing can be smaller. */
5831 gnu_lb_minus_one
= size_binop (MINUS_EXPR
, gnu_lb
, sbitsize_one_node
);
5832 if (TREE_OVERFLOW (gnu_lb_minus_one
))
5835 return !tree_int_cst_lt (gnu_hb
, gnu_lb_minus_one
);
5838 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5841 constructor_address_p (tree gnu_expr
)
5843 while (TREE_CODE (gnu_expr
) == NOP_EXPR
5844 || TREE_CODE (gnu_expr
) == CONVERT_EXPR
5845 || TREE_CODE (gnu_expr
) == NON_LVALUE_EXPR
)
5846 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
5848 return (TREE_CODE (gnu_expr
) == ADDR_EXPR
5849 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == CONSTRUCTOR
);
5852 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5853 be elaborated at the point of its definition, but do nothing else. */
5856 elaborate_entity (Entity_Id gnat_entity
)
5858 switch (Ekind (gnat_entity
))
5860 case E_Signed_Integer_Subtype
:
5861 case E_Modular_Integer_Subtype
:
5862 case E_Enumeration_Subtype
:
5863 case E_Ordinary_Fixed_Point_Subtype
:
5864 case E_Decimal_Fixed_Point_Subtype
:
5865 case E_Floating_Point_Subtype
:
5867 Node_Id gnat_lb
= Type_Low_Bound (gnat_entity
);
5868 Node_Id gnat_hb
= Type_High_Bound (gnat_entity
);
5870 /* ??? Tests to avoid Constraint_Error in static expressions
5871 are needed until after the front stops generating bogus
5872 conversions on bounds of real types. */
5873 if (!Raises_Constraint_Error (gnat_lb
))
5874 elaborate_expression (gnat_lb
, gnat_entity
, get_identifier ("L"),
5875 true, false, Needs_Debug_Info (gnat_entity
));
5876 if (!Raises_Constraint_Error (gnat_hb
))
5877 elaborate_expression (gnat_hb
, gnat_entity
, get_identifier ("U"),
5878 true, false, Needs_Debug_Info (gnat_entity
));
5884 Node_Id full_definition
= Declaration_Node (gnat_entity
);
5885 Node_Id record_definition
= Type_Definition (full_definition
);
5887 /* If this is a record extension, go a level further to find the
5888 record definition. */
5889 if (Nkind (record_definition
) == N_Derived_Type_Definition
)
5890 record_definition
= Record_Extension_Part (record_definition
);
5894 case E_Record_Subtype
:
5895 case E_Private_Subtype
:
5896 case E_Limited_Private_Subtype
:
5897 case E_Record_Subtype_With_Private
:
5898 if (Is_Constrained (gnat_entity
)
5899 && Has_Discriminants (gnat_entity
)
5900 && Present (Discriminant_Constraint (gnat_entity
)))
5902 Node_Id gnat_discriminant_expr
;
5903 Entity_Id gnat_field
;
5906 = First_Discriminant (Implementation_Base_Type (gnat_entity
)),
5907 gnat_discriminant_expr
5908 = First_Elmt (Discriminant_Constraint (gnat_entity
));
5909 Present (gnat_field
);
5910 gnat_field
= Next_Discriminant (gnat_field
),
5911 gnat_discriminant_expr
= Next_Elmt (gnat_discriminant_expr
))
5912 /* ??? For now, ignore access discriminants. */
5913 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr
))))
5914 elaborate_expression (Node (gnat_discriminant_expr
),
5915 gnat_entity
, get_entity_name (gnat_field
),
5916 true, false, false);
5923 /* Return true if the size in units represented by GNU_SIZE can be handled by
5924 an allocation. If STATIC_P is true, consider only what can be done with a
5925 static allocation. */
5928 allocatable_size_p (tree gnu_size
, bool static_p
)
5930 /* We can allocate a fixed size if it hasn't overflowed and can be handled
5931 (efficiently) on the host. */
5932 if (TREE_CODE (gnu_size
) == INTEGER_CST
)
5933 return !TREE_OVERFLOW (gnu_size
) && host_integerp (gnu_size
, 1);
5935 /* We can allocate a variable size if this isn't a static allocation. */
5940 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5941 NAME, ARGS and ERROR_POINT. */
5944 prepend_one_attribute_to (struct attrib
** attr_list
,
5945 enum attr_type attr_type
,
5948 Node_Id attr_error_point
)
5950 struct attrib
* attr
= (struct attrib
*) xmalloc (sizeof (struct attrib
));
5952 attr
->type
= attr_type
;
5953 attr
->name
= attr_name
;
5954 attr
->args
= attr_args
;
5955 attr
->error_point
= attr_error_point
;
5957 attr
->next
= *attr_list
;
5961 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
5964 prepend_attributes (Entity_Id gnat_entity
, struct attrib
** attr_list
)
5968 /* Attributes are stored as Representation Item pragmas. */
5970 for (gnat_temp
= First_Rep_Item (gnat_entity
); Present (gnat_temp
);
5971 gnat_temp
= Next_Rep_Item (gnat_temp
))
5972 if (Nkind (gnat_temp
) == N_Pragma
)
5974 tree gnu_arg0
= NULL_TREE
, gnu_arg1
= NULL_TREE
;
5975 Node_Id gnat_assoc
= Pragma_Argument_Associations (gnat_temp
);
5976 enum attr_type etype
;
5978 /* Map the kind of pragma at hand. Skip if this is not one
5979 we know how to handle. */
5981 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp
))))
5983 case Pragma_Machine_Attribute
:
5984 etype
= ATTR_MACHINE_ATTRIBUTE
;
5987 case Pragma_Linker_Alias
:
5988 etype
= ATTR_LINK_ALIAS
;
5991 case Pragma_Linker_Section
:
5992 etype
= ATTR_LINK_SECTION
;
5995 case Pragma_Linker_Constructor
:
5996 etype
= ATTR_LINK_CONSTRUCTOR
;
5999 case Pragma_Linker_Destructor
:
6000 etype
= ATTR_LINK_DESTRUCTOR
;
6003 case Pragma_Weak_External
:
6004 etype
= ATTR_WEAK_EXTERNAL
;
6007 case Pragma_Thread_Local_Storage
:
6008 etype
= ATTR_THREAD_LOCAL_STORAGE
;
6015 /* See what arguments we have and turn them into GCC trees for
6016 attribute handlers. These expect identifier for strings. We
6017 handle at most two arguments, static expressions only. */
6019 if (Present (gnat_assoc
) && Present (First (gnat_assoc
)))
6021 Node_Id gnat_arg0
= Next (First (gnat_assoc
));
6022 Node_Id gnat_arg1
= Empty
;
6024 if (Present (gnat_arg0
)
6025 && Is_Static_Expression (Expression (gnat_arg0
)))
6027 gnu_arg0
= gnat_to_gnu (Expression (gnat_arg0
));
6029 if (TREE_CODE (gnu_arg0
) == STRING_CST
)
6030 gnu_arg0
= get_identifier (TREE_STRING_POINTER (gnu_arg0
));
6032 gnat_arg1
= Next (gnat_arg0
);
6035 if (Present (gnat_arg1
)
6036 && Is_Static_Expression (Expression (gnat_arg1
)))
6038 gnu_arg1
= gnat_to_gnu (Expression (gnat_arg1
));
6040 if (TREE_CODE (gnu_arg1
) == STRING_CST
)
6041 gnu_arg1
= get_identifier (TREE_STRING_POINTER (gnu_arg1
));
6045 /* Prepend to the list now. Make a list of the argument we might
6046 have, as GCC expects it. */
6047 prepend_one_attribute_to
6050 (gnu_arg1
!= NULL_TREE
)
6051 ? build_tree_list (NULL_TREE
, gnu_arg1
) : NULL_TREE
,
6052 Present (Next (First (gnat_assoc
)))
6053 ? Expression (Next (First (gnat_assoc
))) : gnat_temp
);
6057 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6058 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6059 return the GCC tree to use for that expression. GNU_NAME is the suffix
6060 to use if a variable needs to be created and DEFINITION is true if this
6061 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6062 otherwise, we are just elaborating the expression for side-effects. If
6063 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6064 isn't needed for code generation. */
6067 elaborate_expression (Node_Id gnat_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6068 bool definition
, bool need_value
, bool need_debug
)
6072 /* If we already elaborated this expression (e.g. it was involved
6073 in the definition of a private type), use the old value. */
6074 if (present_gnu_tree (gnat_expr
))
6075 return get_gnu_tree (gnat_expr
);
6077 /* If we don't need a value and this is static or a discriminant,
6078 we don't need to do anything. */
6080 && (Is_OK_Static_Expression (gnat_expr
)
6081 || (Nkind (gnat_expr
) == N_Identifier
6082 && Ekind (Entity (gnat_expr
)) == E_Discriminant
)))
6085 /* If it's a static expression, we don't need a variable for debugging. */
6086 if (need_debug
&& Is_OK_Static_Expression (gnat_expr
))
6089 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6090 gnu_expr
= elaborate_expression_1 (gnat_to_gnu (gnat_expr
), gnat_entity
,
6091 gnu_name
, definition
, need_debug
);
6093 /* Save the expression in case we try to elaborate this entity again. Since
6094 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6095 if (!CONTAINS_PLACEHOLDER_P (gnu_expr
))
6096 save_gnu_tree (gnat_expr
, gnu_expr
, true);
6098 return need_value
? gnu_expr
: error_mark_node
;
6101 /* Similar, but take a GNU expression and always return a result. */
6104 elaborate_expression_1 (tree gnu_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6105 bool definition
, bool need_debug
)
6107 const bool expr_public_p
= Is_Public (gnat_entity
);
6108 const bool expr_global_p
= expr_public_p
|| global_bindings_p ();
6109 bool expr_variable_p
, use_variable
;
6111 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6112 reference will have been replaced with a COMPONENT_REF when the type
6113 is being elaborated. However, there are some cases involving child
6114 types where we will. So convert it to a COMPONENT_REF. We hope it
6115 will be at the highest level of the expression in these cases. */
6116 if (TREE_CODE (gnu_expr
) == FIELD_DECL
)
6117 gnu_expr
= build3 (COMPONENT_REF
, TREE_TYPE (gnu_expr
),
6118 build0 (PLACEHOLDER_EXPR
, DECL_CONTEXT (gnu_expr
)),
6119 gnu_expr
, NULL_TREE
);
6121 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6122 that an expression cannot contain both a discriminant and a variable. */
6123 if (CONTAINS_PLACEHOLDER_P (gnu_expr
))
6126 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6127 a variable that is initialized to contain the expression when the package
6128 containing the definition is elaborated. If this entity is defined at top
6129 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6130 if this is necessary. */
6131 if (CONSTANT_CLASS_P (gnu_expr
))
6132 expr_variable_p
= false;
6135 /* Skip any conversions and simple arithmetics to see if the expression
6136 is based on a read-only variable.
6137 ??? This really should remain read-only, but we have to think about
6138 the typing of the tree here. */
6140 = skip_simple_arithmetic (remove_conversions (gnu_expr
, true));
6142 if (handled_component_p (inner
))
6144 HOST_WIDE_INT bitsize
, bitpos
;
6146 enum machine_mode mode
;
6147 int unsignedp
, volatilep
;
6149 inner
= get_inner_reference (inner
, &bitsize
, &bitpos
, &offset
,
6150 &mode
, &unsignedp
, &volatilep
, false);
6151 /* If the offset is variable, err on the side of caution. */
6158 && TREE_CODE (inner
) == VAR_DECL
6159 && (TREE_READONLY (inner
) || DECL_READONLY_ONCE_ELAB (inner
)));
6162 /* We only need to use the variable if we are in a global context since GCC
6163 can do the right thing in the local case. However, when not optimizing,
6164 use it for bounds of loop iteration scheme to avoid code duplication. */
6165 use_variable
= expr_variable_p
6168 && Is_Itype (gnat_entity
)
6169 && Nkind (Associated_Node_For_Itype (gnat_entity
))
6170 == N_Loop_Parameter_Specification
));
6172 /* Now create it, possibly only for debugging purposes. */
6173 if (use_variable
|| need_debug
)
6177 (create_concat_name (gnat_entity
, IDENTIFIER_POINTER (gnu_name
)),
6178 NULL_TREE
, TREE_TYPE (gnu_expr
), gnu_expr
, true, expr_public_p
,
6179 !definition
, expr_global_p
, !need_debug
, NULL
, gnat_entity
);
6185 return expr_variable_p
? gnat_save_expr (gnu_expr
) : gnu_expr
;
6188 /* Similar, but take an alignment factor and make it explicit in the tree. */
6191 elaborate_expression_2 (tree gnu_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6192 bool definition
, bool need_debug
, unsigned int align
)
6194 tree unit_align
= size_int (align
/ BITS_PER_UNIT
);
6196 size_binop (MULT_EXPR
,
6197 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR
,
6200 gnat_entity
, gnu_name
, definition
,
6205 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6206 the value passed against the list of choices. */
6209 choices_to_gnu (tree operand
, Node_Id choices
)
6213 tree result
= boolean_false_node
;
6214 tree this_test
, low
= 0, high
= 0, single
= 0;
6216 for (choice
= First (choices
); Present (choice
); choice
= Next (choice
))
6218 switch (Nkind (choice
))
6221 low
= gnat_to_gnu (Low_Bound (choice
));
6222 high
= gnat_to_gnu (High_Bound (choice
));
6225 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6226 build_binary_op (GE_EXPR
, boolean_type_node
,
6228 build_binary_op (LE_EXPR
, boolean_type_node
,
6233 case N_Subtype_Indication
:
6234 gnat_temp
= Range_Expression (Constraint (choice
));
6235 low
= gnat_to_gnu (Low_Bound (gnat_temp
));
6236 high
= gnat_to_gnu (High_Bound (gnat_temp
));
6239 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6240 build_binary_op (GE_EXPR
, boolean_type_node
,
6242 build_binary_op (LE_EXPR
, boolean_type_node
,
6247 case N_Expanded_Name
:
6248 /* This represents either a subtype range, an enumeration
6249 literal, or a constant Ekind says which. If an enumeration
6250 literal or constant, fall through to the next case. */
6251 if (Ekind (Entity (choice
)) != E_Enumeration_Literal
6252 && Ekind (Entity (choice
)) != E_Constant
)
6254 tree type
= gnat_to_gnu_type (Entity (choice
));
6256 low
= TYPE_MIN_VALUE (type
);
6257 high
= TYPE_MAX_VALUE (type
);
6260 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6261 build_binary_op (GE_EXPR
, boolean_type_node
,
6263 build_binary_op (LE_EXPR
, boolean_type_node
,
6268 /* ... fall through ... */
6270 case N_Character_Literal
:
6271 case N_Integer_Literal
:
6272 single
= gnat_to_gnu (choice
);
6273 this_test
= build_binary_op (EQ_EXPR
, boolean_type_node
, operand
,
6277 case N_Others_Choice
:
6278 this_test
= boolean_true_node
;
6285 result
= build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
, result
,
6292 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6293 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6296 adjust_packed (tree field_type
, tree record_type
, int packed
)
6298 /* If the field contains an item of variable size, we cannot pack it
6299 because we cannot create temporaries of non-fixed size in case
6300 we need to take the address of the field. See addressable_p and
6301 the notes on the addressability issues for further details. */
6302 if (type_has_variable_size (field_type
))
6305 /* If the alignment of the record is specified and the field type
6306 is over-aligned, request Storage_Unit alignment for the field. */
6309 if (TYPE_ALIGN (field_type
) > TYPE_ALIGN (record_type
))
6318 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6319 placed in GNU_RECORD_TYPE.
6321 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6322 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6323 record has a specified alignment.
6325 DEFINITION is true if this field is for a record being defined.
6327 DEBUG_INFO_P is true if we need to write debug information for types
6328 that we may create in the process. */
6331 gnat_to_gnu_field (Entity_Id gnat_field
, tree gnu_record_type
, int packed
,
6332 bool definition
, bool debug_info_p
)
6334 const Entity_Id gnat_field_type
= Etype (gnat_field
);
6335 tree gnu_field_type
= gnat_to_gnu_type (gnat_field_type
);
6336 tree gnu_field_id
= get_entity_name (gnat_field
);
6337 tree gnu_field
, gnu_size
, gnu_pos
;
6339 = (Treat_As_Volatile (gnat_field
) || Treat_As_Volatile (gnat_field_type
));
6340 bool needs_strict_alignment
6342 || Is_Aliased (gnat_field
)
6343 || Strict_Alignment (gnat_field_type
));
6345 /* If this field requires strict alignment, we cannot pack it because
6346 it would very likely be under-aligned in the record. */
6347 if (needs_strict_alignment
)
6350 packed
= adjust_packed (gnu_field_type
, gnu_record_type
, packed
);
6352 /* If a size is specified, use it. Otherwise, if the record type is packed,
6353 use the official RM size. See "Handling of Type'Size Values" in Einfo
6354 for further details. */
6355 if (Known_Esize (gnat_field
))
6356 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6357 gnat_field
, FIELD_DECL
, false, true);
6358 else if (packed
== 1)
6359 gnu_size
= validate_size (RM_Size (gnat_field_type
), gnu_field_type
,
6360 gnat_field
, FIELD_DECL
, false, true);
6362 gnu_size
= NULL_TREE
;
6364 /* If we have a specified size that is smaller than that of the field's type,
6365 or a position is specified, and the field's type is a record that doesn't
6366 require strict alignment, see if we can get either an integral mode form
6367 of the type or a smaller form. If we can, show a size was specified for
6368 the field if there wasn't one already, so we know to make this a bitfield
6369 and avoid making things wider.
6371 Changing to an integral mode form is useful when the record is packed as
6372 we can then place the field at a non-byte-aligned position and so achieve
6373 tighter packing. This is in addition required if the field shares a byte
6374 with another field and the front-end lets the back-end handle the access
6375 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6377 Changing to a smaller form is required if the specified size is smaller
6378 than that of the field's type and the type contains sub-fields that are
6379 padded, in order to avoid generating accesses to these sub-fields that
6380 are wider than the field.
6382 We avoid the transformation if it is not required or potentially useful,
6383 as it might entail an increase of the field's alignment and have ripple
6384 effects on the outer record type. A typical case is a field known to be
6385 byte-aligned and not to share a byte with another field. */
6386 if (!needs_strict_alignment
6387 && RECORD_OR_UNION_TYPE_P (gnu_field_type
)
6388 && !TYPE_FAT_POINTER_P (gnu_field_type
)
6389 && host_integerp (TYPE_SIZE (gnu_field_type
), 1)
6392 && (tree_int_cst_lt (gnu_size
, TYPE_SIZE (gnu_field_type
))
6393 || (Present (Component_Clause (gnat_field
))
6394 && !(UI_To_Int (Component_Bit_Offset (gnat_field
))
6395 % BITS_PER_UNIT
== 0
6396 && value_factor_p (gnu_size
, BITS_PER_UNIT
)))))))
6398 tree gnu_packable_type
= make_packable_type (gnu_field_type
, true);
6399 if (gnu_packable_type
!= gnu_field_type
)
6401 gnu_field_type
= gnu_packable_type
;
6403 gnu_size
= rm_size (gnu_field_type
);
6407 if (Is_Atomic (gnat_field
))
6408 check_ok_for_atomic (gnu_field_type
, gnat_field
, false);
6410 if (Present (Component_Clause (gnat_field
)))
6412 Entity_Id gnat_parent
6413 = Parent_Subtype (Underlying_Type (Scope (gnat_field
)));
6415 gnu_pos
= UI_To_gnu (Component_Bit_Offset (gnat_field
), bitsizetype
);
6416 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6417 gnat_field
, FIELD_DECL
, false, true);
6419 /* Ensure the position does not overlap with the parent subtype, if there
6420 is one. This test is omitted if the parent of the tagged type has a
6421 full rep clause since, in this case, component clauses are allowed to
6422 overlay the space allocated for the parent type and the front-end has
6423 checked that there are no overlapping components. */
6424 if (Present (gnat_parent
) && !Is_Fully_Repped_Tagged_Type (gnat_parent
))
6426 tree gnu_parent
= gnat_to_gnu_type (gnat_parent
);
6428 if (TREE_CODE (TYPE_SIZE (gnu_parent
)) == INTEGER_CST
6429 && tree_int_cst_lt (gnu_pos
, TYPE_SIZE (gnu_parent
)))
6432 ("offset of& must be beyond parent{, minimum allowed is ^}",
6433 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6434 TYPE_SIZE_UNIT (gnu_parent
));
6438 /* If this field needs strict alignment, check that the record is
6439 sufficiently aligned and that position and size are consistent
6440 with the alignment. But don't do it if we are just annotating
6441 types and the field's type is tagged, since tagged types aren't
6442 fully laid out in this mode. */
6443 if (needs_strict_alignment
6444 && !(type_annotate_only
&& Is_Tagged_Type (gnat_field_type
)))
6446 TYPE_ALIGN (gnu_record_type
)
6447 = MAX (TYPE_ALIGN (gnu_record_type
), TYPE_ALIGN (gnu_field_type
));
6450 && !operand_equal_p (gnu_size
, TYPE_SIZE (gnu_field_type
), 0))
6452 if (Is_Atomic (gnat_field
) || Is_Atomic (gnat_field_type
))
6454 ("atomic field& must be natural size of type{ (^)}",
6455 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6456 TYPE_SIZE (gnu_field_type
));
6458 else if (Is_Aliased (gnat_field
))
6460 ("size of aliased field& must be ^ bits",
6461 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6462 TYPE_SIZE (gnu_field_type
));
6464 else if (Strict_Alignment (gnat_field_type
))
6466 ("size of & with aliased or tagged components not ^ bits",
6467 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6468 TYPE_SIZE (gnu_field_type
));
6470 gnu_size
= NULL_TREE
;
6473 if (!integer_zerop (size_binop
6474 (TRUNC_MOD_EXPR
, gnu_pos
,
6475 bitsize_int (TYPE_ALIGN (gnu_field_type
)))))
6479 ("position of volatile field& must be multiple of ^ bits",
6480 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6481 TYPE_ALIGN (gnu_field_type
));
6483 else if (Is_Aliased (gnat_field
))
6485 ("position of aliased field& must be multiple of ^ bits",
6486 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6487 TYPE_ALIGN (gnu_field_type
));
6489 else if (Strict_Alignment (gnat_field_type
))
6491 ("position of & is not compatible with alignment required "
6492 "by its components",
6493 First_Bit (Component_Clause (gnat_field
)), gnat_field
);
6498 gnu_pos
= NULL_TREE
;
6503 /* If the record has rep clauses and this is the tag field, make a rep
6504 clause for it as well. */
6505 else if (Has_Specified_Layout (Scope (gnat_field
))
6506 && Chars (gnat_field
) == Name_uTag
)
6508 gnu_pos
= bitsize_zero_node
;
6509 gnu_size
= TYPE_SIZE (gnu_field_type
);
6514 gnu_pos
= NULL_TREE
;
6516 /* If we are packing the record and the field is BLKmode, round the
6517 size up to a byte boundary. */
6518 if (packed
&& TYPE_MODE (gnu_field_type
) == BLKmode
&& gnu_size
)
6519 gnu_size
= round_up (gnu_size
, BITS_PER_UNIT
);
6522 /* We need to make the size the maximum for the type if it is
6523 self-referential and an unconstrained type. In that case, we can't
6524 pack the field since we can't make a copy to align it. */
6525 if (TREE_CODE (gnu_field_type
) == RECORD_TYPE
6527 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type
))
6528 && !Is_Constrained (Underlying_Type (gnat_field_type
)))
6530 gnu_size
= max_size (TYPE_SIZE (gnu_field_type
), true);
6534 /* If a size is specified, adjust the field's type to it. */
6537 tree orig_field_type
;
6539 /* If the field's type is justified modular, we would need to remove
6540 the wrapper to (better) meet the layout requirements. However we
6541 can do so only if the field is not aliased to preserve the unique
6542 layout and if the prescribed size is not greater than that of the
6543 packed array to preserve the justification. */
6544 if (!needs_strict_alignment
6545 && TREE_CODE (gnu_field_type
) == RECORD_TYPE
6546 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type
)
6547 && tree_int_cst_compare (gnu_size
, TYPE_ADA_SIZE (gnu_field_type
))
6549 gnu_field_type
= TREE_TYPE (TYPE_FIELDS (gnu_field_type
));
6552 = make_type_from_size (gnu_field_type
, gnu_size
,
6553 Has_Biased_Representation (gnat_field
));
6555 orig_field_type
= gnu_field_type
;
6556 gnu_field_type
= maybe_pad_type (gnu_field_type
, gnu_size
, 0, gnat_field
,
6557 false, false, definition
, true);
6559 /* If a padding record was made, declare it now since it will never be
6560 declared otherwise. This is necessary to ensure that its subtrees
6561 are properly marked. */
6562 if (gnu_field_type
!= orig_field_type
6563 && !DECL_P (TYPE_NAME (gnu_field_type
)))
6564 create_type_decl (TYPE_NAME (gnu_field_type
), gnu_field_type
, NULL
,
6565 true, debug_info_p
, gnat_field
);
6568 /* Otherwise (or if there was an error), don't specify a position. */
6570 gnu_pos
= NULL_TREE
;
6572 gcc_assert (TREE_CODE (gnu_field_type
) != RECORD_TYPE
6573 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type
));
6575 /* Now create the decl for the field. */
6577 = create_field_decl (gnu_field_id
, gnu_field_type
, gnu_record_type
,
6578 gnu_size
, gnu_pos
, packed
, Is_Aliased (gnat_field
));
6579 Sloc_to_locus (Sloc (gnat_field
), &DECL_SOURCE_LOCATION (gnu_field
));
6580 DECL_ALIASED_P (gnu_field
) = Is_Aliased (gnat_field
);
6581 TREE_THIS_VOLATILE (gnu_field
) = TREE_SIDE_EFFECTS (gnu_field
) = is_volatile
;
6583 if (Ekind (gnat_field
) == E_Discriminant
)
6584 DECL_DISCRIMINANT_NUMBER (gnu_field
)
6585 = UI_To_gnu (Discriminant_Number (gnat_field
), sizetype
);
6590 /* Return true if TYPE is a type with variable size or a padding type with a
6591 field of variable size or a record that has a field with such a type. */
6594 type_has_variable_size (tree type
)
6598 if (!TREE_CONSTANT (TYPE_SIZE (type
)))
6601 if (TYPE_IS_PADDING_P (type
)
6602 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type
))))
6605 if (!RECORD_OR_UNION_TYPE_P (type
))
6608 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
6609 if (type_has_variable_size (TREE_TYPE (field
)))
6615 /* Return true if FIELD is an artificial field. */
6618 field_is_artificial (tree field
)
6620 /* These fields are generated by the front-end proper. */
6621 if (IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == '_')
6624 /* These fields are generated by gigi. */
6625 if (DECL_INTERNAL_P (field
))
6631 /* Return true if FIELD is a non-artificial aliased field. */
6634 field_is_aliased (tree field
)
6636 if (field_is_artificial (field
))
6639 return DECL_ALIASED_P (field
);
6642 /* Return true if FIELD is a non-artificial field with self-referential
6646 field_has_self_size (tree field
)
6648 if (field_is_artificial (field
))
6651 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
6654 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field
)));
6657 /* Return true if FIELD is a non-artificial field with variable size. */
6660 field_has_variable_size (tree field
)
6662 if (field_is_artificial (field
))
6665 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
6668 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field
))) != INTEGER_CST
;
6671 /* qsort comparer for the bit positions of two record components. */
6674 compare_field_bitpos (const PTR rt1
, const PTR rt2
)
6676 const_tree
const field1
= * (const_tree
const *) rt1
;
6677 const_tree
const field2
= * (const_tree
const *) rt2
;
6679 = tree_int_cst_compare (bit_position (field1
), bit_position (field2
));
6681 return ret
? ret
: (int) (DECL_UID (field1
) - DECL_UID (field2
));
6684 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
6685 the result as the field list of GNU_RECORD_TYPE and finish it up. When
6686 called from gnat_to_gnu_entity during the processing of a record type
6687 definition, the GCC node for the parent, if any, will be the single field
6688 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6689 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6690 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6692 PACKED is 1 if this is for a packed record, -1 if this is for a record
6693 with Component_Alignment of Storage_Unit, -2 if this is for a record
6694 with a specified alignment.
6696 DEFINITION is true if we are defining this record type.
6698 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6699 out the record. This means the alignment only serves to force fields to
6700 be bitfields, but not to require the record to be that aligned. This is
6703 ALL_REP is true if a rep clause is present for all the fields.
6705 UNCHECKED_UNION is true if we are building this type for a record with a
6706 Pragma Unchecked_Union.
6708 ARTIFICIAL is true if this is a type that was generated by the compiler.
6710 DEBUG_INFO is true if we need to write debug information about the type.
6712 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6713 mean that its contents may be unused as well, only the container itself.
6715 REORDER is true if we are permitted to reorder components of this type.
6717 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6718 the outer record type down to this variant level. It is nonzero only if
6719 all the fields down to this level have a rep clause and ALL_REP is false.
6721 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6722 with a rep clause is to be added; in this case, that is all that should
6723 be done with such fields. */
6726 components_to_record (tree gnu_record_type
, Node_Id gnat_component_list
,
6727 tree gnu_field_list
, int packed
, bool definition
,
6728 bool cancel_alignment
, bool all_rep
,
6729 bool unchecked_union
, bool artificial
,
6730 bool debug_info
, bool maybe_unused
, bool reorder
,
6731 tree first_free_pos
, tree
*p_gnu_rep_list
)
6733 bool all_rep_and_size
= all_rep
&& TYPE_SIZE (gnu_record_type
);
6734 bool layout_with_rep
= false;
6735 bool has_self_field
= false;
6736 bool has_aliased_after_self_field
= false;
6737 Node_Id component_decl
, variant_part
;
6738 tree gnu_field
, gnu_next
, gnu_last
;
6739 tree gnu_rep_part
= NULL_TREE
;
6740 tree gnu_variant_part
= NULL_TREE
;
6741 tree gnu_rep_list
= NULL_TREE
;
6742 tree gnu_var_list
= NULL_TREE
;
6743 tree gnu_self_list
= NULL_TREE
;
6745 /* For each component referenced in a component declaration create a GCC
6746 field and add it to the list, skipping pragmas in the GNAT list. */
6747 gnu_last
= tree_last (gnu_field_list
);
6748 if (Present (Component_Items (gnat_component_list
)))
6750 = First_Non_Pragma (Component_Items (gnat_component_list
));
6751 Present (component_decl
);
6752 component_decl
= Next_Non_Pragma (component_decl
))
6754 Entity_Id gnat_field
= Defining_Entity (component_decl
);
6755 Name_Id gnat_name
= Chars (gnat_field
);
6757 /* If present, the _Parent field must have been created as the single
6758 field of the record type. Put it before any other fields. */
6759 if (gnat_name
== Name_uParent
)
6761 gnu_field
= TYPE_FIELDS (gnu_record_type
);
6762 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
6766 gnu_field
= gnat_to_gnu_field (gnat_field
, gnu_record_type
, packed
,
6767 definition
, debug_info
);
6769 /* If this is the _Tag field, put it before any other fields. */
6770 if (gnat_name
== Name_uTag
)
6771 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
6773 /* If this is the _Controller field, put it before the other
6774 fields except for the _Tag or _Parent field. */
6775 else if (gnat_name
== Name_uController
&& gnu_last
)
6777 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
6778 DECL_CHAIN (gnu_last
) = gnu_field
;
6781 /* If this is a regular field, put it after the other fields. */
6784 DECL_CHAIN (gnu_field
) = gnu_field_list
;
6785 gnu_field_list
= gnu_field
;
6787 gnu_last
= gnu_field
;
6789 /* And record information for the final layout. */
6790 if (field_has_self_size (gnu_field
))
6791 has_self_field
= true;
6792 else if (has_self_field
&& field_is_aliased (gnu_field
))
6793 has_aliased_after_self_field
= true;
6797 save_gnu_tree (gnat_field
, gnu_field
, false);
6800 /* At the end of the component list there may be a variant part. */
6801 variant_part
= Variant_Part (gnat_component_list
);
6803 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6804 mutually exclusive and should go in the same memory. To do this we need
6805 to treat each variant as a record whose elements are created from the
6806 component list for the variant. So here we create the records from the
6807 lists for the variants and put them all into the QUAL_UNION_TYPE.
6808 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6809 use GNU_RECORD_TYPE if there are no fields so far. */
6810 if (Present (variant_part
))
6812 Node_Id gnat_discr
= Name (variant_part
), variant
;
6813 tree gnu_discr
= gnat_to_gnu (gnat_discr
);
6814 tree gnu_name
= TYPE_NAME (gnu_record_type
);
6816 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr
))),
6818 tree gnu_union_type
, gnu_union_name
;
6819 tree this_first_free_pos
, gnu_variant_list
= NULL_TREE
;
6821 if (TREE_CODE (gnu_name
) == TYPE_DECL
)
6822 gnu_name
= DECL_NAME (gnu_name
);
6825 = concat_name (gnu_name
, IDENTIFIER_POINTER (gnu_var_name
));
6827 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
6828 are all in the variant part, to match the layout of C unions. There
6829 is an associated check below. */
6830 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
)
6831 gnu_union_type
= gnu_record_type
;
6835 = make_node (unchecked_union
? UNION_TYPE
: QUAL_UNION_TYPE
);
6837 TYPE_NAME (gnu_union_type
) = gnu_union_name
;
6838 TYPE_ALIGN (gnu_union_type
) = 0;
6839 TYPE_PACKED (gnu_union_type
) = TYPE_PACKED (gnu_record_type
);
6842 /* If all the fields down to this level have a rep clause, find out
6843 whether all the fields at this level also have one. If so, then
6844 compute the new first free position to be passed downward. */
6845 this_first_free_pos
= first_free_pos
;
6846 if (this_first_free_pos
)
6848 for (gnu_field
= gnu_field_list
;
6850 gnu_field
= DECL_CHAIN (gnu_field
))
6851 if (DECL_FIELD_OFFSET (gnu_field
))
6853 tree pos
= bit_position (gnu_field
);
6854 if (!tree_int_cst_lt (pos
, this_first_free_pos
))
6856 = size_binop (PLUS_EXPR
, pos
, DECL_SIZE (gnu_field
));
6860 this_first_free_pos
= NULL_TREE
;
6865 for (variant
= First_Non_Pragma (Variants (variant_part
));
6867 variant
= Next_Non_Pragma (variant
))
6869 tree gnu_variant_type
= make_node (RECORD_TYPE
);
6870 tree gnu_inner_name
;
6873 Get_Variant_Encoding (variant
);
6874 gnu_inner_name
= get_identifier_with_length (Name_Buffer
, Name_Len
);
6875 TYPE_NAME (gnu_variant_type
)
6876 = concat_name (gnu_union_name
,
6877 IDENTIFIER_POINTER (gnu_inner_name
));
6879 /* Set the alignment of the inner type in case we need to make
6880 inner objects into bitfields, but then clear it out so the
6881 record actually gets only the alignment required. */
6882 TYPE_ALIGN (gnu_variant_type
) = TYPE_ALIGN (gnu_record_type
);
6883 TYPE_PACKED (gnu_variant_type
) = TYPE_PACKED (gnu_record_type
);
6885 /* Similarly, if the outer record has a size specified and all
6886 the fields have a rep clause, we can propagate the size. */
6887 if (all_rep_and_size
)
6889 TYPE_SIZE (gnu_variant_type
) = TYPE_SIZE (gnu_record_type
);
6890 TYPE_SIZE_UNIT (gnu_variant_type
)
6891 = TYPE_SIZE_UNIT (gnu_record_type
);
6894 /* Add the fields into the record type for the variant. Note that
6895 we aren't sure to really use it at this point, see below. */
6896 components_to_record (gnu_variant_type
, Component_List (variant
),
6897 NULL_TREE
, packed
, definition
,
6898 !all_rep_and_size
, all_rep
, unchecked_union
,
6899 true, debug_info
, true, reorder
,
6900 this_first_free_pos
,
6901 all_rep
|| this_first_free_pos
6902 ? NULL
: &gnu_rep_list
);
6904 gnu_qual
= choices_to_gnu (gnu_discr
, Discrete_Choices (variant
));
6905 Set_Present_Expr (variant
, annotate_value (gnu_qual
));
6907 /* If this is an Unchecked_Union whose fields are all in the variant
6908 part and we have a single field with no representation clause or
6909 placed at offset zero, use the field directly to match the layout
6911 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
6912 && (gnu_field
= TYPE_FIELDS (gnu_variant_type
)) != NULL_TREE
6913 && !DECL_CHAIN (gnu_field
)
6914 && (!DECL_FIELD_OFFSET (gnu_field
)
6915 || integer_zerop (bit_position (gnu_field
))))
6916 DECL_CONTEXT (gnu_field
) = gnu_union_type
;
6919 /* Deal with packedness like in gnat_to_gnu_field. */
6921 = adjust_packed (gnu_variant_type
, gnu_record_type
, packed
);
6923 /* Finalize the record type now. We used to throw away
6924 empty records but we no longer do that because we need
6925 them to generate complete debug info for the variant;
6926 otherwise, the union type definition will be lacking
6927 the fields associated with these empty variants. */
6928 rest_of_record_type_compilation (gnu_variant_type
);
6929 create_type_decl (TYPE_NAME (gnu_variant_type
), gnu_variant_type
,
6930 NULL
, true, debug_info
, gnat_component_list
);
6933 = create_field_decl (gnu_inner_name
, gnu_variant_type
,
6936 ? TYPE_SIZE (gnu_variant_type
) : 0,
6938 ? bitsize_zero_node
: 0,
6941 DECL_INTERNAL_P (gnu_field
) = 1;
6943 if (!unchecked_union
)
6944 DECL_QUALIFIER (gnu_field
) = gnu_qual
;
6947 DECL_CHAIN (gnu_field
) = gnu_variant_list
;
6948 gnu_variant_list
= gnu_field
;
6951 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
6952 if (gnu_variant_list
)
6954 int union_field_packed
;
6956 if (all_rep_and_size
)
6958 TYPE_SIZE (gnu_union_type
) = TYPE_SIZE (gnu_record_type
);
6959 TYPE_SIZE_UNIT (gnu_union_type
)
6960 = TYPE_SIZE_UNIT (gnu_record_type
);
6963 finish_record_type (gnu_union_type
, nreverse (gnu_variant_list
),
6964 all_rep_and_size
? 1 : 0, debug_info
);
6966 /* If GNU_UNION_TYPE is our record type, it means we must have an
6967 Unchecked_Union with no fields. Verify that and, if so, just
6969 if (gnu_union_type
== gnu_record_type
)
6971 gcc_assert (unchecked_union
6977 create_type_decl (TYPE_NAME (gnu_union_type
), gnu_union_type
,
6978 NULL
, true, debug_info
, gnat_component_list
);
6980 /* Deal with packedness like in gnat_to_gnu_field. */
6982 = adjust_packed (gnu_union_type
, gnu_record_type
, packed
);
6985 = create_field_decl (gnu_var_name
, gnu_union_type
, gnu_record_type
,
6986 all_rep
? TYPE_SIZE (gnu_union_type
) : 0,
6987 all_rep
|| this_first_free_pos
6988 ? bitsize_zero_node
: 0,
6989 union_field_packed
, 0);
6991 DECL_INTERNAL_P (gnu_variant_part
) = 1;
6995 /* From now on, a zero FIRST_FREE_POS is totally useless. */
6996 if (first_free_pos
&& integer_zerop (first_free_pos
))
6997 first_free_pos
= NULL_TREE
;
6999 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7000 permitted to reorder components, self-referential sizes or variable sizes.
7001 If they do, pull them out and put them onto the appropriate list. We have
7002 to do this in a separate pass since we want to handle the discriminants
7003 but can't play with them until we've used them in debugging data above.
7005 ??? If we reorder them, debugging information will be wrong but there is
7006 nothing that can be done about this at the moment. */
7007 gnu_last
= NULL_TREE
;
7009 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7012 DECL_CHAIN (gnu_last) = gnu_next; \
7014 gnu_field_list = gnu_next; \
7016 DECL_CHAIN (gnu_field) = (LIST); \
7017 (LIST) = gnu_field; \
7020 for (gnu_field
= gnu_field_list
; gnu_field
; gnu_field
= gnu_next
)
7022 gnu_next
= DECL_CHAIN (gnu_field
);
7024 if (DECL_FIELD_OFFSET (gnu_field
))
7026 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list
);
7030 if ((reorder
|| has_aliased_after_self_field
)
7031 && field_has_self_size (gnu_field
))
7033 MOVE_FROM_FIELD_LIST_TO (gnu_self_list
);
7037 if (reorder
&& field_has_variable_size (gnu_field
))
7039 MOVE_FROM_FIELD_LIST_TO (gnu_var_list
);
7043 gnu_last
= gnu_field
;
7046 #undef MOVE_FROM_FIELD_LIST_TO
7048 /* If permitted, we reorder the fields as follows:
7050 1) all fixed length fields,
7051 2) all fields whose length doesn't depend on discriminants,
7052 3) all fields whose length depends on discriminants,
7053 4) the variant part,
7055 within the record and within each variant recursively. */
7058 = chainon (nreverse (gnu_self_list
),
7059 chainon (nreverse (gnu_var_list
), gnu_field_list
));
7061 /* Otherwise, if there is an aliased field placed after a field whose length
7062 depends on discriminants, we put all the fields of the latter sort, last.
7063 We need to do this in case an object of this record type is mutable. */
7064 else if (has_aliased_after_self_field
)
7065 gnu_field_list
= chainon (nreverse (gnu_self_list
), gnu_field_list
);
7067 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7068 in our REP list to the previous level because this level needs them in
7069 order to do a correct layout, i.e. avoid having overlapping fields. */
7070 if (p_gnu_rep_list
&& gnu_rep_list
)
7071 *p_gnu_rep_list
= chainon (*p_gnu_rep_list
, gnu_rep_list
);
7073 /* Otherwise, sort the fields by bit position and put them into their own
7074 record, before the others, if we also have fields without rep clause. */
7075 else if (gnu_rep_list
)
7078 = (gnu_field_list
? make_node (RECORD_TYPE
) : gnu_record_type
);
7079 int i
, len
= list_length (gnu_rep_list
);
7080 tree
*gnu_arr
= XALLOCAVEC (tree
, len
);
7082 for (gnu_field
= gnu_rep_list
, i
= 0;
7084 gnu_field
= DECL_CHAIN (gnu_field
), i
++)
7085 gnu_arr
[i
] = gnu_field
;
7087 qsort (gnu_arr
, len
, sizeof (tree
), compare_field_bitpos
);
7089 /* Put the fields in the list in order of increasing position, which
7090 means we start from the end. */
7091 gnu_rep_list
= NULL_TREE
;
7092 for (i
= len
- 1; i
>= 0; i
--)
7094 DECL_CHAIN (gnu_arr
[i
]) = gnu_rep_list
;
7095 gnu_rep_list
= gnu_arr
[i
];
7096 DECL_CONTEXT (gnu_arr
[i
]) = gnu_rep_type
;
7101 finish_record_type (gnu_rep_type
, gnu_rep_list
, 1, debug_info
);
7103 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7104 without rep clause are laid out starting from this position.
7105 Therefore, we force it as a minimal size on the REP part. */
7107 = create_rep_part (gnu_rep_type
, gnu_record_type
, first_free_pos
);
7111 layout_with_rep
= true;
7112 gnu_field_list
= nreverse (gnu_rep_list
);
7116 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields without
7117 rep clause are laid out starting from this position. Therefore, if we
7118 have not already done so, we create a fake REP part with this size. */
7119 if (first_free_pos
&& !layout_with_rep
&& !gnu_rep_part
)
7121 tree gnu_rep_type
= make_node (RECORD_TYPE
);
7122 finish_record_type (gnu_rep_type
, NULL_TREE
, 0, debug_info
);
7124 = create_rep_part (gnu_rep_type
, gnu_record_type
, first_free_pos
);
7127 /* Now chain the REP part at the end of the reversed field list. */
7129 gnu_field_list
= chainon (gnu_field_list
, gnu_rep_part
);
7131 /* And the variant part at the beginning. */
7132 if (gnu_variant_part
)
7134 DECL_CHAIN (gnu_variant_part
) = gnu_field_list
;
7135 gnu_field_list
= gnu_variant_part
;
7138 if (cancel_alignment
)
7139 TYPE_ALIGN (gnu_record_type
) = 0;
7141 finish_record_type (gnu_record_type
, nreverse (gnu_field_list
),
7142 layout_with_rep
? 1 : 0, false);
7143 TYPE_ARTIFICIAL (gnu_record_type
) = artificial
;
7144 if (debug_info
&& !maybe_unused
)
7145 rest_of_record_type_compilation (gnu_record_type
);
7148 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7149 placed into an Esize, Component_Bit_Offset, or Component_Size value
7150 in the GNAT tree. */
7153 annotate_value (tree gnu_size
)
7156 Node_Ref_Or_Val ops
[3], ret
;
7157 struct tree_int_map in
;
7160 /* See if we've already saved the value for this node. */
7161 if (EXPR_P (gnu_size
))
7163 struct tree_int_map
*e
;
7165 if (!annotate_value_cache
)
7166 annotate_value_cache
= htab_create_ggc (512, tree_int_map_hash
,
7167 tree_int_map_eq
, 0);
7168 in
.base
.from
= gnu_size
;
7169 e
= (struct tree_int_map
*)
7170 htab_find (annotate_value_cache
, &in
);
7173 return (Node_Ref_Or_Val
) e
->to
;
7176 in
.base
.from
= NULL_TREE
;
7178 /* If we do not return inside this switch, TCODE will be set to the
7179 code to use for a Create_Node operand and LEN (set above) will be
7180 the number of recursive calls for us to make. */
7182 switch (TREE_CODE (gnu_size
))
7185 if (TREE_OVERFLOW (gnu_size
))
7188 /* This may come from a conversion from some smaller type, so ensure
7189 this is in bitsizetype. */
7190 gnu_size
= convert (bitsizetype
, gnu_size
);
7192 /* For a negative value, build NEGATE_EXPR of the opposite. Such values
7193 appear in expressions containing aligning patterns. Note that, since
7194 sizetype is sign-extended but nonetheless unsigned, we don't directly
7195 use tree_int_cst_sgn. */
7196 if (TREE_INT_CST_HIGH (gnu_size
) < 0)
7198 tree op_size
= fold_build1 (NEGATE_EXPR
, bitsizetype
, gnu_size
);
7199 return annotate_value (build1 (NEGATE_EXPR
, bitsizetype
, op_size
));
7202 return UI_From_gnu (gnu_size
);
7205 /* The only case we handle here is a simple discriminant reference. */
7206 if (TREE_CODE (TREE_OPERAND (gnu_size
, 0)) == PLACEHOLDER_EXPR
7207 && TREE_CODE (TREE_OPERAND (gnu_size
, 1)) == FIELD_DECL
7208 && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size
, 1)))
7209 return Create_Node (Discrim_Val
,
7210 annotate_value (DECL_DISCRIMINANT_NUMBER
7211 (TREE_OPERAND (gnu_size
, 1))),
7216 CASE_CONVERT
: case NON_LVALUE_EXPR
:
7217 return annotate_value (TREE_OPERAND (gnu_size
, 0));
7219 /* Now just list the operations we handle. */
7220 case COND_EXPR
: tcode
= Cond_Expr
; break;
7221 case PLUS_EXPR
: tcode
= Plus_Expr
; break;
7222 case MINUS_EXPR
: tcode
= Minus_Expr
; break;
7223 case MULT_EXPR
: tcode
= Mult_Expr
; break;
7224 case TRUNC_DIV_EXPR
: tcode
= Trunc_Div_Expr
; break;
7225 case CEIL_DIV_EXPR
: tcode
= Ceil_Div_Expr
; break;
7226 case FLOOR_DIV_EXPR
: tcode
= Floor_Div_Expr
; break;
7227 case TRUNC_MOD_EXPR
: tcode
= Trunc_Mod_Expr
; break;
7228 case CEIL_MOD_EXPR
: tcode
= Ceil_Mod_Expr
; break;
7229 case FLOOR_MOD_EXPR
: tcode
= Floor_Mod_Expr
; break;
7230 case EXACT_DIV_EXPR
: tcode
= Exact_Div_Expr
; break;
7231 case NEGATE_EXPR
: tcode
= Negate_Expr
; break;
7232 case MIN_EXPR
: tcode
= Min_Expr
; break;
7233 case MAX_EXPR
: tcode
= Max_Expr
; break;
7234 case ABS_EXPR
: tcode
= Abs_Expr
; break;
7235 case TRUTH_ANDIF_EXPR
: tcode
= Truth_Andif_Expr
; break;
7236 case TRUTH_ORIF_EXPR
: tcode
= Truth_Orif_Expr
; break;
7237 case TRUTH_AND_EXPR
: tcode
= Truth_And_Expr
; break;
7238 case TRUTH_OR_EXPR
: tcode
= Truth_Or_Expr
; break;
7239 case TRUTH_XOR_EXPR
: tcode
= Truth_Xor_Expr
; break;
7240 case TRUTH_NOT_EXPR
: tcode
= Truth_Not_Expr
; break;
7241 case BIT_AND_EXPR
: tcode
= Bit_And_Expr
; break;
7242 case LT_EXPR
: tcode
= Lt_Expr
; break;
7243 case LE_EXPR
: tcode
= Le_Expr
; break;
7244 case GT_EXPR
: tcode
= Gt_Expr
; break;
7245 case GE_EXPR
: tcode
= Ge_Expr
; break;
7246 case EQ_EXPR
: tcode
= Eq_Expr
; break;
7247 case NE_EXPR
: tcode
= Ne_Expr
; break;
7251 tree t
= maybe_inline_call_in_expr (gnu_size
);
7253 return annotate_value (t
);
7256 /* Fall through... */
7262 /* Now get each of the operands that's relevant for this code. If any
7263 cannot be expressed as a repinfo node, say we can't. */
7264 for (i
= 0; i
< 3; i
++)
7267 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (gnu_size
)); i
++)
7269 ops
[i
] = annotate_value (TREE_OPERAND (gnu_size
, i
));
7270 if (ops
[i
] == No_Uint
)
7274 ret
= Create_Node (tcode
, ops
[0], ops
[1], ops
[2]);
7276 /* Save the result in the cache. */
7279 struct tree_int_map
**h
;
7280 /* We can't assume the hash table data hasn't moved since the
7281 initial look up, so we have to search again. Allocating and
7282 inserting an entry at that point would be an alternative, but
7283 then we'd better discard the entry if we decided not to cache
7285 h
= (struct tree_int_map
**)
7286 htab_find_slot (annotate_value_cache
, &in
, INSERT
);
7288 *h
= ggc_alloc_tree_int_map ();
7289 (*h
)->base
.from
= gnu_size
;
7296 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7297 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7298 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7299 BY_REF is true if the object is used by reference and BY_DOUBLE_REF is
7300 true if the object is used by double reference. */
7303 annotate_object (Entity_Id gnat_entity
, tree gnu_type
, tree size
, bool by_ref
,
7309 gnu_type
= TREE_TYPE (gnu_type
);
7311 if (TYPE_IS_FAT_POINTER_P (gnu_type
))
7312 gnu_type
= TYPE_UNCONSTRAINED_ARRAY (gnu_type
);
7314 gnu_type
= TREE_TYPE (gnu_type
);
7317 if (Unknown_Esize (gnat_entity
))
7319 if (TREE_CODE (gnu_type
) == RECORD_TYPE
7320 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
7321 size
= TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
))));
7323 size
= TYPE_SIZE (gnu_type
);
7326 Set_Esize (gnat_entity
, annotate_value (size
));
7329 if (Unknown_Alignment (gnat_entity
))
7330 Set_Alignment (gnat_entity
,
7331 UI_From_Int (TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
));
7334 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7335 Return NULL_TREE if there is no such element in the list. */
7338 purpose_member_field (const_tree elem
, tree list
)
7342 tree field
= TREE_PURPOSE (list
);
7343 if (SAME_FIELD_P (field
, elem
))
7345 list
= TREE_CHAIN (list
);
7350 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7351 set Component_Bit_Offset and Esize of the components to the position and
7352 size used by Gigi. */
7355 annotate_rep (Entity_Id gnat_entity
, tree gnu_type
)
7357 Entity_Id gnat_field
;
7360 /* We operate by first making a list of all fields and their position (we
7361 can get the size easily) and then update all the sizes in the tree. */
7363 = build_position_list (gnu_type
, false, size_zero_node
, bitsize_zero_node
,
7364 BIGGEST_ALIGNMENT
, NULL_TREE
);
7366 for (gnat_field
= First_Entity (gnat_entity
);
7367 Present (gnat_field
);
7368 gnat_field
= Next_Entity (gnat_field
))
7369 if (Ekind (gnat_field
) == E_Component
7370 || (Ekind (gnat_field
) == E_Discriminant
7371 && !Is_Unchecked_Union (Scope (gnat_field
))))
7373 tree t
= purpose_member_field (gnat_to_gnu_field_decl (gnat_field
),
7379 /* If we are just annotating types and the type is tagged, the tag
7380 and the parent components are not generated by the front-end so
7381 we need to add the appropriate offset to each component without
7382 representation clause. */
7383 if (type_annotate_only
7384 && Is_Tagged_Type (gnat_entity
)
7385 && No (Component_Clause (gnat_field
)))
7387 /* For a component appearing in the current extension, the
7388 offset is the size of the parent. */
7389 if (Is_Derived_Type (gnat_entity
)
7390 && Original_Record_Component (gnat_field
) == gnat_field
)
7392 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity
))),
7395 parent_offset
= bitsize_int (POINTER_SIZE
);
7397 if (TYPE_FIELDS (gnu_type
))
7399 = round_up (parent_offset
,
7400 DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
7403 parent_offset
= bitsize_zero_node
;
7405 Set_Component_Bit_Offset
7408 (size_binop (PLUS_EXPR
,
7409 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t
), 0),
7410 TREE_VEC_ELT (TREE_VALUE (t
), 2)),
7413 Set_Esize (gnat_field
,
7414 annotate_value (DECL_SIZE (TREE_PURPOSE (t
))));
7416 else if (Is_Tagged_Type (gnat_entity
) && Is_Derived_Type (gnat_entity
))
7418 /* If there is no entry, this is an inherited component whose
7419 position is the same as in the parent type. */
7420 Set_Component_Bit_Offset
7422 Component_Bit_Offset (Original_Record_Component (gnat_field
)));
7424 Set_Esize (gnat_field
,
7425 Esize (Original_Record_Component (gnat_field
)));
7430 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7431 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7432 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7433 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7434 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7435 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7436 pre-existing list to be chained to the newly created entries. */
7439 build_position_list (tree gnu_type
, bool do_not_flatten_variant
, tree gnu_pos
,
7440 tree gnu_bitpos
, unsigned int offset_align
, tree gnu_list
)
7444 for (gnu_field
= TYPE_FIELDS (gnu_type
);
7446 gnu_field
= DECL_CHAIN (gnu_field
))
7448 tree gnu_our_bitpos
= size_binop (PLUS_EXPR
, gnu_bitpos
,
7449 DECL_FIELD_BIT_OFFSET (gnu_field
));
7450 tree gnu_our_offset
= size_binop (PLUS_EXPR
, gnu_pos
,
7451 DECL_FIELD_OFFSET (gnu_field
));
7452 unsigned int our_offset_align
7453 = MIN (offset_align
, DECL_OFFSET_ALIGN (gnu_field
));
7454 tree v
= make_tree_vec (3);
7456 TREE_VEC_ELT (v
, 0) = gnu_our_offset
;
7457 TREE_VEC_ELT (v
, 1) = size_int (our_offset_align
);
7458 TREE_VEC_ELT (v
, 2) = gnu_our_bitpos
;
7459 gnu_list
= tree_cons (gnu_field
, v
, gnu_list
);
7461 /* Recurse on internal fields, flattening the nested fields except for
7462 those in the variant part, if requested. */
7463 if (DECL_INTERNAL_P (gnu_field
))
7465 tree gnu_field_type
= TREE_TYPE (gnu_field
);
7466 if (do_not_flatten_variant
7467 && TREE_CODE (gnu_field_type
) == QUAL_UNION_TYPE
)
7469 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
7470 size_zero_node
, bitsize_zero_node
,
7471 BIGGEST_ALIGNMENT
, gnu_list
);
7474 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
7475 gnu_our_offset
, gnu_our_bitpos
,
7476 our_offset_align
, gnu_list
);
7483 /* Return a list describing the substitutions needed to reflect the
7484 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7485 be in any order. The values in an element of the list are in the form
7486 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7487 a definition of GNAT_SUBTYPE. */
7489 static VEC(subst_pair
,heap
) *
7490 build_subst_list (Entity_Id gnat_subtype
, Entity_Id gnat_type
, bool definition
)
7492 VEC(subst_pair
,heap
) *gnu_list
= NULL
;
7493 Entity_Id gnat_discrim
;
7496 for (gnat_discrim
= First_Stored_Discriminant (gnat_type
),
7497 gnat_value
= First_Elmt (Stored_Constraint (gnat_subtype
));
7498 Present (gnat_discrim
);
7499 gnat_discrim
= Next_Stored_Discriminant (gnat_discrim
),
7500 gnat_value
= Next_Elmt (gnat_value
))
7501 /* Ignore access discriminants. */
7502 if (!Is_Access_Type (Etype (Node (gnat_value
))))
7504 tree gnu_field
= gnat_to_gnu_field_decl (gnat_discrim
);
7505 tree replacement
= convert (TREE_TYPE (gnu_field
),
7506 elaborate_expression
7507 (Node (gnat_value
), gnat_subtype
,
7508 get_entity_name (gnat_discrim
),
7509 definition
, true, false));
7510 subst_pair s
= {gnu_field
, replacement
};
7511 VEC_safe_push (subst_pair
, heap
, gnu_list
, s
);
7517 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7518 variants of QUAL_UNION_TYPE that are still relevant after applying
7519 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7520 list to be prepended to the newly created entries. */
7522 static VEC(variant_desc
,heap
) *
7523 build_variant_list (tree qual_union_type
, VEC(subst_pair
,heap
) *subst_list
,
7524 VEC(variant_desc
,heap
) *gnu_list
)
7528 for (gnu_field
= TYPE_FIELDS (qual_union_type
);
7530 gnu_field
= DECL_CHAIN (gnu_field
))
7532 tree qual
= DECL_QUALIFIER (gnu_field
);
7536 FOR_EACH_VEC_ELT (subst_pair
, subst_list
, i
, s
)
7537 qual
= SUBSTITUTE_IN_EXPR (qual
, s
->discriminant
, s
->replacement
);
7539 /* If the new qualifier is not unconditionally false, its variant may
7540 still be accessed. */
7541 if (!integer_zerop (qual
))
7543 tree variant_type
= TREE_TYPE (gnu_field
), variant_subpart
;
7544 variant_desc v
= {variant_type
, gnu_field
, qual
, NULL_TREE
};
7546 VEC_safe_push (variant_desc
, heap
, gnu_list
, v
);
7548 /* Recurse on the variant subpart of the variant, if any. */
7549 variant_subpart
= get_variant_part (variant_type
);
7550 if (variant_subpart
)
7551 gnu_list
= build_variant_list (TREE_TYPE (variant_subpart
),
7552 subst_list
, gnu_list
);
7554 /* If the new qualifier is unconditionally true, the subsequent
7555 variants cannot be accessed. */
7556 if (integer_onep (qual
))
7564 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7565 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7566 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7567 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7568 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7569 true if we are being called to process the Component_Size of GNAT_OBJECT;
7570 this is used only for error messages. ZERO_OK is true if a size of zero
7571 is permitted; if ZERO_OK is false, it means that a size of zero should be
7572 treated as an unspecified size. */
7575 validate_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_object
,
7576 enum tree_code kind
, bool component_p
, bool zero_ok
)
7578 Node_Id gnat_error_node
;
7579 tree type_size
, size
;
7581 /* Return 0 if no size was specified. */
7582 if (uint_size
== No_Uint
)
7585 /* Ignore a negative size since that corresponds to our back-annotation. */
7586 if (UI_Lt (uint_size
, Uint_0
))
7589 /* Find the node to use for error messages. */
7590 if ((Ekind (gnat_object
) == E_Component
7591 || Ekind (gnat_object
) == E_Discriminant
)
7592 && Present (Component_Clause (gnat_object
)))
7593 gnat_error_node
= Last_Bit (Component_Clause (gnat_object
));
7594 else if (Present (Size_Clause (gnat_object
)))
7595 gnat_error_node
= Expression (Size_Clause (gnat_object
));
7597 gnat_error_node
= gnat_object
;
7599 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7600 but cannot be represented in bitsizetype. */
7601 size
= UI_To_gnu (uint_size
, bitsizetype
);
7602 if (TREE_OVERFLOW (size
))
7605 post_error_ne ("component size for& is too large", gnat_error_node
,
7608 post_error_ne ("size for& is too large", gnat_error_node
,
7613 /* Ignore a zero size if it is not permitted. */
7614 if (!zero_ok
&& integer_zerop (size
))
7617 /* The size of objects is always a multiple of a byte. */
7618 if (kind
== VAR_DECL
7619 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
, size
, bitsize_unit_node
)))
7622 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7623 gnat_error_node
, gnat_object
);
7625 post_error_ne ("size for& is not a multiple of Storage_Unit",
7626 gnat_error_node
, gnat_object
);
7630 /* If this is an integral type or a packed array type, the front-end has
7631 already verified the size, so we need not do it here (which would mean
7632 checking against the bounds). However, if this is an aliased object,
7633 it may not be smaller than the type of the object. */
7634 if ((INTEGRAL_TYPE_P (gnu_type
) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type
))
7635 && !(kind
== VAR_DECL
&& Is_Aliased (gnat_object
)))
7638 /* If the object is a record that contains a template, add the size of the
7639 template to the specified size. */
7640 if (TREE_CODE (gnu_type
) == RECORD_TYPE
7641 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
7642 size
= size_binop (PLUS_EXPR
, DECL_SIZE (TYPE_FIELDS (gnu_type
)), size
);
7644 if (kind
== VAR_DECL
7645 /* If a type needs strict alignment, a component of this type in
7646 a packed record cannot be packed and thus uses the type size. */
7647 || (kind
== TYPE_DECL
&& Strict_Alignment (gnat_object
)))
7648 type_size
= TYPE_SIZE (gnu_type
);
7650 type_size
= rm_size (gnu_type
);
7652 /* Modify the size of a discriminated type to be the maximum size. */
7653 if (type_size
&& CONTAINS_PLACEHOLDER_P (type_size
))
7654 type_size
= max_size (type_size
, true);
7656 /* If this is an access type or a fat pointer, the minimum size is that given
7657 by the smallest integral mode that's valid for pointers. */
7658 if (TREE_CODE (gnu_type
) == POINTER_TYPE
|| TYPE_IS_FAT_POINTER_P (gnu_type
))
7660 enum machine_mode p_mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
7661 while (!targetm
.valid_pointer_mode (p_mode
))
7662 p_mode
= GET_MODE_WIDER_MODE (p_mode
);
7663 type_size
= bitsize_int (GET_MODE_BITSIZE (p_mode
));
7666 /* Issue an error either if the default size of the object isn't a constant
7667 or if the new size is smaller than it. */
7668 if (TREE_CODE (type_size
) != INTEGER_CST
7669 || TREE_OVERFLOW (type_size
)
7670 || tree_int_cst_lt (size
, type_size
))
7674 ("component size for& too small{, minimum allowed is ^}",
7675 gnat_error_node
, gnat_object
, type_size
);
7678 ("size for& too small{, minimum allowed is ^}",
7679 gnat_error_node
, gnat_object
, type_size
);
7686 /* Similarly, but both validate and process a value of RM size. This routine
7687 is only called for types. */
7690 set_rm_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_entity
)
7692 Node_Id gnat_attr_node
;
7693 tree old_size
, size
;
7695 /* Do nothing if no size was specified. */
7696 if (uint_size
== No_Uint
)
7699 /* Ignore a negative size since that corresponds to our back-annotation. */
7700 if (UI_Lt (uint_size
, Uint_0
))
7703 /* Only issue an error if a Value_Size clause was explicitly given.
7704 Otherwise, we'd be duplicating an error on the Size clause. */
7706 = Get_Attribute_Definition_Clause (gnat_entity
, Attr_Value_Size
);
7708 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7709 but cannot be represented in bitsizetype. */
7710 size
= UI_To_gnu (uint_size
, bitsizetype
);
7711 if (TREE_OVERFLOW (size
))
7713 if (Present (gnat_attr_node
))
7714 post_error_ne ("Value_Size for& is too large", gnat_attr_node
,
7719 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7720 exists, or this is an integer type, in which case the front-end will
7721 have always set it. */
7722 if (No (gnat_attr_node
)
7723 && integer_zerop (size
)
7724 && !Has_Size_Clause (gnat_entity
)
7725 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
7728 old_size
= rm_size (gnu_type
);
7730 /* If the old size is self-referential, get the maximum size. */
7731 if (CONTAINS_PLACEHOLDER_P (old_size
))
7732 old_size
= max_size (old_size
, true);
7734 /* Issue an error either if the old size of the object isn't a constant or
7735 if the new size is smaller than it. The front-end has already verified
7736 this for scalar and packed array types. */
7737 if (TREE_CODE (old_size
) != INTEGER_CST
7738 || TREE_OVERFLOW (old_size
)
7739 || (AGGREGATE_TYPE_P (gnu_type
)
7740 && !(TREE_CODE (gnu_type
) == ARRAY_TYPE
7741 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type
))
7742 && !(TYPE_IS_PADDING_P (gnu_type
)
7743 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
))) == ARRAY_TYPE
7744 && TYPE_PACKED_ARRAY_TYPE_P
7745 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))
7746 && tree_int_cst_lt (size
, old_size
)))
7748 if (Present (gnat_attr_node
))
7750 ("Value_Size for& too small{, minimum allowed is ^}",
7751 gnat_attr_node
, gnat_entity
, old_size
);
7755 /* Otherwise, set the RM size proper for integral types... */
7756 if ((TREE_CODE (gnu_type
) == INTEGER_TYPE
7757 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
7758 || (TREE_CODE (gnu_type
) == ENUMERAL_TYPE
7759 || TREE_CODE (gnu_type
) == BOOLEAN_TYPE
))
7760 SET_TYPE_RM_SIZE (gnu_type
, size
);
7762 /* ...or the Ada size for record and union types. */
7763 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
7764 && !TYPE_FAT_POINTER_P (gnu_type
))
7765 SET_TYPE_ADA_SIZE (gnu_type
, size
);
7768 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7769 a type or object whose present alignment is ALIGN. If this alignment is
7770 valid, return it. Otherwise, give an error and return ALIGN. */
7773 validate_alignment (Uint alignment
, Entity_Id gnat_entity
, unsigned int align
)
7775 unsigned int max_allowed_alignment
= get_target_maximum_allowed_alignment ();
7776 unsigned int new_align
;
7777 Node_Id gnat_error_node
;
7779 /* Don't worry about checking alignment if alignment was not specified
7780 by the source program and we already posted an error for this entity. */
7781 if (Error_Posted (gnat_entity
) && !Has_Alignment_Clause (gnat_entity
))
7784 /* Post the error on the alignment clause if any. Note, for the implicit
7785 base type of an array type, the alignment clause is on the first
7787 if (Present (Alignment_Clause (gnat_entity
)))
7788 gnat_error_node
= Expression (Alignment_Clause (gnat_entity
));
7790 else if (Is_Itype (gnat_entity
)
7791 && Is_Array_Type (gnat_entity
)
7792 && Etype (gnat_entity
) == gnat_entity
7793 && Present (Alignment_Clause (First_Subtype (gnat_entity
))))
7795 Expression (Alignment_Clause (First_Subtype (gnat_entity
)));
7798 gnat_error_node
= gnat_entity
;
7800 /* Within GCC, an alignment is an integer, so we must make sure a value is
7801 specified that fits in that range. Also, there is an upper bound to
7802 alignments we can support/allow. */
7803 if (!UI_Is_In_Int_Range (alignment
)
7804 || ((new_align
= UI_To_Int (alignment
)) > max_allowed_alignment
))
7805 post_error_ne_num ("largest supported alignment for& is ^",
7806 gnat_error_node
, gnat_entity
, max_allowed_alignment
);
7807 else if (!(Present (Alignment_Clause (gnat_entity
))
7808 && From_At_Mod (Alignment_Clause (gnat_entity
)))
7809 && new_align
* BITS_PER_UNIT
< align
)
7811 unsigned int double_align
;
7812 bool is_capped_double
, align_clause
;
7814 /* If the default alignment of "double" or larger scalar types is
7815 specifically capped and the new alignment is above the cap, do
7816 not post an error and change the alignment only if there is an
7817 alignment clause; this makes it possible to have the associated
7818 GCC type overaligned by default for performance reasons. */
7819 if ((double_align
= double_float_alignment
) > 0)
7822 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
7824 = is_double_float_or_array (gnat_type
, &align_clause
);
7826 else if ((double_align
= double_scalar_alignment
) > 0)
7829 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
7831 = is_double_scalar_or_array (gnat_type
, &align_clause
);
7834 is_capped_double
= align_clause
= false;
7836 if (is_capped_double
&& new_align
>= double_align
)
7839 align
= new_align
* BITS_PER_UNIT
;
7843 if (is_capped_double
)
7844 align
= double_align
* BITS_PER_UNIT
;
7846 post_error_ne_num ("alignment for& must be at least ^",
7847 gnat_error_node
, gnat_entity
,
7848 align
/ BITS_PER_UNIT
);
7853 new_align
= (new_align
> 0 ? new_align
* BITS_PER_UNIT
: 1);
7854 if (new_align
> align
)
7861 /* Verify that OBJECT, a type or decl, is something we can implement
7862 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
7863 if we require atomic components. */
7866 check_ok_for_atomic (tree object
, Entity_Id gnat_entity
, bool comp_p
)
7868 Node_Id gnat_error_point
= gnat_entity
;
7870 enum machine_mode mode
;
7874 /* There are three case of what OBJECT can be. It can be a type, in which
7875 case we take the size, alignment and mode from the type. It can be a
7876 declaration that was indirect, in which case the relevant values are
7877 that of the type being pointed to, or it can be a normal declaration,
7878 in which case the values are of the decl. The code below assumes that
7879 OBJECT is either a type or a decl. */
7880 if (TYPE_P (object
))
7882 /* If this is an anonymous base type, nothing to check. Error will be
7883 reported on the source type. */
7884 if (!Comes_From_Source (gnat_entity
))
7887 mode
= TYPE_MODE (object
);
7888 align
= TYPE_ALIGN (object
);
7889 size
= TYPE_SIZE (object
);
7891 else if (DECL_BY_REF_P (object
))
7893 mode
= TYPE_MODE (TREE_TYPE (TREE_TYPE (object
)));
7894 align
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object
)));
7895 size
= TYPE_SIZE (TREE_TYPE (TREE_TYPE (object
)));
7899 mode
= DECL_MODE (object
);
7900 align
= DECL_ALIGN (object
);
7901 size
= DECL_SIZE (object
);
7904 /* Consider all floating-point types atomic and any types that that are
7905 represented by integers no wider than a machine word. */
7906 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
7907 || ((GET_MODE_CLASS (mode
) == MODE_INT
7908 || GET_MODE_CLASS (mode
) == MODE_PARTIAL_INT
)
7909 && GET_MODE_BITSIZE (mode
) <= BITS_PER_WORD
))
7912 /* For the moment, also allow anything that has an alignment equal
7913 to its size and which is smaller than a word. */
7914 if (size
&& TREE_CODE (size
) == INTEGER_CST
7915 && compare_tree_int (size
, align
) == 0
7916 && align
<= BITS_PER_WORD
)
7919 for (gnat_node
= First_Rep_Item (gnat_entity
); Present (gnat_node
);
7920 gnat_node
= Next_Rep_Item (gnat_node
))
7922 if (!comp_p
&& Nkind (gnat_node
) == N_Pragma
7923 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)))
7925 gnat_error_point
= First (Pragma_Argument_Associations (gnat_node
));
7926 else if (comp_p
&& Nkind (gnat_node
) == N_Pragma
7927 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)))
7928 == Pragma_Atomic_Components
))
7929 gnat_error_point
= First (Pragma_Argument_Associations (gnat_node
));
7933 post_error_ne ("atomic access to component of & cannot be guaranteed",
7934 gnat_error_point
, gnat_entity
);
7936 post_error_ne ("atomic access to & cannot be guaranteed",
7937 gnat_error_point
, gnat_entity
);
7941 /* Helper for the intrin compatibility checks family. Evaluate whether
7942 two types are definitely incompatible. */
7945 intrin_types_incompatible_p (tree t1
, tree t2
)
7947 enum tree_code code
;
7949 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
7952 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
7955 if (TREE_CODE (t1
) != TREE_CODE (t2
))
7958 code
= TREE_CODE (t1
);
7964 return TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
);
7967 case REFERENCE_TYPE
:
7968 /* Assume designated types are ok. We'd need to account for char * and
7969 void * variants to do better, which could rapidly get messy and isn't
7970 clearly worth the effort. */
7980 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
7981 on the Ada/builtin argument lists for the INB binding. */
7984 intrin_arglists_compatible_p (intrin_binding_t
* inb
)
7986 function_args_iterator ada_iter
, btin_iter
;
7988 function_args_iter_init (&ada_iter
, inb
->ada_fntype
);
7989 function_args_iter_init (&btin_iter
, inb
->btin_fntype
);
7991 /* Sequence position of the last argument we checked. */
7996 tree ada_type
= function_args_iter_cond (&ada_iter
);
7997 tree btin_type
= function_args_iter_cond (&btin_iter
);
7999 /* If we've exhausted both lists simultaneously, we're done. */
8000 if (ada_type
== NULL_TREE
&& btin_type
== NULL_TREE
)
8003 /* If one list is shorter than the other, they fail to match. */
8004 if (ada_type
== NULL_TREE
|| btin_type
== NULL_TREE
)
8007 /* If we're done with the Ada args and not with the internal builtin
8008 args, or the other way around, complain. */
8009 if (ada_type
== void_type_node
8010 && btin_type
!= void_type_node
)
8012 post_error ("?Ada arguments list too short!", inb
->gnat_entity
);
8016 if (btin_type
== void_type_node
8017 && ada_type
!= void_type_node
)
8019 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8020 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8024 /* Otherwise, check that types match for the current argument. */
8026 if (intrin_types_incompatible_p (ada_type
, btin_type
))
8028 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8029 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8034 function_args_iter_next (&ada_iter
);
8035 function_args_iter_next (&btin_iter
);
8041 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8042 on the Ada/builtin return values for the INB binding. */
8045 intrin_return_compatible_p (intrin_binding_t
* inb
)
8047 tree ada_return_type
= TREE_TYPE (inb
->ada_fntype
);
8048 tree btin_return_type
= TREE_TYPE (inb
->btin_fntype
);
8050 /* Accept function imported as procedure, common and convenient. */
8051 if (VOID_TYPE_P (ada_return_type
)
8052 && !VOID_TYPE_P (btin_return_type
))
8055 /* If return type is Address (integer type), map it to void *. */
8056 if (Is_Descendent_Of_Address (Etype (inb
->gnat_entity
)))
8057 ada_return_type
= ptr_void_type_node
;
8059 /* Check return types compatibility otherwise. Note that this
8060 handles void/void as well. */
8061 if (intrin_types_incompatible_p (btin_return_type
, ada_return_type
))
8063 post_error ("?intrinsic binding type mismatch on return value!",
8071 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8072 compatible. Issue relevant warnings when they are not.
8074 This is intended as a light check to diagnose the most obvious cases, not
8075 as a full fledged type compatibility predicate. It is the programmer's
8076 responsibility to ensure correctness of the Ada declarations in Imports,
8077 especially when binding straight to a compiler internal. */
8080 intrin_profiles_compatible_p (intrin_binding_t
* inb
)
8082 /* Check compatibility on return values and argument lists, each responsible
8083 for posting warnings as appropriate. Ensure use of the proper sloc for
8086 bool arglists_compatible_p
, return_compatible_p
;
8087 location_t saved_location
= input_location
;
8089 Sloc_to_locus (Sloc (inb
->gnat_entity
), &input_location
);
8091 return_compatible_p
= intrin_return_compatible_p (inb
);
8092 arglists_compatible_p
= intrin_arglists_compatible_p (inb
);
8094 input_location
= saved_location
;
8096 return return_compatible_p
&& arglists_compatible_p
;
8099 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8100 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8101 specified size for this field. POS_LIST is a position list describing
8102 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8106 create_field_decl_from (tree old_field
, tree field_type
, tree record_type
,
8107 tree size
, tree pos_list
,
8108 VEC(subst_pair
,heap
) *subst_list
)
8110 tree t
= TREE_VALUE (purpose_member (old_field
, pos_list
));
8111 tree pos
= TREE_VEC_ELT (t
, 0), bitpos
= TREE_VEC_ELT (t
, 2);
8112 unsigned int offset_align
= tree_low_cst (TREE_VEC_ELT (t
, 1), 1);
8113 tree new_pos
, new_field
;
8117 if (CONTAINS_PLACEHOLDER_P (pos
))
8118 FOR_EACH_VEC_ELT (subst_pair
, subst_list
, i
, s
)
8119 pos
= SUBSTITUTE_IN_EXPR (pos
, s
->discriminant
, s
->replacement
);
8121 /* If the position is now a constant, we can set it as the position of the
8122 field when we make it. Otherwise, we need to deal with it specially. */
8123 if (TREE_CONSTANT (pos
))
8124 new_pos
= bit_from_pos (pos
, bitpos
);
8126 new_pos
= NULL_TREE
;
8129 = create_field_decl (DECL_NAME (old_field
), field_type
, record_type
,
8130 size
, new_pos
, DECL_PACKED (old_field
),
8131 !DECL_NONADDRESSABLE_P (old_field
));
8135 normalize_offset (&pos
, &bitpos
, offset_align
);
8136 DECL_FIELD_OFFSET (new_field
) = pos
;
8137 DECL_FIELD_BIT_OFFSET (new_field
) = bitpos
;
8138 SET_DECL_OFFSET_ALIGN (new_field
, offset_align
);
8139 DECL_SIZE (new_field
) = size
;
8140 DECL_SIZE_UNIT (new_field
)
8141 = convert (sizetype
,
8142 size_binop (CEIL_DIV_EXPR
, size
, bitsize_unit_node
));
8143 layout_decl (new_field
, DECL_OFFSET_ALIGN (new_field
));
8146 DECL_INTERNAL_P (new_field
) = DECL_INTERNAL_P (old_field
);
8147 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, old_field
);
8148 DECL_DISCRIMINANT_NUMBER (new_field
) = DECL_DISCRIMINANT_NUMBER (old_field
);
8149 TREE_THIS_VOLATILE (new_field
) = TREE_THIS_VOLATILE (old_field
);
8154 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8155 it is the minimal size the REP_PART must have. */
8158 create_rep_part (tree rep_type
, tree record_type
, tree min_size
)
8162 if (min_size
&& !tree_int_cst_lt (TYPE_SIZE (rep_type
), min_size
))
8163 min_size
= NULL_TREE
;
8165 field
= create_field_decl (get_identifier ("REP"), rep_type
, record_type
,
8166 min_size
, bitsize_zero_node
, 0, 1);
8167 DECL_INTERNAL_P (field
) = 1;
8172 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8175 get_rep_part (tree record_type
)
8177 tree field
= TYPE_FIELDS (record_type
);
8179 /* The REP part is the first field, internal, another record, and its name
8180 starts with an 'R'. */
8182 && DECL_INTERNAL_P (field
)
8183 && TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
8184 && IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == 'R')
8190 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8193 get_variant_part (tree record_type
)
8197 /* The variant part is the only internal field that is a qualified union. */
8198 for (field
= TYPE_FIELDS (record_type
); field
; field
= DECL_CHAIN (field
))
8199 if (DECL_INTERNAL_P (field
)
8200 && TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
)
8206 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8207 the list of variants to be used and RECORD_TYPE is the type of the parent.
8208 POS_LIST is a position list describing the layout of fields present in
8209 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8213 create_variant_part_from (tree old_variant_part
,
8214 VEC(variant_desc
,heap
) *variant_list
,
8215 tree record_type
, tree pos_list
,
8216 VEC(subst_pair
,heap
) *subst_list
)
8218 tree offset
= DECL_FIELD_OFFSET (old_variant_part
);
8219 tree old_union_type
= TREE_TYPE (old_variant_part
);
8220 tree new_union_type
, new_variant_part
;
8221 tree union_field_list
= NULL_TREE
;
8225 /* First create the type of the variant part from that of the old one. */
8226 new_union_type
= make_node (QUAL_UNION_TYPE
);
8227 TYPE_NAME (new_union_type
)
8228 = concat_name (TYPE_NAME (record_type
),
8229 IDENTIFIER_POINTER (DECL_NAME (old_variant_part
)));
8231 /* If the position of the variant part is constant, subtract it from the
8232 size of the type of the parent to get the new size. This manual CSE
8233 reduces the code size when not optimizing. */
8234 if (TREE_CODE (offset
) == INTEGER_CST
)
8236 tree bitpos
= DECL_FIELD_BIT_OFFSET (old_variant_part
);
8237 tree first_bit
= bit_from_pos (offset
, bitpos
);
8238 TYPE_SIZE (new_union_type
)
8239 = size_binop (MINUS_EXPR
, TYPE_SIZE (record_type
), first_bit
);
8240 TYPE_SIZE_UNIT (new_union_type
)
8241 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (record_type
),
8242 byte_from_pos (offset
, bitpos
));
8243 SET_TYPE_ADA_SIZE (new_union_type
,
8244 size_binop (MINUS_EXPR
, TYPE_ADA_SIZE (record_type
),
8246 TYPE_ALIGN (new_union_type
) = TYPE_ALIGN (old_union_type
);
8247 relate_alias_sets (new_union_type
, old_union_type
, ALIAS_SET_COPY
);
8250 copy_and_substitute_in_size (new_union_type
, old_union_type
, subst_list
);
8252 /* Now finish up the new variants and populate the union type. */
8253 FOR_EACH_VEC_ELT_REVERSE (variant_desc
, variant_list
, i
, v
)
8255 tree old_field
= v
->field
, new_field
;
8256 tree old_variant
, old_variant_subpart
, new_variant
, field_list
;
8258 /* Skip variants that don't belong to this nesting level. */
8259 if (DECL_CONTEXT (old_field
) != old_union_type
)
8262 /* Retrieve the list of fields already added to the new variant. */
8263 new_variant
= v
->new_type
;
8264 field_list
= TYPE_FIELDS (new_variant
);
8266 /* If the old variant had a variant subpart, we need to create a new
8267 variant subpart and add it to the field list. */
8268 old_variant
= v
->type
;
8269 old_variant_subpart
= get_variant_part (old_variant
);
8270 if (old_variant_subpart
)
8272 tree new_variant_subpart
8273 = create_variant_part_from (old_variant_subpart
, variant_list
,
8274 new_variant
, pos_list
, subst_list
);
8275 DECL_CHAIN (new_variant_subpart
) = field_list
;
8276 field_list
= new_variant_subpart
;
8279 /* Finish up the new variant and create the field. No need for debug
8280 info thanks to the XVS type. */
8281 finish_record_type (new_variant
, nreverse (field_list
), 2, false);
8282 compute_record_mode (new_variant
);
8283 create_type_decl (TYPE_NAME (new_variant
), new_variant
, NULL
,
8284 true, false, Empty
);
8287 = create_field_decl_from (old_field
, new_variant
, new_union_type
,
8288 TYPE_SIZE (new_variant
),
8289 pos_list
, subst_list
);
8290 DECL_QUALIFIER (new_field
) = v
->qual
;
8291 DECL_INTERNAL_P (new_field
) = 1;
8292 DECL_CHAIN (new_field
) = union_field_list
;
8293 union_field_list
= new_field
;
8296 /* Finish up the union type and create the variant part. No need for debug
8297 info thanks to the XVS type. Note that we don't reverse the field list
8298 because VARIANT_LIST has been traversed in reverse order. */
8299 finish_record_type (new_union_type
, union_field_list
, 2, false);
8300 compute_record_mode (new_union_type
);
8301 create_type_decl (TYPE_NAME (new_union_type
), new_union_type
, NULL
,
8302 true, false, Empty
);
8305 = create_field_decl_from (old_variant_part
, new_union_type
, record_type
,
8306 TYPE_SIZE (new_union_type
),
8307 pos_list
, subst_list
);
8308 DECL_INTERNAL_P (new_variant_part
) = 1;
8310 /* With multiple discriminants it is possible for an inner variant to be
8311 statically selected while outer ones are not; in this case, the list
8312 of fields of the inner variant is not flattened and we end up with a
8313 qualified union with a single member. Drop the useless container. */
8314 if (!DECL_CHAIN (union_field_list
))
8316 DECL_CONTEXT (union_field_list
) = record_type
;
8317 DECL_FIELD_OFFSET (union_field_list
)
8318 = DECL_FIELD_OFFSET (new_variant_part
);
8319 DECL_FIELD_BIT_OFFSET (union_field_list
)
8320 = DECL_FIELD_BIT_OFFSET (new_variant_part
);
8321 SET_DECL_OFFSET_ALIGN (union_field_list
,
8322 DECL_OFFSET_ALIGN (new_variant_part
));
8323 new_variant_part
= union_field_list
;
8326 return new_variant_part
;
8329 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8330 which are both RECORD_TYPE, after applying the substitutions described
8334 copy_and_substitute_in_size (tree new_type
, tree old_type
,
8335 VEC(subst_pair
,heap
) *subst_list
)
8340 TYPE_SIZE (new_type
) = TYPE_SIZE (old_type
);
8341 TYPE_SIZE_UNIT (new_type
) = TYPE_SIZE_UNIT (old_type
);
8342 SET_TYPE_ADA_SIZE (new_type
, TYPE_ADA_SIZE (old_type
));
8343 TYPE_ALIGN (new_type
) = TYPE_ALIGN (old_type
);
8344 relate_alias_sets (new_type
, old_type
, ALIAS_SET_COPY
);
8346 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type
)))
8347 FOR_EACH_VEC_ELT (subst_pair
, subst_list
, i
, s
)
8348 TYPE_SIZE (new_type
)
8349 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type
),
8350 s
->discriminant
, s
->replacement
);
8352 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type
)))
8353 FOR_EACH_VEC_ELT (subst_pair
, subst_list
, i
, s
)
8354 TYPE_SIZE_UNIT (new_type
)
8355 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type
),
8356 s
->discriminant
, s
->replacement
);
8358 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type
)))
8359 FOR_EACH_VEC_ELT (subst_pair
, subst_list
, i
, s
)
8361 (new_type
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type
),
8362 s
->discriminant
, s
->replacement
));
8364 /* Finalize the size. */
8365 TYPE_SIZE (new_type
) = variable_size (TYPE_SIZE (new_type
));
8366 TYPE_SIZE_UNIT (new_type
) = variable_size (TYPE_SIZE_UNIT (new_type
));
8369 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8370 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8371 updated by replacing F with R.
8373 The function doesn't update the layout of the type, i.e. it assumes
8374 that the substitution is purely formal. That's why the replacement
8375 value R must itself contain a PLACEHOLDER_EXPR. */
8378 substitute_in_type (tree t
, tree f
, tree r
)
8382 gcc_assert (CONTAINS_PLACEHOLDER_P (r
));
8384 switch (TREE_CODE (t
))
8391 /* First the domain types of arrays. */
8392 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t
))
8393 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t
)))
8395 tree low
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t
), f
, r
);
8396 tree high
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t
), f
, r
);
8398 if (low
== TYPE_GCC_MIN_VALUE (t
) && high
== TYPE_GCC_MAX_VALUE (t
))
8402 TYPE_GCC_MIN_VALUE (nt
) = low
;
8403 TYPE_GCC_MAX_VALUE (nt
) = high
;
8405 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_INDEX_TYPE (t
))
8407 (nt
, substitute_in_type (TYPE_INDEX_TYPE (t
), f
, r
));
8412 /* Then the subtypes. */
8413 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t
))
8414 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t
)))
8416 tree low
= SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t
), f
, r
);
8417 tree high
= SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t
), f
, r
);
8419 if (low
== TYPE_RM_MIN_VALUE (t
) && high
== TYPE_RM_MAX_VALUE (t
))
8423 SET_TYPE_RM_MIN_VALUE (nt
, low
);
8424 SET_TYPE_RM_MAX_VALUE (nt
, high
);
8432 nt
= substitute_in_type (TREE_TYPE (t
), f
, r
);
8433 if (nt
== TREE_TYPE (t
))
8436 return build_complex_type (nt
);
8439 /* These should never show up here. */
8444 tree component
= substitute_in_type (TREE_TYPE (t
), f
, r
);
8445 tree domain
= substitute_in_type (TYPE_DOMAIN (t
), f
, r
);
8447 if (component
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
8450 nt
= build_nonshared_array_type (component
, domain
);
8451 TYPE_ALIGN (nt
) = TYPE_ALIGN (t
);
8452 TYPE_USER_ALIGN (nt
) = TYPE_USER_ALIGN (t
);
8453 SET_TYPE_MODE (nt
, TYPE_MODE (t
));
8454 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
8455 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
8456 TYPE_NONALIASED_COMPONENT (nt
) = TYPE_NONALIASED_COMPONENT (t
);
8457 TYPE_MULTI_ARRAY_P (nt
) = TYPE_MULTI_ARRAY_P (t
);
8458 TYPE_CONVENTION_FORTRAN_P (nt
) = TYPE_CONVENTION_FORTRAN_P (t
);
8464 case QUAL_UNION_TYPE
:
8466 bool changed_field
= false;
8469 /* Start out with no fields, make new fields, and chain them
8470 in. If we haven't actually changed the type of any field,
8471 discard everything we've done and return the old type. */
8473 TYPE_FIELDS (nt
) = NULL_TREE
;
8475 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
8477 tree new_field
= copy_node (field
), new_n
;
8479 new_n
= substitute_in_type (TREE_TYPE (field
), f
, r
);
8480 if (new_n
!= TREE_TYPE (field
))
8482 TREE_TYPE (new_field
) = new_n
;
8483 changed_field
= true;
8486 new_n
= SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field
), f
, r
);
8487 if (new_n
!= DECL_FIELD_OFFSET (field
))
8489 DECL_FIELD_OFFSET (new_field
) = new_n
;
8490 changed_field
= true;
8493 /* Do the substitution inside the qualifier, if any. */
8494 if (TREE_CODE (t
) == QUAL_UNION_TYPE
)
8496 new_n
= SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field
), f
, r
);
8497 if (new_n
!= DECL_QUALIFIER (field
))
8499 DECL_QUALIFIER (new_field
) = new_n
;
8500 changed_field
= true;
8504 DECL_CONTEXT (new_field
) = nt
;
8505 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, field
);
8507 DECL_CHAIN (new_field
) = TYPE_FIELDS (nt
);
8508 TYPE_FIELDS (nt
) = new_field
;
8514 TYPE_FIELDS (nt
) = nreverse (TYPE_FIELDS (nt
));
8515 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
8516 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
8517 SET_TYPE_ADA_SIZE (nt
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t
), f
, r
));
8526 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8527 needed to represent the object. */
8530 rm_size (tree gnu_type
)
8532 /* For integral types, we store the RM size explicitly. */
8533 if (INTEGRAL_TYPE_P (gnu_type
) && TYPE_RM_SIZE (gnu_type
))
8534 return TYPE_RM_SIZE (gnu_type
);
8536 /* Return the RM size of the actual data plus the size of the template. */
8537 if (TREE_CODE (gnu_type
) == RECORD_TYPE
8538 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
8540 size_binop (PLUS_EXPR
,
8541 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
)))),
8542 DECL_SIZE (TYPE_FIELDS (gnu_type
)));
8544 /* For record or union types, we store the size explicitly. */
8545 if (RECORD_OR_UNION_TYPE_P (gnu_type
)
8546 && !TYPE_FAT_POINTER_P (gnu_type
)
8547 && TYPE_ADA_SIZE (gnu_type
))
8548 return TYPE_ADA_SIZE (gnu_type
);
8550 /* For other types, this is just the size. */
8551 return TYPE_SIZE (gnu_type
);
8554 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8555 fully-qualified name, possibly with type information encoding.
8556 Otherwise, return the name. */
8559 get_entity_name (Entity_Id gnat_entity
)
8561 Get_Encoded_Name (gnat_entity
);
8562 return get_identifier_with_length (Name_Buffer
, Name_Len
);
8565 /* Return an identifier representing the external name to be used for
8566 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8567 and the specified suffix. */
8570 create_concat_name (Entity_Id gnat_entity
, const char *suffix
)
8572 Entity_Kind kind
= Ekind (gnat_entity
);
8576 String_Template temp
= {1, (int) strlen (suffix
)};
8577 Fat_Pointer fp
= {suffix
, &temp
};
8578 Get_External_Name_With_Suffix (gnat_entity
, fp
);
8581 Get_External_Name (gnat_entity
, 0);
8583 /* A variable using the Stdcall convention lives in a DLL. We adjust
8584 its name to use the jump table, the _imp__NAME contains the address
8585 for the NAME variable. */
8586 if ((kind
== E_Variable
|| kind
== E_Constant
)
8587 && Has_Stdcall_Convention (gnat_entity
))
8589 const int len
= 6 + Name_Len
;
8590 char *new_name
= (char *) alloca (len
+ 1);
8591 strcpy (new_name
, "_imp__");
8592 strcat (new_name
, Name_Buffer
);
8593 return get_identifier_with_length (new_name
, len
);
8596 return get_identifier_with_length (Name_Buffer
, Name_Len
);
8599 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8600 string, return a new IDENTIFIER_NODE that is the concatenation of
8601 the name followed by "___" and the specified suffix. */
8604 concat_name (tree gnu_name
, const char *suffix
)
8606 const int len
= IDENTIFIER_LENGTH (gnu_name
) + 3 + strlen (suffix
);
8607 char *new_name
= (char *) alloca (len
+ 1);
8608 strcpy (new_name
, IDENTIFIER_POINTER (gnu_name
));
8609 strcat (new_name
, "___");
8610 strcat (new_name
, suffix
);
8611 return get_identifier_with_length (new_name
, len
);
8614 #include "gt-ada-decl.h"