OSDN Git Service

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