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