OSDN Git Service

* ada-tree.h (TYPE_RM_SIZE_NUM): Delete.
[pf3gnuchains/gcc-fork.git] / gcc / ada / gcc-interface / decl.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                                 D E C L                                  *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *          Copyright (C) 1992-2009, Free Software Foundation, Inc.         *
10  *                                                                          *
11  * GNAT is free software;  you can  redistribute it  and/or modify it under *
12  * terms of the  GNU General Public License as published  by the Free Soft- *
13  * ware  Foundation;  either version 3,  or (at your option) any later ver- *
14  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
17  * for  more details.  You should have received a copy of the GNU General   *
18  * Public License along with GCC; see the file COPYING3.  If not see        *
19  * <http://www.gnu.org/licenses/>.                                          *
20  *                                                                          *
21  * GNAT was originally developed  by the GNAT team at  New York University. *
22  * Extensive contributions were provided by Ada Core Technologies Inc.      *
23  *                                                                          *
24  ****************************************************************************/
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "convert.h"
34 #include "ggc.h"
35 #include "obstack.h"
36 #include "target.h"
37 #include "expr.h"
38
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "stringt.h"
48 #include "uintp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "hashtab.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
55
56 #ifndef MAX_FIXED_MODE_SIZE
57 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
58 #endif
59
60 /* Convention_Stdcall should be processed in a specific way on Windows targets
61    only.  The macro below is a helper to avoid having to check for a Windows
62    specific attribute throughout this unit.  */
63
64 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #else
67 #define Has_Stdcall_Convention(E) (0)
68 #endif
69
70 /* Stack realignment for functions with foreign conventions is provided on a
71    per back-end basis now, as it is handled by the prologue expanders and not
72    as part of the function's body any more.  It might be requested by way of a
73    dedicated function type attribute on the targets that support it.
74
75    We need a way to avoid setting the attribute on the targets that don't
76    support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
77
78    It is defined on targets where the circuitry is available, and indicates
79    whether the realignment is needed for 'main'.  We use this to decide for
80    foreign subprograms as well.
81
82    It is not defined on targets where the circuitry is not implemented, and
83    we just never set the attribute in these cases.
84
85    Whether it is defined on all targets that would need it in theory is
86    not entirely clear.  We currently trust the base GCC settings for this
87    purpose.  */
88
89 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
90 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
91 #endif
92
93 struct incomplete
94 {
95   struct incomplete *next;
96   tree old_type;
97   Entity_Id full_type;
98 };
99
100 /* These variables are used to defer recursively expanding incomplete types
101    while we are processing an array, a record or a subprogram type.  */
102 static int defer_incomplete_level = 0;
103 static struct incomplete *defer_incomplete_list;
104
105 /* This variable is used to delay expanding From_With_Type types until the
106    end of the spec.  */
107 static struct incomplete *defer_limited_with;
108
109 /* These variables are used to defer finalizing types.  The element of the
110    list is the TYPE_DECL associated with the type.  */
111 static int defer_finalize_level = 0;
112 static VEC (tree,heap) *defer_finalize_list;
113
114 /* A hash table used to cache the result of annotate_value.  */
115 static GTY ((if_marked ("tree_int_map_marked_p"),
116              param_is (struct tree_int_map))) htab_t annotate_value_cache;
117
118 enum alias_set_op
119 {
120   ALIAS_SET_COPY,
121   ALIAS_SET_SUBSET,
122   ALIAS_SET_SUPERSET
123 };
124
125 static void relate_alias_sets (tree, tree, enum alias_set_op);
126
127 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
128 static bool allocatable_size_p (tree, bool);
129 static void prepend_one_attribute_to (struct attrib **,
130                                       enum attr_type, tree, tree, Node_Id);
131 static void prepend_attributes (Entity_Id, struct attrib **);
132 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
133 static bool is_variable_size (tree);
134 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
135                                     bool, bool);
136 static tree make_packable_type (tree, bool);
137 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
138 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
139                                bool *);
140 static bool same_discriminant_p (Entity_Id, Entity_Id);
141 static bool array_type_has_nonaliased_component (Entity_Id, tree);
142 static bool compile_time_known_address_p (Node_Id);
143 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
144                                   bool, bool, bool, bool);
145 static Uint annotate_value (tree);
146 static void annotate_rep (Entity_Id, tree);
147 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
148 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
149 static void set_rm_size (Uint, tree, Entity_Id);
150 static tree make_type_from_size (tree, tree, bool);
151 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
152 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
153 static void check_ok_for_atomic (tree, Entity_Id, bool);
154 static int compatible_signatures_p (tree ftype1, tree ftype2);
155 static void rest_of_type_decl_compilation_no_defer (tree);
156 \f
157 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
158    entity, return the equivalent GCC tree for that entity (a ..._DECL node)
159    and associate the ..._DECL node with the input GNAT defining identifier.
160
161    If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
162    initial value (in GCC tree form).  This is optional for a variable.  For
163    a renamed entity, GNU_EXPR gives the object being renamed.
164
165    DEFINITION is nonzero if this call is intended for a definition.  This is
166    used for separate compilation where it is necessary to know whether an
167    external declaration or a definition must be created if the GCC equivalent
168    was not created previously.  The value of 1 is normally used for a nonzero
169    DEFINITION, but a value of 2 is used in special circumstances, defined in
170    the code.  */
171
172 tree
173 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
174 {
175   Entity_Id gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
176   Entity_Id gnat_temp;
177   Entity_Kind kind = Ekind (gnat_entity);
178   /* Contains the GCC DECL node which is equivalent to the input GNAT node.
179      This node will be associated with the GNAT node by calling at the end
180      of the `switch' statement.  */
181   tree gnu_decl = NULL_TREE;
182   /* Contains the GCC type to be used for the GCC node.  */
183   tree gnu_type = NULL_TREE;
184   /* Contains the GCC size tree to be used for the GCC node.  */
185   tree gnu_size = NULL_TREE;
186   /* Contains the GCC name to be used for the GCC node.  */
187   tree gnu_entity_id;
188   /* True if we have already saved gnu_decl as a GNAT association.  */
189   bool saved = false;
190   /* True if we incremented defer_incomplete_level.  */
191   bool this_deferred = false;
192   /* True if we incremented force_global.  */
193   bool this_global = false;
194   /* True if we should check to see if elaborated during processing.  */
195   bool maybe_present = false;
196   /* True if we made GNU_DECL and its type here.  */
197   bool this_made_decl = false;
198   /* True if debug info is requested for this entity.  */
199   bool debug_info_p = (Needs_Debug_Info (gnat_entity)
200                        || debug_info_level == DINFO_LEVEL_VERBOSE);
201   /* True if this entity is to be considered as imported.  */
202   bool imported_p = (Is_Imported (gnat_entity)
203                      && No (Address_Clause (gnat_entity)));
204   unsigned int esize, align = 0;
205   struct attrib *attr_list = NULL;
206
207   /* First compute a default value for the size of the entity.  */
208   if (Known_Esize (gnat_entity) && UI_Is_In_Int_Range (Esize (gnat_entity)))
209     {
210       unsigned int max_esize;
211       esize = UI_To_Int (Esize (gnat_entity));
212
213       if (IN (kind, Float_Kind))
214         max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
215       else if (IN (kind, Access_Kind))
216         max_esize = POINTER_SIZE * 2;
217       else
218         max_esize = LONG_LONG_TYPE_SIZE;
219
220       esize = MIN (esize, max_esize);
221     }
222   else
223     esize = LONG_LONG_TYPE_SIZE;
224
225   /* Since a use of an Itype is a definition, process it as such if it
226      is not in a with'ed unit.  */
227   if (!definition
228       && Is_Itype (gnat_entity)
229       && !present_gnu_tree (gnat_entity)
230       && In_Extended_Main_Code_Unit (gnat_entity))
231     {
232       /* Ensure that we are in a subprogram mentioned in the Scope chain of
233          this entity, our current scope is global, or we encountered a task
234          or entry (where we can't currently accurately check scoping).  */
235       if (!current_function_decl
236           || DECL_ELABORATION_PROC_P (current_function_decl))
237         {
238           process_type (gnat_entity);
239           return get_gnu_tree (gnat_entity);
240         }
241
242       for (gnat_temp = Scope (gnat_entity);
243            Present (gnat_temp);
244            gnat_temp = Scope (gnat_temp))
245         {
246           if (Is_Type (gnat_temp))
247             gnat_temp = Underlying_Type (gnat_temp);
248
249           if (Ekind (gnat_temp) == E_Subprogram_Body)
250             gnat_temp
251               = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
252
253           if (IN (Ekind (gnat_temp), Subprogram_Kind)
254               && Present (Protected_Body_Subprogram (gnat_temp)))
255             gnat_temp = Protected_Body_Subprogram (gnat_temp);
256
257           if (Ekind (gnat_temp) == E_Entry
258               || Ekind (gnat_temp) == E_Entry_Family
259               || Ekind (gnat_temp) == E_Task_Type
260               || (IN (Ekind (gnat_temp), Subprogram_Kind)
261                   && present_gnu_tree (gnat_temp)
262                   && (current_function_decl
263                       == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
264             {
265               process_type (gnat_entity);
266               return get_gnu_tree (gnat_entity);
267             }
268         }
269
270       /* This abort means the entity has an incorrect scope, i.e. that its
271          scope does not correspond to the subprogram it is declared in.  */
272       gcc_unreachable ();
273     }
274
275   /* If the entiy is not present, something went badly wrong.  */
276   gcc_assert (Present (gnat_entity));
277
278   /* If we've already processed this entity, return what we got last time.
279      If we are defining the node, we should not have already processed it.
280      In that case, we will abort below when we try to save a new GCC tree
281      for this object.  We also need to handle the case of getting a dummy
282      type when a Full_View exists.  */
283   if (present_gnu_tree (gnat_entity)
284       && (!definition || (Is_Type (gnat_entity) && imported_p)))
285     {
286       gnu_decl = get_gnu_tree (gnat_entity);
287
288       if (TREE_CODE (gnu_decl) == TYPE_DECL
289           && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
290           && IN (kind, Incomplete_Or_Private_Kind)
291           && Present (Full_View (gnat_entity)))
292         {
293           gnu_decl
294             = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
295           save_gnu_tree (gnat_entity, NULL_TREE, false);
296           save_gnu_tree (gnat_entity, gnu_decl, false);
297         }
298
299       return gnu_decl;
300     }
301
302   /* If this is a numeric or enumeral type, or an access type, a nonzero
303      Esize must be specified unless it was specified by the programmer.  */
304   gcc_assert (!Unknown_Esize (gnat_entity)
305               || Has_Size_Clause (gnat_entity)
306               || (!IN (kind, Numeric_Kind)
307                   && !IN (kind, Enumeration_Kind)
308                   && (!IN (kind, Access_Kind)
309                       || kind == E_Access_Protected_Subprogram_Type
310                       || kind == E_Anonymous_Access_Protected_Subprogram_Type
311                       || kind == E_Access_Subtype)));
312
313   /* The RM size must be specified for all discrete and fixed-point types.  */
314   gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
315               || !Unknown_RM_Size (gnat_entity));
316
317   /* Get the name of the entity and set up the line number and filename of
318      the original definition for use in any decl we make.  */
319   gnu_entity_id = get_entity_name (gnat_entity);
320   Sloc_to_locus (Sloc (gnat_entity), &input_location);
321
322   /* If we get here, it means we have not yet done anything with this
323      entity.  If we are not defining it here, it must be external,
324      otherwise we should have defined it already.  */
325   gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
326               || kind == E_Discriminant || kind == E_Component
327               || kind == E_Label
328               || (kind == E_Constant && Present (Full_View (gnat_entity)))
329               || IN (kind, Type_Kind));
330
331   /* For cases when we are not defining (i.e., we are referencing from
332      another compilation unit) public entities, show we are at global level
333      for the purpose of computing scopes.  Don't do this for components or
334      discriminants since the relevant test is whether or not the record is
335      being defined.  */
336   if (!definition
337       && Is_Public (gnat_entity)
338       && !Is_Statically_Allocated (gnat_entity)
339       && kind != E_Component
340       && kind != E_Discriminant)
341     force_global++, this_global = true;
342
343   /* Handle any attributes directly attached to the entity.  */
344   if (Has_Gigi_Rep_Item (gnat_entity))
345     prepend_attributes (gnat_entity, &attr_list);
346
347   /* Machine_Attributes on types are expected to be propagated to subtypes.
348      The corresponding Gigi_Rep_Items are only attached to the first subtype
349      though, so we handle the propagation here.  */
350   if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
351       && !Is_First_Subtype (gnat_entity)
352       && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
353     prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
354
355   switch (kind)
356     {
357     case E_Constant:
358       /* If this is a use of a deferred constant without address clause,
359          get its full definition.  */
360       if (!definition
361           && No (Address_Clause (gnat_entity))
362           && Present (Full_View (gnat_entity)))
363         {
364           gnu_decl
365             = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
366           saved = true;
367           break;
368         }
369
370       /* If we have an external constant that we are not defining, get the
371          expression that is was defined to represent.  We may throw that
372          expression away later if it is not a constant.  Do not retrieve the
373          expression if it is an aggregate or allocator, because in complex
374          instantiation contexts it may not be expanded  */
375       if (!definition
376           && Present (Expression (Declaration_Node (gnat_entity)))
377           && !No_Initialization (Declaration_Node (gnat_entity))
378           && (Nkind (Expression (Declaration_Node (gnat_entity)))
379               != N_Aggregate)
380           && (Nkind (Expression (Declaration_Node (gnat_entity)))
381               != N_Allocator))
382         gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
383
384       /* Ignore deferred constant definitions without address clause since
385          they are processed fully in the front-end.  If No_Initialization
386          is set, this is not a deferred constant but a constant whose value
387          is built manually.  And constants that are renamings are handled
388          like variables.  */
389       if (definition
390           && !gnu_expr
391           && No (Address_Clause (gnat_entity))
392           && !No_Initialization (Declaration_Node (gnat_entity))
393           && No (Renamed_Object (gnat_entity)))
394         {
395           gnu_decl = error_mark_node;
396           saved = true;
397           break;
398         }
399
400       /* Ignore constant definitions already marked with the error node.  See
401          the N_Object_Declaration case of gnat_to_gnu for the rationale.  */
402       if (definition
403           && gnu_expr
404           && present_gnu_tree (gnat_entity)
405           && get_gnu_tree (gnat_entity) == error_mark_node)
406         {
407           maybe_present = true;
408           break;
409         }
410
411       goto object;
412
413     case E_Exception:
414       /* We used to special case VMS exceptions here to directly map them to
415          their associated condition code.  Since this code had to be masked
416          dynamically to strip off the severity bits, this caused trouble in
417          the GCC/ZCX case because the "type" pointers we store in the tables
418          have to be static.  We now don't special case here anymore, and let
419          the regular processing take place, which leaves us with a regular
420          exception data object for VMS exceptions too.  The condition code
421          mapping is taken care of by the front end and the bitmasking by the
422          runtime library.  */
423       goto object;
424
425     case E_Discriminant:
426     case E_Component:
427       {
428         /* The GNAT record where the component was defined.  */
429         Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
430
431         /* If the variable is an inherited record component (in the case of
432            extended record types), just return the inherited entity, which
433            must be a FIELD_DECL.  Likewise for discriminants.
434            For discriminants of untagged records which have explicit
435            stored discriminants, return the entity for the corresponding
436            stored discriminant.  Also use Original_Record_Component
437            if the record has a private extension.  */
438         if (Present (Original_Record_Component (gnat_entity))
439             && Original_Record_Component (gnat_entity) != gnat_entity)
440           {
441             gnu_decl
442               = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
443                                     gnu_expr, definition);
444             saved = true;
445             break;
446           }
447
448         /* If the enclosing record has explicit stored discriminants,
449            then it is an untagged record.  If the Corresponding_Discriminant
450            is not empty then this must be a renamed discriminant and its
451            Original_Record_Component must point to the corresponding explicit
452            stored discriminant (i.e. we should have taken the previous
453            branch).  */
454         else if (Present (Corresponding_Discriminant (gnat_entity))
455                  && Is_Tagged_Type (gnat_record))
456           {
457             /* A tagged record has no explicit stored discriminants.  */
458             gcc_assert (First_Discriminant (gnat_record)
459                        == First_Stored_Discriminant (gnat_record));
460             gnu_decl
461               = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
462                                     gnu_expr, definition);
463             saved = true;
464             break;
465           }
466
467         else if (Present (CR_Discriminant (gnat_entity))
468                  && type_annotate_only)
469           {
470             gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
471                                            gnu_expr, definition);
472             saved = true;
473             break;
474           }
475
476         /* If the enclosing record has explicit stored discriminants, then
477            it is an untagged record.  If the Corresponding_Discriminant
478            is not empty then this must be a renamed discriminant and its
479            Original_Record_Component must point to the corresponding explicit
480            stored discriminant (i.e. we should have taken the first
481            branch).  */
482         else if (Present (Corresponding_Discriminant (gnat_entity))
483                  && (First_Discriminant (gnat_record)
484                      != First_Stored_Discriminant (gnat_record)))
485           gcc_unreachable ();
486
487         /* Otherwise, if we are not defining this and we have no GCC type
488            for the containing record, make one for it.  Then we should
489            have made our own equivalent.  */
490         else if (!definition && !present_gnu_tree (gnat_record))
491           {
492             /* ??? If this is in a record whose scope is a protected
493                type and we have an Original_Record_Component, use it.
494                This is a workaround for major problems in protected type
495                handling.  */
496             Entity_Id Scop = Scope (Scope (gnat_entity));
497             if ((Is_Protected_Type (Scop)
498                  || (Is_Private_Type (Scop)
499                      && Present (Full_View (Scop))
500                      && Is_Protected_Type (Full_View (Scop))))
501                 && Present (Original_Record_Component (gnat_entity)))
502               {
503                 gnu_decl
504                   = gnat_to_gnu_entity (Original_Record_Component
505                                         (gnat_entity),
506                                         gnu_expr, 0);
507                 saved = true;
508                 break;
509               }
510
511             gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
512             gnu_decl = get_gnu_tree (gnat_entity);
513             saved = true;
514             break;
515           }
516
517         else
518           /* Here we have no GCC type and this is a reference rather than a
519              definition.  This should never happen.  Most likely the cause is
520              reference before declaration in the gnat tree for gnat_entity.  */
521           gcc_unreachable ();
522       }
523
524     case E_Loop_Parameter:
525     case E_Out_Parameter:
526     case E_Variable:
527
528       /* Simple variables, loop variables, Out parameters, and exceptions.  */
529     object:
530       {
531         bool used_by_ref = false;
532         bool const_flag
533           = ((kind == E_Constant || kind == E_Variable)
534              && Is_True_Constant (gnat_entity)
535              && !Treat_As_Volatile (gnat_entity)
536              && (((Nkind (Declaration_Node (gnat_entity))
537                    == N_Object_Declaration)
538                   && Present (Expression (Declaration_Node (gnat_entity))))
539                  || Present (Renamed_Object (gnat_entity))));
540         bool inner_const_flag = const_flag;
541         bool static_p = Is_Statically_Allocated (gnat_entity);
542         bool mutable_p = false;
543         tree gnu_ext_name = NULL_TREE;
544         tree renamed_obj = NULL_TREE;
545         tree gnu_object_size;
546
547         if (Present (Renamed_Object (gnat_entity)) && !definition)
548           {
549             if (kind == E_Exception)
550               gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
551                                              NULL_TREE, 0);
552             else
553               gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
554           }
555
556         /* Get the type after elaborating the renamed object.  */
557         gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
558
559         /* For a debug renaming declaration, build a pure debug entity.  */
560         if (Present (Debug_Renaming_Link (gnat_entity)))
561           {
562             rtx addr;
563             gnu_decl = build_decl (VAR_DECL, gnu_entity_id, gnu_type);
564             /* The (MEM (CONST (0))) pattern is prescribed by STABS.  */
565             if (global_bindings_p ())
566               addr = gen_rtx_CONST (VOIDmode, const0_rtx);
567             else
568               addr = stack_pointer_rtx;
569             SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
570             gnat_pushdecl (gnu_decl, gnat_entity);
571             break;
572           }
573
574         /* If this is a loop variable, its type should be the base type.
575            This is because the code for processing a loop determines whether
576            a normal loop end test can be done by comparing the bounds of the
577            loop against those of the base type, which is presumed to be the
578            size used for computation.  But this is not correct when the size
579            of the subtype is smaller than the type.  */
580         if (kind == E_Loop_Parameter)
581           gnu_type = get_base_type (gnu_type);
582
583         /* Reject non-renamed objects whose types are unconstrained arrays or
584            any object whose type is a dummy type or VOID_TYPE.  */
585
586         if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
587              && No (Renamed_Object (gnat_entity)))
588             || TYPE_IS_DUMMY_P (gnu_type)
589             || TREE_CODE (gnu_type) == VOID_TYPE)
590           {
591             gcc_assert (type_annotate_only);
592             if (this_global)
593               force_global--;
594             return error_mark_node;
595           }
596
597         /* If an alignment is specified, use it if valid.   Note that
598            exceptions are objects but don't have alignments.  We must do this
599            before we validate the size, since the alignment can affect the
600            size.  */
601         if (kind != E_Exception && Known_Alignment (gnat_entity))
602           {
603             gcc_assert (Present (Alignment (gnat_entity)));
604             align = validate_alignment (Alignment (gnat_entity), gnat_entity,
605                                         TYPE_ALIGN (gnu_type));
606             gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
607                                        "PAD", false, definition, true);
608           }
609
610         /* If we are defining the object, see if it has a Size value and
611            validate it if so.  If we are not defining the object and a Size
612            clause applies, simply retrieve the value.  We don't want to ignore
613            the clause and it is expected to have been validated already.  Then
614            get the new type, if any.  */
615         if (definition)
616           gnu_size = validate_size (Esize (gnat_entity), gnu_type,
617                                     gnat_entity, VAR_DECL, false,
618                                     Has_Size_Clause (gnat_entity));
619         else if (Has_Size_Clause (gnat_entity))
620           gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
621
622         if (gnu_size)
623           {
624             gnu_type
625               = make_type_from_size (gnu_type, gnu_size,
626                                      Has_Biased_Representation (gnat_entity));
627
628             if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
629               gnu_size = NULL_TREE;
630           }
631
632         /* If this object has self-referential size, it must be a record with
633            a default value.  We are supposed to allocate an object of the
634            maximum size in this case unless it is a constant with an
635            initializing expression, in which case we can get the size from
636            that.  Note that the resulting size may still be a variable, so
637            this may end up with an indirect allocation.  */
638         if (No (Renamed_Object (gnat_entity))
639             && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
640           {
641             if (gnu_expr && kind == E_Constant)
642               {
643                 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
644                 if (CONTAINS_PLACEHOLDER_P (size))
645                   {
646                     /* If the initializing expression is itself a constant,
647                        despite having a nominal type with self-referential
648                        size, we can get the size directly from it.  */
649                     if (TREE_CODE (gnu_expr) == COMPONENT_REF
650                         && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
651                            == RECORD_TYPE
652                         && TYPE_IS_PADDING_P
653                            (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
654                         && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
655                         && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
656                             || DECL_READONLY_ONCE_ELAB
657                                (TREE_OPERAND (gnu_expr, 0))))
658                       gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
659                     else
660                       gnu_size
661                         = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
662                   }
663                 else
664                   gnu_size = size;
665               }
666             /* We may have no GNU_EXPR because No_Initialization is
667                set even though there's an Expression.  */
668             else if (kind == E_Constant
669                      && (Nkind (Declaration_Node (gnat_entity))
670                          == N_Object_Declaration)
671                      && Present (Expression (Declaration_Node (gnat_entity))))
672               gnu_size
673                 = TYPE_SIZE (gnat_to_gnu_type
674                              (Etype
675                               (Expression (Declaration_Node (gnat_entity)))));
676             else
677               {
678                 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
679                 mutable_p = true;
680               }
681           }
682
683         /* If the size is zero bytes, make it one byte since some linkers have
684            trouble with zero-sized objects.  If the object will have a
685            template, that will make it nonzero so don't bother.  Also avoid
686            doing that for an object renaming or an object with an address
687            clause, as we would lose useful information on the view size
688            (e.g. for null array slices) and we are not allocating the object
689            here anyway.  */
690         if (((gnu_size
691               && integer_zerop (gnu_size)
692               && !TREE_OVERFLOW (gnu_size))
693              || (TYPE_SIZE (gnu_type)
694                  && integer_zerop (TYPE_SIZE (gnu_type))
695                  && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
696             && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
697                 || !Is_Array_Type (Etype (gnat_entity)))
698             && !Present (Renamed_Object (gnat_entity))
699             && !Present (Address_Clause (gnat_entity)))
700           gnu_size = bitsize_unit_node;
701
702         /* If this is an object with no specified size and alignment, and
703            if either it is atomic or we are not optimizing alignment for
704            space and it is composite and not an exception, an Out parameter
705            or a reference to another object, and the size of its type is a
706            constant, set the alignment to the smallest one which is not
707            smaller than the size, with an appropriate cap.  */
708         if (!gnu_size && align == 0
709             && (Is_Atomic (gnat_entity)
710                 || (!Optimize_Alignment_Space (gnat_entity)
711                     && kind != E_Exception
712                     && kind != E_Out_Parameter
713                     && Is_Composite_Type (Etype (gnat_entity))
714                     && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
715                     && !imported_p
716                     && No (Renamed_Object (gnat_entity))
717                     && No (Address_Clause (gnat_entity))))
718             && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
719           {
720             /* No point in jumping through all the hoops needed in order
721                to support BIGGEST_ALIGNMENT if we don't really have to.
722                So we cap to the smallest alignment that corresponds to
723                a known efficient memory access pattern of the target.  */
724             unsigned int align_cap = Is_Atomic (gnat_entity)
725                                      ? BIGGEST_ALIGNMENT
726                                      : get_mode_alignment (ptr_mode);
727
728             if (!host_integerp (TYPE_SIZE (gnu_type), 1)
729                 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
730               align = align_cap;
731             else
732               align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
733
734             /* But make sure not to under-align the object.  */
735             if (align <= TYPE_ALIGN (gnu_type))
736               align = 0;
737
738             /* And honor the minimum valid atomic alignment, if any.  */
739 #ifdef MINIMUM_ATOMIC_ALIGNMENT
740             else if (align < MINIMUM_ATOMIC_ALIGNMENT)
741               align = MINIMUM_ATOMIC_ALIGNMENT;
742 #endif
743           }
744
745         /* If the object is set to have atomic components, find the component
746            type and validate it.
747
748            ??? Note that we ignore Has_Volatile_Components on objects; it's
749            not at all clear what to do in that case.  */
750
751         if (Has_Atomic_Components (gnat_entity))
752           {
753             tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
754                               ? TREE_TYPE (gnu_type) : gnu_type);
755
756             while (TREE_CODE (gnu_inner) == ARRAY_TYPE
757                    && TYPE_MULTI_ARRAY_P (gnu_inner))
758               gnu_inner = TREE_TYPE (gnu_inner);
759
760             check_ok_for_atomic (gnu_inner, gnat_entity, true);
761           }
762
763         /* Now check if the type of the object allows atomic access.  Note
764            that we must test the type, even if this object has size and
765            alignment to allow such access, because we will be going
766            inside the padded record to assign to the object.  We could fix
767            this by always copying via an intermediate value, but it's not
768            clear it's worth the effort.  */
769         if (Is_Atomic (gnat_entity))
770           check_ok_for_atomic (gnu_type, gnat_entity, false);
771
772         /* If this is an aliased object with an unconstrained nominal subtype,
773            make a type that includes the template.  */
774         if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
775             && Is_Array_Type (Etype (gnat_entity))
776             && !type_annotate_only)
777         {
778           tree gnu_fat
779             = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
780
781           gnu_type
782             = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
783                                      concat_id_with_name (gnu_entity_id,
784                                                           "UNC"));
785         }
786
787 #ifdef MINIMUM_ATOMIC_ALIGNMENT
788         /* If the size is a constant and no alignment is specified, force
789            the alignment to be the minimum valid atomic alignment.  The
790            restriction on constant size avoids problems with variable-size
791            temporaries; if the size is variable, there's no issue with
792            atomic access.  Also don't do this for a constant, since it isn't
793            necessary and can interfere with constant replacement.  Finally,
794            do not do it for Out parameters since that creates an
795            size inconsistency with In parameters.  */
796         if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
797             && !FLOAT_TYPE_P (gnu_type)
798             && !const_flag && No (Renamed_Object (gnat_entity))
799             && !imported_p && No (Address_Clause (gnat_entity))
800             && kind != E_Out_Parameter
801             && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
802                 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
803           align = MINIMUM_ATOMIC_ALIGNMENT;
804 #endif
805
806         /* Make a new type with the desired size and alignment, if needed.
807            But do not take into account alignment promotions to compute the
808            size of the object.  */
809         gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
810         if (gnu_size || align > 0)
811           gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
812                                      "PAD", false, definition,
813                                      gnu_size ? true : false);
814
815         /* If this is a renaming, avoid as much as possible to create a new
816            object.  However, in several cases, creating it is required.
817            This processing needs to be applied to the raw expression so
818            as to make it more likely to rename the underlying object.  */
819         if (Present (Renamed_Object (gnat_entity)))
820           {
821             bool create_normal_object = false;
822
823             /* If the renamed object had padding, strip off the reference
824                to the inner object and reset our type.  */
825             if ((TREE_CODE (gnu_expr) == COMPONENT_REF
826                  && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
827                     == RECORD_TYPE
828                  && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
829                 /* Strip useless conversions around the object.  */
830                 || (TREE_CODE (gnu_expr) == NOP_EXPR
831                     && gnat_types_compatible_p
832                        (TREE_TYPE (gnu_expr),
833                         TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
834               {
835                 gnu_expr = TREE_OPERAND (gnu_expr, 0);
836                 gnu_type = TREE_TYPE (gnu_expr);
837               }
838
839             /* Case 1: If this is a constant renaming stemming from a function
840                call, treat it as a normal object whose initial value is what
841                is being renamed.  RM 3.3 says that the result of evaluating a
842                function call is a constant object.  As a consequence, it can
843                be the inner object of a constant renaming.  In this case, the
844                renaming must be fully instantiated, i.e. it cannot be a mere
845                reference to (part of) an existing object.  */
846             if (const_flag)
847               {
848                 tree inner_object = gnu_expr;
849                 while (handled_component_p (inner_object))
850                   inner_object = TREE_OPERAND (inner_object, 0);
851                 if (TREE_CODE (inner_object) == CALL_EXPR)
852                   create_normal_object = true;
853               }
854
855             /* Otherwise, see if we can proceed with a stabilized version of
856                the renamed entity or if we need to make a new object.  */
857             if (!create_normal_object)
858               {
859                 tree maybe_stable_expr = NULL_TREE;
860                 bool stable = false;
861
862                 /* Case 2: If the renaming entity need not be materialized and
863                    the renamed expression is something we can stabilize, use
864                    that for the renaming.  At the global level, we can only do
865                    this if we know no SAVE_EXPRs need be made, because the
866                    expression we return might be used in arbitrary conditional
867                    branches so we must force the SAVE_EXPRs evaluation
868                    immediately and this requires a function context.  */
869                 if (!Materialize_Entity (gnat_entity)
870                     && (!global_bindings_p ()
871                         || (staticp (gnu_expr)
872                             && !TREE_SIDE_EFFECTS (gnu_expr))))
873                   {
874                     maybe_stable_expr
875                       = maybe_stabilize_reference (gnu_expr, true, &stable);
876
877                     if (stable)
878                       {
879                         gnu_decl = maybe_stable_expr;
880                         /* ??? No DECL_EXPR is created so we need to mark
881                            the expression manually lest it is shared.  */
882                         if (global_bindings_p ())
883                           mark_visited (&gnu_decl);
884                         save_gnu_tree (gnat_entity, gnu_decl, true);
885                         saved = true;
886                         break;
887                       }
888
889                     /* The stabilization failed.  Keep maybe_stable_expr
890                        untouched here to let the pointer case below know
891                        about that failure.  */
892                   }
893
894                 /* Case 3: If this is a constant renaming and creating a
895                    new object is allowed and cheap, treat it as a normal
896                    object whose initial value is what is being renamed.  */
897                 if (const_flag
898                     && !Is_Composite_Type
899                         (Underlying_Type (Etype (gnat_entity))))
900                   ;
901
902                 /* Case 4: Make this into a constant pointer to the object we
903                    are to rename and attach the object to the pointer if it is
904                    something we can stabilize.
905
906                    From the proper scope, attached objects will be referenced
907                    directly instead of indirectly via the pointer to avoid
908                    subtle aliasing problems with non-addressable entities.
909                    They have to be stable because we must not evaluate the
910                    variables in the expression every time the renaming is used.
911                    The pointer is called a "renaming" pointer in this case.
912
913                    In the rare cases where we cannot stabilize the renamed
914                    object, we just make a "bare" pointer, and the renamed
915                    entity is always accessed indirectly through it.  */
916                 else
917                   {
918                     gnu_type = build_reference_type (gnu_type);
919                     inner_const_flag = TREE_READONLY (gnu_expr);
920                     const_flag = true;
921
922                     /* If the previous attempt at stabilizing failed, there
923                        is no point in trying again and we reuse the result
924                        without attaching it to the pointer.  In this case it
925                        will only be used as the initializing expression of
926                        the pointer and thus needs no special treatment with
927                        regard to multiple evaluations.  */
928                     if (maybe_stable_expr)
929                       ;
930
931                     /* Otherwise, try to stabilize and attach the expression
932                        to the pointer if the stabilization succeeds.
933
934                        Note that this might introduce SAVE_EXPRs and we don't
935                        check whether we're at the global level or not.  This
936                        is fine since we are building a pointer initializer and
937                        neither the pointer nor the initializing expression can
938                        be accessed before the pointer elaboration has taken
939                        place in a correct program.
940
941                        These SAVE_EXPRs will be evaluated at the right place
942                        by either the evaluation of the initializer for the
943                        non-global case or the elaboration code for the global
944                        case, and will be attached to the elaboration procedure
945                        in the latter case.  */
946                     else
947                      {
948                         maybe_stable_expr
949                           = maybe_stabilize_reference (gnu_expr, true, &stable);
950
951                         if (stable)
952                           renamed_obj = maybe_stable_expr;
953
954                         /* Attaching is actually performed downstream, as soon
955                            as we have a VAR_DECL for the pointer we make.  */
956                       }
957
958                     gnu_expr
959                       = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
960
961                     gnu_size = NULL_TREE;
962                     used_by_ref = true;
963                   }
964               }
965           }
966
967         /* Make a volatile version of this object's type if we are to make
968            the object volatile.  We also interpret 13.3(19) conservatively
969            and disallow any optimizations for such a non-constant object.  */
970         if ((Treat_As_Volatile (gnat_entity)
971              || (!const_flag
972                  && (Is_Exported (gnat_entity)
973                      || Is_Imported (gnat_entity)
974                      || Present (Address_Clause (gnat_entity)))))
975             && !TYPE_VOLATILE (gnu_type))
976           gnu_type = build_qualified_type (gnu_type,
977                                            (TYPE_QUALS (gnu_type)
978                                             | TYPE_QUAL_VOLATILE));
979
980         /* If we are defining an aliased object whose nominal subtype is
981            unconstrained, the object is a record that contains both the
982            template and the object.  If there is an initializer, it will
983            have already been converted to the right type, but we need to
984            create the template if there is no initializer.  */
985         if (definition
986             && !gnu_expr
987             && TREE_CODE (gnu_type) == RECORD_TYPE
988             && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
989                 /* Beware that padding might have been introduced
990                    via maybe_pad_type above.  */
991                 || (TYPE_IS_PADDING_P (gnu_type)
992                     && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
993                        == RECORD_TYPE
994                     && TYPE_CONTAINS_TEMPLATE_P
995                        (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
996           {
997             tree template_field
998               = TYPE_IS_PADDING_P (gnu_type)
999                 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1000                 : TYPE_FIELDS (gnu_type);
1001
1002             gnu_expr
1003               = gnat_build_constructor
1004               (gnu_type,
1005                tree_cons
1006                (template_field,
1007                 build_template (TREE_TYPE (template_field),
1008                                 TREE_TYPE (TREE_CHAIN (template_field)),
1009                                 NULL_TREE),
1010                 NULL_TREE));
1011           }
1012
1013         /* Convert the expression to the type of the object except in the
1014            case where the object's type is unconstrained or the object's type
1015            is a padded record whose field is of self-referential size.  In
1016            the former case, converting will generate unnecessary evaluations
1017            of the CONSTRUCTOR to compute the size and in the latter case, we
1018            want to only copy the actual data.  */
1019         if (gnu_expr
1020             && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1021             && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1022             && !(TREE_CODE (gnu_type) == RECORD_TYPE
1023                  && TYPE_IS_PADDING_P (gnu_type)
1024                  && (CONTAINS_PLACEHOLDER_P
1025                      (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1026           gnu_expr = convert (gnu_type, gnu_expr);
1027
1028         /* If this is a pointer and it does not have an initializing
1029            expression, initialize it to NULL, unless the object is
1030            imported.  */
1031         if (definition
1032             && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1033             && !Is_Imported (gnat_entity) && !gnu_expr)
1034           gnu_expr = integer_zero_node;
1035
1036         /* If we are defining the object and it has an Address clause, we must
1037            either get the address expression from the saved GCC tree for the
1038            object if it has a Freeze node, or elaborate the address expression
1039            here since the front-end has guaranteed that the elaboration has no
1040            effects in this case.  */
1041         if (definition && Present (Address_Clause (gnat_entity)))
1042           {
1043             tree gnu_address
1044               = present_gnu_tree (gnat_entity)
1045                 ? get_gnu_tree (gnat_entity)
1046                 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1047
1048             save_gnu_tree (gnat_entity, NULL_TREE, false);
1049
1050             /* Ignore the size.  It's either meaningless or was handled
1051                above.  */
1052             gnu_size = NULL_TREE;
1053             /* Convert the type of the object to a reference type that can
1054                alias everything as per 13.3(19).  */
1055             gnu_type
1056               = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1057             gnu_address = convert (gnu_type, gnu_address);
1058             used_by_ref = true;
1059             const_flag = !Is_Public (gnat_entity)
1060               || compile_time_known_address_p (Expression (Address_Clause
1061                                                            (gnat_entity)));
1062
1063             /* If this is a deferred constant, the initializer is attached to
1064                the full view.  */
1065             if (kind == E_Constant && Present (Full_View (gnat_entity)))
1066               gnu_expr
1067                 = gnat_to_gnu
1068                     (Expression (Declaration_Node (Full_View (gnat_entity))));
1069
1070             /* If we don't have an initializing expression for the underlying
1071                variable, the initializing expression for the pointer is the
1072                specified address.  Otherwise, we have to make a COMPOUND_EXPR
1073                to assign both the address and the initial value.  */
1074             if (!gnu_expr)
1075               gnu_expr = gnu_address;
1076             else
1077               gnu_expr
1078                 = build2 (COMPOUND_EXPR, gnu_type,
1079                           build_binary_op
1080                           (MODIFY_EXPR, NULL_TREE,
1081                            build_unary_op (INDIRECT_REF, NULL_TREE,
1082                                            gnu_address),
1083                            gnu_expr),
1084                           gnu_address);
1085           }
1086
1087         /* If it has an address clause and we are not defining it, mark it
1088            as an indirect object.  Likewise for Stdcall objects that are
1089            imported.  */
1090         if ((!definition && Present (Address_Clause (gnat_entity)))
1091             || (Is_Imported (gnat_entity)
1092                 && Has_Stdcall_Convention (gnat_entity)))
1093           {
1094             /* Convert the type of the object to a reference type that can
1095                alias everything as per 13.3(19).  */
1096             gnu_type
1097               = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1098             gnu_size = NULL_TREE;
1099
1100             /* No point in taking the address of an initializing expression
1101                that isn't going to be used.  */
1102             gnu_expr = NULL_TREE;
1103
1104             /* If it has an address clause whose value is known at compile
1105                time, make the object a CONST_DECL.  This will avoid a
1106                useless dereference.  */
1107             if (Present (Address_Clause (gnat_entity)))
1108               {
1109                 Node_Id gnat_address
1110                   = Expression (Address_Clause (gnat_entity));
1111
1112                 if (compile_time_known_address_p (gnat_address))
1113                   {
1114                     gnu_expr = gnat_to_gnu (gnat_address);
1115                     const_flag = true;
1116                   }
1117               }
1118
1119             used_by_ref = true;
1120           }
1121
1122         /* If we are at top level and this object is of variable size,
1123            make the actual type a hidden pointer to the real type and
1124            make the initializer be a memory allocation and initialization.
1125            Likewise for objects we aren't defining (presumed to be
1126            external references from other packages), but there we do
1127            not set up an initialization.
1128
1129            If the object's size overflows, make an allocator too, so that
1130            Storage_Error gets raised.  Note that we will never free
1131            such memory, so we presume it never will get allocated.  */
1132
1133         if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1134                                  global_bindings_p () || !definition
1135                                  || static_p)
1136             || (gnu_size
1137                 && ! allocatable_size_p (gnu_size,
1138                                          global_bindings_p () || !definition
1139                                          || static_p)))
1140           {
1141             gnu_type = build_reference_type (gnu_type);
1142             gnu_size = NULL_TREE;
1143             used_by_ref = true;
1144             const_flag = true;
1145
1146             /* In case this was a aliased object whose nominal subtype is
1147                unconstrained, the pointer above will be a thin pointer and
1148                build_allocator will automatically make the template.
1149
1150                If we have a template initializer only (that we made above),
1151                pretend there is none and rely on what build_allocator creates
1152                again anyway.  Otherwise (if we have a full initializer), get
1153                the data part and feed that to build_allocator.
1154
1155                If we are elaborating a mutable object, tell build_allocator to
1156                ignore a possibly simpler size from the initializer, if any, as
1157                we must allocate the maximum possible size in this case.  */
1158
1159             if (definition)
1160               {
1161                 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1162
1163                 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1164                     && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1165                   {
1166                     gnu_alloc_type
1167                       = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1168
1169                     if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1170                         && 1 == VEC_length (constructor_elt,
1171                                             CONSTRUCTOR_ELTS (gnu_expr)))
1172                       gnu_expr = 0;
1173                     else
1174                       gnu_expr
1175                         = build_component_ref
1176                             (gnu_expr, NULL_TREE,
1177                              TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1178                              false);
1179                   }
1180
1181                 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1182                     && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1183                     && !Is_Imported (gnat_entity))
1184                   post_error ("?Storage_Error will be raised at run-time!",
1185                               gnat_entity);
1186
1187                 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1188                                             0, 0, gnat_entity, mutable_p);
1189               }
1190             else
1191               {
1192                 gnu_expr = NULL_TREE;
1193                 const_flag = false;
1194               }
1195           }
1196
1197         /* If this object would go into the stack and has an alignment larger
1198            than the largest stack alignment the back-end can honor, resort to
1199            a variable of "aligning type".  */
1200         if (!global_bindings_p () && !static_p && definition
1201             && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1202           {
1203             /* Create the new variable.  No need for extra room before the
1204                aligned field as this is in automatic storage.  */
1205             tree gnu_new_type
1206               = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1207                                     TYPE_SIZE_UNIT (gnu_type),
1208                                     BIGGEST_ALIGNMENT, 0);
1209             tree gnu_new_var
1210               = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1211                                  NULL_TREE, gnu_new_type, NULL_TREE, false,
1212                                  false, false, false, NULL, gnat_entity);
1213
1214             /* Initialize the aligned field if we have an initializer.  */
1215             if (gnu_expr)
1216               add_stmt_with_node
1217                 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1218                                   build_component_ref
1219                                   (gnu_new_var, NULL_TREE,
1220                                    TYPE_FIELDS (gnu_new_type), false),
1221                                   gnu_expr),
1222                  gnat_entity);
1223
1224             /* And setup this entity as a reference to the aligned field.  */
1225             gnu_type = build_reference_type (gnu_type);
1226             gnu_expr
1227               = build_unary_op
1228                 (ADDR_EXPR, gnu_type,
1229                  build_component_ref (gnu_new_var, NULL_TREE,
1230                                       TYPE_FIELDS (gnu_new_type), false));
1231
1232             gnu_size = NULL_TREE;
1233             used_by_ref = true;
1234             const_flag = true;
1235           }
1236
1237         if (const_flag)
1238           gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1239                                                       | TYPE_QUAL_CONST));
1240
1241         /* Convert the expression to the type of the object except in the
1242            case where the object's type is unconstrained or the object's type
1243            is a padded record whose field is of self-referential size.  In
1244            the former case, converting will generate unnecessary evaluations
1245            of the CONSTRUCTOR to compute the size and in the latter case, we
1246            want to only copy the actual data.  */
1247         if (gnu_expr
1248             && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1249             && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1250             && !(TREE_CODE (gnu_type) == RECORD_TYPE
1251                  && TYPE_IS_PADDING_P (gnu_type)
1252                  && (CONTAINS_PLACEHOLDER_P
1253                      (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1254           gnu_expr = convert (gnu_type, gnu_expr);
1255
1256         /* If this name is external or there was a name specified, use it,
1257            unless this is a VMS exception object since this would conflict
1258            with the symbol we need to export in addition.  Don't use the
1259            Interface_Name if there is an address clause (see CD30005).  */
1260         if (!Is_VMS_Exception (gnat_entity)
1261             && ((Present (Interface_Name (gnat_entity))
1262                  && No (Address_Clause (gnat_entity)))
1263                 || (Is_Public (gnat_entity)
1264                     && (!Is_Imported (gnat_entity)
1265                         || Is_Exported (gnat_entity)))))
1266           gnu_ext_name = create_concat_name (gnat_entity, 0);
1267
1268         /* If this is constant initialized to a static constant and the
1269            object has an aggregate type, force it to be statically
1270            allocated.  This will avoid an initialization copy.  */
1271         if (!static_p && const_flag
1272             && gnu_expr && TREE_CONSTANT (gnu_expr)
1273             && AGGREGATE_TYPE_P (gnu_type)
1274             && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1275             && !(TREE_CODE (gnu_type) == RECORD_TYPE
1276                  && TYPE_IS_PADDING_P (gnu_type)
1277                  && !host_integerp (TYPE_SIZE_UNIT
1278                                     (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1279           static_p = true;
1280
1281         gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1282                                     gnu_expr, const_flag,
1283                                     Is_Public (gnat_entity),
1284                                     imported_p || !definition,
1285                                     static_p, attr_list, gnat_entity);
1286         DECL_BY_REF_P (gnu_decl) = used_by_ref;
1287         DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1288         if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1289           {
1290             SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1291             if (global_bindings_p ())
1292               {
1293                 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1294                 record_global_renaming_pointer (gnu_decl);
1295               }
1296           }
1297
1298         if (definition && DECL_SIZE_UNIT (gnu_decl)
1299             && get_block_jmpbuf_decl ()
1300             && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1301                 || (flag_stack_check == GENERIC_STACK_CHECK
1302                     && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1303                                          STACK_CHECK_MAX_VAR_SIZE) > 0)))
1304           add_stmt_with_node (build_call_1_expr
1305                               (update_setjmp_buf_decl,
1306                                build_unary_op (ADDR_EXPR, NULL_TREE,
1307                                                get_block_jmpbuf_decl ())),
1308                               gnat_entity);
1309
1310         /* If we are defining an Out parameter and we're not optimizing,
1311            create a fake PARM_DECL for debugging purposes and make it
1312            point to the VAR_DECL.  Suppress debug info for the latter
1313            but make sure it will still live on the stack so it can be
1314            accessed from within the debugger through the PARM_DECL.  */
1315         if (kind == E_Out_Parameter && definition && !optimize)
1316           {
1317             tree param = create_param_decl (gnu_entity_id, gnu_type, false);
1318             gnat_pushdecl (param, gnat_entity);
1319             SET_DECL_VALUE_EXPR (param, gnu_decl);
1320             DECL_HAS_VALUE_EXPR_P (param) = 1;
1321             if (debug_info_p)
1322               debug_info_p = false;
1323             else
1324               DECL_IGNORED_P (param) = 1;
1325             TREE_ADDRESSABLE (gnu_decl) = 1;
1326           }
1327
1328         /* If this is a public constant or we're not optimizing and we're not
1329            making a VAR_DECL for it, make one just for export or debugger use.
1330            Likewise if the address is taken or if either the object or type is
1331            aliased.  Make an external declaration for a reference, unless this
1332            is a Standard entity since there no real symbol at the object level
1333            for these.  */
1334         if (TREE_CODE (gnu_decl) == CONST_DECL
1335             && (definition || Sloc (gnat_entity) > Standard_Location)
1336             && ((Is_Public (gnat_entity)
1337                  && !Present (Address_Clause (gnat_entity)))
1338                 || !optimize
1339                 || Address_Taken (gnat_entity)
1340                 || Is_Aliased (gnat_entity)
1341                 || Is_Aliased (Etype (gnat_entity))))
1342           {
1343             tree gnu_corr_var
1344               = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1345                                       gnu_expr, true, Is_Public (gnat_entity),
1346                                       !definition, static_p, NULL,
1347                                       gnat_entity);
1348
1349             SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1350
1351             /* As debugging information will be generated for the variable,
1352                do not generate information for the constant.  */
1353             DECL_IGNORED_P (gnu_decl) = 1;
1354           }
1355
1356         /* If this is declared in a block that contains a block with an
1357            exception handler, we must force this variable in memory to
1358            suppress an invalid optimization.  */
1359         if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1360             && Exception_Mechanism != Back_End_Exceptions)
1361           TREE_ADDRESSABLE (gnu_decl) = 1;
1362
1363         gnu_type = TREE_TYPE (gnu_decl);
1364
1365         /* Back-annotate Alignment and Esize of the object if not already
1366            known, except for when the object is actually a pointer to the
1367            real object, since alignment and size of a pointer don't have
1368            anything to do with those of the designated object.  Note that
1369            we pick the values of the type, not those of the object, to
1370            shield ourselves from low-level platform-dependent adjustments
1371            like alignment promotion.  This is both consistent with all the
1372            treatment above, where alignment and size are set on the type of
1373            the object and not on the object directly, and makes it possible
1374            to support confirming representation clauses in all cases.  */
1375
1376         if (!used_by_ref && Unknown_Alignment (gnat_entity))
1377           Set_Alignment (gnat_entity,
1378                          UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1379
1380         if (!used_by_ref && Unknown_Esize (gnat_entity))
1381           {
1382             if (TREE_CODE (gnu_type) == RECORD_TYPE
1383                 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1384               gnu_object_size
1385                 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1386
1387             Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1388           }
1389       }
1390       break;
1391
1392     case E_Void:
1393       /* Return a TYPE_DECL for "void" that we previously made.  */
1394       gnu_decl = TYPE_NAME (void_type_node);
1395       break;
1396
1397     case E_Enumeration_Type:
1398       /* A special case, for the types Character and Wide_Character in
1399          Standard, we do not list all the literals.  So if the literals
1400          are not specified, make this an unsigned type.  */
1401       if (No (First_Literal (gnat_entity)))
1402         {
1403           gnu_type = make_unsigned_type (esize);
1404           TYPE_NAME (gnu_type) = gnu_entity_id;
1405
1406           /* Set TYPE_STRING_FLAG for Ada Character and Wide_Character types.
1407              This is needed by the DWARF-2 back-end to distinguish between
1408              unsigned integer types and character types.  */
1409           TYPE_STRING_FLAG (gnu_type) = 1;
1410           break;
1411         }
1412
1413       /* Normal case of non-character type, or non-Standard character type */
1414       {
1415         /* Here we have a list of enumeral constants in First_Literal.
1416            We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1417            the list to be places into TYPE_FIELDS.  Each node in the list
1418            is a TREE_LIST node whose TREE_VALUE is the literal name
1419            and whose TREE_PURPOSE is the value of the literal.
1420
1421            Esize contains the number of bits needed to represent the enumeral
1422            type, Type_Low_Bound also points to the first literal and
1423            Type_High_Bound points to the last literal.  */
1424
1425         Entity_Id gnat_literal;
1426         tree gnu_literal_list = NULL_TREE;
1427
1428         if (Is_Unsigned_Type (gnat_entity))
1429           gnu_type = make_unsigned_type (esize);
1430         else
1431           gnu_type = make_signed_type (esize);
1432
1433         TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1434
1435         for (gnat_literal = First_Literal (gnat_entity);
1436              Present (gnat_literal);
1437              gnat_literal = Next_Literal (gnat_literal))
1438           {
1439             tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1440                                         gnu_type);
1441             tree gnu_literal
1442               = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1443                                  gnu_type, gnu_value, true, false, false,
1444                                  false, NULL, gnat_literal);
1445
1446             save_gnu_tree (gnat_literal, gnu_literal, false);
1447             gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1448                                           gnu_value, gnu_literal_list);
1449           }
1450
1451         TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1452
1453         /* Note that the bounds are updated at the end of this function
1454            because to avoid an infinite recursion when we get the bounds of
1455            this type, since those bounds are objects of this type.    */
1456       }
1457       break;
1458
1459     case E_Signed_Integer_Type:
1460     case E_Ordinary_Fixed_Point_Type:
1461     case E_Decimal_Fixed_Point_Type:
1462       /* For integer types, just make a signed type the appropriate number
1463          of bits.  */
1464       gnu_type = make_signed_type (esize);
1465       break;
1466
1467     case E_Modular_Integer_Type:
1468       {
1469         /* For modular types, make the unsigned type of the proper number
1470            of bits and then set up the modulus, if required.  */
1471         tree gnu_modulus, gnu_high = NULL_TREE;
1472         enum machine_mode mode;
1473
1474         /* Packed array types are supposed to be subtypes only.  */
1475         gcc_assert (!Is_Packed_Array_Type (gnat_entity));
1476
1477         /* Find the smallest mode at least ESIZE bits wide and make a class
1478            using that mode.  */
1479         for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1480              GET_MODE_BITSIZE (mode) < esize;
1481              mode = GET_MODE_WIDER_MODE (mode))
1482           ;
1483
1484         gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1485
1486         /* Get the modulus in this type.  If it overflows, assume it is because
1487            it is equal to 2**Esize.  Note that there is no overflow checking
1488            done on unsigned type, so we detect the overflow by looking for
1489            a modulus of zero, which is otherwise invalid.  */
1490         gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1491
1492         if (!integer_zerop (gnu_modulus))
1493           {
1494             TYPE_MODULAR_P (gnu_type) = 1;
1495             SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1496             gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1497                                     convert (gnu_type, integer_one_node));
1498           }
1499
1500         /* If we have to set TYPE_PRECISION different from its natural value,
1501            make a subtype to do do.  Likewise if there is a modulus and
1502            it is not one greater than TYPE_MAX_VALUE.  */
1503         if (TYPE_PRECISION (gnu_type) != esize
1504             || (TYPE_MODULAR_P (gnu_type)
1505                 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1506           {
1507             tree gnu_subtype = make_node (INTEGER_TYPE);
1508             TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1509             TREE_TYPE (gnu_subtype) = gnu_type;
1510             TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1511             TYPE_MAX_VALUE (gnu_subtype)
1512               = TYPE_MODULAR_P (gnu_type)
1513                 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1514             TYPE_PRECISION (gnu_subtype) = esize;
1515             TYPE_UNSIGNED (gnu_subtype) = 1;
1516             TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1517             layout_type (gnu_subtype);
1518             gnu_type = gnu_subtype;
1519           }
1520       }
1521       break;
1522
1523     case E_Signed_Integer_Subtype:
1524     case E_Enumeration_Subtype:
1525     case E_Modular_Integer_Subtype:
1526     case E_Ordinary_Fixed_Point_Subtype:
1527     case E_Decimal_Fixed_Point_Subtype:
1528
1529       /* For integral subtypes, we make a new INTEGER_TYPE.  Note
1530          that we do not want to call build_range_type since we would
1531          like each subtype node to be distinct.  This will be important
1532          when memory aliasing is implemented.
1533
1534          The TREE_TYPE field of the INTEGER_TYPE we make points to the
1535          parent type; this fact is used by the arithmetic conversion
1536          functions.
1537
1538          We elaborate the Ancestor_Subtype if it is not in the current
1539          unit and one of our bounds is non-static.  We do this to ensure
1540          consistent naming in the case where several subtypes share the same
1541          bounds by always elaborating the first such subtype first, thus
1542          using its name.  */
1543
1544       if (!definition
1545           && Present (Ancestor_Subtype (gnat_entity))
1546           && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1547           && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1548               || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1549         gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity), gnu_expr, 0);
1550
1551       gnu_type = make_node (INTEGER_TYPE);
1552       TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1553
1554       /* Set the precision to the Esize except for bit-packed arrays and
1555          subtypes of Standard.Boolean.  */
1556       if (Is_Packed_Array_Type (gnat_entity)
1557           && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1558         esize = UI_To_Int (RM_Size (gnat_entity));
1559       else if (TREE_CODE (TREE_TYPE (gnu_type)) == BOOLEAN_TYPE)
1560         esize = 1;
1561
1562       TYPE_PRECISION (gnu_type) = esize;
1563
1564       TYPE_MIN_VALUE (gnu_type)
1565         = convert (TREE_TYPE (gnu_type),
1566                    elaborate_expression (Type_Low_Bound (gnat_entity),
1567                                          gnat_entity,
1568                                          get_identifier ("L"), definition, 1,
1569                                          Needs_Debug_Info (gnat_entity)));
1570
1571       TYPE_MAX_VALUE (gnu_type)
1572         = convert (TREE_TYPE (gnu_type),
1573                    elaborate_expression (Type_High_Bound (gnat_entity),
1574                                          gnat_entity,
1575                                          get_identifier ("U"), definition, 1,
1576                                          Needs_Debug_Info (gnat_entity)));
1577
1578       /* One of the above calls might have caused us to be elaborated,
1579          so don't blow up if so.  */
1580       if (present_gnu_tree (gnat_entity))
1581         {
1582           maybe_present = true;
1583           break;
1584         }
1585
1586       TYPE_BIASED_REPRESENTATION_P (gnu_type)
1587         = Has_Biased_Representation (gnat_entity);
1588
1589      /* This should be an unsigned type if the lower bound is constant
1590          and non-negative or if the base type is unsigned; a signed type
1591          otherwise.    */
1592       TYPE_UNSIGNED (gnu_type)
1593         = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1594            || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1595                && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1596            || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1597            || Is_Unsigned_Type (gnat_entity));
1598
1599       layout_type (gnu_type);
1600
1601       /* Inherit our alias set from what we're a subtype of.  Subtypes
1602          are not different types and a pointer can designate any instance
1603          within a subtype hierarchy.  */
1604       relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1605
1606       /* If the type we are dealing with represents a bit-packed array,
1607          we need to have the bits left justified on big-endian targets
1608          and right justified on little-endian targets.  We also need to
1609          ensure that when the value is read (e.g. for comparison of two
1610          such values), we only get the good bits, since the unused bits
1611          are uninitialized.  Both goals are accomplished by wrapping up
1612          the modular type in an enclosing record type.  */
1613       if (Is_Packed_Array_Type (gnat_entity)
1614           && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1615         {
1616           tree gnu_field_type, gnu_field;
1617
1618           /* Set the RM size before wrapping up the type.  */
1619           TYPE_RM_SIZE (gnu_type)
1620             = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1621           TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1622           gnu_field_type = gnu_type;
1623
1624           gnu_type = make_node (RECORD_TYPE);
1625           TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1626
1627           /* Propagate the alignment of the modular type to the record.
1628              This means that bit-packed arrays have "ceil" alignment for
1629              their size, which may seem counter-intuitive but makes it
1630              possible to easily overlay them on modular types.  */
1631           TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1632           TYPE_PACKED (gnu_type) = 1;
1633
1634           /* Create a stripped-down declaration of the original type, mainly
1635              for debugging.  */
1636           create_type_decl (gnu_entity_id, gnu_field_type, NULL, true,
1637                             debug_info_p, gnat_entity);
1638
1639           /* Don't notify the field as "addressable", since we won't be taking
1640              it's address and it would prevent create_field_decl from making a
1641              bitfield.  */
1642           gnu_field = create_field_decl (get_identifier ("OBJECT"),
1643                                          gnu_field_type, gnu_type, 1, 0, 0, 0);
1644
1645           finish_record_type (gnu_type, gnu_field, 0, false);
1646           TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1647
1648           relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1649         }
1650
1651       /* If the type we are dealing with has got a smaller alignment than the
1652          natural one, we need to wrap it up in a record type and under-align
1653          the latter.  We reuse the padding machinery for this purpose.  */
1654       else if (Known_Alignment (gnat_entity)
1655                && UI_Is_In_Int_Range (Alignment (gnat_entity))
1656                && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1657                && align < TYPE_ALIGN (gnu_type))
1658         {
1659           tree gnu_field_type, gnu_field;
1660
1661           /* Set the RM size before wrapping up the type.  */
1662           TYPE_RM_SIZE (gnu_type)
1663             = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1664           gnu_field_type = gnu_type;
1665
1666           gnu_type = make_node (RECORD_TYPE);
1667           TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1668
1669           TYPE_ALIGN (gnu_type) = align;
1670           TYPE_PACKED (gnu_type) = 1;
1671
1672           /* Create a stripped-down declaration of the original type, mainly
1673              for debugging.  */
1674           create_type_decl (gnu_entity_id, gnu_field_type, NULL, true,
1675                             debug_info_p, gnat_entity);
1676
1677           /* Don't notify the field as "addressable", since we won't be taking
1678              it's address and it would prevent create_field_decl from making a
1679              bitfield.  */
1680           gnu_field = create_field_decl (get_identifier ("OBJECT"),
1681                                          gnu_field_type, gnu_type, 1, 0, 0, 0);
1682
1683           finish_record_type (gnu_type, gnu_field, 0, false);
1684           TYPE_IS_PADDING_P (gnu_type) = 1;
1685
1686           relate_alias_sets (gnu_type, gnu_field_type, ALIAS_SET_COPY);
1687         }
1688
1689       /* Otherwise reset the alignment lest we computed it above.  */
1690       else
1691         align = 0;
1692
1693       break;
1694
1695     case E_Floating_Point_Type:
1696       /* If this is a VAX floating-point type, use an integer of the proper
1697          size.  All the operations will be handled with ASM statements.  */
1698       if (Vax_Float (gnat_entity))
1699         {
1700           gnu_type = make_signed_type (esize);
1701           TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1702           SET_TYPE_DIGITS_VALUE (gnu_type,
1703                                  UI_To_gnu (Digits_Value (gnat_entity),
1704                                             sizetype));
1705           break;
1706         }
1707
1708       /* The type of the Low and High bounds can be our type if this is
1709          a type from Standard, so set them at the end of the function.  */
1710       gnu_type = make_node (REAL_TYPE);
1711       TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1712       layout_type (gnu_type);
1713       break;
1714
1715     case E_Floating_Point_Subtype:
1716       if (Vax_Float (gnat_entity))
1717         {
1718           gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1719           break;
1720         }
1721
1722       {
1723         if (!definition
1724             && Present (Ancestor_Subtype (gnat_entity))
1725             && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1726             && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1727                 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1728           gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1729                               gnu_expr, 0);
1730
1731         gnu_type = make_node (REAL_TYPE);
1732         TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1733         TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1734
1735         TYPE_MIN_VALUE (gnu_type)
1736           = convert (TREE_TYPE (gnu_type),
1737                      elaborate_expression (Type_Low_Bound (gnat_entity),
1738                                            gnat_entity, get_identifier ("L"),
1739                                            definition, 1,
1740                                            Needs_Debug_Info (gnat_entity)));
1741
1742         TYPE_MAX_VALUE (gnu_type)
1743           = convert (TREE_TYPE (gnu_type),
1744                      elaborate_expression (Type_High_Bound (gnat_entity),
1745                                            gnat_entity, get_identifier ("U"),
1746                                            definition, 1,
1747                                            Needs_Debug_Info (gnat_entity)));
1748
1749         /* One of the above calls might have caused us to be elaborated,
1750            so don't blow up if so.  */
1751         if (present_gnu_tree (gnat_entity))
1752           {
1753             maybe_present = true;
1754             break;
1755           }
1756
1757         layout_type (gnu_type);
1758
1759         /* Inherit our alias set from what we're a subtype of, as for
1760            integer subtypes.  */
1761         relate_alias_sets (gnu_type, TREE_TYPE (gnu_type), ALIAS_SET_COPY);
1762       }
1763     break;
1764
1765       /* Array and String Types and Subtypes
1766
1767          Unconstrained array types are represented by E_Array_Type and
1768          constrained array types are represented by E_Array_Subtype.  There
1769          are no actual objects of an unconstrained array type; all we have
1770          are pointers to that type.
1771
1772          The following fields are defined on array types and subtypes:
1773
1774                 Component_Type     Component type of the array.
1775                 Number_Dimensions  Number of dimensions (an int).
1776                 First_Index        Type of first index.  */
1777
1778     case E_String_Type:
1779     case E_Array_Type:
1780       {
1781         tree gnu_template_fields = NULL_TREE;
1782         tree gnu_template_type = make_node (RECORD_TYPE);
1783         tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1784         tree gnu_fat_type = make_node (RECORD_TYPE);
1785         int ndim = Number_Dimensions (gnat_entity);
1786         int firstdim
1787           = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1788         int nextdim
1789           = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1790         int index;
1791         tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1792         tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1793         tree gnu_comp_size = 0;
1794         tree gnu_max_size = size_one_node;
1795         tree gnu_max_size_unit;
1796         Entity_Id gnat_ind_subtype;
1797         Entity_Id gnat_ind_base_subtype;
1798         tree gnu_template_reference;
1799         tree tem;
1800
1801         TYPE_NAME (gnu_template_type)
1802           = create_concat_name (gnat_entity, "XUB");
1803
1804         /* Make a node for the array.  If we are not defining the array
1805            suppress expanding incomplete types.  */
1806         gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1807
1808         if (!definition)
1809           defer_incomplete_level++, this_deferred = true;
1810
1811         /* Build the fat pointer type.  Use a "void *" object instead of
1812            a pointer to the array type since we don't have the array type
1813            yet (it will reference the fat pointer via the bounds).  */
1814         tem = chainon (chainon (NULL_TREE,
1815                                 create_field_decl (get_identifier ("P_ARRAY"),
1816                                                    ptr_void_type_node,
1817                                                    gnu_fat_type, 0, 0, 0, 0)),
1818                        create_field_decl (get_identifier ("P_BOUNDS"),
1819                                           gnu_ptr_template,
1820                                           gnu_fat_type, 0, 0, 0, 0));
1821
1822         /* Make sure we can put this into a register.  */
1823         TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1824
1825         /* Do not finalize this record type since the types of its fields
1826            are still incomplete at this point.  */
1827         finish_record_type (gnu_fat_type, tem, 0, true);
1828         TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1829
1830         /* Build a reference to the template from a PLACEHOLDER_EXPR that
1831            is the fat pointer.  This will be used to access the individual
1832            fields once we build them.  */
1833         tem = build3 (COMPONENT_REF, gnu_ptr_template,
1834                       build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1835                       TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1836         gnu_template_reference
1837           = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1838         TREE_READONLY (gnu_template_reference) = 1;
1839
1840         /* Now create the GCC type for each index and add the fields for
1841            that index to the template.  */
1842         for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1843              gnat_ind_base_subtype
1844                = First_Index (Implementation_Base_Type (gnat_entity));
1845              index < ndim && index >= 0;
1846              index += nextdim,
1847              gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1848              gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1849           {
1850             char field_name[10];
1851             tree gnu_ind_subtype
1852               = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1853             tree gnu_base_subtype
1854               = get_unpadded_type (Etype (gnat_ind_base_subtype));
1855             tree gnu_base_min
1856               = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1857             tree gnu_base_max
1858               = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1859             tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1860
1861             /* Make the FIELD_DECLs for the minimum and maximum of this
1862                type and then make extractions of that field from the
1863                template.  */
1864             sprintf (field_name, "LB%d", index);
1865             gnu_min_field = create_field_decl (get_identifier (field_name),
1866                                                gnu_ind_subtype,
1867                                                gnu_template_type, 0, 0, 0, 0);
1868             field_name[0] = 'U';
1869             gnu_max_field = create_field_decl (get_identifier (field_name),
1870                                                gnu_ind_subtype,
1871                                                gnu_template_type, 0, 0, 0, 0);
1872
1873             Sloc_to_locus (Sloc (gnat_entity),
1874                            &DECL_SOURCE_LOCATION (gnu_min_field));
1875             Sloc_to_locus (Sloc (gnat_entity),
1876                            &DECL_SOURCE_LOCATION (gnu_max_field));
1877             gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1878
1879             /* We can't use build_component_ref here since the template
1880                type isn't complete yet.  */
1881             gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1882                               gnu_template_reference, gnu_min_field,
1883                               NULL_TREE);
1884             gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1885                               gnu_template_reference, gnu_max_field,
1886                               NULL_TREE);
1887             TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1888
1889             /* Make a range type with the new ranges, but using
1890                the Ada subtype.  Then we convert to sizetype.  */
1891             gnu_index_types[index]
1892               = create_index_type (convert (sizetype, gnu_min),
1893                                    convert (sizetype, gnu_max),
1894                                    build_range_type (gnu_ind_subtype,
1895                                                      gnu_min, gnu_max),
1896                                    gnat_entity);
1897             /* Update the maximum size of the array, in elements.  */
1898             gnu_max_size
1899               = size_binop (MULT_EXPR, gnu_max_size,
1900                             size_binop (PLUS_EXPR, size_one_node,
1901                                         size_binop (MINUS_EXPR, gnu_base_max,
1902                                                     gnu_base_min)));
1903
1904             TYPE_NAME (gnu_index_types[index])
1905               = create_concat_name (gnat_entity, field_name);
1906           }
1907
1908         for (index = 0; index < ndim; index++)
1909           gnu_template_fields
1910             = chainon (gnu_template_fields, gnu_temp_fields[index]);
1911
1912         /* Install all the fields into the template.  */
1913         finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1914         TYPE_READONLY (gnu_template_type) = 1;
1915
1916         /* Now make the array of arrays and update the pointer to the array
1917            in the fat pointer.  Note that it is the first field.  */
1918         tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1919
1920         /* Try to get a smaller form of the component if needed.  */
1921         if ((Is_Packed (gnat_entity)
1922              || Has_Component_Size_Clause (gnat_entity))
1923             && !Is_Bit_Packed_Array (gnat_entity)
1924             && !Has_Aliased_Components (gnat_entity)
1925             && !Strict_Alignment (Component_Type (gnat_entity))
1926             && TREE_CODE (tem) == RECORD_TYPE
1927             && !TYPE_IS_FAT_POINTER_P (tem)
1928             && host_integerp (TYPE_SIZE (tem), 1))
1929           tem = make_packable_type (tem, false);
1930
1931         if (Has_Atomic_Components (gnat_entity))
1932           check_ok_for_atomic (tem, gnat_entity, true);
1933
1934         /* Get and validate any specified Component_Size, but if Packed,
1935            ignore it since the front end will have taken care of it.  */
1936         gnu_comp_size
1937           = validate_size (Component_Size (gnat_entity), tem,
1938                            gnat_entity,
1939                            (Is_Bit_Packed_Array (gnat_entity)
1940                             ? TYPE_DECL : VAR_DECL),
1941                            true, Has_Component_Size_Clause (gnat_entity));
1942
1943         /* If the component type is a RECORD_TYPE that has a self-referential
1944            size, use the maximum size.  */
1945         if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1946             && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1947           gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1948
1949         if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1950           {
1951             tree orig_tem;
1952             tem = make_type_from_size (tem, gnu_comp_size, false);
1953             orig_tem = tem;
1954             tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1955                                   "C_PAD", false, definition, true);
1956             /* If a padding record was made, declare it now since it will
1957                never be declared otherwise.  This is necessary to ensure
1958                that its subtrees are properly marked.  */
1959             if (tem != orig_tem)
1960               create_type_decl (TYPE_NAME (tem), tem, NULL, true,
1961                                 debug_info_p, gnat_entity);
1962           }
1963
1964         if (Has_Volatile_Components (gnat_entity))
1965           tem = build_qualified_type (tem,
1966                                       TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1967
1968         /* If Component_Size is not already specified, annotate it with the
1969            size of the component.  */
1970         if (Unknown_Component_Size (gnat_entity))
1971           Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1972
1973         gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1974                                         size_binop (MULT_EXPR, gnu_max_size,
1975                                                     TYPE_SIZE_UNIT (tem)));
1976         gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
1977                                    size_binop (MULT_EXPR,
1978                                                convert (bitsizetype,
1979                                                         gnu_max_size),
1980                                                TYPE_SIZE (tem)));
1981
1982         for (index = ndim - 1; index >= 0; index--)
1983           {
1984             tem = build_array_type (tem, gnu_index_types[index]);
1985             TYPE_MULTI_ARRAY_P (tem) = (index > 0);
1986             if (array_type_has_nonaliased_component (gnat_entity, tem))
1987               TYPE_NONALIASED_COMPONENT (tem) = 1;
1988           }
1989
1990         /* If an alignment is specified, use it if valid.  But ignore it for
1991            types that represent the unpacked base type for packed arrays.  If
1992            the alignment was requested with an explicit user alignment clause,
1993            state so.  */
1994         if (No (Packed_Array_Type (gnat_entity))
1995             && Known_Alignment (gnat_entity))
1996           {
1997             gcc_assert (Present (Alignment (gnat_entity)));
1998             TYPE_ALIGN (tem)
1999               = validate_alignment (Alignment (gnat_entity), gnat_entity,
2000                                     TYPE_ALIGN (tem));
2001             if (Present (Alignment_Clause (gnat_entity)))
2002               TYPE_USER_ALIGN (tem) = 1;
2003           }
2004
2005         TYPE_CONVENTION_FORTRAN_P (tem)
2006           = (Convention (gnat_entity) == Convention_Fortran);
2007         TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2008
2009         /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2010            corresponding fat pointer.  */
2011         TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2012           = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2013         SET_TYPE_MODE (gnu_type, BLKmode);
2014         TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2015         SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2016
2017         /* If the maximum size doesn't overflow, use it.  */
2018         if (TREE_CODE (gnu_max_size) == INTEGER_CST
2019             && !TREE_OVERFLOW (gnu_max_size))
2020           TYPE_SIZE (tem)
2021             = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
2022         if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2023             && !TREE_OVERFLOW (gnu_max_size_unit))
2024           TYPE_SIZE_UNIT (tem)
2025             = size_binop (MIN_EXPR, gnu_max_size_unit,
2026                           TYPE_SIZE_UNIT (tem));
2027
2028         create_type_decl (create_concat_name (gnat_entity, "XUA"),
2029                           tem, NULL, !Comes_From_Source (gnat_entity),
2030                           debug_info_p, gnat_entity);
2031
2032         /* Give the fat pointer type a name.  */
2033         create_type_decl (create_concat_name (gnat_entity, "XUP"),
2034                           gnu_fat_type, NULL, true,
2035                           debug_info_p, gnat_entity);
2036
2037        /* Create the type to be used as what a thin pointer designates: an
2038           record type for the object and its template with the field offsets
2039           shifted to have the template at a negative offset.  */
2040         tem = build_unc_object_type (gnu_template_type, tem,
2041                                      create_concat_name (gnat_entity, "XUT"));
2042         shift_unc_components_for_thin_pointers (tem);
2043
2044         SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2045         TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2046
2047         /* Give the thin pointer type a name.  */
2048         create_type_decl (create_concat_name (gnat_entity, "XUX"),
2049                           build_pointer_type (tem), NULL, true,
2050                           debug_info_p, gnat_entity);
2051       }
2052       break;
2053
2054     case E_String_Subtype:
2055     case E_Array_Subtype:
2056
2057       /* This is the actual data type for array variables.  Multidimensional
2058          arrays are implemented in the gnu tree as arrays of arrays.  Note
2059          that for the moment arrays which have sparse enumeration subtypes as
2060          index components create sparse arrays, which is obviously space
2061          inefficient but so much easier to code for now.
2062
2063          Also note that the subtype never refers to the unconstrained
2064          array type, which is somewhat at variance with Ada semantics.
2065
2066          First check to see if this is simply a renaming of the array
2067          type.  If so, the result is the array type.  */
2068
2069       gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2070       if (!Is_Constrained (gnat_entity))
2071         break;
2072       else
2073         {
2074           int index;
2075           int array_dim = Number_Dimensions (gnat_entity);
2076           int first_dim
2077             = ((Convention (gnat_entity) == Convention_Fortran)
2078                ? array_dim - 1 : 0);
2079           int next_dim
2080             = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
2081           Entity_Id gnat_ind_subtype;
2082           Entity_Id gnat_ind_base_subtype;
2083           tree gnu_base_type = gnu_type;
2084           tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
2085           tree gnu_comp_size = NULL_TREE;
2086           tree gnu_max_size = size_one_node;
2087           tree gnu_max_size_unit;
2088           bool need_index_type_struct = false;
2089           bool max_overflow = false;
2090
2091           /* First create the gnu types for each index.  Create types for
2092              debugging information to point to the index types if the
2093              are not integer types, have variable bounds, or are
2094              wider than sizetype.  */
2095
2096           for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
2097                gnat_ind_base_subtype
2098                  = First_Index (Implementation_Base_Type (gnat_entity));
2099                index < array_dim && index >= 0;
2100                index += next_dim,
2101                gnat_ind_subtype = Next_Index (gnat_ind_subtype),
2102                gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
2103             {
2104               tree gnu_index_subtype
2105                 = get_unpadded_type (Etype (gnat_ind_subtype));
2106               tree gnu_min
2107                 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
2108               tree gnu_max
2109                 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
2110               tree gnu_base_subtype
2111                 = get_unpadded_type (Etype (gnat_ind_base_subtype));
2112               tree gnu_base_min
2113                 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
2114               tree gnu_base_max
2115                 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
2116               tree gnu_base_type = get_base_type (gnu_base_subtype);
2117               tree gnu_base_base_min
2118                 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
2119               tree gnu_base_base_max
2120                 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
2121               tree gnu_high;
2122               tree gnu_this_max;
2123
2124               /* If the minimum and maximum values both overflow in
2125                  SIZETYPE, but the difference in the original type
2126                  does not overflow in SIZETYPE, ignore the overflow
2127                  indications.  */
2128               if ((TYPE_PRECISION (gnu_index_subtype)
2129                    > TYPE_PRECISION (sizetype)
2130                    || TYPE_UNSIGNED (gnu_index_subtype)
2131                       != TYPE_UNSIGNED (sizetype))
2132                   && TREE_CODE (gnu_min) == INTEGER_CST
2133                   && TREE_CODE (gnu_max) == INTEGER_CST
2134                   && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2135                   && (!TREE_OVERFLOW
2136                       (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2137                                     TYPE_MAX_VALUE (gnu_index_subtype),
2138                                     TYPE_MIN_VALUE (gnu_index_subtype)))))
2139                 {
2140                   TREE_OVERFLOW (gnu_min) = 0;
2141                   TREE_OVERFLOW (gnu_max) = 0;
2142                 }
2143
2144               /* Similarly, if the range is null, use bounds of 1..0 for
2145                  the sizetype bounds.  */
2146               else if ((TYPE_PRECISION (gnu_index_subtype)
2147                         > TYPE_PRECISION (sizetype)
2148                        || TYPE_UNSIGNED (gnu_index_subtype)
2149                           != TYPE_UNSIGNED (sizetype))
2150                        && TREE_CODE (gnu_min) == INTEGER_CST
2151                        && TREE_CODE (gnu_max) == INTEGER_CST
2152                        && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2153                        && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2154                                            TYPE_MIN_VALUE (gnu_index_subtype)))
2155                 gnu_min = size_one_node, gnu_max = size_zero_node;
2156
2157               /* Now compute the size of this bound.  We need to provide
2158                  GCC with an upper bound to use but have to deal with the
2159                  "superflat" case.  There are three ways to do this.  If we
2160                  can prove that the array can never be superflat, we can
2161                  just use the high bound of the index subtype.  If we can
2162                  prove that the low bound minus one can't overflow, we
2163                  can do this as MAX (hb, lb - 1).  Otherwise, we have to use
2164                  the expression hb >= lb ? hb : lb - 1.  */
2165               gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2166
2167               /* See if the base array type is already flat.  If it is, we
2168                  are probably compiling an ACVC test, but it will cause the
2169                  code below to malfunction if we don't handle it specially.  */
2170               if (TREE_CODE (gnu_base_min) == INTEGER_CST
2171                   && TREE_CODE (gnu_base_max) == INTEGER_CST
2172                   && !TREE_OVERFLOW (gnu_base_min)
2173                   && !TREE_OVERFLOW (gnu_base_max)
2174                   && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2175                 gnu_high = size_zero_node, gnu_min = size_one_node;
2176
2177               /* If gnu_high is now an integer which overflowed, the array
2178                  cannot be superflat.  */
2179               else if (TREE_CODE (gnu_high) == INTEGER_CST
2180                        && TREE_OVERFLOW (gnu_high))
2181                 gnu_high = gnu_max;
2182               else if (TYPE_UNSIGNED (gnu_base_subtype)
2183                        || TREE_CODE (gnu_high) == INTEGER_CST)
2184                 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2185               else
2186                 gnu_high
2187                   = build_cond_expr
2188                     (sizetype, build_binary_op (GE_EXPR, integer_type_node,
2189                                                 gnu_max, gnu_min),
2190                      gnu_max, gnu_high);
2191
2192               gnu_index_type[index]
2193                 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2194                                      gnat_entity);
2195
2196               /* Also compute the maximum size of the array.  Here we
2197                  see if any constraint on the index type of the base type
2198                  can be used in the case of self-referential bound on
2199                  the index type of the subtype.  We look for a non-"infinite"
2200                  and non-self-referential bound from any type involved and
2201                  handle each bound separately.  */
2202
2203               if ((TREE_CODE (gnu_min) == INTEGER_CST
2204                    && !TREE_OVERFLOW (gnu_min)
2205                    && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2206                   || !CONTAINS_PLACEHOLDER_P (gnu_min)
2207                   || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2208                        && !TREE_OVERFLOW (gnu_base_min)))
2209                 gnu_base_min = gnu_min;
2210
2211               if ((TREE_CODE (gnu_max) == INTEGER_CST
2212                    && !TREE_OVERFLOW (gnu_max)
2213                    && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2214                   || !CONTAINS_PLACEHOLDER_P (gnu_max)
2215                   || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2216                        && !TREE_OVERFLOW (gnu_base_max)))
2217                 gnu_base_max = gnu_max;
2218
2219               if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2220                    && TREE_OVERFLOW (gnu_base_min))
2221                   || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2222                   || (TREE_CODE (gnu_base_max) == INTEGER_CST
2223                       && TREE_OVERFLOW (gnu_base_max))
2224                   || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2225                 max_overflow = true;
2226
2227               gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2228               gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2229
2230               gnu_this_max
2231                 = size_binop (MAX_EXPR,
2232                               size_binop (PLUS_EXPR, size_one_node,
2233                                           size_binop (MINUS_EXPR, gnu_base_max,
2234                                                       gnu_base_min)),
2235                               size_zero_node);
2236
2237               if (TREE_CODE (gnu_this_max) == INTEGER_CST
2238                   && TREE_OVERFLOW (gnu_this_max))
2239                 max_overflow = true;
2240
2241               gnu_max_size
2242                 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2243
2244               if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2245                   || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2246                       != INTEGER_CST)
2247                   || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2248                   || (TREE_TYPE (gnu_index_subtype)
2249                       && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2250                           != INTEGER_TYPE))
2251                   || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2252                   || (TYPE_PRECISION (gnu_index_subtype)
2253                       > TYPE_PRECISION (sizetype)))
2254                 need_index_type_struct = true;
2255             }
2256
2257           /* Then flatten: create the array of arrays.  For an array type
2258              used to implement a packed array, get the component type from
2259              the original array type since the representation clauses that
2260              can affect it are on the latter.  */
2261           if (Is_Packed_Array_Type (gnat_entity)
2262               && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2263             {
2264               gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2265               for (index = array_dim - 1; index >= 0; index--)
2266                 gnu_type = TREE_TYPE (gnu_type);
2267
2268               /* One of the above calls might have caused us to be elaborated,
2269                  so don't blow up if so.  */
2270               if (present_gnu_tree (gnat_entity))
2271                 {
2272                   maybe_present = true;
2273                   break;
2274                 }
2275             }
2276           else
2277             {
2278               gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2279
2280               /* One of the above calls might have caused us to be elaborated,
2281                  so don't blow up if so.  */
2282               if (present_gnu_tree (gnat_entity))
2283                 {
2284                   maybe_present = true;
2285                   break;
2286                 }
2287
2288               /* Try to get a smaller form of the component if needed.  */
2289               if ((Is_Packed (gnat_entity)
2290                    || Has_Component_Size_Clause (gnat_entity))
2291                   && !Is_Bit_Packed_Array (gnat_entity)
2292                   && !Has_Aliased_Components (gnat_entity)
2293                   && !Strict_Alignment (Component_Type (gnat_entity))
2294                   && TREE_CODE (gnu_type) == RECORD_TYPE
2295                   && !TYPE_IS_FAT_POINTER_P (gnu_type)
2296                   && host_integerp (TYPE_SIZE (gnu_type), 1))
2297                 gnu_type = make_packable_type (gnu_type, false);
2298
2299               /* Get and validate any specified Component_Size, but if Packed,
2300                  ignore it since the front end will have taken care of it.  */
2301               gnu_comp_size
2302                 = validate_size (Component_Size (gnat_entity), gnu_type,
2303                                  gnat_entity,
2304                                  (Is_Bit_Packed_Array (gnat_entity)
2305                                   ? TYPE_DECL : VAR_DECL), true,
2306                                  Has_Component_Size_Clause (gnat_entity));
2307
2308               /* If the component type is a RECORD_TYPE that has a
2309                  self-referential size, use the maximum size.  */
2310               if (!gnu_comp_size
2311                   && TREE_CODE (gnu_type) == RECORD_TYPE
2312                   && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2313                 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2314
2315               if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2316                 {
2317                   tree orig_gnu_type;
2318                   gnu_type
2319                     = make_type_from_size (gnu_type, gnu_comp_size, false);
2320                   orig_gnu_type = gnu_type;
2321                   gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2322                                              gnat_entity, "C_PAD", false,
2323                                              definition, true);
2324                   /* If a padding record was made, declare it now since it
2325                      will never be declared otherwise.  This is necessary
2326                      to ensure that its subtrees are properly marked.  */
2327                   if (gnu_type != orig_gnu_type)
2328                     create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2329                                       true, debug_info_p, gnat_entity);
2330                 }
2331
2332               if (Has_Volatile_Components (Base_Type (gnat_entity)))
2333                 gnu_type = build_qualified_type (gnu_type,
2334                                                  (TYPE_QUALS (gnu_type)
2335                                                   | TYPE_QUAL_VOLATILE));
2336             }
2337
2338           gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2339                                           TYPE_SIZE_UNIT (gnu_type));
2340           gnu_max_size = size_binop (MULT_EXPR,
2341                                      convert (bitsizetype, gnu_max_size),
2342                                      TYPE_SIZE (gnu_type));
2343
2344           for (index = array_dim - 1; index >= 0; index --)
2345             {
2346               gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2347               TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2348               if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2349                 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2350             }
2351
2352           /* Attach the TYPE_STUB_DECL in case we have a parallel type.  */
2353           if (need_index_type_struct)
2354             TYPE_STUB_DECL (gnu_type)
2355               = create_type_stub_decl (gnu_entity_id, gnu_type);
2356
2357           /* If we are at file level and this is a multi-dimensional array, we
2358              need to make a variable corresponding to the stride of the
2359              inner dimensions.   */
2360           if (global_bindings_p () && array_dim > 1)
2361             {
2362               tree gnu_str_name = get_identifier ("ST");
2363               tree gnu_arr_type;
2364
2365               for (gnu_arr_type = TREE_TYPE (gnu_type);
2366                    TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2367                    gnu_arr_type = TREE_TYPE (gnu_arr_type),
2368                    gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2369                 {
2370                   tree eltype = TREE_TYPE (gnu_arr_type);
2371
2372                   TYPE_SIZE (gnu_arr_type)
2373                     = elaborate_expression_1 (gnat_entity, gnat_entity,
2374                                               TYPE_SIZE (gnu_arr_type),
2375                                               gnu_str_name, definition, 0);
2376
2377                   /* ??? For now, store the size as a multiple of the
2378                      alignment of the element type in bytes so that we
2379                      can see the alignment from the tree.  */
2380                   TYPE_SIZE_UNIT (gnu_arr_type)
2381                     = build_binary_op
2382                       (MULT_EXPR, sizetype,
2383                        elaborate_expression_1
2384                        (gnat_entity, gnat_entity,
2385                         build_binary_op (EXACT_DIV_EXPR, sizetype,
2386                                          TYPE_SIZE_UNIT (gnu_arr_type),
2387                                          size_int (TYPE_ALIGN (eltype)
2388                                                    / BITS_PER_UNIT)),
2389                         concat_id_with_name (gnu_str_name, "A_U"),
2390                         definition, 0),
2391                        size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2392
2393                   /* ??? create_type_decl is not invoked on the inner types so
2394                      the MULT_EXPR node built above will never be marked.  */
2395                   mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2396                 }
2397             }
2398
2399           /* If we need to write out a record type giving the names of
2400              the bounds, do it now.  Make sure to reference the index
2401              types themselves, not just their names, as the debugger
2402              may fall back on them in some cases.  */
2403           if (need_index_type_struct && debug_info_p)
2404             {
2405               tree gnu_bound_rec = make_node (RECORD_TYPE);
2406               tree gnu_field_list = NULL_TREE;
2407               tree gnu_field;
2408
2409               TYPE_NAME (gnu_bound_rec)
2410                 = create_concat_name (gnat_entity, "XA");
2411
2412               for (index = array_dim - 1; index >= 0; index--)
2413                 {
2414                   tree gnu_index = TYPE_INDEX_TYPE (gnu_index_type[index]);
2415                   tree gnu_index_name = TYPE_NAME (gnu_index);
2416
2417                   if (TREE_CODE (gnu_index_name) == TYPE_DECL)
2418                     gnu_index_name = DECL_NAME (gnu_index_name);
2419
2420                   gnu_field = create_field_decl (gnu_index_name, gnu_index,
2421                                                  gnu_bound_rec,
2422                                                  0, NULL_TREE, NULL_TREE, 0);
2423                   TREE_CHAIN (gnu_field) = gnu_field_list;
2424                   gnu_field_list = gnu_field;
2425                 }
2426
2427               finish_record_type (gnu_bound_rec, gnu_field_list, 0, false);
2428               add_parallel_type (TYPE_STUB_DECL (gnu_type), gnu_bound_rec);
2429             }
2430
2431           TYPE_CONVENTION_FORTRAN_P (gnu_type)
2432             = (Convention (gnat_entity) == Convention_Fortran);
2433           TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2434             = (Is_Packed_Array_Type (gnat_entity)
2435                && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2436
2437           /* If our size depends on a placeholder and the maximum size doesn't
2438              overflow, use it.  */
2439           if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2440               && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2441                    && TREE_OVERFLOW (gnu_max_size))
2442               && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2443                    && TREE_OVERFLOW (gnu_max_size_unit))
2444               && !max_overflow)
2445             {
2446               TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2447                                                  TYPE_SIZE (gnu_type));
2448               TYPE_SIZE_UNIT (gnu_type)
2449                 = size_binop (MIN_EXPR, gnu_max_size_unit,
2450                               TYPE_SIZE_UNIT (gnu_type));
2451             }
2452
2453           /* Set our alias set to that of our base type.  This gives all
2454              array subtypes the same alias set.  */
2455           relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
2456         }
2457
2458       /* If this is a packed type, make this type the same as the packed
2459          array type, but do some adjusting in the type first.  */
2460       if (Present (Packed_Array_Type (gnat_entity)))
2461         {
2462           Entity_Id gnat_index;
2463           tree gnu_inner_type;
2464
2465           /* First finish the type we had been making so that we output
2466              debugging information for it.  */
2467           gnu_type
2468             = build_qualified_type (gnu_type,
2469                                     (TYPE_QUALS (gnu_type)
2470                                      | (TYPE_QUAL_VOLATILE
2471                                         * Treat_As_Volatile (gnat_entity))));
2472
2473           /* Make it artificial only if the base type was artificial as well.
2474              That's sort of "morally" true and will make it possible for the
2475              debugger to look it up by name in DWARF more easily.  */
2476           gnu_decl
2477             = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2478                                 !Comes_From_Source (gnat_entity)
2479                                 && !Comes_From_Source (Etype (gnat_entity)),
2480                                 debug_info_p, gnat_entity);
2481
2482           /* Save it as our equivalent in case the call below elaborates
2483              this type again.  */
2484           save_gnu_tree (gnat_entity, gnu_decl, false);
2485
2486           gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2487                                          NULL_TREE, 0);
2488           this_made_decl = true;
2489           gnu_type = TREE_TYPE (gnu_decl);
2490           save_gnu_tree (gnat_entity, NULL_TREE, false);
2491
2492           gnu_inner_type = gnu_type;
2493           while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2494                  && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2495                      || TYPE_IS_PADDING_P (gnu_inner_type)))
2496             gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2497
2498           /* We need to point the type we just made to our index type so
2499              the actual bounds can be put into a template.  */
2500
2501           if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2502                && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2503               || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2504                   && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2505             {
2506               if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2507                 {
2508                   /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2509                      If it is, we need to make another type.  */
2510                   if (TYPE_MODULAR_P (gnu_inner_type))
2511                     {
2512                       tree gnu_subtype;
2513
2514                       gnu_subtype = make_node (INTEGER_TYPE);
2515
2516                       TREE_TYPE (gnu_subtype) = gnu_inner_type;
2517                       TYPE_MIN_VALUE (gnu_subtype)
2518                         = TYPE_MIN_VALUE (gnu_inner_type);
2519                       TYPE_MAX_VALUE (gnu_subtype)
2520                         = TYPE_MAX_VALUE (gnu_inner_type);
2521                       TYPE_PRECISION (gnu_subtype)
2522                         = TYPE_PRECISION (gnu_inner_type);
2523                       TYPE_UNSIGNED (gnu_subtype)
2524                         = TYPE_UNSIGNED (gnu_inner_type);
2525                       TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2526                       layout_type (gnu_subtype);
2527
2528                       gnu_inner_type = gnu_subtype;
2529                     }
2530
2531                   TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2532                 }
2533
2534               SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2535
2536               for (gnat_index = First_Index (gnat_entity);
2537                    Present (gnat_index); gnat_index = Next_Index (gnat_index))
2538                 SET_TYPE_ACTUAL_BOUNDS
2539                   (gnu_inner_type,
2540                    tree_cons (NULL_TREE,
2541                               get_unpadded_type (Etype (gnat_index)),
2542                               TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2543
2544               if (Convention (gnat_entity) != Convention_Fortran)
2545                 SET_TYPE_ACTUAL_BOUNDS
2546                   (gnu_inner_type,
2547                    nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2548
2549               if (TREE_CODE (gnu_type) == RECORD_TYPE
2550                   && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2551                 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2552             }
2553         }
2554
2555       /* Abort if packed array with no packed array type field set.  */
2556       else
2557         gcc_assert (!Is_Packed (gnat_entity));
2558
2559       break;
2560
2561     case E_String_Literal_Subtype:
2562       /* Create the type for a string literal.  */
2563       {
2564         Entity_Id gnat_full_type
2565           = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2566              && Present (Full_View (Etype (gnat_entity)))
2567              ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2568         tree gnu_string_type = get_unpadded_type (gnat_full_type);
2569         tree gnu_string_array_type
2570           = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2571         tree gnu_string_index_type
2572           = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2573                                       (TYPE_DOMAIN (gnu_string_array_type))));
2574         tree gnu_lower_bound
2575           = convert (gnu_string_index_type,
2576                      gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2577         int length = UI_To_Int (String_Literal_Length (gnat_entity));
2578         tree gnu_length = ssize_int (length - 1);
2579         tree gnu_upper_bound
2580           = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2581                              gnu_lower_bound,
2582                              convert (gnu_string_index_type, gnu_length));
2583         tree gnu_range_type
2584           = build_range_type (gnu_string_index_type,
2585                               gnu_lower_bound, gnu_upper_bound);
2586         tree gnu_index_type
2587           = create_index_type (convert (sizetype,
2588                                         TYPE_MIN_VALUE (gnu_range_type)),
2589                                convert (sizetype,
2590                                         TYPE_MAX_VALUE (gnu_range_type)),
2591                                gnu_range_type, gnat_entity);
2592
2593         gnu_type
2594           = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2595                               gnu_index_type);
2596         if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2597           TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2598         relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2599       }
2600       break;
2601
2602     /* Record Types and Subtypes
2603
2604        The following fields are defined on record types:
2605
2606                 Has_Discriminants       True if the record has discriminants
2607                 First_Discriminant      Points to head of list of discriminants
2608                 First_Entity            Points to head of list of fields
2609                 Is_Tagged_Type          True if the record is tagged
2610
2611        Implementation of Ada records and discriminated records:
2612
2613        A record type definition is transformed into the equivalent of a C
2614        struct definition.  The fields that are the discriminants which are
2615        found in the Full_Type_Declaration node and the elements of the
2616        Component_List found in the Record_Type_Definition node.  The
2617        Component_List can be a recursive structure since each Variant of
2618        the Variant_Part of the Component_List has a Component_List.
2619
2620        Processing of a record type definition comprises starting the list of
2621        field declarations here from the discriminants and the calling the
2622        function components_to_record to add the rest of the fields from the
2623        component list and return the gnu type node.  The function
2624        components_to_record will call itself recursively as it traverses
2625        the tree.  */
2626
2627     case E_Record_Type:
2628       if (Has_Complex_Representation (gnat_entity))
2629         {
2630           gnu_type
2631             = build_complex_type
2632               (get_unpadded_type
2633                (Etype (Defining_Entity
2634                        (First (Component_Items
2635                                (Component_List
2636                                 (Type_Definition
2637                                  (Declaration_Node (gnat_entity)))))))));
2638
2639           break;
2640         }
2641
2642       {
2643         Node_Id full_definition = Declaration_Node (gnat_entity);
2644         Node_Id record_definition = Type_Definition (full_definition);
2645         Entity_Id gnat_field;
2646         tree gnu_field;
2647         tree gnu_field_list = NULL_TREE;
2648         tree gnu_get_parent;
2649         /* Set PACKED in keeping with gnat_to_gnu_field.  */
2650         int packed
2651           = Is_Packed (gnat_entity)
2652             ? 1
2653             : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2654               ? -1
2655               : (Known_Alignment (gnat_entity)
2656                  || (Strict_Alignment (gnat_entity)
2657                      && Known_Static_Esize (gnat_entity)))
2658                 ? -2
2659                 : 0;
2660         bool has_rep = Has_Specified_Layout (gnat_entity);
2661         bool all_rep = has_rep;
2662         bool is_extension
2663           = (Is_Tagged_Type (gnat_entity)
2664              && Nkind (record_definition) == N_Derived_Type_Definition);
2665
2666         /* See if all fields have a rep clause.  Stop when we find one
2667            that doesn't.  */
2668         for (gnat_field = First_Entity (gnat_entity);
2669              Present (gnat_field) && all_rep;
2670              gnat_field = Next_Entity (gnat_field))
2671           if ((Ekind (gnat_field) == E_Component
2672                || Ekind (gnat_field) == E_Discriminant)
2673               && No (Component_Clause (gnat_field)))
2674             all_rep = false;
2675
2676         /* If this is a record extension, go a level further to find the
2677            record definition.  Also, verify we have a Parent_Subtype.  */
2678         if (is_extension)
2679           {
2680             if (!type_annotate_only
2681                 || Present (Record_Extension_Part (record_definition)))
2682               record_definition = Record_Extension_Part (record_definition);
2683
2684             gcc_assert (type_annotate_only
2685                         || Present (Parent_Subtype (gnat_entity)));
2686           }
2687
2688         /* Make a node for the record.  If we are not defining the record,
2689            suppress expanding incomplete types.  */
2690         gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2691         TYPE_NAME (gnu_type) = gnu_entity_id;
2692         TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2693
2694         if (!definition)
2695           defer_incomplete_level++, this_deferred = true;
2696
2697         /* If both a size and rep clause was specified, put the size in
2698            the record type now so that it can get the proper mode.  */
2699         if (has_rep && Known_Esize (gnat_entity))
2700           TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2701
2702         /* Always set the alignment here so that it can be used to
2703            set the mode, if it is making the alignment stricter.  If
2704            it is invalid, it will be checked again below.  If this is to
2705            be Atomic, choose a default alignment of a word unless we know
2706            the size and it's smaller.  */
2707         if (Known_Alignment (gnat_entity))
2708           TYPE_ALIGN (gnu_type)
2709             = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2710         else if (Is_Atomic (gnat_entity))
2711           TYPE_ALIGN (gnu_type)
2712             = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2713         /* If a type needs strict alignment, the minimum size will be the
2714            type size instead of the RM size (see validate_size).  Cap the
2715            alignment, lest it causes this type size to become too large.  */
2716         else if (Strict_Alignment (gnat_entity)
2717                  && Known_Static_Esize (gnat_entity))
2718           {
2719             unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2720             unsigned int raw_align = raw_size & -raw_size;
2721             if (raw_align < BIGGEST_ALIGNMENT)
2722               TYPE_ALIGN (gnu_type) = raw_align;
2723           }
2724         else
2725           TYPE_ALIGN (gnu_type) = 0;
2726
2727         /* If we have a Parent_Subtype, make a field for the parent.  If
2728            this record has rep clauses, force the position to zero.  */
2729         if (Present (Parent_Subtype (gnat_entity)))
2730           {
2731             Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2732             tree gnu_parent;
2733
2734             /* A major complexity here is that the parent subtype will
2735                reference our discriminants in its Discriminant_Constraint
2736                list.  But those must reference the parent component of this
2737                record which is of the parent subtype we have not built yet!
2738                To break the circle we first build a dummy COMPONENT_REF which
2739                represents the "get to the parent" operation and initialize
2740                each of those discriminants to a COMPONENT_REF of the above
2741                dummy parent referencing the corresponding discriminant of the
2742                base type of the parent subtype.  */
2743             gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2744                                      build0 (PLACEHOLDER_EXPR, gnu_type),
2745                                      build_decl (FIELD_DECL, NULL_TREE,
2746                                                  void_type_node),
2747                                      NULL_TREE);
2748
2749             if (Has_Discriminants (gnat_entity))
2750               for (gnat_field = First_Stored_Discriminant (gnat_entity);
2751                    Present (gnat_field);
2752                    gnat_field = Next_Stored_Discriminant (gnat_field))
2753                 if (Present (Corresponding_Discriminant (gnat_field)))
2754                   save_gnu_tree
2755                     (gnat_field,
2756                      build3 (COMPONENT_REF,
2757                              get_unpadded_type (Etype (gnat_field)),
2758                              gnu_get_parent,
2759                              gnat_to_gnu_field_decl (Corresponding_Discriminant
2760                                                      (gnat_field)),
2761                              NULL_TREE),
2762                      true);
2763
2764             /* Then we build the parent subtype.  If it has discriminants but
2765                the type itself has unknown discriminants, this means that it
2766                doesn't contain information about how the discriminants are
2767                derived from those of the ancestor type, so it cannot be used
2768                directly.  Instead it is built by cloning the parent subtype
2769                of the underlying record view of the type, for which the above
2770                derivation of discriminants has been made explicit.  */
2771             if (Has_Discriminants (gnat_parent)
2772                 && Has_Unknown_Discriminants (gnat_entity))
2773               {
2774                 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2775
2776                 /* If we are defining the type, the underlying record
2777                    view must already have been elaborated at this point.
2778                    Otherwise do it now as its parent subtype cannot be
2779                    technically elaborated on its own.  */
2780                 if (definition)
2781                   gcc_assert (present_gnu_tree (gnat_uview));
2782                 else
2783                   gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2784
2785                 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2786
2787                 /* Substitute the "get to the parent" of the type for that
2788                    of its underlying record view in the cloned type.  */
2789                 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2790                      Present (gnat_field);
2791                      gnat_field = Next_Stored_Discriminant (gnat_field))
2792                   if (Present (Corresponding_Discriminant (gnat_field)))
2793                     {
2794                       tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2795                       tree gnu_ref
2796                         = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2797                                   gnu_get_parent, gnu_field, NULL_TREE);
2798                       gnu_parent
2799                         = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2800                     }
2801               }
2802             else
2803               gnu_parent = gnat_to_gnu_type (gnat_parent);
2804
2805             /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2806                initially built.  The discriminants must reference the fields
2807                of the parent subtype and not those of its base type for the
2808                placeholder machinery to properly work.  */
2809             if (Has_Discriminants (gnat_entity))
2810               for (gnat_field = First_Stored_Discriminant (gnat_entity);
2811                    Present (gnat_field);
2812                    gnat_field = Next_Stored_Discriminant (gnat_field))
2813                 if (Present (Corresponding_Discriminant (gnat_field)))
2814                   {
2815                     Entity_Id field = Empty;
2816                     for (field = First_Stored_Discriminant (gnat_parent);
2817                          Present (field);
2818                          field = Next_Stored_Discriminant (field))
2819                       if (same_discriminant_p (gnat_field, field))
2820                         break;
2821                     gcc_assert (Present (field));
2822                     TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2823                       = gnat_to_gnu_field_decl (field);
2824                   }
2825
2826             /* The "get to the parent" COMPONENT_REF must be given its
2827                proper type...  */
2828             TREE_TYPE (gnu_get_parent) = gnu_parent;
2829
2830             /* ...and reference the _parent field of this record.  */
2831             gnu_field_list
2832               = create_field_decl (get_identifier
2833                                    (Get_Name_String (Name_uParent)),
2834                                    gnu_parent, gnu_type, 0,
2835                                    has_rep ? TYPE_SIZE (gnu_parent) : 0,
2836                                    has_rep ? bitsize_zero_node : 0, 1);
2837             DECL_INTERNAL_P (gnu_field_list) = 1;
2838             TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2839           }
2840
2841         /* Make the fields for the discriminants and put them into the record
2842            unless it's an Unchecked_Union.  */
2843         if (Has_Discriminants (gnat_entity))
2844           for (gnat_field = First_Stored_Discriminant (gnat_entity);
2845                Present (gnat_field);
2846                gnat_field = Next_Stored_Discriminant (gnat_field))
2847             {
2848               /* If this is a record extension and this discriminant
2849                  is the renaming of another discriminant, we've already
2850                  handled the discriminant above.  */
2851               if (Present (Parent_Subtype (gnat_entity))
2852                   && Present (Corresponding_Discriminant (gnat_field)))
2853                 continue;
2854
2855               gnu_field
2856                 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2857
2858               /* Make an expression using a PLACEHOLDER_EXPR from the
2859                  FIELD_DECL node just created and link that with the
2860                  corresponding GNAT defining identifier.  Then add to the
2861                  list of fields.  */
2862               save_gnu_tree (gnat_field,
2863                              build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2864                                      build0 (PLACEHOLDER_EXPR,
2865                                              DECL_CONTEXT (gnu_field)),
2866                                      gnu_field, NULL_TREE),
2867                              true);
2868
2869               if (!Is_Unchecked_Union (gnat_entity))
2870                 {
2871                   TREE_CHAIN (gnu_field) = gnu_field_list;
2872                   gnu_field_list = gnu_field;
2873                 }
2874             }
2875
2876         /* Put the discriminants into the record (backwards), so we can
2877            know the appropriate discriminant to use for the names of the
2878            variants.  */
2879         TYPE_FIELDS (gnu_type) = gnu_field_list;
2880
2881         /* Add the listed fields into the record and finish it up.  */
2882         components_to_record (gnu_type, Component_List (record_definition),
2883                               gnu_field_list, packed, definition, NULL,
2884                               false, all_rep, false,
2885                               Is_Unchecked_Union (gnat_entity));
2886
2887         /* We used to remove the associations of the discriminants and
2888            _Parent for validity checking, but we may need them if there's
2889            Freeze_Node for a subtype used in this record.  */
2890         TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2891         TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2892
2893         /* If it is a tagged record force the type to BLKmode to insure
2894            that these objects will always be placed in memory.  Do the
2895            same thing for limited record types.  */
2896         if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2897           SET_TYPE_MODE (gnu_type, BLKmode);
2898
2899         /* Fill in locations of fields.  */
2900         annotate_rep (gnat_entity, gnu_type);
2901
2902         /* If there are any entities in the chain corresponding to
2903            components that we did not elaborate, ensure we elaborate their
2904            types if they are Itypes.  */
2905         for (gnat_temp = First_Entity (gnat_entity);
2906              Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2907           if ((Ekind (gnat_temp) == E_Component
2908                || Ekind (gnat_temp) == E_Discriminant)
2909               && Is_Itype (Etype (gnat_temp))
2910               && !present_gnu_tree (gnat_temp))
2911             gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2912       }
2913       break;
2914
2915     case E_Class_Wide_Subtype:
2916       /* If an equivalent type is present, that is what we should use.
2917          Otherwise, fall through to handle this like a record subtype
2918          since it may have constraints.  */
2919       if (gnat_equiv_type != gnat_entity)
2920         {
2921           gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2922           maybe_present = true;
2923           break;
2924         }
2925
2926       /* ... fall through ... */
2927
2928     case E_Record_Subtype:
2929
2930       /* If Cloned_Subtype is Present it means this record subtype has
2931          identical layout to that type or subtype and we should use
2932          that GCC type for this one.  The front end guarantees that
2933          the component list is shared.  */
2934       if (Present (Cloned_Subtype (gnat_entity)))
2935         {
2936           gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2937                                          NULL_TREE, 0);
2938           maybe_present = true;
2939         }
2940
2941       /* Otherwise, first ensure the base type is elaborated.  Then, if we are
2942          changing the type, make a new type with each field having the
2943          type of the field in the new subtype but having the position
2944          computed by transforming every discriminant reference according
2945          to the constraints.  We don't see any difference between
2946          private and nonprivate type here since derivations from types should
2947          have been deferred until the completion of the private type.  */
2948       else
2949         {
2950           Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2951           tree gnu_base_type;
2952           tree gnu_orig_type;
2953
2954           if (!definition)
2955             defer_incomplete_level++, this_deferred = true;
2956
2957           /* Get the base type initially for its alignment and sizes.  But
2958              if it is a padded type, we do all the other work with the
2959              unpadded type.  */
2960           gnu_base_type = gnat_to_gnu_type (gnat_base_type);
2961
2962           if (TREE_CODE (gnu_base_type) == RECORD_TYPE
2963               && TYPE_IS_PADDING_P (gnu_base_type))
2964             gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
2965           else
2966             gnu_type = gnu_orig_type = gnu_base_type;
2967
2968           if (present_gnu_tree (gnat_entity))
2969             {
2970               maybe_present = true;
2971               break;
2972             }
2973
2974           /* When the type has discriminants, and these discriminants
2975              affect the shape of what it built, factor them in.
2976
2977              If we are making a subtype of an Unchecked_Union (must be an
2978              Itype), just return the type.
2979
2980              We can't just use Is_Constrained because private subtypes without
2981              discriminants of full types with discriminants with default
2982              expressions are Is_Constrained but aren't constrained!  */
2983
2984           if (IN (Ekind (gnat_base_type), Record_Kind)
2985               && !Is_For_Access_Subtype (gnat_entity)
2986               && !Is_Unchecked_Union (gnat_base_type)
2987               && Is_Constrained (gnat_entity)
2988               && Stored_Constraint (gnat_entity) != No_Elist
2989               && Present (Discriminant_Constraint (gnat_entity)))
2990             {
2991               Entity_Id gnat_field;
2992               tree gnu_field_list = 0;
2993               tree gnu_pos_list
2994                 = compute_field_positions (gnu_orig_type, NULL_TREE,
2995                                            size_zero_node, bitsize_zero_node,
2996                                            BIGGEST_ALIGNMENT);
2997               tree gnu_subst_list
2998                 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2999                                      definition);
3000               tree gnu_temp;
3001
3002               gnu_type = make_node (RECORD_TYPE);
3003               TYPE_NAME (gnu_type) = gnu_entity_id;
3004               TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3005
3006               /* Set the size, alignment and alias set of the new type to
3007                  match that of the old one, doing required substitutions.
3008                  We do it this early because we need the size of the new
3009                  type below to discard old fields if necessary.  */
3010               TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3011               TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3012               SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3013               TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3014               relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
3015
3016               if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3017                 for (gnu_temp = gnu_subst_list;
3018                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3019                   TYPE_SIZE (gnu_type)
3020                     = substitute_in_expr (TYPE_SIZE (gnu_type),
3021                                           TREE_PURPOSE (gnu_temp),
3022                                           TREE_VALUE (gnu_temp));
3023
3024               if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3025                 for (gnu_temp = gnu_subst_list;
3026                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3027                   TYPE_SIZE_UNIT (gnu_type)
3028                     = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3029                                           TREE_PURPOSE (gnu_temp),
3030                                           TREE_VALUE (gnu_temp));
3031
3032               if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3033                 for (gnu_temp = gnu_subst_list;
3034                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3035                   SET_TYPE_ADA_SIZE
3036                     (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3037                                                    TREE_PURPOSE (gnu_temp),
3038                                                    TREE_VALUE (gnu_temp)));
3039
3040               for (gnat_field = First_Entity (gnat_entity);
3041                    Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3042                 if ((Ekind (gnat_field) == E_Component
3043                      || Ekind (gnat_field) == E_Discriminant)
3044                     && (Underlying_Type (Scope (Original_Record_Component
3045                                                 (gnat_field)))
3046                         == gnat_base_type)
3047                     && (No (Corresponding_Discriminant (gnat_field))
3048                         || !Is_Tagged_Type (gnat_base_type)))
3049                   {
3050                     tree gnu_old_field
3051                       = gnat_to_gnu_field_decl (Original_Record_Component
3052                                                 (gnat_field));
3053                     tree gnu_offset
3054                       = TREE_VALUE (purpose_member (gnu_old_field,
3055                                                     gnu_pos_list));
3056                     tree gnu_pos = TREE_PURPOSE (gnu_offset);
3057                     tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3058                     tree gnu_field_type
3059                       = gnat_to_gnu_type (Etype (gnat_field));
3060                     tree gnu_size = TYPE_SIZE (gnu_field_type);
3061                     tree gnu_new_pos = NULL_TREE;
3062                     unsigned int offset_align
3063                       = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
3064                                       1);
3065                     tree gnu_field;
3066
3067                     /* If there was a component clause, the field types must be
3068                        the same for the type and subtype, so copy the data from
3069                        the old field to avoid recomputation here.  Also if the
3070                        field is justified modular and the optimization in
3071                        gnat_to_gnu_field was applied.  */
3072                     if (Present (Component_Clause
3073                                  (Original_Record_Component (gnat_field)))
3074                         || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3075                             && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3076                             && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3077                                == TREE_TYPE (gnu_old_field)))
3078                       {
3079                         gnu_size = DECL_SIZE (gnu_old_field);
3080                         gnu_field_type = TREE_TYPE (gnu_old_field);
3081                       }
3082
3083                     /* If the old field was packed and of constant size, we
3084                        have to get the old size here, as it might differ from
3085                        what the Etype conveys and the latter might overlap
3086                        onto the following field.  Try to arrange the type for
3087                        possible better packing along the way.  */
3088                     else if (DECL_PACKED (gnu_old_field)
3089                              && TREE_CODE (DECL_SIZE (gnu_old_field))
3090                                 == INTEGER_CST)
3091                       {
3092                         gnu_size = DECL_SIZE (gnu_old_field);
3093                         if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3094                             && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
3095                             && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3096                           gnu_field_type
3097                             = make_packable_type (gnu_field_type, true);
3098                       }
3099
3100                     if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3101                       for (gnu_temp = gnu_subst_list;
3102                            gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3103                         gnu_pos = substitute_in_expr (gnu_pos,
3104                                                       TREE_PURPOSE (gnu_temp),
3105                                                       TREE_VALUE (gnu_temp));
3106
3107                     /* If the position is now a constant, we can set it as the
3108                        position of the field when we make it.  Otherwise, we need
3109                        to deal with it specially below.  */
3110                     if (TREE_CONSTANT (gnu_pos))
3111                       {
3112                         gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3113
3114                         /* Discard old fields that are outside the new type.
3115                            This avoids confusing code scanning it to decide
3116                            how to pass it to functions on some platforms.  */
3117                         if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3118                             && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3119                             && !integer_zerop (gnu_size)
3120                             && !tree_int_cst_lt (gnu_new_pos,
3121                                                  TYPE_SIZE (gnu_type)))
3122                           continue;
3123                       }
3124
3125                     gnu_field
3126                       = create_field_decl
3127                         (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3128                          DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3129                          !DECL_NONADDRESSABLE_P (gnu_old_field));
3130
3131                     if (!TREE_CONSTANT (gnu_pos))
3132                       {
3133                         normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3134                         DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3135                         DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3136                         SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3137                         DECL_SIZE (gnu_field) = gnu_size;
3138                         DECL_SIZE_UNIT (gnu_field)
3139                           = convert (sizetype,
3140                                      size_binop (CEIL_DIV_EXPR, gnu_size,
3141                                                  bitsize_unit_node));
3142                         layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3143                       }
3144
3145                     DECL_INTERNAL_P (gnu_field)
3146                       = DECL_INTERNAL_P (gnu_old_field);
3147                     SET_DECL_ORIGINAL_FIELD
3148                       (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3149                                    ? DECL_ORIGINAL_FIELD (gnu_old_field)
3150                                    : gnu_old_field));
3151                     DECL_DISCRIMINANT_NUMBER (gnu_field)
3152                       = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3153                     TREE_THIS_VOLATILE (gnu_field)
3154                       = TREE_THIS_VOLATILE (gnu_old_field);
3155
3156                     /* To match the layout crafted in components_to_record, if
3157                        this is the _Tag field, put it before any discriminants
3158                        instead of after them as for all other fields.  */
3159                     if (Chars (gnat_field) == Name_uTag)
3160                       gnu_field_list = chainon (gnu_field_list, gnu_field);
3161                     else
3162                       {
3163                         TREE_CHAIN (gnu_field) = gnu_field_list;
3164                         gnu_field_list = gnu_field;
3165                       }
3166
3167                     save_gnu_tree (gnat_field, gnu_field, false);
3168                   }
3169
3170               /* Now go through the entities again looking for Itypes that
3171                  we have not elaborated but should (e.g., Etypes of fields
3172                  that have Original_Components).  */
3173               for (gnat_field = First_Entity (gnat_entity);
3174                    Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3175                 if ((Ekind (gnat_field) == E_Discriminant
3176                      || Ekind (gnat_field) == E_Component)
3177                     && !present_gnu_tree (Etype (gnat_field)))
3178                   gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3179
3180               /* Do not finalize it since we're going to modify it below.  */
3181               gnu_field_list = nreverse (gnu_field_list);
3182               finish_record_type (gnu_type, gnu_field_list, 2, true);
3183
3184               /* Finalize size and mode.  */
3185               TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
3186               TYPE_SIZE_UNIT (gnu_type)
3187                 = variable_size (TYPE_SIZE_UNIT (gnu_type));
3188
3189               compute_record_mode (gnu_type);
3190
3191               /* Fill in locations of fields.  */
3192               annotate_rep (gnat_entity, gnu_type);
3193
3194               /* We've built a new type, make an XVS type to show what this
3195                  is a subtype of.  Some debuggers require the XVS type to be
3196                  output first, so do it in that order.  */
3197               if (debug_info_p)
3198                 {
3199                   tree gnu_subtype_marker = make_node (RECORD_TYPE);
3200                   tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
3201
3202                   if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
3203                     gnu_orig_name = DECL_NAME (gnu_orig_name);
3204
3205                   TYPE_NAME (gnu_subtype_marker)
3206                     = create_concat_name (gnat_entity, "XVS");
3207                   finish_record_type (gnu_subtype_marker,
3208                                       create_field_decl (gnu_orig_name,
3209                                                          integer_type_node,
3210                                                          gnu_subtype_marker,
3211                                                          0, NULL_TREE,
3212                                                          NULL_TREE, 0),
3213                                       0, false);
3214
3215                   add_parallel_type (TYPE_STUB_DECL (gnu_type),
3216                                      gnu_subtype_marker);
3217                 }
3218
3219               /* Now we can finalize it.  */
3220               rest_of_record_type_compilation (gnu_type);
3221             }
3222
3223           /* Otherwise, go down all the components in the new type and
3224              make them equivalent to those in the base type.  */
3225           else
3226             for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3227                  gnat_temp = Next_Entity (gnat_temp))
3228               if ((Ekind (gnat_temp) == E_Discriminant
3229                    && !Is_Unchecked_Union (gnat_base_type))
3230                   || Ekind (gnat_temp) == E_Component)
3231                 save_gnu_tree (gnat_temp,
3232                                gnat_to_gnu_field_decl
3233                                (Original_Record_Component (gnat_temp)), false);
3234         }
3235       break;
3236
3237     case E_Access_Subprogram_Type:
3238       /* Use the special descriptor type for dispatch tables if needed,
3239          that is to say for the Prim_Ptr of a-tags.ads and its clones.
3240          Note that we are only required to do so for static tables in
3241          order to be compatible with the C++ ABI, but Ada 2005 allows
3242          to extend library level tagged types at the local level so
3243          we do it in the non-static case as well.  */
3244       if (TARGET_VTABLE_USES_DESCRIPTORS
3245           && Is_Dispatch_Table_Entity (gnat_entity))
3246         {
3247             gnu_type = fdesc_type_node;
3248             gnu_size = TYPE_SIZE (gnu_type);
3249             break;
3250         }
3251
3252       /* ... fall through ... */
3253
3254     case E_Anonymous_Access_Subprogram_Type:
3255       /* If we are not defining this entity, and we have incomplete
3256          entities being processed above us, make a dummy type and
3257          fill it in later.  */
3258       if (!definition && defer_incomplete_level != 0)
3259         {
3260           struct incomplete *p
3261             = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3262
3263           gnu_type
3264             = build_pointer_type
3265               (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3266           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3267                                        !Comes_From_Source (gnat_entity),
3268                                        debug_info_p, gnat_entity);
3269           this_made_decl = tr