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