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