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