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