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