OSDN Git Service

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