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