OSDN Git Service

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