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