OSDN Git Service

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