OSDN Git Service

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