1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2009, 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"
53 #ifndef MAX_FIXED_MODE_SIZE
54 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
57 /* Convention_Stdcall should be processed in a specific way on Windows targets
58 only. The macro below is a helper to avoid having to check for a Windows
59 specific attribute throughout this unit. */
61 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
62 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
64 #define Has_Stdcall_Convention(E) (0)
67 /* Stack realignment for functions with foreign conventions is provided on a
68 per back-end basis now, as it is handled by the prologue expanders and not
69 as part of the function's body any more. It might be requested by way of a
70 dedicated function type attribute on the targets that support it.
72 We need a way to avoid setting the attribute on the targets that don't
73 support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
75 It is defined on targets where the circuitry is available, and indicates
76 whether the realignment is needed for 'main'. We use this to decide for
77 foreign subprograms as well.
79 It is not defined on targets where the circuitry is not implemented, and
80 we just never set the attribute in these cases.
82 Whether it is defined on all targets that would need it in theory is
83 not entirely clear. We currently trust the base GCC settings for this
86 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
87 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
92 struct incomplete *next;
97 /* These variables are used to defer recursively expanding incomplete types
98 while we are processing an array, a record or a subprogram type. */
99 static int defer_incomplete_level = 0;
100 static struct incomplete *defer_incomplete_list;
102 /* This variable is used to delay expanding From_With_Type types until the
104 static struct incomplete *defer_limited_with;
106 /* These variables are used to defer finalizing types. The element of the
107 list is the TYPE_DECL associated with the type. */
108 static int defer_finalize_level = 0;
109 static VEC (tree,heap) *defer_finalize_list;
111 /* A hash table used to cache the result of annotate_value. */
112 static GTY ((if_marked ("tree_int_map_marked_p"),
113 param_is (struct tree_int_map))) htab_t annotate_value_cache;
122 static void relate_alias_sets (tree, tree, enum alias_set_op);
124 static tree build_subst_list (Entity_Id, Entity_Id, bool);
125 static bool allocatable_size_p (tree, bool);
126 static void prepend_one_attribute_to (struct attrib **,
127 enum attr_type, tree, tree, Node_Id);
128 static void prepend_attributes (Entity_Id, struct attrib **);
129 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
130 static bool is_variable_size (tree);
131 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
132 static tree make_packable_type (tree, bool);
133 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
134 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
136 static bool same_discriminant_p (Entity_Id, Entity_Id);
137 static bool array_type_has_nonaliased_component (Entity_Id, tree);
138 static bool compile_time_known_address_p (Node_Id);
139 static bool cannot_be_superflat_p (Node_Id);
140 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
141 bool, bool, bool, bool);
142 static Uint annotate_value (tree);
143 static void annotate_rep (Entity_Id, tree);
144 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
145 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
146 static void set_rm_size (Uint, tree, Entity_Id);
147 static tree make_type_from_size (tree, tree, bool);
148 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
149 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
150 static void check_ok_for_atomic (tree, Entity_Id, bool);
151 static int compatible_signatures_p (tree ftype1, tree ftype2);
152 static void rest_of_type_decl_compilation_no_defer (tree);
154 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
155 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
156 and associate the ..._DECL node with the input GNAT defining identifier.
158 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
159 initial value (in GCC tree form). This is optional for a variable. For
160 a renamed entity, GNU_EXPR gives the object being renamed.
162 DEFINITION is nonzero if this call is intended for a definition. This is
163 used for separate compilation where it is necessary to know whether an
164 external declaration or a definition must be created if the GCC equivalent
165 was not created previously. The value of 1 is normally used for a nonzero
166 DEFINITION, but a value of 2 is used in special circumstances, defined in
170 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
172 /* Contains the kind of the input GNAT node. */
173 const Entity_Kind kind = Ekind (gnat_entity);
174 /* True if this is a type. */
175 const bool is_type = IN (kind, Type_Kind);
176 /* For a type, contains the equivalent GNAT node to be used in gigi. */
177 Entity_Id gnat_equiv_type = Empty;
178 /* Temporary used to walk the GNAT tree. */
180 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
181 This node will be associated with the GNAT node by calling at the end
182 of the `switch' statement. */
183 tree gnu_decl = NULL_TREE;
184 /* Contains the GCC type to be used for the GCC node. */
185 tree gnu_type = NULL_TREE;
186 /* Contains the GCC size tree to be used for the GCC node. */
187 tree gnu_size = NULL_TREE;
188 /* Contains the GCC name to be used for the GCC node. */
189 tree gnu_entity_name;
190 /* True if we have already saved gnu_decl as a GNAT association. */
192 /* True if we incremented defer_incomplete_level. */
193 bool this_deferred = false;
194 /* True if we incremented force_global. */
195 bool this_global = false;
196 /* True if we should check to see if elaborated during processing. */
197 bool maybe_present = false;
198 /* True if we made GNU_DECL and its type here. */
199 bool this_made_decl = false;
200 /* True if debug info is requested for this entity. */
201 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
202 || debug_info_level == DINFO_LEVEL_VERBOSE);
203 /* True if this entity is to be considered as imported. */
204 bool imported_p = (Is_Imported (gnat_entity)
205 && No (Address_Clause (gnat_entity)));
206 /* Size and alignment of the GCC node, if meaningful. */
207 unsigned int esize = 0, align = 0;
208 /* Contains the list of attributes directly attached to the entity. */
209 struct attrib *attr_list = NULL;
211 /* Since a use of an Itype is a definition, process it as such if it
212 is not in a with'ed unit. */
215 && Is_Itype (gnat_entity)
216 && !present_gnu_tree (gnat_entity)
217 && In_Extended_Main_Code_Unit (gnat_entity))
219 /* Ensure that we are in a subprogram mentioned in the Scope chain of
220 this entity, our current scope is global, or we encountered a task
221 or entry (where we can't currently accurately check scoping). */
222 if (!current_function_decl
223 || DECL_ELABORATION_PROC_P (current_function_decl))
225 process_type (gnat_entity);
226 return get_gnu_tree (gnat_entity);
229 for (gnat_temp = Scope (gnat_entity);
231 gnat_temp = Scope (gnat_temp))
233 if (Is_Type (gnat_temp))
234 gnat_temp = Underlying_Type (gnat_temp);
236 if (Ekind (gnat_temp) == E_Subprogram_Body)
238 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
240 if (IN (Ekind (gnat_temp), Subprogram_Kind)
241 && Present (Protected_Body_Subprogram (gnat_temp)))
242 gnat_temp = Protected_Body_Subprogram (gnat_temp);
244 if (Ekind (gnat_temp) == E_Entry
245 || Ekind (gnat_temp) == E_Entry_Family
246 || Ekind (gnat_temp) == E_Task_Type
247 || (IN (Ekind (gnat_temp), Subprogram_Kind)
248 && present_gnu_tree (gnat_temp)
249 && (current_function_decl
250 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
252 process_type (gnat_entity);
253 return get_gnu_tree (gnat_entity);
257 /* This abort means the Itype has an incorrect scope, i.e. that its
258 scope does not correspond to the subprogram it is declared in. */
262 /* If we've already processed this entity, return what we got last time.
263 If we are defining the node, we should not have already processed it.
264 In that case, we will abort below when we try to save a new GCC tree
265 for this object. We also need to handle the case of getting a dummy
266 type when a Full_View exists. */
267 if ((!definition || (is_type && imported_p))
268 && present_gnu_tree (gnat_entity))
270 gnu_decl = get_gnu_tree (gnat_entity);
272 if (TREE_CODE (gnu_decl) == TYPE_DECL
273 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
274 && IN (kind, Incomplete_Or_Private_Kind)
275 && Present (Full_View (gnat_entity)))
278 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
279 save_gnu_tree (gnat_entity, NULL_TREE, false);
280 save_gnu_tree (gnat_entity, gnu_decl, false);
286 /* If this is a numeric or enumeral type, or an access type, a nonzero
287 Esize must be specified unless it was specified by the programmer. */
288 gcc_assert (!Unknown_Esize (gnat_entity)
289 || Has_Size_Clause (gnat_entity)
290 || (!IN (kind, Numeric_Kind)
291 && !IN (kind, Enumeration_Kind)
292 && (!IN (kind, Access_Kind)
293 || kind == E_Access_Protected_Subprogram_Type
294 || kind == E_Anonymous_Access_Protected_Subprogram_Type
295 || kind == E_Access_Subtype)));
297 /* The RM size must be specified for all discrete and fixed-point types. */
298 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
299 && Unknown_RM_Size (gnat_entity)));
301 /* If we get here, it means we have not yet done anything with this entity.
302 If we are not defining it, it must be a type or an entity that is defined
303 elsewhere or externally, otherwise we should have defined it already. */
304 gcc_assert (definition
305 || type_annotate_only
307 || kind == E_Discriminant
308 || kind == E_Component
310 || (kind == E_Constant && Present (Full_View (gnat_entity)))
311 || Is_Public (gnat_entity));
313 /* Get the name of the entity and set up the line number and filename of
314 the original definition for use in any decl we make. */
315 gnu_entity_name = get_entity_name (gnat_entity);
316 Sloc_to_locus (Sloc (gnat_entity), &input_location);
318 /* For cases when we are not defining (i.e., we are referencing from
319 another compilation unit) public entities, show we are at global level
320 for the purpose of computing scopes. Don't do this for components or
321 discriminants since the relevant test is whether or not the record is
324 && kind != E_Component
325 && kind != E_Discriminant
326 && Is_Public (gnat_entity)
327 && !Is_Statically_Allocated (gnat_entity))
328 force_global++, this_global = true;
330 /* Handle any attributes directly attached to the entity. */
331 if (Has_Gigi_Rep_Item (gnat_entity))
332 prepend_attributes (gnat_entity, &attr_list);
334 /* Do some common processing for types. */
337 /* Compute the equivalent type to be used in gigi. */
338 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
340 /* Machine_Attributes on types are expected to be propagated to
341 subtypes. The corresponding Gigi_Rep_Items are only attached
342 to the first subtype though, so we handle the propagation here. */
343 if (Base_Type (gnat_entity) != gnat_entity
344 && !Is_First_Subtype (gnat_entity)
345 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
346 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
349 /* Compute a default value for the size of the type. */
350 if (Known_Esize (gnat_entity)
351 && UI_Is_In_Int_Range (Esize (gnat_entity)))
353 unsigned int max_esize;
354 esize = UI_To_Int (Esize (gnat_entity));
356 if (IN (kind, Float_Kind))
357 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
358 else if (IN (kind, Access_Kind))
359 max_esize = POINTER_SIZE * 2;
361 max_esize = LONG_LONG_TYPE_SIZE;
363 if (esize > max_esize)
367 esize = LONG_LONG_TYPE_SIZE;
373 /* If this is a use of a deferred constant without address clause,
374 get its full definition. */
376 && No (Address_Clause (gnat_entity))
377 && Present (Full_View (gnat_entity)))
380 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
385 /* If we have an external constant that we are not defining, get the
386 expression that is was defined to represent. We may throw that
387 expression away later if it is not a constant. Do not retrieve the
388 expression if it is an aggregate or allocator, because in complex
389 instantiation contexts it may not be expanded */
391 && Present (Expression (Declaration_Node (gnat_entity)))
392 && !No_Initialization (Declaration_Node (gnat_entity))
393 && (Nkind (Expression (Declaration_Node (gnat_entity)))
395 && (Nkind (Expression (Declaration_Node (gnat_entity)))
397 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
399 /* Ignore deferred constant definitions without address clause since
400 they are processed fully in the front-end. If No_Initialization
401 is set, this is not a deferred constant but a constant whose value
402 is built manually. And constants that are renamings are handled
406 && No (Address_Clause (gnat_entity))
407 && !No_Initialization (Declaration_Node (gnat_entity))
408 && No (Renamed_Object (gnat_entity)))
410 gnu_decl = error_mark_node;
415 /* Ignore constant definitions already marked with the error node. See
416 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
419 && present_gnu_tree (gnat_entity)
420 && get_gnu_tree (gnat_entity) == error_mark_node)
422 maybe_present = true;
429 /* We used to special case VMS exceptions here to directly map them to
430 their associated condition code. Since this code had to be masked
431 dynamically to strip off the severity bits, this caused trouble in
432 the GCC/ZCX case because the "type" pointers we store in the tables
433 have to be static. We now don't special case here anymore, and let
434 the regular processing take place, which leaves us with a regular
435 exception data object for VMS exceptions too. The condition code
436 mapping is taken care of by the front end and the bitmasking by the
443 /* The GNAT record where the component was defined. */
444 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
446 /* If the variable is an inherited record component (in the case of
447 extended record types), just return the inherited entity, which
448 must be a FIELD_DECL. Likewise for discriminants.
449 For discriminants of untagged records which have explicit
450 stored discriminants, return the entity for the corresponding
451 stored discriminant. Also use Original_Record_Component
452 if the record has a private extension. */
453 if (Present (Original_Record_Component (gnat_entity))
454 && Original_Record_Component (gnat_entity) != gnat_entity)
457 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
458 gnu_expr, definition);
463 /* If the enclosing record has explicit stored discriminants,
464 then it is an untagged record. If the Corresponding_Discriminant
465 is not empty then this must be a renamed discriminant and its
466 Original_Record_Component must point to the corresponding explicit
467 stored discriminant (i.e. we should have taken the previous
469 else if (Present (Corresponding_Discriminant (gnat_entity))
470 && Is_Tagged_Type (gnat_record))
472 /* A tagged record has no explicit stored discriminants. */
473 gcc_assert (First_Discriminant (gnat_record)
474 == First_Stored_Discriminant (gnat_record));
476 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
477 gnu_expr, definition);
482 else if (Present (CR_Discriminant (gnat_entity))
483 && type_annotate_only)
485 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
486 gnu_expr, definition);
491 /* If the enclosing record has explicit stored discriminants, then
492 it is an untagged record. If the Corresponding_Discriminant
493 is not empty then this must be a renamed discriminant and its
494 Original_Record_Component must point to the corresponding explicit
495 stored discriminant (i.e. we should have taken the first
497 else if (Present (Corresponding_Discriminant (gnat_entity))
498 && (First_Discriminant (gnat_record)
499 != First_Stored_Discriminant (gnat_record)))
502 /* Otherwise, if we are not defining this and we have no GCC type
503 for the containing record, make one for it. Then we should
504 have made our own equivalent. */
505 else if (!definition && !present_gnu_tree (gnat_record))
507 /* ??? If this is in a record whose scope is a protected
508 type and we have an Original_Record_Component, use it.
509 This is a workaround for major problems in protected type
511 Entity_Id Scop = Scope (Scope (gnat_entity));
512 if ((Is_Protected_Type (Scop)
513 || (Is_Private_Type (Scop)
514 && Present (Full_View (Scop))
515 && Is_Protected_Type (Full_View (Scop))))
516 && Present (Original_Record_Component (gnat_entity)))
519 = gnat_to_gnu_entity (Original_Record_Component
526 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
527 gnu_decl = get_gnu_tree (gnat_entity);
533 /* Here we have no GCC type and this is a reference rather than a
534 definition. This should never happen. Most likely the cause is
535 reference before declaration in the gnat tree for gnat_entity. */
539 case E_Loop_Parameter:
540 case E_Out_Parameter:
543 /* Simple variables, loop variables, Out parameters, and exceptions. */
546 bool used_by_ref = false;
548 = ((kind == E_Constant || kind == E_Variable)
549 && Is_True_Constant (gnat_entity)
550 && !Treat_As_Volatile (gnat_entity)
551 && (((Nkind (Declaration_Node (gnat_entity))
552 == N_Object_Declaration)
553 && Present (Expression (Declaration_Node (gnat_entity))))
554 || Present (Renamed_Object (gnat_entity))));
555 bool inner_const_flag = const_flag;
556 bool static_p = Is_Statically_Allocated (gnat_entity);
557 bool mutable_p = false;
558 tree gnu_ext_name = NULL_TREE;
559 tree renamed_obj = NULL_TREE;
560 tree gnu_object_size;
562 if (Present (Renamed_Object (gnat_entity)) && !definition)
564 if (kind == E_Exception)
565 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
568 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
571 /* Get the type after elaborating the renamed object. */
572 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
574 /* For a debug renaming declaration, build a pure debug entity. */
575 if (Present (Debug_Renaming_Link (gnat_entity)))
578 gnu_decl = build_decl (input_location,
579 VAR_DECL, gnu_entity_name, gnu_type);
580 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
581 if (global_bindings_p ())
582 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
584 addr = stack_pointer_rtx;
585 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
586 gnat_pushdecl (gnu_decl, gnat_entity);
590 /* If this is a loop variable, its type should be the base type.
591 This is because the code for processing a loop determines whether
592 a normal loop end test can be done by comparing the bounds of the
593 loop against those of the base type, which is presumed to be the
594 size used for computation. But this is not correct when the size
595 of the subtype is smaller than the type. */
596 if (kind == E_Loop_Parameter)
597 gnu_type = get_base_type (gnu_type);
599 /* Reject non-renamed objects whose types are unconstrained arrays or
600 any object whose type is a dummy type or VOID_TYPE. */
602 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
603 && No (Renamed_Object (gnat_entity)))
604 || TYPE_IS_DUMMY_P (gnu_type)
605 || TREE_CODE (gnu_type) == VOID_TYPE)
607 gcc_assert (type_annotate_only);
610 return error_mark_node;
613 /* If an alignment is specified, use it if valid. Note that exceptions
614 are objects but don't have an alignment. We must do this before we
615 validate the size, since the alignment can affect the size. */
616 if (kind != E_Exception && Known_Alignment (gnat_entity))
618 gcc_assert (Present (Alignment (gnat_entity)));
619 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
620 TYPE_ALIGN (gnu_type));
621 /* No point in changing the type if there is an address clause
622 as the final type of the object will be a reference type. */
623 if (Present (Address_Clause (gnat_entity)))
627 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
628 "PAD", false, definition, true);
631 /* If we are defining the object, see if it has a Size value and
632 validate it if so. If we are not defining the object and a Size
633 clause applies, simply retrieve the value. We don't want to ignore
634 the clause and it is expected to have been validated already. Then
635 get the new type, if any. */
637 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
638 gnat_entity, VAR_DECL, false,
639 Has_Size_Clause (gnat_entity));
640 else if (Has_Size_Clause (gnat_entity))
641 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
646 = make_type_from_size (gnu_type, gnu_size,
647 Has_Biased_Representation (gnat_entity));
649 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
650 gnu_size = NULL_TREE;
653 /* If this object has self-referential size, it must be a record with
654 a default value. We are supposed to allocate an object of the
655 maximum size in this case unless it is a constant with an
656 initializing expression, in which case we can get the size from
657 that. Note that the resulting size may still be a variable, so
658 this may end up with an indirect allocation. */
659 if (No (Renamed_Object (gnat_entity))
660 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
662 if (gnu_expr && kind == E_Constant)
664 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
665 if (CONTAINS_PLACEHOLDER_P (size))
667 /* If the initializing expression is itself a constant,
668 despite having a nominal type with self-referential
669 size, we can get the size directly from it. */
670 if (TREE_CODE (gnu_expr) == COMPONENT_REF
671 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
674 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
675 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
676 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
677 || DECL_READONLY_ONCE_ELAB
678 (TREE_OPERAND (gnu_expr, 0))))
679 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
682 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
687 /* We may have no GNU_EXPR because No_Initialization is
688 set even though there's an Expression. */
689 else if (kind == E_Constant
690 && (Nkind (Declaration_Node (gnat_entity))
691 == N_Object_Declaration)
692 && Present (Expression (Declaration_Node (gnat_entity))))
694 = TYPE_SIZE (gnat_to_gnu_type
696 (Expression (Declaration_Node (gnat_entity)))));
699 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
704 /* If the size is zero bytes, make it one byte since some linkers have
705 trouble with zero-sized objects. If the object will have a
706 template, that will make it nonzero so don't bother. Also avoid
707 doing that for an object renaming or an object with an address
708 clause, as we would lose useful information on the view size
709 (e.g. for null array slices) and we are not allocating the object
712 && integer_zerop (gnu_size)
713 && !TREE_OVERFLOW (gnu_size))
714 || (TYPE_SIZE (gnu_type)
715 && integer_zerop (TYPE_SIZE (gnu_type))
716 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
717 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
718 || !Is_Array_Type (Etype (gnat_entity)))
719 && No (Renamed_Object (gnat_entity))
720 && No (Address_Clause (gnat_entity)))
721 gnu_size = bitsize_unit_node;
723 /* If this is an object with no specified size and alignment, and
724 if either it is atomic or we are not optimizing alignment for
725 space and it is composite and not an exception, an Out parameter
726 or a reference to another object, and the size of its type is a
727 constant, set the alignment to the smallest one which is not
728 smaller than the size, with an appropriate cap. */
729 if (!gnu_size && align == 0
730 && (Is_Atomic (gnat_entity)
731 || (!Optimize_Alignment_Space (gnat_entity)
732 && kind != E_Exception
733 && kind != E_Out_Parameter
734 && Is_Composite_Type (Etype (gnat_entity))
735 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
737 && No (Renamed_Object (gnat_entity))
738 && No (Address_Clause (gnat_entity))))
739 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
741 /* No point in jumping through all the hoops needed in order
742 to support BIGGEST_ALIGNMENT if we don't really have to.
743 So we cap to the smallest alignment that corresponds to
744 a known efficient memory access pattern of the target. */
745 unsigned int align_cap = Is_Atomic (gnat_entity)
747 : get_mode_alignment (ptr_mode);
749 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
750 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
753 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
755 /* But make sure not to under-align the object. */
756 if (align <= TYPE_ALIGN (gnu_type))
759 /* And honor the minimum valid atomic alignment, if any. */
760 #ifdef MINIMUM_ATOMIC_ALIGNMENT
761 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
762 align = MINIMUM_ATOMIC_ALIGNMENT;
766 /* If the object is set to have atomic components, find the component
767 type and validate it.
769 ??? Note that we ignore Has_Volatile_Components on objects; it's
770 not at all clear what to do in that case. */
772 if (Has_Atomic_Components (gnat_entity))
774 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
775 ? TREE_TYPE (gnu_type) : gnu_type);
777 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
778 && TYPE_MULTI_ARRAY_P (gnu_inner))
779 gnu_inner = TREE_TYPE (gnu_inner);
781 check_ok_for_atomic (gnu_inner, gnat_entity, true);
784 /* Now check if the type of the object allows atomic access. Note
785 that we must test the type, even if this object has size and
786 alignment to allow such access, because we will be going
787 inside the padded record to assign to the object. We could fix
788 this by always copying via an intermediate value, but it's not
789 clear it's worth the effort. */
790 if (Is_Atomic (gnat_entity))
791 check_ok_for_atomic (gnu_type, gnat_entity, false);
793 /* If this is an aliased object with an unconstrained nominal subtype,
794 make a type that includes the template. */
795 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
796 && Is_Array_Type (Etype (gnat_entity))
797 && !type_annotate_only)
800 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
803 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
804 concat_name (gnu_entity_name,
808 #ifdef MINIMUM_ATOMIC_ALIGNMENT
809 /* If the size is a constant and no alignment is specified, force
810 the alignment to be the minimum valid atomic alignment. The
811 restriction on constant size avoids problems with variable-size
812 temporaries; if the size is variable, there's no issue with
813 atomic access. Also don't do this for a constant, since it isn't
814 necessary and can interfere with constant replacement. Finally,
815 do not do it for Out parameters since that creates an
816 size inconsistency with In parameters. */
817 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
818 && !FLOAT_TYPE_P (gnu_type)
819 && !const_flag && No (Renamed_Object (gnat_entity))
820 && !imported_p && No (Address_Clause (gnat_entity))
821 && kind != E_Out_Parameter
822 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
823 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
824 align = MINIMUM_ATOMIC_ALIGNMENT;
827 /* Make a new type with the desired size and alignment, if needed.
828 But do not take into account alignment promotions to compute the
829 size of the object. */
830 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
831 if (gnu_size || align > 0)
832 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
833 "PAD", false, definition,
834 gnu_size ? true : false);
836 /* If this is a renaming, avoid as much as possible to create a new
837 object. However, in several cases, creating it is required.
838 This processing needs to be applied to the raw expression so
839 as to make it more likely to rename the underlying object. */
840 if (Present (Renamed_Object (gnat_entity)))
842 bool create_normal_object = false;
844 /* If the renamed object had padding, strip off the reference
845 to the inner object and reset our type. */
846 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
849 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
850 /* Strip useless conversions around the object. */
851 || (TREE_CODE (gnu_expr) == NOP_EXPR
852 && gnat_types_compatible_p
853 (TREE_TYPE (gnu_expr),
854 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
856 gnu_expr = TREE_OPERAND (gnu_expr, 0);
857 gnu_type = TREE_TYPE (gnu_expr);
860 /* Case 1: If this is a constant renaming stemming from a function
861 call, treat it as a normal object whose initial value is what
862 is being renamed. RM 3.3 says that the result of evaluating a
863 function call is a constant object. As a consequence, it can
864 be the inner object of a constant renaming. In this case, the
865 renaming must be fully instantiated, i.e. it cannot be a mere
866 reference to (part of) an existing object. */
869 tree inner_object = gnu_expr;
870 while (handled_component_p (inner_object))
871 inner_object = TREE_OPERAND (inner_object, 0);
872 if (TREE_CODE (inner_object) == CALL_EXPR)
873 create_normal_object = true;
876 /* Otherwise, see if we can proceed with a stabilized version of
877 the renamed entity or if we need to make a new object. */
878 if (!create_normal_object)
880 tree maybe_stable_expr = NULL_TREE;
883 /* Case 2: If the renaming entity need not be materialized and
884 the renamed expression is something we can stabilize, use
885 that for the renaming. At the global level, we can only do
886 this if we know no SAVE_EXPRs need be made, because the
887 expression we return might be used in arbitrary conditional
888 branches so we must force the SAVE_EXPRs evaluation
889 immediately and this requires a function context. */
890 if (!Materialize_Entity (gnat_entity)
891 && (!global_bindings_p ()
892 || (staticp (gnu_expr)
893 && !TREE_SIDE_EFFECTS (gnu_expr))))
896 = maybe_stabilize_reference (gnu_expr, true, &stable);
900 gnu_decl = maybe_stable_expr;
901 /* ??? No DECL_EXPR is created so we need to mark
902 the expression manually lest it is shared. */
903 if (global_bindings_p ())
904 mark_visited (&gnu_decl);
905 save_gnu_tree (gnat_entity, gnu_decl, true);
910 /* The stabilization failed. Keep maybe_stable_expr
911 untouched here to let the pointer case below know
912 about that failure. */
915 /* Case 3: If this is a constant renaming and creating a
916 new object is allowed and cheap, treat it as a normal
917 object whose initial value is what is being renamed. */
919 && !Is_Composite_Type
920 (Underlying_Type (Etype (gnat_entity))))
923 /* Case 4: Make this into a constant pointer to the object we
924 are to rename and attach the object to the pointer if it is
925 something we can stabilize.
927 From the proper scope, attached objects will be referenced
928 directly instead of indirectly via the pointer to avoid
929 subtle aliasing problems with non-addressable entities.
930 They have to be stable because we must not evaluate the
931 variables in the expression every time the renaming is used.
932 The pointer is called a "renaming" pointer in this case.
934 In the rare cases where we cannot stabilize the renamed
935 object, we just make a "bare" pointer, and the renamed
936 entity is always accessed indirectly through it. */
939 gnu_type = build_reference_type (gnu_type);
940 inner_const_flag = TREE_READONLY (gnu_expr);
943 /* If the previous attempt at stabilizing failed, there
944 is no point in trying again and we reuse the result
945 without attaching it to the pointer. In this case it
946 will only be used as the initializing expression of
947 the pointer and thus needs no special treatment with
948 regard to multiple evaluations. */
949 if (maybe_stable_expr)
952 /* Otherwise, try to stabilize and attach the expression
953 to the pointer if the stabilization succeeds.
955 Note that this might introduce SAVE_EXPRs and we don't
956 check whether we're at the global level or not. This
957 is fine since we are building a pointer initializer and
958 neither the pointer nor the initializing expression can
959 be accessed before the pointer elaboration has taken
960 place in a correct program.
962 These SAVE_EXPRs will be evaluated at the right place
963 by either the evaluation of the initializer for the
964 non-global case or the elaboration code for the global
965 case, and will be attached to the elaboration procedure
966 in the latter case. */
970 = maybe_stabilize_reference (gnu_expr, true, &stable);
973 renamed_obj = maybe_stable_expr;
975 /* Attaching is actually performed downstream, as soon
976 as we have a VAR_DECL for the pointer we make. */
980 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
982 gnu_size = NULL_TREE;
988 /* Make a volatile version of this object's type if we are to make
989 the object volatile. We also interpret 13.3(19) conservatively
990 and disallow any optimizations for such a non-constant object. */
991 if ((Treat_As_Volatile (gnat_entity)
993 && (Is_Exported (gnat_entity)
994 || Is_Imported (gnat_entity)
995 || Present (Address_Clause (gnat_entity)))))
996 && !TYPE_VOLATILE (gnu_type))
997 gnu_type = build_qualified_type (gnu_type,
998 (TYPE_QUALS (gnu_type)
999 | TYPE_QUAL_VOLATILE));
1001 /* If we are defining an aliased object whose nominal subtype is
1002 unconstrained, the object is a record that contains both the
1003 template and the object. If there is an initializer, it will
1004 have already been converted to the right type, but we need to
1005 create the template if there is no initializer. */
1008 && TREE_CODE (gnu_type) == RECORD_TYPE
1009 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1010 /* Beware that padding might have been introduced
1011 via maybe_pad_type above. */
1012 || (TYPE_IS_PADDING_P (gnu_type)
1013 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1015 && TYPE_CONTAINS_TEMPLATE_P
1016 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1019 = TYPE_IS_PADDING_P (gnu_type)
1020 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1021 : TYPE_FIELDS (gnu_type);
1024 = gnat_build_constructor
1028 build_template (TREE_TYPE (template_field),
1029 TREE_TYPE (TREE_CHAIN (template_field)),
1034 /* Convert the expression to the type of the object except in the
1035 case where the object's type is unconstrained or the object's type
1036 is a padded record whose field is of self-referential size. In
1037 the former case, converting will generate unnecessary evaluations
1038 of the CONSTRUCTOR to compute the size and in the latter case, we
1039 want to only copy the actual data. */
1041 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1042 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1043 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1044 && TYPE_IS_PADDING_P (gnu_type)
1045 && (CONTAINS_PLACEHOLDER_P
1046 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1047 gnu_expr = convert (gnu_type, gnu_expr);
1049 /* If this is a pointer and it does not have an initializing
1050 expression, initialize it to NULL, unless the object is
1053 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1054 && !Is_Imported (gnat_entity) && !gnu_expr)
1055 gnu_expr = integer_zero_node;
1057 /* If we are defining the object and it has an Address clause, we must
1058 either get the address expression from the saved GCC tree for the
1059 object if it has a Freeze node, or elaborate the address expression
1060 here since the front-end has guaranteed that the elaboration has no
1061 effects in this case. */
1062 if (definition && Present (Address_Clause (gnat_entity)))
1065 = present_gnu_tree (gnat_entity)
1066 ? get_gnu_tree (gnat_entity)
1067 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1069 save_gnu_tree (gnat_entity, NULL_TREE, false);
1071 /* Ignore the size. It's either meaningless or was handled
1073 gnu_size = NULL_TREE;
1074 /* Convert the type of the object to a reference type that can
1075 alias everything as per 13.3(19). */
1077 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1078 gnu_address = convert (gnu_type, gnu_address);
1080 const_flag = !Is_Public (gnat_entity)
1081 || compile_time_known_address_p (Expression (Address_Clause
1084 /* If this is a deferred constant, the initializer is attached to
1086 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1089 (Expression (Declaration_Node (Full_View (gnat_entity))));
1091 /* If we don't have an initializing expression for the underlying
1092 variable, the initializing expression for the pointer is the
1093 specified address. Otherwise, we have to make a COMPOUND_EXPR
1094 to assign both the address and the initial value. */
1096 gnu_expr = gnu_address;
1099 = build2 (COMPOUND_EXPR, gnu_type,
1101 (MODIFY_EXPR, NULL_TREE,
1102 build_unary_op (INDIRECT_REF, NULL_TREE,
1108 /* If it has an address clause and we are not defining it, mark it
1109 as an indirect object. Likewise for Stdcall objects that are
1111 if ((!definition && Present (Address_Clause (gnat_entity)))
1112 || (Is_Imported (gnat_entity)
1113 && Has_Stdcall_Convention (gnat_entity)))
1115 /* Convert the type of the object to a reference type that can
1116 alias everything as per 13.3(19). */
1118 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1119 gnu_size = NULL_TREE;
1121 /* No point in taking the address of an initializing expression
1122 that isn't going to be used. */
1123 gnu_expr = NULL_TREE;
1125 /* If it has an address clause whose value is known at compile
1126 time, make the object a CONST_DECL. This will avoid a
1127 useless dereference. */
1128 if (Present (Address_Clause (gnat_entity)))
1130 Node_Id gnat_address
1131 = Expression (Address_Clause (gnat_entity));
1133 if (compile_time_known_address_p (gnat_address))
1135 gnu_expr = gnat_to_gnu (gnat_address);
1143 /* If we are at top level and this object is of variable size,
1144 make the actual type a hidden pointer to the real type and
1145 make the initializer be a memory allocation and initialization.
1146 Likewise for objects we aren't defining (presumed to be
1147 external references from other packages), but there we do
1148 not set up an initialization.
1150 If the object's size overflows, make an allocator too, so that
1151 Storage_Error gets raised. Note that we will never free
1152 such memory, so we presume it never will get allocated. */
1154 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1155 global_bindings_p () || !definition
1158 && ! allocatable_size_p (gnu_size,
1159 global_bindings_p () || !definition
1162 gnu_type = build_reference_type (gnu_type);
1163 gnu_size = NULL_TREE;
1167 /* In case this was a aliased object whose nominal subtype is
1168 unconstrained, the pointer above will be a thin pointer and
1169 build_allocator will automatically make the template.
1171 If we have a template initializer only (that we made above),
1172 pretend there is none and rely on what build_allocator creates
1173 again anyway. Otherwise (if we have a full initializer), get
1174 the data part and feed that to build_allocator.
1176 If we are elaborating a mutable object, tell build_allocator to
1177 ignore a possibly simpler size from the initializer, if any, as
1178 we must allocate the maximum possible size in this case. */
1182 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1184 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1185 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1188 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1190 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1191 && 1 == VEC_length (constructor_elt,
1192 CONSTRUCTOR_ELTS (gnu_expr)))
1196 = build_component_ref
1197 (gnu_expr, NULL_TREE,
1198 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1202 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1203 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1204 && !Is_Imported (gnat_entity))
1205 post_error ("?Storage_Error will be raised at run-time!",
1209 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1210 Empty, Empty, gnat_entity, mutable_p);
1214 gnu_expr = NULL_TREE;
1219 /* If this object would go into the stack and has an alignment larger
1220 than the largest stack alignment the back-end can honor, resort to
1221 a variable of "aligning type". */
1222 if (!global_bindings_p () && !static_p && definition
1223 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1225 /* Create the new variable. No need for extra room before the
1226 aligned field as this is in automatic storage. */
1228 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1229 TYPE_SIZE_UNIT (gnu_type),
1230 BIGGEST_ALIGNMENT, 0);
1232 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1233 NULL_TREE, gnu_new_type, NULL_TREE, false,
1234 false, false, false, NULL, gnat_entity);
1236 /* Initialize the aligned field if we have an initializer. */
1239 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1241 (gnu_new_var, NULL_TREE,
1242 TYPE_FIELDS (gnu_new_type), false),
1246 /* And setup this entity as a reference to the aligned field. */
1247 gnu_type = build_reference_type (gnu_type);
1250 (ADDR_EXPR, gnu_type,
1251 build_component_ref (gnu_new_var, NULL_TREE,
1252 TYPE_FIELDS (gnu_new_type), false));
1254 gnu_size = NULL_TREE;
1260 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1261 | TYPE_QUAL_CONST));
1263 /* Convert the expression to the type of the object except in the
1264 case where the object's type is unconstrained or the object's type
1265 is a padded record whose field is of self-referential size. In
1266 the former case, converting will generate unnecessary evaluations
1267 of the CONSTRUCTOR to compute the size and in the latter case, we
1268 want to only copy the actual data. */
1270 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1271 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1272 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1273 && TYPE_IS_PADDING_P (gnu_type)
1274 && (CONTAINS_PLACEHOLDER_P
1275 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1276 gnu_expr = convert (gnu_type, gnu_expr);
1278 /* If this name is external or there was a name specified, use it,
1279 unless this is a VMS exception object since this would conflict
1280 with the symbol we need to export in addition. Don't use the
1281 Interface_Name if there is an address clause (see CD30005). */
1282 if (!Is_VMS_Exception (gnat_entity)
1283 && ((Present (Interface_Name (gnat_entity))
1284 && No (Address_Clause (gnat_entity)))
1285 || (Is_Public (gnat_entity)
1286 && (!Is_Imported (gnat_entity)
1287 || Is_Exported (gnat_entity)))))
1288 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1290 /* If this is constant initialized to a static constant and the
1291 object has an aggregate type, force it to be statically
1292 allocated. This will avoid an initialization copy. */
1293 if (!static_p && const_flag
1294 && gnu_expr && TREE_CONSTANT (gnu_expr)
1295 && AGGREGATE_TYPE_P (gnu_type)
1296 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1297 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1298 && TYPE_IS_PADDING_P (gnu_type)
1299 && !host_integerp (TYPE_SIZE_UNIT
1300 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1303 gnu_decl = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1304 gnu_expr, const_flag,
1305 Is_Public (gnat_entity),
1306 imported_p || !definition,
1307 static_p, attr_list, gnat_entity);
1308 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1309 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1310 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1312 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1313 if (global_bindings_p ())
1315 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1316 record_global_renaming_pointer (gnu_decl);
1320 if (definition && DECL_SIZE_UNIT (gnu_decl)
1321 && get_block_jmpbuf_decl ()
1322 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1323 || (flag_stack_check == GENERIC_STACK_CHECK
1324 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1325 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1326 add_stmt_with_node (build_call_1_expr
1327 (update_setjmp_buf_decl,
1328 build_unary_op (ADDR_EXPR, NULL_TREE,
1329 get_block_jmpbuf_decl ())),
1332 /* If we are defining an Out parameter and we're not optimizing,
1333 create a fake PARM_DECL for debugging purposes and make it
1334 point to the VAR_DECL. Suppress debug info for the latter
1335 but make sure it will still live on the stack so it can be
1336 accessed from within the debugger through the PARM_DECL. */
1337 if (kind == E_Out_Parameter && definition && !optimize)
1339 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1340 gnat_pushdecl (param, gnat_entity);
1341 SET_DECL_VALUE_EXPR (param, gnu_decl);
1342 DECL_HAS_VALUE_EXPR_P (param) = 1;
1344 debug_info_p = false;
1346 DECL_IGNORED_P (param) = 1;
1347 TREE_ADDRESSABLE (gnu_decl) = 1;
1350 /* If this is a public constant or we're not optimizing and we're not
1351 making a VAR_DECL for it, make one just for export or debugger use.
1352 Likewise if the address is taken or if either the object or type is
1353 aliased. Make an external declaration for a reference, unless this
1354 is a Standard entity since there no real symbol at the object level
1356 if (TREE_CODE (gnu_decl) == CONST_DECL
1357 && (definition || Sloc (gnat_entity) > Standard_Location)
1358 && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
1360 || Address_Taken (gnat_entity)
1361 || Is_Aliased (gnat_entity)
1362 || Is_Aliased (Etype (gnat_entity))))
1365 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1366 gnu_expr, true, Is_Public (gnat_entity),
1367 !definition, static_p, NULL,
1370 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1372 /* As debugging information will be generated for the variable,
1373 do not generate information for the constant. */
1374 DECL_IGNORED_P (gnu_decl) = 1;
1377 /* If this is declared in a block that contains a block with an
1378 exception handler, we must force this variable in memory to
1379 suppress an invalid optimization. */
1380 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1381 && Exception_Mechanism != Back_End_Exceptions)
1382 TREE_ADDRESSABLE (gnu_decl) = 1;
1384 gnu_type = TREE_TYPE (gnu_decl);
1386 /* Back-annotate Alignment and Esize of the object if not already
1387 known, except for when the object is actually a pointer to the
1388 real object, since alignment and size of a pointer don't have
1389 anything to do with those of the designated object. Note that
1390 we pick the values of the type, not those of the object, to
1391 shield ourselves from low-level platform-dependent adjustments
1392 like alignment promotion. This is both consistent with all the
1393 treatment above, where alignment and size are set on the type of
1394 the object and not on the object directly, and makes it possible
1395 to support confirming representation clauses in all cases. */
1397 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1398 Set_Alignment (gnat_entity,
1399 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1401 if (!used_by_ref && Unknown_Esize (gnat_entity))
1403 if (TREE_CODE (gnu_type) == RECORD_TYPE
1404 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1406 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1408 Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1414 /* Return a TYPE_DECL for "void" that we previously made. */
1415 gnu_decl = TYPE_NAME (void_type_node);
1418 case E_Enumeration_Type:
1419 /* A special case: for the types Character and Wide_Character in
1420 Standard, we do not list all the literals. So if the literals
1421 are not specified, make this an unsigned type. */
1422 if (No (First_Literal (gnat_entity)))
1424 gnu_type = make_unsigned_type (esize);
1425 TYPE_NAME (gnu_type) = gnu_entity_name;
1427 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1428 This is needed by the DWARF-2 back-end to distinguish between
1429 unsigned integer types and character types. */
1430 TYPE_STRING_FLAG (gnu_type) = 1;
1434 /* Normal case of non-character type or non-Standard character type. */
1436 /* Here we have a list of enumeral constants in First_Literal.
1437 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1438 the list to be placed into TYPE_FIELDS. Each node in the list
1439 is a TREE_LIST whose TREE_VALUE is the literal name and whose
1440 TREE_PURPOSE is the value of the literal. */
1442 Entity_Id gnat_literal;
1443 tree gnu_literal_list = NULL_TREE;
1445 if (Is_Unsigned_Type (gnat_entity))
1446 gnu_type = make_unsigned_type (esize);
1448 gnu_type = make_signed_type (esize);
1450 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1452 for (gnat_literal = First_Literal (gnat_entity);
1453 Present (gnat_literal);
1454 gnat_literal = Next_Literal (gnat_literal))
1456 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1459 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1460 gnu_type, gnu_value, true, false, false,
1461 false, NULL, gnat_literal);
1463 save_gnu_tree (gnat_literal, gnu_literal, false);
1464 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1465 gnu_value, gnu_literal_list);
1468 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1470 /* Note that the bounds are updated at the end of this function
1471 to avoid an infinite recursion since they refer to the type. */
1475 case E_Signed_Integer_Type:
1476 case E_Ordinary_Fixed_Point_Type:
1477 case E_Decimal_Fixed_Point_Type:
1478 /* For integer types, just make a signed type the appropriate number
1480 gnu_type = make_signed_type (esize);
1483 case E_Modular_Integer_Type:
1485 /* For modular types, make the unsigned type of the proper number
1486 of bits and then set up the modulus, if required. */
1487 tree gnu_modulus, gnu_high = NULL_TREE;
1489 /* Packed array types are supposed to be subtypes only. */
1490 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1492 gnu_type = make_unsigned_type (esize);
1494 /* Get the modulus in this type. If it overflows, assume it is because
1495 it is equal to 2**Esize. Note that there is no overflow checking
1496 done on unsigned type, so we detect the overflow by looking for
1497 a modulus of zero, which is otherwise invalid. */
1498 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1500 if (!integer_zerop (gnu_modulus))
1502 TYPE_MODULAR_P (gnu_type) = 1;
1503 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1504 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1505 convert (gnu_type, integer_one_node));
1508 /* If the upper bound is not maximal, make an extra subtype. */
1510 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1512 tree gnu_subtype = make_unsigned_type (esize);
1513 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1514 TREE_TYPE (gnu_subtype) = gnu_type;
1515 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1516 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1517 gnu_type = gnu_subtype;
1522 case E_Signed_Integer_Subtype:
1523 case E_Enumeration_Subtype:
1524 case E_Modular_Integer_Subtype:
1525 case E_Ordinary_Fixed_Point_Subtype:
1526 case E_Decimal_Fixed_Point_Subtype:
1528 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1529 not want to call create_range_type since we would like each subtype
1530 node to be distinct. ??? Historically this was in preparation for
1531 when memory aliasing is implemented, but that's obsolete now given
1532 the call to relate_alias_sets below.
1534 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1535 this fact is used by the arithmetic conversion functions.
1537 We elaborate the Ancestor_Subtype if it is not in the current unit
1538 and one of our bounds is non-static. We do this to ensure consistent
1539 naming in the case where several subtypes share the same bounds, by
1540 elaborating the first such subtype first, thus using its name. */
1543 && Present (Ancestor_Subtype (gnat_entity))
1544 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1545 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1546 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1547 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1549 /* Set the precision to the Esize except for bit-packed arrays. */
1550 if (Is_Packed_Array_Type (gnat_entity)
1551 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1552 esize = UI_To_Int (RM_Size (gnat_entity));
1554 /* This should be an unsigned type if the base type is unsigned or
1555 if the lower bound is constant and non-negative or if the type
1557 if (Is_Unsigned_Type (Etype (gnat_entity))
1558 || Is_Unsigned_Type (gnat_entity)
1559 || Has_Biased_Representation (gnat_entity))
1560 gnu_type = make_unsigned_type (esize);
1562 gnu_type = make_signed_type (esize);
1563 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1565 SET_TYPE_RM_MIN_VALUE
1567 convert (TREE_TYPE (gnu_type),
1568 elaborate_expression (Type_Low_Bound (gnat_entity),
1569 gnat_entity, get_identifier ("L"),
1571 Needs_Debug_Info (gnat_entity))));
1573 SET_TYPE_RM_MAX_VALUE
1575 convert (TREE_TYPE (gnu_type),
1576 elaborate_expression (Type_High_Bound (gnat_entity),
1577 gnat_entity, get_identifier ("U"),
1579 Needs_Debug_Info (gnat_entity))));
1581 /* One of the above calls might have caused us to be elaborated,
1582 so don't blow up if so. */
1583 if (present_gnu_tree (gnat_entity))
1585 maybe_present = true;
1589 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1590 = Has_Biased_Representation (gnat_entity);
1592 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1593 TYPE_STUB_DECL (gnu_type)
1594 = create_type_stub_decl (gnu_entity_name, gnu_type);
1596 /* Inherit our alias set from what we're a subtype of. Subtypes
1597 are not different types and a pointer can designate any instance
1598 within a subtype hierarchy. */
1599 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1601 /* For a packed array, make the original array type a parallel type. */
1603 && Is_Packed_Array_Type (gnat_entity)
1604 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1605 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1607 (Original_Array_Type (gnat_entity)));
1609 /* If the type we are dealing with represents a bit-packed array,
1610 we need to have the bits left justified on big-endian targets
1611 and right justified on little-endian targets. We also need to
1612 ensure that when the value is read (e.g. for comparison of two
1613 such values), we only get the good bits, since the unused bits
1614 are uninitialized. Both goals are accomplished by wrapping up
1615 the modular type in an enclosing record type. */
1616 if (Is_Packed_Array_Type (gnat_entity)
1617 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1619 tree gnu_field_type, gnu_field;
1621 /* Set the RM size before wrapping up the type. */
1622 SET_TYPE_RM_SIZE (gnu_type,
1623 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1624 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1625 gnu_field_type = gnu_type;
1627 gnu_type = make_node (RECORD_TYPE);
1628 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1630 /* Propagate the alignment of the modular type to the record.
1631 This means that bit-packed arrays have "ceil" alignment for
1632 their size, which may seem counter-intuitive but makes it
1633 possible to easily overlay them on modular types. */
1634 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1635 TYPE_PACKED (gnu_type) = 1;
1637 /* Create a stripped-down declaration of the original type, mainly
1639 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1640 debug_info_p, gnat_entity);
1642 /* Don't notify the field as "addressable", since we won't be taking
1643 it's address and it would prevent create_field_decl from making a
1645 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1646 gnu_field_type, gnu_type, 1, 0, 0, 0);
1648 /* Do not finalize it until after the parallel type is added. */
1649 finish_record_type (gnu_type, gnu_field, 0, true);
1650 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1652 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1654 /* Make the original array type a parallel type. */
1656 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1657 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1659 (Original_Array_Type (gnat_entity)));
1661 rest_of_record_type_compilation (gnu_type);
1664 /* If the type we are dealing with has got a smaller alignment than the
1665 natural one, we need to wrap it up in a record type and under-align
1666 the latter. We reuse the padding machinery for this purpose. */
1667 else if (Present (Alignment_Clause (gnat_entity))
1668 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1669 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1670 && align < TYPE_ALIGN (gnu_type))
1672 tree gnu_field_type, gnu_field;
1674 /* Set the RM size before wrapping up the type. */
1675 SET_TYPE_RM_SIZE (gnu_type,
1676 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1677 gnu_field_type = gnu_type;
1679 gnu_type = make_node (RECORD_TYPE);
1680 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1682 TYPE_ALIGN (gnu_type) = align;
1683 TYPE_PACKED (gnu_type) = 1;
1685 /* Create a stripped-down declaration of the original type, mainly
1687 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1688 debug_info_p, gnat_entity);
1690 /* Don't notify the field as "addressable", since we won't be taking
1691 it's address and it would prevent create_field_decl from making a
1693 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1694 gnu_field_type, gnu_type, 1, 0, 0, 0);
1696 finish_record_type (gnu_type, gnu_field, 0, false);
1697 TYPE_IS_PADDING_P (gnu_type) = 1;
1699 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1702 /* Otherwise reset the alignment lest we computed it above. */
1708 case E_Floating_Point_Type:
1709 /* If this is a VAX floating-point type, use an integer of the proper
1710 size. All the operations will be handled with ASM statements. */
1711 if (Vax_Float (gnat_entity))
1713 gnu_type = make_signed_type (esize);
1714 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1715 SET_TYPE_DIGITS_VALUE (gnu_type,
1716 UI_To_gnu (Digits_Value (gnat_entity),
1721 /* The type of the Low and High bounds can be our type if this is
1722 a type from Standard, so set them at the end of the function. */
1723 gnu_type = make_node (REAL_TYPE);
1724 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1725 layout_type (gnu_type);
1728 case E_Floating_Point_Subtype:
1729 if (Vax_Float (gnat_entity))
1731 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1737 && Present (Ancestor_Subtype (gnat_entity))
1738 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1739 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1740 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1741 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1744 gnu_type = make_node (REAL_TYPE);
1745 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1746 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1747 TYPE_GCC_MIN_VALUE (gnu_type)
1748 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1749 TYPE_GCC_MAX_VALUE (gnu_type)
1750 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1751 layout_type (gnu_type);
1753 SET_TYPE_RM_MIN_VALUE
1755 convert (TREE_TYPE (gnu_type),
1756 elaborate_expression (Type_Low_Bound (gnat_entity),
1757 gnat_entity, get_identifier ("L"),
1759 Needs_Debug_Info (gnat_entity))));
1761 SET_TYPE_RM_MAX_VALUE
1763 convert (TREE_TYPE (gnu_type),
1764 elaborate_expression (Type_High_Bound (gnat_entity),
1765 gnat_entity, get_identifier ("U"),
1767 Needs_Debug_Info (gnat_entity))));
1769 /* One of the above calls might have caused us to be elaborated,
1770 so don't blow up if so. */
1771 if (present_gnu_tree (gnat_entity))
1773 maybe_present = true;
1777 /* Inherit our alias set from what we're a subtype of, as for
1778 integer subtypes. */
1779 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1783 /* Array and String Types and Subtypes
1785 Unconstrained array types are represented by E_Array_Type and
1786 constrained array types are represented by E_Array_Subtype. There
1787 are no actual objects of an unconstrained array type; all we have
1788 are pointers to that type.
1790 The following fields are defined on array types and subtypes:
1792 Component_Type Component type of the array.
1793 Number_Dimensions Number of dimensions (an int).
1794 First_Index Type of first index. */
1799 Entity_Id gnat_index;
1800 const bool convention_fortran_p
1801 = (Convention (gnat_entity) == Convention_Fortran);
1802 const int ndim = Number_Dimensions (gnat_entity);
1803 tree gnu_template_fields = NULL_TREE;
1804 tree gnu_template_type = make_node (RECORD_TYPE);
1805 tree gnu_template_reference;
1806 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1807 tree gnu_fat_type = make_node (RECORD_TYPE);
1808 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
1809 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
1810 tree gnu_max_size = size_one_node, gnu_max_size_unit;
1811 tree gnu_comp_size, tem;
1814 TYPE_NAME (gnu_template_type)
1815 = create_concat_name (gnat_entity, "XUB");
1817 /* Make a node for the array. If we are not defining the array
1818 suppress expanding incomplete types. */
1819 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1823 defer_incomplete_level++;
1824 this_deferred = true;
1827 /* Build the fat pointer type. Use a "void *" object instead of
1828 a pointer to the array type since we don't have the array type
1829 yet (it will reference the fat pointer via the bounds). */
1830 tem = chainon (chainon (NULL_TREE,
1831 create_field_decl (get_identifier ("P_ARRAY"),
1834 NULL_TREE, NULL_TREE, 0)),
1835 create_field_decl (get_identifier ("P_BOUNDS"),
1838 NULL_TREE, NULL_TREE, 0));
1840 /* Make sure we can put this into a register. */
1841 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1843 /* Do not finalize this record type since the types of its fields
1844 are still incomplete at this point. */
1845 finish_record_type (gnu_fat_type, tem, 0, true);
1846 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1848 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1849 is the fat pointer. This will be used to access the individual
1850 fields once we build them. */
1851 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1852 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1853 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1854 gnu_template_reference
1855 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1856 TREE_READONLY (gnu_template_reference) = 1;
1858 /* Now create the GCC type for each index and add the fields for that
1859 index to the template. */
1860 for (index = (convention_fortran_p ? ndim - 1 : 0),
1861 gnat_index = First_Index (gnat_entity);
1862 0 <= index && index < ndim;
1863 index += (convention_fortran_p ? - 1 : 1),
1864 gnat_index = Next_Index (gnat_index))
1866 char field_name[16];
1867 tree gnu_index_base_type
1868 = get_unpadded_type (Base_Type (Etype (gnat_index)));
1869 tree gnu_low_field, gnu_high_field, gnu_low, gnu_high;
1871 /* Make the FIELD_DECLs for the low and high bounds of this
1872 type and then make extractions of these fields from the
1874 sprintf (field_name, "LB%d", index);
1875 gnu_low_field = create_field_decl (get_identifier (field_name),
1876 gnu_index_base_type,
1877 gnu_template_type, 0,
1878 NULL_TREE, NULL_TREE, 0);
1879 Sloc_to_locus (Sloc (gnat_entity),
1880 &DECL_SOURCE_LOCATION (gnu_low_field));
1882 field_name[0] = 'U';
1883 gnu_high_field = create_field_decl (get_identifier (field_name),
1884 gnu_index_base_type,
1885 gnu_template_type, 0,
1886 NULL_TREE, NULL_TREE, 0);
1887 Sloc_to_locus (Sloc (gnat_entity),
1888 &DECL_SOURCE_LOCATION (gnu_high_field));
1890 gnu_temp_fields[index] = chainon (gnu_low_field, gnu_high_field);
1892 /* We can't use build_component_ref here since the template type
1893 isn't complete yet. */
1894 gnu_low = build3 (COMPONENT_REF, gnu_index_base_type,
1895 gnu_template_reference, gnu_low_field,
1897 gnu_high = build3 (COMPONENT_REF, gnu_index_base_type,
1898 gnu_template_reference, gnu_high_field,
1900 TREE_READONLY (gnu_low) = TREE_READONLY (gnu_high) = 1;
1902 /* Make a range type with the new range in the Ada base type.
1903 Then make an index type with the new range in sizetype. */
1904 gnu_index_types[index]
1905 = create_index_type (convert (sizetype, gnu_low),
1906 convert (sizetype, gnu_high),
1907 create_range_type (gnu_index_base_type,
1911 /* Update the maximum size of the array in elements. */
1914 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
1916 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
1918 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
1920 = size_binop (MAX_EXPR,
1921 size_binop (PLUS_EXPR, size_one_node,
1922 size_binop (MINUS_EXPR,
1926 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1927 && TREE_OVERFLOW (gnu_this_max))
1928 gnu_max_size = NULL_TREE;
1931 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1934 TYPE_NAME (gnu_index_types[index])
1935 = create_concat_name (gnat_entity, field_name);
1938 for (index = 0; index < ndim; index++)
1940 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1942 /* Install all the fields into the template. */
1943 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1944 TYPE_READONLY (gnu_template_type) = 1;
1946 /* Now make the array of arrays and update the pointer to the array
1947 in the fat pointer. Note that it is the first field. */
1948 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1950 /* Try to get a smaller form of the component if needed. */
1951 if ((Is_Packed (gnat_entity)
1952 || Has_Component_Size_Clause (gnat_entity))
1953 && !Is_Bit_Packed_Array (gnat_entity)
1954 && !Has_Aliased_Components (gnat_entity)
1955 && !Strict_Alignment (Component_Type (gnat_entity))
1956 && TREE_CODE (tem) == RECORD_TYPE
1957 && !TYPE_IS_FAT_POINTER_P (tem)
1958 && host_integerp (TYPE_SIZE (tem), 1))
1959 tem = make_packable_type (tem, false);
1961 if (Has_Atomic_Components (gnat_entity))
1962 check_ok_for_atomic (tem, gnat_entity, true);
1964 /* Get and validate any specified Component_Size, but if Packed,
1965 ignore it since the front end will have taken care of it. */
1967 = validate_size (Component_Size (gnat_entity), tem,
1969 (Is_Bit_Packed_Array (gnat_entity)
1970 ? TYPE_DECL : VAR_DECL),
1971 true, Has_Component_Size_Clause (gnat_entity));
1973 /* If the component type is a RECORD_TYPE that has a self-referential
1974 size, use the maximum size. */
1976 && TREE_CODE (tem) == RECORD_TYPE
1977 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1978 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1980 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1982 tree orig_tem = tem;
1983 unsigned int max_align;
1985 /* If an alignment is specified, use it as a cap on the component
1986 type so that it can be honored for the whole type. But ignore
1987 it for the original type of packed array types. */
1988 if (No (Packed_Array_Type (gnat_entity))
1989 && Known_Alignment (gnat_entity))
1990 max_align = validate_alignment (Alignment (gnat_entity),
1995 tem = make_type_from_size (tem, gnu_comp_size, false);
1996 if (max_align > 0 && TYPE_ALIGN (tem) > max_align)
2001 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
2002 "C_PAD", false, definition, true);
2004 /* If a padding record was made, declare it now since it will
2005 never be declared otherwise. This is necessary to ensure
2006 that its subtrees are properly marked. */
2007 if (tem != orig_tem)
2008 create_type_decl (TYPE_NAME (tem), tem, NULL, true,
2009 debug_info_p, gnat_entity);
2012 if (Has_Volatile_Components (gnat_entity))
2013 tem = build_qualified_type (tem,
2014 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
2016 /* If Component_Size is not already specified, annotate it with the
2017 size of the component. */
2018 if (Unknown_Component_Size (gnat_entity))
2019 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2021 /* Compute the maximum size of the array in units and bits. */
2024 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2025 TYPE_SIZE_UNIT (tem));
2026 gnu_max_size = size_binop (MULT_EXPR,
2027 convert (bitsizetype, gnu_max_size),
2031 gnu_max_size_unit = NULL_TREE;
2033 /* Now build the array type. */
2034 for (index = ndim - 1; index >= 0; index--)
2036 tem = build_array_type (tem, gnu_index_types[index]);
2037 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2038 if (array_type_has_nonaliased_component (gnat_entity, tem))
2039 TYPE_NONALIASED_COMPONENT (tem) = 1;
2042 /* If an alignment is specified, use it if valid. But ignore it
2043 for the original type of packed array types. If the alignment
2044 was requested with an explicit alignment clause, state so. */
2045 if (No (Packed_Array_Type (gnat_entity))
2046 && Known_Alignment (gnat_entity))
2049 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2051 if (Present (Alignment_Clause (gnat_entity)))
2052 TYPE_USER_ALIGN (tem) = 1;
2055 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2056 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2058 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2059 corresponding fat pointer. */
2060 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2061 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2062 SET_TYPE_MODE (gnu_type, BLKmode);
2063 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2064 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2066 /* If the maximum size doesn't overflow, use it. */
2068 && TREE_CODE (gnu_max_size) == INTEGER_CST
2069 && !TREE_OVERFLOW (gnu_max_size)
2070 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2071 && !TREE_OVERFLOW (gnu_max_size_unit))
2073 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2075 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2076 TYPE_SIZE_UNIT (tem));
2079 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2080 tem, NULL, !Comes_From_Source (gnat_entity),
2081 debug_info_p, gnat_entity);
2083 /* Give the fat pointer type a name. */
2084 create_type_decl (create_concat_name (gnat_entity, "XUP"),
2085 gnu_fat_type, NULL, true,
2086 debug_info_p, gnat_entity);
2088 /* Create the type to be used as what a thin pointer designates: an
2089 record type for the object and its template with the field offsets
2090 shifted to have the template at a negative offset. */
2091 tem = build_unc_object_type (gnu_template_type, tem,
2092 create_concat_name (gnat_entity, "XUT"));
2093 shift_unc_components_for_thin_pointers (tem);
2095 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2096 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2098 /* Give the thin pointer type a name. */
2099 create_type_decl (create_concat_name (gnat_entity, "XUX"),
2100 build_pointer_type (tem), NULL, true,
2101 debug_info_p, gnat_entity);
2105 case E_String_Subtype:
2106 case E_Array_Subtype:
2108 /* This is the actual data type for array variables. Multidimensional
2109 arrays are implemented as arrays of arrays. Note that arrays which
2110 have sparse enumeration subtypes as index components create sparse
2111 arrays, which is obviously space inefficient but so much easier to
2114 Also note that the subtype never refers to the unconstrained array
2115 type, which is somewhat at variance with Ada semantics.
2117 First check to see if this is simply a renaming of the array type.
2118 If so, the result is the array type. */
2120 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2121 if (!Is_Constrained (gnat_entity))
2125 Entity_Id gnat_index, gnat_base_index;
2126 const bool convention_fortran_p
2127 = (Convention (gnat_entity) == Convention_Fortran);
2128 const int ndim = Number_Dimensions (gnat_entity);
2129 tree gnu_base_type = gnu_type;
2130 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
2131 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2132 bool need_index_type_struct = false;
2135 /* First create the GCC type for each index and find out whether
2136 special types are needed for debugging information. */
2137 for (index = (convention_fortran_p ? ndim - 1 : 0),
2138 gnat_index = First_Index (gnat_entity),
2140 = First_Index (Implementation_Base_Type (gnat_entity));
2141 0 <= index && index < ndim;
2142 index += (convention_fortran_p ? - 1 : 1),
2143 gnat_index = Next_Index (gnat_index),
2144 gnat_base_index = Next_Index (gnat_base_index))
2146 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2147 tree prec = TYPE_RM_SIZE (gnu_index_type);
2149 = (compare_tree_int (prec, TYPE_PRECISION (sizetype)) > 0
2150 || (compare_tree_int (prec, TYPE_PRECISION (sizetype)) == 0
2151 && TYPE_UNSIGNED (gnu_index_type)
2152 != TYPE_UNSIGNED (sizetype)));
2153 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2154 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2155 tree gnu_min = convert (sizetype, gnu_orig_min);
2156 tree gnu_max = convert (sizetype, gnu_orig_max);
2157 tree gnu_base_index_type
2158 = get_unpadded_type (Etype (gnat_base_index));
2159 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2160 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2163 /* See if the base array type is already flat. If it is, we
2164 are probably compiling an ACATS test but it will cause the
2165 code below to malfunction if we don't handle it specially. */
2166 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2167 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2168 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2170 gnu_min = size_one_node;
2171 gnu_max = size_zero_node;
2175 /* Similarly, if one of the values overflows in sizetype and the
2176 range is null, use 1..0 for the sizetype bounds. */
2178 && TREE_CODE (gnu_min) == INTEGER_CST
2179 && TREE_CODE (gnu_max) == INTEGER_CST
2180 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2181 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2183 gnu_min = size_one_node;
2184 gnu_max = size_zero_node;
2188 /* If the minimum and maximum values both overflow in sizetype,
2189 but the difference in the original type does not overflow in
2190 sizetype, ignore the overflow indication. */
2192 && TREE_CODE (gnu_min) == INTEGER_CST
2193 && TREE_CODE (gnu_max) == INTEGER_CST
2194 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2197 fold_build2 (MINUS_EXPR, gnu_index_type,
2201 TREE_OVERFLOW (gnu_min) = 0;
2202 TREE_OVERFLOW (gnu_max) = 0;
2206 /* Compute the size of this dimension in the general case. We
2207 need to provide GCC with an upper bound to use but have to
2208 deal with the "superflat" case. There are three ways to do
2209 this. If we can prove that the array can never be superflat,
2210 we can just use the high bound of the index type. */
2211 else if (Nkind (gnat_index) == N_Range
2212 && cannot_be_superflat_p (gnat_index))
2215 /* Otherwise, if we can prove that the low bound minus one and
2216 the high bound cannot overflow, we can just use the expression
2217 MAX (hb, lb - 1). Otherwise, we have to use the most general
2218 expression (hb >= lb) ? hb : lb - 1. Note that the comparison
2219 must be done in the original index type, to avoid any overflow
2220 during the conversion. */
2223 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2225 /* If gnu_high is a constant that has overflowed, the bound
2226 is the smallest integer so cannot be the maximum. */
2227 if (TREE_CODE (gnu_high) == INTEGER_CST
2228 && TREE_OVERFLOW (gnu_high))
2231 /* If the index type is not wider and gnu_high is a constant
2232 that hasn't overflowed, we can use the maximum. */
2233 else if (!wider_p && TREE_CODE (gnu_high) == INTEGER_CST)
2234 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2238 = build_cond_expr (sizetype,
2239 build_binary_op (GE_EXPR,
2246 gnu_index_types[index]
2247 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2250 /* Update the maximum size of the array in elements. Here we
2251 see if any constraint on the index type of the base type
2252 can be used in the case of self-referential bound on the
2253 index type of the subtype. We look for a non-"infinite"
2254 and non-self-referential bound from any type involved and
2255 handle each bound separately. */
2258 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2259 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2260 tree gnu_base_index_base_type
2261 = get_base_type (gnu_base_index_type);
2262 tree gnu_base_base_min
2263 = convert (sizetype,
2264 TYPE_MIN_VALUE (gnu_base_index_base_type));
2265 tree gnu_base_base_max
2266 = convert (sizetype,
2267 TYPE_MAX_VALUE (gnu_base_index_base_type));
2269 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2270 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2271 && !TREE_OVERFLOW (gnu_base_min)))
2272 gnu_base_min = gnu_min;
2274 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2275 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2276 && !TREE_OVERFLOW (gnu_base_max)))
2277 gnu_base_max = gnu_max;
2279 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2280 && TREE_OVERFLOW (gnu_base_min))
2281 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2282 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2283 && TREE_OVERFLOW (gnu_base_max))
2284 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2285 gnu_max_size = NULL_TREE;
2289 = size_binop (MAX_EXPR,
2290 size_binop (PLUS_EXPR, size_one_node,
2291 size_binop (MINUS_EXPR,
2296 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2297 && TREE_OVERFLOW (gnu_this_max))
2298 gnu_max_size = NULL_TREE;
2301 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2305 /* We need special types for debugging information to point to
2306 the index types if they have variable bounds, are not integer
2307 types, are biased or are wider than sizetype. */
2308 if (!integer_onep (gnu_orig_min)
2309 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2310 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2311 || (TREE_TYPE (gnu_index_type)
2312 && TREE_CODE (TREE_TYPE (gnu_index_type))
2314 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2315 || compare_tree_int (prec, TYPE_PRECISION (sizetype)) > 0)
2316 need_index_type_struct = true;
2319 /* Then flatten: create the array of arrays. For an array type
2320 used to implement a packed array, get the component type from
2321 the original array type since the representation clauses that
2322 can affect it are on the latter. */
2323 if (Is_Packed_Array_Type (gnat_entity)
2324 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2326 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2327 for (index = ndim - 1; index >= 0; index--)
2328 gnu_type = TREE_TYPE (gnu_type);
2330 /* One of the above calls might have caused us to be elaborated,
2331 so don't blow up if so. */
2332 if (present_gnu_tree (gnat_entity))
2334 maybe_present = true;
2342 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2344 /* One of the above calls might have caused us to be elaborated,
2345 so don't blow up if so. */
2346 if (present_gnu_tree (gnat_entity))
2348 maybe_present = true;
2352 /* Try to get a smaller form of the component if needed. */
2353 if ((Is_Packed (gnat_entity)
2354 || Has_Component_Size_Clause (gnat_entity))
2355 && !Is_Bit_Packed_Array (gnat_entity)
2356 && !Has_Aliased_Components (gnat_entity)
2357 && !Strict_Alignment (Component_Type (gnat_entity))
2358 && TREE_CODE (gnu_type) == RECORD_TYPE
2359 && !TYPE_IS_FAT_POINTER_P (gnu_type)
2360 && host_integerp (TYPE_SIZE (gnu_type), 1))
2361 gnu_type = make_packable_type (gnu_type, false);
2363 /* Get and validate any specified Component_Size, but if Packed,
2364 ignore it since the front end will have taken care of it. */
2366 = validate_size (Component_Size (gnat_entity), gnu_type,
2368 (Is_Bit_Packed_Array (gnat_entity)
2369 ? TYPE_DECL : VAR_DECL), true,
2370 Has_Component_Size_Clause (gnat_entity));
2372 /* If the component type is a RECORD_TYPE that has a
2373 self-referential size, use the maximum size. */
2375 && TREE_CODE (gnu_type) == RECORD_TYPE
2376 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2377 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2379 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2381 tree orig_gnu_type = gnu_type;
2382 unsigned int max_align;
2384 /* If an alignment is specified, use it as a cap on the
2385 component type so that it can be honored for the whole
2386 type. But ignore it for the original type of packed
2388 if (No (Packed_Array_Type (gnat_entity))
2389 && Known_Alignment (gnat_entity))
2390 max_align = validate_alignment (Alignment (gnat_entity),
2396 = make_type_from_size (gnu_type, gnu_comp_size, false);
2397 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
2398 gnu_type = orig_gnu_type;
2400 orig_gnu_type = gnu_type;
2402 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2403 gnat_entity, "C_PAD", false,
2406 /* If a padding record was made, declare it now since it
2407 will never be declared otherwise. This is necessary
2408 to ensure that its subtrees are properly marked. */
2409 if (gnu_type != orig_gnu_type)
2410 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2411 true, debug_info_p, gnat_entity);
2414 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2415 gnu_type = build_qualified_type (gnu_type,
2416 (TYPE_QUALS (gnu_type)
2417 | TYPE_QUAL_VOLATILE));
2420 /* Compute the maximum size of the array in units and bits. */
2423 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2424 TYPE_SIZE_UNIT (gnu_type));
2425 gnu_max_size = size_binop (MULT_EXPR,
2426 convert (bitsizetype, gnu_max_size),
2427 TYPE_SIZE (gnu_type));
2430 gnu_max_size_unit = NULL_TREE;
2432 /* Now build the array type. */
2433 for (index = ndim - 1; index >= 0; index --)
2435 gnu_type = build_array_type (gnu_type, gnu_index_types[index]);
2436 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2437 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2438 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2441 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2442 TYPE_STUB_DECL (gnu_type)
2443 = create_type_stub_decl (gnu_entity_name, gnu_type);
2445 /* If we are at file level and this is a multi-dimensional array,
2446 we need to make a variable corresponding to the stride of the
2447 inner dimensions. */
2448 if (global_bindings_p () && ndim > 1)
2450 tree gnu_str_name = get_identifier ("ST");
2453 for (gnu_arr_type = TREE_TYPE (gnu_type);
2454 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2455 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2456 gnu_str_name = concat_name (gnu_str_name, "ST"))
2458 tree eltype = TREE_TYPE (gnu_arr_type);
2460 TYPE_SIZE (gnu_arr_type)
2461 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2462 gnat_entity, gnu_str_name,
2465 /* ??? For now, store the size as a multiple of the
2466 alignment of the element type in bytes so that we
2467 can see the alignment from the tree. */
2468 TYPE_SIZE_UNIT (gnu_arr_type)
2470 (MULT_EXPR, sizetype,
2471 elaborate_expression_1
2472 (build_binary_op (EXACT_DIV_EXPR, sizetype,
2473 TYPE_SIZE_UNIT (gnu_arr_type),
2474 size_int (TYPE_ALIGN (eltype)
2476 gnat_entity, concat_name (gnu_str_name, "A_U"),
2478 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2480 /* ??? create_type_decl is not invoked on the inner types so
2481 the MULT_EXPR node built above will never be marked. */
2482 mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2486 /* If we need to write out a record type giving the names of the
2487 bounds for debugging purposes, do it now and make the record
2488 type a parallel type. This is not needed for a packed array
2489 since the bounds are conveyed by the original array type. */
2490 if (need_index_type_struct
2492 && !Is_Packed_Array_Type (gnat_entity))
2494 tree gnu_bound_rec = make_node (RECORD_TYPE);
2495 tree gnu_field_list = NULL_TREE;
2498 TYPE_NAME (gnu_bound_rec)
2499 = create_concat_name (gnat_entity, "XA");
2501 for (index = ndim - 1; index >= 0; index--)
2503 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2504 tree gnu_index_name = TYPE_NAME (gnu_index);
2506 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2507 gnu_index_name = DECL_NAME (gnu_index_name);
2509 /* Make sure to reference the types themselves, and not just
2510 their names, as the debugger may fall back on them. */
2511 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2513 0, NULL_TREE, NULL_TREE, 0);
2514 TREE_CHAIN (gnu_field) = gnu_field_list;
2515 gnu_field_list = gnu_field;
2518 finish_record_type (gnu_bound_rec, gnu_field_list, 0, false);
2519 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2522 /* Otherwise, for a packed array, make the original array type a
2524 else if (debug_info_p
2525 && Is_Packed_Array_Type (gnat_entity)
2526 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2527 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2529 (Original_Array_Type (gnat_entity)));
2531 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2532 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2533 = (Is_Packed_Array_Type (gnat_entity)
2534 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2536 /* If the size is self-referential and the maximum size doesn't
2537 overflow, use it. */
2538 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2540 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2541 && TREE_OVERFLOW (gnu_max_size))
2542 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2543 && TREE_OVERFLOW (gnu_max_size_unit)))
2545 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2546 TYPE_SIZE (gnu_type));
2547 TYPE_SIZE_UNIT (gnu_type)
2548 = size_binop (MIN_EXPR, gnu_max_size_unit,
2549 TYPE_SIZE_UNIT (gnu_type));
2552 /* Set our alias set to that of our base type. This gives all
2553 array subtypes the same alias set. */
2554 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2557 /* If this is a packed type, make this type the same as the packed
2558 array type, but do some adjusting in the type first. */
2559 if (Present (Packed_Array_Type (gnat_entity)))
2561 Entity_Id gnat_index;
2562 tree gnu_inner_type;
2564 /* First finish the type we had been making so that we output
2565 debugging information for it. */
2567 = build_qualified_type (gnu_type,
2568 (TYPE_QUALS (gnu_type)
2569 | (TYPE_QUAL_VOLATILE
2570 * Treat_As_Volatile (gnat_entity))));
2572 /* Make it artificial only if the base type was artificial as well.
2573 That's sort of "morally" true and will make it possible for the
2574 debugger to look it up by name in DWARF, which is necessary in
2575 order to decode the packed array type. */
2577 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2578 !Comes_From_Source (gnat_entity)
2579 && !Comes_From_Source (Etype (gnat_entity)),
2580 debug_info_p, gnat_entity);
2582 /* Save it as our equivalent in case the call below elaborates
2584 save_gnu_tree (gnat_entity, gnu_decl, false);
2586 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2588 this_made_decl = true;
2589 gnu_type = TREE_TYPE (gnu_decl);
2590 save_gnu_tree (gnat_entity, NULL_TREE, false);
2592 gnu_inner_type = gnu_type;
2593 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2594 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2595 || TYPE_IS_PADDING_P (gnu_inner_type)))
2596 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2598 /* We need to attach the index type to the type we just made so
2599 that the actual bounds can later be put into a template. */
2600 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2601 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2602 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2603 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2605 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2607 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2608 TYPE_MODULUS for modular types so we make an extra
2609 subtype if necessary. */
2610 if (TYPE_MODULAR_P (gnu_inner_type))
2613 = make_unsigned_type (TYPE_PRECISION (gnu_inner_type));
2614 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2615 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2616 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2617 TYPE_MIN_VALUE (gnu_inner_type));
2618 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2619 TYPE_MAX_VALUE (gnu_inner_type));
2620 gnu_inner_type = gnu_subtype;
2623 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2625 #ifdef ENABLE_CHECKING
2626 /* Check for other cases of overloading. */
2627 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2631 /* ??? This is necessary to make sure that the container is
2632 allocated with a null tree upfront; otherwise, it could
2633 be allocated with an uninitialized tree that is accessed
2634 before being set below. See ada-tree.h for details. */
2635 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2637 for (gnat_index = First_Index (gnat_entity);
2638 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2639 SET_TYPE_ACTUAL_BOUNDS
2641 tree_cons (NULL_TREE,
2642 get_unpadded_type (Etype (gnat_index)),
2643 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2645 if (Convention (gnat_entity) != Convention_Fortran)
2646 SET_TYPE_ACTUAL_BOUNDS
2648 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2650 if (TREE_CODE (gnu_type) == RECORD_TYPE
2651 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2652 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2656 /* Abort if packed array with no packed array type field set. */
2658 gcc_assert (!Is_Packed (gnat_entity));
2662 case E_String_Literal_Subtype:
2663 /* Create the type for a string literal. */
2665 Entity_Id gnat_full_type
2666 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2667 && Present (Full_View (Etype (gnat_entity)))
2668 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2669 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2670 tree gnu_string_array_type
2671 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2672 tree gnu_string_index_type
2673 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2674 (TYPE_DOMAIN (gnu_string_array_type))));
2675 tree gnu_lower_bound
2676 = convert (gnu_string_index_type,
2677 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2678 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2679 tree gnu_length = ssize_int (length - 1);
2680 tree gnu_upper_bound
2681 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2683 convert (gnu_string_index_type, gnu_length));
2685 = create_index_type (convert (sizetype, gnu_lower_bound),
2686 convert (sizetype, gnu_upper_bound),
2687 create_range_type (gnu_string_index_type,
2693 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2695 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2696 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2697 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2701 /* Record Types and Subtypes
2703 The following fields are defined on record types:
2705 Has_Discriminants True if the record has discriminants
2706 First_Discriminant Points to head of list of discriminants
2707 First_Entity Points to head of list of fields
2708 Is_Tagged_Type True if the record is tagged
2710 Implementation of Ada records and discriminated records:
2712 A record type definition is transformed into the equivalent of a C
2713 struct definition. The fields that are the discriminants which are
2714 found in the Full_Type_Declaration node and the elements of the
2715 Component_List found in the Record_Type_Definition node. The
2716 Component_List can be a recursive structure since each Variant of
2717 the Variant_Part of the Component_List has a Component_List.
2719 Processing of a record type definition comprises starting the list of
2720 field declarations here from the discriminants and the calling the
2721 function components_to_record to add the rest of the fields from the
2722 component list and return the gnu type node. The function
2723 components_to_record will call itself recursively as it traverses
2727 if (Has_Complex_Representation (gnat_entity))
2730 = build_complex_type
2732 (Etype (Defining_Entity
2733 (First (Component_Items
2736 (Declaration_Node (gnat_entity)))))))));
2742 Node_Id full_definition = Declaration_Node (gnat_entity);
2743 Node_Id record_definition = Type_Definition (full_definition);
2744 Entity_Id gnat_field;
2745 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2746 /* Set PACKED in keeping with gnat_to_gnu_field. */
2748 = Is_Packed (gnat_entity)
2750 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2752 : (Known_Alignment (gnat_entity)
2753 || (Strict_Alignment (gnat_entity)
2754 && Known_Static_Esize (gnat_entity)))
2757 bool has_discr = Has_Discriminants (gnat_entity);
2758 bool has_rep = Has_Specified_Layout (gnat_entity);
2759 bool all_rep = has_rep;
2761 = (Is_Tagged_Type (gnat_entity)
2762 && Nkind (record_definition) == N_Derived_Type_Definition);
2763 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2765 /* See if all fields have a rep clause. Stop when we find one
2768 for (gnat_field = First_Entity (gnat_entity);
2769 Present (gnat_field);
2770 gnat_field = Next_Entity (gnat_field))
2771 if ((Ekind (gnat_field) == E_Component
2772 || Ekind (gnat_field) == E_Discriminant)
2773 && No (Component_Clause (gnat_field)))
2779 /* If this is a record extension, go a level further to find the
2780 record definition. Also, verify we have a Parent_Subtype. */
2783 if (!type_annotate_only
2784 || Present (Record_Extension_Part (record_definition)))
2785 record_definition = Record_Extension_Part (record_definition);
2787 gcc_assert (type_annotate_only
2788 || Present (Parent_Subtype (gnat_entity)));
2791 /* Make a node for the record. If we are not defining the record,
2792 suppress expanding incomplete types. */
2793 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2794 TYPE_NAME (gnu_type) = gnu_entity_name;
2795 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2799 defer_incomplete_level++;
2800 this_deferred = true;
2803 /* If both a size and rep clause was specified, put the size in
2804 the record type now so that it can get the proper mode. */
2805 if (has_rep && Known_Esize (gnat_entity))
2806 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2808 /* Always set the alignment here so that it can be used to
2809 set the mode, if it is making the alignment stricter. If
2810 it is invalid, it will be checked again below. If this is to
2811 be Atomic, choose a default alignment of a word unless we know
2812 the size and it's smaller. */
2813 if (Known_Alignment (gnat_entity))
2814 TYPE_ALIGN (gnu_type)
2815 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2816 else if (Is_Atomic (gnat_entity))
2817 TYPE_ALIGN (gnu_type)
2818 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2819 /* If a type needs strict alignment, the minimum size will be the
2820 type size instead of the RM size (see validate_size). Cap the
2821 alignment, lest it causes this type size to become too large. */
2822 else if (Strict_Alignment (gnat_entity)
2823 && Known_Static_Esize (gnat_entity))
2825 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2826 unsigned int raw_align = raw_size & -raw_size;
2827 if (raw_align < BIGGEST_ALIGNMENT)
2828 TYPE_ALIGN (gnu_type) = raw_align;
2831 TYPE_ALIGN (gnu_type) = 0;
2833 /* If we have a Parent_Subtype, make a field for the parent. If
2834 this record has rep clauses, force the position to zero. */
2835 if (Present (Parent_Subtype (gnat_entity)))
2837 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2840 /* A major complexity here is that the parent subtype will
2841 reference our discriminants in its Discriminant_Constraint
2842 list. But those must reference the parent component of this
2843 record which is of the parent subtype we have not built yet!
2844 To break the circle we first build a dummy COMPONENT_REF which
2845 represents the "get to the parent" operation and initialize
2846 each of those discriminants to a COMPONENT_REF of the above
2847 dummy parent referencing the corresponding discriminant of the
2848 base type of the parent subtype. */
2849 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2850 build0 (PLACEHOLDER_EXPR, gnu_type),
2851 build_decl (input_location,
2852 FIELD_DECL, NULL_TREE,
2857 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2858 Present (gnat_field);
2859 gnat_field = Next_Stored_Discriminant (gnat_field))
2860 if (Present (Corresponding_Discriminant (gnat_field)))
2863 build3 (COMPONENT_REF,
2864 get_unpadded_type (Etype (gnat_field)),
2866 gnat_to_gnu_field_decl (Corresponding_Discriminant
2871 /* Then we build the parent subtype. If it has discriminants but
2872 the type itself has unknown discriminants, this means that it
2873 doesn't contain information about how the discriminants are
2874 derived from those of the ancestor type, so it cannot be used
2875 directly. Instead it is built by cloning the parent subtype
2876 of the underlying record view of the type, for which the above
2877 derivation of discriminants has been made explicit. */
2878 if (Has_Discriminants (gnat_parent)
2879 && Has_Unknown_Discriminants (gnat_entity))
2881 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2883 /* If we are defining the type, the underlying record
2884 view must already have been elaborated at this point.
2885 Otherwise do it now as its parent subtype cannot be
2886 technically elaborated on its own. */
2888 gcc_assert (present_gnu_tree (gnat_uview));
2890 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2892 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2894 /* Substitute the "get to the parent" of the type for that
2895 of its underlying record view in the cloned type. */
2896 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2897 Present (gnat_field);
2898 gnat_field = Next_Stored_Discriminant (gnat_field))
2899 if (Present (Corresponding_Discriminant (gnat_field)))
2901 gnu_field = gnat_to_gnu_field_decl (gnat_field);
2903 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2904 gnu_get_parent, gnu_field, NULL_TREE);
2906 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2910 gnu_parent = gnat_to_gnu_type (gnat_parent);
2912 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2913 initially built. The discriminants must reference the fields
2914 of the parent subtype and not those of its base type for the
2915 placeholder machinery to properly work. */
2918 /* The actual parent subtype is the full view. */
2919 if (IN (Ekind (gnat_parent), Private_Kind))
2921 if (Present (Full_View (gnat_parent)))
2922 gnat_parent = Full_View (gnat_parent);
2924 gnat_parent = Underlying_Full_View (gnat_parent);
2927 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2928 Present (gnat_field);
2929 gnat_field = Next_Stored_Discriminant (gnat_field))
2930 if (Present (Corresponding_Discriminant (gnat_field)))
2932 Entity_Id field = Empty;
2933 for (field = First_Stored_Discriminant (gnat_parent);
2935 field = Next_Stored_Discriminant (field))
2936 if (same_discriminant_p (gnat_field, field))
2938 gcc_assert (Present (field));
2939 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2940 = gnat_to_gnu_field_decl (field);
2944 /* The "get to the parent" COMPONENT_REF must be given its
2946 TREE_TYPE (gnu_get_parent) = gnu_parent;
2948 /* ...and reference the _Parent field of this record. */
2950 = create_field_decl (get_identifier
2951 (Get_Name_String (Name_uParent)),
2952 gnu_parent, gnu_type, 0,
2954 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2956 ? bitsize_zero_node : NULL_TREE, 1);
2957 DECL_INTERNAL_P (gnu_field) = 1;
2958 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2959 TYPE_FIELDS (gnu_type) = gnu_field;
2962 /* Make the fields for the discriminants and put them into the record
2963 unless it's an Unchecked_Union. */
2965 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2966 Present (gnat_field);
2967 gnat_field = Next_Stored_Discriminant (gnat_field))
2969 /* If this is a record extension and this discriminant is the
2970 renaming of another discriminant, we've handled it above. */
2971 if (Present (Parent_Subtype (gnat_entity))
2972 && Present (Corresponding_Discriminant (gnat_field)))
2976 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2978 /* Make an expression using a PLACEHOLDER_EXPR from the
2979 FIELD_DECL node just created and link that with the
2980 corresponding GNAT defining identifier. */
2981 save_gnu_tree (gnat_field,
2982 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2983 build0 (PLACEHOLDER_EXPR, gnu_type),
2984 gnu_field, NULL_TREE),
2987 if (!is_unchecked_union)
2989 TREE_CHAIN (gnu_field) = gnu_field_list;
2990 gnu_field_list = gnu_field;
2994 /* Add the fields into the record type and finish it up. */
2995 components_to_record (gnu_type, Component_List (record_definition),
2996 gnu_field_list, packed, definition, NULL,
2997 false, all_rep, false, is_unchecked_union);
2999 /* If it is a tagged record force the type to BLKmode to insure that
3000 these objects will always be put in memory. Likewise for limited
3002 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
3003 SET_TYPE_MODE (gnu_type, BLKmode);
3005 /* We used to remove the associations of the discriminants and _Parent
3006 for validity checking but we may need them if there's a Freeze_Node
3007 for a subtype used in this record. */
3008 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3010 /* Fill in locations of fields. */
3011 annotate_rep (gnat_entity, gnu_type);
3013 /* If there are any entities in the chain corresponding to components
3014 that we did not elaborate, ensure we elaborate their types if they
3016 for (gnat_temp = First_Entity (gnat_entity);
3017 Present (gnat_temp);
3018 gnat_temp = Next_Entity (gnat_temp))
3019 if ((Ekind (gnat_temp) == E_Component
3020 || Ekind (gnat_temp) == E_Discriminant)
3021 && Is_Itype (Etype (gnat_temp))
3022 && !present_gnu_tree (gnat_temp))
3023 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3027 case E_Class_Wide_Subtype:
3028 /* If an equivalent type is present, that is what we should use.
3029 Otherwise, fall through to handle this like a record subtype
3030 since it may have constraints. */
3031 if (gnat_equiv_type != gnat_entity)
3033 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3034 maybe_present = true;
3038 /* ... fall through ... */
3040 case E_Record_Subtype:
3041 /* If Cloned_Subtype is Present it means this record subtype has
3042 identical layout to that type or subtype and we should use
3043 that GCC type for this one. The front end guarantees that
3044 the component list is shared. */
3045 if (Present (Cloned_Subtype (gnat_entity)))
3047 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3049 maybe_present = true;
3053 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3054 changing the type, make a new type with each field having the type of
3055 the field in the new subtype but the position computed by transforming
3056 every discriminant reference according to the constraints. We don't
3057 see any difference between private and non-private type here since
3058 derivations from types should have been deferred until the completion
3059 of the private type. */
3062 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3067 defer_incomplete_level++;
3068 this_deferred = true;
3071 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3073 if (present_gnu_tree (gnat_entity))
3075 maybe_present = true;
3079 /* When the subtype has discriminants and these discriminants affect
3080 the initial shape it has inherited, factor them in. But for the
3081 of an Unchecked_Union (it must be an Itype), just return the type.
3083 We can't just test Is_Constrained because private subtypes without
3084 discriminants of types with discriminants with default expressions
3085 are Is_Constrained but aren't constrained! */
3086 if (IN (Ekind (gnat_base_type), Record_Kind)
3087 && !Is_Unchecked_Union (gnat_base_type)
3088 && !Is_For_Access_Subtype (gnat_entity)
3089 && Is_Constrained (gnat_entity)
3090 && Has_Discriminants (gnat_entity)
3091 && Present (Discriminant_Constraint (gnat_entity))
3092 && Stored_Constraint (gnat_entity) != No_Elist)
3095 = build_subst_list (gnat_entity, gnat_base_type, definition);
3096 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3097 tree gnu_unpad_base_type, t;
3098 Entity_Id gnat_field;
3100 gnu_type = make_node (RECORD_TYPE);
3101 TYPE_NAME (gnu_type) = gnu_entity_name;
3103 /* Set the size, alignment and alias set of the new type to
3104 match that of the old one, doing required substitutions.
3105 We do it this early because we need the size of the new
3106 type below to discard old fields if necessary. */
3107 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3108 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3109 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3110 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3111 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
3113 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3114 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3115 TYPE_SIZE (gnu_type)
3116 = substitute_in_expr (TYPE_SIZE (gnu_type),
3120 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3121 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3122 TYPE_SIZE_UNIT (gnu_type)
3123 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3127 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3128 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3130 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3134 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
3135 && TYPE_IS_PADDING_P (gnu_base_type))
3136 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3138 gnu_unpad_base_type = gnu_base_type;
3141 = compute_field_positions (gnu_unpad_base_type, NULL_TREE,
3142 size_zero_node, bitsize_zero_node,
3145 for (gnat_field = First_Entity (gnat_entity);
3146 Present (gnat_field);
3147 gnat_field = Next_Entity (gnat_field))
3148 if ((Ekind (gnat_field) == E_Component
3149 || Ekind (gnat_field) == E_Discriminant)
3150 && !(Present (Corresponding_Discriminant (gnat_field))
3151 && Is_Tagged_Type (gnat_base_type))
3152 && Underlying_Type (Scope (Original_Record_Component
3156 Name_Id gnat_name = Chars (gnat_field);
3157 Entity_Id gnat_old_field
3158 = Original_Record_Component (gnat_field);
3160 = gnat_to_gnu_field_decl (gnat_old_field);
3163 (purpose_member (gnu_old_field, gnu_pos_list));
3164 tree gnu_pos = TREE_PURPOSE (gnu_offset);
3165 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3166 tree gnu_field, gnu_field_type, gnu_size, gnu_new_pos;
3167 tree gnu_last = NULL_TREE;
3168 unsigned int offset_align
3170 (TREE_PURPOSE (TREE_VALUE (gnu_offset)), 1);
3172 /* If the type is the same, retrieve the GCC type from the
3173 old field to take into account possible adjustments. */
3174 if (Etype (gnat_field) == Etype (gnat_old_field))
3175 gnu_field_type = TREE_TYPE (gnu_old_field);
3177 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3179 /* If there was a component clause, the field types must be
3180 the same for the type and subtype, so copy the data from
3181 the old field to avoid recomputation here. Also if the
3182 field is justified modular and the optimization in
3183 gnat_to_gnu_field was applied. */
3184 if (Present (Component_Clause (gnat_old_field))
3185 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3186 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3187 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3188 == TREE_TYPE (gnu_old_field)))
3190 gnu_size = DECL_SIZE (gnu_old_field);
3191 gnu_field_type = TREE_TYPE (gnu_old_field);
3194 /* If the old field was packed and of constant size, we
3195 have to get the old size here, as it might differ from
3196 what the Etype conveys and the latter might overlap
3197 onto the following field. Try to arrange the type for
3198 possible better packing along the way. */
3199 else if (DECL_PACKED (gnu_old_field)
3200 && TREE_CODE (DECL_SIZE (gnu_old_field))
3203 gnu_size = DECL_SIZE (gnu_old_field);
3204 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3205 && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
3206 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3208 = make_packable_type (gnu_field_type, true);
3212 gnu_size = TYPE_SIZE (gnu_field_type);
3214 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3215 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3216 gnu_pos = substitute_in_expr (gnu_pos,
3220 /* If the position is now a constant, we can set it as the
3221 position of the field when we make it. Otherwise, we
3222 need to deal with it specially below. */
3223 if (TREE_CONSTANT (gnu_pos))
3225 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3227 /* Discard old fields that are outside the new type.
3228 This avoids confusing code scanning it to decide
3229 how to pass it to functions on some platforms. */
3230 if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3231 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3232 && !integer_zerop (gnu_size)
3233 && !tree_int_cst_lt (gnu_new_pos,
3234 TYPE_SIZE (gnu_type)))
3238 gnu_new_pos = NULL_TREE;
3242 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3243 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3244 !DECL_NONADDRESSABLE_P (gnu_old_field));
3246 if (!TREE_CONSTANT (gnu_pos))
3248 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3249 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3250 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3251 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3252 DECL_SIZE (gnu_field) = gnu_size;
3253 DECL_SIZE_UNIT (gnu_field)
3254 = convert (sizetype,
3255 size_binop (CEIL_DIV_EXPR, gnu_size,
3256 bitsize_unit_node));
3257 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));