OSDN Git Service

* gcc-interface/decl.c (gnat_to_gnu_entity) <E_String_Literal_Subtype>:
[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-2008, 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 "convert.h"
34 #include "ggc.h"
35 #include "obstack.h"
36 #include "target.h"
37 #include "expr.h"
38
39 #include "ada.h"
40 #include "types.h"
41 #include "atree.h"
42 #include "elists.h"
43 #include "namet.h"
44 #include "nlists.h"
45 #include "repinfo.h"
46 #include "snames.h"
47 #include "stringt.h"
48 #include "uintp.h"
49 #include "fe.h"
50 #include "sinfo.h"
51 #include "einfo.h"
52 #include "hashtab.h"
53 #include "ada-tree.h"
54 #include "gigi.h"
55
56 #ifndef MAX_FIXED_MODE_SIZE
57 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
58 #endif
59
60 /* Convention_Stdcall should be processed in a specific way on Windows targets
61    only.  The macro below is a helper to avoid having to check for a Windows
62    specific attribute throughout this unit.  */
63
64 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
65 #define Has_Stdcall_Convention(E) (Convention (E) == Convention_Stdcall)
66 #else
67 #define Has_Stdcall_Convention(E) (0)
68 #endif
69
70 /* Stack realignment for functions with foreign conventions is provided on a
71    per back-end basis now, as it is handled by the prologue expanders and not
72    as part of the function's body any more.  It might be requested by way of a
73    dedicated function type attribute on the targets that support it.
74
75    We need a way to avoid setting the attribute on the targets that don't
76    support it and use FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN for this purpose.
77
78    It is defined on targets where the circuitry is available, and indicates
79    whether the realignment is needed for 'main'.  We use this to decide for
80    foreign subprograms as well.
81
82    It is not defined on targets where the circuitry is not implemented, and
83    we just never set the attribute in these cases.
84
85    Whether it is defined on all targets that would need it in theory is
86    not entirely clear.  We currently trust the base GCC settings for this
87    purpose.  */
88
89 #ifndef FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
90 #define FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN 0
91 #endif
92
93 struct incomplete
94 {
95   struct incomplete *next;
96   tree old_type;
97   Entity_Id full_type;
98 };
99
100 /* These variables are used to defer recursively expanding incomplete types
101    while we are processing an array, a record or a subprogram type.  */
102 static int defer_incomplete_level = 0;
103 static struct incomplete *defer_incomplete_list;
104
105 /* This variable is used to delay expanding From_With_Type types until the
106    end of the spec.  */
107 static struct incomplete *defer_limited_with;
108
109 /* These variables are used to defer finalizing types.  The element of the
110    list is the TYPE_DECL associated with the type.  */
111 static int defer_finalize_level = 0;
112 static VEC (tree,heap) *defer_finalize_list;
113
114 /* A hash table used to cache the result of annotate_value.  */
115 static GTY ((if_marked ("tree_int_map_marked_p"),
116              param_is (struct tree_int_map))) htab_t annotate_value_cache;
117
118 static void copy_alias_set (tree, tree);
119 static tree substitution_list (Entity_Id, Entity_Id, tree, bool);
120 static bool allocatable_size_p (tree, bool);
121 static void prepend_one_attribute_to (struct attrib **,
122                                       enum attr_type, tree, tree, Node_Id);
123 static void prepend_attributes (Entity_Id, struct attrib **);
124 static tree elaborate_expression (Node_Id, Entity_Id, tree, bool, bool, bool);
125 static bool is_variable_size (tree);
126 static tree elaborate_expression_1 (Node_Id, Entity_Id, tree, tree,
127                                     bool, bool);
128 static tree make_packable_type (tree, bool);
129 static tree gnat_to_gnu_field (Entity_Id, tree, int, bool);
130 static tree gnat_to_gnu_param (Entity_Id, Mechanism_Type, Entity_Id, bool,
131                                bool *);
132 static bool same_discriminant_p (Entity_Id, Entity_Id);
133 static bool array_type_has_nonaliased_component (Entity_Id, tree);
134 static void components_to_record (tree, Node_Id, tree, int, bool, tree *,
135                                   bool, bool, bool, bool);
136 static Uint annotate_value (tree);
137 static void annotate_rep (Entity_Id, tree);
138 static tree compute_field_positions (tree, tree, tree, tree, unsigned int);
139 static tree validate_size (Uint, tree, Entity_Id, enum tree_code, bool, bool);
140 static void set_rm_size (Uint, tree, Entity_Id);
141 static tree make_type_from_size (tree, tree, bool);
142 static unsigned int validate_alignment (Uint, Entity_Id, unsigned int);
143 static unsigned int ceil_alignment (unsigned HOST_WIDE_INT);
144 static void check_ok_for_atomic (tree, Entity_Id, bool);
145 static int compatible_signatures_p (tree ftype1, tree ftype2);
146 static void rest_of_type_decl_compilation_no_defer (tree);
147
148 /* Return true if GNAT_ADDRESS is a compile time known value.
149    In particular catch System'To_Address.  */
150
151 static bool
152 compile_time_known_address_p (Node_Id gnat_address)
153 {
154   return ((Nkind (gnat_address) == N_Unchecked_Type_Conversion
155            && Compile_Time_Known_Value (Expression (gnat_address)))
156           || Compile_Time_Known_Value (gnat_address));
157 }
158
159 /* Given GNAT_ENTITY, an entity in the incoming GNAT tree, return a
160    GCC type corresponding to that entity.  GNAT_ENTITY is assumed to
161    refer to an Ada type.  */
162
163 tree
164 gnat_to_gnu_type (Entity_Id gnat_entity)
165 {
166   tree gnu_decl;
167
168   /* The back end never attempts to annotate generic types */
169   if (Is_Generic_Type (gnat_entity) && type_annotate_only)
170      return void_type_node;
171
172   /* Convert the ada entity type into a GCC TYPE_DECL node.  */
173   gnu_decl = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
174   gcc_assert (TREE_CODE (gnu_decl) == TYPE_DECL);
175   return TREE_TYPE (gnu_decl);
176 }
177 \f
178 /* Given GNAT_ENTITY, a GNAT defining identifier node, which denotes some Ada
179    entity, this routine returns the equivalent GCC tree for that entity
180    (an ..._DECL node) and associates the ..._DECL node with the input GNAT
181    defining identifier.
182
183    If GNAT_ENTITY is a variable or a constant declaration, GNU_EXPR gives its
184    initial value (in GCC tree form).  This is optional for variables.
185    For renamed entities, GNU_EXPR gives the object being renamed.
186
187    DEFINITION is nonzero if this call is intended for a definition.  This is
188    used for separate compilation where it necessary to know whether an
189    external declaration or a definition should be created if the GCC equivalent
190    was not created previously.  The value of 1 is normally used for a nonzero
191    DEFINITION, but a value of 2 is used in special circumstances, defined in
192    the code.  */
193
194 tree
195 gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, int definition)
196 {
197   Entity_Id gnat_equiv_type = Gigi_Equivalent_Type (gnat_entity);
198   tree gnu_entity_id;
199   tree gnu_type = NULL_TREE;
200   /* Contains the gnu XXXX_DECL tree node which is equivalent to the input
201      GNAT tree.  This node will be associated with the GNAT node by calling
202      the save_gnu_tree routine at the end of the `switch' statement.  */
203   tree gnu_decl = NULL_TREE;
204   /* true if we have already saved gnu_decl as a gnat association.  */
205   bool saved = false;
206   /* Nonzero if we incremented defer_incomplete_level.  */
207   bool this_deferred = false;
208   /* Nonzero if we incremented force_global.  */
209   bool this_global = false;
210   /* Nonzero if we should check to see if elaborated during processing.  */
211   bool maybe_present = false;
212   /* Nonzero if we made GNU_DECL and its type here.  */
213   bool this_made_decl = false;
214   struct attrib *attr_list = NULL;
215   bool debug_info_p = (Needs_Debug_Info (gnat_entity)
216                        || debug_info_level == DINFO_LEVEL_VERBOSE);
217   Entity_Kind kind = Ekind (gnat_entity);
218   Entity_Id gnat_temp;
219   unsigned int esize
220     = ((Known_Esize (gnat_entity)
221         && UI_Is_In_Int_Range (Esize (gnat_entity)))
222        ? MIN (UI_To_Int (Esize (gnat_entity)),
223               IN (kind, Float_Kind)
224               ? fp_prec_to_size (LONG_DOUBLE_TYPE_SIZE)
225               : IN (kind, Access_Kind) ? POINTER_SIZE * 2
226               : LONG_LONG_TYPE_SIZE)
227        : LONG_LONG_TYPE_SIZE);
228   tree gnu_size = 0;
229   bool imported_p
230     = (Is_Imported (gnat_entity) && No (Address_Clause (gnat_entity)));
231   unsigned int align = 0;
232
233   /* Since a use of an Itype is a definition, process it as such if it
234      is not in a with'ed unit.  */
235
236   if (!definition && Is_Itype (gnat_entity)
237       && !present_gnu_tree (gnat_entity)
238       && In_Extended_Main_Code_Unit (gnat_entity))
239     {
240       /* Ensure that we are in a subprogram mentioned in the Scope
241          chain of this entity, our current scope is global,
242          or that we encountered a task or entry (where we can't currently
243          accurately check scoping).  */
244       if (!current_function_decl
245           || DECL_ELABORATION_PROC_P (current_function_decl))
246         {
247           process_type (gnat_entity);
248           return get_gnu_tree (gnat_entity);
249         }
250
251       for (gnat_temp = Scope (gnat_entity);
252            Present (gnat_temp); gnat_temp = Scope (gnat_temp))
253         {
254           if (Is_Type (gnat_temp))
255             gnat_temp = Underlying_Type (gnat_temp);
256
257           if (Ekind (gnat_temp) == E_Subprogram_Body)
258             gnat_temp
259               = Corresponding_Spec (Parent (Declaration_Node (gnat_temp)));
260
261           if (IN (Ekind (gnat_temp), Subprogram_Kind)
262               && Present (Protected_Body_Subprogram (gnat_temp)))
263             gnat_temp = Protected_Body_Subprogram (gnat_temp);
264
265           if (Ekind (gnat_temp) == E_Entry
266               || Ekind (gnat_temp) == E_Entry_Family
267               || Ekind (gnat_temp) == E_Task_Type
268               || (IN (Ekind (gnat_temp), Subprogram_Kind)
269                   && present_gnu_tree (gnat_temp)
270                   && (current_function_decl
271                       == gnat_to_gnu_entity (gnat_temp, NULL_TREE, 0))))
272             {
273               process_type (gnat_entity);
274               return get_gnu_tree (gnat_entity);
275             }
276         }
277
278       /* This abort means the entity "gnat_entity" has an incorrect scope,
279          i.e. that its scope does not correspond to the subprogram in which
280          it is declared */
281       gcc_unreachable ();
282     }
283
284   /* If this is entity 0, something went badly wrong.  */
285   gcc_assert (Present (gnat_entity));
286
287   /* If we've already processed this entity, return what we got last time.
288      If we are defining the node, we should not have already processed it.
289      In that case, we will abort below when we try to save a new GCC tree for
290      this object.   We also need to handle the case of getting a dummy type
291      when a Full_View exists.  */
292
293   if (present_gnu_tree (gnat_entity)
294       && (!definition || (Is_Type (gnat_entity) && imported_p)))
295     {
296       gnu_decl = get_gnu_tree (gnat_entity);
297
298       if (TREE_CODE (gnu_decl) == TYPE_DECL
299           && TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl))
300           && IN (kind, Incomplete_Or_Private_Kind)
301           && Present (Full_View (gnat_entity)))
302         {
303           gnu_decl = gnat_to_gnu_entity (Full_View (gnat_entity),
304                                          NULL_TREE, 0);
305
306           save_gnu_tree (gnat_entity, NULL_TREE, false);
307           save_gnu_tree (gnat_entity, gnu_decl, false);
308         }
309
310       return gnu_decl;
311     }
312
313   /* If this is a numeric or enumeral type, or an access type, a nonzero
314      Esize must be specified unless it was specified by the programmer.  */
315   gcc_assert (!Unknown_Esize (gnat_entity)
316               || Has_Size_Clause (gnat_entity)
317               || (!IN (kind, Numeric_Kind) && !IN (kind, Enumeration_Kind)
318                   && (!IN (kind, Access_Kind)
319                       || kind == E_Access_Protected_Subprogram_Type
320                       || kind == E_Anonymous_Access_Protected_Subprogram_Type
321                       || kind == E_Access_Subtype)));
322
323   /* Likewise, RM_Size must be specified for all discrete and fixed-point
324      types.  */
325   gcc_assert (!IN (kind, Discrete_Or_Fixed_Point_Kind)
326               || !Unknown_RM_Size (gnat_entity));
327
328   /* Get the name of the entity and set up the line number and filename of
329      the original definition for use in any decl we make.  */
330   gnu_entity_id = get_entity_name (gnat_entity);
331   Sloc_to_locus (Sloc (gnat_entity), &input_location);
332
333   /* If we get here, it means we have not yet done anything with this
334      entity.  If we are not defining it here, it must be external,
335      otherwise we should have defined it already.  */
336   gcc_assert (definition || Is_Public (gnat_entity) || type_annotate_only
337               || kind == E_Discriminant || kind == E_Component
338               || kind == E_Label
339               || (kind == E_Constant && Present (Full_View (gnat_entity)))
340               || IN (kind, Type_Kind));
341
342   /* For cases when we are not defining (i.e., we are referencing from
343      another compilation unit) Public entities, show we are at global level
344      for the purpose of computing scopes.  Don't do this for components or
345      discriminants since the relevant test is whether or not the record is
346      being defined.  But do this for Imported functions or procedures in
347      all cases.  */
348   if ((!definition && Is_Public (gnat_entity)
349        && !Is_Statically_Allocated (gnat_entity)
350        && kind != E_Discriminant && kind != E_Component)
351       || (Is_Imported (gnat_entity)
352           && (kind == E_Function || kind == E_Procedure)))
353     force_global++, this_global = true;
354
355   /* Handle any attributes directly attached to the entity.  */
356   if (Has_Gigi_Rep_Item (gnat_entity))
357     prepend_attributes (gnat_entity, &attr_list);
358
359   /* Machine_Attributes on types are expected to be propagated to subtypes.
360      The corresponding Gigi_Rep_Items are only attached to the first subtype
361      though, so we handle the propagation here.  */
362   if (Is_Type (gnat_entity) && Base_Type (gnat_entity) != gnat_entity
363       && !Is_First_Subtype (gnat_entity)
364       && Has_Gigi_Rep_Item (First_Subtype (Base_Type (gnat_entity))))
365     prepend_attributes (First_Subtype (Base_Type (gnat_entity)), &attr_list);
366
367   switch (kind)
368     {
369     case E_Constant:
370       /* If this is a use of a deferred constant without address clause,
371          get its full definition.  */
372       if (!definition
373           && No (Address_Clause (gnat_entity))
374           && Present (Full_View (gnat_entity)))
375         {
376           gnu_decl
377             = gnat_to_gnu_entity (Full_View (gnat_entity), gnu_expr, 0);
378           saved = true;
379           break;
380         }
381
382       /* If we have an external constant that we are not defining, get the
383          expression that is was defined to represent.  We may throw that
384          expression away later if it is not a constant.  Do not retrieve the
385          expression if it is an aggregate or allocator, because in complex
386          instantiation contexts it may not be expanded  */
387       if (!definition
388           && Present (Expression (Declaration_Node (gnat_entity)))
389           && !No_Initialization (Declaration_Node (gnat_entity))
390           && (Nkind (Expression (Declaration_Node (gnat_entity)))
391               != N_Aggregate)
392           && (Nkind (Expression (Declaration_Node (gnat_entity)))
393               != N_Allocator))
394         gnu_expr = gnat_to_gnu (Expression (Declaration_Node (gnat_entity)));
395
396       /* Ignore deferred constant definitions without address clause since
397          they are processed fully in the front-end.  If No_Initialization
398          is set, this is not a deferred constant but a constant whose value
399          is built manually.  And constants that are renamings are handled
400          like variables.  */
401       if (definition
402           && !gnu_expr
403           && No (Address_Clause (gnat_entity))
404           && !No_Initialization (Declaration_Node (gnat_entity))
405           && No (Renamed_Object (gnat_entity)))
406         {
407           gnu_decl = error_mark_node;
408           saved = true;
409           break;
410         }
411
412       /* Ignore constant definitions already marked with the error node.  See
413          the N_Object_Declaration case of gnat_to_gnu for the rationale.  */
414       if (definition
415           && gnu_expr
416           && present_gnu_tree (gnat_entity)
417           && get_gnu_tree (gnat_entity) == error_mark_node)
418         {
419           maybe_present = true;
420           break;
421         }
422
423       goto object;
424
425     case E_Exception:
426       /* We used to special case VMS exceptions here to directly map them to
427          their associated condition code.  Since this code had to be masked
428          dynamically to strip off the severity bits, this caused trouble in
429          the GCC/ZCX case because the "type" pointers we store in the tables
430          have to be static.  We now don't special case here anymore, and let
431          the regular processing take place, which leaves us with a regular
432          exception data object for VMS exceptions too.  The condition code
433          mapping is taken care of by the front end and the bitmasking by the
434          runtime library.   */
435       goto object;
436
437     case E_Discriminant:
438     case E_Component:
439       {
440         /* The GNAT record where the component was defined.  */
441         Entity_Id gnat_record = Underlying_Type (Scope (gnat_entity));
442
443         /* If the variable is an inherited record component (in the case of
444            extended record types), just return the inherited entity, which
445            must be a FIELD_DECL.  Likewise for discriminants.
446            For discriminants of untagged records which have explicit
447            stored discriminants, return the entity for the corresponding
448            stored discriminant.  Also use Original_Record_Component
449            if the record has a private extension.  */
450
451         if (Present (Original_Record_Component (gnat_entity))
452             && Original_Record_Component (gnat_entity) != gnat_entity)
453           {
454             gnu_decl
455               = gnat_to_gnu_entity (Original_Record_Component (gnat_entity),
456                                     gnu_expr, definition);
457             saved = true;
458             break;
459           }
460
461         /* If the enclosing record has explicit stored discriminants,
462            then it is an untagged record.  If the Corresponding_Discriminant
463            is not empty then this must be a renamed discriminant and its
464            Original_Record_Component must point to the corresponding explicit
465            stored discriminant (i.e., we should have taken the previous
466            branch).  */
467
468         else if (Present (Corresponding_Discriminant (gnat_entity))
469                  && Is_Tagged_Type (gnat_record))
470           {
471             /* A tagged record has no explicit stored discriminants.  */
472
473             gcc_assert (First_Discriminant (gnat_record)
474                        == First_Stored_Discriminant (gnat_record));
475             gnu_decl
476               = gnat_to_gnu_entity (Corresponding_Discriminant (gnat_entity),
477                                     gnu_expr, definition);
478             saved = true;
479             break;
480           }
481
482         else if (Present (CR_Discriminant (gnat_entity))
483                  && type_annotate_only)
484           {
485             gnu_decl = gnat_to_gnu_entity (CR_Discriminant (gnat_entity),
486                                            gnu_expr, definition);
487             saved = true;
488             break;
489           }
490
491         /* If the enclosing record has explicit stored discriminants, then
492            it is an untagged record.  If the Corresponding_Discriminant
493            is not empty then this must be a renamed discriminant and its
494            Original_Record_Component must point to the corresponding explicit
495            stored discriminant (i.e., we should have taken the first
496            branch).  */
497
498         else if (Present (Corresponding_Discriminant (gnat_entity))
499                  && (First_Discriminant (gnat_record)
500                      != First_Stored_Discriminant (gnat_record)))
501           gcc_unreachable ();
502
503         /* Otherwise, if we are not defining this and we have no GCC type
504            for the containing record, make one for it.  Then we should
505            have made our own equivalent.  */
506         else if (!definition && !present_gnu_tree (gnat_record))
507           {
508             /* ??? If this is in a record whose scope is a protected
509                type and we have an Original_Record_Component, use it.
510                This is a workaround for major problems in protected type
511                handling.  */
512             Entity_Id Scop = Scope (Scope (gnat_entity));
513             if ((Is_Protected_Type (Scop)
514                  || (Is_Private_Type (Scop)
515                      && Present (Full_View (Scop))
516                      && Is_Protected_Type (Full_View (Scop))))
517                 && Present (Original_Record_Component (gnat_entity)))
518               {
519                 gnu_decl
520                   = gnat_to_gnu_entity (Original_Record_Component
521                                         (gnat_entity),
522                                         gnu_expr, 0);
523                 saved = true;
524                 break;
525               }
526
527             gnat_to_gnu_entity (Scope (gnat_entity), NULL_TREE, 0);
528             gnu_decl = get_gnu_tree (gnat_entity);
529             saved = true;
530             break;
531           }
532
533         else
534           /* Here we have no GCC type and this is a reference rather than a
535              definition.  This should never happen.  Most likely the cause is
536              reference before declaration in the gnat tree for gnat_entity.  */
537           gcc_unreachable ();
538       }
539
540     case E_Loop_Parameter:
541     case E_Out_Parameter:
542     case E_Variable:
543
544       /* Simple variables, loop variables, Out parameters, and exceptions.  */
545     object:
546       {
547         bool used_by_ref = false;
548         bool const_flag
549           = ((kind == E_Constant || kind == E_Variable)
550              && Is_True_Constant (gnat_entity)
551              && !Treat_As_Volatile (gnat_entity)
552              && (((Nkind (Declaration_Node (gnat_entity))
553                    == N_Object_Declaration)
554                   && Present (Expression (Declaration_Node (gnat_entity))))
555                  || Present (Renamed_Object (gnat_entity))));
556         bool inner_const_flag = const_flag;
557         bool static_p = Is_Statically_Allocated (gnat_entity);
558         bool mutable_p = false;
559         tree gnu_ext_name = NULL_TREE;
560         tree renamed_obj = NULL_TREE;
561         tree gnu_object_size;
562
563         if (Present (Renamed_Object (gnat_entity)) && !definition)
564           {
565             if (kind == E_Exception)
566               gnu_expr = gnat_to_gnu_entity (Renamed_Entity (gnat_entity),
567                                              NULL_TREE, 0);
568             else
569               gnu_expr = gnat_to_gnu (Renamed_Object (gnat_entity));
570           }
571
572         /* Get the type after elaborating the renamed object.  */
573         gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
574
575         /* For a debug renaming declaration, build a pure debug entity.  */
576         if (Present (Debug_Renaming_Link (gnat_entity)))
577           {
578             rtx addr;
579             gnu_decl = build_decl (VAR_DECL, gnu_entity_id, gnu_type);
580             /* The (MEM (CONST (0))) pattern is prescribed by STABS.  */
581             if (global_bindings_p ())
582               addr = gen_rtx_CONST (VOIDmode, const0_rtx);
583             else
584               addr = stack_pointer_rtx;
585             SET_DECL_RTL (gnu_decl, gen_rtx_MEM (Pmode, addr));
586             gnat_pushdecl (gnu_decl, gnat_entity);
587             break;
588           }
589
590         /* If this is a loop variable, its type should be the base type.
591            This is because the code for processing a loop determines whether
592            a normal loop end test can be done by comparing the bounds of the
593            loop against those of the base type, which is presumed to be the
594            size used for computation.  But this is not correct when the size
595            of the subtype is smaller than the type.  */
596         if (kind == E_Loop_Parameter)
597           gnu_type = get_base_type (gnu_type);
598
599         /* Reject non-renamed objects whose types are unconstrained arrays or
600            any object whose type is a dummy type or VOID_TYPE.  */
601
602         if ((TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE
603              && No (Renamed_Object (gnat_entity)))
604             || TYPE_IS_DUMMY_P (gnu_type)
605             || TREE_CODE (gnu_type) == VOID_TYPE)
606           {
607             gcc_assert (type_annotate_only);
608             if (this_global)
609               force_global--;
610             return error_mark_node;
611           }
612
613         /* If an alignment is specified, use it if valid.   Note that
614            exceptions are objects but don't have alignments.  We must do this
615            before we validate the size, since the alignment can affect the
616            size.  */
617         if (kind != E_Exception && Known_Alignment (gnat_entity))
618           {
619             gcc_assert (Present (Alignment (gnat_entity)));
620             align = validate_alignment (Alignment (gnat_entity), gnat_entity,
621                                         TYPE_ALIGN (gnu_type));
622             gnu_type = maybe_pad_type (gnu_type, NULL_TREE, align, gnat_entity,
623                                        "PAD", false, definition, true);
624           }
625
626         /* If we are defining the object, see if it has a Size value and
627            validate it if so.  If we are not defining the object and a Size
628            clause applies, simply retrieve the value.  We don't want to ignore
629            the clause and it is expected to have been validated already.  Then
630            get the new type, if any.  */
631         if (definition)
632           gnu_size = validate_size (Esize (gnat_entity), gnu_type,
633                                     gnat_entity, VAR_DECL, false,
634                                     Has_Size_Clause (gnat_entity));
635         else if (Has_Size_Clause (gnat_entity))
636           gnu_size = UI_To_gnu (Esize (gnat_entity), bitsizetype);
637
638         if (gnu_size)
639           {
640             gnu_type
641               = make_type_from_size (gnu_type, gnu_size,
642                                      Has_Biased_Representation (gnat_entity));
643
644             if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0))
645               gnu_size = NULL_TREE;
646           }
647
648         /* If this object has self-referential size, it must be a record with
649            a default value.  We are supposed to allocate an object of the
650            maximum size in this case unless it is a constant with an
651            initializing expression, in which case we can get the size from
652            that.  Note that the resulting size may still be a variable, so
653            this may end up with an indirect allocation.  */
654         if (No (Renamed_Object (gnat_entity))
655             && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
656           {
657             if (gnu_expr && kind == E_Constant)
658               {
659                 tree size = TYPE_SIZE (TREE_TYPE (gnu_expr));
660                 if (CONTAINS_PLACEHOLDER_P (size))
661                   {
662                     /* If the initializing expression is itself a constant,
663                        despite having a nominal type with self-referential
664                        size, we can get the size directly from it.  */
665                     if (TREE_CODE (gnu_expr) == COMPONENT_REF
666                         && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
667                            == RECORD_TYPE
668                         && TYPE_IS_PADDING_P
669                            (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
670                         && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == VAR_DECL
671                         && (TREE_READONLY (TREE_OPERAND (gnu_expr, 0))
672                             || DECL_READONLY_ONCE_ELAB
673                                (TREE_OPERAND (gnu_expr, 0))))
674                       gnu_size = DECL_SIZE (TREE_OPERAND (gnu_expr, 0));
675                     else
676                       gnu_size
677                         = SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, gnu_expr);
678                   }
679                 else
680                   gnu_size = size;
681               }
682             /* We may have no GNU_EXPR because No_Initialization is
683                set even though there's an Expression.  */
684             else if (kind == E_Constant
685                      && (Nkind (Declaration_Node (gnat_entity))
686                          == N_Object_Declaration)
687                      && Present (Expression (Declaration_Node (gnat_entity))))
688               gnu_size
689                 = TYPE_SIZE (gnat_to_gnu_type
690                              (Etype
691                               (Expression (Declaration_Node (gnat_entity)))));
692             else
693               {
694                 gnu_size = max_size (TYPE_SIZE (gnu_type), true);
695                 mutable_p = true;
696               }
697           }
698
699         /* If the size is zero bytes, make it one byte since some linkers have
700            trouble with zero-sized objects.  If the object will have a
701            template, that will make it nonzero so don't bother.  Also avoid
702            doing that for an object renaming or an object with an address
703            clause, as we would lose useful information on the view size
704            (e.g. for null array slices) and we are not allocating the object
705            here anyway.  */
706         if (((gnu_size
707               && integer_zerop (gnu_size)
708               && !TREE_OVERFLOW (gnu_size))
709              || (TYPE_SIZE (gnu_type)
710                  && integer_zerop (TYPE_SIZE (gnu_type))
711                  && !TREE_OVERFLOW (TYPE_SIZE (gnu_type))))
712             && (!Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
713                 || !Is_Array_Type (Etype (gnat_entity)))
714             && !Present (Renamed_Object (gnat_entity))
715             && !Present (Address_Clause (gnat_entity)))
716           gnu_size = bitsize_unit_node;
717
718         /* If this is an object with no specified size and alignment, and
719            if either it is atomic or we are not optimizing alignment for
720            space and it is composite and not an exception, an Out parameter
721            or a reference to another object, and the size of its type is a
722            constant, set the alignment to the smallest one which is not
723            smaller than the size, with an appropriate cap.  */
724         if (!gnu_size && align == 0
725             && (Is_Atomic (gnat_entity)
726                 || (!Optimize_Alignment_Space (gnat_entity)
727                     && kind != E_Exception
728                     && kind != E_Out_Parameter
729                     && Is_Composite_Type (Etype (gnat_entity))
730                     && !Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
731                     && !imported_p
732                     && No (Renamed_Object (gnat_entity))
733                     && No (Address_Clause (gnat_entity))))
734             && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST)
735           {
736             /* No point in jumping through all the hoops needed in order
737                to support BIGGEST_ALIGNMENT if we don't really have to.
738                So we cap to the smallest alignment that corresponds to
739                a known efficient memory access pattern of the target.  */
740             unsigned int align_cap = Is_Atomic (gnat_entity)
741                                      ? BIGGEST_ALIGNMENT
742                                      : get_mode_alignment (ptr_mode);
743
744             if (!host_integerp (TYPE_SIZE (gnu_type), 1)
745                 || compare_tree_int (TYPE_SIZE (gnu_type), align_cap) >= 0)
746               align = align_cap;
747             else
748               align = ceil_alignment (tree_low_cst (TYPE_SIZE (gnu_type), 1));
749
750             /* But make sure not to under-align the object.  */
751             if (align <= TYPE_ALIGN (gnu_type))
752               align = 0;
753
754             /* And honor the minimum valid atomic alignment, if any.  */
755 #ifdef MINIMUM_ATOMIC_ALIGNMENT
756             else if (align < MINIMUM_ATOMIC_ALIGNMENT)
757               align = MINIMUM_ATOMIC_ALIGNMENT;
758 #endif
759           }
760
761         /* If the object is set to have atomic components, find the component
762            type and validate it.
763
764            ??? Note that we ignore Has_Volatile_Components on objects; it's
765            not at all clear what to do in that case.  */
766
767         if (Has_Atomic_Components (gnat_entity))
768           {
769             tree gnu_inner = (TREE_CODE (gnu_type) == ARRAY_TYPE
770                               ? TREE_TYPE (gnu_type) : gnu_type);
771
772             while (TREE_CODE (gnu_inner) == ARRAY_TYPE
773                    && TYPE_MULTI_ARRAY_P (gnu_inner))
774               gnu_inner = TREE_TYPE (gnu_inner);
775
776             check_ok_for_atomic (gnu_inner, gnat_entity, true);
777           }
778
779         /* Now check if the type of the object allows atomic access.  Note
780            that we must test the type, even if this object has size and
781            alignment to allow such access, because we will be going
782            inside the padded record to assign to the object.  We could fix
783            this by always copying via an intermediate value, but it's not
784            clear it's worth the effort.  */
785         if (Is_Atomic (gnat_entity))
786           check_ok_for_atomic (gnu_type, gnat_entity, false);
787
788         /* If this is an aliased object with an unconstrained nominal subtype,
789            make a type that includes the template.  */
790         if (Is_Constr_Subt_For_UN_Aliased (Etype (gnat_entity))
791             && Is_Array_Type (Etype (gnat_entity))
792             && !type_annotate_only)
793         {
794           tree gnu_fat
795             = TREE_TYPE (gnat_to_gnu_type (Base_Type (Etype (gnat_entity))));
796
797           gnu_type
798             = build_unc_object_type_from_ptr (gnu_fat, gnu_type,
799                                      concat_id_with_name (gnu_entity_id,
800                                                           "UNC"));
801         }
802
803 #ifdef MINIMUM_ATOMIC_ALIGNMENT
804         /* If the size is a constant and no alignment is specified, force
805            the alignment to be the minimum valid atomic alignment.  The
806            restriction on constant size avoids problems with variable-size
807            temporaries; if the size is variable, there's no issue with
808            atomic access.  Also don't do this for a constant, since it isn't
809            necessary and can interfere with constant replacement.  Finally,
810            do not do it for Out parameters since that creates an
811            size inconsistency with In parameters.  */
812         if (align == 0 && MINIMUM_ATOMIC_ALIGNMENT > TYPE_ALIGN (gnu_type)
813             && !FLOAT_TYPE_P (gnu_type)
814             && !const_flag && No (Renamed_Object (gnat_entity))
815             && !imported_p && No (Address_Clause (gnat_entity))
816             && kind != E_Out_Parameter
817             && (gnu_size ? TREE_CODE (gnu_size) == INTEGER_CST
818                 : TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST))
819           align = MINIMUM_ATOMIC_ALIGNMENT;
820 #endif
821
822         /* Make a new type with the desired size and alignment, if needed.
823            But do not take into account alignment promotions to compute the
824            size of the object.  */
825         gnu_object_size = gnu_size ? gnu_size : TYPE_SIZE (gnu_type);
826         if (gnu_size || align > 0)
827           gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
828                                      "PAD", false, definition,
829                                      gnu_size ? true : false);
830
831         /* If this is a renaming, avoid as much as possible to create a new
832            object.  However, in several cases, creating it is required.
833            This processing needs to be applied to the raw expression so
834            as to make it more likely to rename the underlying object.  */
835         if (Present (Renamed_Object (gnat_entity)))
836           {
837             bool create_normal_object = false;
838
839             /* If the renamed object had padding, strip off the reference
840                to the inner object and reset our type.  */
841             if ((TREE_CODE (gnu_expr) == COMPONENT_REF
842                  && TREE_CODE (TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))
843                     == RECORD_TYPE
844                  && TYPE_IS_PADDING_P (TREE_TYPE (TREE_OPERAND (gnu_expr, 0))))
845                 /* Strip useless conversions around the object.  */
846                 || (TREE_CODE (gnu_expr) == NOP_EXPR
847                     && gnat_types_compatible_p
848                        (TREE_TYPE (gnu_expr),
849                         TREE_TYPE (TREE_OPERAND (gnu_expr, 0)))))
850               {
851                 gnu_expr = TREE_OPERAND (gnu_expr, 0);
852                 gnu_type = TREE_TYPE (gnu_expr);
853               }
854
855             /* Case 1: If this is a constant renaming stemming from a function
856                call, treat it as a normal object whose initial value is what
857                is being renamed.  RM 3.3 says that the result of evaluating a
858                function call is a constant object.  As a consequence, it can
859                be the inner object of a constant renaming.  In this case, the
860                renaming must be fully instantiated, i.e. it cannot be a mere
861                reference to (part of) an existing object.  */
862             if (const_flag)
863               {
864                 tree inner_object = gnu_expr;
865                 while (handled_component_p (inner_object))
866                   inner_object = TREE_OPERAND (inner_object, 0);
867                 if (TREE_CODE (inner_object) == CALL_EXPR)
868                   create_normal_object = true;
869               }
870
871             /* Otherwise, see if we can proceed with a stabilized version of
872                the renamed entity or if we need to make a new object.  */
873             if (!create_normal_object)
874               {
875                 tree maybe_stable_expr = NULL_TREE;
876                 bool stable = false;
877
878                 /* Case 2: If the renaming entity need not be materialized and
879                    the renamed expression is something we can stabilize, use
880                    that for the renaming.  At the global level, we can only do
881                    this if we know no SAVE_EXPRs need be made, because the
882                    expression we return might be used in arbitrary conditional
883                    branches so we must force the SAVE_EXPRs evaluation
884                    immediately and this requires a function context.  */
885                 if (!Materialize_Entity (gnat_entity)
886                     && (!global_bindings_p ()
887                         || (staticp (gnu_expr)
888                             && !TREE_SIDE_EFFECTS (gnu_expr))))
889                   {
890                     maybe_stable_expr
891                       = maybe_stabilize_reference (gnu_expr, true, &stable);
892
893                     if (stable)
894                       {
895                         gnu_decl = maybe_stable_expr;
896                         /* ??? No DECL_EXPR is created so we need to mark
897                            the expression manually lest it is shared.  */
898                         if (global_bindings_p ())
899                           mark_visited (&gnu_decl);
900                         save_gnu_tree (gnat_entity, gnu_decl, true);
901                         saved = true;
902                         break;
903                       }
904
905                     /* The stabilization failed.  Keep maybe_stable_expr
906                        untouched here to let the pointer case below know
907                        about that failure.  */
908                   }
909
910                 /* Case 3: If this is a constant renaming and creating a
911                    new object is allowed and cheap, treat it as a normal
912                    object whose initial value is what is being renamed.  */
913                 if (const_flag && Is_Elementary_Type (Etype (gnat_entity)))
914                   ;
915
916                 /* Case 4: Make this into a constant pointer to the object we
917                    are to rename and attach the object to the pointer if it is
918                    something we can stabilize.
919
920                    From the proper scope, attached objects will be referenced
921                    directly instead of indirectly via the pointer to avoid
922                    subtle aliasing problems with non-addressable entities.
923                    They have to be stable because we must not evaluate the
924                    variables in the expression every time the renaming is used.
925                    The pointer is called a "renaming" pointer in this case.
926
927                    In the rare cases where we cannot stabilize the renamed
928                    object, we just make a "bare" pointer, and the renamed
929                    entity is always accessed indirectly through it.  */
930                 else
931                   {
932                     gnu_type = build_reference_type (gnu_type);
933                     inner_const_flag = TREE_READONLY (gnu_expr);
934                     const_flag = true;
935
936                     /* If the previous attempt at stabilizing failed, there
937                        is no point in trying again and we reuse the result
938                        without attaching it to the pointer.  In this case it
939                        will only be used as the initializing expression of
940                        the pointer and thus needs no special treatment with
941                        regard to multiple evaluations.  */
942                     if (maybe_stable_expr)
943                       ;
944
945                     /* Otherwise, try to stabilize and attach the expression
946                        to the pointer if the stabilization succeeds.
947
948                        Note that this might introduce SAVE_EXPRs and we don't
949                        check whether we're at the global level or not.  This
950                        is fine since we are building a pointer initializer and
951                        neither the pointer nor the initializing expression can
952                        be accessed before the pointer elaboration has taken
953                        place in a correct program.
954
955                        These SAVE_EXPRs will be evaluated at the right place
956                        by either the evaluation of the initializer for the
957                        non-global case or the elaboration code for the global
958                        case, and will be attached to the elaboration procedure
959                        in the latter case.  */
960                     else
961                      {
962                         maybe_stable_expr
963                           = maybe_stabilize_reference (gnu_expr, true, &stable);
964
965                         if (stable)
966                           renamed_obj = maybe_stable_expr;
967
968                         /* Attaching is actually performed downstream, as soon
969                            as we have a VAR_DECL for the pointer we make.  */
970                       }
971
972                     gnu_expr
973                       = build_unary_op (ADDR_EXPR, gnu_type, maybe_stable_expr);
974
975                     gnu_size = NULL_TREE;
976                     used_by_ref = true;
977                   }
978               }
979           }
980
981         /* Make a volatile version of this object's type if we are to make
982            the object volatile.  We also interpret 13.3(19) conservatively
983            and disallow any optimizations for an object covered by it.  */
984         if ((Treat_As_Volatile (gnat_entity)
985              || (Is_Exported (gnat_entity)
986                  /* Exclude exported constants created by the compiler,
987                     which should boil down to static dispatch tables and
988                     make it possible to put them in read-only memory.  */
989                  && (Comes_From_Source (gnat_entity) || !const_flag))
990              || Is_Imported (gnat_entity)
991              || Present (Address_Clause (gnat_entity)))
992             && !TYPE_VOLATILE (gnu_type))
993           gnu_type = build_qualified_type (gnu_type,
994                                            (TYPE_QUALS (gnu_type)
995                                             | TYPE_QUAL_VOLATILE));
996
997         /* If we are defining an aliased object whose nominal subtype is
998            unconstrained, the object is a record that contains both the
999            template and the object.  If there is an initializer, it will
1000            have already been converted to the right type, but we need to
1001            create the template if there is no initializer.  */
1002         if (definition
1003             && !gnu_expr
1004             && TREE_CODE (gnu_type) == RECORD_TYPE
1005             && (TYPE_CONTAINS_TEMPLATE_P (gnu_type)
1006                 /* Beware that padding might have been introduced
1007                    via maybe_pad_type above.  */
1008                 || (TYPE_IS_PADDING_P (gnu_type)
1009                     && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1010                        == RECORD_TYPE
1011                     && TYPE_CONTAINS_TEMPLATE_P
1012                        (TREE_TYPE (TYPE_FIELDS (gnu_type))))))
1013           {
1014             tree template_field
1015               = TYPE_IS_PADDING_P (gnu_type)
1016                 ? TYPE_FIELDS (TREE_TYPE (TYPE_FIELDS (gnu_type)))
1017                 : TYPE_FIELDS (gnu_type);
1018
1019             gnu_expr
1020               = gnat_build_constructor
1021               (gnu_type,
1022                tree_cons
1023                (template_field,
1024                 build_template (TREE_TYPE (template_field),
1025                                 TREE_TYPE (TREE_CHAIN (template_field)),
1026                                 NULL_TREE),
1027                 NULL_TREE));
1028           }
1029
1030         /* Convert the expression to the type of the object except in the
1031            case where the object's type is unconstrained or the object's type
1032            is a padded record whose field is of self-referential size.  In
1033            the former case, converting will generate unnecessary evaluations
1034            of the CONSTRUCTOR to compute the size and in the latter case, we
1035            want to only copy the actual data.  */
1036         if (gnu_expr
1037             && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1038             && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1039             && !(TREE_CODE (gnu_type) == RECORD_TYPE
1040                  && TYPE_IS_PADDING_P (gnu_type)
1041                  && (CONTAINS_PLACEHOLDER_P
1042                      (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1043           gnu_expr = convert (gnu_type, gnu_expr);
1044
1045         /* If this is a pointer and it does not have an initializing
1046            expression, initialize it to NULL, unless the object is
1047            imported.  */
1048         if (definition
1049             && (POINTER_TYPE_P (gnu_type) || TYPE_FAT_POINTER_P (gnu_type))
1050             && !Is_Imported (gnat_entity) && !gnu_expr)
1051           gnu_expr = integer_zero_node;
1052
1053         /* If we are defining the object and it has an Address clause, we must
1054            either get the address expression from the saved GCC tree for the
1055            object if it has a Freeze node, or elaborate the address expression
1056            here since the front-end has guaranteed that the elaboration has no
1057            effects in this case.  */
1058         if (definition && Present (Address_Clause (gnat_entity)))
1059           {
1060             tree gnu_address
1061               = present_gnu_tree (gnat_entity)
1062                 ? get_gnu_tree (gnat_entity)
1063                 : gnat_to_gnu (Expression (Address_Clause (gnat_entity)));
1064
1065             save_gnu_tree (gnat_entity, NULL_TREE, false);
1066
1067             /* Ignore the size.  It's either meaningless or was handled
1068                above.  */
1069             gnu_size = NULL_TREE;
1070             /* Convert the type of the object to a reference type that can
1071                alias everything as per 13.3(19).  */
1072             gnu_type
1073               = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1074             gnu_address = convert (gnu_type, gnu_address);
1075             used_by_ref = true;
1076             const_flag = !Is_Public (gnat_entity)
1077               || compile_time_known_address_p (Expression (Address_Clause
1078                                                            (gnat_entity)));
1079
1080             /* If this is a deferred constant, the initializer is attached to
1081                the full view.  */
1082             if (kind == E_Constant && Present (Full_View (gnat_entity)))
1083               gnu_expr
1084                 = gnat_to_gnu
1085                     (Expression (Declaration_Node (Full_View (gnat_entity))));
1086
1087             /* If we don't have an initializing expression for the underlying
1088                variable, the initializing expression for the pointer is the
1089                specified address.  Otherwise, we have to make a COMPOUND_EXPR
1090                to assign both the address and the initial value.  */
1091             if (!gnu_expr)
1092               gnu_expr = gnu_address;
1093             else
1094               gnu_expr
1095                 = build2 (COMPOUND_EXPR, gnu_type,
1096                           build_binary_op
1097                           (MODIFY_EXPR, NULL_TREE,
1098                            build_unary_op (INDIRECT_REF, NULL_TREE,
1099                                            gnu_address),
1100                            gnu_expr),
1101                           gnu_address);
1102           }
1103
1104         /* If it has an address clause and we are not defining it, mark it
1105            as an indirect object.  Likewise for Stdcall objects that are
1106            imported.  */
1107         if ((!definition && Present (Address_Clause (gnat_entity)))
1108             || (Is_Imported (gnat_entity)
1109                 && Has_Stdcall_Convention (gnat_entity)))
1110           {
1111             /* Convert the type of the object to a reference type that can
1112                alias everything as per 13.3(19).  */
1113             gnu_type
1114               = build_reference_type_for_mode (gnu_type, ptr_mode, true);
1115             gnu_size = NULL_TREE;
1116
1117             /* No point in taking the address of an initializing expression
1118                that isn't going to be used.  */
1119             gnu_expr = NULL_TREE;
1120
1121             /* If it has an address clause whose value is known at compile
1122                time, make the object a CONST_DECL.  This will avoid a
1123                useless dereference.  */
1124             if (Present (Address_Clause (gnat_entity)))
1125               {
1126                 Node_Id gnat_address
1127                   = Expression (Address_Clause (gnat_entity));
1128
1129                 if (compile_time_known_address_p (gnat_address))
1130                   {
1131                     gnu_expr = gnat_to_gnu (gnat_address);
1132                     const_flag = true;
1133                   }
1134               }
1135
1136             used_by_ref = true;
1137           }
1138
1139         /* If we are at top level and this object is of variable size,
1140            make the actual type a hidden pointer to the real type and
1141            make the initializer be a memory allocation and initialization.
1142            Likewise for objects we aren't defining (presumed to be
1143            external references from other packages), but there we do
1144            not set up an initialization.
1145
1146            If the object's size overflows, make an allocator too, so that
1147            Storage_Error gets raised.  Note that we will never free
1148            such memory, so we presume it never will get allocated.  */
1149
1150         if (!allocatable_size_p (TYPE_SIZE_UNIT (gnu_type),
1151                                  global_bindings_p () || !definition
1152                                  || static_p)
1153             || (gnu_size
1154                 && ! allocatable_size_p (gnu_size,
1155                                          global_bindings_p () || !definition
1156                                          || static_p)))
1157           {
1158             gnu_type = build_reference_type (gnu_type);
1159             gnu_size = NULL_TREE;
1160             used_by_ref = true;
1161             const_flag = true;
1162
1163             /* In case this was a aliased object whose nominal subtype is
1164                unconstrained, the pointer above will be a thin pointer and
1165                build_allocator will automatically make the template.
1166
1167                If we have a template initializer only (that we made above),
1168                pretend there is none and rely on what build_allocator creates
1169                again anyway.  Otherwise (if we have a full initializer), get
1170                the data part and feed that to build_allocator.
1171
1172                If we are elaborating a mutable object, tell build_allocator to
1173                ignore a possibly simpler size from the initializer, if any, as
1174                we must allocate the maximum possible size in this case.  */
1175
1176             if (definition)
1177               {
1178                 tree gnu_alloc_type = TREE_TYPE (gnu_type);
1179
1180                 if (TREE_CODE (gnu_alloc_type) == RECORD_TYPE
1181                     && TYPE_CONTAINS_TEMPLATE_P (gnu_alloc_type))
1182                   {
1183                     gnu_alloc_type
1184                       = TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_alloc_type)));
1185
1186                     if (TREE_CODE (gnu_expr) == CONSTRUCTOR
1187                         && 1 == VEC_length (constructor_elt,
1188                                             CONSTRUCTOR_ELTS (gnu_expr)))
1189                       gnu_expr = 0;
1190                     else
1191                       gnu_expr
1192                         = build_component_ref
1193                             (gnu_expr, NULL_TREE,
1194                              TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (gnu_expr))),
1195                              false);
1196                   }
1197
1198                 if (TREE_CODE (TYPE_SIZE_UNIT (gnu_alloc_type)) == INTEGER_CST
1199                     && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_alloc_type))
1200                     && !Is_Imported (gnat_entity))
1201                   post_error ("?Storage_Error will be raised at run-time!",
1202                               gnat_entity);
1203
1204                 gnu_expr = build_allocator (gnu_alloc_type, gnu_expr, gnu_type,
1205                                             0, 0, gnat_entity, mutable_p);
1206               }
1207             else
1208               {
1209                 gnu_expr = NULL_TREE;
1210                 const_flag = false;
1211               }
1212           }
1213
1214         /* If this object would go into the stack and has an alignment larger
1215            than the largest stack alignment the back-end can honor, resort to
1216            a variable of "aligning type".  */
1217         if (!global_bindings_p () && !static_p && definition
1218             && !imported_p && TYPE_ALIGN (gnu_type) > BIGGEST_ALIGNMENT)
1219           {
1220             /* Create the new variable.  No need for extra room before the
1221                aligned field as this is in automatic storage.  */
1222             tree gnu_new_type
1223               = make_aligning_type (gnu_type, TYPE_ALIGN (gnu_type),
1224                                     TYPE_SIZE_UNIT (gnu_type),
1225                                     BIGGEST_ALIGNMENT, 0);
1226             tree gnu_new_var
1227               = create_var_decl (create_concat_name (gnat_entity, "ALIGN"),
1228                                  NULL_TREE, gnu_new_type, NULL_TREE, false,
1229                                  false, false, false, NULL, gnat_entity);
1230
1231             /* Initialize the aligned field if we have an initializer.  */
1232             if (gnu_expr)
1233               add_stmt_with_node
1234                 (build_binary_op (MODIFY_EXPR, NULL_TREE,
1235                                   build_component_ref
1236                                   (gnu_new_var, NULL_TREE,
1237                                    TYPE_FIELDS (gnu_new_type), false),
1238                                   gnu_expr),
1239                  gnat_entity);
1240
1241             /* And setup this entity as a reference to the aligned field.  */
1242             gnu_type = build_reference_type (gnu_type);
1243             gnu_expr
1244               = build_unary_op
1245                 (ADDR_EXPR, gnu_type,
1246                  build_component_ref (gnu_new_var, NULL_TREE,
1247                                       TYPE_FIELDS (gnu_new_type), false));
1248
1249             gnu_size = NULL_TREE;
1250             used_by_ref = true;
1251             const_flag = true;
1252           }
1253
1254         if (const_flag)
1255           gnu_type = build_qualified_type (gnu_type, (TYPE_QUALS (gnu_type)
1256                                                       | TYPE_QUAL_CONST));
1257
1258         /* Convert the expression to the type of the object except in the
1259            case where the object's type is unconstrained or the object's type
1260            is a padded record whose field is of self-referential size.  In
1261            the former case, converting will generate unnecessary evaluations
1262            of the CONSTRUCTOR to compute the size and in the latter case, we
1263            want to only copy the actual data.  */
1264         if (gnu_expr
1265             && TREE_CODE (gnu_type) != UNCONSTRAINED_ARRAY_TYPE
1266             && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
1267             && !(TREE_CODE (gnu_type) == RECORD_TYPE
1268                  && TYPE_IS_PADDING_P (gnu_type)
1269                  && (CONTAINS_PLACEHOLDER_P
1270                      (TYPE_SIZE (TREE_TYPE (TYPE_FIELDS (gnu_type)))))))
1271           gnu_expr = convert (gnu_type, gnu_expr);
1272
1273         /* If this name is external or there was a name specified, use it,
1274            unless this is a VMS exception object since this would conflict
1275            with the symbol we need to export in addition.  Don't use the
1276            Interface_Name if there is an address clause (see CD30005).  */
1277         if (!Is_VMS_Exception (gnat_entity)
1278             && ((Present (Interface_Name (gnat_entity))
1279                  && No (Address_Clause (gnat_entity)))
1280                 || (Is_Public (gnat_entity)
1281                     && (!Is_Imported (gnat_entity)
1282                         || Is_Exported (gnat_entity)))))
1283           gnu_ext_name = create_concat_name (gnat_entity, 0);
1284
1285         /* If this is constant initialized to a static constant and the
1286            object has an aggregate type, force it to be statically
1287            allocated.  This will avoid an initialization copy.  */
1288         if (!static_p && const_flag
1289             && gnu_expr && TREE_CONSTANT (gnu_expr)
1290             && AGGREGATE_TYPE_P (gnu_type)
1291             && host_integerp (TYPE_SIZE_UNIT (gnu_type), 1)
1292             && !(TREE_CODE (gnu_type) == RECORD_TYPE
1293                  && TYPE_IS_PADDING_P (gnu_type)
1294                  && !host_integerp (TYPE_SIZE_UNIT
1295                                     (TREE_TYPE (TYPE_FIELDS (gnu_type))), 1)))
1296           static_p = true;
1297
1298         gnu_decl = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1299                                     gnu_expr, const_flag,
1300                                     Is_Public (gnat_entity),
1301                                     imported_p || !definition,
1302                                     static_p, attr_list, gnat_entity);
1303         DECL_BY_REF_P (gnu_decl) = used_by_ref;
1304         DECL_POINTS_TO_READONLY_P (gnu_decl) = used_by_ref && inner_const_flag;
1305         if (TREE_CODE (gnu_decl) == VAR_DECL && renamed_obj)
1306           {
1307             SET_DECL_RENAMED_OBJECT (gnu_decl, renamed_obj);
1308             if (global_bindings_p ())
1309               {
1310                 DECL_RENAMING_GLOBAL_P (gnu_decl) = 1;
1311                 record_global_renaming_pointer (gnu_decl);
1312               }
1313           }
1314
1315         if (definition && DECL_SIZE_UNIT (gnu_decl)
1316             && get_block_jmpbuf_decl ()
1317             && (TREE_CODE (DECL_SIZE_UNIT (gnu_decl)) != INTEGER_CST
1318                 || (flag_stack_check == GENERIC_STACK_CHECK
1319                     && compare_tree_int (DECL_SIZE_UNIT (gnu_decl),
1320                                          STACK_CHECK_MAX_VAR_SIZE) > 0)))
1321           add_stmt_with_node (build_call_1_expr
1322                               (update_setjmp_buf_decl,
1323                                build_unary_op (ADDR_EXPR, NULL_TREE,
1324                                                get_block_jmpbuf_decl ())),
1325                               gnat_entity);
1326
1327         /* If we are defining an Out parameter and we're not optimizing,
1328            create a fake PARM_DECL for debugging purposes and make it
1329            point to the VAR_DECL.  Suppress debug info for the latter
1330            but make sure it will still live on the stack so it can be
1331            accessed from within the debugger through the PARM_DECL.  */
1332         if (kind == E_Out_Parameter && definition && !optimize)
1333           {
1334             tree param = create_param_decl (gnu_entity_id, gnu_type, false);
1335             gnat_pushdecl (param, gnat_entity);
1336             SET_DECL_VALUE_EXPR (param, gnu_decl);
1337             DECL_HAS_VALUE_EXPR_P (param) = 1;
1338             if (debug_info_p)
1339               debug_info_p = false;
1340             else
1341               DECL_IGNORED_P (param) = 1;
1342             TREE_ADDRESSABLE (gnu_decl) = 1;
1343           }
1344
1345         /* If this is a public constant or we're not optimizing and we're not
1346            making a VAR_DECL for it, make one just for export or debugger use.
1347            Likewise if the address is taken or if either the object or type is
1348            aliased.  Make an external declaration for a reference, unless this
1349            is a Standard entity since there no real symbol at the object level
1350            for these.  */
1351         if (TREE_CODE (gnu_decl) == CONST_DECL
1352             && (definition || Sloc (gnat_entity) > Standard_Location)
1353             && ((Is_Public (gnat_entity)
1354                  && !Present (Address_Clause (gnat_entity)))
1355                 || !optimize
1356                 || Address_Taken (gnat_entity)
1357                 || Is_Aliased (gnat_entity)
1358                 || Is_Aliased (Etype (gnat_entity))))
1359           {
1360             tree gnu_corr_var
1361               = create_true_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
1362                                       gnu_expr, true, Is_Public (gnat_entity),
1363                                       !definition, static_p, NULL,
1364                                       gnat_entity);
1365
1366             SET_DECL_CONST_CORRESPONDING_VAR (gnu_decl, gnu_corr_var);
1367
1368             /* As debugging information will be generated for the variable,
1369                do not generate information for the constant.  */
1370             DECL_IGNORED_P (gnu_decl) = 1;
1371           }
1372
1373         /* If this is declared in a block that contains a block with an
1374            exception handler, we must force this variable in memory to
1375            suppress an invalid optimization.  */
1376         if (Has_Nested_Block_With_Handler (Scope (gnat_entity))
1377             && Exception_Mechanism != Back_End_Exceptions)
1378           TREE_ADDRESSABLE (gnu_decl) = 1;
1379
1380         gnu_type = TREE_TYPE (gnu_decl);
1381
1382         /* Back-annotate Alignment and Esize of the object if not already
1383            known, except for when the object is actually a pointer to the
1384            real object, since alignment and size of a pointer don't have
1385            anything to do with those of the designated object.  Note that
1386            we pick the values of the type, not those of the object, to
1387            shield ourselves from low-level platform-dependent adjustments
1388            like alignment promotion.  This is both consistent with all the
1389            treatment above, where alignment and size are set on the type of
1390            the object and not on the object directly, and makes it possible
1391            to support confirming representation clauses in all cases.  */
1392
1393         if (!used_by_ref && Unknown_Alignment (gnat_entity))
1394           Set_Alignment (gnat_entity,
1395                          UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
1396
1397         if (!used_by_ref && Unknown_Esize (gnat_entity))
1398           {
1399             if (TREE_CODE (gnu_type) == RECORD_TYPE
1400                 && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
1401               gnu_object_size
1402                 = TYPE_SIZE (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type))));
1403
1404             Set_Esize (gnat_entity, annotate_value (gnu_object_size));
1405           }
1406       }
1407       break;
1408
1409     case E_Void:
1410       /* Return a TYPE_DECL for "void" that we previously made.  */
1411       gnu_decl = void_type_decl_node;
1412       break;
1413
1414     case E_Enumeration_Type:
1415       /* A special case, for the types Character and Wide_Character in
1416          Standard, we do not list all the literals.  So if the literals
1417          are not specified, make this an unsigned type.  */
1418       if (No (First_Literal (gnat_entity)))
1419         {
1420           gnu_type = make_unsigned_type (esize);
1421           TYPE_NAME (gnu_type) = gnu_entity_id;
1422
1423           /* Set TYPE_STRING_FLAG for Ada Character and Wide_Character types.
1424              This is needed by the DWARF-2 back-end to distinguish between
1425              unsigned integer types and character types.  */
1426           TYPE_STRING_FLAG (gnu_type) = 1;
1427           break;
1428         }
1429
1430       /* Normal case of non-character type, or non-Standard character type */
1431       {
1432         /* Here we have a list of enumeral constants in First_Literal.
1433            We make a CONST_DECL for each and build into GNU_LITERAL_LIST
1434            the list to be places into TYPE_FIELDS.  Each node in the list
1435            is a TREE_LIST node whose TREE_VALUE is the literal name
1436            and whose TREE_PURPOSE is the value of the literal.
1437
1438            Esize contains the number of bits needed to represent the enumeral
1439            type, Type_Low_Bound also points to the first literal and
1440            Type_High_Bound points to the last literal.  */
1441
1442         Entity_Id gnat_literal;
1443         tree gnu_literal_list = NULL_TREE;
1444
1445         if (Is_Unsigned_Type (gnat_entity))
1446           gnu_type = make_unsigned_type (esize);
1447         else
1448           gnu_type = make_signed_type (esize);
1449
1450         TREE_SET_CODE (gnu_type, ENUMERAL_TYPE);
1451
1452         for (gnat_literal = First_Literal (gnat_entity);
1453              Present (gnat_literal);
1454              gnat_literal = Next_Literal (gnat_literal))
1455           {
1456             tree gnu_value = UI_To_gnu (Enumeration_Rep (gnat_literal),
1457                                         gnu_type);
1458             tree gnu_literal
1459               = create_var_decl (get_entity_name (gnat_literal), NULL_TREE,
1460                                  gnu_type, gnu_value, true, false, false,
1461                                  false, NULL, gnat_literal);
1462
1463             save_gnu_tree (gnat_literal, gnu_literal, false);
1464             gnu_literal_list = tree_cons (DECL_NAME (gnu_literal),
1465                                           gnu_value, gnu_literal_list);
1466           }
1467
1468         TYPE_VALUES (gnu_type) = nreverse (gnu_literal_list);
1469
1470         /* Note that the bounds are updated at the end of this function
1471            because to avoid an infinite recursion when we get the bounds of
1472            this type, since those bounds are objects of this type.    */
1473       }
1474       break;
1475
1476     case E_Signed_Integer_Type:
1477     case E_Ordinary_Fixed_Point_Type:
1478     case E_Decimal_Fixed_Point_Type:
1479       /* For integer types, just make a signed type the appropriate number
1480          of bits.  */
1481       gnu_type = make_signed_type (esize);
1482       break;
1483
1484     case E_Modular_Integer_Type:
1485       /* For modular types, make the unsigned type of the proper number of
1486          bits and then set up the modulus, if required.  */
1487       {
1488         enum machine_mode mode;
1489         tree gnu_modulus;
1490         tree gnu_high = 0;
1491
1492         if (Is_Packed_Array_Type (gnat_entity))
1493           esize = UI_To_Int (RM_Size (gnat_entity));
1494
1495         /* Find the smallest mode at least ESIZE bits wide and make a class
1496            using that mode.  */
1497
1498         for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1499              GET_MODE_BITSIZE (mode) < esize;
1500              mode = GET_MODE_WIDER_MODE (mode))
1501           ;
1502
1503         gnu_type = make_unsigned_type (GET_MODE_BITSIZE (mode));
1504         TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
1505           = (Is_Packed_Array_Type (gnat_entity)
1506              && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1507
1508         /* Get the modulus in this type.  If it overflows, assume it is because
1509            it is equal to 2**Esize.  Note that there is no overflow checking
1510            done on unsigned type, so we detect the overflow by looking for
1511            a modulus of zero, which is otherwise invalid.  */
1512         gnu_modulus = UI_To_gnu (Modulus (gnat_entity), gnu_type);
1513
1514         if (!integer_zerop (gnu_modulus))
1515           {
1516             TYPE_MODULAR_P (gnu_type) = 1;
1517             SET_TYPE_MODULUS (gnu_type, gnu_modulus);
1518             gnu_high = fold_build2 (MINUS_EXPR, gnu_type, gnu_modulus,
1519                                     convert (gnu_type, integer_one_node));
1520           }
1521
1522         /* If we have to set TYPE_PRECISION different from its natural value,
1523            make a subtype to do do.  Likewise if there is a modulus and
1524            it is not one greater than TYPE_MAX_VALUE.  */
1525         if (TYPE_PRECISION (gnu_type) != esize
1526             || (TYPE_MODULAR_P (gnu_type)
1527                 && !tree_int_cst_equal (TYPE_MAX_VALUE (gnu_type), gnu_high)))
1528           {
1529             tree gnu_subtype = make_node (INTEGER_TYPE);
1530
1531             TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "UMT");
1532             TREE_TYPE (gnu_subtype) = gnu_type;
1533             TYPE_MIN_VALUE (gnu_subtype) = TYPE_MIN_VALUE (gnu_type);
1534             TYPE_MAX_VALUE (gnu_subtype)
1535               = TYPE_MODULAR_P (gnu_type)
1536                 ? gnu_high : TYPE_MAX_VALUE (gnu_type);
1537             TYPE_PRECISION (gnu_subtype) = esize;
1538             TYPE_UNSIGNED (gnu_subtype) = 1;
1539             TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
1540             TYPE_PACKED_ARRAY_TYPE_P (gnu_subtype)
1541               = (Is_Packed_Array_Type (gnat_entity)
1542                  && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
1543             layout_type (gnu_subtype);
1544
1545             gnu_type = gnu_subtype;
1546           }
1547       }
1548       break;
1549
1550     case E_Signed_Integer_Subtype:
1551     case E_Enumeration_Subtype:
1552     case E_Modular_Integer_Subtype:
1553     case E_Ordinary_Fixed_Point_Subtype:
1554     case E_Decimal_Fixed_Point_Subtype:
1555
1556       /* For integral subtypes, we make a new INTEGER_TYPE.  Note
1557          that we do not want to call build_range_type since we would
1558          like each subtype node to be distinct.  This will be important
1559          when memory aliasing is implemented.
1560
1561          The TREE_TYPE field of the INTEGER_TYPE we make points to the
1562          parent type; this fact is used by the arithmetic conversion
1563          functions.
1564
1565          We elaborate the Ancestor_Subtype if it is not in the current
1566          unit and one of our bounds is non-static.  We do this to ensure
1567          consistent naming in the case where several subtypes share the same
1568          bounds by always elaborating the first such subtype first, thus
1569          using its name.  */
1570
1571       if (!definition
1572           && Present (Ancestor_Subtype (gnat_entity))
1573           && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1574           && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1575               || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1576         gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1577                             gnu_expr, 0);
1578
1579       gnu_type = make_node (INTEGER_TYPE);
1580       TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1581
1582       /* Set the precision to the Esize except for bit-packed arrays and
1583          subtypes of Standard.Boolean.  */
1584       if (Is_Packed_Array_Type (gnat_entity)
1585           && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1586         {
1587           esize = UI_To_Int (RM_Size (gnat_entity));
1588           TYPE_PACKED_ARRAY_TYPE_P (gnu_type) = 1;
1589         }
1590       else if (TREE_CODE (TREE_TYPE (gnu_type)) == BOOLEAN_TYPE)
1591         esize = 1;
1592
1593       TYPE_PRECISION (gnu_type) = esize;
1594
1595       TYPE_MIN_VALUE (gnu_type)
1596         = convert (TREE_TYPE (gnu_type),
1597                    elaborate_expression (Type_Low_Bound (gnat_entity),
1598                                          gnat_entity,
1599                                          get_identifier ("L"), definition, 1,
1600                                          Needs_Debug_Info (gnat_entity)));
1601
1602       TYPE_MAX_VALUE (gnu_type)
1603         = convert (TREE_TYPE (gnu_type),
1604                    elaborate_expression (Type_High_Bound (gnat_entity),
1605                                          gnat_entity,
1606                                          get_identifier ("U"), definition, 1,
1607                                          Needs_Debug_Info (gnat_entity)));
1608
1609       /* One of the above calls might have caused us to be elaborated,
1610          so don't blow up if so.  */
1611       if (present_gnu_tree (gnat_entity))
1612         {
1613           maybe_present = true;
1614           break;
1615         }
1616
1617       TYPE_BIASED_REPRESENTATION_P (gnu_type)
1618         = Has_Biased_Representation (gnat_entity);
1619
1620      /* This should be an unsigned type if the lower bound is constant
1621          and non-negative or if the base type is unsigned; a signed type
1622          otherwise.    */
1623       TYPE_UNSIGNED (gnu_type)
1624         = (TYPE_UNSIGNED (TREE_TYPE (gnu_type))
1625            || (TREE_CODE (TYPE_MIN_VALUE (gnu_type)) == INTEGER_CST
1626                && TREE_INT_CST_HIGH (TYPE_MIN_VALUE (gnu_type)) >= 0)
1627            || TYPE_BIASED_REPRESENTATION_P (gnu_type)
1628            || Is_Unsigned_Type (gnat_entity));
1629
1630       layout_type (gnu_type);
1631
1632       /* Inherit our alias set from what we're a subtype of.  Subtypes
1633          are not different types and a pointer can designate any instance
1634          within a subtype hierarchy.  */
1635       copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1636
1637       /* If the type we are dealing with is to represent a packed array,
1638          we need to have the bits left justified on big-endian targets
1639          and right justified on little-endian targets.  We also need to
1640          ensure that when the value is read (e.g. for comparison of two
1641          such values), we only get the good bits, since the unused bits
1642          are uninitialized.  Both goals are accomplished by wrapping the
1643          modular value in an enclosing struct.  */
1644       if (Is_Packed_Array_Type (gnat_entity)
1645           && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
1646         {
1647           tree gnu_field_type = gnu_type;
1648           tree gnu_field;
1649
1650           TYPE_RM_SIZE_NUM (gnu_field_type)
1651             = UI_To_gnu (RM_Size (gnat_entity), bitsizetype);
1652           gnu_type = make_node (RECORD_TYPE);
1653           TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "JM");
1654
1655           /* Propagate the alignment of the modular type to the record.
1656              This means that bitpacked arrays have "ceil" alignment for
1657              their size, which may seem counter-intuitive but makes it
1658              possible to easily overlay them on modular types.  */
1659           TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_field_type);
1660           TYPE_PACKED (gnu_type) = 1;
1661
1662           /* Create a stripped-down declaration of the original type, mainly
1663              for debugging.  */
1664           create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1665                             NULL, true, debug_info_p, gnat_entity);
1666
1667           /* Don't notify the field as "addressable", since we won't be taking
1668              it's address and it would prevent create_field_decl from making a
1669              bitfield.  */
1670           gnu_field = create_field_decl (get_identifier ("OBJECT"),
1671                                          gnu_field_type, gnu_type, 1, 0, 0, 0);
1672
1673           finish_record_type (gnu_type, gnu_field, 0, false);
1674           TYPE_JUSTIFIED_MODULAR_P (gnu_type) = 1;
1675           SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1676
1677           copy_alias_set (gnu_type, gnu_field_type);
1678         }
1679
1680       /* If the type we are dealing with has got a smaller alignment than the
1681          natural one, we need to wrap it up in a record type and under-align
1682          the latter.  We reuse the padding machinery for this purpose.  */
1683       else if (Known_Alignment (gnat_entity)
1684                && UI_Is_In_Int_Range (Alignment (gnat_entity))
1685                && (align = UI_To_Int (Alignment (gnat_entity)) * BITS_PER_UNIT)
1686                && align < TYPE_ALIGN (gnu_type))
1687         {
1688           tree gnu_field_type = gnu_type;
1689           tree gnu_field;
1690
1691           gnu_type = make_node (RECORD_TYPE);
1692           TYPE_NAME (gnu_type) = create_concat_name (gnat_entity, "PAD");
1693
1694           TYPE_ALIGN (gnu_type) = align;
1695           TYPE_PACKED (gnu_type) = 1;
1696
1697           /* Create a stripped-down declaration of the original type, mainly
1698              for debugging.  */
1699           create_type_decl (get_entity_name (gnat_entity), gnu_field_type,
1700                             NULL, true, debug_info_p, gnat_entity);
1701
1702           /* Don't notify the field as "addressable", since we won't be taking
1703              it's address and it would prevent create_field_decl from making a
1704              bitfield.  */
1705           gnu_field = create_field_decl (get_identifier ("OBJECT"),
1706                                          gnu_field_type, gnu_type, 1, 0, 0, 0);
1707
1708           finish_record_type (gnu_type, gnu_field, 0, false);
1709           TYPE_IS_PADDING_P (gnu_type) = 1;
1710           SET_TYPE_ADA_SIZE (gnu_type, bitsize_int (esize));
1711
1712           copy_alias_set (gnu_type, gnu_field_type);
1713         }
1714
1715       /* Otherwise reset the alignment lest we computed it above.  */
1716       else
1717         align = 0;
1718
1719       break;
1720
1721     case E_Floating_Point_Type:
1722       /* If this is a VAX floating-point type, use an integer of the proper
1723          size.  All the operations will be handled with ASM statements.  */
1724       if (Vax_Float (gnat_entity))
1725         {
1726           gnu_type = make_signed_type (esize);
1727           TYPE_VAX_FLOATING_POINT_P (gnu_type) = 1;
1728           SET_TYPE_DIGITS_VALUE (gnu_type,
1729                                  UI_To_gnu (Digits_Value (gnat_entity),
1730                                             sizetype));
1731           break;
1732         }
1733
1734       /* The type of the Low and High bounds can be our type if this is
1735          a type from Standard, so set them at the end of the function.  */
1736       gnu_type = make_node (REAL_TYPE);
1737       TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1738       layout_type (gnu_type);
1739       break;
1740
1741     case E_Floating_Point_Subtype:
1742       if (Vax_Float (gnat_entity))
1743         {
1744           gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
1745           break;
1746         }
1747
1748       {
1749         if (!definition
1750             && Present (Ancestor_Subtype (gnat_entity))
1751             && !In_Extended_Main_Code_Unit (Ancestor_Subtype (gnat_entity))
1752             && (!Compile_Time_Known_Value (Type_Low_Bound (gnat_entity))
1753                 || !Compile_Time_Known_Value (Type_High_Bound (gnat_entity))))
1754           gnat_to_gnu_entity (Ancestor_Subtype (gnat_entity),
1755                               gnu_expr, 0);
1756
1757         gnu_type = make_node (REAL_TYPE);
1758         TREE_TYPE (gnu_type) = get_unpadded_type (Etype (gnat_entity));
1759         TYPE_PRECISION (gnu_type) = fp_size_to_prec (esize);
1760
1761         TYPE_MIN_VALUE (gnu_type)
1762           = convert (TREE_TYPE (gnu_type),
1763                      elaborate_expression (Type_Low_Bound (gnat_entity),
1764                                            gnat_entity, get_identifier ("L"),
1765                                            definition, 1,
1766                                            Needs_Debug_Info (gnat_entity)));
1767
1768         TYPE_MAX_VALUE (gnu_type)
1769           = convert (TREE_TYPE (gnu_type),
1770                      elaborate_expression (Type_High_Bound (gnat_entity),
1771                                            gnat_entity, get_identifier ("U"),
1772                                            definition, 1,
1773                                            Needs_Debug_Info (gnat_entity)));
1774
1775         /* One of the above calls might have caused us to be elaborated,
1776            so don't blow up if so.  */
1777         if (present_gnu_tree (gnat_entity))
1778           {
1779             maybe_present = true;
1780             break;
1781           }
1782
1783         layout_type (gnu_type);
1784
1785         /* Inherit our alias set from what we're a subtype of, as for
1786            integer subtypes.  */
1787         copy_alias_set (gnu_type, TREE_TYPE (gnu_type));
1788       }
1789     break;
1790
1791       /* Array and String Types and Subtypes
1792
1793          Unconstrained array types are represented by E_Array_Type and
1794          constrained array types are represented by E_Array_Subtype.  There
1795          are no actual objects of an unconstrained array type; all we have
1796          are pointers to that type.
1797
1798          The following fields are defined on array types and subtypes:
1799
1800                 Component_Type     Component type of the array.
1801                 Number_Dimensions  Number of dimensions (an int).
1802                 First_Index        Type of first index.  */
1803
1804     case E_String_Type:
1805     case E_Array_Type:
1806       {
1807         tree gnu_template_fields = NULL_TREE;
1808         tree gnu_template_type = make_node (RECORD_TYPE);
1809         tree gnu_ptr_template = build_pointer_type (gnu_template_type);
1810         tree gnu_fat_type = make_node (RECORD_TYPE);
1811         int ndim = Number_Dimensions (gnat_entity);
1812         int firstdim
1813           = (Convention (gnat_entity) == Convention_Fortran) ? ndim - 1 : 0;
1814         int nextdim
1815           = (Convention (gnat_entity) == Convention_Fortran) ? - 1 : 1;
1816         int index;
1817         tree *gnu_index_types = (tree *) alloca (ndim * sizeof (tree *));
1818         tree *gnu_temp_fields = (tree *) alloca (ndim * sizeof (tree *));
1819         tree gnu_comp_size = 0;
1820         tree gnu_max_size = size_one_node;
1821         tree gnu_max_size_unit;
1822         Entity_Id gnat_ind_subtype;
1823         Entity_Id gnat_ind_base_subtype;
1824         tree gnu_template_reference;
1825         tree tem;
1826
1827         TYPE_NAME (gnu_template_type)
1828           = create_concat_name (gnat_entity, "XUB");
1829
1830         /* Make a node for the array.  If we are not defining the array
1831            suppress expanding incomplete types.  */
1832         gnu_type = make_node (UNCONSTRAINED_ARRAY_TYPE);
1833
1834         if (!definition)
1835           defer_incomplete_level++, this_deferred = true;
1836
1837         /* Build the fat pointer type.  Use a "void *" object instead of
1838            a pointer to the array type since we don't have the array type
1839            yet (it will reference the fat pointer via the bounds).  */
1840         tem = chainon (chainon (NULL_TREE,
1841                                 create_field_decl (get_identifier ("P_ARRAY"),
1842                                                    ptr_void_type_node,
1843                                                    gnu_fat_type, 0, 0, 0, 0)),
1844                        create_field_decl (get_identifier ("P_BOUNDS"),
1845                                           gnu_ptr_template,
1846                                           gnu_fat_type, 0, 0, 0, 0));
1847
1848         /* Make sure we can put this into a register.  */
1849         TYPE_ALIGN (gnu_fat_type) = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
1850
1851         /* Do not finalize this record type since the types of its fields
1852            are still incomplete at this point.  */
1853         finish_record_type (gnu_fat_type, tem, 0, true);
1854         TYPE_IS_FAT_POINTER_P (gnu_fat_type) = 1;
1855
1856         /* Build a reference to the template from a PLACEHOLDER_EXPR that
1857            is the fat pointer.  This will be used to access the individual
1858            fields once we build them.  */
1859         tem = build3 (COMPONENT_REF, gnu_ptr_template,
1860                       build0 (PLACEHOLDER_EXPR, gnu_fat_type),
1861                       TREE_CHAIN (TYPE_FIELDS (gnu_fat_type)), NULL_TREE);
1862         gnu_template_reference
1863           = build_unary_op (INDIRECT_REF, gnu_template_type, tem);
1864         TREE_READONLY (gnu_template_reference) = 1;
1865
1866         /* Now create the GCC type for each index and add the fields for
1867            that index to the template.  */
1868         for (index = firstdim, gnat_ind_subtype = First_Index (gnat_entity),
1869              gnat_ind_base_subtype
1870                = First_Index (Implementation_Base_Type (gnat_entity));
1871              index < ndim && index >= 0;
1872              index += nextdim,
1873              gnat_ind_subtype = Next_Index (gnat_ind_subtype),
1874              gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
1875           {
1876             char field_name[10];
1877             tree gnu_ind_subtype
1878               = get_unpadded_type (Base_Type (Etype (gnat_ind_subtype)));
1879             tree gnu_base_subtype
1880               = get_unpadded_type (Etype (gnat_ind_base_subtype));
1881             tree gnu_base_min
1882               = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
1883             tree gnu_base_max
1884               = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
1885             tree gnu_min_field, gnu_max_field, gnu_min, gnu_max;
1886
1887             /* Make the FIELD_DECLs for the minimum and maximum of this
1888                type and then make extractions of that field from the
1889                template.  */
1890             sprintf (field_name, "LB%d", index);
1891             gnu_min_field = create_field_decl (get_identifier (field_name),
1892                                                gnu_ind_subtype,
1893                                                gnu_template_type, 0, 0, 0, 0);
1894             field_name[0] = 'U';
1895             gnu_max_field = create_field_decl (get_identifier (field_name),
1896                                                gnu_ind_subtype,
1897                                                gnu_template_type, 0, 0, 0, 0);
1898
1899             Sloc_to_locus (Sloc (gnat_entity),
1900                            &DECL_SOURCE_LOCATION (gnu_min_field));
1901             Sloc_to_locus (Sloc (gnat_entity),
1902                            &DECL_SOURCE_LOCATION (gnu_max_field));
1903             gnu_temp_fields[index] = chainon (gnu_min_field, gnu_max_field);
1904
1905             /* We can't use build_component_ref here since the template
1906                type isn't complete yet.  */
1907             gnu_min = build3 (COMPONENT_REF, gnu_ind_subtype,
1908                               gnu_template_reference, gnu_min_field,
1909                               NULL_TREE);
1910             gnu_max = build3 (COMPONENT_REF, gnu_ind_subtype,
1911                               gnu_template_reference, gnu_max_field,
1912                               NULL_TREE);
1913             TREE_READONLY (gnu_min) = TREE_READONLY (gnu_max) = 1;
1914
1915             /* Make a range type with the new ranges, but using
1916                the Ada subtype.  Then we convert to sizetype.  */
1917             gnu_index_types[index]
1918               = create_index_type (convert (sizetype, gnu_min),
1919                                    convert (sizetype, gnu_max),
1920                                    build_range_type (gnu_ind_subtype,
1921                                                      gnu_min, gnu_max),
1922                                    gnat_entity);
1923             /* Update the maximum size of the array, in elements.  */
1924             gnu_max_size
1925               = size_binop (MULT_EXPR, gnu_max_size,
1926                             size_binop (PLUS_EXPR, size_one_node,
1927                                         size_binop (MINUS_EXPR, gnu_base_max,
1928                                                     gnu_base_min)));
1929
1930             TYPE_NAME (gnu_index_types[index])
1931               = create_concat_name (gnat_entity, field_name);
1932           }
1933
1934         for (index = 0; index < ndim; index++)
1935           gnu_template_fields
1936             = chainon (gnu_template_fields, gnu_temp_fields[index]);
1937
1938         /* Install all the fields into the template.  */
1939         finish_record_type (gnu_template_type, gnu_template_fields, 0, false);
1940         TYPE_READONLY (gnu_template_type) = 1;
1941
1942         /* Now make the array of arrays and update the pointer to the array
1943            in the fat pointer.  Note that it is the first field.  */
1944         tem = gnat_to_gnu_type (Component_Type (gnat_entity));
1945
1946         /* Try to get a smaller form of the component if needed.  */
1947         if ((Is_Packed (gnat_entity)
1948              || Has_Component_Size_Clause (gnat_entity))
1949             && !Is_Bit_Packed_Array (gnat_entity)
1950             && !Has_Aliased_Components (gnat_entity)
1951             && !Strict_Alignment (Component_Type (gnat_entity))
1952             && TREE_CODE (tem) == RECORD_TYPE
1953             && host_integerp (TYPE_SIZE (tem), 1))
1954           tem = make_packable_type (tem, false);
1955
1956         if (Has_Atomic_Components (gnat_entity))
1957           check_ok_for_atomic (tem, gnat_entity, true);
1958
1959         /* Get and validate any specified Component_Size, but if Packed,
1960            ignore it since the front end will have taken care of it.  */
1961         gnu_comp_size
1962           = validate_size (Component_Size (gnat_entity), tem,
1963                            gnat_entity,
1964                            (Is_Bit_Packed_Array (gnat_entity)
1965                             ? TYPE_DECL : VAR_DECL),
1966                            true, Has_Component_Size_Clause (gnat_entity));
1967
1968         /* If the component type is a RECORD_TYPE that has a self-referential
1969            size, use the maximum size.  */
1970         if (!gnu_comp_size && TREE_CODE (tem) == RECORD_TYPE
1971             && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (tem)))
1972           gnu_comp_size = max_size (TYPE_SIZE (tem), true);
1973
1974         if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
1975           {
1976             tree orig_tem;
1977             tem = make_type_from_size (tem, gnu_comp_size, false);
1978             orig_tem = tem;
1979             tem = maybe_pad_type (tem, gnu_comp_size, 0, gnat_entity,
1980                                   "C_PAD", false, definition, true);
1981             /* If a padding record was made, declare it now since it will
1982                never be declared otherwise.  This is necessary to ensure
1983                that its subtrees are properly marked.  */
1984             if (tem != orig_tem)
1985               create_type_decl (TYPE_NAME (tem), tem, NULL, true,
1986                                 debug_info_p, gnat_entity);
1987           }
1988
1989         if (Has_Volatile_Components (gnat_entity))
1990           tem = build_qualified_type (tem,
1991                                       TYPE_QUALS (tem) | TYPE_QUAL_VOLATILE);
1992
1993         /* If Component_Size is not already specified, annotate it with the
1994            size of the component.  */
1995         if (Unknown_Component_Size (gnat_entity))
1996           Set_Component_Size (gnat_entity, annotate_value (TYPE_SIZE (tem)));
1997
1998         gnu_max_size_unit = size_binop (MAX_EXPR, size_zero_node,
1999                                         size_binop (MULT_EXPR, gnu_max_size,
2000                                                     TYPE_SIZE_UNIT (tem)));
2001         gnu_max_size = size_binop (MAX_EXPR, bitsize_zero_node,
2002                                    size_binop (MULT_EXPR,
2003                                                convert (bitsizetype,
2004                                                         gnu_max_size),
2005                                                TYPE_SIZE (tem)));
2006
2007         for (index = ndim - 1; index >= 0; index--)
2008           {
2009             tem = build_array_type (tem, gnu_index_types[index]);
2010             TYPE_MULTI_ARRAY_P (tem) = (index > 0);
2011             if (array_type_has_nonaliased_component (gnat_entity, tem))
2012               TYPE_NONALIASED_COMPONENT (tem) = 1;
2013           }
2014
2015         /* If an alignment is specified, use it if valid.  But ignore it for
2016            types that represent the unpacked base type for packed arrays.  If
2017            the alignment was requested with an explicit user alignment clause,
2018            state so.  */
2019         if (No (Packed_Array_Type (gnat_entity))
2020             && Known_Alignment (gnat_entity))
2021           {
2022             gcc_assert (Present (Alignment (gnat_entity)));
2023             TYPE_ALIGN (tem)
2024               = validate_alignment (Alignment (gnat_entity), gnat_entity,
2025                                     TYPE_ALIGN (tem));
2026             if (Present (Alignment_Clause (gnat_entity)))
2027               TYPE_USER_ALIGN (tem) = 1;
2028           }
2029
2030         TYPE_CONVENTION_FORTRAN_P (tem)
2031           = (Convention (gnat_entity) == Convention_Fortran);
2032         TREE_TYPE (TYPE_FIELDS (gnu_fat_type)) = build_pointer_type (tem);
2033
2034         /* The result type is an UNCONSTRAINED_ARRAY_TYPE that indicates the
2035            corresponding fat pointer.  */
2036         TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type)
2037           = TYPE_REFERENCE_TO (gnu_type) = gnu_fat_type;
2038         SET_TYPE_MODE (gnu_type, BLKmode);
2039         TYPE_ALIGN (gnu_type) = TYPE_ALIGN (tem);
2040         SET_TYPE_UNCONSTRAINED_ARRAY (gnu_fat_type, gnu_type);
2041
2042         /* If the maximum size doesn't overflow, use it.  */
2043         if (TREE_CODE (gnu_max_size) == INTEGER_CST
2044             && !TREE_OVERFLOW (gnu_max_size))
2045           TYPE_SIZE (tem)
2046             = size_binop (MIN_EXPR, gnu_max_size, TYPE_SIZE (tem));
2047         if (TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2048             && !TREE_OVERFLOW (gnu_max_size_unit))
2049           TYPE_SIZE_UNIT (tem)
2050             = size_binop (MIN_EXPR, gnu_max_size_unit,
2051                           TYPE_SIZE_UNIT (tem));
2052
2053         create_type_decl (create_concat_name (gnat_entity, "XUA"),
2054                           tem, NULL, !Comes_From_Source (gnat_entity),
2055                           debug_info_p, gnat_entity);
2056
2057         /* Give the fat pointer type a name.  */
2058         create_type_decl (create_concat_name (gnat_entity, "XUP"),
2059                           gnu_fat_type, NULL, !Comes_From_Source (gnat_entity),
2060                           debug_info_p, gnat_entity);
2061
2062        /* Create the type to be used as what a thin pointer designates: an
2063           record type for the object and its template with the field offsets
2064           shifted to have the template at a negative offset.  */
2065         tem = build_unc_object_type (gnu_template_type, tem,
2066                                      create_concat_name (gnat_entity, "XUT"));
2067         shift_unc_components_for_thin_pointers (tem);
2068
2069         SET_TYPE_UNCONSTRAINED_ARRAY (tem, gnu_type);
2070         TYPE_OBJECT_RECORD_TYPE (gnu_type) = tem;
2071
2072         /* Give the thin pointer type a name.  */
2073         create_type_decl (create_concat_name (gnat_entity, "XUX"),
2074                           build_pointer_type (tem), NULL,
2075                           !Comes_From_Source (gnat_entity), debug_info_p,
2076                           gnat_entity);
2077       }
2078       break;
2079
2080     case E_String_Subtype:
2081     case E_Array_Subtype:
2082
2083       /* This is the actual data type for array variables.  Multidimensional
2084          arrays are implemented in the gnu tree as arrays of arrays.  Note
2085          that for the moment arrays which have sparse enumeration subtypes as
2086          index components create sparse arrays, which is obviously space
2087          inefficient but so much easier to code for now.
2088
2089          Also note that the subtype never refers to the unconstrained
2090          array type, which is somewhat at variance with Ada semantics.
2091
2092          First check to see if this is simply a renaming of the array
2093          type.  If so, the result is the array type.  */
2094
2095       gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
2096       if (!Is_Constrained (gnat_entity))
2097         break;
2098       else
2099         {
2100           int index;
2101           int array_dim = Number_Dimensions (gnat_entity);
2102           int first_dim
2103             = ((Convention (gnat_entity) == Convention_Fortran)
2104                ? array_dim - 1 : 0);
2105           int next_dim
2106             = (Convention (gnat_entity) == Convention_Fortran) ? -1 : 1;
2107           Entity_Id gnat_ind_subtype;
2108           Entity_Id gnat_ind_base_subtype;
2109           tree gnu_base_type = gnu_type;
2110           tree *gnu_index_type = (tree *) alloca (array_dim * sizeof (tree *));
2111           tree gnu_comp_size = NULL_TREE;
2112           tree gnu_max_size = size_one_node;
2113           tree gnu_max_size_unit;
2114           bool need_index_type_struct = false;
2115           bool max_overflow = false;
2116
2117           /* First create the gnu types for each index.  Create types for
2118              debugging information to point to the index types if the
2119              are not integer types, have variable bounds, or are
2120              wider than sizetype.  */
2121
2122           for (index = first_dim, gnat_ind_subtype = First_Index (gnat_entity),
2123                gnat_ind_base_subtype
2124                  = First_Index (Implementation_Base_Type (gnat_entity));
2125                index < array_dim && index >= 0;
2126                index += next_dim,
2127                gnat_ind_subtype = Next_Index (gnat_ind_subtype),
2128                gnat_ind_base_subtype = Next_Index (gnat_ind_base_subtype))
2129             {
2130               tree gnu_index_subtype
2131                 = get_unpadded_type (Etype (gnat_ind_subtype));
2132               tree gnu_min
2133                 = convert (sizetype, TYPE_MIN_VALUE (gnu_index_subtype));
2134               tree gnu_max
2135                 = convert (sizetype, TYPE_MAX_VALUE (gnu_index_subtype));
2136               tree gnu_base_subtype
2137                 = get_unpadded_type (Etype (gnat_ind_base_subtype));
2138               tree gnu_base_min
2139                 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_subtype));
2140               tree gnu_base_max
2141                 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_subtype));
2142               tree gnu_base_type = get_base_type (gnu_base_subtype);
2143               tree gnu_base_base_min
2144                 = convert (sizetype, TYPE_MIN_VALUE (gnu_base_type));
2145               tree gnu_base_base_max
2146                 = convert (sizetype, TYPE_MAX_VALUE (gnu_base_type));
2147               tree gnu_high;
2148               tree gnu_this_max;
2149
2150               /* If the minimum and maximum values both overflow in
2151                  SIZETYPE, but the difference in the original type
2152                  does not overflow in SIZETYPE, ignore the overflow
2153                  indications.  */
2154               if ((TYPE_PRECISION (gnu_index_subtype)
2155                    > TYPE_PRECISION (sizetype)
2156                    || TYPE_UNSIGNED (gnu_index_subtype)
2157                       != TYPE_UNSIGNED (sizetype))
2158                   && TREE_CODE (gnu_min) == INTEGER_CST
2159                   && TREE_CODE (gnu_max) == INTEGER_CST
2160                   && TREE_OVERFLOW (gnu_min) && TREE_OVERFLOW (gnu_max)
2161                   && (!TREE_OVERFLOW
2162                       (fold_build2 (MINUS_EXPR, gnu_index_subtype,
2163                                     TYPE_MAX_VALUE (gnu_index_subtype),
2164                                     TYPE_MIN_VALUE (gnu_index_subtype)))))
2165                 {
2166                   TREE_OVERFLOW (gnu_min) = 0;
2167                   TREE_OVERFLOW (gnu_max) = 0;
2168                 }
2169
2170               /* Similarly, if the range is null, use bounds of 1..0 for
2171                  the sizetype bounds.  */
2172               else if ((TYPE_PRECISION (gnu_index_subtype)
2173                         > TYPE_PRECISION (sizetype)
2174                        || TYPE_UNSIGNED (gnu_index_subtype)
2175                           != TYPE_UNSIGNED (sizetype))
2176                        && TREE_CODE (gnu_min) == INTEGER_CST
2177                        && TREE_CODE (gnu_max) == INTEGER_CST
2178                        && (TREE_OVERFLOW (gnu_min) || TREE_OVERFLOW (gnu_max))
2179                        && tree_int_cst_lt (TYPE_MAX_VALUE (gnu_index_subtype),
2180                                            TYPE_MIN_VALUE (gnu_index_subtype)))
2181                 gnu_min = size_one_node, gnu_max = size_zero_node;
2182
2183               /* Now compute the size of this bound.  We need to provide
2184                  GCC with an upper bound to use but have to deal with the
2185                  "superflat" case.  There are three ways to do this.  If we
2186                  can prove that the array can never be superflat, we can
2187                  just use the high bound of the index subtype.  If we can
2188                  prove that the low bound minus one can't overflow, we
2189                  can do this as MAX (hb, lb - 1).  Otherwise, we have to use
2190                  the expression hb >= lb ? hb : lb - 1.  */
2191               gnu_high = size_binop (MINUS_EXPR, gnu_min, size_one_node);
2192
2193               /* See if the base array type is already flat.  If it is, we
2194                  are probably compiling an ACVC test, but it will cause the
2195                  code below to malfunction if we don't handle it specially.  */
2196               if (TREE_CODE (gnu_base_min) == INTEGER_CST
2197                   && TREE_CODE (gnu_base_max) == INTEGER_CST
2198                   && !TREE_OVERFLOW (gnu_base_min)
2199                   && !TREE_OVERFLOW (gnu_base_max)
2200                   && tree_int_cst_lt (gnu_base_max, gnu_base_min))
2201                 gnu_high = size_zero_node, gnu_min = size_one_node;
2202
2203               /* If gnu_high is now an integer which overflowed, the array
2204                  cannot be superflat.  */
2205               else if (TREE_CODE (gnu_high) == INTEGER_CST
2206                        && TREE_OVERFLOW (gnu_high))
2207                 gnu_high = gnu_max;
2208               else if (TYPE_UNSIGNED (gnu_base_subtype)
2209                        || TREE_CODE (gnu_high) == INTEGER_CST)
2210                 gnu_high = size_binop (MAX_EXPR, gnu_max, gnu_high);
2211               else
2212                 gnu_high
2213                   = build_cond_expr
2214                     (sizetype, build_binary_op (GE_EXPR, integer_type_node,
2215                                                 gnu_max, gnu_min),
2216                      gnu_max, gnu_high);
2217
2218               gnu_index_type[index]
2219                 = create_index_type (gnu_min, gnu_high, gnu_index_subtype,
2220                                      gnat_entity);
2221
2222               /* Also compute the maximum size of the array.  Here we
2223                  see if any constraint on the index type of the base type
2224                  can be used in the case of self-referential bound on
2225                  the index type of the subtype.  We look for a non-"infinite"
2226                  and non-self-referential bound from any type involved and
2227                  handle each bound separately.  */
2228
2229               if ((TREE_CODE (gnu_min) == INTEGER_CST
2230                    && !TREE_OVERFLOW (gnu_min)
2231                    && !operand_equal_p (gnu_min, gnu_base_base_min, 0))
2232                   || !CONTAINS_PLACEHOLDER_P (gnu_min)
2233                   || !(TREE_CODE (gnu_base_min) == INTEGER_CST
2234                        && !TREE_OVERFLOW (gnu_base_min)))
2235                 gnu_base_min = gnu_min;
2236
2237               if ((TREE_CODE (gnu_max) == INTEGER_CST
2238                    && !TREE_OVERFLOW (gnu_max)
2239                    && !operand_equal_p (gnu_max, gnu_base_base_max, 0))
2240                   || !CONTAINS_PLACEHOLDER_P (gnu_max)
2241                   || !(TREE_CODE (gnu_base_max) == INTEGER_CST
2242                        && !TREE_OVERFLOW (gnu_base_max)))
2243                 gnu_base_max = gnu_max;
2244
2245               if ((TREE_CODE (gnu_base_min) == INTEGER_CST
2246                    && TREE_OVERFLOW (gnu_base_min))
2247                   || operand_equal_p (gnu_base_min, gnu_base_base_min, 0)
2248                   || (TREE_CODE (gnu_base_max) == INTEGER_CST
2249                       && TREE_OVERFLOW (gnu_base_max))
2250                   || operand_equal_p (gnu_base_max, gnu_base_base_max, 0))
2251                 max_overflow = true;
2252
2253               gnu_base_min = size_binop (MAX_EXPR, gnu_base_min, gnu_min);
2254               gnu_base_max = size_binop (MIN_EXPR, gnu_base_max, gnu_max);
2255
2256               gnu_this_max
2257                 = size_binop (MAX_EXPR,
2258                               size_binop (PLUS_EXPR, size_one_node,
2259                                           size_binop (MINUS_EXPR, gnu_base_max,
2260                                                       gnu_base_min)),
2261                               size_zero_node);
2262
2263               if (TREE_CODE (gnu_this_max) == INTEGER_CST
2264                   && TREE_OVERFLOW (gnu_this_max))
2265                 max_overflow = true;
2266
2267               gnu_max_size
2268                 = size_binop (MULT_EXPR, gnu_max_size, gnu_this_max);
2269
2270               if (!integer_onep (TYPE_MIN_VALUE (gnu_index_subtype))
2271                   || (TREE_CODE (TYPE_MAX_VALUE (gnu_index_subtype))
2272                       != INTEGER_CST)
2273                   || TREE_CODE (gnu_index_subtype) != INTEGER_TYPE
2274                   || (TREE_TYPE (gnu_index_subtype)
2275                       && (TREE_CODE (TREE_TYPE (gnu_index_subtype))
2276                           != INTEGER_TYPE))
2277                   || TYPE_BIASED_REPRESENTATION_P (gnu_index_subtype)
2278                   || (TYPE_PRECISION (gnu_index_subtype)
2279                       > TYPE_PRECISION (sizetype)))
2280                 need_index_type_struct = true;
2281             }
2282
2283           /* Then flatten: create the array of arrays.  For an array type
2284              used to implement a packed array, get the component type from
2285              the original array type since the representation clauses that
2286              can affect it are on the latter.  */
2287           if (Is_Packed_Array_Type (gnat_entity)
2288               && !Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)))
2289             {
2290               gnu_type = gnat_to_gnu_type (Original_Array_Type (gnat_entity));
2291               for (index = array_dim - 1; index >= 0; index--)
2292                 gnu_type = TREE_TYPE (gnu_type);
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           else
2303             {
2304               gnu_type = gnat_to_gnu_type (Component_Type (gnat_entity));
2305
2306               /* One of the above calls might have caused us to be elaborated,
2307                  so don't blow up if so.  */
2308               if (present_gnu_tree (gnat_entity))
2309                 {
2310                   maybe_present = true;
2311                   break;
2312                 }
2313
2314               /* Try to get a smaller form of the component if needed.  */
2315               if ((Is_Packed (gnat_entity)
2316                    || Has_Component_Size_Clause (gnat_entity))
2317                   && !Is_Bit_Packed_Array (gnat_entity)
2318                   && !Has_Aliased_Components (gnat_entity)
2319                   && !Strict_Alignment (Component_Type (gnat_entity))
2320                   && TREE_CODE (gnu_type) == RECORD_TYPE
2321                   && host_integerp (TYPE_SIZE (gnu_type), 1))
2322                 gnu_type = make_packable_type (gnu_type, false);
2323
2324               /* Get and validate any specified Component_Size, but if Packed,
2325                  ignore it since the front end will have taken care of it.  */
2326               gnu_comp_size
2327                 = validate_size (Component_Size (gnat_entity), gnu_type,
2328                                  gnat_entity,
2329                                  (Is_Bit_Packed_Array (gnat_entity)
2330                                   ? TYPE_DECL : VAR_DECL), true,
2331                                  Has_Component_Size_Clause (gnat_entity));
2332
2333               /* If the component type is a RECORD_TYPE that has a
2334                  self-referential size, use the maximum size.  */
2335               if (!gnu_comp_size
2336                   && TREE_CODE (gnu_type) == RECORD_TYPE
2337                   && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
2338                 gnu_comp_size = max_size (TYPE_SIZE (gnu_type), true);
2339
2340               if (gnu_comp_size && !Is_Bit_Packed_Array (gnat_entity))
2341                 {
2342                   tree orig_gnu_type;
2343                   gnu_type
2344                     = make_type_from_size (gnu_type, gnu_comp_size, false);
2345                   orig_gnu_type = gnu_type;
2346                   gnu_type = maybe_pad_type (gnu_type, gnu_comp_size, 0,
2347                                              gnat_entity, "C_PAD", false,
2348                                              definition, true);
2349                   /* If a padding record was made, declare it now since it
2350                      will never be declared otherwise.  This is necessary
2351                      to ensure that its subtrees are properly marked.  */
2352                   if (gnu_type != orig_gnu_type)
2353                     create_type_decl (TYPE_NAME (gnu_type), gnu_type, NULL,
2354                                       true, debug_info_p, gnat_entity);
2355                 }
2356
2357               if (Has_Volatile_Components (Base_Type (gnat_entity)))
2358                 gnu_type = build_qualified_type (gnu_type,
2359                                                  (TYPE_QUALS (gnu_type)
2360                                                   | TYPE_QUAL_VOLATILE));
2361             }
2362
2363           gnu_max_size_unit = size_binop (MULT_EXPR, gnu_max_size,
2364                                           TYPE_SIZE_UNIT (gnu_type));
2365           gnu_max_size = size_binop (MULT_EXPR,
2366                                      convert (bitsizetype, gnu_max_size),
2367                                      TYPE_SIZE (gnu_type));
2368
2369           for (index = array_dim - 1; index >= 0; index --)
2370             {
2371               gnu_type = build_array_type (gnu_type, gnu_index_type[index]);
2372               TYPE_MULTI_ARRAY_P (gnu_type) = (index > 0);
2373               if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2374                 TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2375             }
2376
2377           /* If we are at file level and this is a multi-dimensional array, we
2378              need to make a variable corresponding to the stride of the
2379              inner dimensions.   */
2380           if (global_bindings_p () && array_dim > 1)
2381             {
2382               tree gnu_str_name = get_identifier ("ST");
2383               tree gnu_arr_type;
2384
2385               for (gnu_arr_type = TREE_TYPE (gnu_type);
2386                    TREE_CODE (gnu_arr_type) == ARRAY_TYPE;
2387                    gnu_arr_type = TREE_TYPE (gnu_arr_type),
2388                    gnu_str_name = concat_id_with_name (gnu_str_name, "ST"))
2389                 {
2390                   tree eltype = TREE_TYPE (gnu_arr_type);
2391
2392                   TYPE_SIZE (gnu_arr_type)
2393                     = elaborate_expression_1 (gnat_entity, gnat_entity,
2394                                               TYPE_SIZE (gnu_arr_type),
2395                                               gnu_str_name, definition, 0);
2396
2397                   /* ??? For now, store the size as a multiple of the
2398                      alignment of the element type in bytes so that we
2399                      can see the alignment from the tree.  */
2400                   TYPE_SIZE_UNIT (gnu_arr_type)
2401                     = build_binary_op
2402                       (MULT_EXPR, sizetype,
2403                        elaborate_expression_1
2404                        (gnat_entity, gnat_entity,
2405                         build_binary_op (EXACT_DIV_EXPR, sizetype,
2406                                          TYPE_SIZE_UNIT (gnu_arr_type),
2407                                          size_int (TYPE_ALIGN (eltype)
2408                                                    / BITS_PER_UNIT)),
2409                         concat_id_with_name (gnu_str_name, "A_U"),
2410                         definition, 0),
2411                        size_int (TYPE_ALIGN (eltype) / BITS_PER_UNIT));
2412
2413                   /* ??? create_type_decl is not invoked on the inner types so
2414                      the MULT_EXPR node built above will never be marked.  */
2415                   mark_visited (&TYPE_SIZE_UNIT (gnu_arr_type));
2416                 }
2417             }
2418
2419           /* If we need to write out a record type giving the names of
2420              the bounds, do it now.  */
2421           if (need_index_type_struct && debug_info_p)
2422             {
2423               tree gnu_bound_rec_type = make_node (RECORD_TYPE);
2424               tree gnu_field_list = NULL_TREE;
2425               tree gnu_field;
2426
2427               TYPE_NAME (gnu_bound_rec_type)
2428                 = create_concat_name (gnat_entity, "XA");
2429
2430               for (index = array_dim - 1; index >= 0; index--)
2431                 {
2432                   tree gnu_type_name
2433                     = TYPE_NAME (TYPE_INDEX_TYPE (gnu_index_type[index]));
2434
2435                   if (TREE_CODE (gnu_type_name) == TYPE_DECL)
2436                     gnu_type_name = DECL_NAME (gnu_type_name);
2437
2438                   gnu_field = create_field_decl (gnu_type_name,
2439                                                  integer_type_node,
2440                                                  gnu_bound_rec_type,
2441                                                  0, NULL_TREE, NULL_TREE, 0);
2442                   TREE_CHAIN (gnu_field) = gnu_field_list;
2443                   gnu_field_list = gnu_field;
2444                 }
2445
2446               finish_record_type (gnu_bound_rec_type, gnu_field_list,
2447                                   0, false);
2448
2449               TYPE_STUB_DECL (gnu_type)
2450                 = build_decl (TYPE_DECL, NULL_TREE, gnu_type);
2451
2452               add_parallel_type
2453                 (TYPE_STUB_DECL (gnu_type), gnu_bound_rec_type);
2454             }
2455
2456           TYPE_CONVENTION_FORTRAN_P (gnu_type)
2457             = (Convention (gnat_entity) == Convention_Fortran);
2458           TYPE_PACKED_ARRAY_TYPE_P (gnu_type)
2459             = (Is_Packed_Array_Type (gnat_entity)
2460                && Is_Bit_Packed_Array (Original_Array_Type (gnat_entity)));
2461
2462           /* If our size depends on a placeholder and the maximum size doesn't
2463              overflow, use it.  */
2464           if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type))
2465               && !(TREE_CODE (gnu_max_size) == INTEGER_CST
2466                    && TREE_OVERFLOW (gnu_max_size))
2467               && !(TREE_CODE (gnu_max_size_unit) == INTEGER_CST
2468                    && TREE_OVERFLOW (gnu_max_size_unit))
2469               && !max_overflow)
2470             {
2471               TYPE_SIZE (gnu_type) = size_binop (MIN_EXPR, gnu_max_size,
2472                                                  TYPE_SIZE (gnu_type));
2473               TYPE_SIZE_UNIT (gnu_type)
2474                 = size_binop (MIN_EXPR, gnu_max_size_unit,
2475                               TYPE_SIZE_UNIT (gnu_type));
2476             }
2477
2478           /* Set our alias set to that of our base type.  This gives all
2479              array subtypes the same alias set.  */
2480           copy_alias_set (gnu_type, gnu_base_type);
2481         }
2482
2483       /* If this is a packed type, make this type the same as the packed
2484          array type, but do some adjusting in the type first.   */
2485
2486       if (Present (Packed_Array_Type (gnat_entity)))
2487         {
2488           Entity_Id gnat_index;
2489           tree gnu_inner_type;
2490
2491           /* First finish the type we had been making so that we output
2492              debugging information for it  */
2493           gnu_type
2494             = build_qualified_type (gnu_type,
2495                                     (TYPE_QUALS (gnu_type)
2496                                      | (TYPE_QUAL_VOLATILE
2497                                         * Treat_As_Volatile (gnat_entity))));
2498           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
2499                                        !Comes_From_Source (gnat_entity),
2500                                        debug_info_p, gnat_entity);
2501           if (!Comes_From_Source (gnat_entity))
2502             DECL_ARTIFICIAL (gnu_decl) = 1;
2503
2504           /* Save it as our equivalent in case the call below elaborates
2505              this type again.  */
2506           save_gnu_tree (gnat_entity, gnu_decl, false);
2507
2508           gnu_decl = gnat_to_gnu_entity (Packed_Array_Type (gnat_entity),
2509                                          NULL_TREE, 0);
2510           this_made_decl = true;
2511           gnu_type = TREE_TYPE (gnu_decl);
2512           save_gnu_tree (gnat_entity, NULL_TREE, false);
2513
2514           gnu_inner_type = gnu_type;
2515           while (TREE_CODE (gnu_inner_type) == RECORD_TYPE
2516                  && (TYPE_JUSTIFIED_MODULAR_P (gnu_inner_type)
2517                      || TYPE_IS_PADDING_P (gnu_inner_type)))
2518             gnu_inner_type = TREE_TYPE (TYPE_FIELDS (gnu_inner_type));
2519
2520           /* We need to point the type we just made to our index type so
2521              the actual bounds can be put into a template.  */
2522
2523           if ((TREE_CODE (gnu_inner_type) == ARRAY_TYPE
2524                && !TYPE_ACTUAL_BOUNDS (gnu_inner_type))
2525               || (TREE_CODE (gnu_inner_type) == INTEGER_TYPE
2526                   && !TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type)))
2527             {
2528               if (TREE_CODE (gnu_inner_type) == INTEGER_TYPE)
2529                 {
2530                   /* The TYPE_ACTUAL_BOUNDS field is also used for the modulus.
2531                      If it is, we need to make another type.  */
2532                   if (TYPE_MODULAR_P (gnu_inner_type))
2533                     {
2534                       tree gnu_subtype;
2535
2536                       gnu_subtype = make_node (INTEGER_TYPE);
2537
2538                       TREE_TYPE (gnu_subtype) = gnu_inner_type;
2539                       TYPE_MIN_VALUE (gnu_subtype)
2540                         = TYPE_MIN_VALUE (gnu_inner_type);
2541                       TYPE_MAX_VALUE (gnu_subtype)
2542                         = TYPE_MAX_VALUE (gnu_inner_type);
2543                       TYPE_PRECISION (gnu_subtype)
2544                         = TYPE_PRECISION (gnu_inner_type);
2545                       TYPE_UNSIGNED (gnu_subtype)
2546                         = TYPE_UNSIGNED (gnu_inner_type);
2547                       TYPE_EXTRA_SUBTYPE_P (gnu_subtype) = 1;
2548                       layout_type (gnu_subtype);
2549
2550                       gnu_inner_type = gnu_subtype;
2551                     }
2552
2553                   TYPE_HAS_ACTUAL_BOUNDS_P (gnu_inner_type) = 1;
2554                 }
2555
2556               SET_TYPE_ACTUAL_BOUNDS (gnu_inner_type, NULL_TREE);
2557
2558               for (gnat_index = First_Index (gnat_entity);
2559                    Present (gnat_index); gnat_index = Next_Index (gnat_index))
2560                 SET_TYPE_ACTUAL_BOUNDS
2561                   (gnu_inner_type,
2562                    tree_cons (NULL_TREE,
2563                               get_unpadded_type (Etype (gnat_index)),
2564                               TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2565
2566               if (Convention (gnat_entity) != Convention_Fortran)
2567                 SET_TYPE_ACTUAL_BOUNDS
2568                   (gnu_inner_type,
2569                    nreverse (TYPE_ACTUAL_BOUNDS (gnu_inner_type)));
2570
2571               if (TREE_CODE (gnu_type) == RECORD_TYPE
2572                   && TYPE_JUSTIFIED_MODULAR_P (gnu_type))
2573                 TREE_TYPE (TYPE_FIELDS (gnu_type)) = gnu_inner_type;
2574             }
2575         }
2576
2577       /* Abort if packed array with no packed array type field set.  */
2578       else
2579         gcc_assert (!Is_Packed (gnat_entity));
2580
2581       break;
2582
2583     case E_String_Literal_Subtype:
2584       /* Create the type for a string literal.  */
2585       {
2586         Entity_Id gnat_full_type
2587           = (IN (Ekind (Etype (gnat_entity)), Private_Kind)
2588              && Present (Full_View (Etype (gnat_entity)))
2589              ? Full_View (Etype (gnat_entity)) : Etype (gnat_entity));
2590         tree gnu_string_type = get_unpadded_type (gnat_full_type);
2591         tree gnu_string_array_type
2592           = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_string_type))));
2593         tree gnu_string_index_type
2594           = get_base_type (TREE_TYPE (TYPE_INDEX_TYPE
2595                                       (TYPE_DOMAIN (gnu_string_array_type))));
2596         tree gnu_lower_bound
2597           = convert (gnu_string_index_type,
2598                      gnat_to_gnu (String_Literal_Low_Bound (gnat_entity)));
2599         int length = UI_To_Int (String_Literal_Length (gnat_entity));
2600         tree gnu_length = ssize_int (length - 1);
2601         tree gnu_upper_bound
2602           = build_binary_op (PLUS_EXPR, gnu_string_index_type,
2603                              gnu_lower_bound,
2604                              convert (gnu_string_index_type, gnu_length));
2605         tree gnu_range_type
2606           = build_range_type (gnu_string_index_type,
2607                               gnu_lower_bound, gnu_upper_bound);
2608         tree gnu_index_type
2609           = create_index_type (convert (sizetype,
2610                                         TYPE_MIN_VALUE (gnu_range_type)),
2611                                convert (sizetype,
2612                                         TYPE_MAX_VALUE (gnu_range_type)),
2613                                gnu_range_type, gnat_entity);
2614
2615         gnu_type
2616           = build_array_type (gnat_to_gnu_type (Component_Type (gnat_entity)),
2617                               gnu_index_type);
2618         if (array_type_has_nonaliased_component (gnat_entity, gnu_type))
2619           TYPE_NONALIASED_COMPONENT (gnu_type) = 1;
2620         copy_alias_set (gnu_type, gnu_string_type);
2621       }
2622       break;
2623
2624     /* Record Types and Subtypes
2625
2626        The following fields are defined on record types:
2627
2628                 Has_Discriminants       True if the record has discriminants
2629                 First_Discriminant      Points to head of list of discriminants
2630                 First_Entity            Points to head of list of fields
2631                 Is_Tagged_Type          True if the record is tagged
2632
2633        Implementation of Ada records and discriminated records:
2634
2635        A record type definition is transformed into the equivalent of a C
2636        struct definition.  The fields that are the discriminants which are
2637        found in the Full_Type_Declaration node and the elements of the
2638        Component_List found in the Record_Type_Definition node.  The
2639        Component_List can be a recursive structure since each Variant of
2640        the Variant_Part of the Component_List has a Component_List.
2641
2642        Processing of a record type definition comprises starting the list of
2643        field declarations here from the discriminants and the calling the
2644        function components_to_record to add the rest of the fields from the
2645        component list and return the gnu type node.  The function
2646        components_to_record will call itself recursively as it traverses
2647        the tree.  */
2648
2649     case E_Record_Type:
2650       if (Has_Complex_Representation (gnat_entity))
2651         {
2652           gnu_type
2653             = build_complex_type
2654               (get_unpadded_type
2655                (Etype (Defining_Entity
2656                        (First (Component_Items
2657                                (Component_List
2658                                 (Type_Definition
2659                                  (Declaration_Node (gnat_entity)))))))));
2660
2661           break;
2662         }
2663
2664       {
2665         Node_Id full_definition = Declaration_Node (gnat_entity);
2666         Node_Id record_definition = Type_Definition (full_definition);
2667         Entity_Id gnat_field;
2668         tree gnu_field;
2669         tree gnu_field_list = NULL_TREE;
2670         tree gnu_get_parent;
2671         /* Set PACKED in keeping with gnat_to_gnu_field.  */
2672         int packed
2673           = Is_Packed (gnat_entity)
2674             ? 1
2675             : Component_Alignment (gnat_entity) == Calign_Storage_Unit
2676               ? -1
2677               : (Known_Alignment (gnat_entity)
2678                  || (Strict_Alignment (gnat_entity)
2679                      && Known_Static_Esize (gnat_entity)))
2680                 ? -2
2681                 : 0;
2682         bool has_rep = Has_Specified_Layout (gnat_entity);
2683         bool all_rep = has_rep;
2684         bool is_extension
2685           = (Is_Tagged_Type (gnat_entity)
2686              && Nkind (record_definition) == N_Derived_Type_Definition);
2687
2688         /* See if all fields have a rep clause.  Stop when we find one
2689            that doesn't.  */
2690         for (gnat_field = First_Entity (gnat_entity);
2691              Present (gnat_field) && all_rep;
2692              gnat_field = Next_Entity (gnat_field))
2693           if ((Ekind (gnat_field) == E_Component
2694                || Ekind (gnat_field) == E_Discriminant)
2695               && No (Component_Clause (gnat_field)))
2696             all_rep = false;
2697
2698         /* If this is a record extension, go a level further to find the
2699            record definition.  Also, verify we have a Parent_Subtype.  */
2700         if (is_extension)
2701           {
2702             if (!type_annotate_only
2703                 || Present (Record_Extension_Part (record_definition)))
2704               record_definition = Record_Extension_Part (record_definition);
2705
2706             gcc_assert (type_annotate_only
2707                         || Present (Parent_Subtype (gnat_entity)));
2708           }
2709
2710         /* Make a node for the record.  If we are not defining the record,
2711            suppress expanding incomplete types.  */
2712         gnu_type = make_node (tree_code_for_record_type (gnat_entity));
2713         TYPE_NAME (gnu_type) = gnu_entity_id;
2714         TYPE_PACKED (gnu_type) = (packed != 0) || has_rep;
2715
2716         if (!definition)
2717           defer_incomplete_level++, this_deferred = true;
2718
2719         /* If both a size and rep clause was specified, put the size in
2720            the record type now so that it can get the proper mode.  */
2721         if (has_rep && Known_Esize (gnat_entity))
2722           TYPE_SIZE (gnu_type) = UI_To_gnu (Esize (gnat_entity), sizetype);
2723
2724         /* Always set the alignment here so that it can be used to
2725            set the mode, if it is making the alignment stricter.  If
2726            it is invalid, it will be checked again below.  If this is to
2727            be Atomic, choose a default alignment of a word unless we know
2728            the size and it's smaller.  */
2729         if (Known_Alignment (gnat_entity))
2730           TYPE_ALIGN (gnu_type)
2731             = validate_alignment (Alignment (gnat_entity), gnat_entity, 0);
2732         else if (Is_Atomic (gnat_entity))
2733           TYPE_ALIGN (gnu_type)
2734             = esize >= BITS_PER_WORD ? BITS_PER_WORD : ceil_alignment (esize);
2735         /* If a type needs strict alignment, the minimum size will be the
2736            type size instead of the RM size (see validate_size).  Cap the
2737            alignment, lest it causes this type size to become too large.  */
2738         else if (Strict_Alignment (gnat_entity)
2739                  && Known_Static_Esize (gnat_entity))
2740           {
2741             unsigned int raw_size = UI_To_Int (Esize (gnat_entity));
2742             unsigned int raw_align = raw_size & -raw_size;
2743             if (raw_align < BIGGEST_ALIGNMENT)
2744               TYPE_ALIGN (gnu_type) = raw_align;
2745           }
2746         else
2747           TYPE_ALIGN (gnu_type) = 0;
2748
2749         /* If we have a Parent_Subtype, make a field for the parent.  If
2750            this record has rep clauses, force the position to zero.  */
2751         if (Present (Parent_Subtype (gnat_entity)))
2752           {
2753             Entity_Id gnat_parent = Parent_Subtype (gnat_entity);
2754             tree gnu_parent;
2755
2756             /* A major complexity here is that the parent subtype will
2757                reference our discriminants in its Discriminant_Constraint
2758                list.  But those must reference the parent component of this
2759                record which is of the parent subtype we have not built yet!
2760                To break the circle we first build a dummy COMPONENT_REF which
2761                represents the "get to the parent" operation and initialize
2762                each of those discriminants to a COMPONENT_REF of the above
2763                dummy parent referencing the corresponding discriminant of the
2764                base type of the parent subtype.  */
2765             gnu_get_parent = build3 (COMPONENT_REF, void_type_node,
2766                                      build0 (PLACEHOLDER_EXPR, gnu_type),
2767                                      build_decl (FIELD_DECL, NULL_TREE,
2768                                                  void_type_node),
2769                                      NULL_TREE);
2770
2771             if (Has_Discriminants (gnat_entity))
2772               for (gnat_field = First_Stored_Discriminant (gnat_entity);
2773                    Present (gnat_field);
2774                    gnat_field = Next_Stored_Discriminant (gnat_field))
2775                 if (Present (Corresponding_Discriminant (gnat_field)))
2776                   save_gnu_tree
2777                     (gnat_field,
2778                      build3 (COMPONENT_REF,
2779                              get_unpadded_type (Etype (gnat_field)),
2780                              gnu_get_parent,
2781                              gnat_to_gnu_field_decl (Corresponding_Discriminant
2782                                                      (gnat_field)),
2783                              NULL_TREE),
2784                      true);
2785
2786             /* Then we build the parent subtype.  */
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_Discriminants (gnat_entity))
2794               for (gnat_field = First_Stored_Discriminant (gnat_entity);
2795                    Present (gnat_field);
2796                    gnat_field = Next_Stored_Discriminant (gnat_field))
2797                 if (Present (Corresponding_Discriminant (gnat_field)))
2798                   {
2799                     Entity_Id field = Empty;
2800                     for (field = First_Stored_Discriminant (gnat_parent);
2801                          Present (field);
2802                          field = Next_Stored_Discriminant (field))
2803                       if (same_discriminant_p (gnat_field, field))
2804                         break;
2805                     gcc_assert (Present (field));
2806                     TREE_OPERAND (get_gnu_tree (gnat_field), 1)
2807                       = gnat_to_gnu_field_decl (field);
2808                   }
2809
2810             /* The "get to the parent" COMPONENT_REF must be given its
2811                proper type...  */
2812             TREE_TYPE (gnu_get_parent) = gnu_parent;
2813
2814             /* ...and reference the _parent field of this record.  */
2815             gnu_field_list
2816               = create_field_decl (get_identifier
2817                                    (Get_Name_String (Name_uParent)),
2818                                    gnu_parent, gnu_type, 0,
2819                                    has_rep ? TYPE_SIZE (gnu_parent) : 0,
2820                                    has_rep ? bitsize_zero_node : 0, 1);
2821             DECL_INTERNAL_P (gnu_field_list) = 1;
2822             TREE_OPERAND (gnu_get_parent, 1) = gnu_field_list;
2823           }
2824
2825         /* Make the fields for the discriminants and put them into the record
2826            unless it's an Unchecked_Union.  */
2827         if (Has_Discriminants (gnat_entity))
2828           for (gnat_field = First_Stored_Discriminant (gnat_entity);
2829                Present (gnat_field);
2830                gnat_field = Next_Stored_Discriminant (gnat_field))
2831             {
2832               /* If this is a record extension and this discriminant
2833                  is the renaming of another discriminant, we've already
2834                  handled the discriminant above.  */
2835               if (Present (Parent_Subtype (gnat_entity))
2836                   && Present (Corresponding_Discriminant (gnat_field)))
2837                 continue;
2838
2839               gnu_field
2840                 = gnat_to_gnu_field (gnat_field, gnu_type, packed, definition);
2841
2842               /* Make an expression using a PLACEHOLDER_EXPR from the
2843                  FIELD_DECL node just created and link that with the
2844                  corresponding GNAT defining identifier.  Then add to the
2845                  list of fields.  */
2846               save_gnu_tree (gnat_field,
2847                              build3 (COMPONENT_REF, TREE_TYPE (gnu_field),
2848                                      build0 (PLACEHOLDER_EXPR,
2849                                              DECL_CONTEXT (gnu_field)),
2850                                      gnu_field, NULL_TREE),
2851                              true);
2852
2853               if (!Is_Unchecked_Union (gnat_entity))
2854                 {
2855                   TREE_CHAIN (gnu_field) = gnu_field_list;
2856                   gnu_field_list = gnu_field;
2857                 }
2858             }
2859
2860         /* Put the discriminants into the record (backwards), so we can
2861            know the appropriate discriminant to use for the names of the
2862            variants.  */
2863         TYPE_FIELDS (gnu_type) = gnu_field_list;
2864
2865         /* Add the listed fields into the record and finish it up.  */
2866         components_to_record (gnu_type, Component_List (record_definition),
2867                               gnu_field_list, packed, definition, NULL,
2868                               false, all_rep, false,
2869                               Is_Unchecked_Union (gnat_entity));
2870
2871         /* We used to remove the associations of the discriminants and
2872            _Parent for validity checking, but we may need them if there's
2873            Freeze_Node for a subtype used in this record.  */
2874         TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2875         TYPE_BY_REFERENCE_P (gnu_type) = Is_By_Reference_Type (gnat_entity);
2876
2877         /* If it is a tagged record force the type to BLKmode to insure
2878            that these objects will always be placed in memory.  Do the
2879            same thing for limited record types.  */
2880         if (Is_Tagged_Type (gnat_entity) || Is_Limited_Record (gnat_entity))
2881           SET_TYPE_MODE (gnu_type, BLKmode);
2882
2883         /* If this is a derived type, we must make the alias set of this type
2884            the same as that of the type we are derived from.  We assume here
2885            that the other type is already frozen.  */
2886         if (Etype (gnat_entity) != gnat_entity
2887             && !(Is_Private_Type (Etype (gnat_entity))
2888                  && Full_View (Etype (gnat_entity)) == gnat_entity))
2889           copy_alias_set (gnu_type, gnat_to_gnu_type (Etype (gnat_entity)));
2890
2891         /* Fill in locations of fields.  */
2892         annotate_rep (gnat_entity, gnu_type);
2893
2894         /* If there are any entities in the chain corresponding to
2895            components that we did not elaborate, ensure we elaborate their
2896            types if they are Itypes.  */
2897         for (gnat_temp = First_Entity (gnat_entity);
2898              Present (gnat_temp); gnat_temp = Next_Entity (gnat_temp))
2899           if ((Ekind (gnat_temp) == E_Component
2900                || Ekind (gnat_temp) == E_Discriminant)
2901               && Is_Itype (Etype (gnat_temp))
2902               && !present_gnu_tree (gnat_temp))
2903             gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
2904       }
2905       break;
2906
2907     case E_Class_Wide_Subtype:
2908       /* If an equivalent type is present, that is what we should use.
2909          Otherwise, fall through to handle this like a record subtype
2910          since it may have constraints.  */
2911       if (gnat_equiv_type != gnat_entity)
2912         {
2913           gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
2914           maybe_present = true;
2915           break;
2916         }
2917
2918       /* ... fall through ... */
2919
2920     case E_Record_Subtype:
2921
2922       /* If Cloned_Subtype is Present it means this record subtype has
2923          identical layout to that type or subtype and we should use
2924          that GCC type for this one.  The front end guarantees that
2925          the component list is shared.  */
2926       if (Present (Cloned_Subtype (gnat_entity)))
2927         {
2928           gnu_decl = gnat_to_gnu_entity (Cloned_Subtype (gnat_entity),
2929                                          NULL_TREE, 0);
2930           maybe_present = true;
2931         }
2932
2933       /* Otherwise, first ensure the base type is elaborated.  Then, if we are
2934          changing the type, make a new type with each field having the
2935          type of the field in the new subtype but having the position
2936          computed by transforming every discriminant reference according
2937          to the constraints.  We don't see any difference between
2938          private and nonprivate type here since derivations from types should
2939          have been deferred until the completion of the private type.  */
2940       else
2941         {
2942           Entity_Id gnat_base_type = Implementation_Base_Type (gnat_entity);
2943           tree gnu_base_type;
2944           tree gnu_orig_type;
2945
2946           if (!definition)
2947             defer_incomplete_level++, this_deferred = true;
2948
2949           /* Get the base type initially for its alignment and sizes.  But
2950              if it is a padded type, we do all the other work with the
2951              unpadded type.  */
2952           gnu_base_type = gnat_to_gnu_type (gnat_base_type);
2953
2954           if (TREE_CODE (gnu_base_type) == RECORD_TYPE
2955               && TYPE_IS_PADDING_P (gnu_base_type))
2956             gnu_type = gnu_orig_type = TREE_TYPE (TYPE_FIELDS (gnu_base_type));
2957           else
2958             gnu_type = gnu_orig_type = gnu_base_type;
2959
2960           if (present_gnu_tree (gnat_entity))
2961             {
2962               maybe_present = true;
2963               break;
2964             }
2965
2966           /* When the type has discriminants, and these discriminants
2967              affect the shape of what it built, factor them in.
2968
2969              If we are making a subtype of an Unchecked_Union (must be an
2970              Itype), just return the type.
2971
2972              We can't just use Is_Constrained because private subtypes without
2973              discriminants of full types with discriminants with default
2974              expressions are Is_Constrained but aren't constrained!  */
2975
2976           if (IN (Ekind (gnat_base_type), Record_Kind)
2977               && !Is_For_Access_Subtype (gnat_entity)
2978               && !Is_Unchecked_Union (gnat_base_type)
2979               && Is_Constrained (gnat_entity)
2980               && Stored_Constraint (gnat_entity) != No_Elist
2981               && Present (Discriminant_Constraint (gnat_entity)))
2982             {
2983               Entity_Id gnat_field;
2984               tree gnu_field_list = 0;
2985               tree gnu_pos_list
2986                 = compute_field_positions (gnu_orig_type, NULL_TREE,
2987                                            size_zero_node, bitsize_zero_node,
2988                                            BIGGEST_ALIGNMENT);
2989               tree gnu_subst_list
2990                 = substitution_list (gnat_entity, gnat_base_type, NULL_TREE,
2991                                      definition);
2992               tree gnu_temp;
2993
2994               gnu_type = make_node (RECORD_TYPE);
2995               TYPE_NAME (gnu_type) = gnu_entity_id;
2996               TYPE_VOLATILE (gnu_type) = Treat_As_Volatile (gnat_entity);
2997
2998               /* Set the size, alignment and alias set of the new type to
2999                  match that of the old one, doing required substitutions.
3000                  We do it this early because we need the size of the new
3001                  type below to discard old fields if necessary.  */
3002               TYPE_SIZE (gnu_type) = TYPE_SIZE (gnu_base_type);
3003               TYPE_SIZE_UNIT (gnu_type) = TYPE_SIZE_UNIT (gnu_base_type);
3004               SET_TYPE_ADA_SIZE (gnu_type, TYPE_ADA_SIZE (gnu_base_type));
3005               TYPE_ALIGN (gnu_type) = TYPE_ALIGN (gnu_base_type);
3006               copy_alias_set (gnu_type, gnu_base_type);
3007
3008               if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
3009                 for (gnu_temp = gnu_subst_list;
3010                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3011                   TYPE_SIZE (gnu_type)
3012                     = substitute_in_expr (TYPE_SIZE (gnu_type),
3013                                           TREE_PURPOSE (gnu_temp),
3014                                           TREE_VALUE (gnu_temp));
3015
3016               if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (gnu_type)))
3017                 for (gnu_temp = gnu_subst_list;
3018                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3019                   TYPE_SIZE_UNIT (gnu_type)
3020                     = substitute_in_expr (TYPE_SIZE_UNIT (gnu_type),
3021                                           TREE_PURPOSE (gnu_temp),
3022                                           TREE_VALUE (gnu_temp));
3023
3024               if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (gnu_type)))
3025                 for (gnu_temp = gnu_subst_list;
3026                      gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3027                   SET_TYPE_ADA_SIZE
3028                     (gnu_type, substitute_in_expr (TYPE_ADA_SIZE (gnu_type),
3029                                                    TREE_PURPOSE (gnu_temp),
3030                                                    TREE_VALUE (gnu_temp)));
3031
3032               for (gnat_field = First_Entity (gnat_entity);
3033                    Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3034                 if ((Ekind (gnat_field) == E_Component
3035                      || Ekind (gnat_field) == E_Discriminant)
3036                     && (Underlying_Type (Scope (Original_Record_Component
3037                                                 (gnat_field)))
3038                         == gnat_base_type)
3039                     && (No (Corresponding_Discriminant (gnat_field))
3040                         || !Is_Tagged_Type (gnat_base_type)))
3041                   {
3042                     tree gnu_old_field
3043                       = gnat_to_gnu_field_decl (Original_Record_Component
3044                                                 (gnat_field));
3045                     tree gnu_offset
3046                       = TREE_VALUE (purpose_member (gnu_old_field,
3047                                                     gnu_pos_list));
3048                     tree gnu_pos = TREE_PURPOSE (gnu_offset);
3049                     tree gnu_bitpos = TREE_VALUE (TREE_VALUE (gnu_offset));
3050                     tree gnu_field_type
3051                       = gnat_to_gnu_type (Etype (gnat_field));
3052                     tree gnu_size = TYPE_SIZE (gnu_field_type);
3053                     tree gnu_new_pos = NULL_TREE;
3054                     unsigned int offset_align
3055                       = tree_low_cst (TREE_PURPOSE (TREE_VALUE (gnu_offset)),
3056                                       1);
3057                     tree gnu_field;
3058
3059                     /* If there was a component clause, the field types must be
3060                        the same for the type and subtype, so copy the data from
3061                        the old field to avoid recomputation here.  Also if the
3062                        field is justified modular and the optimization in
3063                        gnat_to_gnu_field was applied.  */
3064                     if (Present (Component_Clause
3065                                  (Original_Record_Component (gnat_field)))
3066                         || (TREE_CODE (gnu_field_type) == RECORD_TYPE
3067                             && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
3068                             && TREE_TYPE (TYPE_FIELDS (gnu_field_type))
3069                                == TREE_TYPE (gnu_old_field)))
3070                       {
3071                         gnu_size = DECL_SIZE (gnu_old_field);
3072                         gnu_field_type = TREE_TYPE (gnu_old_field);
3073                       }
3074
3075                     /* If the old field was packed and of constant size, we
3076                        have to get the old size here, as it might differ from
3077                        what the Etype conveys and the latter might overlap
3078                        onto the following field.  Try to arrange the type for
3079                        possible better packing along the way.  */
3080                     else if (DECL_PACKED (gnu_old_field)
3081                              && TREE_CODE (DECL_SIZE (gnu_old_field))
3082                                 == INTEGER_CST)
3083                       {
3084                         gnu_size = DECL_SIZE (gnu_old_field);
3085                         if (TYPE_MODE (gnu_field_type) == BLKmode
3086                             && TREE_CODE (gnu_field_type) == RECORD_TYPE
3087                             && host_integerp (TYPE_SIZE (gnu_field_type), 1))
3088                           gnu_field_type
3089                             = make_packable_type (gnu_field_type, true);
3090                       }
3091
3092                     if (CONTAINS_PLACEHOLDER_P (gnu_pos))
3093                       for (gnu_temp = gnu_subst_list;
3094                            gnu_temp; gnu_temp = TREE_CHAIN (gnu_temp))
3095                         gnu_pos = substitute_in_expr (gnu_pos,
3096                                                       TREE_PURPOSE (gnu_temp),
3097                                                       TREE_VALUE (gnu_temp));
3098
3099                     /* If the position is now a constant, we can set it as the
3100                        position of the field when we make it.  Otherwise, we need
3101                        to deal with it specially below.  */
3102                     if (TREE_CONSTANT (gnu_pos))
3103                       {
3104                         gnu_new_pos = bit_from_pos (gnu_pos, gnu_bitpos);
3105
3106                         /* Discard old fields that are outside the new type.
3107                            This avoids confusing code scanning it to decide
3108                            how to pass it to functions on some platforms.  */
3109                         if (TREE_CODE (gnu_new_pos) == INTEGER_CST
3110                             && TREE_CODE (TYPE_SIZE (gnu_type)) == INTEGER_CST
3111                             && !integer_zerop (gnu_size)
3112                             && !tree_int_cst_lt (gnu_new_pos,
3113                                                  TYPE_SIZE (gnu_type)))
3114                           continue;
3115                       }
3116
3117                     gnu_field
3118                       = create_field_decl
3119                         (DECL_NAME (gnu_old_field), gnu_field_type, gnu_type,
3120                          DECL_PACKED (gnu_old_field), gnu_size, gnu_new_pos,
3121                          !DECL_NONADDRESSABLE_P (gnu_old_field));
3122
3123                     if (!TREE_CONSTANT (gnu_pos))
3124                       {
3125                         normalize_offset (&gnu_pos, &gnu_bitpos, offset_align);
3126                         DECL_FIELD_OFFSET (gnu_field) = gnu_pos;
3127                         DECL_FIELD_BIT_OFFSET (gnu_field) = gnu_bitpos;
3128                         SET_DECL_OFFSET_ALIGN (gnu_field, offset_align);
3129                         DECL_SIZE (gnu_field) = gnu_size;
3130                         DECL_SIZE_UNIT (gnu_field)
3131                           = convert (sizetype,
3132                                      size_binop (CEIL_DIV_EXPR, gnu_size,
3133                                                  bitsize_unit_node));
3134                         layout_decl (gnu_field, DECL_OFFSET_ALIGN (gnu_field));
3135                       }
3136
3137                     DECL_INTERNAL_P (gnu_field)
3138                       = DECL_INTERNAL_P (gnu_old_field);
3139                     SET_DECL_ORIGINAL_FIELD
3140                       (gnu_field, (DECL_ORIGINAL_FIELD (gnu_old_field)
3141                                    ? DECL_ORIGINAL_FIELD (gnu_old_field)
3142                                    : gnu_old_field));
3143                     DECL_DISCRIMINANT_NUMBER (gnu_field)
3144                       = DECL_DISCRIMINANT_NUMBER (gnu_old_field);
3145                     TREE_THIS_VOLATILE (gnu_field)
3146                       = TREE_THIS_VOLATILE (gnu_old_field);
3147
3148                     /* To match the layout crafted in components_to_record, if
3149                        this is the _Tag field, put it before any discriminants
3150                        instead of after them as for all other fields.  */
3151                     if (Chars (gnat_field) == Name_uTag)
3152                       gnu_field_list = chainon (gnu_field_list, gnu_field);
3153                     else
3154                       {
3155                         TREE_CHAIN (gnu_field) = gnu_field_list;
3156                         gnu_field_list = gnu_field;
3157                       }
3158
3159                     save_gnu_tree (gnat_field, gnu_field, false);
3160                   }
3161
3162               /* Now go through the entities again looking for Itypes that
3163                  we have not elaborated but should (e.g., Etypes of fields
3164                  that have Original_Components).  */
3165               for (gnat_field = First_Entity (gnat_entity);
3166                    Present (gnat_field); gnat_field = Next_Entity (gnat_field))
3167                 if ((Ekind (gnat_field) == E_Discriminant
3168                      || Ekind (gnat_field) == E_Component)
3169                     && !present_gnu_tree (Etype (gnat_field)))
3170                   gnat_to_gnu_entity (Etype (gnat_field), NULL_TREE, 0);
3171
3172               /* Do not finalize it since we're going to modify it below.  */
3173               gnu_field_list = nreverse (gnu_field_list);
3174               finish_record_type (gnu_type, gnu_field_list, 2, true);
3175
3176               /* Finalize size and mode.  */
3177               TYPE_SIZE (gnu_type) = variable_size (TYPE_SIZE (gnu_type));
3178               TYPE_SIZE_UNIT (gnu_type)
3179                 = variable_size (TYPE_SIZE_UNIT (gnu_type));
3180
3181               compute_record_mode (gnu_type);
3182
3183               /* Fill in locations of fields.  */
3184               annotate_rep (gnat_entity, gnu_type);
3185
3186               /* We've built a new type, make an XVS type to show what this
3187                  is a subtype of.  Some debuggers require the XVS type to be
3188                  output first, so do it in that order.  */
3189               if (debug_info_p)
3190                 {
3191                   tree gnu_subtype_marker = make_node (RECORD_TYPE);
3192                   tree gnu_orig_name = TYPE_NAME (gnu_orig_type);
3193
3194                   if (TREE_CODE (gnu_orig_name) == TYPE_DECL)
3195                     gnu_orig_name = DECL_NAME (gnu_orig_name);
3196
3197                   TYPE_NAME (gnu_subtype_marker)
3198                     = create_concat_name (gnat_entity, "XVS");
3199                   finish_record_type (gnu_subtype_marker,
3200                                       create_field_decl (gnu_orig_name,
3201                                                          integer_type_node,
3202                                                          gnu_subtype_marker,
3203                                                          0, NULL_TREE,
3204                                                          NULL_TREE, 0),
3205                                       0, false);
3206
3207                   add_parallel_type (TYPE_STUB_DECL (gnu_type),
3208                                      gnu_subtype_marker);
3209                 }
3210
3211               /* Now we can finalize it.  */
3212               rest_of_record_type_compilation (gnu_type);
3213             }
3214
3215           /* Otherwise, go down all the components in the new type and
3216              make them equivalent to those in the base type.  */
3217           else
3218             for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
3219                  gnat_temp = Next_Entity (gnat_temp))
3220               if ((Ekind (gnat_temp) == E_Discriminant
3221                    && !Is_Unchecked_Union (gnat_base_type))
3222                   || Ekind (gnat_temp) == E_Component)
3223                 save_gnu_tree (gnat_temp,
3224                                gnat_to_gnu_field_decl
3225                                (Original_Record_Component (gnat_temp)), false);
3226         }
3227       break;
3228
3229     case E_Access_Subprogram_Type:
3230       /* Use the special descriptor type for dispatch tables if needed,
3231          that is to say for the Prim_Ptr of a-tags.ads and its clones.
3232          Note that we are only required to do so for static tables in
3233          order to be compatible with the C++ ABI, but Ada 2005 allows
3234          to extend library level tagged types at the local level so
3235          we do it in the non-static case as well.  */
3236       if (TARGET_VTABLE_USES_DESCRIPTORS
3237           && Is_Dispatch_Table_Entity (gnat_entity))
3238         {
3239             gnu_type = fdesc_type_node;
3240             gnu_size = TYPE_SIZE (gnu_type);
3241             break;
3242         }
3243
3244       /* ... fall through ... */
3245
3246     case E_Anonymous_Access_Subprogram_Type:
3247       /* If we are not defining this entity, and we have incomplete
3248          entities being processed above us, make a dummy type and
3249          fill it in later.  */
3250       if (!definition && defer_incomplete_level != 0)
3251         {
3252           struct incomplete *p
3253             = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3254
3255           gnu_type
3256             = build_pointer_type
3257               (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3258           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3259                                        !Comes_From_Source (gnat_entity),
3260                                        debug_info_p, gnat_entity);
3261           this_made_decl = true;
3262           gnu_type = TREE_TYPE (gnu_decl);
3263           save_gnu_tree (gnat_entity, gnu_decl, false);
3264           saved = true;
3265
3266           p->old_type = TREE_TYPE (gnu_type);
3267           p->full_type = Directly_Designated_Type (gnat_entity);
3268           p->next = defer_incomplete_list;
3269           defer_incomplete_list = p;
3270           break;
3271         }
3272
3273       /* ... fall through ... */
3274
3275     case E_Allocator_Type:
3276     case E_Access_Type:
3277     case E_Access_Attribute_Type:
3278     case E_Anonymous_Access_Type:
3279     case E_General_Access_Type:
3280       {
3281         Entity_Id gnat_desig_type = Directly_Designated_Type (gnat_entity);
3282         Entity_Id gnat_desig_equiv = Gigi_Equivalent_Type (gnat_desig_type);
3283         bool is_from_limited_with
3284           = (IN (Ekind (gnat_desig_equiv), Incomplete_Kind)
3285              && From_With_Type (gnat_desig_equiv));
3286
3287         /* Get the "full view" of this entity.  If this is an incomplete
3288            entity from a limited with, treat its non-limited view as the full
3289            view.  Otherwise, if this is an incomplete or private type, use the
3290            full view.  In the former case, we might point to a private type,
3291            in which case, we need its full view.  Also, we want to look at the
3292            actual type used for the representation, so this takes a total of
3293            three steps.  */
3294         Entity_Id gnat_desig_full_direct_first
3295           = (is_from_limited_with ? Non_Limited_View (gnat_desig_equiv)
3296              : (IN (Ekind (gnat_desig_equiv), Incomplete_Or_Private_Kind)
3297                 ? Full_View (gnat_desig_equiv) : Empty));
3298         Entity_Id gnat_desig_full_direct
3299           = ((is_from_limited_with
3300               && Present (gnat_desig_full_direct_first)
3301               && IN (Ekind (gnat_desig_full_direct_first), Private_Kind))
3302              ? Full_View (gnat_desig_full_direct_first)
3303              : gnat_desig_full_direct_first);
3304         Entity_Id gnat_desig_full
3305           = Gigi_Equivalent_Type (gnat_desig_full_direct);
3306
3307         /* This the type actually used to represent the designated type,
3308            either gnat_desig_full or gnat_desig_equiv.  */
3309         Entity_Id gnat_desig_rep;
3310
3311         /* Nonzero if this is a pointer to an unconstrained array.  */
3312         bool is_unconstrained_array;
3313
3314         /* We want to know if we'll be seeing the freeze node for any
3315            incomplete type we may be pointing to.  */
3316         bool in_main_unit
3317           = (Present (gnat_desig_full)
3318              ? In_Extended_Main_Code_Unit (gnat_desig_full)
3319              : In_Extended_Main_Code_Unit (gnat_desig_type));
3320
3321         /* Nonzero if we make a dummy type here.  */
3322         bool got_fat_p = false;
3323         /* Nonzero if the dummy is a fat pointer.  */
3324         bool made_dummy = false;
3325         tree gnu_desig_type = NULL_TREE;
3326         enum machine_mode p_mode = mode_for_size (esize, MODE_INT, 0);
3327
3328         if (!targetm.valid_pointer_mode (p_mode))
3329           p_mode = ptr_mode;
3330
3331         /* If either the designated type or its full view is an unconstrained
3332            array subtype, replace it with the type it's a subtype of.  This
3333            avoids problems with multiple copies of unconstrained array types.
3334            Likewise, if the designated type is a subtype of an incomplete
3335            record type, use the parent type to avoid order of elaboration
3336            issues.  This can lose some code efficiency, but there is no
3337            alternative.  */
3338         if (Ekind (gnat_desig_equiv) == E_Array_Subtype
3339             && ! Is_Constrained (gnat_desig_equiv))
3340           gnat_desig_equiv = Etype (gnat_desig_equiv);
3341         if (Present (gnat_desig_full)
3342             && ((Ekind (gnat_desig_full) == E_Array_Subtype
3343                  && ! Is_Constrained (gnat_desig_full))
3344                 || (Ekind (gnat_desig_full) == E_Record_Subtype
3345                     && Ekind (Etype (gnat_desig_full)) == E_Record_Type)))
3346           gnat_desig_full = Etype (gnat_desig_full);
3347
3348         /* Now set the type that actually marks the representation of
3349            the designated type and also flag whether we have a unconstrained
3350            array.  */
3351         gnat_desig_rep = gnat_desig_full ? gnat_desig_full : gnat_desig_equiv;
3352         is_unconstrained_array
3353           = (Is_Array_Type (gnat_desig_rep)
3354              && ! Is_Constrained (gnat_desig_rep));
3355
3356         /* If we are pointing to an incomplete type whose completion is an
3357            unconstrained array, make a fat pointer type.  The two types in our
3358            fields will be pointers to dummy nodes and will be replaced in
3359            update_pointer_to.  Similarly, if the type itself is a dummy type or
3360            an unconstrained array.  Also make a dummy TYPE_OBJECT_RECORD_TYPE
3361            in case we have any thin pointers to it.  */
3362         if (is_unconstrained_array
3363             && (Present (gnat_desig_full)
3364                 || (present_gnu_tree (gnat_desig_equiv)
3365                     && TYPE_IS_DUMMY_P (TREE_TYPE
3366                                         (get_gnu_tree (gnat_desig_equiv))))
3367                 || (No (gnat_desig_full) && ! in_main_unit
3368                     && defer_incomplete_level != 0
3369                     && ! present_gnu_tree (gnat_desig_equiv))
3370                 || (in_main_unit && is_from_limited_with
3371                     && Present (Freeze_Node (gnat_desig_rep)))))
3372           {
3373             tree gnu_old
3374               = (present_gnu_tree (gnat_desig_rep)
3375                  ? TREE_TYPE (get_gnu_tree (gnat_desig_rep))
3376                  : make_dummy_type (gnat_desig_rep));
3377             tree fields;
3378
3379             /* Show the dummy we get will be a fat pointer.  */
3380             got_fat_p = made_dummy = true;
3381
3382             /* If the call above got something that has a pointer, that
3383                pointer is our type.  This could have happened either
3384                because the type was elaborated or because somebody
3385                else executed the code below.  */
3386             gnu_type = TYPE_POINTER_TO (gnu_old);
3387             if (!gnu_type)
3388               {
3389                 tree gnu_template_type = make_node (ENUMERAL_TYPE);
3390                 tree gnu_ptr_template = build_pointer_type (gnu_template_type);
3391                 tree gnu_array_type = make_node (ENUMERAL_TYPE);
3392                 tree gnu_ptr_array = build_pointer_type (gnu_array_type);
3393
3394                 TYPE_NAME (gnu_template_type)
3395                   = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3396                                          "XUB");
3397                 TYPE_DUMMY_P (gnu_template_type) = 1;
3398
3399                 TYPE_NAME (gnu_array_type)
3400                   = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3401                                          "XUA");
3402                 TYPE_DUMMY_P (gnu_array_type) = 1;
3403
3404                 gnu_type = make_node (RECORD_TYPE);
3405                 SET_TYPE_UNCONSTRAINED_ARRAY (gnu_type, gnu_old);
3406                 TYPE_POINTER_TO (gnu_old) = gnu_type;
3407
3408                 Sloc_to_locus (Sloc (gnat_entity), &input_location);
3409                 fields
3410                   = chainon (chainon (NULL_TREE,
3411                                       create_field_decl
3412                                       (get_identifier ("P_ARRAY"),
3413                                        gnu_ptr_array,
3414                                        gnu_type, 0, 0, 0, 0)),
3415                              create_field_decl (get_identifier ("P_BOUNDS"),
3416                                                 gnu_ptr_template,
3417                                                 gnu_type, 0, 0, 0, 0));
3418
3419                 /* Make sure we can place this into a register.  */
3420                 TYPE_ALIGN (gnu_type)
3421                   = MIN (BIGGEST_ALIGNMENT, 2 * POINTER_SIZE);
3422                 TYPE_IS_FAT_POINTER_P (gnu_type) = 1;
3423
3424                 /* Do not finalize this record type since the types of
3425                    its fields are incomplete.  */
3426                 finish_record_type (gnu_type, fields, 0, true);
3427
3428                 TYPE_OBJECT_RECORD_TYPE (gnu_old) = make_node (RECORD_TYPE);
3429                 TYPE_NAME (TYPE_OBJECT_RECORD_TYPE (gnu_old))
3430                   = concat_id_with_name (get_entity_name (gnat_desig_equiv),
3431                                          "XUT");
3432                 TYPE_DUMMY_P (TYPE_OBJECT_RECORD_TYPE (gnu_old)) = 1;
3433               }
3434           }
3435
3436         /* If we already know what the full type is, use it.  */
3437         else if (Present (gnat_desig_full)
3438                  && present_gnu_tree (gnat_desig_full))
3439           gnu_desig_type = TREE_TYPE (get_gnu_tree (gnat_desig_full));
3440
3441         /* Get the type of the thing we are to point to and build a pointer
3442            to it.  If it is a reference to an incomplete or private type with a
3443            full view that is a record, make a dummy type node and get the
3444            actual type later when we have verified it is safe.  */
3445         else if ((! in_main_unit
3446                   && ! present_gnu_tree (gnat_desig_equiv)
3447                   && Present (gnat_desig_full)
3448                   && ! present_gnu_tree (gnat_desig_full)
3449                   && Is_Record_Type (gnat_desig_full))
3450                  /* Likewise if we are pointing to a record or array and we
3451                     are to defer elaborating incomplete types.  We do this
3452                     since this access type may be the full view of some
3453                     private type.  Note that the unconstrained array case is
3454                     handled above.  */
3455                  || ((! in_main_unit || imported_p)
3456                      && defer_incomplete_level != 0
3457                      && ! present_gnu_tree (gnat_desig_equiv)
3458                      && ((Is_Record_Type (gnat_desig_rep)
3459                           || Is_Array_Type (gnat_desig_rep))))
3460                  /* If this is a reference from a limited_with type back to our
3461                     main unit and there's a Freeze_Node for it, either we have
3462                     already processed the declaration and made the dummy type,
3463                     in which case we just reuse the latter, or we have not yet,
3464                     in which case we make the dummy type and it will be reused
3465                     when the declaration is processed.  In both cases, the
3466                     pointer eventually created below will be automatically
3467                     adjusted when the Freeze_Node is processed.  Note that the
3468                     unconstrained array case is handled above.  */
3469                  ||  (in_main_unit && is_from_limited_with
3470                       && Present (Freeze_Node (gnat_desig_rep))))
3471           {
3472             gnu_desig_type = make_dummy_type (gnat_desig_equiv);
3473             made_dummy = true;
3474           }
3475
3476         /* Otherwise handle the case of a pointer to itself.  */
3477         else if (gnat_desig_equiv == gnat_entity)
3478           {
3479             gnu_type
3480               = build_pointer_type_for_mode (void_type_node, p_mode,
3481                                              No_Strict_Aliasing (gnat_entity));
3482             TREE_TYPE (gnu_type) = TYPE_POINTER_TO (gnu_type) = gnu_type;
3483           }
3484
3485         /* If expansion is disabled, the equivalent type of a concurrent
3486            type is absent, so build a dummy pointer type.  */
3487         else if (type_annotate_only && No (gnat_desig_equiv))
3488           gnu_type = ptr_void_type_node;
3489
3490         /* Finally, handle the straightforward case where we can just
3491            elaborate our designated type and point to it.  */
3492         else
3493           gnu_desig_type = gnat_to_gnu_type (gnat_desig_equiv);
3494
3495         /* It is possible that a call to gnat_to_gnu_type above resolved our
3496            type.  If so, just return it.  */
3497         if (present_gnu_tree (gnat_entity))
3498           {
3499             maybe_present = true;
3500             break;
3501           }
3502
3503         /* If we have a GCC type for the designated type, possibly modify it
3504            if we are pointing only to constant objects and then make a pointer
3505            to it.  Don't do this for unconstrained arrays.  */
3506         if (!gnu_type && gnu_desig_type)
3507           {
3508             if (Is_Access_Constant (gnat_entity)
3509                 && TREE_CODE (gnu_desig_type) != UNCONSTRAINED_ARRAY_TYPE)
3510               {
3511                 gnu_desig_type
3512                   = build_qualified_type
3513                     (gnu_desig_type,
3514                      TYPE_QUALS (gnu_desig_type) | TYPE_QUAL_CONST);
3515
3516                 /* Some extra processing is required if we are building a
3517                    pointer to an incomplete type (in the GCC sense).  We might
3518                    have such a type if we just made a dummy, or directly out
3519                    of the call to gnat_to_gnu_type above if we are processing
3520                    an access type for a record component designating the
3521                    record type itself.  */
3522                 if (TYPE_MODE (gnu_desig_type) == VOIDmode)
3523                   {
3524                     /* We must ensure that the pointer to variant we make will
3525                        be processed by update_pointer_to when the initial type
3526                        is completed.  Pretend we made a dummy and let further
3527                        processing act as usual.  */
3528                     made_dummy = true;
3529
3530                     /* We must ensure that update_pointer_to will not retrieve
3531                        the dummy variant when building a properly qualified
3532                        version of the complete type.  We take advantage of the
3533                        fact that get_qualified_type is requiring TYPE_NAMEs to
3534                        match to influence build_qualified_type and then also
3535                        update_pointer_to here.  */
3536                     TYPE_NAME (gnu_desig_type)
3537                       = create_concat_name (gnat_desig_type, "INCOMPLETE_CST");
3538                   }
3539               }
3540
3541             gnu_type
3542               = build_pointer_type_for_mode (gnu_desig_type, p_mode,
3543                                              No_Strict_Aliasing (gnat_entity));
3544           }
3545
3546         /* If we are not defining this object and we made a dummy pointer,
3547            save our current definition, evaluate the actual type, and replace
3548            the tentative type we made with the actual one.  If we are to defer
3549            actually looking up the actual type, make an entry in the
3550            deferred list.  If this is from a limited with, we have to defer
3551            to the end of the current spec in two cases: first if the
3552            designated type is in the current unit and second if the access
3553            type is.  */
3554         if ((! in_main_unit || is_from_limited_with) && made_dummy)
3555           {
3556             tree gnu_old_type
3557               = TYPE_FAT_POINTER_P (gnu_type)
3558                 ? TYPE_UNCONSTRAINED_ARRAY (gnu_type) : TREE_TYPE (gnu_type);
3559
3560             if (esize == POINTER_SIZE
3561                 && (got_fat_p || TYPE_FAT_POINTER_P (gnu_type)))
3562               gnu_type
3563                 = build_pointer_type
3564                   (TYPE_OBJECT_RECORD_TYPE
3565                    (TYPE_UNCONSTRAINED_ARRAY (gnu_type)));
3566
3567             gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
3568                                          !Comes_From_Source (gnat_entity),
3569                                          debug_info_p, gnat_entity);
3570             this_made_decl = true;
3571             gnu_type = TREE_TYPE (gnu_decl);
3572             save_gnu_tree (gnat_entity, gnu_decl, false);
3573             saved = true;
3574
3575             if (defer_incomplete_level == 0
3576                 && ! (is_from_limited_with
3577                       && (in_main_unit
3578                           || In_Extended_Main_Code_Unit (gnat_entity))))
3579               update_pointer_to (TYPE_MAIN_VARIANT (gnu_old_type),
3580                                  gnat_to_gnu_type (gnat_desig_equiv));
3581
3582               /* Note that the call to gnat_to_gnu_type here might have
3583                  updated gnu_old_type directly, in which case it is not a
3584                  dummy type any more when we get into update_pointer_to.
3585
3586                  This may happen for instance when the designated type is a
3587                  record type, because their elaboration starts with an
3588                  initial node from make_dummy_type, which may yield the same
3589                  node as the one we got.
3590
3591                  Besides, variants of this non-dummy type might have been
3592                  created along the way.  update_pointer_to is expected to
3593                  properly take care of those situations.  */
3594             else
3595               {
3596                 struct incomplete *p
3597                   = (struct incomplete *) xmalloc (sizeof
3598                                                    (struct incomplete));
3599                 struct incomplete **head
3600                   = (is_from_limited_with
3601                      && (in_main_unit
3602                          || In_Extended_Main_Code_Unit (gnat_entity))
3603                      ? &defer_limited_with : &defer_incomplete_list);
3604
3605                 p->old_type = gnu_old_type;
3606                 p->full_type = gnat_desig_equiv;
3607                 p->next = *head;
3608                 *head = p;
3609               }
3610           }
3611       }
3612       break;
3613
3614     case E_Access_Protected_Subprogram_Type:
3615     case E_Anonymous_Access_Protected_Subprogram_Type:
3616       if (type_annotate_only && No (gnat_equiv_type))
3617         gnu_type = ptr_void_type_node;
3618       else
3619         {
3620           /* The runtime representation is the equivalent type.  */
3621           gnu_type = gnat_to_gnu_type (gnat_equiv_type);
3622           maybe_present = true;
3623         }
3624
3625       if (Is_Itype (Directly_Designated_Type (gnat_entity))
3626           && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3627           && No (Freeze_Node (Directly_Designated_Type (gnat_entity)))
3628           && !Is_Record_Type (Scope (Directly_Designated_Type (gnat_entity))))
3629         gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3630                             NULL_TREE, 0);
3631
3632       break;
3633
3634     case E_Access_Subtype:
3635
3636       /* We treat this as identical to its base type; any constraint is
3637          meaningful only to the front end.
3638
3639          The designated type must be elaborated as well, if it does
3640          not have its own freeze node.  Designated (sub)types created
3641          for constrained components of records with discriminants are
3642          not frozen by the front end and thus not elaborated by gigi,
3643          because their use may appear before the base type is frozen,
3644          and because it is not clear that they are needed anywhere in
3645          Gigi.  With the current model, there is no correct place where
3646          they could be elaborated.  */
3647
3648       gnu_type = gnat_to_gnu_type (Etype (gnat_entity));
3649       if (Is_Itype (Directly_Designated_Type (gnat_entity))
3650           && !present_gnu_tree (Directly_Designated_Type (gnat_entity))
3651           && Is_Frozen (Directly_Designated_Type (gnat_entity))
3652           && No (Freeze_Node (Directly_Designated_Type (gnat_entity))))
3653         {
3654           /* If we are not defining this entity, and we have incomplete
3655              entities being processed above us, make a dummy type and
3656              elaborate it later.  */
3657           if (!definition && defer_incomplete_level != 0)
3658             {
3659               struct incomplete *p
3660                 = (struct incomplete *) xmalloc (sizeof (struct incomplete));
3661               tree gnu_ptr_type
3662                 = build_pointer_type
3663                   (make_dummy_type (Directly_Designated_Type (gnat_entity)));
3664
3665               p->old_type = TREE_TYPE (gnu_ptr_type);
3666               p->full_type = Directly_Designated_Type (gnat_entity);
3667               p->next = defer_incomplete_list;
3668               defer_incomplete_list = p;
3669             }
3670           else if (!IN (Ekind (Base_Type
3671                               (Directly_Designated_Type (gnat_entity))),
3672                        Incomplete_Or_Private_Kind))
3673             gnat_to_gnu_entity (Directly_Designated_Type (gnat_entity),
3674                                 NULL_TREE, 0);
3675         }
3676
3677       maybe_present = true;
3678       break;
3679
3680     /* Subprogram Entities
3681
3682        The following access functions are defined for subprograms (functions
3683        or procedures):
3684
3685                 First_Formal    The first formal parameter.
3686                 Is_Imported     Indicates that the subprogram has appeared in
3687                                 an INTERFACE or IMPORT pragma.  For now we
3688                                 assume that the external language is C.
3689                 Is_Exported     Likewise but for an EXPORT pragma.
3690                 Is_Inlined      True if the subprogram is to be inlined.
3691
3692        In addition for function subprograms we have:
3693
3694                 Etype           Return type of the function.
3695
3696        Each parameter is first checked by calling must_pass_by_ref on its
3697        type to determine if it is passed by reference.  For parameters which
3698        are copied in, if they are Ada In Out or Out parameters, their return
3699        value becomes part of a record which becomes the return type of the
3700        function (C function - note that this applies only to Ada procedures
3701        so there is no Ada return type).  Additional code to store back the
3702        parameters will be generated on the caller side.  This transformation
3703        is done here, not in the front-end.
3704
3705        The intended result of the transformation can be seen from the
3706        equivalent source rewritings that follow:
3707
3708                                                 struct temp {int a,b};
3709        procedure P (A,B: In Out ...) is         temp P (int A,B)
3710        begin                                    {
3711          ..                                       ..
3712        end P;                                     return {A,B};
3713                                                 }
3714
3715                                                 temp t;
3716        P(X,Y);                                  t = P(X,Y);
3717                                                 X = t.a , Y = t.b;
3718
3719        For subprogram types we need to perform mainly the same conversions to
3720        GCC form that are needed for procedures and function declarations.  The
3721        only difference is that at the end, we make a type declaration instead
3722        of a function declaration.  */
3723
3724     case E_Subprogram_Type:
3725     case E_Function:
3726     case E_Procedure:
3727       {
3728         /* The first GCC parameter declaration (a PARM_DECL node).  The
3729            PARM_DECL nodes are chained through the TREE_CHAIN field, so this
3730            actually is the head of this parameter list.  */
3731         tree gnu_param_list = NULL_TREE;
3732         /* Likewise for the stub associated with an exported procedure.  */
3733         tree gnu_stub_param_list = NULL_TREE;
3734         /* The type returned by a function.  If the subprogram is a procedure
3735            this type should be void_type_node.  */
3736         tree gnu_return_type = void_type_node;
3737         /* List of fields in return type of procedure with copy-in copy-out
3738            parameters.  */
3739         tree gnu_field_list = NULL_TREE;
3740         /* Non-null for subprograms containing parameters passed by copy-in
3741            copy-out (Ada In Out or Out parameters not passed by reference),
3742            in which case it is the list of nodes used to specify the values of
3743            the in out/out parameters that are returned as a record upon
3744            procedure return.  The TREE_PURPOSE of an element of this list is
3745            a field of the record and the TREE_VALUE is the PARM_DECL
3746            corresponding to that field.  This list will be saved in the
3747            TYPE_CI_CO_LIST field of the FUNCTION_TYPE node we create.  */
3748         tree gnu_return_list = NULL_TREE;
3749         /* If an import pragma asks to map this subprogram to a GCC builtin,
3750            this is the builtin DECL node.  */
3751         tree gnu_builtin_decl = NULL_TREE;
3752         /* For the stub associated with an exported procedure.  */
3753         tree gnu_stub_type = NULL_TREE, gnu_stub_name = NULL_TREE;
3754         tree gnu_ext_name = create_concat_name (gnat_entity, NULL);
3755         Entity_Id gnat_param;
3756         bool inline_flag = Is_Inlined (gnat_entity);
3757         bool public_flag = Is_Public (gnat_entity) || imported_p;
3758         bool extern_flag
3759           = (Is_Public (gnat_entity) && !definition) || imported_p;
3760
3761        /* The semantics of "pure" in Ada essentially matches that of "const"
3762           in the back-end.  In particular, both properties are orthogonal to
3763           the "nothrow" property if the EH circuitry is explicit in the
3764           internal representation of the back-end.  If we are to completely
3765           hide the EH circuitry from it, we need to declare that calls to pure
3766           Ada subprograms that can throw have side effects since they can
3767           trigger an "abnormal" transfer of control flow; thus they can be
3768           neither "const" nor "pure" in the back-end sense.  */
3769         bool const_flag
3770           = (Exception_Mechanism == Back_End_Exceptions
3771              && Is_Pure (gnat_entity));
3772
3773         bool volatile_flag = No_Return (gnat_entity);
3774         bool returns_by_ref = false;
3775         bool returns_unconstrained = false;
3776         bool returns_by_target_ptr = false;
3777         bool has_copy_in_out = false;
3778         bool has_stub = false;
3779         int parmnum;
3780
3781         if (kind == E_Subprogram_Type && !definition)
3782           /* A parameter may refer to this type, so defer completion
3783              of any incomplete types.  */
3784           defer_incomplete_level++, this_deferred = true;
3785
3786         /* If the subprogram has an alias, it is probably inherited, so
3787            we can use the original one.  If the original "subprogram"
3788            is actually an enumeration literal, it may be the first use
3789            of its type, so we must elaborate that type now.  */
3790         if (Present (Alias (gnat_entity)))
3791           {
3792             if (Ekind (Alias (gnat_entity)) == E_Enumeration_Literal)
3793               gnat_to_gnu_entity (Etype (Alias (gnat_entity)), NULL_TREE, 0);
3794
3795             gnu_decl = gnat_to_gnu_entity (Alias (gnat_entity),
3796                                            gnu_expr, 0);
3797
3798             /* Elaborate any Itypes in the parameters of this entity.  */
3799             for (gnat_temp = First_Formal_With_Extras (gnat_entity);
3800                  Present (gnat_temp);
3801                  gnat_temp = Next_Formal_With_Extras (gnat_temp))
3802               if (Is_Itype (Etype (gnat_temp)))
3803                 gnat_to_gnu_entity (Etype (gnat_temp), NULL_TREE, 0);
3804
3805             break;
3806           }
3807
3808         /* If this subprogram is expectedly bound to a GCC builtin, fetch the
3809            corresponding DECL node.
3810
3811            We still want the parameter associations to take place because the
3812            proper generation of calls depends on it (a GNAT parameter without
3813            a corresponding GCC tree has a very specific meaning), so we don't
3814            just break here.  */
3815         if (Convention (gnat_entity) == Convention_Intrinsic)
3816           gnu_builtin_decl = builtin_decl_for (gnu_ext_name);
3817
3818         /* ??? What if we don't find the builtin node above ? warn ? err ?
3819            In the current state we neither warn nor err, and calls will just
3820            be handled as for regular subprograms.  */
3821
3822         if (kind == E_Function || kind == E_Subprogram_Type)
3823           gnu_return_type = gnat_to_gnu_type (Etype (gnat_entity));
3824
3825         /* If this function returns by reference, make the actual
3826            return type of this function the pointer and mark the decl.  */
3827         if (Returns_By_Ref (gnat_entity))
3828           {
3829             returns_by_ref = true;
3830             gnu_return_type = build_pointer_type (gnu_return_type);
3831           }
3832
3833         /* If the Mechanism is By_Reference, ensure the return type uses
3834            the machine's by-reference mechanism, which may not the same
3835            as above (e.g., it might be by passing a fake parameter).  */
3836         else if (kind == E_Function
3837                  && Mechanism (gnat_entity) == By_Reference)
3838           {
3839             TREE_ADDRESSABLE (gnu_return_type) = 1;
3840
3841             /* We expect this bit to be reset by gigi shortly, so can avoid a
3842                type node copy here.  This actually also prevents troubles with
3843                the generation of debug information for the function, because
3844                we might have issued such info for this type already, and would
3845                be attaching a distinct type node to the function if we made a
3846                copy here.  */
3847           }
3848
3849         /* If we are supposed to return an unconstrained array,
3850            actually return a fat pointer and make a note of that.  Return
3851            a pointer to an unconstrained record of variable size.  */
3852         else if (TREE_CODE (gnu_return_type) == UNCONSTRAINED_ARRAY_TYPE)
3853           {
3854             gnu_return_type = TREE_TYPE (gnu_return_type);
3855             returns_unconstrained = true;
3856           }
3857
3858         /* If the type requires a transient scope, the result is allocated
3859            on the secondary stack, so the result type of the function is
3860            just a pointer.  */
3861         else if (Requires_Transient_Scope (Etype (gnat_entity)))
3862           {
3863             gnu_return_type = build_pointer_type (gnu_return_type);
3864             returns_unconstrained = true;
3865           }
3866
3867         /* If the type is a padded type and the underlying type would not
3868            be passed by reference or this function has a foreign convention,
3869            return the underlying type.  */
3870         else if (TREE_CODE (gnu_return_type) == RECORD_TYPE
3871                  && TYPE_IS_PADDING_P (gnu_return_type)
3872                  && (!default_pass_by_ref (TREE_TYPE
3873                                            (TYPE_FIELDS (gnu_return_type)))
3874                      || Has_Foreign_Convention (gnat_entity)))
3875           gnu_return_type = TREE_TYPE (TYPE_FIELDS (gnu_return_type));
3876
3877         /* If the return type has a non-constant size, we convert the function
3878            into a procedure and its caller will pass a pointer to an object as
3879            the first parameter when we call the function.  This can happen for
3880            an unconstrained type with a maximum size or a constrained type with
3881            a size not known at compile time.  */
3882         if (TYPE_SIZE_UNIT (gnu_return_type)
3883             && !TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type)))
3884           {
3885             returns_by_target_ptr = true;
3886             gnu_param_list
3887               = create_param_decl (get_identifier ("TARGET"),
3888                                    build_reference_type (gnu_return_type),
3889                                    true);
3890             gnu_return_type = void_type_node;
3891           }
3892
3893         /* If the return type has a size that overflows, we cannot have
3894            a function that returns that type.  This usage doesn't make
3895            sense anyway, so give an error here.  */
3896         if (TYPE_SIZE_UNIT (gnu_return_type)
3897             && TREE_CONSTANT (TYPE_SIZE_UNIT (gnu_return_type))
3898             && TREE_OVERFLOW (TYPE_SIZE_UNIT (gnu_return_type)))
3899           {
3900             post_error ("cannot return type whose size overflows",
3901                         gnat_entity);
3902             gnu_return_type = copy_node (gnu_return_type);
3903             TYPE_SIZE (gnu_return_type) = bitsize_zero_node;
3904             TYPE_SIZE_UNIT (gnu_return_type) = size_zero_node;
3905             TYPE_MAIN_VARIANT (gnu_return_type) = gnu_return_type;
3906             TYPE_NEXT_VARIANT (gnu_return_type) = NULL_TREE;
3907           }
3908
3909         /* Look at all our parameters and get the type of
3910            each.  While doing this, build a copy-out structure if
3911            we need one.  */
3912
3913         /* Loop over the parameters and get their associated GCC tree.
3914            While doing this, build a copy-out structure if we need one.  */
3915         for (gnat_param = First_Formal_With_Extras (gnat_entity), parmnum = 0;
3916              Present (gnat_param);
3917              gnat_param = Next_Formal_With_Extras (gnat_param), parmnum++)
3918           {
3919             tree gnu_param_name = get_entity_name (gnat_param);
3920             tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
3921             tree gnu_param, gnu_field;
3922             bool copy_in_copy_out = false;
3923             Mechanism_Type mech = Mechanism (gnat_param);
3924
3925             /* Builtins are expanded inline and there is no real call sequence
3926                involved.  So the type expected by the underlying expander is
3927                always the type of each argument "as is".  */
3928             if (gnu_builtin_decl)
3929               mech = By_Copy;
3930             /* Handle the first parameter of a valued procedure specially.  */
3931             else if (Is_Valued_Procedure (gnat_entity) && parmnum == 0)
3932               mech = By_Copy_Return;
3933             /* Otherwise, see if a Mechanism was supplied that forced this
3934                parameter to be passed one way or another.  */
3935             else if (mech == Default
3936                      || mech == By_Copy || mech == By_Reference)
3937               ;
3938             else if (By_Descriptor_Last <= mech && mech <= By_Descriptor)
3939               mech = By_Descriptor;
3940
3941             else if (By_Short_Descriptor_Last <= mech &&
3942                      mech <= By_Short_Descriptor)
3943               mech = By_Short_Descriptor;
3944
3945             else if (mech > 0)
3946               {
3947                 if (TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE
3948                     || TREE_CODE (TYPE_SIZE (gnu_param_type)) != INTEGER_CST
3949                     || 0 < compare_tree_int (TYPE_SIZE (gnu_param_type),
3950                                              mech))
3951                   mech = By_Reference;
3952                 else
3953                   mech = By_Copy;
3954               }
3955             else
3956               {
3957                 post_error ("unsupported mechanism for&", gnat_param);
3958                 mech = Default;
3959               }
3960
3961             gnu_param
3962               = gnat_to_gnu_param (gnat_param, mech, gnat_entity,
3963                                    Has_Foreign_Convention (gnat_entity),
3964                                    &copy_in_copy_out);
3965
3966             /* We are returned either a PARM_DECL or a type if no parameter
3967                needs to be passed; in either case, adjust the type.  */
3968             if (DECL_P (gnu_param))
3969               gnu_param_type = TREE_TYPE (gnu_param);
3970             else
3971               {
3972                 gnu_param_type = gnu_param;
3973                 gnu_param = NULL_TREE;
3974               }
3975
3976             if (gnu_param)
3977               {
3978                 /* If it's an exported subprogram, we build a parameter list
3979                    in parallel, in case we need to emit a stub for it.  */
3980                 if (Is_Exported (gnat_entity))
3981                   {
3982                     gnu_stub_param_list
3983                       = chainon (gnu_param, gnu_stub_param_list);
3984                     /* Change By_Descriptor parameter to By_Reference for
3985                        the internal version of an exported subprogram.  */
3986                     if (mech == By_Descriptor || mech == By_Short_Descriptor)
3987                       {
3988                         gnu_param
3989                           = gnat_to_gnu_param (gnat_param, By_Reference,
3990                                                gnat_entity, false,
3991                                                &copy_in_copy_out);
3992                         has_stub = true;
3993                       }
3994                     else
3995                       gnu_param = copy_node (gnu_param);
3996                   }
3997
3998                 gnu_param_list = chainon (gnu_param, gnu_param_list);
3999                 Sloc_to_locus (Sloc (gnat_param),
4000                                &DECL_SOURCE_LOCATION (gnu_param));
4001                 save_gnu_tree (gnat_param, gnu_param, false);
4002
4003                 /* If a parameter is a pointer, this function may modify
4004                    memory through it and thus shouldn't be considered
4005                    a const function.  Also, the memory may be modified
4006                    between two calls, so they can't be CSE'ed.  The latter
4007                    case also handles by-ref parameters.  */
4008                 if (POINTER_TYPE_P (gnu_param_type)
4009                     || TYPE_FAT_POINTER_P (gnu_param_type))
4010                   const_flag = false;
4011               }
4012
4013             if (copy_in_copy_out)
4014               {
4015                 if (!has_copy_in_out)
4016                   {
4017                     gcc_assert (TREE_CODE (gnu_return_type) == VOID_TYPE);
4018                     gnu_return_type = make_node (RECORD_TYPE);
4019                     TYPE_NAME (gnu_return_type) = get_identifier ("RETURN");
4020                     has_copy_in_out = true;
4021                   }
4022
4023                 gnu_field = create_field_decl (gnu_param_name, gnu_param_type,
4024                                                gnu_return_type, 0, 0, 0, 0);
4025                 Sloc_to_locus (Sloc (gnat_param),
4026                                &DECL_SOURCE_LOCATION (gnu_field));
4027                 TREE_CHAIN (gnu_field) = gnu_field_list;
4028                 gnu_field_list = gnu_field;
4029                 gnu_return_list = tree_cons (gnu_field, gnu_param,
4030                                              gnu_return_list);
4031               }
4032           }
4033
4034         /* Do not compute record for out parameters if subprogram is
4035            stubbed since structures are incomplete for the back-end.  */
4036         if (gnu_field_list && Convention (gnat_entity) != Convention_Stubbed)
4037           finish_record_type (gnu_return_type, nreverse (gnu_field_list),
4038                               0, false);
4039
4040         /* If we have a CICO list but it has only one entry, we convert
4041            this function into a function that simply returns that one
4042            object.  */
4043         if (list_length (gnu_return_list) == 1)
4044           gnu_return_type = TREE_TYPE (TREE_PURPOSE (gnu_return_list));
4045
4046         if (Has_Stdcall_Convention (gnat_entity))
4047           prepend_one_attribute_to
4048             (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4049              get_identifier ("stdcall"), NULL_TREE,
4050              gnat_entity);
4051
4052         /* If we are on a target where stack realignment is needed for 'main'
4053            to honor GCC's implicit expectations (stack alignment greater than
4054            what the base ABI guarantees), ensure we do the same for foreign
4055            convention subprograms as they might be used as callbacks from code
4056            breaking such expectations.  Note that this applies to task entry
4057            points in particular.  */
4058         if (FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
4059             && Has_Foreign_Convention (gnat_entity))
4060           prepend_one_attribute_to
4061             (&attr_list, ATTR_MACHINE_ATTRIBUTE,
4062              get_identifier ("force_align_arg_pointer"), NULL_TREE,
4063              gnat_entity);
4064
4065         /* The lists have been built in reverse.  */
4066         gnu_param_list = nreverse (gnu_param_list);
4067         if (has_stub)
4068           gnu_stub_param_list = nreverse (gnu_stub_param_list);
4069         gnu_return_list = nreverse (gnu_return_list);
4070
4071         if (Ekind (gnat_entity) == E_Function)
4072           Set_Mechanism (gnat_entity,
4073                          (returns_by_ref || returns_unconstrained
4074                           ? By_Reference : By_Copy));
4075         gnu_type
4076           = create_subprog_type (gnu_return_type, gnu_param_list,
4077                                  gnu_return_list, returns_unconstrained,
4078                                  returns_by_ref, returns_by_target_ptr);
4079
4080         if (has_stub)
4081           gnu_stub_type
4082             = create_subprog_type (gnu_return_type, gnu_stub_param_list,
4083                                    gnu_return_list, returns_unconstrained,
4084                                    returns_by_ref, returns_by_target_ptr);
4085
4086         /* A subprogram (something that doesn't return anything) shouldn't
4087            be considered const since there would be no reason for such a
4088            subprogram.  Note that procedures with Out (or In Out) parameters
4089            have already been converted into a function with a return type.  */
4090         if (TREE_CODE (gnu_return_type) == VOID_TYPE)
4091           const_flag = false;
4092
4093         gnu_type
4094           = build_qualified_type (gnu_type,
4095                                   TYPE_QUALS (gnu_type)
4096                                   | (TYPE_QUAL_CONST * const_flag)
4097                                   | (TYPE_QUAL_VOLATILE * volatile_flag));
4098
4099         Sloc_to_locus (Sloc (gnat_entity), &input_location);
4100
4101         if (has_stub)
4102           gnu_stub_type
4103             = build_qualified_type (gnu_stub_type,
4104                                     TYPE_QUALS (gnu_stub_type)
4105                                     | (TYPE_QUAL_CONST * const_flag)
4106                                     | (TYPE_QUAL_VOLATILE * volatile_flag));
4107
4108         /* If we have a builtin decl for that function, check the signatures
4109            compatibilities.  If the signatures are compatible, use the builtin
4110            decl.  If they are not, we expect the checker predicate to have
4111            posted the appropriate errors, and just continue with what we have
4112            so far.  */
4113         if (gnu_builtin_decl)
4114           {
4115             tree gnu_builtin_type = TREE_TYPE (gnu_builtin_decl);
4116
4117             if (compatible_signatures_p (gnu_type, gnu_builtin_type))
4118               {
4119                 gnu_decl = gnu_builtin_decl;
4120                 gnu_type = gnu_builtin_type;
4121                 break;
4122               }
4123           }
4124
4125         /* If there was no specified Interface_Name and the external and
4126            internal names of the subprogram are the same, only use the
4127            internal name to allow disambiguation of nested subprograms.  */
4128         if (No (Interface_Name (gnat_entity)) && gnu_ext_name == gnu_entity_id)
4129           gnu_ext_name = NULL_TREE;
4130
4131         /* If we are defining the subprogram and it has an Address clause
4132            we must get the address expression from the saved GCC tree for the
4133            subprogram if it has a Freeze_Node.  Otherwise, we elaborate
4134            the address expression here since the front-end has guaranteed
4135            in that case that the elaboration has no effects.  If there is
4136            an Address clause and we are not defining the object, just
4137            make it a constant.  */
4138         if (Present (Address_Clause (gnat_entity)))
4139           {
4140             tree gnu_address = NULL_TREE;
4141
4142             if (definition)
4143               gnu_address
4144                 = (present_gnu_tree (gnat_entity)
4145                    ? get_gnu_tree (gnat_entity)
4146                    : gnat_to_gnu (Expression (Address_Clause (gnat_entity))));
4147
4148             save_gnu_tree (gnat_entity, NULL_TREE, false);
4149
4150             /* Convert the type of the object to a reference type that can
4151                alias everything as per 13.3(19).  */
4152             gnu_type
4153               = build_reference_type_for_mode (gnu_type, ptr_mode, true);
4154             if (gnu_address)
4155               gnu_address = convert (gnu_type, gnu_address);
4156
4157             gnu_decl
4158               = create_var_decl (gnu_entity_id, gnu_ext_name, gnu_type,
4159                                  gnu_address, false, Is_Public (gnat_entity),
4160                                  extern_flag, false, NULL, gnat_entity);
4161             DECL_BY_REF_P (gnu_decl) = 1;
4162           }
4163
4164         else if (kind == E_Subprogram_Type)
4165           gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4166                                        !Comes_From_Source (gnat_entity),
4167                                        debug_info_p, gnat_entity);
4168         else
4169           {
4170             if (has_stub)
4171               {
4172                 gnu_stub_name = gnu_ext_name;
4173                 gnu_ext_name = create_concat_name (gnat_entity, "internal");
4174                 public_flag = false;
4175               }
4176
4177             gnu_decl = create_subprog_decl (gnu_entity_id, gnu_ext_name,
4178                                             gnu_type, gnu_param_list,
4179                                             inline_flag, public_flag,
4180                                             extern_flag, attr_list,
4181                                             gnat_entity);
4182             if (has_stub)
4183               {
4184                 tree gnu_stub_decl
4185                   = create_subprog_decl (gnu_entity_id, gnu_stub_name,
4186                                          gnu_stub_type, gnu_stub_param_list,
4187                                          inline_flag, true,
4188                                          extern_flag, attr_list,
4189                                          gnat_entity);
4190                 SET_DECL_FUNCTION_STUB (gnu_decl, gnu_stub_decl);
4191               }
4192
4193             /* This is unrelated to the stub built right above.  */
4194             DECL_STUBBED_P (gnu_decl)
4195               = Convention (gnat_entity) == Convention_Stubbed;
4196           }
4197       }
4198       break;
4199
4200     case E_Incomplete_Type:
4201     case E_Incomplete_Subtype:
4202     case E_Private_Type:
4203     case E_Private_Subtype:
4204     case E_Limited_Private_Type:
4205     case E_Limited_Private_Subtype:
4206     case E_Record_Type_With_Private:
4207     case E_Record_Subtype_With_Private:
4208       {
4209         /* Get the "full view" of this entity.  If this is an incomplete
4210            entity from a limited with, treat its non-limited view as the
4211            full view.  Otherwise, use either the full view or the underlying
4212            full view, whichever is present.  This is used in all the tests
4213            below.  */
4214         Entity_Id full_view
4215           = (IN (Ekind (gnat_entity), Incomplete_Kind)
4216              && From_With_Type (gnat_entity))
4217             ? Non_Limited_View (gnat_entity)
4218             : Present (Full_View (gnat_entity))
4219               ? Full_View (gnat_entity)
4220               : Underlying_Full_View (gnat_entity);
4221
4222         /* If this is an incomplete type with no full view, it must be a Taft
4223            Amendment type, in which case we return a dummy type.  Otherwise,
4224            just get the type from its Etype.  */
4225         if (No (full_view))
4226           {
4227             if (kind == E_Incomplete_Type)
4228               gnu_type = make_dummy_type (gnat_entity);
4229             else
4230               {
4231                 gnu_decl = gnat_to_gnu_entity (Etype (gnat_entity),
4232                                                NULL_TREE, 0);
4233                 maybe_present = true;
4234               }
4235             break;
4236           }
4237
4238         /* If we already made a type for the full view, reuse it.  */
4239         else if (present_gnu_tree (full_view))
4240           {
4241             gnu_decl = get_gnu_tree (full_view);
4242             break;
4243           }
4244
4245         /* Otherwise, if we are not defining the type now, get the type
4246            from the full view.  But always get the type from the full view
4247            for define on use types, since otherwise we won't see them!  */
4248         else if (!definition
4249                  || (Is_Itype (full_view)
4250                    && No (Freeze_Node (gnat_entity)))
4251                  || (Is_Itype (gnat_entity)
4252                    && No (Freeze_Node (full_view))))
4253           {
4254             gnu_decl = gnat_to_gnu_entity (full_view, NULL_TREE, 0);
4255             maybe_present = true;
4256             break;
4257           }
4258
4259         /* For incomplete types, make a dummy type entry which will be
4260            replaced later.  */
4261         gnu_type = make_dummy_type (gnat_entity);
4262
4263         /* Save this type as the full declaration's type so we can do any
4264            needed updates when we see it.  */
4265         gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4266                                      !Comes_From_Source (gnat_entity),
4267                                      debug_info_p, gnat_entity);
4268         save_gnu_tree (full_view, gnu_decl, 0);
4269         break;
4270       }
4271
4272       /* Simple class_wide types are always viewed as their root_type
4273          by Gigi unless an Equivalent_Type is specified.  */
4274     case E_Class_Wide_Type:
4275       gnu_decl = gnat_to_gnu_entity (gnat_equiv_type, NULL_TREE, 0);
4276       maybe_present = true;
4277       break;
4278
4279     case E_Task_Type:
4280     case E_Task_Subtype:
4281     case E_Protected_Type:
4282     case E_Protected_Subtype:
4283       if (type_annotate_only && No (gnat_equiv_type))
4284         gnu_type = void_type_node;
4285       else
4286         gnu_type = gnat_to_gnu_type (gnat_equiv_type);
4287
4288       maybe_present = true;
4289       break;
4290
4291     case E_Label:
4292       gnu_decl = create_label_decl (gnu_entity_id);
4293       break;
4294
4295     case E_Block:
4296     case E_Loop:
4297       /* Nothing at all to do here, so just return an ERROR_MARK and claim
4298          we've already saved it, so we don't try to.  */
4299       gnu_decl = error_mark_node;
4300       saved = true;
4301       break;
4302
4303     default:
4304       gcc_unreachable ();
4305     }
4306
4307   /* If we had a case where we evaluated another type and it might have
4308      defined this one, handle it here.  */
4309   if (maybe_present && present_gnu_tree (gnat_entity))
4310     {
4311       gnu_decl = get_gnu_tree (gnat_entity);
4312       saved = true;
4313     }
4314
4315   /* If we are processing a type and there is either no decl for it or
4316      we just made one, do some common processing for the type, such as
4317      handling alignment and possible padding.  */
4318
4319   if ((!gnu_decl || this_made_decl) && IN (kind, Type_Kind))
4320     {
4321       if (Is_Tagged_Type (gnat_entity)
4322           || Is_Class_Wide_Equivalent_Type (gnat_entity))
4323         TYPE_ALIGN_OK (gnu_type) = 1;
4324
4325       if (AGGREGATE_TYPE_P (gnu_type) && Is_By_Reference_Type (gnat_entity))
4326         TYPE_BY_REFERENCE_P (gnu_type) = 1;
4327
4328       /* ??? Don't set the size for a String_Literal since it is either
4329          confirming or we don't handle it properly (if the low bound is
4330          non-constant).  */
4331       if (!gnu_size && kind != E_String_Literal_Subtype)
4332         gnu_size = validate_size (Esize (gnat_entity), gnu_type, gnat_entity,
4333                                   TYPE_DECL, false,
4334                                   Has_Size_Clause (gnat_entity));
4335
4336       /* If a size was specified, see if we can make a new type of that size
4337          by rearranging the type, for example from a fat to a thin pointer.  */
4338       if (gnu_size)
4339         {
4340           gnu_type
4341             = make_type_from_size (gnu_type, gnu_size,
4342                                    Has_Biased_Representation (gnat_entity));
4343
4344           if (operand_equal_p (TYPE_SIZE (gnu_type), gnu_size, 0)
4345               && operand_equal_p (rm_size (gnu_type), gnu_size, 0))
4346             gnu_size = 0;
4347         }
4348
4349       /* If the alignment hasn't already been processed and this is
4350          not an unconstrained array, see if an alignment is specified.
4351          If not, we pick a default alignment for atomic objects.  */
4352       if (align != 0 || TREE_CODE (gnu_type) == UNCONSTRAINED_ARRAY_TYPE)
4353         ;
4354       else if (Known_Alignment (gnat_entity))
4355         {
4356           align = validate_alignment (Alignment (gnat_entity), gnat_entity,
4357                                       TYPE_ALIGN (gnu_type));
4358
4359           /* Warn on suspiciously large alignments.  This should catch
4360              errors about the (alignment,byte)/(size,bit) discrepancy.  */
4361           if (align > BIGGEST_ALIGNMENT && Has_Alignment_Clause (gnat_entity))
4362             {
4363               tree size;
4364
4365               /* If a size was specified, take it into account.  Otherwise
4366                  use the RM size for records as the type size has already
4367                  been adjusted to the alignment.  */
4368               if (gnu_size)
4369                 size = gnu_size;
4370               else if ((TREE_CODE (gnu_type) == RECORD_TYPE
4371                         || TREE_CODE (gnu_type) == UNION_TYPE
4372                         || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
4373                        && !TYPE_IS_FAT_POINTER_P (gnu_type))
4374                 size = rm_size (gnu_type);
4375               else
4376                 size = TYPE_SIZE (gnu_type);
4377
4378               /* Consider an alignment as suspicious if the alignment/size
4379                  ratio is greater or equal to the byte/bit ratio.  */
4380               if (host_integerp (size, 1)
4381                   && align >= TREE_INT_CST_LOW (size) * BITS_PER_UNIT)
4382                 post_error_ne ("?suspiciously large alignment specified for&",
4383                                Expression (Alignment_Clause (gnat_entity)),
4384                                gnat_entity);
4385             }
4386         }
4387       else if (Is_Atomic (gnat_entity) && !gnu_size
4388                && host_integerp (TYPE_SIZE (gnu_type), 1)
4389                && integer_pow2p (TYPE_SIZE (gnu_type)))
4390         align = MIN (BIGGEST_ALIGNMENT,
4391                      tree_low_cst (TYPE_SIZE (gnu_type), 1));
4392       else if (Is_Atomic (gnat_entity) && gnu_size
4393                && host_integerp (gnu_size, 1)
4394                && integer_pow2p (gnu_size))
4395         align = MIN (BIGGEST_ALIGNMENT, tree_low_cst (gnu_size, 1));
4396
4397       /* See if we need to pad the type.  If we did, and made a record,
4398          the name of the new type may be changed.  So get it back for
4399          us when we make the new TYPE_DECL below.  */
4400       if (gnu_size || align > 0)
4401         gnu_type = maybe_pad_type (gnu_type, gnu_size, align, gnat_entity,
4402                                    "PAD", true, definition, false);
4403
4404       if (TREE_CODE (gnu_type) == RECORD_TYPE
4405           && TYPE_IS_PADDING_P (gnu_type))
4406         {
4407           gnu_entity_id = TYPE_NAME (gnu_type);
4408           if (TREE_CODE (gnu_entity_id) == TYPE_DECL)
4409             gnu_entity_id = DECL_NAME (gnu_entity_id);
4410         }
4411
4412       set_rm_size (RM_Size (gnat_entity), gnu_type, gnat_entity);
4413
4414       /* If we are at global level, GCC will have applied variable_size to
4415          the type, but that won't have done anything.  So, if it's not
4416          a constant or self-referential, call elaborate_expression_1 to
4417          make a variable for the size rather than calculating it each time.
4418          Handle both the RM size and the actual size.  */
4419       if (global_bindings_p ()
4420           && TYPE_SIZE (gnu_type)
4421           && !TREE_CONSTANT (TYPE_SIZE (gnu_type))
4422           && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_type)))
4423         {
4424           if (TREE_CODE (gnu_type) == RECORD_TYPE
4425               && operand_equal_p (TYPE_ADA_SIZE (gnu_type),
4426                                   TYPE_SIZE (gnu_type), 0))
4427             {
4428               TYPE_SIZE (gnu_type)
4429                 = elaborate_expression_1 (gnat_entity, gnat_entity,
4430                                           TYPE_SIZE (gnu_type),
4431                                           get_identifier ("SIZE"),
4432                                           definition, 0);
4433               SET_TYPE_ADA_SIZE (gnu_type, TYPE_SIZE (gnu_type));
4434             }
4435           else
4436             {
4437               TYPE_SIZE (gnu_type)
4438                 = elaborate_expression_1 (gnat_entity, gnat_entity,
4439                                           TYPE_SIZE (gnu_type),
4440                                           get_identifier ("SIZE"),
4441                                           definition, 0);
4442
4443               /* ??? For now, store the size as a multiple of the alignment
4444                  in bytes so that we can see the alignment from the tree.  */
4445               TYPE_SIZE_UNIT (gnu_type)
4446                 = build_binary_op
4447                   (MULT_EXPR, sizetype,
4448                    elaborate_expression_1
4449                    (gnat_entity, gnat_entity,
4450                     build_binary_op (EXACT_DIV_EXPR, sizetype,
4451                                      TYPE_SIZE_UNIT (gnu_type),
4452                                      size_int (TYPE_ALIGN (gnu_type)
4453                                                / BITS_PER_UNIT)),
4454                     get_identifier ("SIZE_A_UNIT"),
4455                     definition, 0),
4456                    size_int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4457
4458               if (TREE_CODE (gnu_type) == RECORD_TYPE)
4459                 SET_TYPE_ADA_SIZE
4460                   (gnu_type,
4461                    elaborate_expression_1 (gnat_entity,
4462                                            gnat_entity,
4463                                            TYPE_ADA_SIZE (gnu_type),
4464                                            get_identifier ("RM_SIZE"),
4465                                            definition, 0));
4466                  }
4467         }
4468
4469       /* If this is a record type or subtype, call elaborate_expression_1 on
4470          any field position.  Do this for both global and local types.
4471          Skip any fields that we haven't made trees for to avoid problems with
4472          class wide types.  */
4473       if (IN (kind, Record_Kind))
4474         for (gnat_temp = First_Entity (gnat_entity); Present (gnat_temp);
4475              gnat_temp = Next_Entity (gnat_temp))
4476           if (Ekind (gnat_temp) == E_Component && present_gnu_tree (gnat_temp))
4477             {
4478               tree gnu_field = get_gnu_tree (gnat_temp);
4479
4480               /* ??? Unfortunately, GCC needs to be able to prove the
4481                  alignment of this offset and if it's a variable, it can't.
4482                  In GCC 3.4, we'll use DECL_OFFSET_ALIGN in some way, but
4483                  right now, we have to put in an explicit multiply and
4484                  divide by that value.  */
4485               if (!CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (gnu_field)))
4486                 {
4487                 DECL_FIELD_OFFSET (gnu_field)
4488                   = build_binary_op
4489                     (MULT_EXPR, sizetype,
4490                      elaborate_expression_1
4491                      (gnat_temp, gnat_temp,
4492                       build_binary_op (EXACT_DIV_EXPR, sizetype,
4493                                        DECL_FIELD_OFFSET (gnu_field),
4494                                        size_int (DECL_OFFSET_ALIGN (gnu_field)
4495                                                  / BITS_PER_UNIT)),
4496                       get_identifier ("OFFSET"),
4497                       definition, 0),
4498                      size_int (DECL_OFFSET_ALIGN (gnu_field) / BITS_PER_UNIT));
4499
4500                 /* ??? The context of gnu_field is not necessarily gnu_type so
4501                    the MULT_EXPR node built above may not be marked by the call
4502                    to create_type_decl below.  */
4503                 if (global_bindings_p ())
4504                   mark_visited (&DECL_FIELD_OFFSET (gnu_field));
4505                 }
4506             }
4507
4508       gnu_type = build_qualified_type (gnu_type,
4509                                        (TYPE_QUALS (gnu_type)
4510                                         | (TYPE_QUAL_VOLATILE
4511                                            * Treat_As_Volatile (gnat_entity))));
4512
4513       if (Is_Atomic (gnat_entity))
4514         check_ok_for_atomic (gnu_type, gnat_entity, false);
4515
4516       if (Present (Alignment_Clause (gnat_entity)))
4517         TYPE_USER_ALIGN (gnu_type) = 1;
4518
4519       if (Universal_Aliasing (gnat_entity))
4520         TYPE_UNIVERSAL_ALIASING_P (TYPE_MAIN_VARIANT (gnu_type)) = 1;
4521
4522       if (!gnu_decl)
4523         gnu_decl = create_type_decl (gnu_entity_id, gnu_type, attr_list,
4524                                      !Comes_From_Source (gnat_entity),
4525                                      debug_info_p, gnat_entity);
4526       else
4527         TREE_TYPE (gnu_decl) = gnu_type;
4528     }
4529
4530   if (IN (kind, Type_Kind) && !TYPE_IS_DUMMY_P (TREE_TYPE (gnu_decl)))
4531     {
4532       gnu_type = TREE_TYPE (gnu_decl);
4533
4534       /* Back-annotate the Alignment of the type if not already in the
4535          tree.  Likewise for sizes.  */
4536       if (Unknown_Alignment (gnat_entity))
4537         Set_Alignment (gnat_entity,
4538                        UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
4539
4540       if (Unknown_Esize (gnat_entity) && TYPE_SIZE (gnu_type))
4541         {
4542           /* If the size is self-referential, we annotate the maximum
4543              value of that size.  */
4544           tree gnu_size = TYPE_SIZE (gnu_type);
4545
4546           if (CONTAINS_PLACEHOLDER_P (gnu_size))
4547             gnu_size = max_size (gnu_size, true);
4548
4549           Set_Esize (gnat_entity, annotate_value (gnu_size));
4550
4551           if (type_annotate_only && Is_Tagged_Type (gnat_entity))
4552             {
4553               /* In this mode the tag and the parent components are not
4554                  generated by the front-end, so the sizes must be adjusted
4555                  explicitly now.  */
4556               int size_offset, new_size;
4557
4558               if (Is_Derived_Type (gnat_entity))
4559                 {
4560                   size_offset
4561                     = UI_To_Int (Esize (Etype (Base_Type (gnat_entity))));
4562                   Set_Alignment (gnat_entity,
4563                                  Alignment (Etype (Base_Type (gnat_entity))));
4564                 }
4565               else
4566                 size_offset = POINTER_SIZE;
4567
4568               new_size = UI_To_Int (Esize (gnat_entity)) + size_offset;
4569               Set_Esize (gnat_entity,
4570                          UI_From_Int (((new_size + (POINTER_SIZE - 1))
4571                                        / POINTER_SIZE) * POINTER_SIZE));
4572               Set_RM_Size (gnat_entity, Esize (gnat_entity));
4573             }
4574         }
4575
4576       if (Unknown_RM_Size (gnat_entity) && rm_size (gnu_type))
4577         Set_RM_Size (gnat_entity, annotate_value (rm_size (gnu_type)));
4578     }
4579
4580   if (!Comes_From_Source (gnat_entity) && DECL_P (gnu_decl))
4581     DECL_ARTIFICIAL (gnu_decl) = 1;
4582
4583   if (!debug_info_p && DECL_P (gnu_decl)
4584       && TREE_CODE (gnu_decl) != FUNCTION_DECL
4585       && No (Renamed_Object (gnat_entity)))
4586     DECL_IGNORED_P (gnu_decl) = 1;
4587
4588   /* If we haven't already, associate the ..._DECL node that we just made with
4589      the input GNAT entity node.  */
4590   if (!saved)
4591     save_gnu_tree (gnat_entity, gnu_decl, false);
4592
4593   /* If this is an enumeral or floating-point type, we were not able to set
4594      the bounds since they refer to the type.  These bounds are always static.
4595
4596      For enumeration types, also write debugging information and declare the
4597      enumeration literal  table, if needed.  */
4598
4599   if ((kind == E_Enumeration_Type && Present (First_Literal (gnat_entity)))
4600       || (kind == E_Floating_Point_Type && !Vax_Float (gnat_entity)))
4601     {
4602       tree gnu_scalar_type = gnu_type;
4603
4604       /* If this is a padded type, we need to use the underlying type.  */
4605       if (TREE_CODE (gnu_scalar_type) == RECORD_TYPE
4606           && TYPE_IS_PADDING_P (gnu_scalar_type))
4607         gnu_scalar_type = TREE_TYPE (TYPE_FIELDS (gnu_scalar_type));
4608
4609       /* If this is a floating point type and we haven't set a floating
4610          point type yet, use this in the evaluation of the bounds.  */
4611       if (!longest_float_type_node && kind == E_Floating_Point_Type)
4612         longest_float_type_node = gnu_type;
4613
4614       TYPE_MIN_VALUE (gnu_scalar_type)
4615         = gnat_to_gnu (Type_Low_Bound (gnat_entity));
4616       TYPE_MAX_VALUE (gnu_scalar_type)
4617         = gnat_to_gnu (Type_High_Bound (gnat_entity));
4618
4619       if (TREE_CODE (gnu_scalar_type) == ENUMERAL_TYPE)
4620         {
4621           /* Since this has both a typedef and a tag, avoid outputting
4622              the name twice.  */
4623           DECL_ARTIFICIAL (gnu_decl) = 1;
4624           rest_of_type_decl_compilation (gnu_decl);
4625         }
4626     }
4627
4628   /* If we deferred processing of incomplete types, re-enable it.  If there
4629      were no other disables and we have some to process, do so.  */
4630   if (this_deferred && --defer_incomplete_level == 0)
4631     {
4632       if (defer_incomplete_list)
4633         {
4634           struct incomplete *incp, *next;
4635
4636           /* We are back to level 0 for the deferring of incomplete types.
4637              But processing these incomplete types below may itself require
4638              deferring, so preserve what we have and restart from scratch.  */
4639           incp = defer_incomplete_list;
4640           defer_incomplete_list = NULL;
4641
4642           /* For finalization, however, all types must be complete so we
4643              cannot do the same because deferred incomplete types may end up
4644              referencing each other.  Process them all recursively first.  */
4645           defer_finalize_level++;
4646
4647           for (; incp; incp = next)
4648             {
4649               next = incp->next;
4650
4651               if (incp->old_type)
4652                 update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4653                                    gnat_to_gnu_type (incp->full_type));
4654               free (incp);
4655             }
4656
4657           defer_finalize_level--;
4658         }
4659
4660       /* All the deferred incomplete types have been processed so we can
4661          now proceed with the finalization of the deferred types.  */
4662       if (defer_finalize_level == 0 && defer_finalize_list)
4663         {
4664           unsigned int i;
4665           tree t;
4666
4667           for (i = 0; VEC_iterate (tree, defer_finalize_list, i, t); i++)
4668             rest_of_type_decl_compilation_no_defer (t);
4669
4670           VEC_free (tree, heap, defer_finalize_list);
4671         }
4672     }
4673
4674   /* If we are not defining this type, see if it's in the incomplete list.
4675      If so, handle that list entry now.  */
4676   else if (!definition)
4677     {
4678       struct incomplete *incp;
4679
4680       for (incp = defer_incomplete_list; incp; incp = incp->next)
4681         if (incp->old_type && incp->full_type == gnat_entity)
4682           {
4683             update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4684                                TREE_TYPE (gnu_decl));
4685             incp->old_type = NULL_TREE;
4686           }
4687     }
4688
4689   if (this_global)
4690     force_global--;
4691
4692   if (Is_Packed_Array_Type (gnat_entity)
4693       && Is_Itype (Associated_Node_For_Itype (gnat_entity))
4694       && No (Freeze_Node (Associated_Node_For_Itype (gnat_entity)))
4695       && !present_gnu_tree (Associated_Node_For_Itype (gnat_entity)))
4696     gnat_to_gnu_entity (Associated_Node_For_Itype (gnat_entity), NULL_TREE, 0);
4697
4698   return gnu_decl;
4699 }
4700
4701 /* Similar, but if the returned value is a COMPONENT_REF, return the
4702    FIELD_DECL.  */
4703
4704 tree
4705 gnat_to_gnu_field_decl (Entity_Id gnat_entity)
4706 {
4707   tree gnu_field = gnat_to_gnu_entity (gnat_entity, NULL_TREE, 0);
4708
4709   if (TREE_CODE (gnu_field) == COMPONENT_REF)
4710     gnu_field = TREE_OPERAND (gnu_field, 1);
4711
4712   return gnu_field;
4713 }
4714
4715 /* Wrap up compilation of DECL, a TYPE_DECL, possibly deferring it.
4716    Every TYPE_DECL generated for a type definition must be passed
4717    to this function once everything else has been done for it.  */
4718
4719 void
4720 rest_of_type_decl_compilation (tree decl)
4721 {
4722   /* We need to defer finalizing the type if incomplete types
4723      are being deferred or if they are being processed.  */
4724   if (defer_incomplete_level || defer_finalize_level)
4725     VEC_safe_push (tree, heap, defer_finalize_list, decl);
4726   else
4727     rest_of_type_decl_compilation_no_defer (decl);
4728 }
4729
4730 /* Same as above but without deferring the compilation.  This
4731    function should not be invoked directly on a TYPE_DECL.  */
4732
4733 static void
4734 rest_of_type_decl_compilation_no_defer (tree decl)
4735 {
4736   const int toplev = global_bindings_p ();
4737   tree t = TREE_TYPE (decl);
4738
4739   rest_of_decl_compilation (decl, toplev, 0);
4740
4741   /* Now process all the variants.  This is needed for STABS.  */
4742   for (t = TYPE_MAIN_VARIANT (t); t; t = TYPE_NEXT_VARIANT (t))
4743     {
4744       if (t == TREE_TYPE (decl))
4745         continue;
4746
4747       if (!TYPE_STUB_DECL (t))
4748         {
4749           TYPE_STUB_DECL (t) = build_decl (TYPE_DECL, DECL_NAME (decl), t);
4750           DECL_ARTIFICIAL (TYPE_STUB_DECL (t)) = 1;
4751         }
4752
4753       rest_of_type_compilation (t, toplev);
4754     }
4755 }
4756
4757 /* Finalize any From_With_Type incomplete types.  We do this after processing
4758    our compilation unit and after processing its spec, if this is a body.  */
4759
4760 void
4761 finalize_from_with_types (void)
4762 {
4763   struct incomplete *incp = defer_limited_with;
4764   struct incomplete *next;
4765
4766   defer_limited_with = 0;
4767   for (; incp; incp = next)
4768     {
4769       next = incp->next;
4770
4771       if (incp->old_type != 0)
4772         update_pointer_to (TYPE_MAIN_VARIANT (incp->old_type),
4773                            gnat_to_gnu_type (incp->full_type));
4774       free (incp);
4775     }
4776 }
4777
4778 /* Return the equivalent type to be used for GNAT_ENTITY, if it's a
4779    kind of type (such E_Task_Type) that has a different type which Gigi
4780    uses for its representation.  If the type does not have a special type
4781    for its representation, return GNAT_ENTITY.  If a type is supposed to
4782    exist, but does not, abort unless annotating types, in which case
4783    return Empty.  If GNAT_ENTITY is Empty, return Empty.  */
4784
4785 Entity_Id
4786 Gigi_Equivalent_Type (Entity_Id gnat_entity)
4787 {
4788   Entity_Id gnat_equiv = gnat_entity;
4789
4790   if (No (gnat_entity))
4791     return gnat_entity;
4792
4793   switch (Ekind (gnat_entity))
4794     {
4795     case E_Class_Wide_Subtype:
4796       if (Present (Equivalent_Type (gnat_entity)))
4797         gnat_equiv = Equivalent_Type (gnat_entity);
4798       break;
4799
4800     case E_Access_Protected_Subprogram_Type:
4801     case E_Anonymous_Access_Protected_Subprogram_Type:
4802       gnat_equiv = Equivalent_Type (gnat_entity);
4803       break;
4804
4805     case E_Class_Wide_Type:
4806       gnat_equiv = ((Present (Equivalent_Type (gnat_entity)))
4807                     ? Equivalent_Type (gnat_entity)
4808                     : Root_Type (gnat_entity));
4809       break;
4810
4811     case E_Task_Type:
4812     case E_Task_Subtype:
4813     case E_Protected_Type:
4814     case E_Protected_Subtype:
4815       gnat_equiv = Corresponding_Record_Type (gnat_entity);
4816       break;
4817
4818     default:
4819       break;
4820     }
4821
4822   gcc_assert (Present (gnat_equiv) || type_annotate_only);
4823   return gnat_equiv;
4824 }
4825
4826 /* Return a GCC tree for a parameter corresponding to GNAT_PARAM and
4827    using MECH as its passing mechanism, to be placed in the parameter
4828    list built for GNAT_SUBPROG.  Assume a foreign convention for the
4829    latter if FOREIGN is true.  Also set CICO to true if the parameter
4830    must use the copy-in copy-out implementation mechanism.
4831
4832    The returned tree is a PARM_DECL, except for those cases where no
4833    parameter needs to be actually passed to the subprogram; the type
4834    of this "shadow" parameter is then returned instead.  */
4835
4836 static tree
4837 gnat_to_gnu_param (Entity_Id gnat_param, Mechanism_Type mech,
4838                    Entity_Id gnat_subprog, bool foreign, bool *cico)
4839 {
4840   tree gnu_param_name = get_entity_name (gnat_param);
4841   tree gnu_param_type = gnat_to_gnu_type (Etype (gnat_param));
4842   tree gnu_param_type_alt = NULL_TREE;
4843   bool in_param = (Ekind (gnat_param) == E_In_Parameter);
4844   /* The parameter can be indirectly modified if its address is taken.  */
4845   bool ro_param = in_param && !Address_Taken (gnat_param);
4846   bool by_return = false, by_component_ptr = false, by_ref = false;
4847   tree gnu_param;
4848
4849   /* Copy-return is used only for the first parameter of a valued procedure.
4850      It's a copy mechanism for which a parameter is never allocated.  */
4851   if (mech == By_Copy_Return)
4852     {
4853       gcc_assert (Ekind (gnat_param) == E_Out_Parameter);
4854       mech = By_Copy;
4855       by_return = true;
4856     }
4857
4858   /* If this is either a foreign function or if the underlying type won't
4859      be passed by reference, strip off possible padding type.  */
4860   if (TREE_CODE (gnu_param_type) == RECORD_TYPE
4861       && TYPE_IS_PADDING_P (gnu_param_type))
4862     {
4863       tree unpadded_type = TREE_TYPE (TYPE_FIELDS (gnu_param_type));
4864
4865       if (mech == By_Reference
4866           || foreign
4867           || (!must_pass_by_ref (unpadded_type)
4868               && (mech == By_Copy || !default_pass_by_ref (unpadded_type))))
4869         gnu_param_type = unpadded_type;
4870     }
4871
4872   /* If this is a read-only parameter, make a variant of the type that is
4873      read-only.  ??? However, if this is an unconstrained array, that type
4874      can be very complex, so skip it for now.  Likewise for any other
4875      self-referential type.  */
4876   if (ro_param
4877       && TREE_CODE (gnu_param_type) != UNCONSTRAINED_ARRAY_TYPE
4878       && !CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_param_type)))
4879     gnu_param_type = build_qualified_type (gnu_param_type,
4880                                            (TYPE_QUALS (gnu_param_type)
4881                                             | TYPE_QUAL_CONST));
4882
4883   /* For foreign conventions, pass arrays as pointers to the element type.
4884      First check for unconstrained array and get the underlying array.  */
4885   if (foreign && TREE_CODE (gnu_param_type) == UNCONSTRAINED_ARRAY_TYPE)
4886     gnu_param_type
4887       = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_param_type))));
4888
4889   /* VMS descriptors are themselves passed by reference.  */
4890   if (mech == By_Short_Descriptor ||
4891       (mech == By_Descriptor && TARGET_ABI_OPEN_VMS && !TARGET_MALLOC64))
4892     gnu_param_type
4893       = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
4894                                                     Mechanism (gnat_param),
4895                                                     gnat_subprog));
4896   else if (mech == By_Descriptor)
4897     {
4898       /* Build both a 32-bit and 64-bit descriptor, one of which will be
4899          chosen in fill_vms_descriptor.  */
4900       gnu_param_type_alt
4901         = build_pointer_type (build_vms_descriptor32 (gnu_param_type,
4902                                                       Mechanism (gnat_param),
4903                                                       gnat_subprog));
4904       gnu_param_type
4905         = build_pointer_type (build_vms_descriptor (gnu_param_type,
4906                                                     Mechanism (gnat_param),
4907                                                     gnat_subprog));
4908     }
4909
4910   /* Arrays are passed as pointers to element type for foreign conventions.  */
4911   else if (foreign
4912            && mech != By_Copy
4913            && TREE_CODE (gnu_param_type) == ARRAY_TYPE)
4914     {
4915       /* Strip off any multi-dimensional entries, then strip
4916          off the last array to get the component type.  */
4917       while (TREE_CODE (TREE_TYPE (gnu_param_type)) == ARRAY_TYPE
4918              && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_param_type)))
4919         gnu_param_type = TREE_TYPE (gnu_param_type);
4920
4921       by_component_ptr = true;
4922       gnu_param_type = TREE_TYPE (gnu_param_type);
4923
4924       if (ro_param)
4925         gnu_param_type = build_qualified_type (gnu_param_type,
4926                                                (TYPE_QUALS (gnu_param_type)
4927                                                 | TYPE_QUAL_CONST));
4928
4929       gnu_param_type = build_pointer_type (gnu_param_type);
4930     }
4931
4932   /* Fat pointers are passed as thin pointers for foreign conventions.  */
4933   else if (foreign && TYPE_FAT_POINTER_P (gnu_param_type))
4934     gnu_param_type
4935       = make_type_from_size (gnu_param_type, size_int (POINTER_SIZE), 0);
4936
4937   /* If we must pass or were requested to pass by reference, do so.
4938      If we were requested to pass by copy, do so.
4939      Otherwise, for foreign conventions, pass In Out or Out parameters
4940      or aggregates by reference.  For COBOL and Fortran, pass all
4941      integer and FP types that way too.  For Convention Ada, use
4942      the standard Ada default.  */
4943   else if (must_pass_by_ref (gnu_param_type)
4944            || mech == By_Reference
4945            || (mech != By_Copy
4946                && ((foreign
4947                     && (!in_param || AGGREGATE_TYPE_P (gnu_param_type)))
4948                    || (foreign
4949                        && (Convention (gnat_subprog) == Convention_Fortran
4950                            || Convention (gnat_subprog) == Convention_COBOL)
4951                        && (INTEGRAL_TYPE_P (gnu_param_type)
4952                            || FLOAT_TYPE_P (gnu_param_type)))
4953                    || (!foreign
4954                        && default_pass_by_ref (gnu_param_type)))))
4955     {
4956       gnu_param_type = build_reference_type (gnu_param_type);
4957       by_ref = true;
4958     }
4959
4960   /* Pass In Out or Out parameters using copy-in copy-out mechanism.  */
4961   else if (!in_param)
4962     *cico = true;
4963
4964   if (mech == By_Copy && (by_ref || by_component_ptr))
4965     post_error ("?cannot pass & by copy", gnat_param);
4966
4967   /* If this is an Out parameter that isn't passed by reference and isn't
4968      a pointer or aggregate, we don't make a PARM_DECL for it.  Instead,
4969      it will be a VAR_DECL created when we process the procedure, so just
4970      return its type.  For the special parameter of a valued procedure,
4971      never pass it in.
4972
4973      An exception is made to cover the RM-6.4.1 rule requiring "by copy"
4974      Out parameters with discriminants or implicit initial values to be
4975      handled like In Out parameters.  These type are normally built as
4976      aggregates, hence passed by reference, except for some packed arrays
4977      which end up encoded in special integer types.
4978
4979      The exception we need to make is then for packed arrays of records
4980      with discriminants or implicit initial values.  We have no light/easy
4981      way to check for the latter case, so we merely check for packed arrays
4982      of records.  This may lead to useless copy-in operations, but in very
4983      rare cases only, as these would be exceptions in a set of already
4984      exceptional situations.  */
4985   if (Ekind (gnat_param) == E_Out_Parameter
4986       && !by_ref
4987       && (by_return
4988           || (mech != By_Descriptor
4989               && mech != By_Short_Descriptor
4990               && !POINTER_TYPE_P (gnu_param_type)
4991               && !AGGREGATE_TYPE_P (gnu_param_type)))
4992       && !(Is_Array_Type (Etype (gnat_param))
4993            && Is_Packed (Etype (gnat_param))
4994            && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
4995     return gnu_param_type;
4996
4997   gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
4998                                  ro_param || by_ref || by_component_ptr);
4999   DECL_BY_REF_P (gnu_param) = by_ref;
5000   DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5001   DECL_BY_DESCRIPTOR_P (gnu_param) = (mech == By_Descriptor ||
5002                                       mech == By_Short_Descriptor);
5003   DECL_POINTS_TO_READONLY_P (gnu_param)
5004     = (ro_param && (by_ref || by_component_ptr));
5005
5006   /* Save the alternate descriptor type, if any.  */
5007   if (gnu_param_type_alt)
5008     SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5009
5010   /* If no Mechanism was specified, indicate what we're using, then
5011      back-annotate it.  */
5012   if (mech == Default)
5013     mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5014
5015   Set_Mechanism (gnat_param, mech);
5016   return gnu_param;
5017 }
5018
5019 /* Return true if DISCR1 and DISCR2 represent the same discriminant.  */
5020
5021 static bool
5022 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5023 {
5024   while (Present (Corresponding_Discriminant (discr1)))
5025     discr1 = Corresponding_Discriminant (discr1);
5026
5027   while (Present (Corresponding_Discriminant (discr2)))
5028     discr2 = Corresponding_Discriminant (discr2);
5029
5030   return
5031     Original_Record_Component (discr1) == Original_Record_Component (discr2);
5032 }
5033
5034 /* Return true if the array type specified by GNAT_TYPE and GNU_TYPE has
5035    a non-aliased component in the back-end sense.  */
5036
5037 static bool
5038 array_type_has_nonaliased_component (Entity_Id gnat_type, tree gnu_type)
5039 {
5040   /* If the type below this is a multi-array type, then
5041      this does not have aliased components.  */
5042   if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5043       && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5044     return true;
5045
5046   if (Has_Aliased_Components (gnat_type))
5047     return false;
5048
5049   return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5050 }
5051 \f
5052 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5053    be elaborated at the point of its definition, but do nothing else.  */
5054
5055 void
5056 elaborate_entity (Entity_Id gnat_entity)
5057 {
5058   switch (Ekind (gnat_entity))
5059     {
5060     case E_Signed_Integer_Subtype:
5061     case E_Modular_Integer_Subtype:
5062     case E_Enumeration_Subtype:
5063     case E_Ordinary_Fixed_Point_Subtype:
5064     case E_Decimal_Fixed_Point_Subtype:
5065     case E_Floating_Point_Subtype:
5066       {
5067         Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5068         Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5069
5070         /* ??? Tests for avoiding static constraint error expression
5071            is needed until the front stops generating bogus conversions
5072            on bounds of real types.  */
5073
5074         if (!Raises_Constraint_Error (gnat_lb))
5075           elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5076                                 1, 0, Needs_Debug_Info (gnat_entity));
5077         if (!Raises_Constraint_Error (gnat_hb))
5078           elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5079                                 1, 0, Needs_Debug_Info (gnat_entity));
5080       break;
5081       }
5082
5083     case E_Record_Type:
5084       {
5085         Node_Id full_definition = Declaration_Node (gnat_entity);
5086         Node_Id record_definition = Type_Definition (full_definition);
5087
5088         /* If this is a record extension, go a level further to find the
5089            record definition.  */
5090         if (Nkind (record_definition) == N_Derived_Type_Definition)
5091           record_definition = Record_Extension_Part (record_definition);
5092       }
5093       break;
5094
5095     case E_Record_Subtype:
5096     case E_Private_Subtype:
5097     case E_Limited_Private_Subtype:
5098     case E_Record_Subtype_With_Private:
5099       if (Is_Constrained (gnat_entity)
5100           && Has_Discriminants (Base_Type (gnat_entity))
5101           && Present (Discriminant_Constraint (gnat_entity)))
5102         {
5103           Node_Id gnat_discriminant_expr;
5104           Entity_Id gnat_field;
5105
5106           for (gnat_field = First_Discriminant (Base_Type (gnat_entity)),
5107                gnat_discriminant_expr
5108                = First_Elmt (Discriminant_Constraint (gnat_entity));
5109                Present (gnat_field);
5110                gnat_field = Next_Discriminant (gnat_field),
5111                gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5112             /* ??? For now, ignore access discriminants.  */
5113             if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5114               elaborate_expression (Node (gnat_discriminant_expr),
5115                                     gnat_entity,
5116                                     get_entity_name (gnat_field), 1, 0, 0);
5117         }
5118       break;
5119
5120     }
5121 }
5122 \f
5123 /* Mark GNAT_ENTITY as going out of scope at this point.  Recursively mark
5124    any entities on its entity chain similarly.  */
5125
5126 void
5127 mark_out_of_scope (Entity_Id gnat_entity)
5128 {
5129   Entity_Id gnat_sub_entity;
5130   unsigned int kind = Ekind (gnat_entity);
5131
5132   /* If this has an entity list, process all in the list.  */
5133   if (IN (kind, Class_Wide_Kind) || IN (kind, Concurrent_Kind)
5134       || IN (kind, Private_Kind)
5135       || kind == E_Block || kind == E_Entry || kind == E_Entry_Family
5136       || kind == E_Function || kind == E_Generic_Function
5137       || kind == E_Generic_Package || kind == E_Generic_Procedure
5138       || kind == E_Loop || kind == E_Operator || kind == E_Package
5139       || kind == E_Package_Body || kind == E_Procedure
5140       || kind == E_Record_Type || kind == E_Record_Subtype
5141       || kind == E_Subprogram_Body || kind == E_Subprogram_Type)
5142     for (gnat_sub_entity = First_Entity (gnat_entity);
5143          Present (gnat_sub_entity);
5144          gnat_sub_entity = Next_Entity (gnat_sub_entity))
5145       if (Scope (gnat_sub_entity) == gnat_entity
5146           && gnat_sub_entity != gnat_entity)
5147         mark_out_of_scope (gnat_sub_entity);
5148
5149   /* Now clear this if it has been defined, but only do so if it isn't
5150      a subprogram or parameter.  We could refine this, but it isn't
5151      worth it.  If this is statically allocated, it is supposed to
5152      hang around out of cope.  */
5153   if (present_gnu_tree (gnat_entity) && !Is_Statically_Allocated (gnat_entity)
5154       && kind != E_Procedure && kind != E_Function && !IN (kind, Formal_Kind))
5155     {
5156       save_gnu_tree (gnat_entity, NULL_TREE, true);
5157       save_gnu_tree (gnat_entity, error_mark_node, true);
5158     }
5159 }
5160 \f
5161 /* Set the alias set of GNU_NEW_TYPE to be that of GNU_OLD_TYPE.  If this
5162    is a multi-dimensional array type, do this recursively.  */
5163
5164 static void
5165 copy_alias_set (tree gnu_new_type, tree gnu_old_type)
5166 {
5167   /* Remove any padding from GNU_OLD_TYPE.  It doesn't matter in the case
5168      of a one-dimensional array, since the padding has the same alias set
5169      as the field type, but if it's a multi-dimensional array, we need to
5170      see the inner types.  */
5171   while (TREE_CODE (gnu_old_type) == RECORD_TYPE
5172          && (TYPE_JUSTIFIED_MODULAR_P (gnu_old_type)
5173              || TYPE_IS_PADDING_P (gnu_old_type)))
5174     gnu_old_type = TREE_TYPE (TYPE_FIELDS (gnu_old_type));
5175
5176   /* We need to be careful here in case GNU_OLD_TYPE is an unconstrained
5177      array.  In that case, it doesn't have the same shape as GNU_NEW_TYPE,
5178      so we need to go down to what does.  */
5179   if (TREE_CODE (gnu_old_type) == UNCONSTRAINED_ARRAY_TYPE)
5180     gnu_old_type
5181       = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_old_type))));
5182
5183   if (TREE_CODE (gnu_new_type) == ARRAY_TYPE
5184       && TREE_CODE (TREE_TYPE (gnu_new_type)) == ARRAY_TYPE
5185       && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_new_type)))
5186     copy_alias_set (TREE_TYPE (gnu_new_type), TREE_TYPE (gnu_old_type));
5187
5188   /* The alias set shouldn't be copied between array types with different
5189      aliasing settings because this can break the aliasing relationship
5190      between the array type and its element type.  */
5191 #ifndef ENABLE_CHECKING
5192   if (flag_strict_aliasing)
5193 #endif
5194     gcc_assert (!(TREE_CODE (gnu_new_type) == ARRAY_TYPE
5195                   && TREE_CODE (gnu_old_type) == ARRAY_TYPE
5196                   && TYPE_NONALIASED_COMPONENT (gnu_new_type)
5197                      != TYPE_NONALIASED_COMPONENT (gnu_old_type)));
5198
5199   TYPE_ALIAS_SET (gnu_new_type) = get_alias_set (gnu_old_type);
5200   record_component_aliases (gnu_new_type);
5201 }
5202 \f
5203 /* Return a TREE_LIST describing the substitutions needed to reflect
5204    discriminant substitutions from GNAT_SUBTYPE to GNAT_TYPE and add
5205    them to GNU_LIST.  If GNAT_TYPE is not specified, use the base type
5206    of GNAT_SUBTYPE.  The substitutions can be in any order.  TREE_PURPOSE
5207    gives the tree for the discriminant and TREE_VALUES is the replacement
5208    value.  They are in the form of operands to substitute_in_expr.
5209    DEFINITION is as in gnat_to_gnu_entity.  */
5210
5211 static tree
5212 substitution_list (Entity_Id gnat_subtype, Entity_Id gnat_type,
5213                    tree gnu_list, bool definition)
5214 {
5215   Entity_Id gnat_discrim;
5216   Node_Id gnat_value;
5217
5218   if (No (gnat_type))
5219     gnat_type = Implementation_Base_Type (gnat_subtype);
5220
5221   if (Has_Discriminants (gnat_type))
5222     for (gnat_discrim = First_Stored_Discriminant (gnat_type),
5223          gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
5224          Present (gnat_discrim);
5225          gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
5226          gnat_value = Next_Elmt (gnat_value))
5227       /* Ignore access discriminants.  */
5228       if (!Is_Access_Type (Etype (Node (gnat_value))))
5229         gnu_list = tree_cons (gnat_to_gnu_field_decl (gnat_discrim),
5230                               elaborate_expression
5231                               (Node (gnat_value), gnat_subtype,
5232                                get_entity_name (gnat_discrim), definition,
5233                                1, 0),
5234                               gnu_list);
5235
5236   return gnu_list;
5237 }
5238 \f
5239 /* Return true if the size represented by GNU_SIZE can be handled by an
5240    allocation.  If STATIC_P is true, consider only what can be done with a
5241    static allocation.  */
5242
5243 static bool
5244 allocatable_size_p (tree gnu_size, bool static_p)
5245 {
5246   HOST_WIDE_INT our_size;
5247
5248   /* If this is not a static allocation, the only case we want to forbid
5249      is an overflowing size.  That will be converted into a raise a
5250      Storage_Error.  */
5251   if (!static_p)
5252     return !(TREE_CODE (gnu_size) == INTEGER_CST
5253              && TREE_OVERFLOW (gnu_size));
5254
5255   /* Otherwise, we need to deal with both variable sizes and constant
5256      sizes that won't fit in a host int.  We use int instead of HOST_WIDE_INT
5257      since assemblers may not like very large sizes.  */
5258   if (!host_integerp (gnu_size, 1))
5259     return false;
5260
5261   our_size = tree_low_cst (gnu_size, 1);
5262   return (int) our_size == our_size;
5263 }
5264 \f
5265 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5266    NAME, ARGS and ERROR_POINT.  */
5267
5268 static void
5269 prepend_one_attribute_to (struct attrib ** attr_list,
5270                           enum attr_type attr_type,
5271                           tree attr_name,
5272                           tree attr_args,
5273                           Node_Id attr_error_point)
5274 {
5275   struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5276
5277   attr->type = attr_type;
5278   attr->name = attr_name;
5279   attr->args = attr_args;
5280   attr->error_point = attr_error_point;
5281
5282   attr->next = *attr_list;
5283   *attr_list = attr;
5284 }
5285
5286 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any.  */
5287
5288 static void
5289 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
5290 {
5291   Node_Id gnat_temp;
5292
5293   for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
5294        gnat_temp = Next_Rep_Item (gnat_temp))
5295     if (Nkind (gnat_temp) == N_Pragma)
5296       {
5297         tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
5298         Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
5299         enum attr_type etype;
5300
5301         if (Present (gnat_assoc) && Present (First (gnat_assoc))
5302             && Present (Next (First (gnat_assoc)))
5303             && (Nkind (Expression (Next (First (gnat_assoc))))
5304                 == N_String_Literal))
5305           {
5306             gnu_arg0 = get_identifier (TREE_STRING_POINTER
5307                                        (gnat_to_gnu
5308                                         (Expression (Next
5309                                                      (First (gnat_assoc))))));
5310             if (Present (Next (Next (First (gnat_assoc))))
5311                 && (Nkind (Expression (Next (Next (First (gnat_assoc)))))
5312                     == N_String_Literal))
5313               gnu_arg1 = get_identifier (TREE_STRING_POINTER
5314                                          (gnat_to_gnu
5315                                           (Expression
5316                                            (Next (Next
5317                                                   (First (gnat_assoc)))))));
5318           }
5319
5320         switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
5321           {
5322           case Pragma_Machine_Attribute:
5323             etype = ATTR_MACHINE_ATTRIBUTE;
5324             break;
5325
5326           case Pragma_Linker_Alias:
5327             etype = ATTR_LINK_ALIAS;
5328             break;
5329
5330           case Pragma_Linker_Section:
5331             etype = ATTR_LINK_SECTION;
5332             break;
5333
5334           case Pragma_Linker_Constructor:
5335             etype = ATTR_LINK_CONSTRUCTOR;
5336             break;
5337
5338           case Pragma_Linker_Destructor:
5339             etype = ATTR_LINK_DESTRUCTOR;
5340             break;
5341
5342           case Pragma_Weak_External:
5343             etype = ATTR_WEAK_EXTERNAL;
5344             break;
5345
5346           default:
5347             continue;
5348           }
5349
5350
5351         /* Prepend to the list now.  Make a list of the argument we might
5352            have, as GCC expects it.  */
5353         prepend_one_attribute_to
5354           (attr_list,
5355            etype, gnu_arg0,
5356            (gnu_arg1 != NULL_TREE)
5357            ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
5358            Present (Next (First (gnat_assoc)))
5359            ? Expression (Next (First (gnat_assoc))) : gnat_temp);
5360       }
5361 }
5362 \f
5363 /* Get the unpadded version of a GNAT type.  */
5364
5365 tree
5366 get_unpadded_type (Entity_Id gnat_entity)
5367 {
5368   tree type = gnat_to_gnu_type (gnat_entity);
5369
5370   if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5371     type = TREE_TYPE (TYPE_FIELDS (type));
5372
5373   return type;
5374 }
5375 \f
5376 /* Called when we need to protect a variable object using a save_expr.  */
5377
5378 tree
5379 maybe_variable (tree gnu_operand)
5380 {
5381   if (TREE_CONSTANT (gnu_operand) || TREE_READONLY (gnu_operand)
5382       || TREE_CODE (gnu_operand) == SAVE_EXPR
5383       || TREE_CODE (gnu_operand) == NULL_EXPR)
5384     return gnu_operand;
5385
5386   if (TREE_CODE (gnu_operand) == UNCONSTRAINED_ARRAY_REF)
5387     {
5388       tree gnu_result = build1 (UNCONSTRAINED_ARRAY_REF,
5389                                 TREE_TYPE (gnu_operand),
5390                                 variable_size (TREE_OPERAND (gnu_operand, 0)));
5391
5392       TREE_READONLY (gnu_result) = TREE_STATIC (gnu_result)
5393         = TYPE_READONLY (TREE_TYPE (TREE_TYPE (gnu_operand)));
5394       return gnu_result;
5395     }
5396   else
5397     return variable_size (gnu_operand);
5398 }
5399 \f
5400 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
5401    type definition (either a bound or a discriminant value) for GNAT_ENTITY,
5402    return the GCC tree to use for that expression.  GNU_NAME is the
5403    qualification to use if an external name is appropriate and DEFINITION is
5404    nonzero if this is a definition of GNAT_ENTITY.  If NEED_VALUE is nonzero,
5405    we need a result.  Otherwise, we are just elaborating this for
5406    side-effects.  If NEED_DEBUG is nonzero we need the symbol for debugging
5407    purposes even if it isn't needed for code generation.  */
5408
5409 static tree
5410 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity,
5411                       tree gnu_name, bool definition, bool need_value,
5412                       bool need_debug)
5413 {
5414   tree gnu_expr;
5415
5416   /* If we already elaborated this expression (e.g., it was involved
5417      in the definition of a private type), use the old value.  */
5418   if (present_gnu_tree (gnat_expr))
5419     return get_gnu_tree (gnat_expr);
5420
5421   /* If we don't need a value and this is static or a discriminant, we
5422      don't need to do anything.  */
5423   else if (!need_value
5424            && (Is_OK_Static_Expression (gnat_expr)
5425                || (Nkind (gnat_expr) == N_Identifier
5426                    && Ekind (Entity (gnat_expr)) == E_Discriminant)))
5427     return 0;
5428
5429   /* Otherwise, convert this tree to its GCC equivalent.  */
5430   gnu_expr
5431     = elaborate_expression_1 (gnat_expr, gnat_entity, gnat_to_gnu (gnat_expr),
5432                               gnu_name, definition, need_debug);
5433
5434   /* Save the expression in case we try to elaborate this entity again.  Since
5435      it's not a DECL, don't check it.  Don't save if it's a discriminant.  */
5436   if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
5437     save_gnu_tree (gnat_expr, gnu_expr, true);
5438
5439   return need_value ? gnu_expr : error_mark_node;
5440 }
5441
5442 /* Similar, but take a GNU expression.  */
5443
5444 static tree
5445 elaborate_expression_1 (Node_Id gnat_expr, Entity_Id gnat_entity,
5446                         tree gnu_expr, tree gnu_name, bool definition,
5447                         bool need_debug)
5448 {
5449   tree gnu_decl = NULL_TREE;
5450   /* Skip any conversions and simple arithmetics to see if the expression
5451      is a read-only variable.
5452      ??? This really should remain read-only, but we have to think about
5453      the typing of the tree here.  */
5454   tree gnu_inner_expr
5455     = skip_simple_arithmetic (remove_conversions (gnu_expr, true));
5456   bool expr_global = Is_Public (gnat_entity) || global_bindings_p ();
5457   bool expr_variable;
5458
5459   /* In most cases, we won't see a naked FIELD_DECL here because a
5460      discriminant reference will have been replaced with a COMPONENT_REF
5461      when the type is being elaborated.  However, there are some cases
5462      involving child types where we will.  So convert it to a COMPONENT_REF
5463      here.  We have to hope it will be at the highest level of the
5464      expression in these cases.  */
5465   if (TREE_CODE (gnu_expr) == FIELD_DECL)
5466     gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
5467                        build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
5468                        gnu_expr, NULL_TREE);
5469
5470   /* If GNU_EXPR is neither a placeholder nor a constant, nor a variable
5471      that is read-only, make a variable that is initialized to contain the
5472      bound when the package containing the definition is elaborated.  If
5473      this entity is defined at top level and a bound or discriminant value
5474      isn't a constant or a reference to a discriminant, replace the bound
5475      by the variable; otherwise use a SAVE_EXPR if needed.  Note that we
5476      rely here on the fact that an expression cannot contain both the
5477      discriminant and some other variable.  */
5478
5479   expr_variable = (!CONSTANT_CLASS_P (gnu_expr)
5480                    && !(TREE_CODE (gnu_inner_expr) == VAR_DECL
5481                         && (TREE_READONLY (gnu_inner_expr)
5482                             || DECL_READONLY_ONCE_ELAB (gnu_inner_expr)))
5483                    && !CONTAINS_PLACEHOLDER_P (gnu_expr));
5484
5485   /* If this is a static expression or contains a discriminant, we don't
5486      need the variable for debugging (and can't elaborate anyway if a
5487      discriminant).  */
5488   if (need_debug
5489       && (Is_OK_Static_Expression (gnat_expr)
5490           || CONTAINS_PLACEHOLDER_P (gnu_expr)))
5491     need_debug = false;
5492
5493   /* Now create the variable if we need it.  */
5494   if (need_debug || (expr_variable && expr_global))
5495     gnu_decl
5496       = create_var_decl (create_concat_name (gnat_entity,
5497                                              IDENTIFIER_POINTER (gnu_name)),
5498                          NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr,
5499                          !need_debug, Is_Public (gnat_entity),
5500                          !definition, false, NULL, gnat_entity);
5501
5502   /* We only need to use this variable if we are in global context since GCC
5503      can do the right thing in the local case.  */
5504   if (expr_global && expr_variable)
5505     return gnu_decl;
5506   else if (!expr_variable)
5507     return gnu_expr;
5508   else
5509     return maybe_variable (gnu_expr);
5510 }
5511 \f
5512 /* Create a record type that contains a SIZE bytes long field of TYPE with a
5513    starting bit position so that it is aligned to ALIGN bits, and leaving at
5514    least ROOM bytes free before the field.  BASE_ALIGN is the alignment the
5515    record is guaranteed to get.  */
5516
5517 tree
5518 make_aligning_type (tree type, unsigned int align, tree size,
5519                     unsigned int base_align, int room)
5520 {
5521   /* We will be crafting a record type with one field at a position set to be
5522      the next multiple of ALIGN past record'address + room bytes.  We use a
5523      record placeholder to express record'address.  */
5524
5525   tree record_type = make_node (RECORD_TYPE);
5526   tree record = build0 (PLACEHOLDER_EXPR, record_type);
5527
5528   tree record_addr_st
5529     = convert (sizetype, build_unary_op (ADDR_EXPR, NULL_TREE, record));
5530
5531   /* The diagram below summarizes the shape of what we manipulate:
5532
5533                     <--------- pos ---------->
5534                 {  +------------+-------------+-----------------+
5535       record  =>{  |############|     ...     | field (type)    |
5536                 {  +------------+-------------+-----------------+
5537                    |<-- room -->|<- voffset ->|<---- size ----->|
5538                    o            o
5539                    |            |
5540                    record_addr  vblock_addr
5541
5542      Every length is in sizetype bytes there, except "pos" which has to be
5543      set as a bit position in the GCC tree for the record.  */
5544
5545   tree room_st = size_int (room);
5546   tree vblock_addr_st = size_binop (PLUS_EXPR, record_addr_st, room_st);
5547   tree voffset_st, pos, field;
5548
5549   tree name = TYPE_NAME (type);
5550
5551   if (TREE_CODE (name) == TYPE_DECL)
5552     name = DECL_NAME (name);
5553
5554   TYPE_NAME (record_type) = concat_id_with_name (name, "_ALIGN");
5555
5556   /* Compute VOFFSET and then POS.  The next byte position multiple of some
5557      alignment after some address is obtained by "and"ing the alignment minus
5558      1 with the two's complement of the address.   */
5559
5560   voffset_st = size_binop (BIT_AND_EXPR,
5561                            size_diffop (size_zero_node, vblock_addr_st),
5562                            ssize_int ((align / BITS_PER_UNIT) - 1));
5563
5564   /* POS = (ROOM + VOFFSET) * BIT_PER_UNIT, in bitsizetype.  */
5565
5566   pos = size_binop (MULT_EXPR,
5567                     convert (bitsizetype,
5568                              size_binop (PLUS_EXPR, room_st, voffset_st)),
5569                     bitsize_unit_node);
5570
5571   /* Craft the GCC record representation.  We exceptionally do everything
5572      manually here because 1) our generic circuitry is not quite ready to
5573      handle the complex position/size expressions we are setting up, 2) we
5574      have a strong simplifying factor at hand: we know the maximum possible
5575      value of voffset, and 3) we have to set/reset at least the sizes in
5576      accordance with this maximum value anyway, as we need them to convey
5577      what should be "alloc"ated for this type.
5578
5579      Use -1 as the 'addressable' indication for the field to prevent the
5580      creation of a bitfield.  We don't need one, it would have damaging
5581      consequences on the alignment computation, and create_field_decl would
5582      make one without this special argument, for instance because of the
5583      complex position expression.  */
5584
5585   field = create_field_decl (get_identifier ("F"), type, record_type,
5586                              1, size, pos, -1);
5587   TYPE_FIELDS (record_type) = field;
5588
5589   TYPE_ALIGN (record_type) = base_align;
5590   TYPE_USER_ALIGN (record_type) = 1;
5591
5592   TYPE_SIZE (record_type)
5593     = size_binop (PLUS_EXPR,
5594                   size_binop (MULT_EXPR, convert (bitsizetype, size),
5595                               bitsize_unit_node),
5596                   bitsize_int (align + room * BITS_PER_UNIT));
5597   TYPE_SIZE_UNIT (record_type)
5598     = size_binop (PLUS_EXPR, size,
5599                   size_int (room + align / BITS_PER_UNIT));
5600
5601   SET_TYPE_MODE (record_type, BLKmode);
5602
5603   copy_alias_set (record_type, type);
5604   return record_type;
5605 }
5606 \f
5607 /* Return the result of rounding T up to ALIGN.  */
5608
5609 static inline unsigned HOST_WIDE_INT
5610 round_up_to_align (unsigned HOST_WIDE_INT t, unsigned int align)
5611 {
5612   t += align - 1;
5613   t /= align;
5614   t *= align;
5615   return t;
5616 }
5617
5618 /* TYPE is a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE that is being used
5619    as the field type of a packed record if IN_RECORD is true, or as the
5620    component type of a packed array if IN_RECORD is false.  See if we can
5621    rewrite it either as a type that has a non-BLKmode, which we can pack
5622    tighter in the packed record case, or as a smaller type with BLKmode.
5623    If so, return the new type.  If not, return the original type.  */
5624
5625 static tree
5626 make_packable_type (tree type, bool in_record)
5627 {
5628   unsigned HOST_WIDE_INT size = tree_low_cst (TYPE_SIZE (type), 1);
5629   unsigned HOST_WIDE_INT new_size;
5630   tree new_type, old_field, field_list = NULL_TREE;
5631
5632   /* No point in doing anything if the size is zero.  */
5633   if (size == 0)
5634     return type;
5635
5636   new_type = make_node (TREE_CODE (type));
5637
5638   /* Copy the name and flags from the old type to that of the new.
5639      Note that we rely on the pointer equality created here for
5640      TYPE_NAME to look through conversions in various places.  */
5641   TYPE_NAME (new_type) = TYPE_NAME (type);
5642   TYPE_JUSTIFIED_MODULAR_P (new_type) = TYPE_JUSTIFIED_MODULAR_P (type);
5643   TYPE_CONTAINS_TEMPLATE_P (new_type) = TYPE_CONTAINS_TEMPLATE_P (type);
5644   if (TREE_CODE (type) == RECORD_TYPE)
5645     TYPE_IS_PADDING_P (new_type) = TYPE_IS_PADDING_P (type);
5646
5647   /* If we are in a record and have a small size, set the alignment to
5648      try for an integral mode.  Otherwise set it to try for a smaller
5649      type with BLKmode.  */
5650   if (in_record && size <= MAX_FIXED_MODE_SIZE)
5651     {
5652       TYPE_ALIGN (new_type) = ceil_alignment (size);
5653       new_size = round_up_to_align (size, TYPE_ALIGN (new_type));
5654     }
5655   else
5656     {
5657       unsigned HOST_WIDE_INT align;
5658
5659       /* Do not try to shrink the size if the RM size is not constant.  */
5660       if (TYPE_CONTAINS_TEMPLATE_P (type)
5661           || !host_integerp (TYPE_ADA_SIZE (type), 1))
5662         return type;
5663
5664       /* Round the RM size up to a unit boundary to get the minimal size
5665          for a BLKmode record.  Give up if it's already the size.  */
5666       new_size = TREE_INT_CST_LOW (TYPE_ADA_SIZE (type));
5667       new_size = round_up_to_align (new_size, BITS_PER_UNIT);
5668       if (new_size == size)
5669         return type;
5670
5671       align = new_size & -new_size;
5672       TYPE_ALIGN (new_type) = MIN (TYPE_ALIGN (type), align);
5673     }
5674
5675   TYPE_USER_ALIGN (new_type) = 1;
5676
5677   /* Now copy the fields, keeping the position and size as we don't want
5678      to change the layout by propagating the packedness downwards.  */
5679   for (old_field = TYPE_FIELDS (type); old_field;
5680        old_field = TREE_CHAIN (old_field))
5681     {
5682       tree new_field_type = TREE_TYPE (old_field);
5683       tree new_field, new_size;
5684
5685       if (TYPE_MODE (new_field_type) == BLKmode
5686           && (TREE_CODE (new_field_type) == RECORD_TYPE
5687               || TREE_CODE (new_field_type) == UNION_TYPE
5688               || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5689           && host_integerp (TYPE_SIZE (new_field_type), 1))
5690         new_field_type = make_packable_type (new_field_type, true);
5691
5692       /* However, for the last field in a not already packed record type
5693          that is of an aggregate type, we need to use the RM_Size in the
5694          packable version of the record type, see finish_record_type.  */
5695       if (!TREE_CHAIN (old_field)
5696           && !TYPE_PACKED (type)
5697           && (TREE_CODE (new_field_type) == RECORD_TYPE
5698               || TREE_CODE (new_field_type) == UNION_TYPE
5699               || TREE_CODE (new_field_type) == QUAL_UNION_TYPE)
5700           && !TYPE_IS_FAT_POINTER_P (new_field_type)
5701           && !TYPE_CONTAINS_TEMPLATE_P (new_field_type)
5702           && TYPE_ADA_SIZE (new_field_type))
5703         new_size = TYPE_ADA_SIZE (new_field_type);
5704       else
5705         new_size = DECL_SIZE (old_field);
5706
5707       new_field = create_field_decl (DECL_NAME (old_field), new_field_type,
5708                                      new_type, TYPE_PACKED (type), new_size,
5709                                      bit_position (old_field),
5710                                      !DECL_NONADDRESSABLE_P (old_field));
5711
5712       DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
5713       SET_DECL_ORIGINAL_FIELD
5714         (new_field, (DECL_ORIGINAL_FIELD (old_field)
5715                      ? DECL_ORIGINAL_FIELD (old_field) : old_field));
5716
5717       if (TREE_CODE (new_type) == QUAL_UNION_TYPE)
5718         DECL_QUALIFIER (new_field) = DECL_QUALIFIER (old_field);
5719
5720       TREE_CHAIN (new_field) = field_list;
5721       field_list = new_field;
5722     }
5723
5724   finish_record_type (new_type, nreverse (field_list), 2, true);
5725   copy_alias_set (new_type, type);
5726
5727   /* If this is a padding record, we never want to make the size smaller
5728      than what was specified.  For QUAL_UNION_TYPE, also copy the size.  */
5729   if ((TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5730       || TREE_CODE (type) == QUAL_UNION_TYPE)
5731     {
5732       TYPE_SIZE (new_type) = TYPE_SIZE (type);
5733       TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (type);
5734     }
5735   else
5736     {
5737       TYPE_SIZE (new_type) = bitsize_int (new_size);
5738       TYPE_SIZE_UNIT (new_type)
5739         = size_int ((new_size + BITS_PER_UNIT - 1) / BITS_PER_UNIT);
5740     }
5741
5742   if (!TYPE_CONTAINS_TEMPLATE_P (type))
5743     SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (type));
5744
5745   compute_record_mode (new_type);
5746
5747   /* Try harder to get a packable type if necessary, for example
5748      in case the record itself contains a BLKmode field.  */
5749   if (in_record && TYPE_MODE (new_type) == BLKmode)
5750     SET_TYPE_MODE (new_type,
5751                    mode_for_size_tree (TYPE_SIZE (new_type), MODE_INT, 1));
5752
5753   /* If neither the mode nor the size has shrunk, return the old type.  */
5754   if (TYPE_MODE (new_type) == BLKmode && new_size >= size)
5755     return type;
5756
5757   return new_type;
5758 }
5759 \f
5760 /* Ensure that TYPE has SIZE and ALIGN.  Make and return a new padded type
5761    if needed.  We have already verified that SIZE and TYPE are large enough.
5762
5763    GNAT_ENTITY and NAME_TRAILER are used to name the resulting record and
5764    to issue a warning.
5765
5766    IS_USER_TYPE is true if we must complete the original type.
5767
5768    DEFINITION is true if this type is being defined.
5769
5770    SAME_RM_SIZE is true if the RM_Size of the resulting type is to be set
5771    to SIZE too; otherwise, it's set to the RM_Size of the original type.  */
5772
5773 tree
5774 maybe_pad_type (tree type, tree size, unsigned int align,
5775                 Entity_Id gnat_entity, const char *name_trailer,
5776                 bool is_user_type, bool definition, bool same_rm_size)
5777 {
5778   tree orig_rm_size = same_rm_size ? NULL_TREE : rm_size (type);
5779   tree orig_size = TYPE_SIZE (type);
5780   unsigned int orig_align = align;
5781   tree record, field;
5782
5783   /* If TYPE is a padded type, see if it agrees with any size and alignment
5784      we were given.  If so, return the original type.  Otherwise, strip
5785      off the padding, since we will either be returning the inner type
5786      or repadding it.  If no size or alignment is specified, use that of
5787      the original padded type.  */
5788   if (TREE_CODE (type) == RECORD_TYPE && TYPE_IS_PADDING_P (type))
5789     {
5790       if ((!size
5791            || operand_equal_p (round_up (size,
5792                                          MAX (align, TYPE_ALIGN (type))),
5793                                round_up (TYPE_SIZE (type),
5794                                          MAX (align, TYPE_ALIGN (type))),
5795                                0))
5796           && (align == 0 || align == TYPE_ALIGN (type)))
5797         return type;
5798
5799       if (!size)
5800         size = TYPE_SIZE (type);
5801       if (align == 0)
5802         align = TYPE_ALIGN (type);
5803
5804       type = TREE_TYPE (TYPE_FIELDS (type));
5805       orig_size = TYPE_SIZE (type);
5806     }
5807
5808   /* If the size is either not being changed or is being made smaller (which
5809      is not done here (and is only valid for bitfields anyway), show the size
5810      isn't changing.  Likewise, clear the alignment if it isn't being
5811      changed.  Then return if we aren't doing anything.  */
5812   if (size
5813       && (operand_equal_p (size, orig_size, 0)
5814           || (TREE_CODE (orig_size) == INTEGER_CST
5815               && tree_int_cst_lt (size, orig_size))))
5816     size = NULL_TREE;
5817
5818   if (align == TYPE_ALIGN (type))
5819     align = 0;
5820
5821   if (align == 0 && !size)
5822     return type;
5823
5824   /* If requested, complete the original type and give it a name.  */
5825   if (is_user_type)
5826     create_type_decl (get_entity_name (gnat_entity), type,
5827                       NULL, !Comes_From_Source (gnat_entity),
5828                       !(TYPE_NAME (type)
5829                         && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5830                         && DECL_IGNORED_P (TYPE_NAME (type))),
5831                       gnat_entity);
5832
5833   /* We used to modify the record in place in some cases, but that could
5834      generate incorrect debugging information.  So make a new record
5835      type and name.  */
5836   record = make_node (RECORD_TYPE);
5837   TYPE_IS_PADDING_P (record) = 1;
5838
5839   if (Present (gnat_entity))
5840     TYPE_NAME (record) = create_concat_name (gnat_entity, name_trailer);
5841
5842   TYPE_VOLATILE (record)
5843     = Present (gnat_entity) && Treat_As_Volatile (gnat_entity);
5844
5845   TYPE_ALIGN (record) = align;
5846   if (orig_align)
5847     TYPE_USER_ALIGN (record) = align;
5848
5849   TYPE_SIZE (record) = size ? size : orig_size;
5850   TYPE_SIZE_UNIT (record)
5851     = convert (sizetype,
5852                size_binop (CEIL_DIV_EXPR, TYPE_SIZE (record),
5853                            bitsize_unit_node));
5854
5855   /* If we are changing the alignment and the input type is a record with
5856      BLKmode and a small constant size, try to make a form that has an
5857      integral mode.  This might allow the padding record to also have an
5858      integral mode, which will be much more efficient.  There is no point
5859      in doing so if a size is specified unless it is also a small constant
5860      size and it is incorrect to do so if we cannot guarantee that the mode
5861      will be naturally aligned since the field must always be addressable.
5862
5863      ??? This might not always be a win when done for a stand-alone object:
5864      since the nominal and the effective type of the object will now have
5865      different modes, a VIEW_CONVERT_EXPR will be required for converting
5866      between them and it might be hard to overcome afterwards, including
5867      at the RTL level when the stand-alone object is accessed as a whole.  */
5868   if (align != 0
5869       && TREE_CODE (type) == RECORD_TYPE
5870       && TYPE_MODE (type) == BLKmode
5871       && TREE_CODE (orig_size) == INTEGER_CST
5872       && !TREE_CONSTANT_OVERFLOW (orig_size)
5873       && compare_tree_int (orig_size, MAX_FIXED_MODE_SIZE) <= 0
5874       && (!size
5875           || (TREE_CODE (size) == INTEGER_CST
5876               && compare_tree_int (size, MAX_FIXED_MODE_SIZE) <= 0)))
5877     {
5878       tree packable_type = make_packable_type (type, true);
5879       if (TYPE_MODE (packable_type) != BLKmode
5880           && align >= TYPE_ALIGN (packable_type))
5881         type = packable_type;
5882     }
5883
5884   /* Now create the field with the original size.  */
5885   field  = create_field_decl (get_identifier ("F"), type, record, 0,
5886                               orig_size, bitsize_zero_node, 1);
5887   DECL_INTERNAL_P (field) = 1;
5888
5889   /* Do not finalize it until after the auxiliary record is built.  */
5890   finish_record_type (record, field, 1, true);
5891
5892   /* Set the same size for its RM_size if requested; otherwise reuse
5893      the RM_size of the original type.  */
5894   SET_TYPE_ADA_SIZE (record, same_rm_size ? size : orig_rm_size);
5895
5896   /* Unless debugging information isn't being written for the input type,
5897      write a record that shows what we are a subtype of and also make a
5898      variable that indicates our size, if still variable.  */
5899   if (TYPE_NAME (record)
5900       && AGGREGATE_TYPE_P (type)
5901       && TREE_CODE (orig_size) != INTEGER_CST
5902       && !(TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5903            && DECL_IGNORED_P (TYPE_NAME (type))))
5904     {
5905       tree marker = make_node (RECORD_TYPE);
5906       tree name = TYPE_NAME (record);
5907       tree orig_name = TYPE_NAME (type);
5908
5909       if (TREE_CODE (name) == TYPE_DECL)
5910         name = DECL_NAME (name);
5911
5912       if (TREE_CODE (orig_name) == TYPE_DECL)
5913         orig_name = DECL_NAME (orig_name);
5914
5915       TYPE_NAME (marker) = concat_id_with_name (name, "XVS");
5916       finish_record_type (marker,
5917                           create_field_decl (orig_name, integer_type_node,
5918                                              marker, 0, NULL_TREE, NULL_TREE,
5919                                              0),
5920                           0, false);
5921
5922       add_parallel_type (TYPE_STUB_DECL (record), marker);
5923
5924       if (size && TREE_CODE (size) != INTEGER_CST && definition)
5925         create_var_decl (concat_id_with_name (name, "XVZ"), NULL_TREE,
5926                          sizetype, TYPE_SIZE_UNIT (record), false, false,
5927                          false, false, NULL, gnat_entity);
5928     }
5929
5930   rest_of_record_type_compilation (record);
5931
5932   /* If the size was widened explicitly, maybe give a warning.  Take the
5933      original size as the maximum size of the input if there was an
5934      unconstrained record involved and round it up to the specified alignment,
5935      if one was specified.  */
5936   if (CONTAINS_PLACEHOLDER_P (orig_size))
5937     orig_size = max_size (orig_size, true);
5938
5939   if (align)
5940     orig_size = round_up (orig_size, align);
5941
5942   if (size && Present (gnat_entity)
5943       && !operand_equal_p (size, orig_size, 0)
5944       && !(TREE_CODE (size) == INTEGER_CST
5945            && TREE_CODE (orig_size) == INTEGER_CST
5946            && tree_int_cst_lt (size, orig_size)))
5947     {
5948       Node_Id gnat_error_node = Empty;
5949
5950       if (Is_Packed_Array_Type (gnat_entity))
5951         gnat_entity = Original_Array_Type (gnat_entity);
5952
5953       if ((Ekind (gnat_entity) == E_Component
5954            || Ekind (gnat_entity) == E_Discriminant)
5955           && Present (Component_Clause (gnat_entity)))
5956         gnat_error_node = Last_Bit (Component_Clause (gnat_entity));
5957       else if (Present (Size_Clause (gnat_entity)))
5958         gnat_error_node = Expression (Size_Clause (gnat_entity));
5959
5960       /* Generate message only for entities that come from source, since
5961          if we have an entity created by expansion, the message will be
5962          generated for some other corresponding source entity.  */
5963       if (Comes_From_Source (gnat_entity) && Present (gnat_error_node))
5964         post_error_ne_tree ("{^ }bits of & unused?", gnat_error_node,
5965                             gnat_entity,
5966                             size_diffop (size, orig_size));
5967
5968       else if (*name_trailer == 'C' && !Is_Internal (gnat_entity))
5969         post_error_ne_tree ("component of& padded{ by ^ bits}?",
5970                             gnat_entity, gnat_entity,
5971                             size_diffop (size, orig_size));
5972     }
5973
5974   return record;
5975 }
5976 \f
5977 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
5978    the value passed against the list of choices.  */
5979
5980 tree
5981 choices_to_gnu (tree operand, Node_Id choices)
5982 {
5983   Node_Id choice;
5984   Node_Id gnat_temp;
5985   tree result = integer_zero_node;
5986   tree this_test, low = 0, high = 0, single = 0;
5987
5988   for (choice = First (choices); Present (choice); choice = Next (choice))
5989     {
5990       switch (Nkind (choice))
5991         {
5992         case N_Range:
5993           low = gnat_to_gnu (Low_Bound (choice));
5994           high = gnat_to_gnu (High_Bound (choice));
5995
5996           /* There's no good type to use here, so we might as well use
5997              integer_type_node.  */
5998           this_test
5999             = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6000                                build_binary_op (GE_EXPR, integer_type_node,
6001                                                 operand, low),
6002                                build_binary_op (LE_EXPR, integer_type_node,
6003                                                 operand, high));
6004
6005           break;
6006
6007         case N_Subtype_Indication:
6008           gnat_temp = Range_Expression (Constraint (choice));
6009           low = gnat_to_gnu (Low_Bound (gnat_temp));
6010           high = gnat_to_gnu (High_Bound (gnat_temp));
6011
6012           this_test
6013             = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6014                                build_binary_op (GE_EXPR, integer_type_node,
6015                                                 operand, low),
6016                                build_binary_op (LE_EXPR, integer_type_node,
6017                                                 operand, high));
6018           break;
6019
6020         case N_Identifier:
6021         case N_Expanded_Name:
6022           /* This represents either a subtype range, an enumeration
6023              literal, or a constant  Ekind says which.  If an enumeration
6024              literal or constant, fall through to the next case.  */
6025           if (Ekind (Entity (choice)) != E_Enumeration_Literal
6026               && Ekind (Entity (choice)) != E_Constant)
6027             {
6028               tree type = gnat_to_gnu_type (Entity (choice));
6029
6030               low = TYPE_MIN_VALUE (type);
6031               high = TYPE_MAX_VALUE (type);
6032
6033               this_test
6034                 = build_binary_op (TRUTH_ANDIF_EXPR, integer_type_node,
6035                                    build_binary_op (GE_EXPR, integer_type_node,
6036                                                     operand, low),
6037                                    build_binary_op (LE_EXPR, integer_type_node,
6038                                                     operand, high));
6039               break;
6040             }
6041
6042           /* ... fall through ... */
6043
6044         case N_Character_Literal:
6045         case N_Integer_Literal:
6046           single = gnat_to_gnu (choice);
6047           this_test = build_binary_op (EQ_EXPR, integer_type_node, operand,
6048                                        single);
6049           break;
6050
6051         case N_Others_Choice:
6052           this_test = integer_one_node;
6053           break;
6054
6055         default:
6056           gcc_unreachable ();
6057         }
6058
6059       result = build_binary_op (TRUTH_ORIF_EXPR, integer_type_node,
6060                                 result, this_test);
6061     }
6062
6063   return result;
6064 }
6065 \f
6066 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6067    type FIELD_TYPE to be placed in RECORD_TYPE.  Return the result.  */
6068
6069 static int
6070 adjust_packed (tree field_type, tree record_type, int packed)
6071 {
6072   /* If the field contains an item of variable size, we cannot pack it
6073      because we cannot create temporaries of non-fixed size in case
6074      we need to take the address of the field.  See addressable_p and
6075      the notes on the addressability issues for further details.  */
6076   if (is_variable_size (field_type))
6077     return 0;
6078
6079   /* If the alignment of the record is specified and the field type
6080      is over-aligned, request Storage_Unit alignment for the field.  */
6081   if (packed == -2)
6082     {
6083       if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6084         return -1;
6085       else
6086         return 0;
6087     }
6088
6089   return packed;
6090 }
6091
6092 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6093    placed in GNU_RECORD_TYPE.
6094
6095    PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6096    record has Component_Alignment of Storage_Unit, -2 if the enclosing
6097    record has a specified alignment.
6098
6099    DEFINITION is true if this field is for a record being defined.  */
6100
6101 static tree
6102 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6103                    bool definition)
6104 {
6105   tree gnu_field_id = get_entity_name (gnat_field);
6106   tree gnu_field_type = gnat_to_gnu_type (Etype (gnat_field));
6107   tree gnu_field, gnu_size, gnu_pos;
6108   bool needs_strict_alignment
6109     = (Is_Aliased (gnat_field) || Strict_Alignment (Etype (gnat_field))
6110        || Treat_As_Volatile (gnat_field));
6111
6112   /* If this field requires strict alignment, we cannot pack it because
6113      it would very likely be under-aligned in the record.  */
6114   if (needs_strict_alignment)
6115     packed = 0;
6116   else
6117     packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6118
6119   /* If a size is specified, use it.  Otherwise, if the record type is packed,
6120      use the official RM size.  See "Handling of Type'Size Values" in Einfo
6121      for further details.  */
6122   if (Known_Static_Esize (gnat_field))
6123     gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6124                               gnat_field, FIELD_DECL, false, true);
6125   else if (packed == 1)
6126     gnu_size = validate_size (RM_Size (Etype (gnat_field)), gnu_field_type,
6127                               gnat_field, FIELD_DECL, false, true);
6128   else
6129     gnu_size = NULL_TREE;
6130
6131   /* If we have a specified size that's smaller than that of the field type,
6132      or a position is specified, and the field type is also a record that's
6133      BLKmode, see if we can get either an integral mode form of the type or
6134      a smaller BLKmode form.  If we can, show a size was specified for the
6135      field if there wasn't one already, so we know to make this a bitfield
6136      and avoid making things wider.
6137
6138      Doing this is first useful if the record is packed because we may then
6139      place the field at a non-byte-aligned position and so achieve tighter
6140      packing.
6141
6142      This is in addition *required* if the field shares a byte with another
6143      field and the front-end lets the back-end handle the references, because
6144      GCC does not handle BLKmode bitfields properly.
6145
6146      We avoid the transformation if it is not required or potentially useful,
6147      as it might entail an increase of the field's alignment and have ripple
6148      effects on the outer record type.  A typical case is a field known to be
6149      byte aligned and not to share a byte with another field.
6150
6151      Besides, we don't even look the possibility of a transformation in cases
6152      known to be in error already, for instance when an invalid size results
6153      from a component clause.  */
6154
6155   if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6156       && TYPE_MODE (gnu_field_type) == BLKmode
6157       && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6158       && (packed == 1
6159           || (gnu_size
6160               && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6161                   || Present (Component_Clause (gnat_field))))))
6162     {
6163       /* See what the alternate type and size would be.  */
6164       tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6165
6166       bool has_byte_aligned_clause
6167         = Present (Component_Clause (gnat_field))
6168           && (UI_To_Int (Component_Bit_Offset (gnat_field))
6169               % BITS_PER_UNIT == 0);
6170
6171       /* Compute whether we should avoid the substitution.  */
6172       bool reject
6173         /* There is no point substituting if there is no change...  */
6174         = (gnu_packable_type == gnu_field_type)
6175          /* ... nor when the field is known to be byte aligned and not to
6176             share a byte with another field.  */
6177           || (has_byte_aligned_clause
6178               && value_factor_p (gnu_size, BITS_PER_UNIT))
6179          /* The size of an aliased field must be an exact multiple of the
6180             type's alignment, which the substitution might increase.  Reject
6181             substitutions that would so invalidate a component clause when the
6182             specified position is byte aligned, as the change would have no
6183             real benefit from the packing standpoint anyway.  */
6184           || (Is_Aliased (gnat_field)
6185               && has_byte_aligned_clause
6186               && !value_factor_p (gnu_size, TYPE_ALIGN (gnu_packable_type)));
6187
6188       /* Substitute unless told otherwise.  */
6189       if (!reject)
6190         {
6191           gnu_field_type = gnu_packable_type;
6192
6193           if (!gnu_size)
6194             gnu_size = rm_size (gnu_field_type);
6195         }
6196     }
6197
6198   /* If we are packing the record and the field is BLKmode, round the
6199      size up to a byte boundary.  */
6200   if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6201     gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6202
6203   if (Present (Component_Clause (gnat_field)))
6204     {
6205       gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6206       gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6207                                 gnat_field, FIELD_DECL, false, true);
6208
6209       /* Ensure the position does not overlap with the parent subtype,
6210          if there is one.  */
6211       if (Present (Parent_Subtype (Underlying_Type (Scope (gnat_field)))))
6212         {
6213           tree gnu_parent
6214             = gnat_to_gnu_type (Parent_Subtype
6215                                 (Underlying_Type (Scope (gnat_field))));
6216
6217           if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6218               && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6219             {
6220               post_error_ne_tree
6221                 ("offset of& must be beyond parent{, minimum allowed is ^}",
6222                  First_Bit (Component_Clause (gnat_field)), gnat_field,
6223                  TYPE_SIZE_UNIT (gnu_parent));
6224             }
6225         }
6226
6227       /* If this field needs strict alignment, ensure the record is
6228          sufficiently aligned and that that position and size are
6229          consistent with the alignment.  */
6230       if (needs_strict_alignment)
6231         {
6232           TYPE_ALIGN (gnu_record_type)
6233             = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6234
6235           if (gnu_size
6236               && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6237             {
6238               if (Is_Atomic (gnat_field) || Is_Atomic (Etype (gnat_field)))
6239                 post_error_ne_tree
6240                   ("atomic field& must be natural size of type{ (^)}",
6241                    Last_Bit (Component_Clause (gnat_field)), gnat_field,
6242                    TYPE_SIZE (gnu_field_type));
6243
6244               else if (Is_Aliased (gnat_field))
6245                 post_error_ne_tree
6246                   ("size of aliased field& must be ^ bits",
6247                    Last_Bit (Component_Clause (gnat_field)), gnat_field,
6248                    TYPE_SIZE (gnu_field_type));
6249
6250               else if (Strict_Alignment (Etype (gnat_field)))
6251                 post_error_ne_tree
6252                   ("size of & with aliased or tagged components not ^ bits",
6253                    Last_Bit (Component_Clause (gnat_field)), gnat_field,
6254                    TYPE_SIZE (gnu_field_type));
6255
6256               gnu_size = NULL_TREE;
6257             }
6258
6259           if (!integer_zerop (size_binop
6260                               (TRUNC_MOD_EXPR, gnu_pos,
6261                                bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6262             {
6263               if (Is_Aliased (gnat_field))
6264                 post_error_ne_num
6265                   ("position of aliased field& must be multiple of ^ bits",
6266                    First_Bit (Component_Clause (gnat_field)), gnat_field,
6267                    TYPE_ALIGN (gnu_field_type));
6268
6269               else if (Treat_As_Volatile (gnat_field))
6270                 post_error_ne_num
6271                   ("position of volatile field& must be multiple of ^ bits",
6272                    First_Bit (Component_Clause (gnat_field)), gnat_field,
6273                    TYPE_ALIGN (gnu_field_type));
6274
6275               else if (Strict_Alignment (Etype (gnat_field)))
6276                 post_error_ne_num
6277   ("position of & with aliased or tagged components not multiple of ^ bits",
6278                    First_Bit (Component_Clause (gnat_field)), gnat_field,
6279                    TYPE_ALIGN (gnu_field_type));
6280
6281               else
6282                 gcc_unreachable ();
6283
6284               gnu_pos = NULL_TREE;
6285             }
6286         }
6287
6288       if (Is_Atomic (gnat_field))
6289         check_ok_for_atomic (gnu_field_type, gnat_field, false);
6290     }
6291
6292   /* If the record has rep clauses and this is the tag field, make a rep
6293      clause for it as well.  */
6294   else if (Has_Specified_Layout (Scope (gnat_field))
6295            && Chars (gnat_field) == Name_uTag)
6296     {
6297       gnu_pos = bitsize_zero_node;
6298       gnu_size = TYPE_SIZE (gnu_field_type);
6299     }
6300
6301   else
6302     gnu_pos = NULL_TREE;
6303
6304   /* We need to make the size the maximum for the type if it is
6305      self-referential and an unconstrained type.  In that case, we can't
6306      pack the field since we can't make a copy to align it.  */
6307   if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6308       && !gnu_size
6309       && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6310       && !Is_Constrained (Underlying_Type (Etype (gnat_field))))
6311     {
6312       gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6313       packed = 0;
6314     }
6315
6316   /* If a size is specified, adjust the field's type to it.  */
6317   if (gnu_size)
6318     {
6319       /* If the field's type is justified modular, we would need to remove
6320          the wrapper to (better) meet the layout requirements.  However we
6321          can do so only if the field is not aliased to preserve the unique
6322          layout and if the prescribed size is not greater than that of the
6323          packed array to preserve the justification.  */
6324       if (!needs_strict_alignment
6325           && TREE_CODE (gnu_field_type) == RECORD_TYPE
6326           && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6327           && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6328                <= 0)
6329         gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6330
6331       gnu_field_type
6332         = make_type_from_size (gnu_field_type, gnu_size,
6333                                Has_Biased_Representation (gnat_field));
6334       gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6335                                        "PAD", false, definition, true);
6336     }
6337
6338   /* Otherwise (or if there was an error), don't specify a position.  */
6339   else
6340     gnu_pos = NULL_TREE;
6341
6342   gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6343               || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6344
6345   /* Now create the decl for the field.  */
6346   gnu_field = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6347                                  packed, gnu_size, gnu_pos,
6348                                  Is_Aliased (gnat_field));
6349   Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6350   TREE_THIS_VOLATILE (gnu_field) = Treat_As_Volatile (gnat_field);
6351
6352   if (Ekind (gnat_field) == E_Discriminant)
6353     DECL_DISCRIMINANT_NUMBER (gnu_field)
6354       = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6355
6356   return gnu_field;
6357 }
6358 \f
6359 /* Return true if TYPE is a type with variable size, a padding type with a
6360    field of variable size or is a record that has a field such a field.  */
6361
6362 static bool
6363 is_variable_size (tree type)
6364 {
6365   tree field;
6366
6367   if (!TREE_CONSTANT (TYPE_SIZE (type)))
6368     return true;
6369
6370   if (TREE_CODE (type) == RECORD_TYPE
6371       && TYPE_IS_PADDING_P (type)
6372       && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6373     return true;
6374
6375   if (TREE_CODE (type) != RECORD_TYPE
6376       && TREE_CODE (type) != UNION_TYPE
6377       && TREE_CODE (type) != QUAL_UNION_TYPE)
6378     return false;
6379
6380   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6381     if (is_variable_size (TREE_TYPE (field)))
6382       return true;
6383
6384   return false;
6385 }
6386 \f
6387 /* qsort comparer for the bit positions of two record components.  */
6388
6389 static int
6390 compare_field_bitpos (const PTR rt1, const PTR rt2)
6391 {
6392   const_tree const field1 = * (const_tree const *) rt1;
6393   const_tree const field2 = * (const_tree const *) rt2;
6394   const int ret
6395     = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6396
6397   return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6398 }
6399
6400 /* Return a GCC tree for a record type given a GNAT Component_List and a chain
6401    of GCC trees for fields that are in the record and have already been
6402    processed.  When called from gnat_to_gnu_entity during the processing of a
6403    record type definition, the GCC nodes for the discriminants will be on
6404    the chain.  The other calls to this function are recursive calls from
6405    itself for the Component_List of a variant and the chain is empty.
6406
6407    PACKED is 1 if this is for a packed record, -1 if this is for a record
6408    with Component_Alignment of Storage_Unit, -2 if this is for a record
6409    with a specified alignment.
6410
6411    DEFINITION is true if we are defining this record.
6412
6413    P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6414    with a rep clause is to be added.  If it is nonzero, that is all that
6415    should be done with such fields.
6416
6417    CANCEL_ALIGNMENT, if true, means the alignment should be zeroed before
6418    laying out the record.  This means the alignment only serves to force fields
6419    to be bitfields, but not require the record to be that aligned.  This is
6420    used for variants.
6421
6422    ALL_REP, if true, means a rep clause was found for all the fields.  This
6423    simplifies the logic since we know we're not in the mixed case.
6424
6425    DO_NOT_FINALIZE, if true, means that the record type is expected to be
6426    modified afterwards so it will not be sent to the back-end for finalization.
6427
6428    UNCHECKED_UNION, if true, means that we are building a type for a record
6429    with a Pragma Unchecked_Union.
6430
6431    The processing of the component list fills in the chain with all of the
6432    fields of the record and then the record type is finished.  */
6433
6434 static void
6435 components_to_record (tree gnu_record_type, Node_Id component_list,
6436                       tree gnu_field_list, int packed, bool definition,
6437                       tree *p_gnu_rep_list, bool cancel_alignment,
6438                       bool all_rep, bool do_not_finalize, bool unchecked_union)
6439 {
6440   Node_Id component_decl;
6441   Entity_Id gnat_field;
6442   Node_Id variant_part;
6443   tree gnu_our_rep_list = NULL_TREE;
6444   tree gnu_field, gnu_last;
6445   bool layout_with_rep = false;
6446   bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6447
6448   /* For each variable within each component declaration create a GCC field
6449      and add it to the list, skipping any pragmas in the list.  */
6450   if (Present (Component_Items (component_list)))
6451     for (component_decl = First_Non_Pragma (Component_Items (component_list));
6452          Present (component_decl);
6453          component_decl = Next_Non_Pragma (component_decl))
6454       {
6455         gnat_field = Defining_Entity (component_decl);
6456
6457         if (Chars (gnat_field) == Name_uParent)
6458           gnu_field = tree_last (TYPE_FIELDS (gnu_record_type));
6459         else
6460           {
6461             gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type,
6462                                            packed, definition);
6463
6464             /* If this is the _Tag field, put it before any discriminants,
6465                instead of after them as is the case for all other fields.  */
6466             if (Chars (gnat_field) == Name_uTag)
6467               gnu_field_list = chainon (gnu_field_list, gnu_field);
6468             else
6469               {
6470                 TREE_CHAIN (gnu_field) = gnu_field_list;
6471                 gnu_field_list = gnu_field;
6472               }
6473           }
6474
6475         save_gnu_tree (gnat_field, gnu_field, false);
6476       }
6477
6478   /* At the end of the component list there may be a variant part.  */
6479   variant_part = Variant_Part (component_list);
6480
6481   /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6482      mutually exclusive and should go in the same memory.  To do this we need
6483      to treat each variant as a record whose elements are created from the
6484      component list for the variant.  So here we create the records from the
6485      lists for the variants and put them all into the QUAL_UNION_TYPE.
6486      If this is an Unchecked_Union, we make a UNION_TYPE instead or
6487      use GNU_RECORD_TYPE if there are no fields so far.  */
6488   if (Present (variant_part))
6489     {
6490       tree gnu_discriminant = gnat_to_gnu (Name (variant_part));
6491       Node_Id variant;
6492       tree gnu_name = TYPE_NAME (gnu_record_type);
6493       tree gnu_var_name
6494         = concat_id_with_name (get_identifier (Get_Name_String
6495                                                (Chars (Name (variant_part)))),
6496                                "XVN");
6497       tree gnu_union_type;
6498       tree gnu_union_name;
6499       tree gnu_union_field;
6500       tree gnu_variant_list = NULL_TREE;
6501
6502       if (TREE_CODE (gnu_name) == TYPE_DECL)
6503         gnu_name = DECL_NAME (gnu_name);
6504
6505       gnu_union_name = concat_id_with_name (gnu_name,
6506                                             IDENTIFIER_POINTER (gnu_var_name));
6507
6508       /* Reuse an enclosing union if all fields are in the variant part
6509          and there is no representation clause on the record, to match
6510          the layout of C unions.  There is an associated check below.  */
6511       if (!gnu_field_list
6512           && TREE_CODE (gnu_record_type) == UNION_TYPE
6513           && !TYPE_PACKED (gnu_record_type))
6514         gnu_union_type = gnu_record_type;
6515       else
6516         {
6517           gnu_union_type
6518             = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6519
6520           TYPE_NAME (gnu_union_type) = gnu_union_name;
6521           TYPE_ALIGN (gnu_union_type) = 0;
6522           TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6523         }
6524
6525       for (variant = First_Non_Pragma (Variants (variant_part));
6526            Present (variant);
6527            variant = Next_Non_Pragma (variant))
6528         {
6529           tree gnu_variant_type = make_node (RECORD_TYPE);
6530           tree gnu_inner_name;
6531           tree gnu_qual;
6532
6533           Get_Variant_Encoding (variant);
6534           gnu_inner_name = get_identifier (Name_Buffer);
6535           TYPE_NAME (gnu_variant_type)
6536             = concat_id_with_name (gnu_union_name,
6537                                    IDENTIFIER_POINTER (gnu_inner_name));
6538
6539           /* Set the alignment of the inner type in case we need to make
6540              inner objects into bitfields, but then clear it out
6541              so the record actually gets only the alignment required.  */
6542           TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6543           TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6544
6545           /* Similarly, if the outer record has a size specified and all fields
6546              have record rep clauses, we can propagate the size into the
6547              variant part.  */
6548           if (all_rep_and_size)
6549             {
6550               TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6551               TYPE_SIZE_UNIT (gnu_variant_type)
6552                 = TYPE_SIZE_UNIT (gnu_record_type);
6553             }
6554
6555           /* Create the record type for the variant.  Note that we defer
6556              finalizing it until after we are sure to actually use it.  */
6557           components_to_record (gnu_variant_type, Component_List (variant),
6558                                 NULL_TREE, packed, definition,
6559                                 &gnu_our_rep_list, !all_rep_and_size, all_rep,
6560                                 true, unchecked_union);
6561
6562           gnu_qual = choices_to_gnu (gnu_discriminant,
6563                                      Discrete_Choices (variant));
6564
6565           Set_Present_Expr (variant, annotate_value (gnu_qual));
6566
6567           /* If this is an Unchecked_Union and we have exactly one field,
6568              use this field directly to match the layout of C unions.  */
6569           if (unchecked_union
6570               && TYPE_FIELDS (gnu_variant_type)
6571               && !TREE_CHAIN (TYPE_FIELDS (gnu_variant_type)))
6572             gnu_field = TYPE_FIELDS (gnu_variant_type);
6573           else
6574             {
6575               /* Deal with packedness like in gnat_to_gnu_field.  */
6576               int field_packed
6577                 = adjust_packed (gnu_variant_type, gnu_record_type, packed);
6578
6579               /* Finalize the record type now.  We used to throw away
6580                  empty records but we no longer do that because we need
6581                  them to generate complete debug info for the variant;
6582                  otherwise, the union type definition will be lacking
6583                  the fields associated with these empty variants.  */
6584               rest_of_record_type_compilation (gnu_variant_type);
6585
6586               gnu_field = create_field_decl (gnu_inner_name, gnu_variant_type,
6587                                              gnu_union_type, field_packed,
6588                                              (all_rep_and_size
6589                                               ? TYPE_SIZE (gnu_variant_type)
6590                                               : 0),
6591                                              (all_rep_and_size
6592                                               ? bitsize_zero_node : 0),
6593                                              0);
6594
6595               DECL_INTERNAL_P (gnu_field) = 1;
6596
6597               if (!unchecked_union)
6598                 DECL_QUALIFIER (gnu_field) = gnu_qual;
6599             }
6600
6601           TREE_CHAIN (gnu_field) = gnu_variant_list;
6602           gnu_variant_list = gnu_field;
6603         }
6604
6605       /* Only make the QUAL_UNION_TYPE if there are any non-empty variants.  */
6606       if (gnu_variant_list)
6607         {
6608           int union_field_packed;
6609
6610           if (all_rep_and_size)
6611             {
6612               TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
6613               TYPE_SIZE_UNIT (gnu_union_type)
6614                 = TYPE_SIZE_UNIT (gnu_record_type);
6615             }
6616
6617           finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
6618                               all_rep_and_size ? 1 : 0, false);
6619
6620           /* If GNU_UNION_TYPE is our record type, it means we must have an
6621              Unchecked_Union with no fields.  Verify that and, if so, just
6622              return.  */
6623           if (gnu_union_type == gnu_record_type)
6624             {
6625               gcc_assert (unchecked_union
6626                           && !gnu_field_list
6627                           && !gnu_our_rep_list);
6628               return;
6629             }
6630
6631           /* Deal with packedness like in gnat_to_gnu_field.  */
6632           union_field_packed
6633             = adjust_packed (gnu_union_type, gnu_record_type, packed);
6634
6635           gnu_union_field
6636             = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
6637                                  union_field_packed,
6638                                  all_rep ? TYPE_SIZE (gnu_union_type) : 0,
6639                                  all_rep ? bitsize_zero_node : 0, 0);
6640
6641           DECL_INTERNAL_P (gnu_union_field) = 1;
6642           TREE_CHAIN (gnu_union_field) = gnu_field_list;
6643           gnu_field_list = gnu_union_field;
6644         }
6645     }
6646
6647   /* Scan GNU_FIELD_LIST and see if any fields have rep clauses.  If they
6648      do, pull them out and put them into GNU_OUR_REP_LIST.  We have to do this
6649      in a separate pass since we want to handle the discriminants but can't
6650      play with them until we've used them in debugging data above.
6651
6652      ??? Note: if we then reorder them, debugging information will be wrong,
6653      but there's nothing that can be done about this at the moment.  */
6654   for (gnu_field = gnu_field_list, gnu_last = NULL_TREE; gnu_field; )
6655     {
6656       if (DECL_FIELD_OFFSET (gnu_field))
6657         {
6658           tree gnu_next = TREE_CHAIN (gnu_field);
6659
6660           if (!gnu_last)
6661             gnu_field_list = gnu_next;
6662           else
6663             TREE_CHAIN (gnu_last) = gnu_next;
6664
6665           TREE_CHAIN (gnu_field) = gnu_our_rep_list;
6666           gnu_our_rep_list = gnu_field;
6667           gnu_field = gnu_next;
6668         }
6669       else
6670         {
6671           gnu_last = gnu_field;
6672           gnu_field = TREE_CHAIN (gnu_field);
6673         }
6674     }
6675
6676   /* If we have any items in our rep'ed field list, it is not the case that all
6677      the fields in the record have rep clauses, and P_REP_LIST is nonzero,
6678      set it and ignore the items.  */
6679   if (gnu_our_rep_list && p_gnu_rep_list && !all_rep)
6680     *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_our_rep_list);
6681   else if (gnu_our_rep_list)
6682     {
6683       /* Otherwise, sort the fields by bit position and put them into their
6684          own record if we have any fields without rep clauses.  */
6685       tree gnu_rep_type
6686         = (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
6687       int len = list_length (gnu_our_rep_list);
6688       tree *gnu_arr = (tree *) alloca (sizeof (tree) * len);
6689       int i;
6690
6691       for (i = 0, gnu_field = gnu_our_rep_list; gnu_field;
6692            gnu_field = TREE_CHAIN (gnu_field), i++)
6693         gnu_arr[i] = gnu_field;
6694
6695       qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
6696
6697       /* Put the fields in the list in order of increasing position, which
6698          means we start from the end.  */
6699       gnu_our_rep_list = NULL_TREE;
6700       for (i = len - 1; i >= 0; i--)
6701         {
6702           TREE_CHAIN (gnu_arr[i]) = gnu_our_rep_list;
6703           gnu_our_rep_list = gnu_arr[i];
6704           DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
6705         }
6706
6707       if (gnu_field_list)
6708         {
6709           finish_record_type (gnu_rep_type, gnu_our_rep_list, 1, false);
6710           gnu_field = create_field_decl (get_identifier ("REP"), gnu_rep_type,
6711                                          gnu_record_type, 0, 0, 0, 1);
6712           DECL_INTERNAL_P (gnu_field) = 1;
6713           gnu_field_list = chainon (gnu_field_list, gnu_field);
6714         }
6715       else
6716         {
6717           layout_with_rep = true;
6718           gnu_field_list = nreverse (gnu_our_rep_list);
6719         }
6720     }
6721
6722   if (cancel_alignment)
6723     TYPE_ALIGN (gnu_record_type) = 0;
6724
6725   finish_record_type (gnu_record_type, nreverse (gnu_field_list),
6726                       layout_with_rep ? 1 : 0, do_not_finalize);
6727 }
6728 \f
6729 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
6730    placed into an Esize, Component_Bit_Offset, or Component_Size value
6731    in the GNAT tree.  */
6732
6733 static Uint
6734 annotate_value (tree gnu_size)
6735 {
6736   int len = TREE_CODE_LENGTH (TREE_CODE (gnu_size));
6737   TCode tcode;
6738   Node_Ref_Or_Val ops[3], ret;
6739   int i;
6740   int size;
6741   struct tree_int_map **h = NULL;
6742
6743   /* See if we've already saved the value for this node.  */
6744   if (EXPR_P (gnu_size))
6745     {
6746       struct tree_int_map in;
6747       if (!annotate_value_cache)
6748         annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
6749                                                 tree_int_map_eq, 0);
6750       in.base.from = gnu_size;
6751       h = (struct tree_int_map **)
6752             htab_find_slot (annotate_value_cache, &in, INSERT);
6753
6754       if (*h)
6755         return (Node_Ref_Or_Val) (*h)->to;
6756     }
6757
6758   /* If we do not return inside this switch, TCODE will be set to the
6759      code to use for a Create_Node operand and LEN (set above) will be
6760      the number of recursive calls for us to make.  */
6761
6762   switch (TREE_CODE (gnu_size))
6763     {
6764     case INTEGER_CST:
6765       if (TREE_OVERFLOW (gnu_size))
6766         return No_Uint;
6767
6768       /* This may have come from a conversion from some smaller type,
6769          so ensure this is in bitsizetype.  */
6770       gnu_size = convert (bitsizetype, gnu_size);
6771
6772       /* For negative values, use NEGATE_EXPR of the supplied value.  */
6773       if (tree_int_cst_sgn (gnu_size) < 0)
6774         {
6775           /* The ridiculous code below is to handle the case of the largest
6776              negative integer.  */
6777           tree negative_size = size_diffop (bitsize_zero_node, gnu_size);
6778           bool adjust = false;
6779           tree temp;
6780
6781           if (TREE_OVERFLOW (negative_size))
6782             {
6783               negative_size
6784                 = size_binop (MINUS_EXPR, bitsize_zero_node,
6785                               size_binop (PLUS_EXPR, gnu_size,
6786                                           bitsize_one_node));
6787               adjust = true;
6788             }
6789
6790           temp = build1 (NEGATE_EXPR, bitsizetype, negative_size);
6791           if (adjust)
6792             temp = build2 (MINUS_EXPR, bitsizetype, temp, bitsize_one_node);
6793
6794           return annotate_value (temp);
6795         }
6796
6797       if (!host_integerp (gnu_size, 1))
6798         return No_Uint;
6799
6800       size = tree_low_cst (gnu_size, 1);
6801
6802       /* This peculiar test is to make sure that the size fits in an int
6803          on machines where HOST_WIDE_INT is not "int".  */
6804       if (tree_low_cst (gnu_size, 1) == size)
6805         return UI_From_Int (size);
6806       else
6807         return No_Uint;
6808
6809     case COMPONENT_REF:
6810       /* The only case we handle here is a simple discriminant reference.  */
6811       if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR
6812           && TREE_CODE (TREE_OPERAND (gnu_size, 1)) == FIELD_DECL
6813           && DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
6814         return Create_Node (Discrim_Val,
6815                             annotate_value (DECL_DISCRIMINANT_NUMBER
6816                                             (TREE_OPERAND (gnu_size, 1))),
6817                             No_Uint, No_Uint);
6818       else
6819         return No_Uint;
6820
6821     CASE_CONVERT:   case NON_LVALUE_EXPR:
6822       return annotate_value (TREE_OPERAND (gnu_size, 0));
6823
6824       /* Now just list the operations we handle.  */
6825     case COND_EXPR:             tcode = Cond_Expr; break;
6826     case PLUS_EXPR:             tcode = Plus_Expr; break;
6827     case MINUS_EXPR:            tcode = Minus_Expr; break;
6828     case MULT_EXPR:             tcode = Mult_Expr; break;
6829     case TRUNC_DIV_EXPR:        tcode = Trunc_Div_Expr; break;
6830     case CEIL_DIV_EXPR:         tcode = Ceil_Div_Expr; break;
6831     case FLOOR_DIV_EXPR:        tcode = Floor_Div_Expr; break;
6832     case TRUNC_MOD_EXPR:        tcode = Trunc_Mod_Expr; break;
6833     case CEIL_MOD_EXPR:         tcode = Ceil_Mod_Expr; break;
6834     case FLOOR_MOD_EXPR:        tcode = Floor_Mod_Expr; break;
6835     case EXACT_DIV_EXPR:        tcode = Exact_Div_Expr; break;
6836     case NEGATE_EXPR:           tcode = Negate_Expr; break;
6837     case MIN_EXPR:              tcode = Min_Expr; break;
6838     case MAX_EXPR:              tcode = Max_Expr; break;
6839     case ABS_EXPR:              tcode = Abs_Expr; break;
6840     case TRUTH_ANDIF_EXPR:      tcode = Truth_Andif_Expr; break;
6841     case TRUTH_ORIF_EXPR:       tcode = Truth_Orif_Expr; break;
6842     case TRUTH_AND_EXPR:        tcode = Truth_And_Expr; break;
6843     case TRUTH_OR_EXPR:         tcode = Truth_Or_Expr; break;
6844     case TRUTH_XOR_EXPR:        tcode = Truth_Xor_Expr; break;
6845     case TRUTH_NOT_EXPR:        tcode = Truth_Not_Expr; break;
6846     case BIT_AND_EXPR:          tcode = Bit_And_Expr; break;
6847     case LT_EXPR:               tcode = Lt_Expr; break;
6848     case LE_EXPR:               tcode = Le_Expr; break;
6849     case GT_EXPR:               tcode = Gt_Expr; break;
6850     case GE_EXPR:               tcode = Ge_Expr; break;
6851     case EQ_EXPR:               tcode = Eq_Expr; break;
6852     case NE_EXPR:               tcode = Ne_Expr; break;
6853
6854     default:
6855       return No_Uint;
6856     }
6857
6858   /* Now get each of the operands that's relevant for this code.  If any
6859      cannot be expressed as a repinfo node, say we can't.  */
6860   for (i = 0; i < 3; i++)
6861     ops[i] = No_Uint;
6862
6863   for (i = 0; i < len; i++)
6864     {
6865       ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
6866       if (ops[i] == No_Uint)
6867         return No_Uint;
6868     }
6869
6870   ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
6871
6872   /* Save the result in the cache.  */
6873   if (h)
6874     {
6875       *h = GGC_NEW (struct tree_int_map);
6876       (*h)->base.from = gnu_size;
6877       (*h)->to = ret;
6878     }
6879
6880   return ret;
6881 }
6882
6883 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding
6884    GCC type, set Component_Bit_Offset and Esize to the position and size
6885    used by Gigi.  */
6886
6887 static void
6888 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
6889 {
6890   tree gnu_list;
6891   tree gnu_entry;
6892   Entity_Id gnat_field;
6893
6894   /* We operate by first making a list of all fields and their positions
6895      (we can get the sizes easily at any time) by a recursive call
6896      and then update all the sizes into the tree.  */
6897   gnu_list = compute_field_positions (gnu_type, NULL_TREE,
6898                                       size_zero_node, bitsize_zero_node,
6899                                       BIGGEST_ALIGNMENT);
6900
6901   for (gnat_field = First_Entity (gnat_entity); Present (gnat_field);
6902        gnat_field = Next_Entity (gnat_field))
6903     if ((Ekind (gnat_field) == E_Component
6904          || (Ekind (gnat_field) == E_Discriminant
6905              && !Is_Unchecked_Union (Scope (gnat_field)))))
6906       {
6907         tree parent_offset = bitsize_zero_node;
6908
6909         gnu_entry = purpose_member (gnat_to_gnu_field_decl (gnat_field),
6910                                     gnu_list);
6911
6912         if (gnu_entry)
6913           {
6914             if (type_annotate_only && Is_Tagged_Type (gnat_entity))
6915               {
6916                 /* In this mode the tag and parent components have not been
6917                    generated, so we add the appropriate offset to each
6918                    component.  For a component appearing in the current
6919                    extension, the offset is the size of the parent.  */
6920             if (Is_Derived_Type (gnat_entity)
6921                 && Original_Record_Component (gnat_field) == gnat_field)
6922               parent_offset
6923                 = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
6924                              bitsizetype);
6925             else
6926               parent_offset = bitsize_int (POINTER_SIZE);
6927               }
6928
6929           Set_Component_Bit_Offset
6930             (gnat_field,
6931              annotate_value
6932              (size_binop (PLUS_EXPR,
6933                           bit_from_pos (TREE_PURPOSE (TREE_VALUE (gnu_entry)),
6934                                         TREE_VALUE (TREE_VALUE
6935                                                     (TREE_VALUE (gnu_entry)))),
6936                           parent_offset)));
6937
6938             Set_Esize (gnat_field,
6939                        annotate_value (DECL_SIZE (TREE_PURPOSE (gnu_entry))));
6940           }
6941         else if (Is_Tagged_Type (gnat_entity)
6942                  && Is_Derived_Type (gnat_entity))
6943           {
6944             /* If there is no gnu_entry, this is an inherited component whose
6945                position is the same as in the parent type.  */
6946             Set_Component_Bit_Offset
6947               (gnat_field,
6948                Component_Bit_Offset (Original_Record_Component (gnat_field)));
6949             Set_Esize (gnat_field,
6950                        Esize (Original_Record_Component (gnat_field)));
6951           }
6952       }
6953 }
6954
6955 /* Scan all fields in GNU_TYPE and build entries where TREE_PURPOSE is the
6956    FIELD_DECL and TREE_VALUE a TREE_LIST with TREE_PURPOSE being the byte
6957    position and TREE_VALUE being a TREE_LIST with TREE_PURPOSE the value to be
6958    placed into DECL_OFFSET_ALIGN and TREE_VALUE the bit position.  GNU_POS is
6959    to be added to the position, GNU_BITPOS to the bit position, OFFSET_ALIGN is
6960    the present value of DECL_OFFSET_ALIGN and GNU_LIST is a list of the entries
6961    so far.  */
6962
6963 static tree
6964 compute_field_positions (tree gnu_type, tree gnu_list, tree gnu_pos,
6965                          tree gnu_bitpos, unsigned int offset_align)
6966 {
6967   tree gnu_field;
6968   tree gnu_result = gnu_list;
6969
6970   for (gnu_field = TYPE_FIELDS (gnu_type); gnu_field;
6971        gnu_field = TREE_CHAIN (gnu_field))
6972     {
6973       tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
6974                                         DECL_FIELD_BIT_OFFSET (gnu_field));
6975       tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
6976                                         DECL_FIELD_OFFSET (gnu_field));
6977       unsigned int our_offset_align
6978         = MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
6979
6980       gnu_result
6981         = tree_cons (gnu_field,
6982                      tree_cons (gnu_our_offset,
6983                                 tree_cons (size_int (our_offset_align),
6984                                            gnu_our_bitpos, NULL_TREE),
6985                                 NULL_TREE),
6986                      gnu_result);
6987
6988       if (DECL_INTERNAL_P (gnu_field))
6989         gnu_result
6990           = compute_field_positions (TREE_TYPE (gnu_field), gnu_result,
6991                                      gnu_our_offset, gnu_our_bitpos,
6992                                      our_offset_align);
6993     }
6994
6995   return gnu_result;
6996 }
6997 \f
6998 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
6999    corresponding to GNAT_OBJECT.  If size is valid, return a tree corresponding
7000    to its value.  Otherwise return 0.  KIND is VAR_DECL is we are specifying
7001    the size for an object, TYPE_DECL for the size of a type, and FIELD_DECL
7002    for the size of a field.  COMPONENT_P is true if we are being called
7003    to process the Component_Size of GNAT_OBJECT.  This is used for error
7004    message handling and to indicate to use the object size of GNU_TYPE.
7005    ZERO_OK is true if a size of zero is permitted; if ZERO_OK is false,
7006    it means that a size of zero should be treated as an unspecified size.  */
7007
7008 static tree
7009 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7010                enum tree_code kind, bool component_p, bool zero_ok)
7011 {
7012   Node_Id gnat_error_node;
7013   tree type_size, size;
7014
7015   if (kind == VAR_DECL
7016       /* If a type needs strict alignment, a component of this type in
7017          a packed record cannot be packed and thus uses the type size.  */
7018       || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7019     type_size = TYPE_SIZE (gnu_type);
7020   else
7021     type_size = rm_size (gnu_type);
7022
7023   /* Find the node to use for errors.  */
7024   if ((Ekind (gnat_object) == E_Component
7025        || Ekind (gnat_object) == E_Discriminant)
7026       && Present (Component_Clause (gnat_object)))
7027     gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7028   else if (Present (Size_Clause (gnat_object)))
7029     gnat_error_node = Expression (Size_Clause (gnat_object));
7030   else
7031     gnat_error_node = gnat_object;
7032
7033   /* Return 0 if no size was specified, either because Esize was not Present or
7034      the specified size was zero.  */
7035   if (No (uint_size) || uint_size == No_Uint)
7036     return NULL_TREE;
7037
7038   /* Get the size as a tree.  Give an error if a size was specified, but cannot
7039      be represented as in sizetype.  */
7040   size = UI_To_gnu (uint_size, bitsizetype);
7041   if (TREE_OVERFLOW (size))
7042     {
7043       post_error_ne (component_p ? "component size of & is too large"
7044                      : "size of & is too large",
7045                      gnat_error_node, gnat_object);
7046       return NULL_TREE;
7047     }
7048
7049   /* Ignore a negative size since that corresponds to our back-annotation.
7050      Also ignore a zero size unless a size clause exists.  */
7051   else if (tree_int_cst_sgn (size) < 0 || (integer_zerop (size) && !zero_ok))
7052     return NULL_TREE;
7053
7054   /* The size of objects is always a multiple of a byte.  */
7055   if (kind == VAR_DECL
7056       && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7057     {
7058       if (component_p)
7059         post_error_ne ("component size for& is not a multiple of Storage_Unit",
7060                        gnat_error_node, gnat_object);
7061       else
7062         post_error_ne ("size for& is not a multiple of Storage_Unit",
7063                        gnat_error_node, gnat_object);
7064       return NULL_TREE;
7065     }
7066
7067   /* If this is an integral type or a packed array type, the front-end has
7068      verified the size, so we need not do it here (which would entail
7069      checking against the bounds).  However, if this is an aliased object, it
7070      may not be smaller than the type of the object.  */
7071   if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7072       && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7073     return size;
7074
7075   /* If the object is a record that contains a template, add the size of
7076      the template to the specified size.  */
7077   if (TREE_CODE (gnu_type) == RECORD_TYPE
7078       && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7079     size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7080
7081   /* Modify the size of the type to be that of the maximum size if it has a
7082      discriminant.  */
7083   if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7084     type_size = max_size (type_size, true);
7085
7086   /* If this is an access type or a fat pointer, the minimum size is that given
7087      by the smallest integral mode that's valid for pointers.  */
7088   if ((TREE_CODE (gnu_type) == POINTER_TYPE) || TYPE_FAT_POINTER_P (gnu_type))
7089     {
7090       enum machine_mode p_mode;
7091
7092       for (p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7093            !targetm.valid_pointer_mode (p_mode);
7094            p_mode = GET_MODE_WIDER_MODE (p_mode))
7095         ;
7096
7097       type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7098     }
7099
7100   /* If the size of the object is a constant, the new size must not be
7101      smaller.  */
7102   if (TREE_CODE (type_size) != INTEGER_CST
7103       || TREE_OVERFLOW (type_size)
7104       || tree_int_cst_lt (size, type_size))
7105     {
7106       if (component_p)
7107         post_error_ne_tree
7108           ("component size for& too small{, minimum allowed is ^}",
7109            gnat_error_node, gnat_object, type_size);
7110       else
7111         post_error_ne_tree ("size for& too small{, minimum allowed is ^}",
7112                             gnat_error_node, gnat_object, type_size);
7113
7114       if (kind == VAR_DECL && !component_p
7115           && TREE_CODE (rm_size (gnu_type)) == INTEGER_CST
7116           && !tree_int_cst_lt (size, rm_size (gnu_type)))
7117         post_error_ne_tree_2
7118           ("\\size of ^ is not a multiple of alignment (^ bits)",
7119            gnat_error_node, gnat_object, rm_size (gnu_type),
7120            TYPE_ALIGN (gnu_type));
7121
7122       else if (INTEGRAL_TYPE_P (gnu_type))
7123         post_error_ne ("\\size would be legal if & were not aliased!",
7124                        gnat_error_node, gnat_object);
7125
7126       return NULL_TREE;
7127     }
7128
7129   return size;
7130 }
7131 \f
7132 /* Similarly, but both validate and process a value of RM_Size.  This
7133    routine is only called for types.  */
7134
7135 static void
7136 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7137 {
7138   /* Only give an error if a Value_Size clause was explicitly given.
7139      Otherwise, we'd be duplicating an error on the Size clause.  */
7140   Node_Id gnat_attr_node
7141     = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7142   tree old_size = rm_size (gnu_type);
7143   tree size;
7144
7145   /* Get the size as a tree.  Do nothing if none was specified, either
7146      because RM_Size was not Present or if the specified size was zero.
7147      Give an error if a size was specified, but cannot be represented as
7148      in sizetype.  */
7149   if (No (uint_size) || uint_size == No_Uint)
7150     return;
7151
7152   size = UI_To_gnu (uint_size, bitsizetype);
7153   if (TREE_OVERFLOW (size))
7154     {
7155       if (Present (gnat_attr_node))
7156         post_error_ne ("Value_Size of & is too large", gnat_attr_node,
7157                        gnat_entity);
7158
7159       return;
7160     }
7161
7162   /* Ignore a negative size since that corresponds to our back-annotation.
7163      Also ignore a zero size unless a size clause exists, a Value_Size
7164      clause exists, or this is an integer type, in which case the
7165      front end will have always set it.  */
7166   else if (tree_int_cst_sgn (size) < 0
7167            || (integer_zerop (size) && No (gnat_attr_node)
7168                && !Has_Size_Clause (gnat_entity)
7169                && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity)))
7170     return;
7171
7172   /* If the old size is self-referential, get the maximum size.  */
7173   if (CONTAINS_PLACEHOLDER_P (old_size))
7174     old_size = max_size (old_size, true);
7175
7176   /* If the size of the object is a constant, the new size must not be
7177      smaller (the front end checks this for scalar types).  */
7178   if (TREE_CODE (old_size) != INTEGER_CST
7179       || TREE_OVERFLOW (old_size)
7180       || (AGGREGATE_TYPE_P (gnu_type)
7181           && tree_int_cst_lt (size, old_size)))
7182     {
7183       if (Present (gnat_attr_node))
7184         post_error_ne_tree
7185           ("Value_Size for& too small{, minimum allowed is ^}",
7186            gnat_attr_node, gnat_entity, old_size);
7187
7188       return;
7189     }
7190
7191   /* Otherwise, set the RM_Size.  */
7192   if (TREE_CODE (gnu_type) == INTEGER_TYPE
7193       && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7194     TYPE_RM_SIZE_NUM (gnu_type) = size;
7195   else if (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7196            || TREE_CODE (gnu_type) == BOOLEAN_TYPE)
7197     TYPE_RM_SIZE_NUM (gnu_type) = size;
7198   else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7199             || TREE_CODE (gnu_type) == UNION_TYPE
7200             || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7201            && !TYPE_IS_FAT_POINTER_P (gnu_type))
7202     SET_TYPE_ADA_SIZE (gnu_type, size);
7203 }
7204 \f
7205 /* Given a type TYPE, return a new type whose size is appropriate for SIZE.
7206    If TYPE is the best type, return it.  Otherwise, make a new type.  We
7207    only support new integral and pointer types.  FOR_BIASED is nonzero if
7208    we are making a biased type.  */
7209
7210 static tree
7211 make_type_from_size (tree type, tree size_tree, bool for_biased)
7212 {
7213   unsigned HOST_WIDE_INT size;
7214   bool biased_p;
7215   tree new_type;
7216
7217   /* If size indicates an error, just return TYPE to avoid propagating
7218      the error.  Likewise if it's too large to represent.  */
7219   if (!size_tree || !host_integerp (size_tree, 1))
7220     return type;
7221
7222   size = tree_low_cst (size_tree, 1);
7223
7224   switch (TREE_CODE (type))
7225     {
7226     case INTEGER_TYPE:
7227     case ENUMERAL_TYPE:
7228     case BOOLEAN_TYPE:
7229       biased_p = (TREE_CODE (type) == INTEGER_TYPE
7230                   && TYPE_BIASED_REPRESENTATION_P (type));
7231
7232       /* Only do something if the type is not a packed array type and
7233          doesn't already have the proper size.  */
7234       if (TYPE_PACKED_ARRAY_TYPE_P (type)
7235           || (TYPE_PRECISION (type) == size && biased_p == for_biased))
7236         break;
7237
7238       biased_p |= for_biased;
7239       size = MIN (size, LONG_LONG_TYPE_SIZE);
7240
7241       if (TYPE_UNSIGNED (type) || biased_p)
7242         new_type = make_unsigned_type (size);
7243       else
7244         new_type = make_signed_type (size);
7245       TREE_TYPE (new_type) = TREE_TYPE (type) ? TREE_TYPE (type) : type;
7246       TYPE_MIN_VALUE (new_type)
7247         = convert (TREE_TYPE (new_type), TYPE_MIN_VALUE (type));
7248       TYPE_MAX_VALUE (new_type)
7249         = convert (TREE_TYPE (new_type), TYPE_MAX_VALUE (type));
7250       /* Propagate the name to avoid creating a fake subrange type.  */
7251       if (TYPE_NAME (type))
7252         {
7253           if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
7254             TYPE_NAME (new_type) = DECL_NAME (TYPE_NAME (type));
7255           else
7256             TYPE_NAME (new_type) = TYPE_NAME (type);
7257         }
7258       TYPE_BIASED_REPRESENTATION_P (new_type) = biased_p;
7259       TYPE_RM_SIZE_NUM (new_type) = bitsize_int (size);
7260       return new_type;
7261
7262     case RECORD_TYPE:
7263       /* Do something if this is a fat pointer, in which case we
7264          may need to return the thin pointer.  */
7265       if (TYPE_IS_FAT_POINTER_P (type) && size < POINTER_SIZE * 2)
7266         {
7267           enum machine_mode p_mode = mode_for_size (size, MODE_INT, 0);
7268           if (!targetm.valid_pointer_mode (p_mode))
7269             p_mode = ptr_mode;
7270           return
7271             build_pointer_type_for_mode
7272               (TYPE_OBJECT_RECORD_TYPE (TYPE_UNCONSTRAINED_ARRAY (type)),
7273                p_mode, 0);
7274         }
7275       break;
7276
7277     case POINTER_TYPE:
7278       /* Only do something if this is a thin pointer, in which case we
7279          may need to return the fat pointer.  */
7280       if (TYPE_THIN_POINTER_P (type) && size >= POINTER_SIZE * 2)
7281         return
7282           build_pointer_type (TYPE_UNCONSTRAINED_ARRAY (TREE_TYPE (type)));
7283       break;
7284
7285     default:
7286       break;
7287     }
7288
7289   return type;
7290 }
7291 \f
7292 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7293    a type or object whose present alignment is ALIGN.  If this alignment is
7294    valid, return it.  Otherwise, give an error and return ALIGN.  */
7295
7296 static unsigned int
7297 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7298 {
7299   unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7300   unsigned int new_align;
7301   Node_Id gnat_error_node;
7302
7303   /* Don't worry about checking alignment if alignment was not specified
7304      by the source program and we already posted an error for this entity.  */
7305   if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7306     return align;
7307
7308   /* Post the error on the alignment clause if any.  */
7309   if (Present (Alignment_Clause (gnat_entity)))
7310     gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7311   else
7312     gnat_error_node = gnat_entity;
7313
7314   /* Within GCC, an alignment is an integer, so we must make sure a value is
7315      specified that fits in that range.  Also, there is an upper bound to
7316      alignments we can support/allow.  */
7317   if (!UI_Is_In_Int_Range (alignment)
7318       || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7319     post_error_ne_num ("largest supported alignment for& is ^",
7320                        gnat_error_node, gnat_entity, max_allowed_alignment);
7321   else if (!(Present (Alignment_Clause (gnat_entity))
7322              && From_At_Mod (Alignment_Clause (gnat_entity)))
7323            && new_align * BITS_PER_UNIT < align)
7324     post_error_ne_num ("alignment for& must be at least ^",
7325                        gnat_error_node, gnat_entity,
7326                        align / BITS_PER_UNIT);
7327   else
7328     {
7329       new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7330       if (new_align > align)
7331         align = new_align;
7332     }
7333
7334   return align;
7335 }
7336
7337 /* Return the smallest alignment not less than SIZE.  */
7338
7339 static unsigned int
7340 ceil_alignment (unsigned HOST_WIDE_INT size)
7341 {
7342   return (unsigned int) 1 << (floor_log2 (size - 1) + 1);
7343 }
7344 \f
7345 /* Verify that OBJECT, a type or decl, is something we can implement
7346    atomically.  If not, give an error for GNAT_ENTITY.  COMP_P is true
7347    if we require atomic components.  */
7348
7349 static void
7350 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7351 {
7352   Node_Id gnat_error_point = gnat_entity;
7353   Node_Id gnat_node;
7354   enum machine_mode mode;
7355   unsigned int align;
7356   tree size;
7357
7358   /* There are three case of what OBJECT can be.  It can be a type, in which
7359      case we take the size, alignment and mode from the type.  It can be a
7360      declaration that was indirect, in which case the relevant values are
7361      that of the type being pointed to, or it can be a normal declaration,
7362      in which case the values are of the decl.  The code below assumes that
7363      OBJECT is either a type or a decl.  */
7364   if (TYPE_P (object))
7365     {
7366       mode = TYPE_MODE (object);
7367       align = TYPE_ALIGN (object);
7368       size = TYPE_SIZE (object);
7369     }
7370   else if (DECL_BY_REF_P (object))
7371     {
7372       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
7373       align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
7374       size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
7375     }
7376   else
7377     {
7378       mode = DECL_MODE (object);
7379       align = DECL_ALIGN (object);
7380       size = DECL_SIZE (object);
7381     }
7382
7383   /* Consider all floating-point types atomic and any types that that are
7384      represented by integers no wider than a machine word.  */
7385   if (GET_MODE_CLASS (mode) == MODE_FLOAT
7386       || ((GET_MODE_CLASS (mode) == MODE_INT
7387            || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
7388           && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
7389     return;
7390
7391   /* For the moment, also allow anything that has an alignment equal
7392      to its size and which is smaller than a word.  */
7393   if (size && TREE_CODE (size) == INTEGER_CST
7394       && compare_tree_int (size, align) == 0
7395       && align <= BITS_PER_WORD)
7396     return;
7397
7398   for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
7399        gnat_node = Next_Rep_Item (gnat_node))
7400     {
7401       if (!comp_p && Nkind (gnat_node) == N_Pragma
7402           && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7403               == Pragma_Atomic))
7404         gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7405       else if (comp_p && Nkind (gnat_node) == N_Pragma
7406                && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
7407                    == Pragma_Atomic_Components))
7408         gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
7409     }
7410
7411   if (comp_p)
7412     post_error_ne ("atomic access to component of & cannot be guaranteed",
7413                    gnat_error_point, gnat_entity);
7414   else
7415     post_error_ne ("atomic access to & cannot be guaranteed",
7416                    gnat_error_point, gnat_entity);
7417 }
7418 \f
7419 /* Check if FTYPE1 and FTYPE2, two potentially different function type nodes,
7420    have compatible signatures so that a call using one type may be safely
7421    issued if the actual target function type is the other.  Return 1 if it is
7422    the case, 0 otherwise, and post errors on the incompatibilities.
7423
7424    This is used when an Ada subprogram is mapped onto a GCC builtin, to ensure
7425    that calls to the subprogram will have arguments suitable for the later
7426    underlying builtin expansion.  */
7427
7428 static int
7429 compatible_signatures_p (tree ftype1, tree ftype2)
7430 {
7431   /* As of now, we only perform very trivial tests and consider it's the
7432      programmer's responsibility to ensure the type correctness in the Ada
7433      declaration, as in the regular Import cases.
7434
7435      Mismatches typically result in either error messages from the builtin
7436      expander, internal compiler errors, or in a real call sequence.  This
7437      should be refined to issue diagnostics helping error detection and
7438      correction.  */
7439
7440   /* Almost fake test, ensuring a use of each argument.  */
7441   if (ftype1 == ftype2)
7442     return 1;
7443
7444   return 1;
7445 }
7446 \f
7447 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a new
7448    type with all size expressions that contain F updated by replacing F
7449    with R.  If F is NULL_TREE, always make a new RECORD_TYPE, even if
7450    nothing has changed.  */
7451
7452 tree
7453 substitute_in_type (tree t, tree f, tree r)
7454 {
7455   tree new = t;
7456   tree tem;
7457
7458   switch (TREE_CODE (t))
7459     {
7460     case INTEGER_TYPE:
7461     case ENUMERAL_TYPE:
7462     case BOOLEAN_TYPE:
7463       if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7464           || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7465         {
7466           tree low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7467           tree high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7468
7469           if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7470             return t;
7471
7472           new = build_range_type (TREE_TYPE (t), low, high);
7473           if (TYPE_INDEX_TYPE (t))
7474             SET_TYPE_INDEX_TYPE
7475               (new, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
7476           return new;
7477         }
7478
7479       return t;
7480
7481     case REAL_TYPE:
7482       if (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (t))
7483           || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (t)))
7484         {
7485           tree low = NULL_TREE, high = NULL_TREE;
7486
7487           if (TYPE_MIN_VALUE (t))
7488             low = SUBSTITUTE_IN_EXPR (TYPE_MIN_VALUE (t), f, r);
7489           if (TYPE_MAX_VALUE (t))
7490             high = SUBSTITUTE_IN_EXPR (TYPE_MAX_VALUE (t), f, r);
7491
7492           if (low == TYPE_MIN_VALUE (t) && high == TYPE_MAX_VALUE (t))
7493             return t;
7494
7495           t = copy_type (t);
7496           TYPE_MIN_VALUE (t) = low;
7497           TYPE_MAX_VALUE (t) = high;
7498         }
7499       return t;
7500
7501     case COMPLEX_TYPE:
7502       tem = substitute_in_type (TREE_TYPE (t), f, r);
7503       if (tem == TREE_TYPE (t))
7504         return t;
7505
7506       return build_complex_type (tem);
7507
7508     case OFFSET_TYPE:
7509     case METHOD_TYPE:
7510     case FUNCTION_TYPE:
7511     case LANG_TYPE:
7512       /* Don't know how to do these yet.  */
7513       gcc_unreachable ();
7514
7515     case ARRAY_TYPE:
7516       {
7517         tree component = substitute_in_type (TREE_TYPE (t), f, r);
7518         tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
7519
7520         if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7521           return t;
7522
7523         new = build_array_type (component, domain);
7524         TYPE_SIZE (new) = 0;
7525         TYPE_NONALIASED_COMPONENT (new) = TYPE_NONALIASED_COMPONENT (t);
7526         TYPE_MULTI_ARRAY_P (new) = TYPE_MULTI_ARRAY_P (t);
7527         TYPE_CONVENTION_FORTRAN_P (new) = TYPE_CONVENTION_FORTRAN_P (t);
7528         layout_type (new);
7529         TYPE_ALIGN (new) = TYPE_ALIGN (t);
7530         TYPE_USER_ALIGN (new) = TYPE_USER_ALIGN (t);
7531
7532         /* If we had bounded the sizes of T by a constant, bound the sizes of
7533            NEW by the same constant.  */
7534         if (TREE_CODE (TYPE_SIZE (t)) == MIN_EXPR)
7535           TYPE_SIZE (new)
7536             = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE (t), 1),
7537                           TYPE_SIZE (new));
7538         if (TREE_CODE (TYPE_SIZE_UNIT (t)) == MIN_EXPR)
7539           TYPE_SIZE_UNIT (new)
7540             = size_binop (MIN_EXPR, TREE_OPERAND (TYPE_SIZE_UNIT (t), 1),
7541                           TYPE_SIZE_UNIT (new));
7542         return new;
7543       }
7544
7545     case RECORD_TYPE:
7546     case UNION_TYPE:
7547     case QUAL_UNION_TYPE:
7548       {
7549         tree field;
7550         bool changed_field
7551           = (f == NULL_TREE && !TREE_CONSTANT (TYPE_SIZE (t)));
7552         bool field_has_rep = false;
7553         tree last_field = NULL_TREE;
7554
7555         tree new = copy_type (t);
7556
7557         /* Start out with no fields, make new fields, and chain them
7558            in.  If we haven't actually changed the type of any field,
7559            discard everything we've done and return the old type.  */
7560
7561         TYPE_FIELDS (new) = NULL_TREE;
7562         TYPE_SIZE (new) = NULL_TREE;
7563
7564         for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7565           {
7566             tree new_field = copy_node (field);
7567
7568             TREE_TYPE (new_field)
7569               = substitute_in_type (TREE_TYPE (new_field), f, r);
7570
7571             if (DECL_HAS_REP_P (field) && !DECL_INTERNAL_P (field))
7572               field_has_rep = true;
7573             else if (TREE_TYPE (new_field) != TREE_TYPE (field))
7574               changed_field = true;
7575
7576             /* If this is an internal field and the type of this field is
7577                a UNION_TYPE or RECORD_TYPE with no elements, ignore it.  If
7578                the type just has one element, treat that as the field.
7579                But don't do this if we are processing a QUAL_UNION_TYPE.  */
7580             if (TREE_CODE (t) != QUAL_UNION_TYPE
7581                 && DECL_INTERNAL_P (new_field)
7582                 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
7583                     || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
7584               {
7585                 if (!TYPE_FIELDS (TREE_TYPE (new_field)))
7586                   continue;
7587
7588                 if (!TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))))
7589                   {
7590                     tree next_new_field
7591                       = copy_node (TYPE_FIELDS (TREE_TYPE (new_field)));
7592
7593                     /* Make sure omitting the union doesn't change
7594                        the layout.  */
7595                     DECL_ALIGN (next_new_field) = DECL_ALIGN (new_field);
7596                     new_field = next_new_field;
7597                   }
7598               }
7599
7600             DECL_CONTEXT (new_field) = new;
7601             SET_DECL_ORIGINAL_FIELD (new_field,
7602                                      (DECL_ORIGINAL_FIELD (field)
7603                                       ? DECL_ORIGINAL_FIELD (field) : field));
7604
7605             /* If the size of the old field was set at a constant,
7606                propagate the size in case the type's size was variable.
7607                (This occurs in the case of a variant or discriminated
7608                record with a default size used as a field of another
7609                record.)  */
7610             DECL_SIZE (new_field)
7611               = TREE_CODE (DECL_SIZE (field)) == INTEGER_CST
7612                 ? DECL_SIZE (field) : NULL_TREE;
7613             DECL_SIZE_UNIT (new_field)
7614               = TREE_CODE (DECL_SIZE_UNIT (field)) == INTEGER_CST
7615                 ? DECL_SIZE_UNIT (field) : NULL_TREE;
7616
7617             if (TREE_CODE (t) == QUAL_UNION_TYPE)
7618               {
7619                 tree new_q = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
7620
7621                 if (new_q != DECL_QUALIFIER (new_field))
7622                   changed_field = true;
7623
7624                 /* Do the substitution inside the qualifier and if we find
7625                    that this field will not be present, omit it.  */
7626                 DECL_QUALIFIER (new_field) = new_q;
7627
7628                 if (integer_zerop (DECL_QUALIFIER (new_field)))
7629                   continue;
7630               }
7631
7632             if (!last_field)
7633               TYPE_FIELDS (new) = new_field;
7634             else
7635               TREE_CHAIN (last_field) = new_field;
7636
7637             last_field = new_field;
7638
7639             /* If this is a qualified type and this field will always be
7640                present, we are done.  */
7641             if (TREE_CODE (t) == QUAL_UNION_TYPE
7642                 && integer_onep (DECL_QUALIFIER (new_field)))
7643               break;
7644           }
7645
7646         /* If this used to be a qualified union type, but we now know what
7647            field will be present, make this a normal union.  */
7648         if (changed_field && TREE_CODE (new) == QUAL_UNION_TYPE
7649             && (!TYPE_FIELDS (new)
7650                 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
7651           TREE_SET_CODE (new, UNION_TYPE);
7652         else if (!changed_field)
7653           return t;
7654
7655         gcc_assert (!field_has_rep);
7656         layout_type (new);
7657
7658         /* If the size was originally a constant use it.  */
7659         if (TYPE_SIZE (t) && TREE_CODE (TYPE_SIZE (t)) == INTEGER_CST
7660             && TREE_CODE (TYPE_SIZE (new)) != INTEGER_CST)
7661           {
7662             TYPE_SIZE (new) = TYPE_SIZE (t);
7663             TYPE_SIZE_UNIT (new) = TYPE_SIZE_UNIT (t);
7664             SET_TYPE_ADA_SIZE (new, TYPE_ADA_SIZE (t));
7665           }
7666
7667         return new;
7668       }
7669
7670     default:
7671       return t;
7672     }
7673 }
7674 \f
7675 /* Return the "RM size" of GNU_TYPE.  This is the actual number of bits
7676    needed to represent the object.  */
7677
7678 tree
7679 rm_size (tree gnu_type)
7680 {
7681   /* For integer types, this is the precision.  For record types, we store
7682      the size explicitly.  For other types, this is just the size.  */
7683
7684   if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
7685     return TYPE_RM_SIZE (gnu_type);
7686   else if (TREE_CODE (gnu_type) == RECORD_TYPE
7687            && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7688     /* Return the rm_size of the actual data plus the size of the template.  */
7689     return
7690       size_binop (PLUS_EXPR,
7691                   rm_size (TREE_TYPE (TREE_CHAIN (TYPE_FIELDS (gnu_type)))),
7692                   DECL_SIZE (TYPE_FIELDS (gnu_type)));
7693   else if ((TREE_CODE (gnu_type) == RECORD_TYPE
7694             || TREE_CODE (gnu_type) == UNION_TYPE
7695             || TREE_CODE (gnu_type) == QUAL_UNION_TYPE)
7696            && !TYPE_IS_FAT_POINTER_P (gnu_type)
7697            && TYPE_ADA_SIZE (gnu_type))
7698     return TYPE_ADA_SIZE (gnu_type);
7699   else
7700     return TYPE_SIZE (gnu_type);
7701 }
7702 \f
7703 /* Return an identifier representing the external name to be used for
7704    GNAT_ENTITY.  If SUFFIX is specified, the name is followed by "___"
7705    and the specified suffix.  */
7706
7707 tree
7708 create_concat_name (Entity_Id gnat_entity, const char *suffix)
7709 {
7710   Entity_Kind kind = Ekind (gnat_entity);
7711
7712   const char *str = (!suffix ? "" : suffix);
7713   String_Template temp = {1, strlen (str)};
7714   Fat_Pointer fp = {str, &temp};
7715
7716   Get_External_Name_With_Suffix (gnat_entity, fp);
7717
7718   /* A variable using the Stdcall convention (meaning we are running
7719      on a Windows box) live in a DLL.  Here we adjust its name to use
7720      the jump-table, the _imp__NAME contains the address for the NAME
7721      variable.  */
7722   if ((kind == E_Variable || kind == E_Constant)
7723       && Has_Stdcall_Convention (gnat_entity))
7724     {
7725       const char *prefix = "_imp__";
7726       int k, plen = strlen (prefix);
7727
7728       for (k = 0; k <= Name_Len; k++)
7729         Name_Buffer [Name_Len - k + plen] = Name_Buffer [Name_Len - k];
7730       strncpy (Name_Buffer, prefix, plen);
7731     }
7732
7733   return get_identifier (Name_Buffer);
7734 }
7735
7736 /* Return the name to be used for GNAT_ENTITY.  If a type, create a
7737    fully-qualified name, possibly with type information encoding.
7738    Otherwise, return the name.  */
7739
7740 tree
7741 get_entity_name (Entity_Id gnat_entity)
7742 {
7743   Get_Encoded_Name (gnat_entity);
7744   return get_identifier (Name_Buffer);
7745 }
7746
7747 /* Given GNU_ID, an IDENTIFIER_NODE containing a name and SUFFIX, a
7748    string, return a new IDENTIFIER_NODE that is the concatenation of
7749    the name in GNU_ID and SUFFIX.  */
7750
7751 tree
7752 concat_id_with_name (tree gnu_id, const char *suffix)
7753 {
7754   int len = IDENTIFIER_LENGTH (gnu_id);
7755
7756   strncpy (Name_Buffer, IDENTIFIER_POINTER (gnu_id), len);
7757   strncpy (Name_Buffer + len, "___", 3);
7758   len += 3;
7759   strcpy (Name_Buffer + len, suffix);
7760   return get_identifier (Name_Buffer);
7761 }
7762
7763 #include "gt-ada-decl.h"