OSDN Git Service

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