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 void components_to_record (tree, Node_Id, tree, int, bool, tree *,
140 bool, bool, bool, bool);
141 static Uint annotate_value (tree);
142 static void annotate_rep (Entity_Id, tree);
143 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
144 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
145 static void set_rm_size (Uint, tree, Entity_Id);
146 static tree make_type_from_size (tree, tree, bool);
147 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
148 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
149 static void check_ok_for_atomic (tree, Entity_Id, bool);
150 static int compatible_signatures_p (tree ftype1, tree ftype2);
151 static void rest_of_type_decl_compilation_no_defer (tree);
153 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
154 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
155 and associate the ..._DECL node with the input GNAT defining identifier.
157 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
158 initial value (in GCC tree form). This is optional for a variable. For
159 a renamed entity, GNU_EXPR gives the object being renamed.
161 DEFINITION is nonzero if this call is intended for a definition. This is
162 used for separate compilation where it is necessary to know whether an
163 external declaration or a definition must be created if the GCC equivalent
164 was not created previously. The value of 1 is normally used for a nonzero
165 DEFINITION, but a value of 2 is used in special circumstances, defined in
169 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
171 /* Contains the kind of the input GNAT node. */
172 const Entity_Kind kind = Ekind (gnat_entity);
173 /* True if this is a type. */
174 const bool is_type = IN (kind, Type_Kind);
175 /* For a type, contains the equivalent GNAT node to be used in gigi. */
176 Entity_Id gnat_equiv_type = Empty;
177 /* Temporary used to walk the GNAT tree. */
179 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
180 This node will be associated with the GNAT node by calling at the end
181 of the `switch' statement. */
182 tree gnu_decl = NULL_TREE;
183 /* Contains the GCC type to be used for the GCC node. */
184 tree gnu_type = NULL_TREE;
185 /* Contains the GCC size tree to be used for the GCC node. */
186 tree gnu_size = NULL_TREE;
187 /* Contains the GCC name to be used for the GCC node. */
188 tree gnu_entity_name;
189 /* True if we have already saved gnu_decl as a GNAT association. */
191 /* True if we incremented defer_incomplete_level. */
192 bool this_deferred = false;
193 /* True if we incremented force_global. */
194 bool this_global = false;
195 /* True if we should check to see if elaborated during processing. */
196 bool maybe_present = false;
197 /* True if we made GNU_DECL and its type here. */
198 bool this_made_decl = false;
199 /* True if debug info is requested for this entity. */
200 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
201 || debug_info_level == DINFO_LEVEL_VERBOSE);
202 /* True if this entity is to be considered as imported. */
203 bool imported_p = (Is_Imported (gnat_entity)
204 && No (Address_Clause (gnat_entity)));
205 /* Size and alignment of the GCC node, if meaningful. */
206 unsigned int esize = 0, align = 0;
207 /* Contains the list of attributes directly attached to the entity. */
208 struct attrib *attr_list = NULL;
210 /* Since a use of an Itype is a definition, process it as such if it
211 is not in a with'ed unit. */
214 && Is_Itype (gnat_entity)
215 && !present_gnu_tree (gnat_entity)
216 && In_Extended_Main_Code_Unit (gnat_entity))
218 /* Ensure that we are in a subprogram mentioned in the Scope chain of
219 this entity, our current scope is global, or we encountered a task
220 or entry (where we can't currently accurately check scoping). */
221 if (!current_function_decl
222 || DECL_ELABORATION_PROC_P (current_function_decl))
224 process_type (gnat_entity);
225 return get_gnu_tree (gnat_entity);
228 for (gnat_temp = Scope (gnat_entity);
230 gnat_temp = Scope (gnat_temp))
232 if (Is_Type (gnat_temp))
233 gnat_temp = Underlying_Type (gnat_temp);
235 if (Ekind (gnat_temp) == E_Subprogram_Body)
237 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
239 if (IN (Ekind (gnat_temp), Subprogram_Kind)
240 && Present (Protected_Body_Subprogram (gnat_temp)))
241 gnat_temp = Protected_Body_Subprogram (gnat_temp);
243 if (Ekind (gnat_temp) == E_Entry
244 || Ekind (gnat_temp) == E_Entry_Family
245 || Ekind (gnat_temp) == E_Task_Type
246 || (IN (Ekind (gnat_temp), Subprogram_Kind)
247 && present_gnu_tree (gnat_temp)
248 && (current_function_decl
249 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
251 process_type (gnat_entity);
252 return get_gnu_tree (gnat_entity);
256 /* This abort means the Itype has an incorrect scope, i.e. that its
257 scope does not correspond to the subprogram it is declared in. */
261 /* If we've already processed this entity, return what we got last time.
262 If we are defining the node, we should not have already processed it.
263 In that case, we will abort below when we try to save a new GCC tree
264 for this object. We also need to handle the case of getting a dummy
265 type when a Full_View exists. */
266 if ((!definition || (is_type && imported_p))
267 && present_gnu_tree (gnat_entity))
269 gnu_decl = get_gnu_tree (gnat_entity);
271 if (TREE_CODE (gnu_decl) == TYPE_DECL
272 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
273 && IN (kind, Incomplete_Or_Private_Kind)
274 && Present (Full_View (gnat_entity)))
277 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
278 save_gnu_tree (gnat_entity, NULL_TREE, false);
279 save_gnu_tree (gnat_entity, gnu_decl, false);
285 /* If this is a numeric or enumeral type, or an access type, a nonzero
286 Esize must be specified unless it was specified by the programmer. */
287 gcc_assert (!Unknown_Esize (gnat_entity)
288 || Has_Size_Clause (gnat_entity)
289 || (!IN (kind, Numeric_Kind)
290 && !IN (kind, Enumeration_Kind)
291 && (!IN (kind, Access_Kind)
292 || kind == E_Access_Protected_Subprogram_Type
293 || kind == E_Anonymous_Access_Protected_Subprogram_Type
294 || kind == E_Access_Subtype)));
296 /* The RM size must be specified for all discrete and fixed-point types. */
297 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
298 && Unknown_RM_Size (gnat_entity)));
300 /* If we get here, it means we have not yet done anything with this entity.
301 If we are not defining it, it must be a type or an entity that is defined
302 elsewhere or externally, otherwise we should have defined it already. */
303 gcc_assert (definition
304 || type_annotate_only
306 || kind == E_Discriminant
307 || kind == E_Component
309 || (kind == E_Constant && Present (Full_View (gnat_entity)))
310 || Is_Public (gnat_entity));
312 /* Get the name of the entity and set up the line number and filename of
313 the original definition for use in any decl we make. */
314 gnu_entity_name = get_entity_name (gnat_entity);
315 Sloc_to_locus (Sloc (gnat_entity), &input_location);
317 /* For cases when we are not defining (i.e., we are referencing from
318 another compilation unit) public entities, show we are at global level
319 for the purpose of computing scopes. Don't do this for components or
320 discriminants since the relevant test is whether or not the record is
323 && kind != E_Component
324 && kind != E_Discriminant
325 && Is_Public (gnat_entity)
326 && !Is_Statically_Allocated (gnat_entity))
327 force_global++, this_global = true;
329 /* Handle any attributes directly attached to the entity. */
330 if (Has_Gigi_Rep_Item (gnat_entity))
331 prepend_attributes (gnat_entity, &attr_list);
333 /* Do some common processing for types. */
336 /* Compute the equivalent type to be used in gigi. */
337 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
339 /* Machine_Attributes on types are expected to be propagated to
340 subtypes. The corresponding Gigi_Rep_Items are only attached
341 to the first subtype though, so we handle the propagation here. */
342 if (Base_Type (gnat_entity) != gnat_entity
343 && !Is_First_Subtype (gnat_entity)
344 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
345 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
348 /* Compute a default value for the size of the type. */
349 if (Known_Esize (gnat_entity)
350 && UI_Is_In_Int_Range (Esize (gnat_entity)))
352 unsigned int max_esize;
353 esize = UI_To_Int (Esize (gnat_entity));
355 if (IN (kind, Float_Kind))
356 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
357 else if (IN (kind, Access_Kind))
358 max_esize = POINTER_SIZE * 2;
360 max_esize = LONG_LONG_TYPE_SIZE;
362 if (esize > max_esize)
366 esize = LONG_LONG_TYPE_SIZE;
372 /* If this is a use of a deferred constant without address clause,
373 get its full definition. */
375 && No (Address_Clause (gnat_entity))
376 && Present (Full_View (gnat_entity)))
379 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
384 /* If we have an external constant that we are not defining, get the
385 expression that is was defined to represent. We may throw that
386 expression away later if it is not a constant. Do not retrieve the
387 expression if it is an aggregate or allocator, because in complex
388 instantiation contexts it may not be expanded */
390 && Present (Expression (Declaration_Node (gnat_entity)))
391 && !No_Initialization (Declaration_Node (gnat_entity))
392 && (Nkind (Expression (Declaration_Node (gnat_entity)))
394 && (Nkind (Expression (Declaration_Node (gnat_entity)))
396 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
398 /* Ignore deferred constant definitions without address clause since
399 they are processed fully in the front-end. If No_Initialization
400 is set, this is not a deferred constant but a constant whose value
401 is built manually. And constants that are renamings are handled
405 && No (Address_Clause (gnat_entity))
406 && !No_Initialization (Declaration_Node (gnat_entity))
407 && No (Renamed_Object (gnat_entity)))
409 gnu_decl = error_mark_node;
414 /* Ignore constant definitions already marked with the error node. See
415 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
418 && present_gnu_tree (gnat_entity)
419 && get_gnu_tree (gnat_entity) == error_mark_node)
421 maybe_present = true;
428 /* We used to special case VMS exceptions here to directly map them to
429 their associated condition code. Since this code had to be masked
430 dynamically to strip off the severity bits, this caused trouble in
431 the GCC/ZCX case because the "type" pointers we store in the tables
432 have to be static. We now don't special case here anymore, and let
433 the regular processing take place, which leaves us with a regular
434 exception data object for VMS exceptions too. The condition code
435 mapping is taken care of by the front end and the bitmasking by the
442 /* The GNAT record where the component was defined. */
443 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
445 /* If the variable is an inherited record component (in the case of
446 extended record types), just return the inherited entity, which
447 must be a FIELD_DECL. Likewise for discriminants.
448 For discriminants of untagged records which have explicit
449 stored discriminants, return the entity for the corresponding
450 stored discriminant. Also use Original_Record_Component
451 if the record has a private extension. */
452 if (Present (Original_Record_Component (gnat_entity))
453 && Original_Record_Component (gnat_entity) != gnat_entity)
456 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
457 gnu_expr, definition);
462 /* If the enclosing record has explicit stored discriminants,
463 then it is an untagged record. If the Corresponding_Discriminant
464 is not empty then this must be a renamed discriminant and its
465 Original_Record_Component must point to the corresponding explicit
466 stored discriminant (i.e. we should have taken the previous
468 else if (Present (Corresponding_Discriminant (gnat_entity))
469 && Is_Tagged_Type (gnat_record))
471 /* A tagged record has no explicit stored discriminants. */
472 gcc_assert (First_Discriminant (gnat_record)
473 == First_Stored_Discriminant (gnat_record));
475 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
476 gnu_expr, definition);
481 else if (Present (CR_Discriminant (gnat_entity))
482 && type_annotate_only)
484 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
485 gnu_expr, definition);
490 /* If the enclosing record has explicit stored discriminants, then
491 it is an untagged record. If the Corresponding_Discriminant
492 is not empty then this must be a renamed discriminant and its
493 Original_Record_Component must point to the corresponding explicit
494 stored discriminant (i.e. we should have taken the first
496 else if (Present (Corresponding_Discriminant (gnat_entity))
497 && (First_Discriminant (gnat_record)
498 != First_Stored_Discriminant (gnat_record)))
501 /* Otherwise, if we are not defining this and we have no GCC type
502 for the containing record, make one for it. Then we should
503 have made our own equivalent. */
504 else if (!definition && !present_gnu_tree (gnat_record))
506 /* ??? If this is in a record whose scope is a protected
507 type and we have an Original_Record_Component, use it.
508 This is a workaround for major problems in protected type
510 Entity_Id Scop = Scope (Scope (gnat_entity));
511 if ((Is_Protected_Type (Scop)
512 || (Is_Private_Type (Scop)
513 && Present (Full_View (Scop))
514 && Is_Protected_Type (Full_View (Scop))))
515 && Present (Original_Record_Component (gnat_entity)))
518 = gnat_to_gnu_entity (Original_Record_Component
525 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
526 gnu_decl = get_gnu_tree (gnat_entity);
532 /* Here we have no GCC type and this is a reference rather than a
533 definition. This should never happen. Most likely the cause is
534 reference before declaration in the gnat tree for gnat_entity. */
538 case E_Loop_Parameter:
539 case E_Out_Parameter:
542 /* Simple variables, loop variables, Out parameters, and exceptions. */
545 bool used_by_ref = false;
547 = ((kind == E_Constant || kind == E_Variable)
548 && Is_True_Constant (gnat_entity)
549 && !Treat_As_Volatile (gnat_entity)
550 && (((Nkind (Declaration_Node (gnat_entity))
551 == N_Object_Declaration)
552 && Present (Expression (Declaration_Node (gnat_entity))))
553 || Present (Renamed_Object (gnat_entity))));
554 bool inner_const_flag = const_flag;
555 bool static_p = Is_Statically_Allocated (gnat_entity);
556 bool mutable_p = false;
557 tree gnu_ext_name = NULL_TREE;
558 tree renamed_obj = NULL_TREE;
559 tree gnu_object_size;
561 if (Present (Renamed_Object (gnat_entity)) && !definition)
563 if (kind == E_Exception)
564 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
567 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
570 /* Get the type after elaborating the renamed object. */
571 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
573 /* For a debug renaming declaration, build a pure debug entity. */
574 if (Present (Debug_Renaming_Link (gnat_entity)))
577 gnu_decl = build_decl (VAR_DECL, gnu_entity_name, gnu_type);
578 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
579 if (global_bindings_p ())
580 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
582 addr = stack_pointer_rtx;
583 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
584 gnat_pushdecl (gnu_decl, gnat_entity);
588 /* If this is a loop variable, its type should be the base type.
589 This is because the code for processing a loop determines whether
590 a normal loop end test can be done by comparing the bounds of the
591 loop against those of the base type, which is presumed to be the
592 size used for computation. But this is not correct when the size
593 of the subtype is smaller than the type. */
594 if (kind == E_Loop_Parameter)
595 gnu_type = get_base_type (gnu_type);
597 /* Reject non-renamed objects whose types are unconstrained arrays or
598 any object whose type is a dummy type or VOID_TYPE. */
600 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
601 && No (Renamed_Object (gnat_entity)))
602 || TYPE_IS_DUMMY_P (gnu_type)
603 || TREE_CODE (gnu_type) == VOID_TYPE)
605 gcc_assert (type_annotate_only);
608 return error_mark_node;
611 /* If an alignment is specified, use it if valid. Note that exceptions
612 are objects but don't have an alignment. We must do this before we
613 validate the size, since the alignment can affect the size. */
614 if (kind != E_Exception && Known_Alignment (gnat_entity))
616 gcc_assert (Present (Alignment (gnat_entity)));
617 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
618 TYPE_ALIGN (gnu_type));
619 /* No point in changing the type if there is an address clause
620 as the final type of the object will be a reference type. */
621 if (Present (Address_Clause (gnat_entity)))
625 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
626 "PAD", false, definition, true);
629 /* If we are defining the object, see if it has a Size value and
630 validate it if so. If we are not defining the object and a Size
631 clause applies, simply retrieve the value. We don't want to ignore
632 the clause and it is expected to have been validated already. Then
633 get the new type, if any. */
635 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
636 gnat_entity, VAR_DECL, false,
637 Has_Size_Clause (gnat_entity));
638 else if (Has_Size_Clause (gnat_entity))
639 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
644 = make_type_from_size (gnu_type, gnu_size,
645 Has_Biased_Representation (gnat_entity));
647 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
648 gnu_size = NULL_TREE;
651 /* If this object has self-referential size, it must be a record with
652 a default value. We are supposed to allocate an object of the
653 maximum size in this case unless it is a constant with an
654 initializing expression, in which case we can get the size from
655 that. Note that the resulting size may still be a variable, so
656 this may end up with an indirect allocation. */
657 if (No (Renamed_Object (gnat_entity))
658 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
660 if (gnu_expr && kind == E_Constant)
662 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
663 if (CONTAINS_PLACEHOLDER_P (size))
665 /* If the initializing expression is itself a constant,
666 despite having a nominal type with self-referential
667 size, we can get the size directly from it. */
668 if (TREE_CODE (gnu_expr) == COMPONENT_REF
669 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
672 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
673 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
674 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
675 || DECL_READONLY_ONCE_ELAB
676 (TREE_OPERAND (gnu_expr, 0))))
677 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
680 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
685 /* We may have no GNU_EXPR because No_Initialization is
686 set even though there's an Expression. */
687 else if (kind == E_Constant
688 && (Nkind (Declaration_Node (gnat_entity))
689 == N_Object_Declaration)
690 && Present (Expression (Declaration_Node (gnat_entity))))
692 = TYPE_SIZE (gnat_to_gnu_type
694 (Expression (Declaration_Node (gnat_entity)))));
697 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
702 /* If the size is zero bytes, make it one byte since some linkers have
703 trouble with zero-sized objects. If the object will have a
704 template, that will make it nonzero so don't bother. Also avoid
705 doing that for an object renaming or an object with an address
706 clause, as we would lose useful information on the view size
707 (e.g. for null array slices) and we are not allocating the object
710 && integer_zerop (gnu_size)
711 && !TREE_OVERFLOW (gnu_size))
712 || (TYPE_SIZE (gnu_type)
713 && integer_zerop (TYPE_SIZE (gnu_type))
714 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
715 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
716 || !Is_Array_Type (Etype (gnat_entity)))
717 && No (Renamed_Object (gnat_entity))
718 && No (Address_Clause (gnat_entity)))
719 gnu_size = bitsize_unit_node;
721 /* If this is an object with no specified size and alignment, and
722 if either it is atomic or we are not optimizing alignment for
723 space and it is composite and not an exception, an Out parameter
724 or a reference to another object, and the size of its type is a
725 constant, set the alignment to the smallest one which is not
726 smaller than the size, with an appropriate cap. */
727 if (!gnu_size && align == 0
728 && (Is_Atomic (gnat_entity)
729 || (!Optimize_Alignment_Space (gnat_entity)
730 && kind != E_Exception
731 && kind != E_Out_Parameter
732 && Is_Composite_Type (Etype (gnat_entity))
733 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
735 && No (Renamed_Object (gnat_entity))
736 && No (Address_Clause (gnat_entity))))
737 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
739 /* No point in jumping through all the hoops needed in order
740 to support BIGGEST_ALIGNMENT if we don't really have to.
741 So we cap to the smallest alignment that corresponds to
742 a known efficient memory access pattern of the target. */
743 unsigned int align_cap = Is_Atomic (gnat_entity)
745 : get_mode_alignment (ptr_mode);
747 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
748 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
751 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
753 /* But make sure not to under-align the object. */
754 if (align <= TYPE_ALIGN (gnu_type))
757 /* And honor the minimum valid atomic alignment, if any. */
758 #ifdef MINIMUM_ATOMIC_ALIGNMENT
759 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
760 align = MINIMUM_ATOMIC_ALIGNMENT;
764 /* If the object is set to have atomic components, find the component
765 type and validate it.
767 ??? Note that we ignore Has_Volatile_Components on objects; it's
768 not at all clear what to do in that case. */
770 if (Has_Atomic_Components (gnat_entity))
772 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
773 ? TREE_TYPE (gnu_type) : gnu_type);
775 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
776 && TYPE_MULTI_ARRAY_P (gnu_inner))
777 gnu_inner = TREE_TYPE (gnu_inner);
779 check_ok_for_atomic (gnu_inner, gnat_entity, true);
782 /* Now check if the type of the object allows atomic access. Note
783 that we must test the type, even if this object has size and
784 alignment to allow such access, because we will be going
785 inside the padded record to assign to the object. We could fix
786 this by always copying via an intermediate value, but it's not
787 clear it's worth the effort. */
788 if (Is_Atomic (gnat_entity))
789 check_ok_for_atomic (gnu_type, gnat_entity, false);
791 /* If this is an aliased object with an unconstrained nominal subtype,
792 make a type that includes the template. */
793 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
794 && Is_Array_Type (Etype (gnat_entity))
795 && !type_annotate_only)
798 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
801 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
802 concat_name (gnu_entity_name,
806 #ifdef MINIMUM_ATOMIC_ALIGNMENT
807 /* If the size is a constant and no alignment is specified, force
808 the alignment to be the minimum valid atomic alignment. The
809 restriction on constant size avoids problems with variable-size
810 temporaries; if the size is variable, there's no issue with
811 atomic access. Also don't do this for a constant, since it isn't
812 necessary and can interfere with constant replacement. Finally,
813 do not do it for Out parameters since that creates an
814 size inconsistency with In parameters. */
815 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
816 && !FLOAT_TYPE_P (gnu_type)
817 && !const_flag && No (Renamed_Object (gnat_entity))
818 && !imported_p && No (Address_Clause (gnat_entity))
819 && kind != E_Out_Parameter
820 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
821 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
822 align = MINIMUM_ATOMIC_ALIGNMENT;
825 /* Make a new type with the desired size and alignment, if needed.
826 But do not take into account alignment promotions to compute the
827 size of the object. */
828 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
829 if (gnu_size || align > 0)
830 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
831 "PAD", false, definition,
832 gnu_size ? true : false);
834 /* If this is a renaming, avoid as much as possible to create a new
835 object. However, in several cases, creating it is required.
836 This processing needs to be applied to the raw expression so
837 as to make it more likely to rename the underlying object. */
838 if (Present (Renamed_Object (gnat_entity)))
840 bool create_normal_object = false;
842 /* If the renamed object had padding, strip off the reference
843 to the inner object and reset our type. */
844 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
845 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
847 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
848 /* Strip useless conversions around the object. */
849 || (TREE_CODE (gnu_expr) == NOP_EXPR
850 && gnat_types_compatible_p
851 (TREE_TYPE (gnu_expr),
852 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
854 gnu_expr = TREE_OPERAND (gnu_expr, 0);
855 gnu_type = TREE_TYPE (gnu_expr);
858 /* Case 1: If this is a constant renaming stemming from a function
859 call, treat it as a normal object whose initial value is what
860 is being renamed. RM 3.3 says that the result of evaluating a
861 function call is a constant object. As a consequence, it can
862 be the inner object of a constant renaming. In this case, the
863 renaming must be fully instantiated, i.e. it cannot be a mere
864 reference to (part of) an existing object. */
867 tree inner_object = gnu_expr;
868 while (handled_component_p (inner_object))
869 inner_object = TREE_OPERAND (inner_object, 0);
870 if (TREE_CODE (inner_object) == CALL_EXPR)
871 create_normal_object = true;
874 /* Otherwise, see if we can proceed with a stabilized version of
875 the renamed entity or if we need to make a new object. */
876 if (!create_normal_object)
878 tree maybe_stable_expr = NULL_TREE;
881 /* Case 2: If the renaming entity need not be materialized and
882 the renamed expression is something we can stabilize, use
883 that for the renaming. At the global level, we can only do
884 this if we know no SAVE_EXPRs need be made, because the
885 expression we return might be used in arbitrary conditional
886 branches so we must force the SAVE_EXPRs evaluation
887 immediately and this requires a function context. */
888 if (!Materialize_Entity (gnat_entity)
889 && (!global_bindings_p ()
890 || (staticp (gnu_expr)
891 && !TREE_SIDE_EFFECTS (gnu_expr))))
894 = maybe_stabilize_reference (gnu_expr, true, &stable);
898 gnu_decl = maybe_stable_expr;
899 /* ??? No DECL_EXPR is created so we need to mark
900 the expression manually lest it is shared. */
901 if (global_bindings_p ())
902 mark_visited (&gnu_decl);
903 save_gnu_tree (gnat_entity, gnu_decl, true);
908 /* The stabilization failed. Keep maybe_stable_expr
909 untouched here to let the pointer case below know
910 about that failure. */
913 /* Case 3: If this is a constant renaming and creating a
914 new object is allowed and cheap, treat it as a normal
915 object whose initial value is what is being renamed. */
917 && !Is_Composite_Type
918 (Underlying_Type (Etype (gnat_entity))))
921 /* Case 4: Make this into a constant pointer to the object we
922 are to rename and attach the object to the pointer if it is
923 something we can stabilize.
925 From the proper scope, attached objects will be referenced
926 directly instead of indirectly via the pointer to avoid
927 subtle aliasing problems with non-addressable entities.
928 They have to be stable because we must not evaluate the
929 variables in the expression every time the renaming is used.
930 The pointer is called a "renaming" pointer in this case.
932 In the rare cases where we cannot stabilize the renamed
933 object, we just make a "bare" pointer, and the renamed
934 entity is always accessed indirectly through it. */
937 gnu_type = build_reference_type (gnu_type);
938 inner_const_flag = TREE_READONLY (gnu_expr);
941 /* If the previous attempt at stabilizing failed, there
942 is no point in trying again and we reuse the result
943 without attaching it to the pointer. In this case it
944 will only be used as the initializing expression of
945 the pointer and thus needs no special treatment with
946 regard to multiple evaluations. */
947 if (maybe_stable_expr)
950 /* Otherwise, try to stabilize and attach the expression
951 to the pointer if the stabilization succeeds.
953 Note that this might introduce SAVE_EXPRs and we don't
954 check whether we're at the global level or not. This
955 is fine since we are building a pointer initializer and
956 neither the pointer nor the initializing expression can
957 be accessed before the pointer elaboration has taken
958 place in a correct program.
960 These SAVE_EXPRs will be evaluated at the right place
961 by either the evaluation of the initializer for the
962 non-global case or the elaboration code for the global
963 case, and will be attached to the elaboration procedure
964 in the latter case. */
968 = maybe_stabilize_reference (gnu_expr, true, &stable);
971 renamed_obj = maybe_stable_expr;
973 /* Attaching is actually performed downstream, as soon
974 as we have a VAR_DECL for the pointer we make. */
978 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
980 gnu_size = NULL_TREE;
986 /* Make a volatile version of this object's type if we are to make
987 the object volatile. We also interpret 13.3(19) conservatively
988 and disallow any optimizations for such a non-constant object. */
989 if ((Treat_As_Volatile (gnat_entity)
991 && (Is_Exported (gnat_entity)
992 || Is_Imported (gnat_entity)
993 || Present (Address_Clause (gnat_entity)))))
994 && !TYPE_VOLATILE (gnu_type))
995 gnu_type = build_qualified_type (gnu_type,
996 (TYPE_QUALS (gnu_type)
997 | TYPE_QUAL_VOLATILE));
999 /* If we are defining an aliased object whose nominal subtype is
1000 unconstrained, the object is a record that contains both the
1001 template and the object. If there is an initializer, it will
1002 have already been converted to the right type, but we need to
1003 create the template if there is no initializer. */
1006 && TREE_CODE (gnu_type) == RECORD_TYPE
1007 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1008 /* Beware that padding might have been introduced
1009 via maybe_pad_type above. */
1010 || (TYPE_IS_PADDING_P (gnu_type)
1011 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1013 && TYPE_CONTAINS_TEMPLATE_P
1014 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1017 = TYPE_IS_PADDING_P (gnu_type)
1018 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1019 : TYPE_FIELDS (gnu_type);
1022 = gnat_build_constructor
1026 build_template (TREE_TYPE (template_field),
1027 TREE_TYPE (TREE_CHAIN (template_field)),
1032 /* Convert the expression to the type of the object except in the
1033 case where the object's type is unconstrained or the object's type
1034 is a padded record whose field is of self-referential size. In
1035 the former case, converting will generate unnecessary evaluations
1036 of the CONSTRUCTOR to compute the size and in the latter case, we
1037 want to only copy the actual data. */
1039 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1040 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1041 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1042 && TYPE_IS_PADDING_P (gnu_type)
1043 && (CONTAINS_PLACEHOLDER_P
1044 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1045 gnu_expr = convert (gnu_type, gnu_expr);
1047 /* If this is a pointer and it does not have an initializing
1048 expression, initialize it to NULL, unless the object is
1051 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1052 && !Is_Imported (gnat_entity) && !gnu_expr)
1053 gnu_expr = integer_zero_node;
1055 /* If we are defining the object and it has an Address clause, we must
1056 either get the address expression from the saved GCC tree for the
1057 object if it has a Freeze node, or elaborate the address expression
1058 here since the front-end has guaranteed that the elaboration has no
1059 effects in this case. */
1060 if (definition && Present (Address_Clause (gnat_entity)))
1063 = present_gnu_tree (gnat_entity)
1064 ? get_gnu_tree (gnat_entity)
1065 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1067 save_gnu_tree (gnat_entity, NULL_TREE, false);
1069 /* Ignore the size. It's either meaningless or was handled
1071 gnu_size = NULL_TREE;
1072 /* Convert the type of the object to a reference type that can
1073 alias everything as per 13.3(19). */
1075 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1076 gnu_address = convert (gnu_type, gnu_address);
1078 const_flag = !Is_Public (gnat_entity)
1079 || compile_time_known_address_p (Expression (Address_Clause
1082 /* If this is a deferred constant, the initializer is attached to
1084 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1087 (Expression (Declaration_Node (Full_View (gnat_entity))));
1089 /* If we don't have an initializing expression for the underlying
1090 variable, the initializing expression for the pointer is the
1091 specified address. Otherwise, we have to make a COMPOUND_EXPR
1092 to assign both the address and the initial value. */
1094 gnu_expr = gnu_address;
1097 = build2 (COMPOUND_EXPR, gnu_type,
1099 (MODIFY_EXPR, NULL_TREE,
1100 build_unary_op (INDIRECT_REF, NULL_TREE,
1106 /* If it has an address clause and we are not defining it, mark it
1107 as an indirect object. Likewise for Stdcall objects that are
1109 if ((!definition && Present (Address_Clause (gnat_entity)))
1110 || (Is_Imported (gnat_entity)
1111 && Has_Stdcall_Convention (gnat_entity)))
1113 /* Convert the type of the object to a reference type that can
1114 alias everything as per 13.3(19). */
1116 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1117 gnu_size = NULL_TREE;
1119 /* No point in taking the address of an initializing expression
1120 that isn't going to be used. */
1121 gnu_expr = NULL_TREE;
1123 /* If it has an address clause whose value is known at compile
1124 time, make the object a CONST_DECL. This will avoid a
1125 useless dereference. */
1126 if (Present (Address_Clause (gnat_entity)))
1128 Node_Id gnat_address
1129 = Expression (Address_Clause (gnat_entity));
1131 if (compile_time_known_address_p (gnat_address))
1133 gnu_expr = gnat_to_gnu (gnat_address);
1141 /* If we are at top level and this object is of variable size,
1142 make the actual type a hidden pointer to the real type and
1143 make the initializer be a memory allocation and initialization.
1144 Likewise for objects we aren't defining (presumed to be
1145 external references from other packages), but there we do
1146 not set up an initialization.
1148 If the object's size overflows, make an allocator too, so that
1149 Storage_Error gets raised. Note that we will never free
1150 such memory, so we presume it never will get allocated. */
1152 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1153 global_bindings_p () || !definition
1156 && ! allocatable_size_p (gnu_size,
1157 global_bindings_p () || !definition
1160 gnu_type = build_reference_type (gnu_type);
1161 gnu_size = NULL_TREE;
1165 /* In case this was a aliased object whose nominal subtype is
1166 unconstrained, the pointer above will be a thin pointer and
1167 build_allocator will automatically make the template.
1169 If we have a template initializer only (that we made above),
1170 pretend there is none and rely on what build_allocator creates
1171 again anyway. Otherwise (if we have a full initializer), get
1172 the data part and feed that to build_allocator.
1174 If we are elaborating a mutable object, tell build_allocator to
1175 ignore a possibly simpler size from the initializer, if any, as
1176 we must allocate the maximum possible size in this case. */
1180 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1182 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1183 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1186 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1188 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1189 && 1 == VEC_length (constructor_elt,
1190 CONSTRUCTOR_ELTS (gnu_expr)))
1194 = build_component_ref
1195 (gnu_expr, NULL_TREE,
1196 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1200 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1201 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1202 && !Is_Imported (gnat_entity))
1203 post_error ("?Storage_Error will be raised at run-time!",
1207 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1208 Empty, Empty, gnat_entity, mutable_p);
1212 gnu_expr = NULL_TREE;
1217 /* If this object would go into the stack and has an alignment larger
1218 than the largest stack alignment the back-end can honor, resort to
1219 a variable of "aligning type". */
1220 if (!global_bindings_p () && !static_p && definition
1221 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1223 /* Create the new variable. No need for extra room before the
1224 aligned field as this is in automatic storage. */
1226 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1227 TYPE_SIZE_UNIT (gnu_type),
1228 BIGGEST_ALIGNMENT, 0);
1230 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1231 NULL_TREE, gnu_new_type, NULL_TREE, false,
1232 false, false, false, NULL, gnat_entity);
1234 /* Initialize the aligned field if we have an initializer. */
1237 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1239 (gnu_new_var, NULL_TREE,
1240 TYPE_FIELDS (gnu_new_type), false),
1244 /* And setup this entity as a reference to the aligned field. */
1245 gnu_type = build_reference_type (gnu_type);
1248 (ADDR_EXPR, gnu_type,
1249 build_component_ref (gnu_new_var, NULL_TREE,
1250 TYPE_FIELDS (gnu_new_type), false));
1252 gnu_size = NULL_TREE;
1258 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1259 | TYPE_QUAL_CONST));
1261 /* Convert the expression to the type of the object except in the
1262 case where the object's type is unconstrained or the object's type
1263 is a padded record whose field is of self-referential size. In
1264 the former case, converting will generate unnecessary evaluations
1265 of the CONSTRUCTOR to compute the size and in the latter case, we
1266 want to only copy the actual data. */
1268 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1269 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1270 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1271 && TYPE_IS_PADDING_P (gnu_type)
1272 && (CONTAINS_PLACEHOLDER_P
1273 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1274 gnu_expr = convert (gnu_type, gnu_expr);
1276 /* If this name is external or there was a name specified, use it,
1277 unless this is a VMS exception object since this would conflict
1278 with the symbol we need to export in addition. Don't use the
1279 Interface_Name if there is an address clause (see CD30005). */
1280 if (!Is_VMS_Exception (gnat_entity)
1281 && ((Present (Interface_Name (gnat_entity))
1282 && No (Address_Clause (gnat_entity)))
1283 || (Is_Public (gnat_entity)
1284 && (!Is_Imported (gnat_entity)
1285 || Is_Exported (gnat_entity)))))
1286 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1288 /* If this is constant initialized to a static constant and the
1289 object has an aggregate type, force it to be statically
1290 allocated. This will avoid an initialization copy. */
1291 if (!static_p && const_flag
1292 && gnu_expr && TREE_CONSTANT (gnu_expr)
1293 && AGGREGATE_TYPE_P (gnu_type)
1294 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1295 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1296 && TYPE_IS_PADDING_P (gnu_type)
1297 && !host_integerp (TYPE_SIZE_UNIT
1298 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1301 gnu_decl = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1302 gnu_expr, const_flag,
1303 Is_Public (gnat_entity),
1304 imported_p || !definition,
1305 static_p, attr_list, gnat_entity);
1306 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1307 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1308 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1310 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1311 if (global_bindings_p ())
1313 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1314 record_global_renaming_pointer (gnu_decl);
1318 if (definition && DECL_SIZE_UNIT (gnu_decl)
1319 && get_block_jmpbuf_decl ()
1320 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1321 || (flag_stack_check == GENERIC_STACK_CHECK
1322 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1323 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1324 add_stmt_with_node (build_call_1_expr
1325 (update_setjmp_buf_decl,
1326 build_unary_op (ADDR_EXPR, NULL_TREE,
1327 get_block_jmpbuf_decl ())),
1330 /* If we are defining an Out parameter and we're not optimizing,
1331 create a fake PARM_DECL for debugging purposes and make it
1332 point to the VAR_DECL. Suppress debug info for the latter
1333 but make sure it will still live on the stack so it can be
1334 accessed from within the debugger through the PARM_DECL. */
1335 if (kind == E_Out_Parameter && definition && !optimize)
1337 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1338 gnat_pushdecl (param, gnat_entity);
1339 SET_DECL_VALUE_EXPR (param, gnu_decl);
1340 DECL_HAS_VALUE_EXPR_P (param) = 1;
1342 debug_info_p = false;
1344 DECL_IGNORED_P (param) = 1;
1345 TREE_ADDRESSABLE (gnu_decl) = 1;
1348 /* If this is a public constant or we're not optimizing and we're not
1349 making a VAR_DECL for it, make one just for export or debugger use.
1350 Likewise if the address is taken or if either the object or type is
1351 aliased. Make an external declaration for a reference, unless this
1352 is a Standard entity since there no real symbol at the object level
1354 if (TREE_CODE (gnu_decl) == CONST_DECL
1355 && (definition || Sloc (gnat_entity) > Standard_Location)
1356 && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
1358 || Address_Taken (gnat_entity)
1359 || Is_Aliased (gnat_entity)
1360 || Is_Aliased (Etype (gnat_entity))))
1363 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1364 gnu_expr, true, Is_Public (gnat_entity),
1365 !definition, static_p, NULL,
1368 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1370 /* As debugging information will be generated for the variable,
1371 do not generate information for the constant. */
1372 DECL_IGNORED_P (gnu_decl) = 1;
1375 /* If this is declared in a block that contains a block with an
1376 exception handler, we must force this variable in memory to
1377 suppress an invalid optimization. */
1378 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1379 && Exception_Mechanism != Back_End_Exceptions)
1380 TREE_ADDRESSABLE (gnu_decl) = 1;
1382 gnu_type = TREE_TYPE (gnu_decl);
1384 /* Back-annotate Alignment and Esize of the object if not already
1385 known, except for when the object is actually a pointer to the
1386 real object, since alignment and size of a pointer don't have
1387 anything to do with those of the designated object. Note that
1388 we pick the values of the type, not those of the object, to
1389 shield ourselves from low-level platform-dependent adjustments
1390 like alignment promotion. This is both consistent with all the
1391 treatment above, where alignment and size are set on the type of
1392 the object and not on the object directly, and makes it possible
1393 to support confirming representation clauses in all cases. */
1395 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1396 Set_Alignment (gnat_entity,
1397 UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1399 if (!used_by_ref && Unknown_Esize (gnat_entity))
1401 if (TREE_CODE (gnu_type) == RECORD_TYPE
1402 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1404 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1406 Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1412 /* Return a TYPE_DECL for "void" that we previously made. */
1413 gnu_decl = TYPE_NAME (void_type_node);
1416 case E_Enumeration_Type:
1417 /* A special case: for the types Character and Wide_Character in
1418 Standard, we do not list all the literals. So if the literals
1419 are not specified, make this an unsigned type. */
1420 if (No (First_Literal (gnat_entity)))
1422 gnu_type = make_unsigned_type (esize);
1423 TYPE_NAME (gnu_type) = gnu_entity_name;
1425 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1426 This is needed by the DWARF-2 back-end to distinguish between
1427 unsigned integer types and character types. */
1428 TYPE_STRING_FLAG (gnu_type) = 1;
1432 /* Normal case of non-character type or non-Standard character type. */
1434 /* Here we have a list of enumeral constants in First_Literal.
1435 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1436 the list to be placed into TYPE_FIELDS. Each node in the list
1437 is a TREE_LIST whose TREE_VALUE is the literal name and whose
1438 TREE_PURPOSE is the value of the literal. */
1440 Entity_Id gnat_literal;
1441 tree gnu_literal_list = NULL_TREE;
1443 if (Is_Unsigned_Type (gnat_entity))
1444 gnu_type = make_unsigned_type (esize);
1446 gnu_type = make_signed_type (esize);
1448 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1450 for (gnat_literal = First_Literal (gnat_entity);
1451 Present (gnat_literal);
1452 gnat_literal = Next_Literal (gnat_literal))
1454 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1457 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1458 gnu_type, gnu_value, true, false, false,
1459 false, NULL, gnat_literal);
1461 save_gnu_tree (gnat_literal, gnu_literal, false);
1462 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1463 gnu_value, gnu_literal_list);
1466 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1468 /* Note that the bounds are updated at the end of this function
1469 to avoid an infinite recursion since they refer to the type. */
1473 case E_Signed_Integer_Type:
1474 case E_Ordinary_Fixed_Point_Type:
1475 case E_Decimal_Fixed_Point_Type:
1476 /* For integer types, just make a signed type the appropriate number
1478 gnu_type = make_signed_type (esize);
1481 case E_Modular_Integer_Type:
1483 /* For modular types, make the unsigned type of the proper number
1484 of bits and then set up the modulus, if required. */
1485 tree gnu_modulus, gnu_high = NULL_TREE;
1487 /* Packed array types are supposed to be subtypes only. */
1488 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1490 gnu_type = make_unsigned_type (esize);
1492 /* Get the modulus in this type. If it overflows, assume it is because
1493 it is equal to 2**Esize. Note that there is no overflow checking
1494 done on unsigned type, so we detect the overflow by looking for
1495 a modulus of zero, which is otherwise invalid. */
1496 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1498 if (!integer_zerop (gnu_modulus))
1500 TYPE_MODULAR_P (gnu_type) = 1;
1501 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1502 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1503 convert (gnu_type, integer_one_node));
1506 /* If the upper bound is not maximal, make an extra subtype. */
1508 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1510 tree gnu_subtype = make_unsigned_type (esize);
1511 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1512 TREE_TYPE (gnu_subtype) = gnu_type;
1513 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1514 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1515 gnu_type = gnu_subtype;
1520 case E_Signed_Integer_Subtype:
1521 case E_Enumeration_Subtype:
1522 case E_Modular_Integer_Subtype:
1523 case E_Ordinary_Fixed_Point_Subtype:
1524 case E_Decimal_Fixed_Point_Subtype:
1526 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1527 not want to call create_range_type since we would like each subtype
1528 node to be distinct. ??? Historically this was in preparation for
1529 when memory aliasing is implemented, but that's obsolete now given
1530 the call to relate_alias_sets below.
1532 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1533 this fact is used by the arithmetic conversion functions.
1535 We elaborate the Ancestor_Subtype if it is not in the current unit
1536 and one of our bounds is non-static. We do this to ensure consistent
1537 naming in the case where several subtypes share the same bounds, by
1538 elaborating the first such subtype first, thus using its name. */
1541 && Present (Ancestor_Subtype (gnat_entity))
1542 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1543 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1544 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1545 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1547 /* Set the precision to the Esize except for bit-packed arrays. */
1548 if (Is_Packed_Array_Type (gnat_entity)
1549 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1550 esize = UI_To_Int (RM_Size (gnat_entity));
1552 /* This should be an unsigned type if the base type is unsigned or
1553 if the lower bound is constant and non-negative or if the type
1555 if (Is_Unsigned_Type (Etype (gnat_entity))
1556 || Is_Unsigned_Type (gnat_entity)
1557 || Has_Biased_Representation (gnat_entity))
1558 gnu_type = make_unsigned_type (esize);
1560 gnu_type = make_signed_type (esize);
1561 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1563 SET_TYPE_RM_MIN_VALUE
1565 convert (TREE_TYPE (gnu_type),
1566 elaborate_expression (Type_Low_Bound (gnat_entity),
1567 gnat_entity, get_identifier ("L"),
1569 Needs_Debug_Info (gnat_entity))));
1571 SET_TYPE_RM_MAX_VALUE
1573 convert (TREE_TYPE (gnu_type),
1574 elaborate_expression (Type_High_Bound (gnat_entity),
1575 gnat_entity, get_identifier ("U"),
1577 Needs_Debug_Info (gnat_entity))));
1579 /* One of the above calls might have caused us to be elaborated,
1580 so don't blow up if so. */
1581 if (present_gnu_tree (gnat_entity))
1583 maybe_present = true;
1587 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1588 = Has_Biased_Representation (gnat_entity);
1590 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1591 TYPE_STUB_DECL (gnu_type)
1592 = create_type_stub_decl (gnu_entity_name, gnu_type);
1594 /* Inherit our alias set from what we're a subtype of. Subtypes
1595 are not different types and a pointer can designate any instance
1596 within a subtype hierarchy. */
1597 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1599 /* For a packed array, make the original array type a parallel type. */
1601 && Is_Packed_Array_Type (gnat_entity)
1602 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1603 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1605 (Original_Array_Type (gnat_entity)));
1607 /* If the type we are dealing with represents a bit-packed array,
1608 we need to have the bits left justified on big-endian targets
1609 and right justified on little-endian targets. We also need to
1610 ensure that when the value is read (e.g. for comparison of two
1611 such values), we only get the good bits, since the unused bits
1612 are uninitialized. Both goals are accomplished by wrapping up
1613 the modular type in an enclosing record type. */
1614 if (Is_Packed_Array_Type (gnat_entity)
1615 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1617 tree gnu_field_type, gnu_field;
1619 /* Set the RM size before wrapping up the type. */
1620 SET_TYPE_RM_SIZE (gnu_type,
1621 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1622 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1623 gnu_field_type = gnu_type;
1625 gnu_type = make_node (RECORD_TYPE);
1626 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1628 /* Propagate the alignment of the modular type to the record.
1629 This means that bit-packed arrays have "ceil" alignment for
1630 their size, which may seem counter-intuitive but makes it
1631 possible to easily overlay them on modular types. */
1632 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1633 TYPE_PACKED (gnu_type) = 1;
1635 /* Create a stripped-down declaration of the original type, mainly
1637 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1638 debug_info_p, gnat_entity);
1640 /* Don't notify the field as "addressable", since we won't be taking
1641 it's address and it would prevent create_field_decl from making a
1643 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1644 gnu_field_type, gnu_type, 1, 0, 0, 0);
1646 /* Do not finalize it until after the parallel type is added. */
1647 finish_record_type (gnu_type, gnu_field, 0, true);
1648 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1650 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1652 /* Make the original array type a parallel type. */
1654 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1655 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1657 (Original_Array_Type (gnat_entity)));
1659 rest_of_record_type_compilation (gnu_type);
1662 /* If the type we are dealing with has got a smaller alignment than the
1663 natural one, we need to wrap it up in a record type and under-align
1664 the latter. We reuse the padding machinery for this purpose. */
1665 else if (Present (Alignment_Clause (gnat_entity))
1666 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1667 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1668 && align < TYPE_ALIGN (gnu_type))
1670 tree gnu_field_type, gnu_field;
1672 /* Set the RM size before wrapping up the type. */
1673 SET_TYPE_RM_SIZE (gnu_type,
1674 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1675 gnu_field_type = gnu_type;
1677 gnu_type = make_node (RECORD_TYPE);
1678 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1680 TYPE_ALIGN (gnu_type) = align;
1681 TYPE_PACKED (gnu_type) = 1;
1683 /* Create a stripped-down declaration of the original type, mainly
1685 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1686 debug_info_p, gnat_entity);
1688 /* Don't notify the field as "addressable", since we won't be taking
1689 it's address and it would prevent create_field_decl from making a
1691 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1692 gnu_field_type, gnu_type, 1, 0, 0, 0);
1694 finish_record_type (gnu_type, gnu_field, 0, false);
1695 TYPE_IS_PADDING_P (gnu_type) = 1;
1697 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1700 /* Otherwise reset the alignment lest we computed it above. */
1706 case E_Floating_Point_Type:
1707 /* If this is a VAX floating-point type, use an integer of the proper
1708 size. All the operations will be handled with ASM statements. */
1709 if (Vax_Float (gnat_entity))
1711 gnu_type = make_signed_type (esize);
1712 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1713 SET_TYPE_DIGITS_VALUE (gnu_type,
1714 UI_To_gnu (Digits_Value (gnat_entity),
1719 /* The type of the Low and High bounds can be our type if this is
1720 a type from Standard, so set them at the end of the function. */
1721 gnu_type = make_node (REAL_TYPE);
1722 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1723 layout_type (gnu_type);
1726 case E_Floating_Point_Subtype:
1727 if (Vax_Float (gnat_entity))
1729 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1735 && Present (Ancestor_Subtype (gnat_entity))
1736 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1737 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1738 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1739 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1742 gnu_type = make_node (REAL_TYPE);
1743 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1744 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1745 TYPE_GCC_MIN_VALUE (gnu_type)
1746 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1747 TYPE_GCC_MAX_VALUE (gnu_type)
1748 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1749 layout_type (gnu_type);
1751 SET_TYPE_RM_MIN_VALUE
1753 convert (TREE_TYPE (gnu_type),
1754 elaborate_expression (Type_Low_Bound (gnat_entity),
1755 gnat_entity, get_identifier ("L"),
1757 Needs_Debug_Info (gnat_entity))));
1759 SET_TYPE_RM_MAX_VALUE
1761 convert (TREE_TYPE (gnu_type),
1762 elaborate_expression (Type_High_Bound (gnat_entity),
1763 gnat_entity, get_identifier ("U"),
1765 Needs_Debug_Info (gnat_entity))));
1767 /* One of the above calls might have caused us to be elaborated,
1768 so don't blow up if so. */
1769 if (present_gnu_tree (gnat_entity))
1771 maybe_present = true;
1775 /* Inherit our alias set from what we're a subtype of, as for
1776 integer subtypes. */
1777 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1781 /* Array and String Types and Subtypes
1783 Unconstrained array types are represented by E_Array_Type and
1784 constrained array types are represented by E_Array_Subtype. There
1785 are no actual objects of an unconstrained array type; all we have
1786 are pointers to that type.
1788 The following fields are defined on array types and subtypes:
1790 Component_Type Component type of the array.
1791 Number_Dimensions Number of dimensions (an int).
1792 First_Index Type of first index. */
1797 Entity_Id gnat_ind_subtype;
1798 Entity_Id gnat_ind_base_subtype;
1799 int ndim = Number_Dimensions (gnat_entity);
1801 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1803 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1805 tree gnu_template_fields = NULL_TREE;
1806 tree gnu_template_type = make_node (RECORD_TYPE);
1807 tree gnu_template_reference;
1808 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1809 tree gnu_fat_type = make_node (RECORD_TYPE);
1810 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
1811 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
1812 tree gnu_max_size = size_one_node, gnu_max_size_unit;
1813 tree gnu_comp_size, tem;
1815 TYPE_NAME (gnu_template_type)
1816 = create_concat_name (gnat_entity, "XUB");
1818 /* Make a node for the array. If we are not defining the array
1819 suppress expanding incomplete types. */
1820 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1824 defer_incomplete_level++;
1825 this_deferred = true;
1828 /* Build the fat pointer type. Use a "void *" object instead of
1829 a pointer to the array type since we don't have the array type
1830 yet (it will reference the fat pointer via the bounds). */
1831 tem = chainon (chainon (NULL_TREE,
1832 create_field_decl (get_identifier ("P_ARRAY"),
1834 gnu_fat_type, 0, 0, 0, 0)),
1835 create_field_decl (get_identifier ("P_BOUNDS"),
1837 gnu_fat_type, 0, 0, 0, 0));
1839 /* Make sure we can put this into a register. */
1840 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1842 /* Do not finalize this record type since the types of its fields
1843 are still incomplete at this point. */
1844 finish_record_type (gnu_fat_type, tem, 0, true);
1845 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1847 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1848 is the fat pointer. This will be used to access the individual
1849 fields once we build them. */
1850 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1851 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1852 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1853 gnu_template_reference
1854 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1855 TREE_READONLY (gnu_template_reference) = 1;
1857 /* Now create the GCC type for each index and add the fields for
1858 that index to the template. */
1859 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1860 gnat_ind_base_subtype
1861 = First_Index (Implementation_Base_Type (gnat_entity));
1862 index < ndim && index >= 0;
1864 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1865 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1867 char field_name[10];
1868 tree gnu_ind_subtype
1869 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1870 tree gnu_base_subtype
1871 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1873 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1875 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1876 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1878 /* Make the FIELD_DECLs for the minimum and maximum of this
1879 type and then make extractions of that field from the
1881 sprintf (field_name, "LB%d", index);
1882 gnu_min_field = create_field_decl (get_identifier (field_name),
1884 gnu_template_type, 0, 0, 0, 0);
1885 field_name[0] = 'U';
1886 gnu_max_field = create_field_decl (get_identifier (field_name),
1888 gnu_template_type, 0, 0, 0, 0);
1890 Sloc_to_locus (Sloc (gnat_entity),
1891 &DECL_SOURCE_LOCATION (gnu_min_field));
1892 Sloc_to_locus (Sloc (gnat_entity),
1893 &DECL_SOURCE_LOCATION (gnu_max_field));
1894 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1896 /* We can't use build_component_ref here since the template
1897 type isn't complete yet. */
1898 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1899 gnu_template_reference, gnu_min_field,
1901 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1902 gnu_template_reference, gnu_max_field,
1904 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1906 /* Make a range type with the new ranges, but using
1907 the Ada subtype. Then we convert to sizetype. */
1908 gnu_index_types[index]
1909 = create_index_type (convert (sizetype, gnu_min),
1910 convert (sizetype, gnu_max),
1911 create_range_type (gnu_ind_subtype,
1914 /* Update the maximum size of the array, in elements. */
1916 = size_binop (MULT_EXPR, gnu_max_size,
1917 size_binop (PLUS_EXPR, size_one_node,
1918 size_binop (MINUS_EXPR, gnu_base_max,
1921 TYPE_NAME (gnu_index_types[index])
1922 = create_concat_name (gnat_entity, field_name);
1925 for (index = 0; index < ndim; index++)
1927 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1929 /* Install all the fields into the template. */
1930 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1931 TYPE_READONLY (gnu_template_type) = 1;
1933 /* Now make the array of arrays and update the pointer to the array
1934 in the fat pointer. Note that it is the first field. */
1935 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1937 /* Try to get a smaller form of the component if needed. */
1938 if ((Is_Packed (gnat_entity)
1939 || Has_Component_Size_Clause (gnat_entity))
1940 && !Is_Bit_Packed_Array (gnat_entity)
1941 && !Has_Aliased_Components (gnat_entity)
1942 && !Strict_Alignment (Component_Type (gnat_entity))
1943 && TREE_CODE (tem) == RECORD_TYPE
1944 && !TYPE_IS_FAT_POINTER_P (tem)
1945 && host_integerp (TYPE_SIZE (tem), 1))
1946 tem = make_packable_type (tem, false);
1948 if (Has_Atomic_Components (gnat_entity))
1949 check_ok_for_atomic (tem, gnat_entity, true);
1951 /* Get and validate any specified Component_Size, but if Packed,
1952 ignore it since the front end will have taken care of it. */
1954 = validate_size (Component_Size (gnat_entity), tem,
1956 (Is_Bit_Packed_Array (gnat_entity)
1957 ? TYPE_DECL : VAR_DECL),
1958 true, Has_Component_Size_Clause (gnat_entity));
1960 /* If the component type is a RECORD_TYPE that has a self-referential
1961 size, use the maximum size. */
1963 && TREE_CODE (tem) == RECORD_TYPE
1964 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1965 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1967 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1969 tree orig_tem = tem;
1970 unsigned int max_align;
1972 /* If an alignment is specified, use it as a cap on the component
1973 type so that it can be honored for the whole type. But ignore
1974 it for the original type of packed array types. */
1975 if (No (Packed_Array_Type (gnat_entity))
1976 && Known_Alignment (gnat_entity))
1977 max_align = validate_alignment (Alignment (gnat_entity),
1982 tem = make_type_from_size (tem, gnu_comp_size, false);
1983 if (max_align > 0 && TYPE_ALIGN (tem) > max_align)
1988 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1989 "C_PAD", false, definition, true);
1991 /* If a padding record was made, declare it now since it will
1992 never be declared otherwise. This is necessary to ensure
1993 that its subtrees are properly marked. */
1994 if (tem != orig_tem)
1995 create_type_decl (TYPE_NAME (tem), tem, NULL, true,
1996 debug_info_p, gnat_entity);
1999 if (Has_Volatile_Components (gnat_entity))
2000 tem = build_qualified_type (tem,
2001 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
2003 /* If Component_Size is not already specified, annotate it with the
2004 size of the component. */
2005 if (Unknown_Component_Size (gnat_entity))
2006 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2008 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
2009 size_binop (MULT_EXPR, gnu_max_size,
2010 TYPE_SIZE_UNIT (tem)));
2011 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
2012 size_binop (MULT_EXPR,
2013 convert (bitsizetype,
2017 for (index = ndim - 1; index >= 0; index--)
2019 tem = build_array_type (tem, gnu_index_types[index]);
2020 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2021 if (array_type_has_nonaliased_component (gnat_entity, tem))
2022 TYPE_NONALIASED_COMPONENT (tem) = 1;
2025 /* If an alignment is specified, use it if valid. But ignore it
2026 for the original type of packed array types. If the alignment
2027 was requested with an explicit alignment clause, state so. */
2028 if (No (Packed_Array_Type (gnat_entity))
2029 && Known_Alignment (gnat_entity))
2032 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2034 if (Present (Alignment_Clause (gnat_entity)))
2035 TYPE_USER_ALIGN (tem) = 1;
2038 TYPE_CONVENTION_FORTRAN_P (tem)
2039 = (Convention (gnat_entity) == Convention_Fortran);
2040 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2042 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2043 corresponding fat pointer. */
2044 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2045 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2046 SET_TYPE_MODE (gnu_type, BLKmode);
2047 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2048 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2050 /* If the maximum size doesn't overflow, use it. */
2051 if (TREE_CODE (gnu_max_size) == INTEGER_CST
2052 && !TREE_OVERFLOW (gnu_max_size))
2054 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
2055 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2056 && !TREE_OVERFLOW (gnu_max_size_unit))
2057 TYPE_SIZE_UNIT (tem)
2058 = size_binop (MIN_EXPR, gnu_max_size_unit,
2059 TYPE_SIZE_UNIT (tem));
2061 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2062 tem, NULL, !Comes_From_Source (gnat_entity),
2063 debug_info_p, gnat_entity);
2065 /* Give the fat pointer type a name. */
2066 create_type_decl (create_concat_name (gnat_entity, "XUP"),
2067 gnu_fat_type, NULL, true,
2068 debug_info_p, gnat_entity);
2070 /* Create the type to be used as what a thin pointer designates: an
2071 record type for the object and its template with the field offsets
2072 shifted to have the template at a negative offset. */
2073 tem = build_unc_object_type (gnu_template_type, tem,
2074 create_concat_name (gnat_entity, "XUT"));
2075 shift_unc_components_for_thin_pointers (tem);
2077 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2078 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2080 /* Give the thin pointer type a name. */
2081 create_type_decl (create_concat_name (gnat_entity, "XUX"),
2082 build_pointer_type (tem), NULL, true,
2083 debug_info_p, gnat_entity);
2087 case E_String_Subtype:
2088 case E_Array_Subtype:
2090 /* This is the actual data type for array variables. Multidimensional
2091 arrays are implemented in the gnu tree as arrays of arrays. Note
2092 that for the moment arrays which have sparse enumeration subtypes as
2093 index components create sparse arrays, which is obviously space
2094 inefficient but so much easier to code for now.
2096 Also note that the subtype never refers to the unconstrained
2097 array type, which is somewhat at variance with Ada semantics.
2099 First check to see if this is simply a renaming of the array
2100 type. If so, the result is the array type. */
2102 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2103 if (!Is_Constrained (gnat_entity))
2107 Entity_Id gnat_ind_subtype;
2108 Entity_Id gnat_ind_base_subtype;
2109 int dim = Number_Dimensions (gnat_entity);
2111 = (Convention (gnat_entity) == Convention_Fortran) ? dim - 1 : 0;
2113 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
2115 tree gnu_base_type = gnu_type;
2116 tree *gnu_index_type = (tree *) alloca (dim * sizeof (tree));
2117 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2118 bool need_index_type_struct = false;
2119 bool max_overflow = false;
2121 /* First create the gnu types for each index. Create types for
2122 debugging information to point to the index types if the
2123 are not integer types, have variable bounds, or are
2124 wider than sizetype. */
2126 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
2127 gnat_ind_base_subtype
2128 = First_Index (Implementation_Base_Type (gnat_entity));
2129 index < dim && index >= 0;
2131 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
2132 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
2134 tree gnu_index_subtype
2135 = get_unpadded_type (Etype (gnat_ind_subtype));
2137 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
2139 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
2140 tree gnu_base_subtype
2141 = get_unpadded_type (Etype (gnat_ind_base_subtype));
2143 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
2145 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
2146 tree gnu_base_type = get_base_type (gnu_base_subtype);
2147 tree gnu_base_base_min
2148 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
2149 tree gnu_base_base_max
2150 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
2154 /* If the minimum and maximum values both overflow in
2155 SIZETYPE, but the difference in the original type
2156 does not overflow in SIZETYPE, ignore the overflow
2158 if ((TYPE_PRECISION (gnu_index_subtype)
2159 > TYPE_PRECISION (sizetype)
2160 || TYPE_UNSIGNED (gnu_index_subtype)
2161 != TYPE_UNSIGNED (sizetype))
2162 && TREE_CODE (gnu_min) == INTEGER_CST
2163 && TREE_CODE (gnu_max) == INTEGER_CST
2164 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2166 (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2167 TYPE_MAX_VALUE (gnu_index_subtype),
2168 TYPE_MIN_VALUE (gnu_index_subtype))))
2170 TREE_OVERFLOW (gnu_min) = 0;
2171 TREE_OVERFLOW (gnu_max) = 0;
2172 if (tree_int_cst_lt (gnu_max, gnu_min))
2174 gnu_min = size_one_node;
2175 gnu_max = size_zero_node;
2180 /* Similarly, if the range is null, use bounds of 1..0 for
2181 the sizetype bounds. */
2182 else if ((TYPE_PRECISION (gnu_index_subtype)
2183 > TYPE_PRECISION (sizetype)
2184 || TYPE_UNSIGNED (gnu_index_subtype)
2185 != TYPE_UNSIGNED (sizetype))
2186 && TREE_CODE (gnu_min) == INTEGER_CST
2187 && TREE_CODE (gnu_max) == INTEGER_CST
2188 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2189 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2190 TYPE_MIN_VALUE (gnu_index_subtype)))
2192 gnu_min = size_one_node;
2193 gnu_max = size_zero_node;
2197 /* See if the base array type is already flat. If it is, we
2198 are probably compiling an ACATS test, but it will cause the
2199 code below to malfunction if we don't handle it specially. */
2200 else if (TREE_CODE (gnu_base_min) == INTEGER_CST
2201 && TREE_CODE (gnu_base_max) == INTEGER_CST
2202 && !TREE_OVERFLOW (gnu_base_min)
2203 && !TREE_OVERFLOW (gnu_base_max)
2204 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2206 gnu_min = size_one_node;
2207 gnu_max = size_zero_node;
2213 /* Now compute the size of this bound. We need to provide
2214 GCC with an upper bound to use but have to deal with the
2215 "superflat" case. There are three ways to do this. If
2216 we can prove that the array can never be superflat, we
2217 can just use the high bound of the index subtype. If we
2218 can prove that the low bound minus one can't overflow,
2219 we can do this as MAX (hb, lb - 1). Otherwise, we have
2220 to use the expression hb >= lb ? hb : lb - 1. */
2221 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2223 /* If gnu_high is now an integer which overflowed, the array
2224 cannot be superflat. */
2225 if (TREE_CODE (gnu_high) == INTEGER_CST
2226 && TREE_OVERFLOW (gnu_high))
2229 /* gnu_high cannot overflow if the subtype is unsigned since
2230 sizetype is signed, or if it is now a constant that hasn't
2232 else if (TYPE_UNSIGNED (gnu_base_subtype)
2233 || 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,
2245 gnu_index_type[index]
2246 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2249 /* Also compute the maximum size of the array. Here we
2250 see if any constraint on the index type of the base type
2251 can be used in the case of self-referential bound on
2252 the index type of the subtype. We look for a non-"infinite"
2253 and non-self-referential bound from any type involved and
2254 handle each bound separately. */
2256 if ((TREE_CODE (gnu_min) == INTEGER_CST
2257 && !TREE_OVERFLOW (gnu_min)
2258 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2259 || !CONTAINS_PLACEHOLDER_P (gnu_min)
2260 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2261 && !TREE_OVERFLOW (gnu_base_min)))
2262 gnu_base_min = gnu_min;
2264 if ((TREE_CODE (gnu_max) == INTEGER_CST
2265 && !TREE_OVERFLOW (gnu_max)
2266 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2267 || !CONTAINS_PLACEHOLDER_P (gnu_max)
2268 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2269 && !TREE_OVERFLOW (gnu_base_max)))
2270 gnu_base_max = gnu_max;
2272 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2273 && TREE_OVERFLOW (gnu_base_min))
2274 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2275 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2276 && TREE_OVERFLOW (gnu_base_max))
2277 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2278 max_overflow = true;
2280 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2281 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2284 = size_binop (MAX_EXPR,
2285 size_binop (PLUS_EXPR, size_one_node,
2286 size_binop (MINUS_EXPR, gnu_base_max,
2290 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2291 && TREE_OVERFLOW (gnu_this_max))
2292 max_overflow = true;
2295 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2297 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2298 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2300 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2301 || (TREE_TYPE (gnu_index_subtype)
2302 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2304 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2305 || (TYPE_PRECISION (gnu_index_subtype)
2306 > TYPE_PRECISION (sizetype)))
2307 need_index_type_struct = true;
2310 /* Then flatten: create the array of arrays. For an array type
2311 used to implement a packed array, get the component type from
2312 the original array type since the representation clauses that
2313 can affect it are on the latter. */
2314 if (Is_Packed_Array_Type (gnat_entity)
2315 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2317 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2318 for (index = dim - 1; index >= 0; index--)
2319 gnu_type = TREE_TYPE (gnu_type);
2321 /* One of the above calls might have caused us to be elaborated,
2322 so don't blow up if so. */
2323 if (present_gnu_tree (gnat_entity))
2325 maybe_present = true;
2333 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2335 /* One of the above calls might have caused us to be elaborated,
2336 so don't blow up if so. */
2337 if (present_gnu_tree (gnat_entity))
2339 maybe_present = true;
2343 /* Try to get a smaller form of the component if needed. */
2344 if ((Is_Packed (gnat_entity)
2345 || Has_Component_Size_Clause (gnat_entity))
2346 && !Is_Bit_Packed_Array (gnat_entity)
2347 && !Has_Aliased_Components (gnat_entity)
2348 && !Strict_Alignment (Component_Type (gnat_entity))
2349 && TREE_CODE (gnu_type) == RECORD_TYPE
2350 && !TYPE_IS_FAT_POINTER_P (gnu_type)
2351 && host_integerp (TYPE_SIZE (gnu_type), 1))
2352 gnu_type = make_packable_type (gnu_type, false);
2354 /* Get and validate any specified Component_Size, but if Packed,
2355 ignore it since the front end will have taken care of it. */
2357 = validate_size (Component_Size (gnat_entity), gnu_type,
2359 (Is_Bit_Packed_Array (gnat_entity)
2360 ? TYPE_DECL : VAR_DECL), true,
2361 Has_Component_Size_Clause (gnat_entity));
2363 /* If the component type is a RECORD_TYPE that has a
2364 self-referential size, use the maximum size. */
2366 && TREE_CODE (gnu_type) == RECORD_TYPE
2367 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2368 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2370 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2372 tree orig_gnu_type = gnu_type;
2373 unsigned int max_align;
2375 /* If an alignment is specified, use it as a cap on the
2376 component type so that it can be honored for the whole
2377 type. But ignore it for the original type of packed
2379 if (No (Packed_Array_Type (gnat_entity))
2380 && Known_Alignment (gnat_entity))
2381 max_align = validate_alignment (Alignment (gnat_entity),
2387 = make_type_from_size (gnu_type, gnu_comp_size, false);
2388 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
2389 gnu_type = orig_gnu_type;
2391 orig_gnu_type = gnu_type;
2393 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2394 gnat_entity, "C_PAD", false,
2397 /* If a padding record was made, declare it now since it
2398 will never be declared otherwise. This is necessary
2399 to ensure that its subtrees are properly marked. */
2400 if (gnu_type != orig_gnu_type)
2401 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2402 true, debug_info_p, gnat_entity);
2405 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2406 gnu_type = build_qualified_type (gnu_type,
2407 (TYPE_QUALS (gnu_type)
2408 | TYPE_QUAL_VOLATILE));
2411 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2412 TYPE_SIZE_UNIT (gnu_type));
2413 gnu_max_size = size_binop (MULT_EXPR,
2414 convert (bitsizetype, gnu_max_size),
2415 TYPE_SIZE (gnu_type));
2417 for (index = dim - 1; index >= 0; index --)
2419 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2420 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2421 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2422 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2425 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2426 TYPE_STUB_DECL (gnu_type)
2427 = create_type_stub_decl (gnu_entity_name, gnu_type);
2429 /* If we are at file level and this is a multi-dimensional array, we
2430 need to make a variable corresponding to the stride of the
2431 inner dimensions. */
2432 if (global_bindings_p () && dim > 1)
2434 tree gnu_str_name = get_identifier ("ST");
2437 for (gnu_arr_type = TREE_TYPE (gnu_type);
2438 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2439 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2440 gnu_str_name = concat_name (gnu_str_name, "ST"))
2442 tree eltype = TREE_TYPE (gnu_arr_type);
2444 TYPE_SIZE (gnu_arr_type)
2445 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2446 gnat_entity, gnu_str_name,
2449 /* ??? For now, store the size as a multiple of the
2450 alignment of the element type in bytes so that we
2451 can see the alignment from the tree. */
2452 TYPE_SIZE_UNIT (gnu_arr_type)
2454 (MULT_EXPR, sizetype,
2455 elaborate_expression_1
2456 (build_binary_op (EXACT_DIV_EXPR, sizetype,
2457 TYPE_SIZE_UNIT (gnu_arr_type),
2458 size_int (TYPE_ALIGN (eltype)
2460 gnat_entity, concat_name (gnu_str_name, "A_U"),
2462 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2464 /* ??? create_type_decl is not invoked on the inner types so
2465 the MULT_EXPR node built above will never be marked. */
2466 mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2470 /* If we need to write out a record type giving the names of the
2471 bounds for debugging purposes, do it now and make the record
2472 type a parallel type. This is not needed for a packed array
2473 since the bounds are conveyed by the original array type. */
2474 if (need_index_type_struct
2476 && !Is_Packed_Array_Type (gnat_entity))
2478 tree gnu_bound_rec = make_node (RECORD_TYPE);
2479 tree gnu_field_list = NULL_TREE;
2482 TYPE_NAME (gnu_bound_rec)
2483 = create_concat_name (gnat_entity, "XA");
2485 for (index = dim - 1; index >= 0; index--)
2487 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_type[index]);
2488 tree gnu_index_name = TYPE_NAME (gnu_index);
2490 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2491 gnu_index_name = DECL_NAME (gnu_index_name);
2493 /* Make sure to reference the types themselves, and not just
2494 their names, as the debugger may fall back on them. */
2495 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2497 0, NULL_TREE, NULL_TREE, 0);
2498 TREE_CHAIN (gnu_field) = gnu_field_list;
2499 gnu_field_list = gnu_field;
2502 finish_record_type (gnu_bound_rec, gnu_field_list, 0, false);
2503 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2506 /* Otherwise, for a packed array, make the original array type a
2508 else if (debug_info_p
2509 && Is_Packed_Array_Type (gnat_entity)
2510 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2511 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2513 (Original_Array_Type (gnat_entity)));
2515 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2516 = (Convention (gnat_entity) == Convention_Fortran);
2517 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2518 = (Is_Packed_Array_Type (gnat_entity)
2519 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2521 /* If our size depends on a placeholder and the maximum size doesn't
2522 overflow, use it. */
2523 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2524 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2525 && TREE_OVERFLOW (gnu_max_size))
2526 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2527 && TREE_OVERFLOW (gnu_max_size_unit))
2530 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2531 TYPE_SIZE (gnu_type));
2532 TYPE_SIZE_UNIT (gnu_type)
2533 = size_binop (MIN_EXPR, gnu_max_size_unit,
2534 TYPE_SIZE_UNIT (gnu_type));
2537 /* Set our alias set to that of our base type. This gives all
2538 array subtypes the same alias set. */
2539 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2542 /* If this is a packed type, make this type the same as the packed
2543 array type, but do some adjusting in the type first. */
2544 if (Present (Packed_Array_Type (gnat_entity)))
2546 Entity_Id gnat_index;
2547 tree gnu_inner_type;
2549 /* First finish the type we had been making so that we output
2550 debugging information for it. */
2552 = build_qualified_type (gnu_type,
2553 (TYPE_QUALS (gnu_type)
2554 | (TYPE_QUAL_VOLATILE
2555 * Treat_As_Volatile (gnat_entity))));
2557 /* Make it artificial only if the base type was artificial as well.
2558 That's sort of "morally" true and will make it possible for the
2559 debugger to look it up by name in DWARF, which is necessary in
2560 order to decode the packed array type. */
2562 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2563 !Comes_From_Source (gnat_entity)
2564 && !Comes_From_Source (Etype (gnat_entity)),
2565 debug_info_p, gnat_entity);
2567 /* Save it as our equivalent in case the call below elaborates
2569 save_gnu_tree (gnat_entity, gnu_decl, false);
2571 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2573 this_made_decl = true;
2574 gnu_type = TREE_TYPE (gnu_decl);
2575 save_gnu_tree (gnat_entity, NULL_TREE, false);
2577 gnu_inner_type = gnu_type;
2578 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2579 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2580 || TYPE_IS_PADDING_P (gnu_inner_type)))
2581 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2583 /* We need to attach the index type to the type we just made so
2584 that the actual bounds can later be put into a template. */
2585 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2586 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2587 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2588 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2590 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2592 /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2593 TYPE_MODULUS for modular types so we make an extra
2594 subtype if necessary. */
2595 if (TYPE_MODULAR_P (gnu_inner_type))
2598 = make_unsigned_type (TYPE_PRECISION (gnu_inner_type));
2599 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2600 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2601 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2602 TYPE_MIN_VALUE (gnu_inner_type));
2603 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2604 TYPE_MAX_VALUE (gnu_inner_type));
2605 gnu_inner_type = gnu_subtype;
2608 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2610 #ifdef ENABLE_CHECKING
2611 /* Check for other cases of overloading. */
2612 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2616 /* ??? This is necessary to make sure that the container is
2617 allocated with a null tree upfront; otherwise, it could
2618 be allocated with an uninitialized tree that is accessed
2619 before being set below. See ada-tree.h for details. */
2620 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2622 for (gnat_index = First_Index (gnat_entity);
2623 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2624 SET_TYPE_ACTUAL_BOUNDS
2626 tree_cons (NULL_TREE,
2627 get_unpadded_type (Etype (gnat_index)),
2628 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2630 if (Convention (gnat_entity) != Convention_Fortran)
2631 SET_TYPE_ACTUAL_BOUNDS
2633 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2635 if (TREE_CODE (gnu_type) == RECORD_TYPE
2636 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2637 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2641 /* Abort if packed array with no packed array type field set. */
2643 gcc_assert (!Is_Packed (gnat_entity));
2647 case E_String_Literal_Subtype:
2648 /* Create the type for a string literal. */
2650 Entity_Id gnat_full_type
2651 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2652 && Present (Full_View (Etype (gnat_entity)))
2653 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2654 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2655 tree gnu_string_array_type
2656 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2657 tree gnu_string_index_type
2658 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2659 (TYPE_DOMAIN (gnu_string_array_type))));
2660 tree gnu_lower_bound
2661 = convert (gnu_string_index_type,
2662 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2663 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2664 tree gnu_length = ssize_int (length - 1);
2665 tree gnu_upper_bound
2666 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2668 convert (gnu_string_index_type, gnu_length));
2670 = create_index_type (convert (sizetype, gnu_lower_bound),
2671 convert (sizetype, gnu_upper_bound),
2672 create_range_type (gnu_string_index_type,
2678 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2680 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2681 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2682 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2686 /* Record Types and Subtypes
2688 The following fields are defined on record types:
2690 Has_Discriminants True if the record has discriminants
2691 First_Discriminant Points to head of list of discriminants
2692 First_Entity Points to head of list of fields
2693 Is_Tagged_Type True if the record is tagged
2695 Implementation of Ada records and discriminated records:
2697 A record type definition is transformed into the equivalent of a C
2698 struct definition. The fields that are the discriminants which are
2699 found in the Full_Type_Declaration node and the elements of the
2700 Component_List found in the Record_Type_Definition node. The
2701 Component_List can be a recursive structure since each Variant of
2702 the Variant_Part of the Component_List has a Component_List.
2704 Processing of a record type definition comprises starting the list of
2705 field declarations here from the discriminants and the calling the
2706 function components_to_record to add the rest of the fields from the
2707 component list and return the gnu type node. The function
2708 components_to_record will call itself recursively as it traverses
2712 if (Has_Complex_Representation (gnat_entity))
2715 = build_complex_type
2717 (Etype (Defining_Entity
2718 (First (Component_Items
2721 (Declaration_Node (gnat_entity)))))))));
2727 Node_Id full_definition = Declaration_Node (gnat_entity);
2728 Node_Id record_definition = Type_Definition (full_definition);
2729 Entity_Id gnat_field;
2730 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2731 /* Set PACKED in keeping with gnat_to_gnu_field. */
2733 = Is_Packed (gnat_entity)
2735 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2737 : (Known_Alignment (gnat_entity)
2738 || (Strict_Alignment (gnat_entity)
2739 && Known_Static_Esize (gnat_entity)))
2742 bool has_discr = Has_Discriminants (gnat_entity);
2743 bool has_rep = Has_Specified_Layout (gnat_entity);
2744 bool all_rep = has_rep;
2746 = (Is_Tagged_Type (gnat_entity)
2747 && Nkind (record_definition) == N_Derived_Type_Definition);
2748 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2750 /* See if all fields have a rep clause. Stop when we find one
2753 for (gnat_field = First_Entity (gnat_entity);
2754 Present (gnat_field);
2755 gnat_field = Next_Entity (gnat_field))
2756 if ((Ekind (gnat_field) == E_Component
2757 || Ekind (gnat_field) == E_Discriminant)
2758 && No (Component_Clause (gnat_field)))
2764 /* If this is a record extension, go a level further to find the
2765 record definition. Also, verify we have a Parent_Subtype. */
2768 if (!type_annotate_only
2769 || Present (Record_Extension_Part (record_definition)))
2770 record_definition = Record_Extension_Part (record_definition);
2772 gcc_assert (type_annotate_only
2773 || Present (Parent_Subtype (gnat_entity)));
2776 /* Make a node for the record. If we are not defining the record,
2777 suppress expanding incomplete types. */
2778 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2779 TYPE_NAME (gnu_type) = gnu_entity_name;
2780 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2784 defer_incomplete_level++;
2785 this_deferred = true;
2788 /* If both a size and rep clause was specified, put the size in
2789 the record type now so that it can get the proper mode. */
2790 if (has_rep && Known_Esize (gnat_entity))
2791 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2793 /* Always set the alignment here so that it can be used to
2794 set the mode, if it is making the alignment stricter. If
2795 it is invalid, it will be checked again below. If this is to
2796 be Atomic, choose a default alignment of a word unless we know
2797 the size and it's smaller. */
2798 if (Known_Alignment (gnat_entity))
2799 TYPE_ALIGN (gnu_type)
2800 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2801 else if (Is_Atomic (gnat_entity))
2802 TYPE_ALIGN (gnu_type)
2803 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2804 /* If a type needs strict alignment, the minimum size will be the
2805 type size instead of the RM size (see validate_size). Cap the
2806 alignment, lest it causes this type size to become too large. */
2807 else if (Strict_Alignment (gnat_entity)
2808 && Known_Static_Esize (gnat_entity))
2810 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2811 unsigned int raw_align = raw_size & -raw_size;
2812 if (raw_align < BIGGEST_ALIGNMENT)
2813 TYPE_ALIGN (gnu_type) = raw_align;
2816 TYPE_ALIGN (gnu_type) = 0;
2818 /* If we have a Parent_Subtype, make a field for the parent. If
2819 this record has rep clauses, force the position to zero. */
2820 if (Present (Parent_Subtype (gnat_entity)))
2822 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2825 /* A major complexity here is that the parent subtype will
2826 reference our discriminants in its Discriminant_Constraint
2827 list. But those must reference the parent component of this
2828 record which is of the parent subtype we have not built yet!
2829 To break the circle we first build a dummy COMPONENT_REF which
2830 represents the "get to the parent" operation and initialize
2831 each of those discriminants to a COMPONENT_REF of the above
2832 dummy parent referencing the corresponding discriminant of the
2833 base type of the parent subtype. */
2834 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2835 build0 (PLACEHOLDER_EXPR, gnu_type),
2836 build_decl (FIELD_DECL, NULL_TREE,
2841 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2842 Present (gnat_field);
2843 gnat_field = Next_Stored_Discriminant (gnat_field))
2844 if (Present (Corresponding_Discriminant (gnat_field)))
2847 build3 (COMPONENT_REF,
2848 get_unpadded_type (Etype (gnat_field)),
2850 gnat_to_gnu_field_decl (Corresponding_Discriminant
2855 /* Then we build the parent subtype. If it has discriminants but
2856 the type itself has unknown discriminants, this means that it
2857 doesn't contain information about how the discriminants are
2858 derived from those of the ancestor type, so it cannot be used
2859 directly. Instead it is built by cloning the parent subtype
2860 of the underlying record view of the type, for which the above
2861 derivation of discriminants has been made explicit. */
2862 if (Has_Discriminants (gnat_parent)
2863 && Has_Unknown_Discriminants (gnat_entity))
2865 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2867 /* If we are defining the type, the underlying record
2868 view must already have been elaborated at this point.
2869 Otherwise do it now as its parent subtype cannot be
2870 technically elaborated on its own. */
2872 gcc_assert (present_gnu_tree (gnat_uview));
2874 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2876 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2878 /* Substitute the "get to the parent" of the type for that
2879 of its underlying record view in the cloned type. */
2880 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2881 Present (gnat_field);
2882 gnat_field = Next_Stored_Discriminant (gnat_field))
2883 if (Present (Corresponding_Discriminant (gnat_field)))
2885 gnu_field = gnat_to_gnu_field_decl (gnat_field);
2887 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2888 gnu_get_parent, gnu_field, NULL_TREE);
2890 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2894 gnu_parent = gnat_to_gnu_type (gnat_parent);
2896 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2897 initially built. The discriminants must reference the fields
2898 of the parent subtype and not those of its base type for the
2899 placeholder machinery to properly work. */
2902 /* The actual parent subtype is the full view. */
2903 if (IN (Ekind (gnat_parent), Private_Kind))
2905 if (Present (Full_View (gnat_parent)))
2906 gnat_parent = Full_View (gnat_parent);
2908 gnat_parent = Underlying_Full_View (gnat_parent);
2911 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2912 Present (gnat_field);
2913 gnat_field = Next_Stored_Discriminant (gnat_field))
2914 if (Present (Corresponding_Discriminant (gnat_field)))
2916 Entity_Id field = Empty;
2917 for (field = First_Stored_Discriminant (gnat_parent);
2919 field = Next_Stored_Discriminant (field))
2920 if (same_discriminant_p (gnat_field, field))
2922 gcc_assert (Present (field));
2923 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2924 = gnat_to_gnu_field_decl (field);
2928 /* The "get to the parent" COMPONENT_REF must be given its
2930 TREE_TYPE (gnu_get_parent) = gnu_parent;
2932 /* ...and reference the _Parent field of this record. */
2934 = create_field_decl (get_identifier
2935 (Get_Name_String (Name_uParent)),
2936 gnu_parent, gnu_type, 0,
2938 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2940 ? bitsize_zero_node : NULL_TREE, 1);
2941 DECL_INTERNAL_P (gnu_field) = 1;
2942 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2943 TYPE_FIELDS (gnu_type) = gnu_field;
2946 /* Make the fields for the discriminants and put them into the record
2947 unless it's an Unchecked_Union. */
2949 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2950 Present (gnat_field);
2951 gnat_field = Next_Stored_Discriminant (gnat_field))
2953 /* If this is a record extension and this discriminant is the
2954 renaming of another discriminant, we've handled it above. */
2955 if (Present (Parent_Subtype (gnat_entity))
2956 && Present (Corresponding_Discriminant (gnat_field)))
2960 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2962 /* Make an expression using a PLACEHOLDER_EXPR from the
2963 FIELD_DECL node just created and link that with the
2964 corresponding GNAT defining identifier. */
2965 save_gnu_tree (gnat_field,
2966 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2967 build0 (PLACEHOLDER_EXPR, gnu_type),
2968 gnu_field, NULL_TREE),
2971 if (!is_unchecked_union)
2973 TREE_CHAIN (gnu_field) = gnu_field_list;
2974 gnu_field_list = gnu_field;
2978 /* Add the fields into the record type and finish it up. */
2979 components_to_record (gnu_type, Component_List (record_definition),
2980 gnu_field_list, packed, definition, NULL,
2981 false, all_rep, false, is_unchecked_union);
2983 /* If it is a tagged record force the type to BLKmode to insure that
2984 these objects will always be put in memory. Likewise for limited
2986 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2987 SET_TYPE_MODE (gnu_type, BLKmode);
2989 /* We used to remove the associations of the discriminants and _Parent
2990 for validity checking but we may need them if there's a Freeze_Node
2991 for a subtype used in this record. */
2992 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2994 /* Fill in locations of fields. */
2995 annotate_rep (gnat_entity, gnu_type);
2997 /* If there are any entities in the chain corresponding to components
2998 that we did not elaborate, ensure we elaborate their types if they
3000 for (gnat_temp = First_Entity (gnat_entity);
3001 Present (gnat_temp);
3002 gnat_temp = Next_Entity (gnat_temp))
3003 if ((Ekind (gnat_temp) == E_Component
3004 || Ekind (gnat_temp) == E_Discriminant)
3005 && Is_Itype (Etype (gnat_temp))
3006 && !present_gnu_tree (gnat_temp))
3007 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3011 case E_Class_Wide_Subtype:
3012 /* If an equivalent type is present, that is what we should use.
3013 Otherwise, fall through to handle this like a record subtype
3014 since it may have constraints. */
3015 if (gnat_equiv_type != gnat_entity)
3017 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3018 maybe_present = true;
3022 /* ... fall through ... */
3024 case E_Record_Subtype:
3025 /* If Cloned_Subtype is Present it means this record subtype has
3026 identical layout to that type or subtype and we should use
3027 that GCC type for this one. The front end guarantees that
3028 the component list is shared. */
3029 if (Present (Cloned_Subtype (gnat_entity)))
3031 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3033 maybe_present = true;
3037 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3038 changing the type, make a new type with each field having the type of
3039 the field in the new subtype but the position computed by transforming
3040 every discriminant reference according to the constraints. We don't
3041 see any difference between private and non-private type here since
3042 derivations from types should have been deferred until the completion
3043 of the private type. */
3046 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3051 defer_incomplete_level++;
3052 this_deferred = true;
3055 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3057 if (present_gnu_tree (gnat_entity))
3059 maybe_present = true;
3063 /* When the subtype has discriminants and these discriminants affect
3064 the initial shape it has inherited, factor them in. But for the
3065 of an Unchecked_Union (it must be an Itype), just return the type.
3067 We can't just test Is_Constrained because private subtypes without
3068 discriminants of types with discriminants with default expressions
3069 are Is_Constrained but aren't constrained! */
3070 if (IN (Ekind (gnat_base_type), Record_Kind)
3071 && !Is_Unchecked_Union (gnat_base_type)
3072 && !Is_For_Access_Subtype (gnat_entity)
3073 && Is_Constrained (gnat_entity)
3074 && Has_Discriminants (gnat_entity)
3075 && Present (Discriminant_Constraint (gnat_entity))
3076 && Stored_Constraint (gnat_entity) != No_Elist)
3079 = build_subst_list (gnat_entity, gnat_base_type, definition);
3080 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3081 tree gnu_unpad_base_type, t;
3082 Entity_Id gnat_field;
3084 gnu_type = make_node (RECORD_TYPE);
3085 TYPE_NAME (gnu_type) = gnu_entity_name;
3087 /* Set the size, alignment and alias set of the new type to
3088 match that of the old one, doing required substitutions.
3089 We do it this early because we need the size of the new
3090 type below to discard old fields if necessary. */
3091 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3092 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3093 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3094 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3095 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
3097 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3098 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3099 TYPE_SIZE (gnu_type)
3100 = substitute_in_expr (TYPE_SIZE (gnu_type),
3104 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3105 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3106 TYPE_SIZE_UNIT (gnu_type)
3107 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3111 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3112 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3114 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3118 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
3119 && TYPE_IS_PADDING_P (gnu_base_type))
3120 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3122 gnu_unpad_base_type = gnu_base_type;
3125 = compute_field_positions (gnu_unpad_base_type, NULL_TREE,
3126 size_zero_node, bitsize_zero_node,
3129 for (gnat_field = First_Entity (gnat_entity);
3130 Present (gnat_field);
3131 gnat_field = Next_Entity (gnat_field))
3132 if ((Ekind (gnat_field) == E_Component
3133 || Ekind (gnat_field) == E_Discriminant)
3134 && !(Present (Corresponding_Discriminant (gnat_field))
3135 && Is_Tagged_Type (gnat_base_type))
3136 && Underlying_Type (Scope (Original_Record_Component
3140 Name_Id gnat_name = Chars (gnat_field);
3141 Entity_Id gnat_old_field
3142 = Original_Record_Component (gnat_field);
3144 = gnat_to_gnu_field_decl (gnat_old_field);
3147 (purpose_member (gnu_old_field, gnu_pos_list));
3148 tree gnu_pos = TREE_PURPOSE (gnu_offset);
3149 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3150 tree gnu_field, gnu_field_type, gnu_size, gnu_new_pos;
3151 tree gnu_last = NULL_TREE;
3152 unsigned int offset_align
3154 (TREE_PURPOSE (TREE_VALUE (gnu_offset)), 1);
3156 /* If the type is the same, retrieve the GCC type from the
3157 old field to take into account possible adjustments. */
3158 if (Etype (gnat_field) == Etype (gnat_old_field))
3159 gnu_field_type = TREE_TYPE (gnu_old_field);
3161 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3163 /* If there was a component clause, the field types must be
3164 the same for the type and subtype, so copy the data from
3165 the old field to avoid recomputation here. Also if the
3166 field is justified modular and the optimization in
3167 gnat_to_gnu_field was applied. */
3168 if (Present (Component_Clause (gnat_old_field))
3169 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3170 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3171 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3172 == TREE_TYPE (gnu_old_field)))
3174 gnu_size = DECL_SIZE (gnu_old_field);
3175 gnu_field_type = TREE_TYPE (gnu_old_field);
3178 /* If the old field was packed and of constant size, we
3179 have to get the old size here, as it might differ from
3180 what the Etype conveys and the latter might overlap
3181 onto the following field. Try to arrange the type for
3182 possible better packing along the way. */
3183 else if (DECL_PACKED (gnu_old_field)
3184 && TREE_CODE (DECL_SIZE (gnu_old_field))
3187 gnu_size = DECL_SIZE (gnu_old_field);
3188 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3189 && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
3190 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3192 = make_packable_type (gnu_field_type, true);
3196 gnu_size = TYPE_SIZE (gnu_field_type);
3198 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3199 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3200 gnu_pos = substitute_in_expr (gnu_pos,
3204 /* If the position is now a constant, we can set it as the
3205 position of the field when we make it. Otherwise, we
3206 need to deal with it specially below. */
3207 if (TREE_CONSTANT (gnu_pos))
3209 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3211 /* Discard old fields that are outside the new type.
3212 This avoids confusing code scanning it to decide
3213 how to pass it to functions on some platforms. */
3214 if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3215 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3216 && !integer_zerop (gnu_size)
3217 && !tree_int_cst_lt (gnu_new_pos,
3218 TYPE_SIZE (gnu_type)))
3222 gnu_new_pos = NULL_TREE;
3226 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3227 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3228 !DECL_NONADDRESSABLE_P (gnu_old_field));
3230 if (!TREE_CONSTANT (gnu_pos))
3232 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3233 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3234 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3235 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3236 DECL_SIZE (gnu_field) = gnu_size;
3237 DECL_SIZE_UNIT (gnu_field)
3238 = convert (sizetype,
3239 size_binop (CEIL_DIV_EXPR, gnu_size,
3240 bitsize_unit_node));
3241 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3244 DECL_INTERNAL_P (gnu_field)
3245 = DECL_INTERNAL_P (gnu_old_field);
3246 SET_DECL_ORIGINAL_FIELD
3247 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3248 ? DECL_ORIGINAL_FIELD (gnu_old_field)
3250 DECL_DISCRIMINANT_NUMBER (gnu_field)
3251 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3252 TREE_THIS_VOLATILE (gnu_field)
3253 = TREE_THIS_VOLATILE (gnu_old_field);
3255 /* To match the layout crafted in components_to_record,
3256 if this is the _Tag or _Parent field, put it before
3257 any other fields. */
3258 if (gnat_name == Name_uTag || gnat_name == Name_uParent)
3259 gnu_field_list = chainon (gnu_field_list, gnu_field);
3261 /* Similarly, if this is the _Controller field, put
3262 it before the other fields except for the _Tag or
3264 else if (gnat_name == Name_uController && gnu_last)