1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2013, 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"
36 #include "diagnostic-core.h"
54 /* "stdcall" and "thiscall" conventions should be processed in a specific way
55 on 32-bit x86/Windows only. The macros below are helpers to avoid having
56 to check for a Windows specific attribute throughout this unit. */
58 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
60 #define Has_Stdcall_Convention(E) \
61 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
62 #define Has_Thiscall_Convention(E) \
63 (!TARGET_64BIT && is_cplusplus_method (E))
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
69 #define Has_Stdcall_Convention(E) 0
70 #define Has_Thiscall_Convention(E) 0
73 /* Stack realignment is necessary for functions with foreign conventions when
74 the ABI doesn't mandate as much as what the compiler assumes - that is, up
75 to PREFERRED_STACK_BOUNDARY.
77 Such realignment can be requested with a dedicated function type attribute
78 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
79 characterize the situations where the attribute should be set. We rely on
80 compiler configuration settings for 'main' to decide. */
82 #ifdef MAIN_STACK_BOUNDARY
83 #define FOREIGN_FORCE_REALIGN_STACK \
84 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
86 #define FOREIGN_FORCE_REALIGN_STACK 0
91 struct incomplete
*next
;
96 /* These variables are used to defer recursively expanding incomplete types
97 while we are processing an array, a record or a subprogram type. */
98 static int defer_incomplete_level
= 0;
99 static struct incomplete
*defer_incomplete_list
;
101 /* This variable is used to delay expanding From_With_Type types until the
103 static struct incomplete
*defer_limited_with
;
105 typedef struct subst_pair_d
{
111 typedef struct variant_desc_d
{
112 /* The type of the variant. */
115 /* The associated field. */
118 /* The value of the qualifier. */
121 /* The type of the variant after transformation. */
126 /* A hash table used to cache the result of annotate_value. */
127 static GTY ((if_marked ("tree_int_map_marked_p"),
128 param_is (struct tree_int_map
))) htab_t annotate_value_cache
;
130 static bool allocatable_size_p (tree
, bool);
131 static void prepend_one_attribute_to (struct attrib
**,
132 enum attr_type
, tree
, tree
, Node_Id
);
133 static void prepend_attributes (Entity_Id
, struct attrib
**);
134 static tree
elaborate_expression (Node_Id
, Entity_Id
, tree
, bool, bool, bool);
135 static bool type_has_variable_size (tree
);
136 static tree
elaborate_expression_1 (tree
, Entity_Id
, tree
, bool, bool);
137 static tree
elaborate_expression_2 (tree
, Entity_Id
, tree
, bool, bool,
139 static tree
gnat_to_gnu_component_type (Entity_Id
, bool, bool);
140 static tree
gnat_to_gnu_param (Entity_Id
, Mechanism_Type
, Entity_Id
, bool,
142 static tree
gnat_to_gnu_field (Entity_Id
, tree
, int, bool, bool);
143 static bool same_discriminant_p (Entity_Id
, Entity_Id
);
144 static bool array_type_has_nonaliased_component (tree
, Entity_Id
);
145 static bool compile_time_known_address_p (Node_Id
);
146 static bool cannot_be_superflat_p (Node_Id
);
147 static bool constructor_address_p (tree
);
148 static bool components_to_record (tree
, Node_Id
, tree
, int, bool, bool, bool,
149 bool, bool, bool, bool, bool, tree
, tree
*);
150 static Uint
annotate_value (tree
);
151 static void annotate_rep (Entity_Id
, tree
);
152 static tree
build_position_list (tree
, bool, tree
, tree
, unsigned int, tree
);
153 static vec
<subst_pair
> build_subst_list (Entity_Id
, Entity_Id
, bool);
154 static vec
<variant_desc
> build_variant_list (tree
,
157 static tree
validate_size (Uint
, tree
, Entity_Id
, enum tree_code
, bool, bool);
158 static void set_rm_size (Uint
, tree
, Entity_Id
);
159 static unsigned int validate_alignment (Uint
, Entity_Id
, unsigned int);
160 static void check_ok_for_atomic (tree
, Entity_Id
, bool);
161 static tree
create_field_decl_from (tree
, tree
, tree
, tree
, tree
,
163 static tree
create_rep_part (tree
, tree
, tree
);
164 static tree
get_rep_part (tree
);
165 static tree
create_variant_part_from (tree
, vec
<variant_desc
> , tree
,
166 tree
, vec
<subst_pair
> );
167 static void copy_and_substitute_in_size (tree
, tree
, vec
<subst_pair
> );
169 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
170 to pass around calls performing profile compatibility checks. */
173 Entity_Id gnat_entity
; /* The Ada subprogram entity. */
174 tree ada_fntype
; /* The corresponding GCC type node. */
175 tree btin_fntype
; /* The GCC builtin function type node. */
178 static bool intrin_profiles_compatible_p (intrin_binding_t
*);
180 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
181 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
182 and associate the ..._DECL node with the input GNAT defining identifier.
184 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
185 initial value (in GCC tree form). This is optional for a variable. For
186 a renamed entity, GNU_EXPR gives the object being renamed.
188 DEFINITION is nonzero if this call is intended for a definition. This is
189 used for separate compilation where it is necessary to know whether an
190 external declaration or a definition must be created if the GCC equivalent
191 was not created previously. The value of 1 is normally used for a nonzero
192 DEFINITION, but a value of 2 is used in special circumstances, defined in
196 gnat_to_gnu_entity (Entity_Id gnat_entity
, tree gnu_expr
, int definition
)
198 /* Contains the kind of the input GNAT node. */
199 const Entity_Kind kind
= Ekind (gnat_entity
);
200 /* True if this is a type. */
201 const bool is_type
= IN (kind
, Type_Kind
);
202 /* True if debug info is requested for this entity. */
203 const bool debug_info_p
= Needs_Debug_Info (gnat_entity
);
204 /* True if this entity is to be considered as imported. */
205 const bool imported_p
206 = (Is_Imported (gnat_entity
) && No (Address_Clause (gnat_entity
)));
207 /* For a type, contains the equivalent GNAT node to be used in gigi. */
208 Entity_Id gnat_equiv_type
= Empty
;
209 /* Temporary used to walk the GNAT tree. */
211 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
212 This node will be associated with the GNAT node by calling at the end
213 of the `switch' statement. */
214 tree gnu_decl
= NULL_TREE
;
215 /* Contains the GCC type to be used for the GCC node. */
216 tree gnu_type
= NULL_TREE
;
217 /* Contains the GCC size tree to be used for the GCC node. */
218 tree gnu_size
= NULL_TREE
;
219 /* Contains the GCC name to be used for the GCC node. */
220 tree gnu_entity_name
;
221 /* True if we have already saved gnu_decl as a GNAT association. */
223 /* True if we incremented defer_incomplete_level. */
224 bool this_deferred
= false;
225 /* True if we incremented force_global. */
226 bool this_global
= false;
227 /* True if we should check to see if elaborated during processing. */
228 bool maybe_present
= false;
229 /* True if we made GNU_DECL and its type here. */
230 bool this_made_decl
= false;
231 /* Size and alignment of the GCC node, if meaningful. */
232 unsigned int esize
= 0, align
= 0;
233 /* Contains the list of attributes directly attached to the entity. */
234 struct attrib
*attr_list
= NULL
;
236 /* Since a use of an Itype is a definition, process it as such if it
237 is not in a with'ed unit. */
240 && Is_Itype (gnat_entity
)
241 && !present_gnu_tree (gnat_entity
)
242 && In_Extended_Main_Code_Unit (gnat_entity
))
244 /* Ensure that we are in a subprogram mentioned in the Scope chain of
245 this entity, our current scope is global, or we encountered a task
246 or entry (where we can't currently accurately check scoping). */
247 if (!current_function_decl
248 || DECL_ELABORATION_PROC_P (current_function_decl
))
250 process_type (gnat_entity
);
251 return get_gnu_tree (gnat_entity
);
254 for (gnat_temp
= Scope (gnat_entity
);
256 gnat_temp
= Scope (gnat_temp
))
258 if (Is_Type (gnat_temp
))
259 gnat_temp
= Underlying_Type (gnat_temp
);
261 if (Ekind (gnat_temp
) == E_Subprogram_Body
)
263 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp
)));
265 if (IN (Ekind (gnat_temp
), Subprogram_Kind
)
266 && Present (Protected_Body_Subprogram (gnat_temp
)))
267 gnat_temp
= Protected_Body_Subprogram (gnat_temp
);
269 if (Ekind (gnat_temp
) == E_Entry
270 || Ekind (gnat_temp
) == E_Entry_Family
271 || Ekind (gnat_temp
) == E_Task_Type
272 || (IN (Ekind (gnat_temp
), Subprogram_Kind
)
273 && present_gnu_tree (gnat_temp
)
274 && (current_function_decl
275 == gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, 0))))
277 process_type (gnat_entity
);
278 return get_gnu_tree (gnat_entity
);
282 /* This abort means the Itype has an incorrect scope, i.e. that its
283 scope does not correspond to the subprogram it is declared in. */
287 /* If we've already processed this entity, return what we got last time.
288 If we are defining the node, we should not have already processed it.
289 In that case, we will abort below when we try to save a new GCC tree
290 for this object. We also need to handle the case of getting a dummy
291 type when a Full_View exists but be careful so as not to trigger its
292 premature elaboration. */
293 if ((!definition
|| (is_type
&& imported_p
))
294 && present_gnu_tree (gnat_entity
))
296 gnu_decl
= get_gnu_tree (gnat_entity
);
298 if (TREE_CODE (gnu_decl
) == TYPE_DECL
299 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
))
300 && IN (kind
, Incomplete_Or_Private_Kind
)
301 && Present (Full_View (gnat_entity
))
302 && (present_gnu_tree (Full_View (gnat_entity
))
303 || No (Freeze_Node (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 Esize
315 must be specified unless it was specified by the programmer. Exceptions
316 are for access-to-protected-subprogram types and all access subtypes, as
317 another GNAT type is used to lay out the GCC type for them. */
318 gcc_assert (!Unknown_Esize (gnat_entity
)
319 || Has_Size_Clause (gnat_entity
)
320 || (!IN (kind
, Numeric_Kind
)
321 && !IN (kind
, Enumeration_Kind
)
322 && (!IN (kind
, Access_Kind
)
323 || kind
== E_Access_Protected_Subprogram_Type
324 || kind
== E_Anonymous_Access_Protected_Subprogram_Type
325 || kind
== E_Access_Subtype
326 || type_annotate_only
)));
328 /* The RM size must be specified for all discrete and fixed-point types. */
329 gcc_assert (!(IN (kind
, Discrete_Or_Fixed_Point_Kind
)
330 && Unknown_RM_Size (gnat_entity
)));
332 /* If we get here, it means we have not yet done anything with this entity.
333 If we are not defining it, it must be a type or an entity that is defined
334 elsewhere or externally, otherwise we should have defined it already. */
335 gcc_assert (definition
336 || type_annotate_only
338 || kind
== E_Discriminant
339 || kind
== E_Component
341 || (kind
== E_Constant
&& Present (Full_View (gnat_entity
)))
342 || Is_Public (gnat_entity
));
344 /* Get the name of the entity and set up the line number and filename of
345 the original definition for use in any decl we make. */
346 gnu_entity_name
= get_entity_name (gnat_entity
);
347 Sloc_to_locus (Sloc (gnat_entity
), &input_location
);
349 /* For cases when we are not defining (i.e., we are referencing from
350 another compilation unit) public entities, show we are at global level
351 for the purpose of computing scopes. Don't do this for components or
352 discriminants since the relevant test is whether or not the record is
355 && kind
!= E_Component
356 && kind
!= E_Discriminant
357 && Is_Public (gnat_entity
)
358 && !Is_Statically_Allocated (gnat_entity
))
359 force_global
++, this_global
= true;
361 /* Handle any attributes directly attached to the entity. */
362 if (Has_Gigi_Rep_Item (gnat_entity
))
363 prepend_attributes (gnat_entity
, &attr_list
);
365 /* Do some common processing for types. */
368 /* Compute the equivalent type to be used in gigi. */
369 gnat_equiv_type
= Gigi_Equivalent_Type (gnat_entity
);
371 /* Machine_Attributes on types are expected to be propagated to
372 subtypes. The corresponding Gigi_Rep_Items are only attached
373 to the first subtype though, so we handle the propagation here. */
374 if (Base_Type (gnat_entity
) != gnat_entity
375 && !Is_First_Subtype (gnat_entity
)
376 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity
))))
377 prepend_attributes (First_Subtype (Base_Type (gnat_entity
)),
380 /* Compute a default value for the size of an elementary type. */
381 if (Known_Esize (gnat_entity
) && Is_Elementary_Type (gnat_entity
))
383 unsigned int max_esize
;
385 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity
)));
386 esize
= UI_To_Int (Esize (gnat_entity
));
388 if (IN (kind
, Float_Kind
))
389 max_esize
= fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE
);
390 else if (IN (kind
, Access_Kind
))
391 max_esize
= POINTER_SIZE
* 2;
393 max_esize
= LONG_LONG_TYPE_SIZE
;
395 if (esize
> max_esize
)
403 /* If this is a use of a deferred constant without address clause,
404 get its full definition. */
406 && No (Address_Clause (gnat_entity
))
407 && Present (Full_View (gnat_entity
)))
410 = gnat_to_gnu_entity (Full_View (gnat_entity
), gnu_expr
, 0);
415 /* If we have an external constant that we are not defining, get the
416 expression that is was defined to represent. We may throw it away
417 later if it is not a constant. But do not retrieve the expression
418 if it is an allocator because the designated type might be dummy
421 && !No_Initialization (Declaration_Node (gnat_entity
))
422 && Present (Expression (Declaration_Node (gnat_entity
)))
423 && Nkind (Expression (Declaration_Node (gnat_entity
)))
426 bool went_into_elab_proc
= false;
427 int save_force_global
= force_global
;
429 /* The expression may contain N_Expression_With_Actions nodes and
430 thus object declarations from other units. In this case, even
431 though the expression will eventually be discarded since not a
432 constant, the declarations would be stuck either in the global
433 varpool or in the current scope. Therefore we force the local
434 context and create a fake scope that we'll zap at the end. */
435 if (!current_function_decl
)
437 current_function_decl
= get_elaboration_procedure ();
438 went_into_elab_proc
= true;
443 gnu_expr
= gnat_to_gnu (Expression (Declaration_Node (gnat_entity
)));
446 force_global
= save_force_global
;
447 if (went_into_elab_proc
)
448 current_function_decl
= NULL_TREE
;
451 /* Ignore deferred constant definitions without address clause since
452 they are processed fully in the front-end. If No_Initialization
453 is set, this is not a deferred constant but a constant whose value
454 is built manually. And constants that are renamings are handled
458 && No (Address_Clause (gnat_entity
))
459 && !No_Initialization (Declaration_Node (gnat_entity
))
460 && No (Renamed_Object (gnat_entity
)))
462 gnu_decl
= error_mark_node
;
467 /* Ignore constant definitions already marked with the error node. See
468 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
471 && present_gnu_tree (gnat_entity
)
472 && get_gnu_tree (gnat_entity
) == error_mark_node
)
474 maybe_present
= true;
481 /* We used to special case VMS exceptions here to directly map them to
482 their associated condition code. Since this code had to be masked
483 dynamically to strip off the severity bits, this caused trouble in
484 the GCC/ZCX case because the "type" pointers we store in the tables
485 have to be static. We now don't special case here anymore, and let
486 the regular processing take place, which leaves us with a regular
487 exception data object for VMS exceptions too. The condition code
488 mapping is taken care of by the front end and the bitmasking by the
495 /* The GNAT record where the component was defined. */
496 Entity_Id gnat_record
= Underlying_Type (Scope (gnat_entity
));
498 /* If the variable is an inherited record component (in the case of
499 extended record types), just return the inherited entity, which
500 must be a FIELD_DECL. Likewise for discriminants.
501 For discriminants of untagged records which have explicit
502 stored discriminants, return the entity for the corresponding
503 stored discriminant. Also use Original_Record_Component
504 if the record has a private extension. */
505 if (Present (Original_Record_Component (gnat_entity
))
506 && Original_Record_Component (gnat_entity
) != gnat_entity
)
509 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity
),
510 gnu_expr
, definition
);
515 /* If the enclosing record has explicit stored discriminants,
516 then it is an untagged record. If the Corresponding_Discriminant
517 is not empty then this must be a renamed discriminant and its
518 Original_Record_Component must point to the corresponding explicit
519 stored discriminant (i.e. we should have taken the previous
521 else if (Present (Corresponding_Discriminant (gnat_entity
))
522 && Is_Tagged_Type (gnat_record
))
524 /* A tagged record has no explicit stored discriminants. */
525 gcc_assert (First_Discriminant (gnat_record
)
526 == First_Stored_Discriminant (gnat_record
));
528 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity
),
529 gnu_expr
, definition
);
534 else if (Present (CR_Discriminant (gnat_entity
))
535 && type_annotate_only
)
537 gnu_decl
= gnat_to_gnu_entity (CR_Discriminant (gnat_entity
),
538 gnu_expr
, definition
);
543 /* If the enclosing record has explicit stored discriminants, then
544 it is an untagged record. If the Corresponding_Discriminant
545 is not empty then this must be a renamed discriminant and its
546 Original_Record_Component must point to the corresponding explicit
547 stored discriminant (i.e. we should have taken the first
549 else if (Present (Corresponding_Discriminant (gnat_entity
))
550 && (First_Discriminant (gnat_record
)
551 != First_Stored_Discriminant (gnat_record
)))
554 /* Otherwise, if we are not defining this and we have no GCC type
555 for the containing record, make one for it. Then we should
556 have made our own equivalent. */
557 else if (!definition
&& !present_gnu_tree (gnat_record
))
559 /* ??? If this is in a record whose scope is a protected
560 type and we have an Original_Record_Component, use it.
561 This is a workaround for major problems in protected type
563 Entity_Id Scop
= Scope (Scope (gnat_entity
));
564 if ((Is_Protected_Type (Scop
)
565 || (Is_Private_Type (Scop
)
566 && Present (Full_View (Scop
))
567 && Is_Protected_Type (Full_View (Scop
))))
568 && Present (Original_Record_Component (gnat_entity
)))
571 = gnat_to_gnu_entity (Original_Record_Component
578 gnat_to_gnu_entity (Scope (gnat_entity
), NULL_TREE
, 0);
579 gnu_decl
= get_gnu_tree (gnat_entity
);
585 /* Here we have no GCC type and this is a reference rather than a
586 definition. This should never happen. Most likely the cause is
587 reference before declaration in the gnat tree for gnat_entity. */
591 case E_Loop_Parameter
:
592 case E_Out_Parameter
:
595 /* Simple variables, loop variables, Out parameters and exceptions. */
599 = ((kind
== E_Constant
|| kind
== E_Variable
)
600 && Is_True_Constant (gnat_entity
)
601 && !Treat_As_Volatile (gnat_entity
)
602 && (((Nkind (Declaration_Node (gnat_entity
))
603 == N_Object_Declaration
)
604 && Present (Expression (Declaration_Node (gnat_entity
))))
605 || Present (Renamed_Object (gnat_entity
))
607 bool inner_const_flag
= const_flag
;
608 bool static_p
= Is_Statically_Allocated (gnat_entity
);
609 bool mutable_p
= false;
610 bool used_by_ref
= false;
611 tree gnu_ext_name
= NULL_TREE
;
612 tree renamed_obj
= NULL_TREE
;
613 tree gnu_object_size
;
615 if (Present (Renamed_Object (gnat_entity
)) && !definition
)
617 if (kind
== E_Exception
)
618 gnu_expr
= gnat_to_gnu_entity (Renamed_Entity (gnat_entity
),
621 gnu_expr
= gnat_to_gnu (Renamed_Object (gnat_entity
));
624 /* Get the type after elaborating the renamed object. */
625 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
627 /* If this is a standard exception definition, then use the standard
628 exception type. This is necessary to make sure that imported and
629 exported views of exceptions are properly merged in LTO mode. */
630 if (TREE_CODE (TYPE_NAME (gnu_type
)) == TYPE_DECL
631 && DECL_NAME (TYPE_NAME (gnu_type
)) == exception_data_name_id
)
632 gnu_type
= except_type_node
;
634 /* For a debug renaming declaration, build a debug-only entity. */
635 if (Present (Debug_Renaming_Link (gnat_entity
)))
637 /* Force a non-null value to make sure the symbol is retained. */
638 tree value
= build1 (INDIRECT_REF
, gnu_type
,
640 build_pointer_type (gnu_type
),
641 integer_minus_one_node
));
642 gnu_decl
= build_decl (input_location
,
643 VAR_DECL
, gnu_entity_name
, gnu_type
);
644 SET_DECL_VALUE_EXPR (gnu_decl
, value
);
645 DECL_HAS_VALUE_EXPR_P (gnu_decl
) = 1;
646 gnat_pushdecl (gnu_decl
, gnat_entity
);
650 /* If this is a loop variable, its type should be the base type.
651 This is because the code for processing a loop determines whether
652 a normal loop end test can be done by comparing the bounds of the
653 loop against those of the base type, which is presumed to be the
654 size used for computation. But this is not correct when the size
655 of the subtype is smaller than the type. */
656 if (kind
== E_Loop_Parameter
)
657 gnu_type
= get_base_type (gnu_type
);
659 /* Reject non-renamed objects whose type is an unconstrained array or
660 any object whose type is a dummy type or void. */
661 if ((TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
662 && No (Renamed_Object (gnat_entity
)))
663 || TYPE_IS_DUMMY_P (gnu_type
)
664 || TREE_CODE (gnu_type
) == VOID_TYPE
)
666 gcc_assert (type_annotate_only
);
669 return error_mark_node
;
672 /* If an alignment is specified, use it if valid. Note that exceptions
673 are objects but don't have an alignment. We must do this before we
674 validate the size, since the alignment can affect the size. */
675 if (kind
!= E_Exception
&& Known_Alignment (gnat_entity
))
677 gcc_assert (Present (Alignment (gnat_entity
)));
679 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
680 TYPE_ALIGN (gnu_type
));
682 /* No point in changing the type if there is an address clause
683 as the final type of the object will be a reference type. */
684 if (Present (Address_Clause (gnat_entity
)))
688 tree orig_type
= gnu_type
;
691 = maybe_pad_type (gnu_type
, NULL_TREE
, align
, gnat_entity
,
692 false, false, definition
, true);
694 /* If a padding record was made, declare it now since it will
695 never be declared otherwise. This is necessary to ensure
696 that its subtrees are properly marked. */
697 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
698 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true,
699 debug_info_p
, gnat_entity
);
703 /* If we are defining the object, see if it has a Size and validate it
704 if so. If we are not defining the object and a Size clause applies,
705 simply retrieve the value. We don't want to ignore the clause and
706 it is expected to have been validated already. Then get the new
709 gnu_size
= validate_size (Esize (gnat_entity
), gnu_type
,
710 gnat_entity
, VAR_DECL
, false,
711 Has_Size_Clause (gnat_entity
));
712 else if (Has_Size_Clause (gnat_entity
))
713 gnu_size
= UI_To_gnu (Esize (gnat_entity
), bitsizetype
);
718 = make_type_from_size (gnu_type
, gnu_size
,
719 Has_Biased_Representation (gnat_entity
));
721 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0))
722 gnu_size
= NULL_TREE
;
725 /* If this object has self-referential size, it must be a record with
726 a default discriminant. We are supposed to allocate an object of
727 the maximum size in this case, unless it is a constant with an
728 initializing expression, in which case we can get the size from
729 that. Note that the resulting size may still be a variable, so
730 this may end up with an indirect allocation. */
731 if (No (Renamed_Object (gnat_entity
))
732 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
734 if (gnu_expr
&& kind
== E_Constant
)
736 tree size
= TYPE_SIZE (TREE_TYPE (gnu_expr
));
737 if (CONTAINS_PLACEHOLDER_P (size
))
739 /* If the initializing expression is itself a constant,
740 despite having a nominal type with self-referential
741 size, we can get the size directly from it. */
742 if (TREE_CODE (gnu_expr
) == COMPONENT_REF
744 (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0)))
745 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == VAR_DECL
746 && (TREE_READONLY (TREE_OPERAND (gnu_expr
, 0))
747 || DECL_READONLY_ONCE_ELAB
748 (TREE_OPERAND (gnu_expr
, 0))))
749 gnu_size
= DECL_SIZE (TREE_OPERAND (gnu_expr
, 0));
752 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, gnu_expr
);
757 /* We may have no GNU_EXPR because No_Initialization is
758 set even though there's an Expression. */
759 else if (kind
== E_Constant
760 && (Nkind (Declaration_Node (gnat_entity
))
761 == N_Object_Declaration
)
762 && Present (Expression (Declaration_Node (gnat_entity
))))
764 = TYPE_SIZE (gnat_to_gnu_type
766 (Expression (Declaration_Node (gnat_entity
)))));
769 gnu_size
= max_size (TYPE_SIZE (gnu_type
), true);
773 /* If we are at global level and the size isn't constant, call
774 elaborate_expression_1 to make a variable for it rather than
775 calculating it each time. */
776 if (global_bindings_p () && !TREE_CONSTANT (gnu_size
))
777 gnu_size
= elaborate_expression_1 (gnu_size
, gnat_entity
,
778 get_identifier ("SIZE"),
782 /* If the size is zero byte, make it one byte since some linkers have
783 troubles with zero-sized objects. If the object will have a
784 template, that will make it nonzero so don't bother. Also avoid
785 doing that for an object renaming or an object with an address
786 clause, as we would lose useful information on the view size
787 (e.g. for null array slices) and we are not allocating the object
790 && integer_zerop (gnu_size
)
791 && !TREE_OVERFLOW (gnu_size
))
792 || (TYPE_SIZE (gnu_type
)
793 && integer_zerop (TYPE_SIZE (gnu_type
))
794 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type
))))
795 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
796 || !Is_Array_Type (Etype (gnat_entity
)))
797 && No (Renamed_Object (gnat_entity
))
798 && No (Address_Clause (gnat_entity
)))
799 gnu_size
= bitsize_unit_node
;
801 /* If this is an object with no specified size and alignment, and
802 if either it is atomic or we are not optimizing alignment for
803 space and it is composite and not an exception, an Out parameter
804 or a reference to another object, and the size of its type is a
805 constant, set the alignment to the smallest one which is not
806 smaller than the size, with an appropriate cap. */
807 if (!gnu_size
&& align
== 0
808 && (Is_Atomic (gnat_entity
)
809 || (!Optimize_Alignment_Space (gnat_entity
)
810 && kind
!= E_Exception
811 && kind
!= E_Out_Parameter
812 && Is_Composite_Type (Etype (gnat_entity
))
813 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
814 && !Is_Exported (gnat_entity
)
816 && No (Renamed_Object (gnat_entity
))
817 && No (Address_Clause (gnat_entity
))))
818 && TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
)
820 unsigned int size_cap
, align_cap
;
822 /* No point in promoting the alignment if this doesn't prevent
823 BLKmode access to the object, in particular block copy, as
824 this will for example disable the NRV optimization for it.
825 No point in jumping through all the hoops needed in order
826 to support BIGGEST_ALIGNMENT if we don't really have to.
827 So we cap to the smallest alignment that corresponds to
828 a known efficient memory access pattern of the target. */
829 if (Is_Atomic (gnat_entity
))
832 align_cap
= BIGGEST_ALIGNMENT
;
836 size_cap
= MAX_FIXED_MODE_SIZE
;
837 align_cap
= get_mode_alignment (ptr_mode
);
840 if (!host_integerp (TYPE_SIZE (gnu_type
), 1)
841 || compare_tree_int (TYPE_SIZE (gnu_type
), size_cap
) > 0)
843 else if (compare_tree_int (TYPE_SIZE (gnu_type
), align_cap
) > 0)
846 align
= ceil_pow2 (tree_low_cst (TYPE_SIZE (gnu_type
), 1));
848 /* But make sure not to under-align the object. */
849 if (align
<= TYPE_ALIGN (gnu_type
))
852 /* And honor the minimum valid atomic alignment, if any. */
853 #ifdef MINIMUM_ATOMIC_ALIGNMENT
854 else if (align
< MINIMUM_ATOMIC_ALIGNMENT
)
855 align
= MINIMUM_ATOMIC_ALIGNMENT
;
859 /* If the object is set to have atomic components, find the component
860 type and validate it.
862 ??? Note that we ignore Has_Volatile_Components on objects; it's
863 not at all clear what to do in that case. */
864 if (Has_Atomic_Components (gnat_entity
))
866 tree gnu_inner
= (TREE_CODE (gnu_type
) == ARRAY_TYPE
867 ? TREE_TYPE (gnu_type
) : gnu_type
);
869 while (TREE_CODE (gnu_inner
) == ARRAY_TYPE
870 && TYPE_MULTI_ARRAY_P (gnu_inner
))
871 gnu_inner
= TREE_TYPE (gnu_inner
);
873 check_ok_for_atomic (gnu_inner
, gnat_entity
, true);
876 /* Now check if the type of the object allows atomic access. Note
877 that we must test the type, even if this object has size and
878 alignment to allow such access, because we will be going inside
879 the padded record to assign to the object. We could fix this by
880 always copying via an intermediate value, but it's not clear it's
882 if (Is_Atomic (gnat_entity
))
883 check_ok_for_atomic (gnu_type
, gnat_entity
, false);
885 /* If this is an aliased object with an unconstrained nominal subtype,
886 make a type that includes the template. */
887 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
888 && Is_Array_Type (Etype (gnat_entity
))
889 && !type_annotate_only
)
892 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
894 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array
),
896 concat_name (gnu_entity_name
,
901 /* ??? If this is an object of CW type initialized to a value, try to
902 ensure that the object is sufficient aligned for this value, but
903 without pessimizing the allocation. This is a kludge necessary
904 because we don't support dynamic alignment. */
906 && Ekind (Etype (gnat_entity
)) == E_Class_Wide_Subtype
907 && No (Renamed_Object (gnat_entity
))
908 && No (Address_Clause (gnat_entity
)))
909 align
= get_target_system_allocator_alignment () * BITS_PER_UNIT
;
911 #ifdef MINIMUM_ATOMIC_ALIGNMENT
912 /* If the size is a constant and no alignment is specified, force
913 the alignment to be the minimum valid atomic alignment. The
914 restriction on constant size avoids problems with variable-size
915 temporaries; if the size is variable, there's no issue with
916 atomic access. Also don't do this for a constant, since it isn't
917 necessary and can interfere with constant replacement. Finally,
918 do not do it for Out parameters since that creates an
919 size inconsistency with In parameters. */
921 && MINIMUM_ATOMIC_ALIGNMENT
> TYPE_ALIGN (gnu_type
)
922 && !FLOAT_TYPE_P (gnu_type
)
923 && !const_flag
&& No (Renamed_Object (gnat_entity
))
924 && !imported_p
&& No (Address_Clause (gnat_entity
))
925 && kind
!= E_Out_Parameter
926 && (gnu_size
? TREE_CODE (gnu_size
) == INTEGER_CST
927 : TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
))
928 align
= MINIMUM_ATOMIC_ALIGNMENT
;
931 /* Make a new type with the desired size and alignment, if needed.
932 But do not take into account alignment promotions to compute the
933 size of the object. */
934 gnu_object_size
= gnu_size
? gnu_size
: TYPE_SIZE (gnu_type
);
935 if (gnu_size
|| align
> 0)
937 tree orig_type
= gnu_type
;
939 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
940 false, false, definition
, true);
942 /* If a padding record was made, declare it now since it will
943 never be declared otherwise. This is necessary to ensure
944 that its subtrees are properly marked. */
945 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
946 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true,
947 debug_info_p
, gnat_entity
);
950 /* If this is a renaming, avoid as much as possible to create a new
951 object. However, in several cases, creating it is required.
952 This processing needs to be applied to the raw expression so
953 as to make it more likely to rename the underlying object. */
954 if (Present (Renamed_Object (gnat_entity
)))
956 bool create_normal_object
= false;
958 /* If the renamed object had padding, strip off the reference
959 to the inner object and reset our type. */
960 if ((TREE_CODE (gnu_expr
) == COMPONENT_REF
961 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0))))
962 /* Strip useless conversions around the object. */
963 || gnat_useless_type_conversion (gnu_expr
))
965 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
966 gnu_type
= TREE_TYPE (gnu_expr
);
969 /* Or else, if the renamed object has an unconstrained type with
970 default discriminant, use the padded type. */
971 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr
))
972 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr
)))
974 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
975 gnu_type
= TREE_TYPE (gnu_expr
);
977 /* Case 1: If this is a constant renaming stemming from a function
978 call, treat it as a normal object whose initial value is what
979 is being renamed. RM 3.3 says that the result of evaluating a
980 function call is a constant object. As a consequence, it can
981 be the inner object of a constant renaming. In this case, the
982 renaming must be fully instantiated, i.e. it cannot be a mere
983 reference to (part of) an existing object. */
986 tree inner_object
= gnu_expr
;
987 while (handled_component_p (inner_object
))
988 inner_object
= TREE_OPERAND (inner_object
, 0);
989 if (TREE_CODE (inner_object
) == CALL_EXPR
)
990 create_normal_object
= true;
993 /* Otherwise, see if we can proceed with a stabilized version of
994 the renamed entity or if we need to make a new object. */
995 if (!create_normal_object
)
997 tree maybe_stable_expr
= NULL_TREE
;
1000 /* Case 2: If the renaming entity need not be materialized and
1001 the renamed expression is something we can stabilize, use
1002 that for the renaming. At the global level, we can only do
1003 this if we know no SAVE_EXPRs need be made, because the
1004 expression we return might be used in arbitrary conditional
1005 branches so we must force the evaluation of the SAVE_EXPRs
1006 immediately and this requires a proper function context.
1007 Note that an external constant is at the global level. */
1008 if (!Materialize_Entity (gnat_entity
)
1009 && (!((!definition
&& kind
== E_Constant
)
1010 || global_bindings_p ())
1011 || (staticp (gnu_expr
)
1012 && !TREE_SIDE_EFFECTS (gnu_expr
))))
1015 = gnat_stabilize_reference (gnu_expr
, true, &stable
);
1019 /* ??? No DECL_EXPR is created so we need to mark
1020 the expression manually lest it is shared. */
1021 if ((!definition
&& kind
== E_Constant
)
1022 || global_bindings_p ())
1023 MARK_VISITED (maybe_stable_expr
);
1024 gnu_decl
= maybe_stable_expr
;
1025 save_gnu_tree (gnat_entity
, gnu_decl
, true);
1027 annotate_object (gnat_entity
, gnu_type
, NULL_TREE
,
1029 /* This assertion will fail if the renamed object
1030 isn't aligned enough as to make it possible to
1031 honor the alignment set on the renaming. */
1034 unsigned int renamed_align
1036 ? DECL_ALIGN (gnu_decl
)
1037 : TYPE_ALIGN (TREE_TYPE (gnu_decl
));
1038 gcc_assert (renamed_align
>= align
);
1043 /* The stabilization failed. Keep maybe_stable_expr
1044 untouched here to let the pointer case below know
1045 about that failure. */
1048 /* Case 3: If this is a constant renaming and creating a
1049 new object is allowed and cheap, treat it as a normal
1050 object whose initial value is what is being renamed. */
1052 && !Is_Composite_Type
1053 (Underlying_Type (Etype (gnat_entity
))))
1056 /* Case 4: Make this into a constant pointer to the object we
1057 are to rename and attach the object to the pointer if it is
1058 something we can stabilize.
1060 From the proper scope, attached objects will be referenced
1061 directly instead of indirectly via the pointer to avoid
1062 subtle aliasing problems with non-addressable entities.
1063 They have to be stable because we must not evaluate the
1064 variables in the expression every time the renaming is used.
1065 The pointer is called a "renaming" pointer in this case.
1067 In the rare cases where we cannot stabilize the renamed
1068 object, we just make a "bare" pointer, and the renamed
1069 entity is always accessed indirectly through it. */
1072 /* We need to preserve the volatileness of the renamed
1073 object through the indirection. */
1074 if (TREE_THIS_VOLATILE (gnu_expr
)
1075 && !TYPE_VOLATILE (gnu_type
))
1077 = build_qualified_type (gnu_type
,
1078 (TYPE_QUALS (gnu_type
)
1079 | TYPE_QUAL_VOLATILE
));
1080 gnu_type
= build_reference_type (gnu_type
);
1081 inner_const_flag
= TREE_READONLY (gnu_expr
);
1084 /* If the previous attempt at stabilizing failed, there
1085 is no point in trying again and we reuse the result
1086 without attaching it to the pointer. In this case it
1087 will only be used as the initializing expression of
1088 the pointer and thus needs no special treatment with
1089 regard to multiple evaluations. */
1090 if (maybe_stable_expr
)
1093 /* Otherwise, try to stabilize and attach the expression
1094 to the pointer if the stabilization succeeds.
1096 Note that this might introduce SAVE_EXPRs and we don't
1097 check whether we're at the global level or not. This
1098 is fine since we are building a pointer initializer and
1099 neither the pointer nor the initializing expression can
1100 be accessed before the pointer elaboration has taken
1101 place in a correct program.
1103 These SAVE_EXPRs will be evaluated at the right place
1104 by either the evaluation of the initializer for the
1105 non-global case or the elaboration code for the global
1106 case, and will be attached to the elaboration procedure
1107 in the latter case. */
1111 = gnat_stabilize_reference (gnu_expr
, true, &stable
);
1114 renamed_obj
= maybe_stable_expr
;
1116 /* Attaching is actually performed downstream, as soon
1117 as we have a VAR_DECL for the pointer we make. */
1120 gnu_expr
= build_unary_op (ADDR_EXPR
, gnu_type
,
1123 gnu_size
= NULL_TREE
;
1129 /* Make a volatile version of this object's type if we are to make
1130 the object volatile. We also interpret 13.3(19) conservatively
1131 and disallow any optimizations for such a non-constant object. */
1132 if ((Treat_As_Volatile (gnat_entity
)
1134 && gnu_type
!= except_type_node
1135 && (Is_Exported (gnat_entity
)
1137 || Present (Address_Clause (gnat_entity
)))))
1138 && !TYPE_VOLATILE (gnu_type
))
1139 gnu_type
= build_qualified_type (gnu_type
,
1140 (TYPE_QUALS (gnu_type
)
1141 | TYPE_QUAL_VOLATILE
));
1143 /* If we are defining an aliased object whose nominal subtype is
1144 unconstrained, the object is a record that contains both the
1145 template and the object. If there is an initializer, it will
1146 have already been converted to the right type, but we need to
1147 create the template if there is no initializer. */
1150 && TREE_CODE (gnu_type
) == RECORD_TYPE
1151 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type
)
1152 /* Beware that padding might have been introduced above. */
1153 || (TYPE_PADDING_P (gnu_type
)
1154 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1156 && TYPE_CONTAINS_TEMPLATE_P
1157 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))))
1160 = TYPE_PADDING_P (gnu_type
)
1161 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1162 : TYPE_FIELDS (gnu_type
);
1163 vec
<constructor_elt
, va_gc
> *v
;
1165 tree t
= build_template (TREE_TYPE (template_field
),
1166 TREE_TYPE (DECL_CHAIN (template_field
)),
1168 CONSTRUCTOR_APPEND_ELT (v
, template_field
, t
);
1169 gnu_expr
= gnat_build_constructor (gnu_type
, v
);
1172 /* Convert the expression to the type of the object except in the
1173 case where the object's type is unconstrained or the object's type
1174 is a padded record whose field is of self-referential size. In
1175 the former case, converting will generate unnecessary evaluations
1176 of the CONSTRUCTOR to compute the size and in the latter case, we
1177 want to only copy the actual data. Also don't convert to a record
1178 type with a variant part from a record type without one, to keep
1179 the object simpler. */
1181 && TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
1182 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
1183 && !(TYPE_IS_PADDING_P (gnu_type
)
1184 && CONTAINS_PLACEHOLDER_P
1185 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))))
1186 && !(TREE_CODE (gnu_type
) == RECORD_TYPE
1187 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
1188 && get_variant_part (gnu_type
) != NULL_TREE
1189 && get_variant_part (TREE_TYPE (gnu_expr
)) == NULL_TREE
))
1190 gnu_expr
= convert (gnu_type
, gnu_expr
);
1192 /* If this is a pointer that doesn't have an initializing expression,
1193 initialize it to NULL, unless the object is imported. */
1195 && (POINTER_TYPE_P (gnu_type
) || TYPE_IS_FAT_POINTER_P (gnu_type
))
1197 && !Is_Imported (gnat_entity
))
1198 gnu_expr
= integer_zero_node
;
1200 /* If we are defining the object and it has an Address clause, we must
1201 either get the address expression from the saved GCC tree for the
1202 object if it has a Freeze node, or elaborate the address expression
1203 here since the front-end has guaranteed that the elaboration has no
1204 effects in this case. */
1205 if (definition
&& Present (Address_Clause (gnat_entity
)))
1207 Node_Id gnat_expr
= Expression (Address_Clause (gnat_entity
));
1209 = present_gnu_tree (gnat_entity
)
1210 ? get_gnu_tree (gnat_entity
) : gnat_to_gnu (gnat_expr
);
1212 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
1214 /* Ignore the size. It's either meaningless or was handled
1216 gnu_size
= NULL_TREE
;
1217 /* Convert the type of the object to a reference type that can
1218 alias everything as per 13.3(19). */
1220 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1221 gnu_address
= convert (gnu_type
, gnu_address
);
1224 = !Is_Public (gnat_entity
)
1225 || compile_time_known_address_p (gnat_expr
);
1227 /* If this is a deferred constant, the initializer is attached to
1229 if (kind
== E_Constant
&& Present (Full_View (gnat_entity
)))
1232 (Expression (Declaration_Node (Full_View (gnat_entity
))));
1234 /* If we don't have an initializing expression for the underlying
1235 variable, the initializing expression for the pointer is the
1236 specified address. Otherwise, we have to make a COMPOUND_EXPR
1237 to assign both the address and the initial value. */
1239 gnu_expr
= gnu_address
;
1242 = build2 (COMPOUND_EXPR
, gnu_type
,
1244 (MODIFY_EXPR
, NULL_TREE
,
1245 build_unary_op (INDIRECT_REF
, NULL_TREE
,
1251 /* If it has an address clause and we are not defining it, mark it
1252 as an indirect object. Likewise for Stdcall objects that are
1254 if ((!definition
&& Present (Address_Clause (gnat_entity
)))
1255 || (Is_Imported (gnat_entity
)
1256 && Has_Stdcall_Convention (gnat_entity
)))
1258 /* Convert the type of the object to a reference type that can
1259 alias everything as per 13.3(19). */
1261 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1262 gnu_size
= NULL_TREE
;
1264 /* No point in taking the address of an initializing expression
1265 that isn't going to be used. */
1266 gnu_expr
= NULL_TREE
;
1268 /* If it has an address clause whose value is known at compile
1269 time, make the object a CONST_DECL. This will avoid a
1270 useless dereference. */
1271 if (Present (Address_Clause (gnat_entity
)))
1273 Node_Id gnat_address
1274 = Expression (Address_Clause (gnat_entity
));
1276 if (compile_time_known_address_p (gnat_address
))
1278 gnu_expr
= gnat_to_gnu (gnat_address
);
1286 /* If we are at top level and this object is of variable size,
1287 make the actual type a hidden pointer to the real type and
1288 make the initializer be a memory allocation and initialization.
1289 Likewise for objects we aren't defining (presumed to be
1290 external references from other packages), but there we do
1291 not set up an initialization.
1293 If the object's size overflows, make an allocator too, so that
1294 Storage_Error gets raised. Note that we will never free
1295 such memory, so we presume it never will get allocated. */
1296 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type
),
1297 global_bindings_p ()
1301 && !allocatable_size_p (convert (sizetype
,
1303 (CEIL_DIV_EXPR
, gnu_size
,
1304 bitsize_unit_node
)),
1305 global_bindings_p ()
1309 gnu_type
= build_reference_type (gnu_type
);
1310 gnu_size
= NULL_TREE
;
1313 /* In case this was a aliased object whose nominal subtype is
1314 unconstrained, the pointer above will be a thin pointer and
1315 build_allocator will automatically make the template.
1317 If we have a template initializer only (that we made above),
1318 pretend there is none and rely on what build_allocator creates
1319 again anyway. Otherwise (if we have a full initializer), get
1320 the data part and feed that to build_allocator.
1322 If we are elaborating a mutable object, tell build_allocator to
1323 ignore a possibly simpler size from the initializer, if any, as
1324 we must allocate the maximum possible size in this case. */
1325 if (definition
&& !imported_p
)
1327 tree gnu_alloc_type
= TREE_TYPE (gnu_type
);
1329 if (TREE_CODE (gnu_alloc_type
) == RECORD_TYPE
1330 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type
))
1333 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type
)));
1335 if (TREE_CODE (gnu_expr
) == CONSTRUCTOR
1336 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr
)))
1340 = build_component_ref
1341 (gnu_expr
, NULL_TREE
,
1342 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr
))),
1346 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type
)) == INTEGER_CST
1347 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type
)))
1348 post_error ("?`Storage_Error` will be raised at run time!",
1352 = build_allocator (gnu_alloc_type
, gnu_expr
, gnu_type
,
1353 Empty
, Empty
, gnat_entity
, mutable_p
);
1358 gnu_expr
= NULL_TREE
;
1363 /* If this object would go into the stack and has an alignment larger
1364 than the largest stack alignment the back-end can honor, resort to
1365 a variable of "aligning type". */
1366 if (!global_bindings_p () && !static_p
&& definition
1367 && !imported_p
&& TYPE_ALIGN (gnu_type
) > BIGGEST_ALIGNMENT
)
1369 /* Create the new variable. No need for extra room before the
1370 aligned field as this is in automatic storage. */
1372 = make_aligning_type (gnu_type
, TYPE_ALIGN (gnu_type
),
1373 TYPE_SIZE_UNIT (gnu_type
),
1374 BIGGEST_ALIGNMENT
, 0, gnat_entity
);
1376 = create_var_decl (create_concat_name (gnat_entity
, "ALIGN"),
1377 NULL_TREE
, gnu_new_type
, NULL_TREE
, false,
1378 false, false, false, NULL
, gnat_entity
);
1380 /* Initialize the aligned field if we have an initializer. */
1383 (build_binary_op (MODIFY_EXPR
, NULL_TREE
,
1385 (gnu_new_var
, NULL_TREE
,
1386 TYPE_FIELDS (gnu_new_type
), false),
1390 /* And setup this entity as a reference to the aligned field. */
1391 gnu_type
= build_reference_type (gnu_type
);
1394 (ADDR_EXPR
, gnu_type
,
1395 build_component_ref (gnu_new_var
, NULL_TREE
,
1396 TYPE_FIELDS (gnu_new_type
), false));
1398 gnu_size
= NULL_TREE
;
1403 /* If this is an aliased object with an unconstrained nominal subtype,
1404 we make its type a thin reference, i.e. the reference counterpart
1405 of a thin pointer, so that it points to the array part. This is
1406 aimed at making it easier for the debugger to decode the object.
1407 Note that we have to do that this late because of the couple of
1408 allocation adjustments that might be made just above. */
1409 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
1410 && Is_Array_Type (Etype (gnat_entity
))
1411 && !type_annotate_only
)
1414 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
1416 /* In case the object with the template has already been allocated
1417 just above, we have nothing to do here. */
1418 if (!TYPE_IS_THIN_POINTER_P (gnu_type
))
1421 = create_var_decl (concat_name (gnu_entity_name
, "UNC"),
1422 NULL_TREE
, gnu_type
, gnu_expr
,
1423 const_flag
, Is_Public (gnat_entity
),
1424 imported_p
|| !definition
, static_p
,
1427 = build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_unc_var
);
1428 TREE_CONSTANT (gnu_expr
) = 1;
1430 gnu_size
= NULL_TREE
;
1436 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array
));
1440 gnu_type
= build_qualified_type (gnu_type
, (TYPE_QUALS (gnu_type
)
1441 | TYPE_QUAL_CONST
));
1443 /* Convert the expression to the type of the object except in the
1444 case where the object's type is unconstrained or the object's type
1445 is a padded record whose field is of self-referential size. In
1446 the former case, converting will generate unnecessary evaluations
1447 of the CONSTRUCTOR to compute the size and in the latter case, we
1448 want to only copy the actual data. Also don't convert to a record
1449 type with a variant part from a record type without one, to keep
1450 the object simpler. */
1452 && TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
1453 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
1454 && !(TYPE_IS_PADDING_P (gnu_type
)
1455 && CONTAINS_PLACEHOLDER_P
1456 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))))
1457 && !(TREE_CODE (gnu_type
) == RECORD_TYPE
1458 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
1459 && get_variant_part (gnu_type
) != NULL_TREE
1460 && get_variant_part (TREE_TYPE (gnu_expr
)) == NULL_TREE
))
1461 gnu_expr
= convert (gnu_type
, gnu_expr
);
1463 /* If this name is external or there was a name specified, use it,
1464 unless this is a VMS exception object since this would conflict
1465 with the symbol we need to export in addition. Don't use the
1466 Interface_Name if there is an address clause (see CD30005). */
1467 if (!Is_VMS_Exception (gnat_entity
)
1468 && ((Present (Interface_Name (gnat_entity
))
1469 && No (Address_Clause (gnat_entity
)))
1470 || (Is_Public (gnat_entity
)
1471 && (!Is_Imported (gnat_entity
)
1472 || Is_Exported (gnat_entity
)))))
1473 gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
1475 /* If this is an aggregate constant initialized to a constant, force it
1476 to be statically allocated. This saves an initialization copy. */
1479 && gnu_expr
&& TREE_CONSTANT (gnu_expr
)
1480 && AGGREGATE_TYPE_P (gnu_type
)
1481 && host_integerp (TYPE_SIZE_UNIT (gnu_type
), 1)
1482 && !(TYPE_IS_PADDING_P (gnu_type
)
1483 && !host_integerp (TYPE_SIZE_UNIT
1484 (TREE_TYPE (TYPE_FIELDS (gnu_type
))), 1)))
1487 /* Now create the variable or the constant and set various flags. */
1489 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
1490 gnu_expr
, const_flag
, Is_Public (gnat_entity
),
1491 imported_p
|| !definition
, static_p
, attr_list
,
1493 DECL_BY_REF_P (gnu_decl
) = used_by_ref
;
1494 DECL_POINTS_TO_READONLY_P (gnu_decl
) = used_by_ref
&& inner_const_flag
;
1495 DECL_CAN_NEVER_BE_NULL_P (gnu_decl
) = Can_Never_Be_Null (gnat_entity
);
1497 /* If we are defining an Out parameter and optimization isn't enabled,
1498 create a fake PARM_DECL for debugging purposes and make it point to
1499 the VAR_DECL. Suppress debug info for the latter but make sure it
1500 will live on the stack so that it can be accessed from within the
1501 debugger through the PARM_DECL. */
1502 if (kind
== E_Out_Parameter
1506 && !flag_generate_lto
)
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
,
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 integer 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;
1632 /* We have a list of enumeral constants in First_Literal. We make a
1633 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1634 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1635 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1636 value of the literal. But when we have a regular boolean type, we
1637 simplify this a little by using a BOOLEAN_TYPE. */
1638 const bool is_boolean
= Is_Boolean_Type (gnat_entity
)
1639 && !Has_Non_Standard_Rep (gnat_entity
);
1640 const bool is_unsigned
= Is_Unsigned_Type (gnat_entity
);
1641 tree gnu_list
= NULL_TREE
;
1642 Entity_Id gnat_literal
;
1644 gnu_type
= make_node (is_boolean
? BOOLEAN_TYPE
: ENUMERAL_TYPE
);
1645 TYPE_PRECISION (gnu_type
) = esize
;
1646 TYPE_UNSIGNED (gnu_type
) = is_unsigned
;
1647 set_min_and_max_values_for_integral_type (gnu_type
, esize
,
1649 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
1650 layout_type (gnu_type
);
1652 for (gnat_literal
= First_Literal (gnat_entity
);
1653 Present (gnat_literal
);
1654 gnat_literal
= Next_Literal (gnat_literal
))
1657 = UI_To_gnu (Enumeration_Rep (gnat_literal
), gnu_type
);
1659 = create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
1660 gnu_type
, gnu_value
, true, false, false,
1661 false, NULL
, gnat_literal
);
1662 /* Do not generate debug info for individual enumerators. */
1663 DECL_IGNORED_P (gnu_literal
) = 1;
1664 save_gnu_tree (gnat_literal
, gnu_literal
, false);
1666 = tree_cons (DECL_NAME (gnu_literal
), gnu_value
, gnu_list
);
1670 TYPE_VALUES (gnu_type
) = nreverse (gnu_list
);
1672 /* Note that the bounds are updated at the end of this function
1673 to avoid an infinite recursion since they refer to the type. */
1678 case E_Signed_Integer_Type
:
1679 case E_Ordinary_Fixed_Point_Type
:
1680 case E_Decimal_Fixed_Point_Type
:
1681 /* For integer types, just make a signed type the appropriate number
1683 gnu_type
= make_signed_type (esize
);
1686 case E_Modular_Integer_Type
:
1688 /* For modular types, make the unsigned type of the proper number
1689 of bits and then set up the modulus, if required. */
1690 tree gnu_modulus
, gnu_high
= NULL_TREE
;
1692 /* Packed array types are supposed to be subtypes only. */
1693 gcc_assert (!Is_Packed_Array_Type (gnat_entity
));
1695 gnu_type
= make_unsigned_type (esize
);
1697 /* Get the modulus in this type. If it overflows, assume it is because
1698 it is equal to 2**Esize. Note that there is no overflow checking
1699 done on unsigned type, so we detect the overflow by looking for
1700 a modulus of zero, which is otherwise invalid. */
1701 gnu_modulus
= UI_To_gnu (Modulus (gnat_entity
), gnu_type
);
1703 if (!integer_zerop (gnu_modulus
))
1705 TYPE_MODULAR_P (gnu_type
) = 1;
1706 SET_TYPE_MODULUS (gnu_type
, gnu_modulus
);
1707 gnu_high
= fold_build2 (MINUS_EXPR
, gnu_type
, gnu_modulus
,
1708 convert (gnu_type
, integer_one_node
));
1711 /* If the upper bound is not maximal, make an extra subtype. */
1713 && !tree_int_cst_equal (gnu_high
, TYPE_MAX_VALUE (gnu_type
)))
1715 tree gnu_subtype
= make_unsigned_type (esize
);
1716 SET_TYPE_RM_MAX_VALUE (gnu_subtype
, gnu_high
);
1717 TREE_TYPE (gnu_subtype
) = gnu_type
;
1718 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
1719 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "UMT");
1720 gnu_type
= gnu_subtype
;
1725 case E_Signed_Integer_Subtype
:
1726 case E_Enumeration_Subtype
:
1727 case E_Modular_Integer_Subtype
:
1728 case E_Ordinary_Fixed_Point_Subtype
:
1729 case E_Decimal_Fixed_Point_Subtype
:
1731 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1732 not want to call create_range_type since we would like each subtype
1733 node to be distinct. ??? Historically this was in preparation for
1734 when memory aliasing is implemented, but that's obsolete now given
1735 the call to relate_alias_sets below.
1737 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1738 this fact is used by the arithmetic conversion functions.
1740 We elaborate the Ancestor_Subtype if it is not in the current unit
1741 and one of our bounds is non-static. We do this to ensure consistent
1742 naming in the case where several subtypes share the same bounds, by
1743 elaborating the first such subtype first, thus using its name. */
1746 && Present (Ancestor_Subtype (gnat_entity
))
1747 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1748 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1749 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
1750 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
), gnu_expr
, 0);
1752 /* Set the precision to the Esize except for bit-packed arrays. */
1753 if (Is_Packed_Array_Type (gnat_entity
)
1754 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1755 esize
= UI_To_Int (RM_Size (gnat_entity
));
1757 /* This should be an unsigned type if the base type is unsigned or
1758 if the lower bound is constant and non-negative or if the type
1760 if (Is_Unsigned_Type (Etype (gnat_entity
))
1761 || Is_Unsigned_Type (gnat_entity
)
1762 || Has_Biased_Representation (gnat_entity
))
1763 gnu_type
= make_unsigned_type (esize
);
1765 gnu_type
= make_signed_type (esize
);
1766 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
1768 SET_TYPE_RM_MIN_VALUE
1770 convert (TREE_TYPE (gnu_type
),
1771 elaborate_expression (Type_Low_Bound (gnat_entity
),
1772 gnat_entity
, get_identifier ("L"),
1774 Needs_Debug_Info (gnat_entity
))));
1776 SET_TYPE_RM_MAX_VALUE
1778 convert (TREE_TYPE (gnu_type
),
1779 elaborate_expression (Type_High_Bound (gnat_entity
),
1780 gnat_entity
, get_identifier ("U"),
1782 Needs_Debug_Info (gnat_entity
))));
1784 TYPE_BIASED_REPRESENTATION_P (gnu_type
)
1785 = Has_Biased_Representation (gnat_entity
);
1787 /* Inherit our alias set from what we're a subtype of. Subtypes
1788 are not different types and a pointer can designate any instance
1789 within a subtype hierarchy. */
1790 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
1792 /* One of the above calls might have caused us to be elaborated,
1793 so don't blow up if so. */
1794 if (present_gnu_tree (gnat_entity
))
1796 maybe_present
= true;
1800 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1801 TYPE_STUB_DECL (gnu_type
)
1802 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
1804 /* For a packed array, make the original array type a parallel type. */
1806 && Is_Packed_Array_Type (gnat_entity
)
1807 && present_gnu_tree (Original_Array_Type (gnat_entity
)))
1808 add_parallel_type (gnu_type
,
1810 (Original_Array_Type (gnat_entity
)));
1814 /* We have to handle clauses that under-align the type specially. */
1815 if ((Present (Alignment_Clause (gnat_entity
))
1816 || (Is_Packed_Array_Type (gnat_entity
)
1818 (Alignment_Clause (Original_Array_Type (gnat_entity
)))))
1819 && UI_Is_In_Int_Range (Alignment (gnat_entity
)))
1821 align
= UI_To_Int (Alignment (gnat_entity
)) * BITS_PER_UNIT
;
1822 if (align
>= TYPE_ALIGN (gnu_type
))
1826 /* If the type we are dealing with represents a bit-packed array,
1827 we need to have the bits left justified on big-endian targets
1828 and right justified on little-endian targets. We also need to
1829 ensure that when the value is read (e.g. for comparison of two
1830 such values), we only get the good bits, since the unused bits
1831 are uninitialized. Both goals are accomplished by wrapping up
1832 the modular type in an enclosing record type. */
1833 if (Is_Packed_Array_Type (gnat_entity
)
1834 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1836 tree gnu_field_type
, gnu_field
;
1838 /* Set the RM size before wrapping up the original type. */
1839 SET_TYPE_RM_SIZE (gnu_type
,
1840 UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
));
1841 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
) = 1;
1843 /* Create a stripped-down declaration, mainly for debugging. */
1844 create_type_decl (gnu_entity_name
, gnu_type
, true, debug_info_p
,
1847 /* Now save it and build the enclosing record type. */
1848 gnu_field_type
= gnu_type
;
1850 gnu_type
= make_node (RECORD_TYPE
);
1851 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "JM");
1852 TYPE_PACKED (gnu_type
) = 1;
1853 TYPE_SIZE (gnu_type
) = TYPE_SIZE (gnu_field_type
);
1854 TYPE_SIZE_UNIT (gnu_type
) = TYPE_SIZE_UNIT (gnu_field_type
);
1855 SET_TYPE_ADA_SIZE (gnu_type
, TYPE_RM_SIZE (gnu_field_type
));
1857 /* Propagate the alignment of the modular type to the record type,
1858 unless there is an alignment clause that under-aligns the type.
1859 This means that bit-packed arrays are given "ceil" alignment for
1860 their size by default, which may seem counter-intuitive but makes
1861 it possible to overlay them on modular types easily. */
1862 TYPE_ALIGN (gnu_type
)
1863 = align
> 0 ? align
: TYPE_ALIGN (gnu_field_type
);
1865 relate_alias_sets (gnu_type
, gnu_field_type
, ALIAS_SET_COPY
);
1867 /* Don't declare the field as addressable since we won't be taking
1868 its address and this would prevent create_field_decl from making
1871 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type
,
1872 gnu_type
, NULL_TREE
, bitsize_zero_node
, 1, 0);
1874 /* Do not emit debug info until after the parallel type is added. */
1875 finish_record_type (gnu_type
, gnu_field
, 2, false);
1876 compute_record_mode (gnu_type
);
1877 TYPE_JUSTIFIED_MODULAR_P (gnu_type
) = 1;
1881 /* Make the original array type a parallel type. */
1882 if (present_gnu_tree (Original_Array_Type (gnat_entity
)))
1883 add_parallel_type (gnu_type
,
1885 (Original_Array_Type (gnat_entity
)));
1887 rest_of_record_type_compilation (gnu_type
);
1891 /* If the type we are dealing with has got a smaller alignment than the
1892 natural one, we need to wrap it up in a record type and misalign the
1893 latter; we reuse the padding machinery for this purpose. Note that,
1894 even if the record type is marked as packed because of misalignment,
1895 we don't pack the field so as to give it the size of the type. */
1898 tree gnu_field_type
, gnu_field
;
1900 /* Set the RM size before wrapping up the type. */
1901 SET_TYPE_RM_SIZE (gnu_type
,
1902 UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
));
1904 /* Create a stripped-down declaration, mainly for debugging. */
1905 create_type_decl (gnu_entity_name
, gnu_type
, true, debug_info_p
,
1908 /* Now save it and build the enclosing record type. */
1909 gnu_field_type
= gnu_type
;
1911 gnu_type
= make_node (RECORD_TYPE
);
1912 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "PAD");
1913 TYPE_PACKED (gnu_type
) = 1;
1914 TYPE_SIZE (gnu_type
) = TYPE_SIZE (gnu_field_type
);
1915 TYPE_SIZE_UNIT (gnu_type
) = TYPE_SIZE_UNIT (gnu_field_type
);
1916 SET_TYPE_ADA_SIZE (gnu_type
, TYPE_RM_SIZE (gnu_field_type
));
1917 TYPE_ALIGN (gnu_type
) = align
;
1918 relate_alias_sets (gnu_type
, gnu_field_type
, ALIAS_SET_COPY
);
1920 /* Don't declare the field as addressable since we won't be taking
1921 its address and this would prevent create_field_decl from making
1924 = create_field_decl (get_identifier ("F"), gnu_field_type
,
1925 gnu_type
, TYPE_SIZE (gnu_field_type
),
1926 bitsize_zero_node
, 0, 0);
1928 finish_record_type (gnu_type
, gnu_field
, 2, debug_info_p
);
1929 compute_record_mode (gnu_type
);
1930 TYPE_PADDING_P (gnu_type
) = 1;
1935 case E_Floating_Point_Type
:
1936 /* If this is a VAX floating-point type, use an integer of the proper
1937 size. All the operations will be handled with ASM statements. */
1938 if (Vax_Float (gnat_entity
))
1940 gnu_type
= make_signed_type (esize
);
1941 TYPE_VAX_FLOATING_POINT_P (gnu_type
) = 1;
1942 SET_TYPE_DIGITS_VALUE (gnu_type
,
1943 UI_To_gnu (Digits_Value (gnat_entity
),
1948 /* The type of the Low and High bounds can be our type if this is
1949 a type from Standard, so set them at the end of the function. */
1950 gnu_type
= make_node (REAL_TYPE
);
1951 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
1952 layout_type (gnu_type
);
1955 case E_Floating_Point_Subtype
:
1956 if (Vax_Float (gnat_entity
))
1958 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
1962 /* See the E_Signed_Integer_Subtype case for the rationale. */
1964 && Present (Ancestor_Subtype (gnat_entity
))
1965 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1966 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1967 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
1968 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
), gnu_expr
, 0);
1970 gnu_type
= make_node (REAL_TYPE
);
1971 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
1972 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
1973 TYPE_GCC_MIN_VALUE (gnu_type
)
1974 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type
));
1975 TYPE_GCC_MAX_VALUE (gnu_type
)
1976 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type
));
1977 layout_type (gnu_type
);
1979 SET_TYPE_RM_MIN_VALUE
1981 convert (TREE_TYPE (gnu_type
),
1982 elaborate_expression (Type_Low_Bound (gnat_entity
),
1983 gnat_entity
, get_identifier ("L"),
1985 Needs_Debug_Info (gnat_entity
))));
1987 SET_TYPE_RM_MAX_VALUE
1989 convert (TREE_TYPE (gnu_type
),
1990 elaborate_expression (Type_High_Bound (gnat_entity
),
1991 gnat_entity
, get_identifier ("U"),
1993 Needs_Debug_Info (gnat_entity
))));
1995 /* Inherit our alias set from what we're a subtype of, as for
1996 integer subtypes. */
1997 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
1999 /* One of the above calls might have caused us to be elaborated,
2000 so don't blow up if so. */
2001 maybe_present
= true;
2004 /* Array and String Types and Subtypes
2006 Unconstrained array types are represented by E_Array_Type and
2007 constrained array types are represented by E_Array_Subtype. There
2008 are no actual objects of an unconstrained array type; all we have
2009 are pointers to that type.
2011 The following fields are defined on array types and subtypes:
2013 Component_Type Component type of the array.
2014 Number_Dimensions Number of dimensions (an int).
2015 First_Index Type of first index. */
2020 const bool convention_fortran_p
2021 = (Convention (gnat_entity
) == Convention_Fortran
);
2022 const int ndim
= Number_Dimensions (gnat_entity
);
2023 tree gnu_template_type
;
2024 tree gnu_ptr_template
;
2025 tree gnu_template_reference
, gnu_template_fields
, gnu_fat_type
;
2026 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2027 tree
*gnu_temp_fields
= XALLOCAVEC (tree
, ndim
);
2028 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
, tem
, t
;
2029 Entity_Id gnat_index
, gnat_name
;
2033 /* Create the type for the component now, as it simplifies breaking
2034 type reference loops. */
2036 = gnat_to_gnu_component_type (gnat_entity
, definition
, debug_info_p
);
2037 if (present_gnu_tree (gnat_entity
))
2039 /* As a side effect, the type may have been translated. */
2040 maybe_present
= true;
2044 /* We complete an existing dummy fat pointer type in place. This both
2045 avoids further complex adjustments in update_pointer_to and yields
2046 better debugging information in DWARF by leveraging the support for
2047 incomplete declarations of "tagged" types in the DWARF back-end. */
2048 gnu_type
= get_dummy_type (gnat_entity
);
2049 if (gnu_type
&& TYPE_POINTER_TO (gnu_type
))
2051 gnu_fat_type
= TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type
));
2052 TYPE_NAME (gnu_fat_type
) = NULL_TREE
;
2053 /* Save the contents of the dummy type for update_pointer_to. */
2054 TYPE_POINTER_TO (gnu_type
) = copy_type (gnu_fat_type
);
2056 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type
)));
2057 gnu_template_type
= TREE_TYPE (gnu_ptr_template
);
2061 gnu_fat_type
= make_node (RECORD_TYPE
);
2062 gnu_template_type
= make_node (RECORD_TYPE
);
2063 gnu_ptr_template
= build_pointer_type (gnu_template_type
);
2066 /* Make a node for the array. If we are not defining the array
2067 suppress expanding incomplete types. */
2068 gnu_type
= make_node (UNCONSTRAINED_ARRAY_TYPE
);
2072 defer_incomplete_level
++;
2073 this_deferred
= true;
2076 /* Build the fat pointer type. Use a "void *" object instead of
2077 a pointer to the array type since we don't have the array type
2078 yet (it will reference the fat pointer via the bounds). */
2080 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node
,
2081 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2083 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template
,
2084 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2086 if (COMPLETE_TYPE_P (gnu_fat_type
))
2088 /* We are going to lay it out again so reset the alias set. */
2089 alias_set_type alias_set
= TYPE_ALIAS_SET (gnu_fat_type
);
2090 TYPE_ALIAS_SET (gnu_fat_type
) = -1;
2091 finish_fat_pointer_type (gnu_fat_type
, tem
);
2092 TYPE_ALIAS_SET (gnu_fat_type
) = alias_set
;
2093 for (t
= gnu_fat_type
; t
; t
= TYPE_NEXT_VARIANT (t
))
2095 TYPE_FIELDS (t
) = tem
;
2096 SET_TYPE_UNCONSTRAINED_ARRAY (t
, gnu_type
);
2101 finish_fat_pointer_type (gnu_fat_type
, tem
);
2102 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type
, gnu_type
);
2105 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2106 is the fat pointer. This will be used to access the individual
2107 fields once we build them. */
2108 tem
= build3 (COMPONENT_REF
, gnu_ptr_template
,
2109 build0 (PLACEHOLDER_EXPR
, gnu_fat_type
),
2110 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type
)), NULL_TREE
);
2111 gnu_template_reference
2112 = build_unary_op (INDIRECT_REF
, gnu_template_type
, tem
);
2113 TREE_READONLY (gnu_template_reference
) = 1;
2114 TREE_THIS_NOTRAP (gnu_template_reference
) = 1;
2116 /* Now create the GCC type for each index and add the fields for that
2117 index to the template. */
2118 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2119 gnat_index
= First_Index (gnat_entity
);
2120 0 <= index
&& index
< ndim
;
2121 index
+= (convention_fortran_p
? - 1 : 1),
2122 gnat_index
= Next_Index (gnat_index
))
2124 char field_name
[16];
2125 tree gnu_index_base_type
2126 = get_unpadded_type (Base_Type (Etype (gnat_index
)));
2127 tree gnu_lb_field
, gnu_hb_field
, gnu_orig_min
, gnu_orig_max
;
2128 tree gnu_min
, gnu_max
, gnu_high
;
2130 /* Make the FIELD_DECLs for the low and high bounds of this
2131 type and then make extractions of these fields from the
2133 sprintf (field_name
, "LB%d", index
);
2134 gnu_lb_field
= create_field_decl (get_identifier (field_name
),
2135 gnu_index_base_type
,
2136 gnu_template_type
, NULL_TREE
,
2138 Sloc_to_locus (Sloc (gnat_entity
),
2139 &DECL_SOURCE_LOCATION (gnu_lb_field
));
2141 field_name
[0] = 'U';
2142 gnu_hb_field
= create_field_decl (get_identifier (field_name
),
2143 gnu_index_base_type
,
2144 gnu_template_type
, NULL_TREE
,
2146 Sloc_to_locus (Sloc (gnat_entity
),
2147 &DECL_SOURCE_LOCATION (gnu_hb_field
));
2149 gnu_temp_fields
[index
] = chainon (gnu_lb_field
, gnu_hb_field
);
2151 /* We can't use build_component_ref here since the template type
2152 isn't complete yet. */
2153 gnu_orig_min
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2154 gnu_template_reference
, gnu_lb_field
,
2156 gnu_orig_max
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2157 gnu_template_reference
, gnu_hb_field
,
2159 TREE_READONLY (gnu_orig_min
) = TREE_READONLY (gnu_orig_max
) = 1;
2161 gnu_min
= convert (sizetype
, gnu_orig_min
);
2162 gnu_max
= convert (sizetype
, gnu_orig_max
);
2164 /* Compute the size of this dimension. See the E_Array_Subtype
2165 case below for the rationale. */
2167 = build3 (COND_EXPR
, sizetype
,
2168 build2 (GE_EXPR
, boolean_type_node
,
2169 gnu_orig_max
, gnu_orig_min
),
2171 size_binop (MINUS_EXPR
, gnu_min
, size_one_node
));
2173 /* Make a range type with the new range in the Ada base type.
2174 Then make an index type with the size range in sizetype. */
2175 gnu_index_types
[index
]
2176 = create_index_type (gnu_min
, gnu_high
,
2177 create_range_type (gnu_index_base_type
,
2182 /* Update the maximum size of the array in elements. */
2185 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2187 = convert (sizetype
, TYPE_MIN_VALUE (gnu_index_type
));
2189 = convert (sizetype
, TYPE_MAX_VALUE (gnu_index_type
));
2191 = size_binop (MAX_EXPR
,
2192 size_binop (PLUS_EXPR
, size_one_node
,
2193 size_binop (MINUS_EXPR
,
2197 if (TREE_CODE (gnu_this_max
) == INTEGER_CST
2198 && TREE_OVERFLOW (gnu_this_max
))
2199 gnu_max_size
= NULL_TREE
;
2202 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2205 TYPE_NAME (gnu_index_types
[index
])
2206 = create_concat_name (gnat_entity
, field_name
);
2209 /* Install all the fields into the template. */
2210 TYPE_NAME (gnu_template_type
)
2211 = create_concat_name (gnat_entity
, "XUB");
2212 gnu_template_fields
= NULL_TREE
;
2213 for (index
= 0; index
< ndim
; index
++)
2215 = chainon (gnu_template_fields
, gnu_temp_fields
[index
]);
2216 finish_record_type (gnu_template_type
, gnu_template_fields
, 0,
2218 TYPE_READONLY (gnu_template_type
) = 1;
2220 /* If Component_Size is not already specified, annotate it with the
2221 size of the component. */
2222 if (Unknown_Component_Size (gnat_entity
))
2223 Set_Component_Size (gnat_entity
,
2224 annotate_value (TYPE_SIZE (comp_type
)));
2226 /* Compute the maximum size of the array in units and bits. */
2229 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2230 TYPE_SIZE_UNIT (comp_type
));
2231 gnu_max_size
= size_binop (MULT_EXPR
,
2232 convert (bitsizetype
, gnu_max_size
),
2233 TYPE_SIZE (comp_type
));
2236 gnu_max_size_unit
= NULL_TREE
;
2238 /* Now build the array type. */
2240 for (index
= ndim
- 1; index
>= 0; index
--)
2242 tem
= build_nonshared_array_type (tem
, gnu_index_types
[index
]);
2243 if (Reverse_Storage_Order (gnat_entity
))
2244 sorry ("non-default Scalar_Storage_Order");
2245 TYPE_MULTI_ARRAY_P (tem
) = (index
> 0);
2246 if (array_type_has_nonaliased_component (tem
, gnat_entity
))
2247 TYPE_NONALIASED_COMPONENT (tem
) = 1;
2249 /* If it is passed by reference, force BLKmode to ensure that
2250 objects of this type will always be put in memory. */
2251 if (TYPE_MODE (tem
) != BLKmode
2252 && Is_By_Reference_Type (gnat_entity
))
2253 SET_TYPE_MODE (tem
, BLKmode
);
2256 /* If an alignment is specified, use it if valid. But ignore it
2257 for the original type of packed array types. If the alignment
2258 was requested with an explicit alignment clause, state so. */
2259 if (No (Packed_Array_Type (gnat_entity
))
2260 && Known_Alignment (gnat_entity
))
2263 = validate_alignment (Alignment (gnat_entity
), gnat_entity
,
2265 if (Present (Alignment_Clause (gnat_entity
)))
2266 TYPE_USER_ALIGN (tem
) = 1;
2269 TYPE_CONVENTION_FORTRAN_P (tem
) = convention_fortran_p
;
2271 /* Adjust the type of the pointer-to-array field of the fat pointer
2272 and record the aliasing relationships if necessary. */
2273 TREE_TYPE (TYPE_FIELDS (gnu_fat_type
)) = build_pointer_type (tem
);
2274 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type
))
2275 record_component_aliases (gnu_fat_type
);
2277 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2278 corresponding fat pointer. */
2279 TREE_TYPE (gnu_type
) = gnu_fat_type
;
2280 TYPE_POINTER_TO (gnu_type
) = gnu_fat_type
;
2281 TYPE_REFERENCE_TO (gnu_type
) = gnu_fat_type
;
2282 SET_TYPE_MODE (gnu_type
, BLKmode
);
2283 TYPE_ALIGN (gnu_type
) = TYPE_ALIGN (tem
);
2285 /* If the maximum size doesn't overflow, use it. */
2287 && TREE_CODE (gnu_max_size
) == INTEGER_CST
2288 && !TREE_OVERFLOW (gnu_max_size
)
2289 && TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2290 && !TREE_OVERFLOW (gnu_max_size_unit
))
2292 TYPE_SIZE (tem
) = size_binop (MIN_EXPR
, gnu_max_size
,
2294 TYPE_SIZE_UNIT (tem
) = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2295 TYPE_SIZE_UNIT (tem
));
2298 create_type_decl (create_concat_name (gnat_entity
, "XUA"), tem
,
2299 !Comes_From_Source (gnat_entity
), debug_info_p
,
2302 /* Give the fat pointer type a name. If this is a packed type, tell
2303 the debugger how to interpret the underlying bits. */
2304 if (Present (Packed_Array_Type (gnat_entity
)))
2305 gnat_name
= Packed_Array_Type (gnat_entity
);
2307 gnat_name
= gnat_entity
;
2308 create_type_decl (create_concat_name (gnat_name
, "XUP"), gnu_fat_type
,
2309 !Comes_From_Source (gnat_entity
), debug_info_p
,
2312 /* Create the type to be designated by thin pointers: a record type for
2313 the array and its template. We used to shift the fields to have the
2314 template at a negative offset, but this was somewhat of a kludge; we
2315 now shift thin pointer values explicitly but only those which have a
2316 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2317 tem
= build_unc_object_type (gnu_template_type
, tem
,
2318 create_concat_name (gnat_name
, "XUT"),
2321 SET_TYPE_UNCONSTRAINED_ARRAY (tem
, gnu_type
);
2322 TYPE_OBJECT_RECORD_TYPE (gnu_type
) = tem
;
2326 case E_String_Subtype
:
2327 case E_Array_Subtype
:
2329 /* This is the actual data type for array variables. Multidimensional
2330 arrays are implemented as arrays of arrays. Note that arrays which
2331 have sparse enumeration subtypes as index components create sparse
2332 arrays, which is obviously space inefficient but so much easier to
2335 Also note that the subtype never refers to the unconstrained array
2336 type, which is somewhat at variance with Ada semantics.
2338 First check to see if this is simply a renaming of the array type.
2339 If so, the result is the array type. */
2341 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
2342 if (!Is_Constrained (gnat_entity
))
2346 Entity_Id gnat_index
, gnat_base_index
;
2347 const bool convention_fortran_p
2348 = (Convention (gnat_entity
) == Convention_Fortran
);
2349 const int ndim
= Number_Dimensions (gnat_entity
);
2350 tree gnu_base_type
= gnu_type
;
2351 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2352 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
;
2353 bool need_index_type_struct
= false;
2356 /* First create the GCC type for each index and find out whether
2357 special types are needed for debugging information. */
2358 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2359 gnat_index
= First_Index (gnat_entity
),
2361 = First_Index (Implementation_Base_Type (gnat_entity
));
2362 0 <= index
&& index
< ndim
;
2363 index
+= (convention_fortran_p
? - 1 : 1),
2364 gnat_index
= Next_Index (gnat_index
),
2365 gnat_base_index
= Next_Index (gnat_base_index
))
2367 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2368 tree gnu_orig_min
= TYPE_MIN_VALUE (gnu_index_type
);
2369 tree gnu_orig_max
= TYPE_MAX_VALUE (gnu_index_type
);
2370 tree gnu_min
= convert (sizetype
, gnu_orig_min
);
2371 tree gnu_max
= convert (sizetype
, gnu_orig_max
);
2372 tree gnu_base_index_type
2373 = get_unpadded_type (Etype (gnat_base_index
));
2374 tree gnu_base_orig_min
= TYPE_MIN_VALUE (gnu_base_index_type
);
2375 tree gnu_base_orig_max
= TYPE_MAX_VALUE (gnu_base_index_type
);
2378 /* See if the base array type is already flat. If it is, we
2379 are probably compiling an ACATS test but it will cause the
2380 code below to malfunction if we don't handle it specially. */
2381 if (TREE_CODE (gnu_base_orig_min
) == INTEGER_CST
2382 && TREE_CODE (gnu_base_orig_max
) == INTEGER_CST
2383 && tree_int_cst_lt (gnu_base_orig_max
, gnu_base_orig_min
))
2385 gnu_min
= size_one_node
;
2386 gnu_max
= size_zero_node
;
2390 /* Similarly, if one of the values overflows in sizetype and the
2391 range is null, use 1..0 for the sizetype bounds. */
2392 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2393 && TREE_CODE (gnu_max
) == INTEGER_CST
2394 && (TREE_OVERFLOW (gnu_min
) || TREE_OVERFLOW (gnu_max
))
2395 && tree_int_cst_lt (gnu_orig_max
, gnu_orig_min
))
2397 gnu_min
= size_one_node
;
2398 gnu_max
= size_zero_node
;
2402 /* If the minimum and maximum values both overflow in sizetype,
2403 but the difference in the original type does not overflow in
2404 sizetype, ignore the overflow indication. */
2405 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2406 && TREE_CODE (gnu_max
) == INTEGER_CST
2407 && TREE_OVERFLOW (gnu_min
) && TREE_OVERFLOW (gnu_max
)
2410 fold_build2 (MINUS_EXPR
, gnu_index_type
,
2414 TREE_OVERFLOW (gnu_min
) = 0;
2415 TREE_OVERFLOW (gnu_max
) = 0;
2419 /* Compute the size of this dimension in the general case. We
2420 need to provide GCC with an upper bound to use but have to
2421 deal with the "superflat" case. There are three ways to do
2422 this. If we can prove that the array can never be superflat,
2423 we can just use the high bound of the index type. */
2424 else if ((Nkind (gnat_index
) == N_Range
2425 && cannot_be_superflat_p (gnat_index
))
2426 /* Packed Array Types are never superflat. */
2427 || Is_Packed_Array_Type (gnat_entity
))
2430 /* Otherwise, if the high bound is constant but the low bound is
2431 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2432 lower bound. Note that the comparison must be done in the
2433 original type to avoid any overflow during the conversion. */
2434 else if (TREE_CODE (gnu_max
) == INTEGER_CST
2435 && TREE_CODE (gnu_min
) != INTEGER_CST
)
2439 = build_cond_expr (sizetype
,
2440 build_binary_op (GE_EXPR
,
2445 int_const_binop (PLUS_EXPR
, gnu_max
,
2449 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2450 in all the other cases. Note that, here as well as above,
2451 the condition used in the comparison must be equivalent to
2452 the condition (length != 0). This is relied upon in order
2453 to optimize array comparisons in compare_arrays. Moreover
2454 we use int_const_binop for the shift by 1 if the bound is
2455 constant to avoid any unwanted overflow. */
2458 = build_cond_expr (sizetype
,
2459 build_binary_op (GE_EXPR
,
2464 TREE_CODE (gnu_min
) == INTEGER_CST
2465 ? int_const_binop (MINUS_EXPR
, gnu_min
,
2467 : size_binop (MINUS_EXPR
, gnu_min
,
2470 /* Reuse the index type for the range type. Then make an index
2471 type with the size range in sizetype. */
2472 gnu_index_types
[index
]
2473 = create_index_type (gnu_min
, gnu_high
, gnu_index_type
,
2476 /* Update the maximum size of the array in elements. Here we
2477 see if any constraint on the index type of the base type
2478 can be used in the case of self-referential bound on the
2479 index type of the subtype. We look for a non-"infinite"
2480 and non-self-referential bound from any type involved and
2481 handle each bound separately. */
2484 tree gnu_base_min
= convert (sizetype
, gnu_base_orig_min
);
2485 tree gnu_base_max
= convert (sizetype
, gnu_base_orig_max
);
2486 tree gnu_base_index_base_type
2487 = get_base_type (gnu_base_index_type
);
2488 tree gnu_base_base_min
2489 = convert (sizetype
,
2490 TYPE_MIN_VALUE (gnu_base_index_base_type
));
2491 tree gnu_base_base_max
2492 = convert (sizetype
,
2493 TYPE_MAX_VALUE (gnu_base_index_base_type
));
2495 if (!CONTAINS_PLACEHOLDER_P (gnu_min
)
2496 || !(TREE_CODE (gnu_base_min
) == INTEGER_CST
2497 && !TREE_OVERFLOW (gnu_base_min
)))
2498 gnu_base_min
= gnu_min
;
2500 if (!CONTAINS_PLACEHOLDER_P (gnu_max
)
2501 || !(TREE_CODE (gnu_base_max
) == INTEGER_CST
2502 && !TREE_OVERFLOW (gnu_base_max
)))
2503 gnu_base_max
= gnu_max
;
2505 if ((TREE_CODE (gnu_base_min
) == INTEGER_CST
2506 && TREE_OVERFLOW (gnu_base_min
))
2507 || operand_equal_p (gnu_base_min
, gnu_base_base_min
, 0)
2508 || (TREE_CODE (gnu_base_max
) == INTEGER_CST
2509 && TREE_OVERFLOW (gnu_base_max
))
2510 || operand_equal_p (gnu_base_max
, gnu_base_base_max
, 0))
2511 gnu_max_size
= NULL_TREE
;
2515 = size_binop (MAX_EXPR
,
2516 size_binop (PLUS_EXPR
, size_one_node
,
2517 size_binop (MINUS_EXPR
,
2522 if (TREE_CODE (gnu_this_max
) == INTEGER_CST
2523 && TREE_OVERFLOW (gnu_this_max
))
2524 gnu_max_size
= NULL_TREE
;
2527 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2531 /* We need special types for debugging information to point to
2532 the index types if they have variable bounds, are not integer
2533 types, are biased or are wider than sizetype. */
2534 if (!integer_onep (gnu_orig_min
)
2535 || TREE_CODE (gnu_orig_max
) != INTEGER_CST
2536 || TREE_CODE (gnu_index_type
) != INTEGER_TYPE
2537 || (TREE_TYPE (gnu_index_type
)
2538 && TREE_CODE (TREE_TYPE (gnu_index_type
))
2540 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type
)
2541 || compare_tree_int (rm_size (gnu_index_type
),
2542 TYPE_PRECISION (sizetype
)) > 0)
2543 need_index_type_struct
= true;
2546 /* Then flatten: create the array of arrays. For an array type
2547 used to implement a packed array, get the component type from
2548 the original array type since the representation clauses that
2549 can affect it are on the latter. */
2550 if (Is_Packed_Array_Type (gnat_entity
)
2551 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
2553 gnu_type
= gnat_to_gnu_type (Original_Array_Type (gnat_entity
));
2554 for (index
= ndim
- 1; index
>= 0; index
--)
2555 gnu_type
= TREE_TYPE (gnu_type
);
2557 /* One of the above calls might have caused us to be elaborated,
2558 so don't blow up if so. */
2559 if (present_gnu_tree (gnat_entity
))
2561 maybe_present
= true;
2567 gnu_type
= gnat_to_gnu_component_type (gnat_entity
, definition
,
2570 /* One of the above calls might have caused us to be elaborated,
2571 so don't blow up if so. */
2572 if (present_gnu_tree (gnat_entity
))
2574 maybe_present
= true;
2579 /* Compute the maximum size of the array in units and bits. */
2582 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2583 TYPE_SIZE_UNIT (gnu_type
));
2584 gnu_max_size
= size_binop (MULT_EXPR
,
2585 convert (bitsizetype
, gnu_max_size
),
2586 TYPE_SIZE (gnu_type
));
2589 gnu_max_size_unit
= NULL_TREE
;
2591 /* Now build the array type. */
2592 for (index
= ndim
- 1; index
>= 0; index
--)
2594 gnu_type
= build_nonshared_array_type (gnu_type
,
2595 gnu_index_types
[index
]);
2596 TYPE_MULTI_ARRAY_P (gnu_type
) = (index
> 0);
2597 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2598 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2600 /* See the E_Array_Type case for the rationale. */
2601 if (TYPE_MODE (gnu_type
) != BLKmode
2602 && Is_By_Reference_Type (gnat_entity
))
2603 SET_TYPE_MODE (gnu_type
, BLKmode
);
2606 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2607 TYPE_STUB_DECL (gnu_type
)
2608 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
2610 /* If we are at file level and this is a multi-dimensional array,
2611 we need to make a variable corresponding to the stride of the
2612 inner dimensions. */
2613 if (global_bindings_p () && ndim
> 1)
2615 tree gnu_st_name
= get_identifier ("ST");
2618 for (gnu_arr_type
= TREE_TYPE (gnu_type
);
2619 TREE_CODE (gnu_arr_type
) == ARRAY_TYPE
;
2620 gnu_arr_type
= TREE_TYPE (gnu_arr_type
),
2621 gnu_st_name
= concat_name (gnu_st_name
, "ST"))
2623 tree eltype
= TREE_TYPE (gnu_arr_type
);
2625 TYPE_SIZE (gnu_arr_type
)
2626 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type
),
2627 gnat_entity
, gnu_st_name
,
2630 /* ??? For now, store the size as a multiple of the
2631 alignment of the element type in bytes so that we
2632 can see the alignment from the tree. */
2633 TYPE_SIZE_UNIT (gnu_arr_type
)
2634 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type
),
2636 concat_name (gnu_st_name
, "A_U"),
2638 TYPE_ALIGN (eltype
));
2640 /* ??? create_type_decl is not invoked on the inner types so
2641 the MULT_EXPR node built above will never be marked. */
2642 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type
));
2646 /* If we need to write out a record type giving the names of the
2647 bounds for debugging purposes, do it now and make the record
2648 type a parallel type. This is not needed for a packed array
2649 since the bounds are conveyed by the original array type. */
2650 if (need_index_type_struct
2652 && !Is_Packed_Array_Type (gnat_entity
))
2654 tree gnu_bound_rec
= make_node (RECORD_TYPE
);
2655 tree gnu_field_list
= NULL_TREE
;
2658 TYPE_NAME (gnu_bound_rec
)
2659 = create_concat_name (gnat_entity
, "XA");
2661 for (index
= ndim
- 1; index
>= 0; index
--)
2663 tree gnu_index
= TYPE_INDEX_TYPE (gnu_index_types
[index
]);
2664 tree gnu_index_name
= TYPE_NAME (gnu_index
);
2666 if (TREE_CODE (gnu_index_name
) == TYPE_DECL
)
2667 gnu_index_name
= DECL_NAME (gnu_index_name
);
2669 /* Make sure to reference the types themselves, and not just
2670 their names, as the debugger may fall back on them. */
2671 gnu_field
= create_field_decl (gnu_index_name
, gnu_index
,
2672 gnu_bound_rec
, NULL_TREE
,
2674 DECL_CHAIN (gnu_field
) = gnu_field_list
;
2675 gnu_field_list
= gnu_field
;
2678 finish_record_type (gnu_bound_rec
, gnu_field_list
, 0, true);
2679 add_parallel_type (gnu_type
, gnu_bound_rec
);
2682 /* If this is a packed array type, make the original array type a
2683 parallel type. Otherwise, do it for the base array type if it
2684 isn't artificial to make sure it is kept in the debug info. */
2687 if (Is_Packed_Array_Type (gnat_entity
)
2688 && present_gnu_tree (Original_Array_Type (gnat_entity
)))
2689 add_parallel_type (gnu_type
,
2691 (Original_Array_Type (gnat_entity
)));
2695 = gnat_to_gnu_entity (Etype (gnat_entity
), NULL_TREE
, 0);
2696 if (!DECL_ARTIFICIAL (gnu_base_decl
))
2697 add_parallel_type (gnu_type
,
2698 TREE_TYPE (TREE_TYPE (gnu_base_decl
)));
2702 TYPE_CONVENTION_FORTRAN_P (gnu_type
) = convention_fortran_p
;
2703 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
)
2704 = (Is_Packed_Array_Type (gnat_entity
)
2705 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)));
2707 /* If the size is self-referential and the maximum size doesn't
2708 overflow, use it. */
2709 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
2711 && !(TREE_CODE (gnu_max_size
) == INTEGER_CST
2712 && TREE_OVERFLOW (gnu_max_size
))
2713 && !(TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2714 && TREE_OVERFLOW (gnu_max_size_unit
)))
2716 TYPE_SIZE (gnu_type
) = size_binop (MIN_EXPR
, gnu_max_size
,
2717 TYPE_SIZE (gnu_type
));
2718 TYPE_SIZE_UNIT (gnu_type
)
2719 = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2720 TYPE_SIZE_UNIT (gnu_type
));
2723 /* Set our alias set to that of our base type. This gives all
2724 array subtypes the same alias set. */
2725 relate_alias_sets (gnu_type
, gnu_base_type
, ALIAS_SET_COPY
);
2727 /* If this is a packed type, make this type the same as the packed
2728 array type, but do some adjusting in the type first. */
2729 if (Present (Packed_Array_Type (gnat_entity
)))
2731 Entity_Id gnat_index
;
2734 /* First finish the type we had been making so that we output
2735 debugging information for it. */
2736 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
2737 if (Treat_As_Volatile (gnat_entity
))
2739 = build_qualified_type (gnu_type
,
2740 TYPE_QUALS (gnu_type
)
2741 | TYPE_QUAL_VOLATILE
);
2742 /* Make it artificial only if the base type was artificial too.
2743 That's sort of "morally" true and will make it possible for
2744 the debugger to look it up by name in DWARF, which is needed
2745 in order to decode the packed array type. */
2747 = create_type_decl (gnu_entity_name
, gnu_type
,
2748 !Comes_From_Source (Etype (gnat_entity
))
2749 && !Comes_From_Source (gnat_entity
),
2750 debug_info_p
, gnat_entity
);
2752 /* Save it as our equivalent in case the call below elaborates
2754 save_gnu_tree (gnat_entity
, gnu_decl
, false);
2756 gnu_decl
= gnat_to_gnu_entity (Packed_Array_Type (gnat_entity
),
2758 this_made_decl
= true;
2759 gnu_type
= TREE_TYPE (gnu_decl
);
2760 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
2762 gnu_inner
= gnu_type
;
2763 while (TREE_CODE (gnu_inner
) == RECORD_TYPE
2764 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner
)
2765 || TYPE_PADDING_P (gnu_inner
)))
2766 gnu_inner
= TREE_TYPE (TYPE_FIELDS (gnu_inner
));
2768 /* We need to attach the index type to the type we just made so
2769 that the actual bounds can later be put into a template. */
2770 if ((TREE_CODE (gnu_inner
) == ARRAY_TYPE
2771 && !TYPE_ACTUAL_BOUNDS (gnu_inner
))
2772 || (TREE_CODE (gnu_inner
) == INTEGER_TYPE
2773 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
)))
2775 if (TREE_CODE (gnu_inner
) == INTEGER_TYPE
)
2777 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2778 TYPE_MODULUS for modular types so we make an extra
2779 subtype if necessary. */
2780 if (TYPE_MODULAR_P (gnu_inner
))
2783 = make_unsigned_type (TYPE_PRECISION (gnu_inner
));
2784 TREE_TYPE (gnu_subtype
) = gnu_inner
;
2785 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
2786 SET_TYPE_RM_MIN_VALUE (gnu_subtype
,
2787 TYPE_MIN_VALUE (gnu_inner
));
2788 SET_TYPE_RM_MAX_VALUE (gnu_subtype
,
2789 TYPE_MAX_VALUE (gnu_inner
));
2790 gnu_inner
= gnu_subtype
;
2793 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
) = 1;
2795 #ifdef ENABLE_CHECKING
2796 /* Check for other cases of overloading. */
2797 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner
));
2801 for (gnat_index
= First_Index (gnat_entity
);
2802 Present (gnat_index
);
2803 gnat_index
= Next_Index (gnat_index
))
2804 SET_TYPE_ACTUAL_BOUNDS
2806 tree_cons (NULL_TREE
,
2807 get_unpadded_type (Etype (gnat_index
)),
2808 TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2810 if (Convention (gnat_entity
) != Convention_Fortran
)
2811 SET_TYPE_ACTUAL_BOUNDS
2812 (gnu_inner
, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2814 if (TREE_CODE (gnu_type
) == RECORD_TYPE
2815 && TYPE_JUSTIFIED_MODULAR_P (gnu_type
))
2816 TREE_TYPE (TYPE_FIELDS (gnu_type
)) = gnu_inner
;
2821 /* Abort if packed array with no Packed_Array_Type field set. */
2822 gcc_assert (!Is_Packed (gnat_entity
));
2826 case E_String_Literal_Subtype
:
2827 /* Create the type for a string literal. */
2829 Entity_Id gnat_full_type
2830 = (IN (Ekind (Etype (gnat_entity
)), Private_Kind
)
2831 && Present (Full_View (Etype (gnat_entity
)))
2832 ? Full_View (Etype (gnat_entity
)) : Etype (gnat_entity
));
2833 tree gnu_string_type
= get_unpadded_type (gnat_full_type
);
2834 tree gnu_string_array_type
2835 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type
))));
2836 tree gnu_string_index_type
2837 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2838 (TYPE_DOMAIN (gnu_string_array_type
))));
2839 tree gnu_lower_bound
2840 = convert (gnu_string_index_type
,
2841 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity
)));
2843 = UI_To_gnu (String_Literal_Length (gnat_entity
),
2844 gnu_string_index_type
);
2845 tree gnu_upper_bound
2846 = build_binary_op (PLUS_EXPR
, gnu_string_index_type
,
2848 int_const_binop (MINUS_EXPR
, gnu_length
,
2851 = create_index_type (convert (sizetype
, gnu_lower_bound
),
2852 convert (sizetype
, gnu_upper_bound
),
2853 create_range_type (gnu_string_index_type
,
2859 = build_nonshared_array_type (gnat_to_gnu_type
2860 (Component_Type (gnat_entity
)),
2862 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2863 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2864 relate_alias_sets (gnu_type
, gnu_string_type
, ALIAS_SET_COPY
);
2868 /* Record Types and Subtypes
2870 The following fields are defined on record types:
2872 Has_Discriminants True if the record has discriminants
2873 First_Discriminant Points to head of list of discriminants
2874 First_Entity Points to head of list of fields
2875 Is_Tagged_Type True if the record is tagged
2877 Implementation of Ada records and discriminated records:
2879 A record type definition is transformed into the equivalent of a C
2880 struct definition. The fields that are the discriminants which are
2881 found in the Full_Type_Declaration node and the elements of the
2882 Component_List found in the Record_Type_Definition node. The
2883 Component_List can be a recursive structure since each Variant of
2884 the Variant_Part of the Component_List has a Component_List.
2886 Processing of a record type definition comprises starting the list of
2887 field declarations here from the discriminants and the calling the
2888 function components_to_record to add the rest of the fields from the
2889 component list and return the gnu type node. The function
2890 components_to_record will call itself recursively as it traverses
2894 if (Has_Complex_Representation (gnat_entity
))
2897 = build_complex_type
2899 (Etype (Defining_Entity
2900 (First (Component_Items
2903 (Declaration_Node (gnat_entity
)))))))));
2909 Node_Id full_definition
= Declaration_Node (gnat_entity
);
2910 Node_Id record_definition
= Type_Definition (full_definition
);
2911 Node_Id gnat_constr
;
2912 Entity_Id gnat_field
;
2913 tree gnu_field
, gnu_field_list
= NULL_TREE
;
2914 tree gnu_get_parent
;
2915 /* Set PACKED in keeping with gnat_to_gnu_field. */
2917 = Is_Packed (gnat_entity
)
2919 : Component_Alignment (gnat_entity
) == Calign_Storage_Unit
2921 : (Known_Alignment (gnat_entity
)
2922 || (Strict_Alignment (gnat_entity
)
2923 && Known_RM_Size (gnat_entity
)))
2926 const bool has_discr
= Has_Discriminants (gnat_entity
);
2927 const bool has_rep
= Has_Specified_Layout (gnat_entity
);
2928 const bool is_extension
2929 = (Is_Tagged_Type (gnat_entity
)
2930 && Nkind (record_definition
) == N_Derived_Type_Definition
);
2931 const bool is_unchecked_union
= Is_Unchecked_Union (gnat_entity
);
2932 bool all_rep
= has_rep
;
2934 /* See if all fields have a rep clause. Stop when we find one
2937 for (gnat_field
= First_Entity (gnat_entity
);
2938 Present (gnat_field
);
2939 gnat_field
= Next_Entity (gnat_field
))
2940 if ((Ekind (gnat_field
) == E_Component
2941 || Ekind (gnat_field
) == E_Discriminant
)
2942 && No (Component_Clause (gnat_field
)))
2948 /* If this is a record extension, go a level further to find the
2949 record definition. Also, verify we have a Parent_Subtype. */
2952 if (!type_annotate_only
2953 || Present (Record_Extension_Part (record_definition
)))
2954 record_definition
= Record_Extension_Part (record_definition
);
2956 gcc_assert (type_annotate_only
2957 || Present (Parent_Subtype (gnat_entity
)));
2960 /* Make a node for the record. If we are not defining the record,
2961 suppress expanding incomplete types. */
2962 gnu_type
= make_node (tree_code_for_record_type (gnat_entity
));
2963 TYPE_NAME (gnu_type
) = gnu_entity_name
;
2964 TYPE_PACKED (gnu_type
) = (packed
!= 0) || has_rep
;
2965 if (Reverse_Storage_Order (gnat_entity
))
2966 sorry ("non-default Scalar_Storage_Order");
2967 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
2971 defer_incomplete_level
++;
2972 this_deferred
= true;
2975 /* If both a size and rep clause was specified, put the size in
2976 the record type now so that it can get the proper mode. */
2977 if (has_rep
&& Known_RM_Size (gnat_entity
))
2978 TYPE_SIZE (gnu_type
)
2979 = UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
);
2981 /* Always set the alignment here so that it can be used to
2982 set the mode, if it is making the alignment stricter. If
2983 it is invalid, it will be checked again below. If this is to
2984 be Atomic, choose a default alignment of a word unless we know
2985 the size and it's smaller. */
2986 if (Known_Alignment (gnat_entity
))
2987 TYPE_ALIGN (gnu_type
)
2988 = validate_alignment (Alignment (gnat_entity
), gnat_entity
, 0);
2989 else if (Is_Atomic (gnat_entity
) && Known_Esize (gnat_entity
))
2991 unsigned int size
= UI_To_Int (Esize (gnat_entity
));
2992 TYPE_ALIGN (gnu_type
)
2993 = size
>= BITS_PER_WORD
? BITS_PER_WORD
: ceil_pow2 (size
);
2995 /* If a type needs strict alignment, the minimum size will be the
2996 type size instead of the RM size (see validate_size). Cap the
2997 alignment, lest it causes this type size to become too large. */
2998 else if (Strict_Alignment (gnat_entity
) && Known_RM_Size (gnat_entity
))
3000 unsigned int raw_size
= UI_To_Int (RM_Size (gnat_entity
));
3001 unsigned int raw_align
= raw_size
& -raw_size
;
3002 if (raw_align
< BIGGEST_ALIGNMENT
)
3003 TYPE_ALIGN (gnu_type
) = raw_align
;
3006 TYPE_ALIGN (gnu_type
) = 0;
3008 /* If we have a Parent_Subtype, make a field for the parent. If
3009 this record has rep clauses, force the position to zero. */
3010 if (Present (Parent_Subtype (gnat_entity
)))
3012 Entity_Id gnat_parent
= Parent_Subtype (gnat_entity
);
3013 tree gnu_dummy_parent_type
= make_node (RECORD_TYPE
);
3016 /* A major complexity here is that the parent subtype will
3017 reference our discriminants in its Stored_Constraint list.
3018 But those must reference the parent component of this record
3019 which is precisely of the parent subtype we have not built yet!
3020 To break the circle we first build a dummy COMPONENT_REF which
3021 represents the "get to the parent" operation and initialize
3022 each of those discriminants to a COMPONENT_REF of the above
3023 dummy parent referencing the corresponding discriminant of the
3024 base type of the parent subtype. */
3025 gnu_get_parent
= build3 (COMPONENT_REF
, gnu_dummy_parent_type
,
3026 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3027 build_decl (input_location
,
3028 FIELD_DECL
, NULL_TREE
,
3029 gnu_dummy_parent_type
),
3033 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3034 Present (gnat_field
);
3035 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3036 if (Present (Corresponding_Discriminant (gnat_field
)))
3039 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3043 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3044 gnu_get_parent
, gnu_field
, NULL_TREE
),
3048 /* Then we build the parent subtype. If it has discriminants but
3049 the type itself has unknown discriminants, this means that it
3050 doesn't contain information about how the discriminants are
3051 derived from those of the ancestor type, so it cannot be used
3052 directly. Instead it is built by cloning the parent subtype
3053 of the underlying record view of the type, for which the above
3054 derivation of discriminants has been made explicit. */
3055 if (Has_Discriminants (gnat_parent
)
3056 && Has_Unknown_Discriminants (gnat_entity
))
3058 Entity_Id gnat_uview
= Underlying_Record_View (gnat_entity
);
3060 /* If we are defining the type, the underlying record
3061 view must already have been elaborated at this point.
3062 Otherwise do it now as its parent subtype cannot be
3063 technically elaborated on its own. */
3065 gcc_assert (present_gnu_tree (gnat_uview
));
3067 gnat_to_gnu_entity (gnat_uview
, NULL_TREE
, 0);
3069 gnu_parent
= gnat_to_gnu_type (Parent_Subtype (gnat_uview
));
3071 /* Substitute the "get to the parent" of the type for that
3072 of its underlying record view in the cloned type. */
3073 for (gnat_field
= First_Stored_Discriminant (gnat_uview
);
3074 Present (gnat_field
);
3075 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3076 if (Present (Corresponding_Discriminant (gnat_field
)))
3078 tree gnu_field
= gnat_to_gnu_field_decl (gnat_field
);
3080 = build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3081 gnu_get_parent
, gnu_field
, NULL_TREE
);
3083 = substitute_in_type (gnu_parent
, gnu_field
, gnu_ref
);
3087 gnu_parent
= gnat_to_gnu_type (gnat_parent
);
3089 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3090 initially built. The discriminants must reference the fields
3091 of the parent subtype and not those of its base type for the
3092 placeholder machinery to properly work. */
3095 /* The actual parent subtype is the full view. */
3096 if (IN (Ekind (gnat_parent
), Private_Kind
))
3098 if (Present (Full_View (gnat_parent
)))
3099 gnat_parent
= Full_View (gnat_parent
);
3101 gnat_parent
= Underlying_Full_View (gnat_parent
);
3104 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3105 Present (gnat_field
);
3106 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3107 if (Present (Corresponding_Discriminant (gnat_field
)))
3109 Entity_Id field
= Empty
;
3110 for (field
= First_Stored_Discriminant (gnat_parent
);
3112 field
= Next_Stored_Discriminant (field
))
3113 if (same_discriminant_p (gnat_field
, field
))
3115 gcc_assert (Present (field
));
3116 TREE_OPERAND (get_gnu_tree (gnat_field
), 1)
3117 = gnat_to_gnu_field_decl (field
);
3121 /* The "get to the parent" COMPONENT_REF must be given its
3123 TREE_TYPE (gnu_get_parent
) = gnu_parent
;
3125 /* ...and reference the _Parent field of this record. */
3127 = create_field_decl (parent_name_id
,
3128 gnu_parent
, gnu_type
,
3130 ? TYPE_SIZE (gnu_parent
) : NULL_TREE
,
3132 ? bitsize_zero_node
: NULL_TREE
,
3134 DECL_INTERNAL_P (gnu_field
) = 1;
3135 TREE_OPERAND (gnu_get_parent
, 1) = gnu_field
;
3136 TYPE_FIELDS (gnu_type
) = gnu_field
;
3139 /* Make the fields for the discriminants and put them into the record
3140 unless it's an Unchecked_Union. */
3142 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3143 Present (gnat_field
);
3144 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3146 /* If this is a record extension and this discriminant is the
3147 renaming of another discriminant, we've handled it above. */
3148 if (Present (Parent_Subtype (gnat_entity
))
3149 && Present (Corresponding_Discriminant (gnat_field
)))
3153 = gnat_to_gnu_field (gnat_field
, gnu_type
, packed
, definition
,
3156 /* Make an expression using a PLACEHOLDER_EXPR from the
3157 FIELD_DECL node just created and link that with the
3158 corresponding GNAT defining identifier. */
3159 save_gnu_tree (gnat_field
,
3160 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3161 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3162 gnu_field
, NULL_TREE
),
3165 if (!is_unchecked_union
)
3167 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3168 gnu_field_list
= gnu_field
;
3172 /* If we have a derived untagged type that renames discriminants in
3173 the root type, the (stored) discriminants are a just copy of the
3174 discriminants of the root type. This means that any constraints
3175 added by the renaming in the derivation are disregarded as far
3176 as the layout of the derived type is concerned. To rescue them,
3177 we change the type of the (stored) discriminants to a subtype
3178 with the bounds of the type of the visible discriminants. */
3181 && Stored_Constraint (gnat_entity
) != No_Elist
)
3182 for (gnat_constr
= First_Elmt (Stored_Constraint (gnat_entity
));
3183 gnat_constr
!= No_Elmt
;
3184 gnat_constr
= Next_Elmt (gnat_constr
))
3185 if (Nkind (Node (gnat_constr
)) == N_Identifier
3186 /* Ignore access discriminants. */
3187 && !Is_Access_Type (Etype (Node (gnat_constr
)))
3188 && Ekind (Entity (Node (gnat_constr
))) == E_Discriminant
)
3190 Entity_Id gnat_discr
= Entity (Node (gnat_constr
));
3191 tree gnu_discr_type
= gnat_to_gnu_type (Etype (gnat_discr
));
3193 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr
),
3196 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3197 just above for one of the stored discriminants. */
3198 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref
, 0)) == gnu_type
);
3200 if (gnu_discr_type
!= TREE_TYPE (gnu_ref
))
3202 const unsigned prec
= TYPE_PRECISION (TREE_TYPE (gnu_ref
));
3204 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref
))
3205 ? make_unsigned_type (prec
) : make_signed_type (prec
);
3206 TREE_TYPE (gnu_subtype
) = TREE_TYPE (gnu_ref
);
3207 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
3208 SET_TYPE_RM_MIN_VALUE (gnu_subtype
,
3209 TYPE_MIN_VALUE (gnu_discr_type
));
3210 SET_TYPE_RM_MAX_VALUE (gnu_subtype
,
3211 TYPE_MAX_VALUE (gnu_discr_type
));
3213 = TREE_TYPE (TREE_OPERAND (gnu_ref
, 1)) = gnu_subtype
;
3217 /* Add the fields into the record type and finish it up. */
3218 components_to_record (gnu_type
, Component_List (record_definition
),
3219 gnu_field_list
, packed
, definition
, false,
3220 all_rep
, is_unchecked_union
,
3221 !Comes_From_Source (gnat_entity
), debug_info_p
,
3222 false, OK_To_Reorder_Components (gnat_entity
),
3223 all_rep
? NULL_TREE
: bitsize_zero_node
, NULL
);
3225 /* If it is passed by reference, force BLKmode to ensure that objects
3226 of this type will always be put in memory. */
3227 if (TYPE_MODE (gnu_type
) != BLKmode
3228 && Is_By_Reference_Type (gnat_entity
))
3229 SET_TYPE_MODE (gnu_type
, BLKmode
);
3231 /* We used to remove the associations of the discriminants and _Parent
3232 for validity checking but we may need them if there's a Freeze_Node
3233 for a subtype used in this record. */
3234 TYPE_VOLATILE (gnu_type
) = Treat_As_Volatile (gnat_entity
);
3236 /* Fill in locations of fields. */
3237 annotate_rep (gnat_entity
, gnu_type
);
3239 /* If there are any entities in the chain corresponding to components
3240 that we did not elaborate, ensure we elaborate their types if they
3242 for (gnat_temp
= First_Entity (gnat_entity
);
3243 Present (gnat_temp
);
3244 gnat_temp
= Next_Entity (gnat_temp
))
3245 if ((Ekind (gnat_temp
) == E_Component
3246 || Ekind (gnat_temp
) == E_Discriminant
)
3247 && Is_Itype (Etype (gnat_temp
))
3248 && !present_gnu_tree (gnat_temp
))
3249 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
3251 /* If this is a record type associated with an exception definition,
3252 equate its fields to those of the standard exception type. This
3253 will make it possible to convert between them. */
3254 if (gnu_entity_name
== exception_data_name_id
)
3257 for (gnu_field
= TYPE_FIELDS (gnu_type
),
3258 gnu_std_field
= TYPE_FIELDS (except_type_node
);
3260 gnu_field
= DECL_CHAIN (gnu_field
),
3261 gnu_std_field
= DECL_CHAIN (gnu_std_field
))
3262 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field
, gnu_std_field
);
3263 gcc_assert (!gnu_std_field
);
3268 case E_Class_Wide_Subtype
:
3269 /* If an equivalent type is present, that is what we should use.
3270 Otherwise, fall through to handle this like a record subtype
3271 since it may have constraints. */
3272 if (gnat_equiv_type
!= gnat_entity
)
3274 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
3275 maybe_present
= true;
3279 /* ... fall through ... */
3281 case E_Record_Subtype
:
3282 /* If Cloned_Subtype is Present it means this record subtype has
3283 identical layout to that type or subtype and we should use
3284 that GCC type for this one. The front end guarantees that
3285 the component list is shared. */
3286 if (Present (Cloned_Subtype (gnat_entity
)))
3288 gnu_decl
= gnat_to_gnu_entity (Cloned_Subtype (gnat_entity
),
3290 maybe_present
= true;
3294 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3295 changing the type, make a new type with each field having the type of
3296 the field in the new subtype but the position computed by transforming
3297 every discriminant reference according to the constraints. We don't
3298 see any difference between private and non-private type here since
3299 derivations from types should have been deferred until the completion
3300 of the private type. */
3303 Entity_Id gnat_base_type
= Implementation_Base_Type (gnat_entity
);
3308 defer_incomplete_level
++;
3309 this_deferred
= true;
3312 gnu_base_type
= gnat_to_gnu_type (gnat_base_type
);
3314 if (present_gnu_tree (gnat_entity
))
3316 maybe_present
= true;
3320 /* If this is a record subtype associated with a dispatch table,
3321 strip the suffix. This is necessary to make sure 2 different
3322 subtypes associated with the imported and exported views of a
3323 dispatch table are properly merged in LTO mode. */
3324 if (Is_Dispatch_Table_Entity (gnat_entity
))
3327 Get_Encoded_Name (gnat_entity
);
3328 p
= strchr (Name_Buffer
, '_');
3330 strcpy (p
+2, "dtS");
3331 gnu_entity_name
= get_identifier (Name_Buffer
);
3334 /* When the subtype has discriminants and these discriminants affect
3335 the initial shape it has inherited, factor them in. But for an
3336 Unchecked_Union (it must be an Itype), just return the type.
3337 We can't just test Is_Constrained because private subtypes without
3338 discriminants of types with discriminants with default expressions
3339 are Is_Constrained but aren't constrained! */
3340 if (IN (Ekind (gnat_base_type
), Record_Kind
)
3341 && !Is_Unchecked_Union (gnat_base_type
)
3342 && !Is_For_Access_Subtype (gnat_entity
)
3343 && Has_Discriminants (gnat_entity
)
3344 && Is_Constrained (gnat_entity
)
3345 && Stored_Constraint (gnat_entity
) != No_Elist
)
3347 vec
<subst_pair
> gnu_subst_list
3348 = build_subst_list (gnat_entity
, gnat_base_type
, definition
);
3349 tree gnu_unpad_base_type
, gnu_rep_part
, gnu_variant_part
, t
;
3350 tree gnu_pos_list
, gnu_field_list
= NULL_TREE
;
3351 bool selected_variant
= false;
3352 Entity_Id gnat_field
;
3353 vec
<variant_desc
> gnu_variant_list
;
3355 gnu_type
= make_node (RECORD_TYPE
);
3356 TYPE_NAME (gnu_type
) = gnu_entity_name
;
3357 TYPE_PACKED (gnu_type
) = TYPE_PACKED (gnu_base_type
);
3358 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
3360 /* Set the size, alignment and alias set of the new type to
3361 match that of the old one, doing required substitutions. */
3362 copy_and_substitute_in_size (gnu_type
, gnu_base_type
,
3365 if (TYPE_IS_PADDING_P (gnu_base_type
))
3366 gnu_unpad_base_type
= TREE_TYPE (TYPE_FIELDS (gnu_base_type
));
3368 gnu_unpad_base_type
= gnu_base_type
;
3370 /* Look for a variant part in the base type. */
3371 gnu_variant_part
= get_variant_part (gnu_unpad_base_type
);
3373 /* If there is a variant part, we must compute whether the
3374 constraints statically select a particular variant. If
3375 so, we simply drop the qualified union and flatten the
3376 list of fields. Otherwise we'll build a new qualified
3377 union for the variants that are still relevant. */
3378 if (gnu_variant_part
)
3384 = build_variant_list (TREE_TYPE (gnu_variant_part
),
3388 /* If all the qualifiers are unconditionally true, the
3389 innermost variant is statically selected. */
3390 selected_variant
= true;
3391 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3392 if (!integer_onep (v
->qual
))
3394 selected_variant
= false;
3398 /* Otherwise, create the new variants. */
3399 if (!selected_variant
)
3400 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3402 tree old_variant
= v
->type
;
3403 tree new_variant
= make_node (RECORD_TYPE
);
3405 = concat_name (DECL_NAME (gnu_variant_part
),
3407 (DECL_NAME (v
->field
)));
3408 TYPE_NAME (new_variant
)
3409 = concat_name (TYPE_NAME (gnu_type
),
3410 IDENTIFIER_POINTER (suffix
));
3411 copy_and_substitute_in_size (new_variant
, old_variant
,
3413 v
->new_type
= new_variant
;
3418 gnu_variant_list
.create (0);
3419 selected_variant
= false;
3423 = build_position_list (gnu_unpad_base_type
,
3424 gnu_variant_list
.exists ()
3425 && !selected_variant
,
3426 size_zero_node
, bitsize_zero_node
,
3427 BIGGEST_ALIGNMENT
, NULL_TREE
);
3429 for (gnat_field
= First_Entity (gnat_entity
);
3430 Present (gnat_field
);
3431 gnat_field
= Next_Entity (gnat_field
))
3432 if ((Ekind (gnat_field
) == E_Component
3433 || Ekind (gnat_field
) == E_Discriminant
)
3434 && !(Present (Corresponding_Discriminant (gnat_field
))
3435 && Is_Tagged_Type (gnat_base_type
))
3436 && Underlying_Type (Scope (Original_Record_Component
3440 Name_Id gnat_name
= Chars (gnat_field
);
3441 Entity_Id gnat_old_field
3442 = Original_Record_Component (gnat_field
);
3444 = gnat_to_gnu_field_decl (gnat_old_field
);
3445 tree gnu_context
= DECL_CONTEXT (gnu_old_field
);
3446 tree gnu_field
, gnu_field_type
, gnu_size
;
3447 tree gnu_cont_type
, gnu_last
= NULL_TREE
;
3449 /* If the type is the same, retrieve the GCC type from the
3450 old field to take into account possible adjustments. */
3451 if (Etype (gnat_field
) == Etype (gnat_old_field
))
3452 gnu_field_type
= TREE_TYPE (gnu_old_field
);
3454 gnu_field_type
= gnat_to_gnu_type (Etype (gnat_field
));
3456 /* If there was a component clause, the field types must be
3457 the same for the type and subtype, so copy the data from
3458 the old field to avoid recomputation here. Also if the
3459 field is justified modular and the optimization in
3460 gnat_to_gnu_field was applied. */
3461 if (Present (Component_Clause (gnat_old_field
))
3462 || (TREE_CODE (gnu_field_type
) == RECORD_TYPE
3463 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type
)
3464 && TREE_TYPE (TYPE_FIELDS (gnu_field_type
))
3465 == TREE_TYPE (gnu_old_field
)))
3467 gnu_size
= DECL_SIZE (gnu_old_field
);
3468 gnu_field_type
= TREE_TYPE (gnu_old_field
);
3471 /* If the old field was packed and of constant size, we
3472 have to get the old size here, as it might differ from
3473 what the Etype conveys and the latter might overlap
3474 onto the following field. Try to arrange the type for
3475 possible better packing along the way. */
3476 else if (DECL_PACKED (gnu_old_field
)
3477 && TREE_CODE (DECL_SIZE (gnu_old_field
))
3480 gnu_size
= DECL_SIZE (gnu_old_field
);
3481 if (RECORD_OR_UNION_TYPE_P (gnu_field_type
)
3482 && !TYPE_FAT_POINTER_P (gnu_field_type
)
3483 && host_integerp (TYPE_SIZE (gnu_field_type
), 1))
3485 = make_packable_type (gnu_field_type
, true);
3489 gnu_size
= TYPE_SIZE (gnu_field_type
);
3491 /* If the context of the old field is the base type or its
3492 REP part (if any), put the field directly in the new
3493 type; otherwise look up the context in the variant list
3494 and put the field either in the new type if there is a
3495 selected variant or in one of the new variants. */
3496 if (gnu_context
== gnu_unpad_base_type
3497 || ((gnu_rep_part
= get_rep_part (gnu_unpad_base_type
))
3498 && gnu_context
== TREE_TYPE (gnu_rep_part
)))
3499 gnu_cont_type
= gnu_type
;
3506 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3507 if (gnu_context
== v
->type
3508 || ((gnu_rep_part
= get_rep_part (v
->type
))
3509 && gnu_context
== TREE_TYPE (gnu_rep_part
)))
3516 if (selected_variant
)
3517 gnu_cont_type
= gnu_type
;
3519 gnu_cont_type
= v
->new_type
;
3522 /* The front-end may pass us "ghost" components if
3523 it fails to recognize that a constrained subtype
3524 is statically constrained. Discard them. */
3528 /* Now create the new field modeled on the old one. */
3530 = create_field_decl_from (gnu_old_field
, gnu_field_type
,
3531 gnu_cont_type
, gnu_size
,
3532 gnu_pos_list
, gnu_subst_list
);
3534 /* Put it in one of the new variants directly. */
3535 if (gnu_cont_type
!= gnu_type
)
3537 DECL_CHAIN (gnu_field
) = TYPE_FIELDS (gnu_cont_type
);
3538 TYPE_FIELDS (gnu_cont_type
) = gnu_field
;
3541 /* To match the layout crafted in components_to_record,
3542 if this is the _Tag or _Parent field, put it before
3543 any other fields. */
3544 else if (gnat_name
== Name_uTag
3545 || gnat_name
== Name_uParent
)
3546 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
3548 /* Similarly, if this is the _Controller field, put
3549 it before the other fields except for the _Tag or
3551 else if (gnat_name
== Name_uController
&& gnu_last
)
3553 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
3554 DECL_CHAIN (gnu_last
) = gnu_field
;
3557 /* Otherwise, if this is a regular field, put it after
3558 the other fields. */
3561 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3562 gnu_field_list
= gnu_field
;
3564 gnu_last
= gnu_field
;
3567 save_gnu_tree (gnat_field
, gnu_field
, false);
3570 /* If there is a variant list and no selected variant, we need
3571 to create the nest of variant parts from the old nest. */
3572 if (gnu_variant_list
.exists () && !selected_variant
)
3574 tree new_variant_part
3575 = create_variant_part_from (gnu_variant_part
,
3576 gnu_variant_list
, gnu_type
,
3577 gnu_pos_list
, gnu_subst_list
);
3578 DECL_CHAIN (new_variant_part
) = gnu_field_list
;
3579 gnu_field_list
= new_variant_part
;
3582 /* Now go through the entities again looking for Itypes that
3583 we have not elaborated but should (e.g., Etypes of fields
3584 that have Original_Components). */
3585 for (gnat_field
= First_Entity (gnat_entity
);
3586 Present (gnat_field
); gnat_field
= Next_Entity (gnat_field
))
3587 if ((Ekind (gnat_field
) == E_Discriminant
3588 || Ekind (gnat_field
) == E_Component
)
3589 && !present_gnu_tree (Etype (gnat_field
)))
3590 gnat_to_gnu_entity (Etype (gnat_field
), NULL_TREE
, 0);
3592 /* Do not emit debug info for the type yet since we're going to
3594 finish_record_type (gnu_type
, nreverse (gnu_field_list
), 2,
3596 compute_record_mode (gnu_type
);
3598 /* See the E_Record_Type case for the rationale. */
3599 if (TYPE_MODE (gnu_type
) != BLKmode
3600 && Is_By_Reference_Type (gnat_entity
))
3601 SET_TYPE_MODE (gnu_type
, BLKmode
);
3603 TYPE_VOLATILE (gnu_type
) = Treat_As_Volatile (gnat_entity
);
3605 /* Fill in locations of fields. */
3606 annotate_rep (gnat_entity
, gnu_type
);
3608 /* If debugging information is being written for the type, write
3609 a record that shows what we are a subtype of and also make a
3610 variable that indicates our size, if still variable. */
3613 tree gnu_subtype_marker
= make_node (RECORD_TYPE
);
3614 tree gnu_unpad_base_name
= TYPE_NAME (gnu_unpad_base_type
);
3615 tree gnu_size_unit
= TYPE_SIZE_UNIT (gnu_type
);
3617 if (TREE_CODE (gnu_unpad_base_name
) == TYPE_DECL
)
3618 gnu_unpad_base_name
= DECL_NAME (gnu_unpad_base_name
);
3620 TYPE_NAME (gnu_subtype_marker
)
3621 = create_concat_name (gnat_entity
, "XVS");
3622 finish_record_type (gnu_subtype_marker
,
3623 create_field_decl (gnu_unpad_base_name
,
3624 build_reference_type
3625 (gnu_unpad_base_type
),
3627 NULL_TREE
, NULL_TREE
,
3631 add_parallel_type (gnu_type
, gnu_subtype_marker
);
3634 && TREE_CODE (gnu_size_unit
) != INTEGER_CST
3635 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit
))
3636 TYPE_SIZE_UNIT (gnu_subtype_marker
)
3637 = create_var_decl (create_concat_name (gnat_entity
,
3639 NULL_TREE
, sizetype
, gnu_size_unit
,
3640 false, false, false, false, NULL
,
3644 gnu_variant_list
.release ();
3645 gnu_subst_list
.release ();
3647 /* Now we can finalize it. */
3648 rest_of_record_type_compilation (gnu_type
);
3651 /* Otherwise, go down all the components in the new type and make
3652 them equivalent to those in the base type. */
3655 gnu_type
= gnu_base_type
;
3657 for (gnat_temp
= First_Entity (gnat_entity
);
3658 Present (gnat_temp
);
3659 gnat_temp
= Next_Entity (gnat_temp
))
3660 if ((Ekind (gnat_temp
) == E_Discriminant
3661 && !Is_Unchecked_Union (gnat_base_type
))
3662 || Ekind (gnat_temp
) == E_Component
)
3663 save_gnu_tree (gnat_temp
,
3664 gnat_to_gnu_field_decl
3665 (Original_Record_Component (gnat_temp
)),
3671 case E_Access_Subprogram_Type
:
3672 /* Use the special descriptor type for dispatch tables if needed,
3673 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3674 Note that we are only required to do so for static tables in
3675 order to be compatible with the C++ ABI, but Ada 2005 allows
3676 to extend library level tagged types at the local level so
3677 we do it in the non-static case as well. */
3678 if (TARGET_VTABLE_USES_DESCRIPTORS
3679 && Is_Dispatch_Table_Entity (gnat_entity
))
3681 gnu_type
= fdesc_type_node
;
3682 gnu_size
= TYPE_SIZE (gnu_type
);
3686 /* ... fall through ... */
3688 case E_Anonymous_Access_Subprogram_Type
:
3689 /* If we are not defining this entity, and we have incomplete
3690 entities being processed above us, make a dummy type and
3691 fill it in later. */
3692 if (!definition
&& defer_incomplete_level
!= 0)
3694 struct incomplete
*p
= XNEW (struct incomplete
);
3697 = build_pointer_type
3698 (make_dummy_type (Directly_Designated_Type (gnat_entity
)));
3699 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
3700 !Comes_From_Source (gnat_entity
),
3701 debug_info_p
, gnat_entity
);
3702 this_made_decl
= true;
3703 gnu_type
= TREE_TYPE (gnu_decl
);
3704 save_gnu_tree (gnat_entity
, gnu_decl
, false);
3707 p
->old_type
= TREE_TYPE (gnu_type
);
3708 p
->full_type
= Directly_Designated_Type (gnat_entity
);
3709 p
->next
= defer_incomplete_list
;
3710 defer_incomplete_list
= p
;
3714 /* ... fall through ... */
3716 case E_Allocator_Type
:
3718 case E_Access_Attribute_Type
:
3719 case E_Anonymous_Access_Type
:
3720 case E_General_Access_Type
:
3722 /* The designated type and its equivalent type for gigi. */
3723 Entity_Id gnat_desig_type
= Directly_Designated_Type (gnat_entity
);
3724 Entity_Id gnat_desig_equiv
= Gigi_Equivalent_Type (gnat_desig_type
);
3725 /* Whether it comes from a limited with. */
3726 bool is_from_limited_with
3727 = (IN (Ekind (gnat_desig_equiv
), Incomplete_Kind
)
3728 && From_With_Type (gnat_desig_equiv
));
3729 /* The "full view" of the designated type. If this is an incomplete
3730 entity from a limited with, treat its non-limited view as the full
3731 view. Otherwise, if this is an incomplete or private type, use the
3732 full view. In the former case, we might point to a private type,
3733 in which case, we need its full view. Also, we want to look at the
3734 actual type used for the representation, so this takes a total of
3736 Entity_Id gnat_desig_full_direct_first
3737 = (is_from_limited_with
3738 ? Non_Limited_View (gnat_desig_equiv
)
3739 : (IN (Ekind (gnat_desig_equiv
), Incomplete_Or_Private_Kind
)
3740 ? Full_View (gnat_desig_equiv
) : Empty
));
3741 Entity_Id gnat_desig_full_direct
3742 = ((is_from_limited_with
3743 && Present (gnat_desig_full_direct_first
)
3744 && IN (Ekind (gnat_desig_full_direct_first
), Private_Kind
))
3745 ? Full_View (gnat_desig_full_direct_first
)
3746 : gnat_desig_full_direct_first
);
3747 Entity_Id gnat_desig_full
3748 = Gigi_Equivalent_Type (gnat_desig_full_direct
);
3749 /* The type actually used to represent the designated type, either
3750 gnat_desig_full or gnat_desig_equiv. */
3751 Entity_Id gnat_desig_rep
;
3752 /* True if this is a pointer to an unconstrained array. */
3753 bool is_unconstrained_array
;
3754 /* We want to know if we'll be seeing the freeze node for any
3755 incomplete type we may be pointing to. */
3757 = (Present (gnat_desig_full
)
3758 ? In_Extended_Main_Code_Unit (gnat_desig_full
)
3759 : In_Extended_Main_Code_Unit (gnat_desig_type
));
3760 /* True if we make a dummy type here. */
3761 bool made_dummy
= false;
3762 /* The mode to be used for the pointer type. */
3763 enum machine_mode p_mode
= mode_for_size (esize
, MODE_INT
, 0);
3764 /* The GCC type used for the designated type. */
3765 tree gnu_desig_type
= NULL_TREE
;
3767 if (!targetm
.valid_pointer_mode (p_mode
))
3770 /* If either the designated type or its full view is an unconstrained
3771 array subtype, replace it with the type it's a subtype of. This
3772 avoids problems with multiple copies of unconstrained array types.
3773 Likewise, if the designated type is a subtype of an incomplete
3774 record type, use the parent type to avoid order of elaboration
3775 issues. This can lose some code efficiency, but there is no
3777 if (Ekind (gnat_desig_equiv
) == E_Array_Subtype
3778 && !Is_Constrained (gnat_desig_equiv
))
3779 gnat_desig_equiv
= Etype (gnat_desig_equiv
);
3780 if (Present (gnat_desig_full
)
3781 && ((Ekind (gnat_desig_full
) == E_Array_Subtype
3782 && !Is_Constrained (gnat_desig_full
))
3783 || (Ekind (gnat_desig_full
) == E_Record_Subtype
3784 && Ekind (Etype (gnat_desig_full
)) == E_Record_Type
)))
3785 gnat_desig_full
= Etype (gnat_desig_full
);
3787 /* Set the type that's actually the representation of the designated
3788 type and also flag whether we have a unconstrained array. */
3790 = Present (gnat_desig_full
) ? gnat_desig_full
: gnat_desig_equiv
;
3791 is_unconstrained_array
3792 = Is_Array_Type (gnat_desig_rep
) && !Is_Constrained (gnat_desig_rep
);
3794 /* If we are pointing to an incomplete type whose completion is an
3795 unconstrained array, make dummy fat and thin pointer types to it.
3796 Likewise if the type itself is dummy or an unconstrained array. */
3797 if (is_unconstrained_array
3798 && (Present (gnat_desig_full
)
3799 || (present_gnu_tree (gnat_desig_equiv
)
3801 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv
))))
3803 && defer_incomplete_level
!= 0
3804 && !present_gnu_tree (gnat_desig_equiv
))
3806 && is_from_limited_with
3807 && Present (Freeze_Node (gnat_desig_equiv
)))))
3809 if (present_gnu_tree (gnat_desig_rep
))
3810 gnu_desig_type
= TREE_TYPE (get_gnu_tree (gnat_desig_rep
));
3813 gnu_desig_type
= make_dummy_type (gnat_desig_rep
);
3817 /* If the call above got something that has a pointer, the pointer
3818 is our type. This could have happened either because the type
3819 was elaborated or because somebody else executed the code. */
3820 if (!TYPE_POINTER_TO (gnu_desig_type
))
3821 build_dummy_unc_pointer_types (gnat_desig_equiv
, gnu_desig_type
);
3822 gnu_type
= TYPE_POINTER_TO (gnu_desig_type
);
3825 /* If we already know what the full type is, use it. */
3826 else if (Present (gnat_desig_full
)
3827 && present_gnu_tree (gnat_desig_full
))
3828 gnu_desig_type
= TREE_TYPE (get_gnu_tree (gnat_desig_full
));
3830 /* Get the type of the thing we are to point to and build a pointer to
3831 it. If it is a reference to an incomplete or private type with a
3832 full view that is a record, make a dummy type node and get the
3833 actual type later when we have verified it is safe. */
3834 else if ((!in_main_unit
3835 && !present_gnu_tree (gnat_desig_equiv
)
3836 && Present (gnat_desig_full
)
3837 && !present_gnu_tree (gnat_desig_full
)
3838 && Is_Record_Type (gnat_desig_full
))
3839 /* Likewise if we are pointing to a record or array and we are
3840 to defer elaborating incomplete types. We do this as this
3841 access type may be the full view of a private type. Note
3842 that the unconstrained array case is handled above. */
3843 || ((!in_main_unit
|| imported_p
)
3844 && defer_incomplete_level
!= 0
3845 && !present_gnu_tree (gnat_desig_equiv
)
3846 && (Is_Record_Type (gnat_desig_rep
)
3847 || Is_Array_Type (gnat_desig_rep
)))
3848 /* If this is a reference from a limited_with type back to our
3849 main unit and there's a freeze node for it, either we have
3850 already processed the declaration and made the dummy type,
3851 in which case we just reuse the latter, or we have not yet,
3852 in which case we make the dummy type and it will be reused
3853 when the declaration is finally processed. In both cases,
3854 the pointer eventually created below will be automatically
3855 adjusted when the freeze node is processed. Note that the
3856 unconstrained array case is handled above. */
3858 && is_from_limited_with
3859 && Present (Freeze_Node (gnat_desig_rep
))))
3861 gnu_desig_type
= make_dummy_type (gnat_desig_equiv
);
3865 /* Otherwise handle the case of a pointer to itself. */
3866 else if (gnat_desig_equiv
== gnat_entity
)
3869 = build_pointer_type_for_mode (void_type_node
, p_mode
,
3870 No_Strict_Aliasing (gnat_entity
));
3871 TREE_TYPE (gnu_type
) = TYPE_POINTER_TO (gnu_type
) = gnu_type
;
3874 /* If expansion is disabled, the equivalent type of a concurrent type
3875 is absent, so build a dummy pointer type. */
3876 else if (type_annotate_only
&& No (gnat_desig_equiv
))
3877 gnu_type
= ptr_void_type_node
;
3879 /* Finally, handle the default case where we can just elaborate our
3882 gnu_desig_type
= gnat_to_gnu_type (gnat_desig_equiv
);
3884 /* It is possible that a call to gnat_to_gnu_type above resolved our
3885 type. If so, just return it. */
3886 if (present_gnu_tree (gnat_entity
))
3888 maybe_present
= true;
3892 /* If we haven't done it yet, build the pointer type the usual way. */
3895 /* Modify the designated type if we are pointing only to constant
3896 objects, but don't do it for unconstrained arrays. */
3897 if (Is_Access_Constant (gnat_entity
)
3898 && TREE_CODE (gnu_desig_type
) != UNCONSTRAINED_ARRAY_TYPE
)
3901 = build_qualified_type
3903 TYPE_QUALS (gnu_desig_type
) | TYPE_QUAL_CONST
);
3905 /* Some extra processing is required if we are building a
3906 pointer to an incomplete type (in the GCC sense). We might
3907 have such a type if we just made a dummy, or directly out
3908 of the call to gnat_to_gnu_type above if we are processing
3909 an access type for a record component designating the
3910 record type itself. */
3911 if (TYPE_MODE (gnu_desig_type
) == VOIDmode
)
3913 /* We must ensure that the pointer to variant we make will
3914 be processed by update_pointer_to when the initial type
3915 is completed. Pretend we made a dummy and let further
3916 processing act as usual. */
3919 /* We must ensure that update_pointer_to will not retrieve
3920 the dummy variant when building a properly qualified
3921 version of the complete type. We take advantage of the
3922 fact that get_qualified_type is requiring TYPE_NAMEs to
3923 match to influence build_qualified_type and then also
3924 update_pointer_to here. */
3925 TYPE_NAME (gnu_desig_type
)
3926 = create_concat_name (gnat_desig_type
, "INCOMPLETE_CST");
3931 = build_pointer_type_for_mode (gnu_desig_type
, p_mode
,
3932 No_Strict_Aliasing (gnat_entity
));
3935 /* If we are not defining this object and we have made a dummy pointer,
3936 save our current definition, evaluate the actual type, and replace
3937 the tentative type we made with the actual one. If we are to defer
3938 actually looking up the actual type, make an entry in the deferred
3939 list. If this is from a limited with, we may have to defer to the
3940 end of the current unit. */
3941 if ((!in_main_unit
|| is_from_limited_with
) && made_dummy
)
3943 tree gnu_old_desig_type
;
3945 if (TYPE_IS_FAT_POINTER_P (gnu_type
))
3947 gnu_old_desig_type
= TYPE_UNCONSTRAINED_ARRAY (gnu_type
);
3948 if (esize
== POINTER_SIZE
)
3949 gnu_type
= build_pointer_type
3950 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type
));
3953 gnu_old_desig_type
= TREE_TYPE (gnu_type
);
3955 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
3956 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
3957 !Comes_From_Source (gnat_entity
),
3958 debug_info_p
, gnat_entity
);
3959 this_made_decl
= true;
3960 gnu_type
= TREE_TYPE (gnu_decl
);
3961 save_gnu_tree (gnat_entity
, gnu_decl
, false);
3964 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3965 update gnu_old_desig_type directly, in which case it will not be
3966 a dummy type any more when we get into update_pointer_to.
3968 This can happen e.g. when the designated type is a record type,
3969 because their elaboration starts with an initial node from
3970 make_dummy_type, which may be the same node as the one we got.
3972 Besides, variants of this non-dummy type might have been created
3973 along the way. update_pointer_to is expected to properly take
3974 care of those situations. */
3975 if (defer_incomplete_level
== 0 && !is_from_limited_with
)
3977 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type
),
3978 gnat_to_gnu_type (gnat_desig_equiv
));
3982 struct incomplete
*p
= XNEW (struct incomplete
);
3983 struct incomplete
**head
3984 = (is_from_limited_with
3985 ? &defer_limited_with
: &defer_incomplete_list
);
3986 p
->old_type
= gnu_old_desig_type
;
3987 p
->full_type
= gnat_desig_equiv
;
3995 case E_Access_Protected_Subprogram_Type
:
3996 case E_Anonymous_Access_Protected_Subprogram_Type
:
3997 if (type_annotate_only
&& No (gnat_equiv_type
))
3998 gnu_type
= ptr_void_type_node
;
4001 /* The run-time representation is the equivalent type. */
4002 gnu_type
= gnat_to_gnu_type (gnat_equiv_type
);
4003 maybe_present
= true;
4006 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
4007 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
4008 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
)))
4009 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity
))))
4010 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
4015 case E_Access_Subtype
:
4017 /* We treat this as identical to its base type; any constraint is
4018 meaningful only to the front-end.
4020 The designated type must be elaborated as well, if it does
4021 not have its own freeze node. Designated (sub)types created
4022 for constrained components of records with discriminants are
4023 not frozen by the front-end and thus not elaborated by gigi,
4024 because their use may appear before the base type is frozen,
4025 and because it is not clear that they are needed anywhere in
4026 gigi. With the current model, there is no correct place where
4027 they could be elaborated. */
4029 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
4030 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
4031 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
4032 && Is_Frozen (Directly_Designated_Type (gnat_entity
))
4033 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
))))
4035 /* If we are not defining this entity, and we have incomplete
4036 entities being processed above us, make a dummy type and
4037 elaborate it later. */
4038 if (!definition
&& defer_incomplete_level
!= 0)
4040 struct incomplete
*p
= XNEW (struct incomplete
);
4043 = make_dummy_type (Directly_Designated_Type (gnat_entity
));
4044 p
->full_type
= Directly_Designated_Type (gnat_entity
);
4045 p
->next
= defer_incomplete_list
;
4046 defer_incomplete_list
= p
;
4048 else if (!IN (Ekind (Base_Type
4049 (Directly_Designated_Type (gnat_entity
))),
4050 Incomplete_Or_Private_Kind
))
4051 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
4055 maybe_present
= true;
4058 /* Subprogram Entities
4060 The following access functions are defined for subprograms:
4062 Etype Return type or Standard_Void_Type.
4063 First_Formal The first formal parameter.
4064 Is_Imported Indicates that the subprogram has appeared in
4065 an INTERFACE or IMPORT pragma. For now we
4066 assume that the external language is C.
4067 Is_Exported Likewise but for an EXPORT pragma.
4068 Is_Inlined True if the subprogram is to be inlined.
4070 Each parameter is first checked by calling must_pass_by_ref on its
4071 type to determine if it is passed by reference. For parameters which
4072 are copied in, if they are Ada In Out or Out parameters, their return
4073 value becomes part of a record which becomes the return type of the
4074 function (C function - note that this applies only to Ada procedures
4075 so there is no Ada return type). Additional code to store back the
4076 parameters will be generated on the caller side. This transformation
4077 is done here, not in the front-end.
4079 The intended result of the transformation can be seen from the
4080 equivalent source rewritings that follow:
4082 struct temp {int a,b};
4083 procedure P (A,B: In Out ...) is temp P (int A,B)
4086 end P; return {A,B};
4093 For subprogram types we need to perform mainly the same conversions to
4094 GCC form that are needed for procedures and function declarations. The
4095 only difference is that at the end, we make a type declaration instead
4096 of a function declaration. */
4098 case E_Subprogram_Type
:
4102 /* The type returned by a function or else Standard_Void_Type for a
4104 Entity_Id gnat_return_type
= Etype (gnat_entity
);
4105 tree gnu_return_type
;
4106 /* The first GCC parameter declaration (a PARM_DECL node). The
4107 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4108 actually is the head of this parameter list. */
4109 tree gnu_param_list
= NULL_TREE
;
4110 /* Likewise for the stub associated with an exported procedure. */
4111 tree gnu_stub_param_list
= NULL_TREE
;
4112 /* Non-null for subprograms containing parameters passed by copy-in
4113 copy-out (Ada In Out or Out parameters not passed by reference),
4114 in which case it is the list of nodes used to specify the values
4115 of the In Out/Out parameters that are returned as a record upon
4116 procedure return. The TREE_PURPOSE of an element of this list is
4117 a field of the record and the TREE_VALUE is the PARM_DECL
4118 corresponding to that field. This list will be saved in the
4119 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4120 tree gnu_cico_list
= NULL_TREE
;
4121 /* List of fields in return type of procedure with copy-in copy-out
4123 tree gnu_field_list
= NULL_TREE
;
4124 /* If an import pragma asks to map this subprogram to a GCC builtin,
4125 this is the builtin DECL node. */
4126 tree gnu_builtin_decl
= NULL_TREE
;
4127 /* For the stub associated with an exported procedure. */
4128 tree gnu_stub_type
= NULL_TREE
, gnu_stub_name
= NULL_TREE
;
4129 tree gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
4130 Entity_Id gnat_param
;
4131 enum inline_status_t inline_status
4132 = Has_Pragma_No_Inline (gnat_entity
)
4134 : (Is_Inlined (gnat_entity
) ? is_enabled
: is_disabled
);
4135 bool public_flag
= Is_Public (gnat_entity
) || imported_p
;
4137 = (Is_Public (gnat_entity
) && !definition
) || imported_p
;
4138 bool artificial_flag
= !Comes_From_Source (gnat_entity
);
4139 /* The semantics of "pure" in Ada essentially matches that of "const"
4140 in the back-end. In particular, both properties are orthogonal to
4141 the "nothrow" property if the EH circuitry is explicit in the
4142 internal representation of the back-end. If we are to completely
4143 hide the EH circuitry from it, we need to declare that calls to pure
4144 Ada subprograms that can throw have side effects since they can
4145 trigger an "abnormal" transfer of control flow; thus they can be
4146 neither "const" nor "pure" in the back-end sense. */
4148 = (Exception_Mechanism
== Back_End_Exceptions
4149 && Is_Pure (gnat_entity
));
4150 bool volatile_flag
= No_Return (gnat_entity
);
4151 bool return_by_direct_ref_p
= false;
4152 bool return_by_invisi_ref_p
= false;
4153 bool return_unconstrained_p
= false;
4154 bool has_stub
= false;
4157 /* A parameter may refer to this type, so defer completion of any
4158 incomplete types. */
4159 if (kind
== E_Subprogram_Type
&& !definition
)
4161 defer_incomplete_level
++;
4162 this_deferred
= true;
4165 /* If the subprogram has an alias, it is probably inherited, so
4166 we can use the original one. If the original "subprogram"
4167 is actually an enumeration literal, it may be the first use
4168 of its type, so we must elaborate that type now. */
4169 if (Present (Alias (gnat_entity
)))
4171 if (Ekind (Alias (gnat_entity
)) == E_Enumeration_Literal
)
4172 gnat_to_gnu_entity (Etype (Alias (gnat_entity
)), NULL_TREE
, 0);
4174 gnu_decl
= gnat_to_gnu_entity (Alias (gnat_entity
), gnu_expr
, 0);
4176 /* Elaborate any Itypes in the parameters of this entity. */
4177 for (gnat_temp
= First_Formal_With_Extras (gnat_entity
);
4178 Present (gnat_temp
);
4179 gnat_temp
= Next_Formal_With_Extras (gnat_temp
))
4180 if (Is_Itype (Etype (gnat_temp
)))
4181 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
4186 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4187 corresponding DECL node. Proper generation of calls later on need
4188 proper parameter associations so we don't "break;" here. */
4189 if (Convention (gnat_entity
) == Convention_Intrinsic
4190 && Present (Interface_Name (gnat_entity
)))
4192 gnu_builtin_decl
= builtin_decl_for (gnu_ext_name
);
4194 /* Inability to find the builtin decl most often indicates a
4195 genuine mistake, but imports of unregistered intrinsics are
4196 sometimes issued on purpose to allow hooking in alternate
4197 bodies. We post a warning conditioned on Wshadow in this case,
4198 to let developers be notified on demand without risking false
4199 positives with common default sets of options. */
4201 if (gnu_builtin_decl
== NULL_TREE
&& warn_shadow
)
4202 post_error ("?gcc intrinsic not found for&!", gnat_entity
);
4205 /* ??? What if we don't find the builtin node above ? warn ? err ?
4206 In the current state we neither warn nor err, and calls will just
4207 be handled as for regular subprograms. */
4209 /* Look into the return type and get its associated GCC tree. If it
4210 is not void, compute various flags for the subprogram type. */
4211 if (Ekind (gnat_return_type
) == E_Void
)
4212 gnu_return_type
= void_type_node
;
4215 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4216 context may now appear in parameter and result profiles. If
4217 we are only annotating types, break circularities here. */
4218 if (type_annotate_only
4219 && IN (Ekind (gnat_return_type
), Incomplete_Kind
)
4220 && From_With_Type (gnat_return_type
)
4221 && In_Extended_Main_Code_Unit
4222 (Non_Limited_View (gnat_return_type
))
4223 && !present_gnu_tree (Non_Limited_View (gnat_return_type
)))
4224 gnu_return_type
= ptr_void_type_node
;
4226 gnu_return_type
= gnat_to_gnu_type (gnat_return_type
);
4228 /* If this function returns by reference, make the actual return
4229 type the pointer type and make a note of that. */
4230 if (Returns_By_Ref (gnat_entity
))
4232 gnu_return_type
= build_pointer_type (gnu_return_type
);
4233 return_by_direct_ref_p
= true;
4236 /* If we are supposed to return an unconstrained array type, make
4237 the actual return type the fat pointer type. */
4238 else if (TREE_CODE (gnu_return_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4240 gnu_return_type
= TREE_TYPE (gnu_return_type
);
4241 return_unconstrained_p
= true;
4244 /* Likewise, if the return type requires a transient scope, the
4245 return value will be allocated on the secondary stack so the
4246 actual return type is the pointer type. */
4247 else if (Requires_Transient_Scope (gnat_return_type
))
4249 gnu_return_type
= build_pointer_type (gnu_return_type
);
4250 return_unconstrained_p
= true;
4253 /* If the Mechanism is By_Reference, ensure this function uses the
4254 target's by-invisible-reference mechanism, which may not be the
4255 same as above (e.g. it might be passing an extra parameter). */
4256 else if (kind
== E_Function
4257 && Mechanism (gnat_entity
) == By_Reference
)
4258 return_by_invisi_ref_p
= true;
4260 /* Likewise, if the return type is itself By_Reference. */
4261 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type
))
4262 return_by_invisi_ref_p
= true;
4264 /* If the type is a padded type and the underlying type would not
4265 be passed by reference or the function has a foreign convention,
4266 return the underlying type. */
4267 else if (TYPE_IS_PADDING_P (gnu_return_type
)
4268 && (!default_pass_by_ref
4269 (TREE_TYPE (TYPE_FIELDS (gnu_return_type
)))
4270 || Has_Foreign_Convention (gnat_entity
)))
4271 gnu_return_type
= TREE_TYPE (TYPE_FIELDS (gnu_return_type
));
4273 /* If the return type is unconstrained, that means it must have a
4274 maximum size. Use the padded type as the effective return type.
4275 And ensure the function uses the target's by-invisible-reference
4276 mechanism to avoid copying too much data when it returns. */
4277 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type
)))
4279 tree orig_type
= gnu_return_type
;
4282 = maybe_pad_type (gnu_return_type
,
4283 max_size (TYPE_SIZE (gnu_return_type
),
4285 0, gnat_entity
, false, false, false, true);
4287 /* Declare it now since it will never be declared otherwise.
4288 This is necessary to ensure that its subtrees are properly
4290 if (gnu_return_type
!= orig_type
4291 && !DECL_P (TYPE_NAME (gnu_return_type
)))
4292 create_type_decl (TYPE_NAME (gnu_return_type
),
4293 gnu_return_type
, true, debug_info_p
,
4296 return_by_invisi_ref_p
= true;
4299 /* If the return type has a size that overflows, we cannot have
4300 a function that returns that type. This usage doesn't make
4301 sense anyway, so give an error here. */
4302 if (TYPE_SIZE_UNIT (gnu_return_type
)
4303 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type
)) == INTEGER_CST
4304 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type
)))
4306 post_error ("cannot return type whose size overflows",
4308 gnu_return_type
= copy_node (gnu_return_type
);
4309 TYPE_SIZE (gnu_return_type
) = bitsize_zero_node
;
4310 TYPE_SIZE_UNIT (gnu_return_type
) = size_zero_node
;
4311 TYPE_MAIN_VARIANT (gnu_return_type
) = gnu_return_type
;
4312 TYPE_NEXT_VARIANT (gnu_return_type
) = NULL_TREE
;
4316 /* Loop over the parameters and get their associated GCC tree. While
4317 doing this, build a copy-in copy-out structure if we need one. */
4318 for (gnat_param
= First_Formal_With_Extras (gnat_entity
), parmnum
= 0;
4319 Present (gnat_param
);
4320 gnat_param
= Next_Formal_With_Extras (gnat_param
), parmnum
++)
4322 Entity_Id gnat_param_type
= Etype (gnat_param
);
4323 tree gnu_param_name
= get_entity_name (gnat_param
);
4324 tree gnu_param_type
, gnu_param
, gnu_field
;
4325 Mechanism_Type mech
= Mechanism (gnat_param
);
4326 bool copy_in_copy_out
= false, fake_param_type
;
4328 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4329 context may now appear in parameter and result profiles. If
4330 we are only annotating types, break circularities here. */
4331 if (type_annotate_only
4332 && IN (Ekind (gnat_param_type
), Incomplete_Kind
)
4333 && From_With_Type (Etype (gnat_param_type
))
4334 && In_Extended_Main_Code_Unit
4335 (Non_Limited_View (gnat_param_type
))
4336 && !present_gnu_tree (Non_Limited_View (gnat_param_type
)))
4338 gnu_param_type
= ptr_void_type_node
;
4339 fake_param_type
= true;
4343 gnu_param_type
= gnat_to_gnu_type (gnat_param_type
);
4344 fake_param_type
= false;
4347 /* Builtins are expanded inline and there is no real call sequence
4348 involved. So the type expected by the underlying expander is
4349 always the type of each argument "as is". */
4350 if (gnu_builtin_decl
)
4352 /* Handle the first parameter of a valued procedure specially. */
4353 else if (Is_Valued_Procedure (gnat_entity
) && parmnum
== 0)
4354 mech
= By_Copy_Return
;
4355 /* Otherwise, see if a Mechanism was supplied that forced this
4356 parameter to be passed one way or another. */
4357 else if (mech
== Default
4358 || mech
== By_Copy
|| mech
== By_Reference
)
4360 else if (By_Descriptor_Last
<= mech
&& mech
<= By_Descriptor
)
4361 mech
= By_Descriptor
;
4363 else if (By_Short_Descriptor_Last
<= mech
&&
4364 mech
<= By_Short_Descriptor
)
4365 mech
= By_Short_Descriptor
;
4369 if (TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
4370 || TREE_CODE (TYPE_SIZE (gnu_param_type
)) != INTEGER_CST
4371 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type
),
4373 mech
= By_Reference
;
4379 post_error ("unsupported mechanism for&", gnat_param
);
4383 /* Do not call gnat_to_gnu_param for a fake parameter type since
4384 it will try to use the real type again. */
4385 if (fake_param_type
)
4387 if (Ekind (gnat_param
) == E_Out_Parameter
)
4388 gnu_param
= NULL_TREE
;
4392 = create_param_decl (gnu_param_name
, gnu_param_type
,
4394 Set_Mechanism (gnat_param
,
4395 mech
== Default
? By_Copy
: mech
);
4396 if (Ekind (gnat_param
) == E_In_Out_Parameter
)
4397 copy_in_copy_out
= true;
4402 = gnat_to_gnu_param (gnat_param
, mech
, gnat_entity
,
4403 Has_Foreign_Convention (gnat_entity
),
4406 /* We are returned either a PARM_DECL or a type if no parameter
4407 needs to be passed; in either case, adjust the type. */
4408 if (DECL_P (gnu_param
))
4409 gnu_param_type
= TREE_TYPE (gnu_param
);
4412 gnu_param_type
= gnu_param
;
4413 gnu_param
= NULL_TREE
;
4416 /* The failure of this assertion will very likely come from an
4417 order of elaboration issue for the type of the parameter. */
4418 gcc_assert (kind
== E_Subprogram_Type
4419 || !TYPE_IS_DUMMY_P (gnu_param_type
)
4420 || type_annotate_only
);
4424 /* If it's an exported subprogram, we build a parameter list
4425 in parallel, in case we need to emit a stub for it. */
4426 if (Is_Exported (gnat_entity
))
4429 = chainon (gnu_param
, gnu_stub_param_list
);
4430 /* Change By_Descriptor parameter to By_Reference for
4431 the internal version of an exported subprogram. */
4432 if (mech
== By_Descriptor
|| mech
== By_Short_Descriptor
)
4435 = gnat_to_gnu_param (gnat_param
, By_Reference
,
4441 gnu_param
= copy_node (gnu_param
);
4444 gnu_param_list
= chainon (gnu_param
, gnu_param_list
);
4445 Sloc_to_locus (Sloc (gnat_param
),
4446 &DECL_SOURCE_LOCATION (gnu_param
));
4447 save_gnu_tree (gnat_param
, gnu_param
, false);
4449 /* If a parameter is a pointer, this function may modify
4450 memory through it and thus shouldn't be considered
4451 a const function. Also, the memory may be modified
4452 between two calls, so they can't be CSE'ed. The latter
4453 case also handles by-ref parameters. */
4454 if (POINTER_TYPE_P (gnu_param_type
)
4455 || TYPE_IS_FAT_POINTER_P (gnu_param_type
))
4459 if (copy_in_copy_out
)
4463 tree gnu_new_ret_type
= make_node (RECORD_TYPE
);
4465 /* If this is a function, we also need a field for the
4466 return value to be placed. */
4467 if (TREE_CODE (gnu_return_type
) != VOID_TYPE
)
4470 = create_field_decl (get_identifier ("RETVAL"),
4472 gnu_new_ret_type
, NULL_TREE
,
4474 Sloc_to_locus (Sloc (gnat_entity
),
4475 &DECL_SOURCE_LOCATION (gnu_field
));
4476 gnu_field_list
= gnu_field
;
4478 = tree_cons (gnu_field
, void_type_node
, NULL_TREE
);
4481 gnu_return_type
= gnu_new_ret_type
;
4482 TYPE_NAME (gnu_return_type
) = get_identifier ("RETURN");
4483 /* Set a default alignment to speed up accesses. But we
4484 shouldn't increase the size of the structure too much,
4485 lest it doesn't fit in return registers anymore. */
4486 TYPE_ALIGN (gnu_return_type
)
4487 = get_mode_alignment (ptr_mode
);
4491 = create_field_decl (gnu_param_name
, gnu_param_type
,
4492 gnu_return_type
, NULL_TREE
, NULL_TREE
,
4494 Sloc_to_locus (Sloc (gnat_param
),
4495 &DECL_SOURCE_LOCATION (gnu_field
));
4496 DECL_CHAIN (gnu_field
) = gnu_field_list
;
4497 gnu_field_list
= gnu_field
;
4499 = tree_cons (gnu_field
, gnu_param
, gnu_cico_list
);
4505 /* If we have a CICO list but it has only one entry, we convert
4506 this function into a function that returns this object. */
4507 if (list_length (gnu_cico_list
) == 1)
4508 gnu_return_type
= TREE_TYPE (TREE_PURPOSE (gnu_cico_list
));
4510 /* Do not finalize the return type if the subprogram is stubbed
4511 since structures are incomplete for the back-end. */
4512 else if (Convention (gnat_entity
) != Convention_Stubbed
)
4514 finish_record_type (gnu_return_type
, nreverse (gnu_field_list
),
4517 /* Try to promote the mode of the return type if it is passed
4518 in registers, again to speed up accesses. */
4519 if (TYPE_MODE (gnu_return_type
) == BLKmode
4520 && !targetm
.calls
.return_in_memory (gnu_return_type
,
4524 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type
));
4525 unsigned int i
= BITS_PER_UNIT
;
4526 enum machine_mode mode
;
4530 mode
= mode_for_size (i
, MODE_INT
, 0);
4531 if (mode
!= BLKmode
)
4533 SET_TYPE_MODE (gnu_return_type
, mode
);
4534 TYPE_ALIGN (gnu_return_type
)
4535 = GET_MODE_ALIGNMENT (mode
);
4536 TYPE_SIZE (gnu_return_type
)
4537 = bitsize_int (GET_MODE_BITSIZE (mode
));
4538 TYPE_SIZE_UNIT (gnu_return_type
)
4539 = size_int (GET_MODE_SIZE (mode
));
4544 rest_of_record_type_compilation (gnu_return_type
);
4548 if (Has_Stdcall_Convention (gnat_entity
))
4549 prepend_one_attribute_to
4550 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4551 get_identifier ("stdcall"), NULL_TREE
,
4553 else if (Has_Thiscall_Convention (gnat_entity
))
4554 prepend_one_attribute_to
4555 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4556 get_identifier ("thiscall"), NULL_TREE
,
4559 /* If we should request stack realignment for a foreign convention
4560 subprogram, do so. Note that this applies to task entry points in
4562 if (FOREIGN_FORCE_REALIGN_STACK
4563 && Has_Foreign_Convention (gnat_entity
))
4564 prepend_one_attribute_to
4565 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4566 get_identifier ("force_align_arg_pointer"), NULL_TREE
,
4569 /* The lists have been built in reverse. */
4570 gnu_param_list
= nreverse (gnu_param_list
);
4572 gnu_stub_param_list
= nreverse (gnu_stub_param_list
);
4573 gnu_cico_list
= nreverse (gnu_cico_list
);
4575 if (kind
== E_Function
)
4576 Set_Mechanism (gnat_entity
, return_unconstrained_p
4577 || return_by_direct_ref_p
4578 || return_by_invisi_ref_p
4579 ? By_Reference
: By_Copy
);
4581 = create_subprog_type (gnu_return_type
, gnu_param_list
,
4582 gnu_cico_list
, return_unconstrained_p
,
4583 return_by_direct_ref_p
,
4584 return_by_invisi_ref_p
);
4588 = create_subprog_type (gnu_return_type
, gnu_stub_param_list
,
4589 gnu_cico_list
, return_unconstrained_p
,
4590 return_by_direct_ref_p
,
4591 return_by_invisi_ref_p
);
4593 /* A subprogram (something that doesn't return anything) shouldn't
4594 be considered const since there would be no reason for such a
4595 subprogram. Note that procedures with Out (or In Out) parameters
4596 have already been converted into a function with a return type. */
4597 if (TREE_CODE (gnu_return_type
) == VOID_TYPE
)
4601 = build_qualified_type (gnu_type
,
4602 TYPE_QUALS (gnu_type
)
4603 | (TYPE_QUAL_CONST
* const_flag
)
4604 | (TYPE_QUAL_VOLATILE
* volatile_flag
));
4608 = build_qualified_type (gnu_stub_type
,
4609 TYPE_QUALS (gnu_stub_type
)
4610 | (TYPE_QUAL_CONST
* const_flag
)
4611 | (TYPE_QUAL_VOLATILE
* volatile_flag
));
4613 /* If we have a builtin decl for that function, use it. Check if the
4614 profiles are compatible and warn if they are not. The checker is
4615 expected to post extra diagnostics in this case. */
4616 if (gnu_builtin_decl
)
4618 intrin_binding_t inb
;
4620 inb
.gnat_entity
= gnat_entity
;
4621 inb
.ada_fntype
= gnu_type
;
4622 inb
.btin_fntype
= TREE_TYPE (gnu_builtin_decl
);
4624 if (!intrin_profiles_compatible_p (&inb
))
4626 ("?profile of& doesn''t match the builtin it binds!",
4629 gnu_decl
= gnu_builtin_decl
;
4630 gnu_type
= TREE_TYPE (gnu_builtin_decl
);
4634 /* If there was no specified Interface_Name and the external and
4635 internal names of the subprogram are the same, only use the
4636 internal name to allow disambiguation of nested subprograms. */
4637 if (No (Interface_Name (gnat_entity
))
4638 && gnu_ext_name
== gnu_entity_name
)
4639 gnu_ext_name
= NULL_TREE
;
4641 /* If we are defining the subprogram and it has an Address clause
4642 we must get the address expression from the saved GCC tree for the
4643 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4644 the address expression here since the front-end has guaranteed
4645 in that case that the elaboration has no effects. If there is
4646 an Address clause and we are not defining the object, just
4647 make it a constant. */
4648 if (Present (Address_Clause (gnat_entity
)))
4650 tree gnu_address
= NULL_TREE
;
4654 = (present_gnu_tree (gnat_entity
)
4655 ? get_gnu_tree (gnat_entity
)
4656 : gnat_to_gnu (Expression (Address_Clause (gnat_entity
))));
4658 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
4660 /* Convert the type of the object to a reference type that can
4661 alias everything as per 13.3(19). */
4663 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
4665 gnu_address
= convert (gnu_type
, gnu_address
);
4668 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4669 gnu_address
, false, Is_Public (gnat_entity
),
4670 extern_flag
, false, NULL
, gnat_entity
);
4671 DECL_BY_REF_P (gnu_decl
) = 1;
4674 else if (kind
== E_Subprogram_Type
)
4676 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
4678 = create_type_decl (gnu_entity_name
, gnu_type
, artificial_flag
,
4679 debug_info_p
, gnat_entity
);
4685 gnu_stub_name
= gnu_ext_name
;
4686 gnu_ext_name
= create_concat_name (gnat_entity
, "internal");
4687 public_flag
= false;
4688 artificial_flag
= true;
4692 = create_subprog_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4693 gnu_param_list
, inline_status
,
4694 public_flag
, extern_flag
, artificial_flag
,
4695 attr_list
, gnat_entity
);
4699 = create_subprog_decl (gnu_entity_name
, gnu_stub_name
,
4700 gnu_stub_type
, gnu_stub_param_list
,
4701 inline_status
, true, extern_flag
,
4702 false, attr_list
, gnat_entity
);
4703 SET_DECL_FUNCTION_STUB (gnu_decl
, gnu_stub_decl
);
4706 /* This is unrelated to the stub built right above. */
4707 DECL_STUBBED_P (gnu_decl
)
4708 = Convention (gnat_entity
) == Convention_Stubbed
;
4713 case E_Incomplete_Type
:
4714 case E_Incomplete_Subtype
:
4715 case E_Private_Type
:
4716 case E_Private_Subtype
:
4717 case E_Limited_Private_Type
:
4718 case E_Limited_Private_Subtype
:
4719 case E_Record_Type_With_Private
:
4720 case E_Record_Subtype_With_Private
:
4722 /* Get the "full view" of this entity. If this is an incomplete
4723 entity from a limited with, treat its non-limited view as the
4724 full view. Otherwise, use either the full view or the underlying
4725 full view, whichever is present. This is used in all the tests
4728 = (IN (kind
, Incomplete_Kind
) && From_With_Type (gnat_entity
))
4729 ? Non_Limited_View (gnat_entity
)
4730 : Present (Full_View (gnat_entity
))
4731 ? Full_View (gnat_entity
)
4732 : Underlying_Full_View (gnat_entity
);
4734 /* If this is an incomplete type with no full view, it must be a Taft
4735 Amendment type, in which case we return a dummy type. Otherwise,
4736 just get the type from its Etype. */
4739 if (kind
== E_Incomplete_Type
)
4741 gnu_type
= make_dummy_type (gnat_entity
);
4742 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4746 gnu_decl
= gnat_to_gnu_entity (Etype (gnat_entity
),
4748 maybe_present
= true;
4753 /* If we already made a type for the full view, reuse it. */
4754 else if (present_gnu_tree (full_view
))
4756 gnu_decl
= get_gnu_tree (full_view
);
4760 /* Otherwise, if we are not defining the type now, get the type
4761 from the full view. But always get the type from the full view
4762 for define on use types, since otherwise we won't see them! */
4763 else if (!definition
4764 || (Is_Itype (full_view
)
4765 && No (Freeze_Node (gnat_entity
)))
4766 || (Is_Itype (gnat_entity
)
4767 && No (Freeze_Node (full_view
))))
4769 gnu_decl
= gnat_to_gnu_entity (full_view
, NULL_TREE
, 0);
4770 maybe_present
= true;
4774 /* For incomplete types, make a dummy type entry which will be
4775 replaced later. Save it as the full declaration's type so
4776 we can do any needed updates when we see it. */
4777 gnu_type
= make_dummy_type (gnat_entity
);
4778 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4779 if (Has_Completion_In_Body (gnat_entity
))
4780 DECL_TAFT_TYPE_P (gnu_decl
) = 1;
4781 save_gnu_tree (full_view
, gnu_decl
, 0);
4785 case E_Class_Wide_Type
:
4786 /* Class-wide types are always transformed into their root type. */
4787 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
4788 maybe_present
= true;
4792 case E_Task_Subtype
:
4793 case E_Protected_Type
:
4794 case E_Protected_Subtype
:
4795 /* Concurrent types are always transformed into their record type. */
4796 if (type_annotate_only
&& No (gnat_equiv_type
))
4797 gnu_type
= void_type_node
;
4799 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
4800 maybe_present
= true;
4804 gnu_decl
= create_label_decl (gnu_entity_name
, gnat_entity
);
4809 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4810 we've already saved it, so we don't try to. */
4811 gnu_decl
= error_mark_node
;
4819 /* If we had a case where we evaluated another type and it might have
4820 defined this one, handle it here. */
4821 if (maybe_present
&& present_gnu_tree (gnat_entity
))
4823 gnu_decl
= get_gnu_tree (gnat_entity
);
4827 /* If we are processing a type and there is either no decl for it or
4828 we just made one, do some common processing for the type, such as
4829 handling alignment and possible padding. */
4830 if (is_type
&& (!gnu_decl
|| this_made_decl
))
4832 /* Process the attributes, if not already done. Note that the type is
4833 already defined so we cannot pass True for IN_PLACE here. */
4834 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
4836 /* Tell the middle-end that objects of tagged types are guaranteed to
4837 be properly aligned. This is necessary because conversions to the
4838 class-wide type are translated into conversions to the root type,
4839 which can be less aligned than some of its derived types. */
4840 if (Is_Tagged_Type (gnat_entity
)
4841 || Is_Class_Wide_Equivalent_Type (gnat_entity
))
4842 TYPE_ALIGN_OK (gnu_type
) = 1;
4844 /* Record whether the type is passed by reference. */
4845 if (!VOID_TYPE_P (gnu_type
) && Is_By_Reference_Type (gnat_entity
))
4846 TYPE_BY_REFERENCE_P (gnu_type
) = 1;
4848 /* ??? Don't set the size for a String_Literal since it is either
4849 confirming or we don't handle it properly (if the low bound is
4851 if (!gnu_size
&& kind
!= E_String_Literal_Subtype
)
4853 Uint gnat_size
= Known_Esize (gnat_entity
)
4854 ? Esize (gnat_entity
) : RM_Size (gnat_entity
);
4856 = validate_size (gnat_size
, gnu_type
, gnat_entity
, TYPE_DECL
,
4857 false, Has_Size_Clause (gnat_entity
));
4860 /* If a size was specified, see if we can make a new type of that size
4861 by rearranging the type, for example from a fat to a thin pointer. */
4865 = make_type_from_size (gnu_type
, gnu_size
,
4866 Has_Biased_Representation (gnat_entity
));
4868 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0)
4869 && operand_equal_p (rm_size (gnu_type
), gnu_size
, 0))
4870 gnu_size
= NULL_TREE
;
4873 /* If the alignment hasn't already been processed and this is
4874 not an unconstrained array, see if an alignment is specified.
4875 If not, we pick a default alignment for atomic objects. */
4876 if (align
!= 0 || TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4878 else if (Known_Alignment (gnat_entity
))
4880 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
4881 TYPE_ALIGN (gnu_type
));
4883 /* Warn on suspiciously large alignments. This should catch
4884 errors about the (alignment,byte)/(size,bit) discrepancy. */
4885 if (align
> BIGGEST_ALIGNMENT
&& Has_Alignment_Clause (gnat_entity
))
4889 /* If a size was specified, take it into account. Otherwise
4890 use the RM size for records or unions as the type size has
4891 already been adjusted to the alignment. */
4894 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
4895 && !TYPE_FAT_POINTER_P (gnu_type
))
4896 size
= rm_size (gnu_type
);
4898 size
= TYPE_SIZE (gnu_type
);
4900 /* Consider an alignment as suspicious if the alignment/size
4901 ratio is greater or equal to the byte/bit ratio. */
4902 if (host_integerp (size
, 1)
4903 && align
>= TREE_INT_CST_LOW (size
) * BITS_PER_UNIT
)
4904 post_error_ne ("?suspiciously large alignment specified for&",
4905 Expression (Alignment_Clause (gnat_entity
)),
4909 else if (Is_Atomic (gnat_entity
) && !gnu_size
4910 && host_integerp (TYPE_SIZE (gnu_type
), 1)
4911 && integer_pow2p (TYPE_SIZE (gnu_type
)))
4912 align
= MIN (BIGGEST_ALIGNMENT
,
4913 tree_low_cst (TYPE_SIZE (gnu_type
), 1));
4914 else if (Is_Atomic (gnat_entity
) && gnu_size
4915 && host_integerp (gnu_size
, 1)
4916 && integer_pow2p (gnu_size
))
4917 align
= MIN (BIGGEST_ALIGNMENT
, tree_low_cst (gnu_size
, 1));
4919 /* See if we need to pad the type. If we did, and made a record,
4920 the name of the new type may be changed. So get it back for
4921 us when we make the new TYPE_DECL below. */
4922 if (gnu_size
|| align
> 0)
4923 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
4924 false, !gnu_decl
, definition
, false);
4926 if (TYPE_IS_PADDING_P (gnu_type
))
4928 gnu_entity_name
= TYPE_NAME (gnu_type
);
4929 if (TREE_CODE (gnu_entity_name
) == TYPE_DECL
)
4930 gnu_entity_name
= DECL_NAME (gnu_entity_name
);
4933 /* Now set the RM size of the type. We cannot do it before padding
4934 because we need to accept arbitrary RM sizes on integral types. */
4935 set_rm_size (RM_Size (gnat_entity
), gnu_type
, gnat_entity
);
4937 /* If we are at global level, GCC will have applied variable_size to
4938 the type, but that won't have done anything. So, if it's not
4939 a constant or self-referential, call elaborate_expression_1 to
4940 make a variable for the size rather than calculating it each time.
4941 Handle both the RM size and the actual size. */
4942 if (global_bindings_p ()
4943 && TYPE_SIZE (gnu_type
)
4944 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
))
4945 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
4947 tree size
= TYPE_SIZE (gnu_type
);
4949 TYPE_SIZE (gnu_type
)
4950 = elaborate_expression_1 (size
, gnat_entity
,
4951 get_identifier ("SIZE"),
4954 /* ??? For now, store the size as a multiple of the alignment in
4955 bytes so that we can see the alignment from the tree. */
4956 TYPE_SIZE_UNIT (gnu_type
)
4957 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type
), gnat_entity
,
4958 get_identifier ("SIZE_A_UNIT"),
4960 TYPE_ALIGN (gnu_type
));
4962 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4963 may not be marked by the call to create_type_decl below. */
4964 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type
));
4966 if (TREE_CODE (gnu_type
) == RECORD_TYPE
)
4968 tree variant_part
= get_variant_part (gnu_type
);
4969 tree ada_size
= TYPE_ADA_SIZE (gnu_type
);
4973 tree union_type
= TREE_TYPE (variant_part
);
4974 tree offset
= DECL_FIELD_OFFSET (variant_part
);
4976 /* If the position of the variant part is constant, subtract
4977 it from the size of the type of the parent to get the new
4978 size. This manual CSE reduces the data size. */
4979 if (TREE_CODE (offset
) == INTEGER_CST
)
4981 tree bitpos
= DECL_FIELD_BIT_OFFSET (variant_part
);
4982 TYPE_SIZE (union_type
)
4983 = size_binop (MINUS_EXPR
, TYPE_SIZE (gnu_type
),
4984 bit_from_pos (offset
, bitpos
));
4985 TYPE_SIZE_UNIT (union_type
)
4986 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (gnu_type
),
4987 byte_from_pos (offset
, bitpos
));
4991 TYPE_SIZE (union_type
)
4992 = elaborate_expression_1 (TYPE_SIZE (union_type
),
4994 get_identifier ("VSIZE"),
4997 /* ??? For now, store the size as a multiple of the
4998 alignment in bytes so that we can see the alignment
5000 TYPE_SIZE_UNIT (union_type
)
5001 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type
),
5006 TYPE_ALIGN (union_type
));
5008 /* ??? For now, store the offset as a multiple of the
5009 alignment in bytes so that we can see the alignment
5011 DECL_FIELD_OFFSET (variant_part
)
5012 = elaborate_expression_2 (offset
,
5014 get_identifier ("VOFFSET"),
5020 DECL_SIZE (variant_part
) = TYPE_SIZE (union_type
);
5021 DECL_SIZE_UNIT (variant_part
) = TYPE_SIZE_UNIT (union_type
);
5024 if (operand_equal_p (ada_size
, size
, 0))
5025 ada_size
= TYPE_SIZE (gnu_type
);
5028 = elaborate_expression_1 (ada_size
, gnat_entity
,
5029 get_identifier ("RM_SIZE"),
5031 SET_TYPE_ADA_SIZE (gnu_type
, ada_size
);
5035 /* If this is a record type or subtype, call elaborate_expression_2 on
5036 any field position. Do this for both global and local types.
5037 Skip any fields that we haven't made trees for to avoid problems with
5038 class wide types. */
5039 if (IN (kind
, Record_Kind
))
5040 for (gnat_temp
= First_Entity (gnat_entity
); Present (gnat_temp
);
5041 gnat_temp
= Next_Entity (gnat_temp
))
5042 if (Ekind (gnat_temp
) == E_Component
&& present_gnu_tree (gnat_temp
))
5044 tree gnu_field
= get_gnu_tree (gnat_temp
);
5046 /* ??? For now, store the offset as a multiple of the alignment
5047 in bytes so that we can see the alignment from the tree. */
5048 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field
)))
5050 DECL_FIELD_OFFSET (gnu_field
)
5051 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field
),
5053 get_identifier ("OFFSET"),
5055 DECL_OFFSET_ALIGN (gnu_field
));
5057 /* ??? The context of gnu_field is not necessarily gnu_type
5058 so the MULT_EXPR node built above may not be marked by
5059 the call to create_type_decl below. */
5060 if (global_bindings_p ())
5061 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field
));
5065 if (Treat_As_Volatile (gnat_entity
))
5067 = build_qualified_type (gnu_type
,
5068 TYPE_QUALS (gnu_type
) | TYPE_QUAL_VOLATILE
);
5070 if (Is_Atomic (gnat_entity
))
5071 check_ok_for_atomic (gnu_type
, gnat_entity
, false);
5073 if (Present (Alignment_Clause (gnat_entity
)))
5074 TYPE_USER_ALIGN (gnu_type
) = 1;
5076 if (Universal_Aliasing (gnat_entity
))
5077 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type
)) = 1;
5080 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
5081 !Comes_From_Source (gnat_entity
),
5082 debug_info_p
, gnat_entity
);
5085 TREE_TYPE (gnu_decl
) = gnu_type
;
5086 TYPE_STUB_DECL (gnu_type
) = gnu_decl
;
5090 if (is_type
&& !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
)))
5092 gnu_type
= TREE_TYPE (gnu_decl
);
5094 /* If this is a derived type, relate its alias set to that of its parent
5095 to avoid troubles when a call to an inherited primitive is inlined in
5096 a context where a derived object is accessed. The inlined code works
5097 on the parent view so the resulting code may access the same object
5098 using both the parent and the derived alias sets, which thus have to
5099 conflict. As the same issue arises with component references, the
5100 parent alias set also has to conflict with composite types enclosing
5101 derived components. For instance, if we have:
5108 we want T to conflict with both D and R, in addition to R being a
5109 superset of D by record/component construction.
5111 One way to achieve this is to perform an alias set copy from the
5112 parent to the derived type. This is not quite appropriate, though,
5113 as we don't want separate derived types to conflict with each other:
5115 type I1 is new Integer;
5116 type I2 is new Integer;
5118 We want I1 and I2 to both conflict with Integer but we do not want
5119 I1 to conflict with I2, and an alias set copy on derivation would
5122 The option chosen is to make the alias set of the derived type a
5123 superset of that of its parent type. It trivially fulfills the
5124 simple requirement for the Integer derivation example above, and
5125 the component case as well by superset transitivity:
5128 R ----------> D ----------> T
5130 However, for composite types, conversions between derived types are
5131 translated into VIEW_CONVERT_EXPRs so a sequence like:
5133 type Comp1 is new Comp;
5134 type Comp2 is new Comp;
5135 procedure Proc (C : Comp1);
5143 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5145 and gimplified into:
5152 i.e. generates code involving type punning. Therefore, Comp1 needs
5153 to conflict with Comp2 and an alias set copy is required.
5155 The language rules ensure the parent type is already frozen here. */
5156 if (Is_Derived_Type (gnat_entity
))
5158 tree gnu_parent_type
= gnat_to_gnu_type (Etype (gnat_entity
));
5159 relate_alias_sets (gnu_type
, gnu_parent_type
,
5160 Is_Composite_Type (gnat_entity
)
5161 ? ALIAS_SET_COPY
: ALIAS_SET_SUPERSET
);
5164 /* Back-annotate the Alignment of the type if not already in the
5165 tree. Likewise for sizes. */
5166 if (Unknown_Alignment (gnat_entity
))
5168 unsigned int double_align
, align
;
5169 bool is_capped_double
, align_clause
;
5171 /* If the default alignment of "double" or larger scalar types is
5172 specifically capped and this is not an array with an alignment
5173 clause on the component type, return the cap. */
5174 if ((double_align
= double_float_alignment
) > 0)
5176 = is_double_float_or_array (gnat_entity
, &align_clause
);
5177 else if ((double_align
= double_scalar_alignment
) > 0)
5179 = is_double_scalar_or_array (gnat_entity
, &align_clause
);
5181 is_capped_double
= align_clause
= false;
5183 if (is_capped_double
&& !align_clause
)
5184 align
= double_align
;
5186 align
= TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
;
5188 Set_Alignment (gnat_entity
, UI_From_Int (align
));
5191 if (Unknown_Esize (gnat_entity
) && TYPE_SIZE (gnu_type
))
5193 tree gnu_size
= TYPE_SIZE (gnu_type
);
5195 /* If the size is self-referential, annotate the maximum value. */
5196 if (CONTAINS_PLACEHOLDER_P (gnu_size
))
5197 gnu_size
= max_size (gnu_size
, true);
5199 /* If we are just annotating types and the type is tagged, the tag
5200 and the parent components are not generated by the front-end so
5201 sizes must be adjusted if there is no representation clause. */
5202 if (type_annotate_only
5203 && Is_Tagged_Type (gnat_entity
)
5204 && !VOID_TYPE_P (gnu_type
)
5205 && (!TYPE_FIELDS (gnu_type
)
5206 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type
)))))
5208 tree pointer_size
= bitsize_int (POINTER_SIZE
), offset
;
5211 if (Is_Derived_Type (gnat_entity
))
5213 Entity_Id gnat_parent
= Etype (Base_Type (gnat_entity
));
5214 offset
= UI_To_gnu (Esize (gnat_parent
), bitsizetype
);
5215 Set_Alignment (gnat_entity
, Alignment (gnat_parent
));
5218 offset
= pointer_size
;
5220 if (TYPE_FIELDS (gnu_type
))
5222 = round_up (offset
, DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
5224 gnu_size
= size_binop (PLUS_EXPR
, gnu_size
, offset
);
5225 gnu_size
= round_up (gnu_size
, POINTER_SIZE
);
5226 uint_size
= annotate_value (gnu_size
);
5227 Set_Esize (gnat_entity
, uint_size
);
5228 Set_RM_Size (gnat_entity
, uint_size
);
5231 Set_Esize (gnat_entity
, annotate_value (gnu_size
));
5234 if (Unknown_RM_Size (gnat_entity
) && rm_size (gnu_type
))
5235 Set_RM_Size (gnat_entity
, annotate_value (rm_size (gnu_type
)));
5238 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5239 cannot do so if we are reusing the ..._DECL node made for an equivalent
5240 type or an alias or a renamed object as the predicates don't apply to it
5241 but to GNAT_ENTITY. */
5242 if (DECL_P (gnu_decl
)
5243 && !(is_type
&& gnat_equiv_type
!= gnat_entity
)
5244 && !Present (Alias (gnat_entity
))
5245 && !(Present (Renamed_Object (gnat_entity
)) && saved
))
5247 if (!Comes_From_Source (gnat_entity
))
5248 DECL_ARTIFICIAL (gnu_decl
) = 1;
5251 DECL_IGNORED_P (gnu_decl
) = 1;
5254 /* If we haven't already, associate the ..._DECL node that we just made with
5255 the input GNAT entity node. */
5257 save_gnu_tree (gnat_entity
, gnu_decl
, false);
5259 /* If this is an enumeration or floating-point type, we were not able to set
5260 the bounds since they refer to the type. These are always static. */
5261 if ((kind
== E_Enumeration_Type
&& Present (First_Literal (gnat_entity
)))
5262 || (kind
== E_Floating_Point_Type
&& !Vax_Float (gnat_entity
)))
5264 tree gnu_scalar_type
= gnu_type
;
5265 tree gnu_low_bound
, gnu_high_bound
;
5267 /* If this is a padded type, we need to use the underlying type. */
5268 if (TYPE_IS_PADDING_P (gnu_scalar_type
))
5269 gnu_scalar_type
= TREE_TYPE (TYPE_FIELDS (gnu_scalar_type
));
5271 /* If this is a floating point type and we haven't set a floating
5272 point type yet, use this in the evaluation of the bounds. */
5273 if (!longest_float_type_node
&& kind
== E_Floating_Point_Type
)
5274 longest_float_type_node
= gnu_scalar_type
;
5276 gnu_low_bound
= gnat_to_gnu (Type_Low_Bound (gnat_entity
));
5277 gnu_high_bound
= gnat_to_gnu (Type_High_Bound (gnat_entity
));
5279 if (kind
== E_Enumeration_Type
)
5281 /* Enumeration types have specific RM bounds. */
5282 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type
, gnu_low_bound
);
5283 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type
, gnu_high_bound
);
5287 /* Floating-point types don't have specific RM bounds. */
5288 TYPE_GCC_MIN_VALUE (gnu_scalar_type
) = gnu_low_bound
;
5289 TYPE_GCC_MAX_VALUE (gnu_scalar_type
) = gnu_high_bound
;
5293 /* If we deferred processing of incomplete types, re-enable it. If there
5294 were no other disables and we have deferred types to process, do so. */
5296 && --defer_incomplete_level
== 0
5297 && defer_incomplete_list
)
5299 struct incomplete
*p
, *next
;
5301 /* We are back to level 0 for the deferring of incomplete types.
5302 But processing these incomplete types below may itself require
5303 deferring, so preserve what we have and restart from scratch. */
5304 p
= defer_incomplete_list
;
5305 defer_incomplete_list
= NULL
;
5312 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5313 gnat_to_gnu_type (p
->full_type
));
5318 /* If we are not defining this type, see if it's on one of the lists of
5319 incomplete types. If so, handle the list entry now. */
5320 if (is_type
&& !definition
)
5322 struct incomplete
*p
;
5324 for (p
= defer_incomplete_list
; p
; p
= p
->next
)
5325 if (p
->old_type
&& p
->full_type
== gnat_entity
)
5327 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5328 TREE_TYPE (gnu_decl
));
5329 p
->old_type
= NULL_TREE
;
5332 for (p
= defer_limited_with
; p
; p
= p
->next
)
5333 if (p
->old_type
&& Non_Limited_View (p
->full_type
) == gnat_entity
)
5335 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5336 TREE_TYPE (gnu_decl
));
5337 p
->old_type
= NULL_TREE
;
5344 /* If this is a packed array type whose original array type is itself
5345 an Itype without freeze node, make sure the latter is processed. */
5346 if (Is_Packed_Array_Type (gnat_entity
)
5347 && Is_Itype (Original_Array_Type (gnat_entity
))
5348 && No (Freeze_Node (Original_Array_Type (gnat_entity
)))
5349 && !present_gnu_tree (Original_Array_Type (gnat_entity
)))
5350 gnat_to_gnu_entity (Original_Array_Type (gnat_entity
), NULL_TREE
, 0);
5355 /* Similar, but if the returned value is a COMPONENT_REF, return the
5359 gnat_to_gnu_field_decl (Entity_Id gnat_entity
)
5361 tree gnu_field
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
5363 if (TREE_CODE (gnu_field
) == COMPONENT_REF
)
5364 gnu_field
= TREE_OPERAND (gnu_field
, 1);
5369 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5370 the GCC type corresponding to that entity. */
5373 gnat_to_gnu_type (Entity_Id gnat_entity
)
5377 /* The back end never attempts to annotate generic types. */
5378 if (Is_Generic_Type (gnat_entity
) && type_annotate_only
)
5379 return void_type_node
;
5381 gnu_decl
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
5382 gcc_assert (TREE_CODE (gnu_decl
) == TYPE_DECL
);
5384 return TREE_TYPE (gnu_decl
);
5387 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5388 the unpadded version of the GCC type corresponding to that entity. */
5391 get_unpadded_type (Entity_Id gnat_entity
)
5393 tree type
= gnat_to_gnu_type (gnat_entity
);
5395 if (TYPE_IS_PADDING_P (type
))
5396 type
= TREE_TYPE (TYPE_FIELDS (type
));
5401 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5402 type has been changed to that of the parameterless procedure, except if an
5403 alias is already present, in which case it is returned instead. */
5406 get_minimal_subprog_decl (Entity_Id gnat_entity
)
5408 tree gnu_entity_name
, gnu_ext_name
;
5409 struct attrib
*attr_list
= NULL
;
5411 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5412 of the handling applied here. */
5414 while (Present (Alias (gnat_entity
)))
5416 gnat_entity
= Alias (gnat_entity
);
5417 if (present_gnu_tree (gnat_entity
))
5418 return get_gnu_tree (gnat_entity
);
5421 gnu_entity_name
= get_entity_name (gnat_entity
);
5422 gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
5424 if (Has_Stdcall_Convention (gnat_entity
))
5425 prepend_one_attribute_to (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
5426 get_identifier ("stdcall"), NULL_TREE
,
5428 else if (Has_Thiscall_Convention (gnat_entity
))
5429 prepend_one_attribute_to (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
5430 get_identifier ("thiscall"), NULL_TREE
,
5433 if (No (Interface_Name (gnat_entity
)) && gnu_ext_name
== gnu_entity_name
)
5434 gnu_ext_name
= NULL_TREE
;
5437 create_subprog_decl (gnu_entity_name
, gnu_ext_name
, void_ftype
, NULL_TREE
,
5438 is_disabled
, true, true, true, attr_list
, gnat_entity
);
5441 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5442 a C++ imported method or equivalent.
5444 We use the predicate on 32-bit x86/Windows to find out whether we need to
5445 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5446 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5449 is_cplusplus_method (Entity_Id gnat_entity
)
5451 if (Convention (gnat_entity
) != Convention_CPP
)
5454 /* This is the main case: C++ method imported as a primitive operation. */
5455 if (Is_Dispatching_Operation (gnat_entity
))
5458 /* A thunk needs to be handled like its associated primitive operation. */
5459 if (Is_Subprogram (gnat_entity
) && Is_Thunk (gnat_entity
))
5462 /* C++ classes with no virtual functions can be imported as limited
5463 record types, but we need to return true for the constructors. */
5464 if (Is_Constructor (gnat_entity
))
5467 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5468 if (Is_Dispatch_Table_Entity (gnat_entity
))
5474 /* Finalize the processing of From_With_Type incomplete types. */
5477 finalize_from_with_types (void)
5479 struct incomplete
*p
, *next
;
5481 p
= defer_limited_with
;
5482 defer_limited_with
= NULL
;
5489 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5490 gnat_to_gnu_type (p
->full_type
));
5495 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5496 kind of type (such E_Task_Type) that has a different type which Gigi
5497 uses for its representation. If the type does not have a special type
5498 for its representation, return GNAT_ENTITY. If a type is supposed to
5499 exist, but does not, abort unless annotating types, in which case
5500 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5503 Gigi_Equivalent_Type (Entity_Id gnat_entity
)
5505 Entity_Id gnat_equiv
= gnat_entity
;
5507 if (No (gnat_entity
))
5510 switch (Ekind (gnat_entity
))
5512 case E_Class_Wide_Subtype
:
5513 if (Present (Equivalent_Type (gnat_entity
)))
5514 gnat_equiv
= Equivalent_Type (gnat_entity
);
5517 case E_Access_Protected_Subprogram_Type
:
5518 case E_Anonymous_Access_Protected_Subprogram_Type
:
5519 gnat_equiv
= Equivalent_Type (gnat_entity
);
5522 case E_Class_Wide_Type
:
5523 gnat_equiv
= Root_Type (gnat_entity
);
5527 case E_Task_Subtype
:
5528 case E_Protected_Type
:
5529 case E_Protected_Subtype
:
5530 gnat_equiv
= Corresponding_Record_Type (gnat_entity
);
5537 gcc_assert (Present (gnat_equiv
) || type_annotate_only
);
5542 /* Return a GCC tree for a type corresponding to the component type of the
5543 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5544 is for an array being defined. DEBUG_INFO_P is true if we need to write
5545 debug information for other types that we may create in the process. */
5548 gnat_to_gnu_component_type (Entity_Id gnat_array
, bool definition
,
5551 const Entity_Id gnat_type
= Component_Type (gnat_array
);
5552 tree gnu_type
= gnat_to_gnu_type (gnat_type
);
5555 /* Try to get a smaller form of the component if needed. */
5556 if ((Is_Packed (gnat_array
)
5557 || Has_Component_Size_Clause (gnat_array
))
5558 && !Is_Bit_Packed_Array (gnat_array
)
5559 && !Has_Aliased_Components (gnat_array
)
5560 && !Strict_Alignment (gnat_type
)
5561 && RECORD_OR_UNION_TYPE_P (gnu_type
)
5562 && !TYPE_FAT_POINTER_P (gnu_type
)
5563 && host_integerp (TYPE_SIZE (gnu_type
), 1))
5564 gnu_type
= make_packable_type (gnu_type
, false);
5566 if (Has_Atomic_Components (gnat_array
))
5567 check_ok_for_atomic (gnu_type
, gnat_array
, true);
5569 /* Get and validate any specified Component_Size. */
5571 = validate_size (Component_Size (gnat_array
), gnu_type
, gnat_array
,
5572 Is_Bit_Packed_Array (gnat_array
) ? TYPE_DECL
: VAR_DECL
,
5573 true, Has_Component_Size_Clause (gnat_array
));
5575 /* If the array has aliased components and the component size can be zero,
5576 force at least unit size to ensure that the components have distinct
5579 && Has_Aliased_Components (gnat_array
)
5580 && (integer_zerop (TYPE_SIZE (gnu_type
))
5581 || (TREE_CODE (gnu_type
) == ARRAY_TYPE
5582 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
)))))
5584 = size_binop (MAX_EXPR
, TYPE_SIZE (gnu_type
), bitsize_unit_node
);
5586 /* If the component type is a RECORD_TYPE that has a self-referential size,
5587 then use the maximum size for the component size. */
5589 && TREE_CODE (gnu_type
) == RECORD_TYPE
5590 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
5591 gnu_comp_size
= max_size (TYPE_SIZE (gnu_type
), true);
5593 /* Honor the component size. This is not needed for bit-packed arrays. */
5594 if (gnu_comp_size
&& !Is_Bit_Packed_Array (gnat_array
))
5596 tree orig_type
= gnu_type
;
5597 unsigned int max_align
;
5599 /* If an alignment is specified, use it as a cap on the component type
5600 so that it can be honored for the whole type. But ignore it for the
5601 original type of packed array types. */
5602 if (No (Packed_Array_Type (gnat_array
)) && Known_Alignment (gnat_array
))
5603 max_align
= validate_alignment (Alignment (gnat_array
), gnat_array
, 0);
5607 gnu_type
= make_type_from_size (gnu_type
, gnu_comp_size
, false);
5608 if (max_align
> 0 && TYPE_ALIGN (gnu_type
) > max_align
)
5609 gnu_type
= orig_type
;
5611 orig_type
= gnu_type
;
5613 gnu_type
= maybe_pad_type (gnu_type
, gnu_comp_size
, 0, gnat_array
,
5614 true, false, definition
, true);
5616 /* If a padding record was made, declare it now since it will never be
5617 declared otherwise. This is necessary to ensure that its subtrees
5618 are properly marked. */
5619 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
5620 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true, debug_info_p
,
5624 if (Has_Volatile_Components (gnat_array
))
5626 = build_qualified_type (gnu_type
,
5627 TYPE_QUALS (gnu_type
) | TYPE_QUAL_VOLATILE
);
5632 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5633 using MECH as its passing mechanism, to be placed in the parameter
5634 list built for GNAT_SUBPROG. Assume a foreign convention for the
5635 latter if FOREIGN is true. Also set CICO to true if the parameter
5636 must use the copy-in copy-out implementation mechanism.
5638 The returned tree is a PARM_DECL, except for those cases where no
5639 parameter needs to be actually passed to the subprogram; the type
5640 of this "shadow" parameter is then returned instead. */
5643 gnat_to_gnu_param (Entity_Id gnat_param
, Mechanism_Type mech
,
5644 Entity_Id gnat_subprog
, bool foreign
, bool *cico
)
5646 tree gnu_param_name
= get_entity_name (gnat_param
);
5647 tree gnu_param_type
= gnat_to_gnu_type (Etype (gnat_param
));
5648 tree gnu_param_type_alt
= NULL_TREE
;
5649 bool in_param
= (Ekind (gnat_param
) == E_In_Parameter
);
5650 /* The parameter can be indirectly modified if its address is taken. */
5651 bool ro_param
= in_param
&& !Address_Taken (gnat_param
);
5652 bool by_return
= false, by_component_ptr
= false;
5653 bool by_ref
= false;
5656 /* Copy-return is used only for the first parameter of a valued procedure.
5657 It's a copy mechanism for which a parameter is never allocated. */
5658 if (mech
== By_Copy_Return
)
5660 gcc_assert (Ekind (gnat_param
) == E_Out_Parameter
);
5665 /* If this is either a foreign function or if the underlying type won't
5666 be passed by reference, strip off possible padding type. */
5667 if (TYPE_IS_PADDING_P (gnu_param_type
))
5669 tree unpadded_type
= TREE_TYPE (TYPE_FIELDS (gnu_param_type
));
5671 if (mech
== By_Reference
5673 || (!must_pass_by_ref (unpadded_type
)
5674 && (mech
== By_Copy
|| !default_pass_by_ref (unpadded_type
))))
5675 gnu_param_type
= unpadded_type
;
5678 /* If this is a read-only parameter, make a variant of the type that is
5679 read-only. ??? However, if this is an unconstrained array, that type
5680 can be very complex, so skip it for now. Likewise for any other
5681 self-referential type. */
5683 && TREE_CODE (gnu_param_type
) != UNCONSTRAINED_ARRAY_TYPE
5684 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type
)))
5685 gnu_param_type
= build_qualified_type (gnu_param_type
,
5686 (TYPE_QUALS (gnu_param_type
)
5687 | TYPE_QUAL_CONST
));
5689 /* For foreign conventions, pass arrays as pointers to the element type.
5690 First check for unconstrained array and get the underlying array. */
5691 if (foreign
&& TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5693 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type
))));
5695 /* For GCC builtins, pass Address integer types as (void *) */
5696 if (Convention (gnat_subprog
) == Convention_Intrinsic
5697 && Present (Interface_Name (gnat_subprog
))
5698 && Is_Descendent_Of_Address (Etype (gnat_param
)))
5699 gnu_param_type
= ptr_void_type_node
;
5701 /* VMS descriptors are themselves passed by reference. */
5702 if (mech
== By_Short_Descriptor
||
5703 (mech
== By_Descriptor
&& TARGET_ABI_OPEN_VMS
&& !flag_vms_malloc64
))
5705 = build_pointer_type (build_vms_descriptor32 (gnu_param_type
,
5706 Mechanism (gnat_param
),
5708 else if (mech
== By_Descriptor
)
5710 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5711 chosen in fill_vms_descriptor. */
5713 = build_pointer_type (build_vms_descriptor32 (gnu_param_type
,
5714 Mechanism (gnat_param
),
5717 = build_pointer_type (build_vms_descriptor (gnu_param_type
,
5718 Mechanism (gnat_param
),
5722 /* Arrays are passed as pointers to element type for foreign conventions. */
5725 && TREE_CODE (gnu_param_type
) == ARRAY_TYPE
)
5727 /* Strip off any multi-dimensional entries, then strip
5728 off the last array to get the component type. */
5729 while (TREE_CODE (TREE_TYPE (gnu_param_type
)) == ARRAY_TYPE
5730 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type
)))
5731 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5733 by_component_ptr
= true;
5734 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5737 gnu_param_type
= build_qualified_type (gnu_param_type
,
5738 (TYPE_QUALS (gnu_param_type
)
5739 | TYPE_QUAL_CONST
));
5741 gnu_param_type
= build_pointer_type (gnu_param_type
);
5744 /* Fat pointers are passed as thin pointers for foreign conventions. */
5745 else if (foreign
&& TYPE_IS_FAT_POINTER_P (gnu_param_type
))
5747 = make_type_from_size (gnu_param_type
, size_int (POINTER_SIZE
), 0);
5749 /* If we must pass or were requested to pass by reference, do so.
5750 If we were requested to pass by copy, do so.
5751 Otherwise, for foreign conventions, pass In Out or Out parameters
5752 or aggregates by reference. For COBOL and Fortran, pass all
5753 integer and FP types that way too. For Convention Ada, use
5754 the standard Ada default. */
5755 else if (must_pass_by_ref (gnu_param_type
)
5756 || mech
== By_Reference
5759 && (!in_param
|| AGGREGATE_TYPE_P (gnu_param_type
)))
5761 && (Convention (gnat_subprog
) == Convention_Fortran
5762 || Convention (gnat_subprog
) == Convention_COBOL
)
5763 && (INTEGRAL_TYPE_P (gnu_param_type
)
5764 || FLOAT_TYPE_P (gnu_param_type
)))
5766 && default_pass_by_ref (gnu_param_type
)))))
5768 /* We take advantage of 6.2(12) by considering that references built for
5769 parameters whose type isn't by-ref and for which the mechanism hasn't
5770 been forced to by-ref are restrict-qualified in the C sense. */
5772 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type
) && mech
!= By_Reference
;
5773 gnu_param_type
= build_reference_type (gnu_param_type
);
5776 = build_qualified_type (gnu_param_type
, TYPE_QUAL_RESTRICT
);
5780 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5784 if (mech
== By_Copy
&& (by_ref
|| by_component_ptr
))
5785 post_error ("?cannot pass & by copy", gnat_param
);
5787 /* If this is an Out parameter that isn't passed by reference and isn't
5788 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5789 it will be a VAR_DECL created when we process the procedure, so just
5790 return its type. For the special parameter of a valued procedure,
5793 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5794 Out parameters with discriminants or implicit initial values to be
5795 handled like In Out parameters. These type are normally built as
5796 aggregates, hence passed by reference, except for some packed arrays
5797 which end up encoded in special integer types.
5799 The exception we need to make is then for packed arrays of records
5800 with discriminants or implicit initial values. We have no light/easy
5801 way to check for the latter case, so we merely check for packed arrays
5802 of records. This may lead to useless copy-in operations, but in very
5803 rare cases only, as these would be exceptions in a set of already
5804 exceptional situations. */
5805 if (Ekind (gnat_param
) == E_Out_Parameter
5808 || (mech
!= By_Descriptor
5809 && mech
!= By_Short_Descriptor
5810 && !POINTER_TYPE_P (gnu_param_type
)
5811 && !AGGREGATE_TYPE_P (gnu_param_type
)))
5812 && !(Is_Array_Type (Etype (gnat_param
))
5813 && Is_Packed (Etype (gnat_param
))
5814 && Is_Composite_Type (Component_Type (Etype (gnat_param
)))))
5815 return gnu_param_type
;
5817 gnu_param
= create_param_decl (gnu_param_name
, gnu_param_type
,
5818 ro_param
|| by_ref
|| by_component_ptr
);
5819 DECL_BY_REF_P (gnu_param
) = by_ref
;
5820 DECL_BY_COMPONENT_PTR_P (gnu_param
) = by_component_ptr
;
5821 DECL_BY_DESCRIPTOR_P (gnu_param
) = (mech
== By_Descriptor
||
5822 mech
== By_Short_Descriptor
);
5823 /* Note that, in case of a parameter passed by double reference, the
5824 DECL_POINTS_TO_READONLY_P flag is meant for the second reference.
5825 The first reference always points to read-only, as it points to
5826 the second reference, i.e. the reference to the actual parameter. */
5827 DECL_POINTS_TO_READONLY_P (gnu_param
)
5828 = (ro_param
&& (by_ref
|| by_component_ptr
));
5829 DECL_CAN_NEVER_BE_NULL_P (gnu_param
) = Can_Never_Be_Null (gnat_param
);
5831 /* Save the alternate descriptor type, if any. */
5832 if (gnu_param_type_alt
)
5833 SET_DECL_PARM_ALT_TYPE (gnu_param
, gnu_param_type_alt
);
5835 /* If no Mechanism was specified, indicate what we're using, then
5836 back-annotate it. */
5837 if (mech
== Default
)
5838 mech
= (by_ref
|| by_component_ptr
) ? By_Reference
: By_Copy
;
5840 Set_Mechanism (gnat_param
, mech
);
5844 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5847 same_discriminant_p (Entity_Id discr1
, Entity_Id discr2
)
5849 while (Present (Corresponding_Discriminant (discr1
)))
5850 discr1
= Corresponding_Discriminant (discr1
);
5852 while (Present (Corresponding_Discriminant (discr2
)))
5853 discr2
= Corresponding_Discriminant (discr2
);
5856 Original_Record_Component (discr1
) == Original_Record_Component (discr2
);
5859 /* Return true if the array type GNU_TYPE, which represents a dimension of
5860 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5863 array_type_has_nonaliased_component (tree gnu_type
, Entity_Id gnat_type
)
5865 /* If the array type is not the innermost dimension of the GNAT type,
5866 then it has a non-aliased component. */
5867 if (TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
5868 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
)))
5871 /* If the array type has an aliased component in the front-end sense,
5872 then it also has an aliased component in the back-end sense. */
5873 if (Has_Aliased_Components (gnat_type
))
5876 /* If this is a derived type, then it has a non-aliased component if
5877 and only if its parent type also has one. */
5878 if (Is_Derived_Type (gnat_type
))
5880 tree gnu_parent_type
= gnat_to_gnu_type (Etype (gnat_type
));
5882 if (TREE_CODE (gnu_parent_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5884 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type
))));
5885 for (index
= Number_Dimensions (gnat_type
) - 1; index
> 0; index
--)
5886 gnu_parent_type
= TREE_TYPE (gnu_parent_type
);
5887 return TYPE_NONALIASED_COMPONENT (gnu_parent_type
);
5890 /* Otherwise, rely exclusively on properties of the element type. */
5891 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type
));
5894 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5897 compile_time_known_address_p (Node_Id gnat_address
)
5899 /* Catch System'To_Address. */
5900 if (Nkind (gnat_address
) == N_Unchecked_Type_Conversion
)
5901 gnat_address
= Expression (gnat_address
);
5903 return Compile_Time_Known_Value (gnat_address
);
5906 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5907 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5910 cannot_be_superflat_p (Node_Id gnat_range
)
5912 Node_Id gnat_lb
= Low_Bound (gnat_range
), gnat_hb
= High_Bound (gnat_range
);
5913 Node_Id scalar_range
;
5914 tree gnu_lb
, gnu_hb
, gnu_lb_minus_one
;
5916 /* If the low bound is not constant, try to find an upper bound. */
5917 while (Nkind (gnat_lb
) != N_Integer_Literal
5918 && (Ekind (Etype (gnat_lb
)) == E_Signed_Integer_Subtype
5919 || Ekind (Etype (gnat_lb
)) == E_Modular_Integer_Subtype
)
5920 && (scalar_range
= Scalar_Range (Etype (gnat_lb
)))
5921 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
5922 || Nkind (scalar_range
) == N_Range
))
5923 gnat_lb
= High_Bound (scalar_range
);
5925 /* If the high bound is not constant, try to find a lower bound. */
5926 while (Nkind (gnat_hb
) != N_Integer_Literal
5927 && (Ekind (Etype (gnat_hb
)) == E_Signed_Integer_Subtype
5928 || Ekind (Etype (gnat_hb
)) == E_Modular_Integer_Subtype
)
5929 && (scalar_range
= Scalar_Range (Etype (gnat_hb
)))
5930 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
5931 || Nkind (scalar_range
) == N_Range
))
5932 gnat_hb
= Low_Bound (scalar_range
);
5934 /* If we have failed to find constant bounds, punt. */
5935 if (Nkind (gnat_lb
) != N_Integer_Literal
5936 || Nkind (gnat_hb
) != N_Integer_Literal
)
5939 /* We need at least a signed 64-bit type to catch most cases. */
5940 gnu_lb
= UI_To_gnu (Intval (gnat_lb
), sbitsizetype
);
5941 gnu_hb
= UI_To_gnu (Intval (gnat_hb
), sbitsizetype
);
5942 if (TREE_OVERFLOW (gnu_lb
) || TREE_OVERFLOW (gnu_hb
))
5945 /* If the low bound is the smallest integer, nothing can be smaller. */
5946 gnu_lb_minus_one
= size_binop (MINUS_EXPR
, gnu_lb
, sbitsize_one_node
);
5947 if (TREE_OVERFLOW (gnu_lb_minus_one
))
5950 return !tree_int_cst_lt (gnu_hb
, gnu_lb_minus_one
);
5953 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5956 constructor_address_p (tree gnu_expr
)
5958 while (TREE_CODE (gnu_expr
) == NOP_EXPR
5959 || TREE_CODE (gnu_expr
) == CONVERT_EXPR
5960 || TREE_CODE (gnu_expr
) == NON_LVALUE_EXPR
)
5961 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
5963 return (TREE_CODE (gnu_expr
) == ADDR_EXPR
5964 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == CONSTRUCTOR
);
5967 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5968 be elaborated at the point of its definition, but do nothing else. */
5971 elaborate_entity (Entity_Id gnat_entity
)
5973 switch (Ekind (gnat_entity
))
5975 case E_Signed_Integer_Subtype
:
5976 case E_Modular_Integer_Subtype
:
5977 case E_Enumeration_Subtype
:
5978 case E_Ordinary_Fixed_Point_Subtype
:
5979 case E_Decimal_Fixed_Point_Subtype
:
5980 case E_Floating_Point_Subtype
:
5982 Node_Id gnat_lb
= Type_Low_Bound (gnat_entity
);
5983 Node_Id gnat_hb
= Type_High_Bound (gnat_entity
);
5985 /* ??? Tests to avoid Constraint_Error in static expressions
5986 are needed until after the front stops generating bogus
5987 conversions on bounds of real types. */
5988 if (!Raises_Constraint_Error (gnat_lb
))
5989 elaborate_expression (gnat_lb
, gnat_entity
, get_identifier ("L"),
5990 true, false, Needs_Debug_Info (gnat_entity
));
5991 if (!Raises_Constraint_Error (gnat_hb
))
5992 elaborate_expression (gnat_hb
, gnat_entity
, get_identifier ("U"),
5993 true, false, Needs_Debug_Info (gnat_entity
));
5997 case E_Record_Subtype
:
5998 case E_Private_Subtype
:
5999 case E_Limited_Private_Subtype
:
6000 case E_Record_Subtype_With_Private
:
6001 if (Has_Discriminants (gnat_entity
) && Is_Constrained (gnat_entity
))
6003 Node_Id gnat_discriminant_expr
;
6004 Entity_Id gnat_field
;
6007 = First_Discriminant (Implementation_Base_Type (gnat_entity
)),
6008 gnat_discriminant_expr
6009 = First_Elmt (Discriminant_Constraint (gnat_entity
));
6010 Present (gnat_field
);
6011 gnat_field
= Next_Discriminant (gnat_field
),
6012 gnat_discriminant_expr
= Next_Elmt (gnat_discriminant_expr
))
6013 /* Ignore access discriminants. */
6014 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr
))))
6015 elaborate_expression (Node (gnat_discriminant_expr
),
6016 gnat_entity
, get_entity_name (gnat_field
),
6017 true, false, false);
6024 /* Return true if the size in units represented by GNU_SIZE can be handled by
6025 an allocation. If STATIC_P is true, consider only what can be done with a
6026 static allocation. */
6029 allocatable_size_p (tree gnu_size
, bool static_p
)
6031 /* We can allocate a fixed size if it is a valid for the middle-end. */
6032 if (TREE_CODE (gnu_size
) == INTEGER_CST
)
6033 return valid_constant_size_p (gnu_size
);
6035 /* We can allocate a variable size if this isn't a static allocation. */
6040 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6041 NAME, ARGS and ERROR_POINT. */
6044 prepend_one_attribute_to (struct attrib
** attr_list
,
6045 enum attr_type attr_type
,
6048 Node_Id attr_error_point
)
6050 struct attrib
* attr
= (struct attrib
*) xmalloc (sizeof (struct attrib
));
6052 attr
->type
= attr_type
;
6053 attr
->name
= attr_name
;
6054 attr
->args
= attr_args
;
6055 attr
->error_point
= attr_error_point
;
6057 attr
->next
= *attr_list
;
6061 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6064 prepend_attributes (Entity_Id gnat_entity
, struct attrib
** attr_list
)
6068 /* Attributes are stored as Representation Item pragmas. */
6070 for (gnat_temp
= First_Rep_Item (gnat_entity
); Present (gnat_temp
);
6071 gnat_temp
= Next_Rep_Item (gnat_temp
))
6072 if (Nkind (gnat_temp
) == N_Pragma
)
6074 tree gnu_arg0
= NULL_TREE
, gnu_arg1
= NULL_TREE
;
6075 Node_Id gnat_assoc
= Pragma_Argument_Associations (gnat_temp
);
6076 enum attr_type etype
;
6078 /* Map the kind of pragma at hand. Skip if this is not one
6079 we know how to handle. */
6081 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp
))))
6083 case Pragma_Machine_Attribute
:
6084 etype
= ATTR_MACHINE_ATTRIBUTE
;
6087 case Pragma_Linker_Alias
:
6088 etype
= ATTR_LINK_ALIAS
;
6091 case Pragma_Linker_Section
:
6092 etype
= ATTR_LINK_SECTION
;
6095 case Pragma_Linker_Constructor
:
6096 etype
= ATTR_LINK_CONSTRUCTOR
;
6099 case Pragma_Linker_Destructor
:
6100 etype
= ATTR_LINK_DESTRUCTOR
;
6103 case Pragma_Weak_External
:
6104 etype
= ATTR_WEAK_EXTERNAL
;
6107 case Pragma_Thread_Local_Storage
:
6108 etype
= ATTR_THREAD_LOCAL_STORAGE
;
6115 /* See what arguments we have and turn them into GCC trees for
6116 attribute handlers. These expect identifier for strings. We
6117 handle at most two arguments, static expressions only. */
6119 if (Present (gnat_assoc
) && Present (First (gnat_assoc
)))
6121 Node_Id gnat_arg0
= Next (First (gnat_assoc
));
6122 Node_Id gnat_arg1
= Empty
;
6124 if (Present (gnat_arg0
)
6125 && Is_Static_Expression (Expression (gnat_arg0
)))
6127 gnu_arg0
= gnat_to_gnu (Expression (gnat_arg0
));
6129 if (TREE_CODE (gnu_arg0
) == STRING_CST
)
6130 gnu_arg0
= get_identifier (TREE_STRING_POINTER (gnu_arg0
));
6132 gnat_arg1
= Next (gnat_arg0
);
6135 if (Present (gnat_arg1
)
6136 && Is_Static_Expression (Expression (gnat_arg1
)))
6138 gnu_arg1
= gnat_to_gnu (Expression (gnat_arg1
));
6140 if (TREE_CODE (gnu_arg1
) == STRING_CST
)
6141 gnu_arg1
= get_identifier (TREE_STRING_POINTER (gnu_arg1
));
6145 /* Prepend to the list now. Make a list of the argument we might
6146 have, as GCC expects it. */
6147 prepend_one_attribute_to
6150 (gnu_arg1
!= NULL_TREE
)
6151 ? build_tree_list (NULL_TREE
, gnu_arg1
) : NULL_TREE
,
6152 Present (Next (First (gnat_assoc
)))
6153 ? Expression (Next (First (gnat_assoc
))) : gnat_temp
);
6157 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6158 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6159 return the GCC tree to use for that expression. GNU_NAME is the suffix
6160 to use if a variable needs to be created and DEFINITION is true if this
6161 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6162 otherwise, we are just elaborating the expression for side-effects. If
6163 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6164 isn't needed for code generation. */
6167 elaborate_expression (Node_Id gnat_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6168 bool definition
, bool need_value
, bool need_debug
)
6172 /* If we already elaborated this expression (e.g. it was involved
6173 in the definition of a private type), use the old value. */
6174 if (present_gnu_tree (gnat_expr
))
6175 return get_gnu_tree (gnat_expr
);
6177 /* If we don't need a value and this is static or a discriminant,
6178 we don't need to do anything. */
6180 && (Is_OK_Static_Expression (gnat_expr
)
6181 || (Nkind (gnat_expr
) == N_Identifier
6182 && Ekind (Entity (gnat_expr
)) == E_Discriminant
)))
6185 /* If it's a static expression, we don't need a variable for debugging. */
6186 if (need_debug
&& Is_OK_Static_Expression (gnat_expr
))
6189 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6190 gnu_expr
= elaborate_expression_1 (gnat_to_gnu (gnat_expr
), gnat_entity
,
6191 gnu_name
, definition
, need_debug
);
6193 /* Save the expression in case we try to elaborate this entity again. Since
6194 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6195 if (!CONTAINS_PLACEHOLDER_P (gnu_expr
))
6196 save_gnu_tree (gnat_expr
, gnu_expr
, true);
6198 return need_value
? gnu_expr
: error_mark_node
;
6201 /* Similar, but take a GNU expression and always return a result. */
6204 elaborate_expression_1 (tree gnu_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6205 bool definition
, bool need_debug
)
6207 const bool expr_public_p
= Is_Public (gnat_entity
);
6208 const bool expr_global_p
= expr_public_p
|| global_bindings_p ();
6209 bool expr_variable_p
, use_variable
;
6211 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6212 reference will have been replaced with a COMPONENT_REF when the type
6213 is being elaborated. However, there are some cases involving child
6214 types where we will. So convert it to a COMPONENT_REF. We hope it
6215 will be at the highest level of the expression in these cases. */
6216 if (TREE_CODE (gnu_expr
) == FIELD_DECL
)
6217 gnu_expr
= build3 (COMPONENT_REF
, TREE_TYPE (gnu_expr
),
6218 build0 (PLACEHOLDER_EXPR
, DECL_CONTEXT (gnu_expr
)),
6219 gnu_expr
, NULL_TREE
);
6221 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6222 that an expression cannot contain both a discriminant and a variable. */
6223 if (CONTAINS_PLACEHOLDER_P (gnu_expr
))
6226 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6227 a variable that is initialized to contain the expression when the package
6228 containing the definition is elaborated. If this entity is defined at top
6229 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6230 if this is necessary. */
6231 if (CONSTANT_CLASS_P (gnu_expr
))
6232 expr_variable_p
= false;
6235 /* Skip any conversions and simple constant arithmetics to see if the
6236 expression is based on a read-only variable.
6237 ??? This really should remain read-only, but we have to think about
6238 the typing of the tree here. */
6239 tree inner
= remove_conversions (gnu_expr
, true);
6241 inner
= skip_simple_constant_arithmetic (inner
);
6243 if (handled_component_p (inner
))
6245 HOST_WIDE_INT bitsize
, bitpos
;
6247 enum machine_mode mode
;
6248 int unsignedp
, volatilep
;
6250 inner
= get_inner_reference (inner
, &bitsize
, &bitpos
, &offset
,
6251 &mode
, &unsignedp
, &volatilep
, false);
6252 /* If the offset is variable, err on the side of caution. */
6259 && TREE_CODE (inner
) == VAR_DECL
6260 && (TREE_READONLY (inner
) || DECL_READONLY_ONCE_ELAB (inner
)));
6263 /* We only need to use the variable if we are in a global context since GCC
6264 can do the right thing in the local case. However, when not optimizing,
6265 use it for bounds of loop iteration scheme to avoid code duplication. */
6266 use_variable
= expr_variable_p
6270 && Is_Itype (gnat_entity
)
6271 && Nkind (Associated_Node_For_Itype (gnat_entity
))
6272 == N_Loop_Parameter_Specification
));
6274 /* Now create it, possibly only for debugging purposes. */
6275 if (use_variable
|| need_debug
)
6279 (create_concat_name (gnat_entity
, IDENTIFIER_POINTER (gnu_name
)),
6280 NULL_TREE
, TREE_TYPE (gnu_expr
), gnu_expr
, true, expr_public_p
,
6281 !definition
, expr_global_p
, !need_debug
, NULL
, gnat_entity
);
6287 return expr_variable_p
? gnat_save_expr (gnu_expr
) : gnu_expr
;
6290 /* Similar, but take an alignment factor and make it explicit in the tree. */
6293 elaborate_expression_2 (tree gnu_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6294 bool definition
, bool need_debug
, unsigned int align
)
6296 tree unit_align
= size_int (align
/ BITS_PER_UNIT
);
6298 size_binop (MULT_EXPR
,
6299 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR
,
6302 gnat_entity
, gnu_name
, definition
,
6307 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6308 the value passed against the list of choices. */
6311 choices_to_gnu (tree operand
, Node_Id choices
)
6315 tree result
= boolean_false_node
;
6316 tree this_test
, low
= 0, high
= 0, single
= 0;
6318 for (choice
= First (choices
); Present (choice
); choice
= Next (choice
))
6320 switch (Nkind (choice
))
6323 low
= gnat_to_gnu (Low_Bound (choice
));
6324 high
= gnat_to_gnu (High_Bound (choice
));
6327 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6328 build_binary_op (GE_EXPR
, boolean_type_node
,
6330 build_binary_op (LE_EXPR
, boolean_type_node
,
6335 case N_Subtype_Indication
:
6336 gnat_temp
= Range_Expression (Constraint (choice
));
6337 low
= gnat_to_gnu (Low_Bound (gnat_temp
));
6338 high
= gnat_to_gnu (High_Bound (gnat_temp
));
6341 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6342 build_binary_op (GE_EXPR
, boolean_type_node
,
6344 build_binary_op (LE_EXPR
, boolean_type_node
,
6349 case N_Expanded_Name
:
6350 /* This represents either a subtype range, an enumeration
6351 literal, or a constant Ekind says which. If an enumeration
6352 literal or constant, fall through to the next case. */
6353 if (Ekind (Entity (choice
)) != E_Enumeration_Literal
6354 && Ekind (Entity (choice
)) != E_Constant
)
6356 tree type
= gnat_to_gnu_type (Entity (choice
));
6358 low
= TYPE_MIN_VALUE (type
);
6359 high
= TYPE_MAX_VALUE (type
);
6362 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6363 build_binary_op (GE_EXPR
, boolean_type_node
,
6365 build_binary_op (LE_EXPR
, boolean_type_node
,
6370 /* ... fall through ... */
6372 case N_Character_Literal
:
6373 case N_Integer_Literal
:
6374 single
= gnat_to_gnu (choice
);
6375 this_test
= build_binary_op (EQ_EXPR
, boolean_type_node
, operand
,
6379 case N_Others_Choice
:
6380 this_test
= boolean_true_node
;
6387 result
= build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
, result
,
6394 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6395 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6398 adjust_packed (tree field_type
, tree record_type
, int packed
)
6400 /* If the field contains an item of variable size, we cannot pack it
6401 because we cannot create temporaries of non-fixed size in case
6402 we need to take the address of the field. See addressable_p and
6403 the notes on the addressability issues for further details. */
6404 if (type_has_variable_size (field_type
))
6407 /* If the alignment of the record is specified and the field type
6408 is over-aligned, request Storage_Unit alignment for the field. */
6411 if (TYPE_ALIGN (field_type
) > TYPE_ALIGN (record_type
))
6420 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6421 placed in GNU_RECORD_TYPE.
6423 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6424 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6425 record has a specified alignment.
6427 DEFINITION is true if this field is for a record being defined.
6429 DEBUG_INFO_P is true if we need to write debug information for types
6430 that we may create in the process. */
6433 gnat_to_gnu_field (Entity_Id gnat_field
, tree gnu_record_type
, int packed
,
6434 bool definition
, bool debug_info_p
)
6436 const Entity_Id gnat_field_type
= Etype (gnat_field
);
6437 tree gnu_field_type
= gnat_to_gnu_type (gnat_field_type
);
6438 tree gnu_field_id
= get_entity_name (gnat_field
);
6439 tree gnu_field
, gnu_size
, gnu_pos
;
6441 = (Treat_As_Volatile (gnat_field
) || Treat_As_Volatile (gnat_field_type
));
6442 bool needs_strict_alignment
6444 || Is_Aliased (gnat_field
)
6445 || Strict_Alignment (gnat_field_type
));
6447 /* If this field requires strict alignment, we cannot pack it because
6448 it would very likely be under-aligned in the record. */
6449 if (needs_strict_alignment
)
6452 packed
= adjust_packed (gnu_field_type
, gnu_record_type
, packed
);
6454 /* If a size is specified, use it. Otherwise, if the record type is packed,
6455 use the official RM size. See "Handling of Type'Size Values" in Einfo
6456 for further details. */
6457 if (Known_Esize (gnat_field
))
6458 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6459 gnat_field
, FIELD_DECL
, false, true);
6460 else if (packed
== 1)
6461 gnu_size
= validate_size (RM_Size (gnat_field_type
), gnu_field_type
,
6462 gnat_field
, FIELD_DECL
, false, true);
6464 gnu_size
= NULL_TREE
;
6466 /* If we have a specified size that is smaller than that of the field's type,
6467 or a position is specified, and the field's type is a record that doesn't
6468 require strict alignment, see if we can get either an integral mode form
6469 of the type or a smaller form. If we can, show a size was specified for
6470 the field if there wasn't one already, so we know to make this a bitfield
6471 and avoid making things wider.
6473 Changing to an integral mode form is useful when the record is packed as
6474 we can then place the field at a non-byte-aligned position and so achieve
6475 tighter packing. This is in addition required if the field shares a byte
6476 with another field and the front-end lets the back-end handle the access
6477 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6479 Changing to a smaller form is required if the specified size is smaller
6480 than that of the field's type and the type contains sub-fields that are
6481 padded, in order to avoid generating accesses to these sub-fields that
6482 are wider than the field.
6484 We avoid the transformation if it is not required or potentially useful,
6485 as it might entail an increase of the field's alignment and have ripple
6486 effects on the outer record type. A typical case is a field known to be
6487 byte-aligned and not to share a byte with another field. */
6488 if (!needs_strict_alignment
6489 && RECORD_OR_UNION_TYPE_P (gnu_field_type
)
6490 && !TYPE_FAT_POINTER_P (gnu_field_type
)
6491 && host_integerp (TYPE_SIZE (gnu_field_type
), 1)
6494 && (tree_int_cst_lt (gnu_size
, TYPE_SIZE (gnu_field_type
))
6495 || (Present (Component_Clause (gnat_field
))
6496 && !(UI_To_Int (Component_Bit_Offset (gnat_field
))
6497 % BITS_PER_UNIT
== 0
6498 && value_factor_p (gnu_size
, BITS_PER_UNIT
)))))))
6500 tree gnu_packable_type
= make_packable_type (gnu_field_type
, true);
6501 if (gnu_packable_type
!= gnu_field_type
)
6503 gnu_field_type
= gnu_packable_type
;
6505 gnu_size
= rm_size (gnu_field_type
);
6509 if (Is_Atomic (gnat_field
))
6510 check_ok_for_atomic (gnu_field_type
, gnat_field
, false);
6512 if (Present (Component_Clause (gnat_field
)))
6514 Entity_Id gnat_parent
6515 = Parent_Subtype (Underlying_Type (Scope (gnat_field
)));
6517 gnu_pos
= UI_To_gnu (Component_Bit_Offset (gnat_field
), bitsizetype
);
6518 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6519 gnat_field
, FIELD_DECL
, false, true);
6521 /* Ensure the position does not overlap with the parent subtype, if there
6522 is one. This test is omitted if the parent of the tagged type has a
6523 full rep clause since, in this case, component clauses are allowed to
6524 overlay the space allocated for the parent type and the front-end has
6525 checked that there are no overlapping components. */
6526 if (Present (gnat_parent
) && !Is_Fully_Repped_Tagged_Type (gnat_parent
))
6528 tree gnu_parent
= gnat_to_gnu_type (gnat_parent
);
6530 if (TREE_CODE (TYPE_SIZE (gnu_parent
)) == INTEGER_CST
6531 && tree_int_cst_lt (gnu_pos
, TYPE_SIZE (gnu_parent
)))
6534 ("offset of& must be beyond parent{, minimum allowed is ^}",
6535 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6536 TYPE_SIZE_UNIT (gnu_parent
));
6540 /* If this field needs strict alignment, check that the record is
6541 sufficiently aligned and that position and size are consistent with
6542 the alignment. But don't do it if we are just annotating types and
6543 the field's type is tagged, since tagged types aren't fully laid out
6544 in this mode. Also, note that atomic implies volatile so the inner
6545 test sequences ordering is significant here. */
6546 if (needs_strict_alignment
6547 && !(type_annotate_only
&& Is_Tagged_Type (gnat_field_type
)))
6549 TYPE_ALIGN (gnu_record_type
)
6550 = MAX (TYPE_ALIGN (gnu_record_type
), TYPE_ALIGN (gnu_field_type
));
6553 && !operand_equal_p (gnu_size
, TYPE_SIZE (gnu_field_type
), 0))
6555 if (Is_Atomic (gnat_field
) || Is_Atomic (gnat_field_type
))
6557 ("atomic field& must be natural size of type{ (^)}",
6558 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6559 TYPE_SIZE (gnu_field_type
));
6561 else if (is_volatile
)
6563 ("volatile field& must be natural size of type{ (^)}",
6564 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6565 TYPE_SIZE (gnu_field_type
));
6567 else if (Is_Aliased (gnat_field
))
6569 ("size of aliased field& must be ^ bits",
6570 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6571 TYPE_SIZE (gnu_field_type
));
6573 else if (Strict_Alignment (gnat_field_type
))
6575 ("size of & with aliased or tagged components not ^ bits",
6576 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6577 TYPE_SIZE (gnu_field_type
));
6582 gnu_size
= NULL_TREE
;
6585 if (!integer_zerop (size_binop
6586 (TRUNC_MOD_EXPR
, gnu_pos
,
6587 bitsize_int (TYPE_ALIGN (gnu_field_type
)))))
6589 if (Is_Atomic (gnat_field
) || Is_Atomic (gnat_field_type
))
6591 ("position of atomic field& must be multiple of ^ bits",
6592 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6593 TYPE_ALIGN (gnu_field_type
));
6595 else if (is_volatile
)
6597 ("position of volatile field& must be multiple of ^ bits",
6598 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6599 TYPE_ALIGN (gnu_field_type
));
6601 else if (Is_Aliased (gnat_field
))
6603 ("position of aliased field& must be multiple of ^ bits",
6604 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6605 TYPE_ALIGN (gnu_field_type
));
6607 else if (Strict_Alignment (gnat_field_type
))
6609 ("position of & is not compatible with alignment required "
6610 "by its components",
6611 First_Bit (Component_Clause (gnat_field
)), gnat_field
);
6616 gnu_pos
= NULL_TREE
;
6621 /* If the record has rep clauses and this is the tag field, make a rep
6622 clause for it as well. */
6623 else if (Has_Specified_Layout (Scope (gnat_field
))
6624 && Chars (gnat_field
) == Name_uTag
)
6626 gnu_pos
= bitsize_zero_node
;
6627 gnu_size
= TYPE_SIZE (gnu_field_type
);
6632 gnu_pos
= NULL_TREE
;
6634 /* If we are packing the record and the field is BLKmode, round the
6635 size up to a byte boundary. */
6636 if (packed
&& TYPE_MODE (gnu_field_type
) == BLKmode
&& gnu_size
)
6637 gnu_size
= round_up (gnu_size
, BITS_PER_UNIT
);
6640 /* We need to make the size the maximum for the type if it is
6641 self-referential and an unconstrained type. In that case, we can't
6642 pack the field since we can't make a copy to align it. */
6643 if (TREE_CODE (gnu_field_type
) == RECORD_TYPE
6645 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type
))
6646 && !Is_Constrained (Underlying_Type (gnat_field_type
)))
6648 gnu_size
= max_size (TYPE_SIZE (gnu_field_type
), true);
6652 /* If a size is specified, adjust the field's type to it. */
6655 tree orig_field_type
;
6657 /* If the field's type is justified modular, we would need to remove
6658 the wrapper to (better) meet the layout requirements. However we
6659 can do so only if the field is not aliased to preserve the unique
6660 layout and if the prescribed size is not greater than that of the
6661 packed array to preserve the justification. */
6662 if (!needs_strict_alignment
6663 && TREE_CODE (gnu_field_type
) == RECORD_TYPE
6664 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type
)
6665 && tree_int_cst_compare (gnu_size
, TYPE_ADA_SIZE (gnu_field_type
))
6667 gnu_field_type
= TREE_TYPE (TYPE_FIELDS (gnu_field_type
));
6669 /* Similarly if the field's type is a misaligned integral type, but
6670 there is no restriction on the size as there is no justification. */
6671 if (!needs_strict_alignment
6672 && TYPE_IS_PADDING_P (gnu_field_type
)
6673 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type
))))
6674 gnu_field_type
= TREE_TYPE (TYPE_FIELDS (gnu_field_type
));
6677 = make_type_from_size (gnu_field_type
, gnu_size
,
6678 Has_Biased_Representation (gnat_field
));
6680 orig_field_type
= gnu_field_type
;
6681 gnu_field_type
= maybe_pad_type (gnu_field_type
, gnu_size
, 0, gnat_field
,
6682 false, false, definition
, true);
6684 /* If a padding record was made, declare it now since it will never be
6685 declared otherwise. This is necessary to ensure that its subtrees
6686 are properly marked. */
6687 if (gnu_field_type
!= orig_field_type
6688 && !DECL_P (TYPE_NAME (gnu_field_type
)))
6689 create_type_decl (TYPE_NAME (gnu_field_type
), gnu_field_type
, true,
6690 debug_info_p
, gnat_field
);
6693 /* Otherwise (or if there was an error), don't specify a position. */
6695 gnu_pos
= NULL_TREE
;
6697 gcc_assert (TREE_CODE (gnu_field_type
) != RECORD_TYPE
6698 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type
));
6700 /* Now create the decl for the field. */
6702 = create_field_decl (gnu_field_id
, gnu_field_type
, gnu_record_type
,
6703 gnu_size
, gnu_pos
, packed
, Is_Aliased (gnat_field
));
6704 Sloc_to_locus (Sloc (gnat_field
), &DECL_SOURCE_LOCATION (gnu_field
));
6705 DECL_ALIASED_P (gnu_field
) = Is_Aliased (gnat_field
);
6706 TREE_THIS_VOLATILE (gnu_field
) = TREE_SIDE_EFFECTS (gnu_field
) = is_volatile
;
6708 if (Ekind (gnat_field
) == E_Discriminant
)
6709 DECL_DISCRIMINANT_NUMBER (gnu_field
)
6710 = UI_To_gnu (Discriminant_Number (gnat_field
), sizetype
);
6715 /* Return true if at least one member of COMPONENT_LIST needs strict
6719 components_need_strict_alignment (Node_Id component_list
)
6721 Node_Id component_decl
;
6723 for (component_decl
= First_Non_Pragma (Component_Items (component_list
));
6724 Present (component_decl
);
6725 component_decl
= Next_Non_Pragma (component_decl
))
6727 Entity_Id gnat_field
= Defining_Entity (component_decl
);
6729 if (Is_Aliased (gnat_field
))
6732 if (Strict_Alignment (Etype (gnat_field
)))
6739 /* Return true if TYPE is a type with variable size or a padding type with a
6740 field of variable size or a record that has a field with such a type. */
6743 type_has_variable_size (tree type
)
6747 if (!TREE_CONSTANT (TYPE_SIZE (type
)))
6750 if (TYPE_IS_PADDING_P (type
)
6751 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type
))))
6754 if (!RECORD_OR_UNION_TYPE_P (type
))
6757 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
6758 if (type_has_variable_size (TREE_TYPE (field
)))
6764 /* Return true if FIELD is an artificial field. */
6767 field_is_artificial (tree field
)
6769 /* These fields are generated by the front-end proper. */
6770 if (IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == '_')
6773 /* These fields are generated by gigi. */
6774 if (DECL_INTERNAL_P (field
))
6780 /* Return true if FIELD is a non-artificial aliased field. */
6783 field_is_aliased (tree field
)
6785 if (field_is_artificial (field
))
6788 return DECL_ALIASED_P (field
);
6791 /* Return true if FIELD is a non-artificial field with self-referential
6795 field_has_self_size (tree field
)
6797 if (field_is_artificial (field
))
6800 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
6803 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field
)));
6806 /* Return true if FIELD is a non-artificial field with variable size. */
6809 field_has_variable_size (tree field
)
6811 if (field_is_artificial (field
))
6814 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
6817 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field
))) != INTEGER_CST
;
6820 /* qsort comparer for the bit positions of two record components. */
6823 compare_field_bitpos (const PTR rt1
, const PTR rt2
)
6825 const_tree
const field1
= * (const_tree
const *) rt1
;
6826 const_tree
const field2
= * (const_tree
const *) rt2
;
6828 = tree_int_cst_compare (bit_position (field1
), bit_position (field2
));
6830 return ret
? ret
: (int) (DECL_UID (field1
) - DECL_UID (field2
));
6833 /* Structure holding information for a given variant. */
6834 typedef struct vinfo
6836 /* The record type of the variant. */
6839 /* The name of the variant. */
6842 /* The qualifier of the variant. */
6845 /* Whether the variant has a rep clause. */
6848 /* Whether the variant is packed. */
6853 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6854 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6855 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6856 When called from gnat_to_gnu_entity during the processing of a record type
6857 definition, the GCC node for the parent, if any, will be the single field
6858 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6859 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6860 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6862 PACKED is 1 if this is for a packed record, -1 if this is for a record
6863 with Component_Alignment of Storage_Unit, -2 if this is for a record
6864 with a specified alignment.
6866 DEFINITION is true if we are defining this record type.
6868 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6869 out the record. This means the alignment only serves to force fields to
6870 be bitfields, but not to require the record to be that aligned. This is
6873 ALL_REP is true if a rep clause is present for all the fields.
6875 UNCHECKED_UNION is true if we are building this type for a record with a
6876 Pragma Unchecked_Union.
6878 ARTIFICIAL is true if this is a type that was generated by the compiler.
6880 DEBUG_INFO is true if we need to write debug information about the type.
6882 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6883 mean that its contents may be unused as well, only the container itself.
6885 REORDER is true if we are permitted to reorder components of this type.
6887 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6888 the outer record type down to this variant level. It is nonzero only if
6889 all the fields down to this level have a rep clause and ALL_REP is false.
6891 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6892 with a rep clause is to be added; in this case, that is all that should
6893 be done with such fields and the return value will be false. */
6896 components_to_record (tree gnu_record_type
, Node_Id gnat_component_list
,
6897 tree gnu_field_list
, int packed
, bool definition
,
6898 bool cancel_alignment
, bool all_rep
,
6899 bool unchecked_union
, bool artificial
,
6900 bool debug_info
, bool maybe_unused
, bool reorder
,
6901 tree first_free_pos
, tree
*p_gnu_rep_list
)
6903 bool all_rep_and_size
= all_rep
&& TYPE_SIZE (gnu_record_type
);
6904 bool variants_have_rep
= all_rep
;
6905 bool layout_with_rep
= false;
6906 bool has_self_field
= false;
6907 bool has_aliased_after_self_field
= false;
6908 Node_Id component_decl
, variant_part
;
6909 tree gnu_field
, gnu_next
, gnu_last
;
6910 tree gnu_variant_part
= NULL_TREE
;
6911 tree gnu_rep_list
= NULL_TREE
;
6912 tree gnu_var_list
= NULL_TREE
;
6913 tree gnu_self_list
= NULL_TREE
;
6915 /* For each component referenced in a component declaration create a GCC
6916 field and add it to the list, skipping pragmas in the GNAT list. */
6917 gnu_last
= tree_last (gnu_field_list
);
6918 if (Present (Component_Items (gnat_component_list
)))
6920 = First_Non_Pragma (Component_Items (gnat_component_list
));
6921 Present (component_decl
);
6922 component_decl
= Next_Non_Pragma (component_decl
))
6924 Entity_Id gnat_field
= Defining_Entity (component_decl
);
6925 Name_Id gnat_name
= Chars (gnat_field
);
6927 /* If present, the _Parent field must have been created as the single
6928 field of the record type. Put it before any other fields. */
6929 if (gnat_name
== Name_uParent
)
6931 gnu_field
= TYPE_FIELDS (gnu_record_type
);
6932 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
6936 gnu_field
= gnat_to_gnu_field (gnat_field
, gnu_record_type
, packed
,
6937 definition
, debug_info
);
6939 /* If this is the _Tag field, put it before any other fields. */
6940 if (gnat_name
== Name_uTag
)
6941 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
6943 /* If this is the _Controller field, put it before the other
6944 fields except for the _Tag or _Parent field. */
6945 else if (gnat_name
== Name_uController
&& gnu_last
)
6947 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
6948 DECL_CHAIN (gnu_last
) = gnu_field
;
6951 /* If this is a regular field, put it after the other fields. */
6954 DECL_CHAIN (gnu_field
) = gnu_field_list
;
6955 gnu_field_list
= gnu_field
;
6957 gnu_last
= gnu_field
;
6959 /* And record information for the final layout. */
6960 if (field_has_self_size (gnu_field
))
6961 has_self_field
= true;
6962 else if (has_self_field
&& field_is_aliased (gnu_field
))
6963 has_aliased_after_self_field
= true;
6967 save_gnu_tree (gnat_field
, gnu_field
, false);
6970 /* At the end of the component list there may be a variant part. */
6971 variant_part
= Variant_Part (gnat_component_list
);
6973 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6974 mutually exclusive and should go in the same memory. To do this we need
6975 to treat each variant as a record whose elements are created from the
6976 component list for the variant. So here we create the records from the
6977 lists for the variants and put them all into the QUAL_UNION_TYPE.
6978 If this is an Unchecked_Union, we make a UNION_TYPE instead or
6979 use GNU_RECORD_TYPE if there are no fields so far. */
6980 if (Present (variant_part
))
6982 Node_Id gnat_discr
= Name (variant_part
), variant
;
6983 tree gnu_discr
= gnat_to_gnu (gnat_discr
);
6984 tree gnu_name
= TYPE_NAME (gnu_record_type
);
6986 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr
))),
6988 tree gnu_union_type
, gnu_union_name
;
6989 tree this_first_free_pos
, gnu_variant_list
= NULL_TREE
;
6990 bool union_field_needs_strict_alignment
= false;
6991 vec
<vinfo_t
, va_stack
> variant_types
;
6992 vinfo_t
*gnu_variant
;
6993 unsigned int variants_align
= 0;
6996 vec_stack_alloc (vinfo_t
, variant_types
, 16);
6998 if (TREE_CODE (gnu_name
) == TYPE_DECL
)
6999 gnu_name
= DECL_NAME (gnu_name
);
7002 = concat_name (gnu_name
, IDENTIFIER_POINTER (gnu_var_name
));
7004 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7005 are all in the variant part, to match the layout of C unions. There
7006 is an associated check below. */
7007 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
)
7008 gnu_union_type
= gnu_record_type
;
7012 = make_node (unchecked_union
? UNION_TYPE
: QUAL_UNION_TYPE
);
7014 TYPE_NAME (gnu_union_type
) = gnu_union_name
;
7015 TYPE_ALIGN (gnu_union_type
) = 0;
7016 TYPE_PACKED (gnu_union_type
) = TYPE_PACKED (gnu_record_type
);
7019 /* If all the fields down to this level have a rep clause, find out
7020 whether all the fields at this level also have one. If so, then
7021 compute the new first free position to be passed downward. */
7022 this_first_free_pos
= first_free_pos
;
7023 if (this_first_free_pos
)
7025 for (gnu_field
= gnu_field_list
;
7027 gnu_field
= DECL_CHAIN (gnu_field
))
7028 if (DECL_FIELD_OFFSET (gnu_field
))
7030 tree pos
= bit_position (gnu_field
);
7031 if (!tree_int_cst_lt (pos
, this_first_free_pos
))
7033 = size_binop (PLUS_EXPR
, pos
, DECL_SIZE (gnu_field
));
7037 this_first_free_pos
= NULL_TREE
;
7042 /* We build the variants in two passes. The bulk of the work is done in
7043 the first pass, that is to say translating the GNAT nodes, building
7044 the container types and computing the associated properties. However
7045 we cannot finish up the container types during this pass because we
7046 don't know where the variant part will be placed until the end. */
7047 for (variant
= First_Non_Pragma (Variants (variant_part
));
7049 variant
= Next_Non_Pragma (variant
))
7051 tree gnu_variant_type
= make_node (RECORD_TYPE
);
7052 tree gnu_inner_name
, gnu_qual
;
7057 Get_Variant_Encoding (variant
);
7058 gnu_inner_name
= get_identifier_with_length (Name_Buffer
, Name_Len
);
7059 TYPE_NAME (gnu_variant_type
)
7060 = concat_name (gnu_union_name
,
7061 IDENTIFIER_POINTER (gnu_inner_name
));
7063 /* Set the alignment of the inner type in case we need to make
7064 inner objects into bitfields, but then clear it out so the
7065 record actually gets only the alignment required. */
7066 TYPE_ALIGN (gnu_variant_type
) = TYPE_ALIGN (gnu_record_type
);
7067 TYPE_PACKED (gnu_variant_type
) = TYPE_PACKED (gnu_record_type
);
7069 /* Similarly, if the outer record has a size specified and all
7070 the fields have a rep clause, we can propagate the size. */
7071 if (all_rep_and_size
)
7073 TYPE_SIZE (gnu_variant_type
) = TYPE_SIZE (gnu_record_type
);
7074 TYPE_SIZE_UNIT (gnu_variant_type
)
7075 = TYPE_SIZE_UNIT (gnu_record_type
);
7078 /* Add the fields into the record type for the variant. Note that
7079 we aren't sure to really use it at this point, see below. */
7081 = components_to_record (gnu_variant_type
, Component_List (variant
),
7082 NULL_TREE
, packed
, definition
,
7083 !all_rep_and_size
, all_rep
,
7085 true, debug_info
, true, reorder
,
7086 this_first_free_pos
,
7087 all_rep
|| this_first_free_pos
7088 ? NULL
: &gnu_rep_list
);
7090 /* Translate the qualifier and annotate the GNAT node. */
7091 gnu_qual
= choices_to_gnu (gnu_discr
, Discrete_Choices (variant
));
7092 Set_Present_Expr (variant
, annotate_value (gnu_qual
));
7094 /* Deal with packedness like in gnat_to_gnu_field. */
7095 if (components_need_strict_alignment (Component_List (variant
)))
7098 union_field_needs_strict_alignment
= true;
7102 = adjust_packed (gnu_variant_type
, gnu_record_type
, packed
);
7104 /* Push this variant onto the stack for the second pass. */
7105 vinfo
.type
= gnu_variant_type
;
7106 vinfo
.name
= gnu_inner_name
;
7107 vinfo
.qual
= gnu_qual
;
7108 vinfo
.has_rep
= has_rep
;
7109 vinfo
.packed
= field_packed
;
7110 variant_types
.safe_push (vinfo
);
7112 /* Compute the global properties that will determine the placement of
7113 the variant part. */
7114 variants_have_rep
|= has_rep
;
7115 if (!field_packed
&& TYPE_ALIGN (gnu_variant_type
) > variants_align
)
7116 variants_align
= TYPE_ALIGN (gnu_variant_type
);
7119 /* Round up the first free position to the alignment of the variant part
7120 for the variants without rep clause. This will guarantee a consistent
7121 layout independently of the placement of the variant part. */
7122 if (variants_have_rep
&& variants_align
> 0 && this_first_free_pos
)
7123 this_first_free_pos
= round_up (this_first_free_pos
, variants_align
);
7125 /* In the second pass, the container types are adjusted if necessary and
7126 finished up, then the corresponding fields of the variant part are
7127 built with their qualifier, unless this is an unchecked union. */
7128 FOR_EACH_VEC_ELT (variant_types
, i
, gnu_variant
)
7130 tree gnu_variant_type
= gnu_variant
->type
;
7131 tree gnu_field_list
= TYPE_FIELDS (gnu_variant_type
);
7133 /* If this is an Unchecked_Union whose fields are all in the variant
7134 part and we have a single field with no representation clause or
7135 placed at offset zero, use the field directly to match the layout
7137 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
7139 && !DECL_CHAIN (gnu_field_list
)
7140 && (!DECL_FIELD_OFFSET (gnu_field_list
)
7141 || integer_zerop (bit_position (gnu_field_list
))))
7143 gnu_field
= gnu_field_list
;
7144 DECL_CONTEXT (gnu_field
) = gnu_record_type
;
7148 /* Finalize the variant type now. We used to throw away empty
7149 record types but we no longer do that because we need them to
7150 generate complete debug info for the variant; otherwise, the
7151 union type definition will be lacking the fields associated
7152 with these empty variants. */
7153 if (gnu_field_list
&& variants_have_rep
&& !gnu_variant
->has_rep
)
7155 /* The variant part will be at offset 0 so we need to ensure
7156 that the fields are laid out starting from the first free
7157 position at this level. */
7158 tree gnu_rep_type
= make_node (RECORD_TYPE
);
7160 finish_record_type (gnu_rep_type
, NULL_TREE
, 0, debug_info
);
7162 = create_rep_part (gnu_rep_type
, gnu_variant_type
,
7163 this_first_free_pos
);
7164 DECL_CHAIN (gnu_rep_part
) = gnu_field_list
;
7165 gnu_field_list
= gnu_rep_part
;
7166 finish_record_type (gnu_variant_type
, gnu_field_list
, 0,
7171 rest_of_record_type_compilation (gnu_variant_type
);
7172 create_type_decl (TYPE_NAME (gnu_variant_type
), gnu_variant_type
,
7173 true, debug_info
, gnat_component_list
);
7176 = create_field_decl (gnu_variant
->name
, gnu_variant_type
,
7179 ? TYPE_SIZE (gnu_variant_type
) : 0,
7180 variants_have_rep
? bitsize_zero_node
: 0,
7181 gnu_variant
->packed
, 0);
7183 DECL_INTERNAL_P (gnu_field
) = 1;
7185 if (!unchecked_union
)
7186 DECL_QUALIFIER (gnu_field
) = gnu_variant
->qual
;
7189 DECL_CHAIN (gnu_field
) = gnu_variant_list
;
7190 gnu_variant_list
= gnu_field
;
7193 /* We are done with the variants. */
7194 variant_types
.release ();
7196 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7197 if (gnu_variant_list
)
7199 int union_field_packed
;
7201 if (all_rep_and_size
)
7203 TYPE_SIZE (gnu_union_type
) = TYPE_SIZE (gnu_record_type
);
7204 TYPE_SIZE_UNIT (gnu_union_type
)
7205 = TYPE_SIZE_UNIT (gnu_record_type
);
7208 finish_record_type (gnu_union_type
, nreverse (gnu_variant_list
),
7209 all_rep_and_size
? 1 : 0, debug_info
);
7211 /* If GNU_UNION_TYPE is our record type, it means we must have an
7212 Unchecked_Union with no fields. Verify that and, if so, just
7214 if (gnu_union_type
== gnu_record_type
)
7216 gcc_assert (unchecked_union
7219 return variants_have_rep
;
7222 create_type_decl (TYPE_NAME (gnu_union_type
), gnu_union_type
, true,
7223 debug_info
, gnat_component_list
);
7225 /* Deal with packedness like in gnat_to_gnu_field. */
7226 if (union_field_needs_strict_alignment
)
7227 union_field_packed
= 0;
7230 = adjust_packed (gnu_union_type
, gnu_record_type
, packed
);
7233 = create_field_decl (gnu_var_name
, gnu_union_type
, gnu_record_type
,
7235 ? TYPE_SIZE (gnu_union_type
) : 0,
7236 variants_have_rep
? bitsize_zero_node
: 0,
7237 union_field_packed
, 0);
7239 DECL_INTERNAL_P (gnu_variant_part
) = 1;
7243 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7244 permitted to reorder components, self-referential sizes or variable sizes.
7245 If they do, pull them out and put them onto the appropriate list. We have
7246 to do this in a separate pass since we want to handle the discriminants
7247 but can't play with them until we've used them in debugging data above.
7249 ??? If we reorder them, debugging information will be wrong but there is
7250 nothing that can be done about this at the moment. */
7251 gnu_last
= NULL_TREE
;
7253 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7256 DECL_CHAIN (gnu_last) = gnu_next; \
7258 gnu_field_list = gnu_next; \
7260 DECL_CHAIN (gnu_field) = (LIST); \
7261 (LIST) = gnu_field; \
7264 for (gnu_field
= gnu_field_list
; gnu_field
; gnu_field
= gnu_next
)
7266 gnu_next
= DECL_CHAIN (gnu_field
);
7268 if (DECL_FIELD_OFFSET (gnu_field
))
7270 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list
);
7274 if ((reorder
|| has_aliased_after_self_field
)
7275 && field_has_self_size (gnu_field
))
7277 MOVE_FROM_FIELD_LIST_TO (gnu_self_list
);
7281 if (reorder
&& field_has_variable_size (gnu_field
))
7283 MOVE_FROM_FIELD_LIST_TO (gnu_var_list
);
7287 gnu_last
= gnu_field
;
7290 #undef MOVE_FROM_FIELD_LIST_TO
7292 gnu_field_list
= nreverse (gnu_field_list
);
7294 /* If permitted, we reorder the fields as follows:
7296 1) all fixed length fields,
7297 2) all fields whose length doesn't depend on discriminants,
7298 3) all fields whose length depends on discriminants,
7299 4) the variant part,
7301 within the record and within each variant recursively. */
7304 = chainon (gnu_field_list
, chainon (gnu_var_list
, gnu_self_list
));
7306 /* Otherwise, if there is an aliased field placed after a field whose length
7307 depends on discriminants, we put all the fields of the latter sort, last.
7308 We need to do this in case an object of this record type is mutable. */
7309 else if (has_aliased_after_self_field
)
7310 gnu_field_list
= chainon (gnu_field_list
, gnu_self_list
);
7312 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7313 in our REP list to the previous level because this level needs them in
7314 order to do a correct layout, i.e. avoid having overlapping fields. */
7315 if (p_gnu_rep_list
&& gnu_rep_list
)
7316 *p_gnu_rep_list
= chainon (*p_gnu_rep_list
, gnu_rep_list
);
7318 /* Otherwise, sort the fields by bit position and put them into their own
7319 record, before the others, if we also have fields without rep clause. */
7320 else if (gnu_rep_list
)
7322 tree gnu_rep_type
, gnu_rep_part
;
7323 int i
, len
= list_length (gnu_rep_list
);
7324 tree
*gnu_arr
= XALLOCAVEC (tree
, len
);
7326 /* If all the fields have a rep clause, we can do a flat layout. */
7327 layout_with_rep
= !gnu_field_list
7328 && (!gnu_variant_part
|| variants_have_rep
);
7330 = layout_with_rep
? gnu_record_type
: make_node (RECORD_TYPE
);
7332 for (gnu_field
= gnu_rep_list
, i
= 0;
7334 gnu_field
= DECL_CHAIN (gnu_field
), i
++)
7335 gnu_arr
[i
] = gnu_field
;
7337 qsort (gnu_arr
, len
, sizeof (tree
), compare_field_bitpos
);
7339 /* Put the fields in the list in order of increasing position, which
7340 means we start from the end. */
7341 gnu_rep_list
= NULL_TREE
;
7342 for (i
= len
- 1; i
>= 0; i
--)
7344 DECL_CHAIN (gnu_arr
[i
]) = gnu_rep_list
;
7345 gnu_rep_list
= gnu_arr
[i
];
7346 DECL_CONTEXT (gnu_arr
[i
]) = gnu_rep_type
;
7349 if (layout_with_rep
)
7350 gnu_field_list
= gnu_rep_list
;
7353 finish_record_type (gnu_rep_type
, gnu_rep_list
, 1, debug_info
);
7355 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7356 without rep clause are laid out starting from this position.
7357 Therefore, we force it as a minimal size on the REP part. */
7359 = create_rep_part (gnu_rep_type
, gnu_record_type
, first_free_pos
);
7361 /* Chain the REP part at the beginning of the field list. */
7362 DECL_CHAIN (gnu_rep_part
) = gnu_field_list
;
7363 gnu_field_list
= gnu_rep_part
;
7367 /* Chain the variant part at the end of the field list. */
7368 if (gnu_variant_part
)
7369 gnu_field_list
= chainon (gnu_field_list
, gnu_variant_part
);
7371 if (cancel_alignment
)
7372 TYPE_ALIGN (gnu_record_type
) = 0;
7374 TYPE_ARTIFICIAL (gnu_record_type
) = artificial
;
7376 finish_record_type (gnu_record_type
, gnu_field_list
, layout_with_rep
? 1 : 0,
7377 debug_info
&& !maybe_unused
);
7379 return (gnu_rep_list
&& !p_gnu_rep_list
) || variants_have_rep
;
7382 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7383 placed into an Esize, Component_Bit_Offset, or Component_Size value
7384 in the GNAT tree. */
7387 annotate_value (tree gnu_size
)
7390 Node_Ref_Or_Val ops
[3], ret
, pre_op1
= No_Uint
;
7391 struct tree_int_map in
;
7394 /* See if we've already saved the value for this node. */
7395 if (EXPR_P (gnu_size
))
7397 struct tree_int_map
*e
;
7399 if (!annotate_value_cache
)
7400 annotate_value_cache
= htab_create_ggc (512, tree_int_map_hash
,
7401 tree_int_map_eq
, 0);
7402 in
.base
.from
= gnu_size
;
7403 e
= (struct tree_int_map
*)
7404 htab_find (annotate_value_cache
, &in
);
7407 return (Node_Ref_Or_Val
) e
->to
;
7410 in
.base
.from
= NULL_TREE
;
7412 /* If we do not return inside this switch, TCODE will be set to the
7413 code to use for a Create_Node operand and LEN (set above) will be
7414 the number of recursive calls for us to make. */
7416 switch (TREE_CODE (gnu_size
))
7419 return TREE_OVERFLOW (gnu_size
) ? No_Uint
: UI_From_gnu (gnu_size
);
7422 /* The only case we handle here is a simple discriminant reference. */
7423 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size
, 1)))
7425 tree n
= DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size
, 1));
7427 /* Climb up the chain of successive extensions, if any. */
7428 while (TREE_CODE (TREE_OPERAND (gnu_size
, 0)) == COMPONENT_REF
7429 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size
, 0), 1))
7431 gnu_size
= TREE_OPERAND (gnu_size
, 0);
7433 if (TREE_CODE (TREE_OPERAND (gnu_size
, 0)) == PLACEHOLDER_EXPR
)
7435 Create_Node (Discrim_Val
, annotate_value (n
), No_Uint
, No_Uint
);
7440 CASE_CONVERT
: case NON_LVALUE_EXPR
:
7441 return annotate_value (TREE_OPERAND (gnu_size
, 0));
7443 /* Now just list the operations we handle. */
7444 case COND_EXPR
: tcode
= Cond_Expr
; break;
7445 case PLUS_EXPR
: tcode
= Plus_Expr
; break;
7446 case MINUS_EXPR
: tcode
= Minus_Expr
; break;
7447 case MULT_EXPR
: tcode
= Mult_Expr
; break;
7448 case TRUNC_DIV_EXPR
: tcode
= Trunc_Div_Expr
; break;
7449 case CEIL_DIV_EXPR
: tcode
= Ceil_Div_Expr
; break;
7450 case FLOOR_DIV_EXPR
: tcode
= Floor_Div_Expr
; break;
7451 case TRUNC_MOD_EXPR
: tcode
= Trunc_Mod_Expr
; break;
7452 case CEIL_MOD_EXPR
: tcode
= Ceil_Mod_Expr
; break;
7453 case FLOOR_MOD_EXPR
: tcode
= Floor_Mod_Expr
; break;
7454 case EXACT_DIV_EXPR
: tcode
= Exact_Div_Expr
; break;
7455 case NEGATE_EXPR
: tcode
= Negate_Expr
; break;
7456 case MIN_EXPR
: tcode
= Min_Expr
; break;
7457 case MAX_EXPR
: tcode
= Max_Expr
; break;
7458 case ABS_EXPR
: tcode
= Abs_Expr
; break;
7459 case TRUTH_ANDIF_EXPR
: tcode
= Truth_Andif_Expr
; break;
7460 case TRUTH_ORIF_EXPR
: tcode
= Truth_Orif_Expr
; break;
7461 case TRUTH_AND_EXPR
: tcode
= Truth_And_Expr
; break;
7462 case TRUTH_OR_EXPR
: tcode
= Truth_Or_Expr
; break;
7463 case TRUTH_XOR_EXPR
: tcode
= Truth_Xor_Expr
; break;
7464 case TRUTH_NOT_EXPR
: tcode
= Truth_Not_Expr
; break;
7465 case LT_EXPR
: tcode
= Lt_Expr
; break;
7466 case LE_EXPR
: tcode
= Le_Expr
; break;
7467 case GT_EXPR
: tcode
= Gt_Expr
; break;
7468 case GE_EXPR
: tcode
= Ge_Expr
; break;
7469 case EQ_EXPR
: tcode
= Eq_Expr
; break;
7470 case NE_EXPR
: tcode
= Ne_Expr
; break;
7473 tcode
= Bit_And_Expr
;
7474 /* For negative values, build NEGATE_EXPR of the opposite. Such values
7475 appear in expressions containing aligning patterns. Note that, since
7476 sizetype is unsigned, we have to jump through some hoops. */
7477 if (TREE_CODE (TREE_OPERAND (gnu_size
, 1)) == INTEGER_CST
)
7479 tree op1
= TREE_OPERAND (gnu_size
, 1);
7480 double_int signed_op1
7481 = tree_to_double_int (op1
).sext (TYPE_PRECISION (sizetype
));
7482 if (signed_op1
.is_negative ())
7484 op1
= double_int_to_tree (sizetype
, -signed_op1
);
7485 pre_op1
= annotate_value (build1 (NEGATE_EXPR
, sizetype
, op1
));
7492 tree t
= maybe_inline_call_in_expr (gnu_size
);
7494 return annotate_value (t
);
7497 /* Fall through... */
7503 /* Now get each of the operands that's relevant for this code. If any
7504 cannot be expressed as a repinfo node, say we can't. */
7505 for (i
= 0; i
< 3; i
++)
7508 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (gnu_size
)); i
++)
7510 if (i
== 1 && pre_op1
!= No_Uint
)
7513 ops
[i
] = annotate_value (TREE_OPERAND (gnu_size
, i
));
7514 if (ops
[i
] == No_Uint
)
7518 ret
= Create_Node (tcode
, ops
[0], ops
[1], ops
[2]);
7520 /* Save the result in the cache. */
7523 struct tree_int_map
**h
;
7524 /* We can't assume the hash table data hasn't moved since the
7525 initial look up, so we have to search again. Allocating and
7526 inserting an entry at that point would be an alternative, but
7527 then we'd better discard the entry if we decided not to cache
7529 h
= (struct tree_int_map
**)
7530 htab_find_slot (annotate_value_cache
, &in
, INSERT
);
7532 *h
= ggc_alloc_tree_int_map ();
7533 (*h
)->base
.from
= gnu_size
;
7540 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7541 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7542 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7543 BY_REF is true if the object is used by reference. */
7546 annotate_object (Entity_Id gnat_entity
, tree gnu_type
, tree size
, bool by_ref
)
7550 if (TYPE_IS_FAT_POINTER_P (gnu_type
))
7551 gnu_type
= TYPE_UNCONSTRAINED_ARRAY (gnu_type
);
7553 gnu_type
= TREE_TYPE (gnu_type
);
7556 if (Unknown_Esize (gnat_entity
))
7558 if (TREE_CODE (gnu_type
) == RECORD_TYPE
7559 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
7560 size
= TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
))));
7562 size
= TYPE_SIZE (gnu_type
);
7565 Set_Esize (gnat_entity
, annotate_value (size
));
7568 if (Unknown_Alignment (gnat_entity
))
7569 Set_Alignment (gnat_entity
,
7570 UI_From_Int (TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
));
7573 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7574 Return NULL_TREE if there is no such element in the list. */
7577 purpose_member_field (const_tree elem
, tree list
)
7581 tree field
= TREE_PURPOSE (list
);
7582 if (SAME_FIELD_P (field
, elem
))
7584 list
= TREE_CHAIN (list
);
7589 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7590 set Component_Bit_Offset and Esize of the components to the position and
7591 size used by Gigi. */
7594 annotate_rep (Entity_Id gnat_entity
, tree gnu_type
)
7596 Entity_Id gnat_field
;
7599 /* We operate by first making a list of all fields and their position (we
7600 can get the size easily) and then update all the sizes in the tree. */
7602 = build_position_list (gnu_type
, false, size_zero_node
, bitsize_zero_node
,
7603 BIGGEST_ALIGNMENT
, NULL_TREE
);
7605 for (gnat_field
= First_Entity (gnat_entity
);
7606 Present (gnat_field
);
7607 gnat_field
= Next_Entity (gnat_field
))
7608 if (Ekind (gnat_field
) == E_Component
7609 || (Ekind (gnat_field
) == E_Discriminant
7610 && !Is_Unchecked_Union (Scope (gnat_field
))))
7612 tree t
= purpose_member_field (gnat_to_gnu_field_decl (gnat_field
),
7618 /* If we are just annotating types and the type is tagged, the tag
7619 and the parent components are not generated by the front-end so
7620 we need to add the appropriate offset to each component without
7621 representation clause. */
7622 if (type_annotate_only
7623 && Is_Tagged_Type (gnat_entity
)
7624 && No (Component_Clause (gnat_field
)))
7626 /* For a component appearing in the current extension, the
7627 offset is the size of the parent. */
7628 if (Is_Derived_Type (gnat_entity
)
7629 && Original_Record_Component (gnat_field
) == gnat_field
)
7631 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity
))),
7634 parent_offset
= bitsize_int (POINTER_SIZE
);
7636 if (TYPE_FIELDS (gnu_type
))
7638 = round_up (parent_offset
,
7639 DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
7642 parent_offset
= bitsize_zero_node
;
7644 Set_Component_Bit_Offset
7647 (size_binop (PLUS_EXPR
,
7648 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t
), 0),
7649 TREE_VEC_ELT (TREE_VALUE (t
), 2)),
7652 Set_Esize (gnat_field
,
7653 annotate_value (DECL_SIZE (TREE_PURPOSE (t
))));
7655 else if (Is_Tagged_Type (gnat_entity
) && Is_Derived_Type (gnat_entity
))
7657 /* If there is no entry, this is an inherited component whose
7658 position is the same as in the parent type. */
7659 Set_Component_Bit_Offset
7661 Component_Bit_Offset (Original_Record_Component (gnat_field
)));
7663 Set_Esize (gnat_field
,
7664 Esize (Original_Record_Component (gnat_field
)));
7669 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7670 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7671 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7672 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7673 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7674 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7675 pre-existing list to be chained to the newly created entries. */
7678 build_position_list (tree gnu_type
, bool do_not_flatten_variant
, tree gnu_pos
,
7679 tree gnu_bitpos
, unsigned int offset_align
, tree gnu_list
)
7683 for (gnu_field
= TYPE_FIELDS (gnu_type
);
7685 gnu_field
= DECL_CHAIN (gnu_field
))
7687 tree gnu_our_bitpos
= size_binop (PLUS_EXPR
, gnu_bitpos
,
7688 DECL_FIELD_BIT_OFFSET (gnu_field
));
7689 tree gnu_our_offset
= size_binop (PLUS_EXPR
, gnu_pos
,
7690 DECL_FIELD_OFFSET (gnu_field
));
7691 unsigned int our_offset_align
7692 = MIN (offset_align
, DECL_OFFSET_ALIGN (gnu_field
));
7693 tree v
= make_tree_vec (3);
7695 TREE_VEC_ELT (v
, 0) = gnu_our_offset
;
7696 TREE_VEC_ELT (v
, 1) = size_int (our_offset_align
);
7697 TREE_VEC_ELT (v
, 2) = gnu_our_bitpos
;
7698 gnu_list
= tree_cons (gnu_field
, v
, gnu_list
);
7700 /* Recurse on internal fields, flattening the nested fields except for
7701 those in the variant part, if requested. */
7702 if (DECL_INTERNAL_P (gnu_field
))
7704 tree gnu_field_type
= TREE_TYPE (gnu_field
);
7705 if (do_not_flatten_variant
7706 && TREE_CODE (gnu_field_type
) == QUAL_UNION_TYPE
)
7708 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
7709 size_zero_node
, bitsize_zero_node
,
7710 BIGGEST_ALIGNMENT
, gnu_list
);
7713 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
7714 gnu_our_offset
, gnu_our_bitpos
,
7715 our_offset_align
, gnu_list
);
7722 /* Return a list describing the substitutions needed to reflect the
7723 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7724 be in any order. The values in an element of the list are in the form
7725 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7726 a definition of GNAT_SUBTYPE. */
7728 static vec
<subst_pair
>
7729 build_subst_list (Entity_Id gnat_subtype
, Entity_Id gnat_type
, bool definition
)
7731 vec
<subst_pair
> gnu_list
= vNULL
;
7732 Entity_Id gnat_discrim
;
7733 Node_Id gnat_constr
;
7735 for (gnat_discrim
= First_Stored_Discriminant (gnat_type
),
7736 gnat_constr
= First_Elmt (Stored_Constraint (gnat_subtype
));
7737 Present (gnat_discrim
);
7738 gnat_discrim
= Next_Stored_Discriminant (gnat_discrim
),
7739 gnat_constr
= Next_Elmt (gnat_constr
))
7740 /* Ignore access discriminants. */
7741 if (!Is_Access_Type (Etype (Node (gnat_constr
))))
7743 tree gnu_field
= gnat_to_gnu_field_decl (gnat_discrim
);
7744 tree replacement
= convert (TREE_TYPE (gnu_field
),
7745 elaborate_expression
7746 (Node (gnat_constr
), gnat_subtype
,
7747 get_entity_name (gnat_discrim
),
7748 definition
, true, false));
7749 subst_pair s
= {gnu_field
, replacement
};
7750 gnu_list
.safe_push (s
);
7756 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7757 variants of QUAL_UNION_TYPE that are still relevant after applying
7758 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7759 list to be prepended to the newly created entries. */
7761 static vec
<variant_desc
>
7762 build_variant_list (tree qual_union_type
, vec
<subst_pair
> subst_list
,
7763 vec
<variant_desc
> gnu_list
)
7767 for (gnu_field
= TYPE_FIELDS (qual_union_type
);
7769 gnu_field
= DECL_CHAIN (gnu_field
))
7771 tree qual
= DECL_QUALIFIER (gnu_field
);
7775 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
7776 qual
= SUBSTITUTE_IN_EXPR (qual
, s
->discriminant
, s
->replacement
);
7778 /* If the new qualifier is not unconditionally false, its variant may
7779 still be accessed. */
7780 if (!integer_zerop (qual
))
7782 tree variant_type
= TREE_TYPE (gnu_field
), variant_subpart
;
7783 variant_desc v
= {variant_type
, gnu_field
, qual
, NULL_TREE
};
7785 gnu_list
.safe_push (v
);
7787 /* Recurse on the variant subpart of the variant, if any. */
7788 variant_subpart
= get_variant_part (variant_type
);
7789 if (variant_subpart
)
7790 gnu_list
= build_variant_list (TREE_TYPE (variant_subpart
),
7791 subst_list
, gnu_list
);
7793 /* If the new qualifier is unconditionally true, the subsequent
7794 variants cannot be accessed. */
7795 if (integer_onep (qual
))
7803 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7804 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7805 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7806 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7807 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7808 true if we are being called to process the Component_Size of GNAT_OBJECT;
7809 this is used only for error messages. ZERO_OK is true if a size of zero
7810 is permitted; if ZERO_OK is false, it means that a size of zero should be
7811 treated as an unspecified size. */
7814 validate_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_object
,
7815 enum tree_code kind
, bool component_p
, bool zero_ok
)
7817 Node_Id gnat_error_node
;
7818 tree type_size
, size
;
7820 /* Return 0 if no size was specified. */
7821 if (uint_size
== No_Uint
)
7824 /* Ignore a negative size since that corresponds to our back-annotation. */
7825 if (UI_Lt (uint_size
, Uint_0
))
7828 /* Find the node to use for error messages. */
7829 if ((Ekind (gnat_object
) == E_Component
7830 || Ekind (gnat_object
) == E_Discriminant
)
7831 && Present (Component_Clause (gnat_object
)))
7832 gnat_error_node
= Last_Bit (Component_Clause (gnat_object
));
7833 else if (Present (Size_Clause (gnat_object
)))
7834 gnat_error_node
= Expression (Size_Clause (gnat_object
));
7836 gnat_error_node
= gnat_object
;
7838 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7839 but cannot be represented in bitsizetype. */
7840 size
= UI_To_gnu (uint_size
, bitsizetype
);
7841 if (TREE_OVERFLOW (size
))
7844 post_error_ne ("component size for& is too large", gnat_error_node
,
7847 post_error_ne ("size for& is too large", gnat_error_node
,
7852 /* Ignore a zero size if it is not permitted. */
7853 if (!zero_ok
&& integer_zerop (size
))
7856 /* The size of objects is always a multiple of a byte. */
7857 if (kind
== VAR_DECL
7858 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
, size
, bitsize_unit_node
)))
7861 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7862 gnat_error_node
, gnat_object
);
7864 post_error_ne ("size for& is not a multiple of Storage_Unit",
7865 gnat_error_node
, gnat_object
);
7869 /* If this is an integral type or a packed array type, the front-end has
7870 already verified the size, so we need not do it here (which would mean
7871 checking against the bounds). However, if this is an aliased object,
7872 it may not be smaller than the type of the object. */
7873 if ((INTEGRAL_TYPE_P (gnu_type
) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type
))
7874 && !(kind
== VAR_DECL
&& Is_Aliased (gnat_object
)))
7877 /* If the object is a record that contains a template, add the size of the
7878 template to the specified size. */
7879 if (TREE_CODE (gnu_type
) == RECORD_TYPE
7880 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
7881 size
= size_binop (PLUS_EXPR
, DECL_SIZE (TYPE_FIELDS (gnu_type
)), size
);
7883 if (kind
== VAR_DECL
7884 /* If a type needs strict alignment, a component of this type in
7885 a packed record cannot be packed and thus uses the type size. */
7886 || (kind
== TYPE_DECL
&& Strict_Alignment (gnat_object
)))
7887 type_size
= TYPE_SIZE (gnu_type
);
7889 type_size
= rm_size (gnu_type
);
7891 /* Modify the size of a discriminated type to be the maximum size. */
7892 if (type_size
&& CONTAINS_PLACEHOLDER_P (type_size
))
7893 type_size
= max_size (type_size
, true);
7895 /* If this is an access type or a fat pointer, the minimum size is that given
7896 by the smallest integral mode that's valid for pointers. */
7897 if (TREE_CODE (gnu_type
) == POINTER_TYPE
|| TYPE_IS_FAT_POINTER_P (gnu_type
))
7899 enum machine_mode p_mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
7900 while (!targetm
.valid_pointer_mode (p_mode
))
7901 p_mode
= GET_MODE_WIDER_MODE (p_mode
);
7902 type_size
= bitsize_int (GET_MODE_BITSIZE (p_mode
));
7905 /* Issue an error either if the default size of the object isn't a constant
7906 or if the new size is smaller than it. */
7907 if (TREE_CODE (type_size
) != INTEGER_CST
7908 || TREE_OVERFLOW (type_size
)
7909 || tree_int_cst_lt (size
, type_size
))
7913 ("component size for& too small{, minimum allowed is ^}",
7914 gnat_error_node
, gnat_object
, type_size
);
7917 ("size for& too small{, minimum allowed is ^}",
7918 gnat_error_node
, gnat_object
, type_size
);
7925 /* Similarly, but both validate and process a value of RM size. This routine
7926 is only called for types. */
7929 set_rm_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_entity
)
7931 Node_Id gnat_attr_node
;
7932 tree old_size
, size
;
7934 /* Do nothing if no size was specified. */
7935 if (uint_size
== No_Uint
)
7938 /* Ignore a negative size since that corresponds to our back-annotation. */
7939 if (UI_Lt (uint_size
, Uint_0
))
7942 /* Only issue an error if a Value_Size clause was explicitly given.
7943 Otherwise, we'd be duplicating an error on the Size clause. */
7945 = Get_Attribute_Definition_Clause (gnat_entity
, Attr_Value_Size
);
7947 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7948 but cannot be represented in bitsizetype. */
7949 size
= UI_To_gnu (uint_size
, bitsizetype
);
7950 if (TREE_OVERFLOW (size
))
7952 if (Present (gnat_attr_node
))
7953 post_error_ne ("Value_Size for& is too large", gnat_attr_node
,
7958 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7959 exists, or this is an integer type, in which case the front-end will
7960 have always set it. */
7961 if (No (gnat_attr_node
)
7962 && integer_zerop (size
)
7963 && !Has_Size_Clause (gnat_entity
)
7964 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
7967 old_size
= rm_size (gnu_type
);
7969 /* If the old size is self-referential, get the maximum size. */
7970 if (CONTAINS_PLACEHOLDER_P (old_size
))
7971 old_size
= max_size (old_size
, true);
7973 /* Issue an error either if the old size of the object isn't a constant or
7974 if the new size is smaller than it. The front-end has already verified
7975 this for scalar and packed array types. */
7976 if (TREE_CODE (old_size
) != INTEGER_CST
7977 || TREE_OVERFLOW (old_size
)
7978 || (AGGREGATE_TYPE_P (gnu_type
)
7979 && !(TREE_CODE (gnu_type
) == ARRAY_TYPE
7980 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type
))
7981 && !(TYPE_IS_PADDING_P (gnu_type
)
7982 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
))) == ARRAY_TYPE
7983 && TYPE_PACKED_ARRAY_TYPE_P
7984 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))
7985 && tree_int_cst_lt (size
, old_size
)))
7987 if (Present (gnat_attr_node
))
7989 ("Value_Size for& too small{, minimum allowed is ^}",
7990 gnat_attr_node
, gnat_entity
, old_size
);
7994 /* Otherwise, set the RM size proper for integral types... */
7995 if ((TREE_CODE (gnu_type
) == INTEGER_TYPE
7996 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
7997 || (TREE_CODE (gnu_type
) == ENUMERAL_TYPE
7998 || TREE_CODE (gnu_type
) == BOOLEAN_TYPE
))
7999 SET_TYPE_RM_SIZE (gnu_type
, size
);
8001 /* ...or the Ada size for record and union types. */
8002 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
8003 && !TYPE_FAT_POINTER_P (gnu_type
))
8004 SET_TYPE_ADA_SIZE (gnu_type
, size
);
8007 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8008 a type or object whose present alignment is ALIGN. If this alignment is
8009 valid, return it. Otherwise, give an error and return ALIGN. */
8012 validate_alignment (Uint alignment
, Entity_Id gnat_entity
, unsigned int align
)
8014 unsigned int max_allowed_alignment
= get_target_maximum_allowed_alignment ();
8015 unsigned int new_align
;
8016 Node_Id gnat_error_node
;
8018 /* Don't worry about checking alignment if alignment was not specified
8019 by the source program and we already posted an error for this entity. */
8020 if (Error_Posted (gnat_entity
) && !Has_Alignment_Clause (gnat_entity
))
8023 /* Post the error on the alignment clause if any. Note, for the implicit
8024 base type of an array type, the alignment clause is on the first
8026 if (Present (Alignment_Clause (gnat_entity
)))
8027 gnat_error_node
= Expression (Alignment_Clause (gnat_entity
));
8029 else if (Is_Itype (gnat_entity
)
8030 && Is_Array_Type (gnat_entity
)
8031 && Etype (gnat_entity
) == gnat_entity
8032 && Present (Alignment_Clause (First_Subtype (gnat_entity
))))
8034 Expression (Alignment_Clause (First_Subtype (gnat_entity
)));
8037 gnat_error_node
= gnat_entity
;
8039 /* Within GCC, an alignment is an integer, so we must make sure a value is
8040 specified that fits in that range. Also, there is an upper bound to
8041 alignments we can support/allow. */
8042 if (!UI_Is_In_Int_Range (alignment
)
8043 || ((new_align
= UI_To_Int (alignment
)) > max_allowed_alignment
))
8044 post_error_ne_num ("largest supported alignment for& is ^",
8045 gnat_error_node
, gnat_entity
, max_allowed_alignment
);
8046 else if (!(Present (Alignment_Clause (gnat_entity
))
8047 && From_At_Mod (Alignment_Clause (gnat_entity
)))
8048 && new_align
* BITS_PER_UNIT
< align
)
8050 unsigned int double_align
;
8051 bool is_capped_double
, align_clause
;
8053 /* If the default alignment of "double" or larger scalar types is
8054 specifically capped and the new alignment is above the cap, do
8055 not post an error and change the alignment only if there is an
8056 alignment clause; this makes it possible to have the associated
8057 GCC type overaligned by default for performance reasons. */
8058 if ((double_align
= double_float_alignment
) > 0)
8061 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
8063 = is_double_float_or_array (gnat_type
, &align_clause
);
8065 else if ((double_align
= double_scalar_alignment
) > 0)
8068 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
8070 = is_double_scalar_or_array (gnat_type
, &align_clause
);
8073 is_capped_double
= align_clause
= false;
8075 if (is_capped_double
&& new_align
>= double_align
)
8078 align
= new_align
* BITS_PER_UNIT
;
8082 if (is_capped_double
)
8083 align
= double_align
* BITS_PER_UNIT
;
8085 post_error_ne_num ("alignment for& must be at least ^",
8086 gnat_error_node
, gnat_entity
,
8087 align
/ BITS_PER_UNIT
);
8092 new_align
= (new_align
> 0 ? new_align
* BITS_PER_UNIT
: 1);
8093 if (new_align
> align
)
8100 /* Verify that OBJECT, a type or decl, is something we can implement
8101 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8102 if we require atomic components. */
8105 check_ok_for_atomic (tree object
, Entity_Id gnat_entity
, bool comp_p
)
8107 Node_Id gnat_error_point
= gnat_entity
;
8109 enum machine_mode mode
;
8113 /* There are three case of what OBJECT can be. It can be a type, in which
8114 case we take the size, alignment and mode from the type. It can be a
8115 declaration that was indirect, in which case the relevant values are
8116 that of the type being pointed to, or it can be a normal declaration,
8117 in which case the values are of the decl. The code below assumes that
8118 OBJECT is either a type or a decl. */
8119 if (TYPE_P (object
))
8121 /* If this is an anonymous base type, nothing to check. Error will be
8122 reported on the source type. */
8123 if (!Comes_From_Source (gnat_entity
))
8126 mode
= TYPE_MODE (object
);
8127 align
= TYPE_ALIGN (object
);
8128 size
= TYPE_SIZE (object
);
8130 else if (DECL_BY_REF_P (object
))
8132 mode
= TYPE_MODE (TREE_TYPE (TREE_TYPE (object
)));
8133 align
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object
)));
8134 size
= TYPE_SIZE (TREE_TYPE (TREE_TYPE (object
)));
8138 mode
= DECL_MODE (object
);
8139 align
= DECL_ALIGN (object
);
8140 size
= DECL_SIZE (object
);
8143 /* Consider all floating-point types atomic and any types that that are
8144 represented by integers no wider than a machine word. */
8145 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
8146 || ((GET_MODE_CLASS (mode
) == MODE_INT
8147 || GET_MODE_CLASS (mode
) == MODE_PARTIAL_INT
)
8148 && GET_MODE_BITSIZE (mode
) <= BITS_PER_WORD
))
8151 /* For the moment, also allow anything that has an alignment equal
8152 to its size and which is smaller than a word. */
8153 if (size
&& TREE_CODE (size
) == INTEGER_CST
8154 && compare_tree_int (size
, align
) == 0
8155 && align
<= BITS_PER_WORD
)
8158 for (gnat_node
= First_Rep_Item (gnat_entity
); Present (gnat_node
);
8159 gnat_node
= Next_Rep_Item (gnat_node
))
8161 if (!comp_p
&& Nkind (gnat_node
) == N_Pragma
8162 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)))
8164 gnat_error_point
= First (Pragma_Argument_Associations (gnat_node
));
8165 else if (comp_p
&& Nkind (gnat_node
) == N_Pragma
8166 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)))
8167 == Pragma_Atomic_Components
))
8168 gnat_error_point
= First (Pragma_Argument_Associations (gnat_node
));
8172 post_error_ne ("atomic access to component of & cannot be guaranteed",
8173 gnat_error_point
, gnat_entity
);
8175 post_error_ne ("atomic access to & cannot be guaranteed",
8176 gnat_error_point
, gnat_entity
);
8180 /* Helper for the intrin compatibility checks family. Evaluate whether
8181 two types are definitely incompatible. */
8184 intrin_types_incompatible_p (tree t1
, tree t2
)
8186 enum tree_code code
;
8188 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
8191 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
8194 if (TREE_CODE (t1
) != TREE_CODE (t2
))
8197 code
= TREE_CODE (t1
);
8203 return TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
);
8206 case REFERENCE_TYPE
:
8207 /* Assume designated types are ok. We'd need to account for char * and
8208 void * variants to do better, which could rapidly get messy and isn't
8209 clearly worth the effort. */
8219 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8220 on the Ada/builtin argument lists for the INB binding. */
8223 intrin_arglists_compatible_p (intrin_binding_t
* inb
)
8225 function_args_iterator ada_iter
, btin_iter
;
8227 function_args_iter_init (&ada_iter
, inb
->ada_fntype
);
8228 function_args_iter_init (&btin_iter
, inb
->btin_fntype
);
8230 /* Sequence position of the last argument we checked. */
8235 tree ada_type
= function_args_iter_cond (&ada_iter
);
8236 tree btin_type
= function_args_iter_cond (&btin_iter
);
8238 /* If we've exhausted both lists simultaneously, we're done. */
8239 if (ada_type
== NULL_TREE
&& btin_type
== NULL_TREE
)
8242 /* If one list is shorter than the other, they fail to match. */
8243 if (ada_type
== NULL_TREE
|| btin_type
== NULL_TREE
)
8246 /* If we're done with the Ada args and not with the internal builtin
8247 args, or the other way around, complain. */
8248 if (ada_type
== void_type_node
8249 && btin_type
!= void_type_node
)
8251 post_error ("?Ada arguments list too short!", inb
->gnat_entity
);
8255 if (btin_type
== void_type_node
8256 && ada_type
!= void_type_node
)
8258 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8259 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8263 /* Otherwise, check that types match for the current argument. */
8265 if (intrin_types_incompatible_p (ada_type
, btin_type
))
8267 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8268 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8273 function_args_iter_next (&ada_iter
);
8274 function_args_iter_next (&btin_iter
);
8280 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8281 on the Ada/builtin return values for the INB binding. */
8284 intrin_return_compatible_p (intrin_binding_t
* inb
)
8286 tree ada_return_type
= TREE_TYPE (inb
->ada_fntype
);
8287 tree btin_return_type
= TREE_TYPE (inb
->btin_fntype
);
8289 /* Accept function imported as procedure, common and convenient. */
8290 if (VOID_TYPE_P (ada_return_type
)
8291 && !VOID_TYPE_P (btin_return_type
))
8294 /* If return type is Address (integer type), map it to void *. */
8295 if (Is_Descendent_Of_Address (Etype (inb
->gnat_entity
)))
8296 ada_return_type
= ptr_void_type_node
;
8298 /* Check return types compatibility otherwise. Note that this
8299 handles void/void as well. */
8300 if (intrin_types_incompatible_p (btin_return_type
, ada_return_type
))
8302 post_error ("?intrinsic binding type mismatch on return value!",
8310 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8311 compatible. Issue relevant warnings when they are not.
8313 This is intended as a light check to diagnose the most obvious cases, not
8314 as a full fledged type compatibility predicate. It is the programmer's
8315 responsibility to ensure correctness of the Ada declarations in Imports,
8316 especially when binding straight to a compiler internal. */
8319 intrin_profiles_compatible_p (intrin_binding_t
* inb
)
8321 /* Check compatibility on return values and argument lists, each responsible
8322 for posting warnings as appropriate. Ensure use of the proper sloc for
8325 bool arglists_compatible_p
, return_compatible_p
;
8326 location_t saved_location
= input_location
;
8328 Sloc_to_locus (Sloc (inb
->gnat_entity
), &input_location
);
8330 return_compatible_p
= intrin_return_compatible_p (inb
);
8331 arglists_compatible_p
= intrin_arglists_compatible_p (inb
);
8333 input_location
= saved_location
;
8335 return return_compatible_p
&& arglists_compatible_p
;
8338 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8339 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8340 specified size for this field. POS_LIST is a position list describing
8341 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8345 create_field_decl_from (tree old_field
, tree field_type
, tree record_type
,
8346 tree size
, tree pos_list
,
8347 vec
<subst_pair
> subst_list
)
8349 tree t
= TREE_VALUE (purpose_member (old_field
, pos_list
));
8350 tree pos
= TREE_VEC_ELT (t
, 0), bitpos
= TREE_VEC_ELT (t
, 2);
8351 unsigned int offset_align
= tree_low_cst (TREE_VEC_ELT (t
, 1), 1);
8352 tree new_pos
, new_field
;
8356 if (CONTAINS_PLACEHOLDER_P (pos
))
8357 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8358 pos
= SUBSTITUTE_IN_EXPR (pos
, s
->discriminant
, s
->replacement
);
8360 /* If the position is now a constant, we can set it as the position of the
8361 field when we make it. Otherwise, we need to deal with it specially. */
8362 if (TREE_CONSTANT (pos
))
8363 new_pos
= bit_from_pos (pos
, bitpos
);
8365 new_pos
= NULL_TREE
;
8368 = create_field_decl (DECL_NAME (old_field
), field_type
, record_type
,
8369 size
, new_pos
, DECL_PACKED (old_field
),
8370 !DECL_NONADDRESSABLE_P (old_field
));
8374 normalize_offset (&pos
, &bitpos
, offset_align
);
8375 DECL_FIELD_OFFSET (new_field
) = pos
;
8376 DECL_FIELD_BIT_OFFSET (new_field
) = bitpos
;
8377 SET_DECL_OFFSET_ALIGN (new_field
, offset_align
);
8378 DECL_SIZE (new_field
) = size
;
8379 DECL_SIZE_UNIT (new_field
)
8380 = convert (sizetype
,
8381 size_binop (CEIL_DIV_EXPR
, size
, bitsize_unit_node
));
8382 layout_decl (new_field
, DECL_OFFSET_ALIGN (new_field
));
8385 DECL_INTERNAL_P (new_field
) = DECL_INTERNAL_P (old_field
);
8386 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, old_field
);
8387 DECL_DISCRIMINANT_NUMBER (new_field
) = DECL_DISCRIMINANT_NUMBER (old_field
);
8388 TREE_THIS_VOLATILE (new_field
) = TREE_THIS_VOLATILE (old_field
);
8393 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8394 it is the minimal size the REP_PART must have. */
8397 create_rep_part (tree rep_type
, tree record_type
, tree min_size
)
8401 if (min_size
&& !tree_int_cst_lt (TYPE_SIZE (rep_type
), min_size
))
8402 min_size
= NULL_TREE
;
8404 field
= create_field_decl (get_identifier ("REP"), rep_type
, record_type
,
8405 min_size
, NULL_TREE
, 0, 1);
8406 DECL_INTERNAL_P (field
) = 1;
8411 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8414 get_rep_part (tree record_type
)
8416 tree field
= TYPE_FIELDS (record_type
);
8418 /* The REP part is the first field, internal, another record, and its name
8419 starts with an 'R'. */
8421 && DECL_INTERNAL_P (field
)
8422 && TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
8423 && IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == 'R')
8429 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8432 get_variant_part (tree record_type
)
8436 /* The variant part is the only internal field that is a qualified union. */
8437 for (field
= TYPE_FIELDS (record_type
); field
; field
= DECL_CHAIN (field
))
8438 if (DECL_INTERNAL_P (field
)
8439 && TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
)
8445 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8446 the list of variants to be used and RECORD_TYPE is the type of the parent.
8447 POS_LIST is a position list describing the layout of fields present in
8448 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8452 create_variant_part_from (tree old_variant_part
,
8453 vec
<variant_desc
> variant_list
,
8454 tree record_type
, tree pos_list
,
8455 vec
<subst_pair
> subst_list
)
8457 tree offset
= DECL_FIELD_OFFSET (old_variant_part
);
8458 tree old_union_type
= TREE_TYPE (old_variant_part
);
8459 tree new_union_type
, new_variant_part
;
8460 tree union_field_list
= NULL_TREE
;
8464 /* First create the type of the variant part from that of the old one. */
8465 new_union_type
= make_node (QUAL_UNION_TYPE
);
8466 TYPE_NAME (new_union_type
)
8467 = concat_name (TYPE_NAME (record_type
),
8468 IDENTIFIER_POINTER (DECL_NAME (old_variant_part
)));
8470 /* If the position of the variant part is constant, subtract it from the
8471 size of the type of the parent to get the new size. This manual CSE
8472 reduces the code size when not optimizing. */
8473 if (TREE_CODE (offset
) == INTEGER_CST
)
8475 tree bitpos
= DECL_FIELD_BIT_OFFSET (old_variant_part
);
8476 tree first_bit
= bit_from_pos (offset
, bitpos
);
8477 TYPE_SIZE (new_union_type
)
8478 = size_binop (MINUS_EXPR
, TYPE_SIZE (record_type
), first_bit
);
8479 TYPE_SIZE_UNIT (new_union_type
)
8480 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (record_type
),
8481 byte_from_pos (offset
, bitpos
));
8482 SET_TYPE_ADA_SIZE (new_union_type
,
8483 size_binop (MINUS_EXPR
, TYPE_ADA_SIZE (record_type
),
8485 TYPE_ALIGN (new_union_type
) = TYPE_ALIGN (old_union_type
);
8486 relate_alias_sets (new_union_type
, old_union_type
, ALIAS_SET_COPY
);
8489 copy_and_substitute_in_size (new_union_type
, old_union_type
, subst_list
);
8491 /* Now finish up the new variants and populate the union type. */
8492 FOR_EACH_VEC_ELT_REVERSE (variant_list
, i
, v
)
8494 tree old_field
= v
->field
, new_field
;
8495 tree old_variant
, old_variant_subpart
, new_variant
, field_list
;
8497 /* Skip variants that don't belong to this nesting level. */
8498 if (DECL_CONTEXT (old_field
) != old_union_type
)
8501 /* Retrieve the list of fields already added to the new variant. */
8502 new_variant
= v
->new_type
;
8503 field_list
= TYPE_FIELDS (new_variant
);
8505 /* If the old variant had a variant subpart, we need to create a new
8506 variant subpart and add it to the field list. */
8507 old_variant
= v
->type
;
8508 old_variant_subpart
= get_variant_part (old_variant
);
8509 if (old_variant_subpart
)
8511 tree new_variant_subpart
8512 = create_variant_part_from (old_variant_subpart
, variant_list
,
8513 new_variant
, pos_list
, subst_list
);
8514 DECL_CHAIN (new_variant_subpart
) = field_list
;
8515 field_list
= new_variant_subpart
;
8518 /* Finish up the new variant and create the field. No need for debug
8519 info thanks to the XVS type. */
8520 finish_record_type (new_variant
, nreverse (field_list
), 2, false);
8521 compute_record_mode (new_variant
);
8522 create_type_decl (TYPE_NAME (new_variant
), new_variant
, true, false,
8526 = create_field_decl_from (old_field
, new_variant
, new_union_type
,
8527 TYPE_SIZE (new_variant
),
8528 pos_list
, subst_list
);
8529 DECL_QUALIFIER (new_field
) = v
->qual
;
8530 DECL_INTERNAL_P (new_field
) = 1;
8531 DECL_CHAIN (new_field
) = union_field_list
;
8532 union_field_list
= new_field
;
8535 /* Finish up the union type and create the variant part. No need for debug
8536 info thanks to the XVS type. Note that we don't reverse the field list
8537 because VARIANT_LIST has been traversed in reverse order. */
8538 finish_record_type (new_union_type
, union_field_list
, 2, false);
8539 compute_record_mode (new_union_type
);
8540 create_type_decl (TYPE_NAME (new_union_type
), new_union_type
, true, false,
8544 = create_field_decl_from (old_variant_part
, new_union_type
, record_type
,
8545 TYPE_SIZE (new_union_type
),
8546 pos_list
, subst_list
);
8547 DECL_INTERNAL_P (new_variant_part
) = 1;
8549 /* With multiple discriminants it is possible for an inner variant to be
8550 statically selected while outer ones are not; in this case, the list
8551 of fields of the inner variant is not flattened and we end up with a
8552 qualified union with a single member. Drop the useless container. */
8553 if (!DECL_CHAIN (union_field_list
))
8555 DECL_CONTEXT (union_field_list
) = record_type
;
8556 DECL_FIELD_OFFSET (union_field_list
)
8557 = DECL_FIELD_OFFSET (new_variant_part
);
8558 DECL_FIELD_BIT_OFFSET (union_field_list
)
8559 = DECL_FIELD_BIT_OFFSET (new_variant_part
);
8560 SET_DECL_OFFSET_ALIGN (union_field_list
,
8561 DECL_OFFSET_ALIGN (new_variant_part
));
8562 new_variant_part
= union_field_list
;
8565 return new_variant_part
;
8568 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8569 which are both RECORD_TYPE, after applying the substitutions described
8573 copy_and_substitute_in_size (tree new_type
, tree old_type
,
8574 vec
<subst_pair
> subst_list
)
8579 TYPE_SIZE (new_type
) = TYPE_SIZE (old_type
);
8580 TYPE_SIZE_UNIT (new_type
) = TYPE_SIZE_UNIT (old_type
);
8581 SET_TYPE_ADA_SIZE (new_type
, TYPE_ADA_SIZE (old_type
));
8582 TYPE_ALIGN (new_type
) = TYPE_ALIGN (old_type
);
8583 relate_alias_sets (new_type
, old_type
, ALIAS_SET_COPY
);
8585 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type
)))
8586 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8587 TYPE_SIZE (new_type
)
8588 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type
),
8589 s
->discriminant
, s
->replacement
);
8591 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type
)))
8592 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8593 TYPE_SIZE_UNIT (new_type
)
8594 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type
),
8595 s
->discriminant
, s
->replacement
);
8597 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type
)))
8598 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8600 (new_type
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type
),
8601 s
->discriminant
, s
->replacement
));
8603 /* Finalize the size. */
8604 TYPE_SIZE (new_type
) = variable_size (TYPE_SIZE (new_type
));
8605 TYPE_SIZE_UNIT (new_type
) = variable_size (TYPE_SIZE_UNIT (new_type
));
8608 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8609 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8610 updated by replacing F with R.
8612 The function doesn't update the layout of the type, i.e. it assumes
8613 that the substitution is purely formal. That's why the replacement
8614 value R must itself contain a PLACEHOLDER_EXPR. */
8617 substitute_in_type (tree t
, tree f
, tree r
)
8621 gcc_assert (CONTAINS_PLACEHOLDER_P (r
));
8623 switch (TREE_CODE (t
))
8630 /* First the domain types of arrays. */
8631 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t
))
8632 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t
)))
8634 tree low
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t
), f
, r
);
8635 tree high
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t
), f
, r
);
8637 if (low
== TYPE_GCC_MIN_VALUE (t
) && high
== TYPE_GCC_MAX_VALUE (t
))
8641 TYPE_GCC_MIN_VALUE (nt
) = low
;
8642 TYPE_GCC_MAX_VALUE (nt
) = high
;
8644 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_INDEX_TYPE (t
))
8646 (nt
, substitute_in_type (TYPE_INDEX_TYPE (t
), f
, r
));
8651 /* Then the subtypes. */
8652 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t
))
8653 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t
)))
8655 tree low
= SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t
), f
, r
);
8656 tree high
= SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t
), f
, r
);
8658 if (low
== TYPE_RM_MIN_VALUE (t
) && high
== TYPE_RM_MAX_VALUE (t
))
8662 SET_TYPE_RM_MIN_VALUE (nt
, low
);
8663 SET_TYPE_RM_MAX_VALUE (nt
, high
);
8671 nt
= substitute_in_type (TREE_TYPE (t
), f
, r
);
8672 if (nt
== TREE_TYPE (t
))
8675 return build_complex_type (nt
);
8678 /* These should never show up here. */
8683 tree component
= substitute_in_type (TREE_TYPE (t
), f
, r
);
8684 tree domain
= substitute_in_type (TYPE_DOMAIN (t
), f
, r
);
8686 if (component
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
8689 nt
= build_nonshared_array_type (component
, domain
);
8690 TYPE_ALIGN (nt
) = TYPE_ALIGN (t
);
8691 TYPE_USER_ALIGN (nt
) = TYPE_USER_ALIGN (t
);
8692 SET_TYPE_MODE (nt
, TYPE_MODE (t
));
8693 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
8694 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
8695 TYPE_NONALIASED_COMPONENT (nt
) = TYPE_NONALIASED_COMPONENT (t
);
8696 TYPE_MULTI_ARRAY_P (nt
) = TYPE_MULTI_ARRAY_P (t
);
8697 TYPE_CONVENTION_FORTRAN_P (nt
) = TYPE_CONVENTION_FORTRAN_P (t
);
8703 case QUAL_UNION_TYPE
:
8705 bool changed_field
= false;
8708 /* Start out with no fields, make new fields, and chain them
8709 in. If we haven't actually changed the type of any field,
8710 discard everything we've done and return the old type. */
8712 TYPE_FIELDS (nt
) = NULL_TREE
;
8714 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
8716 tree new_field
= copy_node (field
), new_n
;
8718 new_n
= substitute_in_type (TREE_TYPE (field
), f
, r
);
8719 if (new_n
!= TREE_TYPE (field
))
8721 TREE_TYPE (new_field
) = new_n
;
8722 changed_field
= true;
8725 new_n
= SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field
), f
, r
);
8726 if (new_n
!= DECL_FIELD_OFFSET (field
))
8728 DECL_FIELD_OFFSET (new_field
) = new_n
;
8729 changed_field
= true;
8732 /* Do the substitution inside the qualifier, if any. */
8733 if (TREE_CODE (t
) == QUAL_UNION_TYPE
)
8735 new_n
= SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field
), f
, r
);
8736 if (new_n
!= DECL_QUALIFIER (field
))
8738 DECL_QUALIFIER (new_field
) = new_n
;
8739 changed_field
= true;
8743 DECL_CONTEXT (new_field
) = nt
;
8744 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, field
);
8746 DECL_CHAIN (new_field
) = TYPE_FIELDS (nt
);
8747 TYPE_FIELDS (nt
) = new_field
;
8753 TYPE_FIELDS (nt
) = nreverse (TYPE_FIELDS (nt
));
8754 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
8755 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
8756 SET_TYPE_ADA_SIZE (nt
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t
), f
, r
));
8765 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8766 needed to represent the object. */
8769 rm_size (tree gnu_type
)
8771 /* For integral types, we store the RM size explicitly. */
8772 if (INTEGRAL_TYPE_P (gnu_type
) && TYPE_RM_SIZE (gnu_type
))
8773 return TYPE_RM_SIZE (gnu_type
);
8775 /* Return the RM size of the actual data plus the size of the template. */
8776 if (TREE_CODE (gnu_type
) == RECORD_TYPE
8777 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
8779 size_binop (PLUS_EXPR
,
8780 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
)))),
8781 DECL_SIZE (TYPE_FIELDS (gnu_type
)));
8783 /* For record or union types, we store the size explicitly. */
8784 if (RECORD_OR_UNION_TYPE_P (gnu_type
)
8785 && !TYPE_FAT_POINTER_P (gnu_type
)
8786 && TYPE_ADA_SIZE (gnu_type
))
8787 return TYPE_ADA_SIZE (gnu_type
);
8789 /* For other types, this is just the size. */
8790 return TYPE_SIZE (gnu_type
);
8793 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8794 fully-qualified name, possibly with type information encoding.
8795 Otherwise, return the name. */
8798 get_entity_name (Entity_Id gnat_entity
)
8800 Get_Encoded_Name (gnat_entity
);
8801 return get_identifier_with_length (Name_Buffer
, Name_Len
);
8804 /* Return an identifier representing the external name to be used for
8805 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8806 and the specified suffix. */
8809 create_concat_name (Entity_Id gnat_entity
, const char *suffix
)
8811 Entity_Kind kind
= Ekind (gnat_entity
);
8815 String_Template temp
= {1, (int) strlen (suffix
)};
8816 Fat_Pointer fp
= {suffix
, &temp
};
8817 Get_External_Name_With_Suffix (gnat_entity
, fp
);
8820 Get_External_Name (gnat_entity
, 0);
8822 /* A variable using the Stdcall convention lives in a DLL. We adjust
8823 its name to use the jump table, the _imp__NAME contains the address
8824 for the NAME variable. */
8825 if ((kind
== E_Variable
|| kind
== E_Constant
)
8826 && Has_Stdcall_Convention (gnat_entity
))
8828 const int len
= 6 + Name_Len
;
8829 char *new_name
= (char *) alloca (len
+ 1);
8830 strcpy (new_name
, "_imp__");
8831 strcat (new_name
, Name_Buffer
);
8832 return get_identifier_with_length (new_name
, len
);
8835 return get_identifier_with_length (Name_Buffer
, Name_Len
);
8838 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8839 string, return a new IDENTIFIER_NODE that is the concatenation of
8840 the name followed by "___" and the specified suffix. */
8843 concat_name (tree gnu_name
, const char *suffix
)
8845 const int len
= IDENTIFIER_LENGTH (gnu_name
) + 3 + strlen (suffix
);
8846 char *new_name
= (char *) alloca (len
+ 1);
8847 strcpy (new_name
, IDENTIFIER_POINTER (gnu_name
));
8848 strcat (new_name
, "___");
8849 strcat (new_name
, suffix
);
8850 return get_identifier_with_length (new_name
, len
);
8853 #include "gt-ada-decl.h"