OSDN Git Service

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