OSDN Git Service

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