OSDN Git Service

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