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