OSDN Git Service

* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Array_Type>: If an
[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 (Node_Id, Entity_Id, tree, tree,
132                                     bool, bool);
133 static tree make_packable_type (tree, bool);
134 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
135 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
136                                bool *);
137 static bool same_discriminant_p (Entity_Id, Entity_Id);
138 static bool array_type_has_nonaliased_component (Entity_Id, tree);
139 static bool compile_time_known_address_p (Node_Id);
140 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
141                                   bool, bool, bool, bool);
142 static Uint annotate_value (tree);
143 static void annotate_rep (Entity_Id, tree);
144 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
145 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
146 static void set_rm_size (Uint, tree, Entity_Id);
147 static tree make_type_from_size (tree, tree, bool);
148 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
149 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
150 static void check_ok_for_atomic (tree, Entity_Id, bool);
151 static int compatible_signatures_p (tree ftype1, tree ftype2);
152 static void rest_of_type_decl_compilation_no_defer (tree);
153 \f
154 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
155    entity, return the equivalent GCC tree for that entity (a ..._DECL node)
156    and associate the ..._DECL node with the input GNAT defining identifier.
157
158    If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
159    initial value (in GCC tree form).  This is optional for a variable.  For
160    a renamed entity, GNU_EXPR gives the object being renamed.
161
162    DEFINITION is nonzero if this call is intended for a definition.  This is
163    used for separate compilation where it is necessary to know whether an
164    external declaration or a definition must be created if the GCC equivalent
165    was not created previously.  The value of 1 is normally used for a nonzero
166    DEFINITION, but a value of 2 is used in special circumstances, defined in
167    the code.  */
168
169 tree
170 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
171 {
172   /* Contains the kind of the input GNAT node.  */
173   const Entity_Kind kind = Ekind (gnat_entity);
174   /* True if this is a type.  */
175   const bool is_type = IN (kind, Type_Kind);
176   /* For a type, contains the equivalent GNAT node to be used in gigi.  */
177   Entity_Id gnat_equiv_type = Empty;
178   /* Temporary used to walk the GNAT tree.  */
179   Entity_Id gnat_temp;
180   /* Contains the GCC DECL node which is equivalent to the input GNAT node.
181      This node will be associated with the GNAT node by calling at the end
182      of the `switch' statement.  */
183   tree gnu_decl = NULL_TREE;
184   /* Contains the GCC type to be used for the GCC node.  */
185   tree gnu_type = NULL_TREE;
186   /* Contains the GCC size tree to be used for the GCC node.  */
187   tree gnu_size = NULL_TREE;
188   /* Contains the GCC name to be used for the GCC node.  */
189   tree gnu_entity_name;
190   /* True if we have already saved gnu_decl as a GNAT association.  */
191   bool saved = false;
192   /* True if we incremented defer_incomplete_level.  */
193   bool this_deferred = false;
194   /* True if we incremented force_global.  */
195   bool this_global = false;
196   /* True if we should check to see if elaborated during processing.  */
197   bool maybe_present = false;
198   /* True if we made GNU_DECL and its type here.  */
199   bool this_made_decl = false;
200   /* True if debug info is requested for this entity.  */
201   bool debug_info_p = (Needs_Debug_Info (gnat_entity)
202                        || debug_info_level == DINFO_LEVEL_VERBOSE);
203   /* True if this entity is to be considered as imported.  */
204   bool imported_p = (Is_Imported (gnat_entity)
205                      && No (Address_Clause (gnat_entity)));
206   /* Size and alignment of the GCC node, if meaningful.  */
207   unsigned int esize = 0, align = 0;
208   /* Contains the list of attributes directly attached to the entity.  */
209   struct attrib *attr_list = NULL;
210
211   /* Since a use of an Itype is a definition, process it as such if it
212      is not in a with'ed unit.  */
213   if (!definition
214       && is_type
215       && Is_Itype (gnat_entity)
216       && !present_gnu_tree (gnat_entity)
217       && In_Extended_Main_Code_Unit (gnat_entity))
218     {
219       /* Ensure that we are in a subprogram mentioned in the Scope chain of
220          this entity, our current scope is global, or we encountered a task
221          or entry (where we can't currently accurately check scoping).  */
222       if (!current_function_decl
223           || DECL_ELABORATION_PROC_P (current_function_decl))
224         {
225           process_type (gnat_entity);
226           return get_gnu_tree (gnat_entity);
227         }
228
229       for (gnat_temp = Scope (gnat_entity);
230            Present (gnat_temp);
231            gnat_temp = Scope (gnat_temp))
232         {
233           if (Is_Type (gnat_temp))
234             gnat_temp = Underlying_Type (gnat_temp);
235
236           if (Ekind (gnat_temp) == E_Subprogram_Body)
237             gnat_temp
238               = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
239
240           if (IN (Ekind (gnat_temp), Subprogram_Kind)
241               && Present (Protected_Body_Subprogram (gnat_temp)))
242             gnat_temp = Protected_Body_Subprogram (gnat_temp);
243
244           if (Ekind (gnat_temp) == E_Entry
245               || Ekind (gnat_temp) == E_Entry_Family
246               || Ekind (gnat_temp) == E_Task_Type
247               || (IN (Ekind (gnat_temp), Subprogram_Kind)
248                   && present_gnu_tree (gnat_temp)
249                   && (current_function_decl
250                       == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
251             {
252               process_type (gnat_entity);
253               return get_gnu_tree (gnat_entity);
254             }
255         }
256
257       /* This abort means the Itype has an incorrect scope, i.e. that its
258          scope does not correspond to the subprogram it is declared in.  */
259       gcc_unreachable ();
260     }
261
262   /* If we've already processed this entity, return what we got last time.
263      If we are defining the node, we should not have already processed it.
264      In that case, we will abort below when we try to save a new GCC tree
265      for this object.  We also need to handle the case of getting a dummy
266      type when a Full_View exists.  */
267   if ((!definition || (is_type && imported_p))
268       && present_gnu_tree (gnat_entity))
269     {
270       gnu_decl = get_gnu_tree (gnat_entity);
271
272       if (TREE_CODE (gnu_decl) == TYPE_DECL
273           && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
274           && IN (kind, Incomplete_Or_Private_Kind)
275           && Present (Full_View (gnat_entity)))
276         {
277           gnu_decl
278             = gnat_to_gnu_entity (Full_View (gnat_entity), NULL_TREE, 0);
279           save_gnu_tree (gnat_entity, NULL_TREE, false);
280           save_gnu_tree (gnat_entity, gnu_decl, false);
281         }
282
283       return gnu_decl;
284     }
285
286   /* If this is a numeric or enumeral type, or an access type, a nonzero
287      Esize must be specified unless it was specified by the programmer.  */
288   gcc_assert (!Unknown_Esize (gnat_entity)
289               || Has_Size_Clause (gnat_entity)
290               || (!IN (kind, Numeric_Kind)
291                   && !IN (kind, Enumeration_Kind)
292                   && (!IN (kind, Access_Kind)
293                       || kind == E_Access_Protected_Subprogram_Type
294                       || kind == E_Anonymous_Access_Protected_Subprogram_Type
295                       || kind == E_Access_Subtype)));
296
297   /* The RM size must be specified for all discrete and fixed-point types.  */
298   gcc_assert (!(IN (kind, Discrete_Or_Fixed_Point_Kind)
299                 && Unknown_RM_Size (gnat_entity)));
300
301   /* If we get here, it means we have not yet done anything with this entity.
302      If we are not defining it, it must be a type or an entity that is defined
303      elsewhere or externally, otherwise we should have defined it already.  */
304   gcc_assert (definition
305               || type_annotate_only
306               || is_type
307               || kind == E_Discriminant
308               || kind == E_Component
309               || kind == E_Label
310               || (kind == E_Constant && Present (Full_View (gnat_entity)))
311               || Is_Public (gnat_entity));
312
313   /* Get the name of the entity and set up the line number and filename of
314      the original definition for use in any decl we make.  */
315   gnu_entity_name = get_entity_name (gnat_entity);
316   Sloc_to_locus (Sloc (gnat_entity), &input_location);
317
318   /* For cases when we are not defining (i.e., we are referencing from
319      another compilation unit) public entities, show we are at global level
320      for the purpose of computing scopes.  Don't do this for components or
321      discriminants since the relevant test is whether or not the record is
322      being defined.  */
323   if (!definition
324       && kind != E_Component
325       && kind != E_Discriminant
326       && Is_Public (gnat_entity)
327       && !Is_Statically_Allocated (gnat_entity))
328     force_global++, this_global = true;
329
330   /* Handle any attributes directly attached to the entity.  */
331   if (Has_Gigi_Rep_Item (gnat_entity))
332     prepend_attributes (gnat_entity, &attr_list);
333
334   /* Do some common processing for types.  */
335   if (is_type)
336     {
337       /* Compute the equivalent type to be used in gigi.  */
338       gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
339
340       /* Machine_Attributes on types are expected to be propagated to
341          subtypes.  The corresponding Gigi_Rep_Items are only attached
342          to the first subtype though, so we handle the propagation here.  */
343       if (Base_Type (gnat_entity) != gnat_entity
344           && !Is_First_Subtype (gnat_entity)
345           && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
346         prepend_attributes (First_Subtype (Base_Type (gnat_entity)),
347                             &attr_list);
348
349       /* Compute a default value for the size of the type.  */
350       if (Known_Esize (gnat_entity)
351           && UI_Is_In_Int_Range (Esize (gnat_entity)))
352         {
353           unsigned int max_esize;
354           esize = UI_To_Int (Esize (gnat_entity));
355
356           if (IN (kind, Float_Kind))
357             max_esize = fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE);
358           else if (IN (kind, Access_Kind))
359             max_esize = POINTER_SIZE * 2;
360           else
361             max_esize = LONG_LONG_TYPE_SIZE;
362
363           if (esize > max_esize)
364            esize = max_esize;
365         }
366       else
367         esize = LONG_LONG_TYPE_SIZE;
368     }
369
370   switch (kind)
371     {
372     case E_Constant:
373       /* If this is a use of a deferred constant without address clause,
374          get its full definition.  */
375       if (!definition
376           && No (Address_Clause (gnat_entity))
377           && Present (Full_View (gnat_entity)))
378         {
379           gnu_decl
380             = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
381           saved = true;
382           break;
383         }
384
385       /* If we have an external constant that we are not defining, get the
386          expression that is was defined to represent.  We may throw that
387          expression away later if it is not a constant.  Do not retrieve the
388          expression if it is an aggregate or allocator, because in complex
389          instantiation contexts it may not be expanded  */
390       if (!definition
391           && Present (Expression (Declaration_Node (gnat_entity)))
392           && !No_Initialization (Declaration_Node (gnat_entity))
393           && (Nkind (Expression (Declaration_Node (gnat_entity)))
394               != N_Aggregate)
395           && (Nkind (Expression (Declaration_Node (gnat_entity)))
396               != N_Allocator))
397         gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
398
399       /* Ignore deferred constant definitions without address clause since
400          they are processed fully in the front-end.  If No_Initialization
401          is set, this is not a deferred constant but a constant whose value
402          is built manually.  And constants that are renamings are handled
403          like variables.  */
404       if (definition
405           && !gnu_expr
406           && No (Address_Clause (gnat_entity))
407           && !No_Initialization (Declaration_Node (gnat_entity))
408           && No (Renamed_Object (gnat_entity)))
409         {
410           gnu_decl = error_mark_node;
411           saved = true;
412           break;
413         }
414
415       /* Ignore constant definitions already marked with the error node.  See
416          the N_Object_Declaration case of gnat_to_gnu for the rationale.  */
417       if (definition
418           && gnu_expr
419           && present_gnu_tree (gnat_entity)
420           && get_gnu_tree (gnat_entity) == error_mark_node)
421         {
422           maybe_present = true;
423           break;
424         }
425
426       goto object;
427
428     case E_Exception:
429       /* We used to special case VMS exceptions here to directly map them to
430          their associated condition code.  Since this code had to be masked
431          dynamically to strip off the severity bits, this caused trouble in
432          the GCC/ZCX case because the "type" pointers we store in the tables
433          have to be static.  We now don't special case here anymore, and let
434          the regular processing take place, which leaves us with a regular
435          exception data object for VMS exceptions too.  The condition code
436          mapping is taken care of by the front end and the bitmasking by the
437          runtime library.  */
438       goto object;
439
440     case E_Discriminant:
441     case E_Component:
442       {
443         /* The GNAT record where the component was defined.  */
444         Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
445
446         /* If the variable is an inherited record component (in the case of
447            extended record types), just return the inherited entity, which
448            must be a FIELD_DECL.  Likewise for discriminants.
449            For discriminants of untagged records which have explicit
450            stored discriminants, return the entity for the corresponding
451            stored discriminant.  Also use Original_Record_Component
452            if the record has a private extension.  */
453         if (Present (Original_Record_Component (gnat_entity))
454             && Original_Record_Component (gnat_entity) != gnat_entity)
455           {
456             gnu_decl
457               = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
458                                     gnu_expr, definition);
459             saved = true;
460             break;
461           }
462
463         /* If the enclosing record has explicit stored discriminants,
464            then it is an untagged record.  If the Corresponding_Discriminant
465            is not empty then this must be a renamed discriminant and its
466            Original_Record_Component must point to the corresponding explicit
467            stored discriminant (i.e. we should have taken the previous
468            branch).  */
469         else if (Present (Corresponding_Discriminant (gnat_entity))
470                  && Is_Tagged_Type (gnat_record))
471           {
472             /* A tagged record has no explicit stored discriminants.  */
473             gcc_assert (First_Discriminant (gnat_record)
474                        == First_Stored_Discriminant (gnat_record));
475             gnu_decl
476               = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
477                                     gnu_expr, definition);
478             saved = true;
479             break;
480           }
481
482         else if (Present (CR_Discriminant (gnat_entity))
483                  && type_annotate_only)
484           {
485             gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
486                                            gnu_expr, definition);
487             saved = true;
488             break;
489           }
490
491         /* If the enclosing record has explicit stored discriminants, then
492            it is an untagged record.  If the Corresponding_Discriminant
493            is not empty then this must be a renamed discriminant and its
494            Original_Record_Component must point to the corresponding explicit
495            stored discriminant (i.e. we should have taken the first
496            branch).  */
497         else if (Present (Corresponding_Discriminant (gnat_entity))
498                  && (First_Discriminant (gnat_record)
499                      != First_Stored_Discriminant (gnat_record)))
500           gcc_unreachable ();
501
502         /* Otherwise, if we are not defining this and we have no GCC type
503            for the containing record, make one for it.  Then we should
504            have made our own equivalent.  */
505         else if (!definition && !present_gnu_tree (gnat_record))
506           {
507             /* ??? If this is in a record whose scope is a protected
508                type and we have an Original_Record_Component, use it.
509                This is a workaround for major problems in protected type
510                handling.  */
511             Entity_Id Scop = Scope (Scope (gnat_entity));
512             if ((Is_Protected_Type (Scop)
513                  || (Is_Private_Type (Scop)
514                      && Present (Full_View (Scop))
515                      && Is_Protected_Type (Full_View (Scop))))
516                 && Present (Original_Record_Component (gnat_entity)))
517               {
518                 gnu_decl
519                   = gnat_to_gnu_entity (Original_Record_Component
520                                         (gnat_entity),
521                                         gnu_expr, 0);
522                 saved = true;
523                 break;
524               }
525
526             gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
527             gnu_decl = get_gnu_tree (gnat_entity);
528             saved = true;
529             break;
530           }
531
532         else
533           /* Here we have no GCC type and this is a reference rather than a
534              definition.  This should never happen.  Most likely the cause is
535              reference before declaration in the gnat tree for gnat_entity.  */
536           gcc_unreachable ();
537       }
538
539     case E_Loop_Parameter:
540     case E_Out_Parameter:
541     case E_Variable:
542
543       /* Simple variables, loop variables, Out parameters, and exceptions.  */
544     object:
545       {
546         bool used_by_ref = false;
547         bool const_flag
548           = ((kind == E_Constant || kind == E_Variable)
549              && Is_True_Constant (gnat_entity)
550              && !Treat_As_Volatile (gnat_entity)
551              && (((Nkind (Declaration_Node (gnat_entity))
552                    == N_Object_Declaration)
553                   && Present (Expression (Declaration_Node (gnat_entity))))
554                  || Present (Renamed_Object (gnat_entity))));
555         bool inner_const_flag = const_flag;
556         bool static_p = Is_Statically_Allocated (gnat_entity);
557         bool mutable_p = false;
558         tree gnu_ext_name = NULL_TREE;
559         tree renamed_obj = NULL_TREE;
560         tree gnu_object_size;
561
562         if (Present (Renamed_Object (gnat_entity)) && !definition)
563           {
564             if (kind == E_Exception)
565               gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
566                                              NULL_TREE, 0);
567             else
568               gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
569           }
570
571         /* Get the type after elaborating the renamed object.  */
572         gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
573
574         /* For a debug renaming declaration, build a pure debug entity.  */
575         if (Present (Debug_Renaming_Link (gnat_entity)))
576           {
577             rtx addr;
578             gnu_decl = build_decl (VAR_DECL, gnu_entity_name, gnu_type);
579             /* The (MEM (CONST (0))) pattern is prescribed by STABS.  */
580             if (global_bindings_p ())
581               addr = gen_rtx_CONST (VOIDmode, const0_rtx);
582             else
583               addr = stack_pointer_rtx;
584             SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
585             gnat_pushdecl (gnu_decl, gnat_entity);
586             break;
587           }
588
589         /* If this is a loop variable, its type should be the base type.
590            This is because the code for processing a loop determines whether
591            a normal loop end test can be done by comparing the bounds of the
592            loop against those of the base type, which is presumed to be the
593            size used for computation.  But this is not correct when the size
594            of the subtype is smaller than the type.  */
595         if (kind == E_Loop_Parameter)
596           gnu_type = get_base_type (gnu_type);
597
598         /* Reject non-renamed objects whose types are unconstrained arrays or
599            any object whose type is a dummy type or VOID_TYPE.  */
600
601         if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
602              && No (Renamed_Object (gnat_entity)))
603             || TYPE_IS_DUMMY_P (gnu_type)
604             || TREE_CODE (gnu_type) == VOID_TYPE)
605           {
606             gcc_assert (type_annotate_only);
607             if (this_global)
608               force_global--;
609             return error_mark_node;
610           }
611
612         /* If an alignment is specified, use it if valid.   Note that
613            exceptions are objects but don't have alignments.  We must do this
614            before we validate the size, since the alignment can affect the
615            size.  */
616         if (kind != E_Exception && Known_Alignment (gnat_entity))
617           {
618             gcc_assert (Present (Alignment (gnat_entity)));
619             align = validate_alignment (Alignment (gnat_entity), gnat_entity,
620                                         TYPE_ALIGN (gnu_type));
621             gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
622                                        "PAD", false, definition, true);
623           }
624
625         /* If we are defining the object, see if it has a Size value and
626            validate it if so.  If we are not defining the object and a Size
627            clause applies, simply retrieve the value.  We don't want to ignore
628            the clause and it is expected to have been validated already.  Then
629            get the new type, if any.  */
630         if (definition)
631           gnu_size = validate_size (Esize (gnat_entity), gnu_type,
632                                     gnat_entity, VAR_DECL, false,
633                                     Has_Size_Clause (gnat_entity));
634         else if (Has_Size_Clause (gnat_entity))
635           gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
636
637         if (gnu_size)
638           {
639             gnu_type
640               = make_type_from_size (gnu_type, gnu_size,
641                                      Has_Biased_Representation (gnat_entity));
642
643             if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
644               gnu_size = NULL_TREE;
645           }
646
647         /* If this object has self-referential size, it must be a record with
648            a default value.  We are supposed to allocate an object of the
649            maximum size in this case unless it is a constant with an
650            initializing expression, in which case we can get the size from
651            that.  Note that the resulting size may still be a variable, so
652            this may end up with an indirect allocation.  */
653         if (No (Renamed_Object (gnat_entity))
654             && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
655           {
656             if (gnu_expr && kind == E_Constant)
657               {
658                 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
659                 if (CONTAINS_PLACEHOLDER_P (size))
660                   {
661                     /* If the initializing expression is itself a constant,
662                        despite having a nominal type with self-referential
663                        size, we can get the size directly from it.  */
664                     if (TREE_CODE (gnu_expr) == COMPONENT_REF
665                         && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
666                            == RECORD_TYPE
667                         && TYPE_IS_PADDING_P
668                            (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
669                         && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
670                         && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
671                             || DECL_READONLY_ONCE_ELAB
672                                (TREE_OPERAND (gnu_expr, 0))))
673                       gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
674                     else
675                       gnu_size
676                         = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
677                   }
678                 else
679                   gnu_size = size;
680               }
681             /* We may have no GNU_EXPR because No_Initialization is
682                set even though there's an Expression.  */
683             else if (kind == E_Constant
684                      && (Nkind (Declaration_Node (gnat_entity))
685                          == N_Object_Declaration)
686                      && Present (Expression (Declaration_Node (gnat_entity))))
687               gnu_size
688                 = TYPE_SIZE (gnat_to_gnu_type
689                              (Etype
690                               (Expression (Declaration_Node (gnat_entity)))));
691             else
692               {
693                 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
694                 mutable_p = true;
695               }
696           }
697
698         /* If the size is zero bytes, make it one byte since some linkers have
699            trouble with zero-sized objects.  If the object will have a
700            template, that will make it nonzero so don't bother.  Also avoid
701            doing that for an object renaming or an object with an address
702            clause, as we would lose useful information on the view size
703            (e.g. for null array slices) and we are not allocating the object
704            here anyway.  */
705         if (((gnu_size
706               && integer_zerop (gnu_size)
707               && !TREE_OVERFLOW (gnu_size))
708              || (TYPE_SIZE (gnu_type)
709                  && integer_zerop (TYPE_SIZE (gnu_type))
710                  && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
711             && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
712                 || !Is_Array_Type (Etype (gnat_entity)))
713             && No (Renamed_Object (gnat_entity))
714             && No (Address_Clause (gnat_entity)))
715           gnu_size = bitsize_unit_node;
716
717         /* If this is an object with no specified size and alignment, and
718            if either it is atomic or we are not optimizing alignment for
719            space and it is composite and not an exception, an Out parameter
720            or a reference to another object, and the size of its type is a
721            constant, set the alignment to the smallest one which is not
722            smaller than the size, with an appropriate cap.  */
723         if (!gnu_size && align == 0
724             && (Is_Atomic (gnat_entity)
725                 || (!Optimize_Alignment_Space (gnat_entity)
726                     && kind != E_Exception
727                     && kind != E_Out_Parameter
728                     && Is_Composite_Type (Etype (gnat_entity))
729                     && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
730                     && !imported_p
731                     && No (Renamed_Object (gnat_entity))
732                     && No (Address_Clause (gnat_entity))))
733             && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
734           {
735             /* No point in jumping through all the hoops needed in order
736                to support BIGGEST_ALIGNMENT if we don't really have to.
737                So we cap to the smallest alignment that corresponds to
738                a known efficient memory access pattern of the target.  */
739             unsigned int align_cap = Is_Atomic (gnat_entity)
740                                      ? BIGGEST_ALIGNMENT
741                                      : get_mode_alignment (ptr_mode);
742
743             if (!host_integerp (TYPE_SIZE (gnu_type), 1)
744                 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
745               align = align_cap;
746             else
747               align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
748
749             /* But make sure not to under-align the object.  */
750             if (align <= TYPE_ALIGN (gnu_type))
751               align = 0;
752
753             /* And honor the minimum valid atomic alignment, if any.  */
754 #ifdef MINIMUM_ATOMIC_ALIGNMENT
755             else if (align < MINIMUM_ATOMIC_ALIGNMENT)
756               align = MINIMUM_ATOMIC_ALIGNMENT;
757 #endif
758           }
759
760         /* If the object is set to have atomic components, find the component
761            type and validate it.
762
763            ??? Note that we ignore Has_Volatile_Components on objects; it's
764            not at all clear what to do in that case.  */
765
766         if (Has_Atomic_Components (gnat_entity))
767           {
768             tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
769                               ? TREE_TYPE (gnu_type) : gnu_type);
770
771             while (TREE_CODE (gnu_inner) == ARRAY_TYPE
772                    && TYPE_MULTI_ARRAY_P (gnu_inner))
773               gnu_inner = TREE_TYPE (gnu_inner);
774
775             check_ok_for_atomic (gnu_inner, gnat_entity, true);
776           }
777
778         /* Now check if the type of the object allows atomic access.  Note
779            that we must test the type, even if this object has size and
780            alignment to allow such access, because we will be going
781            inside the padded record to assign to the object.  We could fix
782            this by always copying via an intermediate value, but it's not
783            clear it's worth the effort.  */
784         if (Is_Atomic (gnat_entity))
785           check_ok_for_atomic (gnu_type, gnat_entity, false);
786
787         /* If this is an aliased object with an unconstrained nominal subtype,
788            make a type that includes the template.  */
789         if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
790             && Is_Array_Type (Etype (gnat_entity))
791             && !type_annotate_only)
792         {
793           tree gnu_fat
794             = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
795
796           gnu_type
797             = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
798                                               concat_name (gnu_entity_name,
799                                                            "UNC"));
800         }
801
802 #ifdef MINIMUM_ATOMIC_ALIGNMENT
803         /* If the size is a constant and no alignment is specified, force
804            the alignment to be the minimum valid atomic alignment.  The
805            restriction on constant size avoids problems with variable-size
806            temporaries; if the size is variable, there's no issue with
807            atomic access.  Also don't do this for a constant, since it isn't
808            necessary and can interfere with constant replacement.  Finally,
809            do not do it for Out parameters since that creates an
810            size inconsistency with In parameters.  */
811         if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
812             && !FLOAT_TYPE_P (gnu_type)
813             && !const_flag && No (Renamed_Object (gnat_entity))
814             && !imported_p && No (Address_Clause (gnat_entity))
815             && kind != E_Out_Parameter
816             && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
817                 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
818           align = MINIMUM_ATOMIC_ALIGNMENT;
819 #endif
820
821         /* Make a new type with the desired size and alignment, if needed.
822            But do not take into account alignment promotions to compute the
823            size of the object.  */
824         gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
825         if (gnu_size || align > 0)
826           gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
827                                      "PAD", false, definition,
828                                      gnu_size ? true : false);
829
830         /* If this is a renaming, avoid as much as possible to create a new
831            object.  However, in several cases, creating it is required.
832            This processing needs to be applied to the raw expression so
833            as to make it more likely to rename the underlying object.  */
834         if (Present (Renamed_Object (gnat_entity)))
835           {
836             bool create_normal_object = false;
837
838             /* If the renamed object had padding, strip off the reference
839                to the inner object and reset our type.  */
840             if ((TREE_CODE (gnu_expr) == COMPONENT_REF
841                  && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
842                     == RECORD_TYPE
843                  && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
844                 /* Strip useless conversions around the object.  */
845                 || (TREE_CODE (gnu_expr) == NOP_EXPR
846                     && gnat_types_compatible_p
847                        (TREE_TYPE (gnu_expr),
848                         TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
849               {
850                 gnu_expr = TREE_OPERAND (gnu_expr, 0);
851                 gnu_type = TREE_TYPE (gnu_expr);
852               }
853
854             /* Case 1: If this is a constant renaming stemming from a function
855                call, treat it as a normal object whose initial value is what
856                is being renamed.  RM 3.3 says that the result of evaluating a
857                function call is a constant object.  As a consequence, it can
858                be the inner object of a constant renaming.  In this case, the
859                renaming must be fully instantiated, i.e. it cannot be a mere
860                reference to (part of) an existing object.  */
861             if (const_flag)
862               {
863                 tree inner_object = gnu_expr;
864                 while (handled_component_p (inner_object))
865                   inner_object = TREE_OPERAND (inner_object, 0);
866                 if (TREE_CODE (inner_object) == CALL_EXPR)
867                   create_normal_object = true;
868               }
869
870             /* Otherwise, see if we can proceed with a stabilized version of
871                the renamed entity or if we need to make a new object.  */
872             if (!create_normal_object)
873               {
874                 tree maybe_stable_expr = NULL_TREE;
875                 bool stable = false;
876
877                 /* Case 2: If the renaming entity need not be materialized and
878                    the renamed expression is something we can stabilize, use
879                    that for the renaming.  At the global level, we can only do
880                    this if we know no SAVE_EXPRs need be made, because the
881                    expression we return might be used in arbitrary conditional
882                    branches so we must force the SAVE_EXPRs evaluation
883                    immediately and this requires a function context.  */
884                 if (!Materialize_Entity (gnat_entity)
885                     && (!global_bindings_p ()
886                         || (staticp (gnu_expr)
887                             && !TREE_SIDE_EFFECTS (gnu_expr))))
888                   {
889                     maybe_stable_expr
890                       = maybe_stabilize_reference (gnu_expr, true, &stable);
891
892                     if (stable)
893                       {
894                         gnu_decl = maybe_stable_expr;
895                         /* ??? No DECL_EXPR is created so we need to mark
896                            the expression manually lest it is shared.  */
897                         if (global_bindings_p ())
898                           mark_visited (&gnu_decl);
899                         save_gnu_tree (gnat_entity, gnu_decl, true);
900                         saved = true;
901                         break;
902                       }
903
904                     /* The stabilization failed.  Keep maybe_stable_expr
905                        untouched here to let the pointer case below know
906                        about that failure.  */
907                   }
908
909                 /* Case 3: If this is a constant renaming and creating a
910                    new object is allowed and cheap, treat it as a normal
911                    object whose initial value is what is being renamed.  */
912                 if (const_flag
913                     && !Is_Composite_Type
914                         (Underlying_Type (Etype (gnat_entity))))
915                   ;
916
917                 /* Case 4: Make this into a constant pointer to the object we
918                    are to rename and attach the object to the pointer if it is
919                    something we can stabilize.
920
921                    From the proper scope, attached objects will be referenced
922                    directly instead of indirectly via the pointer to avoid
923                    subtle aliasing problems with non-addressable entities.
924                    They have to be stable because we must not evaluate the
925                    variables in the expression every time the renaming is used.
926                    The pointer is called a "renaming" pointer in this case.
927
928                    In the rare cases where we cannot stabilize the renamed
929                    object, we just make a "bare" pointer, and the renamed
930                    entity is always accessed indirectly through it.  */
931                 else
932                   {
933                     gnu_type = build_reference_type (gnu_type);
934                     inner_const_flag = TREE_READONLY (gnu_expr);
935                     const_flag = true;
936
937                     /* If the previous attempt at stabilizing failed, there
938                        is no point in trying again and we reuse the result
939                        without attaching it to the pointer.  In this case it
940                        will only be used as the initializing expression of
941                        the pointer and thus needs no special treatment with
942                        regard to multiple evaluations.  */
943                     if (maybe_stable_expr)
944                       ;
945
946                     /* Otherwise, try to stabilize and attach the expression
947                        to the pointer if the stabilization succeeds.
948
949                        Note that this might introduce SAVE_EXPRs and we don't
950                        check whether we're at the global level or not.  This
951                        is fine since we are building a pointer initializer and
952                        neither the pointer nor the initializing expression can
953                        be accessed before the pointer elaboration has taken
954                        place in a correct program.
955
956                        These SAVE_EXPRs will be evaluated at the right place
957                        by either the evaluation of the initializer for the
958                        non-global case or the elaboration code for the global
959                        case, and will be attached to the elaboration procedure
960                        in the latter case.  */
961                     else
962                      {
963                         maybe_stable_expr
964                           = maybe_stabilize_reference (gnu_expr, true, &stable);
965
966                         if (stable)
967                           renamed_obj = maybe_stable_expr;
968
969                         /* Attaching is actually performed downstream, as soon
970                            as we have a VAR_DECL for the pointer we make.  */
971                       }
972
973                     gnu_expr
974                       = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
975
976                     gnu_size = NULL_TREE;
977                     used_by_ref = true;
978                   }
979               }
980           }
981
982         /* Make a volatile version of this object's type if we are to make
983            the object volatile.  We also interpret 13.3(19) conservatively
984            and disallow any optimizations for such a non-constant object.  */
985         if ((Treat_As_Volatile (gnat_entity)
986              || (!const_flag
987                  && (Is_Exported (gnat_entity)
988                      || Is_Imported (gnat_entity)
989                      || Present (Address_Clause (gnat_entity)))))
990             && !TYPE_VOLATILE (gnu_type))
991           gnu_type = build_qualified_type (gnu_type,
992                                            (TYPE_QUALS (gnu_type)
993                                             | TYPE_QUAL_VOLATILE));
994
995         /* If we are defining an aliased object whose nominal subtype is
996            unconstrained, the object is a record that contains both the
997            template and the object.  If there is an initializer, it will
998            have already been converted to the right type, but we need to
999            create the template if there is no initializer.  */
1000         if (definition
1001             && !gnu_expr
1002             && TREE_CODE (gnu_type) == RECORD_TYPE
1003             && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1004                 /* Beware that padding might have been introduced
1005                    via maybe_pad_type above.  */
1006                 || (TYPE_IS_PADDING_P (gnu_type)
1007                     && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1008                        == RECORD_TYPE
1009                     && TYPE_CONTAINS_TEMPLATE_P
1010                        (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1011           {
1012             tree template_field
1013               = TYPE_IS_PADDING_P (gnu_type)
1014                 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1015                 : TYPE_FIELDS (gnu_type);
1016
1017             gnu_expr
1018               = gnat_build_constructor
1019               (gnu_type,
1020                tree_cons
1021                (template_field,
1022                 build_template (TREE_TYPE (template_field),
1023                                 TREE_TYPE (TREE_CHAIN (template_field)),
1024                                 NULL_TREE),
1025                 NULL_TREE));
1026           }
1027
1028         /* Convert the expression to the type of the object except in the
1029            case where the object's type is unconstrained or the object's type
1030            is a padded record whose field is of self-referential size.  In
1031            the former case, converting will generate unnecessary evaluations
1032            of the CONSTRUCTOR to compute the size and in the latter case, we
1033            want to only copy the actual data.  */
1034         if (gnu_expr
1035             && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1036             && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1037             && !(TREE_CODE (gnu_type) == RECORD_TYPE
1038                  && TYPE_IS_PADDING_P (gnu_type)
1039                  && (CONTAINS_PLACEHOLDER_P
1040                      (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1041           gnu_expr = convert (gnu_type, gnu_expr);
1042
1043         /* If this is a pointer and it does not have an initializing
1044            expression, initialize it to NULL, unless the object is
1045            imported.  */
1046         if (definition
1047             && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1048             && !Is_Imported (gnat_entity) && !gnu_expr)
1049           gnu_expr = integer_zero_node;
1050
1051         /* If we are defining the object and it has an Address clause, we must
1052            either get the address expression from the saved GCC tree for the
1053            object if it has a Freeze node, or elaborate the address expression
1054            here since the front-end has guaranteed that the elaboration has no
1055            effects in this case.  */
1056         if (definition && Present (Address_Clause (gnat_entity)))
1057           {
1058             tree gnu_address
1059               = present_gnu_tree (gnat_entity)
1060                 ? get_gnu_tree (gnat_entity)
1061                 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1062
1063             save_gnu_tree (gnat_entity, NULL_TREE, false);
1064
1065             /* Ignore the size.  It's either meaningless or was handled
1066                above.  */
1067             gnu_size = NULL_TREE;
1068             /* Convert the type of the object to a reference type that can
1069                alias everything as per 13.3(19).  */
1070             gnu_type
1071               = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1072             gnu_address = convert (gnu_type, gnu_address);
1073             used_by_ref = true;
1074             const_flag = !Is_Public (gnat_entity)
1075               || compile_time_known_address_p (Expression (Address_Clause
1076                                                            (gnat_entity)));
1077
1078             /* If this is a deferred constant, the initializer is attached to
1079                the full view.  */
1080             if (kind == E_Constant && Present (Full_View (gnat_entity)))
1081               gnu_expr
1082                 = gnat_to_gnu
1083                     (Expression (Declaration_Node (Full_View (gnat_entity))));
1084
1085             /* If we don't have an initializing expression for the underlying
1086                variable, the initializing expression for the pointer is the
1087                specified address.  Otherwise, we have to make a COMPOUND_EXPR
1088                to assign both the address and the initial value.  */
1089             if (!gnu_expr)
1090               gnu_expr = gnu_address;
1091             else
1092               gnu_expr
1093                 = build2 (COMPOUND_EXPR, gnu_type,
1094                           build_binary_op
1095                           (MODIFY_EXPR, NULL_TREE,
1096                            build_unary_op (INDIRECT_REF, NULL_TREE,
1097                                            gnu_address),
1098                            gnu_expr),
1099                           gnu_address);
1100           }
1101
1102         /* If it has an address clause and we are not defining it, mark it
1103            as an indirect object.  Likewise for Stdcall objects that are
1104            imported.  */
1105         if ((!definition && Present (Address_Clause (gnat_entity)))
1106             || (Is_Imported (gnat_entity)
1107                 && Has_Stdcall_Convention (gnat_entity)))
1108           {
1109             /* Convert the type of the object to a reference type that can
1110                alias everything as per 13.3(19).  */
1111             gnu_type
1112               = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1113             gnu_size = NULL_TREE;
1114
1115             /* No point in taking the address of an initializing expression
1116                that isn't going to be used.  */
1117             gnu_expr = NULL_TREE;
1118
1119             /* If it has an address clause whose value is known at compile
1120                time, make the object a CONST_DECL.  This will avoid a
1121                useless dereference.  */
1122             if (Present (Address_Clause (gnat_entity)))
1123               {
1124                 Node_Id gnat_address
1125                   = Expression (Address_Clause (gnat_entity));
1126
1127                 if (compile_time_known_address_p (gnat_address))
1128                   {
1129                     gnu_expr = gnat_to_gnu (gnat_address);
1130                     const_flag = true;
1131                   }
1132               }
1133
1134             used_by_ref = true;
1135           }
1136
1137         /* If we are at top level and this object is of variable size,
1138            make the actual type a hidden pointer to the real type and
1139            make the initializer be a memory allocation and initialization.
1140            Likewise for objects we aren't defining (presumed to be
1141            external references from other packages), but there we do
1142            not set up an initialization.
1143
1144            If the object's size overflows, make an allocator too, so that
1145            Storage_Error gets raised.  Note that we will never free
1146            such memory, so we presume it never will get allocated.  */
1147
1148         if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1149                                  global_bindings_p () || !definition
1150                                  || static_p)
1151             || (gnu_size
1152                 && ! allocatable_size_p (gnu_size,
1153                                          global_bindings_p () || !definition
1154                                          || static_p)))
1155           {
1156             gnu_type = build_reference_type (gnu_type);
1157             gnu_size = NULL_TREE;
1158             used_by_ref = true;
1159             const_flag = true;
1160
1161             /* In case this was a aliased object whose nominal subtype is
1162                unconstrained, the pointer above will be a thin pointer and
1163                build_allocator will automatically make the template.
1164
1165                If we have a template initializer only (that we made above),
1166                pretend there is none and rely on what build_allocator creates
1167                again anyway.  Otherwise (if we have a full initializer), get
1168                the data part and feed that to build_allocator.
1169
1170                If we are elaborating a mutable object, tell build_allocator to
1171                ignore a possibly simpler size from the initializer, if any, as
1172                we must allocate the maximum possible size in this case.  */
1173
1174             if (definition)
1175               {
1176                 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1177
1178                 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1179                     && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1180                   {
1181                     gnu_alloc_type
1182                       = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1183
1184                     if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1185                         && 1 == VEC_length (constructor_elt,
1186                                             CONSTRUCTOR_ELTS (gnu_expr)))
1187                       gnu_expr = 0;
1188                     else
1189                       gnu_expr
1190                         = build_component_ref
1191                             (gnu_expr, NULL_TREE,
1192                              TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1193                              false);
1194                   }
1195
1196                 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1197                     && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1198                     && !Is_Imported (gnat_entity))
1199                   post_error ("?Storage_Error will be raised at run-time!",
1200                               gnat_entity);
1201
1202                 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1203                                             0, 0, 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,
1566                                          get_identifier ("L"), definition, 1,
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,
1573                                          get_identifier ("U"), definition, 1,
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 (Known_Alignment (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, 1,
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, 1,
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 (gnat_entity, gnat_entity,
2437                                               TYPE_SIZE (gnu_arr_type),
2438                                               gnu_str_name, definition, 0);
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                        (gnat_entity, gnat_entity,
2448                         build_binary_op (EXACT_DIV_EXPR, sizetype,
2449                                          TYPE_SIZE_UNIT (gnu_arr_type),
2450                                          size_int (TYPE_ALIGN (eltype)
2451                                                    / BITS_PER_UNIT)),
2452                         concat_name (gnu_str_name, "A_U"), definition, 0),
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           gnu_decl
2549             = create_type_decl (gnu_entity_name, gnu_type, attr_list,
2550                                 !Comes_From_Source (gnat_entity),
2551                                 debug_info_p, gnat_entity);
2552
2553           /* Save it as our equivalent in case the call below elaborates
2554              this type again.  */
2555           save_gnu_tree (gnat_entity, gnu_decl, false);
2556
2557           gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2558                                          NULL_TREE, 0);
2559           this_made_decl = true;
2560           gnu_type = TREE_TYPE (gnu_decl);
2561           save_gnu_tree (gnat_entity, NULL_TREE, false);
2562
2563           gnu_inner_type = gnu_type;
2564           while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2565                  && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2566                      || TYPE_IS_PADDING_P (gnu_inner_type)))
2567             gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2568
2569           /* We need to attach the index type to the type we just made so
2570              that the actual bounds can later be put into a template.  */
2571           if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2572                && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2573               || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2574                   && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2575             {
2576               if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2577                 {
2578                   /* The TYPE_ACTUAL_BOUNDS field is overloaded with the
2579                      TYPE_MODULUS for modular types so we make an extra
2580                      subtype if necessary.  */
2581                   if (TYPE_MODULAR_P (gnu_inner_type))
2582                     {
2583                       tree gnu_subtype = make_node (INTEGER_TYPE);
2584                       TREE_TYPE (gnu_subtype) = gnu_inner_type;
2585                       TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2586
2587                       TYPE_UNSIGNED (gnu_subtype) = 1;
2588                       TYPE_PRECISION (gnu_subtype)
2589                         = TYPE_PRECISION (gnu_inner_type);
2590                       TYPE_MIN_VALUE (gnu_subtype)
2591                         = TYPE_MIN_VALUE (gnu_inner_type);
2592                       TYPE_MAX_VALUE (gnu_subtype)
2593                         = TYPE_MAX_VALUE (gnu_inner_type);
2594                       layout_type (gnu_subtype);
2595
2596                       gnu_inner_type = gnu_subtype;
2597                     }
2598
2599                   TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2600
2601 #ifdef ENABLE_CHECKING
2602                   /* Check for other cases of overloading.  */
2603                   gcc_assert (!TYPE_ACTUAL_BOUNDS (gnu_inner_type));
2604 #endif
2605                 }
2606
2607               /* ??? This is necessary to make sure that the container is
2608                  allocated with a null tree upfront; otherwise, it could
2609                  be allocated with an uninitialized tree that is accessed
2610                  before being set below.  See ada-tree.h for details.  */
2611               SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2612
2613               for (gnat_index = First_Index (gnat_entity);
2614                    Present (gnat_index); gnat_index = Next_Index (gnat_index))
2615                 SET_TYPE_ACTUAL_BOUNDS
2616                   (gnu_inner_type,
2617                    tree_cons (NULL_TREE,
2618                               get_unpadded_type (Etype (gnat_index)),
2619                               TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2620
2621               if (Convention (gnat_entity) != Convention_Fortran)
2622                 SET_TYPE_ACTUAL_BOUNDS
2623                   (gnu_inner_type,
2624                    nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2625
2626               if (TREE_CODE (gnu_type) == RECORD_TYPE
2627                   && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2628                 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2629             }
2630         }
2631
2632       /* Abort if packed array with no packed array type field set.  */
2633       else
2634         gcc_assert (!Is_Packed (gnat_entity));
2635
2636       break;
2637
2638     case E_String_Literal_Subtype:
2639       /* Create the type for a string literal.  */
2640       {
2641         Entity_Id gnat_full_type
2642           = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2643              && Present (Full_View (Etype (gnat_entity)))
2644              ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2645         tree gnu_string_type = get_unpadded_type (gnat_full_type);
2646         tree gnu_string_array_type
2647           = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2648         tree gnu_string_index_type
2649           = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2650                                       (TYPE_DOMAIN (gnu_string_array_type))));
2651         tree gnu_lower_bound
2652           = convert (gnu_string_index_type,
2653                      gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2654         int length = UI_To_Int (String_Literal_Length (gnat_entity));
2655         tree gnu_length = ssize_int (length - 1);
2656         tree gnu_upper_bound
2657           = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2658                              gnu_lower_bound,
2659                              convert (gnu_string_index_type, gnu_length));
2660         tree gnu_index_type
2661           = create_index_type (convert (sizetype, gnu_lower_bound),
2662                                convert (sizetype, gnu_upper_bound),
2663                                build_range_type (gnu_string_index_type,
2664                                                  gnu_lower_bound,
2665                                                  gnu_upper_bound),
2666                                gnat_entity);
2667
2668         gnu_type
2669           = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2670                               gnu_index_type);
2671         if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2672           TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2673         relate_alias_sets (gnu_type, gnu_string_type, ALIAS_SET_COPY);
2674       }
2675       break;
2676
2677     /* Record Types and Subtypes
2678
2679        The following fields are defined on record types:
2680
2681                 Has_Discriminants       True if the record has discriminants
2682                 First_Discriminant      Points to head of list of discriminants
2683                 First_Entity            Points to head of list of fields
2684                 Is_Tagged_Type          True if the record is tagged
2685
2686        Implementation of Ada records and discriminated records:
2687
2688        A record type definition is transformed into the equivalent of a C
2689        struct definition.  The fields that are the discriminants which are
2690        found in the Full_Type_Declaration node and the elements of the
2691        Component_List found in the Record_Type_Definition node.  The
2692        Component_List can be a recursive structure since each Variant of
2693        the Variant_Part of the Component_List has a Component_List.
2694
2695        Processing of a record type definition comprises starting the list of
2696        field declarations here from the discriminants and the calling the
2697        function components_to_record to add the rest of the fields from the
2698        component list and return the gnu type node.  The function
2699        components_to_record will call itself recursively as it traverses
2700        the tree.  */
2701
2702     case E_Record_Type:
2703       if (Has_Complex_Representation (gnat_entity))
2704         {
2705           gnu_type
2706             = build_complex_type
2707               (get_unpadded_type
2708                (Etype (Defining_Entity
2709                        (First (Component_Items
2710                                (Component_List
2711                                 (Type_Definition
2712                                  (Declaration_Node (gnat_entity)))))))));
2713
2714           break;
2715         }
2716
2717       {
2718         Node_Id full_definition = Declaration_Node (gnat_entity);
2719         Node_Id record_definition = Type_Definition (full_definition);
2720         Entity_Id gnat_field;
2721         tree gnu_field;
2722         tree gnu_field_list = NULL_TREE;
2723         tree gnu_get_parent;
2724         /* Set PACKED in keeping with gnat_to_gnu_field.  */
2725         int packed
2726           = Is_Packed (gnat_entity)
2727             ? 1
2728             : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2729               ? -1
2730               : (Known_Alignment (gnat_entity)
2731                  || (Strict_Alignment (gnat_entity)
2732                      && Known_Static_Esize (gnat_entity)))
2733                 ? -2
2734                 : 0;
2735         bool has_rep = Has_Specified_Layout (gnat_entity);
2736         bool all_rep = has_rep;
2737         bool is_extension
2738           = (Is_Tagged_Type (gnat_entity)
2739              && Nkind (record_definition) == N_Derived_Type_Definition);
2740
2741         /* See if all fields have a rep clause.  Stop when we find one
2742            that doesn't.  */
2743         for (gnat_field = First_Entity (gnat_entity);
2744              Present (gnat_field) && all_rep;
2745              gnat_field = Next_Entity (gnat_field))
2746           if ((Ekind (gnat_field) == E_Component
2747                || Ekind (gnat_field) == E_Discriminant)
2748               && No (Component_Clause (gnat_field)))
2749             all_rep = false;
2750
2751         /* If this is a record extension, go a level further to find the
2752            record definition.  Also, verify we have a Parent_Subtype.  */
2753         if (is_extension)
2754           {
2755             if (!type_annotate_only
2756                 || Present (Record_Extension_Part (record_definition)))
2757               record_definition = Record_Extension_Part (record_definition);
2758
2759             gcc_assert (type_annotate_only
2760                         || Present (Parent_Subtype (gnat_entity)));
2761           }
2762
2763         /* Make a node for the record.  If we are not defining the record,
2764            suppress expanding incomplete types.  */
2765         gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2766         TYPE_NAME (gnu_type) = gnu_entity_name;
2767         TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2768
2769         if (!definition)
2770           defer_incomplete_level++, this_deferred = true;
2771
2772         /* If both a size and rep clause was specified, put the size in
2773            the record type now so that it can get the proper mode.  */
2774         if (has_rep && Known_Esize (gnat_entity))
2775           TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2776
2777         /* Always set the alignment here so that it can be used to
2778            set the mode, if it is making the alignment stricter.  If
2779            it is invalid, it will be checked again below.  If this is to
2780            be Atomic, choose a default alignment of a word unless we know
2781            the size and it's smaller.  */
2782         if (Known_Alignment (gnat_entity))
2783           TYPE_ALIGN (gnu_type)
2784             = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2785         else if (Is_Atomic (gnat_entity))
2786           TYPE_ALIGN (gnu_type)
2787             = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2788         /* If a type needs strict alignment, the minimum size will be the
2789            type size instead of the RM size (see validate_size).  Cap the
2790            alignment, lest it causes this type size to become too large.  */
2791         else if (Strict_Alignment (gnat_entity)
2792                  && Known_Static_Esize (gnat_entity))
2793           {
2794             unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2795             unsigned int raw_align = raw_size & -raw_size;
2796             if (raw_align < BIGGEST_ALIGNMENT)
2797               TYPE_ALIGN (gnu_type) = raw_align;
2798           }
2799         else
2800           TYPE_ALIGN (gnu_type) = 0;
2801
2802         /* If we have a Parent_Subtype, make a field for the parent.  If
2803            this record has rep clauses, force the position to zero.  */
2804         if (Present (Parent_Subtype (gnat_entity)))
2805           {
2806             Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2807             tree gnu_parent;
2808
2809             /* A major complexity here is that the parent subtype will
2810                reference our discriminants in its Discriminant_Constraint
2811                list.  But those must reference the parent component of this
2812                record which is of the parent subtype we have not built yet!
2813                To break the circle we first build a dummy COMPONENT_REF which
2814                represents the "get to the parent" operation and initialize
2815                each of those discriminants to a COMPONENT_REF of the above
2816                dummy parent referencing the corresponding discriminant of the
2817                base type of the parent subtype.  */
2818             gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2819                                      build0 (PLACEHOLDER_EXPR, gnu_type),
2820                                      build_decl (FIELD_DECL, NULL_TREE,
2821                                                  void_type_node),
2822                                      NULL_TREE);
2823
2824             if (Has_Discriminants (gnat_entity))
2825               for (gnat_field = First_Stored_Discriminant (gnat_entity);
2826                    Present (gnat_field);
2827                    gnat_field = Next_Stored_Discriminant (gnat_field))
2828                 if (Present (Corresponding_Discriminant (gnat_field)))
2829                   save_gnu_tree
2830                     (gnat_field,
2831                      build3 (COMPONENT_REF,
2832                              get_unpadded_type (Etype (gnat_field)),
2833                              gnu_get_parent,
2834                              gnat_to_gnu_field_decl (Corresponding_Discriminant
2835                                                      (gnat_field)),
2836                              NULL_TREE),
2837                      true);
2838
2839             /* Then we build the parent subtype.  If it has discriminants but
2840                the type itself has unknown discriminants, this means that it
2841                doesn't contain information about how the discriminants are
2842                derived from those of the ancestor type, so it cannot be used
2843                directly.  Instead it is built by cloning the parent subtype
2844                of the underlying record view of the type, for which the above
2845                derivation of discriminants has been made explicit.  */
2846             if (Has_Discriminants (gnat_parent)
2847                 && Has_Unknown_Discriminants (gnat_entity))
2848               {
2849                 Entity_Id gnat_uview = Underlying_Record_View (gnat_entity);
2850
2851                 /* If we are defining the type, the underlying record
2852                    view must already have been elaborated at this point.
2853                    Otherwise do it now as its parent subtype cannot be
2854                    technically elaborated on its own.  */
2855                 if (definition)
2856                   gcc_assert (present_gnu_tree (gnat_uview));
2857                 else
2858                   gnat_to_gnu_entity (gnat_uview, NULL_TREE, 0);
2859
2860                 gnu_parent = gnat_to_gnu_type (Parent_Subtype (gnat_uview));
2861
2862                 /* Substitute the "get to the parent" of the type for that
2863                    of its underlying record view in the cloned type.  */
2864                 for (gnat_field = First_Stored_Discriminant (gnat_uview);
2865                      Present (gnat_field);
2866                      gnat_field = Next_Stored_Discriminant (gnat_field))
2867                   if (Present (Corresponding_Discriminant (gnat_field)))
2868                     {
2869                       tree gnu_field = gnat_to_gnu_field_decl (gnat_field);
2870                       tree gnu_ref
2871                         = build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2872                                   gnu_get_parent, gnu_field, NULL_TREE);
2873                       gnu_parent
2874                         = substitute_in_type (gnu_parent, gnu_field, gnu_ref);
2875                     }
2876               }
2877             else
2878               gnu_parent = gnat_to_gnu_type (gnat_parent);
2879
2880             /* Finally we fix up both kinds of twisted COMPONENT_REF we have
2881                initially built.  The discriminants must reference the fields
2882                of the parent subtype and not those of its base type for the
2883                placeholder machinery to properly work.  */
2884             if (Has_Discriminants (gnat_entity))
2885               for (gnat_field = First_Stored_Discriminant (gnat_entity);
2886                    Present (gnat_field);
2887                    gnat_field = Next_Stored_Discriminant (gnat_field))
2888                 if (Present (Corresponding_Discriminant (gnat_field)))
2889                   {
2890                     Entity_Id field = Empty;
2891                     for (field = First_Stored_Discriminant (gnat_parent);
2892                          Present (field);
2893                          field = Next_Stored_Discriminant (field))
2894                       if (same_discriminant_p (gnat_field, field))
2895                         break;
2896                     gcc_assert (Present (field));
2897                     TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2898                       = gnat_to_gnu_field_decl (field);
2899                   }
2900
2901             /* The "get to the parent" COMPONENT_REF must be given its
2902                proper type...  */
2903             TREE_TYPE (gnu_get_parent) = gnu_parent;
2904
2905             /* ...and reference the _parent field of this record.  */
2906             gnu_field_list
2907               = create_field_decl (get_identifier
2908                                    (Get_Name_String (Name_uParent)),
2909                                    gnu_parent, gnu_type, 0,
2910                                    has_rep ? TYPE_SIZE (gnu_parent) : 0,
2911                                    has_rep ? bitsize_zero_node : 0, 1);
2912             DECL_INTERNAL_P (gnu_field_list) = 1;
2913             TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2914           }
2915
2916         /* Make the fields for the discriminants and put them into the record
2917            unless it's an Unchecked_Union.  */
2918         if (Has_Discriminants (gnat_entity))
2919           for (gnat_field = First_Stored_Discriminant (gnat_entity);
2920                Present (gnat_field);
2921                gnat_field = Next_Stored_Discriminant (gnat_field))
2922             {
2923               /* If this is a record extension and this discriminant
2924                  is the renaming of another discriminant, we've already
2925                  handled the discriminant above.  */
2926               if (Present (Parent_Subtype (gnat_entity))
2927                   && Present (Corresponding_Discriminant (gnat_field)))
2928                 continue;
2929
2930               gnu_field
2931                 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2932
2933               /* Make an expression using a PLACEHOLDER_EXPR from the
2934                  FIELD_DECL node just created and link that with the
2935                  corresponding GNAT defining identifier.  Then add to the
2936                  list of fields.  */
2937               save_gnu_tree (gnat_field,
2938                              build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2939                                      build0 (PLACEHOLDER_EXPR,
2940                                              DECL_CONTEXT (gnu_field)),
2941                                      gnu_field, NULL_TREE),
2942                              true);
2943
2944               if (!Is_Unchecked_Union (gnat_entity))
2945                 {
2946                   TREE_CHAIN (gnu_field) = gnu_field_list;
2947                   gnu_field_list = gnu_field;
2948                 }
2949             }
2950
2951         /* Put the discriminants into the record (backwards), so we can
2952            know the appropriate discriminant to use for the names of the
2953            variants.  */
2954         TYPE_FIELDS (gnu_type) = gnu_field_list;
2955
2956         /* Add the listed fields into the record and finish it up.  */
2957         components_to_record (gnu_type, Component_List (record_definition),
2958                               gnu_field_list, packed, definition, NULL,
2959                               false, all_rep, false,
2960                               Is_Unchecked_Union (gnat_entity));
2961
2962         /* We used to remove the associations of the discriminants and
2963            _Parent for validity checking, but we may need them if there's
2964            Freeze_Node for a subtype used in this record.  */
2965         TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2966         TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2967
2968         /* If it is a tagged record force the type to BLKmode to insure
2969            that these objects will always be placed in memory.  Do the
2970            same thing for limited record types.  */
2971         if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2972           SET_TYPE_MODE (gnu_type, BLKmode);
2973
2974         /* Fill in locations of fields.  */
2975         annotate_rep (gnat_entity, gnu_type);
2976
2977         /* If there are any entities in the chain corresponding to
2978            components that we did not elaborate, ensure we elaborate their
2979            types if they are Itypes.  */
2980         for (gnat_temp = First_Entity (gnat_entity);
2981              Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2982           if ((Ekind (gnat_temp) == E_Component
2983                || Ekind (gnat_temp) == E_Discriminant)
2984               && Is_Itype (Etype (gnat_temp))
2985               && !present_gnu_tree (gnat_temp))
2986             gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2987       }
2988       break;
2989
2990     case E_Class_Wide_Subtype:
2991       /* If an equivalent type is present, that is what we should use.
2992          Otherwise, fall through to handle this like a record subtype
2993          since it may have constraints.  */
2994       if (gnat_equiv_type != gnat_entity)
2995         {
2996           gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2997           maybe_present = true;
2998           break;
2999         }
3000
3001       /* ... fall through ... */
3002
3003     case E_Record_Subtype:
3004
3005       /* If Cloned_Subtype is Present it means this record subtype has
3006          identical layout to that type or subtype and we should use
3007          that GCC type for this one.  The front end guarantees that
3008          the component list is shared.  */
3009       if (Present (Cloned_Subtype (gnat_entity)))
3010         {
3011           gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
3012                                          NULL_TREE, 0);
3013           maybe_present = true;
3014         }
3015
3016       /* Otherwise, first ensure the base type is elaborated.  Then, if we are
3017          changing the type, make a new type with each field having the
3018          type of the field in the new subtype but having the position
3019          computed by transforming every discriminant reference according
3020          to the constraints.  We don't see any difference between
3021          private and nonprivate type here since derivations from types should
3022          have been deferred until the completion of the private type.  */
3023       else
3024         {
3025           Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
3026           tree gnu_base_type;
3027           tree gnu_orig_type;
3028
3029           if (!definition)
3030             defer_incomplete_level++, this_deferred = true;
3031
3032           /* Get the base type initially for its alignment and sizes.  But
3033              if it is a padded type, we do all the other work with the
3034              unpadded type.  */
3035           gnu_base_type = gnat_to_gnu_type (gnat_base_type);
3036
3037           if (TREE_CODE (gnu_base_type) == RECORD_TYPE
3038               && TYPE_IS_PADDING_P (gnu_base_type))
3039             gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
3040           else
3041             gnu_type = gnu_orig_type = gnu_base_type;
3042
3043           if (present_gnu_tree (gnat_entity))
3044             {
3045               maybe_present = true;
3046               break;
3047             }
3048
3049           /* When the type has discriminants, and these discriminants
3050              affect the shape of what it built, factor them in.
3051
3052              If we are making a subtype of an Unchecked_Union (must be an
3053              Itype), just return the type.
3054
3055              We can't just use Is_Constrained because private subtypes without
3056              discriminants of full types with discriminants with default
3057              expressions are Is_Constrained but aren't constrained!  */
3058
3059           if (IN (Ekind (gnat_base_type), Record_Kind)
3060               && !Is_For_Access_Subtype (gnat_entity)
3061               && !Is_Unchecked_Union (gnat_base_type)
3062               && Is_Constrained (gnat_entity)
3063               && Stored_Constraint (gnat_entity) != No_Elist
3064               && Present (Discriminant_Constraint (gnat_entity)))
3065             {
3066               Entity_Id gnat_field;
3067               tree gnu_field_list = 0;
3068               tree gnu_pos_list
3069                 = compute_field_positions (gnu_orig_type, NULL_TREE,
3070                                            size_zero_node, bitsize_zero_node,
3071                                            BIGGEST_ALIGNMENT);
3072               tree gnu_subst_list
3073                 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
3074                                      definition);
3075               tree gnu_temp;
3076
3077               gnu_type = make_node (RECORD_TYPE);
3078               TYPE_NAME (gnu_type) = gnu_entity_name;
3079               TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
3080
3081               /* Set the size, alignment and alias set of the new type to
3082                  match that of the old one, doing required substitutions.
3083                  We do it this early because we need the size of the new
3084                  type below to discard old fields if necessary.  */
3085               TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3086               TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3087               SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3088               TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3089               relate_alias_sets (gnu_type, gnu_base_type, ALIAS_SET_COPY);
3090
3091               if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3092                 for (gnu_temp = gnu_subst_list;
3093                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3094                   TYPE_SIZE (gnu_type)
3095                     = substitute_in_expr (TYPE_SIZE (gnu_type),
3096                                           TREE_PURPOSE (gnu_temp),
3097                                           TREE_VALUE (gnu_temp));
3098
3099               if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3100                 for (gnu_temp = gnu_subst_list;
3101                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3102                   TYPE_SIZE_UNIT (gnu_type)
3103                     = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3104                                           TREE_PURPOSE (gnu_temp),
3105                                           TREE_VALUE (gnu_temp));
3106
3107               if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3108                 for (gnu_temp = gnu_subst_list;
3109                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3110                   SET_TYPE_ADA_SIZE
3111                     (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3112                                                    TREE_PURPOSE (gnu_temp),
3113                                                    TREE_VALUE (gnu_temp)));
3114
3115               for (gnat_field = First_Entity (gnat_entity);
3116                    Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3117                 if ((Ekind (gnat_field) == E_Component
3118                      || Ekind (gnat_field) == E_Discriminant)
3119                     && (Underlying_Type (Scope (Original_Record_Component
3120                                                 (gnat_field)))
3121                         == gnat_base_type)
3122                     && (No (Corresponding_Discriminant (gnat_field))
3123                         || !Is_Tagged_Type (gnat_base_type)))
3124                   {
3125                     tree gnu_old_field
3126                       = gnat_to_gnu_field_decl (Original_Record_Component
3127                                                 (gnat_field));
3128                     tree gnu_offset
3129                       = TREE_VALUE (purpose_member (gnu_old_field,
3130                                                     gnu_pos_list));
3131                     tree gnu_pos = TREE_PURPOSE (gnu_offset);
3132                     tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3133                     tree gnu_field_type
3134                       = gnat_to_gnu_type (Etype (gnat_field));
3135                     tree gnu_size = TYPE_SIZE (gnu_field_type);
3136                     tree gnu_new_pos = NULL_TREE;
3137                     unsigned int offset_align
3138                       = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
3139                                       1);
3140                     tree gnu_field;
3141
3142                     /* If there was a component clause, the field types must be
3143                        the same for the type and subtype, so copy the data from
3144                        the old field to avoid recomputation here.  Also if the
3145                        field is justified modular and the optimization in
3146                        gnat_to_gnu_field was applied.  */
3147                     if (Present (Component_Clause
3148                                  (Original_Record_Component (gnat_field)))
3149                         || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3150                             && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3151                             && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3152                                == TREE_TYPE (gnu_old_field)))
3153                       {
3154                         gnu_size = DECL_SIZE (gnu_old_field);
3155                         gnu_field_type = TREE_TYPE (gnu_old_field);
3156                       }
3157
3158                     /* If the old field was packed and of constant size, we
3159                        have to get the old size here, as it might differ from
3160                        what the Etype conveys and the latter might overlap
3161                        onto the following field.  Try to arrange the type for
3162                        possible better packing along the way.  */
3163                     else if (DECL_PACKED (gnu_old_field)
3164                              && TREE_CODE (DECL_SIZE (gnu_old_field))
3165                                 == INTEGER_CST)
3166                       {
3167                         gnu_size = DECL_SIZE (gnu_old_field);
3168                         if (TREE_CODE (gnu_field_type) == RECORD_TYPE
3169                             && !TYPE_IS_FAT_POINTER_P (gnu_field_type)
3170                             && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3171                           gnu_field_type
3172                             = make_packable_type (gnu_field_type, true);
3173                       }
3174
3175                     if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3176                       for (gnu_temp = gnu_subst_list;
3177                            gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3178                         gnu_pos = substitute_in_expr (gnu_pos,
3179                                                       TREE_PURPOSE (gnu_temp),
3180                                                       TREE_VALUE (gnu_temp));
3181
3182                     /* If the position is now a constant, we can set it as the
3183                        position of the field when we make it.  Otherwise, we need
3184                        to deal with it specially below.  */
3185                     if (TREE_CONSTANT (gnu_pos))
3186                       {
3187                         gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3188
3189                         /* Discard old fields that are outside the new type.
3190                            This avoids confusing code scanning it to decide
3191                            how to pass it to functions on some platforms.  */
3192                         if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3193                             && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3194                             && !integer_zerop (gnu_size)
3195                             && !tree_int_cst_lt (gnu_new_pos,
3196                                                  TYPE_SIZE (gnu_type)))
3197                           continue;
3198                       }
3199
3200                     gnu_field
3201                       = create_field_decl
3202                         (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3203                          DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3204                          !DECL_NONADDRESSABLE_P (gnu_old_field));
3205
3206                     if (!TREE_CONSTANT (gnu_pos))
3207                       {
3208                         normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3209                         DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3210                         DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3211                         SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3212                         DECL_SIZE (gnu_field) = gnu_size;
3213                         DECL_SIZE_UNIT (gnu_field)
3214                           = convert (sizetype,
3215                                      size_binop (CEIL_DIV_EXPR, gnu_size,
3216                                                  bitsize_unit_node));
3217                         layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3218                       }
3219
3220                     DECL_INTERNAL_P (gnu_field)
3221                       = DECL_INTERNAL_P (gnu_old_field);
3222                     SET_DECL_ORIGINAL_FIELD
3223                       (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3224                                    ? DECL_ORIGINAL_FIELD (gnu_old_field)
3225                                    : gnu_old_field));
3226                     DECL_DISCRIMINANT_NUMBER (gnu_field)
3227                       = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3228                     TREE_THIS_VOLATILE (gnu_field)
3229                       = TREE_THIS_VOLATILE (gnu_old_field);
3230
3231                     /* To match the layout crafted in components_to_record, if
3232                        this is the _Tag field, put it before any discriminants
3233                        instead of after them as for all other fields.  */
3234                     if (Chars (gnat_field) == Name_uTag)
3235                       gnu_field_list = chainon (gnu_field_list, gnu_field);
3236                     else
3237                       {
3238                         TREE_CHAIN (gnu_field) = gnu_field_list;
3239                         gnu_field_list = gnu_field;
3240                       }
3241
3242                     save_gnu_tree (gnat_field, gnu_field, false);
3243                   }
3244
3245               /* Now go through the entities again looking for Itypes that
3246                  we have not elaborated but should (e.g., Etypes of fields
3247                  that have Original_Components).  */
3248               for (gnat_field = First_Entity (gnat_entity);
3249                    Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3250                 if ((Ekind (gnat_field) == E_Discriminant
3251                      || Ekind (gnat_field) == E_Component)
3252                     && !present_gnu_tree (Etype (gnat_field)))
3253                   gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3254
3255               /* Do not finalize it since we're going to modify it below.  */
3256               gnu_field_list = nreverse (gnu_field_list);
3257               finish_record_type (gnu_type, gnu_field_list, 2, true);
3258
3259               /* Finalize size and mode.  */
3260               TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
3261               TYPE_SIZE_UNIT (gnu_type)
3262                 = variable_size (TYPE_SIZE_UNIT (gnu_type));
3263
3264         &