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