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);
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);
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;
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 
5725   /* Pass In Out or Out parameters using copy-in copy-out mechanism.  */
5726   else if (!in_param)
5727     *cico = true;
5728 
5729   if (mech == By_Copy && (by_ref || by_component_ptr))
5730     post_error ("?cannot pass & by copy", gnat_param);
5731 
5732   /* If this is an Out parameter that isn't passed by reference and isn't
5733      a pointer or aggregate, we don't make a PARM_DECL for it.  Instead,
5734      it will be a VAR_DECL created when we process the procedure, so just
5735      return its type.  For the special parameter of a valued procedure,
5736      never pass it in.
5737 
5738      An exception is made to cover the RM-6.4.1 rule requiring "by copy"
5739      Out parameters with discriminants or implicit initial values to be
5740      handled like In Out parameters.  These type are normally built as
5741      aggregates, hence passed by reference, except for some packed arrays
5742      which end up encoded in special integer types.
5743 
5744      The exception we need to make is then for packed arrays of records
5745      with discriminants or implicit initial values.  We have no light/easy
5746      way to check for the latter case, so we merely check for packed arrays
5747      of records.  This may lead to useless copy-in operations, but in very
5748      rare cases only, as these would be exceptions in a set of already
5749      exceptional situations.  */
5750   if (Ekind (gnat_param) == E_Out_Parameter
5751       && !by_ref
5752       && (by_return
5753 	  || (mech != By_Descriptor
5754               && mech != By_Short_Descriptor
5755 	      && !POINTER_TYPE_P (gnu_param_type)
5756 	      && !AGGREGATE_TYPE_P (gnu_param_type)))
5757       && !(Is_Array_Type (Etype (gnat_param))
5758 	   && Is_Packed (Etype (gnat_param))
5759 	   && Is_Composite_Type (Component_Type (Etype (gnat_param)))))
5760     return gnu_param_type;
5761 
5762   gnu_param = create_param_decl (gnu_param_name, gnu_param_type,
5763 				 ro_param || by_ref || by_component_ptr);
5764   DECL_BY_REF_P (gnu_param) = by_ref;
5765   DECL_BY_COMPONENT_PTR_P (gnu_param) = by_component_ptr;
5766   DECL_BY_DESCRIPTOR_P (gnu_param)
5767     = (mech == By_Descriptor || mech == By_Short_Descriptor);
5768   DECL_POINTS_TO_READONLY_P (gnu_param)
5769     = (ro_param && (by_ref || by_component_ptr));
5770   DECL_CAN_NEVER_BE_NULL_P (gnu_param) = Can_Never_Be_Null (gnat_param);
5771 
5772   /* Save the alternate descriptor type, if any.  */
5773   if (gnu_param_type_alt)
5774     SET_DECL_PARM_ALT_TYPE (gnu_param, gnu_param_type_alt);
5775 
5776   /* If no Mechanism was specified, indicate what we're using, then
5777      back-annotate it.  */
5778   if (mech == Default)
5779     mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
5780 
5781   Set_Mechanism (gnat_param, mech);
5782   return gnu_param;
5783 }
5784 
5785 /* Return true if DISCR1 and DISCR2 represent the same discriminant.  */
5786 
5787 static bool
same_discriminant_p(Entity_Id discr1,Entity_Id discr2)5788 same_discriminant_p (Entity_Id discr1, Entity_Id discr2)
5789 {
5790   while (Present (Corresponding_Discriminant (discr1)))
5791     discr1 = Corresponding_Discriminant (discr1);
5792 
5793   while (Present (Corresponding_Discriminant (discr2)))
5794     discr2 = Corresponding_Discriminant (discr2);
5795 
5796   return
5797     Original_Record_Component (discr1) == Original_Record_Component (discr2);
5798 }
5799 
5800 /* Return true if the array type GNU_TYPE, which represents a dimension of
5801    GNAT_TYPE, has a non-aliased component in the back-end sense.  */
5802 
5803 static bool
array_type_has_nonaliased_component(tree gnu_type,Entity_Id gnat_type)5804 array_type_has_nonaliased_component (tree gnu_type, Entity_Id gnat_type)
5805 {
5806   /* If the array type is not the innermost dimension of the GNAT type,
5807      then it has a non-aliased component.  */
5808   if (TREE_CODE (TREE_TYPE (gnu_type)) == ARRAY_TYPE
5809       && TYPE_MULTI_ARRAY_P (TREE_TYPE (gnu_type)))
5810     return true;
5811 
5812   /* If the array type has an aliased component in the front-end sense,
5813      then it also has an aliased component in the back-end sense.  */
5814   if (Has_Aliased_Components (gnat_type))
5815     return false;
5816 
5817   /* If this is a derived type, then it has a non-aliased component if
5818      and only if its parent type also has one.  */
5819   if (Is_Derived_Type (gnat_type))
5820     {
5821       tree gnu_parent_type = gnat_to_gnu_type (Etype (gnat_type));
5822       int index;
5823       if (TREE_CODE (gnu_parent_type) == UNCONSTRAINED_ARRAY_TYPE)
5824 	gnu_parent_type
5825 	  = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_TYPE (gnu_parent_type))));
5826       for (index = Number_Dimensions (gnat_type) - 1; index > 0; index--)
5827 	gnu_parent_type = TREE_TYPE (gnu_parent_type);
5828       return TYPE_NONALIASED_COMPONENT (gnu_parent_type);
5829     }
5830 
5831   /* Otherwise, rely exclusively on properties of the element type.  */
5832   return type_for_nonaliased_component_p (TREE_TYPE (gnu_type));
5833 }
5834 
5835 /* Return true if GNAT_ADDRESS is a value known at compile-time.  */
5836 
5837 static bool
compile_time_known_address_p(Node_Id gnat_address)5838 compile_time_known_address_p (Node_Id gnat_address)
5839 {
5840   /* Catch System'To_Address.  */
5841   if (Nkind (gnat_address) == N_Unchecked_Type_Conversion)
5842     gnat_address = Expression (gnat_address);
5843 
5844   return Compile_Time_Known_Value (gnat_address);
5845 }
5846 
5847 /* Return true if GNAT_RANGE, a N_Range node, cannot be superflat, i.e. if the
5848    inequality HB >= LB-1 is true.  LB and HB are the low and high bounds.  */
5849 
5850 static bool
cannot_be_superflat_p(Node_Id gnat_range)5851 cannot_be_superflat_p (Node_Id gnat_range)
5852 {
5853   Node_Id gnat_lb = Low_Bound (gnat_range), gnat_hb = High_Bound (gnat_range);
5854   Node_Id scalar_range;
5855   tree gnu_lb, gnu_hb, gnu_lb_minus_one;
5856 
5857   /* If the low bound is not constant, try to find an upper bound.  */
5858   while (Nkind (gnat_lb) != N_Integer_Literal
5859 	 && (Ekind (Etype (gnat_lb)) == E_Signed_Integer_Subtype
5860 	     || Ekind (Etype (gnat_lb)) == E_Modular_Integer_Subtype)
5861 	 && (scalar_range = Scalar_Range (Etype (gnat_lb)))
5862 	 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5863 	     || Nkind (scalar_range) == N_Range))
5864     gnat_lb = High_Bound (scalar_range);
5865 
5866   /* If the high bound is not constant, try to find a lower bound.  */
5867   while (Nkind (gnat_hb) != N_Integer_Literal
5868 	 && (Ekind (Etype (gnat_hb)) == E_Signed_Integer_Subtype
5869 	     || Ekind (Etype (gnat_hb)) == E_Modular_Integer_Subtype)
5870 	 && (scalar_range = Scalar_Range (Etype (gnat_hb)))
5871 	 && (Nkind (scalar_range) == N_Signed_Integer_Type_Definition
5872 	     || Nkind (scalar_range) == N_Range))
5873     gnat_hb = Low_Bound (scalar_range);
5874 
5875   /* If we have failed to find constant bounds, punt.  */
5876   if (Nkind (gnat_lb) != N_Integer_Literal
5877       || Nkind (gnat_hb) != N_Integer_Literal)
5878     return false;
5879 
5880   /* We need at least a signed 64-bit type to catch most cases.  */
5881   gnu_lb = UI_To_gnu (Intval (gnat_lb), sbitsizetype);
5882   gnu_hb = UI_To_gnu (Intval (gnat_hb), sbitsizetype);
5883   if (TREE_OVERFLOW (gnu_lb) || TREE_OVERFLOW (gnu_hb))
5884     return false;
5885 
5886   /* If the low bound is the smallest integer, nothing can be smaller.  */
5887   gnu_lb_minus_one = size_binop (MINUS_EXPR, gnu_lb, sbitsize_one_node);
5888   if (TREE_OVERFLOW (gnu_lb_minus_one))
5889     return true;
5890 
5891   return !tree_int_cst_lt (gnu_hb, gnu_lb_minus_one);
5892 }
5893 
5894 /* Return true if GNU_EXPR is (essentially) the address of a CONSTRUCTOR.  */
5895 
5896 static bool
constructor_address_p(tree gnu_expr)5897 constructor_address_p (tree gnu_expr)
5898 {
5899   while (TREE_CODE (gnu_expr) == NOP_EXPR
5900 	 || TREE_CODE (gnu_expr) == CONVERT_EXPR
5901 	 || TREE_CODE (gnu_expr) == NON_LVALUE_EXPR)
5902     gnu_expr = TREE_OPERAND (gnu_expr, 0);
5903 
5904   return (TREE_CODE (gnu_expr) == ADDR_EXPR
5905 	  && TREE_CODE (TREE_OPERAND (gnu_expr, 0)) == CONSTRUCTOR);
5906 }
5907 
5908 /* Given GNAT_ENTITY, elaborate all expressions that are required to
5909    be elaborated at the point of its definition, but do nothing else.  */
5910 
5911 void
elaborate_entity(Entity_Id gnat_entity)5912 elaborate_entity (Entity_Id gnat_entity)
5913 {
5914   switch (Ekind (gnat_entity))
5915     {
5916     case E_Signed_Integer_Subtype:
5917     case E_Modular_Integer_Subtype:
5918     case E_Enumeration_Subtype:
5919     case E_Ordinary_Fixed_Point_Subtype:
5920     case E_Decimal_Fixed_Point_Subtype:
5921     case E_Floating_Point_Subtype:
5922       {
5923 	Node_Id gnat_lb = Type_Low_Bound (gnat_entity);
5924 	Node_Id gnat_hb = Type_High_Bound (gnat_entity);
5925 
5926 	/* ??? Tests to avoid Constraint_Error in static expressions
5927 	   are needed until after the front stops generating bogus
5928 	   conversions on bounds of real types.  */
5929 	if (!Raises_Constraint_Error (gnat_lb))
5930 	  elaborate_expression (gnat_lb, gnat_entity, get_identifier ("L"),
5931 				true, false, Needs_Debug_Info (gnat_entity));
5932 	if (!Raises_Constraint_Error (gnat_hb))
5933 	  elaborate_expression (gnat_hb, gnat_entity, get_identifier ("U"),
5934 				true, false, Needs_Debug_Info (gnat_entity));
5935       break;
5936       }
5937 
5938     case E_Record_Subtype:
5939     case E_Private_Subtype:
5940     case E_Limited_Private_Subtype:
5941     case E_Record_Subtype_With_Private:
5942       if (Has_Discriminants (gnat_entity) && Is_Constrained (gnat_entity))
5943 	{
5944 	  Node_Id gnat_discriminant_expr;
5945 	  Entity_Id gnat_field;
5946 
5947 	  for (gnat_field
5948 	       = First_Discriminant (Implementation_Base_Type (gnat_entity)),
5949 	       gnat_discriminant_expr
5950 	       = First_Elmt (Discriminant_Constraint (gnat_entity));
5951 	       Present (gnat_field);
5952 	       gnat_field = Next_Discriminant (gnat_field),
5953 	       gnat_discriminant_expr = Next_Elmt (gnat_discriminant_expr))
5954 	    /* ??? For now, ignore access discriminants.  */
5955 	    if (!Is_Access_Type (Etype (Node (gnat_discriminant_expr))))
5956 	      elaborate_expression (Node (gnat_discriminant_expr),
5957 				    gnat_entity, get_entity_name (gnat_field),
5958 				    true, false, false);
5959 	}
5960       break;
5961 
5962     }
5963 }
5964 
5965 /* Return true if the size in units represented by GNU_SIZE can be handled by
5966    an allocation.  If STATIC_P is true, consider only what can be done with a
5967    static allocation.  */
5968 
5969 static bool
allocatable_size_p(tree gnu_size,bool static_p)5970 allocatable_size_p (tree gnu_size, bool static_p)
5971 {
5972   /* We can allocate a fixed size if it is a valid for the middle-end.  */
5973   if (TREE_CODE (gnu_size) == INTEGER_CST)
5974     return valid_constant_size_p (gnu_size);
5975 
5976   /* We can allocate a variable size if this isn't a static allocation.  */
5977   else
5978     return !static_p;
5979 }
5980 
5981 /* Prepend to ATTR_LIST an entry for an attribute with provided TYPE,
5982    NAME, ARGS and ERROR_POINT.  */
5983 
5984 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)5985 prepend_one_attribute_to (struct attrib ** attr_list,
5986 			  enum attr_type attr_type,
5987 			  tree attr_name,
5988 			  tree attr_args,
5989 			  Node_Id attr_error_point)
5990 {
5991   struct attrib * attr = (struct attrib *) xmalloc (sizeof (struct attrib));
5992 
5993   attr->type = attr_type;
5994   attr->name = attr_name;
5995   attr->args = attr_args;
5996   attr->error_point = attr_error_point;
5997 
5998   attr->next = *attr_list;
5999   *attr_list = attr;
6000 }
6001 
6002 /* Prepend to ATTR_LIST the list of attributes for GNAT_ENTITY, if any.  */
6003 
6004 static void
prepend_attributes(Entity_Id gnat_entity,struct attrib ** attr_list)6005 prepend_attributes (Entity_Id gnat_entity, struct attrib ** attr_list)
6006 {
6007   Node_Id gnat_temp;
6008 
6009   /* Attributes are stored as Representation Item pragmas.  */
6010 
6011   for (gnat_temp = First_Rep_Item (gnat_entity); Present (gnat_temp);
6012        gnat_temp = Next_Rep_Item (gnat_temp))
6013     if (Nkind (gnat_temp) == N_Pragma)
6014       {
6015 	tree gnu_arg0 = NULL_TREE, gnu_arg1 = NULL_TREE;
6016 	Node_Id gnat_assoc = Pragma_Argument_Associations (gnat_temp);
6017 	enum attr_type etype;
6018 
6019 	/* Map the kind of pragma at hand.  Skip if this is not one
6020 	   we know how to handle.  */
6021 
6022 	switch (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_temp))))
6023 	  {
6024 	  case Pragma_Machine_Attribute:
6025 	    etype = ATTR_MACHINE_ATTRIBUTE;
6026 	    break;
6027 
6028 	  case Pragma_Linker_Alias:
6029 	    etype = ATTR_LINK_ALIAS;
6030 	    break;
6031 
6032 	  case Pragma_Linker_Section:
6033 	    etype = ATTR_LINK_SECTION;
6034 	    break;
6035 
6036 	  case Pragma_Linker_Constructor:
6037 	    etype = ATTR_LINK_CONSTRUCTOR;
6038 	    break;
6039 
6040 	  case Pragma_Linker_Destructor:
6041 	    etype = ATTR_LINK_DESTRUCTOR;
6042 	    break;
6043 
6044 	  case Pragma_Weak_External:
6045 	    etype = ATTR_WEAK_EXTERNAL;
6046 	    break;
6047 
6048 	  case Pragma_Thread_Local_Storage:
6049 	    etype = ATTR_THREAD_LOCAL_STORAGE;
6050 	    break;
6051 
6052 	  default:
6053 	    continue;
6054 	  }
6055 
6056 	/* See what arguments we have and turn them into GCC trees for
6057 	   attribute handlers.  These expect identifier for strings.  We
6058 	   handle at most two arguments, static expressions only.  */
6059 
6060 	if (Present (gnat_assoc) && Present (First (gnat_assoc)))
6061 	  {
6062 	    Node_Id gnat_arg0 = Next (First (gnat_assoc));
6063 	    Node_Id gnat_arg1 = Empty;
6064 
6065 	    if (Present (gnat_arg0)
6066 		&& Is_Static_Expression (Expression (gnat_arg0)))
6067 	      {
6068 		gnu_arg0 = gnat_to_gnu (Expression (gnat_arg0));
6069 
6070 		if (TREE_CODE (gnu_arg0) == STRING_CST)
6071 		  gnu_arg0 = get_identifier (TREE_STRING_POINTER (gnu_arg0));
6072 
6073 		gnat_arg1 = Next (gnat_arg0);
6074 	      }
6075 
6076 	    if (Present (gnat_arg1)
6077 		&& Is_Static_Expression (Expression (gnat_arg1)))
6078 	      {
6079 		gnu_arg1 = gnat_to_gnu (Expression (gnat_arg1));
6080 
6081 		if (TREE_CODE (gnu_arg1) == STRING_CST)
6082 		  gnu_arg1 = get_identifier (TREE_STRING_POINTER (gnu_arg1));
6083 	      }
6084 	  }
6085 
6086 	/* Prepend to the list now.  Make a list of the argument we might
6087 	   have, as GCC expects it.  */
6088 	prepend_one_attribute_to
6089 	  (attr_list,
6090 	   etype, gnu_arg0,
6091 	   (gnu_arg1 != NULL_TREE)
6092 	   ? build_tree_list (NULL_TREE, gnu_arg1) : NULL_TREE,
6093 	   Present (Next (First (gnat_assoc)))
6094 	   ? Expression (Next (First (gnat_assoc))) : gnat_temp);
6095       }
6096 }
6097 
6098 /* Given a GNAT tree GNAT_EXPR, for an expression which is a value within a
6099    type definition (either a bound or a discriminant value) for GNAT_ENTITY,
6100    return the GCC tree to use for that expression.  GNU_NAME is the suffix
6101    to use if a variable needs to be created and DEFINITION is true if this
6102    is a definition of GNAT_ENTITY.  If NEED_VALUE is true, we need a result;
6103    otherwise, we are just elaborating the expression for side-effects.  If
6104    NEED_DEBUG is true, we need a variable for debugging purposes even if it
6105    isn't needed for code generation.  */
6106 
6107 static tree
elaborate_expression(Node_Id gnat_expr,Entity_Id gnat_entity,tree gnu_name,bool definition,bool need_value,bool need_debug)6108 elaborate_expression (Node_Id gnat_expr, Entity_Id gnat_entity, tree gnu_name,
6109 		      bool definition, bool need_value, bool need_debug)
6110 {
6111   tree gnu_expr;
6112 
6113   /* If we already elaborated this expression (e.g. it was involved
6114      in the definition of a private type), use the old value.  */
6115   if (present_gnu_tree (gnat_expr))
6116     return get_gnu_tree (gnat_expr);
6117 
6118   /* If we don't need a value and this is static or a discriminant,
6119      we don't need to do anything.  */
6120   if (!need_value
6121       && (Is_OK_Static_Expression (gnat_expr)
6122 	  || (Nkind (gnat_expr) == N_Identifier
6123 	      && Ekind (Entity (gnat_expr)) == E_Discriminant)))
6124     return NULL_TREE;
6125 
6126   /* If it's a static expression, we don't need a variable for debugging.  */
6127   if (need_debug && Is_OK_Static_Expression (gnat_expr))
6128     need_debug = false;
6129 
6130   /* Otherwise, convert this tree to its GCC equivalent and elaborate it.  */
6131   gnu_expr = elaborate_expression_1 (gnat_to_gnu (gnat_expr), gnat_entity,
6132 				     gnu_name, definition, need_debug);
6133 
6134   /* Save the expression in case we try to elaborate this entity again.  Since
6135      it's not a DECL, don't check it.  Don't save if it's a discriminant.  */
6136   if (!CONTAINS_PLACEHOLDER_P (gnu_expr))
6137     save_gnu_tree (gnat_expr, gnu_expr, true);
6138 
6139   return need_value ? gnu_expr : error_mark_node;
6140 }
6141 
6142 /* Similar, but take a GNU expression and always return a result.  */
6143 
6144 static tree
elaborate_expression_1(tree gnu_expr,Entity_Id gnat_entity,tree gnu_name,bool definition,bool need_debug)6145 elaborate_expression_1 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6146 			bool definition, bool need_debug)
6147 {
6148   const bool expr_public_p = Is_Public (gnat_entity);
6149   const bool expr_global_p = expr_public_p || global_bindings_p ();
6150   bool expr_variable_p, use_variable;
6151 
6152   /* In most cases, we won't see a naked FIELD_DECL because a discriminant
6153      reference will have been replaced with a COMPONENT_REF when the type
6154      is being elaborated.  However, there are some cases involving child
6155      types where we will.  So convert it to a COMPONENT_REF.  We hope it
6156      will be at the highest level of the expression in these cases.  */
6157   if (TREE_CODE (gnu_expr) == FIELD_DECL)
6158     gnu_expr = build3 (COMPONENT_REF, TREE_TYPE (gnu_expr),
6159 		       build0 (PLACEHOLDER_EXPR, DECL_CONTEXT (gnu_expr)),
6160 		       gnu_expr, NULL_TREE);
6161 
6162   /* If GNU_EXPR contains a placeholder, just return it.  We rely on the fact
6163      that an expression cannot contain both a discriminant and a variable.  */
6164   if (CONTAINS_PLACEHOLDER_P (gnu_expr))
6165     return gnu_expr;
6166 
6167   /* If GNU_EXPR is neither a constant nor based on a read-only variable, make
6168      a variable that is initialized to contain the expression when the package
6169      containing the definition is elaborated.  If this entity is defined at top
6170      level, replace the expression by the variable; otherwise use a SAVE_EXPR
6171      if this is necessary.  */
6172   if (CONSTANT_CLASS_P (gnu_expr))
6173     expr_variable_p = false;
6174   else
6175     {
6176       /* Skip any conversions and simple arithmetics to see if the expression
6177 	 is based on a read-only variable.
6178 	 ??? This really should remain read-only, but we have to think about
6179 	 the typing of the tree here.  */
6180       tree inner
6181 	= skip_simple_arithmetic (remove_conversions (gnu_expr, true));
6182 
6183       if (handled_component_p (inner))
6184 	{
6185 	  HOST_WIDE_INT bitsize, bitpos;
6186 	  tree offset;
6187 	  enum machine_mode mode;
6188 	  int unsignedp, volatilep;
6189 
6190 	  inner = get_inner_reference (inner, &bitsize, &bitpos, &offset,
6191 				       &mode, &unsignedp, &volatilep, false);
6192 	  /* If the offset is variable, err on the side of caution.  */
6193 	  if (offset)
6194 	    inner = NULL_TREE;
6195 	}
6196 
6197       expr_variable_p
6198 	= !(inner
6199 	    && TREE_CODE (inner) == VAR_DECL
6200 	    && (TREE_READONLY (inner) || DECL_READONLY_ONCE_ELAB (inner)));
6201     }
6202 
6203   /* We only need to use the variable if we are in a global context since GCC
6204      can do the right thing in the local case.  However, when not optimizing,
6205      use it for bounds of loop iteration scheme to avoid code duplication.  */
6206   use_variable = expr_variable_p
6207 		 && (expr_global_p
6208 		     || (!optimize
6209 		         && definition
6210 			 && Is_Itype (gnat_entity)
6211 			 && Nkind (Associated_Node_For_Itype (gnat_entity))
6212 			    == N_Loop_Parameter_Specification));
6213 
6214   /* Now create it, possibly only for debugging purposes.  */
6215   if (use_variable || need_debug)
6216     {
6217       tree gnu_decl
6218 	= create_var_decl_1
6219 	  (create_concat_name (gnat_entity, IDENTIFIER_POINTER (gnu_name)),
6220 	   NULL_TREE, TREE_TYPE (gnu_expr), gnu_expr, true, expr_public_p,
6221 	   !definition, expr_global_p, !need_debug, NULL, gnat_entity);
6222 
6223       if (use_variable)
6224 	return gnu_decl;
6225     }
6226 
6227   return expr_variable_p ? gnat_save_expr (gnu_expr) : gnu_expr;
6228 }
6229 
6230 /* Similar, but take an alignment factor and make it explicit in the tree.  */
6231 
6232 static tree
elaborate_expression_2(tree gnu_expr,Entity_Id gnat_entity,tree gnu_name,bool definition,bool need_debug,unsigned int align)6233 elaborate_expression_2 (tree gnu_expr, Entity_Id gnat_entity, tree gnu_name,
6234 			bool definition, bool need_debug, unsigned int align)
6235 {
6236   tree unit_align = size_int (align / BITS_PER_UNIT);
6237   return
6238     size_binop (MULT_EXPR,
6239 		elaborate_expression_1 (size_binop (EXACT_DIV_EXPR,
6240 						    gnu_expr,
6241 						    unit_align),
6242 					gnat_entity, gnu_name, definition,
6243 					need_debug),
6244 		unit_align);
6245 }
6246 
6247 /* Given a GNU tree and a GNAT list of choices, generate an expression to test
6248    the value passed against the list of choices.  */
6249 
6250 tree
choices_to_gnu(tree operand,Node_Id choices)6251 choices_to_gnu (tree operand, Node_Id choices)
6252 {
6253   Node_Id choice;
6254   Node_Id gnat_temp;
6255   tree result = boolean_false_node;
6256   tree this_test, low = 0, high = 0, single = 0;
6257 
6258   for (choice = First (choices); Present (choice); choice = Next (choice))
6259     {
6260       switch (Nkind (choice))
6261 	{
6262 	case N_Range:
6263 	  low = gnat_to_gnu (Low_Bound (choice));
6264 	  high = gnat_to_gnu (High_Bound (choice));
6265 
6266 	  this_test
6267 	    = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6268 			       build_binary_op (GE_EXPR, boolean_type_node,
6269 						operand, low),
6270 			       build_binary_op (LE_EXPR, boolean_type_node,
6271 						operand, high));
6272 
6273 	  break;
6274 
6275 	case N_Subtype_Indication:
6276 	  gnat_temp = Range_Expression (Constraint (choice));
6277 	  low = gnat_to_gnu (Low_Bound (gnat_temp));
6278 	  high = gnat_to_gnu (High_Bound (gnat_temp));
6279 
6280 	  this_test
6281 	    = build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6282 			       build_binary_op (GE_EXPR, boolean_type_node,
6283 						operand, low),
6284 			       build_binary_op (LE_EXPR, boolean_type_node,
6285 						operand, high));
6286 	  break;
6287 
6288 	case N_Identifier:
6289 	case N_Expanded_Name:
6290 	  /* This represents either a subtype range, an enumeration
6291 	     literal, or a constant  Ekind says which.  If an enumeration
6292 	     literal or constant, fall through to the next case.  */
6293 	  if (Ekind (Entity (choice)) != E_Enumeration_Literal
6294 	      && Ekind (Entity (choice)) != E_Constant)
6295 	    {
6296 	      tree type = gnat_to_gnu_type (Entity (choice));
6297 
6298 	      low = TYPE_MIN_VALUE (type);
6299 	      high = TYPE_MAX_VALUE (type);
6300 
6301 	      this_test
6302 		= build_binary_op (TRUTH_ANDIF_EXPR, boolean_type_node,
6303 				   build_binary_op (GE_EXPR, boolean_type_node,
6304 						    operand, low),
6305 				   build_binary_op (LE_EXPR, boolean_type_node,
6306 						    operand, high));
6307 	      break;
6308 	    }
6309 
6310 	  /* ... fall through ... */
6311 
6312 	case N_Character_Literal:
6313 	case N_Integer_Literal:
6314 	  single = gnat_to_gnu (choice);
6315 	  this_test = build_binary_op (EQ_EXPR, boolean_type_node, operand,
6316 				       single);
6317 	  break;
6318 
6319 	case N_Others_Choice:
6320 	  this_test = boolean_true_node;
6321 	  break;
6322 
6323 	default:
6324 	  gcc_unreachable ();
6325 	}
6326 
6327       result = build_binary_op (TRUTH_ORIF_EXPR, boolean_type_node, result,
6328 				this_test);
6329     }
6330 
6331   return result;
6332 }
6333 
6334 /* Adjust PACKED setting as passed to gnat_to_gnu_field for a field of
6335    type FIELD_TYPE to be placed in RECORD_TYPE.  Return the result.  */
6336 
6337 static int
adjust_packed(tree field_type,tree record_type,int packed)6338 adjust_packed (tree field_type, tree record_type, int packed)
6339 {
6340   /* If the field contains an item of variable size, we cannot pack it
6341      because we cannot create temporaries of non-fixed size in case
6342      we need to take the address of the field.  See addressable_p and
6343      the notes on the addressability issues for further details.  */
6344   if (type_has_variable_size (field_type))
6345     return 0;
6346 
6347   /* If the alignment of the record is specified and the field type
6348      is over-aligned, request Storage_Unit alignment for the field.  */
6349   if (packed == -2)
6350     {
6351       if (TYPE_ALIGN (field_type) > TYPE_ALIGN (record_type))
6352 	return -1;
6353       else
6354 	return 0;
6355     }
6356 
6357   return packed;
6358 }
6359 
6360 /* Return a GCC tree for a field corresponding to GNAT_FIELD to be
6361    placed in GNU_RECORD_TYPE.
6362 
6363    PACKED is 1 if the enclosing record is packed, -1 if the enclosing
6364    record has Component_Alignment of Storage_Unit, -2 if the enclosing
6365    record has a specified alignment.
6366 
6367    DEFINITION is true if this field is for a record being defined.
6368 
6369    DEBUG_INFO_P is true if we need to write debug information for types
6370    that we may create in the process.  */
6371 
6372 static tree
gnat_to_gnu_field(Entity_Id gnat_field,tree gnu_record_type,int packed,bool definition,bool debug_info_p)6373 gnat_to_gnu_field (Entity_Id gnat_field, tree gnu_record_type, int packed,
6374 		   bool definition, bool debug_info_p)
6375 {
6376   const Entity_Id gnat_field_type = Etype (gnat_field);
6377   tree gnu_field_type = gnat_to_gnu_type (gnat_field_type);
6378   tree gnu_field_id = get_entity_name (gnat_field);
6379   tree gnu_field, gnu_size, gnu_pos;
6380   bool is_volatile
6381     = (Treat_As_Volatile (gnat_field) || Treat_As_Volatile (gnat_field_type));
6382   bool needs_strict_alignment
6383     = (is_volatile
6384        || Is_Aliased (gnat_field)
6385        || Strict_Alignment (gnat_field_type));
6386 
6387   /* If this field requires strict alignment, we cannot pack it because
6388      it would very likely be under-aligned in the record.  */
6389   if (needs_strict_alignment)
6390     packed = 0;
6391   else
6392     packed = adjust_packed (gnu_field_type, gnu_record_type, packed);
6393 
6394   /* If a size is specified, use it.  Otherwise, if the record type is packed,
6395      use the official RM size.  See "Handling of Type'Size Values" in Einfo
6396      for further details.  */
6397   if (Known_Esize (gnat_field))
6398     gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6399 			      gnat_field, FIELD_DECL, false, true);
6400   else if (packed == 1)
6401     gnu_size = validate_size (RM_Size (gnat_field_type), gnu_field_type,
6402 			      gnat_field, FIELD_DECL, false, true);
6403   else
6404     gnu_size = NULL_TREE;
6405 
6406   /* If we have a specified size that is smaller than that of the field's type,
6407      or a position is specified, and the field's type is a record that doesn't
6408      require strict alignment, see if we can get either an integral mode form
6409      of the type or a smaller form.  If we can, show a size was specified for
6410      the field if there wasn't one already, so we know to make this a bitfield
6411      and avoid making things wider.
6412 
6413      Changing to an integral mode form is useful when the record is packed as
6414      we can then place the field at a non-byte-aligned position and so achieve
6415      tighter packing.  This is in addition required if the field shares a byte
6416      with another field and the front-end lets the back-end handle the access
6417      to the field, because GCC cannot handle non-byte-aligned BLKmode fields.
6418 
6419      Changing to a smaller form is required if the specified size is smaller
6420      than that of the field's type and the type contains sub-fields that are
6421      padded, in order to avoid generating accesses to these sub-fields that
6422      are wider than the field.
6423 
6424      We avoid the transformation if it is not required or potentially useful,
6425      as it might entail an increase of the field's alignment and have ripple
6426      effects on the outer record type.  A typical case is a field known to be
6427      byte-aligned and not to share a byte with another field.  */
6428   if (!needs_strict_alignment
6429       && RECORD_OR_UNION_TYPE_P (gnu_field_type)
6430       && !TYPE_FAT_POINTER_P (gnu_field_type)
6431       && host_integerp (TYPE_SIZE (gnu_field_type), 1)
6432       && (packed == 1
6433 	  || (gnu_size
6434 	      && (tree_int_cst_lt (gnu_size, TYPE_SIZE (gnu_field_type))
6435 		  || (Present (Component_Clause (gnat_field))
6436 		      && !(UI_To_Int (Component_Bit_Offset (gnat_field))
6437 			   % BITS_PER_UNIT == 0
6438 			   && value_factor_p (gnu_size, BITS_PER_UNIT)))))))
6439     {
6440       tree gnu_packable_type = make_packable_type (gnu_field_type, true);
6441       if (gnu_packable_type != gnu_field_type)
6442 	{
6443 	  gnu_field_type = gnu_packable_type;
6444 	  if (!gnu_size)
6445 	    gnu_size = rm_size (gnu_field_type);
6446 	}
6447     }
6448 
6449   if (Is_Atomic (gnat_field))
6450     check_ok_for_atomic (gnu_field_type, gnat_field, false);
6451 
6452   if (Present (Component_Clause (gnat_field)))
6453     {
6454       Entity_Id gnat_parent
6455 	= Parent_Subtype (Underlying_Type (Scope (gnat_field)));
6456 
6457       gnu_pos = UI_To_gnu (Component_Bit_Offset (gnat_field), bitsizetype);
6458       gnu_size = validate_size (Esize (gnat_field), gnu_field_type,
6459 				gnat_field, FIELD_DECL, false, true);
6460 
6461       /* Ensure the position does not overlap with the parent subtype, if there
6462 	 is one.  This test is omitted if the parent of the tagged type has a
6463 	 full rep clause since, in this case, component clauses are allowed to
6464 	 overlay the space allocated for the parent type and the front-end has
6465 	 checked that there are no overlapping components.  */
6466       if (Present (gnat_parent) && !Is_Fully_Repped_Tagged_Type (gnat_parent))
6467 	{
6468 	  tree gnu_parent = gnat_to_gnu_type (gnat_parent);
6469 
6470 	  if (TREE_CODE (TYPE_SIZE (gnu_parent)) == INTEGER_CST
6471 	      && tree_int_cst_lt (gnu_pos, TYPE_SIZE (gnu_parent)))
6472 	    {
6473 	      post_error_ne_tree
6474 		("offset of& must be beyond parent{, minimum allowed is ^}",
6475 		 First_Bit (Component_Clause (gnat_field)), gnat_field,
6476 		 TYPE_SIZE_UNIT (gnu_parent));
6477 	    }
6478 	}
6479 
6480       /* If this field needs strict alignment, check that the record is
6481 	 sufficiently aligned and that position and size are consistent with
6482 	 the alignment.  But don't do it if we are just annotating types and
6483 	 the field's type is tagged, since tagged types aren't fully laid out
6484 	 in this mode.  Also, note that atomic implies volatile so the inner
6485 	 test sequences ordering is significant here.  */
6486       if (needs_strict_alignment
6487 	  && !(type_annotate_only && Is_Tagged_Type (gnat_field_type)))
6488 	{
6489 	  TYPE_ALIGN (gnu_record_type)
6490 	    = MAX (TYPE_ALIGN (gnu_record_type), TYPE_ALIGN (gnu_field_type));
6491 
6492 	  if (gnu_size
6493 	      && !operand_equal_p (gnu_size, TYPE_SIZE (gnu_field_type), 0))
6494 	    {
6495 	      if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6496 		post_error_ne_tree
6497 		  ("atomic field& must be natural size of type{ (^)}",
6498 		   Last_Bit (Component_Clause (gnat_field)), gnat_field,
6499 		   TYPE_SIZE (gnu_field_type));
6500 
6501 	      else if (is_volatile)
6502 		post_error_ne_tree
6503 		  ("volatile field& must be natural size of type{ (^)}",
6504 		   Last_Bit (Component_Clause (gnat_field)), gnat_field,
6505 		   TYPE_SIZE (gnu_field_type));
6506 
6507 	      else if (Is_Aliased (gnat_field))
6508 		post_error_ne_tree
6509 		  ("size of aliased field& must be ^ bits",
6510 		   Last_Bit (Component_Clause (gnat_field)), gnat_field,
6511 		   TYPE_SIZE (gnu_field_type));
6512 
6513 	      else if (Strict_Alignment (gnat_field_type))
6514 		post_error_ne_tree
6515 		  ("size of & with aliased or tagged components not ^ bits",
6516 		   Last_Bit (Component_Clause (gnat_field)), gnat_field,
6517 		   TYPE_SIZE (gnu_field_type));
6518 
6519               else
6520 		gcc_unreachable ();
6521 
6522 	      gnu_size = NULL_TREE;
6523 	    }
6524 
6525 	  if (!integer_zerop (size_binop
6526 			      (TRUNC_MOD_EXPR, gnu_pos,
6527 			       bitsize_int (TYPE_ALIGN (gnu_field_type)))))
6528 	    {
6529 	      if (Is_Atomic (gnat_field) || Is_Atomic (gnat_field_type))
6530 		post_error_ne_num
6531 		  ("position of atomic field& must be multiple of ^ bits",
6532 		   First_Bit (Component_Clause (gnat_field)), gnat_field,
6533 		   TYPE_ALIGN (gnu_field_type));
6534 
6535               else if (is_volatile)
6536 		post_error_ne_num
6537 		  ("position of volatile field& must be multiple of ^ bits",
6538 		   First_Bit (Component_Clause (gnat_field)), gnat_field,
6539 		   TYPE_ALIGN (gnu_field_type));
6540 
6541 	      else if (Is_Aliased (gnat_field))
6542 		post_error_ne_num
6543 		  ("position of aliased field& must be multiple of ^ bits",
6544 		   First_Bit (Component_Clause (gnat_field)), gnat_field,
6545 		   TYPE_ALIGN (gnu_field_type));
6546 
6547 	      else if (Strict_Alignment (gnat_field_type))
6548 		post_error_ne
6549 		  ("position of & is not compatible with alignment required "
6550 		   "by its components",
6551 		    First_Bit (Component_Clause (gnat_field)), gnat_field);
6552 
6553 	      else
6554 		gcc_unreachable ();
6555 
6556 	      gnu_pos = NULL_TREE;
6557 	    }
6558 	}
6559     }
6560 
6561   /* If the record has rep clauses and this is the tag field, make a rep
6562      clause for it as well.  */
6563   else if (Has_Specified_Layout (Scope (gnat_field))
6564 	   && Chars (gnat_field) == Name_uTag)
6565     {
6566       gnu_pos = bitsize_zero_node;
6567       gnu_size = TYPE_SIZE (gnu_field_type);
6568     }
6569 
6570   else
6571     {
6572       gnu_pos = NULL_TREE;
6573 
6574       /* If we are packing the record and the field is BLKmode, round the
6575 	 size up to a byte boundary.  */
6576       if (packed && TYPE_MODE (gnu_field_type) == BLKmode && gnu_size)
6577 	gnu_size = round_up (gnu_size, BITS_PER_UNIT);
6578     }
6579 
6580   /* We need to make the size the maximum for the type if it is
6581      self-referential and an unconstrained type.  In that case, we can't
6582      pack the field since we can't make a copy to align it.  */
6583   if (TREE_CODE (gnu_field_type) == RECORD_TYPE
6584       && !gnu_size
6585       && CONTAINS_PLACEHOLDER_P (TYPE_SIZE (gnu_field_type))
6586       && !Is_Constrained (Underlying_Type (gnat_field_type)))
6587     {
6588       gnu_size = max_size (TYPE_SIZE (gnu_field_type), true);
6589       packed = 0;
6590     }
6591 
6592   /* If a size is specified, adjust the field's type to it.  */
6593   if (gnu_size)
6594     {
6595       tree orig_field_type;
6596 
6597       /* If the field's type is justified modular, we would need to remove
6598 	 the wrapper to (better) meet the layout requirements.  However we
6599 	 can do so only if the field is not aliased to preserve the unique
6600 	 layout and if the prescribed size is not greater than that of the
6601 	 packed array to preserve the justification.  */
6602       if (!needs_strict_alignment
6603 	  && TREE_CODE (gnu_field_type) == RECORD_TYPE
6604 	  && TYPE_JUSTIFIED_MODULAR_P (gnu_field_type)
6605 	  && tree_int_cst_compare (gnu_size, TYPE_ADA_SIZE (gnu_field_type))
6606 	       <= 0)
6607 	gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6608 
6609       /* Similarly if the field's type is a misaligned integral type, but
6610 	 there is no restriction on the size as there is no justification.  */
6611       if (!needs_strict_alignment
6612 	  && TYPE_IS_PADDING_P (gnu_field_type)
6613 	  && INTEGRAL_TYPE_P (TREE_TYPE (TYPE_FIELDS (gnu_field_type))))
6614 	gnu_field_type = TREE_TYPE (TYPE_FIELDS (gnu_field_type));
6615 
6616       gnu_field_type
6617 	= make_type_from_size (gnu_field_type, gnu_size,
6618 			       Has_Biased_Representation (gnat_field));
6619 
6620       orig_field_type = gnu_field_type;
6621       gnu_field_type = maybe_pad_type (gnu_field_type, gnu_size, 0, gnat_field,
6622 				       false, false, definition, true);
6623 
6624       /* If a padding record was made, declare it now since it will never be
6625 	 declared otherwise.  This is necessary to ensure that its subtrees
6626 	 are properly marked.  */
6627       if (gnu_field_type != orig_field_type
6628 	  && !DECL_P (TYPE_NAME (gnu_field_type)))
6629 	create_type_decl (TYPE_NAME (gnu_field_type), gnu_field_type, NULL,
6630 			  true, debug_info_p, gnat_field);
6631     }
6632 
6633   /* Otherwise (or if there was an error), don't specify a position.  */
6634   else
6635     gnu_pos = NULL_TREE;
6636 
6637   gcc_assert (TREE_CODE (gnu_field_type) != RECORD_TYPE
6638 	      || !TYPE_CONTAINS_TEMPLATE_P (gnu_field_type));
6639 
6640   /* Now create the decl for the field.  */
6641   gnu_field
6642     = create_field_decl (gnu_field_id, gnu_field_type, gnu_record_type,
6643 			 gnu_size, gnu_pos, packed, Is_Aliased (gnat_field));
6644   Sloc_to_locus (Sloc (gnat_field), &DECL_SOURCE_LOCATION (gnu_field));
6645   DECL_ALIASED_P (gnu_field) = Is_Aliased (gnat_field);
6646   TREE_THIS_VOLATILE (gnu_field) = TREE_SIDE_EFFECTS (gnu_field) = is_volatile;
6647 
6648   if (Ekind (gnat_field) == E_Discriminant)
6649     DECL_DISCRIMINANT_NUMBER (gnu_field)
6650       = UI_To_gnu (Discriminant_Number (gnat_field), sizetype);
6651 
6652   return gnu_field;
6653 }
6654 
6655 /* Return true if at least one member of COMPONENT_LIST needs strict
6656    alignment.  */
6657 
6658 static bool
components_need_strict_alignment(Node_Id component_list)6659 components_need_strict_alignment (Node_Id component_list)
6660 {
6661   Node_Id component_decl;
6662 
6663   for (component_decl = First_Non_Pragma (Component_Items (component_list));
6664        Present (component_decl);
6665        component_decl = Next_Non_Pragma (component_decl))
6666     {
6667       Entity_Id gnat_field = Defining_Entity (component_decl);
6668 
6669       if (Is_Aliased (gnat_field))
6670 	return True;
6671 
6672       if (Strict_Alignment (Etype (gnat_field)))
6673 	return True;
6674     }
6675 
6676   return False;
6677 }
6678 
6679 /* Return true if TYPE is a type with variable size or a padding type with a
6680    field of variable size or a record that has a field with such a type.  */
6681 
6682 static bool
type_has_variable_size(tree type)6683 type_has_variable_size (tree type)
6684 {
6685   tree field;
6686 
6687   if (!TREE_CONSTANT (TYPE_SIZE (type)))
6688     return true;
6689 
6690   if (TYPE_IS_PADDING_P (type)
6691       && !TREE_CONSTANT (DECL_SIZE (TYPE_FIELDS (type))))
6692     return true;
6693 
6694   if (!RECORD_OR_UNION_TYPE_P (type))
6695     return false;
6696 
6697   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6698     if (type_has_variable_size (TREE_TYPE (field)))
6699       return true;
6700 
6701   return false;
6702 }
6703 
6704 /* Return true if FIELD is an artificial field.  */
6705 
6706 static bool
field_is_artificial(tree field)6707 field_is_artificial (tree field)
6708 {
6709   /* These fields are generated by the front-end proper.  */
6710   if (IDENTIFIER_POINTER (DECL_NAME (field)) [0] == '_')
6711     return true;
6712 
6713   /* These fields are generated by gigi.  */
6714   if (DECL_INTERNAL_P (field))
6715     return true;
6716 
6717   return false;
6718 }
6719 
6720 /* Return true if FIELD is a non-artificial aliased field.  */
6721 
6722 static bool
field_is_aliased(tree field)6723 field_is_aliased (tree field)
6724 {
6725   if (field_is_artificial (field))
6726     return false;
6727 
6728   return DECL_ALIASED_P (field);
6729 }
6730 
6731 /* Return true if FIELD is a non-artificial field with self-referential
6732    size.  */
6733 
6734 static bool
field_has_self_size(tree field)6735 field_has_self_size (tree field)
6736 {
6737   if (field_is_artificial (field))
6738     return false;
6739 
6740   if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6741     return false;
6742 
6743   return CONTAINS_PLACEHOLDER_P (TYPE_SIZE (TREE_TYPE (field)));
6744 }
6745 
6746 /* Return true if FIELD is a non-artificial field with variable size.  */
6747 
6748 static bool
field_has_variable_size(tree field)6749 field_has_variable_size (tree field)
6750 {
6751   if (field_is_artificial (field))
6752     return false;
6753 
6754   if (DECL_SIZE (field) && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
6755     return false;
6756 
6757   return TREE_CODE (TYPE_SIZE (TREE_TYPE (field))) != INTEGER_CST;
6758 }
6759 
6760 /* qsort comparer for the bit positions of two record components.  */
6761 
6762 static int
compare_field_bitpos(const PTR rt1,const PTR rt2)6763 compare_field_bitpos (const PTR rt1, const PTR rt2)
6764 {
6765   const_tree const field1 = * (const_tree const *) rt1;
6766   const_tree const field2 = * (const_tree const *) rt2;
6767   const int ret
6768     = tree_int_cst_compare (bit_position (field1), bit_position (field2));
6769 
6770   return ret ? ret : (int) (DECL_UID (field1) - DECL_UID (field2));
6771 }
6772 
6773 /* Translate and chain the GNAT_COMPONENT_LIST to the GNU_FIELD_LIST, set
6774    the result as the field list of GNU_RECORD_TYPE and finish it up.  When
6775    called from gnat_to_gnu_entity during the processing of a record type
6776    definition, the GCC node for the parent, if any, will be the single field
6777    of GNU_RECORD_TYPE and the GCC nodes for the discriminants will be on the
6778    GNU_FIELD_LIST.  The other calls to this function are recursive calls for
6779    the component list of a variant and, in this case, GNU_FIELD_LIST is empty.
6780 
6781    PACKED is 1 if this is for a packed record, -1 if this is for a record
6782    with Component_Alignment of Storage_Unit, -2 if this is for a record
6783    with a specified alignment.
6784 
6785    DEFINITION is true if we are defining this record type.
6786 
6787    CANCEL_ALIGNMENT is true if the alignment should be zeroed before laying
6788    out the record.  This means the alignment only serves to force fields to
6789    be bitfields, but not to require the record to be that aligned.  This is
6790    used for variants.
6791 
6792    ALL_REP is true if a rep clause is present for all the fields.
6793 
6794    UNCHECKED_UNION is true if we are building this type for a record with a
6795    Pragma Unchecked_Union.
6796 
6797    ARTIFICIAL is true if this is a type that was generated by the compiler.
6798 
6799    DEBUG_INFO is true if we need to write debug information about the type.
6800 
6801    MAYBE_UNUSED is true if this type may be unused in the end; this doesn't
6802    mean that its contents may be unused as well, only the container itself.
6803 
6804    REORDER is true if we are permitted to reorder components of this type.
6805 
6806    FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in
6807    the outer record type down to this variant level.  It is nonzero only if
6808    all the fields down to this level have a rep clause and ALL_REP is false.
6809 
6810    P_GNU_REP_LIST, if nonzero, is a pointer to a list to which each field
6811    with a rep clause is to be added; in this case, that is all that should
6812    be done with such fields.  */
6813 
6814 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)6815 components_to_record (tree gnu_record_type, Node_Id gnat_component_list,
6816 		      tree gnu_field_list, int packed, bool definition,
6817 		      bool cancel_alignment, bool all_rep,
6818 		      bool unchecked_union, bool artificial,
6819 		      bool debug_info, bool maybe_unused, bool reorder,
6820 		      tree first_free_pos, tree *p_gnu_rep_list)
6821 {
6822   bool all_rep_and_size = all_rep && TYPE_SIZE (gnu_record_type);
6823   bool layout_with_rep = false;
6824   bool has_self_field = false;
6825   bool has_aliased_after_self_field = false;
6826   Node_Id component_decl, variant_part;
6827   tree gnu_field, gnu_next, gnu_last;
6828   tree gnu_rep_part = NULL_TREE;
6829   tree gnu_variant_part = NULL_TREE;
6830   tree gnu_rep_list = NULL_TREE;
6831   tree gnu_var_list = NULL_TREE;
6832   tree gnu_self_list = NULL_TREE;
6833 
6834   /* For each component referenced in a component declaration create a GCC
6835      field and add it to the list, skipping pragmas in the GNAT list.  */
6836   gnu_last = tree_last (gnu_field_list);
6837   if (Present (Component_Items (gnat_component_list)))
6838     for (component_decl
6839 	   = First_Non_Pragma (Component_Items (gnat_component_list));
6840 	 Present (component_decl);
6841 	 component_decl = Next_Non_Pragma (component_decl))
6842       {
6843 	Entity_Id gnat_field = Defining_Entity (component_decl);
6844 	Name_Id gnat_name = Chars (gnat_field);
6845 
6846 	/* If present, the _Parent field must have been created as the single
6847 	   field of the record type.  Put it before any other fields.  */
6848 	if (gnat_name == Name_uParent)
6849 	  {
6850 	    gnu_field = TYPE_FIELDS (gnu_record_type);
6851 	    gnu_field_list = chainon (gnu_field_list, gnu_field);
6852 	  }
6853 	else
6854 	  {
6855 	    gnu_field = gnat_to_gnu_field (gnat_field, gnu_record_type, packed,
6856 					   definition, debug_info);
6857 
6858 	    /* If this is the _Tag field, put it before any other fields.  */
6859 	    if (gnat_name == Name_uTag)
6860 	      gnu_field_list = chainon (gnu_field_list, gnu_field);
6861 
6862 	    /* If this is the _Controller field, put it before the other
6863 	       fields except for the _Tag or _Parent field.  */
6864 	    else if (gnat_name == Name_uController && gnu_last)
6865 	      {
6866 		DECL_CHAIN (gnu_field) = DECL_CHAIN (gnu_last);
6867 		DECL_CHAIN (gnu_last) = gnu_field;
6868 	      }
6869 
6870 	    /* If this is a regular field, put it after the other fields.  */
6871 	    else
6872 	      {
6873 		DECL_CHAIN (gnu_field) = gnu_field_list;
6874 		gnu_field_list = gnu_field;
6875 		if (!gnu_last)
6876 		  gnu_last = gnu_field;
6877 
6878 		/* And record information for the final layout.  */
6879 		if (field_has_self_size (gnu_field))
6880 		  has_self_field = true;
6881 		else if (has_self_field && field_is_aliased (gnu_field))
6882 		  has_aliased_after_self_field = true;
6883 	      }
6884 	  }
6885 
6886 	save_gnu_tree (gnat_field, gnu_field, false);
6887       }
6888 
6889   /* At the end of the component list there may be a variant part.  */
6890   variant_part = Variant_Part (gnat_component_list);
6891 
6892   /* We create a QUAL_UNION_TYPE for the variant part since the variants are
6893      mutually exclusive and should go in the same memory.  To do this we need
6894      to treat each variant as a record whose elements are created from the
6895      component list for the variant.  So here we create the records from the
6896      lists for the variants and put them all into the QUAL_UNION_TYPE.
6897      If this is an Unchecked_Union, we make a UNION_TYPE instead or
6898      use GNU_RECORD_TYPE if there are no fields so far.  */
6899   if (Present (variant_part))
6900     {
6901       Node_Id gnat_discr = Name (variant_part), variant;
6902       tree gnu_discr = gnat_to_gnu (gnat_discr);
6903       tree gnu_name = TYPE_NAME (gnu_record_type);
6904       tree gnu_var_name
6905 	= concat_name (get_identifier (Get_Name_String (Chars (gnat_discr))),
6906 		       "XVN");
6907       tree gnu_union_type, gnu_union_name;
6908       tree this_first_free_pos, gnu_variant_list = NULL_TREE;
6909       bool union_field_needs_strict_alignment = false;
6910 
6911       if (TREE_CODE (gnu_name) == TYPE_DECL)
6912 	gnu_name = DECL_NAME (gnu_name);
6913 
6914       gnu_union_name
6915 	= concat_name (gnu_name, IDENTIFIER_POINTER (gnu_var_name));
6916 
6917       /* Reuse the enclosing union if this is an Unchecked_Union whose fields
6918 	 are all in the variant part, to match the layout of C unions.  There
6919 	 is an associated check below.  */
6920       if (TREE_CODE (gnu_record_type) == UNION_TYPE)
6921 	gnu_union_type = gnu_record_type;
6922       else
6923 	{
6924 	  gnu_union_type
6925 	    = make_node (unchecked_union ? UNION_TYPE : QUAL_UNION_TYPE);
6926 
6927 	  TYPE_NAME (gnu_union_type) = gnu_union_name;
6928 	  TYPE_ALIGN (gnu_union_type) = 0;
6929 	  TYPE_PACKED (gnu_union_type) = TYPE_PACKED (gnu_record_type);
6930 	}
6931 
6932       /* If all the fields down to this level have a rep clause, find out
6933 	 whether all the fields at this level also have one.  If so, then
6934 	 compute the new first free position to be passed downward.  */
6935       this_first_free_pos = first_free_pos;
6936       if (this_first_free_pos)
6937 	{
6938 	  for (gnu_field = gnu_field_list;
6939 	       gnu_field;
6940 	       gnu_field = DECL_CHAIN (gnu_field))
6941 	    if (DECL_FIELD_OFFSET (gnu_field))
6942 	      {
6943 		tree pos = bit_position (gnu_field);
6944 		if (!tree_int_cst_lt (pos, this_first_free_pos))
6945 		  this_first_free_pos
6946 		    = size_binop (PLUS_EXPR, pos, DECL_SIZE (gnu_field));
6947 	      }
6948 	    else
6949 	      {
6950 		this_first_free_pos = NULL_TREE;
6951 		break;
6952 	      }
6953 	}
6954 
6955       for (variant = First_Non_Pragma (Variants (variant_part));
6956 	   Present (variant);
6957 	   variant = Next_Non_Pragma (variant))
6958 	{
6959 	  tree gnu_variant_type = make_node (RECORD_TYPE);
6960 	  tree gnu_inner_name;
6961 	  tree gnu_qual;
6962 
6963 	  Get_Variant_Encoding (variant);
6964 	  gnu_inner_name = get_identifier_with_length (Name_Buffer, Name_Len);
6965 	  TYPE_NAME (gnu_variant_type)
6966 	    = concat_name (gnu_union_name,
6967 			   IDENTIFIER_POINTER (gnu_inner_name));
6968 
6969 	  /* Set the alignment of the inner type in case we need to make
6970 	     inner objects into bitfields, but then clear it out so the
6971 	     record actually gets only the alignment required.  */
6972 	  TYPE_ALIGN (gnu_variant_type) = TYPE_ALIGN (gnu_record_type);
6973 	  TYPE_PACKED (gnu_variant_type) = TYPE_PACKED (gnu_record_type);
6974 
6975 	  /* Similarly, if the outer record has a size specified and all
6976 	     the fields have a rep clause, we can propagate the size.  */
6977 	  if (all_rep_and_size)
6978 	    {
6979 	      TYPE_SIZE (gnu_variant_type) = TYPE_SIZE (gnu_record_type);
6980 	      TYPE_SIZE_UNIT (gnu_variant_type)
6981 		= TYPE_SIZE_UNIT (gnu_record_type);
6982 	    }
6983 
6984 	  /* Add the fields into the record type for the variant.  Note that
6985 	     we aren't sure to really use it at this point, see below.  */
6986 	  components_to_record (gnu_variant_type, Component_List (variant),
6987 				NULL_TREE, packed, definition,
6988 				!all_rep_and_size, all_rep, unchecked_union,
6989 				true, debug_info, true, reorder,
6990 				this_first_free_pos,
6991 				all_rep || this_first_free_pos
6992 				? NULL : &gnu_rep_list);
6993 
6994 	  gnu_qual = choices_to_gnu (gnu_discr, Discrete_Choices (variant));
6995 	  Set_Present_Expr (variant, annotate_value (gnu_qual));
6996 
6997 	  /* If this is an Unchecked_Union whose fields are all in the variant
6998 	     part and we have a single field with no representation clause or
6999 	     placed at offset zero, use the field directly to match the layout
7000 	     of C unions.  */
7001 	  if (TREE_CODE (gnu_record_type) == UNION_TYPE
7002 	      && (gnu_field = TYPE_FIELDS (gnu_variant_type)) != NULL_TREE
7003 	      && !DECL_CHAIN (gnu_field)
7004 	      && (!DECL_FIELD_OFFSET (gnu_field)
7005 		  || integer_zerop (bit_position (gnu_field))))
7006 	    DECL_CONTEXT (gnu_field) = gnu_union_type;
7007 	  else
7008 	    {
7009 	      /* Deal with packedness like in gnat_to_gnu_field.  */
7010 	      bool field_needs_strict_alignment
7011 	        = components_need_strict_alignment (Component_List (variant));
7012 	      int field_packed;
7013 
7014 	      if (field_needs_strict_alignment)
7015 		{
7016 		  field_packed = 0;
7017 		  union_field_needs_strict_alignment = true;
7018 		}
7019 	      else
7020 		field_packed
7021 		  = adjust_packed (gnu_variant_type, gnu_record_type, packed);
7022 
7023 	      /* Finalize the record type now.  We used to throw away
7024 		 empty records but we no longer do that because we need
7025 		 them to generate complete debug info for the variant;
7026 		 otherwise, the union type definition will be lacking
7027 		 the fields associated with these empty variants.  */
7028 	      rest_of_record_type_compilation (gnu_variant_type);
7029 	      create_type_decl (TYPE_NAME (gnu_variant_type), gnu_variant_type,
7030 				NULL, true, debug_info, gnat_component_list);
7031 
7032 	      gnu_field
7033 		= create_field_decl (gnu_inner_name, gnu_variant_type,
7034 				     gnu_union_type,
7035 				     all_rep_and_size
7036 				     ? TYPE_SIZE (gnu_variant_type) : 0,
7037 				     all_rep ? bitsize_zero_node : 0,
7038 				     field_packed, 0);
7039 
7040 	      DECL_INTERNAL_P (gnu_field) = 1;
7041 
7042 	      if (!unchecked_union)
7043 		DECL_QUALIFIER (gnu_field) = gnu_qual;
7044 	    }
7045 
7046 	  DECL_CHAIN (gnu_field) = gnu_variant_list;
7047 	  gnu_variant_list = gnu_field;
7048 	}
7049 
7050       /* Only make the QUAL_UNION_TYPE if there are non-empty variants.  */
7051       if (gnu_variant_list)
7052 	{
7053 	  int union_field_packed;
7054 
7055 	  if (all_rep_and_size)
7056 	    {
7057 	      TYPE_SIZE (gnu_union_type) = TYPE_SIZE (gnu_record_type);
7058 	      TYPE_SIZE_UNIT (gnu_union_type)
7059 		= TYPE_SIZE_UNIT (gnu_record_type);
7060 	    }
7061 
7062 	  finish_record_type (gnu_union_type, nreverse (gnu_variant_list),
7063 			      all_rep_and_size ? 1 : 0, debug_info);
7064 
7065 	  /* If GNU_UNION_TYPE is our record type, it means we must have an
7066 	     Unchecked_Union with no fields.  Verify that and, if so, just
7067 	     return.  */
7068 	  if (gnu_union_type == gnu_record_type)
7069 	    {
7070 	      gcc_assert (unchecked_union
7071 			  && !gnu_field_list
7072 			  && !gnu_rep_list);
7073 	      return;
7074 	    }
7075 
7076 	  create_type_decl (TYPE_NAME (gnu_union_type), gnu_union_type,
7077 			    NULL, true, debug_info, gnat_component_list);
7078 
7079 	  /* Deal with packedness like in gnat_to_gnu_field.  */
7080 	  if (union_field_needs_strict_alignment)
7081 	    union_field_packed = 0;
7082 	  else
7083 	    union_field_packed
7084 	      = adjust_packed (gnu_union_type, gnu_record_type, packed);
7085 
7086 	  gnu_variant_part
7087 	    = create_field_decl (gnu_var_name, gnu_union_type, gnu_record_type,
7088 				 all_rep_and_size
7089 				 ? TYPE_SIZE (gnu_union_type) : 0,
7090 				 all_rep || this_first_free_pos
7091 				 ? bitsize_zero_node : 0,
7092 				 union_field_packed, 0);
7093 
7094 	  DECL_INTERNAL_P (gnu_variant_part) = 1;
7095 	}
7096     }
7097 
7098   /* From now on, a zero FIRST_FREE_POS is totally useless.  */
7099   if (first_free_pos && integer_zerop (first_free_pos))
7100     first_free_pos = NULL_TREE;
7101 
7102   /* Scan GNU_FIELD_LIST and see if any fields have rep clauses and, if we are
7103      permitted to reorder components, self-referential sizes or variable sizes.
7104      If they do, pull them out and put them onto the appropriate list.  We have
7105      to do this in a separate pass since we want to handle the discriminants
7106      but can't play with them until we've used them in debugging data above.
7107 
7108      ??? If we reorder them, debugging information will be wrong but there is
7109      nothing that can be done about this at the moment.  */
7110   gnu_last = NULL_TREE;
7111 
7112 #define MOVE_FROM_FIELD_LIST_TO(LIST)	\
7113   do {					\
7114     if (gnu_last)			\
7115       DECL_CHAIN (gnu_last) = gnu_next;	\
7116     else				\
7117       gnu_field_list = gnu_next;	\
7118 					\
7119     DECL_CHAIN (gnu_field) = (LIST);	\
7120     (LIST) = gnu_field;			\
7121   } while (0)
7122 
7123   for (gnu_field = gnu_field_list; gnu_field; gnu_field = gnu_next)
7124     {
7125       gnu_next = DECL_CHAIN (gnu_field);
7126 
7127       if (DECL_FIELD_OFFSET (gnu_field))
7128 	{
7129 	  MOVE_FROM_FIELD_LIST_TO (gnu_rep_list);
7130 	  continue;
7131 	}
7132 
7133       if ((reorder || has_aliased_after_self_field)
7134 	  && field_has_self_size (gnu_field))
7135 	{
7136 	  MOVE_FROM_FIELD_LIST_TO (gnu_self_list);
7137 	  continue;
7138 	}
7139 
7140       if (reorder && field_has_variable_size (gnu_field))
7141 	{
7142 	  MOVE_FROM_FIELD_LIST_TO (gnu_var_list);
7143 	  continue;
7144 	}
7145 
7146       gnu_last = gnu_field;
7147     }
7148 
7149 #undef MOVE_FROM_FIELD_LIST_TO
7150 
7151   /* If permitted, we reorder the fields as follows:
7152 
7153        1) all fixed length fields,
7154        2) all fields whose length doesn't depend on discriminants,
7155        3) all fields whose length depends on discriminants,
7156        4) the variant part,
7157 
7158      within the record and within each variant recursively.  */
7159   if (reorder)
7160     gnu_field_list
7161       = chainon (nreverse (gnu_self_list),
7162 		 chainon (nreverse (gnu_var_list), gnu_field_list));
7163 
7164   /* Otherwise, if there is an aliased field placed after a field whose length
7165      depends on discriminants, we put all the fields of the latter sort, last.
7166      We need to do this in case an object of this record type is mutable.  */
7167   else if (has_aliased_after_self_field)
7168     gnu_field_list = chainon (nreverse (gnu_self_list), gnu_field_list);
7169 
7170   /* If P_REP_LIST is nonzero, this means that we are asked to move the fields
7171      in our REP list to the previous level because this level needs them in
7172      order to do a correct layout, i.e. avoid having overlapping fields.  */
7173   if (p_gnu_rep_list && gnu_rep_list)
7174     *p_gnu_rep_list = chainon (*p_gnu_rep_list, gnu_rep_list);
7175 
7176   /* Otherwise, sort the fields by bit position and put them into their own
7177      record, before the others, if we also have fields without rep clause.  */
7178   else if (gnu_rep_list)
7179     {
7180       tree gnu_rep_type
7181 	= (gnu_field_list ? make_node (RECORD_TYPE) : gnu_record_type);
7182       int i, len = list_length (gnu_rep_list);
7183       tree *gnu_arr = XALLOCAVEC (tree, len);
7184 
7185       for (gnu_field = gnu_rep_list, i = 0;
7186 	   gnu_field;
7187 	   gnu_field = DECL_CHAIN (gnu_field), i++)
7188 	gnu_arr[i] = gnu_field;
7189 
7190       qsort (gnu_arr, len, sizeof (tree), compare_field_bitpos);
7191 
7192       /* Put the fields in the list in order of increasing position, which
7193 	 means we start from the end.  */
7194       gnu_rep_list = NULL_TREE;
7195       for (i = len - 1; i >= 0; i--)
7196 	{
7197 	  DECL_CHAIN (gnu_arr[i]) = gnu_rep_list;
7198 	  gnu_rep_list = gnu_arr[i];
7199 	  DECL_CONTEXT (gnu_arr[i]) = gnu_rep_type;
7200 	}
7201 
7202       if (gnu_field_list)
7203 	{
7204 	  finish_record_type (gnu_rep_type, gnu_rep_list, 1, debug_info);
7205 
7206 	  /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields
7207 	     without rep clause are laid out starting from this position.
7208 	     Therefore, we force it as a minimal size on the REP part.  */
7209 	  gnu_rep_part
7210 	    = create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7211 	}
7212       else
7213 	{
7214 	  layout_with_rep = true;
7215 	  gnu_field_list = nreverse (gnu_rep_list);
7216 	}
7217     }
7218 
7219   /* If FIRST_FREE_POS is nonzero, we need to ensure that the fields without
7220      rep clause are laid out starting from this position.  Therefore, if we
7221      have not already done so, we create a fake REP part with this size.  */
7222   if (first_free_pos && !layout_with_rep && !gnu_rep_part)
7223     {
7224       tree gnu_rep_type = make_node (RECORD_TYPE);
7225       finish_record_type (gnu_rep_type, NULL_TREE, 0, debug_info);
7226       gnu_rep_part
7227 	= create_rep_part (gnu_rep_type, gnu_record_type, first_free_pos);
7228     }
7229 
7230   /* Now chain the REP part at the end of the reversed field list.  */
7231   if (gnu_rep_part)
7232     gnu_field_list = chainon (gnu_field_list, gnu_rep_part);
7233 
7234   /* And the variant part at the beginning.  */
7235   if (gnu_variant_part)
7236     {
7237       DECL_CHAIN (gnu_variant_part) = gnu_field_list;
7238       gnu_field_list = gnu_variant_part;
7239     }
7240 
7241   if (cancel_alignment)
7242     TYPE_ALIGN (gnu_record_type) = 0;
7243 
7244   finish_record_type (gnu_record_type, nreverse (gnu_field_list),
7245 		      layout_with_rep ? 1 : 0, false);
7246   TYPE_ARTIFICIAL (gnu_record_type) = artificial;
7247   if (debug_info && !maybe_unused)
7248     rest_of_record_type_compilation (gnu_record_type);
7249 }
7250 
7251 /* Given GNU_SIZE, a GCC tree representing a size, return a Uint to be
7252    placed into an Esize, Component_Bit_Offset, or Component_Size value
7253    in the GNAT tree.  */
7254 
7255 static Uint
annotate_value(tree gnu_size)7256 annotate_value (tree gnu_size)
7257 {
7258   TCode tcode;
7259   Node_Ref_Or_Val ops[3], ret, pre_op1 = No_Uint;
7260   struct tree_int_map in;
7261   int i;
7262 
7263   /* See if we've already saved the value for this node.  */
7264   if (EXPR_P (gnu_size))
7265     {
7266       struct tree_int_map *e;
7267 
7268       if (!annotate_value_cache)
7269         annotate_value_cache = htab_create_ggc (512, tree_int_map_hash,
7270 					        tree_int_map_eq, 0);
7271       in.base.from = gnu_size;
7272       e = (struct tree_int_map *)
7273 	    htab_find (annotate_value_cache, &in);
7274 
7275       if (e)
7276 	return (Node_Ref_Or_Val) e->to;
7277     }
7278   else
7279     in.base.from = NULL_TREE;
7280 
7281   /* If we do not return inside this switch, TCODE will be set to the
7282      code to use for a Create_Node operand and LEN (set above) will be
7283      the number of recursive calls for us to make.  */
7284 
7285   switch (TREE_CODE (gnu_size))
7286     {
7287     case INTEGER_CST:
7288       return TREE_OVERFLOW (gnu_size) ? No_Uint : UI_From_gnu (gnu_size);
7289 
7290     case COMPONENT_REF:
7291       /* The only case we handle here is a simple discriminant reference.  */
7292       if (DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1)))
7293 	{
7294 	  tree n = DECL_DISCRIMINANT_NUMBER (TREE_OPERAND (gnu_size, 1));
7295 
7296 	  /* Climb up the chain of successive extensions, if any.  */
7297 	  while (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == COMPONENT_REF
7298 		 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (gnu_size, 0), 1))
7299 		    == parent_name_id)
7300 	    gnu_size = TREE_OPERAND (gnu_size, 0);
7301 
7302 	  if (TREE_CODE (TREE_OPERAND (gnu_size, 0)) == PLACEHOLDER_EXPR)
7303 	    return
7304 	      Create_Node (Discrim_Val, annotate_value (n), No_Uint, No_Uint);
7305 	}
7306 
7307       return No_Uint;
7308 
7309     CASE_CONVERT:   case NON_LVALUE_EXPR:
7310       return annotate_value (TREE_OPERAND (gnu_size, 0));
7311 
7312       /* Now just list the operations we handle.  */
7313     case COND_EXPR:		tcode = Cond_Expr; break;
7314     case PLUS_EXPR:		tcode = Plus_Expr; break;
7315     case MINUS_EXPR:		tcode = Minus_Expr; break;
7316     case MULT_EXPR:		tcode = Mult_Expr; break;
7317     case TRUNC_DIV_EXPR:	tcode = Trunc_Div_Expr; break;
7318     case CEIL_DIV_EXPR:		tcode = Ceil_Div_Expr; break;
7319     case FLOOR_DIV_EXPR:	tcode = Floor_Div_Expr; break;
7320     case TRUNC_MOD_EXPR:	tcode = Trunc_Mod_Expr; break;
7321     case CEIL_MOD_EXPR:		tcode = Ceil_Mod_Expr; break;
7322     case FLOOR_MOD_EXPR:	tcode = Floor_Mod_Expr; break;
7323     case EXACT_DIV_EXPR:	tcode = Exact_Div_Expr; break;
7324     case NEGATE_EXPR:		tcode = Negate_Expr; break;
7325     case MIN_EXPR:		tcode = Min_Expr; break;
7326     case MAX_EXPR:		tcode = Max_Expr; break;
7327     case ABS_EXPR:		tcode = Abs_Expr; break;
7328     case TRUTH_ANDIF_EXPR:	tcode = Truth_Andif_Expr; break;
7329     case TRUTH_ORIF_EXPR:	tcode = Truth_Orif_Expr; break;
7330     case TRUTH_AND_EXPR:	tcode = Truth_And_Expr; break;
7331     case TRUTH_OR_EXPR:		tcode = Truth_Or_Expr; break;
7332     case TRUTH_XOR_EXPR:	tcode = Truth_Xor_Expr; break;
7333     case TRUTH_NOT_EXPR:	tcode = Truth_Not_Expr; break;
7334     case LT_EXPR:		tcode = Lt_Expr; break;
7335     case LE_EXPR:		tcode = Le_Expr; break;
7336     case GT_EXPR:		tcode = Gt_Expr; break;
7337     case GE_EXPR:		tcode = Ge_Expr; break;
7338     case EQ_EXPR:		tcode = Eq_Expr; break;
7339     case NE_EXPR:		tcode = Ne_Expr; break;
7340 
7341     case BIT_AND_EXPR:
7342       tcode = Bit_And_Expr;
7343       /* For negative values, build NEGATE_EXPR of the opposite.  Such values
7344 	 appear in expressions containing aligning patterns.  Note that, since
7345 	 sizetype is unsigned, we have to jump through some hoops.   */
7346       if (TREE_CODE (TREE_OPERAND (gnu_size, 1)) == INTEGER_CST)
7347 	{
7348 	  tree op1 = TREE_OPERAND (gnu_size, 1);
7349 	  double_int signed_op1
7350 	    = tree_to_double_int (op1).sext (TYPE_PRECISION (sizetype));
7351 	  if (signed_op1.is_negative ())
7352 	    {
7353 	      op1 = double_int_to_tree (sizetype, -signed_op1);
7354 	      pre_op1 = annotate_value (build1 (NEGATE_EXPR, sizetype, op1));
7355 	    }
7356 	}
7357       break;
7358 
7359     case CALL_EXPR:
7360       {
7361 	tree t = maybe_inline_call_in_expr (gnu_size);
7362 	if (t)
7363 	  return annotate_value (t);
7364       }
7365 
7366       /* Fall through... */
7367 
7368     default:
7369       return No_Uint;
7370     }
7371 
7372   /* Now get each of the operands that's relevant for this code.  If any
7373      cannot be expressed as a repinfo node, say we can't.  */
7374   for (i = 0; i < 3; i++)
7375     ops[i] = No_Uint;
7376 
7377   for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (gnu_size)); i++)
7378     {
7379       if (i == 1 && pre_op1 != No_Uint)
7380 	ops[i] = pre_op1;
7381       else
7382 	ops[i] = annotate_value (TREE_OPERAND (gnu_size, i));
7383       if (ops[i] == No_Uint)
7384 	return No_Uint;
7385     }
7386 
7387   ret = Create_Node (tcode, ops[0], ops[1], ops[2]);
7388 
7389   /* Save the result in the cache.  */
7390   if (in.base.from)
7391     {
7392       struct tree_int_map **h;
7393       /* We can't assume the hash table data hasn't moved since the
7394 	 initial look up, so we have to search again.  Allocating and
7395 	 inserting an entry at that point would be an alternative, but
7396 	 then we'd better discard the entry if we decided not to cache
7397 	 it.  */
7398       h = (struct tree_int_map **)
7399 	    htab_find_slot (annotate_value_cache, &in, INSERT);
7400       gcc_assert (!*h);
7401       *h = ggc_alloc_tree_int_map ();
7402       (*h)->base.from = gnu_size;
7403       (*h)->to = ret;
7404     }
7405 
7406   return ret;
7407 }
7408 
7409 /* Given GNAT_ENTITY, an object (constant, variable, parameter, exception)
7410    and GNU_TYPE, its corresponding GCC type, set Esize and Alignment to the
7411    size and alignment used by Gigi.  Prefer SIZE over TYPE_SIZE if non-null.
7412    BY_REF is true if the object is used by reference.  */
7413 
7414 void
annotate_object(Entity_Id gnat_entity,tree gnu_type,tree size,bool by_ref)7415 annotate_object (Entity_Id gnat_entity, tree gnu_type, tree size, bool by_ref)
7416 {
7417   if (by_ref)
7418     {
7419       if (TYPE_IS_FAT_POINTER_P (gnu_type))
7420 	gnu_type = TYPE_UNCONSTRAINED_ARRAY (gnu_type);
7421       else
7422 	gnu_type = TREE_TYPE (gnu_type);
7423     }
7424 
7425   if (Unknown_Esize (gnat_entity))
7426     {
7427       if (TREE_CODE (gnu_type) == RECORD_TYPE
7428 	  && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7429 	size = TYPE_SIZE (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type))));
7430       else if (!size)
7431 	size = TYPE_SIZE (gnu_type);
7432 
7433       if (size)
7434 	Set_Esize (gnat_entity, annotate_value (size));
7435     }
7436 
7437   if (Unknown_Alignment (gnat_entity))
7438     Set_Alignment (gnat_entity,
7439 		   UI_From_Int (TYPE_ALIGN (gnu_type) / BITS_PER_UNIT));
7440 }
7441 
7442 /* Return first element of field list whose TREE_PURPOSE is the same as ELEM.
7443    Return NULL_TREE if there is no such element in the list.  */
7444 
7445 static tree
purpose_member_field(const_tree elem,tree list)7446 purpose_member_field (const_tree elem, tree list)
7447 {
7448   while (list)
7449     {
7450       tree field = TREE_PURPOSE (list);
7451       if (SAME_FIELD_P (field, elem))
7452 	return list;
7453       list = TREE_CHAIN (list);
7454     }
7455   return NULL_TREE;
7456 }
7457 
7458 /* Given GNAT_ENTITY, a record type, and GNU_TYPE, its corresponding GCC type,
7459    set Component_Bit_Offset and Esize of the components to the position and
7460    size used by Gigi.  */
7461 
7462 static void
annotate_rep(Entity_Id gnat_entity,tree gnu_type)7463 annotate_rep (Entity_Id gnat_entity, tree gnu_type)
7464 {
7465   Entity_Id gnat_field;
7466   tree gnu_list;
7467 
7468   /* We operate by first making a list of all fields and their position (we
7469      can get the size easily) and then update all the sizes in the tree.  */
7470   gnu_list
7471     = build_position_list (gnu_type, false, size_zero_node, bitsize_zero_node,
7472 			   BIGGEST_ALIGNMENT, NULL_TREE);
7473 
7474   for (gnat_field = First_Entity (gnat_entity);
7475        Present (gnat_field);
7476        gnat_field = Next_Entity (gnat_field))
7477     if (Ekind (gnat_field) == E_Component
7478 	|| (Ekind (gnat_field) == E_Discriminant
7479 	    && !Is_Unchecked_Union (Scope (gnat_field))))
7480       {
7481 	tree t = purpose_member_field (gnat_to_gnu_field_decl (gnat_field),
7482 				       gnu_list);
7483 	if (t)
7484 	  {
7485 	    tree parent_offset;
7486 
7487 	    /* If we are just annotating types and the type is tagged, the tag
7488 	       and the parent components are not generated by the front-end so
7489 	       we need to add the appropriate offset to each component without
7490 	       representation clause.  */
7491 	    if (type_annotate_only
7492 		&& Is_Tagged_Type (gnat_entity)
7493 		&& No (Component_Clause (gnat_field)))
7494 	      {
7495 		/* For a component appearing in the current extension, the
7496 		   offset is the size of the parent.  */
7497 		if (Is_Derived_Type (gnat_entity)
7498 		    && Original_Record_Component (gnat_field) == gnat_field)
7499 		  parent_offset
7500 		    = UI_To_gnu (Esize (Etype (Base_Type (gnat_entity))),
7501 				 bitsizetype);
7502 		else
7503 		  parent_offset = bitsize_int (POINTER_SIZE);
7504 
7505 		if (TYPE_FIELDS (gnu_type))
7506 		  parent_offset
7507 		    = round_up (parent_offset,
7508 				DECL_ALIGN (TYPE_FIELDS (gnu_type)));
7509 	      }
7510 	    else
7511 	      parent_offset = bitsize_zero_node;
7512 
7513 	    Set_Component_Bit_Offset
7514 	      (gnat_field,
7515 	       annotate_value
7516 		 (size_binop (PLUS_EXPR,
7517 			      bit_from_pos (TREE_VEC_ELT (TREE_VALUE (t), 0),
7518 					    TREE_VEC_ELT (TREE_VALUE (t), 2)),
7519 			      parent_offset)));
7520 
7521 	    Set_Esize (gnat_field,
7522 		       annotate_value (DECL_SIZE (TREE_PURPOSE (t))));
7523 	  }
7524 	else if (Is_Tagged_Type (gnat_entity) && Is_Derived_Type (gnat_entity))
7525 	  {
7526 	    /* If there is no entry, this is an inherited component whose
7527 	       position is the same as in the parent type.  */
7528 	    Set_Component_Bit_Offset
7529 	      (gnat_field,
7530 	       Component_Bit_Offset (Original_Record_Component (gnat_field)));
7531 
7532 	    Set_Esize (gnat_field,
7533 		       Esize (Original_Record_Component (gnat_field)));
7534 	  }
7535       }
7536 }
7537 
7538 /* Scan all fields in GNU_TYPE and return a TREE_LIST where TREE_PURPOSE is
7539    the FIELD_DECL and TREE_VALUE a TREE_VEC containing the byte position, the
7540    value to be placed into DECL_OFFSET_ALIGN and the bit position.  The list
7541    of fields is flattened, except for variant parts if DO_NOT_FLATTEN_VARIANT
7542    is set to true.  GNU_POS is to be added to the position, GNU_BITPOS to the
7543    bit position, OFFSET_ALIGN is the present offset alignment.  GNU_LIST is a
7544    pre-existing list to be chained to the newly created entries.  */
7545 
7546 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)7547 build_position_list (tree gnu_type, bool do_not_flatten_variant, tree gnu_pos,
7548 		     tree gnu_bitpos, unsigned int offset_align, tree gnu_list)
7549 {
7550   tree gnu_field;
7551 
7552   for (gnu_field = TYPE_FIELDS (gnu_type);
7553        gnu_field;
7554        gnu_field = DECL_CHAIN (gnu_field))
7555     {
7556       tree gnu_our_bitpos = size_binop (PLUS_EXPR, gnu_bitpos,
7557 					DECL_FIELD_BIT_OFFSET (gnu_field));
7558       tree gnu_our_offset = size_binop (PLUS_EXPR, gnu_pos,
7559 					DECL_FIELD_OFFSET (gnu_field));
7560       unsigned int our_offset_align
7561 	= MIN (offset_align, DECL_OFFSET_ALIGN (gnu_field));
7562       tree v = make_tree_vec (3);
7563 
7564       TREE_VEC_ELT (v, 0) = gnu_our_offset;
7565       TREE_VEC_ELT (v, 1) = size_int (our_offset_align);
7566       TREE_VEC_ELT (v, 2) = gnu_our_bitpos;
7567       gnu_list = tree_cons (gnu_field, v, gnu_list);
7568 
7569       /* Recurse on internal fields, flattening the nested fields except for
7570 	 those in the variant part, if requested.  */
7571       if (DECL_INTERNAL_P (gnu_field))
7572 	{
7573 	  tree gnu_field_type = TREE_TYPE (gnu_field);
7574 	  if (do_not_flatten_variant
7575 	      && TREE_CODE (gnu_field_type) == QUAL_UNION_TYPE)
7576 	    gnu_list
7577 	      = build_position_list (gnu_field_type, do_not_flatten_variant,
7578 				     size_zero_node, bitsize_zero_node,
7579 				     BIGGEST_ALIGNMENT, gnu_list);
7580 	  else
7581 	    gnu_list
7582 	      = build_position_list (gnu_field_type, do_not_flatten_variant,
7583 				     gnu_our_offset, gnu_our_bitpos,
7584 				     our_offset_align, gnu_list);
7585 	}
7586     }
7587 
7588   return gnu_list;
7589 }
7590 
7591 /* Return a list describing the substitutions needed to reflect the
7592    discriminant substitutions from GNAT_TYPE to GNAT_SUBTYPE.  They can
7593    be in any order.  The values in an element of the list are in the form
7594    of operands to SUBSTITUTE_IN_EXPR.  DEFINITION is true if this is for
7595    a definition of GNAT_SUBTYPE.  */
7596 
7597 static vec<subst_pair>
build_subst_list(Entity_Id gnat_subtype,Entity_Id gnat_type,bool definition)7598 build_subst_list (Entity_Id gnat_subtype, Entity_Id gnat_type, bool definition)
7599 {
7600   vec<subst_pair> gnu_list = vNULL;
7601   Entity_Id gnat_discrim;
7602   Node_Id gnat_value;
7603 
7604   for (gnat_discrim = First_Stored_Discriminant (gnat_type),
7605        gnat_value = First_Elmt (Stored_Constraint (gnat_subtype));
7606        Present (gnat_discrim);
7607        gnat_discrim = Next_Stored_Discriminant (gnat_discrim),
7608        gnat_value = Next_Elmt (gnat_value))
7609     /* Ignore access discriminants.  */
7610     if (!Is_Access_Type (Etype (Node (gnat_value))))
7611       {
7612 	tree gnu_field = gnat_to_gnu_field_decl (gnat_discrim);
7613 	tree replacement = convert (TREE_TYPE (gnu_field),
7614 				    elaborate_expression
7615 				    (Node (gnat_value), gnat_subtype,
7616 				     get_entity_name (gnat_discrim),
7617 				     definition, true, false));
7618 	subst_pair s = {gnu_field, replacement};
7619 	gnu_list.safe_push (s);
7620       }
7621 
7622   return gnu_list;
7623 }
7624 
7625 /* Scan all fields in QUAL_UNION_TYPE and return a list describing the
7626    variants of QUAL_UNION_TYPE that are still relevant after applying
7627    the substitutions described in SUBST_LIST.  GNU_LIST is a pre-existing
7628    list to be prepended to the newly created entries.  */
7629 
7630 static vec<variant_desc>
build_variant_list(tree qual_union_type,vec<subst_pair> subst_list,vec<variant_desc> gnu_list)7631 build_variant_list (tree qual_union_type, vec<subst_pair> subst_list,
7632 		    vec<variant_desc> gnu_list)
7633 {
7634   tree gnu_field;
7635 
7636   for (gnu_field = TYPE_FIELDS (qual_union_type);
7637        gnu_field;
7638        gnu_field = DECL_CHAIN (gnu_field))
7639     {
7640       tree qual = DECL_QUALIFIER (gnu_field);
7641       unsigned int i;
7642       subst_pair *s;
7643 
7644       FOR_EACH_VEC_ELT (subst_list, i, s)
7645 	qual = SUBSTITUTE_IN_EXPR (qual, s->discriminant, s->replacement);
7646 
7647       /* If the new qualifier is not unconditionally false, its variant may
7648 	 still be accessed.  */
7649       if (!integer_zerop (qual))
7650 	{
7651 	  tree variant_type = TREE_TYPE (gnu_field), variant_subpart;
7652 	  variant_desc v = {variant_type, gnu_field, qual, NULL_TREE};
7653 
7654 	  gnu_list.safe_push (v);
7655 
7656 	  /* Recurse on the variant subpart of the variant, if any.  */
7657 	  variant_subpart = get_variant_part (variant_type);
7658 	  if (variant_subpart)
7659 	    gnu_list = build_variant_list (TREE_TYPE (variant_subpart),
7660 					   subst_list, gnu_list);
7661 
7662 	  /* If the new qualifier is unconditionally true, the subsequent
7663 	     variants cannot be accessed.  */
7664 	  if (integer_onep (qual))
7665 	    break;
7666 	}
7667     }
7668 
7669   return gnu_list;
7670 }
7671 
7672 /* UINT_SIZE is a Uint giving the specified size for an object of GNU_TYPE
7673    corresponding to GNAT_OBJECT.  If the size is valid, return an INTEGER_CST
7674    corresponding to its value.  Otherwise, return NULL_TREE.  KIND is set to
7675    VAR_DECL if we are specifying the size of an object, TYPE_DECL for the
7676    size of a type, and FIELD_DECL for the size of a field.  COMPONENT_P is
7677    true if we are being called to process the Component_Size of GNAT_OBJECT;
7678    this is used only for error messages.  ZERO_OK is true if a size of zero
7679    is permitted; if ZERO_OK is false, it means that a size of zero should be
7680    treated as an unspecified size.  */
7681 
7682 static tree
validate_size(Uint uint_size,tree gnu_type,Entity_Id gnat_object,enum tree_code kind,bool component_p,bool zero_ok)7683 validate_size (Uint uint_size, tree gnu_type, Entity_Id gnat_object,
7684 	       enum tree_code kind, bool component_p, bool zero_ok)
7685 {
7686   Node_Id gnat_error_node;
7687   tree type_size, size;
7688 
7689   /* Return 0 if no size was specified.  */
7690   if (uint_size == No_Uint)
7691     return NULL_TREE;
7692 
7693   /* Ignore a negative size since that corresponds to our back-annotation.  */
7694   if (UI_Lt (uint_size, Uint_0))
7695     return NULL_TREE;
7696 
7697   /* Find the node to use for error messages.  */
7698   if ((Ekind (gnat_object) == E_Component
7699        || Ekind (gnat_object) == E_Discriminant)
7700       && Present (Component_Clause (gnat_object)))
7701     gnat_error_node = Last_Bit (Component_Clause (gnat_object));
7702   else if (Present (Size_Clause (gnat_object)))
7703     gnat_error_node = Expression (Size_Clause (gnat_object));
7704   else
7705     gnat_error_node = gnat_object;
7706 
7707   /* Get the size as an INTEGER_CST.  Issue an error if a size was specified
7708      but cannot be represented in bitsizetype.  */
7709   size = UI_To_gnu (uint_size, bitsizetype);
7710   if (TREE_OVERFLOW (size))
7711     {
7712       if (component_p)
7713 	post_error_ne ("component size for& is too large", gnat_error_node,
7714 		       gnat_object);
7715       else
7716 	post_error_ne ("size for& is too large", gnat_error_node,
7717 		       gnat_object);
7718       return NULL_TREE;
7719     }
7720 
7721   /* Ignore a zero size if it is not permitted.  */
7722   if (!zero_ok && integer_zerop (size))
7723     return NULL_TREE;
7724 
7725   /* The size of objects is always a multiple of a byte.  */
7726   if (kind == VAR_DECL
7727       && !integer_zerop (size_binop (TRUNC_MOD_EXPR, size, bitsize_unit_node)))
7728     {
7729       if (component_p)
7730 	post_error_ne ("component size for& is not a multiple of Storage_Unit",
7731 		       gnat_error_node, gnat_object);
7732       else
7733 	post_error_ne ("size for& is not a multiple of Storage_Unit",
7734 		       gnat_error_node, gnat_object);
7735       return NULL_TREE;
7736     }
7737 
7738   /* If this is an integral type or a packed array type, the front-end has
7739      already verified the size, so we need not do it here (which would mean
7740      checking against the bounds).  However, if this is an aliased object,
7741      it may not be smaller than the type of the object.  */
7742   if ((INTEGRAL_TYPE_P (gnu_type) || TYPE_IS_PACKED_ARRAY_TYPE_P (gnu_type))
7743       && !(kind == VAR_DECL && Is_Aliased (gnat_object)))
7744     return size;
7745 
7746   /* If the object is a record that contains a template, add the size of the
7747      template to the specified size.  */
7748   if (TREE_CODE (gnu_type) == RECORD_TYPE
7749       && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
7750     size = size_binop (PLUS_EXPR, DECL_SIZE (TYPE_FIELDS (gnu_type)), size);
7751 
7752   if (kind == VAR_DECL
7753       /* If a type needs strict alignment, a component of this type in
7754 	 a packed record cannot be packed and thus uses the type size.  */
7755       || (kind == TYPE_DECL && Strict_Alignment (gnat_object)))
7756     type_size = TYPE_SIZE (gnu_type);
7757   else
7758     type_size = rm_size (gnu_type);
7759 
7760   /* Modify the size of a discriminated type to be the maximum size.  */
7761   if (type_size && CONTAINS_PLACEHOLDER_P (type_size))
7762     type_size = max_size (type_size, true);
7763 
7764   /* If this is an access type or a fat pointer, the minimum size is that given
7765      by the smallest integral mode that's valid for pointers.  */
7766   if (TREE_CODE (gnu_type) == POINTER_TYPE || TYPE_IS_FAT_POINTER_P (gnu_type))
7767     {
7768       enum machine_mode p_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
7769       while (!targetm.valid_pointer_mode (p_mode))
7770 	p_mode = GET_MODE_WIDER_MODE (p_mode);
7771       type_size = bitsize_int (GET_MODE_BITSIZE (p_mode));
7772     }
7773 
7774   /* Issue an error either if the default size of the object isn't a constant
7775      or if the new size is smaller than it.  */
7776   if (TREE_CODE (type_size) != INTEGER_CST
7777       || TREE_OVERFLOW (type_size)
7778       || tree_int_cst_lt (size, type_size))
7779     {
7780       if (component_p)
7781 	post_error_ne_tree
7782 	  ("component size for& too small{, minimum allowed is ^}",
7783 	   gnat_error_node, gnat_object, type_size);
7784       else
7785 	post_error_ne_tree
7786 	  ("size for& too small{, minimum allowed is ^}",
7787 	   gnat_error_node, gnat_object, type_size);
7788       return NULL_TREE;
7789     }
7790 
7791   return size;
7792 }
7793 
7794 /* Similarly, but both validate and process a value of RM size.  This routine
7795    is only called for types.  */
7796 
7797 static void
set_rm_size(Uint uint_size,tree gnu_type,Entity_Id gnat_entity)7798 set_rm_size (Uint uint_size, tree gnu_type, Entity_Id gnat_entity)
7799 {
7800   Node_Id gnat_attr_node;
7801   tree old_size, size;
7802 
7803   /* Do nothing if no size was specified.  */
7804   if (uint_size == No_Uint)
7805     return;
7806 
7807   /* Ignore a negative size since that corresponds to our back-annotation.  */
7808   if (UI_Lt (uint_size, Uint_0))
7809     return;
7810 
7811   /* Only issue an error if a Value_Size clause was explicitly given.
7812      Otherwise, we'd be duplicating an error on the Size clause.  */
7813   gnat_attr_node
7814     = Get_Attribute_Definition_Clause (gnat_entity, Attr_Value_Size);
7815 
7816   /* Get the size as an INTEGER_CST.  Issue an error if a size was specified
7817      but cannot be represented in bitsizetype.  */
7818   size = UI_To_gnu (uint_size, bitsizetype);
7819   if (TREE_OVERFLOW (size))
7820     {
7821       if (Present (gnat_attr_node))
7822 	post_error_ne ("Value_Size for& is too large", gnat_attr_node,
7823 		       gnat_entity);
7824       return;
7825     }
7826 
7827   /* Ignore a zero size unless a Value_Size clause exists, or a size clause
7828      exists, or this is an integer type, in which case the front-end will
7829      have always set it.  */
7830   if (No (gnat_attr_node)
7831       && integer_zerop (size)
7832       && !Has_Size_Clause (gnat_entity)
7833       && !Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7834     return;
7835 
7836   old_size = rm_size (gnu_type);
7837 
7838   /* If the old size is self-referential, get the maximum size.  */
7839   if (CONTAINS_PLACEHOLDER_P (old_size))
7840     old_size = max_size (old_size, true);
7841 
7842   /* Issue an error either if the old size of the object isn't a constant or
7843      if the new size is smaller than it.  The front-end has already verified
7844      this for scalar and packed array types.  */
7845   if (TREE_CODE (old_size) != INTEGER_CST
7846       || TREE_OVERFLOW (old_size)
7847       || (AGGREGATE_TYPE_P (gnu_type)
7848 	  && !(TREE_CODE (gnu_type) == ARRAY_TYPE
7849 	       && TYPE_PACKED_ARRAY_TYPE_P (gnu_type))
7850 	  && !(TYPE_IS_PADDING_P (gnu_type)
7851 	       && TREE_CODE (TREE_TYPE (TYPE_FIELDS (gnu_type))) == ARRAY_TYPE
7852 	       && TYPE_PACKED_ARRAY_TYPE_P
7853 		  (TREE_TYPE (TYPE_FIELDS (gnu_type))))
7854 	  && tree_int_cst_lt (size, old_size)))
7855     {
7856       if (Present (gnat_attr_node))
7857 	post_error_ne_tree
7858 	  ("Value_Size for& too small{, minimum allowed is ^}",
7859 	   gnat_attr_node, gnat_entity, old_size);
7860       return;
7861     }
7862 
7863   /* Otherwise, set the RM size proper for integral types...  */
7864   if ((TREE_CODE (gnu_type) == INTEGER_TYPE
7865        && Is_Discrete_Or_Fixed_Point_Type (gnat_entity))
7866       || (TREE_CODE (gnu_type) == ENUMERAL_TYPE
7867 	  || TREE_CODE (gnu_type) == BOOLEAN_TYPE))
7868     SET_TYPE_RM_SIZE (gnu_type, size);
7869 
7870   /* ...or the Ada size for record and union types.  */
7871   else if (RECORD_OR_UNION_TYPE_P (gnu_type)
7872 	   && !TYPE_FAT_POINTER_P (gnu_type))
7873     SET_TYPE_ADA_SIZE (gnu_type, size);
7874 }
7875 
7876 /* ALIGNMENT is a Uint giving the alignment specified for GNAT_ENTITY,
7877    a type or object whose present alignment is ALIGN.  If this alignment is
7878    valid, return it.  Otherwise, give an error and return ALIGN.  */
7879 
7880 static unsigned int
validate_alignment(Uint alignment,Entity_Id gnat_entity,unsigned int align)7881 validate_alignment (Uint alignment, Entity_Id gnat_entity, unsigned int align)
7882 {
7883   unsigned int max_allowed_alignment = get_target_maximum_allowed_alignment ();
7884   unsigned int new_align;
7885   Node_Id gnat_error_node;
7886 
7887   /* Don't worry about checking alignment if alignment was not specified
7888      by the source program and we already posted an error for this entity.  */
7889   if (Error_Posted (gnat_entity) && !Has_Alignment_Clause (gnat_entity))
7890     return align;
7891 
7892   /* Post the error on the alignment clause if any.  Note, for the implicit
7893      base type of an array type, the alignment clause is on the first
7894      subtype.  */
7895   if (Present (Alignment_Clause (gnat_entity)))
7896     gnat_error_node = Expression (Alignment_Clause (gnat_entity));
7897 
7898   else if (Is_Itype (gnat_entity)
7899            && Is_Array_Type (gnat_entity)
7900            && Etype (gnat_entity) == gnat_entity
7901            && Present (Alignment_Clause (First_Subtype (gnat_entity))))
7902     gnat_error_node =
7903       Expression (Alignment_Clause (First_Subtype (gnat_entity)));
7904 
7905   else
7906     gnat_error_node = gnat_entity;
7907 
7908   /* Within GCC, an alignment is an integer, so we must make sure a value is
7909      specified that fits in that range.  Also, there is an upper bound to
7910      alignments we can support/allow.  */
7911   if (!UI_Is_In_Int_Range (alignment)
7912       || ((new_align = UI_To_Int (alignment)) > max_allowed_alignment))
7913     post_error_ne_num ("largest supported alignment for& is ^",
7914 		       gnat_error_node, gnat_entity, max_allowed_alignment);
7915   else if (!(Present (Alignment_Clause (gnat_entity))
7916 	     && From_At_Mod (Alignment_Clause (gnat_entity)))
7917 	   && new_align * BITS_PER_UNIT < align)
7918     {
7919       unsigned int double_align;
7920       bool is_capped_double, align_clause;
7921 
7922       /* If the default alignment of "double" or larger scalar types is
7923 	 specifically capped and the new alignment is above the cap, do
7924 	 not post an error and change the alignment only if there is an
7925 	 alignment clause; this makes it possible to have the associated
7926 	 GCC type overaligned by default for performance reasons.  */
7927       if ((double_align = double_float_alignment) > 0)
7928 	{
7929 	  Entity_Id gnat_type
7930 	    = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7931 	  is_capped_double
7932 	    = is_double_float_or_array (gnat_type, &align_clause);
7933 	}
7934       else if ((double_align = double_scalar_alignment) > 0)
7935 	{
7936 	  Entity_Id gnat_type
7937 	    = Is_Type (gnat_entity) ? gnat_entity : Etype (gnat_entity);
7938 	  is_capped_double
7939 	    = is_double_scalar_or_array (gnat_type, &align_clause);
7940 	}
7941       else
7942 	is_capped_double = align_clause = false;
7943 
7944       if (is_capped_double && new_align >= double_align)
7945 	{
7946 	  if (align_clause)
7947 	    align = new_align * BITS_PER_UNIT;
7948 	}
7949       else
7950 	{
7951 	  if (is_capped_double)
7952 	    align = double_align * BITS_PER_UNIT;
7953 
7954 	  post_error_ne_num ("alignment for& must be at least ^",
7955 			     gnat_error_node, gnat_entity,
7956 			     align / BITS_PER_UNIT);
7957 	}
7958     }
7959   else
7960     {
7961       new_align = (new_align > 0 ? new_align * BITS_PER_UNIT : 1);
7962       if (new_align > align)
7963 	align = new_align;
7964     }
7965 
7966   return align;
7967 }
7968 
7969 /* Verify that OBJECT, a type or decl, is something we can implement
7970    atomically.  If not, give an error for GNAT_ENTITY.  COMP_P is true
7971    if we require atomic components.  */
7972 
7973 static void
check_ok_for_atomic(tree object,Entity_Id gnat_entity,bool comp_p)7974 check_ok_for_atomic (tree object, Entity_Id gnat_entity, bool comp_p)
7975 {
7976   Node_Id gnat_error_point = gnat_entity;
7977   Node_Id gnat_node;
7978   enum machine_mode mode;
7979   unsigned int align;
7980   tree size;
7981 
7982   /* There are three case of what OBJECT can be.  It can be a type, in which
7983      case we take the size, alignment and mode from the type.  It can be a
7984      declaration that was indirect, in which case the relevant values are
7985      that of the type being pointed to, or it can be a normal declaration,
7986      in which case the values are of the decl.  The code below assumes that
7987      OBJECT is either a type or a decl.  */
7988   if (TYPE_P (object))
7989     {
7990       /* If this is an anonymous base type, nothing to check.  Error will be
7991 	 reported on the source type.  */
7992       if (!Comes_From_Source (gnat_entity))
7993 	return;
7994 
7995       mode = TYPE_MODE (object);
7996       align = TYPE_ALIGN (object);
7997       size = TYPE_SIZE (object);
7998     }
7999   else if (DECL_BY_REF_P (object))
8000     {
8001       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (object)));
8002       align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (object)));
8003       size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (object)));
8004     }
8005   else
8006     {
8007       mode = DECL_MODE (object);
8008       align = DECL_ALIGN (object);
8009       size = DECL_SIZE (object);
8010     }
8011 
8012   /* Consider all floating-point types atomic and any types that that are
8013      represented by integers no wider than a machine word.  */
8014   if (GET_MODE_CLASS (mode) == MODE_FLOAT
8015       || ((GET_MODE_CLASS (mode) == MODE_INT
8016 	   || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
8017 	  && GET_MODE_BITSIZE (mode) <= BITS_PER_WORD))
8018     return;
8019 
8020   /* For the moment, also allow anything that has an alignment equal
8021      to its size and which is smaller than a word.  */
8022   if (size && TREE_CODE (size) == INTEGER_CST
8023       && compare_tree_int (size, align) == 0
8024       && align <= BITS_PER_WORD)
8025     return;
8026 
8027   for (gnat_node = First_Rep_Item (gnat_entity); Present (gnat_node);
8028        gnat_node = Next_Rep_Item (gnat_node))
8029     {
8030       if (!comp_p && Nkind (gnat_node) == N_Pragma
8031 	  && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8032               == Pragma_Atomic))
8033 	gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8034       else if (comp_p && Nkind (gnat_node) == N_Pragma
8035 	       && (Get_Pragma_Id (Chars (Pragma_Identifier (gnat_node)))
8036 		   == Pragma_Atomic_Components))
8037 	gnat_error_point = First (Pragma_Argument_Associations (gnat_node));
8038     }
8039 
8040   if (comp_p)
8041     post_error_ne ("atomic access to component of & cannot be guaranteed",
8042 		   gnat_error_point, gnat_entity);
8043   else
8044     post_error_ne ("atomic access to & cannot be guaranteed",
8045 		   gnat_error_point, gnat_entity);
8046 }
8047 
8048 
8049 /* Helper for the intrin compatibility checks family.  Evaluate whether
8050    two types are definitely incompatible.  */
8051 
8052 static bool
intrin_types_incompatible_p(tree t1,tree t2)8053 intrin_types_incompatible_p (tree t1, tree t2)
8054 {
8055   enum tree_code code;
8056 
8057   if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
8058     return false;
8059 
8060   if (TYPE_MODE (t1) != TYPE_MODE (t2))
8061     return true;
8062 
8063   if (TREE_CODE (t1) != TREE_CODE (t2))
8064     return true;
8065 
8066   code = TREE_CODE (t1);
8067 
8068   switch (code)
8069     {
8070     case INTEGER_TYPE:
8071     case REAL_TYPE:
8072       return TYPE_PRECISION (t1) != TYPE_PRECISION (t2);
8073 
8074     case POINTER_TYPE:
8075     case REFERENCE_TYPE:
8076       /* Assume designated types are ok.  We'd need to account for char * and
8077 	 void * variants to do better, which could rapidly get messy and isn't
8078 	 clearly worth the effort.  */
8079       return false;
8080 
8081     default:
8082       break;
8083     }
8084 
8085   return false;
8086 }
8087 
8088 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8089    on the Ada/builtin argument lists for the INB binding.  */
8090 
8091 static bool
intrin_arglists_compatible_p(intrin_binding_t * inb)8092 intrin_arglists_compatible_p (intrin_binding_t * inb)
8093 {
8094   function_args_iterator ada_iter, btin_iter;
8095 
8096   function_args_iter_init (&ada_iter, inb->ada_fntype);
8097   function_args_iter_init (&btin_iter, inb->btin_fntype);
8098 
8099   /* Sequence position of the last argument we checked.  */
8100   int argpos = 0;
8101 
8102   while (1)
8103     {
8104       tree ada_type = function_args_iter_cond (&ada_iter);
8105       tree btin_type = function_args_iter_cond (&btin_iter);
8106 
8107       /* If we've exhausted both lists simultaneously, we're done.  */
8108       if (ada_type == NULL_TREE && btin_type == NULL_TREE)
8109 	break;
8110 
8111       /* If one list is shorter than the other, they fail to match.  */
8112       if (ada_type == NULL_TREE || btin_type == NULL_TREE)
8113 	return false;
8114 
8115       /* If we're done with the Ada args and not with the internal builtin
8116 	 args, or the other way around, complain.  */
8117       if (ada_type == void_type_node
8118 	  && btin_type != void_type_node)
8119 	{
8120 	  post_error ("?Ada arguments list too short!", inb->gnat_entity);
8121 	  return false;
8122 	}
8123 
8124       if (btin_type == void_type_node
8125 	  && ada_type != void_type_node)
8126 	{
8127 	  post_error_ne_num ("?Ada arguments list too long ('> ^)!",
8128 			     inb->gnat_entity, inb->gnat_entity, argpos);
8129 	  return false;
8130 	}
8131 
8132       /* Otherwise, check that types match for the current argument.  */
8133       argpos ++;
8134       if (intrin_types_incompatible_p (ada_type, btin_type))
8135 	{
8136 	  post_error_ne_num ("?intrinsic binding type mismatch on argument ^!",
8137 			     inb->gnat_entity, inb->gnat_entity, argpos);
8138 	  return false;
8139 	}
8140 
8141 
8142       function_args_iter_next (&ada_iter);
8143       function_args_iter_next (&btin_iter);
8144     }
8145 
8146   return true;
8147 }
8148 
8149 /* Helper for intrin_profiles_compatible_p, to perform compatibility checks
8150    on the Ada/builtin return values for the INB binding.  */
8151 
8152 static bool
intrin_return_compatible_p(intrin_binding_t * inb)8153 intrin_return_compatible_p (intrin_binding_t * inb)
8154 {
8155   tree ada_return_type = TREE_TYPE (inb->ada_fntype);
8156   tree btin_return_type = TREE_TYPE (inb->btin_fntype);
8157 
8158   /* Accept function imported as procedure, common and convenient.  */
8159   if (VOID_TYPE_P (ada_return_type)
8160       && !VOID_TYPE_P (btin_return_type))
8161     return true;
8162 
8163   /* If return type is Address (integer type), map it to void *.  */
8164   if (Is_Descendent_Of_Address (Etype (inb->gnat_entity)))
8165     ada_return_type = ptr_void_type_node;
8166 
8167   /* Check return types compatibility otherwise.  Note that this
8168      handles void/void as well.  */
8169   if (intrin_types_incompatible_p (btin_return_type, ada_return_type))
8170     {
8171       post_error ("?intrinsic binding type mismatch on return value!",
8172 		  inb->gnat_entity);
8173       return false;
8174     }
8175 
8176   return true;
8177 }
8178 
8179 /* Check and return whether the Ada and gcc builtin profiles bound by INB are
8180    compatible.  Issue relevant warnings when they are not.
8181 
8182    This is intended as a light check to diagnose the most obvious cases, not
8183    as a full fledged type compatibility predicate.  It is the programmer's
8184    responsibility to ensure correctness of the Ada declarations in Imports,
8185    especially when binding straight to a compiler internal.  */
8186 
8187 static bool
intrin_profiles_compatible_p(intrin_binding_t * inb)8188 intrin_profiles_compatible_p (intrin_binding_t * inb)
8189 {
8190   /* Check compatibility on return values and argument lists, each responsible
8191      for posting warnings as appropriate.  Ensure use of the proper sloc for
8192      this purpose.  */
8193 
8194   bool arglists_compatible_p, return_compatible_p;
8195   location_t saved_location = input_location;
8196 
8197   Sloc_to_locus (Sloc (inb->gnat_entity), &input_location);
8198 
8199   return_compatible_p = intrin_return_compatible_p (inb);
8200   arglists_compatible_p = intrin_arglists_compatible_p (inb);
8201 
8202   input_location = saved_location;
8203 
8204   return return_compatible_p && arglists_compatible_p;
8205 }
8206 
8207 /* Return a FIELD_DECL node modeled on OLD_FIELD.  FIELD_TYPE is its type
8208    and RECORD_TYPE is the type of the parent.  If SIZE is nonzero, it is the
8209    specified size for this field.  POS_LIST is a position list describing
8210    the layout of OLD_FIELD and SUBST_LIST a substitution list to be applied
8211    to this layout.  */
8212 
8213 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)8214 create_field_decl_from (tree old_field, tree field_type, tree record_type,
8215 			tree size, tree pos_list,
8216 			vec<subst_pair> subst_list)
8217 {
8218   tree t = TREE_VALUE (purpose_member (old_field, pos_list));
8219   tree pos = TREE_VEC_ELT (t, 0), bitpos = TREE_VEC_ELT (t, 2);
8220   unsigned int offset_align = tree_low_cst (TREE_VEC_ELT (t, 1), 1);
8221   tree new_pos, new_field;
8222   unsigned int i;
8223   subst_pair *s;
8224 
8225   if (CONTAINS_PLACEHOLDER_P (pos))
8226     FOR_EACH_VEC_ELT (subst_list, i, s)
8227       pos = SUBSTITUTE_IN_EXPR (pos, s->discriminant, s->replacement);
8228 
8229   /* If the position is now a constant, we can set it as the position of the
8230      field when we make it.  Otherwise, we need to deal with it specially.  */
8231   if (TREE_CONSTANT (pos))
8232     new_pos = bit_from_pos (pos, bitpos);
8233   else
8234     new_pos = NULL_TREE;
8235 
8236   new_field
8237     = create_field_decl (DECL_NAME (old_field), field_type, record_type,
8238 			 size, new_pos, DECL_PACKED (old_field),
8239 			 !DECL_NONADDRESSABLE_P (old_field));
8240 
8241   if (!new_pos)
8242     {
8243       normalize_offset (&pos, &bitpos, offset_align);
8244       DECL_FIELD_OFFSET (new_field) = pos;
8245       DECL_FIELD_BIT_OFFSET (new_field) = bitpos;
8246       SET_DECL_OFFSET_ALIGN (new_field, offset_align);
8247       DECL_SIZE (new_field) = size;
8248       DECL_SIZE_UNIT (new_field)
8249 	= convert (sizetype,
8250 		   size_binop (CEIL_DIV_EXPR, size, bitsize_unit_node));
8251       layout_decl (new_field, DECL_OFFSET_ALIGN (new_field));
8252     }
8253 
8254   DECL_INTERNAL_P (new_field) = DECL_INTERNAL_P (old_field);
8255   SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, old_field);
8256   DECL_DISCRIMINANT_NUMBER (new_field) = DECL_DISCRIMINANT_NUMBER (old_field);
8257   TREE_THIS_VOLATILE (new_field) = TREE_THIS_VOLATILE (old_field);
8258 
8259   return new_field;
8260 }
8261 
8262 /* Create the REP part of RECORD_TYPE with REP_TYPE.  If MIN_SIZE is nonzero,
8263    it is the minimal size the REP_PART must have.  */
8264 
8265 static tree
create_rep_part(tree rep_type,tree record_type,tree min_size)8266 create_rep_part (tree rep_type, tree record_type, tree min_size)
8267 {
8268   tree field;
8269 
8270   if (min_size && !tree_int_cst_lt (TYPE_SIZE (rep_type), min_size))
8271     min_size = NULL_TREE;
8272 
8273   field = create_field_decl (get_identifier ("REP"), rep_type, record_type,
8274 			     min_size, bitsize_zero_node, 0, 1);
8275   DECL_INTERNAL_P (field) = 1;
8276 
8277   return field;
8278 }
8279 
8280 /* Return the REP part of RECORD_TYPE, if any.  Otherwise return NULL.  */
8281 
8282 static tree
get_rep_part(tree record_type)8283 get_rep_part (tree record_type)
8284 {
8285   tree field = TYPE_FIELDS (record_type);
8286 
8287   /* The REP part is the first field, internal, another record, and its name
8288      starts with an 'R'.  */
8289   if (field
8290       && DECL_INTERNAL_P (field)
8291       && TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
8292       && IDENTIFIER_POINTER (DECL_NAME (field)) [0] == 'R')
8293     return field;
8294 
8295   return NULL_TREE;
8296 }
8297 
8298 /* Return the variant part of RECORD_TYPE, if any.  Otherwise return NULL.  */
8299 
8300 tree
get_variant_part(tree record_type)8301 get_variant_part (tree record_type)
8302 {
8303   tree field;
8304 
8305   /* The variant part is the only internal field that is a qualified union.  */
8306   for (field = TYPE_FIELDS (record_type); field; field = DECL_CHAIN (field))
8307     if (DECL_INTERNAL_P (field)
8308 	&& TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE)
8309       return field;
8310 
8311   return NULL_TREE;
8312 }
8313 
8314 /* Return a new variant part modeled on OLD_VARIANT_PART.  VARIANT_LIST is
8315    the list of variants to be used and RECORD_TYPE is the type of the parent.
8316    POS_LIST is a position list describing the layout of fields present in
8317    OLD_VARIANT_PART and SUBST_LIST a substitution list to be applied to this
8318    layout.  */
8319 
8320 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)8321 create_variant_part_from (tree old_variant_part,
8322 			  vec<variant_desc> variant_list,
8323 			  tree record_type, tree pos_list,
8324 			  vec<subst_pair> subst_list)
8325 {
8326   tree offset = DECL_FIELD_OFFSET (old_variant_part);
8327   tree old_union_type = TREE_TYPE (old_variant_part);
8328   tree new_union_type, new_variant_part;
8329   tree union_field_list = NULL_TREE;
8330   variant_desc *v;
8331   unsigned int i;
8332 
8333   /* First create the type of the variant part from that of the old one.  */
8334   new_union_type = make_node (QUAL_UNION_TYPE);
8335   TYPE_NAME (new_union_type)
8336     = concat_name (TYPE_NAME (record_type),
8337 		   IDENTIFIER_POINTER (DECL_NAME (old_variant_part)));
8338 
8339   /* If the position of the variant part is constant, subtract it from the
8340      size of the type of the parent to get the new size.  This manual CSE
8341      reduces the code size when not optimizing.  */
8342   if (TREE_CODE (offset) == INTEGER_CST)
8343     {
8344       tree bitpos = DECL_FIELD_BIT_OFFSET (old_variant_part);
8345       tree first_bit = bit_from_pos (offset, bitpos);
8346       TYPE_SIZE (new_union_type)
8347 	= size_binop (MINUS_EXPR, TYPE_SIZE (record_type), first_bit);
8348       TYPE_SIZE_UNIT (new_union_type)
8349 	= size_binop (MINUS_EXPR, TYPE_SIZE_UNIT (record_type),
8350 		      byte_from_pos (offset, bitpos));
8351       SET_TYPE_ADA_SIZE (new_union_type,
8352 			 size_binop (MINUS_EXPR, TYPE_ADA_SIZE (record_type),
8353  				     first_bit));
8354       TYPE_ALIGN (new_union_type) = TYPE_ALIGN (old_union_type);
8355       relate_alias_sets (new_union_type, old_union_type, ALIAS_SET_COPY);
8356     }
8357   else
8358     copy_and_substitute_in_size (new_union_type, old_union_type, subst_list);
8359 
8360   /* Now finish up the new variants and populate the union type.  */
8361   FOR_EACH_VEC_ELT_REVERSE (variant_list, i, v)
8362     {
8363       tree old_field = v->field, new_field;
8364       tree old_variant, old_variant_subpart, new_variant, field_list;
8365 
8366       /* Skip variants that don't belong to this nesting level.  */
8367       if (DECL_CONTEXT (old_field) != old_union_type)
8368 	continue;
8369 
8370       /* Retrieve the list of fields already added to the new variant.  */
8371       new_variant = v->new_type;
8372       field_list = TYPE_FIELDS (new_variant);
8373 
8374       /* If the old variant had a variant subpart, we need to create a new
8375 	 variant subpart and add it to the field list.  */
8376       old_variant = v->type;
8377       old_variant_subpart = get_variant_part (old_variant);
8378       if (old_variant_subpart)
8379 	{
8380 	  tree new_variant_subpart
8381 	    = create_variant_part_from (old_variant_subpart, variant_list,
8382 					new_variant, pos_list, subst_list);
8383 	  DECL_CHAIN (new_variant_subpart) = field_list;
8384 	  field_list = new_variant_subpart;
8385 	}
8386 
8387       /* Finish up the new variant and create the field.  No need for debug
8388 	 info thanks to the XVS type.  */
8389       finish_record_type (new_variant, nreverse (field_list), 2, false);
8390       compute_record_mode (new_variant);
8391       create_type_decl (TYPE_NAME (new_variant), new_variant, NULL,
8392 			true, false, Empty);
8393 
8394       new_field
8395 	= create_field_decl_from (old_field, new_variant, new_union_type,
8396 				  TYPE_SIZE (new_variant),
8397 				  pos_list, subst_list);
8398       DECL_QUALIFIER (new_field) = v->qual;
8399       DECL_INTERNAL_P (new_field) = 1;
8400       DECL_CHAIN (new_field) = union_field_list;
8401       union_field_list = new_field;
8402     }
8403 
8404   /* Finish up the union type and create the variant part.  No need for debug
8405      info thanks to the XVS type.  Note that we don't reverse the field list
8406      because VARIANT_LIST has been traversed in reverse order.  */
8407   finish_record_type (new_union_type, union_field_list, 2, false);
8408   compute_record_mode (new_union_type);
8409   create_type_decl (TYPE_NAME (new_union_type), new_union_type, NULL,
8410 		    true, false, Empty);
8411 
8412   new_variant_part
8413     = create_field_decl_from (old_variant_part, new_union_type, record_type,
8414 			      TYPE_SIZE (new_union_type),
8415 			      pos_list, subst_list);
8416   DECL_INTERNAL_P (new_variant_part) = 1;
8417 
8418   /* With multiple discriminants it is possible for an inner variant to be
8419      statically selected while outer ones are not; in this case, the list
8420      of fields of the inner variant is not flattened and we end up with a
8421      qualified union with a single member.  Drop the useless container.  */
8422   if (!DECL_CHAIN (union_field_list))
8423     {
8424       DECL_CONTEXT (union_field_list) = record_type;
8425       DECL_FIELD_OFFSET (union_field_list)
8426 	= DECL_FIELD_OFFSET (new_variant_part);
8427       DECL_FIELD_BIT_OFFSET (union_field_list)
8428 	= DECL_FIELD_BIT_OFFSET (new_variant_part);
8429       SET_DECL_OFFSET_ALIGN (union_field_list,
8430 			     DECL_OFFSET_ALIGN (new_variant_part));
8431       new_variant_part = union_field_list;
8432     }
8433 
8434   return new_variant_part;
8435 }
8436 
8437 /* Copy the size (and alignment and alias set) from OLD_TYPE to NEW_TYPE,
8438    which are both RECORD_TYPE, after applying the substitutions described
8439    in SUBST_LIST.  */
8440 
8441 static void
copy_and_substitute_in_size(tree new_type,tree old_type,vec<subst_pair> subst_list)8442 copy_and_substitute_in_size (tree new_type, tree old_type,
8443 			     vec<subst_pair> subst_list)
8444 {
8445   unsigned int i;
8446   subst_pair *s;
8447 
8448   TYPE_SIZE (new_type) = TYPE_SIZE (old_type);
8449   TYPE_SIZE_UNIT (new_type) = TYPE_SIZE_UNIT (old_type);
8450   SET_TYPE_ADA_SIZE (new_type, TYPE_ADA_SIZE (old_type));
8451   TYPE_ALIGN (new_type) = TYPE_ALIGN (old_type);
8452   relate_alias_sets (new_type, old_type, ALIAS_SET_COPY);
8453 
8454   if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (new_type)))
8455     FOR_EACH_VEC_ELT (subst_list, i, s)
8456       TYPE_SIZE (new_type)
8457 	= SUBSTITUTE_IN_EXPR (TYPE_SIZE (new_type),
8458 			      s->discriminant, s->replacement);
8459 
8460   if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (new_type)))
8461     FOR_EACH_VEC_ELT (subst_list, i, s)
8462       TYPE_SIZE_UNIT (new_type)
8463 	= SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (new_type),
8464 			      s->discriminant, s->replacement);
8465 
8466   if (CONTAINS_PLACEHOLDER_P (TYPE_ADA_SIZE (new_type)))
8467     FOR_EACH_VEC_ELT (subst_list, i, s)
8468       SET_TYPE_ADA_SIZE
8469 	(new_type, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (new_type),
8470 				       s->discriminant, s->replacement));
8471 
8472   /* Finalize the size.  */
8473   TYPE_SIZE (new_type) = variable_size (TYPE_SIZE (new_type));
8474   TYPE_SIZE_UNIT (new_type) = variable_size (TYPE_SIZE_UNIT (new_type));
8475 }
8476 
8477 /* Given a type T, a FIELD_DECL F, and a replacement value R, return a
8478    type with all size expressions that contain F in a PLACEHOLDER_EXPR
8479    updated by replacing F with R.
8480 
8481    The function doesn't update the layout of the type, i.e. it assumes
8482    that the substitution is purely formal.  That's why the replacement
8483    value R must itself contain a PLACEHOLDER_EXPR.  */
8484 
8485 tree
substitute_in_type(tree t,tree f,tree r)8486 substitute_in_type (tree t, tree f, tree r)
8487 {
8488   tree nt;
8489 
8490   gcc_assert (CONTAINS_PLACEHOLDER_P (r));
8491 
8492   switch (TREE_CODE (t))
8493     {
8494     case INTEGER_TYPE:
8495     case ENUMERAL_TYPE:
8496     case BOOLEAN_TYPE:
8497     case REAL_TYPE:
8498 
8499       /* First the domain types of arrays.  */
8500       if (CONTAINS_PLACEHOLDER_P (TYPE_GCC_MIN_VALUE (t))
8501 	  || CONTAINS_PLACEHOLDER_P (TYPE_GCC_MAX_VALUE (t)))
8502 	{
8503 	  tree low = SUBSTITUTE_IN_EXPR (TYPE_GCC_MIN_VALUE (t), f, r);
8504 	  tree high = SUBSTITUTE_IN_EXPR (TYPE_GCC_MAX_VALUE (t), f, r);
8505 
8506 	  if (low == TYPE_GCC_MIN_VALUE (t) && high == TYPE_GCC_MAX_VALUE (t))
8507 	    return t;
8508 
8509 	  nt = copy_type (t);
8510 	  TYPE_GCC_MIN_VALUE (nt) = low;
8511 	  TYPE_GCC_MAX_VALUE (nt) = high;
8512 
8513 	  if (TREE_CODE (t) == INTEGER_TYPE && TYPE_INDEX_TYPE (t))
8514 	    SET_TYPE_INDEX_TYPE
8515 	      (nt, substitute_in_type (TYPE_INDEX_TYPE (t), f, r));
8516 
8517 	  return nt;
8518 	}
8519 
8520       /* Then the subtypes.  */
8521       if (CONTAINS_PLACEHOLDER_P (TYPE_RM_MIN_VALUE (t))
8522 	  || CONTAINS_PLACEHOLDER_P (TYPE_RM_MAX_VALUE (t)))
8523 	{
8524 	  tree low = SUBSTITUTE_IN_EXPR (TYPE_RM_MIN_VALUE (t), f, r);
8525 	  tree high = SUBSTITUTE_IN_EXPR (TYPE_RM_MAX_VALUE (t), f, r);
8526 
8527 	  if (low == TYPE_RM_MIN_VALUE (t) && high == TYPE_RM_MAX_VALUE (t))
8528 	    return t;
8529 
8530 	  nt = copy_type (t);
8531 	  SET_TYPE_RM_MIN_VALUE (nt, low);
8532 	  SET_TYPE_RM_MAX_VALUE (nt, high);
8533 
8534 	  return nt;
8535 	}
8536 
8537       return t;
8538 
8539     case COMPLEX_TYPE:
8540       nt = substitute_in_type (TREE_TYPE (t), f, r);
8541       if (nt == TREE_TYPE (t))
8542 	return t;
8543 
8544       return build_complex_type (nt);
8545 
8546     case FUNCTION_TYPE:
8547       /* These should never show up here.  */
8548       gcc_unreachable ();
8549 
8550     case ARRAY_TYPE:
8551       {
8552 	tree component = substitute_in_type (TREE_TYPE (t), f, r);
8553 	tree domain = substitute_in_type (TYPE_DOMAIN (t), f, r);
8554 
8555 	if (component == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
8556 	  return t;
8557 
8558 	nt = build_nonshared_array_type (component, domain);
8559 	TYPE_ALIGN (nt) = TYPE_ALIGN (t);
8560 	TYPE_USER_ALIGN (nt) = TYPE_USER_ALIGN (t);
8561 	SET_TYPE_MODE (nt, TYPE_MODE (t));
8562 	TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8563 	TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8564 	TYPE_NONALIASED_COMPONENT (nt) = TYPE_NONALIASED_COMPONENT (t);
8565 	TYPE_MULTI_ARRAY_P (nt) = TYPE_MULTI_ARRAY_P (t);
8566 	TYPE_CONVENTION_FORTRAN_P (nt) = TYPE_CONVENTION_FORTRAN_P (t);
8567 	return nt;
8568       }
8569 
8570     case RECORD_TYPE:
8571     case UNION_TYPE:
8572     case QUAL_UNION_TYPE:
8573       {
8574 	bool changed_field = false;
8575 	tree field;
8576 
8577 	/* Start out with no fields, make new fields, and chain them
8578 	   in.  If we haven't actually changed the type of any field,
8579 	   discard everything we've done and return the old type.  */
8580 	nt = copy_type (t);
8581 	TYPE_FIELDS (nt) = NULL_TREE;
8582 
8583 	for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
8584 	  {
8585 	    tree new_field = copy_node (field), new_n;
8586 
8587 	    new_n = substitute_in_type (TREE_TYPE (field), f, r);
8588 	    if (new_n != TREE_TYPE (field))
8589 	      {
8590 		TREE_TYPE (new_field) = new_n;
8591 		changed_field = true;
8592 	      }
8593 
8594 	    new_n = SUBSTITUTE_IN_EXPR (DECL_FIELD_OFFSET (field), f, r);
8595 	    if (new_n != DECL_FIELD_OFFSET (field))
8596 	      {
8597 		DECL_FIELD_OFFSET (new_field) = new_n;
8598 		changed_field = true;
8599 	      }
8600 
8601 	    /* Do the substitution inside the qualifier, if any.  */
8602 	    if (TREE_CODE (t) == QUAL_UNION_TYPE)
8603 	      {
8604 		new_n = SUBSTITUTE_IN_EXPR (DECL_QUALIFIER (field), f, r);
8605 		if (new_n != DECL_QUALIFIER (field))
8606 		  {
8607 		    DECL_QUALIFIER (new_field) = new_n;
8608 		    changed_field = true;
8609 		  }
8610 	      }
8611 
8612 	    DECL_CONTEXT (new_field) = nt;
8613 	    SET_DECL_ORIGINAL_FIELD_TO_FIELD (new_field, field);
8614 
8615 	    DECL_CHAIN (new_field) = TYPE_FIELDS (nt);
8616 	    TYPE_FIELDS (nt) = new_field;
8617 	  }
8618 
8619 	if (!changed_field)
8620 	  return t;
8621 
8622 	TYPE_FIELDS (nt) = nreverse (TYPE_FIELDS (nt));
8623 	TYPE_SIZE (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE (t), f, r);
8624 	TYPE_SIZE_UNIT (nt) = SUBSTITUTE_IN_EXPR (TYPE_SIZE_UNIT (t), f, r);
8625 	SET_TYPE_ADA_SIZE (nt, SUBSTITUTE_IN_EXPR (TYPE_ADA_SIZE (t), f, r));
8626 	return nt;
8627       }
8628 
8629     default:
8630       return t;
8631     }
8632 }
8633 
8634 /* Return the RM size of GNU_TYPE.  This is the actual number of bits
8635    needed to represent the object.  */
8636 
8637 tree
rm_size(tree gnu_type)8638 rm_size (tree gnu_type)
8639 {
8640   /* For integral types, we store the RM size explicitly.  */
8641   if (INTEGRAL_TYPE_P (gnu_type) && TYPE_RM_SIZE (gnu_type))
8642     return TYPE_RM_SIZE (gnu_type);
8643 
8644   /* Return the RM size of the actual data plus the size of the template.  */
8645   if (TREE_CODE (gnu_type) == RECORD_TYPE
8646       && TYPE_CONTAINS_TEMPLATE_P (gnu_type))
8647     return
8648       size_binop (PLUS_EXPR,
8649 		  rm_size (TREE_TYPE (DECL_CHAIN (TYPE_FIELDS (gnu_type)))),
8650 		  DECL_SIZE (TYPE_FIELDS (gnu_type)));
8651 
8652   /* For record or union types, we store the size explicitly.  */
8653   if (RECORD_OR_UNION_TYPE_P (gnu_type)
8654       && !TYPE_FAT_POINTER_P (gnu_type)
8655       && TYPE_ADA_SIZE (gnu_type))
8656     return TYPE_ADA_SIZE (gnu_type);
8657 
8658   /* For other types, this is just the size.  */
8659   return TYPE_SIZE (gnu_type);
8660 }
8661 
8662 /* Return the name to be used for GNAT_ENTITY.  If a type, create a
8663    fully-qualified name, possibly with type information encoding.
8664    Otherwise, return the name.  */
8665 
8666 tree
get_entity_name(Entity_Id gnat_entity)8667 get_entity_name (Entity_Id gnat_entity)
8668 {
8669   Get_Encoded_Name (gnat_entity);
8670   return get_identifier_with_length (Name_Buffer, Name_Len);
8671 }
8672 
8673 /* Return an identifier representing the external name to be used for
8674    GNAT_ENTITY.  If SUFFIX is specified, the name is followed by "___"
8675    and the specified suffix.  */
8676 
8677 tree
create_concat_name(Entity_Id gnat_entity,const char * suffix)8678 create_concat_name (Entity_Id gnat_entity, const char *suffix)
8679 {
8680   Entity_Kind kind = Ekind (gnat_entity);
8681 
8682   if (suffix)
8683     {
8684       String_Template temp = {1, (int) strlen (suffix)};
8685       Fat_Pointer fp = {suffix, &temp};
8686       Get_External_Name_With_Suffix (gnat_entity, fp);
8687     }
8688   else
8689     Get_External_Name (gnat_entity, 0);
8690 
8691   /* A variable using the Stdcall convention lives in a DLL.  We adjust
8692      its name to use the jump table, the _imp__NAME contains the address
8693      for the NAME variable.  */
8694   if ((kind == E_Variable || kind == E_Constant)
8695       && Has_Stdcall_Convention (gnat_entity))
8696     {
8697       const int len = 6 + Name_Len;
8698       char *new_name = (char *) alloca (len + 1);
8699       strcpy (new_name, "_imp__");
8700       strcat (new_name, Name_Buffer);
8701       return get_identifier_with_length (new_name, len);
8702     }
8703 
8704   return get_identifier_with_length (Name_Buffer, Name_Len);
8705 }
8706 
8707 /* Given GNU_NAME, an IDENTIFIER_NODE containing a name and SUFFIX, a
8708    string, return a new IDENTIFIER_NODE that is the concatenation of
8709    the name followed by "___" and the specified suffix.  */
8710 
8711 tree
concat_name(tree gnu_name,const char * suffix)8712 concat_name (tree gnu_name, const char *suffix)
8713 {
8714   const int len = IDENTIFIER_LENGTH (gnu_name) + 3 + strlen (suffix);
8715   char *new_name = (char *) alloca (len + 1);
8716   strcpy (new_name, IDENTIFIER_POINTER (gnu_name));
8717   strcat (new_name, "___");
8718   strcat (new_name, suffix);
8719   return get_identifier_with_length (new_name, len);
8720 }
8721 
8722 #include "gt-ada-decl.h"
8723