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