1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2014, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License along with GCC; see the file COPYING3. If not see *
19 * <http://www.gnu.org/licenses/>. *
21 * GNAT was originally developed by the GNAT team at New York University. *
22 * Extensive contributions were provided by Ada Core Technologies Inc. *
24 ****************************************************************************/
28 #include "coretypes.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
37 #include "tree-inline.h"
38 #include "diagnostic-core.h"
56 /* "stdcall" and "thiscall" conventions should be processed in a specific way
57 on 32-bit x86/Windows only. The macros below are helpers to avoid having
58 to check for a Windows specific attribute throughout this unit. */
60 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
62 #define Has_Stdcall_Convention(E) \
63 (!TARGET_64BIT && Convention (E) == Convention_Stdcall)
64 #define Has_Thiscall_Convention(E) \
65 (!TARGET_64BIT && is_cplusplus_method (E))
67 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
68 #define Has_Thiscall_Convention(E) (is_cplusplus_method (E))
71 #define Has_Stdcall_Convention(E) 0
72 #define Has_Thiscall_Convention(E) 0
75 #define STDCALL_PREFIX "_imp__"
77 /* Stack realignment is necessary for functions with foreign conventions when
78 the ABI doesn't mandate as much as what the compiler assumes - that is, up
79 to PREFERRED_STACK_BOUNDARY.
81 Such realignment can be requested with a dedicated function type attribute
82 on the targets that support it. We define FOREIGN_FORCE_REALIGN_STACK to
83 characterize the situations where the attribute should be set. We rely on
84 compiler configuration settings for 'main' to decide. */
86 #ifdef MAIN_STACK_BOUNDARY
87 #define FOREIGN_FORCE_REALIGN_STACK \
88 (MAIN_STACK_BOUNDARY < PREFERRED_STACK_BOUNDARY)
90 #define FOREIGN_FORCE_REALIGN_STACK 0
95 struct incomplete
*next
;
100 /* These variables are used to defer recursively expanding incomplete types
101 while we are processing an array, a record or a subprogram type. */
102 static int defer_incomplete_level
= 0;
103 static struct incomplete
*defer_incomplete_list
;
105 /* This variable is used to delay expanding From_Limited_With types until the
107 static struct incomplete
*defer_limited_with
;
109 typedef struct subst_pair_d
{
115 typedef struct variant_desc_d
{
116 /* The type of the variant. */
119 /* The associated field. */
122 /* The value of the qualifier. */
125 /* The type of the variant after transformation. */
130 /* A hash table used to cache the result of annotate_value. */
131 static GTY ((if_marked ("tree_int_map_marked_p"),
132 param_is (struct tree_int_map
))) htab_t annotate_value_cache
;
134 static bool allocatable_size_p (tree
, bool);
135 static void prepend_one_attribute (struct attrib
**,
136 enum attr_type
, tree
, tree
, Node_Id
);
137 static void prepend_one_attribute_pragma (struct attrib
**, Node_Id
);
138 static void prepend_attributes (struct attrib
**, Entity_Id
);
139 static tree
elaborate_expression (Node_Id
, Entity_Id
, tree
, bool, bool, bool);
140 static bool type_has_variable_size (tree
);
141 static tree
elaborate_expression_1 (tree
, Entity_Id
, tree
, bool, bool);
142 static tree
elaborate_expression_2 (tree
, Entity_Id
, tree
, bool, bool,
144 static tree
gnat_to_gnu_component_type (Entity_Id
, bool, bool);
145 static tree
gnat_to_gnu_param (Entity_Id
, Mechanism_Type
, Entity_Id
, bool,
147 static tree
gnat_to_gnu_field (Entity_Id
, tree
, int, bool, bool);
148 static bool same_discriminant_p (Entity_Id
, Entity_Id
);
149 static bool array_type_has_nonaliased_component (tree
, Entity_Id
);
150 static bool compile_time_known_address_p (Node_Id
);
151 static bool cannot_be_superflat_p (Node_Id
);
152 static bool constructor_address_p (tree
);
153 static int compare_field_bitpos (const PTR
, const PTR
);
154 static bool components_to_record (tree
, Node_Id
, tree
, int, bool, bool, bool,
155 bool, bool, bool, bool, bool, tree
, tree
*);
156 static Uint
annotate_value (tree
);
157 static void annotate_rep (Entity_Id
, tree
);
158 static tree
build_position_list (tree
, bool, tree
, tree
, unsigned int, tree
);
159 static vec
<subst_pair
> build_subst_list (Entity_Id
, Entity_Id
, bool);
160 static vec
<variant_desc
> build_variant_list (tree
,
163 static tree
validate_size (Uint
, tree
, Entity_Id
, enum tree_code
, bool, bool);
164 static void set_rm_size (Uint
, tree
, Entity_Id
);
165 static unsigned int validate_alignment (Uint
, Entity_Id
, unsigned int);
166 static void check_ok_for_atomic (tree
, Entity_Id
, bool);
167 static tree
create_field_decl_from (tree
, tree
, tree
, tree
, tree
,
169 static tree
create_rep_part (tree
, tree
, tree
);
170 static tree
get_rep_part (tree
);
171 static tree
create_variant_part_from (tree
, vec
<variant_desc
> , tree
,
172 tree
, vec
<subst_pair
> );
173 static void copy_and_substitute_in_size (tree
, tree
, vec
<subst_pair
> );
175 /* The relevant constituents of a subprogram binding to a GCC builtin. Used
176 to pass around calls performing profile compatibility checks. */
179 Entity_Id gnat_entity
; /* The Ada subprogram entity. */
180 tree ada_fntype
; /* The corresponding GCC type node. */
181 tree btin_fntype
; /* The GCC builtin function type node. */
184 static bool intrin_profiles_compatible_p (intrin_binding_t
*);
186 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
187 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
188 and associate the ..._DECL node with the input GNAT defining identifier.
190 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
191 initial value (in GCC tree form). This is optional for a variable. For
192 a renamed entity, GNU_EXPR gives the object being renamed.
194 DEFINITION is nonzero if this call is intended for a definition. This is
195 used for separate compilation where it is necessary to know whether an
196 external declaration or a definition must be created if the GCC equivalent
197 was not created previously. The value of 1 is normally used for a nonzero
198 DEFINITION, but a value of 2 is used in special circumstances, defined in
202 gnat_to_gnu_entity (Entity_Id gnat_entity
, tree gnu_expr
, int definition
)
204 /* Contains the kind of the input GNAT node. */
205 const Entity_Kind kind
= Ekind (gnat_entity
);
206 /* True if this is a type. */
207 const bool is_type
= IN (kind
, Type_Kind
);
208 /* True if debug info is requested for this entity. */
209 const bool debug_info_p
= Needs_Debug_Info (gnat_entity
);
210 /* True if this entity is to be considered as imported. */
211 const bool imported_p
212 = (Is_Imported (gnat_entity
) && No (Address_Clause (gnat_entity
)));
213 /* For a type, contains the equivalent GNAT node to be used in gigi. */
214 Entity_Id gnat_equiv_type
= Empty
;
215 /* Temporary used to walk the GNAT tree. */
217 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
218 This node will be associated with the GNAT node by calling at the end
219 of the `switch' statement. */
220 tree gnu_decl
= NULL_TREE
;
221 /* Contains the GCC type to be used for the GCC node. */
222 tree gnu_type
= NULL_TREE
;
223 /* Contains the GCC size tree to be used for the GCC node. */
224 tree gnu_size
= NULL_TREE
;
225 /* Contains the GCC name to be used for the GCC node. */
226 tree gnu_entity_name
;
227 /* True if we have already saved gnu_decl as a GNAT association. */
229 /* True if we incremented defer_incomplete_level. */
230 bool this_deferred
= false;
231 /* True if we incremented force_global. */
232 bool this_global
= false;
233 /* True if we should check to see if elaborated during processing. */
234 bool maybe_present
= false;
235 /* True if we made GNU_DECL and its type here. */
236 bool this_made_decl
= false;
237 /* Size and alignment of the GCC node, if meaningful. */
238 unsigned int esize
= 0, align
= 0;
239 /* Contains the list of attributes directly attached to the entity. */
240 struct attrib
*attr_list
= NULL
;
242 /* Since a use of an Itype is a definition, process it as such if it
243 is not in a with'ed unit. */
246 && Is_Itype (gnat_entity
)
247 && !present_gnu_tree (gnat_entity
)
248 && In_Extended_Main_Code_Unit (gnat_entity
))
250 /* Ensure that we are in a subprogram mentioned in the Scope chain of
251 this entity, our current scope is global, or we encountered a task
252 or entry (where we can't currently accurately check scoping). */
253 if (!current_function_decl
254 || DECL_ELABORATION_PROC_P (current_function_decl
))
256 process_type (gnat_entity
);
257 return get_gnu_tree (gnat_entity
);
260 for (gnat_temp
= Scope (gnat_entity
);
262 gnat_temp
= Scope (gnat_temp
))
264 if (Is_Type (gnat_temp
))
265 gnat_temp
= Underlying_Type (gnat_temp
);
267 if (Ekind (gnat_temp
) == E_Subprogram_Body
)
269 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp
)));
271 if (IN (Ekind (gnat_temp
), Subprogram_Kind
)
272 && Present (Protected_Body_Subprogram (gnat_temp
)))
273 gnat_temp
= Protected_Body_Subprogram (gnat_temp
);
275 if (Ekind (gnat_temp
) == E_Entry
276 || Ekind (gnat_temp
) == E_Entry_Family
277 || Ekind (gnat_temp
) == E_Task_Type
278 || (IN (Ekind (gnat_temp
), Subprogram_Kind
)
279 && present_gnu_tree (gnat_temp
)
280 && (current_function_decl
281 == gnat_to_gnu_entity (gnat_temp
, NULL_TREE
, 0))))
283 process_type (gnat_entity
);
284 return get_gnu_tree (gnat_entity
);
288 /* This abort means the Itype has an incorrect scope, i.e. that its
289 scope does not correspond to the subprogram it is declared in. */
293 /* If we've already processed this entity, return what we got last time.
294 If we are defining the node, we should not have already processed it.
295 In that case, we will abort below when we try to save a new GCC tree
296 for this object. We also need to handle the case of getting a dummy
297 type when a Full_View exists but be careful so as not to trigger its
298 premature elaboration. */
299 if ((!definition
|| (is_type
&& imported_p
))
300 && present_gnu_tree (gnat_entity
))
302 gnu_decl
= get_gnu_tree (gnat_entity
);
304 if (TREE_CODE (gnu_decl
) == TYPE_DECL
305 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
))
306 && IN (kind
, Incomplete_Or_Private_Kind
)
307 && Present (Full_View (gnat_entity
))
308 && (present_gnu_tree (Full_View (gnat_entity
))
309 || No (Freeze_Node (Full_View (gnat_entity
)))))
312 = gnat_to_gnu_entity (Full_View (gnat_entity
), NULL_TREE
, 0);
313 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
314 save_gnu_tree (gnat_entity
, gnu_decl
, false);
320 /* If this is a numeric or enumeral type, or an access type, a nonzero Esize
321 must be specified unless it was specified by the programmer. Exceptions
322 are for access-to-protected-subprogram types and all access subtypes, as
323 another GNAT type is used to lay out the GCC type for them. */
324 gcc_assert (!Unknown_Esize (gnat_entity
)
325 || Has_Size_Clause (gnat_entity
)
326 || (!IN (kind
, Numeric_Kind
)
327 && !IN (kind
, Enumeration_Kind
)
328 && (!IN (kind
, Access_Kind
)
329 || kind
== E_Access_Protected_Subprogram_Type
330 || kind
== E_Anonymous_Access_Protected_Subprogram_Type
331 || kind
== E_Access_Subtype
332 || type_annotate_only
)));
334 /* The RM size must be specified for all discrete and fixed-point types. */
335 gcc_assert (!(IN (kind
, Discrete_Or_Fixed_Point_Kind
)
336 && Unknown_RM_Size (gnat_entity
)));
338 /* If we get here, it means we have not yet done anything with this entity.
339 If we are not defining it, it must be a type or an entity that is defined
340 elsewhere or externally, otherwise we should have defined it already. */
341 gcc_assert (definition
342 || type_annotate_only
344 || kind
== E_Discriminant
345 || kind
== E_Component
347 || (kind
== E_Constant
&& Present (Full_View (gnat_entity
)))
348 || Is_Public (gnat_entity
));
350 /* Get the name of the entity and set up the line number and filename of
351 the original definition for use in any decl we make. */
352 gnu_entity_name
= get_entity_name (gnat_entity
);
353 Sloc_to_locus (Sloc (gnat_entity
), &input_location
);
355 /* For cases when we are not defining (i.e., we are referencing from
356 another compilation unit) public entities, show we are at global level
357 for the purpose of computing scopes. Don't do this for components or
358 discriminants since the relevant test is whether or not the record is
361 && kind
!= E_Component
362 && kind
!= E_Discriminant
363 && Is_Public (gnat_entity
)
364 && !Is_Statically_Allocated (gnat_entity
))
365 force_global
++, this_global
= true;
367 /* Handle any attributes directly attached to the entity. */
368 if (Has_Gigi_Rep_Item (gnat_entity
))
369 prepend_attributes (&attr_list
, gnat_entity
);
371 /* Do some common processing for types. */
374 /* Compute the equivalent type to be used in gigi. */
375 gnat_equiv_type
= Gigi_Equivalent_Type (gnat_entity
);
377 /* Machine_Attributes on types are expected to be propagated to
378 subtypes. The corresponding Gigi_Rep_Items are only attached
379 to the first subtype though, so we handle the propagation here. */
380 if (Base_Type (gnat_entity
) != gnat_entity
381 && !Is_First_Subtype (gnat_entity
)
382 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity
))))
383 prepend_attributes (&attr_list
,
384 First_Subtype (Base_Type (gnat_entity
)));
386 /* Compute a default value for the size of an elementary type. */
387 if (Known_Esize (gnat_entity
) && Is_Elementary_Type (gnat_entity
))
389 unsigned int max_esize
;
391 gcc_assert (UI_Is_In_Int_Range (Esize (gnat_entity
)));
392 esize
= UI_To_Int (Esize (gnat_entity
));
394 if (IN (kind
, Float_Kind
))
395 max_esize
= fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE
);
396 else if (IN (kind
, Access_Kind
))
397 max_esize
= POINTER_SIZE
* 2;
399 max_esize
= LONG_LONG_TYPE_SIZE
;
401 if (esize
> max_esize
)
409 /* If this is a use of a deferred constant without address clause,
410 get its full definition. */
412 && No (Address_Clause (gnat_entity
))
413 && Present (Full_View (gnat_entity
)))
416 = gnat_to_gnu_entity (Full_View (gnat_entity
), gnu_expr
, 0);
421 /* If we have an external constant that we are not defining, get the
422 expression that is was defined to represent. We may throw it away
423 later if it is not a constant. But do not retrieve the expression
424 if it is an allocator because the designated type might be dummy
427 && !No_Initialization (Declaration_Node (gnat_entity
))
428 && Present (Expression (Declaration_Node (gnat_entity
)))
429 && Nkind (Expression (Declaration_Node (gnat_entity
)))
432 bool went_into_elab_proc
= false;
433 int save_force_global
= force_global
;
435 /* The expression may contain N_Expression_With_Actions nodes and
436 thus object declarations from other units. In this case, even
437 though the expression will eventually be discarded since not a
438 constant, the declarations would be stuck either in the global
439 varpool or in the current scope. Therefore we force the local
440 context and create a fake scope that we'll zap at the end. */
441 if (!current_function_decl
)
443 current_function_decl
= get_elaboration_procedure ();
444 went_into_elab_proc
= true;
449 gnu_expr
= gnat_to_gnu (Expression (Declaration_Node (gnat_entity
)));
452 force_global
= save_force_global
;
453 if (went_into_elab_proc
)
454 current_function_decl
= NULL_TREE
;
457 /* Ignore deferred constant definitions without address clause since
458 they are processed fully in the front-end. If No_Initialization
459 is set, this is not a deferred constant but a constant whose value
460 is built manually. And constants that are renamings are handled
464 && No (Address_Clause (gnat_entity
))
465 && !No_Initialization (Declaration_Node (gnat_entity
))
466 && No (Renamed_Object (gnat_entity
)))
468 gnu_decl
= error_mark_node
;
473 /* Ignore constant definitions already marked with the error node. See
474 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
477 && present_gnu_tree (gnat_entity
)
478 && get_gnu_tree (gnat_entity
) == error_mark_node
)
480 maybe_present
= true;
487 /* We used to special case VMS exceptions here to directly map them to
488 their associated condition code. Since this code had to be masked
489 dynamically to strip off the severity bits, this caused trouble in
490 the GCC/ZCX case because the "type" pointers we store in the tables
491 have to be static. We now don't special case here anymore, and let
492 the regular processing take place, which leaves us with a regular
493 exception data object for VMS exceptions too. The condition code
494 mapping is taken care of by the front end and the bitmasking by the
501 /* The GNAT record where the component was defined. */
502 Entity_Id gnat_record
= Underlying_Type (Scope (gnat_entity
));
504 /* If the entity is an inherited component (in the case of extended
505 tagged record types), just return the original entity, which must
506 be a FIELD_DECL. Likewise for discriminants. If the entity is a
507 non-girder discriminant (in the case of derived untagged record
508 types), return the stored discriminant it renames. */
509 if (Present (Original_Record_Component (gnat_entity
))
510 && Original_Record_Component (gnat_entity
) != gnat_entity
)
513 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity
),
514 gnu_expr
, definition
);
519 /* If this is a discriminant of an extended tagged type used to rename
520 a discriminant of the parent type, return the latter. */
521 else if (Present (Corresponding_Discriminant (gnat_entity
)))
523 /* If the derived type is untagged, then this is a non-girder
524 discriminant and its Original_Record_Component must point to
525 the stored discriminant it renames (i.e. we should have taken
526 the previous branch). */
527 gcc_assert (Is_Tagged_Type (gnat_record
));
530 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity
),
531 gnu_expr
, definition
);
536 /* Otherwise, if we are not defining this and we have no GCC type
537 for the containing record, make one for it. Then we should
538 have made our own equivalent. */
539 else if (!definition
&& !present_gnu_tree (gnat_record
))
541 /* ??? If this is in a record whose scope is a protected
542 type and we have an Original_Record_Component, use it.
543 This is a workaround for major problems in protected type
545 Entity_Id Scop
= Scope (Scope (gnat_entity
));
546 if ((Is_Protected_Type (Scop
)
547 || (Is_Private_Type (Scop
)
548 && Present (Full_View (Scop
))
549 && Is_Protected_Type (Full_View (Scop
))))
550 && Present (Original_Record_Component (gnat_entity
)))
553 = gnat_to_gnu_entity (Original_Record_Component
560 gnat_to_gnu_entity (Scope (gnat_entity
), NULL_TREE
, 0);
561 gnu_decl
= get_gnu_tree (gnat_entity
);
567 /* Here we have no GCC type and this is a reference rather than a
568 definition. This should never happen. Most likely the cause is
569 reference before declaration in the GNAT tree for gnat_entity. */
573 case E_Loop_Parameter
:
574 case E_Out_Parameter
:
577 /* Simple variables, loop variables, Out parameters and exceptions. */
580 /* Always create a variable for volatile objects and variables seen
581 constant but with a Linker_Section pragma. */
583 = ((kind
== E_Constant
|| kind
== E_Variable
)
584 && Is_True_Constant (gnat_entity
)
585 && !(kind
== E_Variable
586 && Present (Linker_Section_Pragma (gnat_entity
)))
587 && !Treat_As_Volatile (gnat_entity
)
588 && (((Nkind (Declaration_Node (gnat_entity
))
589 == N_Object_Declaration
)
590 && Present (Expression (Declaration_Node (gnat_entity
))))
591 || Present (Renamed_Object (gnat_entity
))
593 bool inner_const_flag
= const_flag
;
594 bool static_p
= Is_Statically_Allocated (gnat_entity
);
595 bool mutable_p
= false;
596 bool used_by_ref
= false;
597 tree gnu_ext_name
= NULL_TREE
;
598 tree renamed_obj
= NULL_TREE
;
599 tree gnu_object_size
;
601 if (Present (Renamed_Object (gnat_entity
)) && !definition
)
603 if (kind
== E_Exception
)
604 gnu_expr
= gnat_to_gnu_entity (Renamed_Entity (gnat_entity
),
607 gnu_expr
= gnat_to_gnu (Renamed_Object (gnat_entity
));
610 /* Get the type after elaborating the renamed object. */
611 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
613 /* If this is a standard exception definition, then use the standard
614 exception type. This is necessary to make sure that imported and
615 exported views of exceptions are properly merged in LTO mode. */
616 if (TREE_CODE (TYPE_NAME (gnu_type
)) == TYPE_DECL
617 && DECL_NAME (TYPE_NAME (gnu_type
)) == exception_data_name_id
)
618 gnu_type
= except_type_node
;
620 /* For a debug renaming declaration, build a debug-only entity. */
621 if (Present (Debug_Renaming_Link (gnat_entity
)))
623 /* Force a non-null value to make sure the symbol is retained. */
624 tree value
= build1 (INDIRECT_REF
, gnu_type
,
626 build_pointer_type (gnu_type
),
627 integer_minus_one_node
));
628 gnu_decl
= build_decl (input_location
,
629 VAR_DECL
, gnu_entity_name
, gnu_type
);
630 SET_DECL_VALUE_EXPR (gnu_decl
, value
);
631 DECL_HAS_VALUE_EXPR_P (gnu_decl
) = 1;
632 gnat_pushdecl (gnu_decl
, gnat_entity
);
636 /* If this is a loop variable, its type should be the base type.
637 This is because the code for processing a loop determines whether
638 a normal loop end test can be done by comparing the bounds of the
639 loop against those of the base type, which is presumed to be the
640 size used for computation. But this is not correct when the size
641 of the subtype is smaller than the type. */
642 if (kind
== E_Loop_Parameter
)
643 gnu_type
= get_base_type (gnu_type
);
645 /* Reject non-renamed objects whose type is an unconstrained array or
646 any object whose type is a dummy type or void. */
647 if ((TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
648 && No (Renamed_Object (gnat_entity
)))
649 || TYPE_IS_DUMMY_P (gnu_type
)
650 || TREE_CODE (gnu_type
) == VOID_TYPE
)
652 gcc_assert (type_annotate_only
);
655 return error_mark_node
;
658 /* If an alignment is specified, use it if valid. Note that exceptions
659 are objects but don't have an alignment. We must do this before we
660 validate the size, since the alignment can affect the size. */
661 if (kind
!= E_Exception
&& Known_Alignment (gnat_entity
))
663 gcc_assert (Present (Alignment (gnat_entity
)));
665 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
666 TYPE_ALIGN (gnu_type
));
668 /* No point in changing the type if there is an address clause
669 as the final type of the object will be a reference type. */
670 if (Present (Address_Clause (gnat_entity
)))
674 tree orig_type
= gnu_type
;
677 = maybe_pad_type (gnu_type
, NULL_TREE
, align
, gnat_entity
,
678 false, false, definition
, true);
680 /* If a padding record was made, declare it now since it will
681 never be declared otherwise. This is necessary to ensure
682 that its subtrees are properly marked. */
683 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
684 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true,
685 debug_info_p
, gnat_entity
);
689 /* If we are defining the object, see if it has a Size and validate it
690 if so. If we are not defining the object and a Size clause applies,
691 simply retrieve the value. We don't want to ignore the clause and
692 it is expected to have been validated already. Then get the new
695 gnu_size
= validate_size (Esize (gnat_entity
), gnu_type
,
696 gnat_entity
, VAR_DECL
, false,
697 Has_Size_Clause (gnat_entity
));
698 else if (Has_Size_Clause (gnat_entity
))
699 gnu_size
= UI_To_gnu (Esize (gnat_entity
), bitsizetype
);
704 = make_type_from_size (gnu_type
, gnu_size
,
705 Has_Biased_Representation (gnat_entity
));
707 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0))
708 gnu_size
= NULL_TREE
;
711 /* If this object has self-referential size, it must be a record with
712 a default discriminant. We are supposed to allocate an object of
713 the maximum size in this case, unless it is a constant with an
714 initializing expression, in which case we can get the size from
715 that. Note that the resulting size may still be a variable, so
716 this may end up with an indirect allocation. */
717 if (No (Renamed_Object (gnat_entity
))
718 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
720 if (gnu_expr
&& kind
== E_Constant
)
722 tree size
= TYPE_SIZE (TREE_TYPE (gnu_expr
));
723 if (CONTAINS_PLACEHOLDER_P (size
))
725 /* If the initializing expression is itself a constant,
726 despite having a nominal type with self-referential
727 size, we can get the size directly from it. */
728 if (TREE_CODE (gnu_expr
) == COMPONENT_REF
730 (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0)))
731 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == VAR_DECL
732 && (TREE_READONLY (TREE_OPERAND (gnu_expr
, 0))
733 || DECL_READONLY_ONCE_ELAB
734 (TREE_OPERAND (gnu_expr
, 0))))
735 gnu_size
= DECL_SIZE (TREE_OPERAND (gnu_expr
, 0));
738 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, gnu_expr
);
743 /* We may have no GNU_EXPR because No_Initialization is
744 set even though there's an Expression. */
745 else if (kind
== E_Constant
746 && (Nkind (Declaration_Node (gnat_entity
))
747 == N_Object_Declaration
)
748 && Present (Expression (Declaration_Node (gnat_entity
))))
750 = TYPE_SIZE (gnat_to_gnu_type
752 (Expression (Declaration_Node (gnat_entity
)))));
755 gnu_size
= max_size (TYPE_SIZE (gnu_type
), true);
759 /* If we are at global level and the size isn't constant, call
760 elaborate_expression_1 to make a variable for it rather than
761 calculating it each time. */
762 if (global_bindings_p () && !TREE_CONSTANT (gnu_size
))
763 gnu_size
= elaborate_expression_1 (gnu_size
, gnat_entity
,
764 get_identifier ("SIZE"),
768 /* If the size is zero byte, make it one byte since some linkers have
769 troubles with zero-sized objects. If the object will have a
770 template, that will make it nonzero so don't bother. Also avoid
771 doing that for an object renaming or an object with an address
772 clause, as we would lose useful information on the view size
773 (e.g. for null array slices) and we are not allocating the object
776 && integer_zerop (gnu_size
)
777 && !TREE_OVERFLOW (gnu_size
))
778 || (TYPE_SIZE (gnu_type
)
779 && integer_zerop (TYPE_SIZE (gnu_type
))
780 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type
))))
781 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
782 && No (Renamed_Object (gnat_entity
))
783 && No (Address_Clause (gnat_entity
)))
784 gnu_size
= bitsize_unit_node
;
786 /* If this is an object with no specified size and alignment, and
787 if either it is atomic or we are not optimizing alignment for
788 space and it is composite and not an exception, an Out parameter
789 or a reference to another object, and the size of its type is a
790 constant, set the alignment to the smallest one which is not
791 smaller than the size, with an appropriate cap. */
792 if (!gnu_size
&& align
== 0
793 && (Is_Atomic (gnat_entity
)
794 || (!Optimize_Alignment_Space (gnat_entity
)
795 && kind
!= E_Exception
796 && kind
!= E_Out_Parameter
797 && Is_Composite_Type (Etype (gnat_entity
))
798 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
799 && !Is_Exported (gnat_entity
)
801 && No (Renamed_Object (gnat_entity
))
802 && No (Address_Clause (gnat_entity
))))
803 && TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
)
805 unsigned int size_cap
, align_cap
;
807 /* No point in promoting the alignment if this doesn't prevent
808 BLKmode access to the object, in particular block copy, as
809 this will for example disable the NRV optimization for it.
810 No point in jumping through all the hoops needed in order
811 to support BIGGEST_ALIGNMENT if we don't really have to.
812 So we cap to the smallest alignment that corresponds to
813 a known efficient memory access pattern of the target. */
814 if (Is_Atomic (gnat_entity
))
817 align_cap
= BIGGEST_ALIGNMENT
;
821 size_cap
= MAX_FIXED_MODE_SIZE
;
822 align_cap
= get_mode_alignment (ptr_mode
);
825 if (!tree_fits_uhwi_p (TYPE_SIZE (gnu_type
))
826 || compare_tree_int (TYPE_SIZE (gnu_type
), size_cap
) > 0)
828 else if (compare_tree_int (TYPE_SIZE (gnu_type
), align_cap
) > 0)
831 align
= ceil_pow2 (tree_to_uhwi (TYPE_SIZE (gnu_type
)));
833 /* But make sure not to under-align the object. */
834 if (align
<= TYPE_ALIGN (gnu_type
))
837 /* And honor the minimum valid atomic alignment, if any. */
838 #ifdef MINIMUM_ATOMIC_ALIGNMENT
839 else if (align
< MINIMUM_ATOMIC_ALIGNMENT
)
840 align
= MINIMUM_ATOMIC_ALIGNMENT
;
844 /* If the object is set to have atomic components, find the component
845 type and validate it.
847 ??? Note that we ignore Has_Volatile_Components on objects; it's
848 not at all clear what to do in that case. */
849 if (Has_Atomic_Components (gnat_entity
))
851 tree gnu_inner
= (TREE_CODE (gnu_type
) == ARRAY_TYPE
852 ? TREE_TYPE (gnu_type
) : gnu_type
);
854 while (TREE_CODE (gnu_inner
) == ARRAY_TYPE
855 && TYPE_MULTI_ARRAY_P (gnu_inner
))
856 gnu_inner
= TREE_TYPE (gnu_inner
);
858 check_ok_for_atomic (gnu_inner
, gnat_entity
, true);
861 /* Now check if the type of the object allows atomic access. Note
862 that we must test the type, even if this object has size and
863 alignment to allow such access, because we will be going inside
864 the padded record to assign to the object. We could fix this by
865 always copying via an intermediate value, but it's not clear it's
867 if (Is_Atomic (gnat_entity
))
868 check_ok_for_atomic (gnu_type
, gnat_entity
, false);
870 /* If this is an aliased object with an unconstrained nominal subtype,
871 make a type that includes the template. */
872 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
873 && (Is_Array_Type (Etype (gnat_entity
))
874 || (Is_Private_Type (Etype (gnat_entity
))
875 && Is_Array_Type (Full_View (Etype (gnat_entity
)))))
876 && !type_annotate_only
)
879 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
881 = build_unc_object_type_from_ptr (TREE_TYPE (gnu_array
),
883 concat_name (gnu_entity_name
,
888 /* ??? If this is an object of CW type initialized to a value, try to
889 ensure that the object is sufficient aligned for this value, but
890 without pessimizing the allocation. This is a kludge necessary
891 because we don't support dynamic alignment. */
893 && Ekind (Etype (gnat_entity
)) == E_Class_Wide_Subtype
894 && No (Renamed_Object (gnat_entity
))
895 && No (Address_Clause (gnat_entity
)))
896 align
= get_target_system_allocator_alignment () * BITS_PER_UNIT
;
898 #ifdef MINIMUM_ATOMIC_ALIGNMENT
899 /* If the size is a constant and no alignment is specified, force
900 the alignment to be the minimum valid atomic alignment. The
901 restriction on constant size avoids problems with variable-size
902 temporaries; if the size is variable, there's no issue with
903 atomic access. Also don't do this for a constant, since it isn't
904 necessary and can interfere with constant replacement. Finally,
905 do not do it for Out parameters since that creates an
906 size inconsistency with In parameters. */
908 && MINIMUM_ATOMIC_ALIGNMENT
> TYPE_ALIGN (gnu_type
)
909 && !FLOAT_TYPE_P (gnu_type
)
910 && !const_flag
&& No (Renamed_Object (gnat_entity
))
911 && !imported_p
&& No (Address_Clause (gnat_entity
))
912 && kind
!= E_Out_Parameter
913 && (gnu_size
? TREE_CODE (gnu_size
) == INTEGER_CST
914 : TREE_CODE (TYPE_SIZE (gnu_type
)) == INTEGER_CST
))
915 align
= MINIMUM_ATOMIC_ALIGNMENT
;
918 /* Make a new type with the desired size and alignment, if needed.
919 But do not take into account alignment promotions to compute the
920 size of the object. */
921 gnu_object_size
= gnu_size
? gnu_size
: TYPE_SIZE (gnu_type
);
922 if (gnu_size
|| align
> 0)
924 tree orig_type
= gnu_type
;
926 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
927 false, false, definition
, true);
929 /* If a padding record was made, declare it now since it will
930 never be declared otherwise. This is necessary to ensure
931 that its subtrees are properly marked. */
932 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
933 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true,
934 debug_info_p
, gnat_entity
);
937 /* If this is a renaming, avoid as much as possible to create a new
938 object. However, in several cases, creating it is required.
939 This processing needs to be applied to the raw expression so
940 as to make it more likely to rename the underlying object. */
941 if (Present (Renamed_Object (gnat_entity
)))
943 bool create_normal_object
= false;
945 /* If the renamed object had padding, strip off the reference
946 to the inner object and reset our type. */
947 if ((TREE_CODE (gnu_expr
) == COMPONENT_REF
948 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr
, 0))))
949 /* Strip useless conversions around the object. */
950 || gnat_useless_type_conversion (gnu_expr
))
952 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
953 gnu_type
= TREE_TYPE (gnu_expr
);
956 /* Or else, if the renamed object has an unconstrained type with
957 default discriminant, use the padded type. */
958 else if (TYPE_IS_PADDING_P (TREE_TYPE (gnu_expr
))
959 && TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_expr
)))
961 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
962 gnu_type
= TREE_TYPE (gnu_expr
);
964 /* Case 1: If this is a constant renaming stemming from a function
965 call, treat it as a normal object whose initial value is what is
966 being renamed. RM 3.3 says that the result of evaluating a
967 function call is a constant object. Treat constant literals
968 the same way. As a consequence, it can be the inner object of
969 a constant renaming. In this case, the renaming must be fully
970 instantiated, i.e. it cannot be a mere reference to (part of) an
974 tree inner_object
= gnu_expr
;
975 while (handled_component_p (inner_object
))
976 inner_object
= TREE_OPERAND (inner_object
, 0);
977 if (TREE_CODE (inner_object
) == CALL_EXPR
978 || CONSTANT_CLASS_P (inner_object
))
979 create_normal_object
= true;
982 /* Otherwise, see if we can proceed with a stabilized version of
983 the renamed entity or if we need to make a new object. */
984 if (!create_normal_object
)
986 tree maybe_stable_expr
= NULL_TREE
;
989 /* Case 2: If the renaming entity need not be materialized and
990 the renamed expression is something we can stabilize, use
991 that for the renaming. At the global level, we can only do
992 this if we know no SAVE_EXPRs need be made, because the
993 expression we return might be used in arbitrary conditional
994 branches so we must force the evaluation of the SAVE_EXPRs
995 immediately and this requires a proper function context.
996 Note that an external constant is at the global level. */
997 if (!Materialize_Entity (gnat_entity
)
998 && (!((!definition
&& kind
== E_Constant
)
999 || global_bindings_p ())
1000 || (staticp (gnu_expr
)
1001 && !TREE_SIDE_EFFECTS (gnu_expr
))))
1004 = gnat_stabilize_reference (gnu_expr
, true, &stable
);
1008 /* ??? No DECL_EXPR is created so we need to mark
1009 the expression manually lest it is shared. */
1010 if ((!definition
&& kind
== E_Constant
)
1011 || global_bindings_p ())
1012 MARK_VISITED (maybe_stable_expr
);
1013 gnu_decl
= maybe_stable_expr
;
1014 save_gnu_tree (gnat_entity
, gnu_decl
, true);
1016 annotate_object (gnat_entity
, gnu_type
, NULL_TREE
,
1018 /* This assertion will fail if the renamed object
1019 isn't aligned enough as to make it possible to
1020 honor the alignment set on the renaming. */
1023 unsigned int renamed_align
1025 ? DECL_ALIGN (gnu_decl
)
1026 : TYPE_ALIGN (TREE_TYPE (gnu_decl
));
1027 gcc_assert (renamed_align
>= align
);
1032 /* The stabilization failed. Keep maybe_stable_expr
1033 untouched here to let the pointer case below know
1034 about that failure. */
1037 /* Case 3: Make this into a constant pointer to the object we
1038 are to rename and attach the object to the pointer if it is
1039 something we can stabilize.
1041 From the proper scope, attached objects will be referenced
1042 directly instead of indirectly via the pointer to avoid
1043 subtle aliasing problems with non-addressable entities.
1044 They have to be stable because we must not evaluate the
1045 variables in the expression every time the renaming is used.
1046 The pointer is called a "renaming" pointer in this case.
1048 In the rare cases where we cannot stabilize the renamed
1049 object, we just make a "bare" pointer and the renamed
1050 object will always be accessed indirectly through it.
1052 Note that we need to preserve the volatility of the renamed
1053 object through the indirection. */
1054 if (TREE_THIS_VOLATILE (gnu_expr
) && !TYPE_VOLATILE (gnu_type
))
1055 gnu_type
= build_qualified_type (gnu_type
,
1056 (TYPE_QUALS (gnu_type
)
1057 | TYPE_QUAL_VOLATILE
));
1058 gnu_type
= build_reference_type (gnu_type
);
1059 inner_const_flag
= TREE_READONLY (gnu_expr
);
1062 /* If the previous attempt at stabilizing failed, there is
1063 no point in trying again and we reuse the result without
1064 attaching it to the pointer. In this case it will only
1065 be used as the initializing expression of the pointer and
1066 thus needs no special treatment with regard to multiple
1069 Otherwise, try to stabilize and attach the expression to
1070 the pointer if the stabilization succeeds.
1072 Note that this might introduce SAVE_EXPRs and we don't
1073 check whether we are at the global level or not. This
1074 is fine since we are building a pointer initializer and
1075 neither the pointer nor the initializing expression can
1076 be accessed before the pointer elaboration has taken
1077 place in a correct program.
1079 These SAVE_EXPRs will be evaluated at the right place
1080 by either the evaluation of the initializer for the
1081 non-global case or the elaboration code for the global
1082 case, and will be attached to the elaboration procedure
1083 in the latter case. */
1084 if (!maybe_stable_expr
)
1087 = gnat_stabilize_reference (gnu_expr
, true, &stable
);
1090 renamed_obj
= maybe_stable_expr
;
1093 if (type_annotate_only
1094 && TREE_CODE (maybe_stable_expr
) == ERROR_MARK
)
1095 gnu_expr
= NULL_TREE
;
1098 = build_unary_op (ADDR_EXPR
, gnu_type
, maybe_stable_expr
);
1100 gnu_size
= NULL_TREE
;
1105 /* Make a volatile version of this object's type if we are to make
1106 the object volatile. We also interpret 13.3(19) conservatively
1107 and disallow any optimizations for such a non-constant object. */
1108 if ((Treat_As_Volatile (gnat_entity
)
1110 && gnu_type
!= except_type_node
1111 && (Is_Exported (gnat_entity
)
1113 || Present (Address_Clause (gnat_entity
)))))
1114 && !TYPE_VOLATILE (gnu_type
))
1115 gnu_type
= build_qualified_type (gnu_type
,
1116 (TYPE_QUALS (gnu_type
)
1117 | TYPE_QUAL_VOLATILE
));
1119 /* If we are defining an aliased object whose nominal subtype is
1120 unconstrained, the object is a record that contains both the
1121 template and the object. If there is an initializer, it will
1122 have already been converted to the right type, but we need to
1123 create the template if there is no initializer. */
1126 && TREE_CODE (gnu_type
) == RECORD_TYPE
1127 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type
)
1128 /* Beware that padding might have been introduced above. */
1129 || (TYPE_PADDING_P (gnu_type
)
1130 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1132 && TYPE_CONTAINS_TEMPLATE_P
1133 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))))
1136 = TYPE_PADDING_P (gnu_type
)
1137 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type
)))
1138 : TYPE_FIELDS (gnu_type
);
1139 vec
<constructor_elt
, va_gc
> *v
;
1141 tree t
= build_template (TREE_TYPE (template_field
),
1142 TREE_TYPE (DECL_CHAIN (template_field
)),
1144 CONSTRUCTOR_APPEND_ELT (v
, template_field
, t
);
1145 gnu_expr
= gnat_build_constructor (gnu_type
, v
);
1148 /* Convert the expression to the type of the object except in the
1149 case where the object's type is unconstrained or the object's type
1150 is a padded record whose field is of self-referential size. In
1151 the former case, converting will generate unnecessary evaluations
1152 of the CONSTRUCTOR to compute the size and in the latter case, we
1153 want to only copy the actual data. Also don't convert to a record
1154 type with a variant part from a record type without one, to keep
1155 the object simpler. */
1157 && TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
1158 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
1159 && !(TYPE_IS_PADDING_P (gnu_type
)
1160 && CONTAINS_PLACEHOLDER_P
1161 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))))
1162 && !(TREE_CODE (gnu_type
) == RECORD_TYPE
1163 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
1164 && get_variant_part (gnu_type
) != NULL_TREE
1165 && get_variant_part (TREE_TYPE (gnu_expr
)) == NULL_TREE
))
1166 gnu_expr
= convert (gnu_type
, gnu_expr
);
1168 /* If this is a pointer that doesn't have an initializing expression,
1169 initialize it to NULL, unless the object is imported. */
1171 && (POINTER_TYPE_P (gnu_type
) || TYPE_IS_FAT_POINTER_P (gnu_type
))
1173 && !Is_Imported (gnat_entity
))
1174 gnu_expr
= integer_zero_node
;
1176 /* If we are defining the object and it has an Address clause, we must
1177 either get the address expression from the saved GCC tree for the
1178 object if it has a Freeze node, or elaborate the address expression
1179 here since the front-end has guaranteed that the elaboration has no
1180 effects in this case. */
1181 if (definition
&& Present (Address_Clause (gnat_entity
)))
1183 Node_Id gnat_expr
= Expression (Address_Clause (gnat_entity
));
1185 = present_gnu_tree (gnat_entity
)
1186 ? get_gnu_tree (gnat_entity
) : gnat_to_gnu (gnat_expr
);
1188 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
1190 /* Ignore the size. It's either meaningless or was handled
1192 gnu_size
= NULL_TREE
;
1193 /* Convert the type of the object to a reference type that can
1194 alias everything as per 13.3(19). */
1196 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1197 gnu_address
= convert (gnu_type
, gnu_address
);
1200 = !Is_Public (gnat_entity
)
1201 || compile_time_known_address_p (gnat_expr
);
1203 /* If this is a deferred constant, the initializer is attached to
1205 if (kind
== E_Constant
&& Present (Full_View (gnat_entity
)))
1208 (Expression (Declaration_Node (Full_View (gnat_entity
))));
1210 /* If we don't have an initializing expression for the underlying
1211 variable, the initializing expression for the pointer is the
1212 specified address. Otherwise, we have to make a COMPOUND_EXPR
1213 to assign both the address and the initial value. */
1215 gnu_expr
= gnu_address
;
1218 = build2 (COMPOUND_EXPR
, gnu_type
,
1220 (MODIFY_EXPR
, NULL_TREE
,
1221 build_unary_op (INDIRECT_REF
, NULL_TREE
,
1227 /* If it has an address clause and we are not defining it, mark it
1228 as an indirect object. Likewise for Stdcall objects that are
1230 if ((!definition
&& Present (Address_Clause (gnat_entity
)))
1231 || (Is_Imported (gnat_entity
)
1232 && Has_Stdcall_Convention (gnat_entity
)))
1234 /* Convert the type of the object to a reference type that can
1235 alias everything as per 13.3(19). */
1237 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
1238 gnu_size
= NULL_TREE
;
1240 /* No point in taking the address of an initializing expression
1241 that isn't going to be used. */
1242 gnu_expr
= NULL_TREE
;
1244 /* If it has an address clause whose value is known at compile
1245 time, make the object a CONST_DECL. This will avoid a
1246 useless dereference. */
1247 if (Present (Address_Clause (gnat_entity
)))
1249 Node_Id gnat_address
1250 = Expression (Address_Clause (gnat_entity
));
1252 if (compile_time_known_address_p (gnat_address
))
1254 gnu_expr
= gnat_to_gnu (gnat_address
);
1262 /* If we are at top level and this object is of variable size,
1263 make the actual type a hidden pointer to the real type and
1264 make the initializer be a memory allocation and initialization.
1265 Likewise for objects we aren't defining (presumed to be
1266 external references from other packages), but there we do
1267 not set up an initialization.
1269 If the object's size overflows, make an allocator too, so that
1270 Storage_Error gets raised. Note that we will never free
1271 such memory, so we presume it never will get allocated. */
1272 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type
),
1273 global_bindings_p ()
1277 && !allocatable_size_p (convert (sizetype
,
1279 (CEIL_DIV_EXPR
, gnu_size
,
1280 bitsize_unit_node
)),
1281 global_bindings_p ()
1285 gnu_type
= build_reference_type (gnu_type
);
1286 gnu_size
= NULL_TREE
;
1289 /* In case this was a aliased object whose nominal subtype is
1290 unconstrained, the pointer above will be a thin pointer and
1291 build_allocator will automatically make the template.
1293 If we have a template initializer only (that we made above),
1294 pretend there is none and rely on what build_allocator creates
1295 again anyway. Otherwise (if we have a full initializer), get
1296 the data part and feed that to build_allocator.
1298 If we are elaborating a mutable object, tell build_allocator to
1299 ignore a possibly simpler size from the initializer, if any, as
1300 we must allocate the maximum possible size in this case. */
1301 if (definition
&& !imported_p
)
1303 tree gnu_alloc_type
= TREE_TYPE (gnu_type
);
1305 if (TREE_CODE (gnu_alloc_type
) == RECORD_TYPE
1306 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type
))
1309 = TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_alloc_type
)));
1311 if (TREE_CODE (gnu_expr
) == CONSTRUCTOR
1312 && 1 == vec_safe_length (CONSTRUCTOR_ELTS (gnu_expr
)))
1316 = build_component_ref
1317 (gnu_expr
, NULL_TREE
,
1318 DECL_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr
))),
1322 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type
)) == INTEGER_CST
1323 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_alloc_type
)))
1324 post_error ("?`Storage_Error` will be raised at run time!",
1328 = build_allocator (gnu_alloc_type
, gnu_expr
, gnu_type
,
1329 Empty
, Empty
, gnat_entity
, mutable_p
);
1334 gnu_expr
= NULL_TREE
;
1339 /* If this object would go into the stack and has an alignment larger
1340 than the largest stack alignment the back-end can honor, resort to
1341 a variable of "aligning type". */
1342 if (!global_bindings_p () && !static_p
&& definition
1343 && !imported_p
&& TYPE_ALIGN (gnu_type
) > BIGGEST_ALIGNMENT
)
1345 /* Create the new variable. No need for extra room before the
1346 aligned field as this is in automatic storage. */
1348 = make_aligning_type (gnu_type
, TYPE_ALIGN (gnu_type
),
1349 TYPE_SIZE_UNIT (gnu_type
),
1350 BIGGEST_ALIGNMENT
, 0, gnat_entity
);
1352 = create_var_decl (create_concat_name (gnat_entity
, "ALIGN"),
1353 NULL_TREE
, gnu_new_type
, NULL_TREE
, false,
1354 false, false, false, NULL
, gnat_entity
);
1356 /* Initialize the aligned field if we have an initializer. */
1359 (build_binary_op (MODIFY_EXPR
, NULL_TREE
,
1361 (gnu_new_var
, NULL_TREE
,
1362 TYPE_FIELDS (gnu_new_type
), false),
1366 /* And setup this entity as a reference to the aligned field. */
1367 gnu_type
= build_reference_type (gnu_type
);
1370 (ADDR_EXPR
, gnu_type
,
1371 build_component_ref (gnu_new_var
, NULL_TREE
,
1372 TYPE_FIELDS (gnu_new_type
), false));
1374 gnu_size
= NULL_TREE
;
1379 /* If this is an aliased object with an unconstrained nominal subtype,
1380 we make its type a thin reference, i.e. the reference counterpart
1381 of a thin pointer, so that it points to the array part. This is
1382 aimed at making it easier for the debugger to decode the object.
1383 Note that we have to do that this late because of the couple of
1384 allocation adjustments that might be made just above. */
1385 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity
))
1386 && (Is_Array_Type (Etype (gnat_entity
))
1387 || (Is_Private_Type (Etype (gnat_entity
))
1388 && Is_Array_Type (Full_View (Etype (gnat_entity
)))))
1389 && !type_annotate_only
)
1392 = gnat_to_gnu_type (Base_Type (Etype (gnat_entity
)));
1394 /* In case the object with the template has already been allocated
1395 just above, we have nothing to do here. */
1396 if (!TYPE_IS_THIN_POINTER_P (gnu_type
))
1399 = create_var_decl (concat_name (gnu_entity_name
, "UNC"),
1400 NULL_TREE
, gnu_type
, gnu_expr
,
1401 const_flag
, Is_Public (gnat_entity
),
1402 imported_p
|| !definition
, static_p
,
1405 = build_unary_op (ADDR_EXPR
, NULL_TREE
, gnu_unc_var
);
1406 TREE_CONSTANT (gnu_expr
) = 1;
1408 gnu_size
= NULL_TREE
;
1414 = build_reference_type (TYPE_OBJECT_RECORD_TYPE (gnu_array
));
1418 gnu_type
= build_qualified_type (gnu_type
, (TYPE_QUALS (gnu_type
)
1419 | TYPE_QUAL_CONST
));
1421 /* Convert the expression to the type of the object except in the
1422 case where the object's type is unconstrained or the object's type
1423 is a padded record whose field is of self-referential size. In
1424 the former case, converting will generate unnecessary evaluations
1425 of the CONSTRUCTOR to compute the size and in the latter case, we
1426 want to only copy the actual data. Also don't convert to a record
1427 type with a variant part from a record type without one, to keep
1428 the object simpler. */
1430 && TREE_CODE (gnu_type
) != UNCONSTRAINED_ARRAY_TYPE
1431 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
1432 && !(TYPE_IS_PADDING_P (gnu_type
)
1433 && CONTAINS_PLACEHOLDER_P
1434 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type
)))))
1435 && !(TREE_CODE (gnu_type
) == RECORD_TYPE
1436 && TREE_CODE (TREE_TYPE (gnu_expr
)) == RECORD_TYPE
1437 && get_variant_part (gnu_type
) != NULL_TREE
1438 && get_variant_part (TREE_TYPE (gnu_expr
)) == NULL_TREE
))
1439 gnu_expr
= convert (gnu_type
, gnu_expr
);
1441 /* If this name is external or there was a name specified, use it,
1442 unless this is a VMS exception object since this would conflict
1443 with the symbol we need to export in addition. Don't use the
1444 Interface_Name if there is an address clause (see CD30005). */
1445 if (!Is_VMS_Exception (gnat_entity
)
1446 && ((Present (Interface_Name (gnat_entity
))
1447 && No (Address_Clause (gnat_entity
)))
1448 || (Is_Public (gnat_entity
)
1449 && (!Is_Imported (gnat_entity
)
1450 || Is_Exported (gnat_entity
)))))
1451 gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
1453 /* If this is an aggregate constant initialized to a constant, force it
1454 to be statically allocated. This saves an initialization copy. */
1457 && gnu_expr
&& TREE_CONSTANT (gnu_expr
)
1458 && AGGREGATE_TYPE_P (gnu_type
)
1459 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (gnu_type
))
1460 && !(TYPE_IS_PADDING_P (gnu_type
)
1461 && !tree_fits_uhwi_p (TYPE_SIZE_UNIT
1462 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))))
1465 /* Deal with a pragma Linker_Section on a constant or variable. */
1466 if ((kind
== E_Constant
|| kind
== E_Variable
)
1467 && Present (Linker_Section_Pragma (gnat_entity
)))
1468 prepend_one_attribute_pragma (&attr_list
,
1469 Linker_Section_Pragma (gnat_entity
));
1471 /* Now create the variable or the constant and set various flags. */
1473 = create_var_decl_1 (gnu_entity_name
, gnu_ext_name
, gnu_type
,
1474 gnu_expr
, const_flag
, Is_Public (gnat_entity
),
1475 imported_p
|| !definition
, static_p
,
1476 !renamed_obj
, attr_list
, gnat_entity
);
1477 DECL_BY_REF_P (gnu_decl
) = used_by_ref
;
1478 DECL_POINTS_TO_READONLY_P (gnu_decl
) = used_by_ref
&& inner_const_flag
;
1479 DECL_CAN_NEVER_BE_NULL_P (gnu_decl
) = Can_Never_Be_Null (gnat_entity
);
1481 /* If we are defining an Out parameter and optimization isn't enabled,
1482 create a fake PARM_DECL for debugging purposes and make it point to
1483 the VAR_DECL. Suppress debug info for the latter but make sure it
1484 will live in memory so that it can be accessed from within the
1485 debugger through the PARM_DECL. */
1486 if (kind
== E_Out_Parameter
1490 && !flag_generate_lto
)
1492 tree param
= create_param_decl (gnu_entity_name
, gnu_type
, false);
1493 gnat_pushdecl (param
, gnat_entity
);
1494 SET_DECL_VALUE_EXPR (param
, gnu_decl
);
1495 DECL_HAS_VALUE_EXPR_P (param
) = 1;
1496 DECL_IGNORED_P (gnu_decl
) = 1;
1497 TREE_ADDRESSABLE (gnu_decl
) = 1;
1500 /* If this is a loop parameter, set the corresponding flag. */
1501 else if (kind
== E_Loop_Parameter
)
1502 DECL_LOOP_PARM_P (gnu_decl
) = 1;
1504 /* If this is a renaming pointer, attach the renamed object to it and
1505 register it if we are at the global level. Note that an external
1506 constant is at the global level. */
1509 SET_DECL_RENAMED_OBJECT (gnu_decl
, renamed_obj
);
1510 if ((!definition
&& kind
== E_Constant
) || global_bindings_p ())
1512 DECL_RENAMING_GLOBAL_P (gnu_decl
) = 1;
1513 record_global_renaming_pointer (gnu_decl
);
1517 /* If this is a constant and we are defining it or it generates a real
1518 symbol at the object level and we are referencing it, we may want
1519 or need to have a true variable to represent it:
1520 - if optimization isn't enabled, for debugging purposes,
1521 - if the constant is public and not overlaid on something else,
1522 - if its address is taken,
1523 - if either itself or its type is aliased. */
1524 if (TREE_CODE (gnu_decl
) == CONST_DECL
1525 && (definition
|| Sloc (gnat_entity
) > Standard_Location
)
1526 && ((!optimize
&& debug_info_p
)
1527 || (Is_Public (gnat_entity
)
1528 && No (Address_Clause (gnat_entity
)))
1529 || Address_Taken (gnat_entity
)
1530 || Is_Aliased (gnat_entity
)
1531 || Is_Aliased (Etype (gnat_entity
))))
1534 = create_true_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
1535 gnu_expr
, true, Is_Public (gnat_entity
),
1536 !definition
, static_p
, attr_list
,
1539 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl
, gnu_corr_var
);
1541 /* As debugging information will be generated for the variable,
1542 do not generate debugging information for the constant. */
1544 DECL_IGNORED_P (gnu_decl
) = 1;
1546 DECL_IGNORED_P (gnu_corr_var
) = 1;
1549 /* If this is a constant, even if we don't need a true variable, we
1550 may need to avoid returning the initializer in every case. That
1551 can happen for the address of a (constant) constructor because,
1552 upon dereferencing it, the constructor will be reinjected in the
1553 tree, which may not be valid in every case; see lvalue_required_p
1554 for more details. */
1555 if (TREE_CODE (gnu_decl
) == CONST_DECL
)
1556 DECL_CONST_ADDRESS_P (gnu_decl
) = constructor_address_p (gnu_expr
);
1558 /* If this object is declared in a block that contains a block with an
1559 exception handler, and we aren't using the GCC exception mechanism,
1560 we must force this variable in memory in order to avoid an invalid
1562 if (Exception_Mechanism
!= Back_End_Exceptions
1563 && Has_Nested_Block_With_Handler (Scope (gnat_entity
)))
1564 TREE_ADDRESSABLE (gnu_decl
) = 1;
1566 /* If this is a local variable with non-BLKmode and aggregate type,
1567 and optimization isn't enabled, then force it in memory so that
1568 a register won't be allocated to it with possible subparts left
1569 uninitialized and reaching the register allocator. */
1570 else if (TREE_CODE (gnu_decl
) == VAR_DECL
1571 && !DECL_EXTERNAL (gnu_decl
)
1572 && !TREE_STATIC (gnu_decl
)
1573 && DECL_MODE (gnu_decl
) != BLKmode
1574 && AGGREGATE_TYPE_P (TREE_TYPE (gnu_decl
))
1575 && !TYPE_IS_FAT_POINTER_P (TREE_TYPE (gnu_decl
))
1577 TREE_ADDRESSABLE (gnu_decl
) = 1;
1579 /* If we are defining an object with variable size or an object with
1580 fixed size that will be dynamically allocated, and we are using the
1581 setjmp/longjmp exception mechanism, update the setjmp buffer. */
1583 && Exception_Mechanism
== Setjmp_Longjmp
1584 && get_block_jmpbuf_decl ()
1585 && DECL_SIZE_UNIT (gnu_decl
)
1586 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl
)) != INTEGER_CST
1587 || (flag_stack_check
== GENERIC_STACK_CHECK
1588 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl
),
1589 STACK_CHECK_MAX_VAR_SIZE
) > 0)))
1590 add_stmt_with_node (build_call_n_expr
1591 (update_setjmp_buf_decl
, 1,
1592 build_unary_op (ADDR_EXPR
, NULL_TREE
,
1593 get_block_jmpbuf_decl ())),
1596 /* Back-annotate Esize and Alignment of the object if not already
1597 known. Note that we pick the values of the type, not those of
1598 the object, to shield ourselves from low-level platform-dependent
1599 adjustments like alignment promotion. This is both consistent with
1600 all the treatment above, where alignment and size are set on the
1601 type of the object and not on the object directly, and makes it
1602 possible to support all confirming representation clauses. */
1603 annotate_object (gnat_entity
, TREE_TYPE (gnu_decl
), gnu_object_size
,
1609 /* Return a TYPE_DECL for "void" that we previously made. */
1610 gnu_decl
= TYPE_NAME (void_type_node
);
1613 case E_Enumeration_Type
:
1614 /* A special case: for the types Character and Wide_Character in
1615 Standard, we do not list all the literals. So if the literals
1616 are not specified, make this an unsigned integer type. */
1617 if (No (First_Literal (gnat_entity
)))
1619 gnu_type
= make_unsigned_type (esize
);
1620 TYPE_NAME (gnu_type
) = gnu_entity_name
;
1622 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1623 This is needed by the DWARF-2 back-end to distinguish between
1624 unsigned integer types and character types. */
1625 TYPE_STRING_FLAG (gnu_type
) = 1;
1629 /* We have a list of enumeral constants in First_Literal. We make a
1630 CONST_DECL for each one and build into GNU_LITERAL_LIST the list
1631 to be placed into TYPE_FIELDS. Each node is itself a TREE_LIST
1632 whose TREE_VALUE is the literal name and whose TREE_PURPOSE is the
1633 value of the literal. But when we have a regular boolean type, we
1634 simplify this a little by using a BOOLEAN_TYPE. */
1635 const bool is_boolean
= Is_Boolean_Type (gnat_entity
)
1636 && !Has_Non_Standard_Rep (gnat_entity
);
1637 const bool is_unsigned
= Is_Unsigned_Type (gnat_entity
);
1638 tree gnu_list
= NULL_TREE
;
1639 Entity_Id gnat_literal
;
1641 gnu_type
= make_node (is_boolean
? BOOLEAN_TYPE
: ENUMERAL_TYPE
);
1642 TYPE_PRECISION (gnu_type
) = esize
;
1643 TYPE_UNSIGNED (gnu_type
) = is_unsigned
;
1644 set_min_and_max_values_for_integral_type (gnu_type
, esize
,
1645 TYPE_SIGN (gnu_type
));
1646 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
1647 layout_type (gnu_type
);
1649 for (gnat_literal
= First_Literal (gnat_entity
);
1650 Present (gnat_literal
);
1651 gnat_literal
= Next_Literal (gnat_literal
))
1654 = UI_To_gnu (Enumeration_Rep (gnat_literal
), gnu_type
);
1656 = create_var_decl (get_entity_name (gnat_literal
), NULL_TREE
,
1657 gnu_type
, gnu_value
, true, false, false,
1658 false, NULL
, gnat_literal
);
1659 /* Do not generate debug info for individual enumerators. */
1660 DECL_IGNORED_P (gnu_literal
) = 1;
1661 save_gnu_tree (gnat_literal
, gnu_literal
, false);
1663 = tree_cons (DECL_NAME (gnu_literal
), gnu_value
, gnu_list
);
1667 TYPE_VALUES (gnu_type
) = nreverse (gnu_list
);
1669 /* Note that the bounds are updated at the end of this function
1670 to avoid an infinite recursion since they refer to the type. */
1675 case E_Signed_Integer_Type
:
1676 case E_Ordinary_Fixed_Point_Type
:
1677 case E_Decimal_Fixed_Point_Type
:
1678 /* For integer types, just make a signed type the appropriate number
1680 gnu_type
= make_signed_type (esize
);
1683 case E_Modular_Integer_Type
:
1685 /* For modular types, make the unsigned type of the proper number
1686 of bits and then set up the modulus, if required. */
1687 tree gnu_modulus
, gnu_high
= NULL_TREE
;
1689 /* Packed array types are supposed to be subtypes only. */
1690 gcc_assert (!Is_Packed_Array_Type (gnat_entity
));
1692 gnu_type
= make_unsigned_type (esize
);
1694 /* Get the modulus in this type. If it overflows, assume it is because
1695 it is equal to 2**Esize. Note that there is no overflow checking
1696 done on unsigned type, so we detect the overflow by looking for
1697 a modulus of zero, which is otherwise invalid. */
1698 gnu_modulus
= UI_To_gnu (Modulus (gnat_entity
), gnu_type
);
1700 if (!integer_zerop (gnu_modulus
))
1702 TYPE_MODULAR_P (gnu_type
) = 1;
1703 SET_TYPE_MODULUS (gnu_type
, gnu_modulus
);
1704 gnu_high
= fold_build2 (MINUS_EXPR
, gnu_type
, gnu_modulus
,
1705 convert (gnu_type
, integer_one_node
));
1708 /* If the upper bound is not maximal, make an extra subtype. */
1710 && !tree_int_cst_equal (gnu_high
, TYPE_MAX_VALUE (gnu_type
)))
1712 tree gnu_subtype
= make_unsigned_type (esize
);
1713 SET_TYPE_RM_MAX_VALUE (gnu_subtype
, gnu_high
);
1714 TREE_TYPE (gnu_subtype
) = gnu_type
;
1715 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
1716 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "UMT");
1717 gnu_type
= gnu_subtype
;
1722 case E_Signed_Integer_Subtype
:
1723 case E_Enumeration_Subtype
:
1724 case E_Modular_Integer_Subtype
:
1725 case E_Ordinary_Fixed_Point_Subtype
:
1726 case E_Decimal_Fixed_Point_Subtype
:
1728 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1729 not want to call create_range_type since we would like each subtype
1730 node to be distinct. ??? Historically this was in preparation for
1731 when memory aliasing is implemented, but that's obsolete now given
1732 the call to relate_alias_sets below.
1734 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1735 this fact is used by the arithmetic conversion functions.
1737 We elaborate the Ancestor_Subtype if it is not in the current unit
1738 and one of our bounds is non-static. We do this to ensure consistent
1739 naming in the case where several subtypes share the same bounds, by
1740 elaborating the first such subtype first, thus using its name. */
1743 && Present (Ancestor_Subtype (gnat_entity
))
1744 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1745 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1746 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
1747 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
), gnu_expr
, 0);
1749 /* Set the precision to the Esize except for bit-packed arrays. */
1750 if (Is_Packed_Array_Type (gnat_entity
)
1751 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1752 esize
= UI_To_Int (RM_Size (gnat_entity
));
1754 /* This should be an unsigned type if the base type is unsigned or
1755 if the lower bound is constant and non-negative or if the type
1757 if (Is_Unsigned_Type (Etype (gnat_entity
))
1758 || Is_Unsigned_Type (gnat_entity
)
1759 || Has_Biased_Representation (gnat_entity
))
1760 gnu_type
= make_unsigned_type (esize
);
1762 gnu_type
= make_signed_type (esize
);
1763 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
1765 SET_TYPE_RM_MIN_VALUE
1767 convert (TREE_TYPE (gnu_type
),
1768 elaborate_expression (Type_Low_Bound (gnat_entity
),
1769 gnat_entity
, get_identifier ("L"),
1771 Needs_Debug_Info (gnat_entity
))));
1773 SET_TYPE_RM_MAX_VALUE
1775 convert (TREE_TYPE (gnu_type
),
1776 elaborate_expression (Type_High_Bound (gnat_entity
),
1777 gnat_entity
, get_identifier ("U"),
1779 Needs_Debug_Info (gnat_entity
))));
1781 TYPE_BIASED_REPRESENTATION_P (gnu_type
)
1782 = Has_Biased_Representation (gnat_entity
);
1784 /* Inherit our alias set from what we're a subtype of. Subtypes
1785 are not different types and a pointer can designate any instance
1786 within a subtype hierarchy. */
1787 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
1789 /* One of the above calls might have caused us to be elaborated,
1790 so don't blow up if so. */
1791 if (present_gnu_tree (gnat_entity
))
1793 maybe_present
= true;
1797 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1798 TYPE_STUB_DECL (gnu_type
)
1799 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
1801 /* For a packed array, make the original array type a parallel type. */
1803 && Is_Packed_Array_Type (gnat_entity
)
1804 && present_gnu_tree (Original_Array_Type (gnat_entity
)))
1805 add_parallel_type (gnu_type
,
1807 (Original_Array_Type (gnat_entity
)));
1811 /* We have to handle clauses that under-align the type specially. */
1812 if ((Present (Alignment_Clause (gnat_entity
))
1813 || (Is_Packed_Array_Type (gnat_entity
)
1815 (Alignment_Clause (Original_Array_Type (gnat_entity
)))))
1816 && UI_Is_In_Int_Range (Alignment (gnat_entity
)))
1818 align
= UI_To_Int (Alignment (gnat_entity
)) * BITS_PER_UNIT
;
1819 if (align
>= TYPE_ALIGN (gnu_type
))
1823 /* If the type we are dealing with represents a bit-packed array,
1824 we need to have the bits left justified on big-endian targets
1825 and right justified on little-endian targets. We also need to
1826 ensure that when the value is read (e.g. for comparison of two
1827 such values), we only get the good bits, since the unused bits
1828 are uninitialized. Both goals are accomplished by wrapping up
1829 the modular type in an enclosing record type. */
1830 if (Is_Packed_Array_Type (gnat_entity
)
1831 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
1833 tree gnu_field_type
, gnu_field
;
1835 /* Set the RM size before wrapping up the original type. */
1836 SET_TYPE_RM_SIZE (gnu_type
,
1837 UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
));
1838 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
) = 1;
1840 /* Create a stripped-down declaration, mainly for debugging. */
1841 create_type_decl (gnu_entity_name
, gnu_type
, true, debug_info_p
,
1844 /* Now save it and build the enclosing record type. */
1845 gnu_field_type
= gnu_type
;
1847 gnu_type
= make_node (RECORD_TYPE
);
1848 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "JM");
1849 TYPE_PACKED (gnu_type
) = 1;
1850 TYPE_SIZE (gnu_type
) = TYPE_SIZE (gnu_field_type
);
1851 TYPE_SIZE_UNIT (gnu_type
) = TYPE_SIZE_UNIT (gnu_field_type
);
1852 SET_TYPE_ADA_SIZE (gnu_type
, TYPE_RM_SIZE (gnu_field_type
));
1854 /* Propagate the alignment of the modular type to the record type,
1855 unless there is an alignment clause that under-aligns the type.
1856 This means that bit-packed arrays are given "ceil" alignment for
1857 their size by default, which may seem counter-intuitive but makes
1858 it possible to overlay them on modular types easily. */
1859 TYPE_ALIGN (gnu_type
)
1860 = align
> 0 ? align
: TYPE_ALIGN (gnu_field_type
);
1862 relate_alias_sets (gnu_type
, gnu_field_type
, ALIAS_SET_COPY
);
1864 /* Don't declare the field as addressable since we won't be taking
1865 its address and this would prevent create_field_decl from making
1868 = create_field_decl (get_identifier ("OBJECT"), gnu_field_type
,
1869 gnu_type
, NULL_TREE
, bitsize_zero_node
, 1, 0);
1871 /* Do not emit debug info until after the parallel type is added. */
1872 finish_record_type (gnu_type
, gnu_field
, 2, false);
1873 compute_record_mode (gnu_type
);
1874 TYPE_JUSTIFIED_MODULAR_P (gnu_type
) = 1;
1878 /* Make the original array type a parallel type. */
1879 if (present_gnu_tree (Original_Array_Type (gnat_entity
)))
1880 add_parallel_type (gnu_type
,
1882 (Original_Array_Type (gnat_entity
)));
1884 rest_of_record_type_compilation (gnu_type
);
1888 /* If the type we are dealing with has got a smaller alignment than the
1889 natural one, we need to wrap it up in a record type and misalign the
1890 latter; we reuse the padding machinery for this purpose. Note that,
1891 even if the record type is marked as packed because of misalignment,
1892 we don't pack the field so as to give it the size of the type. */
1895 tree gnu_field_type
, gnu_field
;
1897 /* Set the RM size before wrapping up the type. */
1898 SET_TYPE_RM_SIZE (gnu_type
,
1899 UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
));
1901 /* Create a stripped-down declaration, mainly for debugging. */
1902 create_type_decl (gnu_entity_name
, gnu_type
, true, debug_info_p
,
1905 /* Now save it and build the enclosing record type. */
1906 gnu_field_type
= gnu_type
;
1908 gnu_type
= make_node (RECORD_TYPE
);
1909 TYPE_NAME (gnu_type
) = create_concat_name (gnat_entity
, "PAD");
1910 TYPE_PACKED (gnu_type
) = 1;
1911 TYPE_SIZE (gnu_type
) = TYPE_SIZE (gnu_field_type
);
1912 TYPE_SIZE_UNIT (gnu_type
) = TYPE_SIZE_UNIT (gnu_field_type
);
1913 SET_TYPE_ADA_SIZE (gnu_type
, TYPE_RM_SIZE (gnu_field_type
));
1914 TYPE_ALIGN (gnu_type
) = align
;
1915 relate_alias_sets (gnu_type
, gnu_field_type
, ALIAS_SET_COPY
);
1917 /* Don't declare the field as addressable since we won't be taking
1918 its address and this would prevent create_field_decl from making
1921 = create_field_decl (get_identifier ("F"), gnu_field_type
,
1922 gnu_type
, TYPE_SIZE (gnu_field_type
),
1923 bitsize_zero_node
, 0, 0);
1925 finish_record_type (gnu_type
, gnu_field
, 2, debug_info_p
);
1926 compute_record_mode (gnu_type
);
1927 TYPE_PADDING_P (gnu_type
) = 1;
1932 case E_Floating_Point_Type
:
1933 /* If this is a VAX floating-point type, use an integer of the proper
1934 size. All the operations will be handled with ASM statements. */
1935 if (Vax_Float (gnat_entity
))
1937 gnu_type
= make_signed_type (esize
);
1938 TYPE_VAX_FLOATING_POINT_P (gnu_type
) = 1;
1939 SET_TYPE_DIGITS_VALUE (gnu_type
,
1940 UI_To_gnu (Digits_Value (gnat_entity
),
1945 /* The type of the Low and High bounds can be our type if this is
1946 a type from Standard, so set them at the end of the function. */
1947 gnu_type
= make_node (REAL_TYPE
);
1948 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
1949 layout_type (gnu_type
);
1952 case E_Floating_Point_Subtype
:
1953 if (Vax_Float (gnat_entity
))
1955 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
1959 /* See the E_Signed_Integer_Subtype case for the rationale. */
1961 && Present (Ancestor_Subtype (gnat_entity
))
1962 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity
))
1963 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity
))
1964 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity
))))
1965 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity
), gnu_expr
, 0);
1967 gnu_type
= make_node (REAL_TYPE
);
1968 TREE_TYPE (gnu_type
) = get_unpadded_type (Etype (gnat_entity
));
1969 TYPE_PRECISION (gnu_type
) = fp_size_to_prec (esize
);
1970 TYPE_GCC_MIN_VALUE (gnu_type
)
1971 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type
));
1972 TYPE_GCC_MAX_VALUE (gnu_type
)
1973 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type
));
1974 layout_type (gnu_type
);
1976 SET_TYPE_RM_MIN_VALUE
1978 convert (TREE_TYPE (gnu_type
),
1979 elaborate_expression (Type_Low_Bound (gnat_entity
),
1980 gnat_entity
, get_identifier ("L"),
1982 Needs_Debug_Info (gnat_entity
))));
1984 SET_TYPE_RM_MAX_VALUE
1986 convert (TREE_TYPE (gnu_type
),
1987 elaborate_expression (Type_High_Bound (gnat_entity
),
1988 gnat_entity
, get_identifier ("U"),
1990 Needs_Debug_Info (gnat_entity
))));
1992 /* Inherit our alias set from what we're a subtype of, as for
1993 integer subtypes. */
1994 relate_alias_sets (gnu_type
, TREE_TYPE (gnu_type
), ALIAS_SET_COPY
);
1996 /* One of the above calls might have caused us to be elaborated,
1997 so don't blow up if so. */
1998 maybe_present
= true;
2001 /* Array and String Types and Subtypes
2003 Unconstrained array types are represented by E_Array_Type and
2004 constrained array types are represented by E_Array_Subtype. There
2005 are no actual objects of an unconstrained array type; all we have
2006 are pointers to that type.
2008 The following fields are defined on array types and subtypes:
2010 Component_Type Component type of the array.
2011 Number_Dimensions Number of dimensions (an int).
2012 First_Index Type of first index. */
2017 const bool convention_fortran_p
2018 = (Convention (gnat_entity
) == Convention_Fortran
);
2019 const int ndim
= Number_Dimensions (gnat_entity
);
2020 tree gnu_template_type
;
2021 tree gnu_ptr_template
;
2022 tree gnu_template_reference
, gnu_template_fields
, gnu_fat_type
;
2023 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2024 tree
*gnu_temp_fields
= XALLOCAVEC (tree
, ndim
);
2025 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
, tem
, t
;
2026 Entity_Id gnat_index
, gnat_name
;
2030 /* Create the type for the component now, as it simplifies breaking
2031 type reference loops. */
2033 = gnat_to_gnu_component_type (gnat_entity
, definition
, debug_info_p
);
2034 if (present_gnu_tree (gnat_entity
))
2036 /* As a side effect, the type may have been translated. */
2037 maybe_present
= true;
2041 /* We complete an existing dummy fat pointer type in place. This both
2042 avoids further complex adjustments in update_pointer_to and yields
2043 better debugging information in DWARF by leveraging the support for
2044 incomplete declarations of "tagged" types in the DWARF back-end. */
2045 gnu_type
= get_dummy_type (gnat_entity
);
2046 if (gnu_type
&& TYPE_POINTER_TO (gnu_type
))
2048 gnu_fat_type
= TYPE_MAIN_VARIANT (TYPE_POINTER_TO (gnu_type
));
2049 TYPE_NAME (gnu_fat_type
) = NULL_TREE
;
2050 /* Save the contents of the dummy type for update_pointer_to. */
2051 TYPE_POINTER_TO (gnu_type
) = copy_type (gnu_fat_type
);
2053 TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_fat_type
)));
2054 gnu_template_type
= TREE_TYPE (gnu_ptr_template
);
2058 gnu_fat_type
= make_node (RECORD_TYPE
);
2059 gnu_template_type
= make_node (RECORD_TYPE
);
2060 gnu_ptr_template
= build_pointer_type (gnu_template_type
);
2063 /* Make a node for the array. If we are not defining the array
2064 suppress expanding incomplete types. */
2065 gnu_type
= make_node (UNCONSTRAINED_ARRAY_TYPE
);
2069 defer_incomplete_level
++;
2070 this_deferred
= true;
2073 /* Build the fat pointer type. Use a "void *" object instead of
2074 a pointer to the array type since we don't have the array type
2075 yet (it will reference the fat pointer via the bounds). */
2077 = create_field_decl (get_identifier ("P_ARRAY"), ptr_void_type_node
,
2078 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2080 = create_field_decl (get_identifier ("P_BOUNDS"), gnu_ptr_template
,
2081 gnu_fat_type
, NULL_TREE
, NULL_TREE
, 0, 0);
2083 if (COMPLETE_TYPE_P (gnu_fat_type
))
2085 /* We are going to lay it out again so reset the alias set. */
2086 alias_set_type alias_set
= TYPE_ALIAS_SET (gnu_fat_type
);
2087 TYPE_ALIAS_SET (gnu_fat_type
) = -1;
2088 finish_fat_pointer_type (gnu_fat_type
, tem
);
2089 TYPE_ALIAS_SET (gnu_fat_type
) = alias_set
;
2090 for (t
= gnu_fat_type
; t
; t
= TYPE_NEXT_VARIANT (t
))
2092 TYPE_FIELDS (t
) = tem
;
2093 SET_TYPE_UNCONSTRAINED_ARRAY (t
, gnu_type
);
2098 finish_fat_pointer_type (gnu_fat_type
, tem
);
2099 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type
, gnu_type
);
2102 /* Build a reference to the template from a PLACEHOLDER_EXPR that
2103 is the fat pointer. This will be used to access the individual
2104 fields once we build them. */
2105 tem
= build3 (COMPONENT_REF
, gnu_ptr_template
,
2106 build0 (PLACEHOLDER_EXPR
, gnu_fat_type
),
2107 DECL_CHAIN (TYPE_FIELDS (gnu_fat_type
)), NULL_TREE
);
2108 gnu_template_reference
2109 = build_unary_op (INDIRECT_REF
, gnu_template_type
, tem
);
2110 TREE_READONLY (gnu_template_reference
) = 1;
2111 TREE_THIS_NOTRAP (gnu_template_reference
) = 1;
2113 /* Now create the GCC type for each index and add the fields for that
2114 index to the template. */
2115 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2116 gnat_index
= First_Index (gnat_entity
);
2117 0 <= index
&& index
< ndim
;
2118 index
+= (convention_fortran_p
? - 1 : 1),
2119 gnat_index
= Next_Index (gnat_index
))
2121 char field_name
[16];
2122 tree gnu_index_base_type
2123 = get_unpadded_type (Base_Type (Etype (gnat_index
)));
2124 tree gnu_lb_field
, gnu_hb_field
, gnu_orig_min
, gnu_orig_max
;
2125 tree gnu_min
, gnu_max
, gnu_high
;
2127 /* Make the FIELD_DECLs for the low and high bounds of this
2128 type and then make extractions of these fields from the
2130 sprintf (field_name
, "LB%d", index
);
2131 gnu_lb_field
= create_field_decl (get_identifier (field_name
),
2132 gnu_index_base_type
,
2133 gnu_template_type
, NULL_TREE
,
2135 Sloc_to_locus (Sloc (gnat_entity
),
2136 &DECL_SOURCE_LOCATION (gnu_lb_field
));
2138 field_name
[0] = 'U';
2139 gnu_hb_field
= create_field_decl (get_identifier (field_name
),
2140 gnu_index_base_type
,
2141 gnu_template_type
, NULL_TREE
,
2143 Sloc_to_locus (Sloc (gnat_entity
),
2144 &DECL_SOURCE_LOCATION (gnu_hb_field
));
2146 gnu_temp_fields
[index
] = chainon (gnu_lb_field
, gnu_hb_field
);
2148 /* We can't use build_component_ref here since the template type
2149 isn't complete yet. */
2150 gnu_orig_min
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2151 gnu_template_reference
, gnu_lb_field
,
2153 gnu_orig_max
= build3 (COMPONENT_REF
, gnu_index_base_type
,
2154 gnu_template_reference
, gnu_hb_field
,
2156 TREE_READONLY (gnu_orig_min
) = TREE_READONLY (gnu_orig_max
) = 1;
2158 gnu_min
= convert (sizetype
, gnu_orig_min
);
2159 gnu_max
= convert (sizetype
, gnu_orig_max
);
2161 /* Compute the size of this dimension. See the E_Array_Subtype
2162 case below for the rationale. */
2164 = build3 (COND_EXPR
, sizetype
,
2165 build2 (GE_EXPR
, boolean_type_node
,
2166 gnu_orig_max
, gnu_orig_min
),
2168 size_binop (MINUS_EXPR
, gnu_min
, size_one_node
));
2170 /* Make a range type with the new range in the Ada base type.
2171 Then make an index type with the size range in sizetype. */
2172 gnu_index_types
[index
]
2173 = create_index_type (gnu_min
, gnu_high
,
2174 create_range_type (gnu_index_base_type
,
2179 /* Update the maximum size of the array in elements. */
2182 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2184 = convert (sizetype
, TYPE_MIN_VALUE (gnu_index_type
));
2186 = convert (sizetype
, TYPE_MAX_VALUE (gnu_index_type
));
2188 = size_binop (MAX_EXPR
,
2189 size_binop (PLUS_EXPR
, size_one_node
,
2190 size_binop (MINUS_EXPR
,
2194 if (TREE_CODE (gnu_this_max
) == INTEGER_CST
2195 && TREE_OVERFLOW (gnu_this_max
))
2196 gnu_max_size
= NULL_TREE
;
2199 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2202 TYPE_NAME (gnu_index_types
[index
])
2203 = create_concat_name (gnat_entity
, field_name
);
2206 /* Install all the fields into the template. */
2207 TYPE_NAME (gnu_template_type
)
2208 = create_concat_name (gnat_entity
, "XUB");
2209 gnu_template_fields
= NULL_TREE
;
2210 for (index
= 0; index
< ndim
; index
++)
2212 = chainon (gnu_template_fields
, gnu_temp_fields
[index
]);
2213 finish_record_type (gnu_template_type
, gnu_template_fields
, 0,
2215 TYPE_READONLY (gnu_template_type
) = 1;
2217 /* If Component_Size is not already specified, annotate it with the
2218 size of the component. */
2219 if (Unknown_Component_Size (gnat_entity
))
2220 Set_Component_Size (gnat_entity
,
2221 annotate_value (TYPE_SIZE (comp_type
)));
2223 /* Compute the maximum size of the array in units and bits. */
2226 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2227 TYPE_SIZE_UNIT (comp_type
));
2228 gnu_max_size
= size_binop (MULT_EXPR
,
2229 convert (bitsizetype
, gnu_max_size
),
2230 TYPE_SIZE (comp_type
));
2233 gnu_max_size_unit
= NULL_TREE
;
2235 /* Now build the array type. */
2237 for (index
= ndim
- 1; index
>= 0; index
--)
2239 tem
= build_nonshared_array_type (tem
, gnu_index_types
[index
]);
2240 if (Reverse_Storage_Order (gnat_entity
))
2241 sorry ("non-default Scalar_Storage_Order");
2242 TYPE_MULTI_ARRAY_P (tem
) = (index
> 0);
2243 if (array_type_has_nonaliased_component (tem
, gnat_entity
))
2244 TYPE_NONALIASED_COMPONENT (tem
) = 1;
2246 /* If it is passed by reference, force BLKmode to ensure that
2247 objects of this type will always be put in memory. */
2248 if (TYPE_MODE (tem
) != BLKmode
2249 && Is_By_Reference_Type (gnat_entity
))
2250 SET_TYPE_MODE (tem
, BLKmode
);
2253 /* If an alignment is specified, use it if valid. But ignore it
2254 for the original type of packed array types. If the alignment
2255 was requested with an explicit alignment clause, state so. */
2256 if (No (Packed_Array_Type (gnat_entity
))
2257 && Known_Alignment (gnat_entity
))
2260 = validate_alignment (Alignment (gnat_entity
), gnat_entity
,
2262 if (Present (Alignment_Clause (gnat_entity
)))
2263 TYPE_USER_ALIGN (tem
) = 1;
2266 TYPE_CONVENTION_FORTRAN_P (tem
) = convention_fortran_p
;
2268 /* Adjust the type of the pointer-to-array field of the fat pointer
2269 and record the aliasing relationships if necessary. */
2270 TREE_TYPE (TYPE_FIELDS (gnu_fat_type
)) = build_pointer_type (tem
);
2271 if (TYPE_ALIAS_SET_KNOWN_P (gnu_fat_type
))
2272 record_component_aliases (gnu_fat_type
);
2274 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2275 corresponding fat pointer. */
2276 TREE_TYPE (gnu_type
) = gnu_fat_type
;
2277 TYPE_POINTER_TO (gnu_type
) = gnu_fat_type
;
2278 TYPE_REFERENCE_TO (gnu_type
) = gnu_fat_type
;
2279 SET_TYPE_MODE (gnu_type
, BLKmode
);
2280 TYPE_ALIGN (gnu_type
) = TYPE_ALIGN (tem
);
2282 /* If the maximum size doesn't overflow, use it. */
2284 && TREE_CODE (gnu_max_size
) == INTEGER_CST
2285 && !TREE_OVERFLOW (gnu_max_size
)
2286 && TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2287 && !TREE_OVERFLOW (gnu_max_size_unit
))
2289 TYPE_SIZE (tem
) = size_binop (MIN_EXPR
, gnu_max_size
,
2291 TYPE_SIZE_UNIT (tem
) = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2292 TYPE_SIZE_UNIT (tem
));
2295 create_type_decl (create_concat_name (gnat_entity
, "XUA"), tem
,
2296 !Comes_From_Source (gnat_entity
), debug_info_p
,
2299 /* Give the fat pointer type a name. If this is a packed type, tell
2300 the debugger how to interpret the underlying bits. */
2301 if (Present (Packed_Array_Type (gnat_entity
)))
2302 gnat_name
= Packed_Array_Type (gnat_entity
);
2304 gnat_name
= gnat_entity
;
2305 create_type_decl (create_concat_name (gnat_name
, "XUP"), gnu_fat_type
,
2306 !Comes_From_Source (gnat_entity
), debug_info_p
,
2309 /* Create the type to be designated by thin pointers: a record type for
2310 the array and its template. We used to shift the fields to have the
2311 template at a negative offset, but this was somewhat of a kludge; we
2312 now shift thin pointer values explicitly but only those which have a
2313 TYPE_UNCONSTRAINED_ARRAY attached to the designated RECORD_TYPE. */
2314 tem
= build_unc_object_type (gnu_template_type
, tem
,
2315 create_concat_name (gnat_name
, "XUT"),
2318 SET_TYPE_UNCONSTRAINED_ARRAY (tem
, gnu_type
);
2319 TYPE_OBJECT_RECORD_TYPE (gnu_type
) = tem
;
2323 case E_String_Subtype
:
2324 case E_Array_Subtype
:
2326 /* This is the actual data type for array variables. Multidimensional
2327 arrays are implemented as arrays of arrays. Note that arrays which
2328 have sparse enumeration subtypes as index components create sparse
2329 arrays, which is obviously space inefficient but so much easier to
2332 Also note that the subtype never refers to the unconstrained array
2333 type, which is somewhat at variance with Ada semantics.
2335 First check to see if this is simply a renaming of the array type.
2336 If so, the result is the array type. */
2338 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
2339 if (!Is_Constrained (gnat_entity
))
2343 Entity_Id gnat_index
, gnat_base_index
;
2344 const bool convention_fortran_p
2345 = (Convention (gnat_entity
) == Convention_Fortran
);
2346 const int ndim
= Number_Dimensions (gnat_entity
);
2347 tree gnu_base_type
= gnu_type
;
2348 tree
*gnu_index_types
= XALLOCAVEC (tree
, ndim
);
2349 tree gnu_max_size
= size_one_node
, gnu_max_size_unit
;
2350 bool need_index_type_struct
= false;
2353 /* First create the GCC type for each index and find out whether
2354 special types are needed for debugging information. */
2355 for (index
= (convention_fortran_p
? ndim
- 1 : 0),
2356 gnat_index
= First_Index (gnat_entity
),
2358 = First_Index (Implementation_Base_Type (gnat_entity
));
2359 0 <= index
&& index
< ndim
;
2360 index
+= (convention_fortran_p
? - 1 : 1),
2361 gnat_index
= Next_Index (gnat_index
),
2362 gnat_base_index
= Next_Index (gnat_base_index
))
2364 tree gnu_index_type
= get_unpadded_type (Etype (gnat_index
));
2365 tree gnu_orig_min
= TYPE_MIN_VALUE (gnu_index_type
);
2366 tree gnu_orig_max
= TYPE_MAX_VALUE (gnu_index_type
);
2367 tree gnu_min
= convert (sizetype
, gnu_orig_min
);
2368 tree gnu_max
= convert (sizetype
, gnu_orig_max
);
2369 tree gnu_base_index_type
2370 = get_unpadded_type (Etype (gnat_base_index
));
2371 tree gnu_base_orig_min
= TYPE_MIN_VALUE (gnu_base_index_type
);
2372 tree gnu_base_orig_max
= TYPE_MAX_VALUE (gnu_base_index_type
);
2375 /* See if the base array type is already flat. If it is, we
2376 are probably compiling an ACATS test but it will cause the
2377 code below to malfunction if we don't handle it specially. */
2378 if (TREE_CODE (gnu_base_orig_min
) == INTEGER_CST
2379 && TREE_CODE (gnu_base_orig_max
) == INTEGER_CST
2380 && tree_int_cst_lt (gnu_base_orig_max
, gnu_base_orig_min
))
2382 gnu_min
= size_one_node
;
2383 gnu_max
= size_zero_node
;
2387 /* Similarly, if one of the values overflows in sizetype and the
2388 range is null, use 1..0 for the sizetype bounds. */
2389 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2390 && TREE_CODE (gnu_max
) == INTEGER_CST
2391 && (TREE_OVERFLOW (gnu_min
) || TREE_OVERFLOW (gnu_max
))
2392 && tree_int_cst_lt (gnu_orig_max
, gnu_orig_min
))
2394 gnu_min
= size_one_node
;
2395 gnu_max
= size_zero_node
;
2399 /* If the minimum and maximum values both overflow in sizetype,
2400 but the difference in the original type does not overflow in
2401 sizetype, ignore the overflow indication. */
2402 else if (TREE_CODE (gnu_min
) == INTEGER_CST
2403 && TREE_CODE (gnu_max
) == INTEGER_CST
2404 && TREE_OVERFLOW (gnu_min
) && TREE_OVERFLOW (gnu_max
)
2407 fold_build2 (MINUS_EXPR
, gnu_index_type
,
2411 TREE_OVERFLOW (gnu_min
) = 0;
2412 TREE_OVERFLOW (gnu_max
) = 0;
2416 /* Compute the size of this dimension in the general case. We
2417 need to provide GCC with an upper bound to use but have to
2418 deal with the "superflat" case. There are three ways to do
2419 this. If we can prove that the array can never be superflat,
2420 we can just use the high bound of the index type. */
2421 else if ((Nkind (gnat_index
) == N_Range
2422 && cannot_be_superflat_p (gnat_index
))
2423 /* Packed Array Types are never superflat. */
2424 || Is_Packed_Array_Type (gnat_entity
))
2427 /* Otherwise, if the high bound is constant but the low bound is
2428 not, we use the expression (hb >= lb) ? lb : hb + 1 for the
2429 lower bound. Note that the comparison must be done in the
2430 original type to avoid any overflow during the conversion. */
2431 else if (TREE_CODE (gnu_max
) == INTEGER_CST
2432 && TREE_CODE (gnu_min
) != INTEGER_CST
)
2436 = build_cond_expr (sizetype
,
2437 build_binary_op (GE_EXPR
,
2442 int_const_binop (PLUS_EXPR
, gnu_max
,
2446 /* Finally we use (hb >= lb) ? hb : lb - 1 for the upper bound
2447 in all the other cases. Note that, here as well as above,
2448 the condition used in the comparison must be equivalent to
2449 the condition (length != 0). This is relied upon in order
2450 to optimize array comparisons in compare_arrays. Moreover
2451 we use int_const_binop for the shift by 1 if the bound is
2452 constant to avoid any unwanted overflow. */
2455 = build_cond_expr (sizetype
,
2456 build_binary_op (GE_EXPR
,
2461 TREE_CODE (gnu_min
) == INTEGER_CST
2462 ? int_const_binop (MINUS_EXPR
, gnu_min
,
2464 : size_binop (MINUS_EXPR
, gnu_min
,
2467 /* Reuse the index type for the range type. Then make an index
2468 type with the size range in sizetype. */
2469 gnu_index_types
[index
]
2470 = create_index_type (gnu_min
, gnu_high
, gnu_index_type
,
2473 /* Update the maximum size of the array in elements. Here we
2474 see if any constraint on the index type of the base type
2475 can be used in the case of self-referential bound on the
2476 index type of the subtype. We look for a non-"infinite"
2477 and non-self-referential bound from any type involved and
2478 handle each bound separately. */
2481 tree gnu_base_min
= convert (sizetype
, gnu_base_orig_min
);
2482 tree gnu_base_max
= convert (sizetype
, gnu_base_orig_max
);
2483 tree gnu_base_index_base_type
2484 = get_base_type (gnu_base_index_type
);
2485 tree gnu_base_base_min
2486 = convert (sizetype
,
2487 TYPE_MIN_VALUE (gnu_base_index_base_type
));
2488 tree gnu_base_base_max
2489 = convert (sizetype
,
2490 TYPE_MAX_VALUE (gnu_base_index_base_type
));
2492 if (!CONTAINS_PLACEHOLDER_P (gnu_min
)
2493 || !(TREE_CODE (gnu_base_min
) == INTEGER_CST
2494 && !TREE_OVERFLOW (gnu_base_min
)))
2495 gnu_base_min
= gnu_min
;
2497 if (!CONTAINS_PLACEHOLDER_P (gnu_max
)
2498 || !(TREE_CODE (gnu_base_max
) == INTEGER_CST
2499 && !TREE_OVERFLOW (gnu_base_max
)))
2500 gnu_base_max
= gnu_max
;
2502 if ((TREE_CODE (gnu_base_min
) == INTEGER_CST
2503 && TREE_OVERFLOW (gnu_base_min
))
2504 || operand_equal_p (gnu_base_min
, gnu_base_base_min
, 0)
2505 || (TREE_CODE (gnu_base_max
) == INTEGER_CST
2506 && TREE_OVERFLOW (gnu_base_max
))
2507 || operand_equal_p (gnu_base_max
, gnu_base_base_max
, 0))
2508 gnu_max_size
= NULL_TREE
;
2512 = size_binop (MAX_EXPR
,
2513 size_binop (PLUS_EXPR
, size_one_node
,
2514 size_binop (MINUS_EXPR
,
2519 if (TREE_CODE (gnu_this_max
) == INTEGER_CST
2520 && TREE_OVERFLOW (gnu_this_max
))
2521 gnu_max_size
= NULL_TREE
;
2524 = size_binop (MULT_EXPR
, gnu_max_size
, gnu_this_max
);
2528 /* We need special types for debugging information to point to
2529 the index types if they have variable bounds, are not integer
2530 types, are biased or are wider than sizetype. */
2531 if (!integer_onep (gnu_orig_min
)
2532 || TREE_CODE (gnu_orig_max
) != INTEGER_CST
2533 || TREE_CODE (gnu_index_type
) != INTEGER_TYPE
2534 || (TREE_TYPE (gnu_index_type
)
2535 && TREE_CODE (TREE_TYPE (gnu_index_type
))
2537 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type
)
2538 || compare_tree_int (rm_size (gnu_index_type
),
2539 TYPE_PRECISION (sizetype
)) > 0)
2540 need_index_type_struct
= true;
2543 /* Then flatten: create the array of arrays. For an array type
2544 used to implement a packed array, get the component type from
2545 the original array type since the representation clauses that
2546 can affect it are on the latter. */
2547 if (Is_Packed_Array_Type (gnat_entity
)
2548 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)))
2550 gnu_type
= gnat_to_gnu_type (Original_Array_Type (gnat_entity
));
2551 for (index
= ndim
- 1; index
>= 0; index
--)
2552 gnu_type
= TREE_TYPE (gnu_type
);
2554 /* One of the above calls might have caused us to be elaborated,
2555 so don't blow up if so. */
2556 if (present_gnu_tree (gnat_entity
))
2558 maybe_present
= true;
2564 gnu_type
= gnat_to_gnu_component_type (gnat_entity
, definition
,
2567 /* One of the above calls might have caused us to be elaborated,
2568 so don't blow up if so. */
2569 if (present_gnu_tree (gnat_entity
))
2571 maybe_present
= true;
2576 /* Compute the maximum size of the array in units and bits. */
2579 gnu_max_size_unit
= size_binop (MULT_EXPR
, gnu_max_size
,
2580 TYPE_SIZE_UNIT (gnu_type
));
2581 gnu_max_size
= size_binop (MULT_EXPR
,
2582 convert (bitsizetype
, gnu_max_size
),
2583 TYPE_SIZE (gnu_type
));
2586 gnu_max_size_unit
= NULL_TREE
;
2588 /* Now build the array type. */
2589 for (index
= ndim
- 1; index
>= 0; index
--)
2591 gnu_type
= build_nonshared_array_type (gnu_type
,
2592 gnu_index_types
[index
]);
2593 TYPE_MULTI_ARRAY_P (gnu_type
) = (index
> 0);
2594 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2595 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2597 /* See the E_Array_Type case for the rationale. */
2598 if (TYPE_MODE (gnu_type
) != BLKmode
2599 && Is_By_Reference_Type (gnat_entity
))
2600 SET_TYPE_MODE (gnu_type
, BLKmode
);
2603 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2604 TYPE_STUB_DECL (gnu_type
)
2605 = create_type_stub_decl (gnu_entity_name
, gnu_type
);
2607 /* If we are at file level and this is a multi-dimensional array,
2608 we need to make a variable corresponding to the stride of the
2609 inner dimensions. */
2610 if (global_bindings_p () && ndim
> 1)
2612 tree gnu_st_name
= get_identifier ("ST");
2615 for (gnu_arr_type
= TREE_TYPE (gnu_type
);
2616 TREE_CODE (gnu_arr_type
) == ARRAY_TYPE
;
2617 gnu_arr_type
= TREE_TYPE (gnu_arr_type
),
2618 gnu_st_name
= concat_name (gnu_st_name
, "ST"))
2620 tree eltype
= TREE_TYPE (gnu_arr_type
);
2622 TYPE_SIZE (gnu_arr_type
)
2623 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type
),
2624 gnat_entity
, gnu_st_name
,
2627 /* ??? For now, store the size as a multiple of the
2628 alignment of the element type in bytes so that we
2629 can see the alignment from the tree. */
2630 TYPE_SIZE_UNIT (gnu_arr_type
)
2631 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_arr_type
),
2633 concat_name (gnu_st_name
, "A_U"),
2635 TYPE_ALIGN (eltype
));
2637 /* ??? create_type_decl is not invoked on the inner types so
2638 the MULT_EXPR node built above will never be marked. */
2639 MARK_VISITED (TYPE_SIZE_UNIT (gnu_arr_type
));
2643 /* If we need to write out a record type giving the names of the
2644 bounds for debugging purposes, do it now and make the record
2645 type a parallel type. This is not needed for a packed array
2646 since the bounds are conveyed by the original array type. */
2647 if (need_index_type_struct
2649 && !Is_Packed_Array_Type (gnat_entity
))
2651 tree gnu_bound_rec
= make_node (RECORD_TYPE
);
2652 tree gnu_field_list
= NULL_TREE
;
2655 TYPE_NAME (gnu_bound_rec
)
2656 = create_concat_name (gnat_entity
, "XA");
2658 for (index
= ndim
- 1; index
>= 0; index
--)
2660 tree gnu_index
= TYPE_INDEX_TYPE (gnu_index_types
[index
]);
2661 tree gnu_index_name
= TYPE_IDENTIFIER (gnu_index
);
2663 /* Make sure to reference the types themselves, and not just
2664 their names, as the debugger may fall back on them. */
2665 gnu_field
= create_field_decl (gnu_index_name
, gnu_index
,
2666 gnu_bound_rec
, NULL_TREE
,
2668 DECL_CHAIN (gnu_field
) = gnu_field_list
;
2669 gnu_field_list
= gnu_field
;
2672 finish_record_type (gnu_bound_rec
, gnu_field_list
, 0, true);
2673 add_parallel_type (gnu_type
, gnu_bound_rec
);
2676 /* If this is a packed array type, make the original array type a
2677 parallel type. Otherwise, do it for the base array type if it
2678 isn't artificial to make sure it is kept in the debug info. */
2681 if (Is_Packed_Array_Type (gnat_entity
)
2682 && present_gnu_tree (Original_Array_Type (gnat_entity
)))
2683 add_parallel_type (gnu_type
,
2685 (Original_Array_Type (gnat_entity
)));
2689 = gnat_to_gnu_entity (Etype (gnat_entity
), NULL_TREE
, 0);
2690 if (!DECL_ARTIFICIAL (gnu_base_decl
))
2691 add_parallel_type (gnu_type
,
2692 TREE_TYPE (TREE_TYPE (gnu_base_decl
)));
2696 TYPE_CONVENTION_FORTRAN_P (gnu_type
) = convention_fortran_p
;
2697 TYPE_PACKED_ARRAY_TYPE_P (gnu_type
)
2698 = (Is_Packed_Array_Type (gnat_entity
)
2699 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity
)));
2701 /* If the size is self-referential and the maximum size doesn't
2702 overflow, use it. */
2703 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
))
2705 && !(TREE_CODE (gnu_max_size
) == INTEGER_CST
2706 && TREE_OVERFLOW (gnu_max_size
))
2707 && !(TREE_CODE (gnu_max_size_unit
) == INTEGER_CST
2708 && TREE_OVERFLOW (gnu_max_size_unit
)))
2710 TYPE_SIZE (gnu_type
) = size_binop (MIN_EXPR
, gnu_max_size
,
2711 TYPE_SIZE (gnu_type
));
2712 TYPE_SIZE_UNIT (gnu_type
)
2713 = size_binop (MIN_EXPR
, gnu_max_size_unit
,
2714 TYPE_SIZE_UNIT (gnu_type
));
2717 /* Set our alias set to that of our base type. This gives all
2718 array subtypes the same alias set. */
2719 relate_alias_sets (gnu_type
, gnu_base_type
, ALIAS_SET_COPY
);
2721 /* If this is a packed type, make this type the same as the packed
2722 array type, but do some adjusting in the type first. */
2723 if (Present (Packed_Array_Type (gnat_entity
)))
2725 Entity_Id gnat_index
;
2728 /* First finish the type we had been making so that we output
2729 debugging information for it. */
2730 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
2731 if (Treat_As_Volatile (gnat_entity
))
2733 = build_qualified_type (gnu_type
,
2734 TYPE_QUALS (gnu_type
)
2735 | TYPE_QUAL_VOLATILE
);
2736 /* Make it artificial only if the base type was artificial too.
2737 That's sort of "morally" true and will make it possible for
2738 the debugger to look it up by name in DWARF, which is needed
2739 in order to decode the packed array type. */
2741 = create_type_decl (gnu_entity_name
, gnu_type
,
2742 !Comes_From_Source (Etype (gnat_entity
))
2743 && !Comes_From_Source (gnat_entity
),
2744 debug_info_p
, gnat_entity
);
2746 /* Save it as our equivalent in case the call below elaborates
2748 save_gnu_tree (gnat_entity
, gnu_decl
, false);
2750 gnu_decl
= gnat_to_gnu_entity (Packed_Array_Type (gnat_entity
),
2752 this_made_decl
= true;
2753 gnu_type
= TREE_TYPE (gnu_decl
);
2754 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
2756 gnu_inner
= gnu_type
;
2757 while (TREE_CODE (gnu_inner
) == RECORD_TYPE
2758 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner
)
2759 || TYPE_PADDING_P (gnu_inner
)))
2760 gnu_inner
= TREE_TYPE (TYPE_FIELDS (gnu_inner
));
2762 /* We need to attach the index type to the type we just made so
2763 that the actual bounds can later be put into a template. */
2764 if ((TREE_CODE (gnu_inner
) == ARRAY_TYPE
2765 && !TYPE_ACTUAL_BOUNDS (gnu_inner
))
2766 || (TREE_CODE (gnu_inner
) == INTEGER_TYPE
2767 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
)))
2769 if (TREE_CODE (gnu_inner
) == INTEGER_TYPE
)
2771 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2772 TYPE_MODULUS for modular types so we make an extra
2773 subtype if necessary. */
2774 if (TYPE_MODULAR_P (gnu_inner
))
2777 = make_unsigned_type (TYPE_PRECISION (gnu_inner
));
2778 TREE_TYPE (gnu_subtype
) = gnu_inner
;
2779 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
2780 SET_TYPE_RM_MIN_VALUE (gnu_subtype
,
2781 TYPE_MIN_VALUE (gnu_inner
));
2782 SET_TYPE_RM_MAX_VALUE (gnu_subtype
,
2783 TYPE_MAX_VALUE (gnu_inner
));
2784 gnu_inner
= gnu_subtype
;
2787 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner
) = 1;
2789 #ifdef ENABLE_CHECKING
2790 /* Check for other cases of overloading. */
2791 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner
));
2795 for (gnat_index
= First_Index (gnat_entity
);
2796 Present (gnat_index
);
2797 gnat_index
= Next_Index (gnat_index
))
2798 SET_TYPE_ACTUAL_BOUNDS
2800 tree_cons (NULL_TREE
,
2801 get_unpadded_type (Etype (gnat_index
)),
2802 TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2804 if (Convention (gnat_entity
) != Convention_Fortran
)
2805 SET_TYPE_ACTUAL_BOUNDS
2806 (gnu_inner
, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner
)));
2808 if (TREE_CODE (gnu_type
) == RECORD_TYPE
2809 && TYPE_JUSTIFIED_MODULAR_P (gnu_type
))
2810 TREE_TYPE (TYPE_FIELDS (gnu_type
)) = gnu_inner
;
2815 /* Abort if packed array with no Packed_Array_Type field set. */
2816 gcc_assert (!Is_Packed (gnat_entity
));
2820 case E_String_Literal_Subtype
:
2821 /* Create the type for a string literal. */
2823 Entity_Id gnat_full_type
2824 = (IN (Ekind (Etype (gnat_entity
)), Private_Kind
)
2825 && Present (Full_View (Etype (gnat_entity
)))
2826 ? Full_View (Etype (gnat_entity
)) : Etype (gnat_entity
));
2827 tree gnu_string_type
= get_unpadded_type (gnat_full_type
);
2828 tree gnu_string_array_type
2829 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type
))));
2830 tree gnu_string_index_type
2831 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2832 (TYPE_DOMAIN (gnu_string_array_type
))));
2833 tree gnu_lower_bound
2834 = convert (gnu_string_index_type
,
2835 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity
)));
2837 = UI_To_gnu (String_Literal_Length (gnat_entity
),
2838 gnu_string_index_type
);
2839 tree gnu_upper_bound
2840 = build_binary_op (PLUS_EXPR
, gnu_string_index_type
,
2842 int_const_binop (MINUS_EXPR
, gnu_length
,
2843 convert (gnu_string_index_type
,
2844 integer_one_node
)));
2846 = create_index_type (convert (sizetype
, gnu_lower_bound
),
2847 convert (sizetype
, gnu_upper_bound
),
2848 create_range_type (gnu_string_index_type
,
2854 = build_nonshared_array_type (gnat_to_gnu_type
2855 (Component_Type (gnat_entity
)),
2857 if (array_type_has_nonaliased_component (gnu_type
, gnat_entity
))
2858 TYPE_NONALIASED_COMPONENT (gnu_type
) = 1;
2859 relate_alias_sets (gnu_type
, gnu_string_type
, ALIAS_SET_COPY
);
2863 /* Record Types and Subtypes
2865 The following fields are defined on record types:
2867 Has_Discriminants True if the record has discriminants
2868 First_Discriminant Points to head of list of discriminants
2869 First_Entity Points to head of list of fields
2870 Is_Tagged_Type True if the record is tagged
2872 Implementation of Ada records and discriminated records:
2874 A record type definition is transformed into the equivalent of a C
2875 struct definition. The fields that are the discriminants which are
2876 found in the Full_Type_Declaration node and the elements of the
2877 Component_List found in the Record_Type_Definition node. The
2878 Component_List can be a recursive structure since each Variant of
2879 the Variant_Part of the Component_List has a Component_List.
2881 Processing of a record type definition comprises starting the list of
2882 field declarations here from the discriminants and the calling the
2883 function components_to_record to add the rest of the fields from the
2884 component list and return the gnu type node. The function
2885 components_to_record will call itself recursively as it traverses
2889 if (Has_Complex_Representation (gnat_entity
))
2892 = build_complex_type
2894 (Etype (Defining_Entity
2895 (First (Component_Items
2898 (Declaration_Node (gnat_entity
)))))))));
2904 Node_Id full_definition
= Declaration_Node (gnat_entity
);
2905 Node_Id record_definition
= Type_Definition (full_definition
);
2906 Node_Id gnat_constr
;
2907 Entity_Id gnat_field
;
2908 tree gnu_field
, gnu_field_list
= NULL_TREE
;
2909 tree gnu_get_parent
;
2910 /* Set PACKED in keeping with gnat_to_gnu_field. */
2912 = Is_Packed (gnat_entity
)
2914 : Component_Alignment (gnat_entity
) == Calign_Storage_Unit
2916 : (Known_Alignment (gnat_entity
)
2917 || (Strict_Alignment (gnat_entity
)
2918 && Known_RM_Size (gnat_entity
)))
2921 const bool has_discr
= Has_Discriminants (gnat_entity
);
2922 const bool has_rep
= Has_Specified_Layout (gnat_entity
);
2923 const bool is_extension
2924 = (Is_Tagged_Type (gnat_entity
)
2925 && Nkind (record_definition
) == N_Derived_Type_Definition
);
2926 const bool is_unchecked_union
= Is_Unchecked_Union (gnat_entity
);
2927 bool all_rep
= has_rep
;
2929 /* See if all fields have a rep clause. Stop when we find one
2932 for (gnat_field
= First_Entity (gnat_entity
);
2933 Present (gnat_field
);
2934 gnat_field
= Next_Entity (gnat_field
))
2935 if ((Ekind (gnat_field
) == E_Component
2936 || Ekind (gnat_field
) == E_Discriminant
)
2937 && No (Component_Clause (gnat_field
)))
2943 /* If this is a record extension, go a level further to find the
2944 record definition. Also, verify we have a Parent_Subtype. */
2947 if (!type_annotate_only
2948 || Present (Record_Extension_Part (record_definition
)))
2949 record_definition
= Record_Extension_Part (record_definition
);
2951 gcc_assert (type_annotate_only
2952 || Present (Parent_Subtype (gnat_entity
)));
2955 /* Make a node for the record. If we are not defining the record,
2956 suppress expanding incomplete types. */
2957 gnu_type
= make_node (tree_code_for_record_type (gnat_entity
));
2958 TYPE_NAME (gnu_type
) = gnu_entity_name
;
2959 TYPE_PACKED (gnu_type
) = (packed
!= 0) || has_rep
;
2960 if (Reverse_Storage_Order (gnat_entity
))
2961 sorry ("non-default Scalar_Storage_Order");
2962 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
2966 defer_incomplete_level
++;
2967 this_deferred
= true;
2970 /* If both a size and rep clause was specified, put the size in
2971 the record type now so that it can get the proper mode. */
2972 if (has_rep
&& Known_RM_Size (gnat_entity
))
2973 TYPE_SIZE (gnu_type
)
2974 = UI_To_gnu (RM_Size (gnat_entity
), bitsizetype
);
2976 /* Always set the alignment here so that it can be used to
2977 set the mode, if it is making the alignment stricter. If
2978 it is invalid, it will be checked again below. If this is to
2979 be Atomic, choose a default alignment of a word unless we know
2980 the size and it's smaller. */
2981 if (Known_Alignment (gnat_entity
))
2982 TYPE_ALIGN (gnu_type
)
2983 = validate_alignment (Alignment (gnat_entity
), gnat_entity
, 0);
2984 else if (Is_Atomic (gnat_entity
) && Known_Esize (gnat_entity
))
2986 unsigned int size
= UI_To_Int (Esize (gnat_entity
));
2987 TYPE_ALIGN (gnu_type
)
2988 = size
>= BITS_PER_WORD
? BITS_PER_WORD
: ceil_pow2 (size
);
2990 /* If a type needs strict alignment, the minimum size will be the
2991 type size instead of the RM size (see validate_size). Cap the
2992 alignment, lest it causes this type size to become too large. */
2993 else if (Strict_Alignment (gnat_entity
) && Known_RM_Size (gnat_entity
))
2995 unsigned int raw_size
= UI_To_Int (RM_Size (gnat_entity
));
2996 unsigned int raw_align
= raw_size
& -raw_size
;
2997 if (raw_align
< BIGGEST_ALIGNMENT
)
2998 TYPE_ALIGN (gnu_type
) = raw_align
;
3001 TYPE_ALIGN (gnu_type
) = 0;
3003 /* If we have a Parent_Subtype, make a field for the parent. If
3004 this record has rep clauses, force the position to zero. */
3005 if (Present (Parent_Subtype (gnat_entity
)))
3007 Entity_Id gnat_parent
= Parent_Subtype (gnat_entity
);
3008 tree gnu_dummy_parent_type
= make_node (RECORD_TYPE
);
3011 /* A major complexity here is that the parent subtype will
3012 reference our discriminants in its Stored_Constraint list.
3013 But those must reference the parent component of this record
3014 which is precisely of the parent subtype we have not built yet!
3015 To break the circle we first build a dummy COMPONENT_REF which
3016 represents the "get to the parent" operation and initialize
3017 each of those discriminants to a COMPONENT_REF of the above
3018 dummy parent referencing the corresponding discriminant of the
3019 base type of the parent subtype. */
3020 gnu_get_parent
= build3 (COMPONENT_REF
, gnu_dummy_parent_type
,
3021 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3022 build_decl (input_location
,
3023 FIELD_DECL
, NULL_TREE
,
3024 gnu_dummy_parent_type
),
3028 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3029 Present (gnat_field
);
3030 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3031 if (Present (Corresponding_Discriminant (gnat_field
)))
3034 = gnat_to_gnu_field_decl (Corresponding_Discriminant
3038 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3039 gnu_get_parent
, gnu_field
, NULL_TREE
),
3043 /* Then we build the parent subtype. If it has discriminants but
3044 the type itself has unknown discriminants, this means that it
3045 doesn't contain information about how the discriminants are
3046 derived from those of the ancestor type, so it cannot be used
3047 directly. Instead it is built by cloning the parent subtype
3048 of the underlying record view of the type, for which the above
3049 derivation of discriminants has been made explicit. */
3050 if (Has_Discriminants (gnat_parent
)
3051 && Has_Unknown_Discriminants (gnat_entity
))
3053 Entity_Id gnat_uview
= Underlying_Record_View (gnat_entity
);
3055 /* If we are defining the type, the underlying record
3056 view must already have been elaborated at this point.
3057 Otherwise do it now as its parent subtype cannot be
3058 technically elaborated on its own. */
3060 gcc_assert (present_gnu_tree (gnat_uview
));
3062 gnat_to_gnu_entity (gnat_uview
, NULL_TREE
, 0);
3064 gnu_parent
= gnat_to_gnu_type (Parent_Subtype (gnat_uview
));
3066 /* Substitute the "get to the parent" of the type for that
3067 of its underlying record view in the cloned type. */
3068 for (gnat_field
= First_Stored_Discriminant (gnat_uview
);
3069 Present (gnat_field
);
3070 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3071 if (Present (Corresponding_Discriminant (gnat_field
)))
3073 tree gnu_field
= gnat_to_gnu_field_decl (gnat_field
);
3075 = build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3076 gnu_get_parent
, gnu_field
, NULL_TREE
);
3078 = substitute_in_type (gnu_parent
, gnu_field
, gnu_ref
);
3082 gnu_parent
= gnat_to_gnu_type (gnat_parent
);
3084 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
3085 initially built. The discriminants must reference the fields
3086 of the parent subtype and not those of its base type for the
3087 placeholder machinery to properly work. */
3090 /* The actual parent subtype is the full view. */
3091 if (IN (Ekind (gnat_parent
), Private_Kind
))
3093 if (Present (Full_View (gnat_parent
)))
3094 gnat_parent
= Full_View (gnat_parent
);
3096 gnat_parent
= Underlying_Full_View (gnat_parent
);
3099 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3100 Present (gnat_field
);
3101 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3102 if (Present (Corresponding_Discriminant (gnat_field
)))
3104 Entity_Id field
= Empty
;
3105 for (field
= First_Stored_Discriminant (gnat_parent
);
3107 field
= Next_Stored_Discriminant (field
))
3108 if (same_discriminant_p (gnat_field
, field
))
3110 gcc_assert (Present (field
));
3111 TREE_OPERAND (get_gnu_tree (gnat_field
), 1)
3112 = gnat_to_gnu_field_decl (field
);
3116 /* The "get to the parent" COMPONENT_REF must be given its
3118 TREE_TYPE (gnu_get_parent
) = gnu_parent
;
3120 /* ...and reference the _Parent field of this record. */
3122 = create_field_decl (parent_name_id
,
3123 gnu_parent
, gnu_type
,
3125 ? TYPE_SIZE (gnu_parent
) : NULL_TREE
,
3127 ? bitsize_zero_node
: NULL_TREE
,
3129 DECL_INTERNAL_P (gnu_field
) = 1;
3130 TREE_OPERAND (gnu_get_parent
, 1) = gnu_field
;
3131 TYPE_FIELDS (gnu_type
) = gnu_field
;
3134 /* Make the fields for the discriminants and put them into the record
3135 unless it's an Unchecked_Union. */
3137 for (gnat_field
= First_Stored_Discriminant (gnat_entity
);
3138 Present (gnat_field
);
3139 gnat_field
= Next_Stored_Discriminant (gnat_field
))
3141 /* If this is a record extension and this discriminant is the
3142 renaming of another discriminant, we've handled it above. */
3143 if (Present (Parent_Subtype (gnat_entity
))
3144 && Present (Corresponding_Discriminant (gnat_field
)))
3148 = gnat_to_gnu_field (gnat_field
, gnu_type
, packed
, definition
,
3151 /* Make an expression using a PLACEHOLDER_EXPR from the
3152 FIELD_DECL node just created and link that with the
3153 corresponding GNAT defining identifier. */
3154 save_gnu_tree (gnat_field
,
3155 build3 (COMPONENT_REF
, TREE_TYPE (gnu_field
),
3156 build0 (PLACEHOLDER_EXPR
, gnu_type
),
3157 gnu_field
, NULL_TREE
),
3160 if (!is_unchecked_union
)
3162 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3163 gnu_field_list
= gnu_field
;
3167 /* If we have a derived untagged type that renames discriminants in
3168 the root type, the (stored) discriminants are a just copy of the
3169 discriminants of the root type. This means that any constraints
3170 added by the renaming in the derivation are disregarded as far
3171 as the layout of the derived type is concerned. To rescue them,
3172 we change the type of the (stored) discriminants to a subtype
3173 with the bounds of the type of the visible discriminants. */
3176 && Stored_Constraint (gnat_entity
) != No_Elist
)
3177 for (gnat_constr
= First_Elmt (Stored_Constraint (gnat_entity
));
3178 gnat_constr
!= No_Elmt
;
3179 gnat_constr
= Next_Elmt (gnat_constr
))
3180 if (Nkind (Node (gnat_constr
)) == N_Identifier
3181 /* Ignore access discriminants. */
3182 && !Is_Access_Type (Etype (Node (gnat_constr
)))
3183 && Ekind (Entity (Node (gnat_constr
))) == E_Discriminant
)
3185 Entity_Id gnat_discr
= Entity (Node (gnat_constr
));
3186 tree gnu_discr_type
= gnat_to_gnu_type (Etype (gnat_discr
));
3188 = gnat_to_gnu_entity (Original_Record_Component (gnat_discr
),
3191 /* GNU_REF must be an expression using a PLACEHOLDER_EXPR built
3192 just above for one of the stored discriminants. */
3193 gcc_assert (TREE_TYPE (TREE_OPERAND (gnu_ref
, 0)) == gnu_type
);
3195 if (gnu_discr_type
!= TREE_TYPE (gnu_ref
))
3197 const unsigned prec
= TYPE_PRECISION (TREE_TYPE (gnu_ref
));
3199 = TYPE_UNSIGNED (TREE_TYPE (gnu_ref
))
3200 ? make_unsigned_type (prec
) : make_signed_type (prec
);
3201 TREE_TYPE (gnu_subtype
) = TREE_TYPE (gnu_ref
);
3202 TYPE_EXTRA_SUBTYPE_P (gnu_subtype
) = 1;
3203 SET_TYPE_RM_MIN_VALUE (gnu_subtype
,
3204 TYPE_MIN_VALUE (gnu_discr_type
));
3205 SET_TYPE_RM_MAX_VALUE (gnu_subtype
,
3206 TYPE_MAX_VALUE (gnu_discr_type
));
3208 = TREE_TYPE (TREE_OPERAND (gnu_ref
, 1)) = gnu_subtype
;
3212 /* Add the fields into the record type and finish it up. */
3213 components_to_record (gnu_type
, Component_List (record_definition
),
3214 gnu_field_list
, packed
, definition
, false,
3215 all_rep
, is_unchecked_union
,
3216 !Comes_From_Source (gnat_entity
), debug_info_p
,
3217 false, OK_To_Reorder_Components (gnat_entity
),
3218 all_rep
? NULL_TREE
: bitsize_zero_node
, NULL
);
3220 /* If it is passed by reference, force BLKmode to ensure that objects
3221 of this type will always be put in memory. */
3222 if (TYPE_MODE (gnu_type
) != BLKmode
3223 && Is_By_Reference_Type (gnat_entity
))
3224 SET_TYPE_MODE (gnu_type
, BLKmode
);
3226 /* We used to remove the associations of the discriminants and _Parent
3227 for validity checking but we may need them if there's a Freeze_Node
3228 for a subtype used in this record. */
3229 TYPE_VOLATILE (gnu_type
) = Treat_As_Volatile (gnat_entity
);
3231 /* Fill in locations of fields. */
3232 annotate_rep (gnat_entity
, gnu_type
);
3234 /* If there are any entities in the chain corresponding to components
3235 that we did not elaborate, ensure we elaborate their types if they
3237 for (gnat_temp
= First_Entity (gnat_entity
);
3238 Present (gnat_temp
);
3239 gnat_temp
= Next_Entity (gnat_temp
))
3240 if ((Ekind (gnat_temp
) == E_Component
3241 || Ekind (gnat_temp
) == E_Discriminant
)
3242 && Is_Itype (Etype (gnat_temp
))
3243 && !present_gnu_tree (gnat_temp
))
3244 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
3246 /* If this is a record type associated with an exception definition,
3247 equate its fields to those of the standard exception type. This
3248 will make it possible to convert between them. */
3249 if (gnu_entity_name
== exception_data_name_id
)
3252 for (gnu_field
= TYPE_FIELDS (gnu_type
),
3253 gnu_std_field
= TYPE_FIELDS (except_type_node
);
3255 gnu_field
= DECL_CHAIN (gnu_field
),
3256 gnu_std_field
= DECL_CHAIN (gnu_std_field
))
3257 SET_DECL_ORIGINAL_FIELD_TO_FIELD (gnu_field
, gnu_std_field
);
3258 gcc_assert (!gnu_std_field
);
3263 case E_Class_Wide_Subtype
:
3264 /* If an equivalent type is present, that is what we should use.
3265 Otherwise, fall through to handle this like a record subtype
3266 since it may have constraints. */
3267 if (gnat_equiv_type
!= gnat_entity
)
3269 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
3270 maybe_present
= true;
3274 /* ... fall through ... */
3276 case E_Record_Subtype
:
3277 /* If Cloned_Subtype is Present it means this record subtype has
3278 identical layout to that type or subtype and we should use
3279 that GCC type for this one. The front end guarantees that
3280 the component list is shared. */
3281 if (Present (Cloned_Subtype (gnat_entity
)))
3283 gnu_decl
= gnat_to_gnu_entity (Cloned_Subtype (gnat_entity
),
3285 maybe_present
= true;
3289 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3290 changing the type, make a new type with each field having the type of
3291 the field in the new subtype but the position computed by transforming
3292 every discriminant reference according to the constraints. We don't
3293 see any difference between private and non-private type here since
3294 derivations from types should have been deferred until the completion
3295 of the private type. */
3298 Entity_Id gnat_base_type
= Implementation_Base_Type (gnat_entity
);
3303 defer_incomplete_level
++;
3304 this_deferred
= true;
3307 gnu_base_type
= gnat_to_gnu_type (gnat_base_type
);
3309 if (present_gnu_tree (gnat_entity
))
3311 maybe_present
= true;
3315 /* If this is a record subtype associated with a dispatch table,
3316 strip the suffix. This is necessary to make sure 2 different
3317 subtypes associated with the imported and exported views of a
3318 dispatch table are properly merged in LTO mode. */
3319 if (Is_Dispatch_Table_Entity (gnat_entity
))
3322 Get_Encoded_Name (gnat_entity
);
3323 p
= strchr (Name_Buffer
, '_');
3325 strcpy (p
+2, "dtS");
3326 gnu_entity_name
= get_identifier (Name_Buffer
);
3329 /* When the subtype has discriminants and these discriminants affect
3330 the initial shape it has inherited, factor them in. But for an
3331 Unchecked_Union (it must be an Itype), just return the type.
3332 We can't just test Is_Constrained because private subtypes without
3333 discriminants of types with discriminants with default expressions
3334 are Is_Constrained but aren't constrained! */
3335 if (IN (Ekind (gnat_base_type
), Record_Kind
)
3336 && !Is_Unchecked_Union (gnat_base_type
)
3337 && !Is_For_Access_Subtype (gnat_entity
)
3338 && Has_Discriminants (gnat_entity
)
3339 && Is_Constrained (gnat_entity
)
3340 && Stored_Constraint (gnat_entity
) != No_Elist
)
3342 vec
<subst_pair
> gnu_subst_list
3343 = build_subst_list (gnat_entity
, gnat_base_type
, definition
);
3344 tree gnu_unpad_base_type
, gnu_rep_part
, gnu_variant_part
;
3345 tree gnu_pos_list
, gnu_field_list
= NULL_TREE
;
3346 bool selected_variant
= false, all_constant_pos
= true;
3347 Entity_Id gnat_field
;
3348 vec
<variant_desc
> gnu_variant_list
;
3350 gnu_type
= make_node (RECORD_TYPE
);
3351 TYPE_NAME (gnu_type
) = gnu_entity_name
;
3352 TYPE_PACKED (gnu_type
) = TYPE_PACKED (gnu_base_type
);
3353 process_attributes (&gnu_type
, &attr_list
, true, gnat_entity
);
3355 /* Set the size, alignment and alias set of the new type to
3356 match that of the old one, doing required substitutions. */
3357 copy_and_substitute_in_size (gnu_type
, gnu_base_type
,
3360 if (TYPE_IS_PADDING_P (gnu_base_type
))
3361 gnu_unpad_base_type
= TREE_TYPE (TYPE_FIELDS (gnu_base_type
));
3363 gnu_unpad_base_type
= gnu_base_type
;
3365 /* Look for REP and variant parts in the base type. */
3366 gnu_rep_part
= get_rep_part (gnu_unpad_base_type
);
3367 gnu_variant_part
= get_variant_part (gnu_unpad_base_type
);
3369 /* If there is a variant part, we must compute whether the
3370 constraints statically select a particular variant. If
3371 so, we simply drop the qualified union and flatten the
3372 list of fields. Otherwise we'll build a new qualified
3373 union for the variants that are still relevant. */
3374 if (gnu_variant_part
)
3380 = build_variant_list (TREE_TYPE (gnu_variant_part
),
3384 /* If all the qualifiers are unconditionally true, the
3385 innermost variant is statically selected. */
3386 selected_variant
= true;
3387 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3388 if (!integer_onep (v
->qual
))
3390 selected_variant
= false;
3394 /* Otherwise, create the new variants. */
3395 if (!selected_variant
)
3396 FOR_EACH_VEC_ELT (gnu_variant_list
, i
, v
)
3398 tree old_variant
= v
->type
;
3399 tree new_variant
= make_node (RECORD_TYPE
);
3401 = concat_name (DECL_NAME (gnu_variant_part
),
3403 (DECL_NAME (v
->field
)));
3404 TYPE_NAME (new_variant
)
3405 = concat_name (TYPE_NAME (gnu_type
),
3406 IDENTIFIER_POINTER (suffix
));
3407 copy_and_substitute_in_size (new_variant
, old_variant
,
3409 v
->new_type
= new_variant
;
3414 gnu_variant_list
.create (0);
3415 selected_variant
= false;
3418 /* Make a list of fields and their position in the base type. */
3420 = build_position_list (gnu_unpad_base_type
,
3421 gnu_variant_list
.exists ()
3422 && !selected_variant
,
3423 size_zero_node
, bitsize_zero_node
,
3424 BIGGEST_ALIGNMENT
, NULL_TREE
);
3426 /* Now go down every component in the subtype and compute its
3427 size and position from those of the component in the base
3428 type and from the constraints of the subtype. */
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
))
3437 (Scope (Original_Record_Component (gnat_field
)))
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
, gnu_pos
;
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 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type
)))
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
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 || ((rep_part
= get_rep_part (v
->type
))
3509 && gnu_context
== TREE_TYPE (rep_part
)))
3513 if (selected_variant
)
3514 gnu_cont_type
= gnu_type
;
3516 gnu_cont_type
= v
->new_type
;
3519 /* The front-end may pass us "ghost" components if
3520 it fails to recognize that a constrained subtype
3521 is statically constrained. Discard them. */
3525 /* Now create the new field modeled on the old one. */
3527 = create_field_decl_from (gnu_old_field
, gnu_field_type
,
3528 gnu_cont_type
, gnu_size
,
3529 gnu_pos_list
, gnu_subst_list
);
3530 gnu_pos
= DECL_FIELD_OFFSET (gnu_field
);
3532 /* Put it in one of the new variants directly. */
3533 if (gnu_cont_type
!= gnu_type
)
3535 DECL_CHAIN (gnu_field
) = TYPE_FIELDS (gnu_cont_type
);
3536 TYPE_FIELDS (gnu_cont_type
) = gnu_field
;
3539 /* To match the layout crafted in components_to_record,
3540 if this is the _Tag or _Parent field, put it before
3541 any other fields. */
3542 else if (gnat_name
== Name_uTag
3543 || gnat_name
== Name_uParent
)
3544 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
3546 /* Similarly, if this is the _Controller field, put
3547 it before the other fields except for the _Tag or
3549 else if (gnat_name
== Name_uController
&& gnu_last
)
3551 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
3552 DECL_CHAIN (gnu_last
) = gnu_field
;
3555 /* Otherwise, if this is a regular field, put it after
3556 the other fields. */
3559 DECL_CHAIN (gnu_field
) = gnu_field_list
;
3560 gnu_field_list
= gnu_field
;
3562 gnu_last
= gnu_field
;
3563 if (TREE_CODE (gnu_pos
) != INTEGER_CST
)
3564 all_constant_pos
= false;
3567 save_gnu_tree (gnat_field
, gnu_field
, false);
3570 /* If there is a variant list, a selected variant and the fields
3571 all have a constant position, put them in order of increasing
3572 position to match that of constant CONSTRUCTORs. Likewise if
3573 there is no variant list but a REP part, since the latter has
3574 been flattened in the process. */
3575 if (((gnu_variant_list
.exists () && selected_variant
)
3576 || (!gnu_variant_list
.exists () && gnu_rep_part
))
3577 && all_constant_pos
)
3579 const int len
= list_length (gnu_field_list
);
3580 tree
*field_arr
= XALLOCAVEC (tree
, len
), t
;
3583 for (t
= gnu_field_list
, i
= 0; t
; t
= DECL_CHAIN (t
), i
++)
3586 qsort (field_arr
, len
, sizeof (tree
), compare_field_bitpos
);
3588 gnu_field_list
= NULL_TREE
;
3589 for (i
= 0; i
< len
; i
++)
3591 DECL_CHAIN (field_arr
[i
]) = gnu_field_list
;
3592 gnu_field_list
= field_arr
[i
];
3596 /* If there is a variant list and no selected variant, we need
3597 to create the nest of variant parts from the old nest. */
3598 else if (gnu_variant_list
.exists () && !selected_variant
)
3600 tree new_variant_part
3601 = create_variant_part_from (gnu_variant_part
,
3602 gnu_variant_list
, gnu_type
,
3603 gnu_pos_list
, gnu_subst_list
);
3604 DECL_CHAIN (new_variant_part
) = gnu_field_list
;
3605 gnu_field_list
= new_variant_part
;
3608 /* Now go through the entities again looking for Itypes that
3609 we have not elaborated but should (e.g., Etypes of fields
3610 that have Original_Components). */
3611 for (gnat_field
= First_Entity (gnat_entity
);
3612 Present (gnat_field
); gnat_field
= Next_Entity (gnat_field
))
3613 if ((Ekind (gnat_field
) == E_Discriminant
3614 || Ekind (gnat_field
) == E_Component
)
3615 && !present_gnu_tree (Etype (gnat_field
)))
3616 gnat_to_gnu_entity (Etype (gnat_field
), NULL_TREE
, 0);
3618 /* Do not emit debug info for the type yet since we're going to
3620 finish_record_type (gnu_type
, nreverse (gnu_field_list
), 2,
3622 compute_record_mode (gnu_type
);
3624 /* See the E_Record_Type case for the rationale. */
3625 if (TYPE_MODE (gnu_type
) != BLKmode
3626 && Is_By_Reference_Type (gnat_entity
))
3627 SET_TYPE_MODE (gnu_type
, BLKmode
);
3629 TYPE_VOLATILE (gnu_type
) = Treat_As_Volatile (gnat_entity
);
3631 /* Fill in locations of fields. */
3632 annotate_rep (gnat_entity
, gnu_type
);
3634 /* If debugging information is being written for the type, write
3635 a record that shows what we are a subtype of and also make a
3636 variable that indicates our size, if still variable. */
3639 tree gnu_subtype_marker
= make_node (RECORD_TYPE
);
3640 tree gnu_unpad_base_name
3641 = TYPE_IDENTIFIER (gnu_unpad_base_type
);
3642 tree gnu_size_unit
= TYPE_SIZE_UNIT (gnu_type
);
3644 TYPE_NAME (gnu_subtype_marker
)
3645 = create_concat_name (gnat_entity
, "XVS");
3646 finish_record_type (gnu_subtype_marker
,
3647 create_field_decl (gnu_unpad_base_name
,
3648 build_reference_type
3649 (gnu_unpad_base_type
),
3651 NULL_TREE
, NULL_TREE
,
3655 add_parallel_type (gnu_type
, gnu_subtype_marker
);
3658 && TREE_CODE (gnu_size_unit
) != INTEGER_CST
3659 && !CONTAINS_PLACEHOLDER_P (gnu_size_unit
))
3660 TYPE_SIZE_UNIT (gnu_subtype_marker
)
3661 = create_var_decl (create_concat_name (gnat_entity
,
3663 NULL_TREE
, sizetype
, gnu_size_unit
,
3664 false, false, false, false, NULL
,
3668 gnu_variant_list
.release ();
3669 gnu_subst_list
.release ();
3671 /* Now we can finalize it. */
3672 rest_of_record_type_compilation (gnu_type
);
3675 /* Otherwise, go down all the components in the new type and make
3676 them equivalent to those in the base type. */
3679 gnu_type
= gnu_base_type
;
3681 for (gnat_temp
= First_Entity (gnat_entity
);
3682 Present (gnat_temp
);
3683 gnat_temp
= Next_Entity (gnat_temp
))
3684 if ((Ekind (gnat_temp
) == E_Discriminant
3685 && !Is_Unchecked_Union (gnat_base_type
))
3686 || Ekind (gnat_temp
) == E_Component
)
3687 save_gnu_tree (gnat_temp
,
3688 gnat_to_gnu_field_decl
3689 (Original_Record_Component (gnat_temp
)),
3695 case E_Access_Subprogram_Type
:
3696 /* Use the special descriptor type for dispatch tables if needed,
3697 that is to say for the Prim_Ptr of a-tags.ads and its clones.
3698 Note that we are only required to do so for static tables in
3699 order to be compatible with the C++ ABI, but Ada 2005 allows
3700 to extend library level tagged types at the local level so
3701 we do it in the non-static case as well. */
3702 if (TARGET_VTABLE_USES_DESCRIPTORS
3703 && Is_Dispatch_Table_Entity (gnat_entity
))
3705 gnu_type
= fdesc_type_node
;
3706 gnu_size
= TYPE_SIZE (gnu_type
);
3710 /* ... fall through ... */
3712 case E_Anonymous_Access_Subprogram_Type
:
3713 /* If we are not defining this entity, and we have incomplete
3714 entities being processed above us, make a dummy type and
3715 fill it in later. */
3716 if (!definition
&& defer_incomplete_level
!= 0)
3718 struct incomplete
*p
= XNEW (struct incomplete
);
3721 = build_pointer_type
3722 (make_dummy_type (Directly_Designated_Type (gnat_entity
)));
3723 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
3724 !Comes_From_Source (gnat_entity
),
3725 debug_info_p
, gnat_entity
);
3726 this_made_decl
= true;
3727 gnu_type
= TREE_TYPE (gnu_decl
);
3728 save_gnu_tree (gnat_entity
, gnu_decl
, false);
3731 p
->old_type
= TREE_TYPE (gnu_type
);
3732 p
->full_type
= Directly_Designated_Type (gnat_entity
);
3733 p
->next
= defer_incomplete_list
;
3734 defer_incomplete_list
= p
;
3738 /* ... fall through ... */
3740 case E_Allocator_Type
:
3742 case E_Access_Attribute_Type
:
3743 case E_Anonymous_Access_Type
:
3744 case E_General_Access_Type
:
3746 /* The designated type and its equivalent type for gigi. */
3747 Entity_Id gnat_desig_type
= Directly_Designated_Type (gnat_entity
);
3748 Entity_Id gnat_desig_equiv
= Gigi_Equivalent_Type (gnat_desig_type
);
3749 /* Whether it comes from a limited with. */
3750 bool is_from_limited_with
3751 = (IN (Ekind (gnat_desig_equiv
), Incomplete_Kind
)
3752 && From_Limited_With (gnat_desig_equiv
));
3753 /* The "full view" of the designated type. If this is an incomplete
3754 entity from a limited with, treat its non-limited view as the full
3755 view. Otherwise, if this is an incomplete or private type, use the
3756 full view. In the former case, we might point to a private type,
3757 in which case, we need its full view. Also, we want to look at the
3758 actual type used for the representation, so this takes a total of
3760 Entity_Id gnat_desig_full_direct_first
3761 = (is_from_limited_with
3762 ? Non_Limited_View (gnat_desig_equiv
)
3763 : (IN (Ekind (gnat_desig_equiv
), Incomplete_Or_Private_Kind
)
3764 ? Full_View (gnat_desig_equiv
) : Empty
));
3765 Entity_Id gnat_desig_full_direct
3766 = ((is_from_limited_with
3767 && Present (gnat_desig_full_direct_first
)
3768 && IN (Ekind (gnat_desig_full_direct_first
), Private_Kind
))
3769 ? Full_View (gnat_desig_full_direct_first
)
3770 : gnat_desig_full_direct_first
);
3771 Entity_Id gnat_desig_full
3772 = Gigi_Equivalent_Type (gnat_desig_full_direct
);
3773 /* The type actually used to represent the designated type, either
3774 gnat_desig_full or gnat_desig_equiv. */
3775 Entity_Id gnat_desig_rep
;
3776 /* True if this is a pointer to an unconstrained array. */
3777 bool is_unconstrained_array
;
3778 /* We want to know if we'll be seeing the freeze node for any
3779 incomplete type we may be pointing to. */
3781 = (Present (gnat_desig_full
)
3782 ? In_Extended_Main_Code_Unit (gnat_desig_full
)
3783 : In_Extended_Main_Code_Unit (gnat_desig_type
));
3784 /* True if we make a dummy type here. */
3785 bool made_dummy
= false;
3786 /* The mode to be used for the pointer type. */
3787 enum machine_mode p_mode
= mode_for_size (esize
, MODE_INT
, 0);
3788 /* The GCC type used for the designated type. */
3789 tree gnu_desig_type
= NULL_TREE
;
3791 if (!targetm
.valid_pointer_mode (p_mode
))
3794 /* If either the designated type or its full view is an unconstrained
3795 array subtype, replace it with the type it's a subtype of. This
3796 avoids problems with multiple copies of unconstrained array types.
3797 Likewise, if the designated type is a subtype of an incomplete
3798 record type, use the parent type to avoid order of elaboration
3799 issues. This can lose some code efficiency, but there is no
3801 if (Ekind (gnat_desig_equiv
) == E_Array_Subtype
3802 && !Is_Constrained (gnat_desig_equiv
))
3803 gnat_desig_equiv
= Etype (gnat_desig_equiv
);
3804 if (Present (gnat_desig_full
)
3805 && ((Ekind (gnat_desig_full
) == E_Array_Subtype
3806 && !Is_Constrained (gnat_desig_full
))
3807 || (Ekind (gnat_desig_full
) == E_Record_Subtype
3808 && Ekind (Etype (gnat_desig_full
)) == E_Record_Type
)))
3809 gnat_desig_full
= Etype (gnat_desig_full
);
3811 /* Set the type that's actually the representation of the designated
3812 type and also flag whether we have a unconstrained array. */
3814 = Present (gnat_desig_full
) ? gnat_desig_full
: gnat_desig_equiv
;
3815 is_unconstrained_array
3816 = Is_Array_Type (gnat_desig_rep
) && !Is_Constrained (gnat_desig_rep
);
3818 /* If we are pointing to an incomplete type whose completion is an
3819 unconstrained array, make dummy fat and thin pointer types to it.
3820 Likewise if the type itself is dummy or an unconstrained array. */
3821 if (is_unconstrained_array
3822 && (Present (gnat_desig_full
)
3823 || (present_gnu_tree (gnat_desig_equiv
)
3825 (TREE_TYPE (get_gnu_tree (gnat_desig_equiv
))))
3827 && defer_incomplete_level
!= 0
3828 && !present_gnu_tree (gnat_desig_equiv
))
3830 && is_from_limited_with
3831 && Present (Freeze_Node (gnat_desig_equiv
)))))
3833 if (present_gnu_tree (gnat_desig_rep
))
3834 gnu_desig_type
= TREE_TYPE (get_gnu_tree (gnat_desig_rep
));
3837 gnu_desig_type
= make_dummy_type (gnat_desig_rep
);
3841 /* If the call above got something that has a pointer, the pointer
3842 is our type. This could have happened either because the type
3843 was elaborated or because somebody else executed the code. */
3844 if (!TYPE_POINTER_TO (gnu_desig_type
))
3845 build_dummy_unc_pointer_types (gnat_desig_equiv
, gnu_desig_type
);
3846 gnu_type
= TYPE_POINTER_TO (gnu_desig_type
);
3849 /* If we already know what the full type is, use it. */
3850 else if (Present (gnat_desig_full
)
3851 && present_gnu_tree (gnat_desig_full
))
3852 gnu_desig_type
= TREE_TYPE (get_gnu_tree (gnat_desig_full
));
3854 /* Get the type of the thing we are to point to and build a pointer to
3855 it. If it is a reference to an incomplete or private type with a
3856 full view that is a record, make a dummy type node and get the
3857 actual type later when we have verified it is safe. */
3858 else if ((!in_main_unit
3859 && !present_gnu_tree (gnat_desig_equiv
)
3860 && Present (gnat_desig_full
)
3861 && !present_gnu_tree (gnat_desig_full
)
3862 && Is_Record_Type (gnat_desig_full
))
3863 /* Likewise if we are pointing to a record or array and we are
3864 to defer elaborating incomplete types. We do this as this
3865 access type may be the full view of a private type. Note
3866 that the unconstrained array case is handled above. */
3867 || ((!in_main_unit
|| imported_p
)
3868 && defer_incomplete_level
!= 0
3869 && !present_gnu_tree (gnat_desig_equiv
)
3870 && (Is_Record_Type (gnat_desig_rep
)
3871 || Is_Array_Type (gnat_desig_rep
)))
3872 /* If this is a reference from a limited_with type back to our
3873 main unit and there's a freeze node for it, either we have
3874 already processed the declaration and made the dummy type,
3875 in which case we just reuse the latter, or we have not yet,
3876 in which case we make the dummy type and it will be reused
3877 when the declaration is finally processed. In both cases,
3878 the pointer eventually created below will be automatically
3879 adjusted when the freeze node is processed. Note that the
3880 unconstrained array case is handled above. */
3882 && is_from_limited_with
3883 && Present (Freeze_Node (gnat_desig_rep
))))
3885 gnu_desig_type
= make_dummy_type (gnat_desig_equiv
);
3889 /* Otherwise handle the case of a pointer to itself. */
3890 else if (gnat_desig_equiv
== gnat_entity
)
3893 = build_pointer_type_for_mode (void_type_node
, p_mode
,
3894 No_Strict_Aliasing (gnat_entity
));
3895 TREE_TYPE (gnu_type
) = TYPE_POINTER_TO (gnu_type
) = gnu_type
;
3898 /* If expansion is disabled, the equivalent type of a concurrent type
3899 is absent, so build a dummy pointer type. */
3900 else if (type_annotate_only
&& No (gnat_desig_equiv
))
3901 gnu_type
= ptr_void_type_node
;
3903 /* Finally, handle the default case where we can just elaborate our
3906 gnu_desig_type
= gnat_to_gnu_type (gnat_desig_equiv
);
3908 /* It is possible that a call to gnat_to_gnu_type above resolved our
3909 type. If so, just return it. */
3910 if (present_gnu_tree (gnat_entity
))
3912 maybe_present
= true;
3916 /* If we haven't done it yet, build the pointer type the usual way. */
3919 /* Modify the designated type if we are pointing only to constant
3920 objects, but don't do it for unconstrained arrays. */
3921 if (Is_Access_Constant (gnat_entity
)
3922 && TREE_CODE (gnu_desig_type
) != UNCONSTRAINED_ARRAY_TYPE
)
3925 = build_qualified_type
3927 TYPE_QUALS (gnu_desig_type
) | TYPE_QUAL_CONST
);
3929 /* Some extra processing is required if we are building a
3930 pointer to an incomplete type (in the GCC sense). We might
3931 have such a type if we just made a dummy, or directly out
3932 of the call to gnat_to_gnu_type above if we are processing
3933 an access type for a record component designating the
3934 record type itself. */
3935 if (TYPE_MODE (gnu_desig_type
) == VOIDmode
)
3937 /* We must ensure that the pointer to variant we make will
3938 be processed by update_pointer_to when the initial type
3939 is completed. Pretend we made a dummy and let further
3940 processing act as usual. */
3943 /* We must ensure that update_pointer_to will not retrieve
3944 the dummy variant when building a properly qualified
3945 version of the complete type. We take advantage of the
3946 fact that get_qualified_type is requiring TYPE_NAMEs to
3947 match to influence build_qualified_type and then also
3948 update_pointer_to here. */
3949 TYPE_NAME (gnu_desig_type
)
3950 = create_concat_name (gnat_desig_type
, "INCOMPLETE_CST");
3955 = build_pointer_type_for_mode (gnu_desig_type
, p_mode
,
3956 No_Strict_Aliasing (gnat_entity
));
3959 /* If we are not defining this object and we have made a dummy pointer,
3960 save our current definition, evaluate the actual type, and replace
3961 the tentative type we made with the actual one. If we are to defer
3962 actually looking up the actual type, make an entry in the deferred
3963 list. If this is from a limited with, we may have to defer to the
3964 end of the current unit. */
3965 if ((!in_main_unit
|| is_from_limited_with
) && made_dummy
)
3967 tree gnu_old_desig_type
;
3969 if (TYPE_IS_FAT_POINTER_P (gnu_type
))
3971 gnu_old_desig_type
= TYPE_UNCONSTRAINED_ARRAY (gnu_type
);
3972 if (esize
== POINTER_SIZE
)
3973 gnu_type
= build_pointer_type
3974 (TYPE_OBJECT_RECORD_TYPE (gnu_old_desig_type
));
3977 gnu_old_desig_type
= TREE_TYPE (gnu_type
);
3979 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
3980 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
3981 !Comes_From_Source (gnat_entity
),
3982 debug_info_p
, gnat_entity
);
3983 this_made_decl
= true;
3984 gnu_type
= TREE_TYPE (gnu_decl
);
3985 save_gnu_tree (gnat_entity
, gnu_decl
, false);
3988 /* Note that the call to gnat_to_gnu_type on gnat_desig_equiv might
3989 update gnu_old_desig_type directly, in which case it will not be
3990 a dummy type any more when we get into update_pointer_to.
3992 This can happen e.g. when the designated type is a record type,
3993 because their elaboration starts with an initial node from
3994 make_dummy_type, which may be the same node as the one we got.
3996 Besides, variants of this non-dummy type might have been created
3997 along the way. update_pointer_to is expected to properly take
3998 care of those situations. */
3999 if (defer_incomplete_level
== 0 && !is_from_limited_with
)
4001 update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_desig_type
),
4002 gnat_to_gnu_type (gnat_desig_equiv
));
4006 struct incomplete
*p
= XNEW (struct incomplete
);
4007 struct incomplete
**head
4008 = (is_from_limited_with
4009 ? &defer_limited_with
: &defer_incomplete_list
);
4010 p
->old_type
= gnu_old_desig_type
;
4011 p
->full_type
= gnat_desig_equiv
;
4019 case E_Access_Protected_Subprogram_Type
:
4020 case E_Anonymous_Access_Protected_Subprogram_Type
:
4021 if (type_annotate_only
&& No (gnat_equiv_type
))
4022 gnu_type
= ptr_void_type_node
;
4025 /* The run-time representation is the equivalent type. */
4026 gnu_type
= gnat_to_gnu_type (gnat_equiv_type
);
4027 maybe_present
= true;
4030 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
4031 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
4032 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
)))
4033 && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity
))))
4034 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
4039 case E_Access_Subtype
:
4041 /* We treat this as identical to its base type; any constraint is
4042 meaningful only to the front-end.
4044 The designated type must be elaborated as well, if it does
4045 not have its own freeze node. Designated (sub)types created
4046 for constrained components of records with discriminants are
4047 not frozen by the front-end and thus not elaborated by gigi,
4048 because their use may appear before the base type is frozen,
4049 and because it is not clear that they are needed anywhere in
4050 gigi. With the current model, there is no correct place where
4051 they could be elaborated. */
4053 gnu_type
= gnat_to_gnu_type (Etype (gnat_entity
));
4054 if (Is_Itype (Directly_Designated_Type (gnat_entity
))
4055 && !present_gnu_tree (Directly_Designated_Type (gnat_entity
))
4056 && Is_Frozen (Directly_Designated_Type (gnat_entity
))
4057 && No (Freeze_Node (Directly_Designated_Type (gnat_entity
))))
4059 /* If we are not defining this entity, and we have incomplete
4060 entities being processed above us, make a dummy type and
4061 elaborate it later. */
4062 if (!definition
&& defer_incomplete_level
!= 0)
4064 struct incomplete
*p
= XNEW (struct incomplete
);
4067 = make_dummy_type (Directly_Designated_Type (gnat_entity
));
4068 p
->full_type
= Directly_Designated_Type (gnat_entity
);
4069 p
->next
= defer_incomplete_list
;
4070 defer_incomplete_list
= p
;
4072 else if (!IN (Ekind (Base_Type
4073 (Directly_Designated_Type (gnat_entity
))),
4074 Incomplete_Or_Private_Kind
))
4075 gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity
),
4079 maybe_present
= true;
4082 /* Subprogram Entities
4084 The following access functions are defined for subprograms:
4086 Etype Return type or Standard_Void_Type.
4087 First_Formal The first formal parameter.
4088 Is_Imported Indicates that the subprogram has appeared in
4089 an INTERFACE or IMPORT pragma. For now we
4090 assume that the external language is C.
4091 Is_Exported Likewise but for an EXPORT pragma.
4092 Is_Inlined True if the subprogram is to be inlined.
4094 Each parameter is first checked by calling must_pass_by_ref on its
4095 type to determine if it is passed by reference. For parameters which
4096 are copied in, if they are Ada In Out or Out parameters, their return
4097 value becomes part of a record which becomes the return type of the
4098 function (C function - note that this applies only to Ada procedures
4099 so there is no Ada return type). Additional code to store back the
4100 parameters will be generated on the caller side. This transformation
4101 is done here, not in the front-end.
4103 The intended result of the transformation can be seen from the
4104 equivalent source rewritings that follow:
4106 struct temp {int a,b};
4107 procedure P (A,B: In Out ...) is temp P (int A,B)
4110 end P; return {A,B};
4117 For subprogram types we need to perform mainly the same conversions to
4118 GCC form that are needed for procedures and function declarations. The
4119 only difference is that at the end, we make a type declaration instead
4120 of a function declaration. */
4122 case E_Subprogram_Type
:
4126 /* The type returned by a function or else Standard_Void_Type for a
4128 Entity_Id gnat_return_type
= Etype (gnat_entity
);
4129 tree gnu_return_type
;
4130 /* The first GCC parameter declaration (a PARM_DECL node). The
4131 PARM_DECL nodes are chained through the DECL_CHAIN field, so this
4132 actually is the head of this parameter list. */
4133 tree gnu_param_list
= NULL_TREE
;
4134 /* Likewise for the stub associated with an exported procedure. */
4135 tree gnu_stub_param_list
= NULL_TREE
;
4136 /* Non-null for subprograms containing parameters passed by copy-in
4137 copy-out (Ada In Out or Out parameters not passed by reference),
4138 in which case it is the list of nodes used to specify the values
4139 of the In Out/Out parameters that are returned as a record upon
4140 procedure return. The TREE_PURPOSE of an element of this list is
4141 a field of the record and the TREE_VALUE is the PARM_DECL
4142 corresponding to that field. This list will be saved in the
4143 TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create. */
4144 tree gnu_cico_list
= NULL_TREE
;
4145 /* List of fields in return type of procedure with copy-in copy-out
4147 tree gnu_field_list
= NULL_TREE
;
4148 /* If an import pragma asks to map this subprogram to a GCC builtin,
4149 this is the builtin DECL node. */
4150 tree gnu_builtin_decl
= NULL_TREE
;
4151 /* For the stub associated with an exported procedure. */
4152 tree gnu_stub_type
= NULL_TREE
, gnu_stub_name
= NULL_TREE
;
4153 tree gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
4154 Entity_Id gnat_param
;
4155 enum inline_status_t inline_status
4156 = Has_Pragma_No_Inline (gnat_entity
)
4158 : (Is_Inlined (gnat_entity
) ? is_enabled
: is_disabled
);
4159 bool public_flag
= Is_Public (gnat_entity
) || imported_p
;
4161 = (Is_Public (gnat_entity
) && !definition
) || imported_p
;
4162 bool artificial_flag
= !Comes_From_Source (gnat_entity
);
4163 /* The semantics of "pure" in Ada essentially matches that of "const"
4164 in the back-end. In particular, both properties are orthogonal to
4165 the "nothrow" property if the EH circuitry is explicit in the
4166 internal representation of the back-end. If we are to completely
4167 hide the EH circuitry from it, we need to declare that calls to pure
4168 Ada subprograms that can throw have side effects since they can
4169 trigger an "abnormal" transfer of control flow; thus they can be
4170 neither "const" nor "pure" in the back-end sense. */
4172 = (Exception_Mechanism
== Back_End_Exceptions
4173 && Is_Pure (gnat_entity
));
4174 bool volatile_flag
= No_Return (gnat_entity
);
4175 bool return_by_direct_ref_p
= false;
4176 bool return_by_invisi_ref_p
= false;
4177 bool return_unconstrained_p
= false;
4178 bool has_stub
= false;
4181 /* A parameter may refer to this type, so defer completion of any
4182 incomplete types. */
4183 if (kind
== E_Subprogram_Type
&& !definition
)
4185 defer_incomplete_level
++;
4186 this_deferred
= true;
4189 /* If the subprogram has an alias, it is probably inherited, so
4190 we can use the original one. If the original "subprogram"
4191 is actually an enumeration literal, it may be the first use
4192 of its type, so we must elaborate that type now. */
4193 if (Present (Alias (gnat_entity
)))
4195 if (Ekind (Alias (gnat_entity
)) == E_Enumeration_Literal
)
4196 gnat_to_gnu_entity (Etype (Alias (gnat_entity
)), NULL_TREE
, 0);
4198 gnu_decl
= gnat_to_gnu_entity (Alias (gnat_entity
), gnu_expr
, 0);
4200 /* Elaborate any Itypes in the parameters of this entity. */
4201 for (gnat_temp
= First_Formal_With_Extras (gnat_entity
);
4202 Present (gnat_temp
);
4203 gnat_temp
= Next_Formal_With_Extras (gnat_temp
))
4204 if (Is_Itype (Etype (gnat_temp
)))
4205 gnat_to_gnu_entity (Etype (gnat_temp
), NULL_TREE
, 0);
4210 /* If this subprogram is expectedly bound to a GCC builtin, fetch the
4211 corresponding DECL node. Proper generation of calls later on need
4212 proper parameter associations so we don't "break;" here. */
4213 if (Convention (gnat_entity
) == Convention_Intrinsic
4214 && Present (Interface_Name (gnat_entity
)))
4216 gnu_builtin_decl
= builtin_decl_for (gnu_ext_name
);
4218 /* Inability to find the builtin decl most often indicates a
4219 genuine mistake, but imports of unregistered intrinsics are
4220 sometimes issued on purpose to allow hooking in alternate
4221 bodies. We post a warning conditioned on Wshadow in this case,
4222 to let developers be notified on demand without risking false
4223 positives with common default sets of options. */
4225 if (gnu_builtin_decl
== NULL_TREE
&& warn_shadow
)
4226 post_error ("?gcc intrinsic not found for&!", gnat_entity
);
4229 /* ??? What if we don't find the builtin node above ? warn ? err ?
4230 In the current state we neither warn nor err, and calls will just
4231 be handled as for regular subprograms. */
4233 /* Look into the return type and get its associated GCC tree. If it
4234 is not void, compute various flags for the subprogram type. */
4235 if (Ekind (gnat_return_type
) == E_Void
)
4236 gnu_return_type
= void_type_node
;
4239 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4240 context may now appear in parameter and result profiles. If
4241 we are only annotating types, break circularities here. */
4242 if (type_annotate_only
4243 && IN (Ekind (gnat_return_type
), Incomplete_Kind
)
4244 && From_Limited_With (gnat_return_type
)
4245 && In_Extended_Main_Code_Unit
4246 (Non_Limited_View (gnat_return_type
))
4247 && !present_gnu_tree (Non_Limited_View (gnat_return_type
)))
4248 gnu_return_type
= ptr_void_type_node
;
4250 gnu_return_type
= gnat_to_gnu_type (gnat_return_type
);
4252 /* If this function returns by reference, make the actual return
4253 type the pointer type and make a note of that. */
4254 if (Returns_By_Ref (gnat_entity
))
4256 gnu_return_type
= build_pointer_type (gnu_return_type
);
4257 return_by_direct_ref_p
= true;
4260 /* If we are supposed to return an unconstrained array type, make
4261 the actual return type the fat pointer type. */
4262 else if (TREE_CODE (gnu_return_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4264 gnu_return_type
= TREE_TYPE (gnu_return_type
);
4265 return_unconstrained_p
= true;
4268 /* Likewise, if the return type requires a transient scope, the
4269 return value will be allocated on the secondary stack so the
4270 actual return type is the pointer type. */
4271 else if (Requires_Transient_Scope (gnat_return_type
))
4273 gnu_return_type
= build_pointer_type (gnu_return_type
);
4274 return_unconstrained_p
= true;
4277 /* If the Mechanism is By_Reference, ensure this function uses the
4278 target's by-invisible-reference mechanism, which may not be the
4279 same as above (e.g. it might be passing an extra parameter). */
4280 else if (kind
== E_Function
4281 && Mechanism (gnat_entity
) == By_Reference
)
4282 return_by_invisi_ref_p
= true;
4284 /* Likewise, if the return type is itself By_Reference. */
4285 else if (TYPE_IS_BY_REFERENCE_P (gnu_return_type
))
4286 return_by_invisi_ref_p
= true;
4288 /* If the type is a padded type and the underlying type would not
4289 be passed by reference or the function has a foreign convention,
4290 return the underlying type. */
4291 else if (TYPE_IS_PADDING_P (gnu_return_type
)
4292 && (!default_pass_by_ref
4293 (TREE_TYPE (TYPE_FIELDS (gnu_return_type
)))
4294 || Has_Foreign_Convention (gnat_entity
)))
4295 gnu_return_type
= TREE_TYPE (TYPE_FIELDS (gnu_return_type
));
4297 /* If the return type is unconstrained, that means it must have a
4298 maximum size. Use the padded type as the effective return type.
4299 And ensure the function uses the target's by-invisible-reference
4300 mechanism to avoid copying too much data when it returns. */
4301 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_return_type
)))
4303 tree orig_type
= gnu_return_type
;
4306 = maybe_pad_type (gnu_return_type
,
4307 max_size (TYPE_SIZE (gnu_return_type
),
4309 0, gnat_entity
, false, false, false, true);
4311 /* Declare it now since it will never be declared otherwise.
4312 This is necessary to ensure that its subtrees are properly
4314 if (gnu_return_type
!= orig_type
4315 && !DECL_P (TYPE_NAME (gnu_return_type
)))
4316 create_type_decl (TYPE_NAME (gnu_return_type
),
4317 gnu_return_type
, true, debug_info_p
,
4320 return_by_invisi_ref_p
= true;
4323 /* If the return type has a size that overflows, we cannot have
4324 a function that returns that type. This usage doesn't make
4325 sense anyway, so give an error here. */
4326 if (TYPE_SIZE_UNIT (gnu_return_type
)
4327 && TREE_CODE (TYPE_SIZE_UNIT (gnu_return_type
)) == INTEGER_CST
4328 && !valid_constant_size_p (TYPE_SIZE_UNIT (gnu_return_type
)))
4330 post_error ("cannot return type whose size overflows",
4332 gnu_return_type
= copy_node (gnu_return_type
);
4333 TYPE_SIZE (gnu_return_type
) = bitsize_zero_node
;
4334 TYPE_SIZE_UNIT (gnu_return_type
) = size_zero_node
;
4335 TYPE_MAIN_VARIANT (gnu_return_type
) = gnu_return_type
;
4336 TYPE_NEXT_VARIANT (gnu_return_type
) = NULL_TREE
;
4340 /* Loop over the parameters and get their associated GCC tree. While
4341 doing this, build a copy-in copy-out structure if we need one. */
4342 for (gnat_param
= First_Formal_With_Extras (gnat_entity
), parmnum
= 0;
4343 Present (gnat_param
);
4344 gnat_param
= Next_Formal_With_Extras (gnat_param
), parmnum
++)
4346 Entity_Id gnat_param_type
= Etype (gnat_param
);
4347 tree gnu_param_name
= get_entity_name (gnat_param
);
4348 tree gnu_param_type
, gnu_param
, gnu_field
;
4349 Mechanism_Type mech
= Mechanism (gnat_param
);
4350 bool copy_in_copy_out
= false, fake_param_type
;
4352 /* Ada 2012 (AI05-0151): Incomplete types coming from a limited
4353 context may now appear in parameter and result profiles. If
4354 we are only annotating types, break circularities here. */
4355 if (type_annotate_only
4356 && IN (Ekind (gnat_param_type
), Incomplete_Kind
)
4357 && From_Limited_With (Etype (gnat_param_type
))
4358 && In_Extended_Main_Code_Unit
4359 (Non_Limited_View (gnat_param_type
))
4360 && !present_gnu_tree (Non_Limited_View (gnat_param_type
)))
4362 gnu_param_type
= ptr_void_type_node
;
4363 fake_param_type
= true;
4367 gnu_param_type
= gnat_to_gnu_type (gnat_param_type
);
4368 fake_param_type
= false;
4371 /* Builtins are expanded inline and there is no real call sequence
4372 involved. So the type expected by the underlying expander is
4373 always the type of each argument "as is". */
4374 if (gnu_builtin_decl
)
4376 /* Handle the first parameter of a valued procedure specially. */
4377 else if (Is_Valued_Procedure (gnat_entity
) && parmnum
== 0)
4378 mech
= By_Copy_Return
;
4379 /* Otherwise, see if a Mechanism was supplied that forced this
4380 parameter to be passed one way or another. */
4381 else if (mech
== Default
4382 || mech
== By_Copy
|| mech
== By_Reference
)
4384 else if (By_Descriptor_Last
<= mech
&& mech
<= By_Descriptor
)
4385 mech
= By_Descriptor
;
4387 else if (By_Short_Descriptor_Last
<= mech
&&
4388 mech
<= By_Short_Descriptor
)
4389 mech
= By_Short_Descriptor
;
4393 if (TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
4394 || TREE_CODE (TYPE_SIZE (gnu_param_type
)) != INTEGER_CST
4395 || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type
),
4397 mech
= By_Reference
;
4403 post_error ("unsupported mechanism for&", gnat_param
);
4407 /* Do not call gnat_to_gnu_param for a fake parameter type since
4408 it will try to use the real type again. */
4409 if (fake_param_type
)
4411 if (Ekind (gnat_param
) == E_Out_Parameter
)
4412 gnu_param
= NULL_TREE
;
4416 = create_param_decl (gnu_param_name
, gnu_param_type
,
4418 Set_Mechanism (gnat_param
,
4419 mech
== Default
? By_Copy
: mech
);
4420 if (Ekind (gnat_param
) == E_In_Out_Parameter
)
4421 copy_in_copy_out
= true;
4426 = gnat_to_gnu_param (gnat_param
, mech
, gnat_entity
,
4427 Has_Foreign_Convention (gnat_entity
),
4430 /* We are returned either a PARM_DECL or a type if no parameter
4431 needs to be passed; in either case, adjust the type. */
4432 if (DECL_P (gnu_param
))
4433 gnu_param_type
= TREE_TYPE (gnu_param
);
4436 gnu_param_type
= gnu_param
;
4437 gnu_param
= NULL_TREE
;
4440 /* The failure of this assertion will very likely come from an
4441 order of elaboration issue for the type of the parameter. */
4442 gcc_assert (kind
== E_Subprogram_Type
4443 || !TYPE_IS_DUMMY_P (gnu_param_type
)
4444 || type_annotate_only
);
4448 /* If it's an exported subprogram, we build a parameter list
4449 in parallel, in case we need to emit a stub for it. */
4450 if (Is_Exported (gnat_entity
))
4453 = chainon (gnu_param
, gnu_stub_param_list
);
4454 /* Change By_Descriptor parameter to By_Reference for
4455 the internal version of an exported subprogram. */
4456 if (mech
== By_Descriptor
|| mech
== By_Short_Descriptor
)
4459 = gnat_to_gnu_param (gnat_param
, By_Reference
,
4465 gnu_param
= copy_node (gnu_param
);
4468 gnu_param_list
= chainon (gnu_param
, gnu_param_list
);
4469 Sloc_to_locus (Sloc (gnat_param
),
4470 &DECL_SOURCE_LOCATION (gnu_param
));
4471 save_gnu_tree (gnat_param
, gnu_param
, false);
4473 /* If a parameter is a pointer, this function may modify
4474 memory through it and thus shouldn't be considered
4475 a const function. Also, the memory may be modified
4476 between two calls, so they can't be CSE'ed. The latter
4477 case also handles by-ref parameters. */
4478 if (POINTER_TYPE_P (gnu_param_type
)
4479 || TYPE_IS_FAT_POINTER_P (gnu_param_type
))
4483 if (copy_in_copy_out
)
4487 tree gnu_new_ret_type
= make_node (RECORD_TYPE
);
4489 /* If this is a function, we also need a field for the
4490 return value to be placed. */
4491 if (TREE_CODE (gnu_return_type
) != VOID_TYPE
)
4494 = create_field_decl (get_identifier ("RETVAL"),
4496 gnu_new_ret_type
, NULL_TREE
,
4498 Sloc_to_locus (Sloc (gnat_entity
),
4499 &DECL_SOURCE_LOCATION (gnu_field
));
4500 gnu_field_list
= gnu_field
;
4502 = tree_cons (gnu_field
, void_type_node
, NULL_TREE
);
4505 gnu_return_type
= gnu_new_ret_type
;
4506 TYPE_NAME (gnu_return_type
) = get_identifier ("RETURN");
4507 /* Set a default alignment to speed up accesses. But we
4508 shouldn't increase the size of the structure too much,
4509 lest it doesn't fit in return registers anymore. */
4510 TYPE_ALIGN (gnu_return_type
)
4511 = get_mode_alignment (ptr_mode
);
4515 = create_field_decl (gnu_param_name
, gnu_param_type
,
4516 gnu_return_type
, NULL_TREE
, NULL_TREE
,
4518 Sloc_to_locus (Sloc (gnat_param
),
4519 &DECL_SOURCE_LOCATION (gnu_field
));
4520 DECL_CHAIN (gnu_field
) = gnu_field_list
;
4521 gnu_field_list
= gnu_field
;
4523 = tree_cons (gnu_field
, gnu_param
, gnu_cico_list
);
4529 /* If we have a CICO list but it has only one entry, we convert
4530 this function into a function that returns this object. */
4531 if (list_length (gnu_cico_list
) == 1)
4532 gnu_return_type
= TREE_TYPE (TREE_PURPOSE (gnu_cico_list
));
4534 /* Do not finalize the return type if the subprogram is stubbed
4535 since structures are incomplete for the back-end. */
4536 else if (Convention (gnat_entity
) != Convention_Stubbed
)
4538 finish_record_type (gnu_return_type
, nreverse (gnu_field_list
),
4541 /* Try to promote the mode of the return type if it is passed
4542 in registers, again to speed up accesses. */
4543 if (TYPE_MODE (gnu_return_type
) == BLKmode
4544 && !targetm
.calls
.return_in_memory (gnu_return_type
,
4548 = TREE_INT_CST_LOW (TYPE_SIZE (gnu_return_type
));
4549 unsigned int i
= BITS_PER_UNIT
;
4550 enum machine_mode mode
;
4554 mode
= mode_for_size (i
, MODE_INT
, 0);
4555 if (mode
!= BLKmode
)
4557 SET_TYPE_MODE (gnu_return_type
, mode
);
4558 TYPE_ALIGN (gnu_return_type
)
4559 = GET_MODE_ALIGNMENT (mode
);
4560 TYPE_SIZE (gnu_return_type
)
4561 = bitsize_int (GET_MODE_BITSIZE (mode
));
4562 TYPE_SIZE_UNIT (gnu_return_type
)
4563 = size_int (GET_MODE_SIZE (mode
));
4568 rest_of_record_type_compilation (gnu_return_type
);
4572 /* Deal with platform-specific calling conventions. */
4573 if (Has_Stdcall_Convention (gnat_entity
))
4574 prepend_one_attribute
4575 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4576 get_identifier ("stdcall"), NULL_TREE
,
4578 else if (Has_Thiscall_Convention (gnat_entity
))
4579 prepend_one_attribute
4580 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4581 get_identifier ("thiscall"), NULL_TREE
,
4584 /* If we should request stack realignment for a foreign convention
4585 subprogram, do so. Note that this applies to task entry points
4587 if (FOREIGN_FORCE_REALIGN_STACK
4588 && Has_Foreign_Convention (gnat_entity
))
4589 prepend_one_attribute
4590 (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
4591 get_identifier ("force_align_arg_pointer"), NULL_TREE
,
4594 /* Deal with a pragma Linker_Section on a subprogram. */
4595 if ((kind
== E_Function
|| kind
== E_Procedure
)
4596 && Present (Linker_Section_Pragma (gnat_entity
)))
4597 prepend_one_attribute_pragma (&attr_list
,
4598 Linker_Section_Pragma (gnat_entity
));
4600 /* The lists have been built in reverse. */
4601 gnu_param_list
= nreverse (gnu_param_list
);
4603 gnu_stub_param_list
= nreverse (gnu_stub_param_list
);
4604 gnu_cico_list
= nreverse (gnu_cico_list
);
4606 if (kind
== E_Function
)
4607 Set_Mechanism (gnat_entity
, return_unconstrained_p
4608 || return_by_direct_ref_p
4609 || return_by_invisi_ref_p
4610 ? By_Reference
: By_Copy
);
4612 = create_subprog_type (gnu_return_type
, gnu_param_list
,
4613 gnu_cico_list
, return_unconstrained_p
,
4614 return_by_direct_ref_p
,
4615 return_by_invisi_ref_p
);
4619 = create_subprog_type (gnu_return_type
, gnu_stub_param_list
,
4620 gnu_cico_list
, return_unconstrained_p
,
4621 return_by_direct_ref_p
,
4622 return_by_invisi_ref_p
);
4624 /* A subprogram (something that doesn't return anything) shouldn't
4625 be considered const since there would be no reason for such a
4626 subprogram. Note that procedures with Out (or In Out) parameters
4627 have already been converted into a function with a return type. */
4628 if (TREE_CODE (gnu_return_type
) == VOID_TYPE
)
4632 = build_qualified_type (gnu_type
,
4633 TYPE_QUALS (gnu_type
)
4634 | (TYPE_QUAL_CONST
* const_flag
)
4635 | (TYPE_QUAL_VOLATILE
* volatile_flag
));
4639 = build_qualified_type (gnu_stub_type
,
4640 TYPE_QUALS (gnu_stub_type
)
4641 | (TYPE_QUAL_CONST
* const_flag
)
4642 | (TYPE_QUAL_VOLATILE
* volatile_flag
));
4644 /* If we have a builtin decl for that function, use it. Check if the
4645 profiles are compatible and warn if they are not. The checker is
4646 expected to post extra diagnostics in this case. */
4647 if (gnu_builtin_decl
)
4649 intrin_binding_t inb
;
4651 inb
.gnat_entity
= gnat_entity
;
4652 inb
.ada_fntype
= gnu_type
;
4653 inb
.btin_fntype
= TREE_TYPE (gnu_builtin_decl
);
4655 if (!intrin_profiles_compatible_p (&inb
))
4657 ("?profile of& doesn''t match the builtin it binds!",
4660 gnu_decl
= gnu_builtin_decl
;
4661 gnu_type
= TREE_TYPE (gnu_builtin_decl
);
4665 /* If there was no specified Interface_Name and the external and
4666 internal names of the subprogram are the same, only use the
4667 internal name to allow disambiguation of nested subprograms. */
4668 if (No (Interface_Name (gnat_entity
))
4669 && gnu_ext_name
== gnu_entity_name
)
4670 gnu_ext_name
= NULL_TREE
;
4672 /* If we are defining the subprogram and it has an Address clause
4673 we must get the address expression from the saved GCC tree for the
4674 subprogram if it has a Freeze_Node. Otherwise, we elaborate
4675 the address expression here since the front-end has guaranteed
4676 in that case that the elaboration has no effects. If there is
4677 an Address clause and we are not defining the object, just
4678 make it a constant. */
4679 if (Present (Address_Clause (gnat_entity
)))
4681 tree gnu_address
= NULL_TREE
;
4685 = (present_gnu_tree (gnat_entity
)
4686 ? get_gnu_tree (gnat_entity
)
4687 : gnat_to_gnu (Expression (Address_Clause (gnat_entity
))));
4689 save_gnu_tree (gnat_entity
, NULL_TREE
, false);
4691 /* Convert the type of the object to a reference type that can
4692 alias everything as per 13.3(19). */
4694 = build_reference_type_for_mode (gnu_type
, ptr_mode
, true);
4696 gnu_address
= convert (gnu_type
, gnu_address
);
4699 = create_var_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4700 gnu_address
, false, Is_Public (gnat_entity
),
4701 extern_flag
, false, NULL
, gnat_entity
);
4702 DECL_BY_REF_P (gnu_decl
) = 1;
4705 else if (kind
== E_Subprogram_Type
)
4707 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
4709 = create_type_decl (gnu_entity_name
, gnu_type
, artificial_flag
,
4710 debug_info_p
, gnat_entity
);
4716 gnu_stub_name
= gnu_ext_name
;
4717 gnu_ext_name
= create_concat_name (gnat_entity
, "internal");
4718 public_flag
= false;
4719 artificial_flag
= true;
4723 = create_subprog_decl (gnu_entity_name
, gnu_ext_name
, gnu_type
,
4724 gnu_param_list
, inline_status
,
4725 public_flag
, extern_flag
, artificial_flag
,
4726 attr_list
, gnat_entity
);
4730 = create_subprog_decl (gnu_entity_name
, gnu_stub_name
,
4731 gnu_stub_type
, gnu_stub_param_list
,
4732 inline_status
, true, extern_flag
,
4733 false, attr_list
, gnat_entity
);
4734 SET_DECL_FUNCTION_STUB (gnu_decl
, gnu_stub_decl
);
4737 /* This is unrelated to the stub built right above. */
4738 DECL_STUBBED_P (gnu_decl
)
4739 = Convention (gnat_entity
) == Convention_Stubbed
;
4744 case E_Incomplete_Type
:
4745 case E_Incomplete_Subtype
:
4746 case E_Private_Type
:
4747 case E_Private_Subtype
:
4748 case E_Limited_Private_Type
:
4749 case E_Limited_Private_Subtype
:
4750 case E_Record_Type_With_Private
:
4751 case E_Record_Subtype_With_Private
:
4753 /* Get the "full view" of this entity. If this is an incomplete
4754 entity from a limited with, treat its non-limited view as the
4755 full view. Otherwise, use either the full view or the underlying
4756 full view, whichever is present. This is used in all the tests
4759 = (IN (kind
, Incomplete_Kind
) && From_Limited_With (gnat_entity
))
4760 ? Non_Limited_View (gnat_entity
)
4761 : Present (Full_View (gnat_entity
))
4762 ? Full_View (gnat_entity
)
4763 : Underlying_Full_View (gnat_entity
);
4765 /* If this is an incomplete type with no full view, it must be a Taft
4766 Amendment type, in which case we return a dummy type. Otherwise,
4767 just get the type from its Etype. */
4770 if (kind
== E_Incomplete_Type
)
4772 gnu_type
= make_dummy_type (gnat_entity
);
4773 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4777 gnu_decl
= gnat_to_gnu_entity (Etype (gnat_entity
),
4779 maybe_present
= true;
4784 /* If we already made a type for the full view, reuse it. */
4785 else if (present_gnu_tree (full_view
))
4787 gnu_decl
= get_gnu_tree (full_view
);
4791 /* Otherwise, if we are not defining the type now, get the type
4792 from the full view. But always get the type from the full view
4793 for define on use types, since otherwise we won't see them! */
4794 else if (!definition
4795 || (Is_Itype (full_view
) && No (Freeze_Node (gnat_entity
)))
4796 || (Is_Itype (gnat_entity
) && No (Freeze_Node (full_view
))))
4798 gnu_decl
= gnat_to_gnu_entity (full_view
, NULL_TREE
, 0);
4799 maybe_present
= true;
4803 /* For incomplete types, make a dummy type entry which will be
4804 replaced later. Save it as the full declaration's type so
4805 we can do any needed updates when we see it. */
4806 gnu_type
= make_dummy_type (gnat_entity
);
4807 gnu_decl
= TYPE_STUB_DECL (gnu_type
);
4808 if (Has_Completion_In_Body (gnat_entity
))
4809 DECL_TAFT_TYPE_P (gnu_decl
) = 1;
4810 save_gnu_tree (full_view
, gnu_decl
, 0);
4814 case E_Class_Wide_Type
:
4815 /* Class-wide types are always transformed into their root type. */
4816 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
4817 maybe_present
= true;
4821 case E_Task_Subtype
:
4822 case E_Protected_Type
:
4823 case E_Protected_Subtype
:
4824 /* Concurrent types are always transformed into their record type. */
4825 if (type_annotate_only
&& No (gnat_equiv_type
))
4826 gnu_type
= void_type_node
;
4828 gnu_decl
= gnat_to_gnu_entity (gnat_equiv_type
, NULL_TREE
, 0);
4829 maybe_present
= true;
4833 gnu_decl
= create_label_decl (gnu_entity_name
, gnat_entity
);
4838 /* Nothing at all to do here, so just return an ERROR_MARK and claim
4839 we've already saved it, so we don't try to. */
4840 gnu_decl
= error_mark_node
;
4844 case E_Abstract_State
:
4845 /* This is a SPARK annotation that only reaches here when compiling in
4846 ASIS mode and has no characteristics to annotate. */
4847 gcc_assert (type_annotate_only
);
4848 return error_mark_node
;
4854 /* If we had a case where we evaluated another type and it might have
4855 defined this one, handle it here. */
4856 if (maybe_present
&& present_gnu_tree (gnat_entity
))
4858 gnu_decl
= get_gnu_tree (gnat_entity
);
4862 /* If we are processing a type and there is either no decl for it or
4863 we just made one, do some common processing for the type, such as
4864 handling alignment and possible padding. */
4865 if (is_type
&& (!gnu_decl
|| this_made_decl
))
4867 /* Process the attributes, if not already done. Note that the type is
4868 already defined so we cannot pass true for IN_PLACE here. */
4869 process_attributes (&gnu_type
, &attr_list
, false, gnat_entity
);
4871 /* Tell the middle-end that objects of tagged types are guaranteed to
4872 be properly aligned. This is necessary because conversions to the
4873 class-wide type are translated into conversions to the root type,
4874 which can be less aligned than some of its derived types. */
4875 if (Is_Tagged_Type (gnat_entity
)
4876 || Is_Class_Wide_Equivalent_Type (gnat_entity
))
4877 TYPE_ALIGN_OK (gnu_type
) = 1;
4879 /* Record whether the type is passed by reference. */
4880 if (!VOID_TYPE_P (gnu_type
) && Is_By_Reference_Type (gnat_entity
))
4881 TYPE_BY_REFERENCE_P (gnu_type
) = 1;
4883 /* ??? Don't set the size for a String_Literal since it is either
4884 confirming or we don't handle it properly (if the low bound is
4886 if (!gnu_size
&& kind
!= E_String_Literal_Subtype
)
4888 Uint gnat_size
= Known_Esize (gnat_entity
)
4889 ? Esize (gnat_entity
) : RM_Size (gnat_entity
);
4891 = validate_size (gnat_size
, gnu_type
, gnat_entity
, TYPE_DECL
,
4892 false, Has_Size_Clause (gnat_entity
));
4895 /* If a size was specified, see if we can make a new type of that size
4896 by rearranging the type, for example from a fat to a thin pointer. */
4900 = make_type_from_size (gnu_type
, gnu_size
,
4901 Has_Biased_Representation (gnat_entity
));
4903 if (operand_equal_p (TYPE_SIZE (gnu_type
), gnu_size
, 0)
4904 && operand_equal_p (rm_size (gnu_type
), gnu_size
, 0))
4905 gnu_size
= NULL_TREE
;
4908 /* If the alignment hasn't already been processed and this is
4909 not an unconstrained array, see if an alignment is specified.
4910 If not, we pick a default alignment for atomic objects. */
4911 if (align
!= 0 || TREE_CODE (gnu_type
) == UNCONSTRAINED_ARRAY_TYPE
)
4913 else if (Known_Alignment (gnat_entity
))
4915 align
= validate_alignment (Alignment (gnat_entity
), gnat_entity
,
4916 TYPE_ALIGN (gnu_type
));
4918 /* Warn on suspiciously large alignments. This should catch
4919 errors about the (alignment,byte)/(size,bit) discrepancy. */
4920 if (align
> BIGGEST_ALIGNMENT
&& Has_Alignment_Clause (gnat_entity
))
4924 /* If a size was specified, take it into account. Otherwise
4925 use the RM size for records or unions as the type size has
4926 already been adjusted to the alignment. */
4929 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
4930 && !TYPE_FAT_POINTER_P (gnu_type
))
4931 size
= rm_size (gnu_type
);
4933 size
= TYPE_SIZE (gnu_type
);
4935 /* Consider an alignment as suspicious if the alignment/size
4936 ratio is greater or equal to the byte/bit ratio. */
4937 if (tree_fits_uhwi_p (size
)
4938 && align
>= tree_to_uhwi (size
) * BITS_PER_UNIT
)
4939 post_error_ne ("?suspiciously large alignment specified for&",
4940 Expression (Alignment_Clause (gnat_entity
)),
4944 else if (Is_Atomic (gnat_entity
) && !gnu_size
4945 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type
))
4946 && integer_pow2p (TYPE_SIZE (gnu_type
)))
4947 align
= MIN (BIGGEST_ALIGNMENT
,
4948 tree_to_uhwi (TYPE_SIZE (gnu_type
)));
4949 else if (Is_Atomic (gnat_entity
) && gnu_size
4950 && tree_fits_uhwi_p (gnu_size
)
4951 && integer_pow2p (gnu_size
))
4952 align
= MIN (BIGGEST_ALIGNMENT
, tree_to_uhwi (gnu_size
));
4954 /* See if we need to pad the type. If we did, and made a record,
4955 the name of the new type may be changed. So get it back for
4956 us when we make the new TYPE_DECL below. */
4957 if (gnu_size
|| align
> 0)
4958 gnu_type
= maybe_pad_type (gnu_type
, gnu_size
, align
, gnat_entity
,
4959 false, !gnu_decl
, definition
, false);
4961 if (TYPE_IS_PADDING_P (gnu_type
))
4962 gnu_entity_name
= TYPE_IDENTIFIER (gnu_type
);
4964 /* Now set the RM size of the type. We cannot do it before padding
4965 because we need to accept arbitrary RM sizes on integral types. */
4966 set_rm_size (RM_Size (gnat_entity
), gnu_type
, gnat_entity
);
4968 /* If we are at global level, GCC will have applied variable_size to
4969 the type, but that won't have done anything. So, if it's not
4970 a constant or self-referential, call elaborate_expression_1 to
4971 make a variable for the size rather than calculating it each time.
4972 Handle both the RM size and the actual size. */
4973 if (global_bindings_p ()
4974 && TYPE_SIZE (gnu_type
)
4975 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
))
4976 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
4978 tree size
= TYPE_SIZE (gnu_type
);
4980 TYPE_SIZE (gnu_type
)
4981 = elaborate_expression_1 (size
, gnat_entity
,
4982 get_identifier ("SIZE"),
4985 /* ??? For now, store the size as a multiple of the alignment in
4986 bytes so that we can see the alignment from the tree. */
4987 TYPE_SIZE_UNIT (gnu_type
)
4988 = elaborate_expression_2 (TYPE_SIZE_UNIT (gnu_type
), gnat_entity
,
4989 get_identifier ("SIZE_A_UNIT"),
4991 TYPE_ALIGN (gnu_type
));
4993 /* ??? gnu_type may come from an existing type so the MULT_EXPR node
4994 may not be marked by the call to create_type_decl below. */
4995 MARK_VISITED (TYPE_SIZE_UNIT (gnu_type
));
4997 if (TREE_CODE (gnu_type
) == RECORD_TYPE
)
4999 tree variant_part
= get_variant_part (gnu_type
);
5000 tree ada_size
= TYPE_ADA_SIZE (gnu_type
);
5004 tree union_type
= TREE_TYPE (variant_part
);
5005 tree offset
= DECL_FIELD_OFFSET (variant_part
);
5007 /* If the position of the variant part is constant, subtract
5008 it from the size of the type of the parent to get the new
5009 size. This manual CSE reduces the data size. */
5010 if (TREE_CODE (offset
) == INTEGER_CST
)
5012 tree bitpos
= DECL_FIELD_BIT_OFFSET (variant_part
);
5013 TYPE_SIZE (union_type
)
5014 = size_binop (MINUS_EXPR
, TYPE_SIZE (gnu_type
),
5015 bit_from_pos (offset
, bitpos
));
5016 TYPE_SIZE_UNIT (union_type
)
5017 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (gnu_type
),
5018 byte_from_pos (offset
, bitpos
));
5022 TYPE_SIZE (union_type
)
5023 = elaborate_expression_1 (TYPE_SIZE (union_type
),
5025 get_identifier ("VSIZE"),
5028 /* ??? For now, store the size as a multiple of the
5029 alignment in bytes so that we can see the alignment
5031 TYPE_SIZE_UNIT (union_type
)
5032 = elaborate_expression_2 (TYPE_SIZE_UNIT (union_type
),
5037 TYPE_ALIGN (union_type
));
5039 /* ??? For now, store the offset as a multiple of the
5040 alignment in bytes so that we can see the alignment
5042 DECL_FIELD_OFFSET (variant_part
)
5043 = elaborate_expression_2 (offset
,
5045 get_identifier ("VOFFSET"),
5051 DECL_SIZE (variant_part
) = TYPE_SIZE (union_type
);
5052 DECL_SIZE_UNIT (variant_part
) = TYPE_SIZE_UNIT (union_type
);
5055 if (operand_equal_p (ada_size
, size
, 0))
5056 ada_size
= TYPE_SIZE (gnu_type
);
5059 = elaborate_expression_1 (ada_size
, gnat_entity
,
5060 get_identifier ("RM_SIZE"),
5062 SET_TYPE_ADA_SIZE (gnu_type
, ada_size
);
5066 /* If this is a record type or subtype, call elaborate_expression_2 on
5067 any field position. Do this for both global and local types.
5068 Skip any fields that we haven't made trees for to avoid problems with
5069 class wide types. */
5070 if (IN (kind
, Record_Kind
))
5071 for (gnat_temp
= First_Entity (gnat_entity
); Present (gnat_temp
);
5072 gnat_temp
= Next_Entity (gnat_temp
))
5073 if (Ekind (gnat_temp
) == E_Component
&& present_gnu_tree (gnat_temp
))
5075 tree gnu_field
= get_gnu_tree (gnat_temp
);
5077 /* ??? For now, store the offset as a multiple of the alignment
5078 in bytes so that we can see the alignment from the tree. */
5079 if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field
)))
5081 DECL_FIELD_OFFSET (gnu_field
)
5082 = elaborate_expression_2 (DECL_FIELD_OFFSET (gnu_field
),
5084 get_identifier ("OFFSET"),
5086 DECL_OFFSET_ALIGN (gnu_field
));
5088 /* ??? The context of gnu_field is not necessarily gnu_type
5089 so the MULT_EXPR node built above may not be marked by
5090 the call to create_type_decl below. */
5091 if (global_bindings_p ())
5092 MARK_VISITED (DECL_FIELD_OFFSET (gnu_field
));
5096 if (Treat_As_Volatile (gnat_entity
))
5098 = build_qualified_type (gnu_type
,
5099 TYPE_QUALS (gnu_type
) | TYPE_QUAL_VOLATILE
);
5101 if (Is_Atomic (gnat_entity
))
5102 check_ok_for_atomic (gnu_type
, gnat_entity
, false);
5104 if (Present (Alignment_Clause (gnat_entity
)))
5105 TYPE_USER_ALIGN (gnu_type
) = 1;
5107 if (Universal_Aliasing (gnat_entity
))
5108 TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type
)) = 1;
5111 gnu_decl
= create_type_decl (gnu_entity_name
, gnu_type
,
5112 !Comes_From_Source (gnat_entity
),
5113 debug_info_p
, gnat_entity
);
5116 TREE_TYPE (gnu_decl
) = gnu_type
;
5117 TYPE_STUB_DECL (gnu_type
) = gnu_decl
;
5121 if (is_type
&& !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl
)))
5123 gnu_type
= TREE_TYPE (gnu_decl
);
5125 /* If this is a derived type, relate its alias set to that of its parent
5126 to avoid troubles when a call to an inherited primitive is inlined in
5127 a context where a derived object is accessed. The inlined code works
5128 on the parent view so the resulting code may access the same object
5129 using both the parent and the derived alias sets, which thus have to
5130 conflict. As the same issue arises with component references, the
5131 parent alias set also has to conflict with composite types enclosing
5132 derived components. For instance, if we have:
5139 we want T to conflict with both D and R, in addition to R being a
5140 superset of D by record/component construction.
5142 One way to achieve this is to perform an alias set copy from the
5143 parent to the derived type. This is not quite appropriate, though,
5144 as we don't want separate derived types to conflict with each other:
5146 type I1 is new Integer;
5147 type I2 is new Integer;
5149 We want I1 and I2 to both conflict with Integer but we do not want
5150 I1 to conflict with I2, and an alias set copy on derivation would
5153 The option chosen is to make the alias set of the derived type a
5154 superset of that of its parent type. It trivially fulfills the
5155 simple requirement for the Integer derivation example above, and
5156 the component case as well by superset transitivity:
5159 R ----------> D ----------> T
5161 However, for composite types, conversions between derived types are
5162 translated into VIEW_CONVERT_EXPRs so a sequence like:
5164 type Comp1 is new Comp;
5165 type Comp2 is new Comp;
5166 procedure Proc (C : Comp1);
5174 Proc ((Comp1 &) &VIEW_CONVERT_EXPR <Comp1> (C));
5176 and gimplified into:
5183 i.e. generates code involving type punning. Therefore, Comp1 needs
5184 to conflict with Comp2 and an alias set copy is required.
5186 The language rules ensure the parent type is already frozen here. */
5187 if (Is_Derived_Type (gnat_entity
) && !type_annotate_only
)
5189 tree gnu_parent_type
= gnat_to_gnu_type (Etype (gnat_entity
));
5190 relate_alias_sets (gnu_type
, gnu_parent_type
,
5191 Is_Composite_Type (gnat_entity
)
5192 ? ALIAS_SET_COPY
: ALIAS_SET_SUPERSET
);
5195 /* Back-annotate the Alignment of the type if not already in the
5196 tree. Likewise for sizes. */
5197 if (Unknown_Alignment (gnat_entity
))
5199 unsigned int double_align
, align
;
5200 bool is_capped_double
, align_clause
;
5202 /* If the default alignment of "double" or larger scalar types is
5203 specifically capped and this is not an array with an alignment
5204 clause on the component type, return the cap. */
5205 if ((double_align
= double_float_alignment
) > 0)
5207 = is_double_float_or_array (gnat_entity
, &align_clause
);
5208 else if ((double_align
= double_scalar_alignment
) > 0)
5210 = is_double_scalar_or_array (gnat_entity
, &align_clause
);
5212 is_capped_double
= align_clause
= false;
5214 if (is_capped_double
&& !align_clause
)
5215 align
= double_align
;
5217 align
= TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
;
5219 Set_Alignment (gnat_entity
, UI_From_Int (align
));
5222 if (Unknown_Esize (gnat_entity
) && TYPE_SIZE (gnu_type
))
5224 tree gnu_size
= TYPE_SIZE (gnu_type
);
5226 /* If the size is self-referential, annotate the maximum value. */
5227 if (CONTAINS_PLACEHOLDER_P (gnu_size
))
5228 gnu_size
= max_size (gnu_size
, true);
5230 /* If we are just annotating types and the type is tagged, the tag
5231 and the parent components are not generated by the front-end so
5232 sizes must be adjusted if there is no representation clause. */
5233 if (type_annotate_only
5234 && Is_Tagged_Type (gnat_entity
)
5235 && !VOID_TYPE_P (gnu_type
)
5236 && (!TYPE_FIELDS (gnu_type
)
5237 || integer_zerop (bit_position (TYPE_FIELDS (gnu_type
)))))
5239 tree pointer_size
= bitsize_int (POINTER_SIZE
), offset
;
5242 if (Is_Derived_Type (gnat_entity
))
5244 Entity_Id gnat_parent
= Etype (Base_Type (gnat_entity
));
5245 offset
= UI_To_gnu (Esize (gnat_parent
), bitsizetype
);
5246 Set_Alignment (gnat_entity
, Alignment (gnat_parent
));
5249 offset
= pointer_size
;
5251 if (TYPE_FIELDS (gnu_type
))
5253 = round_up (offset
, DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
5255 gnu_size
= size_binop (PLUS_EXPR
, gnu_size
, offset
);
5256 gnu_size
= round_up (gnu_size
, POINTER_SIZE
);
5257 uint_size
= annotate_value (gnu_size
);
5258 Set_Esize (gnat_entity
, uint_size
);
5259 Set_RM_Size (gnat_entity
, uint_size
);
5262 Set_Esize (gnat_entity
, annotate_value (gnu_size
));
5265 if (Unknown_RM_Size (gnat_entity
) && rm_size (gnu_type
))
5266 Set_RM_Size (gnat_entity
, annotate_value (rm_size (gnu_type
)));
5269 /* If we really have a ..._DECL node, set a couple of flags on it. But we
5270 cannot do so if we are reusing the ..._DECL node made for an equivalent
5271 type or an alias or a renamed object as the predicates don't apply to it
5272 but to GNAT_ENTITY. */
5273 if (DECL_P (gnu_decl
)
5274 && !(is_type
&& gnat_equiv_type
!= gnat_entity
)
5275 && !Present (Alias (gnat_entity
))
5276 && !(Present (Renamed_Object (gnat_entity
)) && saved
))
5278 if (!Comes_From_Source (gnat_entity
))
5279 DECL_ARTIFICIAL (gnu_decl
) = 1;
5282 DECL_IGNORED_P (gnu_decl
) = 1;
5285 /* If we haven't already, associate the ..._DECL node that we just made with
5286 the input GNAT entity node. */
5288 save_gnu_tree (gnat_entity
, gnu_decl
, false);
5290 /* If this is an enumeration or floating-point type, we were not able to set
5291 the bounds since they refer to the type. These are always static. */
5292 if ((kind
== E_Enumeration_Type
&& Present (First_Literal (gnat_entity
)))
5293 || (kind
== E_Floating_Point_Type
&& !Vax_Float (gnat_entity
)))
5295 tree gnu_scalar_type
= gnu_type
;
5296 tree gnu_low_bound
, gnu_high_bound
;
5298 /* If this is a padded type, we need to use the underlying type. */
5299 if (TYPE_IS_PADDING_P (gnu_scalar_type
))
5300 gnu_scalar_type
= TREE_TYPE (TYPE_FIELDS (gnu_scalar_type
));
5302 /* If this is a floating point type and we haven't set a floating
5303 point type yet, use this in the evaluation of the bounds. */
5304 if (!longest_float_type_node
&& kind
== E_Floating_Point_Type
)
5305 longest_float_type_node
= gnu_scalar_type
;
5307 gnu_low_bound
= gnat_to_gnu (Type_Low_Bound (gnat_entity
));
5308 gnu_high_bound
= gnat_to_gnu (Type_High_Bound (gnat_entity
));
5310 if (kind
== E_Enumeration_Type
)
5312 /* Enumeration types have specific RM bounds. */
5313 SET_TYPE_RM_MIN_VALUE (gnu_scalar_type
, gnu_low_bound
);
5314 SET_TYPE_RM_MAX_VALUE (gnu_scalar_type
, gnu_high_bound
);
5318 /* Floating-point types don't have specific RM bounds. */
5319 TYPE_GCC_MIN_VALUE (gnu_scalar_type
) = gnu_low_bound
;
5320 TYPE_GCC_MAX_VALUE (gnu_scalar_type
) = gnu_high_bound
;
5324 /* If we deferred processing of incomplete types, re-enable it. If there
5325 were no other disables and we have deferred types to process, do so. */
5327 && --defer_incomplete_level
== 0
5328 && defer_incomplete_list
)
5330 struct incomplete
*p
, *next
;
5332 /* We are back to level 0 for the deferring of incomplete types.
5333 But processing these incomplete types below may itself require
5334 deferring, so preserve what we have and restart from scratch. */
5335 p
= defer_incomplete_list
;
5336 defer_incomplete_list
= NULL
;
5343 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5344 gnat_to_gnu_type (p
->full_type
));
5349 /* If we are not defining this type, see if it's on one of the lists of
5350 incomplete types. If so, handle the list entry now. */
5351 if (is_type
&& !definition
)
5353 struct incomplete
*p
;
5355 for (p
= defer_incomplete_list
; p
; p
= p
->next
)
5356 if (p
->old_type
&& p
->full_type
== gnat_entity
)
5358 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5359 TREE_TYPE (gnu_decl
));
5360 p
->old_type
= NULL_TREE
;
5363 for (p
= defer_limited_with
; p
; p
= p
->next
)
5364 if (p
->old_type
&& Non_Limited_View (p
->full_type
) == gnat_entity
)
5366 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5367 TREE_TYPE (gnu_decl
));
5368 p
->old_type
= NULL_TREE
;
5375 /* If this is a packed array type whose original array type is itself
5376 an Itype without freeze node, make sure the latter is processed. */
5377 if (Is_Packed_Array_Type (gnat_entity
)
5378 && Is_Itype (Original_Array_Type (gnat_entity
))
5379 && No (Freeze_Node (Original_Array_Type (gnat_entity
)))
5380 && !present_gnu_tree (Original_Array_Type (gnat_entity
)))
5381 gnat_to_gnu_entity (Original_Array_Type (gnat_entity
), NULL_TREE
, 0);
5386 /* Similar, but if the returned value is a COMPONENT_REF, return the
5390 gnat_to_gnu_field_decl (Entity_Id gnat_entity
)
5392 tree gnu_field
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
5394 if (TREE_CODE (gnu_field
) == COMPONENT_REF
)
5395 gnu_field
= TREE_OPERAND (gnu_field
, 1);
5400 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5401 the GCC type corresponding to that entity. */
5404 gnat_to_gnu_type (Entity_Id gnat_entity
)
5408 /* The back end never attempts to annotate generic types. */
5409 if (Is_Generic_Type (gnat_entity
) && type_annotate_only
)
5410 return void_type_node
;
5412 gnu_decl
= gnat_to_gnu_entity (gnat_entity
, NULL_TREE
, 0);
5413 gcc_assert (TREE_CODE (gnu_decl
) == TYPE_DECL
);
5415 return TREE_TYPE (gnu_decl
);
5418 /* Similar, but GNAT_ENTITY is assumed to refer to a GNAT type. Return
5419 the unpadded version of the GCC type corresponding to that entity. */
5422 get_unpadded_type (Entity_Id gnat_entity
)
5424 tree type
= gnat_to_gnu_type (gnat_entity
);
5426 if (TYPE_IS_PADDING_P (type
))
5427 type
= TREE_TYPE (TYPE_FIELDS (type
));
5432 /* Return the DECL associated with the public subprogram GNAT_ENTITY but whose
5433 type has been changed to that of the parameterless procedure, except if an
5434 alias is already present, in which case it is returned instead. */
5437 get_minimal_subprog_decl (Entity_Id gnat_entity
)
5439 tree gnu_entity_name
, gnu_ext_name
;
5440 struct attrib
*attr_list
= NULL
;
5442 /* See the E_Function/E_Procedure case of gnat_to_gnu_entity for the model
5443 of the handling applied here. */
5445 while (Present (Alias (gnat_entity
)))
5447 gnat_entity
= Alias (gnat_entity
);
5448 if (present_gnu_tree (gnat_entity
))
5449 return get_gnu_tree (gnat_entity
);
5452 gnu_entity_name
= get_entity_name (gnat_entity
);
5453 gnu_ext_name
= create_concat_name (gnat_entity
, NULL
);
5455 if (Has_Stdcall_Convention (gnat_entity
))
5456 prepend_one_attribute (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
5457 get_identifier ("stdcall"), NULL_TREE
,
5459 else if (Has_Thiscall_Convention (gnat_entity
))
5460 prepend_one_attribute (&attr_list
, ATTR_MACHINE_ATTRIBUTE
,
5461 get_identifier ("thiscall"), NULL_TREE
,
5464 if (No (Interface_Name (gnat_entity
)) && gnu_ext_name
== gnu_entity_name
)
5465 gnu_ext_name
= NULL_TREE
;
5468 create_subprog_decl (gnu_entity_name
, gnu_ext_name
, void_ftype
, NULL_TREE
,
5469 is_disabled
, true, true, true, attr_list
, gnat_entity
);
5472 /* Return whether the E_Subprogram_Type/E_Function/E_Procedure GNAT_ENTITY is
5473 a C++ imported method or equivalent.
5475 We use the predicate on 32-bit x86/Windows to find out whether we need to
5476 use the "thiscall" calling convention for GNAT_ENTITY. This convention is
5477 used for C++ methods (functions with METHOD_TYPE) by the back-end. */
5480 is_cplusplus_method (Entity_Id gnat_entity
)
5482 if (Convention (gnat_entity
) != Convention_CPP
)
5485 /* This is the main case: C++ method imported as a primitive operation. */
5486 if (Is_Dispatching_Operation (gnat_entity
))
5489 /* A thunk needs to be handled like its associated primitive operation. */
5490 if (Is_Subprogram (gnat_entity
) && Is_Thunk (gnat_entity
))
5493 /* C++ classes with no virtual functions can be imported as limited
5494 record types, but we need to return true for the constructors. */
5495 if (Is_Constructor (gnat_entity
))
5498 /* This is set on the E_Subprogram_Type built for a dispatching call. */
5499 if (Is_Dispatch_Table_Entity (gnat_entity
))
5505 /* Finalize the processing of From_Limited_With incomplete types. */
5508 finalize_from_limited_with (void)
5510 struct incomplete
*p
, *next
;
5512 p
= defer_limited_with
;
5513 defer_limited_with
= NULL
;
5520 update_pointer_to (TYPE_MAIN_VARIANT (p
->old_type
),
5521 gnat_to_gnu_type (p
->full_type
));
5526 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
5527 kind of type (such E_Task_Type) that has a different type which Gigi
5528 uses for its representation. If the type does not have a special type
5529 for its representation, return GNAT_ENTITY. If a type is supposed to
5530 exist, but does not, abort unless annotating types, in which case
5531 return Empty. If GNAT_ENTITY is Empty, return Empty. */
5534 Gigi_Equivalent_Type (Entity_Id gnat_entity
)
5536 Entity_Id gnat_equiv
= gnat_entity
;
5538 if (No (gnat_entity
))
5541 switch (Ekind (gnat_entity
))
5543 case E_Class_Wide_Subtype
:
5544 if (Present (Equivalent_Type (gnat_entity
)))
5545 gnat_equiv
= Equivalent_Type (gnat_entity
);
5548 case E_Access_Protected_Subprogram_Type
:
5549 case E_Anonymous_Access_Protected_Subprogram_Type
:
5550 gnat_equiv
= Equivalent_Type (gnat_entity
);
5553 case E_Class_Wide_Type
:
5554 gnat_equiv
= Root_Type (gnat_entity
);
5558 case E_Task_Subtype
:
5559 case E_Protected_Type
:
5560 case E_Protected_Subtype
:
5561 gnat_equiv
= Corresponding_Record_Type (gnat_entity
);
5568 gcc_assert (Present (gnat_equiv
) || type_annotate_only
);
5573 /* Return a GCC tree for a type corresponding to the component type of the
5574 array type or subtype GNAT_ARRAY. DEFINITION is true if this component
5575 is for an array being defined. DEBUG_INFO_P is true if we need to write
5576 debug information for other types that we may create in the process. */
5579 gnat_to_gnu_component_type (Entity_Id gnat_array
, bool definition
,
5582 const Entity_Id gnat_type
= Component_Type (gnat_array
);
5583 tree gnu_type
= gnat_to_gnu_type (gnat_type
);
5586 /* Try to get a smaller form of the component if needed. */
5587 if ((Is_Packed (gnat_array
)
5588 || Has_Component_Size_Clause (gnat_array
))
5589 && !Is_Bit_Packed_Array (gnat_array
)
5590 && !Has_Aliased_Components (gnat_array
)
5591 && !Strict_Alignment (gnat_type
)
5592 && RECORD_OR_UNION_TYPE_P (gnu_type
)
5593 && !TYPE_FAT_POINTER_P (gnu_type
)
5594 && tree_fits_uhwi_p (TYPE_SIZE (gnu_type
)))
5595 gnu_type
= make_packable_type (gnu_type
, false);
5597 if (Has_Atomic_Components (gnat_array
))
5598 check_ok_for_atomic (gnu_type
, gnat_array
, true);
5600 /* Get and validate any specified Component_Size. */
5602 = validate_size (Component_Size (gnat_array
), gnu_type
, gnat_array
,
5603 Is_Bit_Packed_Array (gnat_array
) ? TYPE_DECL
: VAR_DECL
,
5604 true, Has_Component_Size_Clause (gnat_array
));
5606 /* If the array has aliased components and the component size can be zero,
5607 force at least unit size to ensure that the components have distinct
5610 && Has_Aliased_Components (gnat_array
)
5611 && (integer_zerop (TYPE_SIZE (gnu_type
))
5612 || (TREE_CODE (gnu_type
) == ARRAY_TYPE
5613 && !TREE_CONSTANT (TYPE_SIZE (gnu_type
)))))
5615 = size_binop (MAX_EXPR
, TYPE_SIZE (gnu_type
), bitsize_unit_node
);
5617 /* If the component type is a RECORD_TYPE that has a self-referential size,
5618 then use the maximum size for the component size. */
5620 && TREE_CODE (gnu_type
) == RECORD_TYPE
5621 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type
)))
5622 gnu_comp_size
= max_size (TYPE_SIZE (gnu_type
), true);
5624 /* Honor the component size. This is not needed for bit-packed arrays. */
5625 if (gnu_comp_size
&& !Is_Bit_Packed_Array (gnat_array
))
5627 tree orig_type
= gnu_type
;
5628 unsigned int max_align
;
5630 /* If an alignment is specified, use it as a cap on the component type
5631 so that it can be honored for the whole type. But ignore it for the
5632 original type of packed array types. */
5633 if (No (Packed_Array_Type (gnat_array
)) && Known_Alignment (gnat_array
))
5634 max_align
= validate_alignment (Alignment (gnat_array
), gnat_array
, 0);
5638 gnu_type
= make_type_from_size (gnu_type
, gnu_comp_size
, false);
5639 if (max_align
> 0 && TYPE_ALIGN (gnu_type
) > max_align
)
5640 gnu_type
= orig_type
;
5642 orig_type
= gnu_type
;
5644 gnu_type
= maybe_pad_type (gnu_type
, gnu_comp_size
, 0, gnat_array
,
5645 true, false, definition
, true);
5647 /* If a padding record was made, declare it now since it will never be
5648 declared otherwise. This is necessary to ensure that its subtrees
5649 are properly marked. */
5650 if (gnu_type
!= orig_type
&& !DECL_P (TYPE_NAME (gnu_type
)))
5651 create_type_decl (TYPE_NAME (gnu_type
), gnu_type
, true, debug_info_p
,
5655 if (Has_Volatile_Components (gnat_array
))
5657 = build_qualified_type (gnu_type
,
5658 TYPE_QUALS (gnu_type
) | TYPE_QUAL_VOLATILE
);
5663 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
5664 using MECH as its passing mechanism, to be placed in the parameter
5665 list built for GNAT_SUBPROG. Assume a foreign convention for the
5666 latter if FOREIGN is true. Also set CICO to true if the parameter
5667 must use the copy-in copy-out implementation mechanism.
5669 The returned tree is a PARM_DECL, except for those cases where no
5670 parameter needs to be actually passed to the subprogram; the type
5671 of this "shadow" parameter is then returned instead. */
5674 gnat_to_gnu_param (Entity_Id gnat_param
, Mechanism_Type mech
,
5675 Entity_Id gnat_subprog
, bool foreign
, bool *cico
)
5677 tree gnu_param_name
= get_entity_name (gnat_param
);
5678 tree gnu_param_type
= gnat_to_gnu_type (Etype (gnat_param
));
5679 tree gnu_param_type_alt
= NULL_TREE
;
5680 bool in_param
= (Ekind (gnat_param
) == E_In_Parameter
);
5681 /* The parameter can be indirectly modified if its address is taken. */
5682 bool ro_param
= in_param
&& !Address_Taken (gnat_param
);
5683 bool by_return
= false, by_component_ptr
= false;
5684 bool by_ref
= false;
5687 /* Copy-return is used only for the first parameter of a valued procedure.
5688 It's a copy mechanism for which a parameter is never allocated. */
5689 if (mech
== By_Copy_Return
)
5691 gcc_assert (Ekind (gnat_param
) == E_Out_Parameter
);
5696 /* If this is either a foreign function or if the underlying type won't
5697 be passed by reference, strip off possible padding type. */
5698 if (TYPE_IS_PADDING_P (gnu_param_type
))
5700 tree unpadded_type
= TREE_TYPE (TYPE_FIELDS (gnu_param_type
));
5702 if (mech
== By_Reference
5704 || (!must_pass_by_ref (unpadded_type
)
5705 && (mech
== By_Copy
|| !default_pass_by_ref (unpadded_type
))))
5706 gnu_param_type
= unpadded_type
;
5709 /* If this is a read-only parameter, make a variant of the type that is
5710 read-only. ??? However, if this is an unconstrained array, that type
5711 can be very complex, so skip it for now. Likewise for any other
5712 self-referential type. */
5714 && TREE_CODE (gnu_param_type
) != UNCONSTRAINED_ARRAY_TYPE
5715 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type
)))
5716 gnu_param_type
= build_qualified_type (gnu_param_type
,
5717 (TYPE_QUALS (gnu_param_type
)
5718 | TYPE_QUAL_CONST
));
5720 /* For foreign conventions, pass arrays as pointers to the element type.
5721 First check for unconstrained array and get the underlying array. */
5722 if (foreign
&& TREE_CODE (gnu_param_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5724 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type
))));
5726 /* For GCC builtins, pass Address integer types as (void *) */
5727 if (Convention (gnat_subprog
) == Convention_Intrinsic
5728 && Present (Interface_Name (gnat_subprog
))
5729 && Is_Descendent_Of_Address (Etype (gnat_param
)))
5730 gnu_param_type
= ptr_void_type_node
;
5732 /* VMS descriptors are themselves passed by reference. */
5733 if (mech
== By_Short_Descriptor
||
5734 (mech
== By_Descriptor
&& TARGET_ABI_OPEN_VMS
&& !flag_vms_malloc64
))
5736 = build_pointer_type (build_vms_descriptor32 (gnu_param_type
,
5737 Mechanism (gnat_param
),
5739 else if (mech
== By_Descriptor
)
5741 /* Build both a 32-bit and 64-bit descriptor, one of which will be
5742 chosen in fill_vms_descriptor. */
5744 = build_pointer_type (build_vms_descriptor32 (gnu_param_type
,
5745 Mechanism (gnat_param
),
5748 = build_pointer_type (build_vms_descriptor (gnu_param_type
,
5749 Mechanism (gnat_param
),
5753 /* Arrays are passed as pointers to element type for foreign conventions. */
5756 && TREE_CODE (gnu_param_type
) == ARRAY_TYPE
)
5758 /* Strip off any multi-dimensional entries, then strip
5759 off the last array to get the component type. */
5760 while (TREE_CODE (TREE_TYPE (gnu_param_type
)) == ARRAY_TYPE
5761 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type
)))
5762 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5764 by_component_ptr
= true;
5765 gnu_param_type
= TREE_TYPE (gnu_param_type
);
5768 gnu_param_type
= build_qualified_type (gnu_param_type
,
5769 (TYPE_QUALS (gnu_param_type
)
5770 | TYPE_QUAL_CONST
));
5772 gnu_param_type
= build_pointer_type (gnu_param_type
);
5775 /* Fat pointers are passed as thin pointers for foreign conventions. */
5776 else if (foreign
&& TYPE_IS_FAT_POINTER_P (gnu_param_type
))
5778 = make_type_from_size (gnu_param_type
, size_int (POINTER_SIZE
), 0);
5780 /* If we must pass or were requested to pass by reference, do so.
5781 If we were requested to pass by copy, do so.
5782 Otherwise, for foreign conventions, pass In Out or Out parameters
5783 or aggregates by reference. For COBOL and Fortran, pass all
5784 integer and FP types that way too. For Convention Ada, use
5785 the standard Ada default. */
5786 else if (must_pass_by_ref (gnu_param_type
)
5787 || mech
== By_Reference
5790 && (!in_param
|| AGGREGATE_TYPE_P (gnu_param_type
)))
5792 && (Convention (gnat_subprog
) == Convention_Fortran
5793 || Convention (gnat_subprog
) == Convention_COBOL
)
5794 && (INTEGRAL_TYPE_P (gnu_param_type
)
5795 || FLOAT_TYPE_P (gnu_param_type
)))
5797 && default_pass_by_ref (gnu_param_type
)))))
5799 /* We take advantage of 6.2(12) by considering that references built for
5800 parameters whose type isn't by-ref and for which the mechanism hasn't
5801 been forced to by-ref are restrict-qualified in the C sense. */
5803 = !TYPE_IS_BY_REFERENCE_P (gnu_param_type
) && mech
!= By_Reference
;
5804 gnu_param_type
= build_reference_type (gnu_param_type
);
5807 = build_qualified_type (gnu_param_type
, TYPE_QUAL_RESTRICT
);
5811 /* Pass In Out or Out parameters using copy-in copy-out mechanism. */
5815 if (mech
== By_Copy
&& (by_ref
|| by_component_ptr
))
5816 post_error ("?cannot pass & by copy", gnat_param
);
5818 /* If this is an Out parameter that isn't passed by reference and isn't
5819 a pointer or aggregate, we don't make a PARM_DECL for it. Instead,
5820 it will be a VAR_DECL created when we process the procedure, so just
5821 return its type. For the special parameter of a valued procedure,
5824 An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5825 Out parameters with discriminants or implicit initial values to be
5826 handled like In Out parameters. These type are normally built as
5827 aggregates, hence passed by reference, except for some packed arrays
5828 which end up encoded in special integer types. Note that scalars can
5829 be given implicit initial values using the Default_Value aspect.
5831 The exception we need to make is then for packed arrays of records
5832 with discriminants or implicit initial values. We have no light/easy
5833 way to check for the latter case, so we merely check for packed arrays
5834 of records. This may lead to useless copy-in operations, but in very
5835 rare cases only, as these would be exceptions in a set of already
5836 exceptional situations. */
5837 if (Ekind (gnat_param
) == E_Out_Parameter
5840 || (mech
!= By_Descriptor
5841 && mech
!= By_Short_Descriptor
5842 && !POINTER_TYPE_P (gnu_param_type
)
5843 && !AGGREGATE_TYPE_P (gnu_param_type
)
5844 && !Has_Default_Aspect (Etype (gnat_param
))))
5845 && !(Is_Array_Type (Etype (gnat_param
))
5846 && Is_Packed (Etype (gnat_param
))
5847 && Is_Composite_Type (Component_Type (Etype (gnat_param
)))))
5848 return gnu_param_type
;
5850 gnu_param
= create_param_decl (gnu_param_name
, gnu_param_type
,
5851 ro_param
|| by_ref
|| by_component_ptr
);
5852 DECL_BY_REF_P (gnu_param
) = by_ref
;
5853 DECL_BY_COMPONENT_PTR_P (gnu_param
) = by_component_ptr
;
5854 DECL_BY_DESCRIPTOR_P (gnu_param
)
5855 = (mech
== By_Descriptor
|| mech
== By_Short_Descriptor
);
5856 DECL_POINTS_TO_READONLY_P (gnu_param
)
5857 = (ro_param
&& (by_ref
|| by_component_ptr
));
5858 DECL_CAN_NEVER_BE_NULL_P (gnu_param
) = Can_Never_Be_Null (gnat_param
);
5860 /* Save the alternate descriptor type, if any. */
5861 if (gnu_param_type_alt
)
5862 SET_DECL_PARM_ALT_TYPE (gnu_param
, gnu_param_type_alt
);
5864 /* If no Mechanism was specified, indicate what we're using, then
5865 back-annotate it. */
5866 if (mech
== Default
)
5867 mech
= (by_ref
|| by_component_ptr
) ? By_Reference
: By_Copy
;
5869 Set_Mechanism (gnat_param
, mech
);
5873 /* Return true if DISCR1 and DISCR2 represent the same discriminant. */
5876 same_discriminant_p (Entity_Id discr1
, Entity_Id discr2
)
5878 while (Present (Corresponding_Discriminant (discr1
)))
5879 discr1
= Corresponding_Discriminant (discr1
);
5881 while (Present (Corresponding_Discriminant (discr2
)))
5882 discr2
= Corresponding_Discriminant (discr2
);
5885 Original_Record_Component (discr1
) == Original_Record_Component (discr2
);
5888 /* Return true if the array type GNU_TYPE, which represents a dimension of
5889 GNAT_TYPE, has a non-aliased component in the back-end sense. */
5892 array_type_has_nonaliased_component (tree gnu_type
, Entity_Id gnat_type
)
5894 /* If the array type is not the innermost dimension of the GNAT type,
5895 then it has a non-aliased component. */
5896 if (TREE_CODE (TREE_TYPE (gnu_type
)) == ARRAY_TYPE
5897 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type
)))
5900 /* If the array type has an aliased component in the front-end sense,
5901 then it also has an aliased component in the back-end sense. */
5902 if (Has_Aliased_Components (gnat_type
))
5905 /* If this is a derived type, then it has a non-aliased component if
5906 and only if its parent type also has one. */
5907 if (Is_Derived_Type (gnat_type
))
5909 tree gnu_parent_type
= gnat_to_gnu_type (Etype (gnat_type
));
5911 if (TREE_CODE (gnu_parent_type
) == UNCONSTRAINED_ARRAY_TYPE
)
5913 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type
))));
5914 for (index
= Number_Dimensions (gnat_type
) - 1; index
> 0; index
--)
5915 gnu_parent_type
= TREE_TYPE (gnu_parent_type
);
5916 return TYPE_NONALIASED_COMPONENT (gnu_parent_type
);
5919 /* Otherwise, rely exclusively on properties of the element type. */
5920 return type_for_nonaliased_component_p (TREE_TYPE (gnu_type
));
5923 /* Return true if GNAT_ADDRESS is a value known at compile-time. */
5926 compile_time_known_address_p (Node_Id gnat_address
)
5928 /* Catch System'To_Address. */
5929 if (Nkind (gnat_address
) == N_Unchecked_Type_Conversion
)
5930 gnat_address
= Expression (gnat_address
);
5932 return Compile_Time_Known_Value (gnat_address
);
5935 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5936 inequality HB >= LB-1 is true. LB and HB are the low and high bounds. */
5939 cannot_be_superflat_p (Node_Id gnat_range
)
5941 Node_Id gnat_lb
= Low_Bound (gnat_range
), gnat_hb
= High_Bound (gnat_range
);
5942 Node_Id scalar_range
;
5943 tree gnu_lb
, gnu_hb
, gnu_lb_minus_one
;
5945 /* If the low bound is not constant, try to find an upper bound. */
5946 while (Nkind (gnat_lb
) != N_Integer_Literal
5947 && (Ekind (Etype (gnat_lb
)) == E_Signed_Integer_Subtype
5948 || Ekind (Etype (gnat_lb
)) == E_Modular_Integer_Subtype
)
5949 && (scalar_range
= Scalar_Range (Etype (gnat_lb
)))
5950 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
5951 || Nkind (scalar_range
) == N_Range
))
5952 gnat_lb
= High_Bound (scalar_range
);
5954 /* If the high bound is not constant, try to find a lower bound. */
5955 while (Nkind (gnat_hb
) != N_Integer_Literal
5956 && (Ekind (Etype (gnat_hb
)) == E_Signed_Integer_Subtype
5957 || Ekind (Etype (gnat_hb
)) == E_Modular_Integer_Subtype
)
5958 && (scalar_range
= Scalar_Range (Etype (gnat_hb
)))
5959 && (Nkind (scalar_range
) == N_Signed_Integer_Type_Definition
5960 || Nkind (scalar_range
) == N_Range
))
5961 gnat_hb
= Low_Bound (scalar_range
);
5963 /* If we have failed to find constant bounds, punt. */
5964 if (Nkind (gnat_lb
) != N_Integer_Literal
5965 || Nkind (gnat_hb
) != N_Integer_Literal
)
5968 /* We need at least a signed 64-bit type to catch most cases. */
5969 gnu_lb
= UI_To_gnu (Intval (gnat_lb
), sbitsizetype
);
5970 gnu_hb
= UI_To_gnu (Intval (gnat_hb
), sbitsizetype
);
5971 if (TREE_OVERFLOW (gnu_lb
) || TREE_OVERFLOW (gnu_hb
))
5974 /* If the low bound is the smallest integer, nothing can be smaller. */
5975 gnu_lb_minus_one
= size_binop (MINUS_EXPR
, gnu_lb
, sbitsize_one_node
);
5976 if (TREE_OVERFLOW (gnu_lb_minus_one
))
5979 return !tree_int_cst_lt (gnu_hb
, gnu_lb_minus_one
);
5982 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR. */
5985 constructor_address_p (tree gnu_expr
)
5987 while (TREE_CODE (gnu_expr
) == NOP_EXPR
5988 || TREE_CODE (gnu_expr
) == CONVERT_EXPR
5989 || TREE_CODE (gnu_expr
) == NON_LVALUE_EXPR
)
5990 gnu_expr
= TREE_OPERAND (gnu_expr
, 0);
5992 return (TREE_CODE (gnu_expr
) == ADDR_EXPR
5993 && TREE_CODE (TREE_OPERAND (gnu_expr
, 0)) == CONSTRUCTOR
);
5996 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5997 be elaborated at the point of its definition, but do nothing else. */
6000 elaborate_entity (Entity_Id gnat_entity
)
6002 switch (Ekind (gnat_entity
))
6004 case E_Signed_Integer_Subtype
:
6005 case E_Modular_Integer_Subtype
:
6006 case E_Enumeration_Subtype
:
6007 case E_Ordinary_Fixed_Point_Subtype
:
6008 case E_Decimal_Fixed_Point_Subtype
:
6009 case E_Floating_Point_Subtype
:
6011 Node_Id gnat_lb
= Type_Low_Bound (gnat_entity
);
6012 Node_Id gnat_hb
= Type_High_Bound (gnat_entity
);
6014 /* ??? Tests to avoid Constraint_Error in static expressions
6015 are needed until after the front stops generating bogus
6016 conversions on bounds of real types. */
6017 if (!Raises_Constraint_Error (gnat_lb
))
6018 elaborate_expression (gnat_lb
, gnat_entity
, get_identifier ("L"),
6019 true, false, Needs_Debug_Info (gnat_entity
));
6020 if (!Raises_Constraint_Error (gnat_hb
))
6021 elaborate_expression (gnat_hb
, gnat_entity
, get_identifier ("U"),
6022 true, false, Needs_Debug_Info (gnat_entity
));
6026 case E_Record_Subtype
:
6027 case E_Private_Subtype
:
6028 case E_Limited_Private_Subtype
:
6029 case E_Record_Subtype_With_Private
:
6030 if (Has_Discriminants (gnat_entity
) && Is_Constrained (gnat_entity
))
6032 Node_Id gnat_discriminant_expr
;
6033 Entity_Id gnat_field
;
6036 = First_Discriminant (Implementation_Base_Type (gnat_entity
)),
6037 gnat_discriminant_expr
6038 = First_Elmt (Discriminant_Constraint (gnat_entity
));
6039 Present (gnat_field
);
6040 gnat_field
= Next_Discriminant (gnat_field
),
6041 gnat_discriminant_expr
= Next_Elmt (gnat_discriminant_expr
))
6042 /* Ignore access discriminants. */
6043 if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr
))))
6044 elaborate_expression (Node (gnat_discriminant_expr
),
6045 gnat_entity
, get_entity_name (gnat_field
),
6046 true, false, false);
6053 /* Return true if the size in units represented by GNU_SIZE can be handled by
6054 an allocation. If STATIC_P is true, consider only what can be done with a
6055 static allocation. */
6058 allocatable_size_p (tree gnu_size
, bool static_p
)
6060 /* We can allocate a fixed size if it is a valid for the middle-end. */
6061 if (TREE_CODE (gnu_size
) == INTEGER_CST
)
6062 return valid_constant_size_p (gnu_size
);
6064 /* We can allocate a variable size if this isn't a static allocation. */
6069 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
6070 NAME, ARGS and ERROR_POINT. */
6073 prepend_one_attribute (struct attrib
**attr_list
,
6074 enum attr_type attr_type
,
6077 Node_Id attr_error_point
)
6079 struct attrib
* attr
= (struct attrib
*) xmalloc (sizeof (struct attrib
));
6081 attr
->type
= attr_type
;
6082 attr
->name
= attr_name
;
6083 attr
->args
= attr_args
;
6084 attr
->error_point
= attr_error_point
;
6086 attr
->next
= *attr_list
;
6090 /* Prepend to ATTR_LIST an entry for an attribute provided by GNAT_PRAGMA. */
6093 prepend_one_attribute_pragma (struct attrib
**attr_list
, Node_Id gnat_pragma
)
6095 const Node_Id gnat_arg
= Pragma_Argument_Associations (gnat_pragma
);
6096 tree gnu_arg0
= NULL_TREE
, gnu_arg1
= NULL_TREE
;
6097 enum attr_type etype
;
6099 /* Map the pragma at hand. Skip if this isn't one we know how to handle. */
6100 switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_pragma
))))
6102 case Pragma_Machine_Attribute
:
6103 etype
= ATTR_MACHINE_ATTRIBUTE
;
6106 case Pragma_Linker_Alias
:
6107 etype
= ATTR_LINK_ALIAS
;
6110 case Pragma_Linker_Section
:
6111 etype
= ATTR_LINK_SECTION
;
6114 case Pragma_Linker_Constructor
:
6115 etype
= ATTR_LINK_CONSTRUCTOR
;
6118 case Pragma_Linker_Destructor
:
6119 etype
= ATTR_LINK_DESTRUCTOR
;
6122 case Pragma_Weak_External
:
6123 etype
= ATTR_WEAK_EXTERNAL
;
6126 case Pragma_Thread_Local_Storage
:
6127 etype
= ATTR_THREAD_LOCAL_STORAGE
;
6134 /* See what arguments we have and turn them into GCC trees for attribute
6135 handlers. These expect identifier for strings. We handle at most two
6136 arguments and static expressions only. */
6137 if (Present (gnat_arg
) && Present (First (gnat_arg
)))
6139 Node_Id gnat_arg0
= Next (First (gnat_arg
));
6140 Node_Id gnat_arg1
= Empty
;
6142 if (Present (gnat_arg0
)
6143 && Is_OK_Static_Expression (Expression (gnat_arg0
)))
6145 gnu_arg0
= gnat_to_gnu (Expression (gnat_arg0
));
6147 if (TREE_CODE (gnu_arg0
) == STRING_CST
)
6149 gnu_arg0
= get_identifier (TREE_STRING_POINTER (gnu_arg0
));
6150 if (IDENTIFIER_LENGTH (gnu_arg0
) == 0)
6154 gnat_arg1
= Next (gnat_arg0
);
6157 if (Present (gnat_arg1
)
6158 && Is_OK_Static_Expression (Expression (gnat_arg1
)))
6160 gnu_arg1
= gnat_to_gnu (Expression (gnat_arg1
));
6162 if (TREE_CODE (gnu_arg1
) == STRING_CST
)
6163 gnu_arg1
= get_identifier (TREE_STRING_POINTER (gnu_arg1
));
6167 /* Prepend to the list. Make a list of the argument we might have, as GCC
6169 prepend_one_attribute (attr_list
, etype
, gnu_arg0
,
6171 ? build_tree_list (NULL_TREE
, gnu_arg1
) : NULL_TREE
,
6172 Present (Next (First (gnat_arg
)))
6173 ? Expression (Next (First (gnat_arg
))) : gnat_pragma
);
6176 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any. */
6179 prepend_attributes (struct attrib
**attr_list
, Entity_Id gnat_entity
)
6183 /* Attributes are stored as Representation Item pragmas. */
6184 for (gnat_temp
= First_Rep_Item (gnat_entity
);
6185 Present (gnat_temp
);
6186 gnat_temp
= Next_Rep_Item (gnat_temp
))
6187 if (Nkind (gnat_temp
) == N_Pragma
)
6188 prepend_one_attribute_pragma (attr_list
, gnat_temp
);
6191 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6192 type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6193 return the GCC tree to use for that expression. GNU_NAME is the suffix
6194 to use if a variable needs to be created and DEFINITION is true if this
6195 is a definition of GNAT_ENTITY. If NEED_VALUE is true, we need a result;
6196 otherwise, we are just elaborating the expression for side-effects. If
6197 NEED_DEBUG is true, we need a variable for debugging purposes even if it
6198 isn't needed for code generation. */
6201 elaborate_expression (Node_Id gnat_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6202 bool definition
, bool need_value
, bool need_debug
)
6206 /* If we already elaborated this expression (e.g. it was involved
6207 in the definition of a private type), use the old value. */
6208 if (present_gnu_tree (gnat_expr
))
6209 return get_gnu_tree (gnat_expr
);
6211 /* If we don't need a value and this is static or a discriminant,
6212 we don't need to do anything. */
6214 && (Is_OK_Static_Expression (gnat_expr
)
6215 || (Nkind (gnat_expr
) == N_Identifier
6216 && Ekind (Entity (gnat_expr
)) == E_Discriminant
)))
6219 /* If it's a static expression, we don't need a variable for debugging. */
6220 if (need_debug
&& Is_OK_Static_Expression (gnat_expr
))
6223 /* Otherwise, convert this tree to its GCC equivalent and elaborate it. */
6224 gnu_expr
= elaborate_expression_1 (gnat_to_gnu (gnat_expr
), gnat_entity
,
6225 gnu_name
, definition
, need_debug
);
6227 /* Save the expression in case we try to elaborate this entity again. Since
6228 it's not a DECL, don't check it. Don't save if it's a discriminant. */
6229 if (!CONTAINS_PLACEHOLDER_P (gnu_expr
))
6230 save_gnu_tree (gnat_expr
, gnu_expr
, true);
6232 return need_value
? gnu_expr
: error_mark_node
;
6235 /* Similar, but take a GNU expression and always return a result. */
6238 elaborate_expression_1 (tree gnu_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6239 bool definition
, bool need_debug
)
6241 const bool expr_public_p
= Is_Public (gnat_entity
);
6242 const bool expr_global_p
= expr_public_p
|| global_bindings_p ();
6243 bool expr_variable_p
, use_variable
;
6245 /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6246 reference will have been replaced with a COMPONENT_REF when the type
6247 is being elaborated. However, there are some cases involving child
6248 types where we will. So convert it to a COMPONENT_REF. We hope it
6249 will be at the highest level of the expression in these cases. */
6250 if (TREE_CODE (gnu_expr
) == FIELD_DECL
)
6251 gnu_expr
= build3 (COMPONENT_REF
, TREE_TYPE (gnu_expr
),
6252 build0 (PLACEHOLDER_EXPR
, DECL_CONTEXT (gnu_expr
)),
6253 gnu_expr
, NULL_TREE
);
6255 /* If GNU_EXPR contains a placeholder, just return it. We rely on the fact
6256 that an expression cannot contain both a discriminant and a variable. */
6257 if (CONTAINS_PLACEHOLDER_P (gnu_expr
))
6260 /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6261 a variable that is initialized to contain the expression when the package
6262 containing the definition is elaborated. If this entity is defined at top
6263 level, replace the expression by the variable; otherwise use a SAVE_EXPR
6264 if this is necessary. */
6265 if (CONSTANT_CLASS_P (gnu_expr
))
6266 expr_variable_p
= false;
6269 /* Skip any conversions and simple constant arithmetics to see if the
6270 expression is based on a read-only variable.
6271 ??? This really should remain read-only, but we have to think about
6272 the typing of the tree here. */
6273 tree inner
= remove_conversions (gnu_expr
, true);
6275 inner
= skip_simple_constant_arithmetic (inner
);
6277 if (handled_component_p (inner
))
6279 HOST_WIDE_INT bitsize
, bitpos
;
6281 enum machine_mode mode
;
6282 int unsignedp
, volatilep
;
6284 inner
= get_inner_reference (inner
, &bitsize
, &bitpos
, &offset
,
6285 &mode
, &unsignedp
, &volatilep
, false);
6286 /* If the offset is variable, err on the side of caution. */
6293 && TREE_CODE (inner
) == VAR_DECL
6294 && (TREE_READONLY (inner
) || DECL_READONLY_ONCE_ELAB (inner
)));
6297 /* We only need to use the variable if we are in a global context since GCC
6298 can do the right thing in the local case. However, when not optimizing,
6299 use it for bounds of loop iteration scheme to avoid code duplication. */
6300 use_variable
= expr_variable_p
6304 && Is_Itype (gnat_entity
)
6305 && Nkind (Associated_Node_For_Itype (gnat_entity
))
6306 == N_Loop_Parameter_Specification
));
6308 /* Now create it, possibly only for debugging purposes. */
6309 if (use_variable
|| need_debug
)
6313 (create_concat_name (gnat_entity
, IDENTIFIER_POINTER (gnu_name
)),
6314 NULL_TREE
, TREE_TYPE (gnu_expr
), gnu_expr
, true, expr_public_p
,
6315 !definition
, expr_global_p
, !need_debug
, NULL
, gnat_entity
);
6321 return expr_variable_p
? gnat_save_expr (gnu_expr
) : gnu_expr
;
6324 /* Similar, but take an alignment factor and make it explicit in the tree. */
6327 elaborate_expression_2 (tree gnu_expr
, Entity_Id gnat_entity
, tree gnu_name
,
6328 bool definition
, bool need_debug
, unsigned int align
)
6330 tree unit_align
= size_int (align
/ BITS_PER_UNIT
);
6332 size_binop (MULT_EXPR
,
6333 elaborate_expression_1 (size_binop (EXACT_DIV_EXPR
,
6336 gnat_entity
, gnu_name
, definition
,
6341 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6342 the value passed against the list of choices. */
6345 choices_to_gnu (tree operand
, Node_Id choices
)
6349 tree result
= boolean_false_node
;
6350 tree this_test
, low
= 0, high
= 0, single
= 0;
6352 for (choice
= First (choices
); Present (choice
); choice
= Next (choice
))
6354 switch (Nkind (choice
))
6357 low
= gnat_to_gnu (Low_Bound (choice
));
6358 high
= gnat_to_gnu (High_Bound (choice
));
6361 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6362 build_binary_op (GE_EXPR
, boolean_type_node
,
6364 build_binary_op (LE_EXPR
, boolean_type_node
,
6369 case N_Subtype_Indication
:
6370 gnat_temp
= Range_Expression (Constraint (choice
));
6371 low
= gnat_to_gnu (Low_Bound (gnat_temp
));
6372 high
= gnat_to_gnu (High_Bound (gnat_temp
));
6375 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6376 build_binary_op (GE_EXPR
, boolean_type_node
,
6378 build_binary_op (LE_EXPR
, boolean_type_node
,
6383 case N_Expanded_Name
:
6384 /* This represents either a subtype range, an enumeration
6385 literal, or a constant Ekind says which. If an enumeration
6386 literal or constant, fall through to the next case. */
6387 if (Ekind (Entity (choice
)) != E_Enumeration_Literal
6388 && Ekind (Entity (choice
)) != E_Constant
)
6390 tree type
= gnat_to_gnu_type (Entity (choice
));
6392 low
= TYPE_MIN_VALUE (type
);
6393 high
= TYPE_MAX_VALUE (type
);
6396 = build_binary_op (TRUTH_ANDIF_EXPR
, boolean_type_node
,
6397 build_binary_op (GE_EXPR
, boolean_type_node
,
6399 build_binary_op (LE_EXPR
, boolean_type_node
,
6404 /* ... fall through ... */
6406 case N_Character_Literal
:
6407 case N_Integer_Literal
:
6408 single
= gnat_to_gnu (choice
);
6409 this_test
= build_binary_op (EQ_EXPR
, boolean_type_node
, operand
,
6413 case N_Others_Choice
:
6414 this_test
= boolean_true_node
;
6421 result
= build_binary_op (TRUTH_ORIF_EXPR
, boolean_type_node
, result
,
6428 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6429 type FIELD_TYPE to be placed in RECORD_TYPE. Return the result. */
6432 adjust_packed (tree field_type
, tree record_type
, int packed
)
6434 /* If the field contains an item of variable size, we cannot pack it
6435 because we cannot create temporaries of non-fixed size in case
6436 we need to take the address of the field. See addressable_p and
6437 the notes on the addressability issues for further details. */
6438 if (type_has_variable_size (field_type
))
6441 /* If the alignment of the record is specified and the field type
6442 is over-aligned, request Storage_Unit alignment for the field. */
6445 if (TYPE_ALIGN (field_type
) > TYPE_ALIGN (record_type
))
6454 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6455 placed in GNU_RECORD_TYPE.
6457 PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6458 record has Component_Alignment of Storage_Unit, -2 if the enclosing
6459 record has a specified alignment.
6461 DEFINITION is true if this field is for a record being defined.
6463 DEBUG_INFO_P is true if we need to write debug information for types
6464 that we may create in the process. */
6467 gnat_to_gnu_field (Entity_Id gnat_field
, tree gnu_record_type
, int packed
,
6468 bool definition
, bool debug_info_p
)
6470 const Entity_Id gnat_field_type
= Etype (gnat_field
);
6471 tree gnu_field_type
= gnat_to_gnu_type (gnat_field_type
);
6472 tree gnu_field_id
= get_entity_name (gnat_field
);
6473 tree gnu_field
, gnu_size
, gnu_pos
;
6475 = (Treat_As_Volatile (gnat_field
) || Treat_As_Volatile (gnat_field_type
));
6476 bool needs_strict_alignment
6478 || Is_Aliased (gnat_field
)
6479 || Strict_Alignment (gnat_field_type
));
6481 /* If this field requires strict alignment, we cannot pack it because
6482 it would very likely be under-aligned in the record. */
6483 if (needs_strict_alignment
)
6486 packed
= adjust_packed (gnu_field_type
, gnu_record_type
, packed
);
6488 /* If a size is specified, use it. Otherwise, if the record type is packed,
6489 use the official RM size. See "Handling of Type'Size Values" in Einfo
6490 for further details. */
6491 if (Known_Esize (gnat_field
))
6492 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6493 gnat_field
, FIELD_DECL
, false, true);
6494 else if (packed
== 1)
6495 gnu_size
= validate_size (RM_Size (gnat_field_type
), gnu_field_type
,
6496 gnat_field
, FIELD_DECL
, false, true);
6498 gnu_size
= NULL_TREE
;
6500 /* If we have a specified size that is smaller than that of the field's type,
6501 or a position is specified, and the field's type is a record that doesn't
6502 require strict alignment, see if we can get either an integral mode form
6503 of the type or a smaller form. If we can, show a size was specified for
6504 the field if there wasn't one already, so we know to make this a bitfield
6505 and avoid making things wider.
6507 Changing to an integral mode form is useful when the record is packed as
6508 we can then place the field at a non-byte-aligned position and so achieve
6509 tighter packing. This is in addition required if the field shares a byte
6510 with another field and the front-end lets the back-end handle the access
6511 to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6513 Changing to a smaller form is required if the specified size is smaller
6514 than that of the field's type and the type contains sub-fields that are
6515 padded, in order to avoid generating accesses to these sub-fields that
6516 are wider than the field.
6518 We avoid the transformation if it is not required or potentially useful,
6519 as it might entail an increase of the field's alignment and have ripple
6520 effects on the outer record type. A typical case is a field known to be
6521 byte-aligned and not to share a byte with another field. */
6522 if (!needs_strict_alignment
6523 && RECORD_OR_UNION_TYPE_P (gnu_field_type
)
6524 && !TYPE_FAT_POINTER_P (gnu_field_type
)
6525 && tree_fits_uhwi_p (TYPE_SIZE (gnu_field_type
))
6528 && (tree_int_cst_lt (gnu_size
, TYPE_SIZE (gnu_field_type
))
6529 || (Present (Component_Clause (gnat_field
))
6530 && !(UI_To_Int (Component_Bit_Offset (gnat_field
))
6531 % BITS_PER_UNIT
== 0
6532 && value_factor_p (gnu_size
, BITS_PER_UNIT
)))))))
6534 tree gnu_packable_type
= make_packable_type (gnu_field_type
, true);
6535 if (gnu_packable_type
!= gnu_field_type
)
6537 gnu_field_type
= gnu_packable_type
;
6539 gnu_size
= rm_size (gnu_field_type
);
6543 if (Is_Atomic (gnat_field
))
6544 check_ok_for_atomic (gnu_field_type
, gnat_field
, false);
6546 if (Present (Component_Clause (gnat_field
)))
6548 Entity_Id gnat_parent
6549 = Parent_Subtype (Underlying_Type (Scope (gnat_field
)));
6551 gnu_pos
= UI_To_gnu (Component_Bit_Offset (gnat_field
), bitsizetype
);
6552 gnu_size
= validate_size (Esize (gnat_field
), gnu_field_type
,
6553 gnat_field
, FIELD_DECL
, false, true);
6555 /* Ensure the position does not overlap with the parent subtype, if there
6556 is one. This test is omitted if the parent of the tagged type has a
6557 full rep clause since, in this case, component clauses are allowed to
6558 overlay the space allocated for the parent type and the front-end has
6559 checked that there are no overlapping components. */
6560 if (Present (gnat_parent
) && !Is_Fully_Repped_Tagged_Type (gnat_parent
))
6562 tree gnu_parent
= gnat_to_gnu_type (gnat_parent
);
6564 if (TREE_CODE (TYPE_SIZE (gnu_parent
)) == INTEGER_CST
6565 && tree_int_cst_lt (gnu_pos
, TYPE_SIZE (gnu_parent
)))
6568 ("offset of& must be beyond parent{, minimum allowed is ^}",
6569 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6570 TYPE_SIZE_UNIT (gnu_parent
));
6574 /* If this field needs strict alignment, check that the record is
6575 sufficiently aligned and that position and size are consistent with
6576 the alignment. But don't do it if we are just annotating types and
6577 the field's type is tagged, since tagged types aren't fully laid out
6578 in this mode. Also, note that atomic implies volatile so the inner
6579 test sequences ordering is significant here. */
6580 if (needs_strict_alignment
6581 && !(type_annotate_only
&& Is_Tagged_Type (gnat_field_type
)))
6583 TYPE_ALIGN (gnu_record_type
)
6584 = MAX (TYPE_ALIGN (gnu_record_type
), TYPE_ALIGN (gnu_field_type
));
6587 && !operand_equal_p (gnu_size
, TYPE_SIZE (gnu_field_type
), 0))
6589 if (Is_Atomic (gnat_field
) || Is_Atomic (gnat_field_type
))
6591 ("atomic field& must be natural size of type{ (^)}",
6592 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6593 TYPE_SIZE (gnu_field_type
));
6595 else if (is_volatile
)
6597 ("volatile field& must be natural size of type{ (^)}",
6598 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6599 TYPE_SIZE (gnu_field_type
));
6601 else if (Is_Aliased (gnat_field
))
6603 ("size of aliased field& must be ^ bits",
6604 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6605 TYPE_SIZE (gnu_field_type
));
6607 else if (Strict_Alignment (gnat_field_type
))
6609 ("size of & with aliased or tagged components not ^ bits",
6610 Last_Bit (Component_Clause (gnat_field
)), gnat_field
,
6611 TYPE_SIZE (gnu_field_type
));
6616 gnu_size
= NULL_TREE
;
6619 if (!integer_zerop (size_binop
6620 (TRUNC_MOD_EXPR
, gnu_pos
,
6621 bitsize_int (TYPE_ALIGN (gnu_field_type
)))))
6623 if (Is_Atomic (gnat_field
) || Is_Atomic (gnat_field_type
))
6625 ("position of atomic field& must be multiple of ^ bits",
6626 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6627 TYPE_ALIGN (gnu_field_type
));
6629 else if (is_volatile
)
6631 ("position of volatile field& must be multiple of ^ bits",
6632 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6633 TYPE_ALIGN (gnu_field_type
));
6635 else if (Is_Aliased (gnat_field
))
6637 ("position of aliased field& must be multiple of ^ bits",
6638 First_Bit (Component_Clause (gnat_field
)), gnat_field
,
6639 TYPE_ALIGN (gnu_field_type
));
6641 else if (Strict_Alignment (gnat_field_type
))
6643 ("position of & is not compatible with alignment required "
6644 "by its components",
6645 First_Bit (Component_Clause (gnat_field
)), gnat_field
);
6650 gnu_pos
= NULL_TREE
;
6655 /* If the record has rep clauses and this is the tag field, make a rep
6656 clause for it as well. */
6657 else if (Has_Specified_Layout (Scope (gnat_field
))
6658 && Chars (gnat_field
) == Name_uTag
)
6660 gnu_pos
= bitsize_zero_node
;
6661 gnu_size
= TYPE_SIZE (gnu_field_type
);
6666 gnu_pos
= NULL_TREE
;
6668 /* If we are packing the record and the field is BLKmode, round the
6669 size up to a byte boundary. */
6670 if (packed
&& TYPE_MODE (gnu_field_type
) == BLKmode
&& gnu_size
)
6671 gnu_size
= round_up (gnu_size
, BITS_PER_UNIT
);
6674 /* We need to make the size the maximum for the type if it is
6675 self-referential and an unconstrained type. In that case, we can't
6676 pack the field since we can't make a copy to align it. */
6677 if (TREE_CODE (gnu_field_type
) == RECORD_TYPE
6679 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type
))
6680 && !Is_Constrained (Underlying_Type (gnat_field_type
)))
6682 gnu_size
= max_size (TYPE_SIZE (gnu_field_type
), true);
6686 /* If a size is specified, adjust the field's type to it. */
6689 tree orig_field_type
;
6691 /* If the field's type is justified modular, we would need to remove
6692 the wrapper to (better) meet the layout requirements. However we
6693 can do so only if the field is not aliased to preserve the unique
6694 layout and if the prescribed size is not greater than that of the
6695 packed array to preserve the justification. */
6696 if (!needs_strict_alignment
6697 && TREE_CODE (gnu_field_type
) == RECORD_TYPE
6698 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type
)
6699 && tree_int_cst_compare (gnu_size
, TYPE_ADA_SIZE (gnu_field_type
))
6701 gnu_field_type
= TREE_TYPE (TYPE_FIELDS (gnu_field_type
));
6703 /* Similarly if the field's type is a misaligned integral type, but
6704 there is no restriction on the size as there is no justification. */
6705 if (!needs_strict_alignment
6706 && TYPE_IS_PADDING_P (gnu_field_type
)
6707 && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type
))))
6708 gnu_field_type
= TREE_TYPE (TYPE_FIELDS (gnu_field_type
));
6711 = make_type_from_size (gnu_field_type
, gnu_size
,
6712 Has_Biased_Representation (gnat_field
));
6714 orig_field_type
= gnu_field_type
;
6715 gnu_field_type
= maybe_pad_type (gnu_field_type
, gnu_size
, 0, gnat_field
,
6716 false, false, definition
, true);
6718 /* If a padding record was made, declare it now since it will never be
6719 declared otherwise. This is necessary to ensure that its subtrees
6720 are properly marked. */
6721 if (gnu_field_type
!= orig_field_type
6722 && !DECL_P (TYPE_NAME (gnu_field_type
)))
6723 create_type_decl (TYPE_NAME (gnu_field_type
), gnu_field_type
, true,
6724 debug_info_p
, gnat_field
);
6727 /* Otherwise (or if there was an error), don't specify a position. */
6729 gnu_pos
= NULL_TREE
;
6731 gcc_assert (TREE_CODE (gnu_field_type
) != RECORD_TYPE
6732 || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type
));
6734 /* Now create the decl for the field. */
6736 = create_field_decl (gnu_field_id
, gnu_field_type
, gnu_record_type
,
6737 gnu_size
, gnu_pos
, packed
, Is_Aliased (gnat_field
));
6738 Sloc_to_locus (Sloc (gnat_field
), &DECL_SOURCE_LOCATION (gnu_field
));
6739 DECL_ALIASED_P (gnu_field
) = Is_Aliased (gnat_field
);
6740 TREE_THIS_VOLATILE (gnu_field
) = TREE_SIDE_EFFECTS (gnu_field
) = is_volatile
;
6742 if (Ekind (gnat_field
) == E_Discriminant
)
6743 DECL_DISCRIMINANT_NUMBER (gnu_field
)
6744 = UI_To_gnu (Discriminant_Number (gnat_field
), sizetype
);
6749 /* Return true if at least one member of COMPONENT_LIST needs strict
6753 components_need_strict_alignment (Node_Id component_list
)
6755 Node_Id component_decl
;
6757 for (component_decl
= First_Non_Pragma (Component_Items (component_list
));
6758 Present (component_decl
);
6759 component_decl
= Next_Non_Pragma (component_decl
))
6761 Entity_Id gnat_field
= Defining_Entity (component_decl
);
6763 if (Is_Aliased (gnat_field
))
6766 if (Strict_Alignment (Etype (gnat_field
)))
6773 /* Return true if TYPE is a type with variable size or a padding type with a
6774 field of variable size or a record that has a field with such a type. */
6777 type_has_variable_size (tree type
)
6781 if (!TREE_CONSTANT (TYPE_SIZE (type
)))
6784 if (TYPE_IS_PADDING_P (type
)
6785 && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type
))))
6788 if (!RECORD_OR_UNION_TYPE_P (type
))
6791 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
6792 if (type_has_variable_size (TREE_TYPE (field
)))
6798 /* Return true if FIELD is an artificial field. */
6801 field_is_artificial (tree field
)
6803 /* These fields are generated by the front-end proper. */
6804 if (IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == '_')
6807 /* These fields are generated by gigi. */
6808 if (DECL_INTERNAL_P (field
))
6814 /* Return true if FIELD is a non-artificial aliased field. */
6817 field_is_aliased (tree field
)
6819 if (field_is_artificial (field
))
6822 return DECL_ALIASED_P (field
);
6825 /* Return true if FIELD is a non-artificial field with self-referential
6829 field_has_self_size (tree field
)
6831 if (field_is_artificial (field
))
6834 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
6837 return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field
)));
6840 /* Return true if FIELD is a non-artificial field with variable size. */
6843 field_has_variable_size (tree field
)
6845 if (field_is_artificial (field
))
6848 if (DECL_SIZE (field
) && TREE_CODE (DECL_SIZE (field
)) == INTEGER_CST
)
6851 return TREE_CODE (TYPE_SIZE (TREE_TYPE (field
))) != INTEGER_CST
;
6854 /* qsort comparer for the bit positions of two record components. */
6857 compare_field_bitpos (const PTR rt1
, const PTR rt2
)
6859 const_tree
const field1
= * (const_tree
const *) rt1
;
6860 const_tree
const field2
= * (const_tree
const *) rt2
;
6862 = tree_int_cst_compare (bit_position (field1
), bit_position (field2
));
6864 return ret
? ret
: (int) (DECL_UID (field1
) - DECL_UID (field2
));
6867 /* Structure holding information for a given variant. */
6868 typedef struct vinfo
6870 /* The record type of the variant. */
6873 /* The name of the variant. */
6876 /* The qualifier of the variant. */
6879 /* Whether the variant has a rep clause. */
6882 /* Whether the variant is packed. */
6887 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set the
6888 result as the field list of GNU_RECORD_TYPE and finish it up. Return true
6889 if GNU_RECORD_TYPE has a rep clause which affects the layout (see below).
6890 When called from gnat_to_gnu_entity during the processing of a record type
6891 definition, the GCC node for the parent, if any, will be the single field
6892 of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6893 GNU_FIELD_LIST. The other calls to this function are recursive calls for
6894 the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6896 PACKED is 1 if this is for a packed record, -1 if this is for a record
6897 with Component_Alignment of Storage_Unit, -2 if this is for a record
6898 with a specified alignment.
6900 DEFINITION is true if we are defining this record type.
6902 CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6903 out the record. This means the alignment only serves to force fields to
6904 be bitfields, but not to require the record to be that aligned. This is
6907 ALL_REP is true if a rep clause is present for all the fields.
6909 UNCHECKED_UNION is true if we are building this type for a record with a
6910 Pragma Unchecked_Union.
6912 ARTIFICIAL is true if this is a type that was generated by the compiler.
6914 DEBUG_INFO is true if we need to write debug information about the type.
6916 MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6917 mean that its contents may be unused as well, only the container itself.
6919 REORDER is true if we are permitted to reorder components of this type.
6921 FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6922 the outer record type down to this variant level. It is nonzero only if
6923 all the fields down to this level have a rep clause and ALL_REP is false.
6925 P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6926 with a rep clause is to be added; in this case, that is all that should
6927 be done with such fields and the return value will be false. */
6930 components_to_record (tree gnu_record_type
, Node_Id gnat_component_list
,
6931 tree gnu_field_list
, int packed
, bool definition
,
6932 bool cancel_alignment
, bool all_rep
,
6933 bool unchecked_union
, bool artificial
,
6934 bool debug_info
, bool maybe_unused
, bool reorder
,
6935 tree first_free_pos
, tree
*p_gnu_rep_list
)
6937 bool all_rep_and_size
= all_rep
&& TYPE_SIZE (gnu_record_type
);
6938 bool variants_have_rep
= all_rep
;
6939 bool layout_with_rep
= false;
6940 bool has_self_field
= false;
6941 bool has_aliased_after_self_field
= false;
6942 Node_Id component_decl
, variant_part
;
6943 tree gnu_field
, gnu_next
, gnu_last
;
6944 tree gnu_variant_part
= NULL_TREE
;
6945 tree gnu_rep_list
= NULL_TREE
;
6946 tree gnu_var_list
= NULL_TREE
;
6947 tree gnu_self_list
= NULL_TREE
;
6948 tree gnu_zero_list
= NULL_TREE
;
6950 /* For each component referenced in a component declaration create a GCC
6951 field and add it to the list, skipping pragmas in the GNAT list. */
6952 gnu_last
= tree_last (gnu_field_list
);
6953 if (Present (Component_Items (gnat_component_list
)))
6955 = First_Non_Pragma (Component_Items (gnat_component_list
));
6956 Present (component_decl
);
6957 component_decl
= Next_Non_Pragma (component_decl
))
6959 Entity_Id gnat_field
= Defining_Entity (component_decl
);
6960 Name_Id gnat_name
= Chars (gnat_field
);
6962 /* If present, the _Parent field must have been created as the single
6963 field of the record type. Put it before any other fields. */
6964 if (gnat_name
== Name_uParent
)
6966 gnu_field
= TYPE_FIELDS (gnu_record_type
);
6967 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
6971 gnu_field
= gnat_to_gnu_field (gnat_field
, gnu_record_type
, packed
,
6972 definition
, debug_info
);
6974 /* If this is the _Tag field, put it before any other fields. */
6975 if (gnat_name
== Name_uTag
)
6976 gnu_field_list
= chainon (gnu_field_list
, gnu_field
);
6978 /* If this is the _Controller field, put it before the other
6979 fields except for the _Tag or _Parent field. */
6980 else if (gnat_name
== Name_uController
&& gnu_last
)
6982 DECL_CHAIN (gnu_field
) = DECL_CHAIN (gnu_last
);
6983 DECL_CHAIN (gnu_last
) = gnu_field
;
6986 /* If this is a regular field, put it after the other fields. */
6989 DECL_CHAIN (gnu_field
) = gnu_field_list
;
6990 gnu_field_list
= gnu_field
;
6992 gnu_last
= gnu_field
;
6994 /* And record information for the final layout. */
6995 if (field_has_self_size (gnu_field
))
6996 has_self_field
= true;
6997 else if (has_self_field
&& field_is_aliased (gnu_field
))
6998 has_aliased_after_self_field
= true;
7002 save_gnu_tree (gnat_field
, gnu_field
, false);
7005 /* At the end of the component list there may be a variant part. */
7006 variant_part
= Variant_Part (gnat_component_list
);
7008 /* We create a QUAL_UNION_TYPE for the variant part since the variants are
7009 mutually exclusive and should go in the same memory. To do this we need
7010 to treat each variant as a record whose elements are created from the
7011 component list for the variant. So here we create the records from the
7012 lists for the variants and put them all into the QUAL_UNION_TYPE.
7013 If this is an Unchecked_Union, we make a UNION_TYPE instead or
7014 use GNU_RECORD_TYPE if there are no fields so far. */
7015 if (Present (variant_part
))
7017 Node_Id gnat_discr
= Name (variant_part
), variant
;
7018 tree gnu_discr
= gnat_to_gnu (gnat_discr
);
7019 tree gnu_name
= TYPE_IDENTIFIER (gnu_record_type
);
7021 = concat_name (get_identifier (Get_Name_String (Chars (gnat_discr
))),
7023 tree gnu_union_type
, gnu_union_name
;
7024 tree this_first_free_pos
, gnu_variant_list
= NULL_TREE
;
7025 bool union_field_needs_strict_alignment
= false;
7026 auto_vec
<vinfo_t
, 16> variant_types
;
7027 vinfo_t
*gnu_variant
;
7028 unsigned int variants_align
= 0;
7032 = concat_name (gnu_name
, IDENTIFIER_POINTER (gnu_var_name
));
7034 /* Reuse the enclosing union if this is an Unchecked_Union whose fields
7035 are all in the variant part, to match the layout of C unions. There
7036 is an associated check below. */
7037 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
)
7038 gnu_union_type
= gnu_record_type
;
7042 = make_node (unchecked_union
? UNION_TYPE
: QUAL_UNION_TYPE
);
7044 TYPE_NAME (gnu_union_type
) = gnu_union_name
;
7045 TYPE_ALIGN (gnu_union_type
) = 0;
7046 TYPE_PACKED (gnu_union_type
) = TYPE_PACKED (gnu_record_type
);
7049 /* If all the fields down to this level have a rep clause, find out
7050 whether all the fields at this level also have one. If so, then
7051 compute the new first free position to be passed downward. */
7052 this_first_free_pos
= first_free_pos
;
7053 if (this_first_free_pos
)
7055 for (gnu_field
= gnu_field_list
;
7057 gnu_field
= DECL_CHAIN (gnu_field
))
7058 if (DECL_FIELD_OFFSET (gnu_field
))
7060 tree pos
= bit_position (gnu_field
);
7061 if (!tree_int_cst_lt (pos
, this_first_free_pos
))
7063 = size_binop (PLUS_EXPR
, pos
, DECL_SIZE (gnu_field
));
7067 this_first_free_pos
= NULL_TREE
;
7072 /* We build the variants in two passes. The bulk of the work is done in
7073 the first pass, that is to say translating the GNAT nodes, building
7074 the container types and computing the associated properties. However
7075 we cannot finish up the container types during this pass because we
7076 don't know where the variant part will be placed until the end. */
7077 for (variant
= First_Non_Pragma (Variants (variant_part
));
7079 variant
= Next_Non_Pragma (variant
))
7081 tree gnu_variant_type
= make_node (RECORD_TYPE
);
7082 tree gnu_inner_name
, gnu_qual
;
7087 Get_Variant_Encoding (variant
);
7088 gnu_inner_name
= get_identifier_with_length (Name_Buffer
, Name_Len
);
7089 TYPE_NAME (gnu_variant_type
)
7090 = concat_name (gnu_union_name
,
7091 IDENTIFIER_POINTER (gnu_inner_name
));
7093 /* Set the alignment of the inner type in case we need to make
7094 inner objects into bitfields, but then clear it out so the
7095 record actually gets only the alignment required. */
7096 TYPE_ALIGN (gnu_variant_type
) = TYPE_ALIGN (gnu_record_type
);
7097 TYPE_PACKED (gnu_variant_type
) = TYPE_PACKED (gnu_record_type
);
7099 /* Similarly, if the outer record has a size specified and all
7100 the fields have a rep clause, we can propagate the size. */
7101 if (all_rep_and_size
)
7103 TYPE_SIZE (gnu_variant_type
) = TYPE_SIZE (gnu_record_type
);
7104 TYPE_SIZE_UNIT (gnu_variant_type
)
7105 = TYPE_SIZE_UNIT (gnu_record_type
);
7108 /* Add the fields into the record type for the variant. Note that
7109 we aren't sure to really use it at this point, see below. */
7111 = components_to_record (gnu_variant_type
, Component_List (variant
),
7112 NULL_TREE
, packed
, definition
,
7113 !all_rep_and_size
, all_rep
,
7115 true, debug_info
, true, reorder
,
7116 this_first_free_pos
,
7117 all_rep
|| this_first_free_pos
7118 ? NULL
: &gnu_rep_list
);
7120 /* Translate the qualifier and annotate the GNAT node. */
7121 gnu_qual
= choices_to_gnu (gnu_discr
, Discrete_Choices (variant
));
7122 Set_Present_Expr (variant
, annotate_value (gnu_qual
));
7124 /* Deal with packedness like in gnat_to_gnu_field. */
7125 if (components_need_strict_alignment (Component_List (variant
)))
7128 union_field_needs_strict_alignment
= true;
7132 = adjust_packed (gnu_variant_type
, gnu_record_type
, packed
);
7134 /* Push this variant onto the stack for the second pass. */
7135 vinfo
.type
= gnu_variant_type
;
7136 vinfo
.name
= gnu_inner_name
;
7137 vinfo
.qual
= gnu_qual
;
7138 vinfo
.has_rep
= has_rep
;
7139 vinfo
.packed
= field_packed
;
7140 variant_types
.safe_push (vinfo
);
7142 /* Compute the global properties that will determine the placement of
7143 the variant part. */
7144 variants_have_rep
|= has_rep
;
7145 if (!field_packed
&& TYPE_ALIGN (gnu_variant_type
) > variants_align
)
7146 variants_align
= TYPE_ALIGN (gnu_variant_type
);
7149 /* Round up the first free position to the alignment of the variant part
7150 for the variants without rep clause. This will guarantee a consistent
7151 layout independently of the placement of the variant part. */
7152 if (variants_have_rep
&& variants_align
> 0 && this_first_free_pos
)
7153 this_first_free_pos
= round_up (this_first_free_pos
, variants_align
);
7155 /* In the second pass, the container types are adjusted if necessary and
7156 finished up, then the corresponding fields of the variant part are
7157 built with their qualifier, unless this is an unchecked union. */
7158 FOR_EACH_VEC_ELT (variant_types
, i
, gnu_variant
)
7160 tree gnu_variant_type
= gnu_variant
->type
;
7161 tree gnu_field_list
= TYPE_FIELDS (gnu_variant_type
);
7163 /* If this is an Unchecked_Union whose fields are all in the variant
7164 part and we have a single field with no representation clause or
7165 placed at offset zero, use the field directly to match the layout
7167 if (TREE_CODE (gnu_record_type
) == UNION_TYPE
7169 && !DECL_CHAIN (gnu_field_list
)
7170 && (!DECL_FIELD_OFFSET (gnu_field_list
)
7171 || integer_zerop (bit_position (gnu_field_list
))))
7173 gnu_field
= gnu_field_list
;
7174 DECL_CONTEXT (gnu_field
) = gnu_record_type
;
7178 /* Finalize the variant type now. We used to throw away empty
7179 record types but we no longer do that because we need them to
7180 generate complete debug info for the variant; otherwise, the
7181 union type definition will be lacking the fields associated
7182 with these empty variants. */
7183 if (gnu_field_list
&& variants_have_rep
&& !gnu_variant
->has_rep
)
7185 /* The variant part will be at offset 0 so we need to ensure
7186 that the fields are laid out starting from the first free
7187 position at this level. */
7188 tree gnu_rep_type
= make_node (RECORD_TYPE
);
7190 finish_record_type (gnu_rep_type
, NULL_TREE
, 0, debug_info
);
7192 = create_rep_part (gnu_rep_type
, gnu_variant_type
,
7193 this_first_free_pos
);
7194 DECL_CHAIN (gnu_rep_part
) = gnu_field_list
;
7195 gnu_field_list
= gnu_rep_part
;
7196 finish_record_type (gnu_variant_type
, gnu_field_list
, 0,
7201 rest_of_record_type_compilation (gnu_variant_type
);
7202 create_type_decl (TYPE_NAME (gnu_variant_type
), gnu_variant_type
,
7203 true, debug_info
, gnat_component_list
);
7206 = create_field_decl (gnu_variant
->name
, gnu_variant_type
,
7209 ? TYPE_SIZE (gnu_variant_type
) : 0,
7210 variants_have_rep
? bitsize_zero_node
: 0,
7211 gnu_variant
->packed
, 0);
7213 DECL_INTERNAL_P (gnu_field
) = 1;
7215 if (!unchecked_union
)
7216 DECL_QUALIFIER (gnu_field
) = gnu_variant
->qual
;
7219 DECL_CHAIN (gnu_field
) = gnu_variant_list
;
7220 gnu_variant_list
= gnu_field
;
7223 /* Only make the QUAL_UNION_TYPE if there are non-empty variants. */
7224 if (gnu_variant_list
)
7226 int union_field_packed
;
7228 if (all_rep_and_size
)
7230 TYPE_SIZE (gnu_union_type
) = TYPE_SIZE (gnu_record_type
);
7231 TYPE_SIZE_UNIT (gnu_union_type
)
7232 = TYPE_SIZE_UNIT (gnu_record_type
);
7235 finish_record_type (gnu_union_type
, nreverse (gnu_variant_list
),
7236 all_rep_and_size
? 1 : 0, debug_info
);
7238 /* If GNU_UNION_TYPE is our record type, it means we must have an
7239 Unchecked_Union with no fields. Verify that and, if so, just
7241 if (gnu_union_type
== gnu_record_type
)
7243 gcc_assert (unchecked_union
7246 return variants_have_rep
;
7249 create_type_decl (TYPE_NAME (gnu_union_type
), gnu_union_type
, true,
7250 debug_info
, gnat_component_list
);
7252 /* Deal with packedness like in gnat_to_gnu_field. */
7253 if (union_field_needs_strict_alignment
)
7254 union_field_packed
= 0;
7257 = adjust_packed (gnu_union_type
, gnu_record_type
, packed
);
7260 = create_field_decl (gnu_var_name
, gnu_union_type
, gnu_record_type
,
7262 ? TYPE_SIZE (gnu_union_type
) : 0,
7263 variants_have_rep
? bitsize_zero_node
: 0,
7264 union_field_packed
, 0);
7266 DECL_INTERNAL_P (gnu_variant_part
) = 1;
7270 /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7271 permitted to reorder components, self-referential sizes or variable sizes.
7272 If they do, pull them out and put them onto the appropriate list. We have
7273 to do this in a separate pass since we want to handle the discriminants
7274 but can't play with them until we've used them in debugging data above.
7276 Similarly, pull out the fields with zero size and no rep clause, as they
7277 would otherwise modify the layout and thus very likely run afoul of the
7278 Ada semantics, which are different from those of C here.
7280 ??? If we reorder them, debugging information will be wrong but there is
7281 nothing that can be done about this at the moment. */
7282 gnu_last
= NULL_TREE
;
7284 #define MOVE_FROM_FIELD_LIST_TO(LIST) \
7287 DECL_CHAIN (gnu_last) = gnu_next; \
7289 gnu_field_list = gnu_next; \
7291 DECL_CHAIN (gnu_field) = (LIST); \
7292 (LIST) = gnu_field; \
7295 for (gnu_field
= gnu_field_list
; gnu_field
; gnu_field
= gnu_next
)
7297 gnu_next
= DECL_CHAIN (gnu_field
);
7299 if (DECL_FIELD_OFFSET (gnu_field
))
7301 MOVE_FROM_FIELD_LIST_TO (gnu_rep_list
);
7305 if ((reorder
|| has_aliased_after_self_field
)
7306 && field_has_self_size (gnu_field
))
7308 MOVE_FROM_FIELD_LIST_TO (gnu_self_list
);
7312 if (reorder
&& field_has_variable_size (gnu_field
))
7314 MOVE_FROM_FIELD_LIST_TO (gnu_var_list
);
7318 if (DECL_SIZE (gnu_field
) && integer_zerop (DECL_SIZE (gnu_field
)))
7320 DECL_FIELD_OFFSET (gnu_field
) = size_zero_node
;
7321 SET_DECL_OFFSET_ALIGN (gnu_field
, BIGGEST_ALIGNMENT
);
7322 DECL_FIELD_BIT_OFFSET (gnu_field
) = bitsize_zero_node
;
7323 if (field_is_aliased (gnu_field
))
7324 TYPE_ALIGN (gnu_record_type
)
7325 = MAX (TYPE_ALIGN (gnu_record_type
),
7326 TYPE_ALIGN (TREE_TYPE (gnu_field
)));
7327 MOVE_FROM_FIELD_LIST_TO (gnu_zero_list
);
7331 gnu_last
= gnu_field
;
7334 #undef MOVE_FROM_FIELD_LIST_TO
7336 gnu_field_list
= nreverse (gnu_field_list
);
7338 /* If permitted, we reorder the fields as follows:
7340 1) all fixed length fields,
7341 2) all fields whose length doesn't depend on discriminants,
7342 3) all fields whose length depends on discriminants,
7343 4) the variant part,
7345 within the record and within each variant recursively. */
7348 = chainon (gnu_field_list
, chainon (gnu_var_list
, gnu_self_list
));
7350 /* Otherwise, if there is an aliased field placed after a field whose length
7351 depends on discriminants, we put all the fields of the latter sort, last.
7352 We need to do this in case an object of this record type is mutable. */
7353 else if (has_aliased_after_self_field
)
7354 gnu_field_list
= chainon (gnu_field_list
, gnu_self_list
);
7356 /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7357 in our REP list to the previous level because this level needs them in
7358 order to do a correct layout, i.e. avoid having overlapping fields. */
7359 if (p_gnu_rep_list
&& gnu_rep_list
)
7360 *p_gnu_rep_list
= chainon (*p_gnu_rep_list
, gnu_rep_list
);
7362 /* Otherwise, sort the fields by bit position and put them into their own
7363 record, before the others, if we also have fields without rep clause. */
7364 else if (gnu_rep_list
)
7366 tree gnu_rep_type
, gnu_rep_part
;
7367 int i
, len
= list_length (gnu_rep_list
);
7368 tree
*gnu_arr
= XALLOCAVEC (tree
, len
);
7370 /* If all the fields have a rep clause, we can do a flat layout. */
7371 layout_with_rep
= !gnu_field_list
7372 && (!gnu_variant_part
|| variants_have_rep
);
7374 = layout_with_rep
? gnu_record_type
: make_node (RECORD_TYPE
);
7376 for (gnu_field
= gnu_rep_list
, i
= 0;
7378 gnu_field
= DECL_CHAIN (gnu_field
), i
++)
7379 gnu_arr
[i
] = gnu_field
;
7381 qsort (gnu_arr
, len
, sizeof (tree
), compare_field_bitpos
);
7383 /* Put the fields in the list in order of increasing position, which
7384 means we start from the end. */
7385 gnu_rep_list
= NULL_TREE
;
7386 for (i
= len
- 1; i
>= 0; i
--)
7388 DECL_CHAIN (gnu_arr
[i
]) = gnu_rep_list
;
7389 gnu_rep_list
= gnu_arr
[i
];
7390 DECL_CONTEXT (gnu_arr
[i
]) = gnu_rep_type
;
7393 if (layout_with_rep
)
7394 gnu_field_list
= gnu_rep_list
;
7397 finish_record_type (gnu_rep_type
, gnu_rep_list
, 1, debug_info
);
7399 /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7400 without rep clause are laid out starting from this position.
7401 Therefore, we force it as a minimal size on the REP part. */
7403 = create_rep_part (gnu_rep_type
, gnu_record_type
, first_free_pos
);
7405 /* Chain the REP part at the beginning of the field list. */
7406 DECL_CHAIN (gnu_rep_part
) = gnu_field_list
;
7407 gnu_field_list
= gnu_rep_part
;
7411 /* Chain the variant part at the end of the field list. */
7412 if (gnu_variant_part
)
7413 gnu_field_list
= chainon (gnu_field_list
, gnu_variant_part
);
7415 if (cancel_alignment
)
7416 TYPE_ALIGN (gnu_record_type
) = 0;
7418 TYPE_ARTIFICIAL (gnu_record_type
) = artificial
;
7420 finish_record_type (gnu_record_type
, gnu_field_list
, layout_with_rep
? 1 : 0,
7421 debug_info
&& !maybe_unused
);
7423 /* Chain the fields with zero size at the beginning of the field list. */
7425 TYPE_FIELDS (gnu_record_type
)
7426 = chainon (gnu_zero_list
, TYPE_FIELDS (gnu_record_type
));
7428 return (gnu_rep_list
&& !p_gnu_rep_list
) || variants_have_rep
;
7431 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7432 placed into an Esize, Component_Bit_Offset, or Component_Size value
7433 in the GNAT tree. */
7436 annotate_value (tree gnu_size
)
7439 Node_Ref_Or_Val ops
[3], ret
, pre_op1
= No_Uint
;
7440 struct tree_int_map in
;
7443 /* See if we've already saved the value for this node. */
7444 if (EXPR_P (gnu_size
))
7446 struct tree_int_map
*e
;
7448 in
.base
.from
= gnu_size
;
7449 e
= (struct tree_int_map
*) htab_find (annotate_value_cache
, &in
);
7452 return (Node_Ref_Or_Val
) e
->to
;
7455 in
.base
.from
= NULL_TREE
;
7457 /* If we do not return inside this switch, TCODE will be set to the
7458 code to use for a Create_Node operand and LEN (set above) will be
7459 the number of recursive calls for us to make. */
7461 switch (TREE_CODE (gnu_size
))
7464 return TREE_OVERFLOW (gnu_size
) ? No_Uint
: UI_From_gnu (gnu_size
);
7467 /* The only case we handle here is a simple discriminant reference. */
7468 if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size
, 1)))
7470 tree n
= DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size
, 1));
7472 /* Climb up the chain of successive extensions, if any. */
7473 while (TREE_CODE (TREE_OPERAND (gnu_size
, 0)) == COMPONENT_REF
7474 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size
, 0), 1))
7476 gnu_size
= TREE_OPERAND (gnu_size
, 0);
7478 if (TREE_CODE (TREE_OPERAND (gnu_size
, 0)) == PLACEHOLDER_EXPR
)
7480 Create_Node (Discrim_Val
, annotate_value (n
), No_Uint
, No_Uint
);
7485 CASE_CONVERT
: case NON_LVALUE_EXPR
:
7486 return annotate_value (TREE_OPERAND (gnu_size
, 0));
7488 /* Now just list the operations we handle. */
7489 case COND_EXPR
: tcode
= Cond_Expr
; break;
7490 case PLUS_EXPR
: tcode
= Plus_Expr
; break;
7491 case MINUS_EXPR
: tcode
= Minus_Expr
; break;
7492 case MULT_EXPR
: tcode
= Mult_Expr
; break;
7493 case TRUNC_DIV_EXPR
: tcode
= Trunc_Div_Expr
; break;
7494 case CEIL_DIV_EXPR
: tcode
= Ceil_Div_Expr
; break;
7495 case FLOOR_DIV_EXPR
: tcode
= Floor_Div_Expr
; break;
7496 case TRUNC_MOD_EXPR
: tcode
= Trunc_Mod_Expr
; break;
7497 case CEIL_MOD_EXPR
: tcode
= Ceil_Mod_Expr
; break;
7498 case FLOOR_MOD_EXPR
: tcode
= Floor_Mod_Expr
; break;
7499 case EXACT_DIV_EXPR
: tcode
= Exact_Div_Expr
; break;
7500 case NEGATE_EXPR
: tcode
= Negate_Expr
; break;
7501 case MIN_EXPR
: tcode
= Min_Expr
; break;
7502 case MAX_EXPR
: tcode
= Max_Expr
; break;
7503 case ABS_EXPR
: tcode
= Abs_Expr
; break;
7504 case TRUTH_ANDIF_EXPR
: tcode
= Truth_Andif_Expr
; break;
7505 case TRUTH_ORIF_EXPR
: tcode
= Truth_Orif_Expr
; break;
7506 case TRUTH_AND_EXPR
: tcode
= Truth_And_Expr
; break;
7507 case TRUTH_OR_EXPR
: tcode
= Truth_Or_Expr
; break;
7508 case TRUTH_XOR_EXPR
: tcode
= Truth_Xor_Expr
; break;
7509 case TRUTH_NOT_EXPR
: tcode
= Truth_Not_Expr
; break;
7510 case LT_EXPR
: tcode
= Lt_Expr
; break;
7511 case LE_EXPR
: tcode
= Le_Expr
; break;
7512 case GT_EXPR
: tcode
= Gt_Expr
; break;
7513 case GE_EXPR
: tcode
= Ge_Expr
; break;
7514 case EQ_EXPR
: tcode
= Eq_Expr
; break;
7515 case NE_EXPR
: tcode
= Ne_Expr
; break;
7518 tcode
= Bit_And_Expr
;
7519 /* For negative values in sizetype, build NEGATE_EXPR of the opposite.
7520 Such values appear in expressions with aligning patterns. Note that,
7521 since sizetype is unsigned, we have to jump through some hoops. */
7522 if (TREE_CODE (TREE_OPERAND (gnu_size
, 1)) == INTEGER_CST
)
7524 tree op1
= TREE_OPERAND (gnu_size
, 1);
7525 wide_int signed_op1
= wi::sext (op1
, TYPE_PRECISION (sizetype
));
7526 if (wi::neg_p (signed_op1
))
7528 op1
= wide_int_to_tree (sizetype
, wi::neg (signed_op1
));
7529 pre_op1
= annotate_value (build1 (NEGATE_EXPR
, sizetype
, op1
));
7535 /* In regular mode, inline back only if symbolic annotation is requested
7536 in order to avoid memory explosion on big discriminated record types.
7537 But not in ASIS mode, as symbolic annotation is required for DDA. */
7538 if (List_Representation_Info
== 3 || type_annotate_only
)
7540 tree t
= maybe_inline_call_in_expr (gnu_size
);
7542 return annotate_value (t
);
7545 return Uint_Minus_1
;
7547 /* Fall through... */
7553 /* Now get each of the operands that's relevant for this code. If any
7554 cannot be expressed as a repinfo node, say we can't. */
7555 for (i
= 0; i
< 3; i
++)
7558 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (gnu_size
)); i
++)
7560 if (i
== 1 && pre_op1
!= No_Uint
)
7563 ops
[i
] = annotate_value (TREE_OPERAND (gnu_size
, i
));
7564 if (ops
[i
] == No_Uint
)
7568 ret
= Create_Node (tcode
, ops
[0], ops
[1], ops
[2]);
7570 /* Save the result in the cache. */
7573 struct tree_int_map
**h
;
7574 /* We can't assume the hash table data hasn't moved since the initial
7575 look up, so we have to search again. Allocating and inserting an
7576 entry at that point would be an alternative, but then we'd better
7577 discard the entry if we decided not to cache it. */
7578 h
= (struct tree_int_map
**)
7579 htab_find_slot (annotate_value_cache
, &in
, INSERT
);
7581 *h
= ggc_alloc
<tree_int_map
> ();
7582 (*h
)->base
.from
= gnu_size
;
7589 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7590 and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7591 size and alignment used by Gigi. Prefer SIZE over TYPE_SIZE if non-null.
7592 BY_REF is true if the object is used by reference. */
7595 annotate_object (Entity_Id gnat_entity
, tree gnu_type
, tree size
, bool by_ref
)
7599 if (TYPE_IS_FAT_POINTER_P (gnu_type
))
7600 gnu_type
= TYPE_UNCONSTRAINED_ARRAY (gnu_type
);
7602 gnu_type
= TREE_TYPE (gnu_type
);
7605 if (Unknown_Esize (gnat_entity
))
7607 if (TREE_CODE (gnu_type
) == RECORD_TYPE
7608 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
7609 size
= TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
))));
7611 size
= TYPE_SIZE (gnu_type
);
7614 Set_Esize (gnat_entity
, annotate_value (size
));
7617 if (Unknown_Alignment (gnat_entity
))
7618 Set_Alignment (gnat_entity
,
7619 UI_From_Int (TYPE_ALIGN (gnu_type
) / BITS_PER_UNIT
));
7622 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7623 Return NULL_TREE if there is no such element in the list. */
7626 purpose_member_field (const_tree elem
, tree list
)
7630 tree field
= TREE_PURPOSE (list
);
7631 if (SAME_FIELD_P (field
, elem
))
7633 list
= TREE_CHAIN (list
);
7638 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7639 set Component_Bit_Offset and Esize of the components to the position and
7640 size used by Gigi. */
7643 annotate_rep (Entity_Id gnat_entity
, tree gnu_type
)
7645 Entity_Id gnat_field
;
7648 /* We operate by first making a list of all fields and their position (we
7649 can get the size easily) and then update all the sizes in the tree. */
7651 = build_position_list (gnu_type
, false, size_zero_node
, bitsize_zero_node
,
7652 BIGGEST_ALIGNMENT
, NULL_TREE
);
7654 for (gnat_field
= First_Entity (gnat_entity
);
7655 Present (gnat_field
);
7656 gnat_field
= Next_Entity (gnat_field
))
7657 if (Ekind (gnat_field
) == E_Component
7658 || (Ekind (gnat_field
) == E_Discriminant
7659 && !Is_Unchecked_Union (Scope (gnat_field
))))
7661 tree t
= purpose_member_field (gnat_to_gnu_field_decl (gnat_field
),
7667 /* If we are just annotating types and the type is tagged, the tag
7668 and the parent components are not generated by the front-end so
7669 we need to add the appropriate offset to each component without
7670 representation clause. */
7671 if (type_annotate_only
7672 && Is_Tagged_Type (gnat_entity
)
7673 && No (Component_Clause (gnat_field
)))
7675 /* For a component appearing in the current extension, the
7676 offset is the size of the parent. */
7677 if (Is_Derived_Type (gnat_entity
)
7678 && Original_Record_Component (gnat_field
) == gnat_field
)
7680 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity
))),
7683 parent_offset
= bitsize_int (POINTER_SIZE
);
7685 if (TYPE_FIELDS (gnu_type
))
7687 = round_up (parent_offset
,
7688 DECL_ALIGN (TYPE_FIELDS (gnu_type
)));
7691 parent_offset
= bitsize_zero_node
;
7693 Set_Component_Bit_Offset
7696 (size_binop (PLUS_EXPR
,
7697 bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t
), 0),
7698 TREE_VEC_ELT (TREE_VALUE (t
), 2)),
7701 Set_Esize (gnat_field
,
7702 annotate_value (DECL_SIZE (TREE_PURPOSE (t
))));
7704 else if (Is_Tagged_Type (gnat_entity
) && Is_Derived_Type (gnat_entity
))
7706 /* If there is no entry, this is an inherited component whose
7707 position is the same as in the parent type. */
7708 Set_Component_Bit_Offset
7710 Component_Bit_Offset (Original_Record_Component (gnat_field
)));
7712 Set_Esize (gnat_field
,
7713 Esize (Original_Record_Component (gnat_field
)));
7718 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7719 the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7720 value to be placed into DECL_OFFSET_ALIGN and the bit position. The list
7721 of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7722 is set to true. GNU_POS is to be added to the position, GNU_BITPOS to the
7723 bit position, OFFSET_ALIGN is the present offset alignment. GNU_LIST is a
7724 pre-existing list to be chained to the newly created entries. */
7727 build_position_list (tree gnu_type
, bool do_not_flatten_variant
, tree gnu_pos
,
7728 tree gnu_bitpos
, unsigned int offset_align
, tree gnu_list
)
7732 for (gnu_field
= TYPE_FIELDS (gnu_type
);
7734 gnu_field
= DECL_CHAIN (gnu_field
))
7736 tree gnu_our_bitpos
= size_binop (PLUS_EXPR
, gnu_bitpos
,
7737 DECL_FIELD_BIT_OFFSET (gnu_field
));
7738 tree gnu_our_offset
= size_binop (PLUS_EXPR
, gnu_pos
,
7739 DECL_FIELD_OFFSET (gnu_field
));
7740 unsigned int our_offset_align
7741 = MIN (offset_align
, DECL_OFFSET_ALIGN (gnu_field
));
7742 tree v
= make_tree_vec (3);
7744 TREE_VEC_ELT (v
, 0) = gnu_our_offset
;
7745 TREE_VEC_ELT (v
, 1) = size_int (our_offset_align
);
7746 TREE_VEC_ELT (v
, 2) = gnu_our_bitpos
;
7747 gnu_list
= tree_cons (gnu_field
, v
, gnu_list
);
7749 /* Recurse on internal fields, flattening the nested fields except for
7750 those in the variant part, if requested. */
7751 if (DECL_INTERNAL_P (gnu_field
))
7753 tree gnu_field_type
= TREE_TYPE (gnu_field
);
7754 if (do_not_flatten_variant
7755 && TREE_CODE (gnu_field_type
) == QUAL_UNION_TYPE
)
7757 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
7758 size_zero_node
, bitsize_zero_node
,
7759 BIGGEST_ALIGNMENT
, gnu_list
);
7762 = build_position_list (gnu_field_type
, do_not_flatten_variant
,
7763 gnu_our_offset
, gnu_our_bitpos
,
7764 our_offset_align
, gnu_list
);
7771 /* Return a list describing the substitutions needed to reflect the
7772 discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE. They can
7773 be in any order. The values in an element of the list are in the form
7774 of operands to SUBSTITUTE_IN_EXPR. DEFINITION is true if this is for
7775 a definition of GNAT_SUBTYPE. */
7777 static vec
<subst_pair
>
7778 build_subst_list (Entity_Id gnat_subtype
, Entity_Id gnat_type
, bool definition
)
7780 vec
<subst_pair
> gnu_list
= vNULL
;
7781 Entity_Id gnat_discrim
;
7782 Node_Id gnat_constr
;
7784 for (gnat_discrim
= First_Stored_Discriminant (gnat_type
),
7785 gnat_constr
= First_Elmt (Stored_Constraint (gnat_subtype
));
7786 Present (gnat_discrim
);
7787 gnat_discrim
= Next_Stored_Discriminant (gnat_discrim
),
7788 gnat_constr
= Next_Elmt (gnat_constr
))
7789 /* Ignore access discriminants. */
7790 if (!Is_Access_Type (Etype (Node (gnat_constr
))))
7792 tree gnu_field
= gnat_to_gnu_field_decl (gnat_discrim
);
7793 tree replacement
= convert (TREE_TYPE (gnu_field
),
7794 elaborate_expression
7795 (Node (gnat_constr
), gnat_subtype
,
7796 get_entity_name (gnat_discrim
),
7797 definition
, true, false));
7798 subst_pair s
= {gnu_field
, replacement
};
7799 gnu_list
.safe_push (s
);
7805 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7806 variants of QUAL_UNION_TYPE that are still relevant after applying
7807 the substitutions described in SUBST_LIST. GNU_LIST is a pre-existing
7808 list to be prepended to the newly created entries. */
7810 static vec
<variant_desc
>
7811 build_variant_list (tree qual_union_type
, vec
<subst_pair
> subst_list
,
7812 vec
<variant_desc
> gnu_list
)
7816 for (gnu_field
= TYPE_FIELDS (qual_union_type
);
7818 gnu_field
= DECL_CHAIN (gnu_field
))
7820 tree qual
= DECL_QUALIFIER (gnu_field
);
7824 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
7825 qual
= SUBSTITUTE_IN_EXPR (qual
, s
->discriminant
, s
->replacement
);
7827 /* If the new qualifier is not unconditionally false, its variant may
7828 still be accessed. */
7829 if (!integer_zerop (qual
))
7831 tree variant_type
= TREE_TYPE (gnu_field
), variant_subpart
;
7832 variant_desc v
= {variant_type
, gnu_field
, qual
, NULL_TREE
};
7834 gnu_list
.safe_push (v
);
7836 /* Recurse on the variant subpart of the variant, if any. */
7837 variant_subpart
= get_variant_part (variant_type
);
7838 if (variant_subpart
)
7839 gnu_list
= build_variant_list (TREE_TYPE (variant_subpart
),
7840 subst_list
, gnu_list
);
7842 /* If the new qualifier is unconditionally true, the subsequent
7843 variants cannot be accessed. */
7844 if (integer_onep (qual
))
7852 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7853 corresponding to GNAT_OBJECT. If the size is valid, return an INTEGER_CST
7854 corresponding to its value. Otherwise, return NULL_TREE. KIND is set to
7855 VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7856 size of a type, and FIELD_DECL for the size of a field. COMPONENT_P is
7857 true if we are being called to process the Component_Size of GNAT_OBJECT;
7858 this is used only for error messages. ZERO_OK is true if a size of zero
7859 is permitted; if ZERO_OK is false, it means that a size of zero should be
7860 treated as an unspecified size. */
7863 validate_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_object
,
7864 enum tree_code kind
, bool component_p
, bool zero_ok
)
7866 Node_Id gnat_error_node
;
7867 tree type_size
, size
;
7869 /* Return 0 if no size was specified. */
7870 if (uint_size
== No_Uint
)
7873 /* Ignore a negative size since that corresponds to our back-annotation. */
7874 if (UI_Lt (uint_size
, Uint_0
))
7877 /* Find the node to use for error messages. */
7878 if ((Ekind (gnat_object
) == E_Component
7879 || Ekind (gnat_object
) == E_Discriminant
)
7880 && Present (Component_Clause (gnat_object
)))
7881 gnat_error_node
= Last_Bit (Component_Clause (gnat_object
));
7882 else if (Present (Size_Clause (gnat_object
)))
7883 gnat_error_node
= Expression (Size_Clause (gnat_object
));
7885 gnat_error_node
= gnat_object
;
7887 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7888 but cannot be represented in bitsizetype. */
7889 size
= UI_To_gnu (uint_size
, bitsizetype
);
7890 if (TREE_OVERFLOW (size
))
7893 post_error_ne ("component size for& is too large", gnat_error_node
,
7896 post_error_ne ("size for& is too large", gnat_error_node
,
7901 /* Ignore a zero size if it is not permitted. */
7902 if (!zero_ok
&& integer_zerop (size
))
7905 /* The size of objects is always a multiple of a byte. */
7906 if (kind
== VAR_DECL
7907 && !integer_zerop (size_binop (TRUNC_MOD_EXPR
, size
, bitsize_unit_node
)))
7910 post_error_ne ("component size for& is not a multiple of Storage_Unit",
7911 gnat_error_node
, gnat_object
);
7913 post_error_ne ("size for& is not a multiple of Storage_Unit",
7914 gnat_error_node
, gnat_object
);
7918 /* If this is an integral type or a packed array type, the front-end has
7919 already verified the size, so we need not do it here (which would mean
7920 checking against the bounds). However, if this is an aliased object,
7921 it may not be smaller than the type of the object. */
7922 if ((INTEGRAL_TYPE_P (gnu_type
) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type
))
7923 && !(kind
== VAR_DECL
&& Is_Aliased (gnat_object
)))
7926 /* If the object is a record that contains a template, add the size of the
7927 template to the specified size. */
7928 if (TREE_CODE (gnu_type
) == RECORD_TYPE
7929 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
7930 size
= size_binop (PLUS_EXPR
, DECL_SIZE (TYPE_FIELDS (gnu_type
)), size
);
7932 if (kind
== VAR_DECL
7933 /* If a type needs strict alignment, a component of this type in
7934 a packed record cannot be packed and thus uses the type size. */
7935 || (kind
== TYPE_DECL
&& Strict_Alignment (gnat_object
)))
7936 type_size
= TYPE_SIZE (gnu_type
);
7938 type_size
= rm_size (gnu_type
);
7940 /* Modify the size of a discriminated type to be the maximum size. */
7941 if (type_size
&& CONTAINS_PLACEHOLDER_P (type_size
))
7942 type_size
= max_size (type_size
, true);
7944 /* If this is an access type or a fat pointer, the minimum size is that given
7945 by the smallest integral mode that's valid for pointers. */
7946 if (TREE_CODE (gnu_type
) == POINTER_TYPE
|| TYPE_IS_FAT_POINTER_P (gnu_type
))
7948 enum machine_mode p_mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
7949 while (!targetm
.valid_pointer_mode (p_mode
))
7950 p_mode
= GET_MODE_WIDER_MODE (p_mode
);
7951 type_size
= bitsize_int (GET_MODE_BITSIZE (p_mode
));
7954 /* Issue an error either if the default size of the object isn't a constant
7955 or if the new size is smaller than it. */
7956 if (TREE_CODE (type_size
) != INTEGER_CST
7957 || TREE_OVERFLOW (type_size
)
7958 || tree_int_cst_lt (size
, type_size
))
7962 ("component size for& too small{, minimum allowed is ^}",
7963 gnat_error_node
, gnat_object
, type_size
);
7966 ("size for& too small{, minimum allowed is ^}",
7967 gnat_error_node
, gnat_object
, type_size
);
7974 /* Similarly, but both validate and process a value of RM size. This routine
7975 is only called for types. */
7978 set_rm_size (Uint uint_size
, tree gnu_type
, Entity_Id gnat_entity
)
7980 Node_Id gnat_attr_node
;
7981 tree old_size
, size
;
7983 /* Do nothing if no size was specified. */
7984 if (uint_size
== No_Uint
)
7987 /* Ignore a negative size since that corresponds to our back-annotation. */
7988 if (UI_Lt (uint_size
, Uint_0
))
7991 /* Only issue an error if a Value_Size clause was explicitly given.
7992 Otherwise, we'd be duplicating an error on the Size clause. */
7994 = Get_Attribute_Definition_Clause (gnat_entity
, Attr_Value_Size
);
7996 /* Get the size as an INTEGER_CST. Issue an error if a size was specified
7997 but cannot be represented in bitsizetype. */
7998 size
= UI_To_gnu (uint_size
, bitsizetype
);
7999 if (TREE_OVERFLOW (size
))
8001 if (Present (gnat_attr_node
))
8002 post_error_ne ("Value_Size for& is too large", gnat_attr_node
,
8007 /* Ignore a zero size unless a Value_Size clause exists, or a size clause
8008 exists, or this is an integer type, in which case the front-end will
8009 have always set it. */
8010 if (No (gnat_attr_node
)
8011 && integer_zerop (size
)
8012 && !Has_Size_Clause (gnat_entity
)
8013 && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
8016 old_size
= rm_size (gnu_type
);
8018 /* If the old size is self-referential, get the maximum size. */
8019 if (CONTAINS_PLACEHOLDER_P (old_size
))
8020 old_size
= max_size (old_size
, true);
8022 /* Issue an error either if the old size of the object isn't a constant or
8023 if the new size is smaller than it. The front-end has already verified
8024 this for scalar and packed array types. */
8025 if (TREE_CODE (old_size
) != INTEGER_CST
8026 || TREE_OVERFLOW (old_size
)
8027 || (AGGREGATE_TYPE_P (gnu_type
)
8028 && !(TREE_CODE (gnu_type
) == ARRAY_TYPE
8029 && TYPE_PACKED_ARRAY_TYPE_P (gnu_type
))
8030 && !(TYPE_IS_PADDING_P (gnu_type
)
8031 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type
))) == ARRAY_TYPE
8032 && TYPE_PACKED_ARRAY_TYPE_P
8033 (TREE_TYPE (TYPE_FIELDS (gnu_type
))))
8034 && tree_int_cst_lt (size
, old_size
)))
8036 if (Present (gnat_attr_node
))
8038 ("Value_Size for& too small{, minimum allowed is ^}",
8039 gnat_attr_node
, gnat_entity
, old_size
);
8043 /* Otherwise, set the RM size proper for integral types... */
8044 if ((TREE_CODE (gnu_type
) == INTEGER_TYPE
8045 && Is_Discrete_Or_Fixed_Point_Type (gnat_entity
))
8046 || (TREE_CODE (gnu_type
) == ENUMERAL_TYPE
8047 || TREE_CODE (gnu_type
) == BOOLEAN_TYPE
))
8048 SET_TYPE_RM_SIZE (gnu_type
, size
);
8050 /* ...or the Ada size for record and union types. */
8051 else if (RECORD_OR_UNION_TYPE_P (gnu_type
)
8052 && !TYPE_FAT_POINTER_P (gnu_type
))
8053 SET_TYPE_ADA_SIZE (gnu_type
, size
);
8056 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
8057 a type or object whose present alignment is ALIGN. If this alignment is
8058 valid, return it. Otherwise, give an error and return ALIGN. */
8061 validate_alignment (Uint alignment
, Entity_Id gnat_entity
, unsigned int align
)
8063 unsigned int max_allowed_alignment
= get_target_maximum_allowed_alignment ();
8064 unsigned int new_align
;
8065 Node_Id gnat_error_node
;
8067 /* Don't worry about checking alignment if alignment was not specified
8068 by the source program and we already posted an error for this entity. */
8069 if (Error_Posted (gnat_entity
) && !Has_Alignment_Clause (gnat_entity
))
8072 /* Post the error on the alignment clause if any. Note, for the implicit
8073 base type of an array type, the alignment clause is on the first
8075 if (Present (Alignment_Clause (gnat_entity
)))
8076 gnat_error_node
= Expression (Alignment_Clause (gnat_entity
));
8078 else if (Is_Itype (gnat_entity
)
8079 && Is_Array_Type (gnat_entity
)
8080 && Etype (gnat_entity
) == gnat_entity
8081 && Present (Alignment_Clause (First_Subtype (gnat_entity
))))
8083 Expression (Alignment_Clause (First_Subtype (gnat_entity
)));
8086 gnat_error_node
= gnat_entity
;
8088 /* Within GCC, an alignment is an integer, so we must make sure a value is
8089 specified that fits in that range. Also, there is an upper bound to
8090 alignments we can support/allow. */
8091 if (!UI_Is_In_Int_Range (alignment
)
8092 || ((new_align
= UI_To_Int (alignment
)) > max_allowed_alignment
))
8093 post_error_ne_num ("largest supported alignment for& is ^",
8094 gnat_error_node
, gnat_entity
, max_allowed_alignment
);
8095 else if (!(Present (Alignment_Clause (gnat_entity
))
8096 && From_At_Mod (Alignment_Clause (gnat_entity
)))
8097 && new_align
* BITS_PER_UNIT
< align
)
8099 unsigned int double_align
;
8100 bool is_capped_double
, align_clause
;
8102 /* If the default alignment of "double" or larger scalar types is
8103 specifically capped and the new alignment is above the cap, do
8104 not post an error and change the alignment only if there is an
8105 alignment clause; this makes it possible to have the associated
8106 GCC type overaligned by default for performance reasons. */
8107 if ((double_align
= double_float_alignment
) > 0)
8110 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
8112 = is_double_float_or_array (gnat_type
, &align_clause
);
8114 else if ((double_align
= double_scalar_alignment
) > 0)
8117 = Is_Type (gnat_entity
) ? gnat_entity
: Etype (gnat_entity
);
8119 = is_double_scalar_or_array (gnat_type
, &align_clause
);
8122 is_capped_double
= align_clause
= false;
8124 if (is_capped_double
&& new_align
>= double_align
)
8127 align
= new_align
* BITS_PER_UNIT
;
8131 if (is_capped_double
)
8132 align
= double_align
* BITS_PER_UNIT
;
8134 post_error_ne_num ("alignment for& must be at least ^",
8135 gnat_error_node
, gnat_entity
,
8136 align
/ BITS_PER_UNIT
);
8141 new_align
= (new_align
> 0 ? new_align
* BITS_PER_UNIT
: 1);
8142 if (new_align
> align
)
8149 /* Verify that OBJECT, a type or decl, is something we can implement
8150 atomically. If not, give an error for GNAT_ENTITY. COMP_P is true
8151 if we require atomic components. */
8154 check_ok_for_atomic (tree object
, Entity_Id gnat_entity
, bool comp_p
)
8156 Node_Id gnat_error_point
= gnat_entity
;
8158 enum machine_mode mode
;
8162 /* There are three case of what OBJECT can be. It can be a type, in which
8163 case we take the size, alignment and mode from the type. It can be a
8164 declaration that was indirect, in which case the relevant values are
8165 that of the type being pointed to, or it can be a normal declaration,
8166 in which case the values are of the decl. The code below assumes that
8167 OBJECT is either a type or a decl. */
8168 if (TYPE_P (object
))
8170 /* If this is an anonymous base type, nothing to check. Error will be
8171 reported on the source type. */
8172 if (!Comes_From_Source (gnat_entity
))
8175 mode
= TYPE_MODE (object
);
8176 align
= TYPE_ALIGN (object
);
8177 size
= TYPE_SIZE (object
);
8179 else if (DECL_BY_REF_P (object
))
8181 mode
= TYPE_MODE (TREE_TYPE (TREE_TYPE (object
)));
8182 align
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object
)));
8183 size
= TYPE_SIZE (TREE_TYPE (TREE_TYPE (object
)));
8187 mode
= DECL_MODE (object
);
8188 align
= DECL_ALIGN (object
);
8189 size
= DECL_SIZE (object
);
8192 /* Consider all floating-point types atomic and any types that that are
8193 represented by integers no wider than a machine word. */
8194 if (GET_MODE_CLASS (mode
) == MODE_FLOAT
8195 || ((GET_MODE_CLASS (mode
) == MODE_INT
8196 || GET_MODE_CLASS (mode
) == MODE_PARTIAL_INT
)
8197 && GET_MODE_BITSIZE (mode
) <= BITS_PER_WORD
))
8200 /* For the moment, also allow anything that has an alignment equal
8201 to its size and which is smaller than a word. */
8202 if (size
&& TREE_CODE (size
) == INTEGER_CST
8203 && compare_tree_int (size
, align
) == 0
8204 && align
<= BITS_PER_WORD
)
8207 for (gnat_node
= First_Rep_Item (gnat_entity
); Present (gnat_node
);
8208 gnat_node
= Next_Rep_Item (gnat_node
))
8210 if (!comp_p
&& Nkind (gnat_node
) == N_Pragma
8211 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)))
8213 gnat_error_point
= First (Pragma_Argument_Associations (gnat_node
));
8214 else if (comp_p
&& Nkind (gnat_node
) == N_Pragma
8215 && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node
)))
8216 == Pragma_Atomic_Components
))
8217 gnat_error_point
= First (Pragma_Argument_Associations (gnat_node
));
8221 post_error_ne ("atomic access to component of & cannot be guaranteed",
8222 gnat_error_point
, gnat_entity
);
8224 post_error_ne ("atomic access to & cannot be guaranteed",
8225 gnat_error_point
, gnat_entity
);
8229 /* Helper for the intrin compatibility checks family. Evaluate whether
8230 two types are definitely incompatible. */
8233 intrin_types_incompatible_p (tree t1
, tree t2
)
8235 enum tree_code code
;
8237 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
8240 if (TYPE_MODE (t1
) != TYPE_MODE (t2
))
8243 if (TREE_CODE (t1
) != TREE_CODE (t2
))
8246 code
= TREE_CODE (t1
);
8252 return TYPE_PRECISION (t1
) != TYPE_PRECISION (t2
);
8255 case REFERENCE_TYPE
:
8256 /* Assume designated types are ok. We'd need to account for char * and
8257 void * variants to do better, which could rapidly get messy and isn't
8258 clearly worth the effort. */
8268 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8269 on the Ada/builtin argument lists for the INB binding. */
8272 intrin_arglists_compatible_p (intrin_binding_t
* inb
)
8274 function_args_iterator ada_iter
, btin_iter
;
8276 function_args_iter_init (&ada_iter
, inb
->ada_fntype
);
8277 function_args_iter_init (&btin_iter
, inb
->btin_fntype
);
8279 /* Sequence position of the last argument we checked. */
8284 tree ada_type
= function_args_iter_cond (&ada_iter
);
8285 tree btin_type
= function_args_iter_cond (&btin_iter
);
8287 /* If we've exhausted both lists simultaneously, we're done. */
8288 if (ada_type
== NULL_TREE
&& btin_type
== NULL_TREE
)
8291 /* If one list is shorter than the other, they fail to match. */
8292 if (ada_type
== NULL_TREE
|| btin_type
== NULL_TREE
)
8295 /* If we're done with the Ada args and not with the internal builtin
8296 args, or the other way around, complain. */
8297 if (ada_type
== void_type_node
8298 && btin_type
!= void_type_node
)
8300 post_error ("?Ada arguments list too short!", inb
->gnat_entity
);
8304 if (btin_type
== void_type_node
8305 && ada_type
!= void_type_node
)
8307 post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8308 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8312 /* Otherwise, check that types match for the current argument. */
8314 if (intrin_types_incompatible_p (ada_type
, btin_type
))
8316 post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8317 inb
->gnat_entity
, inb
->gnat_entity
, argpos
);
8322 function_args_iter_next (&ada_iter
);
8323 function_args_iter_next (&btin_iter
);
8329 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8330 on the Ada/builtin return values for the INB binding. */
8333 intrin_return_compatible_p (intrin_binding_t
* inb
)
8335 tree ada_return_type
= TREE_TYPE (inb
->ada_fntype
);
8336 tree btin_return_type
= TREE_TYPE (inb
->btin_fntype
);
8338 /* Accept function imported as procedure, common and convenient. */
8339 if (VOID_TYPE_P (ada_return_type
)
8340 && !VOID_TYPE_P (btin_return_type
))
8343 /* If return type is Address (integer type), map it to void *. */
8344 if (Is_Descendent_Of_Address (Etype (inb
->gnat_entity
)))
8345 ada_return_type
= ptr_void_type_node
;
8347 /* Check return types compatibility otherwise. Note that this
8348 handles void/void as well. */
8349 if (intrin_types_incompatible_p (btin_return_type
, ada_return_type
))
8351 post_error ("?intrinsic binding type mismatch on return value!",
8359 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8360 compatible. Issue relevant warnings when they are not.
8362 This is intended as a light check to diagnose the most obvious cases, not
8363 as a full fledged type compatibility predicate. It is the programmer's
8364 responsibility to ensure correctness of the Ada declarations in Imports,
8365 especially when binding straight to a compiler internal. */
8368 intrin_profiles_compatible_p (intrin_binding_t
* inb
)
8370 /* Check compatibility on return values and argument lists, each responsible
8371 for posting warnings as appropriate. Ensure use of the proper sloc for
8374 bool arglists_compatible_p
, return_compatible_p
;
8375 location_t saved_location
= input_location
;
8377 Sloc_to_locus (Sloc (inb
->gnat_entity
), &input_location
);
8379 return_compatible_p
= intrin_return_compatible_p (inb
);
8380 arglists_compatible_p
= intrin_arglists_compatible_p (inb
);
8382 input_location
= saved_location
;
8384 return return_compatible_p
&& arglists_compatible_p
;
8387 /* Return a FIELD_DECL node modeled on OLD_FIELD. FIELD_TYPE is its type
8388 and RECORD_TYPE is the type of the parent. If SIZE is nonzero, it is the
8389 specified size for this field. POS_LIST is a position list describing
8390 the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8394 create_field_decl_from (tree old_field
, tree field_type
, tree record_type
,
8395 tree size
, tree pos_list
,
8396 vec
<subst_pair
> subst_list
)
8398 tree t
= TREE_VALUE (purpose_member (old_field
, pos_list
));
8399 tree pos
= TREE_VEC_ELT (t
, 0), bitpos
= TREE_VEC_ELT (t
, 2);
8400 unsigned int offset_align
= tree_to_uhwi (TREE_VEC_ELT (t
, 1));
8401 tree new_pos
, new_field
;
8405 if (CONTAINS_PLACEHOLDER_P (pos
))
8406 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8407 pos
= SUBSTITUTE_IN_EXPR (pos
, s
->discriminant
, s
->replacement
);
8409 /* If the position is now a constant, we can set it as the position of the
8410 field when we make it. Otherwise, we need to deal with it specially. */
8411 if (TREE_CONSTANT (pos
))
8412 new_pos
= bit_from_pos (pos
, bitpos
);
8414 new_pos
= NULL_TREE
;
8417 = create_field_decl (DECL_NAME (old_field
), field_type
, record_type
,
8418 size
, new_pos
, DECL_PACKED (old_field
),
8419 !DECL_NONADDRESSABLE_P (old_field
));
8423 normalize_offset (&pos
, &bitpos
, offset_align
);
8424 /* Finalize the position. */
8425 DECL_FIELD_OFFSET (new_field
) = variable_size (pos
);
8426 DECL_FIELD_BIT_OFFSET (new_field
) = bitpos
;
8427 SET_DECL_OFFSET_ALIGN (new_field
, offset_align
);
8428 DECL_SIZE (new_field
) = size
;
8429 DECL_SIZE_UNIT (new_field
)
8430 = convert (sizetype
,
8431 size_binop (CEIL_DIV_EXPR
, size
, bitsize_unit_node
));
8432 layout_decl (new_field
, DECL_OFFSET_ALIGN (new_field
));
8435 DECL_INTERNAL_P (new_field
) = DECL_INTERNAL_P (old_field
);
8436 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, old_field
);
8437 DECL_DISCRIMINANT_NUMBER (new_field
) = DECL_DISCRIMINANT_NUMBER (old_field
);
8438 TREE_THIS_VOLATILE (new_field
) = TREE_THIS_VOLATILE (old_field
);
8443 /* Create the REP part of RECORD_TYPE with REP_TYPE. If MIN_SIZE is nonzero,
8444 it is the minimal size the REP_PART must have. */
8447 create_rep_part (tree rep_type
, tree record_type
, tree min_size
)
8451 if (min_size
&& !tree_int_cst_lt (TYPE_SIZE (rep_type
), min_size
))
8452 min_size
= NULL_TREE
;
8454 field
= create_field_decl (get_identifier ("REP"), rep_type
, record_type
,
8455 min_size
, NULL_TREE
, 0, 1);
8456 DECL_INTERNAL_P (field
) = 1;
8461 /* Return the REP part of RECORD_TYPE, if any. Otherwise return NULL. */
8464 get_rep_part (tree record_type
)
8466 tree field
= TYPE_FIELDS (record_type
);
8468 /* The REP part is the first field, internal, another record, and its name
8469 starts with an 'R'. */
8471 && DECL_INTERNAL_P (field
)
8472 && TREE_CODE (TREE_TYPE (field
)) == RECORD_TYPE
8473 && IDENTIFIER_POINTER (DECL_NAME (field
)) [0] == 'R')
8479 /* Return the variant part of RECORD_TYPE, if any. Otherwise return NULL. */
8482 get_variant_part (tree record_type
)
8486 /* The variant part is the only internal field that is a qualified union. */
8487 for (field
= TYPE_FIELDS (record_type
); field
; field
= DECL_CHAIN (field
))
8488 if (DECL_INTERNAL_P (field
)
8489 && TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
)
8495 /* Return a new variant part modeled on OLD_VARIANT_PART. VARIANT_LIST is
8496 the list of variants to be used and RECORD_TYPE is the type of the parent.
8497 POS_LIST is a position list describing the layout of fields present in
8498 OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8502 create_variant_part_from (tree old_variant_part
,
8503 vec
<variant_desc
> variant_list
,
8504 tree record_type
, tree pos_list
,
8505 vec
<subst_pair
> subst_list
)
8507 tree offset
= DECL_FIELD_OFFSET (old_variant_part
);
8508 tree old_union_type
= TREE_TYPE (old_variant_part
);
8509 tree new_union_type
, new_variant_part
;
8510 tree union_field_list
= NULL_TREE
;
8514 /* First create the type of the variant part from that of the old one. */
8515 new_union_type
= make_node (QUAL_UNION_TYPE
);
8516 TYPE_NAME (new_union_type
)
8517 = concat_name (TYPE_NAME (record_type
),
8518 IDENTIFIER_POINTER (DECL_NAME (old_variant_part
)));
8520 /* If the position of the variant part is constant, subtract it from the
8521 size of the type of the parent to get the new size. This manual CSE
8522 reduces the code size when not optimizing. */
8523 if (TREE_CODE (offset
) == INTEGER_CST
)
8525 tree bitpos
= DECL_FIELD_BIT_OFFSET (old_variant_part
);
8526 tree first_bit
= bit_from_pos (offset
, bitpos
);
8527 TYPE_SIZE (new_union_type
)
8528 = size_binop (MINUS_EXPR
, TYPE_SIZE (record_type
), first_bit
);
8529 TYPE_SIZE_UNIT (new_union_type
)
8530 = size_binop (MINUS_EXPR
, TYPE_SIZE_UNIT (record_type
),
8531 byte_from_pos (offset
, bitpos
));
8532 SET_TYPE_ADA_SIZE (new_union_type
,
8533 size_binop (MINUS_EXPR
, TYPE_ADA_SIZE (record_type
),
8535 TYPE_ALIGN (new_union_type
) = TYPE_ALIGN (old_union_type
);
8536 relate_alias_sets (new_union_type
, old_union_type
, ALIAS_SET_COPY
);
8539 copy_and_substitute_in_size (new_union_type
, old_union_type
, subst_list
);
8541 /* Now finish up the new variants and populate the union type. */
8542 FOR_EACH_VEC_ELT_REVERSE (variant_list
, i
, v
)
8544 tree old_field
= v
->field
, new_field
;
8545 tree old_variant
, old_variant_subpart
, new_variant
, field_list
;
8547 /* Skip variants that don't belong to this nesting level. */
8548 if (DECL_CONTEXT (old_field
) != old_union_type
)
8551 /* Retrieve the list of fields already added to the new variant. */
8552 new_variant
= v
->new_type
;
8553 field_list
= TYPE_FIELDS (new_variant
);
8555 /* If the old variant had a variant subpart, we need to create a new
8556 variant subpart and add it to the field list. */
8557 old_variant
= v
->type
;
8558 old_variant_subpart
= get_variant_part (old_variant
);
8559 if (old_variant_subpart
)
8561 tree new_variant_subpart
8562 = create_variant_part_from (old_variant_subpart
, variant_list
,
8563 new_variant
, pos_list
, subst_list
);
8564 DECL_CHAIN (new_variant_subpart
) = field_list
;
8565 field_list
= new_variant_subpart
;
8568 /* Finish up the new variant and create the field. No need for debug
8569 info thanks to the XVS type. */
8570 finish_record_type (new_variant
, nreverse (field_list
), 2, false);
8571 compute_record_mode (new_variant
);
8572 create_type_decl (TYPE_NAME (new_variant
), new_variant
, true, false,
8576 = create_field_decl_from (old_field
, new_variant
, new_union_type
,
8577 TYPE_SIZE (new_variant
),
8578 pos_list
, subst_list
);
8579 DECL_QUALIFIER (new_field
) = v
->qual
;
8580 DECL_INTERNAL_P (new_field
) = 1;
8581 DECL_CHAIN (new_field
) = union_field_list
;
8582 union_field_list
= new_field
;
8585 /* Finish up the union type and create the variant part. No need for debug
8586 info thanks to the XVS type. Note that we don't reverse the field list
8587 because VARIANT_LIST has been traversed in reverse order. */
8588 finish_record_type (new_union_type
, union_field_list
, 2, false);
8589 compute_record_mode (new_union_type
);
8590 create_type_decl (TYPE_NAME (new_union_type
), new_union_type
, true, false,
8594 = create_field_decl_from (old_variant_part
, new_union_type
, record_type
,
8595 TYPE_SIZE (new_union_type
),
8596 pos_list
, subst_list
);
8597 DECL_INTERNAL_P (new_variant_part
) = 1;
8599 /* With multiple discriminants it is possible for an inner variant to be
8600 statically selected while outer ones are not; in this case, the list
8601 of fields of the inner variant is not flattened and we end up with a
8602 qualified union with a single member. Drop the useless container. */
8603 if (!DECL_CHAIN (union_field_list
))
8605 DECL_CONTEXT (union_field_list
) = record_type
;
8606 DECL_FIELD_OFFSET (union_field_list
)
8607 = DECL_FIELD_OFFSET (new_variant_part
);
8608 DECL_FIELD_BIT_OFFSET (union_field_list
)
8609 = DECL_FIELD_BIT_OFFSET (new_variant_part
);
8610 SET_DECL_OFFSET_ALIGN (union_field_list
,
8611 DECL_OFFSET_ALIGN (new_variant_part
));
8612 new_variant_part
= union_field_list
;
8615 return new_variant_part
;
8618 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8619 which are both RECORD_TYPE, after applying the substitutions described
8623 copy_and_substitute_in_size (tree new_type
, tree old_type
,
8624 vec
<subst_pair
> subst_list
)
8629 TYPE_SIZE (new_type
) = TYPE_SIZE (old_type
);
8630 TYPE_SIZE_UNIT (new_type
) = TYPE_SIZE_UNIT (old_type
);
8631 SET_TYPE_ADA_SIZE (new_type
, TYPE_ADA_SIZE (old_type
));
8632 TYPE_ALIGN (new_type
) = TYPE_ALIGN (old_type
);
8633 relate_alias_sets (new_type
, old_type
, ALIAS_SET_COPY
);
8635 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type
)))
8636 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8637 TYPE_SIZE (new_type
)
8638 = SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type
),
8639 s
->discriminant
, s
->replacement
);
8641 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type
)))
8642 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8643 TYPE_SIZE_UNIT (new_type
)
8644 = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type
),
8645 s
->discriminant
, s
->replacement
);
8647 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type
)))
8648 FOR_EACH_VEC_ELT (subst_list
, i
, s
)
8650 (new_type
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type
),
8651 s
->discriminant
, s
->replacement
));
8653 /* Finalize the size. */
8654 TYPE_SIZE (new_type
) = variable_size (TYPE_SIZE (new_type
));
8655 TYPE_SIZE_UNIT (new_type
) = variable_size (TYPE_SIZE_UNIT (new_type
));
8658 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8659 type with all size expressions that contain F in a PLACEHOLDER_EXPR
8660 updated by replacing F with R.
8662 The function doesn't update the layout of the type, i.e. it assumes
8663 that the substitution is purely formal. That's why the replacement
8664 value R must itself contain a PLACEHOLDER_EXPR. */
8667 substitute_in_type (tree t
, tree f
, tree r
)
8671 gcc_assert (CONTAINS_PLACEHOLDER_P (r
));
8673 switch (TREE_CODE (t
))
8680 /* First the domain types of arrays. */
8681 if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t
))
8682 || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t
)))
8684 tree low
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t
), f
, r
);
8685 tree high
= SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t
), f
, r
);
8687 if (low
== TYPE_GCC_MIN_VALUE (t
) && high
== TYPE_GCC_MAX_VALUE (t
))
8691 TYPE_GCC_MIN_VALUE (nt
) = low
;
8692 TYPE_GCC_MAX_VALUE (nt
) = high
;
8694 if (TREE_CODE (t
) == INTEGER_TYPE
&& TYPE_INDEX_TYPE (t
))
8696 (nt
, substitute_in_type (TYPE_INDEX_TYPE (t
), f
, r
));
8701 /* Then the subtypes. */
8702 if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t
))
8703 || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t
)))
8705 tree low
= SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t
), f
, r
);
8706 tree high
= SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t
), f
, r
);
8708 if (low
== TYPE_RM_MIN_VALUE (t
) && high
== TYPE_RM_MAX_VALUE (t
))
8712 SET_TYPE_RM_MIN_VALUE (nt
, low
);
8713 SET_TYPE_RM_MAX_VALUE (nt
, high
);
8721 nt
= substitute_in_type (TREE_TYPE (t
), f
, r
);
8722 if (nt
== TREE_TYPE (t
))
8725 return build_complex_type (nt
);
8728 /* These should never show up here. */
8733 tree component
= substitute_in_type (TREE_TYPE (t
), f
, r
);
8734 tree domain
= substitute_in_type (TYPE_DOMAIN (t
), f
, r
);
8736 if (component
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
8739 nt
= build_nonshared_array_type (component
, domain
);
8740 TYPE_ALIGN (nt
) = TYPE_ALIGN (t
);
8741 TYPE_USER_ALIGN (nt
) = TYPE_USER_ALIGN (t
);
8742 SET_TYPE_MODE (nt
, TYPE_MODE (t
));
8743 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
8744 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
8745 TYPE_NONALIASED_COMPONENT (nt
) = TYPE_NONALIASED_COMPONENT (t
);
8746 TYPE_MULTI_ARRAY_P (nt
) = TYPE_MULTI_ARRAY_P (t
);
8747 TYPE_CONVENTION_FORTRAN_P (nt
) = TYPE_CONVENTION_FORTRAN_P (t
);
8753 case QUAL_UNION_TYPE
:
8755 bool changed_field
= false;
8758 /* Start out with no fields, make new fields, and chain them
8759 in. If we haven't actually changed the type of any field,
8760 discard everything we've done and return the old type. */
8762 TYPE_FIELDS (nt
) = NULL_TREE
;
8764 for (field
= TYPE_FIELDS (t
); field
; field
= DECL_CHAIN (field
))
8766 tree new_field
= copy_node (field
), new_n
;
8768 new_n
= substitute_in_type (TREE_TYPE (field
), f
, r
);
8769 if (new_n
!= TREE_TYPE (field
))
8771 TREE_TYPE (new_field
) = new_n
;
8772 changed_field
= true;
8775 new_n
= SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field
), f
, r
);
8776 if (new_n
!= DECL_FIELD_OFFSET (field
))
8778 DECL_FIELD_OFFSET (new_field
) = new_n
;
8779 changed_field
= true;
8782 /* Do the substitution inside the qualifier, if any. */
8783 if (TREE_CODE (t
) == QUAL_UNION_TYPE
)
8785 new_n
= SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field
), f
, r
);
8786 if (new_n
!= DECL_QUALIFIER (field
))
8788 DECL_QUALIFIER (new_field
) = new_n
;
8789 changed_field
= true;
8793 DECL_CONTEXT (new_field
) = nt
;
8794 SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field
, field
);
8796 DECL_CHAIN (new_field
) = TYPE_FIELDS (nt
);
8797 TYPE_FIELDS (nt
) = new_field
;
8803 TYPE_FIELDS (nt
) = nreverse (TYPE_FIELDS (nt
));
8804 TYPE_SIZE (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t
), f
, r
);
8805 TYPE_SIZE_UNIT (nt
) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t
), f
, r
);
8806 SET_TYPE_ADA_SIZE (nt
, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t
), f
, r
));
8815 /* Return the RM size of GNU_TYPE. This is the actual number of bits
8816 needed to represent the object. */
8819 rm_size (tree gnu_type
)
8821 /* For integral types, we store the RM size explicitly. */
8822 if (INTEGRAL_TYPE_P (gnu_type
) && TYPE_RM_SIZE (gnu_type
))
8823 return TYPE_RM_SIZE (gnu_type
);
8825 /* Return the RM size of the actual data plus the size of the template. */
8826 if (TREE_CODE (gnu_type
) == RECORD_TYPE
8827 && TYPE_CONTAINS_TEMPLATE_P (gnu_type
))
8829 size_binop (PLUS_EXPR
,
8830 rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type
)))),
8831 DECL_SIZE (TYPE_FIELDS (gnu_type
)));
8833 /* For record or union types, we store the size explicitly. */
8834 if (RECORD_OR_UNION_TYPE_P (gnu_type
)
8835 && !TYPE_FAT_POINTER_P (gnu_type
)
8836 && TYPE_ADA_SIZE (gnu_type
))
8837 return TYPE_ADA_SIZE (gnu_type
);
8839 /* For other types, this is just the size. */
8840 return TYPE_SIZE (gnu_type
);
8843 /* Return the name to be used for GNAT_ENTITY. If a type, create a
8844 fully-qualified name, possibly with type information encoding.
8845 Otherwise, return the name. */
8848 get_entity_name (Entity_Id gnat_entity
)
8850 Get_Encoded_Name (gnat_entity
);
8851 return get_identifier_with_length (Name_Buffer
, Name_Len
);
8854 /* Return an identifier representing the external name to be used for
8855 GNAT_ENTITY. If SUFFIX is specified, the name is followed by "___"
8856 and the specified suffix. */
8859 create_concat_name (Entity_Id gnat_entity
, const char *suffix
)
8861 const Entity_Kind kind
= Ekind (gnat_entity
);
8862 const bool has_suffix
= (suffix
!= NULL
);
8863 String_Template temp
= {1, has_suffix
? strlen (suffix
) : 0};
8864 String_Pointer sp
= {suffix
, &temp
};
8866 Get_External_Name (gnat_entity
, has_suffix
, sp
);
8868 /* A variable using the Stdcall convention lives in a DLL. We adjust
8869 its name to use the jump table, the _imp__NAME contains the address
8870 for the NAME variable. */
8871 if ((kind
== E_Variable
|| kind
== E_Constant
)
8872 && Has_Stdcall_Convention (gnat_entity
))
8874 const int len
= strlen (STDCALL_PREFIX
) + Name_Len
;
8875 char *new_name
= (char *) alloca (len
+ 1);
8876 strcpy (new_name
, STDCALL_PREFIX
);
8877 strcat (new_name
, Name_Buffer
);
8878 return get_identifier_with_length (new_name
, len
);
8881 return get_identifier_with_length (Name_Buffer
, Name_Len
);
8884 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8885 string, return a new IDENTIFIER_NODE that is the concatenation of
8886 the name followed by "___" and the specified suffix. */
8889 concat_name (tree gnu_name
, const char *suffix
)
8891 const int len
= IDENTIFIER_LENGTH (gnu_name
) + 3 + strlen (suffix
);
8892 char *new_name
= (char *) alloca (len
+ 1);
8893 strcpy (new_name
, IDENTIFIER_POINTER (gnu_name
));
8894 strcat (new_name
, "___");
8895 strcat (new_name
, suffix
);
8896 return get_identifier_with_length (new_name
, len
);
8899 /* Initialize data structures of the decl.c module. */
8902 init_gnat_decl (void)
8904 /* Initialize the cache of annotated values. */
8905 annotate_value_cache
8906 = htab_create_ggc (512, tree_int_map_hash
, tree_int_map_eq
, 0);
8909 /* Destroy data structures of the decl.c module. */
8912 destroy_gnat_decl (void)
8914 /* Destroy the cache of annotated values. */
8915 htab_delete (annotate_value_cache
);
8916 annotate_value_cache
= NULL
;
8919 #include "gt-ada-decl.h"