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