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