1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2007, 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 2, 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 distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
22 * GNAT was originally developed by the GNAT team at New York University. *
23 * Extensive contributions were provided by Ada Core Technologies Inc. *
25 ****************************************************************************/
29 #include "coretypes.h"
57 /* Convention_Stdcall should be processed in a specific way on Windows targets
58 only. The macro below is a helper to avoid having to check for a Windows
59 specific attribute throughout this unit. */
61 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
62 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
64 #define Has_Stdcall_Convention(E) (0)
67 /* These two variables are used to defer recursively expanding incomplete
68 types while we are processing a record or subprogram type. */
70 static int defer_incomplete_level = 0;
71 static struct incomplete
73 struct incomplete *next;
76 } *defer_incomplete_list = 0;
78 /* These two variables are used to defer emission of debug information for
79 nested incomplete record types */
81 static int defer_debug_level = 0;
82 static tree defer_debug_incomplete_list;
84 /* A hash table used as to cache the result of annotate_value. */
85 static GTY ((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
86 htab_t annotate_value_cache;
88 static void copy_alias_set (tree, tree);
89 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
90 static bool allocatable_size_p (tree, bool);
91 static void prepend_attributes (Entity_Id, struct attrib **);
92 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
93 static bool is_variable_size (tree);
94 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
96 static tree make_packable_type (tree);
97 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
98 static bool same_discriminant_p (Entity_Id, Entity_Id);
99 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
100 bool, bool, bool, bool);
101 static int compare_field_bitpos (const PTR, const PTR);
102 static Uint annotate_value (tree);
103 static void annotate_rep (Entity_Id, tree);
104 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
105 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
106 static void set_rm_size (Uint, tree, Entity_Id);
107 static tree make_type_from_size (tree, tree, bool);
108 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
109 static void check_ok_for_atomic (tree, Entity_Id, bool);
110 static int compatible_signatures_p (tree ftype1, tree ftype2);
112 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
113 GCC type corresponding to that entity. GNAT_ENTITY is assumed to
114 refer to an Ada type. */
117 gnat_to_gnu_type (Entity_Id gnat_entity)
121 /* The back end never attempts to annotate generic types */
122 if (Is_Generic_Type (gnat_entity) && type_annotate_only)
123 return void_type_node;
125 /* Convert the ada entity type into a GCC TYPE_DECL node. */
126 gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
127 gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
128 return TREE_TYPE (gnu_decl);
131 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
132 entity, this routine returns the equivalent GCC tree for that entity
133 (an ..._DECL node) and associates the ..._DECL node with the input GNAT
136 If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
137 initial value (in GCC tree form). This is optional for variables.
138 For renamed entities, GNU_EXPR gives the object being renamed.
140 DEFINITION is nonzero if this call is intended for a definition. This is
141 used for separate compilation where it necessary to know whether an
142 external declaration or a definition should be created if the GCC equivalent
143 was not created previously. The value of 1 is normally used for a nonzero
144 DEFINITION, but a value of 2 is used in special circumstances, defined in
148 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
151 tree gnu_type = NULL_TREE;
152 /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
153 GNAT tree. This node will be associated with the GNAT node by calling
154 the save_gnu_tree routine at the end of the `switch' statement. */
155 tree gnu_decl = NULL_TREE;
156 /* true if we have already saved gnu_decl as a gnat association. */
158 /* Nonzero if we incremented defer_incomplete_level. */
159 bool this_deferred = false;
160 /* Nonzero if we incremented defer_debug_level. */
161 bool debug_deferred = false;
162 /* Nonzero if we incremented force_global. */
163 bool this_global = false;
164 /* Nonzero if we should check to see if elaborated during processing. */
165 bool maybe_present = false;
166 /* Nonzero if we made GNU_DECL and its type here. */
167 bool this_made_decl = false;
168 struct attrib *attr_list = NULL;
169 bool debug_info_p = (Needs_Debug_Info (gnat_entity)
170 || debug_info_level == DINFO_LEVEL_VERBOSE);
171 Entity_Kind kind = Ekind (gnat_entity);
174 = ((Known_Esize (gnat_entity)
175 && UI_Is_In_Int_Range (Esize (gnat_entity)))
176 ? MIN (UI_To_Int (Esize (gnat_entity)),
177 IN (kind, Float_Kind)
178 ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
179 : IN (kind, Access_Kind) ? POINTER_SIZE * 2
180 : LONG_LONG_TYPE_SIZE)
181 : LONG_LONG_TYPE_SIZE);
184 = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
185 unsigned int align = 0;
187 /* Since a use of an Itype is a definition, process it as such if it
188 is not in a with'ed unit. */
190 if (!definition && Is_Itype (gnat_entity)
191 && !present_gnu_tree (gnat_entity)
192 && In_Extended_Main_Code_Unit (gnat_entity))
194 /* Ensure that we are in a subprogram mentioned in the Scope
195 chain of this entity, our current scope is global,
196 or that we encountered a task or entry (where we can't currently
197 accurately check scoping). */
198 if (!current_function_decl
199 || DECL_ELABORATION_PROC_P (current_function_decl))
201 process_type (gnat_entity);
202 return get_gnu_tree (gnat_entity);
205 for (gnat_temp = Scope (gnat_entity);
206 Present (gnat_temp); gnat_temp = Scope (gnat_temp))
208 if (Is_Type (gnat_temp))
209 gnat_temp = Underlying_Type (gnat_temp);
211 if (Ekind (gnat_temp) == E_Subprogram_Body)
213 = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
215 if (IN (Ekind (gnat_temp), Subprogram_Kind)
216 && Present (Protected_Body_Subprogram (gnat_temp)))
217 gnat_temp = Protected_Body_Subprogram (gnat_temp);
219 if (Ekind (gnat_temp) == E_Entry
220 || Ekind (gnat_temp) == E_Entry_Family
221 || Ekind (gnat_temp) == E_Task_Type
222 || (IN (Ekind (gnat_temp), Subprogram_Kind)
223 && present_gnu_tree (gnat_temp)
224 && (current_function_decl
225 == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
227 process_type (gnat_entity);
228 return get_gnu_tree (gnat_entity);
232 /* This abort means the entity "gnat_entity" has an incorrect scope,
233 i.e. that its scope does not correspond to the subprogram in which
238 /* If this is entity 0, something went badly wrong. */
239 gcc_assert (Present (gnat_entity));
241 /* If we've already processed this entity, return what we got last time.
242 If we are defining the node, we should not have already processed it.
243 In that case, we will abort below when we try to save a new GCC tree for
244 this object. We also need to handle the case of getting a dummy type
245 when a Full_View exists. */
247 if (present_gnu_tree (gnat_entity)
249 || (Is_Type (gnat_entity) && imported_p)))
251 gnu_decl = get_gnu_tree (gnat_entity);
253 if (TREE_CODE (gnu_decl) == TYPE_DECL
254 && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
255 && IN (kind, Incomplete_Or_Private_Kind)
256 && Present (Full_View (gnat_entity)))
258 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
261 save_gnu_tree (gnat_entity, NULL_TREE, false);
262 save_gnu_tree (gnat_entity, gnu_decl, false);
268 /* If this is a numeric or enumeral type, or an access type, a nonzero
269 Esize must be specified unless it was specified by the programmer. */
270 gcc_assert (!Unknown_Esize (gnat_entity)
271 || Has_Size_Clause (gnat_entity)
272 || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
273 && (!IN (kind, Access_Kind)
274 || kind == E_Access_Protected_Subprogram_Type
275 || kind == E_Access_Subtype)));
277 /* Likewise, RM_Size must be specified for all discrete and fixed-point
279 gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
280 || !Unknown_RM_Size (gnat_entity));
282 /* Get the name of the entity and set up the line number and filename of
283 the original definition for use in any decl we make. */
284 gnu_entity_id = get_entity_name (gnat_entity);
285 Sloc_to_locus (Sloc (gnat_entity), &input_location);
287 /* If we get here, it means we have not yet done anything with this
288 entity. If we are not defining it here, it must be external,
289 otherwise we should have defined it already. */
290 gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
291 || kind == E_Discriminant || kind == E_Component
293 || (kind == E_Constant && Present (Full_View (gnat_entity)))
294 || IN (kind, Type_Kind));
296 /* For cases when we are not defining (i.e., we are referencing from
297 another compilation unit) Public entities, show we are at global level
298 for the purpose of computing scopes. Don't do this for components or
299 discriminants since the relevant test is whether or not the record is
300 being defined. But do this for Imported functions or procedures in
302 if ((!definition && Is_Public (gnat_entity)
303 && !Is_Statically_Allocated (gnat_entity)
304 && kind != E_Discriminant && kind != E_Component)
305 || (Is_Imported (gnat_entity)
306 && (kind == E_Function || kind == E_Procedure)))
307 force_global++, this_global = true;
309 /* Handle any attributes directly attached to the entity. */
310 if (Has_Gigi_Rep_Item (gnat_entity))
311 prepend_attributes (gnat_entity, &attr_list);
313 /* Machine_Attributes on types are expected to be propagated to subtypes.
314 The corresponding Gigi_Rep_Items are only attached to the first subtype
315 though, so we handle the propagation here. */
316 if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
317 && !Is_First_Subtype (gnat_entity)
318 && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
319 prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
324 /* If this is a use of a deferred constant, get its full
326 if (!definition && Present (Full_View (gnat_entity)))
328 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
329 gnu_expr, definition);
334 /* If we have an external constant that we are not defining,
335 get the expression that is was defined to represent. We
336 may throw that expression away later if it is not a
338 Do not retrieve the expression if it is an aggregate, because
339 in complex instantiation contexts it may not be expanded */
342 && Present (Expression (Declaration_Node (gnat_entity)))
343 && !No_Initialization (Declaration_Node (gnat_entity))
344 && (Nkind (Expression (Declaration_Node (gnat_entity)))
346 gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
348 /* Ignore deferred constant definitions; they are processed fully in the
349 front-end. For deferred constant references, get the full
350 definition. On the other hand, constants that are renamings are
351 handled like variable renamings. If No_Initialization is set, this is
352 not a deferred constant but a constant whose value is built
355 if (definition && !gnu_expr
356 && !No_Initialization (Declaration_Node (gnat_entity))
357 && No (Renamed_Object (gnat_entity)))
359 gnu_decl = error_mark_node;
363 else if (!definition && IN (kind, Incomplete_Or_Private_Kind)
364 && Present (Full_View (gnat_entity)))
366 gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
375 /* We used to special case VMS exceptions here to directly map them to
376 their associated condition code. Since this code had to be masked
377 dynamically to strip off the severity bits, this caused trouble in
378 the GCC/ZCX case because the "type" pointers we store in the tables
379 have to be static. We now don't special case here anymore, and let
380 the regular processing take place, which leaves us with a regular
381 exception data object for VMS exceptions too. The condition code
382 mapping is taken care of by the front end and the bitmasking by the
389 /* The GNAT record where the component was defined. */
390 Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
392 /* If the variable is an inherited record component (in the case of
393 extended record types), just return the inherited entity, which
394 must be a FIELD_DECL. Likewise for discriminants.
395 For discriminants of untagged records which have explicit
396 stored discriminants, return the entity for the corresponding
397 stored discriminant. Also use Original_Record_Component
398 if the record has a private extension. */
400 if (Present (Original_Record_Component (gnat_entity))
401 && Original_Record_Component (gnat_entity) != gnat_entity)
404 = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
405 gnu_expr, definition);
410 /* If the enclosing record has explicit stored discriminants,
411 then it is an untagged record. If the Corresponding_Discriminant
412 is not empty then this must be a renamed discriminant and its
413 Original_Record_Component must point to the corresponding explicit
414 stored discriminant (i.e., we should have taken the previous
417 else if (Present (Corresponding_Discriminant (gnat_entity))
418 && Is_Tagged_Type (gnat_record))
420 /* A tagged record has no explicit stored discriminants. */
422 gcc_assert (First_Discriminant (gnat_record)
423 == First_Stored_Discriminant (gnat_record));
425 = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
426 gnu_expr, definition);
431 else if (Present (CR_Discriminant (gnat_entity))
432 && type_annotate_only)
434 gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
435 gnu_expr, definition);
440 /* If the enclosing record has explicit stored discriminants,
441 then it is an untagged record. If the Corresponding_Discriminant
442 is not empty then this must be a renamed discriminant and its
443 Original_Record_Component must point to the corresponding explicit
444 stored discriminant (i.e., we should have taken the first
447 else if (Present (Corresponding_Discriminant (gnat_entity))
448 && (First_Discriminant (gnat_record)
449 != First_Stored_Discriminant (gnat_record)))
452 /* Otherwise, if we are not defining this and we have no GCC type
453 for the containing record, make one for it. Then we should
454 have made our own equivalent. */
455 else if (!definition && !present_gnu_tree (gnat_record))
457 /* ??? If this is in a record whose scope is a protected
458 type and we have an Original_Record_Component, use it.
459 This is a workaround for major problems in protected type
462 Entity_Id Scop = Scope (Scope (gnat_entity));
463 if ((Is_Protected_Type (Scop)
464 || (Is_Private_Type (Scop)
465 && Present (Full_View (Scop))
466 && Is_Protected_Type (Full_View (Scop))))
467 && Present (Original_Record_Component (gnat_entity)))
470 = gnat_to_gnu_entity (Original_Record_Component
472 gnu_expr, definition);
477 gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
478 gnu_decl = get_gnu_tree (gnat_entity);
484 /* Here we have no GCC type and this is a reference rather than a
485 definition. This should never happen. Most likely the cause is a
486 reference before declaration in the gnat tree for gnat_entity. */
490 case E_Loop_Parameter:
491 case E_Out_Parameter:
494 /* Simple variables, loop variables, OUT parameters, and exceptions. */
497 bool used_by_ref = false;
499 = ((kind == E_Constant || kind == E_Variable)
500 && !Is_Statically_Allocated (gnat_entity)
501 && Is_True_Constant (gnat_entity)
502 && (((Nkind (Declaration_Node (gnat_entity))
503 == N_Object_Declaration)
504 && Present (Expression (Declaration_Node (gnat_entity))))
505 || Present (Renamed_Object (gnat_entity))));
506 bool inner_const_flag = const_flag;
507 bool static_p = Is_Statically_Allocated (gnat_entity);
508 bool mutable_p = false;
509 tree gnu_ext_name = NULL_TREE;
510 tree renamed_obj = NULL_TREE;
512 if (Present (Renamed_Object (gnat_entity)) && !definition)
514 if (kind == E_Exception)
515 gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
518 gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
521 /* Get the type after elaborating the renamed object. */
522 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
524 /* If this is a loop variable, its type should be the base type.
525 This is because the code for processing a loop determines whether
526 a normal loop end test can be done by comparing the bounds of the
527 loop against those of the base type, which is presumed to be the
528 size used for computation. But this is not correct when the size
529 of the subtype is smaller than the type. */
530 if (kind == E_Loop_Parameter)
531 gnu_type = get_base_type (gnu_type);
533 /* Reject non-renamed objects whose types are unconstrained arrays or
534 any object whose type is a dummy type or VOID_TYPE. */
536 if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
537 && No (Renamed_Object (gnat_entity)))
538 || TYPE_IS_DUMMY_P (gnu_type)
539 || TREE_CODE (gnu_type) == VOID_TYPE)
541 gcc_assert (type_annotate_only);
544 return error_mark_node;
547 /* If an alignment is specified, use it if valid. Note that
548 exceptions are objects but don't have alignments. We must do this
549 before we validate the size, since the alignment can affect the
551 if (kind != E_Exception && Known_Alignment (gnat_entity))
553 gcc_assert (Present (Alignment (gnat_entity)));
554 align = validate_alignment (Alignment (gnat_entity), gnat_entity,
555 TYPE_ALIGN (gnu_type));
556 gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align,
557 gnat_entity, "PAD", 0, definition, 1);
560 /* If we are defining the object, see if it has a Size value and
561 validate it if so. If we are not defining the object and a Size
562 clause applies, simply retrieve the value. We don't want to ignore
563 the clause and it is expected to have been validated already. Then
564 get the new type, if any. */
566 gnu_size = validate_size (Esize (gnat_entity), gnu_type,
567 gnat_entity, VAR_DECL, false,
568 Has_Size_Clause (gnat_entity));
569 else if (Has_Size_Clause (gnat_entity))
570 gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
575 = make_type_from_size (gnu_type, gnu_size,
576 Has_Biased_Representation (gnat_entity));
578 if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
579 gnu_size = NULL_TREE;
582 /* If this object has self-referential size, it must be a record with
583 a default value. We are supposed to allocate an object of the
584 maximum size in this case unless it is a constant with an
585 initializing expression, in which case we can get the size from
586 that. Note that the resulting size may still be a variable, so
587 this may end up with an indirect allocation. */
589 if (No (Renamed_Object (gnat_entity))
590 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
592 if (gnu_expr && kind == E_Constant)
594 = SUBSTITUTE_PLACEHOLDER_IN_EXPR
595 (TYPE_SIZE (TREE_TYPE (gnu_expr)), gnu_expr);
597 /* We may have no GNU_EXPR because No_Initialization is
598 set even though there's an Expression. */
599 else if (kind == E_Constant
600 && (Nkind (Declaration_Node (gnat_entity))
601 == N_Object_Declaration)
602 && Present (Expression (Declaration_Node (gnat_entity))))
604 = TYPE_SIZE (gnat_to_gnu_type
606 (Expression (Declaration_Node (gnat_entity)))));
609 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
614 /* If the size is zero bytes, make it one byte since some linkers have
615 trouble with zero-sized objects. If the object will have a
616 template, that will make it nonzero so don't bother. Also avoid
617 doing that for an object renaming or an object with an address
618 clause, as we would lose useful information on the view size
619 (e.g. for null array slices) and we are not allocating the object
621 if (((gnu_size && integer_zerop (gnu_size))
622 || (TYPE_SIZE (gnu_type) && integer_zerop (TYPE_SIZE (gnu_type))))
623 && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
624 || !Is_Array_Type (Etype (gnat_entity)))
625 && !Present (Renamed_Object (gnat_entity))
626 && !Present (Address_Clause (gnat_entity)))
627 gnu_size = bitsize_unit_node;
629 /* If this is an atomic object with no specified size and alignment,
630 but where the size of the type is a constant, set the alignment to
631 the lowest power of two greater than the size, or to the
632 biggest meaningful alignment, whichever is smaller. */
634 if (Is_Atomic (gnat_entity) && !gnu_size && align == 0
635 && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
637 if (!host_integerp (TYPE_SIZE (gnu_type), 1)
638 || 0 <= compare_tree_int (TYPE_SIZE (gnu_type),
640 align = BIGGEST_ALIGNMENT;
642 align = ((unsigned int) 1
643 << (floor_log2 (tree_low_cst
644 (TYPE_SIZE (gnu_type), 1) - 1)
648 /* If the object is set to have atomic components, find the component
649 type and validate it.
651 ??? Note that we ignore Has_Volatile_Components on objects; it's
652 not at all clear what to do in that case. */
654 if (Has_Atomic_Components (gnat_entity))
656 tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
657 ? TREE_TYPE (gnu_type) : gnu_type);
659 while (TREE_CODE (gnu_inner) == ARRAY_TYPE
660 && TYPE_MULTI_ARRAY_P (gnu_inner))
661 gnu_inner = TREE_TYPE (gnu_inner);
663 check_ok_for_atomic (gnu_inner, gnat_entity, true);
666 /* Now check if the type of the object allows atomic access. Note
667 that we must test the type, even if this object has size and
668 alignment to allow such access, because we will be going
669 inside the padded record to assign to the object. We could fix
670 this by always copying via an intermediate value, but it's not
671 clear it's worth the effort. */
672 if (Is_Atomic (gnat_entity))
673 check_ok_for_atomic (gnu_type, gnat_entity, false);
675 /* If this is an aliased object with an unconstrained nominal subtype,
676 make a type that includes the template. */
677 if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
678 && Is_Array_Type (Etype (gnat_entity))
679 && !type_annotate_only)
682 = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
685 = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
686 concat_id_with_name (gnu_entity_id,
690 #ifdef MINIMUM_ATOMIC_ALIGNMENT
691 /* If the size is a constant and no alignment is specified, force
692 the alignment to be the minimum valid atomic alignment. The
693 restriction on constant size avoids problems with variable-size
694 temporaries; if the size is variable, there's no issue with
695 atomic access. Also don't do this for a constant, since it isn't
696 necessary and can interfere with constant replacement. Finally,
697 do not do it for Out parameters since that creates an
698 size inconsistency with In parameters. */
699 if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
700 && !FLOAT_TYPE_P (gnu_type)
701 && !const_flag && No (Renamed_Object (gnat_entity))
702 && !imported_p && No (Address_Clause (gnat_entity))
703 && kind != E_Out_Parameter
704 && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
705 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
706 align = MINIMUM_ATOMIC_ALIGNMENT;
709 /* Make a new type with the desired size and alignment, if needed. */
710 gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
711 "PAD", false, definition, true);
713 /* Make a volatile version of this object's type if we are to
714 make the object volatile. Note that 13.3(19) says that we
715 should treat other types of objects as volatile as well. */
716 if ((Treat_As_Volatile (gnat_entity)
717 || Is_Exported (gnat_entity)
718 || Is_Imported (gnat_entity)
719 || Present (Address_Clause (gnat_entity)))
720 && !TYPE_VOLATILE (gnu_type))
721 gnu_type = build_qualified_type (gnu_type,
722 (TYPE_QUALS (gnu_type)
723 | TYPE_QUAL_VOLATILE));
725 /* Convert the expression to the type of the object except in the
726 case where the object's type is unconstrained or the object's type
727 is a padded record whose field is of self-referential size. In
728 the former case, converting will generate unnecessary evaluations
729 of the CONSTRUCTOR to compute the size and in the latter case, we
730 want to only copy the actual data. */
732 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
733 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
734 && !(TREE_CODE (gnu_type) == RECORD_TYPE
735 && TYPE_IS_PADDING_P (gnu_type)
736 && (CONTAINS_PLACEHOLDER_P
737 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
738 gnu_expr = convert (gnu_type, gnu_expr);
740 /* See if this is a renaming, and handle appropriately depending on
741 what is renamed and in which context. There are three major
744 1/ This is a constant renaming and we can just make an object
745 with what is renamed as its initial value,
747 2/ We can reuse a stabilized version of what is renamed in place
750 3/ If neither 1 or 2 applies, we make the renaming entity a constant
751 pointer to what is being renamed. */
753 if (Present (Renamed_Object (gnat_entity)))
755 /* If the renamed object had padding, strip off the reference
756 to the inner object and reset our type. */
757 if (TREE_CODE (gnu_expr) == COMPONENT_REF
758 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
760 && (TYPE_IS_PADDING_P
761 (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
763 gnu_expr = TREE_OPERAND (gnu_expr, 0);
764 gnu_type = TREE_TYPE (gnu_expr);
767 /* Case 1: If this is a constant renaming, treat it as a normal
768 object whose initial value is what is being renamed. We cannot
769 do this if the type is unconstrained or class-wide. */
771 && !TREE_SIDE_EFFECTS (gnu_expr)
772 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
773 && TYPE_MODE (gnu_type) != BLKmode
774 && Ekind (Etype (gnat_entity)) != E_Class_Wide_Type
775 && !Is_Array_Type (Etype (gnat_entity)))
778 /* Otherwise, see if we can proceed with a stabilized version of
779 the renamed entity or if we need to make a pointer. */
782 bool stabilized = false;
783 tree maybe_stable_expr = NULL_TREE;
785 /* Case 2: If the renaming entity need not be materialized and
786 the renamed expression is something we can stabilize, use
787 that for the renaming. At the global level, we can only do
788 this if we know no SAVE_EXPRs need be made, because the
789 expression we return might be used in arbitrary conditional
790 branches so we must force the SAVE_EXPRs evaluation
791 immediately and this requires a function context. */
792 if (!Materialize_Entity (gnat_entity)
793 && (!global_bindings_p ()
794 || (staticp (gnu_expr)
795 && !TREE_SIDE_EFFECTS (gnu_expr))))
798 = maybe_stabilize_reference (gnu_expr, true, false,
803 gnu_decl = maybe_stable_expr;
804 save_gnu_tree (gnat_entity, gnu_decl, true);
809 /* The stabilization failed. Keep maybe_stable_expr
810 untouched here to let the pointer case below know
811 about that failure. */
814 /* Case 3: Make this into a constant pointer to the object we
815 are to rename and attach the object to the pointer if it is
816 an lvalue that can be stabilized.
818 From the proper scope, attached objects will be referenced
819 directly instead of indirectly via the pointer to avoid
820 subtle aliasing problems with non addressable entities.
821 They have to be stable because we must not evaluate the
822 variables in the expression every time the renaming is used.
823 They also have to be lvalues because the context in which
824 they are reused sometimes requires so. We call pointers
825 with an attached object "renaming" pointers.
827 In the rare cases where we cannot stabilize the renamed
828 object, we just make a "bare" pointer, and the renamed
829 entity is always accessed indirectly through it. */
831 inner_const_flag = TREE_READONLY (gnu_expr);
833 gnu_type = build_reference_type (gnu_type);
835 /* If a previous attempt at unrestricted stabilization
836 failed, there is no point trying again and we can reuse
837 the result without attaching it to the pointer. In this
838 case it will only be used as the initializing expression
839 of the pointer and thus needs no special treatment with
840 regard to multiple evaluations. */
841 if (maybe_stable_expr)
844 /* Otherwise, try to stabilize now, restricting to lvalues
845 only, and attach the expression to the pointer if the
846 stabilization succeeds.
848 Note that this might introduce SAVE_EXPRs and we don't
849 check whether we're at the global level or not. This is
850 fine since we are building a pointer initializer and
851 neither the pointer nor the initializing expression can
852 be accessed before the pointer elaboration has taken
853 place in a correct program.
855 SAVE_EXPRs will be evaluated at the right spots by either
856 create_var_decl->expand_decl_init for the non-global case
857 or build_unit_elab for the global case, and will be
858 attached to the elaboration procedure by the RTL expander
859 in the latter case. We have no need to force an early
864 = maybe_stabilize_reference (gnu_expr, true, true,
868 renamed_obj = maybe_stable_expr;
870 /* Attaching is actually performed downstream, as soon
871 as we have a VAR_DECL for the pointer we make. */
875 = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
877 gnu_size = NULL_TREE;
883 /* If this is an aliased object whose nominal subtype is unconstrained,
884 the object is a record that contains both the template and
885 the object. If there is an initializer, it will have already
886 been converted to the right type, but we need to create the
887 template if there is no initializer. */
888 else if (definition && TREE_CODE (gnu_type) == RECORD_TYPE
889 && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
890 /* Beware that padding might have been introduced
891 via maybe_pad_type above. */
892 || (TYPE_IS_PADDING_P (gnu_type)
893 && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
895 && TYPE_CONTAINS_TEMPLATE_P
896 (TREE_TYPE (TYPE_FIELDS (gnu_type)))))
900 = TYPE_IS_PADDING_P (gnu_type)
901 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
902 : TYPE_FIELDS (gnu_type);
905 = gnat_build_constructor
909 build_template (TREE_TYPE (template_field),
910 TREE_TYPE (TREE_CHAIN (template_field)),
915 /* If this is a pointer and it does not have an initializing
916 expression, initialize it to NULL, unless the object is
919 && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
920 && !Is_Imported (gnat_entity) && !gnu_expr)
921 gnu_expr = integer_zero_node;
923 /* If we are defining the object and it has an Address clause we must
924 get the address expression from the saved GCC tree for the
925 object if the object has a Freeze_Node. Otherwise, we elaborate
926 the address expression here since the front-end has guaranteed
927 in that case that the elaboration has no effects. Note that
928 only the latter mechanism is currently in use. */
929 if (definition && Present (Address_Clause (gnat_entity)))
932 = (present_gnu_tree (gnat_entity) ? get_gnu_tree (gnat_entity)
933 : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
935 save_gnu_tree (gnat_entity, NULL_TREE, false);
937 /* Ignore the size. It's either meaningless or was handled
939 gnu_size = NULL_TREE;
940 /* The address expression contains a conversion from pointer type
941 to the system__address integer type, which means the address
942 of the underlying object escapes. We therefore have no other
943 choice than forcing the type of the object being defined to
944 alias everything in order to make type-based alias analysis
945 aware that it will dereference the escaped address.
946 ??? This uncovers problems in ACATS at -O2 with the volatility
947 of the original type: it may not be correctly propagated, thus
948 causing PRE to enter an infinite loop creating value numbers
949 out of volatile expressions. Disable it for now. */
951 = build_reference_type_for_mode (gnu_type, ptr_mode, false);
952 gnu_address = convert (gnu_type, gnu_address);
954 const_flag = !Is_Public (gnat_entity);
956 /* If we don't have an initializing expression for the underlying
957 variable, the initializing expression for the pointer is the
958 specified address. Otherwise, we have to make a COMPOUND_EXPR
959 to assign both the address and the initial value. */
961 gnu_expr = gnu_address;
964 = build2 (COMPOUND_EXPR, gnu_type,
966 (MODIFY_EXPR, NULL_TREE,
967 build_unary_op (INDIRECT_REF, NULL_TREE,
973 /* If it has an address clause and we are not defining it, mark it
974 as an indirect object. Likewise for Stdcall objects that are
976 if ((!definition && Present (Address_Clause (gnat_entity)))
977 || (Is_Imported (gnat_entity)
978 && Has_Stdcall_Convention (gnat_entity)))
980 /* See the definition case above for the rationale. */
982 = build_reference_type_for_mode (gnu_type, ptr_mode, false);
983 gnu_size = NULL_TREE;
985 gnu_expr = NULL_TREE;
986 /* No point in taking the address of an initializing expression
987 that isn't going to be used. */
992 /* If we are at top level and this object is of variable size,
993 make the actual type a hidden pointer to the real type and
994 make the initializer be a memory allocation and initialization.
995 Likewise for objects we aren't defining (presumed to be
996 external references from other packages), but there we do
997 not set up an initialization.
999 If the object's size overflows, make an allocator too, so that
1000 Storage_Error gets raised. Note that we will never free
1001 such memory, so we presume it never will get allocated. */
1003 if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1004 global_bindings_p () || !definition
1007 && ! allocatable_size_p (gnu_size,
1008 global_bindings_p () || !definition
1011 gnu_type = build_reference_type (gnu_type);
1012 gnu_size = NULL_TREE;
1016 /* In case this was a aliased object whose nominal subtype is
1017 unconstrained, the pointer above will be a thin pointer and
1018 build_allocator will automatically make the template.
1020 If we have a template initializer only (that we made above),
1021 pretend there is none and rely on what build_allocator creates
1022 again anyway. Otherwise (if we have a full initializer), get
1023 the data part and feed that to build_allocator.
1025 If we are elaborating a mutable object, tell build_allocator to
1026 ignore a possibly simpler size from the initializer, if any, as
1027 we must allocate the maximum possible size in this case. */
1031 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1033 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1034 && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1037 = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1039 if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1040 && 1 == VEC_length (constructor_elt,
1041 CONSTRUCTOR_ELTS (gnu_expr)))
1045 = build_component_ref
1046 (gnu_expr, NULL_TREE,
1047 TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1051 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1052 && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1053 && !Is_Imported (gnat_entity))
1054 post_error ("Storage_Error will be raised at run-time?",
1057 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1058 0, 0, gnat_entity, mutable_p);
1062 gnu_expr = NULL_TREE;
1067 /* If this object would go into the stack and has an alignment
1068 larger than the default largest alignment, make a variable
1069 to hold the "aligning type" with a modified initial value,
1070 if any, then point to it and make that the value of this
1071 variable, which is now indirect. */
1072 if (!global_bindings_p () && !static_p && definition
1073 && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1076 = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1077 TYPE_SIZE_UNIT (gnu_type));
1081 = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1082 NULL_TREE, gnu_new_type, NULL_TREE, false,
1083 false, false, false, NULL, gnat_entity);
1087 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1089 (gnu_new_var, NULL_TREE,
1090 TYPE_FIELDS (gnu_new_type), false),
1094 gnu_type = build_reference_type (gnu_type);
1097 (ADDR_EXPR, gnu_type,
1098 build_component_ref (gnu_new_var, NULL_TREE,
1099 TYPE_FIELDS (gnu_new_type), false));
1101 gnu_size = NULL_TREE;
1107 gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1108 | TYPE_QUAL_CONST));
1110 /* Convert the expression to the type of the object except in the
1111 case where the object's type is unconstrained or the object's type
1112 is a padded record whose field is of self-referential size. In
1113 the former case, converting will generate unnecessary evaluations
1114 of the CONSTRUCTOR to compute the size and in the latter case, we
1115 want to only copy the actual data. */
1117 && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1118 && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1119 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1120 && TYPE_IS_PADDING_P (gnu_type)
1121 && (CONTAINS_PLACEHOLDER_P
1122 (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1123 gnu_expr = convert (gnu_type, gnu_expr);
1125 /* If this name is external or there was a name specified, use it,
1126 unless this is a VMS exception object since this would conflict
1127 with the symbol we need to export in addition. Don't use the
1128 Interface_Name if there is an address clause (see CD30005). */
1129 if (!Is_VMS_Exception (gnat_entity)
1130 && ((Present (Interface_Name (gnat_entity))
1131 && No (Address_Clause (gnat_entity)))
1132 || (Is_Public (gnat_entity)
1133 && (!Is_Imported (gnat_entity)
1134 || Is_Exported (gnat_entity)))))
1135 gnu_ext_name = create_concat_name (gnat_entity, 0);
1137 /* If this is constant initialized to a static constant and the
1138 object has an aggregate type, force it to be statically
1140 if (const_flag && gnu_expr && TREE_CONSTANT (gnu_expr)
1141 && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1142 && (AGGREGATE_TYPE_P (gnu_type)
1143 && !(TREE_CODE (gnu_type) == RECORD_TYPE
1144 && TYPE_IS_PADDING_P (gnu_type))))
1147 gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1148 gnu_expr, const_flag,
1149 Is_Public (gnat_entity),
1150 imported_p || !definition,
1151 static_p, attr_list, gnat_entity);
1152 DECL_BY_REF_P (gnu_decl) = used_by_ref;
1153 DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1154 if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1156 SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1157 if (global_bindings_p ())
1159 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1160 record_global_renaming_pointer (gnu_decl);
1164 if (definition && DECL_SIZE (gnu_decl)
1165 && get_block_jmpbuf_decl ()
1166 && (TREE_CODE (DECL_SIZE (gnu_decl)) != INTEGER_CST
1167 || (flag_stack_check && !STACK_CHECK_BUILTIN
1168 && 0 < compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1169 STACK_CHECK_MAX_VAR_SIZE))))
1170 add_stmt_with_node (build_call_1_expr
1171 (update_setjmp_buf_decl,
1172 build_unary_op (ADDR_EXPR, NULL_TREE,
1173 get_block_jmpbuf_decl ())),
1176 /* If this is a public constant or we're not optimizing and we're not
1177 making a VAR_DECL for it, make one just for export or debugger
1178 use. Likewise if the address is taken or if the object or type is
1180 if (definition && TREE_CODE (gnu_decl) == CONST_DECL
1181 && (Is_Public (gnat_entity)
1183 || Address_Taken (gnat_entity)
1184 || Is_Aliased (gnat_entity)
1185 || Is_Aliased (Etype (gnat_entity))))
1188 = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1189 gnu_expr, true, Is_Public (gnat_entity),
1190 false, static_p, NULL, gnat_entity);
1192 SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1195 /* If this is declared in a block that contains a block with an
1196 exception handler, we must force this variable in memory to
1197 suppress an invalid optimization. */
1198 if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1199 && Exception_Mechanism != Back_End_Exceptions)
1200 TREE_ADDRESSABLE (gnu_decl) = 1;
1202 /* Back-annotate the Alignment of the object if not already in the
1203 tree. Likewise for Esize if the object is of a constant size.
1204 But if the "object" is actually a pointer to an object, the
1205 alignment and size are the same as the type, so don't back-annotate
1206 the values for the pointer. */
1207 if (!used_by_ref && Unknown_Alignment (gnat_entity))
1208 Set_Alignment (gnat_entity,
1209 UI_From_Int (DECL_ALIGN (gnu_decl) / BITS_PER_UNIT));
1211 if (!used_by_ref && Unknown_Esize (gnat_entity)
1212 && DECL_SIZE (gnu_decl))
1214 tree gnu_back_size = DECL_SIZE (gnu_decl);
1216 if (TREE_CODE (TREE_TYPE (gnu_decl)) == RECORD_TYPE
1217 && TYPE_CONTAINS_TEMPLATE_P (TREE_TYPE (gnu_decl)))
1219 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN
1220 (TYPE_FIELDS (TREE_TYPE (gnu_decl)))));
1222 Set_Esize (gnat_entity, annotate_value (gnu_back_size));
1228 /* Return a TYPE_DECL for "void" that we previously made. */
1229 gnu_decl = void_type_decl_node;
1232 case E_Enumeration_Type:
1233 /* A special case, for the types Character and Wide_Character in
1234 Standard, we do not list all the literals. So if the literals
1235 are not specified, make this an unsigned type. */
1236 if (No (First_Literal (gnat_entity)))
1238 gnu_type = make_unsigned_type (esize);
1239 TYPE_NAME (gnu_type) = gnu_entity_id;
1241 /* Set the TYPE_STRING_FLAG for Ada Character and
1242 Wide_Character types. This is needed by the dwarf-2 debug writer to
1243 distinguish between unsigned integer types and character types. */
1244 TYPE_STRING_FLAG (gnu_type) = 1;
1248 /* Normal case of non-character type, or non-Standard character type */
1250 /* Here we have a list of enumeral constants in First_Literal.
1251 We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1252 the list to be places into TYPE_FIELDS. Each node in the list
1253 is a TREE_LIST node whose TREE_VALUE is the literal name
1254 and whose TREE_PURPOSE is the value of the literal.
1256 Esize contains the number of bits needed to represent the enumeral
1257 type, Type_Low_Bound also points to the first literal and
1258 Type_High_Bound points to the last literal. */
1260 Entity_Id gnat_literal;
1261 tree gnu_literal_list = NULL_TREE;
1263 if (Is_Unsigned_Type (gnat_entity))
1264 gnu_type = make_unsigned_type (esize);
1266 gnu_type = make_signed_type (esize);
1268 TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1270 for (gnat_literal = First_Literal (gnat_entity);
1271 Present (gnat_literal);
1272 gnat_literal = Next_Literal (gnat_literal))
1274 tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1277 = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1278 gnu_type, gnu_value, true, false, false,
1279 false, NULL, gnat_literal);
1281 save_gnu_tree (gnat_literal, gnu_literal, false);
1282 gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1283 gnu_value, gnu_literal_list);
1286 TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1288 /* Note that the bounds are updated at the end of this function
1289 because to avoid an infinite recursion when we get the bounds of
1290 this type, since those bounds are objects of this type. */
1294 case E_Signed_Integer_Type:
1295 case E_Ordinary_Fixed_Point_Type:
1296 case E_Decimal_Fixed_Point_Type:
1297 /* For integer types, just make a signed type the appropriate number
1299 gnu_type = make_signed_type (esize);
1302 case E_Modular_Integer_Type:
1303 /* For modular types, make the unsigned type of the proper number of
1304 bits and then set up the modulus, if required. */
1306 enum machine_mode mode;
1310 if (Is_Packed_Array_Type (gnat_entity))
1311 esize = UI_To_Int (RM_Size (gnat_entity));
1313 /* Find the smallest mode at least ESIZE bits wide and make a class
1316 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1317 GET_MODE_BITSIZE (mode) < esize;
1318 mode = GET_MODE_WIDER_MODE (mode))
1321 gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1322 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1323 = Is_Packed_Array_Type (gnat_entity);
1325 /* Get the modulus in this type. If it overflows, assume it is because
1326 it is equal to 2**Esize. Note that there is no overflow checking
1327 done on unsigned type, so we detect the overflow by looking for
1328 a modulus of zero, which is otherwise invalid. */
1329 gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1331 if (!integer_zerop (gnu_modulus))
1333 TYPE_MODULAR_P (gnu_type) = 1;
1334 SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1335 gnu_high = fold (build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1336 convert (gnu_type, integer_one_node)));
1339 /* If we have to set TYPE_PRECISION different from its natural value,
1340 make a subtype to do do. Likewise if there is a modulus and
1341 it is not one greater than TYPE_MAX_VALUE. */
1342 if (TYPE_PRECISION (gnu_type) != esize
1343 || (TYPE_MODULAR_P (gnu_type)
1344 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1346 tree gnu_subtype = make_node (INTEGER_TYPE);
1348 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1349 TREE_TYPE (gnu_subtype) = gnu_type;
1350 TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1351 TYPE_MAX_VALUE (gnu_subtype)
1352 = TYPE_MODULAR_P (gnu_type)
1353 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1354 TYPE_PRECISION (gnu_subtype) = esize;
1355 TYPE_UNSIGNED (gnu_subtype) = 1;
1356 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1357 TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1358 = Is_Packed_Array_Type (gnat_entity);
1359 layout_type (gnu_subtype);
1361 gnu_type = gnu_subtype;
1366 case E_Signed_Integer_Subtype:
1367 case E_Enumeration_Subtype:
1368 case E_Modular_Integer_Subtype:
1369 case E_Ordinary_Fixed_Point_Subtype:
1370 case E_Decimal_Fixed_Point_Subtype:
1372 /* For integral subtypes, we make a new INTEGER_TYPE. Note
1373 that we do not want to call build_range_type since we would
1374 like each subtype node to be distinct. This will be important
1375 when memory aliasing is implemented.
1377 The TREE_TYPE field of the INTEGER_TYPE we make points to the
1378 parent type; this fact is used by the arithmetic conversion
1381 We elaborate the Ancestor_Subtype if it is not in the current
1382 unit and one of our bounds is non-static. We do this to ensure
1383 consistent naming in the case where several subtypes share the same
1384 bounds by always elaborating the first such subtype first, thus
1388 && Present (Ancestor_Subtype (gnat_entity))
1389 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1390 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1391 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1392 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1393 gnu_expr, definition);
1395 gnu_type = make_node (INTEGER_TYPE);
1396 if (Is_Packed_Array_Type (gnat_entity))
1398 esize = UI_To_Int (RM_Size (gnat_entity));
1399 TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1402 TYPE_PRECISION (gnu_type) = esize;
1403 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1405 TYPE_MIN_VALUE (gnu_type)
1406 = convert (TREE_TYPE (gnu_type),
1407 elaborate_expression (Type_Low_Bound (gnat_entity),
1409 get_identifier ("L"), definition, 1,
1410 Needs_Debug_Info (gnat_entity)));
1412 TYPE_MAX_VALUE (gnu_type)
1413 = convert (TREE_TYPE (gnu_type),
1414 elaborate_expression (Type_High_Bound (gnat_entity),
1416 get_identifier ("U"), definition, 1,
1417 Needs_Debug_Info (gnat_entity)));
1419 /* One of the above calls might have caused us to be elaborated,
1420 so don't blow up if so. */
1421 if (present_gnu_tree (gnat_entity))
1423 maybe_present = true;
1427 TYPE_BIASED_REPRESENTATION_P (gnu_type)
1428 = Has_Biased_Representation (gnat_entity);
1430 /* This should be an unsigned type if the lower bound is constant
1431 and non-negative or if the base type is unsigned; a signed type
1433 TYPE_UNSIGNED (gnu_type)
1434 = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1435 || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1436 && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1437 || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1438 || Is_Unsigned_Type (gnat_entity));
1440 layout_type (gnu_type);
1442 /* Inherit our alias set from what we're a subtype of. Subtypes
1443 are not different types and a pointer can designate any instance
1444 within a subtype hierarchy. */
1445 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1447 /* If the type we are dealing with is to represent a packed array,
1448 we need to have the bits left justified on big-endian targets
1449 and right justified on little-endian targets. We also need to
1450 ensure that when the value is read (e.g. for comparison of two
1451 such values), we only get the good bits, since the unused bits
1452 are uninitialized. Both goals are accomplished by wrapping the
1453 modular value in an enclosing struct. */
1454 if (Is_Packed_Array_Type (gnat_entity))
1456 tree gnu_field_type = gnu_type;
1459 TYPE_RM_SIZE_NUM (gnu_field_type)
1460 = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1461 gnu_type = make_node (RECORD_TYPE);
1462 TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1463 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1464 TYPE_PACKED (gnu_type) = 1;
1466 /* Create a stripped-down declaration of the original type, mainly
1468 create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1469 NULL, true, debug_info_p, gnat_entity);
1471 /* Don't notify the field as "addressable", since we won't be taking
1472 it's address and it would prevent create_field_decl from making a
1474 gnu_field = create_field_decl (get_identifier ("OBJECT"),
1475 gnu_field_type, gnu_type, 1, 0, 0, 0);
1477 finish_record_type (gnu_type, gnu_field, false, false);
1478 TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1479 SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1481 copy_alias_set (gnu_type, gnu_field_type);
1486 case E_Floating_Point_Type:
1487 /* If this is a VAX floating-point type, use an integer of the proper
1488 size. All the operations will be handled with ASM statements. */
1489 if (Vax_Float (gnat_entity))
1491 gnu_type = make_signed_type (esize);
1492 TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1493 SET_TYPE_DIGITS_VALUE (gnu_type,
1494 UI_To_gnu (Digits_Value (gnat_entity),
1499 /* The type of the Low and High bounds can be our type if this is
1500 a type from Standard, so set them at the end of the function. */
1501 gnu_type = make_node (REAL_TYPE);
1502 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1503 layout_type (gnu_type);
1506 case E_Floating_Point_Subtype:
1507 if (Vax_Float (gnat_entity))
1509 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1515 && Present (Ancestor_Subtype (gnat_entity))
1516 && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1517 && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1518 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1519 gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1520 gnu_expr, definition);
1522 gnu_type = make_node (REAL_TYPE);
1523 TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1524 TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1526 TYPE_MIN_VALUE (gnu_type)
1527 = convert (TREE_TYPE (gnu_type),
1528 elaborate_expression (Type_Low_Bound (gnat_entity),
1529 gnat_entity, get_identifier ("L"),
1531 Needs_Debug_Info (gnat_entity)));
1533 TYPE_MAX_VALUE (gnu_type)
1534 = convert (TREE_TYPE (gnu_type),
1535 elaborate_expression (Type_High_Bound (gnat_entity),
1536 gnat_entity, get_identifier ("U"),
1538 Needs_Debug_Info (gnat_entity)));
1540 /* One of the above calls might have caused us to be elaborated,
1541 so don't blow up if so. */
1542 if (present_gnu_tree (gnat_entity))
1544 maybe_present = true;
1548 layout_type (gnu_type);
1550 /* Inherit our alias set from what we're a subtype of, as for
1551 integer subtypes. */
1552 copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1556 /* Array and String Types and Subtypes
1558 Unconstrained array types are represented by E_Array_Type and
1559 constrained array types are represented by E_Array_Subtype. There
1560 are no actual objects of an unconstrained array type; all we have
1561 are pointers to that type.
1563 The following fields are defined on array types and subtypes:
1565 Component_Type Component type of the array.
1566 Number_Dimensions Number of dimensions (an int).
1567 First_Index Type of first index. */
1572 tree gnu_template_fields = NULL_TREE;
1573 tree gnu_template_type = make_node (RECORD_TYPE);
1574 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1575 tree gnu_fat_type = make_node (RECORD_TYPE);
1576 int ndim = Number_Dimensions (gnat_entity);
1578 = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1580 = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1581 tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1582 tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1583 tree gnu_comp_size = 0;
1584 tree gnu_max_size = size_one_node;
1585 tree gnu_max_size_unit;
1587 Entity_Id gnat_ind_subtype;
1588 Entity_Id gnat_ind_base_subtype;
1589 tree gnu_template_reference;
1592 TYPE_NAME (gnu_template_type)
1593 = create_concat_name (gnat_entity, "XUB");
1594 TYPE_NAME (gnu_fat_type) = create_concat_name (gnat_entity, "XUP");
1595 TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1596 TYPE_READONLY (gnu_template_type) = 1;
1598 /* Make a node for the array. If we are not defining the array
1599 suppress expanding incomplete types. */
1600 gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1603 defer_incomplete_level++, this_deferred = true;
1605 /* Build the fat pointer type. Use a "void *" object instead of
1606 a pointer to the array type since we don't have the array type
1607 yet (it will reference the fat pointer via the bounds). */
1608 tem = chainon (chainon (NULL_TREE,
1609 create_field_decl (get_identifier ("P_ARRAY"),
1611 gnu_fat_type, 0, 0, 0, 0)),
1612 create_field_decl (get_identifier ("P_BOUNDS"),
1614 gnu_fat_type, 0, 0, 0, 0));
1616 /* Make sure we can put this into a register. */
1617 TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1618 finish_record_type (gnu_fat_type, tem, false, true);
1620 /* Build a reference to the template from a PLACEHOLDER_EXPR that
1621 is the fat pointer. This will be used to access the individual
1622 fields once we build them. */
1623 tem = build3 (COMPONENT_REF, gnu_ptr_template,
1624 build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1625 TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1626 gnu_template_reference
1627 = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1628 TREE_READONLY (gnu_template_reference) = 1;
1630 /* Now create the GCC type for each index and add the fields for
1631 that index to the template. */
1632 for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1633 gnat_ind_base_subtype
1634 = First_Index (Implementation_Base_Type (gnat_entity));
1635 index < ndim && index >= 0;
1637 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1638 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1640 char field_name[10];
1641 tree gnu_ind_subtype
1642 = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1643 tree gnu_base_subtype
1644 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1646 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1648 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1649 tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1651 /* Make the FIELD_DECLs for the minimum and maximum of this
1652 type and then make extractions of that field from the
1654 sprintf (field_name, "LB%d", index);
1655 gnu_min_field = create_field_decl (get_identifier (field_name),
1657 gnu_template_type, 0, 0, 0, 0);
1658 field_name[0] = 'U';
1659 gnu_max_field = create_field_decl (get_identifier (field_name),
1661 gnu_template_type, 0, 0, 0, 0);
1663 Sloc_to_locus (Sloc (gnat_entity),
1664 &DECL_SOURCE_LOCATION (gnu_min_field));
1665 Sloc_to_locus (Sloc (gnat_entity),
1666 &DECL_SOURCE_LOCATION (gnu_max_field));
1667 gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1669 /* We can't use build_component_ref here since the template
1670 type isn't complete yet. */
1671 gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1672 gnu_template_reference, gnu_min_field,
1674 gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1675 gnu_template_reference, gnu_max_field,
1677 TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1679 /* Make a range type with the new ranges, but using
1680 the Ada subtype. Then we convert to sizetype. */
1681 gnu_index_types[index]
1682 = create_index_type (convert (sizetype, gnu_min),
1683 convert (sizetype, gnu_max),
1684 build_range_type (gnu_ind_subtype,
1686 /* Update the maximum size of the array, in elements. */
1688 = size_binop (MULT_EXPR, gnu_max_size,
1689 size_binop (PLUS_EXPR, size_one_node,
1690 size_binop (MINUS_EXPR, gnu_base_max,
1693 TYPE_NAME (gnu_index_types[index])
1694 = create_concat_name (gnat_entity, field_name);
1697 for (index = 0; index < ndim; index++)
1699 = chainon (gnu_template_fields, gnu_temp_fields[index]);
1701 /* Install all the fields into the template. */
1702 finish_record_type (gnu_template_type, gnu_template_fields,
1704 TYPE_READONLY (gnu_template_type) = 1;
1706 /* Now make the array of arrays and update the pointer to the array
1707 in the fat pointer. Note that it is the first field. */
1709 tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1711 /* Get and validate any specified Component_Size, but if Packed,
1712 ignore it since the front end will have taken care of it. */
1714 = validate_size (Component_Size (gnat_entity), tem,
1716 (Is_Bit_Packed_Array (gnat_entity)
1717 ? TYPE_DECL : VAR_DECL),
1718 true, Has_Component_Size_Clause (gnat_entity));
1720 if (Has_Atomic_Components (gnat_entity))
1721 check_ok_for_atomic (tem, gnat_entity, true);
1723 /* If the component type is a RECORD_TYPE that has a self-referential
1724 size, use the maxium size. */
1725 if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1726 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1727 gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1729 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
1731 tem = make_type_from_size (tem, gnu_comp_size, false);
1732 tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1733 "C_PAD", false, definition, true);
1736 if (Has_Volatile_Components (gnat_entity))
1737 tem = build_qualified_type (tem,
1738 TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1740 /* If Component_Size is not already specified, annotate it with the
1741 size of the component. */
1742 if (Unknown_Component_Size (gnat_entity))
1743 Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1745 gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1746 size_binop (MULT_EXPR, gnu_max_size,
1747 TYPE_SIZE_UNIT (tem)));
1748 gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1749 size_binop (MULT_EXPR,
1750 convert (bitsizetype,
1754 for (index = ndim - 1; index >= 0; index--)
1756 tem = build_array_type (tem, gnu_index_types[index]);
1757 TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1759 /* If the type below this is a multi-array type, then this
1760 does not have aliased components. But we have to make
1761 them addressable if it must be passed by reference or
1762 if that is the default. */
1763 if ((TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
1764 && TYPE_MULTI_ARRAY_P (TREE_TYPE (tem)))
1765 || (!Has_Aliased_Components (gnat_entity)
1766 && !must_pass_by_ref (TREE_TYPE (tem))
1767 && !default_pass_by_ref (TREE_TYPE (tem))))
1768 TYPE_NONALIASED_COMPONENT (tem) = 1;
1771 /* If an alignment is specified, use it if valid. But ignore it for
1772 types that represent the unpacked base type for packed arrays. */
1773 if (No (Packed_Array_Type (gnat_entity))
1774 && Known_Alignment (gnat_entity))
1776 gcc_assert (Present (Alignment (gnat_entity)));
1778 = validate_alignment (Alignment (gnat_entity), gnat_entity,
1782 TYPE_CONVENTION_FORTRAN_P (tem)
1783 = (Convention (gnat_entity) == Convention_Fortran);
1784 TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
1786 /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
1787 corresponding fat pointer. */
1788 TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
1789 = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
1790 TYPE_MODE (gnu_type) = BLKmode;
1791 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
1792 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
1794 /* If the maximum size doesn't overflow, use it. */
1795 if (TREE_CODE (gnu_max_size) == INTEGER_CST
1796 && !TREE_OVERFLOW (gnu_max_size))
1798 = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
1799 if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
1800 && !TREE_OVERFLOW (gnu_max_size_unit))
1801 TYPE_SIZE_UNIT (tem)
1802 = size_binop (MIN_EXPR, gnu_max_size_unit,
1803 TYPE_SIZE_UNIT (tem));
1805 create_type_decl (create_concat_name (gnat_entity, "XUA"),
1806 tem, NULL, !Comes_From_Source (gnat_entity),
1807 debug_info_p, gnat_entity);
1809 /* Create a record type for the object and its template and
1810 set the template at a negative offset. */
1811 tem = build_unc_object_type (gnu_template_type, tem,
1812 create_concat_name (gnat_entity, "XUT"));
1813 DECL_FIELD_OFFSET (TYPE_FIELDS (tem))
1814 = size_binop (MINUS_EXPR, size_zero_node,
1815 byte_position (TREE_CHAIN (TYPE_FIELDS (tem))));
1816 DECL_FIELD_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem))) = size_zero_node;
1817 DECL_FIELD_BIT_OFFSET (TREE_CHAIN (TYPE_FIELDS (tem)))
1818 = bitsize_zero_node;
1819 SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
1820 TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
1822 /* Give the thin pointer type a name. */
1823 create_type_decl (create_concat_name (gnat_entity, "XUX"),
1824 build_pointer_type (tem), NULL,
1825 !Comes_From_Source (gnat_entity), debug_info_p,
1830 case E_String_Subtype:
1831 case E_Array_Subtype:
1833 /* This is the actual data type for array variables. Multidimensional
1834 arrays are implemented in the gnu tree as arrays of arrays. Note
1835 that for the moment arrays which have sparse enumeration subtypes as
1836 index components create sparse arrays, which is obviously space
1837 inefficient but so much easier to code for now.
1839 Also note that the subtype never refers to the unconstrained
1840 array type, which is somewhat at variance with Ada semantics.
1842 First check to see if this is simply a renaming of the array
1843 type. If so, the result is the array type. */
1845 gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1846 if (!Is_Constrained (gnat_entity))
1851 int array_dim = Number_Dimensions (gnat_entity);
1853 = ((Convention (gnat_entity) == Convention_Fortran)
1854 ? array_dim - 1 : 0);
1856 = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
1857 Entity_Id gnat_ind_subtype;
1858 Entity_Id gnat_ind_base_subtype;
1859 tree gnu_base_type = gnu_type;
1860 tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
1861 tree gnu_comp_size = NULL_TREE;
1862 tree gnu_max_size = size_one_node;
1863 tree gnu_max_size_unit;
1864 bool need_index_type_struct = false;
1865 bool max_overflow = false;
1867 /* First create the gnu types for each index. Create types for
1868 debugging information to point to the index types if the
1869 are not integer types, have variable bounds, or are
1870 wider than sizetype. */
1872 for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
1873 gnat_ind_base_subtype
1874 = First_Index (Implementation_Base_Type (gnat_entity));
1875 index < array_dim && index >= 0;
1877 gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1878 gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1880 tree gnu_index_subtype
1881 = get_unpadded_type (Etype (gnat_ind_subtype));
1883 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
1885 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
1886 tree gnu_base_subtype
1887 = get_unpadded_type (Etype (gnat_ind_base_subtype));
1889 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1891 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1892 tree gnu_base_type = get_base_type (gnu_base_subtype);
1893 tree gnu_base_base_min
1894 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
1895 tree gnu_base_base_max
1896 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
1900 /* If the minimum and maximum values both overflow in
1901 SIZETYPE, but the difference in the original type
1902 does not overflow in SIZETYPE, ignore the overflow
1904 if ((TYPE_PRECISION (gnu_index_subtype)
1905 > TYPE_PRECISION (sizetype)
1906 || TYPE_UNSIGNED (gnu_index_subtype)
1907 != TYPE_UNSIGNED (sizetype))
1908 && TREE_CODE (gnu_min) == INTEGER_CST
1909 && TREE_CODE (gnu_max) == INTEGER_CST
1910 && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
1912 (fold (build2 (MINUS_EXPR, gnu_index_subtype,
1913 TYPE_MAX_VALUE (gnu_index_subtype),
1914 TYPE_MIN_VALUE (gnu_index_subtype))))))
1915 TREE_OVERFLOW (gnu_min) = TREE_OVERFLOW (gnu_max) = 0;
1917 /* Similarly, if the range is null, use bounds of 1..0 for
1918 the sizetype bounds. */
1919 else if ((TYPE_PRECISION (gnu_index_subtype)
1920 > TYPE_PRECISION (sizetype)
1921 || TYPE_UNSIGNED (gnu_index_subtype)
1922 != TYPE_UNSIGNED (sizetype))
1923 && TREE_CODE (gnu_min) == INTEGER_CST
1924 && TREE_CODE (gnu_max) == INTEGER_CST
1925 && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
1926 && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
1927 TYPE_MIN_VALUE (gnu_index_subtype)))
1928 gnu_min = size_one_node, gnu_max = size_zero_node;
1930 /* Now compute the size of this bound. We need to provide
1931 GCC with an upper bound to use but have to deal with the
1932 "superflat" case. There are three ways to do this. If we
1933 can prove that the array can never be superflat, we can
1934 just use the high bound of the index subtype. If we can
1935 prove that the low bound minus one can't overflow, we
1936 can do this as MAX (hb, lb - 1). Otherwise, we have to use
1937 the expression hb >= lb ? hb : lb - 1. */
1938 gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
1940 /* See if the base array type is already flat. If it is, we
1941 are probably compiling an ACVC test, but it will cause the
1942 code below to malfunction if we don't handle it specially. */
1943 if (TREE_CODE (gnu_base_min) == INTEGER_CST
1944 && TREE_CODE (gnu_base_max) == INTEGER_CST
1945 && !TREE_OVERFLOW (gnu_base_min)
1946 && !TREE_OVERFLOW (gnu_base_max)
1947 && tree_int_cst_lt (gnu_base_max, gnu_base_min))
1948 gnu_high = size_zero_node, gnu_min = size_one_node;
1950 /* If gnu_high is now an integer which overflowed, the array
1951 cannot be superflat. */
1952 else if (TREE_CODE (gnu_high) == INTEGER_CST
1953 && TREE_OVERFLOW (gnu_high))
1955 else if (TYPE_UNSIGNED (gnu_base_subtype)
1956 || TREE_CODE (gnu_high) == INTEGER_CST)
1957 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
1961 (sizetype, build_binary_op (GE_EXPR, integer_type_node,
1965 gnu_index_type[index]
1966 = create_index_type (gnu_min, gnu_high, gnu_index_subtype);
1968 /* Also compute the maximum size of the array. Here we
1969 see if any constraint on the index type of the base type
1970 can be used in the case of self-referential bound on
1971 the index type of the subtype. We look for a non-"infinite"
1972 and non-self-referential bound from any type involved and
1973 handle each bound separately. */
1975 if ((TREE_CODE (gnu_min) == INTEGER_CST
1976 && !TREE_OVERFLOW (gnu_min)
1977 && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
1978 || !CONTAINS_PLACEHOLDER_P (gnu_min)
1979 || !(TREE_CODE (gnu_base_min) == INTEGER_CST
1980 && !TREE_OVERFLOW (gnu_base_min)))
1981 gnu_base_min = gnu_min;
1983 if ((TREE_CODE (gnu_max) == INTEGER_CST
1984 && !TREE_OVERFLOW (gnu_max)
1985 && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
1986 || !CONTAINS_PLACEHOLDER_P (gnu_max)
1987 || !(TREE_CODE (gnu_base_max) == INTEGER_CST
1988 && !TREE_OVERFLOW (gnu_base_max)))
1989 gnu_base_max = gnu_max;
1991 if ((TREE_CODE (gnu_base_min) == INTEGER_CST
1992 && TREE_OVERFLOW (gnu_base_min))
1993 || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
1994 || (TREE_CODE (gnu_base_max) == INTEGER_CST
1995 && TREE_OVERFLOW (gnu_base_max))
1996 || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
1997 max_overflow = true;
1999 gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2000 gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2003 = size_binop (MAX_EXPR,
2004 size_binop (PLUS_EXPR, size_one_node,
2005 size_binop (MINUS_EXPR, gnu_base_max,
2009 if (TREE_CODE (gnu_this_max) == INTEGER_CST
2010 && TREE_OVERFLOW (gnu_this_max))
2011 max_overflow = true;
2014 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2016 if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2017 || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2019 || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2020 || (TREE_TYPE (gnu_index_subtype)
2021 && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2023 || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2024 || (TYPE_PRECISION (gnu_index_subtype)
2025 > TYPE_PRECISION (sizetype)))
2026 need_index_type_struct = true;
2029 /* Then flatten: create the array of arrays. */
2031 gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2033 /* One of the above calls might have caused us to be elaborated,
2034 so don't blow up if so. */
2035 if (present_gnu_tree (gnat_entity))
2037 maybe_present = true;
2041 /* Get and validate any specified Component_Size, but if Packed,
2042 ignore it since the front end will have taken care of it. */
2044 = validate_size (Component_Size (gnat_entity), gnu_type,
2046 (Is_Bit_Packed_Array (gnat_entity)
2047 ? TYPE_DECL : VAR_DECL),
2048 true, Has_Component_Size_Clause (gnat_entity));
2050 /* If the component type is a RECORD_TYPE that has a self-referential
2051 size, use the maxium size. */
2052 if (!gnu_comp_size && TREE_CODE (gnu_type) == RECORD_TYPE
2053 && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2054 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2056 if (!Is_Bit_Packed_Array (gnat_entity) && gnu_comp_size)
2058 gnu_type = make_type_from_size (gnu_type, gnu_comp_size, false);
2059 gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2060 gnat_entity, "C_PAD", false,
2064 if (Has_Volatile_Components (Base_Type (gnat_entity)))
2065 gnu_type = build_qualified_type (gnu_type,
2066 (TYPE_QUALS (gnu_type)
2067 | TYPE_QUAL_VOLATILE));
2069 gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2070 TYPE_SIZE_UNIT (gnu_type));
2071 gnu_max_size = size_binop (MULT_EXPR,
2072 convert (bitsizetype, gnu_max_size),
2073 TYPE_SIZE (gnu_type));
2075 for (index = array_dim - 1; index >= 0; index --)
2077 gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2078 TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2080 /* If the type below this is a multi-array type, then this
2081 does not have aliased components. But we have to make
2082 them addressable if it must be passed by reference or
2083 if that is the default. */
2084 if ((TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
2085 && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
2086 || (!Has_Aliased_Components (gnat_entity)
2087 && !must_pass_by_ref (TREE_TYPE (gnu_type))
2088 && !default_pass_by_ref (TREE_TYPE (gnu_type))))
2089 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2092 /* If we are at file level and this is a multi-dimensional array, we
2093 need to make a variable corresponding to the stride of the
2094 inner dimensions. */
2095 if (global_bindings_p () && array_dim > 1)
2097 tree gnu_str_name = get_identifier ("ST");
2100 for (gnu_arr_type = TREE_TYPE (gnu_type);
2101 TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2102 gnu_arr_type = TREE_TYPE (gnu_arr_type),
2103 gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2105 tree eltype = TREE_TYPE (gnu_arr_type);
2107 TYPE_SIZE (gnu_arr_type)
2108 = elaborate_expression_1 (gnat_entity, gnat_entity,
2109 TYPE_SIZE (gnu_arr_type),
2110 gnu_str_name, definition, 0);
2112 /* ??? For now, store the size as a multiple of the
2113 alignment of the element type in bytes so that we
2114 can see the alignment from the tree. */
2115 TYPE_SIZE_UNIT (gnu_arr_type)
2117 (MULT_EXPR, sizetype,
2118 elaborate_expression_1
2119 (gnat_entity, gnat_entity,
2120 build_binary_op (EXACT_DIV_EXPR, sizetype,
2121 TYPE_SIZE_UNIT (gnu_arr_type),
2122 size_int (TYPE_ALIGN (eltype)
2124 concat_id_with_name (gnu_str_name, "A_U"),
2126 size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2130 /* If we need to write out a record type giving the names of
2131 the bounds, do it now. */
2132 if (need_index_type_struct && debug_info_p)
2134 tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2135 tree gnu_field_list = NULL_TREE;
2138 TYPE_NAME (gnu_bound_rec_type)
2139 = create_concat_name (gnat_entity, "XA");
2141 for (index = array_dim - 1; index >= 0; index--)
2144 = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2146 if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2147 gnu_type_name = DECL_NAME (gnu_type_name);
2149 gnu_field = create_field_decl (gnu_type_name,
2152 0, NULL_TREE, NULL_TREE, 0);
2153 TREE_CHAIN (gnu_field) = gnu_field_list;
2154 gnu_field_list = gnu_field;
2157 finish_record_type (gnu_bound_rec_type, gnu_field_list,
2161 TYPE_CONVENTION_FORTRAN_P (gnu_type)
2162 = (Convention (gnat_entity) == Convention_Fortran);
2163 TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2164 = Is_Packed_Array_Type (gnat_entity);
2166 /* If our size depends on a placeholder and the maximum size doesn't
2167 overflow, use it. */
2168 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2169 && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2170 && TREE_OVERFLOW (gnu_max_size))
2171 && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2172 && TREE_OVERFLOW (gnu_max_size_unit))
2175 TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2176 TYPE_SIZE (gnu_type));
2177 TYPE_SIZE_UNIT (gnu_type)
2178 = size_binop (MIN_EXPR, gnu_max_size_unit,
2179 TYPE_SIZE_UNIT (gnu_type));
2182 /* Set our alias set to that of our base type. This gives all
2183 array subtypes the same alias set. */
2184 copy_alias_set (gnu_type, gnu_base_type);
2187 /* If this is a packed type, make this type the same as the packed
2188 array type, but do some adjusting in the type first. */
2190 if (Present (Packed_Array_Type (gnat_entity)))
2192 Entity_Id gnat_index;
2193 tree gnu_inner_type;
2195 /* First finish the type we had been making so that we output
2196 debugging information for it */
2198 = build_qualified_type (gnu_type,
2199 (TYPE_QUALS (gnu_type)
2200 | (TYPE_QUAL_VOLATILE
2201 * Treat_As_Volatile (gnat_entity))));
2202 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2203 !Comes_From_Source (gnat_entity),
2204 debug_info_p, gnat_entity);
2205 if (!Comes_From_Source (gnat_entity))
2206 DECL_ARTIFICIAL (gnu_decl) = 1;
2208 /* Save it as our equivalent in case the call below elaborates
2210 save_gnu_tree (gnat_entity, gnu_decl, false);
2212 gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2214 this_made_decl = true;
2215 gnu_inner_type = gnu_type = TREE_TYPE (gnu_decl);
2216 save_gnu_tree (gnat_entity, NULL_TREE, false);
2218 while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2219 && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2220 || TYPE_IS_PADDING_P (gnu_inner_type)))
2221 gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2223 /* We need to point the type we just made to our index type so
2224 the actual bounds can be put into a template. */
2226 if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2227 && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2228 || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2229 && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2231 if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2233 /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2234 If it is, we need to make another type. */
2235 if (TYPE_MODULAR_P (gnu_inner_type))
2239 gnu_subtype = make_node (INTEGER_TYPE);
2241 TREE_TYPE (gnu_subtype) = gnu_inner_type;
2242 TYPE_MIN_VALUE (gnu_subtype)
2243 = TYPE_MIN_VALUE (gnu_inner_type);
2244 TYPE_MAX_VALUE (gnu_subtype)
2245 = TYPE_MAX_VALUE (gnu_inner_type);
2246 TYPE_PRECISION (gnu_subtype)
2247 = TYPE_PRECISION (gnu_inner_type);
2248 TYPE_UNSIGNED (gnu_subtype)
2249 = TYPE_UNSIGNED (gnu_inner_type);
2250 TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2251 layout_type (gnu_subtype);
2253 gnu_inner_type = gnu_subtype;
2256 TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2259 SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2261 for (gnat_index = First_Index (gnat_entity);
2262 Present (gnat_index); gnat_index = Next_Index (gnat_index))
2263 SET_TYPE_ACTUAL_BOUNDS
2265 tree_cons (NULL_TREE,
2266 get_unpadded_type (Etype (gnat_index)),
2267 TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2269 if (Convention (gnat_entity) != Convention_Fortran)
2270 SET_TYPE_ACTUAL_BOUNDS
2272 nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2274 if (TREE_CODE (gnu_type) == RECORD_TYPE
2275 && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2276 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2280 /* Abort if packed array with no packed array type field set. */
2282 gcc_assert (!Is_Packed (gnat_entity));
2286 case E_String_Literal_Subtype:
2287 /* Create the type for a string literal. */
2289 Entity_Id gnat_full_type
2290 = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2291 && Present (Full_View (Etype (gnat_entity)))
2292 ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2293 tree gnu_string_type = get_unpadded_type (gnat_full_type);
2294 tree gnu_string_array_type
2295 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2296 tree gnu_string_index_type
2297 = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2298 (TYPE_DOMAIN (gnu_string_array_type))));
2299 tree gnu_lower_bound
2300 = convert (gnu_string_index_type,
2301 gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2302 int length = UI_To_Int (String_Literal_Length (gnat_entity));
2303 tree gnu_length = ssize_int (length - 1);
2304 tree gnu_upper_bound
2305 = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2307 convert (gnu_string_index_type, gnu_length));
2309 = build_range_type (gnu_string_index_type,
2310 gnu_lower_bound, gnu_upper_bound);
2312 = create_index_type (convert (sizetype,
2313 TYPE_MIN_VALUE (gnu_range_type)),
2315 TYPE_MAX_VALUE (gnu_range_type)),
2319 = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2321 copy_alias_set (gnu_type, gnu_string_type);
2325 /* Record Types and Subtypes
2327 The following fields are defined on record types:
2329 Has_Discriminants True if the record has discriminants
2330 First_Discriminant Points to head of list of discriminants
2331 First_Entity Points to head of list of fields
2332 Is_Tagged_Type True if the record is tagged
2334 Implementation of Ada records and discriminated records:
2336 A record type definition is transformed into the equivalent of a C
2337 struct definition. The fields that are the discriminants which are
2338 found in the Full_Type_Declaration node and the elements of the
2339 Component_List found in the Record_Type_Definition node. The
2340 Component_List can be a recursive structure since each Variant of
2341 the Variant_Part of the Component_List has a Component_List.
2343 Processing of a record type definition comprises starting the list of
2344 field declarations here from the discriminants and the calling the
2345 function components_to_record to add the rest of the fields from the
2346 component list and return the gnu type node. The function
2347 components_to_record will call itself recursively as it traverses
2351 if (Has_Complex_Representation (gnat_entity))
2354 = build_complex_type
2356 (Etype (Defining_Entity
2357 (First (Component_Items
2360 (Declaration_Node (gnat_entity)))))))));
2366 Node_Id full_definition = Declaration_Node (gnat_entity);
2367 Node_Id record_definition = Type_Definition (full_definition);
2368 Entity_Id gnat_field;
2370 tree gnu_field_list = NULL_TREE;
2371 tree gnu_get_parent;
2372 int packed = (Is_Packed (gnat_entity) ? 1
2373 : (Component_Alignment (gnat_entity)
2374 == Calign_Storage_Unit) ? -1
2376 bool has_rep = Has_Specified_Layout (gnat_entity);
2377 bool all_rep = has_rep;
2379 = (Is_Tagged_Type (gnat_entity)
2380 && Nkind (record_definition) == N_Derived_Type_Definition);
2382 /* See if all fields have a rep clause. Stop when we find one
2384 for (gnat_field = First_Entity (gnat_entity);
2385 Present (gnat_field) && all_rep;
2386 gnat_field = Next_Entity (gnat_field))
2387 if ((Ekind (gnat_field) == E_Component
2388 || Ekind (gnat_field) == E_Discriminant)
2389 && No (Component_Clause (gnat_field)))
2392 /* If this is a record extension, go a level further to find the
2393 record definition. Also, verify we have a Parent_Subtype. */
2396 if (!type_annotate_only
2397 || Present (Record_Extension_Part (record_definition)))
2398 record_definition = Record_Extension_Part (record_definition);
2400 gcc_assert (type_annotate_only
2401 || Present (Parent_Subtype (gnat_entity)));
2404 /* Make a node for the record. If we are not defining the record,
2405 suppress expanding incomplete types. */
2406 gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2407 TYPE_NAME (gnu_type) = gnu_entity_id;
2408 /* ??? We should have create_type_decl like in the E_Record_Subtype
2409 case below. Unfortunately this would cause GNU_TYPE to be marked
2410 as visited, thus precluding the subtrees of the type that will be
2411 built below from being marked as visited when the real TYPE_DECL
2412 is eventually created. A solution could be to devise a special
2413 version of the function under the name create_type_stub_decl. */
2414 TYPE_STUB_DECL (gnu_type)
2415 = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
2416 TYPE_ALIGN (gnu_type) = 0;
2417 TYPE_PACKED (gnu_type) = packed || has_rep;
2420 defer_incomplete_level++, this_deferred = true;
2422 /* If both a size and rep clause was specified, put the size in
2423 the record type now so that it can get the proper mode. */
2424 if (has_rep && Known_Esize (gnat_entity))
2425 TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2427 /* Always set the alignment here so that it can be used to
2428 set the mode, if it is making the alignment stricter. If
2429 it is invalid, it will be checked again below. If this is to
2430 be Atomic, choose a default alignment of a word unless we know
2431 the size and it's smaller. */
2432 if (Known_Alignment (gnat_entity))
2433 TYPE_ALIGN (gnu_type)
2434 = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2435 else if (Is_Atomic (gnat_entity))
2436 TYPE_ALIGN (gnu_type)
2437 = (esize >= BITS_PER_WORD ? BITS_PER_WORD
2438 : 1 << (floor_log2 (esize - 1) + 1));
2440 /* If we have a Parent_Subtype, make a field for the parent. If
2441 this record has rep clauses, force the position to zero. */
2442 if (Present (Parent_Subtype (gnat_entity)))
2444 Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2447 /* A major complexity here is that the parent subtype will
2448 reference our discriminants in its Discriminant_Constraint
2449 list. But those must reference the parent component of this
2450 record which is of the parent subtype we have not built yet!
2451 To break the circle we first build a dummy COMPONENT_REF which
2452 represents the "get to the parent" operation and initialize
2453 each of those discriminants to a COMPONENT_REF of the above
2454 dummy parent referencing the corresponding discriminant of the
2455 base type of the parent subtype. */
2456 gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2457 build0 (PLACEHOLDER_EXPR, gnu_type),
2458 build_decl (FIELD_DECL, NULL_TREE,
2462 if (Has_Discriminants (gnat_entity))
2463 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2464 Present (gnat_field);
2465 gnat_field = Next_Stored_Discriminant (gnat_field))
2466 if (Present (Corresponding_Discriminant (gnat_field)))
2469 build3 (COMPONENT_REF,
2470 get_unpadded_type (Etype (gnat_field)),
2472 gnat_to_gnu_field_decl (Corresponding_Discriminant
2477 /* Then we build the parent subtype. */
2478 gnu_parent = gnat_to_gnu_type (gnat_parent);
2480 /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2481 initially built. The discriminants must reference the fields
2482 of the parent subtype and not those of its base type for the
2483 placeholder machinery to properly work. */
2484 if (Has_Discriminants (gnat_entity))
2485 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2486 Present (gnat_field);
2487 gnat_field = Next_Stored_Discriminant (gnat_field))
2488 if (Present (Corresponding_Discriminant (gnat_field)))
2490 Entity_Id field = Empty;
2491 for (field = First_Stored_Discriminant (gnat_parent);
2493 field = Next_Stored_Discriminant (field))
2494 if (same_discriminant_p (gnat_field, field))
2496 gcc_assert (Present (field));
2497 TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2498 = gnat_to_gnu_field_decl (field);
2501 /* The "get to the parent" COMPONENT_REF must be given its
2503 TREE_TYPE (gnu_get_parent) = gnu_parent;
2505 /* ...and reference the _parent field of this record. */
2507 = create_field_decl (get_identifier
2508 (Get_Name_String (Name_uParent)),
2509 gnu_parent, gnu_type, 0,
2510 has_rep ? TYPE_SIZE (gnu_parent) : 0,
2511 has_rep ? bitsize_zero_node : 0, 1);
2512 DECL_INTERNAL_P (gnu_field_list) = 1;
2513 TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2516 /* Make the fields for the discriminants and put them into the record
2517 unless it's an Unchecked_Union. */
2518 if (Has_Discriminants (gnat_entity))
2519 for (gnat_field = First_Stored_Discriminant (gnat_entity);
2520 Present (gnat_field);
2521 gnat_field = Next_Stored_Discriminant (gnat_field))
2523 /* If this is a record extension and this discriminant
2524 is the renaming of another discriminant, we've already
2525 handled the discriminant above. */
2526 if (Present (Parent_Subtype (gnat_entity))
2527 && Present (Corresponding_Discriminant (gnat_field)))
2531 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2533 /* Make an expression using a PLACEHOLDER_EXPR from the
2534 FIELD_DECL node just created and link that with the
2535 corresponding GNAT defining identifier. Then add to the
2537 save_gnu_tree (gnat_field,
2538 build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2539 build0 (PLACEHOLDER_EXPR,
2540 DECL_CONTEXT (gnu_field)),
2541 gnu_field, NULL_TREE),
2544 if (!Is_Unchecked_Union (gnat_entity))
2546 TREE_CHAIN (gnu_field) = gnu_field_list;
2547 gnu_field_list = gnu_field;
2551 /* Put the discriminants into the record (backwards), so we can
2552 know the appropriate discriminant to use for the names of the
2554 TYPE_FIELDS (gnu_type) = gnu_field_list;
2556 /* Add the listed fields into the record and finish up. */
2557 components_to_record (gnu_type, Component_List (record_definition),
2558 gnu_field_list, packed, definition, NULL,
2559 false, all_rep, this_deferred,
2560 Is_Unchecked_Union (gnat_entity));
2564 debug_deferred = true;
2565 defer_debug_level++;
2567 defer_debug_incomplete_list
2568 = tree_cons (NULL_TREE, gnu_type,
2569 defer_debug_incomplete_list);
2572 /* We used to remove the associations of the discriminants and
2573 _Parent for validity checking, but we may need them if there's
2574 Freeze_Node for a subtype used in this record. */
2576 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2577 TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2579 /* If it is a tagged record force the type to BLKmode to insure
2580 that these objects will always be placed in memory. Do the
2581 same thing for limited record types. */
2582 if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2583 TYPE_MODE (gnu_type) = BLKmode;
2585 /* If this is a derived type, we must make the alias set of this type
2586 the same as that of the type we are derived from. We assume here
2587 that the other type is already frozen. */
2588 if (Etype (gnat_entity) != gnat_entity
2589 && !(Is_Private_Type (Etype (gnat_entity))
2590 && Full_View (Etype (gnat_entity)) == gnat_entity))
2591 copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2593 /* Fill in locations of fields. */
2594 annotate_rep (gnat_entity, gnu_type);
2596 /* If there are any entities in the chain corresponding to
2597 components that we did not elaborate, ensure we elaborate their
2598 types if they are Itypes. */
2599 for (gnat_temp = First_Entity (gnat_entity);
2600 Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2601 if ((Ekind (gnat_temp) == E_Component
2602 || Ekind (gnat_temp) == E_Discriminant)
2603 && Is_Itype (Etype (gnat_temp))
2604 && !present_gnu_tree (gnat_temp))
2605 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2609 case E_Class_Wide_Subtype:
2610 /* If an equivalent type is present, that is what we should use.
2611 Otherwise, fall through to handle this like a record subtype
2612 since it may have constraints. */
2614 if (Present (Equivalent_Type (gnat_entity)))
2616 gnu_decl = gnat_to_gnu_entity (Equivalent_Type (gnat_entity),
2618 maybe_present = true;
2622 /* ... fall through ... */
2624 case E_Record_Subtype:
2626 /* If Cloned_Subtype is Present it means this record subtype has
2627 identical layout to that type or subtype and we should use
2628 that GCC type for this one. The front end guarantees that
2629 the component list is shared. */
2630 if (Present (Cloned_Subtype (gnat_entity)))
2632 gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2634 maybe_present = true;
2637 /* Otherwise, first ensure the base type is elaborated. Then, if we are
2638 changing the type, make a new type with each field having the
2639 type of the field in the new subtype but having the position
2640 computed by transforming every discriminant reference according
2641 to the constraints. We don't see any difference between
2642 private and nonprivate type here since derivations from types should
2643 have been deferred until the completion of the private type. */
2646 Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2651 defer_incomplete_level++, this_deferred = true;
2653 /* Get the base type initially for its alignment and sizes. But
2654 if it is a padded type, we do all the other work with the
2656 gnu_type = gnu_orig_type = gnu_base_type
2657 = gnat_to_gnu_type (gnat_base_type);
2659 if (TREE_CODE (gnu_type) == RECORD_TYPE
2660 && TYPE_IS_PADDING_P (gnu_type))
2661 gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_type));
2663 if (present_gnu_tree (gnat_entity))
2665 maybe_present = true;
2669 /* When the type has discriminants, and these discriminants
2670 affect the shape of what it built, factor them in.
2672 If we are making a subtype of an Unchecked_Union (must be an
2673 Itype), just return the type.
2675 We can't just use Is_Constrained because private subtypes without
2676 discriminants of full types with discriminants with default
2677 expressions are Is_Constrained but aren't constrained! */
2679 if (IN (Ekind (gnat_base_type), Record_Kind)
2680 && !Is_For_Access_Subtype (gnat_entity)
2681 && !Is_Unchecked_Union (gnat_base_type)
2682 && Is_Constrained (gnat_entity)
2683 && Stored_Constraint (gnat_entity) != No_Elist
2684 && Present (Discriminant_Constraint (gnat_entity)))
2686 Entity_Id gnat_field;
2687 tree gnu_field_list = 0;
2689 = compute_field_positions (gnu_orig_type, NULL_TREE,
2690 size_zero_node, bitsize_zero_node,
2693 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2697 gnu_type = make_node (RECORD_TYPE);
2698 TYPE_NAME (gnu_type) = gnu_entity_id;
2699 TYPE_STUB_DECL (gnu_type)
2700 = create_type_decl (NULL_TREE, gnu_type, NULL, false, false,
2702 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2704 for (gnat_field = First_Entity (gnat_entity);
2705 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2706 if ((Ekind (gnat_field) == E_Component
2707 || Ekind (gnat_field) == E_Discriminant)
2708 && (Underlying_Type (Scope (Original_Record_Component
2711 && (No (Corresponding_Discriminant (gnat_field))
2712 || !Is_Tagged_Type (gnat_base_type)))
2715 = gnat_to_gnu_field_decl (Original_Record_Component
2718 = TREE_VALUE (purpose_member (gnu_old_field,
2720 tree gnu_pos = TREE_PURPOSE (gnu_offset);
2721 tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
2723 = gnat_to_gnu_type (Etype (gnat_field));
2724 tree gnu_size = TYPE_SIZE (gnu_field_type);
2725 tree gnu_new_pos = 0;
2726 unsigned int offset_align
2727 = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
2731 /* If there was a component clause, the field types must be
2732 the same for the type and subtype, so copy the data from
2733 the old field to avoid recomputation here. Also if the
2734 field is justified modular and the optimization in
2735 gnat_to_gnu_field was applied. */
2736 if (Present (Component_Clause
2737 (Original_Record_Component (gnat_field)))
2738 || (TREE_CODE (gnu_field_type) == RECORD_TYPE
2739 && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
2740 && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
2741 == TREE_TYPE (gnu_old_field)))
2743 gnu_size = DECL_SIZE (gnu_old_field);
2744 gnu_field_type = TREE_TYPE (gnu_old_field);
2747 /* If this was a bitfield, get the size from the old field.
2748 Also ensure the type can be placed into a bitfield. */
2749 else if (DECL_BIT_FIELD (gnu_old_field))
2751 gnu_size = DECL_SIZE (gnu_old_field);
2752 if (TYPE_MODE (gnu_field_type) == BLKmode
2753 && TREE_CODE (gnu_field_type) == RECORD_TYPE
2754 && host_integerp (TYPE_SIZE (gnu_field_type), 1))
2755 gnu_field_type = make_packable_type (gnu_field_type);
2758 if (CONTAINS_PLACEHOLDER_P (gnu_pos))
2759 for (gnu_temp = gnu_subst_list;
2760 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2761 gnu_pos = substitute_in_expr (gnu_pos,
2762 TREE_PURPOSE (gnu_temp),
2763 TREE_VALUE (gnu_temp));
2765 /* If the size is now a constant, we can set it as the
2766 size of the field when we make it. Otherwise, we need
2767 to deal with it specially. */
2768 if (TREE_CONSTANT (gnu_pos))
2769 gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
2773 (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
2774 0, gnu_size, gnu_new_pos,
2775 !DECL_NONADDRESSABLE_P (gnu_old_field));
2777 if (!TREE_CONSTANT (gnu_pos))
2779 normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
2780 DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
2781 DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
2782 SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
2783 DECL_SIZE (gnu_field) = gnu_size;
2784 DECL_SIZE_UNIT (gnu_field)
2785 = convert (sizetype,
2786 size_binop (CEIL_DIV_EXPR, gnu_size,
2787 bitsize_unit_node));
2788 layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
2791 DECL_INTERNAL_P (gnu_field)
2792 = DECL_INTERNAL_P (gnu_old_field);
2793 SET_DECL_ORIGINAL_FIELD
2794 (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
2795 ? DECL_ORIGINAL_FIELD (gnu_old_field)
2797 DECL_DISCRIMINANT_NUMBER (gnu_field)
2798 = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
2799 TREE_THIS_VOLATILE (gnu_field)
2800 = TREE_THIS_VOLATILE (gnu_old_field);
2801 TREE_CHAIN (gnu_field) = gnu_field_list;
2802 gnu_field_list = gnu_field;
2803 save_gnu_tree (gnat_field, gnu_field, false);
2806 /* Now go through the entities again looking for Itypes that
2807 we have not elaborated but should (e.g., Etypes of fields
2808 that have Original_Components). */
2809 for (gnat_field = First_Entity (gnat_entity);
2810 Present (gnat_field); gnat_field = Next_Entity (gnat_field))
2811 if ((Ekind (gnat_field) == E_Discriminant
2812 || Ekind (gnat_field) == E_Component)
2813 && !present_gnu_tree (Etype (gnat_field)))
2814 gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
2816 finish_record_type (gnu_type, nreverse (gnu_field_list),
2819 /* Now set the size, alignment and alias set of the new type to
2820 match that of the old one, doing any substitutions, as
2822 TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
2823 TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
2824 TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
2825 SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
2826 copy_alias_set (gnu_type, gnu_base_type);
2828 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2829 for (gnu_temp = gnu_subst_list;
2830 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2831 TYPE_SIZE (gnu_type)
2832 = substitute_in_expr (TYPE_SIZE (gnu_type),
2833 TREE_PURPOSE (gnu_temp),
2834 TREE_VALUE (gnu_temp));
2836 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
2837 for (gnu_temp = gnu_subst_list;
2838 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2839 TYPE_SIZE_UNIT (gnu_type)
2840 = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
2841 TREE_PURPOSE (gnu_temp),
2842 TREE_VALUE (gnu_temp));
2844 if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
2845 for (gnu_temp = gnu_subst_list;
2846 gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
2848 (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
2849 TREE_PURPOSE (gnu_temp),
2850 TREE_VALUE (gnu_temp)));
2852 /* Recompute the mode of this record type now that we know its
2854 compute_record_mode (gnu_type);
2856 /* Fill in locations of fields. */
2857 annotate_rep (gnat_entity, gnu_type);
2860 /* If we've made a new type, record it and make an XVS type to show
2861 what this is a subtype of. Some debuggers require the XVS
2862 type to be output first, so do it in that order. */
2863 if (gnu_type != gnu_orig_type)
2867 tree gnu_subtype_marker = make_node (RECORD_TYPE);
2868 tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
2870 if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
2871 gnu_orig_name = DECL_NAME (gnu_orig_name);
2873 TYPE_NAME (gnu_subtype_marker)
2874 = create_concat_name (gnat_entity, "XVS");
2875 finish_record_type (gnu_subtype_marker,
2876 create_field_decl (gnu_orig_name,
2884 TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2885 TYPE_NAME (gnu_type) = gnu_entity_id;
2886 TYPE_STUB_DECL (gnu_type)
2887 = create_type_decl (TYPE_NAME (gnu_type), gnu_type,
2888 NULL, true, debug_info_p, gnat_entity);
2891 /* Otherwise, go down all the components in the new type and
2892 make them equivalent to those in the base type. */
2894 for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
2895 gnat_temp = Next_Entity (gnat_temp))
2896 if ((Ekind (gnat_temp) == E_Discriminant
2897 && !Is_Unchecked_Union (gnat_base_type))
2898 || Ekind (gnat_temp) == E_Component)
2899 save_gnu_tree (gnat_temp,
2900 gnat_to_gnu_field_decl
2901 (Original_Record_Component (gnat_temp)), false);
2905 case E_Access_Subprogram_Type:
2906 case E_Anonymous_Access_Subprogram_Type:
2907 /* If we are not defining this entity, and we have incomplete
2908 entities being processed above us, make a dummy type and
2909 fill it in later. */
2910 if (!definition && defer_incomplete_level != 0)
2912 struct incomplete *p
2913 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
2916 = build_pointer_type
2917 (make_dummy_type (Directly_Designated_Type (gnat_entity)));
2918 gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2919 !Comes_From_Source (gnat_entity),
2920 debug_info_p, gnat_entity);
2921 save_gnu_tree (gnat_entity, gnu_decl, false);
2922 this_made_decl = saved = true;
2924 p->old_type = TREE_TYPE (gnu_type);
2925 p->full_type = Directly_Designated_Type (gnat_entity);
2926 p->next = defer_incomplete_list;
2927 defer_incomplete_list = p;
2931 /* ... fall through ... */
2933 case E_Allocator_Type:
2935 case E_Access_Attribute_Type:
2936 case E_Anonymous_Access_Type:
2937 case E_General_Access_Type:
2939 Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
2940 /* Get the "full view" of this entity. If this is an incomplete
2941 entity from a limited with, treat its non-limited view as the
2942 full view. Otherwise, if this is an incomplete or private
2943 type, use the full view. */
2944 Entity_Id gnat_desig_full
2945 = (IN (Ekind (gnat_desig_type), Incomplete_Kind)
2946 && From_With_Type (gnat_desig_type))
2947 ? Non_Limited_View (gnat_desig_type)
2948 : IN (Ekind (gnat_desig_type), Incomplete_Or_Private_Kind)
2949 ? Full_View (gnat_desig_type)
2951 /* We want to know if we'll be seeing the freeze node for any
2952 incomplete type we may be pointing to. */
2954 = (Present (gnat_desig_full)
2955 ? In_Extended_Main_Code_Unit (gnat_desig_full)
2956 : In_Extended_Main_Code_Unit (gnat_desig_type));
2957 bool got_fat_p = false;
2958 bool made_dummy = false;
2959 tree gnu_desig_type = NULL_TREE;
2960 enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
2962 if (!targetm.valid_pointer_mode (p_mode))
2965 if (No (gnat_desig_full)
2966 && (Ekind (gnat_desig_type) == E_Class_Wide_Type
2967 || (Ekind (gnat_desig_type) == E_Class_Wide_Subtype
2968 && Present (Equivalent_Type (gnat_desig_type)))))
2970 if (Present (Equivalent_Type (gnat_desig_type)))
2972 gnat_desig_full = Equivalent_Type (gnat_desig_type);
2973 if (IN (Ekind (gnat_desig_full), Incomplete_Or_Private_Kind))
2974 gnat_desig_full = Full_View (gnat_desig_full);
2976 else if (IN (Ekind (Root_Type (gnat_desig_type)),
2977 Incomplete_Or_Private_Kind))
2978 gnat_desig_full = Full_View (Root_Type (gnat_desig_type));
2981 if (Present (gnat_desig_full) && Is_Concurrent_Type (gnat_desig_full))
2982 gnat_desig_full = Corresponding_Record_Type (gnat_desig_full);
2984 /* If either the designated type or its full view is an
2985 unconstrained array subtype, replace it with the type it's a
2986 subtype of. This avoids problems with multiple copies of
2987 unconstrained array types. */
2988 if (Ekind (gnat_desig_type) == E_Array_Subtype
2989 && !Is_Constrained (gnat_desig_type))
2990 gnat_desig_type = Etype (gnat_desig_type);
2991 if (Present (gnat_desig_full)
2992 && Ekind (gnat_desig_full) == E_Array_Subtype
2993 && !Is_Constrained (gnat_desig_full))
2994 gnat_desig_full = Etype (gnat_desig_full);
2996 /* If the designated type is a subtype of an incomplete record type,
2997 use the parent type to avoid order of elaboration issues. This
2998 can lose some code efficiency, but there is no alternative. */
2999 if (Present (gnat_desig_full)
3000 && Ekind (gnat_desig_full) == E_Record_Subtype
3001 && Ekind (Etype (gnat_desig_full)) == E_Record_Type)
3002 gnat_desig_full = Etype (gnat_desig_full);
3004 /* If we are pointing to an incomplete type whose completion is an
3005 unconstrained array, make a fat pointer type instead of a pointer
3006 to VOID. The two types in our fields will be pointers to VOID and
3007 will be replaced in update_pointer_to. Similarly, if the type
3008 itself is a dummy type or an unconstrained array. Also make
3009 a dummy TYPE_OBJECT_RECORD_TYPE in case we have any thin
3012 if ((Present (gnat_desig_full)
3013 && Is_Array_Type (gnat_desig_full)
3014 && !Is_Constrained (gnat_desig_full))
3015 || (present_gnu_tree (gnat_desig_type)