OSDN Git Service

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