OSDN Git Service

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