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