OSDN Git Service

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