OSDN Git Service

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