OSDN Git Service

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