OSDN Git Service

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