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