OSDN Git Service

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