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