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"
36 #include "tree-inline.h"
54 #ifndef MAX_FIXED_MODE_SIZE
55 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
58 /* Convention_Stdcall should be processed in a specific way on Windows targets
59 only. The macro below is a helper to avoid having to check for a Windows
60 specific attribute throughout this unit. */
62 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
63 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
65 #define Has_Stdcall_Convention(E) (0)
68 /* Stack realignment for functions with foreign conventions is provided on a
69 per back-end basis now, as it is handled by the prologue expanders and not
70 as part of the function's body any more. It might be requested by way of a
71 dedicated function type attribute on the targets that support it.
73 We need a way to avoid setting the attribute on the targets that don't
74 support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
76 It is defined on targets where the circuitry is available, and indicates
77 whether the realignment is needed for 'main'. We use this to decide for
78 foreign subprograms as well.
80 It is not defined on targets where the circuitry is not implemented, and
81 we just never set the attribute in these cases.
83 Whether it is defined on all targets that would need it in theory is
84 not entirely clear. We currently trust the base GCC settings for this
87 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
88 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
93 struct incomplete *next;
98 /* These variables are used to defer recursively expanding incomplete types
99 while we are processing an array, a record or a subprogram type. */
100 static int defer_incomplete_level = 0;
101 static struct incomplete *defer_incomplete_list;
103 /* This variable is used to delay expanding From_With_Type types until the
105 static struct incomplete *defer_limited_with;
107 /* These variables are used to defer finalizing types. The element of the
108 list is the TYPE_DECL associated with the type. */
109 static int defer_finalize_level = 0;
110 static VEC (tree,heap) *defer_finalize_list;
112 /* A hash table used to cache the result of annotate_value. */
113 static GTY ((if_marked ("tree_int_map_marked_p"),
114 param_is (struct tree_int_map))) htab_t annotate_value_cache;
123 static void relate_alias_sets (tree, tree, enum alias_set_op);
125 static tree build_subst_list (Entity_Id, Entity_Id, bool);
126 static bool allocatable_size_p (tree, bool);
127 static void prepend_one_attribute_to (struct attrib **,
128 enum attr_type, tree, tree, Node_Id);
129 static void prepend_attributes (Entity_Id, struct attrib **);
130 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
131 static bool is_variable_size (tree);
132 static tree elaborate_expression_1 (tree, Entity_Id, tree, bool, bool);
133 static tree make_packable_type (tree, bool);
134 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
135 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
137 static bool same_discriminant_p (Entity_Id, Entity_Id);
138 static bool array_type_has_nonaliased_component (Entity_Id, tree);
139 static bool compile_time_known_address_p (Node_Id);
140 static bool cannot_be_superflat_p (Node_Id);
141 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
142 bool, bool, bool, bool);
143 static Uint annotate_value (tree);
144 static void annotate_rep (Entity_Id, tree);
145 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
146 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
147 static void set_rm_size (Uint, tree, Entity_Id);
148 static tree make_type_from_size (tree, tree, bool);
149 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
150 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
151 static void check_ok_for_atomic (tree, Entity_Id, bool);
152 static int compatible_signatures_p (tree ftype1, tree ftype2);
153 static void rest_of_type_decl_compilation_no_defer (tree);
155 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
156 entity, return the equivalent GCC tree for that entity (a ..._DECL node)
157 and associate the ..._DECL node with the input GNAT defining identifier.
159 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
160 initial value (in GCC tree form). This is optional for a variable. For
161 a renamed entity, GNU_EXPR gives the object being renamed.
163 DEFINITION is nonzero if this call is intended for a definition. This is
164 used for separate compilation where it is necessary to know whether an
165 external declaration or a definition must be created if the GCC equivalent
166 was not created previously. The value of 1 is normally used for a nonzero
167 DEFINITION, but a value of 2 is used in special circumstances, defined in
171 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
173 /* Contains the kind of the input GNAT node. */
174 const Entity_Kind kind = Ekind (gnat_entity);
175 /* True if this is a type. */
176 const bool is_type = IN (kind, Type_Kind);
177 /* For a type, contains the equivalent GNAT node to be used in gigi. */
178 Entity_Id gnat_equiv_type = Empty;
179 /* Temporary used to walk the GNAT tree. */
181 /* Contains the GCC DECL node which is equivalent to the input GNAT node.
182 This node will be associated with the GNAT node by calling at the end
183 of the `switch' statement. */
184 tree gnu_decl = NULL_TREE;
185 /* Contains the GCC type to be used for the GCC node. */
186 tree gnu_type = NULL_TREE;
187 /* Contains the GCC size tree to be used for the GCC node. */
188 tree gnu_size = NULL_TREE;
189 /* Contains the GCC name to be used for the GCC node. */
190 tree gnu_entity_name;
191 /* True if we have already saved gnu_decl as a GNAT association. */
193 /* True if we incremented defer_incomplete_level. */
194 bool this_deferred = false;
195 /* True if we incremented force_global. */
196 bool this_global = false;
197 /* True if we should check to see if elaborated during processing. */
198 bool maybe_present = false;
199 /* True if we made GNU_DECL and its type here. */
200 bool this_made_decl = false;
201 /* True if debug info is requested for this entity. */
202 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
203 || debug_info_level == DINFO_LEVEL_VERBOSE);
204 /* True if this entity is to be considered as imported. */
205 bool imported_p = (Is_Imported (gnat_entity)
206 && No (Address_Clause (gnat_entity)));
207 /* Size and alignment of the GCC node, if meaningful. */
208 unsigned int esize = 0, align = 0;
209 /* Contains the list of attributes directly attached to the entity. */
210 struct attrib *attr_list = NULL;
212 /* Since a use of an Itype is a definition, process it as such if it
213 is not in a with'ed unit. */
216 && Is_Itype (gnat_entity)
217 && !present_gnu_tree (gnat_entity)
218 && In_Extended_Main_Code_Unit (gnat_entity))
220 /* Ensure that we are in a subprogram mentioned in the Scope chain of
221 this entity, our current scope is global, or we encountered a task
222 or entry (where we can't currently accurately check scoping). */
223 if (!current_function_decl
224 || DECL_ELABORATION_PROC_P (current_function_decl))
226 process_type (gnat_entity);
227 return get_gnu_tree (gnat_entity);
230 for (gnat_temp = Scope (gnat_entity);
232 gnat_temp = Scope (gnat_temp))
234 if (Is_Type (gnat_temp))
235 gnat_temp = Underlying_Type (gnat_temp);
237 if (Ekind (gnat_temp) == E_Subprogram_Body)
239 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
241 if (IN (Ekind (gnat_temp), Subprogram_Kind)
242 && Present (Protected_Body_Subprogram (gnat_temp)))
243 gnat_temp = Protected_Body_Subprogram (gnat_temp);
245 if (Ekind (gnat_temp) == E_Entry
246 || Ekind (gnat_temp) == E_Entry_Family
247 || Ekind (gnat_temp) == E_Task_Type
248 || (IN (Ekind (gnat_temp), Subprogram_Kind)
249 && present_gnu_tree (gnat_temp)
250 && (current_function_decl
251 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
253 process_type (gnat_entity);
254 return get_gnu_tree (gnat_entity);
258 /* This abort means the Itype has an incorrect scope, i.e. that its
259 scope does not correspond to the subprogram it is declared in. */
263 /* If we've already processed this entity, return what we got last time.
264 If we are defining the node, we should not have already processed it.
265 In that case, we will abort below when we try to save a new GCC tree
266 for this object. We also need to handle the case of getting a dummy
267 type when a Full_View exists. */
268 if ((!definition || (is_type && imported_p))
269 && present_gnu_tree (gnat_entity))
271 gnu_decl = get_gnu_tree (gnat_entity);
273 if (TREE_CODE (gnu_decl) == TYPE_DECL
274 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
275 && IN (kind, Incomplete_Or_Private_Kind)
276 && Present (Full_View (gnat_entity)))
279 = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
280 save_gnu_tree (gnat_entity, NULL_TREE, false);
281 save_gnu_tree (gnat_entity, gnu_decl, false);
287 /* If this is a numeric or enumeral type, or an access type, a nonzero
288 Esize must be specified unless it was specified by the programmer. */
289 gcc_assert (!Unknown_Esize (gnat_entity)
290 || Has_Size_Clause (gnat_entity)
291 || (!IN (kind, Numeric_Kind)
292 && !IN (kind, Enumeration_Kind)
293 && (!IN (kind, Access_Kind)
294 || kind == E_Access_Protected_Subprogram_Type
295 || kind == E_Anonymous_Access_Protected_Subprogram_Type
296 || kind == E_Access_Subtype)));
298 /* The RM size must be specified for all discrete and fixed-point types. */
299 gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
300 && Unknown_RM_Size (gnat_entity)));
302 /* If we get here, it means we have not yet done anything with this entity.
303 If we are not defining it, it must be a type or an entity that is defined
304 elsewhere or externally, otherwise we should have defined it already. */
305 gcc_assert (definition
306 || type_annotate_only
308 || kind == E_Discriminant
309 || kind == E_Component
311 || (kind == E_Constant && Present (Full_View (gnat_entity)))
312 || Is_Public (gnat_entity));
314 /* Get the name of the entity and set up the line number and filename of
315 the original definition for use in any decl we make. */
316 gnu_entity_name = get_entity_name (gnat_entity);
317 Sloc_to_locus (Sloc (gnat_entity), &input_location);
319 /* For cases when we are not defining (i.e., we are referencing from
320 another compilation unit) public entities, show we are at global level
321 for the purpose of computing scopes. Don't do this for components or
322 discriminants since the relevant test is whether or not the record is
325 && kind != E_Component
326 && kind != E_Discriminant
327 && Is_Public (gnat_entity)
328 && !Is_Statically_Allocated (gnat_entity))
329 force_global++, this_global = true;
331 /* Handle any attributes directly attached to the entity. */
332 if (Has_Gigi_Rep_Item (gnat_entity))
333 prepend_attributes (gnat_entity, &attr_list);
335 /* Do some common processing for types. */
338 /* Compute the equivalent type to be used in gigi. */
339 gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
341 /* Machine_Attributes on types are expected to be propagated to
342 subtypes. The corresponding Gigi_Rep_Items are only attached
343 to the first subtype though, so we handle the propagation here. */
344 if (Base_Type (gnat_entity) != gnat_entity
345 && !Is_First_Subtype (gnat_entity)
346 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
347 prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
350 /* Compute a default value for the size of the type. */
351 if (Known_Esize (gnat_entity)
352 && UI_Is_In_Int_Range (Esize (gnat_entity)))
354 unsigned int max_esize;
355 esize = UI_To_Int (Esize (gnat_entity));
357 if (IN (kind, Float_Kind))
358 max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
359 else if (IN (kind, Access_Kind))
360 max_esize = POINTER_SIZE * 2;
362 max_esize = LONG_LONG_TYPE_SIZE;
364 if (esize > max_esize)
368 esize = LONG_LONG_TYPE_SIZE;
374 /* If this is a use of a deferred constant without address clause,
375 get its full definition. */
377 && No (Address_Clause (gnat_entity))
378 && Present (Full_View (gnat_entity)))
381 = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
386 /* If we have an external constant that we are not defining, get the
387 expression that is was defined to represent. We may throw that
388 expression away later if it is not a constant. Do not retrieve the
389 expression if it is an aggregate or allocator, because in complex
390 instantiation contexts it may not be expanded */
392 && Present (Expression (Declaration_Node (gnat_entity)))
393 && !No_Initialization (Declaration_Node (gnat_entity))
394 && (Nkind (Expression (Declaration_Node (gnat_entity)))
396 && (Nkind (Expression (Declaration_Node (gnat_entity)))
398 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
400 /* Ignore deferred constant definitions without address clause since
401 they are processed fully in the front-end. If No_Initialization
402 is set, this is not a deferred constant but a constant whose value
403 is built manually. And constants that are renamings are handled
407 && No (Address_Clause (gnat_entity))
408 && !No_Initialization (Declaration_Node (gnat_entity))
409 && No (Renamed_Object (gnat_entity)))
411 gnu_decl = error_mark_node;
416 /* Ignore constant definitions already marked with the error node. See
417 the N_Object_Declaration case of gnat_to_gnu for the rationale. */
420 && present_gnu_tree (gnat_entity)
421 && get_gnu_tree (gnat_entity) == error_mark_node)
423 maybe_present = true;
430 /* We used to special case VMS exceptions here to directly map them to
431 their associated condition code. Since this code had to be masked
432 dynamically to strip off the severity bits, this caused trouble in
433 the GCC/ZCX case because the "type" pointers we store in the tables
434 have to be static. We now don't special case here anymore, and let
435 the regular processing take place, which leaves us with a regular
436 exception data object for VMS exceptions too. The condition code
437 mapping is taken care of by the front end and the bitmasking by the
444 /* The GNAT record where the component was defined. */
445 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
447 /* If the variable is an inherited record component (in the case of
448 extended record types), just return the inherited entity, which
449 must be a FIELD_DECL. Likewise for discriminants.
450 For discriminants of untagged records which have explicit
451 stored discriminants, return the entity for the corresponding
452 stored discriminant. Also use Original_Record_Component
453 if the record has a private extension. */
454 if (Present (Original_Record_Component (gnat_entity))
455 && Original_Record_Component (gnat_entity) != gnat_entity)
458 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
459 gnu_expr, definition);
464 /* If the enclosing record has explicit stored discriminants,
465 then it is an untagged record. If the Corresponding_Discriminant
466 is not empty then this must be a renamed discriminant and its
467 Original_Record_Component must point to the corresponding explicit
468 stored discriminant (i.e. we should have taken the previous
470 else if (Present (Corresponding_Discriminant (gnat_entity))
471 && Is_Tagged_Type (gnat_record))
473 /* A tagged record has no explicit stored discriminants. */
474 gcc_assert (First_Discriminant (gnat_record)
475 == First_Stored_Discriminant (gnat_record));
477 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
478 gnu_expr, definition);
483 else if (Present (CR_Discriminant (gnat_entity))
484 && type_annotate_only)
486 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
487 gnu_expr, definition);
492 /* If the enclosing record has explicit stored discriminants, then
493 it is an untagged record. If the Corresponding_Discriminant
494 is not empty then this must be a renamed discriminant and its
495 Original_Record_Component must point to the corresponding explicit
496 stored discriminant (i.e. we should have taken the first
498 else if (Present (Corresponding_Discriminant (gnat_entity))
499 && (First_Discriminant (gnat_record)
500 != First_Stored_Discriminant (gnat_record)))
503 /* Otherwise, if we are not defining this and we have no GCC type
504 for the containing record, make one for it. Then we should
505 have made our own equivalent. */
506 else if (!definition && !present_gnu_tree (gnat_record))
508 /* ??? If this is in a record whose scope is a protected
509 type and we have an Original_Record_Component, use it.
510 This is a workaround for major problems in protected type
512 Entity_Id Scop = Scope (Scope (gnat_entity));
513 if ((Is_Protected_Type (Scop)
514 || (Is_Private_Type (Scop)
515 && Present (Full_View (Scop))
516 && Is_Protected_Type (Full_View (Scop))))
517 && Present (Original_Record_Component (gnat_entity)))
520 = gnat_to_gnu_entity (Original_Record_Component
527 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
528 gnu_decl = get_gnu_tree (gnat_entity);
534 /* Here we have no GCC type and this is a reference rather than a
535 definition. This should never happen. Most likely the cause is
536 reference before declaration in the gnat tree for gnat_entity. */
540 case E_Loop_Parameter:
541 case E_Out_Parameter:
544 /* Simple variables, loop variables, Out parameters, and exceptions. */
547 bool used_by_ref = false;
549 = ((kind == E_Constant || kind == E_Variable)
550 && Is_True_Constant (gnat_entity)
551 && !Treat_As_Volatile (gnat_entity)
552 && (((Nkind (Declaration_Node (gnat_entity))
553 == N_Object_Declaration)
554 && Present (Expression (Declaration_Node (gnat_entity))))
555 || Present (Renamed_Object (gnat_entity))));
556 bool inner_const_flag = const_flag;
557 bool static_p = Is_Statically_Allocated (gnat_entity);
558 bool mutable_p = false;
559 tree gnu_ext_name = NULL_TREE;
560 tree renamed_obj = NULL_TREE;
561 tree gnu_object_size;
563 if (Present (Renamed_Object (gnat_entity)) && !definition)
565 if (kind == E_Exception)
566 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
569 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
572 /* Get the type after elaborating the renamed object. */
573 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
575 /* For a debug renaming declaration, build a pure debug entity. */
576 if (Present (Debug_Renaming_Link (gnat_entity)))
579 gnu_decl = build_decl (input_location,
580 VAR_DECL, gnu_entity_name, gnu_type);
581 /* The (MEM (CONST (0))) pattern is prescribed by STABS. */
582 if (global_bindings_p ())
583 addr = gen_rtx_CONST (VOIDmode, const0_rtx);
585 addr = stack_pointer_rtx;
586 SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
587 gnat_pushdecl (gnu_decl, gnat_entity);
591 /* If this is a loop variable, its type should be the base type.
592 This is because the code for processing a loop determines whether
593 a normal loop end test can be done by comparing the bounds of the
594 loop against those of the base type, which is presumed to be the
595 size used for computation. But this is not correct when the size
596 of the subtype is smaller than the type. */
597 if (kind == E_Loop_Parameter)
598 gnu_type = get_base_type (gnu_type);
600 /* Reject non-renamed objects whose types are unconstrained arrays or
601 any object whose type is a dummy type or VOID_TYPE. */
603 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
604 && No (Renamed_Object (gnat_entity)))
605 || TYPE_IS_DUMMY_P (gnu_type)
606 || TREE_CODE (gnu_type) == VOID_TYPE)
608 gcc_assert (type_annotate_only);
611 return error_mark_node;
614 /* If an alignment is specified, use it if valid. Note that exceptions
615 are objects but don't have an alignment. We must do this before we
616 validate the size, since the alignment can affect the size. */
617 if (kind != E_Exception && Known_Alignment (gnat_entity))
619 gcc_assert (Present (Alignment (gnat_entity)));
620 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
621 TYPE_ALIGN (gnu_type));
622 /* No point in changing the type if there is an address clause
623 as the final type of the object will be a reference type. */
624 if (Present (Address_Clause (gnat_entity)))
628 = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
629 "PAD", false, definition, true);
632 /* If we are defining the object, see if it has a Size value and
633 validate it if so. If we are not defining the object and a Size
634 clause applies, simply retrieve the value. We don't want to ignore
635 the clause and it is expected to have been validated already. Then
636 get the new type, if any. */
638 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
639 gnat_entity, VAR_DECL, false,
640 Has_Size_Clause (gnat_entity));
641 else if (Has_Size_Clause (gnat_entity))
642 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
647 = make_type_from_size (gnu_type, gnu_size,
648 Has_Biased_Representation (gnat_entity));
650 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
651 gnu_size = NULL_TREE;
654 /* If this object has self-referential size, it must be a record with
655 a default value. We are supposed to allocate an object of the
656 maximum size in this case unless it is a constant with an
657 initializing expression, in which case we can get the size from
658 that. Note that the resulting size may still be a variable, so
659 this may end up with an indirect allocation. */
660 if (No (Renamed_Object (gnat_entity))
661 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
663 if (gnu_expr && kind == E_Constant)
665 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
666 if (CONTAINS_PLACEHOLDER_P (size))
668 /* If the initializing expression is itself a constant,
669 despite having a nominal type with self-referential
670 size, we can get the size directly from it. */
671 if (TREE_CODE (gnu_expr) == COMPONENT_REF
672 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
675 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
676 && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
677 && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
678 || DECL_READONLY_ONCE_ELAB
679 (TREE_OPERAND (gnu_expr, 0))))
680 gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
683 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
688 /* We may have no GNU_EXPR because No_Initialization is
689 set even though there's an Expression. */
690 else if (kind == E_Constant
691 && (Nkind (Declaration_Node (gnat_entity))
692 == N_Object_Declaration)
693 && Present (Expression (Declaration_Node (gnat_entity))))
695 = TYPE_SIZE (gnat_to_gnu_type
697 (Expression (Declaration_Node (gnat_entity)))));
700 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
705 /* If the size is zero bytes, make it one byte since some linkers have
706 trouble with zero-sized objects. If the object will have a
707 template, that will make it nonzero so don't bother. Also avoid
708 doing that for an object renaming or an object with an address
709 clause, as we would lose useful information on the view size
710 (e.g. for null array slices) and we are not allocating the object
713 && integer_zerop (gnu_size)
714 && !TREE_OVERFLOW (gnu_size))
715 || (TYPE_SIZE (gnu_type)
716 && integer_zerop (TYPE_SIZE (gnu_type))
717 && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
718 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
719 || !Is_Array_Type (Etype (gnat_entity)))
720 && No (Renamed_Object (gnat_entity))
721 && No (Address_Clause (gnat_entity)))
722 gnu_size = bitsize_unit_node;
724 /* If this is an object with no specified size and alignment, and
725 if either it is atomic or we are not optimizing alignment for
726 space and it is composite and not an exception, an Out parameter
727 or a reference to another object, and the size of its type is a
728 constant, set the alignment to the smallest one which is not
729 smaller than the size, with an appropriate cap. */
730 if (!gnu_size && align == 0
731 && (Is_Atomic (gnat_entity)
732 || (!Optimize_Alignment_Space (gnat_entity)
733 && kind != E_Exception
734 && kind != E_Out_Parameter
735 && Is_Composite_Type (Etype (gnat_entity))
736 && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
738 && No (Renamed_Object (gnat_entity))
739 && No (Address_Clause (gnat_entity))))
740 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
742 /* No point in jumping through all the hoops needed in order
743 to support BIGGEST_ALIGNMENT if we don't really have to.
744 So we cap to the smallest alignment that corresponds to
745 a known efficient memory access pattern of the target. */
746 unsigned int align_cap = Is_Atomic (gnat_entity)
748 : get_mode_alignment (ptr_mode);
750 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
751 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
754 align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
756 /* But make sure not to under-align the object. */
757 if (align <= TYPE_ALIGN (gnu_type))
760 /* And honor the minimum valid atomic alignment, if any. */
761 #ifdef MINIMUM_ATOMIC_ALIGNMENT
762 else if (align < MINIMUM_ATOMIC_ALIGNMENT)
763 align = MINIMUM_ATOMIC_ALIGNMENT;
767 /* If the object is set to have atomic components, find the component
768 type and validate it.
770 ??? Note that we ignore Has_Volatile_Components on objects; it's
771 not at all clear what to do in that case. */
773 if (Has_Atomic_Components (gnat_entity))
775 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
776 ? TREE_TYPE (gnu_type) : gnu_type);
778 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
779 && TYPE_MULTI_ARRAY_P (gnu_inner))
780 gnu_inner = TREE_TYPE (gnu_inner);
782 check_ok_for_atomic (gnu_inner, gnat_entity, true);
785 /* Now check if the type of the object allows atomic access. Note
786 that we must test the type, even if this object has size and
787 alignment to allow such access, because we will be going
788 inside the padded record to assign to the object. We could fix
789 this by always copying via an intermediate value, but it's not
790 clear it's worth the effort. */
791 if (Is_Atomic (gnat_entity))
792 check_ok_for_atomic (gnu_type, gnat_entity, false);
794 /* If this is an aliased object with an unconstrained nominal subtype,
795 make a type that includes the template. */
796 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
797 && Is_Array_Type (Etype (gnat_entity))
798 && !type_annotate_only)
801 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
804 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
805 concat_name (gnu_entity_name,
809 #ifdef MINIMUM_ATOMIC_ALIGNMENT
810 /* If the size is a constant and no alignment is specified, force
811 the alignment to be the minimum valid atomic alignment. The
812 restriction on constant size avoids problems with variable-size
813 temporaries; if the size is variable, there's no issue with
814 atomic access. Also don't do this for a constant, since it isn't
815 necessary and can interfere with constant replacement. Finally,
816 do not do it for Out parameters since that creates an
817 size inconsistency with In parameters. */
818 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
819 && !FLOAT_TYPE_P (gnu_type)
820 && !const_flag && No (Renamed_Object (gnat_entity))
821 && !imported_p && No (Address_Clause (gnat_entity))
822 && kind != E_Out_Parameter
823 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
824 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
825 align = MINIMUM_ATOMIC_ALIGNMENT;
828 /* Make a new type with the desired size and alignment, if needed.
829 But do not take into account alignment promotions to compute the
830 size of the object. */
831 gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
832 if (gnu_size || align > 0)
833 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
834 "PAD", false, definition,
835 gnu_size ? true : false);
837 /* If this is a renaming, avoid as much as possible to create a new
838 object. However, in several cases, creating it is required.
839 This processing needs to be applied to the raw expression so
840 as to make it more likely to rename the underlying object. */
841 if (Present (Renamed_Object (gnat_entity)))
843 bool create_normal_object = false;
845 /* If the renamed object had padding, strip off the reference
846 to the inner object and reset our type. */
847 if ((TREE_CODE (gnu_expr) == COMPONENT_REF
848 && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
850 && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
851 /* Strip useless conversions around the object. */
852 || (TREE_CODE (gnu_expr) == NOP_EXPR
853 && gnat_types_compatible_p
854 (TREE_TYPE (gnu_expr),
855 TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
857 gnu_expr = TREE_OPERAND (gnu_expr, 0);
858 gnu_type = TREE_TYPE (gnu_expr);
861 /* Case 1: If this is a constant renaming stemming from a function
862 call, treat it as a normal object whose initial value is what
863 is being renamed. RM 3.3 says that the result of evaluating a
864 function call is a constant object. As a consequence, it can
865 be the inner object of a constant renaming. In this case, the
866 renaming must be fully instantiated, i.e. it cannot be a mere
867 reference to (part of) an existing object. */
870 tree inner_object = gnu_expr;
871 while (handled_component_p (inner_object))
872 inner_object = TREE_OPERAND (inner_object, 0);
873 if (TREE_CODE (inner_object) == CALL_EXPR)
874 create_normal_object = true;
877 /* Otherwise, see if we can proceed with a stabilized version of
878 the renamed entity or if we need to make a new object. */
879 if (!create_normal_object)
881 tree maybe_stable_expr = NULL_TREE;
884 /* Case 2: If the renaming entity need not be materialized and
885 the renamed expression is something we can stabilize, use
886 that for the renaming. At the global level, we can only do
887 this if we know no SAVE_EXPRs need be made, because the
888 expression we return might be used in arbitrary conditional
889 branches so we must force the SAVE_EXPRs evaluation
890 immediately and this requires a function context. */
891 if (!Materialize_Entity (gnat_entity)
892 && (!global_bindings_p ()
893 || (staticp (gnu_expr)
894 && !TREE_SIDE_EFFECTS (gnu_expr))))
897 = maybe_stabilize_reference (gnu_expr, true, &stable);
901 gnu_decl = maybe_stable_expr;
902 /* ??? No DECL_EXPR is created so we need to mark
903 the expression manually lest it is shared. */
904 if (global_bindings_p ())
905 mark_visited (&gnu_decl);
906 save_gnu_tree (gnat_entity, gnu_decl, true);
908 annotate_object (gnat_entity, gnu_type, NULL_TREE,
913 /* The stabilization failed. Keep maybe_stable_expr
914 untouched here to let the pointer case below know
915 about that failure. */
918 /* Case 3: If this is a constant renaming and creating a
919 new object is allowed and cheap, treat it as a normal
920 object whose initial value is what is being renamed. */
922 && !Is_Composite_Type
923 (Underlying_Type (Etype (gnat_entity))))
926 /* Case 4: Make this into a constant pointer to the object we
927 are to rename and attach the object to the pointer if it is
928 something we can stabilize.
930 From the proper scope, attached objects will be referenced
931 directly instead of indirectly via the pointer to avoid
932 subtle aliasing problems with non-addressable entities.
933 They have to be stable because we must not evaluate the
934 variables in the expression every time the renaming is used.
935 The pointer is called a "renaming" pointer in this case.
937 In the rare cases where we cannot stabilize the renamed
938 object, we just make a "bare" pointer, and the renamed
939 entity is always accessed indirectly through it. */
942 gnu_type = build_reference_type (gnu_type);
943 inner_const_flag = TREE_READONLY (gnu_expr);
946 /* If the previous attempt at stabilizing failed, there
947 is no point in trying again and we reuse the result
948 without attaching it to the pointer. In this case it
949 will only be used as the initializing expression of
950 the pointer and thus needs no special treatment with
951 regard to multiple evaluations. */
952 if (maybe_stable_expr)
955 /* Otherwise, try to stabilize and attach the expression
956 to the pointer if the stabilization succeeds.
958 Note that this might introduce SAVE_EXPRs and we don't
959 check whether we're at the global level or not. This
960 is fine since we are building a pointer initializer and
961 neither the pointer nor the initializing expression can
962 be accessed before the pointer elaboration has taken
963 place in a correct program.
965 These SAVE_EXPRs will be evaluated at the right place
966 by either the evaluation of the initializer for the
967 non-global case or the elaboration code for the global
968 case, and will be attached to the elaboration procedure
969 in the latter case. */
973 = maybe_stabilize_reference (gnu_expr, true, &stable);
976 renamed_obj = maybe_stable_expr;
978 /* Attaching is actually performed downstream, as soon
979 as we have a VAR_DECL for the pointer we make. */
983 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
985 gnu_size = NULL_TREE;
991 /* Make a volatile version of this object's type if we are to make
992 the object volatile. We also interpret 13.3(19) conservatively
993 and disallow any optimizations for such a non-constant object. */
994 if ((Treat_As_Volatile (gnat_entity)
996 && (Is_Exported (gnat_entity)
997 || Is_Imported (gnat_entity)
998 || Present (Address_Clause (gnat_entity)))))
999 && !TYPE_VOLATILE (gnu_type))
1000 gnu_type = build_qualified_type (gnu_type,
1001 (TYPE_QUALS (gnu_type)
1002 | TYPE_QUAL_VOLATILE));
1004 /* If we are defining an aliased object whose nominal subtype is
1005 unconstrained, the object is a record that contains both the
1006 template and the object. If there is an initializer, it will
1007 have already been converted to the right type, but we need to
1008 create the template if there is no initializer. */
1011 && TREE_CODE (gnu_type) == RECORD_TYPE
1012 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1013 /* Beware that padding might have been introduced
1014 via maybe_pad_type above. */
1015 || (TYPE_IS_PADDING_P (gnu_type)
1016 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1018 && TYPE_CONTAINS_TEMPLATE_P
1019 (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1022 = TYPE_IS_PADDING_P (gnu_type)
1023 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1024 : TYPE_FIELDS (gnu_type);
1027 = gnat_build_constructor
1031 build_template (TREE_TYPE (template_field),
1032 TREE_TYPE (TREE_CHAIN (template_field)),
1037 /* Convert the expression to the type of the object except in the
1038 case where the object's type is unconstrained or the object's type
1039 is a padded record whose field is of self-referential size. In
1040 the former case, converting will generate unnecessary evaluations
1041 of the CONSTRUCTOR to compute the size and in the latter case, we
1042 want to only copy the actual data. */
1044 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1045 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1046 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1047 && TYPE_IS_PADDING_P (gnu_type)
1048 && (CONTAINS_PLACEHOLDER_P
1049 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1050 gnu_expr = convert (gnu_type, gnu_expr);
1052 /* If this is a pointer and it does not have an initializing
1053 expression, initialize it to NULL, unless the object is
1056 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1057 && !Is_Imported (gnat_entity) && !gnu_expr)
1058 gnu_expr = integer_zero_node;
1060 /* If we are defining the object and it has an Address clause, we must
1061 either get the address expression from the saved GCC tree for the
1062 object if it has a Freeze node, or elaborate the address expression
1063 here since the front-end has guaranteed that the elaboration has no
1064 effects in this case. */
1065 if (definition && Present (Address_Clause (gnat_entity)))
1068 = present_gnu_tree (gnat_entity)
1069 ? get_gnu_tree (gnat_entity)
1070 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1072 save_gnu_tree (gnat_entity, NULL_TREE, false);
1074 /* Ignore the size. It's either meaningless or was handled
1076 gnu_size = NULL_TREE;
1077 /* Convert the type of the object to a reference type that can
1078 alias everything as per 13.3(19). */
1080 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1081 gnu_address = convert (gnu_type, gnu_address);
1083 const_flag = !Is_Public (gnat_entity)
1084 || compile_time_known_address_p (Expression (Address_Clause
1087 /* If this is a deferred constant, the initializer is attached to
1089 if (kind == E_Constant && Present (Full_View (gnat_entity)))
1092 (Expression (Declaration_Node (Full_View (gnat_entity))));
1094 /* If we don't have an initializing expression for the underlying
1095 variable, the initializing expression for the pointer is the
1096 specified address. Otherwise, we have to make a COMPOUND_EXPR
1097 to assign both the address and the initial value. */
1099 gnu_expr = gnu_address;
1102 = build2 (COMPOUND_EXPR, gnu_type,
1104 (MODIFY_EXPR, NULL_TREE,
1105 build_unary_op (INDIRECT_REF, NULL_TREE,
1111 /* If it has an address clause and we are not defining it, mark it
1112 as an indirect object. Likewise for Stdcall objects that are
1114 if ((!definition && Present (Address_Clause (gnat_entity)))
1115 || (Is_Imported (gnat_entity)
1116 && Has_Stdcall_Convention (gnat_entity)))
1118 /* Convert the type of the object to a reference type that can
1119 alias everything as per 13.3(19). */
1121 = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1122 gnu_size = NULL_TREE;
1124 /* No point in taking the address of an initializing expression
1125 that isn't going to be used. */
1126 gnu_expr = NULL_TREE;
1128 /* If it has an address clause whose value is known at compile
1129 time, make the object a CONST_DECL. This will avoid a
1130 useless dereference. */
1131 if (Present (Address_Clause (gnat_entity)))
1133 Node_Id gnat_address
1134 = Expression (Address_Clause (gnat_entity));
1136 if (compile_time_known_address_p (gnat_address))
1138 gnu_expr = gnat_to_gnu (gnat_address);
1146 /* If we are at top level and this object is of variable size,
1147 make the actual type a hidden pointer to the real type and
1148 make the initializer be a memory allocation and initialization.
1149 Likewise for objects we aren't defining (presumed to be
1150 external references from other packages), but there we do
1151 not set up an initialization.
1153 If the object's size overflows, make an allocator too, so that
1154 Storage_Error gets raised. Note that we will never free
1155 such memory, so we presume it never will get allocated. */
1157 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1158 global_bindings_p () || !definition
1161 && ! allocatable_size_p (gnu_size,
1162 global_bindings_p () || !definition
1165 gnu_type = build_reference_type (gnu_type);
1166 gnu_size = NULL_TREE;
1170 /* In case this was a aliased object whose nominal subtype is
1171 unconstrained, the pointer above will be a thin pointer and
1172 build_allocator will automatically make the template.
1174 If we have a template initializer only (that we made above),
1175 pretend there is none and rely on what build_allocator creates
1176 again anyway. Otherwise (if we have a full initializer), get
1177 the data part and feed that to build_allocator.
1179 If we are elaborating a mutable object, tell build_allocator to
1180 ignore a possibly simpler size from the initializer, if any, as
1181 we must allocate the maximum possible size in this case. */
1185 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1187 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1188 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1191 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1193 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1194 && 1 == VEC_length (constructor_elt,
1195 CONSTRUCTOR_ELTS (gnu_expr)))
1199 = build_component_ref
1200 (gnu_expr, NULL_TREE,
1201 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1205 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1206 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1207 && !Is_Imported (gnat_entity))
1208 post_error ("?Storage_Error will be raised at run-time!",
1212 = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1213 Empty, Empty, gnat_entity, mutable_p);
1217 gnu_expr = NULL_TREE;
1222 /* If this object would go into the stack and has an alignment larger
1223 than the largest stack alignment the back-end can honor, resort to
1224 a variable of "aligning type". */
1225 if (!global_bindings_p () && !static_p && definition
1226 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1228 /* Create the new variable. No need for extra room before the
1229 aligned field as this is in automatic storage. */
1231 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1232 TYPE_SIZE_UNIT (gnu_type),
1233 BIGGEST_ALIGNMENT, 0);
1235 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1236 NULL_TREE, gnu_new_type, NULL_TREE, false,
1237 false, false, false, NULL, gnat_entity);
1239 /* Initialize the aligned field if we have an initializer. */
1242 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1244 (gnu_new_var, NULL_TREE,
1245 TYPE_FIELDS (gnu_new_type), false),
1249 /* And setup this entity as a reference to the aligned field. */
1250 gnu_type = build_reference_type (gnu_type);
1253 (ADDR_EXPR, gnu_type,
1254 build_component_ref (gnu_new_var, NULL_TREE,
1255 TYPE_FIELDS (gnu_new_type), false));
1257 gnu_size = NULL_TREE;
1263 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1264 | TYPE_QUAL_CONST));
1266 /* Convert the expression to the type of the object except in the
1267 case where the object's type is unconstrained or the object's type
1268 is a padded record whose field is of self-referential size. In
1269 the former case, converting will generate unnecessary evaluations
1270 of the CONSTRUCTOR to compute the size and in the latter case, we
1271 want to only copy the actual data. */
1273 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1274 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1275 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1276 && TYPE_IS_PADDING_P (gnu_type)
1277 && (CONTAINS_PLACEHOLDER_P
1278 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1279 gnu_expr = convert (gnu_type, gnu_expr);
1281 /* If this name is external or there was a name specified, use it,
1282 unless this is a VMS exception object since this would conflict
1283 with the symbol we need to export in addition. Don't use the
1284 Interface_Name if there is an address clause (see CD30005). */
1285 if (!Is_VMS_Exception (gnat_entity)
1286 && ((Present (Interface_Name (gnat_entity))
1287 && No (Address_Clause (gnat_entity)))
1288 || (Is_Public (gnat_entity)
1289 && (!Is_Imported (gnat_entity)
1290 || Is_Exported (gnat_entity)))))
1291 gnu_ext_name = create_concat_name (gnat_entity, NULL);
1293 /* If this is constant initialized to a static constant and the
1294 object has an aggregate type, force it to be statically
1295 allocated. This will avoid an initialization copy. */
1296 if (!static_p && const_flag
1297 && gnu_expr && TREE_CONSTANT (gnu_expr)
1298 && AGGREGATE_TYPE_P (gnu_type)
1299 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1300 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1301 && TYPE_IS_PADDING_P (gnu_type)
1302 && !host_integerp (TYPE_SIZE_UNIT
1303 (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1306 gnu_decl = create_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1307 gnu_expr, const_flag,
1308 Is_Public (gnat_entity),
1309 imported_p || !definition,
1310 static_p, attr_list, gnat_entity);
1311 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1312 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1313 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1315 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1316 if (global_bindings_p ())
1318 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1319 record_global_renaming_pointer (gnu_decl);
1323 if (definition && DECL_SIZE_UNIT (gnu_decl)
1324 && get_block_jmpbuf_decl ()
1325 && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1326 || (flag_stack_check == GENERIC_STACK_CHECK
1327 && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1328 STACK_CHECK_MAX_VAR_SIZE) > 0)))
1329 add_stmt_with_node (build_call_1_expr
1330 (update_setjmp_buf_decl,
1331 build_unary_op (ADDR_EXPR, NULL_TREE,
1332 get_block_jmpbuf_decl ())),
1335 /* If we are defining an Out parameter and we're not optimizing,
1336 create a fake PARM_DECL for debugging purposes and make it
1337 point to the VAR_DECL. Suppress debug info for the latter
1338 but make sure it will still live on the stack so it can be
1339 accessed from within the debugger through the PARM_DECL. */
1340 if (kind == E_Out_Parameter && definition && !optimize)
1342 tree param = create_param_decl (gnu_entity_name, gnu_type, false);
1343 gnat_pushdecl (param, gnat_entity);
1344 SET_DECL_VALUE_EXPR (param, gnu_decl);
1345 DECL_HAS_VALUE_EXPR_P (param) = 1;
1347 debug_info_p = false;
1349 DECL_IGNORED_P (param) = 1;
1350 TREE_ADDRESSABLE (gnu_decl) = 1;
1353 /* If this is a public constant or we're not optimizing and we're not
1354 making a VAR_DECL for it, make one just for export or debugger use.
1355 Likewise if the address is taken or if either the object or type is
1356 aliased. Make an external declaration for a reference, unless this
1357 is a Standard entity since there no real symbol at the object level
1359 if (TREE_CODE (gnu_decl) == CONST_DECL
1360 && (definition || Sloc (gnat_entity) > Standard_Location)
1361 && ((Is_Public (gnat_entity) && No (Address_Clause (gnat_entity)))
1363 || Address_Taken (gnat_entity)
1364 || Is_Aliased (gnat_entity)
1365 || Is_Aliased (Etype (gnat_entity))))
1368 = create_true_var_decl (gnu_entity_name, gnu_ext_name, gnu_type,
1369 gnu_expr, true, Is_Public (gnat_entity),
1370 !definition, static_p, NULL,
1373 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1375 /* As debugging information will be generated for the variable,
1376 do not generate information for the constant. */
1377 DECL_IGNORED_P (gnu_decl) = 1;
1380 /* If this is declared in a block that contains a block with an
1381 exception handler, we must force this variable in memory to
1382 suppress an invalid optimization. */
1383 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1384 && Exception_Mechanism != Back_End_Exceptions)
1385 TREE_ADDRESSABLE (gnu_decl) = 1;
1387 /* Back-annotate Esize and Alignment of the object if not already
1388 known. Note that we pick the values of the type, not those of
1389 the object, to shield ourselves from low-level platform-dependent
1390 adjustments like alignment promotion. This is both consistent with
1391 all the treatment above, where alignment and size are set on the
1392 type of the object and not on the object directly, and makes it
1393 possible to support all confirming representation clauses. */
1394 annotate_object (gnat_entity, TREE_TYPE (gnu_decl), gnu_object_size,
1400 /* Return a TYPE_DECL for "void" that we previously made. */
1401 gnu_decl = TYPE_NAME (void_type_node);
1404 case E_Enumeration_Type:
1405 /* A special case: for the types Character and Wide_Character in
1406 Standard, we do not list all the literals. So if the literals
1407 are not specified, make this an unsigned type. */
1408 if (No (First_Literal (gnat_entity)))
1410 gnu_type = make_unsigned_type (esize);
1411 TYPE_NAME (gnu_type) = gnu_entity_name;
1413 /* Set TYPE_STRING_FLAG for Character and Wide_Character types.
1414 This is needed by the DWARF-2 back-end to distinguish between
1415 unsigned integer types and character types. */
1416 TYPE_STRING_FLAG (gnu_type) = 1;
1420 /* Normal case of non-character type or non-Standard character type. */
1422 /* Here we have a list of enumeral constants in First_Literal.
1423 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1424 the list to be placed into TYPE_FIELDS. Each node in the list
1425 is a TREE_LIST whose TREE_VALUE is the literal name and whose
1426 TREE_PURPOSE is the value of the literal. */
1428 Entity_Id gnat_literal;
1429 tree gnu_literal_list = NULL_TREE;
1431 if (Is_Unsigned_Type (gnat_entity))
1432 gnu_type = make_unsigned_type (esize);
1434 gnu_type = make_signed_type (esize);
1436 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1438 for (gnat_literal = First_Literal (gnat_entity);
1439 Present (gnat_literal);
1440 gnat_literal = Next_Literal (gnat_literal))
1442 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1445 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1446 gnu_type, gnu_value, true, false, false,
1447 false, NULL, gnat_literal);
1449 save_gnu_tree (gnat_literal, gnu_literal, false);
1450 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1451 gnu_value, gnu_literal_list);
1454 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1456 /* Note that the bounds are updated at the end of this function
1457 to avoid an infinite recursion since they refer to the type. */
1461 case E_Signed_Integer_Type:
1462 case E_Ordinary_Fixed_Point_Type:
1463 case E_Decimal_Fixed_Point_Type:
1464 /* For integer types, just make a signed type the appropriate number
1466 gnu_type = make_signed_type (esize);
1469 case E_Modular_Integer_Type:
1471 /* For modular types, make the unsigned type of the proper number
1472 of bits and then set up the modulus, if required. */
1473 tree gnu_modulus, gnu_high = NULL_TREE;
1475 /* Packed array types are supposed to be subtypes only. */
1476 gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1478 gnu_type = make_unsigned_type (esize);
1480 /* Get the modulus in this type. If it overflows, assume it is because
1481 it is equal to 2**Esize. Note that there is no overflow checking
1482 done on unsigned type, so we detect the overflow by looking for
1483 a modulus of zero, which is otherwise invalid. */
1484 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1486 if (!integer_zerop (gnu_modulus))
1488 TYPE_MODULAR_P (gnu_type) = 1;
1489 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1490 gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1491 convert (gnu_type, integer_one_node));
1494 /* If the upper bound is not maximal, make an extra subtype. */
1496 && !tree_int_cst_equal (gnu_high, TYPE_MAX_VALUE (gnu_type)))
1498 tree gnu_subtype = make_unsigned_type (esize);
1499 SET_TYPE_RM_MAX_VALUE (gnu_subtype, gnu_high);
1500 TREE_TYPE (gnu_subtype) = gnu_type;
1501 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1502 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1503 gnu_type = gnu_subtype;
1508 case E_Signed_Integer_Subtype:
1509 case E_Enumeration_Subtype:
1510 case E_Modular_Integer_Subtype:
1511 case E_Ordinary_Fixed_Point_Subtype:
1512 case E_Decimal_Fixed_Point_Subtype:
1514 /* For integral subtypes, we make a new INTEGER_TYPE. Note that we do
1515 not want to call create_range_type since we would like each subtype
1516 node to be distinct. ??? Historically this was in preparation for
1517 when memory aliasing is implemented, but that's obsolete now given
1518 the call to relate_alias_sets below.
1520 The TREE_TYPE field of the INTEGER_TYPE points to the base type;
1521 this fact is used by the arithmetic conversion functions.
1523 We elaborate the Ancestor_Subtype if it is not in the current unit
1524 and one of our bounds is non-static. We do this to ensure consistent
1525 naming in the case where several subtypes share the same bounds, by
1526 elaborating the first such subtype first, thus using its name. */
1529 && Present (Ancestor_Subtype (gnat_entity))
1530 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1531 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1532 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1533 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1535 /* Set the precision to the Esize except for bit-packed arrays. */
1536 if (Is_Packed_Array_Type (gnat_entity)
1537 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1538 esize = UI_To_Int (RM_Size (gnat_entity));
1540 /* This should be an unsigned type if the base type is unsigned or
1541 if the lower bound is constant and non-negative or if the type
1543 if (Is_Unsigned_Type (Etype (gnat_entity))
1544 || Is_Unsigned_Type (gnat_entity)
1545 || Has_Biased_Representation (gnat_entity))
1546 gnu_type = make_unsigned_type (esize);
1548 gnu_type = make_signed_type (esize);
1549 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1551 SET_TYPE_RM_MIN_VALUE
1553 convert (TREE_TYPE (gnu_type),
1554 elaborate_expression (Type_Low_Bound (gnat_entity),
1555 gnat_entity, get_identifier ("L"),
1557 Needs_Debug_Info (gnat_entity))));
1559 SET_TYPE_RM_MAX_VALUE
1561 convert (TREE_TYPE (gnu_type),
1562 elaborate_expression (Type_High_Bound (gnat_entity),
1563 gnat_entity, get_identifier ("U"),
1565 Needs_Debug_Info (gnat_entity))));
1567 /* One of the above calls might have caused us to be elaborated,
1568 so don't blow up if so. */
1569 if (present_gnu_tree (gnat_entity))
1571 maybe_present = true;
1575 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1576 = Has_Biased_Representation (gnat_entity);
1578 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
1579 TYPE_STUB_DECL (gnu_type)
1580 = create_type_stub_decl (gnu_entity_name, gnu_type);
1582 /* Inherit our alias set from what we're a subtype of. Subtypes
1583 are not different types and a pointer can designate any instance
1584 within a subtype hierarchy. */
1585 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1587 /* For a packed array, make the original array type a parallel type. */
1589 && Is_Packed_Array_Type (gnat_entity)
1590 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1591 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1593 (Original_Array_Type (gnat_entity)));
1595 /* If the type we are dealing with represents a bit-packed array,
1596 we need to have the bits left justified on big-endian targets
1597 and right justified on little-endian targets. We also need to
1598 ensure that when the value is read (e.g. for comparison of two
1599 such values), we only get the good bits, since the unused bits
1600 are uninitialized. Both goals are accomplished by wrapping up
1601 the modular type in an enclosing record type. */
1602 if (Is_Packed_Array_Type (gnat_entity)
1603 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1605 tree gnu_field_type, gnu_field;
1607 /* Set the RM size before wrapping up the type. */
1608 SET_TYPE_RM_SIZE (gnu_type,
1609 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1610 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1611 gnu_field_type = gnu_type;
1613 gnu_type = make_node (RECORD_TYPE);
1614 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1616 /* Propagate the alignment of the modular type to the record.
1617 This means that bit-packed arrays have "ceil" alignment for
1618 their size, which may seem counter-intuitive but makes it
1619 possible to easily overlay them on modular types. */
1620 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1621 TYPE_PACKED (gnu_type) = 1;
1623 /* Create a stripped-down declaration of the original type, mainly
1625 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1626 debug_info_p, gnat_entity);
1628 /* Don't notify the field as "addressable", since we won't be taking
1629 it's address and it would prevent create_field_decl from making a
1631 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1632 gnu_field_type, gnu_type, 1, 0, 0, 0);
1634 /* Do not finalize it until after the parallel type is added. */
1635 finish_record_type (gnu_type, gnu_field, 0, true);
1636 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1638 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1640 /* Make the original array type a parallel type. */
1642 && present_gnu_tree (Original_Array_Type (gnat_entity)))
1643 add_parallel_type (TYPE_STUB_DECL (gnu_type),
1645 (Original_Array_Type (gnat_entity)));
1647 rest_of_record_type_compilation (gnu_type);
1650 /* If the type we are dealing with has got a smaller alignment than the
1651 natural one, we need to wrap it up in a record type and under-align
1652 the latter. We reuse the padding machinery for this purpose. */
1653 else if (Present (Alignment_Clause (gnat_entity))
1654 && UI_Is_In_Int_Range (Alignment (gnat_entity))
1655 && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1656 && align < TYPE_ALIGN (gnu_type))
1658 tree gnu_field_type, gnu_field;
1660 /* Set the RM size before wrapping up the type. */
1661 SET_TYPE_RM_SIZE (gnu_type,
1662 UI_To_gnu (RM_Size (gnat_entity), bitsizetype));
1663 gnu_field_type = gnu_type;
1665 gnu_type = make_node (RECORD_TYPE);
1666 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1668 TYPE_ALIGN (gnu_type) = align;
1669 TYPE_PACKED (gnu_type) = 1;
1671 /* Create a stripped-down declaration of the original type, mainly
1673 create_type_decl (gnu_entity_name, gnu_field_type, NULL, true,
1674 debug_info_p, gnat_entity);
1676 /* Don't notify the field as "addressable", since we won't be taking
1677 it's address and it would prevent create_field_decl from making a
1679 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1680 gnu_field_type, gnu_type, 1, 0, 0, 0);
1682 finish_record_type (gnu_type, gnu_field, 0, false);
1683 TYPE_IS_PADDING_P (gnu_type) = 1;
1685 relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1688 /* Otherwise reset the alignment lest we computed it above. */
1694 case E_Floating_Point_Type:
1695 /* If this is a VAX floating-point type, use an integer of the proper
1696 size. All the operations will be handled with ASM statements. */
1697 if (Vax_Float (gnat_entity))
1699 gnu_type = make_signed_type (esize);
1700 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1701 SET_TYPE_DIGITS_VALUE (gnu_type,
1702 UI_To_gnu (Digits_Value (gnat_entity),
1707 /* The type of the Low and High bounds can be our type if this is
1708 a type from Standard, so set them at the end of the function. */
1709 gnu_type = make_node (REAL_TYPE);
1710 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1711 layout_type (gnu_type);
1714 case E_Floating_Point_Subtype:
1715 if (Vax_Float (gnat_entity))
1717 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1723 && Present (Ancestor_Subtype (gnat_entity))
1724 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1725 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1726 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1727 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1730 gnu_type = make_node (REAL_TYPE);
1731 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1732 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1733 TYPE_GCC_MIN_VALUE (gnu_type)
1734 = TYPE_GCC_MIN_VALUE (TREE_TYPE (gnu_type));
1735 TYPE_GCC_MAX_VALUE (gnu_type)
1736 = TYPE_GCC_MAX_VALUE (TREE_TYPE (gnu_type));
1737 layout_type (gnu_type);
1739 SET_TYPE_RM_MIN_VALUE
1741 convert (TREE_TYPE (gnu_type),
1742 elaborate_expression (Type_Low_Bound (gnat_entity),
1743 gnat_entity, get_identifier ("L"),
1745 Needs_Debug_Info (gnat_entity))));
1747 SET_TYPE_RM_MAX_VALUE
1749 convert (TREE_TYPE (gnu_type),
1750 elaborate_expression (Type_High_Bound (gnat_entity),
1751 gnat_entity, get_identifier ("U"),
1753 Needs_Debug_Info (gnat_entity))));
1755 /* One of the above calls might have caused us to be elaborated,
1756 so don't blow up if so. */
1757 if (present_gnu_tree (gnat_entity))
1759 maybe_present = true;
1763 /* Inherit our alias set from what we're a subtype of, as for
1764 integer subtypes. */
1765 relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1769 /* Array and String Types and Subtypes
1771 Unconstrained array types are represented by E_Array_Type and
1772 constrained array types are represented by E_Array_Subtype. There
1773 are no actual objects of an unconstrained array type; all we have
1774 are pointers to that type.
1776 The following fields are defined on array types and subtypes:
1778 Component_Type Component type of the array.
1779 Number_Dimensions Number of dimensions (an int).
1780 First_Index Type of first index. */
1785 Entity_Id gnat_index, gnat_name;
1786 const bool convention_fortran_p
1787 = (Convention (gnat_entity) == Convention_Fortran);
1788 const int ndim = Number_Dimensions (gnat_entity);
1789 tree gnu_template_fields = NULL_TREE;
1790 tree gnu_template_type = make_node (RECORD_TYPE);
1791 tree gnu_template_reference;
1792 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1793 tree gnu_fat_type = make_node (RECORD_TYPE);
1794 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
1795 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree));
1796 tree gnu_max_size = size_one_node, gnu_max_size_unit;
1797 tree gnu_comp_size, tem;
1800 TYPE_NAME (gnu_template_type)
1801 = create_concat_name (gnat_entity, "XUB");
1803 /* Make a node for the array. If we are not defining the array
1804 suppress expanding incomplete types. */
1805 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1809 defer_incomplete_level++;
1810 this_deferred = true;
1813 /* Build the fat pointer type. Use a "void *" object instead of
1814 a pointer to the array type since we don't have the array type
1815 yet (it will reference the fat pointer via the bounds). */
1816 tem = chainon (chainon (NULL_TREE,
1817 create_field_decl (get_identifier ("P_ARRAY"),
1820 NULL_TREE, NULL_TREE, 0)),
1821 create_field_decl (get_identifier ("P_BOUNDS"),
1824 NULL_TREE, NULL_TREE, 0));
1826 /* Make sure we can put this into a register. */
1827 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1829 /* Do not finalize this record type since the types of its fields
1830 are still incomplete at this point. */
1831 finish_record_type (gnu_fat_type, tem, 0, true);
1832 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1834 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1835 is the fat pointer. This will be used to access the individual
1836 fields once we build them. */
1837 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1838 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1839 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1840 gnu_template_reference
1841 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1842 TREE_READONLY (gnu_template_reference) = 1;
1844 /* Now create the GCC type for each index and add the fields for that
1845 index to the template. */
1846 for (index = (convention_fortran_p ? ndim - 1 : 0),
1847 gnat_index = First_Index (gnat_entity);
1848 0 <= index && index < ndim;
1849 index += (convention_fortran_p ? - 1 : 1),
1850 gnat_index = Next_Index (gnat_index))
1852 char field_name[16];
1853 tree gnu_index_base_type
1854 = get_unpadded_type (Base_Type (Etype (gnat_index)));
1855 tree gnu_low_field, gnu_high_field, gnu_low, gnu_high;
1857 /* Make the FIELD_DECLs for the low and high bounds of this
1858 type and then make extractions of these fields from the
1860 sprintf (field_name, "LB%d", index);
1861 gnu_low_field = create_field_decl (get_identifier (field_name),
1862 gnu_index_base_type,
1863 gnu_template_type, 0,
1864 NULL_TREE, NULL_TREE, 0);
1865 Sloc_to_locus (Sloc (gnat_entity),
1866 &DECL_SOURCE_LOCATION (gnu_low_field));
1868 field_name[0] = 'U';
1869 gnu_high_field = create_field_decl (get_identifier (field_name),
1870 gnu_index_base_type,
1871 gnu_template_type, 0,
1872 NULL_TREE, NULL_TREE, 0);
1873 Sloc_to_locus (Sloc (gnat_entity),
1874 &DECL_SOURCE_LOCATION (gnu_high_field));
1876 gnu_temp_fields[index] = chainon (gnu_low_field, gnu_high_field);
1878 /* We can't use build_component_ref here since the template type
1879 isn't complete yet. */
1880 gnu_low = build3 (COMPONENT_REF, gnu_index_base_type,
1881 gnu_template_reference, gnu_low_field,
1883 gnu_high = build3 (COMPONENT_REF, gnu_index_base_type,
1884 gnu_template_reference, gnu_high_field,
1886 TREE_READONLY (gnu_low) = TREE_READONLY (gnu_high) = 1;
1888 /* Make a range type with the new range in the Ada base type.
1889 Then make an index type with the new range in sizetype. */
1890 gnu_index_types[index]
1891 = create_index_type (convert (sizetype, gnu_low),
1892 convert (sizetype, gnu_high),
1893 create_range_type (gnu_index_base_type,
1897 /* Update the maximum size of the array in elements. */
1900 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
1902 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_type));
1904 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_type));
1906 = size_binop (MAX_EXPR,
1907 size_binop (PLUS_EXPR, size_one_node,
1908 size_binop (MINUS_EXPR,
1912 if (TREE_CODE (gnu_this_max) == INTEGER_CST
1913 && TREE_OVERFLOW (gnu_this_max))
1914 gnu_max_size = NULL_TREE;
1917 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
1920 TYPE_NAME (gnu_index_types[index])
1921 = create_concat_name (gnat_entity, field_name);
1924 for (index = 0; index < ndim; index++)
1926 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1928 /* Install all the fields into the template. */
1929 finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1930 TYPE_READONLY (gnu_template_type) = 1;
1932 /* Now make the array of arrays and update the pointer to the array
1933 in the fat pointer. Note that it is the first field. */
1934 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1936 /* Try to get a smaller form of the component if needed. */
1937 if ((Is_Packed (gnat_entity)
1938 || Has_Component_Size_Clause (gnat_entity))
1939 && !Is_Bit_Packed_Array (gnat_entity)
1940 && !Has_Aliased_Components (gnat_entity)
1941 && !Strict_Alignment (Component_Type (gnat_entity))
1942 && TREE_CODE (tem) == RECORD_TYPE
1943 && !TYPE_IS_FAT_POINTER_P (tem)
1944 && host_integerp (TYPE_SIZE (tem), 1))
1945 tem = make_packable_type (tem, false);
1947 if (Has_Atomic_Components (gnat_entity))
1948 check_ok_for_atomic (tem, gnat_entity, true);
1950 /* Get and validate any specified Component_Size, but if Packed,
1951 ignore it since the front end will have taken care of it. */
1953 = validate_size (Component_Size (gnat_entity), tem,
1955 (Is_Bit_Packed_Array (gnat_entity)
1956 ? TYPE_DECL : VAR_DECL),
1957 true, Has_Component_Size_Clause (gnat_entity));
1959 /* If the component type is a RECORD_TYPE that has a self-referential
1960 size, use the maximum size. */
1962 && TREE_CODE (tem) == RECORD_TYPE
1963 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1964 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1966 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1968 tree orig_tem = tem;
1969 unsigned int max_align;
1971 /* If an alignment is specified, use it as a cap on the component
1972 type so that it can be honored for the whole type. But ignore
1973 it for the original type of packed array types. */
1974 if (No (Packed_Array_Type (gnat_entity))
1975 && Known_Alignment (gnat_entity))
1976 max_align = validate_alignment (Alignment (gnat_entity),
1981 tem = make_type_from_size (tem, gnu_comp_size, false);
1982 if (max_align > 0 && TYPE_ALIGN (tem) > max_align)
1987 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1988 "C_PAD", false, definition, true);
1990 /* If a padding record was made, declare it now since it will
1991 never be declared otherwise. This is necessary to ensure
1992 that its subtrees are properly marked. */
1993 if (tem != orig_tem)
1994 create_type_decl (TYPE_NAME (tem), tem, NULL, true,
1995 debug_info_p, gnat_entity);
1998 if (Has_Volatile_Components (gnat_entity))
1999 tem = build_qualified_type (tem,
2000 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
2002 /* If Component_Size is not already specified, annotate it with the
2003 size of the component. */
2004 if (Unknown_Component_Size (gnat_entity))
2005 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
2007 /* Compute the maximum size of the array in units and bits. */
2010 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2011 TYPE_SIZE_UNIT (tem));
2012 gnu_max_size = size_binop (MULT_EXPR,
2013 convert (bitsizetype, gnu_max_size),
2017 gnu_max_size_unit = NULL_TREE;
2019 /* Now build the array type. */
2020 for (index = ndim - 1; index >= 0; index--)
2022 tem = build_array_type (tem, gnu_index_types[index]);
2023 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2024 if (array_type_has_nonaliased_component (gnat_entity, tem))
2025 TYPE_NONALIASED_COMPONENT (tem) = 1;
2028 /* If an alignment is specified, use it if valid. But ignore it
2029 for the original type of packed array types. If the alignment
2030 was requested with an explicit alignment clause, state so. */
2031 if (No (Packed_Array_Type (gnat_entity))
2032 && Known_Alignment (gnat_entity))
2035 = validate_alignment (Alignment (gnat_entity), gnat_entity,
2037 if (Present (Alignment_Clause (gnat_entity)))
2038 TYPE_USER_ALIGN (tem) = 1;
2041 TYPE_CONVENTION_FORTRAN_P (tem) = convention_fortran_p;
2042 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2044 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2045 corresponding fat pointer. */
2046 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2047 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2048 SET_TYPE_MODE (gnu_type, BLKmode);
2049 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2050 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2052 /* If the maximum size doesn't overflow, use it. */
2054 && TREE_CODE (gnu_max_size) == INTEGER_CST
2055 && !TREE_OVERFLOW (gnu_max_size)
2056 && TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2057 && !TREE_OVERFLOW (gnu_max_size_unit))
2059 TYPE_SIZE (tem) = size_binop (MIN_EXPR, gnu_max_size,
2061 TYPE_SIZE_UNIT (tem) = size_binop (MIN_EXPR, gnu_max_size_unit,
2062 TYPE_SIZE_UNIT (tem));
2065 create_type_decl (create_concat_name (gnat_entity, "XUA"),
2066 tem, NULL, !Comes_From_Source (gnat_entity),
2067 debug_info_p, gnat_entity);
2069 /* Give the fat pointer type a name. If this is a packed type, tell
2070 the debugger how to interpret the underlying bits. */
2071 if (Present (Packed_Array_Type (gnat_entity)))
2072 gnat_name = Packed_Array_Type (gnat_entity);
2074 gnat_name = gnat_entity;
2075 create_type_decl (create_concat_name (gnat_name, "XUP"),
2076 gnu_fat_type, NULL, true,
2077 debug_info_p, gnat_entity);
2079 /* Create the type to be used as what a thin pointer designates: an
2080 record type for the object and its template with the field offsets
2081 shifted to have the template at a negative offset. */
2082 tem = build_unc_object_type (gnu_template_type, tem,
2083 create_concat_name (gnat_name, "XUT"));
2084 shift_unc_components_for_thin_pointers (tem);
2086 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2087 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2091 case E_String_Subtype:
2092 case E_Array_Subtype:
2094 /* This is the actual data type for array variables. Multidimensional
2095 arrays are implemented as arrays of arrays. Note that arrays which
2096 have sparse enumeration subtypes as index components create sparse
2097 arrays, which is obviously space inefficient but so much easier to
2100 Also note that the subtype never refers to the unconstrained array
2101 type, which is somewhat at variance with Ada semantics.
2103 First check to see if this is simply a renaming of the array type.
2104 If so, the result is the array type. */
2106 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2107 if (!Is_Constrained (gnat_entity))
2111 Entity_Id gnat_index, gnat_base_index;
2112 const bool convention_fortran_p
2113 = (Convention (gnat_entity) == Convention_Fortran);
2114 const int ndim = Number_Dimensions (gnat_entity);
2115 tree gnu_base_type = gnu_type;
2116 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree));
2117 tree gnu_max_size = size_one_node, gnu_max_size_unit;
2118 bool need_index_type_struct = false;
2121 /* First create the GCC type for each index and find out whether
2122 special types are needed for debugging information. */
2123 for (index = (convention_fortran_p ? ndim - 1 : 0),
2124 gnat_index = First_Index (gnat_entity),
2126 = First_Index (Implementation_Base_Type (gnat_entity));
2127 0 <= index && index < ndim;
2128 index += (convention_fortran_p ? - 1 : 1),
2129 gnat_index = Next_Index (gnat_index),
2130 gnat_base_index = Next_Index (gnat_base_index))
2132 tree gnu_index_type = get_unpadded_type (Etype (gnat_index));
2133 tree prec = TYPE_RM_SIZE (gnu_index_type);
2135 = (compare_tree_int (prec, TYPE_PRECISION (sizetype)) > 0
2136 || (compare_tree_int (prec, TYPE_PRECISION (sizetype)) == 0
2137 && TYPE_UNSIGNED (gnu_index_type)
2138 != TYPE_UNSIGNED (sizetype)));
2139 tree gnu_orig_min = TYPE_MIN_VALUE (gnu_index_type);
2140 tree gnu_orig_max = TYPE_MAX_VALUE (gnu_index_type);
2141 tree gnu_min = convert (sizetype, gnu_orig_min);
2142 tree gnu_max = convert (sizetype, gnu_orig_max);
2143 tree gnu_base_index_type
2144 = get_unpadded_type (Etype (gnat_base_index));
2145 tree gnu_base_orig_min = TYPE_MIN_VALUE (gnu_base_index_type);
2146 tree gnu_base_orig_max = TYPE_MAX_VALUE (gnu_base_index_type);
2149 /* See if the base array type is already flat. If it is, we
2150 are probably compiling an ACATS test but it will cause the
2151 code below to malfunction if we don't handle it specially. */
2152 if (TREE_CODE (gnu_base_orig_min) == INTEGER_CST
2153 && TREE_CODE (gnu_base_orig_max) == INTEGER_CST
2154 && tree_int_cst_lt (gnu_base_orig_max, gnu_base_orig_min))
2156 gnu_min = size_one_node;
2157 gnu_max = size_zero_node;
2161 /* Similarly, if one of the values overflows in sizetype and the
2162 range is null, use 1..0 for the sizetype bounds. */
2164 && TREE_CODE (gnu_min) == INTEGER_CST
2165 && TREE_CODE (gnu_max) == INTEGER_CST
2166 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2167 && tree_int_cst_lt (gnu_orig_max, gnu_orig_min))
2169 gnu_min = size_one_node;
2170 gnu_max = size_zero_node;
2174 /* If the minimum and maximum values both overflow in sizetype,
2175 but the difference in the original type does not overflow in
2176 sizetype, ignore the overflow indication. */
2178 && TREE_CODE (gnu_min) == INTEGER_CST
2179 && TREE_CODE (gnu_max) == INTEGER_CST
2180 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2183 fold_build2 (MINUS_EXPR, gnu_index_type,
2187 TREE_OVERFLOW (gnu_min) = 0;
2188 TREE_OVERFLOW (gnu_max) = 0;
2192 /* Compute the size of this dimension in the general case. We
2193 need to provide GCC with an upper bound to use but have to
2194 deal with the "superflat" case. There are three ways to do
2195 this. If we can prove that the array can never be superflat,
2196 we can just use the high bound of the index type. */
2197 else if (Nkind (gnat_index) == N_Range
2198 && cannot_be_superflat_p (gnat_index))
2201 /* Otherwise, if we can prove that the low bound minus one and
2202 the high bound cannot overflow, we can just use the expression
2203 MAX (hb, lb - 1). Otherwise, we have to use the most general
2204 expression (hb >= lb) ? hb : lb - 1. Note that the comparison
2205 must be done in the original index type, to avoid any overflow
2206 during the conversion. */
2209 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2211 /* If gnu_high is a constant that has overflowed, the bound
2212 is the smallest integer so cannot be the maximum. */
2213 if (TREE_CODE (gnu_high) == INTEGER_CST
2214 && TREE_OVERFLOW (gnu_high))
2217 /* If the index type is not wider and gnu_high is a constant
2218 that hasn't overflowed, we can use the maximum. */
2219 else if (!wider_p && TREE_CODE (gnu_high) == INTEGER_CST)
2220 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2224 = build_cond_expr (sizetype,
2225 build_binary_op (GE_EXPR,
2232 gnu_index_types[index]
2233 = create_index_type (gnu_min, gnu_high, gnu_index_type,
2236 /* Update the maximum size of the array in elements. Here we
2237 see if any constraint on the index type of the base type
2238 can be used in the case of self-referential bound on the
2239 index type of the subtype. We look for a non-"infinite"
2240 and non-self-referential bound from any type involved and
2241 handle each bound separately. */
2244 tree gnu_base_min = convert (sizetype, gnu_base_orig_min);
2245 tree gnu_base_max = convert (sizetype, gnu_base_orig_max);
2246 tree gnu_base_index_base_type
2247 = get_base_type (gnu_base_index_type);
2248 tree gnu_base_base_min
2249 = convert (sizetype,
2250 TYPE_MIN_VALUE (gnu_base_index_base_type));
2251 tree gnu_base_base_max
2252 = convert (sizetype,
2253 TYPE_MAX_VALUE (gnu_base_index_base_type));
2255 if (!CONTAINS_PLACEHOLDER_P (gnu_min)
2256 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2257 && !TREE_OVERFLOW (gnu_base_min)))
2258 gnu_base_min = gnu_min;
2260 if (!CONTAINS_PLACEHOLDER_P (gnu_max)
2261 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2262 && !TREE_OVERFLOW (gnu_base_max)))
2263 gnu_base_max = gnu_max;
2265 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2266 && TREE_OVERFLOW (gnu_base_min))
2267 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2268 || (TREE_CODE (gnu_base_max) == INTEGER_CST
2269 && TREE_OVERFLOW (gnu_base_max))
2270 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2271 gnu_max_size = NULL_TREE;
2275 = size_binop (MAX_EXPR,
2276 size_binop (PLUS_EXPR, size_one_node,
2277 size_binop (MINUS_EXPR,
2282 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2283 && TREE_OVERFLOW (gnu_this_max))
2284 gnu_max_size = NULL_TREE;
2287 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2291 /* We need special types for debugging information to point to
2292 the index types if they have variable bounds, are not integer
2293 types, are biased or are wider than sizetype. */
2294 if (!integer_onep (gnu_orig_min)
2295 || TREE_CODE (gnu_orig_max) != INTEGER_CST
2296 || TREE_CODE (gnu_index_type) != INTEGER_TYPE
2297 || (TREE_TYPE (gnu_index_type)
2298 && TREE_CODE (TREE_TYPE (gnu_index_type))
2300 || TYPE_BIASED_REPRESENTATION_P (gnu_index_type)
2301 || compare_tree_int (prec, TYPE_PRECISION (sizetype)) > 0)
2302 need_index_type_struct = true;
2305 /* Then flatten: create the array of arrays. For an array type
2306 used to implement a packed array, get the component type from
2307 the original array type since the representation clauses that
2308 can affect it are on the latter. */
2309 if (Is_Packed_Array_Type (gnat_entity)
2310 && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2312 gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2313 for (index = ndim - 1; index >= 0; index--)
2314 gnu_type = TREE_TYPE (gnu_type);
2316 /* One of the above calls might have caused us to be elaborated,
2317 so don't blow up if so. */
2318 if (present_gnu_tree (gnat_entity))
2320 maybe_present = true;
2328 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2330 /* One of the above calls might have caused us to be elaborated,
2331 so don't blow up if so. */
2332 if (present_gnu_tree (gnat_entity))
2334 maybe_present = true;
2338 /* Try to get a smaller form of the component if needed. */
2339 if ((Is_Packed (gnat_entity)
2340 || Has_Component_Size_Clause (gnat_entity))
2341 && !Is_Bit_Packed_Array (gnat_entity)
2342 && !Has_Aliased_Components (gnat_entity)
2343 && !Strict_Alignment (Component_Type (gnat_entity))
2344 && TREE_CODE (gnu_type) == RECORD_TYPE
2345 && !TYPE_IS_FAT_POINTER_P (gnu_type)
2346 && host_integerp (TYPE_SIZE (gnu_type), 1))
2347 gnu_type = make_packable_type (gnu_type, false);
2349 /* Get and validate any specified Component_Size, but if Packed,
2350 ignore it since the front end will have taken care of it. */
2352 = validate_size (Component_Size (gnat_entity), gnu_type,
2354 (Is_Bit_Packed_Array (gnat_entity)
2355 ? TYPE_DECL : VAR_DECL), true,
2356 Has_Component_Size_Clause (gnat_entity));
2358 /* If the component type is a RECORD_TYPE that has a
2359 self-referential size, use the maximum size. */
2361 && TREE_CODE (gnu_type) == RECORD_TYPE
2362 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2363 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2365 if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2367 tree orig_gnu_type = gnu_type;
2368 unsigned int max_align;
2370 /* If an alignment is specified, use it as a cap on the
2371 component type so that it can be honored for the whole
2372 type. But ignore it for the original type of packed
2374 if (No (Packed_Array_Type (gnat_entity))
2375 && Known_Alignment (gnat_entity))
2376 max_align = validate_alignment (Alignment (gnat_entity),
2382 = make_type_from_size (gnu_type, gnu_comp_size, false);
2383 if (max_align > 0 && TYPE_ALIGN (gnu_type) > max_align)
2384 gnu_type = orig_gnu_type;
2386 orig_gnu_type = gnu_type;
2388 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2389 gnat_entity, "C_PAD", false,
2392 /* If a padding record was made, declare it now since it
2393 will never be declared otherwise. This is necessary
2394 to ensure that its subtrees are properly marked. */
2395 if (gnu_type != orig_gnu_type)
2396 create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2397 true, debug_info_p, gnat_entity);
2400 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2401 gnu_type = build_qualified_type (gnu_type,
2402 (TYPE_QUALS (gnu_type)
2403 | TYPE_QUAL_VOLATILE));
2406 /* Compute the maximum size of the array in units and bits. */
2409 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2410 TYPE_SIZE_UNIT (gnu_type));
2411 gnu_max_size = size_binop (MULT_EXPR,
2412 convert (bitsizetype, gnu_max_size),
2413 TYPE_SIZE (gnu_type));
2416 gnu_max_size_unit = NULL_TREE;
2418 /* Now build the array type. */
2419 for (index = ndim - 1; index >= 0; index --)
2421 gnu_type = build_array_type (gnu_type, gnu_index_types[index]);
2422 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2423 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2424 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2427 /* Attach the TYPE_STUB_DECL in case we have a parallel type. */
2428 TYPE_STUB_DECL (gnu_type)
2429 = create_type_stub_decl (gnu_entity_name, gnu_type);
2431 /* If we are at file level and this is a multi-dimensional array,
2432 we need to make a variable corresponding to the stride of the
2433 inner dimensions. */
2434 if (global_bindings_p () && ndim > 1)
2436 tree gnu_str_name = get_identifier ("ST");
2439 for (gnu_arr_type = TREE_TYPE (gnu_type);
2440 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2441 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2442 gnu_str_name = concat_name (gnu_str_name, "ST"))
2444 tree eltype = TREE_TYPE (gnu_arr_type);
2446 TYPE_SIZE (gnu_arr_type)
2447 = elaborate_expression_1 (TYPE_SIZE (gnu_arr_type),
2448 gnat_entity, gnu_str_name,
2451 /* ??? For now, store the size as a multiple of the
2452 alignment of the element type in bytes so that we
2453 can see the alignment from the tree. */
2454 TYPE_SIZE_UNIT (gnu_arr_type)
2456 (MULT_EXPR, sizetype,
2457 elaborate_expression_1
2458 (build_binary_op (EXACT_DIV_EXPR, sizetype,
2459 TYPE_SIZE_UNIT (gnu_arr_type),
2460 size_int (TYPE_ALIGN (eltype)
2462 gnat_entity, concat_name (gnu_str_name, "A_U"),
2464 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2466 /* ??? create_type_decl is not invoked on the inner types so
2467 the MULT_EXPR node built above will never be marked. */
2468 mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2472 /* If we need to write out a record type giving the names of the
2473 bounds for debugging purposes, do it now and make the record
2474 type a parallel type. This is not needed for a packed array
2475 since the bounds are conveyed by the original array type. */
2476 if (need_index_type_struct
2478 && !Is_Packed_Array_Type (gnat_entity))
2480 tree gnu_bound_rec = make_node (RECORD_TYPE);
2481 tree gnu_field_list = NULL_TREE;
2484 TYPE_NAME (gnu_bound_rec)
2485 = create_concat_name (gnat_entity, "XA");
2487 for (index = ndim - 1; index >= 0; index--)
2489 tree gnu_index = TYPE_INDEX_TYPE (gnu_index_types[index]);
2490 tree gnu_index_name = TYPE_NAME (gnu_index);
2492 if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2493 gnu_index_name = DECL_NAME (gnu_index_name);
2495 /* Make sure to reference the types themselves, and not just
2496 their names, as the debugger may fall back on them. */
2497 gnu_field = create_field_decl (gnu_index_name, gnu_index,
2499 0, NULL_TREE, NULL_TREE, 0);
2500 TREE_CHAIN (gnu_field) = gnu_field_list;
2501 gnu_field_list = gnu_field;
2504 finish_record_type (gnu_bound_rec, gnu_field_list, 0, false);
2505 add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2508 /* Otherwise, for a packed array, make the original array type a
2510 else if (debug_info_p
2511 && Is_Packed_Array_Type (gnat_entity)
2512 && present_gnu_tree (Original_Array_Type (gnat_entity)))
2513 add_parallel_type (TYPE_STUB_DECL (gnu_type),
2515 (Original_Array_Type (gnat_entity)));
2517 TYPE_CONVENTION_FORTRAN_P (gnu_type) = convention_fortran_p;
2518 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2519 = (Is_Packed_Array_Type (gnat_entity)
2520 && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2522 /* If the size is self-referential and the maximum size doesn't
2523 overflow, use it. */
2524 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2526 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2527 && TREE_OVERFLOW (gnu_max_size))
2528 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2529 && TREE_OVERFLOW (gnu_max_size_unit)))
2531 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2532 TYPE_SIZE (gnu_type));
2533 TYPE_SIZE_UNIT (gnu_type)
2534 = size_binop (MIN_EXPR, gnu_max_size_unit,
2535 TYPE_SIZE_UNIT (gnu_type));
2538 /* Set our alias set to that of our base type. This gives all
2539 array subtypes the same alias set. */
2540 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;
2549 /* First finish the type we had been making so that we output
2550 debugging information for it. */
2551 if (Treat_As_Volatile (gnat_entity))
2553 = build_qualified_type (gnu_type,
2554 TYPE_QUALS (gnu_type)
2555 | TYPE_QUAL_VOLATILE);
2557 /* Make it artificial only if the base type was artificial too.
2558 That's sort of "morally" true and will make it possible for
2559 the debugger to look it up by name in DWARF, which is needed
2560 in order to decode the packed array type. */
2562 = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2563 !Comes_From_Source (Etype (gnat_entity))
2564 && !Comes_From_Source (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 = gnu_type;
2578 while (TREE_CODE (gnu_inner) == RECORD_TYPE
2579 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner)
2580 || TYPE_IS_PADDING_P (gnu_inner)))
2581 gnu_inner = TREE_TYPE (TYPE_FIELDS (gnu_inner));
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) == ARRAY_TYPE
2586 && !TYPE_ACTUAL_BOUNDS (gnu_inner))
2587 || (TREE_CODE (gnu_inner) == INTEGER_TYPE
2588 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner)))
2590 if (TREE_CODE (gnu_inner) == 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))
2598 = make_unsigned_type (TYPE_PRECISION (gnu_inner));
2599 TREE_TYPE (gnu_subtype) = gnu_inner;
2600 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2601 SET_TYPE_RM_MIN_VALUE (gnu_subtype,
2602 TYPE_MIN_VALUE (gnu_inner));
2603 SET_TYPE_RM_MAX_VALUE (gnu_subtype,
2604 TYPE_MAX_VALUE (gnu_inner));
2605 gnu_inner = gnu_subtype;
2608 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner) = 1;
2610 #ifdef ENABLE_CHECKING
2611 /* Check for other cases of overloading. */
2612 gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner));
2616 for (gnat_index = First_Index (gnat_entity);
2617 Present (gnat_index);
2618 gnat_index = Next_Index (gnat_index))
2619 SET_TYPE_ACTUAL_BOUNDS
2621 tree_cons (NULL_TREE,
2622 get_unpadded_type (Etype (gnat_index)),
2623 TYPE_ACTUAL_BOUNDS (gnu_inner)));
2625 if (Convention (gnat_entity) != Convention_Fortran)
2626 SET_TYPE_ACTUAL_BOUNDS
2627 (gnu_inner, nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner)));
2629 if (TREE_CODE (gnu_type) == RECORD_TYPE
2630 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2631 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner;
2636 /* Abort if packed array with no Packed_Array_Type field set. */
2637 gcc_assert (!Is_Packed (gnat_entity));
2641 case E_String_Literal_Subtype:
2642 /* Create the type for a string literal. */
2644 Entity_Id gnat_full_type
2645 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2646 && Present (Full_View (Etype (gnat_entity)))
2647 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2648 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2649 tree gnu_string_array_type
2650 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2651 tree gnu_string_index_type
2652 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2653 (TYPE_DOMAIN (gnu_string_array_type))));
2654 tree gnu_lower_bound
2655 = convert (gnu_string_index_type,
2656 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2657 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2658 tree gnu_length = ssize_int (length - 1);
2659 tree gnu_upper_bound
2660 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2662 convert (gnu_string_index_type, gnu_length));
2664 = create_index_type (convert (sizetype, gnu_lower_bound),
2665 convert (sizetype, gnu_upper_bound),
2666 create_range_type (gnu_string_index_type,
2672 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2674 if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2675 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2676 relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2680 /* Record Types and Subtypes
2682 The following fields are defined on record types:
2684 Has_Discriminants True if the record has discriminants
2685 First_Discriminant Points to head of list of discriminants
2686 First_Entity Points to head of list of fields
2687 Is_Tagged_Type True if the record is tagged
2689 Implementation of Ada records and discriminated records:
2691 A record type definition is transformed into the equivalent of a C
2692 struct definition. The fields that are the discriminants which are
2693 found in the Full_Type_Declaration node and the elements of the
2694 Component_List found in the Record_Type_Definition node. The
2695 Component_List can be a recursive structure since each Variant of
2696 the Variant_Part of the Component_List has a Component_List.
2698 Processing of a record type definition comprises starting the list of
2699 field declarations here from the discriminants and the calling the
2700 function components_to_record to add the rest of the fields from the
2701 component list and return the gnu type node. The function
2702 components_to_record will call itself recursively as it traverses
2706 if (Has_Complex_Representation (gnat_entity))
2709 = build_complex_type
2711 (Etype (Defining_Entity
2712 (First (Component_Items
2715 (Declaration_Node (gnat_entity)))))))));
2721 Node_Id full_definition = Declaration_Node (gnat_entity);
2722 Node_Id record_definition = Type_Definition (full_definition);
2723 Entity_Id gnat_field;
2724 tree gnu_field, gnu_field_list = NULL_TREE, gnu_get_parent;
2725 /* Set PACKED in keeping with gnat_to_gnu_field. */
2727 = Is_Packed (gnat_entity)
2729 : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2731 : (Known_Alignment (gnat_entity)
2732 || (Strict_Alignment (gnat_entity)
2733 && Known_Static_Esize (gnat_entity)))
2736 bool has_discr = Has_Discriminants (gnat_entity);
2737 bool has_rep = Has_Specified_Layout (gnat_entity);
2738 bool all_rep = has_rep;
2740 = (Is_Tagged_Type (gnat_entity)
2741 && Nkind (record_definition) == N_Derived_Type_Definition);
2742 bool is_unchecked_union = Is_Unchecked_Union (gnat_entity);
2744 /* See if all fields have a rep clause. Stop when we find one
2747 for (gnat_field = First_Entity (gnat_entity);
2748 Present (gnat_field);
2749 gnat_field = Next_Entity (gnat_field))
2750 if ((Ekind (gnat_field) == E_Component
2751 || Ekind (gnat_field) == E_Discriminant)
2752 && No (Component_Clause (gnat_field)))
2758 /* If this is a record extension, go a level further to find the
2759 record definition. Also, verify we have a Parent_Subtype. */
2762 if (!type_annotate_only
2763 || Present (Record_Extension_Part (record_definition)))
2764 record_definition = Record_Extension_Part (record_definition);
2766 gcc_assert (type_annotate_only
2767 || Present (Parent_Subtype (gnat_entity)));
2770 /* Make a node for the record. If we are not defining the record,
2771 suppress expanding incomplete types. */
2772 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2773 TYPE_NAME (gnu_type) = gnu_entity_name;
2774 TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2778 defer_incomplete_level++;
2779 this_deferred = true;
2782 /* If both a size and rep clause was specified, put the size in
2783 the record type now so that it can get the proper mode. */
2784 if (has_rep && Known_Esize (gnat_entity))
2785 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2787 /* Always set the alignment here so that it can be used to
2788 set the mode, if it is making the alignment stricter. If
2789 it is invalid, it will be checked again below. If this is to
2790 be Atomic, choose a default alignment of a word unless we know
2791 the size and it's smaller. */
2792 if (Known_Alignment (gnat_entity))
2793 TYPE_ALIGN (gnu_type)
2794 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2795 else if (Is_Atomic (gnat_entity))
2796 TYPE_ALIGN (gnu_type)
2797 = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2798 /* If a type needs strict alignment, the minimum size will be the
2799 type size instead of the RM size (see validate_size). Cap the
2800 alignment, lest it causes this type size to become too large. */
2801 else if (Strict_Alignment (gnat_entity)
2802 && Known_Static_Esize (gnat_entity))
2804 unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2805 unsigned int raw_align = raw_size & -raw_size;
2806 if (raw_align < BIGGEST_ALIGNMENT)
2807 TYPE_ALIGN (gnu_type) = raw_align;
2810 TYPE_ALIGN (gnu_type) = 0;
2812 /* If we have a Parent_Subtype, make a field for the parent. If
2813 this record has rep clauses, force the position to zero. */
2814 if (Present (Parent_Subtype (gnat_entity)))
2816 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2819 /* A major complexity here is that the parent subtype will
2820 reference our discriminants in its Discriminant_Constraint
2821 list. But those must reference the parent component of this
2822 record which is of the parent subtype we have not built yet!
2823 To break the circle we first build a dummy COMPONENT_REF which
2824 represents the "get to the parent" operation and initialize
2825 each of those discriminants to a COMPONENT_REF of the above
2826 dummy parent referencing the corresponding discriminant of the
2827 base type of the parent subtype. */
2828 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2829 build0 (PLACEHOLDER_EXPR, gnu_type),
2830 build_decl (input_location,
2831 FIELD_DECL, NULL_TREE,
2836 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2837 Present (gnat_field);
2838 gnat_field = Next_Stored_Discriminant (gnat_field))
2839 if (Present (Corresponding_Discriminant (gnat_field)))
2842 build3 (COMPONENT_REF,
2843 get_unpadded_type (Etype (gnat_field)),
2845 gnat_to_gnu_field_decl (Corresponding_Discriminant
2850 /* Then we build the parent subtype. If it has discriminants but
2851 the type itself has unknown discriminants, this means that it
2852 doesn't contain information about how the discriminants are
2853 derived from those of the ancestor type, so it cannot be used
2854 directly. Instead it is built by cloning the parent subtype
2855 of the underlying record view of the type, for which the above
2856 derivation of discriminants has been made explicit. */
2857 if (Has_Discriminants (gnat_parent)
2858 && Has_Unknown_Discriminants (gnat_entity))
2860 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2862 /* If we are defining the type, the underlying record
2863 view must already have been elaborated at this point.
2864 Otherwise do it now as its parent subtype cannot be
2865 technically elaborated on its own. */
2867 gcc_assert (present_gnu_tree (gnat_uview));
2869 gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2871 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2873 /* Substitute the "get to the parent" of the type for that
2874 of its underlying record view in the cloned type. */
2875 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2876 Present (gnat_field);
2877 gnat_field = Next_Stored_Discriminant (gnat_field))
2878 if (Present (Corresponding_Discriminant (gnat_field)))
2880 tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2882 = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2883 gnu_get_parent, gnu_field, NULL_TREE);
2885 = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2889 gnu_parent = gnat_to_gnu_type (gnat_parent);
2891 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2892 initially built. The discriminants must reference the fields
2893 of the parent subtype and not those of its base type for the
2894 placeholder machinery to properly work. */
2897 /* The actual parent subtype is the full view. */
2898 if (IN (Ekind (gnat_parent), Private_Kind))
2900 if (Present (Full_View (gnat_parent)))
2901 gnat_parent = Full_View (gnat_parent);
2903 gnat_parent = Underlying_Full_View (gnat_parent);
2906 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2907 Present (gnat_field);
2908 gnat_field = Next_Stored_Discriminant (gnat_field))
2909 if (Present (Corresponding_Discriminant (gnat_field)))
2911 Entity_Id field = Empty;
2912 for (field = First_Stored_Discriminant (gnat_parent);
2914 field = Next_Stored_Discriminant (field))
2915 if (same_discriminant_p (gnat_field, field))
2917 gcc_assert (Present (field));
2918 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2919 = gnat_to_gnu_field_decl (field);
2923 /* The "get to the parent" COMPONENT_REF must be given its
2925 TREE_TYPE (gnu_get_parent) = gnu_parent;
2927 /* ...and reference the _Parent field of this record. */
2929 = create_field_decl (get_identifier
2930 (Get_Name_String (Name_uParent)),
2931 gnu_parent, gnu_type, 0,
2933 ? TYPE_SIZE (gnu_parent) : NULL_TREE,
2935 ? bitsize_zero_node : NULL_TREE, 1);
2936 DECL_INTERNAL_P (gnu_field) = 1;
2937 TREE_OPERAND (gnu_get_parent, 1) = gnu_field;
2938 TYPE_FIELDS (gnu_type) = gnu_field;
2941 /* Make the fields for the discriminants and put them into the record
2942 unless it's an Unchecked_Union. */
2944 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2945 Present (gnat_field);
2946 gnat_field = Next_Stored_Discriminant (gnat_field))
2948 /* If this is a record extension and this discriminant is the
2949 renaming of another discriminant, we've handled it above. */
2950 if (Present (Parent_Subtype (gnat_entity))
2951 && Present (Corresponding_Discriminant (gnat_field)))
2955 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2957 /* Make an expression using a PLACEHOLDER_EXPR from the
2958 FIELD_DECL node just created and link that with the
2959 corresponding GNAT defining identifier. */
2960 save_gnu_tree (gnat_field,
2961 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2962 build0 (PLACEHOLDER_EXPR, gnu_type),
2963 gnu_field, NULL_TREE),
2966 if (!is_unchecked_union)
2968 TREE_CHAIN (gnu_field) = gnu_field_list;
2969 gnu_field_list = gnu_field;
2973 /* Add the fields into the record type and finish it up. */
2974 components_to_record (gnu_type, Component_List (record_definition),
2975 gnu_field_list, packed, definition, NULL,
2976 false, all_rep, false, is_unchecked_union);
2978 /* If it is a tagged record force the type to BLKmode to insure that
2979 these objects will always be put in memory. Likewise for limited
2981 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2982 SET_TYPE_MODE (gnu_type, BLKmode);
2984 /* We used to remove the associations of the discriminants and _Parent
2985 for validity checking but we may need them if there's a Freeze_Node
2986 for a subtype used in this record. */
2987 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2989 /* Fill in locations of fields. */
2990 annotate_rep (gnat_entity, gnu_type);
2992 /* If there are any entities in the chain corresponding to components
2993 that we did not elaborate, ensure we elaborate their types if they
2995 for (gnat_temp = First_Entity (gnat_entity);
2996 Present (gnat_temp);
2997 gnat_temp = Next_Entity (gnat_temp))
2998 if ((Ekind (gnat_temp) == E_Component
2999 || Ekind (gnat_temp) == E_Discriminant)
3000 && Is_Itype (Etype (gnat_temp))
3001 && !present_gnu_tree (gnat_temp))
3002 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3006 case E_Class_Wide_Subtype:
3007 /* If an equivalent type is present, that is what we should use.
3008 Otherwise, fall through to handle this like a record subtype
3009 since it may have constraints. */
3010 if (gnat_equiv_type != gnat_entity)
3012 gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
3013 maybe_present = true;
3017 /* ... fall through ... */
3019 case E_Record_Subtype:
3020 /* If Cloned_Subtype is Present it means this record subtype has
3021 identical layout to that type or subtype and we should use
3022 that GCC type for this one. The front end guarantees that
3023 the component list is shared. */
3024 if (Present (Cloned_Subtype (gnat_entity)))
3026 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3028 maybe_present = true;
3032 /* Otherwise, first ensure the base type is elaborated. Then, if we are
3033 changing the type, make a new type with each field having the type of
3034 the field in the new subtype but the position computed by transforming
3035 every discriminant reference according to the constraints. We don't
3036 see any difference between private and non-private type here since
3037 derivations from types should have been deferred until the completion
3038 of the private type. */
3041 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3046 defer_incomplete_level++;
3047 this_deferred = true;
3050 gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3052 if (present_gnu_tree (gnat_entity))
3054 maybe_present = true;
3058 /* When the subtype has discriminants and these discriminants affect
3059 the initial shape it has inherited, factor them in. But for the
3060 of an Unchecked_Union (it must be an Itype), just return the type.
3062 We can't just test Is_Constrained because private subtypes without
3063 discriminants of types with discriminants with default expressions
3064 are Is_Constrained but aren't constrained! */
3065 if (IN (Ekind (gnat_base_type), Record_Kind)
3066 && !Is_Unchecked_Union (gnat_base_type)
3067 && !Is_For_Access_Subtype (gnat_entity)
3068 && Is_Constrained (gnat_entity)
3069 && Has_Discriminants (gnat_entity)
3070 && Present (Discriminant_Constraint (gnat_entity))
3071 && Stored_Constraint (gnat_entity) != No_Elist)
3074 = build_subst_list (gnat_entity, gnat_base_type, definition);
3075 tree gnu_pos_list, gnu_field_list = NULL_TREE;
3076 tree gnu_unpad_base_type, t;
3077 Entity_Id gnat_field;
3079 gnu_type = make_node (RECORD_TYPE);
3080 TYPE_NAME (gnu_type) = gnu_entity_name;
3082 /* Set the size, alignment and alias set of the new type to
3083 match that of the old one, doing required substitutions.
3084 We do it this early because we need the size of the new
3085 type below to discard old fields if necessary. */
3086 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3087 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3088 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3089 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3090 relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
3092 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3093 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3094 TYPE_SIZE (gnu_type)
3095 = substitute_in_expr (TYPE_SIZE (gnu_type),
3099 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3100 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3101 TYPE_SIZE_UNIT (gnu_type)
3102 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3106 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3107 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3109 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3113 if (TREE_CODE (gnu_base_type) == RECORD_TYPE
3114 && TYPE_IS_PADDING_P (gnu_base_type))
3115 gnu_unpad_base_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3117 gnu_unpad_base_type = gnu_base_type;
3120 = compute_field_positions (gnu_unpad_base_type, NULL_TREE,
3121 size_zero_node, bitsize_zero_node,
3124 for (gnat_field = First_Entity (gnat_entity);
3125 Present (gnat_field);
3126 gnat_field = Next_Entity (gnat_field))
3127 if ((Ekind (gnat_field) == E_Component
3128 || Ekind (gnat_field) == E_Discriminant)
3129 && !(Present (Corresponding_Discriminant (gnat_field))
3130 && Is_Tagged_Type (gnat_base_type))
3131 && Underlying_Type (Scope (Original_Record_Component
3135 Name_Id gnat_name = Chars (gnat_field);
3136 Entity_Id gnat_old_field
3137 = Original_Record_Component (gnat_field);
3139 = gnat_to_gnu_field_decl (gnat_old_field);
3142 (purpose_member (gnu_old_field, gnu_pos_list));
3143 tree gnu_pos = TREE_PURPOSE (gnu_offset);
3144 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3145 tree gnu_field, gnu_field_type, gnu_size, gnu_new_pos;
3146 tree gnu_last = NULL_TREE;
3147 unsigned int offset_align
3149 (TREE_PURPOSE (TREE_VALUE (gnu_offset)), 1);
3151 /* If the type is the same, retrieve the GCC type from the
3152 old field to take into account possible adjustments. */
3153 if (Etype (gnat_field) == Etype (gnat_old_field))
3154 gnu_field_type = TREE_TYPE (gnu_old_field);
3156 gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
3158 /* If there was a component clause, the field types must be
3159 the same for the type and subtype, so copy the data from
3160 the old field to avoid recomputation here. Also if the
3161 field is justified modular and the optimization in
3162 gnat_to_gnu_field was applied. */
3163 if (Present (Component_Clause (gnat_old_field))
3164 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3165 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3166 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3167 == TREE_TYPE (gnu_old_field)))
3169 gnu_size = DECL_SIZE (gnu_old_field);
3170 gnu_field_type = TREE_TYPE (gnu_old_field);
3173 /* If the old field was packed and of constant size, we
3174 have to get the old size here, as it might differ from
3175 what the Etype conveys and the latter might overlap
3176 onto the following field. Try to arrange the type for
3177 possible better packing along the way. */
3178 else if (DECL_PACKED (gnu_old_field)
3179 && TREE_CODE (DECL_SIZE (gnu_old_field))
3182 gnu_size = DECL_SIZE (gnu_old_field);
3183 if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3184 && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
3185 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3187 = make_packable_type (gnu_field_type, true);
3191 gnu_size = TYPE_SIZE (gnu_field_type);
3193 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3194 for (t = gnu_subst_list; t; t = TREE_CHAIN (t))
3195 gnu_pos = substitute_in_expr (gnu_pos,
3199 /* If the position is now a constant, we can set it as the
3200 position of the field when we make it. Otherwise, we
3201 need to deal with it specially below. */
3202 if (TREE_CONSTANT (gnu_pos))
3204 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3206 /* Discard old fields that are outside the new type.
3207 This avoids confusing code scanning it to decide
3208 how to pass it to functions on some platforms. */
3209 if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3210 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3211 && !integer_zerop (gnu_size)
3212 && !tree_int_cst_lt (gnu_new_pos,
3213 TYPE_SIZE (gnu_type)))
3217 gnu_new_pos = NULL_TREE;
3221 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3222 DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3223 !DECL_NONADDRESSABLE_P (gnu_old_field));
3225 if (!TREE_CONSTANT (gnu_pos))
3227 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3228 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3229 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3230 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3231 DECL_SIZE (gnu_field) = gnu_size;
3232 DECL_SIZE_UNIT (gnu_field)
3233 = convert (sizetype,
3234 size_binop (CEIL_DIV_EXPR, gnu_size,
3235 bitsize_unit_node));
3236 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3239 DECL_INTERNAL_P (gnu_field)
3240 = DECL_INTERNAL_P (gnu_old_field);
3241 SET_DECL_ORIGINAL_FIELD
3242 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3243 ? DECL_ORIGINAL_FIELD (gnu_old_field)
3245 DECL_DISCRIMINANT_NUMBER (gnu_field)
3246 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3247 TREE_THIS_VOLATILE (gnu_field)
3248 = TREE_THIS_VOLATILE (gnu_old_field);
3250 /* To match the layout crafted in components_to_record,
3251 if this is the _Tag or _Parent field,