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