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