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